[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51755] trunk/blender/source/blender/bmesh : code cleanup: move select-similar bmesh operators into their own file since there are 3 operators here that share

Campbell Barton ideasman42 at gmail.com
Tue Oct 30 08:59:27 CET 2012


Revision: 51755
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51755
Author:   campbellbarton
Date:     2012-10-30 07:59:25 +0000 (Tue, 30 Oct 2012)
Log Message:
-----------
code cleanup: move select-similar bmesh operators into their own file since there are 3 operators here that share
utility functions with eachother but have nothing in common with other operators in bmo_utils.c

Modified Paths:
--------------
    trunk/blender/source/blender/bmesh/CMakeLists.txt
    trunk/blender/source/blender/bmesh/operators/bmo_utils.c

Added Paths:
-----------
    trunk/blender/source/blender/bmesh/operators/bmo_select_similar.c

Modified: trunk/blender/source/blender/bmesh/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/bmesh/CMakeLists.txt	2012-10-30 07:29:17 UTC (rev 51754)
+++ trunk/blender/source/blender/bmesh/CMakeLists.txt	2012-10-30 07:59:25 UTC (rev 51755)
@@ -52,6 +52,7 @@
 	operators/bmo_mirror.c
 	operators/bmo_primitive.c
 	operators/bmo_removedoubles.c
+	operators/bmo_select_similar.c
 	operators/bmo_smooth_laplacian.c
 	operators/bmo_symmetrize.c
 	operators/bmo_subdivide.c

Added: trunk/blender/source/blender/bmesh/operators/bmo_select_similar.c
===================================================================
--- trunk/blender/source/blender/bmesh/operators/bmo_select_similar.c	                        (rev 0)
+++ trunk/blender/source/blender/bmesh/operators/bmo_select_similar.c	2012-10-30 07:59:25 UTC (rev 51755)
@@ -0,0 +1,643 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Joseph Eagar, Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/bmesh/operators/bmo_select_similar.c
+ *  \ingroup bmesh
+ *
+ * bmesh operators to select based on
+ * comparisons with the existing selection.
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_meshdata_types.h"
+
+#include "BLI_math.h"
+
+#include "BKE_customdata.h"
+
+#include "bmesh.h"
+
+#include "intern/bmesh_operators_private.h"  /* own include */
+
+/*
+ * compute the fake surface of an ngon
+ * This is done by decomposing the ngon into triangles who share the centroid of the ngon
+ * while this method is far from being exact, it should guarantee an invariance.
+ *
+ * NOTE: This should probably go to bmesh_polygon.c
+ */
+static float ngon_fake_area(BMFace *f)
+{
+	BMIter  liter;
+	BMLoop *l;
+	int     num_verts = 0;
+	float   v[3], sv[3], c[3];
+	float   area = 0.0f;
+
+	BM_face_calc_center_mean(f, c);
+
+	BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
+		if (num_verts == 0) {
+			copy_v3_v3(v, l->v->co);
+			copy_v3_v3(sv, l->v->co);
+			num_verts++;
+		}
+		else {
+			area += area_tri_v3(v, c, l->v->co);
+			copy_v3_v3(v, l->v->co);
+			num_verts++;
+		}
+	}
+
+	area += area_tri_v3(v, c, sv);
+
+	return area;
+}
+
+/*
+ * extra face data (computed data)
+ */
+typedef struct SimSel_FaceExt {
+	BMFace  *f;             /* the face */
+	float    c[3];          /* center */
+	union {
+		float   area;       /* area */
+		float   perim;      /* perimeter */
+		float   d;          /* 4th component of plane (the first three being the normal) */
+		struct Image *t;    /* image pointer */
+	};
+} SimSel_FaceExt;
+
+static int bm_sel_similar_cmp_fl(const float delta, const float thresh, const int compare)
+{
+	switch (compare) {
+		case SIM_CMP_EQ:
+			return (fabsf(delta) <= thresh);
+		case SIM_CMP_GT:
+			return ((delta + thresh) >= 0.0f);
+		case SIM_CMP_LT:
+			return ((delta - thresh) <= 0.0f);
+		default:
+			BLI_assert(0);
+	}
+}
+
+static int bm_sel_similar_cmp_i(const int delta, const int compare)
+{
+	switch (compare) {
+		case SIM_CMP_EQ:
+			return (delta == 0);
+		case SIM_CMP_GT:
+			return (delta > 0);
+		case SIM_CMP_LT:
+			return (delta < 0);
+		default:
+			BLI_assert(0);
+	}
+}
+
+#define FACE_MARK	4
+
+/*
+ * Select similar faces, the choices are in the enum in source/blender/bmesh/bmesh_operators.h
+ * We select either similar faces based on material, image, area, perimeter, normal, or the coplanar faces
+ */
+void bmo_similar_faces_exec(BMesh *bm, BMOperator *op)
+{
+	BMIter fm_iter;
+	BMFace *fs, *fm;
+	BMOIter fs_iter;
+	int num_sels = 0, num_total = 0, i = 0, idx = 0;
+	float angle = 0.0f;
+	SimSel_FaceExt *f_ext = NULL;
+	int *indices = NULL;
+	float t_no[3];	/* temporary normal */
+	const int type = BMO_slot_int_get(op, "type");
+	const float thresh = BMO_slot_float_get(op, "thresh");
+	const float thresh_radians = thresh * (float)M_PI;
+	const int compare = BMO_slot_int_get(op, "compare");
+
+	/* initial_elem - other_elem */
+	float delta_fl;
+	int   delta_i;
+
+	num_total = BM_mesh_elem_count(bm, BM_FACE);
+
+	/*
+	 * The first thing to do is to iterate through all the the selected items and mark them since
+	 * they will be in the selection anyway.
+	 * This will increase performance, (especially when the number of originally selected faces is high)
+	 * so the overall complexity will be less than $O(mn)$ where is the total number of selected faces,
+	 * and n is the total number of faces
+	 */
+	BMO_ITER (fs, &fs_iter, bm, op, "faces", BM_FACE) {
+		if (!BMO_elem_flag_test(bm, fs, FACE_MARK)) {	/* is this really needed ? */
+			BMO_elem_flag_enable(bm, fs, FACE_MARK);
+			num_sels++;
+		}
+	}
+
+	/* allocate memory for the selected faces indices and for all temporary faces */
+	indices = (int *)MEM_callocN(sizeof(int) * num_sels, "face indices util.c");
+	f_ext = (SimSel_FaceExt *)MEM_callocN(sizeof(SimSel_FaceExt) * num_total, "f_ext util.c");
+
+	/* loop through all the faces and fill the faces/indices structure */
+	BM_ITER_MESH (fm, &fm_iter, bm, BM_FACES_OF_MESH) {
+		f_ext[i].f = fm;
+		if (BMO_elem_flag_test(bm, fm, FACE_MARK)) {
+			indices[idx] = i;
+			idx++;
+		}
+		i++;
+	}
+
+	/*
+	 * Save us some computation burden: In case of perimeter/area/coplanar selection we compute
+	 * only once.
+	 */
+	if (type == SIMFACE_PERIMETER || type == SIMFACE_AREA || type == SIMFACE_COPLANAR || type == SIMFACE_IMAGE) {
+		for (i = 0; i < num_total; i++) {
+			switch (type) {
+				case SIMFACE_PERIMETER:
+					/* set the perimeter */
+					f_ext[i].perim = BM_face_calc_perimeter(f_ext[i].f);
+					break;
+
+				case SIMFACE_COPLANAR:
+					/* compute the center of the polygon */
+					BM_face_calc_center_mean(f_ext[i].f, f_ext[i].c);
+
+					/* normalize the polygon normal */
+					copy_v3_v3(t_no, f_ext[i].f->no);
+					normalize_v3(t_no);
+
+					/* compute the plane distance */
+					f_ext[i].d = dot_v3v3(t_no, f_ext[i].c);
+					break;
+
+				case SIMFACE_AREA:
+					f_ext[i].area = ngon_fake_area(f_ext[i].f);
+					break;
+
+				case SIMFACE_IMAGE:
+					f_ext[i].t = NULL;
+					if (CustomData_has_layer(&(bm->pdata), CD_MTEXPOLY)) {
+						MTexPoly *mtpoly = CustomData_bmesh_get(&bm->pdata, f_ext[i].f->head.data, CD_MTEXPOLY);
+						f_ext[i].t = mtpoly->tpage;
+					}
+					break;
+			}
+		}
+	}
+
+	/* now select the rest (if any) */
+	for (i = 0; i < num_total; i++) {
+		fm = f_ext[i].f;
+		if (!BMO_elem_flag_test(bm, fm, FACE_MARK) && !BM_elem_flag_test(fm, BM_ELEM_HIDDEN)) {
+			int cont = TRUE;
+			for (idx = 0; idx < num_sels && cont == TRUE; idx++) {
+				fs = f_ext[indices[idx]].f;
+				switch (type) {
+					case SIMFACE_MATERIAL:
+						if (fm->mat_nr == fs->mat_nr) {
+							BMO_elem_flag_enable(bm, fm, FACE_MARK);
+							cont = FALSE;
+						}
+						break;
+
+					case SIMFACE_IMAGE:
+						if (f_ext[i].t == f_ext[indices[idx]].t) {
+							BMO_elem_flag_enable(bm, fm, FACE_MARK);
+							cont = FALSE;
+						}
+						break;
+
+					case SIMFACE_NORMAL:
+						angle = angle_normalized_v3v3(fs->no, fm->no);	/* if the angle between the normals -> 0 */
+						if (angle <= thresh_radians) {
+							BMO_elem_flag_enable(bm, fm, FACE_MARK);
+							cont = FALSE;
+						}
+						break;
+
+					case SIMFACE_COPLANAR:
+						angle = angle_normalized_v3v3(fs->no, fm->no); /* angle -> 0 */
+						if (angle <= thresh_radians) { /* and dot product difference -> 0 */
+							delta_fl = f_ext[i].d - f_ext[indices[idx]].d;
+							if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
+								BMO_elem_flag_enable(bm, fm, FACE_MARK);
+								cont = FALSE;
+							}
+						}
+						break;
+
+					case SIMFACE_AREA:
+						delta_fl = f_ext[i].area - f_ext[indices[idx]].area;
+						if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
+							BMO_elem_flag_enable(bm, fm, FACE_MARK);
+							cont = FALSE;
+						}
+						break;
+
+					case SIMFACE_SIDES:
+						delta_i = fm->len - fs->len;
+						if (bm_sel_similar_cmp_i(delta_i, compare)) {
+							BMO_elem_flag_enable(bm, fm, FACE_MARK);
+							cont = FALSE;
+						}
+						break;
+
+					case SIMFACE_PERIMETER:
+						delta_fl = f_ext[i].perim - f_ext[indices[idx]].perim;
+						if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
+							BMO_elem_flag_enable(bm, fm, FACE_MARK);
+							cont = FALSE;
+						}
+						break;
+					default:
+						BLI_assert(0);
+				}
+			}
+		}
+	}
+
+	MEM_freeN(f_ext);
+	MEM_freeN(indices);
+
+	/* transfer all marked faces to the output slot */
+	BMO_slot_buffer_from_enabled_flag(bm, op, "faceout", BM_FACE, FACE_MARK);
+}
+
+/**************************************************************************** *
+ * Similar Edges
+ **************************************************************************** */
+#define EDGE_MARK 1
+
+/*
+ * extra edge information
+ */
+typedef struct SimSel_EdgeExt {
+	BMEdge *e;
+	union {
+		float dir[3];
+		float angle;            /* angle between the face */
+	};
+
+	union {
+		float length;           /* edge length */
+		int   faces;            /* faces count */
+	};
+} SimSel_EdgeExt;
+
+/*
+ * select similar edges: the choices are in the enum in source/blender/bmesh/bmesh_operators.h
+ * choices are length, direction, face, ...
+ */
+void bmo_similar_edges_exec(BMesh *bm, BMOperator *op)
+{
+	BMOIter es_iter;	/* selected edges iterator */
+	BMIter e_iter;		/* mesh edges iterator */
+	BMEdge *es;		/* selected edge */
+	BMEdge *e;		/* mesh edge */
+	int idx = 0, i = 0 /* , f = 0 */;
+	int *indices = NULL;
+	SimSel_EdgeExt *e_ext = NULL;
+	// float *angles = NULL;
+	float angle;
+
+	int num_sels = 0, num_total = 0;
+	const int type = BMO_slot_int_get(op, "type");
+	const float thresh = BMO_slot_float_get(op, "thresh");
+	const int compare = BMO_slot_int_get(op, "compare");
+
+	/* initial_elem - other_elem */
+	float delta_fl;
+	int   delta_i;
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list