[Bf-blender-cvs] [ef2bb80] temp_custom_loop_normals: Enhance lnor space handling (by taking into account the 'cone case').

Bastien Montagne noreply at git.blender.org
Wed Aug 6 21:36:35 CEST 2014


Commit: ef2bb808d68ed0f7344b79d95060530af9980024
Author: Bastien Montagne
Date:   Fri Jul 11 19:43:02 2014 +0200
Branches: temp_custom_loop_normals
https://developer.blender.org/rBef2bb808d68ed0f7344b79d95060530af9980024

Enhance lnor space handling (by taking into account the 'cone case').

In other words, we now have two plain angles, and custom normals are encoded as
factors for those two angles.

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

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/cdderivedmesh.c
M	source/blender/blenkernel/intern/customdata.c
M	source/blender/blenkernel/intern/editderivedmesh.c
M	source/blender/blenkernel/intern/mesh_evaluate.c
M	source/blender/bmesh/intern/bmesh_mesh.c
M	source/blender/makesrna/intern/rna_mesh.c

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

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index 78ce331..e83811e 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -182,14 +182,17 @@ void BKE_mesh_loop_tangents_ex(
 void BKE_mesh_loop_tangents(
         struct Mesh *mesh, const char *uvmap, float (*r_looptangents)[4], struct ReportList *reports);
 
+/* XXX This is ugly! */
 #include "BLI_linklist.h"
 #include "BLI_memarena.h"
+#include "BLI_stack.h"
 
 typedef struct MLoopNorSpace {
 	float vec_lnor[3];   /* Automatically computed loop normal. */
 	float vec_ref[3];    /* Reference vector, orthogonal to vec_lnor. */
 	float vec_ortho[3];  /* Third vector, orthogonal to vec_lnor and vec_ref. */
-	float angle;         /* Reference angle, around vec_lnor, in ]0, 2pi] range (0.0 marks that space as invalid). */
+	float ref_alpha;     /* Reference angle, around vec_ortho, in ]0, pi] range (0.0 marks that space as invalid). */
+	float ref_beta;      /* Reference angle, around vec_lnor, in ]0, 2pi] range (0.0 marks that space as invalid). */
 	LinkNode *loops;     /* All indices (uint_in_ptr) of loops using this lnor space (i.e. smooth fan of loops). */
 } MLoopNorSpace;
 typedef struct MLoopsNorSpaces {
@@ -199,8 +202,10 @@ typedef struct MLoopsNorSpaces {
 void BKE_init_loops_normal_spaces(MLoopsNorSpaces *lnors_spaces, const int numLoops);
 void BKE_free_loops_normal_spaces(MLoopsNorSpaces *lnors_spaces);
 MLoopNorSpace *BKE_lnor_space_create(MLoopsNorSpaces *lnors_spaces);
-void BKE_lnor_space_define(MLoopNorSpace *lnor_space, const float lnor[3], float vec_ref[3], float vec_other[3]);
-void BKE_lnor_space_add_loop(MLoopsNorSpaces *lnors_spaces, MLoopNorSpace *lnor_space, const int ml_index, const bool add_to_list);
+void BKE_lnor_space_define(MLoopNorSpace *lnor_space, const float lnor[3], float vec_ref[3], float vec_other[3],
+                           struct BLI_Stack *edge_vectors);
+void BKE_lnor_space_add_loop(MLoopsNorSpaces *lnors_spaces, MLoopNorSpace *lnor_space, const int ml_index,
+                             const bool add_to_list);
 void BKE_lnor_space_custom_data_to_normal(MLoopNorSpace *lnor_space, float r_custom_lnor[3], const float clnor_data[2]);
 void BKE_lnor_space_custom_normal_to_data(MLoopNorSpace *lnor_space, const float custom_lnor[3], float r_clnor_data[2]);
 
@@ -208,7 +213,7 @@ void BKE_mesh_normals_loop_split(
         struct MVert *mverts, const int numVerts, struct MEdge *medges, const int numEdges,
         struct MLoop *mloops, float (*r_loopnors)[3], const int numLoops,
         struct MPoly *mpolys, const float (*polynors)[3], const int numPolys, float split_angle,
-        MLoopsNorSpaces *r_lnor_spaces, const float (*clnors_data)[2], int *r_loop_to_poly);
+        MLoopsNorSpaces *r_lnors_spaces, const float (*clnors_data)[2], int *r_loop_to_poly);
 
 void BKE_mesh_normals_loop_custom_set(
         struct MVert *mverts, const int numVerts, struct MEdge *medges, const int numEdges,
diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c
index 160f0ca..2dede24 100644
--- a/source/blender/blenkernel/intern/cdderivedmesh.c
+++ b/source/blender/blenkernel/intern/cdderivedmesh.c
@@ -2549,16 +2549,16 @@ void CDDM_calc_loop_normals(DerivedMesh *dm, const float split_angle)
 
 		BKE_mesh_normals_loop_split(mverts, numVerts, medges, numEdges, mloops, lnors, numLoops,
 		                            mpolys, (const float (*)[3])pnors, numPolys, split_angle,
-		                            NULL /* &lnors_spaces */, clnor_data, NULL);
+		                            NULL /* &lnors_spaces */ , clnor_data, NULL);
 #if 0
 		for (i = 0; i < numLoops; i++) {
-			if (lnors_spaces.lspaces[i]->angle != 0.0f) {
+			if (lnors_spaces.lspaces[i]->ref_alpha != 0.0f) {
 				LinkNode *loops = lnors_spaces.lspaces[i]->loops;
 				printf("Loop %d uses lnor space %p:\n", i, lnors_spaces.lspaces[i]);
-				print_v3("\tfinal lnor:", lnors[i]);
-				print_v3("\tauto lnor:", lnors_spaces.lspaces[i]->vec_lnor);
-				print_v3("\tref_vec:", lnors_spaces.lspaces[i]->vec_ref);
-				printf("\tangle: %f\n\tloops: %p\n", lnors_spaces.lspaces[i]->angle, lnors_spaces.lspaces[i]->loops);
+				print_v3("\tfinal lnor", lnors[i]);
+				print_v3("\tauto lnor", lnors_spaces.lspaces[i]->vec_lnor);
+				print_v3("\tref_vec", lnors_spaces.lspaces[i]->vec_ref);
+				printf("\talpha: %f\n\tbeta: %f\n\tloops: %p\n", lnors_spaces.lspaces[i]->ref_alpha, lnors_spaces.lspaces[i]->ref_beta, lnors_spaces.lspaces[i]->loops);
 				printf("\t\t(shared with loops");
 				while(loops) {
 					printf(" %d", GET_INT_FROM_POINTER(loops->link));
diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c
index 35db134..633cae6 100644
--- a/source/blender/blenkernel/intern/customdata.c
+++ b/source/blender/blenkernel/intern/customdata.c
@@ -1078,19 +1078,7 @@ static void layerInterp_clnor(void **UNUSED(sources), const float *UNUSED(weight
 	 * would also imply we check all new custom normals are coherent (all loops of a same vert/same smooth fan
 	 * *must* have the same custom normal!!!).
 	 */
-	static float default_clnor[2] = {1.0f, 0.0f};
-	copy_v2_v2((float *)dest, default_clnor);
-}
-
-static void layerDefault_clnor(void *data, int count)
-{
-	static float default_clnor[2] = {1.0f, 0.0f};
-	float (*clnor)[2] = (float (*)[2])data;
-	int i;
-
-	for (i = 0; i < count; i++, clnor++) {
-		copy_v2_v2(*clnor, default_clnor);
-	}
+	zero_v2((float *)dest);
 }
 
 static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
@@ -1207,7 +1195,7 @@ static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
 	/* 40: CD_TESSLOOPNORMAL */
 	{sizeof(short[4][3]), "", 0, NULL, NULL, NULL, NULL, layerSwap_flnor, NULL},
 	/* 41: CD_CUSTOMLOOPNORMAL */
-	{sizeof(float[2]), "vec3f", 1, NULL, NULL, NULL, layerInterp_clnor, NULL, layerDefault_clnor},
+	{sizeof(float[2]), "vec3f", 1, NULL, NULL, NULL, layerInterp_clnor, NULL, NULL},
 };
 
 /* note, numbers are from trunk and need updating for bmesh */
diff --git a/source/blender/blenkernel/intern/editderivedmesh.c b/source/blender/blenkernel/intern/editderivedmesh.c
index 5d3df07..1b8120a 100644
--- a/source/blender/blenkernel/intern/editderivedmesh.c
+++ b/source/blender/blenkernel/intern/editderivedmesh.c
@@ -209,7 +209,9 @@ static void emDM_calcLoopNormals(DerivedMesh *dm, const float split_angle)
 				BMLoop *l_iter, *l_first;
 				l_iter = l_first = BM_FACE_FIRST_LOOP(efa);
 				do {
+					//const float dummy[2] = {0.5f, 0.5f};
 					copy_v2_v2(*clnor, CustomData_bmesh_get(ldata, l_iter->head.data, CD_CUSTOMLOOPNORMAL));
+					//copy_v2_v2(*clnor, dummy);
 					++clnor;
 				} while ((l_iter = l_iter->next) != l_first);
 			}
@@ -219,13 +221,14 @@ static void emDM_calcLoopNormals(DerivedMesh *dm, const float split_angle)
 		                          NULL /* &lnors_spaces */, (const float (*)[2])clnor_data);
 #if 0
 		for (i = 0; i < numLoops; i++) {
-			if (lnors_spaces.lspaces[i]->angle != 0.0f) {
+			//if (i != 1) continue;
+			if (lnors_spaces.lspaces[i]->ref_alpha != 0.0f) {
 				LinkNode *loops = lnors_spaces.lspaces[i]->loops;
 				printf("Loop %d uses lnor space %p:\n", i, lnors_spaces.lspaces[i]);
 				print_v3("\tfinal lnor:", loopNos[i]);
 				print_v3("\tauto lnor:", lnors_spaces.lspaces[i]->vec_lnor);
 				print_v3("\tref_vec:", lnors_spaces.lspaces[i]->vec_ref);
-				printf("\tangle: %f\n\tloops: %p\n", lnors_spaces.lspaces[i]->angle, lnors_spaces.lspaces[i]->loops);
+				printf("\talpha: %f\n\tbeta: %f\n\tloops: %p\n", lnors_spaces.lspaces[i]->ref_alpha, lnors_spaces.lspaces[i]->ref_beta, lnors_spaces.lspaces[i]->loops);
 				printf("\t\t(shared with loops");
 				while(loops) {
 					printf(" %d", GET_INT_FROM_POINTER(loops->link));
diff --git a/source/blender/blenkernel/intern/mesh_evaluate.c b/source/blender/blenkernel/intern/mesh_evaluate.c
index 830efe1..c86badc 100644
--- a/source/blender/blenkernel/intern/mesh_evaluate.c
+++ b/source/blender/blenkernel/intern/mesh_evaluate.c
@@ -338,51 +338,65 @@ MLoopNorSpace *BKE_lnor_space_create(MLoopsNorSpaces *lnors_spaces)
  * Beware, this modifies ref_vec and other_vec in place!
  * Might set *lnor_space to NULL in case no valid space can be generated.
  */
-void BKE_lnor_space_define(MLoopNorSpace *lnor_space, const float lnor[3], float vec_ref[3], float vec_other[3])
+void BKE_lnor_space_define(MLoopNorSpace *lnor_space, const float lnor[3],
+                           float vec_ref[3], float vec_other[3], BLI_Stack *edge_vectors)
 {
-	float tvec[3], dtp;
 	const float pi2 = (float)M_PI * 2.0f;
+	float tvec[3], dtp;
+	const float dtp_ref = dot_v3v3(vec_ref, lnor);
+	const float dtp_other = dot_v3v3(vec_other, lnor);
 
-	/* Project ref_vec on lnor's ortho plane. */
-	dtp = dot_v3v3(vec_ref, lnor);
-	if (fabsf(dtp) > (1.0f - 1e-8f)) {
-		/* If ref_vec is too much aligned with lnor, we can't build lnor space, tag it as invalid and abort. */
-		lnor_space->angle = 0.0f;
+	if (fabsf(dtp_ref) > (1.0f - 1e-8f) || fabsf(dtp_other) > (1.0f - 1e-8f)) {
+		/* If vec_ref or vec_other are too much aligned with lnor, we can't build lnor space,
+		 * tag it as invalid and abort.
+		 */
+		lnor_space->ref_alpha = lnor_space->ref_beta = 0.0f;
 		return;
 	}
-	mul_v3_v3fl(tvec, lnor, dtp);
-	sub_v3_v3(vec_ref, tvec);
-	normalize_v3_v3(lnor_space->vec_ref, vec_ref);
 
 	copy_v3_v3(lnor_space->vec_lnor, lnor);
 
+	/* Compute ref alpha, average angle of all available edge vectors to lnor. */
+	if (edge_vectors) {
+		float vec[3];
+		float alpha = 0.0f;
+		int nbr = 0;
+		while (!BLI_stack_is_empty(edge_vectors)) {
+			BLI_stack_pop(edge_vectors, vec);
+			alpha += saacosf(dot_v3v3(vec, lnor));
+			++nbr;
+		}
+		lnor_space->ref_alpha = alpha / (float)nbr;
+	}
+	else {
+		lnor_space->ref_alpha = (saacosf(dot_v3v3(vec_ref, lnor)) + saacosf(dot_v3v3(vec_other, lnor))) / 2.0f;
+	}
+
+	/* Project vec_ref on lnor's ortho plane. */
+	mul_v3_v3fl(tvec, lnor, dtp_ref);
+	sub_v3_v3(vec_ref, tvec);
+	normalize_v3_v3(lnor_space->vec_ref, vec_ref);
+
 	cross_v3_v3v3(tvec, lnor, lnor_space->vec_ref);
 	normalize_v3_v3(lnor_space->vec_ortho, tvec);
 
-	lnor_space->angle = pi2;
+	

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list