[Bf-blender-cvs] [7787d6b] strand_editmode: Utility methods for mirrored strand editing.

Lukas Tönne noreply at git.blender.org
Sat Feb 7 16:36:28 CET 2015


Commit: 7787d6bfd69bc8b461092a4cc2666847e9d078a8
Author: Lukas Tönne
Date:   Sat Feb 7 16:34:57 2015 +0100
Branches: strand_editmode
https://developer.blender.org/rB7787d6bfd69bc8b461092a4cc2666847e9d078a8

Utility methods for mirrored strand editing.

These are a modified version of their BMEditMesh counterparts.

use_topology is not yet implemented for strands. Native strand topology
is not very useful for this. Instead, the topology of the scalp mesh
should be used for finding mirrored strand roots, then the arc- or
parametric length of a vertex from the root to find mirrored verts.

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

M	source/blender/blenkernel/BKE_editstrands.h
M	source/blender/editors/hair/CMakeLists.txt
A	source/blender/editors/hair/hair_mirror.c
M	source/blender/editors/include/ED_physics.h

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

diff --git a/source/blender/blenkernel/BKE_editstrands.h b/source/blender/blenkernel/BKE_editstrands.h
index 017cdf9..01082a0 100644
--- a/source/blender/blenkernel/BKE_editstrands.h
+++ b/source/blender/blenkernel/BKE_editstrands.h
@@ -59,6 +59,9 @@ typedef struct BMEditStrands {
 	unsigned int vertex_glbuf;
 	unsigned int elem_glbuf;
 	unsigned int dot_glbuf;
+	
+	/*temp variables for x-mirror editing*/
+	int mirror_cdlayer; /* -1 is invalid */
 } BMEditStrands;
 
 /* BMEditStrands->flag */
diff --git a/source/blender/editors/hair/CMakeLists.txt b/source/blender/editors/hair/CMakeLists.txt
index 4430e10..62db4de 100644
--- a/source/blender/editors/hair/CMakeLists.txt
+++ b/source/blender/editors/hair/CMakeLists.txt
@@ -40,6 +40,7 @@ set(INC_SYS
 set(SRC
 	hair_cursor.c
 	hair_edit.c
+	hair_mirror.c
 	hair_ops.c
 	hair_select.c
 	hair_stroke.c
diff --git a/source/blender/editors/hair/hair_mirror.c b/source/blender/editors/hair/hair_mirror.c
new file mode 100644
index 0000000..0e0d122
--- /dev/null
+++ b/source/blender/editors/hair/hair_mirror.c
@@ -0,0 +1,210 @@
+/*
+ * ***** 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.
+ *
+ * The Original Code is Copyright (C) Blender Foundation
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): Lukas Toenne
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/editors/hair/hair_mirror.c
+ *  \ingroup edhair
+ */
+
+#include <stdlib.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_utildefines.h"
+#include "BLI_math.h"
+
+#include "BKE_editmesh_bvh.h"
+#include "BKE_editstrands.h"
+
+#include "ED_physics.h"
+#include "ED_util.h"
+
+#include "bmesh.h"
+
+#include "hair_intern.h"
+
+/* TODO use_topology is not yet implemented for strands.
+ * Native strand topology is not very useful for this.
+ * Instead, the topology of the scalp mesh should be used for finding mirrored strand roots,
+ * then the arc- or parametric length of a vertex from the root to find mirrored verts.
+ */
+
+#define BM_SEARCH_MAXDIST_MIRR 0.00002f
+#define BM_CD_LAYER_ID "__mirror_index"
+/**
+ * \param edit  Edit strands.
+ * \param use_self  Allow a vertex to point to its self (middle verts).
+ * \param use_select  Restrict to selected verts.
+ * \param use_topology  Use topology mirror.
+ * \param maxdist  Distance for close point test.
+ * \param r_index  Optional array to write into, as an alternative to a customdata layer (length of total verts).
+ */
+void ED_strands_mirror_cache_begin_ex(BMEditStrands *edit, const int axis, const bool use_self, const bool use_select,
+                                      /* extra args */
+                                      const bool UNUSED(use_topology), float maxdist, int *r_index)
+{
+	BMesh *bm = edit->bm;
+	BMIter iter;
+	BMVert *v;
+	int cd_vmirr_offset;
+	int i;
+
+	/* one or the other is used depending if topo is enabled */
+	struct BMBVHTree *tree = NULL;
+
+	BM_mesh_elem_table_ensure(bm, BM_VERT);
+
+	if (r_index == NULL) {
+		const char *layer_id = BM_CD_LAYER_ID;
+		edit->mirror_cdlayer = CustomData_get_named_layer_index(&bm->vdata, CD_PROP_INT, layer_id);
+		if (edit->mirror_cdlayer == -1) {
+			BM_data_layer_add_named(bm, &bm->vdata, CD_PROP_INT, layer_id);
+			edit->mirror_cdlayer = CustomData_get_named_layer_index(&bm->vdata, CD_PROP_INT, layer_id);
+		}
+
+		cd_vmirr_offset = CustomData_get_n_offset(&bm->vdata, CD_PROP_INT,
+		                                          edit->mirror_cdlayer - CustomData_get_layer_index(&bm->vdata, CD_PROP_INT));
+
+		bm->vdata.layers[edit->mirror_cdlayer].flag |= CD_FLAG_TEMPORARY;
+	}
+
+	BM_mesh_elem_index_ensure(bm, BM_VERT);
+
+	tree = BKE_bmbvh_new(edit->bm, NULL, 0, 0, NULL, false);
+
+#define VERT_INTPTR(_v, _i) r_index ? &r_index[_i] : BM_ELEM_CD_GET_VOID_P(_v, cd_vmirr_offset);
+
+	BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
+		BLI_assert(BM_elem_index_get(v) == i);
+
+		/* temporary for testing, check for selection */
+		if (use_select && !BM_elem_flag_test(v, BM_ELEM_SELECT)) {
+			/* do nothing */
+		}
+		else {
+			BMVert *v_mirr;
+			float co[3];
+			int *idx = VERT_INTPTR(v, i);
+
+			copy_v3_v3(co, v->co);
+			co[axis] *= -1.0f;
+			v_mirr = BKE_bmbvh_find_vert_closest(tree, co, maxdist);
+
+			if (v_mirr && (use_self || (v_mirr != v))) {
+				const int i_mirr = BM_elem_index_get(v_mirr);
+				*idx = i_mirr;
+				idx = VERT_INTPTR(v_mirr, i_mirr);
+				*idx = i;
+			}
+			else {
+				*idx = -1;
+			}
+		}
+
+	}
+
+#undef VERT_INTPTR
+
+	BKE_bmbvh_free(tree);
+}
+
+void ED_strands_mirror_cache_begin(BMEditStrands *edit, const int axis,
+                                   const bool use_self, const bool use_select,
+                                   const bool use_topology)
+{
+	ED_strands_mirror_cache_begin_ex(edit, axis,
+	                                 use_self, use_select,
+	                                 /* extra args */
+	                                 use_topology, BM_SEARCH_MAXDIST_MIRR, NULL);
+}
+
+BMVert *ED_strands_mirror_get(BMEditStrands *edit, BMVert *v)
+{
+	const int *mirr = CustomData_bmesh_get_layer_n(&edit->bm->vdata, v->head.data, edit->mirror_cdlayer);
+
+	BLI_assert(edit->mirror_cdlayer != -1); /* invalid use */
+
+	if (mirr && *mirr >= 0 && *mirr < edit->bm->totvert) {
+		if (!edit->bm->vtable) {
+			printf("err: should only be called between "
+			       "ED_strands_mirror_cache_begin and ED_strands_mirror_cache_end");
+			return NULL;
+		}
+
+		return edit->bm->vtable[*mirr];
+	}
+
+	return NULL;
+}
+
+BMEdge *ED_strands_mirror_get_edge(BMEditStrands *edit, BMEdge *e)
+{
+	BMVert *v1_mirr = ED_strands_mirror_get(edit, e->v1);
+	if (v1_mirr) {
+		BMVert *v2_mirr = ED_strands_mirror_get(edit, e->v2);
+		if (v2_mirr) {
+			return BM_edge_exists(v1_mirr, v2_mirr);
+		}
+	}
+
+	return NULL;
+}
+
+void ED_strands_mirror_cache_clear(BMEditStrands *edit, BMVert *v)
+{
+	int *mirr = CustomData_bmesh_get_layer_n(&edit->bm->vdata, v->head.data, edit->mirror_cdlayer);
+
+	BLI_assert(edit->mirror_cdlayer != -1); /* invalid use */
+
+	if (mirr) {
+		*mirr = -1;
+	}
+}
+
+void ED_strands_mirror_cache_end(BMEditStrands *edit)
+{
+	edit->mirror_cdlayer = -1;
+}
+
+void ED_strands_mirror_apply(BMEditStrands *edit, const int sel_from, const int sel_to)
+{
+	BMIter iter;
+	BMVert *v;
+
+	BLI_assert((edit->bm->vtable != NULL) && ((edit->bm->elem_table_dirty & BM_VERT) == 0));
+
+	BM_ITER_MESH (v, &iter, edit->bm, BM_VERTS_OF_MESH) {
+		if (BM_elem_flag_test(v, BM_ELEM_SELECT) == sel_from) {
+			BMVert *mirr = ED_strands_mirror_get(edit, v);
+			if (mirr) {
+				if (BM_elem_flag_test(mirr, BM_ELEM_SELECT) == sel_to) {
+					copy_v3_v3(mirr->co, v->co);
+					mirr->co[0] *= -1.0f;
+				}
+			}
+		}
+	}
+}
diff --git a/source/blender/editors/include/ED_physics.h b/source/blender/editors/include/ED_physics.h
index 58baac7..93ac73d 100644
--- a/source/blender/editors/include/ED_physics.h
+++ b/source/blender/editors/include/ED_physics.h
@@ -40,6 +40,7 @@ struct rcti;
 
 struct Scene;
 struct Object;
+struct BMEditStrands;
 
 /* particle_edit.c */
 int PE_poll(struct bContext *C);
@@ -61,6 +62,18 @@ void ED_keymap_physics(struct wmKeyConfig *keyconf);
 /* hair edit */
 void undo_push_strands(struct bContext *C, const char *name);
 
+void           ED_strands_mirror_cache_begin_ex(struct BMEditStrands *em, const int axis,
+                                                const bool use_self, const bool use_select,
+                                                const bool use_topology, float maxdist, int *r_index);
+void           ED_strands_mirror_cache_begin(struct BMEditStrands *em, const int axis,
+                                             const bool use_self, const bool use_select,
+                                             const bool use_topology);
+void           ED_strands_mirror_apply(struct BMEditStrands *em, const int sel_from, const int sel_to);
+struct BMVert *ED_strands_mirror_get(struct BMEditStrands *em, struct BMVert *v);
+struct BMEdge *ED_strands_mirror_get_edge(struct BMEditStrands *em, struct BMEdge *e);
+void           ED_strands_mirror_cache_clear(struct BMEditStrands *em, struct BMVert *v);
+void           ED_strands_mirror_cache_end(struct BMEditStrands *em);
+
 int ED_hair_mouse_select(struct bContext *C, const int mval[2], bool extend, bool deselect, bool toggle);
 int ED_hair_border_select(struct bContext *C, struct rcti *rect, bool select, bool extend);
 int ED_hair_circle_select(struct bContext *C, bool select, const int mval[2], float radius);




More information about the Bf-blender-cvs mailing list