[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [57783] branches/soc-2013-sketch_mesh: Implementation of the system of equations for the deformation with preservation of detail .

Alexander Pinzon apinzonf at gmail.com
Wed Jun 26 22:07:49 CEST 2013


Revision: 57783
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=57783
Author:   apinzonf
Date:     2013-06-26 20:07:49 +0000 (Wed, 26 Jun 2013)
Log Message:
-----------
Implementation of the system of equations for the deformation with preservation of detail.
Calculating the implied rotation for differential coordinates

Modified Paths:
--------------
    branches/soc-2013-sketch_mesh/release/scripts/startup/bl_ui/space_view3d.py
    branches/soc-2013-sketch_mesh/source/blender/bmesh/CMakeLists.txt
    branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_opdefines.c
    branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_operators_private.h
    branches/soc-2013-sketch_mesh/source/blender/editors/mesh/editmesh_tools.c
    branches/soc-2013-sketch_mesh/source/blender/editors/mesh/mesh_intern.h
    branches/soc-2013-sketch_mesh/source/blender/editors/mesh/mesh_ops.c

Added Paths:
-----------
    branches/soc-2013-sketch_mesh/source/blender/bmesh/operators/bmo_deform_laplacian.c
    branches/soc-2013-sketch_mesh/source/blender/bmesh/operators/bmo_deform_utils.cpp
    branches/soc-2013-sketch_mesh/source/blender/bmesh/operators/bmo_deform_utils.h

Modified: branches/soc-2013-sketch_mesh/release/scripts/startup/bl_ui/space_view3d.py
===================================================================
--- branches/soc-2013-sketch_mesh/release/scripts/startup/bl_ui/space_view3d.py	2013-06-26 19:23:03 UTC (rev 57782)
+++ branches/soc-2013-sketch_mesh/release/scripts/startup/bl_ui/space_view3d.py	2013-06-26 20:07:49 UTC (rev 57783)
@@ -1846,6 +1846,7 @@
         layout.operator("mesh.flip_normals")
         layout.operator("mesh.vertices_smooth", text="Smooth")
         layout.operator("mesh.vertices_smooth_laplacian", text="Laplacian Smooth")
+        layout.operator("mesh.vertices_deform_laplacian", text="Laplacian Deform")
 
         layout.separator()
 

Modified: branches/soc-2013-sketch_mesh/source/blender/bmesh/CMakeLists.txt
===================================================================
--- branches/soc-2013-sketch_mesh/source/blender/bmesh/CMakeLists.txt	2013-06-26 19:23:03 UTC (rev 57782)
+++ branches/soc-2013-sketch_mesh/source/blender/bmesh/CMakeLists.txt	2013-06-26 20:07:49 UTC (rev 57783)
@@ -29,6 +29,7 @@
 	../blenkernel
 	../blenlib
 	../makesdna
+	../../../extern/Eigen3
 	../../../intern/guardedalloc
 	../../../extern/rangetree
 	../../../intern/opennl/extern
@@ -44,6 +45,9 @@
 	operators/bmo_bridge.c
 	operators/bmo_connect.c
 	operators/bmo_create.c
+	operators/bmo_deform_laplacian.c
+	operators/bmo_deform_utils.cpp
+	operators/bmo_deform_utils.h
 	operators/bmo_dissolve.c
 	operators/bmo_dupe.c
 	operators/bmo_edgenet.c

Modified: branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_opdefines.c
===================================================================
--- branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_opdefines.c	2013-06-26 19:23:03 UTC (rev 57782)
+++ branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_opdefines.c	2013-06-26 20:07:49 UTC (rev 57783)
@@ -142,6 +142,29 @@
 };
 
 /*
+ * Vertext deform Laplacian.
+ *
+ * Deform vertices by using Laplacian coordinates.
+ * bmo_deform_laplacian_vert_exec
+ */
+static BMOpDefine bmo_deform_laplacian_vert_def = {
+	"deform_laplacian_vert",
+	/* slots_in */
+	{{"verts", BMO_OP_SLOT_ELEMENT_BUF, {BM_VERT}},    /* input vertices */
+	 {"lambda_factor", BMO_OP_SLOT_FLT},           /* lambda param */
+	 {"lambda_border", BMO_OP_SLOT_FLT},    /* lambda param in border */
+	 {"use_x", BMO_OP_SLOT_BOOL},           /* Smooth object along X axis */
+	 {"use_y", BMO_OP_SLOT_BOOL},           /* Smooth object along Y axis */
+	 {"use_z", BMO_OP_SLOT_BOOL},           /* Smooth object along Z axis */
+	 {"preserve_volume", BMO_OP_SLOT_BOOL}, /* Apply volume preservation after smooth */
+	{{'\0'}},
+	},
+	{{{'\0'}}},  /* no output */
+	bmo_deform_laplacian_vert_exec,
+	BMO_OPTYPE_FLAG_NORMALS_CALC,
+};
+
+/*
  * Right-Hand Faces.
  *
  * Computes an "outside" normal for the specified input faces.
@@ -1713,6 +1736,7 @@
 	&bmo_create_monkey_def,
 	&bmo_create_uvsphere_def,
 	&bmo_create_vert_def,
+	&bmo_deform_laplacian_vert_def,
 	&bmo_delete_def,
 	&bmo_dissolve_edge_loop_def,
 	&bmo_dissolve_edges_def,

Modified: branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_operators_private.h
===================================================================
--- branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_operators_private.h	2013-06-26 19:23:03 UTC (rev 57782)
+++ branches/soc-2013-sketch_mesh/source/blender/bmesh/intern/bmesh_operators_private.h	2013-06-26 20:07:49 UTC (rev 57783)
@@ -50,6 +50,7 @@
 void bmo_create_monkey_exec(BMesh *bm, BMOperator *op);
 void bmo_create_uvsphere_exec(BMesh *bm, BMOperator *op);
 void bmo_create_vert_exec(BMesh *bm, BMOperator *op);
+void bmo_deform_laplacian_vert_exec(BMesh *bm, BMOperator *op);
 void bmo_delete_exec(BMesh *bm, BMOperator *op);
 void bmo_dissolve_edgeloop_exec(BMesh *bm, BMOperator *op);
 void bmo_dissolve_edges_exec(BMesh *bm, BMOperator *op);

Added: branches/soc-2013-sketch_mesh/source/blender/bmesh/operators/bmo_deform_laplacian.c
===================================================================
--- branches/soc-2013-sketch_mesh/source/blender/bmesh/operators/bmo_deform_laplacian.c	                        (rev 0)
+++ branches/soc-2013-sketch_mesh/source/blender/bmesh/operators/bmo_deform_laplacian.c	2013-06-26 20:07:49 UTC (rev 57783)
@@ -0,0 +1,458 @@
+/*
+ * ***** 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): Alexander Pinzon
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/bmesh/operators/bmo_deform_laplacian.c
+ *  \ingroup bmesh
+ *
+ * Deform Laplacian.
+ */
+#include "MEM_guardedalloc.h"
+#include "BLI_math.h"
+#include "BLI_array.h"
+#include "bmesh.h"
+#include "ONL_opennl.h"
+#include "intern/bmesh_operators_private.h" /* own include */
+#include "bmo_deform_utils.h"
+
+struct BLaplacianSystem {
+	float *vweights;			/* Total sum of weights per vertice*/
+	float (*delta)[3];			/* Differential Coordinates*/
+	int numVerts;				/* Number of verts*/
+	/* Pointers to data*/
+	BMesh *bm;
+	BMOperator *op;
+	NLContext *context;
+	vptrSpMatrixD spLapMatrix;  /* Sparse Laplacian Matrix*/
+	vptrVectorD VectorB;		/* Array to store vertex positions of handlers*/
+	vptrVectorD VectorX;		/* Array to  store solution */
+	vptrTripletD tripletList;	/* List of triplets in Laplacian Matrix*/
+};
+typedef struct BLaplacianSystem LaplacianSystem;
+
+static void compute_mesh_laplacian();
+static void delete_laplacian_system(LaplacianSystem *sys);
+static void delete_void_pointer(void *data);
+
+static float cotan_weight(float *v1, float *v2, float *v3)
+{
+	float a[3], b[3], c[3], clen;
+
+	sub_v3_v3v3(a, v2, v1);
+	sub_v3_v3v3(b, v3, v1);
+	cross_v3_v3v3(c, a, b);
+
+	clen = len_v3(c);
+	
+	if (clen < FLT_EPSILON)
+		return 0.0f;
+
+	return dot_v3v3(a, b) / clen;
+}
+
+static void delete_void_pointer(void *data)
+{
+	if (data) {
+		MEM_freeN(data);
+	}
+}
+
+static void delete_laplacian_system(LaplacianSystem *sys)
+{
+	if(!sys) return;
+	delete_void_pointer(sys->vweights);
+	delete_void_pointer(sys->delta);
+	if (sys->context) {
+		nlDeleteContext(sys->context);
+	}
+	if (sys->spLapMatrix) {
+		delete_spmatrix(sys->spLapMatrix);
+	}
+	if (sys->VectorB) {
+		delete_vectord(sys->VectorB);
+	}
+	if (sys->VectorX) {
+		delete_vectord(sys->VectorX);
+	}
+	if (sys->tripletList) {
+		delete_triplet(sys->tripletList);
+	}
+	sys->bm = NULL;
+	sys->op = NULL;
+	MEM_freeN(sys);
+}
+
+static void memset_laplacian_system(LaplacianSystem *sys, int val)
+{
+	memset(sys->vweights,     val, sizeof(float) * sys->numVerts);
+	memset(sys->delta,     val, sizeof(float) * sys->numVerts);
+}
+
+static LaplacianSystem *init_laplacian_system(int a_numVerts, int rows, int cols)
+{
+	LaplacianSystem *sys;
+	sys = (LaplacianSystem *)MEM_callocN(sizeof(LaplacianSystem), "bmoLaplDeformSystem");
+	if (!sys) {
+		return NULL;
+	}
+	sys->numVerts = a_numVerts;
+
+	sys->spLapMatrix = new_spmatrix(rows, cols);
+	if (!sys->spLapMatrix) {
+		delete_laplacian_system(sys);
+		return NULL;
+	}
+
+	sys->VectorB = new_vectord(rows);
+	if (!sys->VectorB) {
+		delete_laplacian_system(sys);
+		return NULL;
+	}
+
+	sys->VectorX = new_vectord(cols);
+	if (!sys->VectorX) {
+		delete_laplacian_system(sys);
+		return NULL;
+	}
+	
+	sys->tripletList = new_triplet(a_numVerts*18);
+	if (!sys->tripletList) {
+		delete_laplacian_system(sys);
+		return NULL;
+	}
+
+	sys->vweights =  (float *)MEM_callocN(sizeof(float) * sys->numVerts, "bmoLaplDeformVweights");
+	if (!sys->vweights) {
+		delete_laplacian_system(sys);
+		return NULL;
+	}
+
+	sys->delta =  (float (*)[3])MEM_callocN(sizeof(float) * sys->numVerts * 3, "bmoLaplDeformDelta");
+	if (!sys->delta) {
+		delete_laplacian_system(sys);
+		return NULL;
+	}
+
+	return sys;
+}
+
+static void init_laplacian_matrix(LaplacianSystem *sys)
+{
+	float *v1, *v2, *v3, *v4;
+	float w2, w3, w4;
+	int i, j;
+	bool has_4_vert;
+	unsigned int idv1, idv2, idv3, idv4, idv[4];
+	BMFace *f;
+	BMIter fiter;
+	BMIter vi;
+	BMVert *vn;
+	BMVert *vf[4];
+
+	BM_ITER_MESH (f, &fiter, sys->bm, BM_FACES_OF_MESH) {
+		if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
+
+			BM_ITER_ELEM_INDEX (vn, &vi, f, BM_VERTS_OF_FACE, i) {
+				vf[i] = vn;
+			}
+			has_4_vert = (i == 4) ? 1 : 0;
+			idv1 = BM_elem_index_get(vf[0]);
+			idv2 = BM_elem_index_get(vf[1]);
+			idv3 = BM_elem_index_get(vf[2]);
+			idv4 = has_4_vert ? BM_elem_index_get(vf[3]) : 0;
+
+			v1 = vf[0]->co;
+			v2 = vf[1]->co;
+			v3 = vf[2]->co;
+			v4 = has_4_vert ? vf[3]->co : 0;
+
+			idv[0] = idv1;
+			idv[1] = idv2;
+			idv[2] = idv3;
+			idv[3] = idv4;
+
+			nlRightHandSideAdd(0, idv1						, 0.0f);
+			nlRightHandSideAdd(0, sys->numVerts + idv1		, 0.0f);
+			nlRightHandSideAdd(0, 2*sys->numVerts + idv1	, 0.0f);
+
+			for (j = 0; j < i; j++) {
+				idv1 = idv[j];
+				idv2 = idv[(j + 1) % i];
+				idv3 = idv[(j + 2) % i];
+				idv4 = idv[(j + 3) % i];
+
+				v1 = vf[j]->co;
+				v2 = vf[(j + 1) % i]->co;
+				v3 = vf[(j + 2) % i]->co;
+				v4 = has_4_vert ? vf[(j + 3) % i]->co : 0;
+
+				if (has_4_vert) {
+
+					w2 = (cotan_weight(v4, v1, v2) + cotan_weight(v3, v1, v2)) ;
+					w3 = (cotan_weight(v2, v3, v1) + cotan_weight(v4, v1, v3)) ;
+					w4 = (cotan_weight(v2, v4, v1) + cotan_weight(v3, v4, v1)) ;
+
+					sys->delta[idv1][0] -=  v4[0] * w4;
+					sys->delta[idv1][1] -=  v4[1] * w4;
+					sys->delta[idv1][2] -=  v4[2] * w4;
+
+					nlMatrixAdd(idv1					, idv4						, -w4 );
+					nlMatrixAdd(sys->numVerts + idv1	, sys->numVerts + idv4		, -w4 );
+					nlMatrixAdd(sys->numVerts*2 + idv1	, sys->numVerts*2 + idv4	, -w4 );
+
+					push_back_triplet(sys->tripletList, idv1					, idv4						, -w4 );
+					push_back_triplet(sys->tripletList, sys->numVerts + idv1	, sys->numVerts + idv4		, -w4 );

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list