[Bf-blender-cvs] [dba675fb65a] master: Cleanup: split normal calculation into it's own file

Campbell Barton noreply at git.blender.org
Thu Jul 1 03:08:03 CEST 2021


Commit: dba675fb65a7f8c1fa34ba48d9ce2f10ec8e0189
Author: Campbell Barton
Date:   Thu Jul 1 10:08:47 2021 +1000
Branches: master
https://developer.blender.org/rBdba675fb65a7f8c1fa34ba48d9ce2f10ec8e0189

Cleanup: split normal calculation into it's own file

Normals now includes many functions including normal splitting &
custom normal manipulation split this into it's own file
to centralize related functions.

===================================================================

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/mesh_evaluate.c
A	source/blender/blenkernel/intern/mesh_normals.c
M	source/blender/bmesh/intern/bmesh_mesh_normals.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index 6d74888b810..8d76a025e87 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -299,7 +299,7 @@ void BKE_mesh_recalc_looptri_with_normals(const struct MLoop *mloop,
                                           struct MLoopTri *mlooptri,
                                           const float (*poly_normals)[3]);
 
-/* *** mesh_evaluate.c *** */
+/* *** mesh_normals.c *** */
 
 void BKE_mesh_calc_normals_mapping_simple(struct Mesh *me);
 void BKE_mesh_calc_normals_mapping(struct MVert *mverts,
@@ -494,6 +494,8 @@ void BKE_mesh_calc_normals_split_ex(struct Mesh *mesh,
 void BKE_mesh_set_custom_normals(struct Mesh *mesh, float (*r_custom_loopnors)[3]);
 void BKE_mesh_set_custom_normals_from_vertices(struct Mesh *mesh, float (*r_custom_vertnors)[3]);
 
+/* *** mesh_evaluate.c *** */
+
 void BKE_mesh_calc_poly_normal(const struct MPoly *mpoly,
                                const struct MLoop *loopstart,
                                const struct MVert *mvarray,
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 20663f0a790..b66cb9e224d 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -190,6 +190,7 @@ set(SRC
   intern/mesh_mapping.c
   intern/mesh_merge.c
   intern/mesh_mirror.c
+  intern/mesh_normals.c
   intern/mesh_remap.c
   intern/mesh_remesh_voxel.c
   intern/mesh_runtime.c
diff --git a/source/blender/blenkernel/intern/mesh_evaluate.c b/source/blender/blenkernel/intern/mesh_evaluate.c
index 961c10ea5d3..6eac96ba85b 100644
--- a/source/blender/blenkernel/intern/mesh_evaluate.c
+++ b/source/blender/blenkernel/intern/mesh_evaluate.c
@@ -25,8 +25,6 @@
 
 #include <limits.h>
 
-#include "CLG_log.h"
-
 #include "MEM_guardedalloc.h"
 
 #include "DNA_mesh_types.h"
@@ -36,2118 +34,14 @@
 #include "BLI_alloca.h"
 #include "BLI_bitmap.h"
 #include "BLI_edgehash.h"
-#include "BLI_linklist.h"
-#include "BLI_linklist_stack.h"
+
 #include "BLI_math.h"
-#include "BLI_memarena.h"
-#include "BLI_stack.h"
-#include "BLI_task.h"
 #include "BLI_utildefines.h"
 
 #include "BKE_customdata.h"
-#include "BKE_editmesh_cache.h"
-#include "BKE_global.h"
+
 #include "BKE_mesh.h"
 #include "BKE_multires.h"
-#include "BKE_report.h"
-
-#include "BLI_strict_flags.h"
-
-#include "atomic_ops.h"
-#include "mikktspace.h"
-
-// #define DEBUG_TIME
-
-#include "PIL_time.h"
-#ifdef DEBUG_TIME
-#  include "PIL_time_utildefines.h"
-#endif
-
-static CLG_LogRef LOG = {"bke.mesh_evaluate"};
-
-/* -------------------------------------------------------------------- */
-/** \name Mesh Normal Calculation
- * \{ */
-
-/**
- * Call when there are no polygons.
- */
-static void mesh_calc_normals_vert_fallback(MVert *mverts, int numVerts)
-{
-  for (int i = 0; i < numVerts; i++) {
-    MVert *mv = &mverts[i];
-    float no[3];
-
-    normalize_v3_v3(no, mv->co);
-    normal_float_to_short_v3(mv->no, no);
-  }
-}
-
-/* TODO(Sybren): we can probably rename this to BKE_mesh_calc_normals_mapping(),
- * and remove the function of the same name below, as that one doesn't seem to be
- * called anywhere. */
-void BKE_mesh_calc_normals_mapping_simple(struct Mesh *mesh)
-{
-  const bool only_face_normals = CustomData_is_referenced_layer(&mesh->vdata, CD_MVERT);
-
-  BKE_mesh_calc_normals_mapping_ex(mesh->mvert,
-                                   mesh->totvert,
-                                   mesh->mloop,
-                                   mesh->mpoly,
-                                   mesh->totloop,
-                                   mesh->totpoly,
-                                   NULL,
-                                   mesh->mface,
-                                   mesh->totface,
-                                   NULL,
-                                   NULL,
-                                   only_face_normals);
-}
-
-/* Calculate vertex and face normals, face normals are returned in *r_faceNors if non-NULL
- * and vertex normals are stored in actual mverts.
- */
-void BKE_mesh_calc_normals_mapping(MVert *mverts,
-                                   int numVerts,
-                                   const MLoop *mloop,
-                                   const MPoly *mpolys,
-                                   int numLoops,
-                                   int numPolys,
-                                   float (*r_polyNors)[3],
-                                   const MFace *mfaces,
-                                   int numFaces,
-                                   const int *origIndexFace,
-                                   float (*r_faceNors)[3])
-{
-  BKE_mesh_calc_normals_mapping_ex(mverts,
-                                   numVerts,
-                                   mloop,
-                                   mpolys,
-                                   numLoops,
-                                   numPolys,
-                                   r_polyNors,
-                                   mfaces,
-                                   numFaces,
-                                   origIndexFace,
-                                   r_faceNors,
-                                   false);
-}
-/* extended version of 'BKE_mesh_calc_normals_poly' with option not to calc vertex normals */
-void BKE_mesh_calc_normals_mapping_ex(MVert *mverts,
-                                      int numVerts,
-                                      const MLoop *mloop,
-                                      const MPoly *mpolys,
-                                      int numLoops,
-                                      int numPolys,
-                                      float (*r_polyNors)[3],
-                                      const MFace *mfaces,
-                                      int numFaces,
-                                      const int *origIndexFace,
-                                      float (*r_faceNors)[3],
-                                      const bool only_face_normals)
-{
-  float(*pnors)[3] = r_polyNors, (*fnors)[3] = r_faceNors;
-
-  if (numPolys == 0) {
-    if (only_face_normals == false) {
-      mesh_calc_normals_vert_fallback(mverts, numVerts);
-    }
-    return;
-  }
-
-  /* if we are not calculating verts and no verts were passes then we have nothing to do */
-  if ((only_face_normals == true) && (r_polyNors == NULL) && (r_faceNors == NULL)) {
-    CLOG_WARN(&LOG, "called with nothing to do");
-    return;
-  }
-
-  if (!pnors) {
-    pnors = MEM_calloc_arrayN((size_t)numPolys, sizeof(float[3]), __func__);
-  }
-  /* NO NEED TO ALLOC YET */
-  /* if (!fnors) fnors = MEM_calloc_arrayN(numFaces, sizeof(float[3]), "face nors mesh.c"); */
-
-  if (only_face_normals == false) {
-    /* vertex normals are optional, they require some extra calculations,
-     * so make them optional */
-    BKE_mesh_calc_normals_poly(
-        mverts, NULL, numVerts, mloop, mpolys, numLoops, numPolys, pnors, false);
-  }
-  else {
-    /* only calc poly normals */
-    const MPoly *mp = mpolys;
-    for (int i = 0; i < numPolys; i++, mp++) {
-      BKE_mesh_calc_poly_normal(mp, mloop + mp->loopstart, mverts, pnors[i]);
-    }
-  }
-
-  if (origIndexFace &&
-      /* fnors == r_faceNors */ /* NO NEED TO ALLOC YET */
-          fnors != NULL &&
-      numFaces) {
-    const MFace *mf = mfaces;
-    for (int i = 0; i < numFaces; i++, mf++, origIndexFace++) {
-      if (*origIndexFace < numPolys) {
-        copy_v3_v3(fnors[i], pnors[*origIndexFace]);
-      }
-      else {
-        /* eek, we're not corresponding to polys */
-        CLOG_ERROR(&LOG, "tessellation face indices are incorrect.  normals may look bad.");
-      }
-    }
-  }
-
-  if (pnors != r_polyNors) {
-    MEM_freeN(pnors);
-  }
-  /* if (fnors != r_faceNors) MEM_freeN(fnors); */ /* NO NEED TO ALLOC YET */
-
-  fnors = pnors = NULL;
-}
-
-typedef struct MeshCalcNormalsData {
-  const MPoly *mpolys;
-  const MLoop *mloop;
-  MVert *mverts;
-  float (*pnors)[3];
-  float (*lnors_weighted)[3];
-  float (*vnors)[3];
-} MeshCalcNormalsData;
-
-static void mesh_calc_normals_poly_cb(void *__restrict userdata,
-                                      const int pidx,
-                                      const TaskParallelTLS *__restrict UNUSED(tls))
-{
-  MeshCalcNormalsData *data = userdata;
-  const MPoly *mp = &data->mpolys[pidx];
-
-  BKE_mesh_calc_poly_normal(mp, data->mloop + mp->loopstart, data->mverts, data->pnors[pidx]);
-}
-
-static void mesh_calc_normals_poly_prepare_cb(void *__restrict userdata,
-                                              const int pidx,
-                                              const TaskParallelTLS *__restrict UNUSED(tls))
-{
-  MeshCalcNormalsData *data = userdata;
-  const MPoly *mp = &data->mpolys[pidx];
-  const MLoop *ml = &data->mloop[mp->loopstart];
-  const MVert *mverts = data->mverts;
-
-  float pnor_temp[3];
-  float *pnor = data->pnors ? data->pnors[pidx] : pnor_temp;
-  float(*lnors_weighted)[3] = data->lnors_weighted;
-
-  const int nverts = mp->totloop;
-  float(*edgevecbuf)[3] = BLI_array_alloca(edgevecbuf, (size_t)nverts);
-
-  /* Polygon Normal and edge-vector */
-  /* inline version of #BKE_mesh_calc_poly_normal, also does edge-vectors */
-  {
-    int i_prev = nverts - 1;
-    const float *v_prev = mverts[ml[i_prev].v].co;
-    const float *v_curr;
-
-    zero_v3(pnor);
-    /* Newell's Method */
-    for (int i = 0; i < nverts; i++) {
-      v_curr = mverts[ml[i].v].co;
-      add_newell_cross_v3_v3v3(pnor, v_prev, v_curr);
-
-      /* Unrelated to normalize, calculate edge-vector */
-      sub_v3_v3v3(edgevecbuf[i_prev], v_prev, v_curr);
-      normalize_v3(edgevecbuf[i_prev]);
-      i_prev = i;
-
-      v_prev = v_curr;
-    }
-    if (UNLIKELY(normalize_v3(pnor) == 0.0f)) {
-      pnor[2] = 1.0f; /* other axes set to 0.0 */
-    }
-  }
-
-  /* accumulate angle weighted face normal */
-  /* inline version of #accumulate_vertex_normals_poly_v3,
-   * split between this threaded callback and #mesh_calc_normals_poly_accum_cb. */
-  {
-    const float *prev_edge = edgevecbuf[nverts - 1];
-
-    for (int i = 0; i < nverts; i++) {

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list