<commit_before>#include "meshutils.h"
#include "geomtrans.h"
#include "sort.h"

int32 *ic;
int32 n_c;

#define SwapValues(a, b, work) do {\
  (work) = (a); (a) = (b); (b) = (work);\
} while (0)


/*!
  For 3D, 2D and 1D.

  @par Revision history:
  - 08.06.2006, c
  - 02.08.2006
*/
int32 orient_elements(int32 *flag, int32 flag_n_row,
                      Mesh *mesh, Indices *cells, int32 dcells,
                      int32 *v_roots, int32 v_roots_n_row,
                      int32 *v_vecs, int32 v_vecs_n_row, int32 v_vecs_n_col,
                      int32 *swap_from,
                      int32 swap_from_n_row, int32 swap_from_n_col,
                      int32 *swap_to,
                      int32 swap_to_n_row, int32 swap_to_n_col)
{
#define SWF(ir, is) (swap_from[swap_from_n_col*ir+is])
#define SWT(ir, is) (swap_to[swap_to_n_col*ir+is])
#define IR(ir) (cell_vertices->indices[v_roots[ir]])
#define IV(ir, iv) (cell_vertices->indices[v_vecs[v_vecs_n_col*ir+iv]])
#define CONN(ip) (cell_vertices->indices[ip])

  int32 D = mesh->topology->max_dim;
  float64 *coors = mesh->geometry->coors;
  Indices cell_vertices[1];
  MeshEntityIterator it0[1];
  MeshConnectivity *cD0 = 0; // D -> 0
  int32 ir, iel, ii, ip0, ip1, ip2, ip3, tmp, nc;
  float64 v0[3], v1[3], v2[3], v3[3], cross[3], dot[1];

  cD0 = mesh->topology->conn[IJ(D, D, 0)];

  nc = mesh->geometry->dim;
  if (nc == 3) { // 3D.
    for (mei_init_sub(it0, mesh, cells, dcells); mei_go(it0); mei_next(it0)) {
      iel = it0->entity->ii;
      flag[iel] = 0;

      me_get_incident2(it0->entity, cell_vertices, cD0);

      for (ir = 0; ir < v_roots_n_row; ir++) {
        ip0 = IR(ir);
        ip1 = IV(ir, 0);
        ip2 = IV(ir, 1);
        ip3 = IV(ir, 2);
        for (ii = 0; ii < 3; ii++) {
          v0[ii] = coors[nc*ip0+ii];
          v1[ii] = coors[nc*ip1+ii] - v0[ii];
          v2[ii] = coors[nc*ip2+ii] - v0[ii];
          v3[ii] = coors[nc*ip3+ii] - v0[ii];
        }
        gtr_cross_product(cross, v1, v2);
        gtr_dot_v3(dot, v3, cross, 3);
        /* output("%d %d -> %d %d %d %d %e\n", iel, ir, ip0, ip1, ip2, ip3, */
        /*        dot[0]); */
        if (dot[0] < CONST_MachEps) {
          flag[iel]++;
          for (ii = 0; ii < swap_from_n_col; ii++) {
            SwapValues(CONN(SWF(ir, ii)), CONN(SWT(ir, ii)), tmp);
            /* output("%d %d\n", SWF(ir, ii), SWT(ir, ii)); */
          }
        }
      }
    }
  } else if (nc == 2) { // 2D.
    for (mei_init_sub(it0, mesh, cells, dcells); mei_go(it0); mei_next(it0)) {
      iel = it0->entity->ii;
      flag[iel] = 0;

      me_get_incident2(it0->entity, cell_vertices, cD0);

      for (ir = 0; ir < v_roots_n_row; ir++) {
        ip0 = IR(ir);
        ip1 = IV(ir, 0);
        ip2 = IV(ir, 1);
        for (ii = 0; ii < 2; ii++) {
          v0[ii] = coors[nc*ip0+ii];
          v1[ii] = coors[nc*ip1+ii] - v0[ii];
          v2[ii] = coors[nc*ip2+ii] - v0[ii];
        }
        v1[2] = v2[2] = 0.0;
        gtr_cross_product(cross, v1, v2);
        if (cross[2] < CONST_MachEps) {
          flag[iel]++;
          for (ii = 0; ii < swap_from_n_col; ii++) {
            SwapValues(CONN(SWF(ir, ii)), CONN(SWT(ir, ii)), tmp);
          }
        }
      }
    }
  } else if (nc == 1) { // 1D
    for (mei_init_sub(it0, mesh, cells, dcells); mei_go(it0); mei_next(it0)) {
      iel = it0->entity->ii;
      flag[iel] = 0;

      me_get_incident2(it0->entity, cell_vertices, cD0);

      for (ir = 0; ir < v_roots_n_row; ir++) {
        ip0 = IR(ir);
        ip1 = IV(ir, 0);

        v0[0] = coors[ip0];
        v1[0] = coors[ip1] - v0[0];

        if (v1[0] < CONST_MachEps){
          flag[iel]++;
          SwapValues(CONN(SWF(ir, 0)), CONN(SWT(ir, 0)), tmp);
        }
      }
    }
  }

  return(RET_OK);

#undef IR
#undef IV
#undef CONN
#undef SWF
#undef SWT
}

int32 compare_i32(const void *a, const void *b)
{<commit_msg>fix orient_elements() for cells with very small volumes<commit_after>#include "meshutils.h"
#include "geomtrans.h"
#include "sort.h"

int32 *ic;
int32 n_c;

#define SwapValues(a, b, work) do {\
  (work) = (a); (a) = (b); (b) = (work);\
} while (0)


/*!
  For 3D, 2D and 1D.

  @par Revision history:
  - 08.06.2006, c
  - 02.08.2006
*/
int32 orient_elements(int32 *flag, int32 flag_n_row,
                      Mesh *mesh, Indices *cells, int32 dcells,
                      int32 *v_roots, int32 v_roots_n_row,
                      int32 *v_vecs, int32 v_vecs_n_row, int32 v_vecs_n_col,
                      int32 *swap_from,
                      int32 swap_from_n_row, int32 swap_from_n_col,
                      int32 *swap_to,
                      int32 swap_to_n_row, int32 swap_to_n_col)
{
#define SWF(ir, is) (swap_from[swap_from_n_col*ir+is])
#define SWT(ir, is) (swap_to[swap_to_n_col*ir+is])
#define IR(ir) (cell_vertices->indices[v_roots[ir]])
#define IV(ir, iv) (cell_vertices->indices[v_vecs[v_vecs_n_col*ir+iv]])
#define CONN(ip) (cell_vertices->indices[ip])

  int32 D = mesh->topology->max_dim;
  float64 *coors = mesh->geometry->coors;
  Indices cell_vertices[1];
  MeshEntityIterator it0[1];
  MeshConnectivity *cD0 = 0; // D -> 0
  int32 ir, iel, ii, ip0, ip1, ip2, ip3, tmp, nc;
  float64 v0[3], v1[3], v2[3], v3[3], cross[3], dot[1];

  cD0 = mesh->topology->conn[IJ(D, D, 0)];

  nc = mesh->geometry->dim;
  if (nc == 3) { // 3D.
    for (mei_init_sub(it0, mesh, cells, dcells); mei_go(it0); mei_next(it0)) {
      iel = it0->entity->ii;
      flag[iel] = 0;

      me_get_incident2(it0->entity, cell_vertices, cD0);

      for (ir = 0; ir < v_roots_n_row; ir++) {
        ip0 = IR(ir);
        ip1 = IV(ir, 0);
        ip2 = IV(ir, 1);
        ip3 = IV(ir, 2);
        for (ii = 0; ii < 3; ii++) {
          v0[ii] = coors[nc*ip0+ii];
          v1[ii] = coors[nc*ip1+ii] - v0[ii];
          v2[ii] = coors[nc*ip2+ii] - v0[ii];
          v3[ii] = coors[nc*ip3+ii] - v0[ii];
        }
        gtr_cross_product(cross, v1, v2);
        gtr_dot_v3(dot, v3, cross, 3);
        /* output("%d %d -> %d %d %d %d %e\n", iel, ir, ip0, ip1, ip2, ip3, */
        /*        dot[0]); */
        if (dot[0] < 0.0) {
          flag[iel]++;
          for (ii = 0; ii < swap_from_n_col; ii++) {
            SwapValues(CONN(SWF(ir, ii)), CONN(SWT(ir, ii)), tmp);
            /* output("%d %d\n", SWF(ir, ii), SWT(ir, ii)); */
          }
        }
      }
    }
  } else if (nc == 2) { // 2D.
    for (mei_init_sub(it0, mesh, cells, dcells); mei_go(it0); mei_next(it0)) {
      iel = it0->entity->ii;
      flag[iel] = 0;

      me_get_incident2(it0->entity, cell_vertices, cD0);

      for (ir = 0; ir < v_roots_n_row; ir++) {
        ip0 = IR(ir);
        ip1 = IV(ir, 0);
        ip2 = IV(ir, 1);
        for (ii = 0; ii < 2; ii++) {
          v0[ii] = coors[nc*ip0+ii];
          v1[ii] = coors[nc*ip1+ii] - v0[ii];
          v2[ii] = coors[nc*ip2+ii] - v0[ii];
        }
        v1[2] = v2[2] = 0.0;
        gtr_cross_product(cross, v1, v2);
        if (cross[2] < 0.0) {
          flag[iel]++;
          for (ii = 0; ii < swap_from_n_col; ii++) {
            SwapValues(CONN(SWF(ir, ii)), CONN(SWT(ir, ii)), tmp);
          }
        }
      }
    }
  } else if (nc == 1) { // 1D
    for (mei_init_sub(it0, mesh, cells, dcells); mei_go(it0); mei_next(it0)) {
      iel = it0->entity->ii;
      flag[iel] = 0;

      me_get_incident2(it0->entity, cell_vertices, cD0);

      for (ir = 0; ir < v_roots_n_row; ir++) {
        ip0 = IR(ir);
        ip1 = IV(ir, 0);

        v0[0] = coors[ip0];
        v1[0] = coors[ip1] - v0[0];

        if (v1[0] < 0.0){
          flag[iel]++;
          SwapValues(CONN(SWF(ir, 0)), CONN(SWT(ir, 0)), tmp);
        }
      }
    }
  }

  return(RET_OK);

#undef IR
#undef IV
#undef CONN
#undef SWF
#undef SWT
}

int32 compare_i32(const void *a, const void *b)
{
{'fraction_non_alphanumeric': 0.18122438524590165, 'fraction_numerical': 0.050204918032786885, 'mean_word_length': 2.102502979737783, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3507984', 'n_tokens_mistral': 3586, 'n_tokens_neox': 3358, 'n_words': 701}