[Bf-blender-cvs] [1a8516163fa] master: Cleanup: Simplify handling of loop to poly map in normal calculation

Hans Goudey noreply at git.blender.org
Sat Nov 12 06:27:56 CET 2022


Commit: 1a8516163fa921e857b17ff152cc5264ab42b898
Author: Hans Goudey
Date:   Fri Nov 11 22:56:44 2022 -0600
Branches: master
https://developer.blender.org/rB1a8516163fa921e857b17ff152cc5264ab42b898

Cleanup: Simplify handling of loop to poly map in normal calculation

A Loop to poly map was passed as an optional output to the loop normal
calculation. That meant it was often recalculated more than necessary.
Instead, treat it as an optional argument. This also helps relieve
unnecessary responsibilities from the already-complicated loop normal
calculation code.

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

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/data_transfer.c
M	source/blender/blenkernel/intern/key.c
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/mesh_mirror.c
M	source/blender/blenkernel/intern/mesh_normals.cc
M	source/blender/blenkernel/intern/mesh_remap.c
M	source/blender/draw/intern/draw_cache_extract_mesh_render_data.cc
M	source/blender/modifiers/intern/MOD_normal_edit.cc
M	source/blender/modifiers/intern/MOD_weighted_normal.cc

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

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index bb0b4467bd2..b1488c93ba6 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -621,6 +621,8 @@ void BKE_lnor_space_custom_normal_to_data(MLoopNorSpace *lnor_space,
  * Compute split normals, i.e. vertex normals associated with each poly (hence 'loop normals').
  * Useful to materialize sharp edges (or non-smooth faces) without actually modifying the geometry
  * (splitting edges).
+ *
+ * \param loop_to_poly_map: Optional pre-created map from loops to their polygon.
  */
 void BKE_mesh_normals_loop_split(const struct MVert *mverts,
                                  const float (*vert_normals)[3],
@@ -635,9 +637,9 @@ void BKE_mesh_normals_loop_split(const struct MVert *mverts,
                                  int numPolys,
                                  bool use_split_normals,
                                  float split_angle,
+                                 const int *loop_to_poly_map,
                                  MLoopNorSpaceArray *r_lnors_spacearr,
-                                 short (*clnors_data)[2],
-                                 int *r_loop_to_poly);
+                                 short (*clnors_data)[2]);
 
 void BKE_mesh_normals_loop_custom_set(const struct MVert *mverts,
                                       const float (*vert_normals)[3],
diff --git a/source/blender/blenkernel/intern/data_transfer.c b/source/blender/blenkernel/intern/data_transfer.c
index e6afca11b40..7b81f74206d 100644
--- a/source/blender/blenkernel/intern/data_transfer.c
+++ b/source/blender/blenkernel/intern/data_transfer.c
@@ -300,8 +300,8 @@ static void data_transfer_dtdata_type_preprocess(Mesh *me_src,
                                   use_split_nors_dst,
                                   split_angle_dst,
                                   NULL,
-                                  custom_nors_dst,
-                                  NULL);
+                                  NULL,
+                                  custom_nors_dst);
     }
   }
 }
diff --git a/source/blender/blenkernel/intern/key.c b/source/blender/blenkernel/intern/key.c
index 2ba81c54872..53147c94f43 100644
--- a/source/blender/blenkernel/intern/key.c
+++ b/source/blender/blenkernel/intern/key.c
@@ -2296,8 +2296,8 @@ void BKE_keyblock_mesh_calc_normals(const KeyBlock *kb,
                                 (mesh->flag & ME_AUTOSMOOTH) != 0,
                                 mesh->smoothresh,
                                 NULL,
-                                clnors,
-                                NULL);
+                                NULL,
+                                clnors);
   }
 
   if (free_vert_normals) {
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index 2d613f24a0a..b8658139161 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -1829,9 +1829,9 @@ void BKE_mesh_calc_normals_split_ex(Mesh *mesh,
                               polys.size(),
                               use_split_normals,
                               split_angle,
+                              nullptr,
                               r_lnors_spacearr,
-                              clnors,
-                              nullptr);
+                              clnors);
 
   BKE_mesh_assert_normals_dirty_or_calculated(mesh);
 }
diff --git a/source/blender/blenkernel/intern/mesh_mirror.c b/source/blender/blenkernel/intern/mesh_mirror.c
index ce3fc5d99c8..9f00d8860b8 100644
--- a/source/blender/blenkernel/intern/mesh_mirror.c
+++ b/source/blender/blenkernel/intern/mesh_mirror.c
@@ -418,9 +418,9 @@ Mesh *BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(MirrorModifierData *mmd,
                                 totpoly,
                                 true,
                                 mesh->smoothresh,
+                                NULL,
                                 &lnors_spacearr,
-                                clnors,
-                                NULL);
+                                clnors);
 
     /* mirroring has to account for loops being reversed in polys in second half */
     MPoly *result_polys = BKE_mesh_polys_for_write(result);
diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc
index 510301b7192..404357bda8d 100644
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@ -33,6 +33,7 @@
 #include "BKE_editmesh_cache.h"
 #include "BKE_global.h"
 #include "BKE_mesh.h"
+#include "BKE_mesh_mapping.h"
 
 #include "atomic_ops.h"
 
@@ -816,7 +817,7 @@ struct LoopSplitTaskDataCommon {
   Span<MLoop> loops;
   Span<MPoly> polys;
   int (*edge_to_loops)[2];
-  MutableSpan<int> loop_to_poly;
+  Span<int> loop_to_poly;
   Span<float3> polynors;
   Span<float3> vert_normals;
 };
@@ -834,12 +835,12 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data,
   MutableSpan<MEdge> edges = data->edges;
   const Span<MPoly> polys = data->polys;
   const Span<MLoop> loops = data->loops;
+  const Span<int> loop_to_poly = data->loop_to_poly;
 
   MutableSpan<float3> loopnors = data->loopnors; /* NOTE: loopnors may be empty here. */
   const Span<float3> polynors = data->polynors;
 
   int(*edge_to_loops)[2] = data->edge_to_loops;
-  MutableSpan<int> loop_to_poly = data->loop_to_poly;
 
   BitVector sharp_edges;
   if (do_sharp_edges_tag) {
@@ -859,8 +860,6 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data,
     for (; ml_curr_index <= ml_last_index; ml_curr++, ml_curr_index++) {
       e2l = edge_to_loops[ml_curr->e];
 
-      loop_to_poly[ml_curr_index] = mp_index;
-
       /* Pre-populate all loop normals as if their verts were all-smooth,
        * this way we don't have to compute those later!
        */
@@ -935,6 +934,8 @@ void BKE_edges_sharp_from_angle_set(const MVert *mverts,
                                     const int numPolys,
                                     const float split_angle)
 {
+  using namespace blender;
+  using namespace blender::bke;
   if (split_angle >= float(M_PI)) {
     /* Nothing to do! */
     return;
@@ -945,7 +946,8 @@ void BKE_edges_sharp_from_angle_set(const MVert *mverts,
       size_t(numEdges), sizeof(*edge_to_loops), __func__);
 
   /* Simple mapping from a loop to its polygon index. */
-  int *loop_to_poly = (int *)MEM_malloc_arrayN(size_t(numLoops), sizeof(*loop_to_poly), __func__);
+  const Array<int> loop_to_poly = mesh_topology::build_loop_to_poly_map({mpolys, numPolys},
+                                                                        numLoops);
 
   LoopSplitTaskDataCommon common_data = {};
   common_data.verts = {mverts, numVerts};
@@ -953,13 +955,12 @@ void BKE_edges_sharp_from_angle_set(const MVert *mverts,
   common_data.polys = {mpolys, numPolys};
   common_data.loops = {mloops, numLoops};
   common_data.edge_to_loops = edge_to_loops;
-  common_data.loop_to_poly = {loop_to_poly, numLoops};
+  common_data.loop_to_poly = loop_to_poly;
   common_data.polynors = {reinterpret_cast<const float3 *>(polynors), numPolys};
 
   mesh_edges_sharp_tag(&common_data, true, split_angle, true);
 
   MEM_freeN(edge_to_loops);
-  MEM_freeN(loop_to_poly);
 }
 
 static void loop_manifold_fan_around_vert_next(const Span<MLoop> loops,
@@ -1564,10 +1565,12 @@ void BKE_mesh_normals_loop_split(const MVert *mverts,
                                  const int numPolys,
                                  const bool use_split_normals,
                                  const float split_angle,
+                                 const int *loop_to_poly_map,
                                  MLoopNorSpaceArray *r_lnors_spacearr,
-                                 short (*clnors_data)[2],
-                                 int *r_loop_to_poly)
+                                 short (*clnors_data)[2])
 {
+  using namespace blender;
+  using namespace blender::bke;
   /* For now this is not supported.
    * If we do not use split normals, we do not generate anything fancy! */
   BLI_assert(use_split_normals || !(r_lnors_spacearr));
@@ -1588,9 +1591,6 @@ void BKE_mesh_normals_loop_split(const MVert *mverts,
       const bool is_poly_flat = ((mp->flag & ME_SMOOTH) == 0);
 
       for (; ml_index < ml_index_end; ml_index++) {
-        if (r_loop_to_poly) {
-          r_loop_to_poly[ml_index] = mp_index;
-        }
         if (is_poly_flat) {
           copy_v3_v3(r_loopnors[ml_index], polynors[mp_index]);
         }
@@ -1620,9 +1620,15 @@ void BKE_mesh_normals_loop_split(const MVert *mverts,
       size_t(numEdges), sizeof(*edge_to_loops), __func__);
 
   /* Simple mapping from a loop to its polygon index. */
-  int *loop_to_poly = r_loop_to_poly ? r_loop_to_poly :
-                                       (int *)MEM_malloc_arrayN(
-                                           size_t(numLoops), sizeof(*loop_to_poly), __func__);
+  Span<int> loop_to_poly;
+  Array<int> local_loop_to_poly_map;
+  if (loop_to_poly_map) {
+    loop_to_poly = {loop_to_poly_map, numLoops};
+  }
+  else {
+    local_loop_to_poly_map = mesh_topology::build_loop_to_poly_map({mpolys, numPolys}, numLoops);
+    loop_to_poly = local_loop_to_poly_map;
+  }
 
   /* When using custom loop normals, disable the angle feature! */
   const bool check_angle = (split_angle < float(M_PI)) && (clnors_data == nullptr);
@@ -1651,7 +1657,7 @@ void BKE_mesh_normals_loop_split(const MVert *mverts,
   common_data.polys = {mpolys, numPolys};
   common_data.loops = {mloops, numLoops};
   common_data.edge_to_loops = edge_to_loops;
-  common_data.loop_to_poly = {loop_to_poly, numLoops};
+  common_data.loop_to_poly = loop_to_poly;
   common_data.polynors = {reinterpret_cast<const float3 *>(polynors), numPolys};
   common_data.vert_normals = {reinterpret_cast<const float3 *>(vert_normals), numVerts};
 
@@ -1673,9 +1679,6 @@ void BKE_mesh_normals_loop_split(const MVert *mverts,
   }
 
   MEM_freeN(edge_to_loops);
-  if (!r_loop_to_poly) {
-    MEM_freeN(loop_to_poly);
-  }
 
   if (r_lnors_spacearr) {
     if (r_lnors_spacearr == &_lnor

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list