[Bf-blender-cvs] [93ef006e3c2] soc-2017-normal-tools: More cleanup and optimizations.

Bastien Montagne noreply at git.blender.org
Fri Mar 9 18:50:42 CET 2018


Commit: 93ef006e3c2a0526550819d8bf4dcbe95016a350
Author: Bastien Montagne
Date:   Fri Mar 9 15:03:47 2018 +0100
Branches: soc-2017-normal-tools
https://developer.blender.org/rB93ef006e3c2a0526550819d8bf4dcbe95016a350

More cleanup and optimizations.

Style and naming, arg positons (all loops data together, etc.),
const values and args, and so on...

Also, avoid allocating unused memory, avoid extra loops when possible, etc.

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

M	source/blender/modifiers/intern/MOD_weighted_normal.c

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

diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.c b/source/blender/modifiers/intern/MOD_weighted_normal.c
index e0951fbaed5..478f3e43eff 100644
--- a/source/blender/modifiers/intern/MOD_weighted_normal.c
+++ b/source/blender/modifiers/intern/MOD_weighted_normal.c
@@ -46,6 +46,8 @@
 #define INDEX_INVALID -1
 #define IS_EDGE_SHARP(_e2l) (ELEM((_e2l)[1], INDEX_UNSET, INDEX_INVALID))
 
+#define CLNORS_VALID_VEC_LEN (1e-4f)
+
 typedef struct ModePair {
 	float val;  /* Contains mode based value (face area / corner angle). */
 	int index;  /* Index value per poly or per loop. */
@@ -209,120 +211,134 @@ static void loop_split_worker(
 }
 
 static void apply_weights_vertex_normal(
-        WeightedNormalModifierData *wnmd, Object *UNUSED(ob), DerivedMesh *UNUSED(dm), short(*clnors)[2],
-        MVert *mvert, const int numVerts, MEdge *medge, const int numEdges, MLoop *mloop, const int numLoops,
-        MPoly *mpoly, const int numPoly, float(*polynors)[3], MDeformVert *dvert, int defgrp_index,
-        const bool use_invert_vgroup, const float weight, short mode, ModePair *mode_pair, int *strength)
+        WeightedNormalModifierData *wnmd,
+        MVert *mvert, const int numVerts,
+        MEdge *medge, const int numEdges,
+        MLoop *mloop, const int numLoops, short (*clnors)[2], int *loop_to_poly,
+        MPoly *mpoly, const int numPoly, float (*polynors)[3], int *poly_strength,
+        MDeformVert *dvert, const int defgrp_index, const bool use_invert_vgroup,
+        const float weight, short mode, ModePair *mode_pair)
 {
-	float(*custom_normal)[3] = MEM_callocN(sizeof(*custom_normal) * numVerts, __func__);
-	int *vertcount = MEM_callocN(sizeof(*vertcount) * numVerts, __func__);  /* Count number of loops using this vertex so far. */
-	float *cur_val = MEM_callocN(sizeof(*cur_val) * numVerts, __func__);  /* Current max val for this vertex. */
-	int *cur_strength = MEM_mallocN(sizeof(*cur_strength) * numVerts, __func__); /* Current max strength encountered for this vertex. */
-	int *loops_to_poly = MEM_mallocN(sizeof(*loops_to_poly) * numLoops, __func__);
-
-	for (int mp_index = 0; mp_index < numPoly; mp_index++) {
-		int ml_index = mpoly[mp_index].loopstart;
-		const int ml_index_end = ml_index + mpoly[mp_index].totloop;
-
-		for (; ml_index < ml_index_end; ml_index++) {
-			loops_to_poly[ml_index] = mp_index;
-		}
-	}
-	for (int i = 0; i < numVerts; i++) {
-		cur_strength[i] = FACE_STRENGTH_WEAK;
-	}
+	float (*vert_normals)[3] = MEM_callocN(sizeof(*vert_normals) * numVerts, __func__);
+	int *vert_loops_count = MEM_callocN(sizeof(*vert_loops_count) * numVerts, __func__);  /* Count number of loops using this vertex so far. */
+	float *curr_vert_val = MEM_callocN(sizeof(*curr_vert_val) * numVerts, __func__);  /* Current max val for this vertex. */
+	int *curr_vert_strength = NULL;  /* Current max strength encountered for this vertex. */
 
 	const bool keep_sharp = (wnmd->flag & MOD_WEIGHTEDNORMAL_KEEP_SHARP) != 0;
-	const bool face_influence = (wnmd->flag & MOD_WEIGHTEDNORMAL_FACE_INFLUENCE) != 0;
+	const bool use_face_influence = (wnmd->flag & MOD_WEIGHTEDNORMAL_FACE_INFLUENCE) != 0 && poly_strength != NULL;
 	const bool has_vgroup = dvert != NULL;
 
+	if (use_face_influence) {
+		curr_vert_strength = MEM_mallocN(sizeof(*curr_vert_strength) * numVerts, __func__);
+		for (int i = 0; i < numVerts; i++) {
+			curr_vert_strength[i] = FACE_STRENGTH_WEAK;
+		}
+	}
+
 	if (mode == MOD_WEIGHTEDNORMAL_MODE_FACE) {
 		for (int i = 0; i < numPoly; i++) {  /* Iterate through each pair in descending order. */
-			int ml_index = mpoly[mode_pair[i].index].loopstart;
-			const int ml_index_end = ml_index + mpoly[mode_pair[i].index].totloop;
+			const int mp_index = mode_pair[i].index;
+			int ml_index = mpoly[mp_index].loopstart;
+			const int ml_index_end = ml_index + mpoly[mp_index].totloop;
 
 			for (; ml_index < ml_index_end; ml_index++) {
-				int mv_index = mloop[ml_index].v;
+				const int mv_index = mloop[ml_index].v;
 				const bool vert_of_group = has_vgroup && dvert[mv_index].dw != NULL && dvert[mv_index].dw->def_nr == defgrp_index;
 
 				if ((vert_of_group ^ use_invert_vgroup) || !dvert) {
+					if (use_face_influence &&
+					    !check_strength(poly_strength[mp_index], &curr_vert_strength[mv_index],
+					                    &curr_vert_val[mv_index], &vert_loops_count[mv_index], &vert_normals[mv_index]))
+					{
+						continue;
+					}
+
+					const float mp_val = mode_pair[i].val;
 					float wnor[3];
-					bool do_loop = true;
 
-					if (face_influence && strength) {
-						do_loop = check_strength(strength[mode_pair[i].index], &cur_strength[mv_index], 
-						                         &cur_val[mv_index], &vertcount[mv_index], &custom_normal[mv_index]);
+					if (curr_vert_val[mv_index] == 0.0f) {  /* If cur_val is 0 init it to present value. */
+						curr_vert_val[mv_index] = mp_val;
 					}
-					if (do_loop) {
-						if (!cur_val[mv_index]) {  /* If cur_val is 0 init it to present value. */
-							cur_val[mv_index] = mode_pair[i].val;
-						}
-						if (!compare_ff(cur_val[mv_index], mode_pair[i].val, wnmd->thresh)) {
-							vertcount[mv_index]++;  /* cur_val and present value differ more than threshold, update. */
-							cur_val[mv_index] = mode_pair[i].val;
-						}
-						float n_weight = pow(weight, vertcount[mv_index]);  /* Exponentially divided weight for each normal. */
-
-						copy_v3_v3(wnor, polynors[mode_pair[i].index]);
-						mul_v3_fl(wnor, mode_pair[i].val * (1.0f / n_weight));
-						add_v3_v3(custom_normal[mv_index], wnor);
+					if (!compare_ff(curr_vert_val[mv_index], mp_val, wnmd->thresh)) {
+						vert_loops_count[mv_index]++;  /* cur_val and present value differ more than threshold, update. */
+						curr_vert_val[mv_index] = mp_val;
 					}
+
+					/* Exponentially divided weight for each normal. */
+					const float invert_n_weight = 1.0f / powf(weight, vert_loops_count[mv_index]);
+
+					copy_v3_v3(wnor, polynors[mp_index]);
+					mul_v3_fl(wnor, mp_val * invert_n_weight);
+					add_v3_v3(vert_normals[mv_index], wnor);
 				}
 			}
 		}
 	}
 	else if (ELEM(mode, MOD_WEIGHTEDNORMAL_MODE_ANGLE, MOD_WEIGHTEDNORMAL_MODE_FACE_ANGLE)) {
+		BLI_assert(loop_to_poly != NULL);
+
 		for (int i = 0; i < numLoops; i++) {
 			float wnor[3];
-			int ml_index = mode_pair[i].index;
-			int mv_index = mloop[ml_index].v;
+			const int ml_index = mode_pair[i].index;
+			const int mv_index = mloop[ml_index].v;
 			const bool vert_of_group = has_vgroup && dvert[mv_index].dw != NULL && dvert[mv_index].dw->def_nr == defgrp_index;
 
 			if ((vert_of_group ^ use_invert_vgroup) || !dvert) {
-				bool do_loop = true;
-
-				if (face_influence && strength) {
-					do_loop = check_strength(strength[loops_to_poly[ml_index]], &cur_strength[mv_index],
-					                         &cur_val[mv_index], &vertcount[mv_index], &custom_normal[mv_index]);
+				if (use_face_influence &&
+				    !check_strength(poly_strength[loop_to_poly[ml_index]], &curr_vert_strength[mv_index],
+				                    &curr_vert_val[mv_index], &vert_loops_count[mv_index], &vert_normals[mv_index]))
+				{
+					continue;
 				}
-				if (do_loop) {
-					if (!cur_val[mv_index]) {
-						cur_val[mv_index] = mode_pair[i].val;
-					}
-					if (!compare_ff(cur_val[mv_index], mode_pair[i].val, wnmd->thresh)) {
-						vertcount[mv_index]++;
-						cur_val[mv_index] = mode_pair[i].val;
-					}
-					float n_weight = pow(weight, vertcount[mv_index]);
 
-					copy_v3_v3(wnor, polynors[loops_to_poly[ml_index]]);
-					mul_v3_fl(wnor, mode_pair[i].val * (1.0f / n_weight));
-					add_v3_v3(custom_normal[mv_index], wnor);
+				const float ml_val = mode_pair[i].val;
+				if (!curr_vert_val[mv_index]) {
+					curr_vert_val[mv_index] = ml_val;
+				}
+				if (!compare_ff(curr_vert_val[mv_index], ml_val, wnmd->thresh)) {
+					vert_loops_count[mv_index]++;
+					curr_vert_val[mv_index] = ml_val;
 				}
+				const float invert_n_weight = 1.0f / powf(weight, vert_loops_count[mv_index]);
+
+				copy_v3_v3(wnor, polynors[loop_to_poly[ml_index]]);
+				mul_v3_fl(wnor, ml_val * invert_n_weight);
+				add_v3_v3(vert_normals[mv_index], wnor);
 			}
 		}
 	}
 	for (int mv_index = 0; mv_index < numVerts; mv_index++) {
-		normalize_v3(custom_normal[mv_index]);
+		if (normalize_v3(vert_normals[mv_index]) < CLNORS_VALID_VEC_LEN) {
+			zero_v3(vert_normals[mv_index]);
+		}
 	}
 
 	if (!keep_sharp && !has_vgroup) {
-		BKE_mesh_normals_loop_custom_from_vertices_set(mvert, custom_normal, numVerts, medge, numEdges,
+		BKE_mesh_normals_loop_custom_from_vertices_set(mvert, vert_normals, numVerts, medge, numEdges,
 		                                               mloop, numLoops, mpoly, polynors, numPoly, clnors);
 	}
 	else {
-		float(*loop_normal)[3] = MEM_callocN(sizeof(*loop_normal) * numLoops, "__func__");
+		float (*loop_normal)[3] = MEM_callocN(sizeof(*loop_normal) * numLoops, "__func__");
+		int *loop_to_poly_mem = NULL;
+
+		/* We need loop to poly mapping at this stage, but conviniently BKE_mesh_normals_loop_split
+		 * will generate it for us if we don't have it yet. */
+		if (loop_to_poly == NULL) {
+			loop_to_poly_mem = MEM_malloc_arrayN((size_t)numLoops, sizeof(*loop_to_poly_mem), __func__);
+			loop_to_poly = loop_to_poly_mem;
+		}
 
 		BKE_mesh_normals_loop_split(mvert, numVerts, medge, numEdges, mloop, loop_normal, numLoops, mpoly, polynors,
-		                            numPoly, true, (float)M_PI, NULL, clnors, loops_to_poly);
+		                            numPoly, true, (float)M_PI, NULL, clnors, loop_to_poly);
 
 		for (int mp_index = 0; mp_index < numPoly; mp_index++) {
-			int ml_index = mpoly[mp_index].loopstart;
+			const int ml_index = mpoly[mp_index].loopstart;
 			const int ml_index_end = ml_index + mpoly[mp_index].totloop;
 
 			for (int i = ml_index; i < ml_index_end; i++) {
-				if (!is_zero_v3(custom_normal[mloop[i].v])) {
-					copy_v3_v3(loop_normal[i], custom_normal[mloop[i].v]);
+				const int mv_index = mloop[i].v;
+				if (!is_zero_v3(vert_normals[mv_index])) {
+					copy_v3_v3(loop_normal[i], vert_normals[mv_index]);
 				}
 			}
 		}
@@ -381,13 +397,13 @@ static void apply_weights_vertex_normal(
 					if (IS_EDGE_SHARP(e2l_curr)) {
 						

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list