[Bf-blender-cvs] [3fee39220f2] refactor-mesh-corners-generic: Fix corner normal calculation
Hans Goudey
noreply at git.blender.org
Wed Dec 7 23:23:30 CET 2022
Commit: 3fee39220f250da2b760b1ed9c0e7232bafac608
Author: Hans Goudey
Date: Wed Dec 7 13:41:00 2022 -0600
Branches: refactor-mesh-corners-generic
https://developer.blender.org/rB3fee39220f250da2b760b1ed9c0e7232bafac608
Fix corner normal calculation
===================================================================
M source/blender/blenkernel/intern/mesh_normals.cc
===================================================================
diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc
index 7546bc67bfd..2b3981971dc 100644
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@ -331,8 +331,7 @@ static void calculate_normals_poly_and_vert(const Span<float3> positions,
float *no = vert_normals[vert_i];
if (UNLIKELY(normalize_v3(no) == 0.0f)) {
- /* Following Mesh convention; we use vertex coordinate itself for normal in this
- * case. */
+ /* Following Mesh convention; we use vertex coordinate itself for normal in this case. */
normalize_v3_v3(no, positions[vert_i]);
}
}
@@ -692,8 +691,7 @@ void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space,
const float custom_lnor[3],
short r_clnor_data[2])
{
- /* We use nullptr vector as NOP custom normal (can be simpler than giving auto-computed
- * `lnor`).
+ /* We use nullptr vector as NOP custom normal (can be simpler than giving auto-computed `lnor`).
*/
if (is_zero_v3(custom_lnor) || compare_v3v3(lnor_space->vec_lnor, custom_lnor, 1e-4f)) {
r_clnor_data[0] = r_clnor_data[1] = 0;
@@ -831,8 +829,7 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data,
/* Check whether current edge might be smooth or sharp */
if ((e2l[0] | e2l[1]) == 0) {
- /* 'Empty' edge until now, set e2l[0] (and e2l[1] to INDEX_UNSET to tag it as unset).
- */
+ /* 'Empty' edge until now, set e2l[0] (and e2l[1] to INDEX_UNSET to tag it as unset). */
e2l[0] = ml_curr_index;
/* We have to check this here too, else we might miss some flat faces!!! */
e2l[1] = (poly.flag & ME_SMOOTH) ? INDEX_UNSET : INDEX_INVALID;
@@ -844,9 +841,8 @@ static void mesh_edges_sharp_tag(LoopSplitTaskDataCommon *data,
/* Second loop using this edge, time to test its sharpness.
* An edge is sharp if it is tagged as such, or its face is not smooth,
- * or both poly have opposed (flipped) normals, i.e. both loops on the same edge share
- * the same vertex, or angle between both its polys' normals is above split_angle
- * value.
+ * or both poly have opposed (flipped) normals, i.e. both loops on the same edge share the
+ * same vertex, or angle between both its polys' normals is above split_angle value.
*/
if (!(poly.flag & ME_SMOOTH) || (edges[edge_i].flag & ME_SHARP) ||
vert_i == corner_verts[e2l[0]] || is_angle_sharp) {
@@ -893,7 +889,7 @@ void BKE_edges_sharp_from_angle_set(const float (*positions)[3],
const int numEdges,
const int *corner_verts,
const int *corner_edges,
- const int corners_num,
+ const int numLoops,
const MPoly *mpolys,
const float (*polynors)[3],
const int numPolys,
@@ -912,14 +908,14 @@ void BKE_edges_sharp_from_angle_set(const float (*positions)[3],
/* Simple mapping from a loop to its polygon index. */
const Array<int> loop_to_poly = mesh_topology::build_loop_to_poly_map({mpolys, numPolys},
- corners_num);
+ numLoops);
LoopSplitTaskDataCommon common_data = {};
common_data.positions = {reinterpret_cast<const float3 *>(positions), numVerts};
common_data.edges = {medges, numEdges};
common_data.polys = {mpolys, numPolys};
- common_data.corner_verts = {corner_verts, corners_num};
- common_data.corner_edges = {corner_edges, corners_num};
+ common_data.corner_verts = {corner_verts, numLoops};
+ common_data.corner_edges = {corner_edges, numLoops};
common_data.edge_to_loops = edge_to_loops;
common_data.loop_to_poly = loop_to_poly;
common_data.polynors = {reinterpret_cast<const float3 *>(polynors), numPolys};
@@ -938,11 +934,15 @@ static void loop_manifold_fan_around_vert_next(const Span<int> corner_verts,
int *r_mlfan_vert_index,
int *r_mpfan_curr_index)
{
+ const int fan_vert_curr = corner_verts[*r_mlfan_curr_index];
+
/* WARNING: This is rather complex!
* We have to find our next edge around the vertex (fan mode).
- * First we find the next loop, which is either previous or next to mlfan_curr_index,
- * depending whether both loops using current edge are in the same direction or not, and
- * whether mlfan_curr_index actually uses the vertex we are fanning around! */
+ * First we find the next loop, which is either previous or next to mlfan_curr_index, depending
+ * whether both loops using current edge are in the same direction or not, and whether
+ * mlfan_curr_index actually uses the vertex we are fanning around!
+ * mlfan_curr_index is the index of mlfan_next here, and mlfan_next is not the real next one
+ * (i.e. not the future `mlfan_curr`). */
*r_mlfan_curr_index = (e2lfan_curr[0] == *r_mlfan_curr_index) ? e2lfan_curr[1] : e2lfan_curr[0];
*r_mpfan_curr_index = loop_to_poly[*r_mlfan_curr_index];
@@ -950,7 +950,6 @@ static void loop_manifold_fan_around_vert_next(const Span<int> corner_verts,
BLI_assert(*r_mpfan_curr_index >= 0);
const int fan_vert_next = corner_verts[*r_mlfan_curr_index];
- const int fan_vert_curr = corner_verts[*r_mlfan_curr_index];
const MPoly &mpfan_next = polys[*r_mpfan_curr_index];
@@ -969,7 +968,6 @@ static void loop_manifold_fan_around_vert_next(const Span<int> corner_verts,
}
*r_mlfan_vert_index = *r_mlfan_curr_index;
}
- /* And now we are back in sync, mlfan_curr_index is the index of `mlfan_curr`! Pff! */
}
static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopSplitTaskData *data)
@@ -1010,8 +1008,8 @@ static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopS
if (lnors_spacearr) {
float vec_curr[3], vec_prev[3];
- const int mv_pivot_index =
- corner_verts[ml_curr_index]; /* The vertex we are "fanning" around! */
+ /* The vertex we are "fanning" around! */
+ const int mv_pivot_index = corner_verts[ml_curr_index];
const float3 &mv_pivot = positions[mv_pivot_index];
const MEdge *me_curr = &edges[corner_edges[ml_curr_index]];
const float3 &mv_2 = (me_curr->v1 == mv_pivot_index) ? positions[me_curr->v2] :
@@ -1026,8 +1024,7 @@ static void split_loop_nor_single_do(LoopSplitTaskDataCommon *common_data, LoopS
normalize_v3(vec_prev);
BKE_lnor_space_define(lnor_space, *lnor, vec_curr, vec_prev, nullptr);
- /* We know there is only one loop in this space, no need to create a link-list in this
- * case. */
+ /* We know there is only one loop in this space, no need to create a link-list in this case. */
BKE_lnor_space_add_loop(lnors_spacearr, lnor_space, ml_curr_index, nullptr, true);
if (!clnors_data.is_empty()) {
@@ -1064,10 +1061,10 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli
/* Sigh! we have to fan around current vertex, until we find the other non-smooth edge,
* and accumulate face normals into the vertex!
- * Note in case this vertex has only one sharp edges, this is a waste because the normal is
- * the same as the vertex normal, but I do not see any easy way to detect that (would need to
- * count number of sharp edges per vertex, I doubt the additional memory usage would be worth
- * it, especially as it should not be a common case in real-life meshes anyway). */
+ * Note in case this vertex has only one sharp edges, this is a waste because the normal is the
+ * same as the vertex normal, but I do not see any easy way to detect that (would need to count
+ * number of sharp edges per vertex, I doubt the additional memory usage would be worth it,
+ * especially as it should not be a common case in real-life meshes anyway). */
const int mv_pivot_index = corner_verts[ml_curr_index]; /* The vertex we are "fanning" around! */
const float3 &mv_pivot = positions[mv_pivot_index];
@@ -1089,9 +1086,7 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli
BLI_SMALLSTACK_DECLARE(clnors, short *);
const int *e2lfan_curr = e2l_prev;
- const int mlfan_curr = ml_prev_index;
- /* `mlfan_vert_index` the loop of our current edge might not be the loop of our current
- * vertex!
+ /* `mlfan_vert_index` the loop of our current edge might not be the loop of our current vertex!
*/
int mlfan_curr_index = ml_prev_index;
int mlfan_vert_index = ml_curr_index;
@@ -1101,8 +1096,7 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli
BLI_assert(mlfan_vert_index >= 0);
BLI_assert(mpfan_curr_index >= 0);
- /* Only need to compute previous edge's vector once, then we can just reuse old current one!
- */
+ /* Only need to compute previous edge's vector once, then we can just reuse old current one! */
{
const float3 &mv_2 = (me_org->v1 == mv_pivot_index) ? positions[me_org->v2] :
positions[me_org->v1];
@@ -1119,11 +1113,11 @@ static void split_loop_nor_fan_do(LoopSplitTaskDataCommon *common_data, LoopSpli
// printf("FAN: vert %d, start edge %d\n", mv_pivot_index, ml_curr->e);
while (true) {
- const MEdge *me_curr = &edges[corner_edges[mlfan_curr]];
+ const MEdge *me_curr = &edges[corner_edges[mlfan_curr_index]];
/* Compute edge vectors.
- * NOTE: We could pre-compute those into an array, in the first iteration, instead of
- * computing them twice (or more) here. However, time gained is not worth memory and time
- * lost, given the fact that this code should not be called that much in real-life meshes.
+ *
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list