Page MenuHome
Paste P2286

D11993 alternative (loop-weight as float), 501bca9f5b1265e6f336bc7c1878b9b50a70356a
ActivePublic

Authored by Campbell Barton (campbellbarton) on Jul 26 2021, 9:21 AM.
diff --git a/source/blender/blenkernel/intern/mesh_normals.cc b/source/blender/blenkernel/intern/mesh_normals.cc
index f496d6eada1..c9a78cd65e0 100644
--- a/source/blender/blenkernel/intern/mesh_normals.cc
+++ b/source/blender/blenkernel/intern/mesh_normals.cc
@@ -52,10 +52,10 @@
// #define DEBUG_TIME
-#ifdef DEBUG_TIME
-# include "PIL_time.h"
-# include "PIL_time_utildefines.h"
-#endif
+// #ifdef DEBUG_TIME
+#include "PIL_time.h"
+#include "PIL_time_utildefines.h"
+// #endif
static CLG_LogRef LOG = {"bke.mesh_normals"};
@@ -204,7 +204,7 @@ struct MeshCalcNormalsData {
const MLoop *mloop;
MVert *mverts;
float (*pnors)[3];
- float (*lnors_weighted)[3];
+ float *lnors_weights;
float (*vnors)[3];
};
@@ -227,9 +227,8 @@ static void mesh_calc_normals_poly_prepare_cb(void *__restrict userdata,
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;
+ float *pnor = data->pnors[pidx];
+ float *lnors_weights = &data->lnors_weights[mp->loopstart];
const int nverts = mp->totloop;
float(*edgevecbuf)[3] = (float(*)[3])BLI_array_alloca(edgevecbuf, (size_t)nverts);
@@ -266,15 +265,11 @@ static void mesh_calc_normals_poly_prepare_cb(void *__restrict userdata,
const float *prev_edge = edgevecbuf[nverts - 1];
for (int i = 0; i < nverts; i++) {
- const int lidx = mp->loopstart + i;
const float *cur_edge = edgevecbuf[i];
/* calculate angle between the two poly edges incident on
* this vertex */
- const float fac = saacos(-dot_v3v3(cur_edge, prev_edge));
-
- /* Store for later accumulation */
- mul_v3_v3fl(lnors_weighted[lidx], pnor, fac);
+ lnors_weights[i] = saacos(-dot_v3v3(cur_edge, prev_edge));
prev_edge = cur_edge;
}
@@ -308,6 +303,7 @@ void BKE_mesh_calc_normals_poly(MVert *mverts,
float (*r_polynors)[3],
const bool only_face_normals)
{
+ TIMEIT_START(BKE_mesh_calc_normals_poly);
float(*pnors)[3] = r_polynors;
TaskParallelSettings settings;
@@ -329,9 +325,10 @@ void BKE_mesh_calc_normals_poly(MVert *mverts,
}
float(*vnors)[3] = r_vertnors;
- float(*lnors_weighted)[3] = (float(*)[3])MEM_malloc_arrayN(
- (size_t)numLoops, sizeof(*lnors_weighted), __func__);
+ float *lnors_weights = (float *)MEM_malloc_arrayN(
+ (size_t)numLoops, sizeof(*lnors_weights), __func__);
bool free_vnors = false;
+ bool free_pnors = false;
/* first go through and calculate normals for all the polys */
if (vnors == nullptr) {
@@ -342,12 +339,17 @@ void BKE_mesh_calc_normals_poly(MVert *mverts,
memset(vnors, 0, sizeof(*vnors) * (size_t)numVerts);
}
+ if (pnors == nullptr) {
+ pnors = (float(*)[3])MEM_malloc_arrayN((size_t)numPolys, sizeof(*pnors), __func__);
+ free_pnors = true;
+ }
+
MeshCalcNormalsData data;
data.mpolys = mpolys;
data.mloop = mloop;
data.mverts = mverts;
data.pnors = pnors;
- data.lnors_weighted = lnors_weighted;
+ data.lnors_weights = lnors_weights;
data.vnors = vnors;
/* Compute poly normals, and prepare weighted loop normals. */
@@ -357,8 +359,13 @@ void BKE_mesh_calc_normals_poly(MVert *mverts,
/* Unfortunately, not possible to thread that
* (not in a reasonable, totally lock- and barrier-free fashion),
* since several loops will point to the same vertex... */
- for (int lidx = 0; lidx < numLoops; lidx++) {
- add_v3_v3(vnors[mloop[lidx].v], data.lnors_weighted[lidx]);
+ for (int pidx = 0; pidx < numPolys; pidx++) {
+ const float *no = pnors[pidx];
+ int lidx = mpolys[pidx].loopstart;
+ int loopterminate = lidx + mpolys[pidx].totloop;
+ for (; lidx < loopterminate; lidx++) {
+ madd_v3_v3fl(vnors[mloop[lidx].v], no, data.lnors_weights[lidx]);
+ }
}
/* Normalize and validate computed vertex normals. */
@@ -367,7 +374,12 @@ void BKE_mesh_calc_normals_poly(MVert *mverts,
if (free_vnors) {
MEM_freeN(vnors);
}
- MEM_freeN(lnors_weighted);
+ if (free_pnors) {
+ MEM_freeN(pnors);
+ }
+ MEM_freeN(lnors_weights);
+
+ TIMEIT_END(BKE_mesh_calc_normals_poly);
}
void BKE_mesh_ensure_normals(Mesh *mesh)