[Bf-blender-cvs] [121c94b0829] blender2.8: Cleanup: mesh iterators

Campbell Barton noreply at git.blender.org
Thu Oct 11 01:35:12 CEST 2018


Commit: 121c94b0829e57503bf90167e6e903f2fb55e328
Author: Campbell Barton
Date:   Thu Oct 11 10:31:11 2018 +1100
Branches: blender2.8
https://developer.blender.org/rB121c94b0829e57503bf90167e6e903f2fb55e328

Cleanup: mesh iterators

- Split indexed/non-indexed into separate loops.
- Avoid assigning the same value in the loop.
- Use const variables.

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

M	source/blender/blenkernel/intern/mesh_iterators.c

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

diff --git a/source/blender/blenkernel/intern/mesh_iterators.c b/source/blender/blenkernel/intern/mesh_iterators.c
index f96a91c6e4d..73dcd912f48 100644
--- a/source/blender/blenkernel/intern/mesh_iterators.c
+++ b/source/blender/blenkernel/intern/mesh_iterators.c
@@ -45,20 +45,21 @@ void BKE_mesh_foreach_mapped_vert(
         void *userData,
         MeshForeachFlag flag)
 {
-	MVert *mv = mesh->mvert;
+	const MVert *mv = mesh->mvert;
 	const int *index = CustomData_get_layer(&mesh->vdata, CD_ORIGINDEX);
-	int i;
 
 	if (index) {
-		for (i = 0; i < mesh->totvert; i++, mv++) {
+		for (int i = 0; i < mesh->totvert; i++, mv++) {
 			const short *no = (flag & MESH_FOREACH_USE_NORMAL) ? mv->no : NULL;
 			const int orig = *index++;
-			if (orig == ORIGINDEX_NONE) continue;
+			if (orig == ORIGINDEX_NONE) {
+				continue;
+			}
 			func(userData, orig, mv->co, NULL, no);
 		}
 	}
 	else {
-		for (i = 0; i < mesh->totvert; i++, mv++) {
+		for (int i = 0; i < mesh->totvert; i++, mv++) {
 			const short *no = (flag & MESH_FOREACH_USE_NORMAL) ? mv->no : NULL;
 			func(userData, i, mv->co, NULL, no);
 		}
@@ -71,18 +72,23 @@ void BKE_mesh_foreach_mapped_edge(
         void (*func)(void *userData, int index, const float v0co[3], const float v1co[3]),
         void *userData)
 {
-	MVert *mv = mesh->mvert;
-	MEdge *med = mesh->medge;
-	int i, orig, *index = CustomData_get_layer(&mesh->edata, CD_ORIGINDEX);
-
-	for (i = 0; i < mesh->totedge; i++, med++) {
-		if (index) {
-			orig = *index++;
-			if (orig == ORIGINDEX_NONE) continue;
+	const MVert *mv = mesh->mvert;
+	const MEdge *med = mesh->medge;
+	const int *index = CustomData_get_layer(&mesh->edata, CD_ORIGINDEX);
+
+	if (index) {
+		for (int i = 0; i < mesh->totedge; i++, med++) {
+			const int orig = *index++;
+			if (orig == ORIGINDEX_NONE) {
+				continue;
+			}
 			func(userData, orig, mv[med->v1].co, mv[med->v2].co);
 		}
-		else
+	}
+	else {
+		for (int i = 0; i < mesh->totedge; i++, med++) {
 			func(userData, i, mv[med->v1].co, mv[med->v2].co);
+		}
 	}
 }
 
@@ -104,12 +110,25 @@ void BKE_mesh_foreach_mapped_loop(
 	const int *f_index = CustomData_get_layer(&mesh->pdata, CD_ORIGINDEX);
 	int p_idx, i;
 
-	for (p_idx = 0; p_idx < mesh->totpoly; ++p_idx, ++mp) {
-		for (i = 0; i < mp->totloop; ++i, ++ml) {
-			const int v_idx = v_index ? v_index[ml->v] : ml->v;
-			const int f_idx = f_index ? f_index[p_idx] : p_idx;
-			const float *no = lnors ? *lnors++ : NULL;
-			if (!ELEM(ORIGINDEX_NONE, v_idx, f_idx)) {
+	if (v_index || f_index) {
+		for (p_idx = 0; p_idx < mesh->totpoly; p_idx++, mp++) {
+			for (i = 0; i < mp->totloop; i++, ml++) {
+				const int v_idx = v_index ? v_index[ml->v] : ml->v;
+				const int f_idx = f_index ? f_index[p_idx] : p_idx;
+				const float *no = lnors ? *lnors++ : NULL;
+				if (ELEM(ORIGINDEX_NONE, v_idx, f_idx)) {
+					continue;
+				}
+				func(userData, v_idx, f_idx, mv[ml->v].co, no);
+			}
+		}
+	}
+	else {
+		for (p_idx = 0; p_idx < mesh->totpoly; p_idx++, mp++) {
+			for (i = 0; i < mp->totloop; i++, ml++) {
+				const int v_idx = ml->v;
+				const int f_idx = p_idx;
+				const float *no = lnors ? *lnors++ : NULL;
 				func(userData, v_idx, f_idx, mv[ml->v].co, no);
 			}
 		}
@@ -123,38 +142,39 @@ void BKE_mesh_foreach_mapped_face_center(
         void *userData,
         MeshForeachFlag flag)
 {
-	MVert *mvert = mesh->mvert;
-	MPoly *mp;
-	MLoop *ml;
-	int i, orig, *index;
-
-	index = CustomData_get_layer(&mesh->pdata, CD_ORIGINDEX);
-	mp = mesh->mpoly;
-	for (i = 0; i < mesh->totpoly; i++, mp++) {
-		float cent[3];
-		float *no, _no[3];
-
-		if (index) {
-			orig = *index++;
-			if (orig == ORIGINDEX_NONE) continue;
-		}
-		else {
-			orig = i;
-		}
-
-		ml = &mesh->mloop[mp->loopstart];
-		BKE_mesh_calc_poly_center(mp, ml, mvert, cent);
+	const MVert *mvert = mesh->mvert;
+	const MPoly *mp = mesh->mpoly;
+	const MLoop *ml;
+	float _no_buf[3];
+	float *no = (flag & MESH_FOREACH_USE_NORMAL) ? _no_buf : NULL;
+	const int *index = CustomData_get_layer(&mesh->pdata, CD_ORIGINDEX);
 
-		if (flag & MESH_FOREACH_USE_NORMAL) {
-			BKE_mesh_calc_poly_normal(mp, ml, mvert, (no = _no));
+	if (index) {
+		for (int i = 0; i < mesh->totpoly; i++, mp++) {
+			const int orig = *index++;
+			if (orig == ORIGINDEX_NONE) {
+				continue;
+			}
+			float cent[3];
+			ml = &mesh->mloop[mp->loopstart];
+			BKE_mesh_calc_poly_center(mp, ml, mvert, cent);
+			if (flag & MESH_FOREACH_USE_NORMAL) {
+				BKE_mesh_calc_poly_normal(mp, ml, mvert, no);
+			}
+			func(userData, orig, cent, no);
 		}
-		else {
-			no = NULL;
+	}
+	else {
+		for (int i = 0; i < mesh->totpoly; i++, mp++) {
+			float cent[3];
+			ml = &mesh->mloop[mp->loopstart];
+			BKE_mesh_calc_poly_center(mp, ml, mvert, cent);
+			if (flag & MESH_FOREACH_USE_NORMAL) {
+				BKE_mesh_calc_poly_normal(mp, ml, mvert, no);
+			}
+			func(userData, i, cent, no);
 		}
-
-		func(userData, orig, cent, no);
 	}
-
 }



More information about the Bf-blender-cvs mailing list