[Bf-blender-cvs] [7f9b00bd464] sculpt-mode-features: Refactor: Move remesher functions to blenderkernel

Pablo Dobarro noreply at git.blender.org
Mon Apr 8 01:22:54 CEST 2019


Commit: 7f9b00bd464df2f2abbeaf016fe6dc633a034691
Author: Pablo Dobarro
Date:   Mon Apr 8 01:22:39 2019 +0200
Branches: sculpt-mode-features
https://developer.blender.org/rB7f9b00bd464df2f2abbeaf016fe6dc633a034691

Refactor: Move remesher functions to blenderkernel

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

A	source/blender/blenkernel/BKE_remesh.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/remesh_voxel.c
M	source/blender/editors/object/object_edit.c

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

diff --git a/source/blender/blenkernel/BKE_remesh.h b/source/blender/blenkernel/BKE_remesh.h
new file mode 100644
index 00000000000..e51d92237ca
--- /dev/null
+++ b/source/blender/blenkernel/BKE_remesh.h
@@ -0,0 +1,39 @@
+/*
+ * 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) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ */
+#ifndef __BKE_REMESH_H__
+#define __BKE_REMESH_H__
+
+/** \file
+ * \ingroup bke
+ */
+
+#include "DNA_mesh_types.h"
+
+#include "openvdb_capi.h"
+
+/* OpenVDB Voxel Remesher */
+struct OpenVDBLevelSet *BKE_remesh_voxel_ovdb_mesh_to_level_set_create(Mesh *mesh, struct OpenVDBTransform *transform);
+Mesh *BKE_remesh_voxel_ovdb_volume_to_mesh_nomain(struct OpenVDBLevelSet *level_set, double isovalue, double adaptivity,
+												  bool relax_disoriented_triangles);
+
+/* Reprojection */
+void BKE_remesh_voxel_init_empty_vertex_color_layer(Mesh *mesh);
+void BKE_remesh_voxel_reproject_vertex_paint(Mesh *target, Mesh *source);
+
+#endif /* __BKE_REMESH_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index c882be972f5..86b403d1f1f 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -220,6 +220,7 @@ set(SRC
 	intern/workspace.c
 	intern/world.c
 	intern/writeavi.c
+	intern/remesh_voxel.c
 
 	BKE_DerivedMesh.h
 	BKE_action.h
@@ -341,6 +342,7 @@ set(SRC
 	BKE_workspace.h
 	BKE_world.h
 	BKE_writeavi.h
+	BKE_remesh.h
 
 	nla_private.h
 	particle_private.h
diff --git a/source/blender/blenkernel/intern/remesh_voxel.c b/source/blender/blenkernel/intern/remesh_voxel.c
new file mode 100644
index 00000000000..78de777493f
--- /dev/null
+++ b/source/blender/blenkernel/intern/remesh_voxel.c
@@ -0,0 +1,152 @@
+/*
+ * 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) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+#include <float.h>
+#include <ctype.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_math.h"
+#include "BLI_utildefines.h"
+
+#include "DNA_object_types.h"
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+
+#include "BKE_mesh.h"
+#include "BKE_mesh_runtime.h"
+#include "BKE_library.h"
+#include "BKE_customdata.h"
+#include "BKE_bvhutils.h"
+#include "BKE_remesh.h"
+
+#ifdef WITH_OPENVDB
+	#include "openvdb_capi.h"
+#endif
+
+
+struct OpenVDBLevelSet *BKE_remesh_voxel_ovdb_mesh_to_level_set_create(Mesh *mesh, struct OpenVDBTransform *transform)
+{
+	BKE_mesh_runtime_looptri_recalc(mesh);
+	const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh);
+	MVertTri *verttri = MEM_callocN(sizeof(*verttri) * BKE_mesh_runtime_looptri_len(mesh), "remesh_looptri");
+	BKE_mesh_runtime_verttri_from_looptri(verttri, mesh->mloop, looptri, BKE_mesh_runtime_looptri_len(mesh));
+
+	int totfaces = BKE_mesh_runtime_looptri_len(mesh);
+	unsigned int totverts = mesh->totvert;
+	float *verts = (float *)MEM_calloc_arrayN(totverts * 3, sizeof(float), "remesh_input_verts");
+	unsigned int *faces = (unsigned int *)MEM_calloc_arrayN(totfaces * 3, sizeof(unsigned int), "remesh_intput_faces");
+
+	for(int i = 0; i < totverts; i++) {
+		MVert mvert = mesh->mvert[i];
+		verts[i * 3] = mvert.co[0];
+		verts[i * 3 + 1] = mvert.co[1];
+		verts[i * 3 + 2] = mvert.co[2];
+	}
+
+	for(int i = 0; i < totfaces; i++) {
+		MVertTri vt = verttri[i];
+		faces[i * 3] = vt.tri[0];
+		faces[i * 3 + 1] = vt.tri[1];
+		faces[i * 3 + 2] = vt.tri[2];
+	}
+
+	struct OpenVDBLevelSet *level_set = OpenVDBLevelSet_create(false, NULL);
+	OpenVDBLevelSet_mesh_to_level_set(level_set, verts, faces, totverts, totfaces, transform);
+
+	MEM_freeN(verts);
+	MEM_freeN(faces);
+	MEM_freeN(verttri);
+
+	return level_set;
+}
+
+Mesh *BKE_remesh_voxel_ovdb_volume_to_mesh_nomain(struct OpenVDBLevelSet *level_set, double isovalue, double adaptivity,
+												  bool relax_disoriented_triangles)
+{
+	struct OpenVDBVolumeToMeshData output_mesh;
+	OpenVDBLevelSet_volume_to_mesh(level_set, &output_mesh, isovalue, 0.0, false);
+
+	Mesh *mesh = BKE_mesh_new_nomain(output_mesh.totvertices, 0, output_mesh.totquads, 0, 0);
+
+	for(int i = 0; i < output_mesh.totvertices; i++) {
+		float vco[3] = { output_mesh.vertices[i * 3], output_mesh.vertices[i * 3 + 1], output_mesh.vertices[i * 3 + 2]};
+		copy_v3_v3(mesh->mvert[i].co, vco);
+	}
+
+	for(int i = 0; i < output_mesh.totquads; i++) {
+		mesh->mface[i].v4 = output_mesh.quads[i * 4];
+		mesh->mface[i].v3 = output_mesh.quads[i * 4 + 1];
+		mesh->mface[i].v2 = output_mesh.quads[i * 4 + 2];
+		mesh->mface[i].v1 = output_mesh.quads[i * 4 + 3];
+	}
+
+	BKE_mesh_calc_edges_tessface(mesh);
+	BKE_mesh_convert_mfaces_to_mpolys(mesh);
+	BKE_mesh_calc_normals(mesh);
+
+	MEM_freeN(output_mesh.quads);
+	MEM_freeN(output_mesh.vertices);
+
+	return mesh;
+}
+
+void BKE_remesh_voxel_init_empty_vertex_color_layer(Mesh *mesh)
+{
+	MVertCol *color = CustomData_add_layer_named(&mesh->vdata, CD_MVERTCOL, CD_CALLOC, NULL, mesh->totvert, "vcols");
+	for (int i = 0; i < mesh->totvert; i++) {
+		color[i].r = 255;
+		color[i].g = 255;
+		color[i].b = 255;
+		color[i].a = 255;
+	}
+}
+
+void BKE_remesh_voxel_reproject_vertex_paint(Mesh *target, Mesh *source)
+{
+	BVHTreeFromMesh bvhtree = {NULL};
+	BKE_bvhtree_from_mesh_get(&bvhtree, source, BVHTREE_FROM_VERTS, 2);
+	MVertCol *target_color = CustomData_get_layer(&target->vdata, CD_MVERTCOL);
+	MVert *target_verts =  CustomData_get_layer(&target->vdata, CD_MVERT);
+	MVertCol *source_color  = CustomData_get_layer(&source->vdata, CD_MVERTCOL);
+	for(int i = 0; i < target->totvert; i++) {
+		float from_co[3];
+		BVHTreeNearest nearest;
+		nearest.index = -1;
+		nearest.dist_sq = FLT_MAX;
+		copy_v3_v3(from_co, target_verts[i].co);
+		BLI_bvhtree_find_nearest(bvhtree.tree, from_co, &nearest, bvhtree.nearest_callback, &bvhtree);
+		if (nearest.index != -1) {
+			target_color[i].r = source_color[nearest.index].r;
+			target_color[i].g = source_color[nearest.index].g;
+			target_color[i].b = source_color[nearest.index].b;
+			target_color[i].a = source_color[nearest.index].a;
+		}
+	}
+	free_bvhtree_from_mesh(&bvhtree);
+}
diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c
index 35e8935016d..d87327f0277 100644
--- a/source/blender/editors/object/object_edit.c
+++ b/source/blender/editors/object/object_edit.c
@@ -84,6 +84,7 @@
 #include "BKE_library.h"
 #include "BKE_customdata.h"
 #include "BKE_bvhutils.h"
+#include "BKE_remesh.h"
 
 #include "DEG_depsgraph.h"
 #include "DEG_depsgraph_build.h"
@@ -1751,6 +1752,7 @@ void OBJECT_OT_link_to_collection(wmOperatorType *ot)
 	RNA_def_property_flag(prop, PROP_SKIP_SAVE);
 }
 
+
 static int remesh_exec(bContext *C, wmOperator *op)
 {
 	bool linked_data = false;
@@ -1764,11 +1766,10 @@ static int remesh_exec(bContext *C, wmOperator *op)
 		linked_data = true;
 		return OPERATOR_CANCELLED;
 	}
-
 	if (ob->type == OB_MESH) {
 		Mesh *mesh = ob->data;
-		BVHTreeFromMesh bvhtree = {NULL};
-		Mesh *bvhMesh;
+		Mesh *newMesh;
+
 		if (mesh->voxel_size <= 0.0f) {
 			return OPERATOR_CANCELLED;
 		}
@@ -1786,98 +1787,31 @@ static int remesh_exec(bContext *C, wmOperator *op)
 			sculpt_undo_push_begin("voxel remesh");
 			sculpt_undo_push_node(ob, nodes[0], SCULPT_UNDO_REMESH);
 		}
-		BKE_mesh_runtime_looptri_recalc(mesh);
-		const MLoopTri *looptri = BKE_mesh_runtime_looptri_ensure(mesh);
-		MVertTri *verttri = MEM_callocN(sizeof(*verttri) * BKE_mesh_runtime_looptri_len(mesh), "remesh_looptri");
-		BKE_mesh_runtime_verttri_from_looptri(verttri, mesh->mloop, looptri, BKE_mesh_runtime_looptri_len(mesh));
-
-		int totfaces = BKE_mesh_runtime_looptri_len(mesh);
-		int totverts = mesh->totvert;
-		float *verts = (float *)MEM_calloc_arrayN(totverts * 3, sizeof(float), "remesh_input_verts");
-		unsigned int *faces = (unsigned int *)MEM_calloc_arrayN(totfaces * 3, sizeof(unsigned int), "remesh_intput_faces");
-		float voxel_size = mesh->voxel_size;
-		float isovalue = 0.0f;
-
-		for(int i = 0; i < totverts; i++) {
-			MVert mvert = mesh->mvert[i];
-			verts[i * 3] = mvert.co[0];
-			verts[i * 3 + 1] = mvert.co[1];
-			verts[i * 3 + 2] = mvert.co[2];
-		}
-
-		for(int i = 0; i < totfaces; i++) {
-			MVertTri vt = verttri[i];
-			faces[i * 3] = vt.tri[0];
-			faces[i * 3 + 1] = vt.tri[1];
-			faces[i * 3 + 2] = vt.tri[2];
-		}
 
+		struct OpenVDBLevelSet *level_set;
 		struct OpenVDBTransform *xform = OpenVDBTransform_create();
-		OpenVDBTransform_create_linear_transform(xform, voxel_size);
-		struct OpenVDBLevelSet *level_set = OpenVDBLevelSet_create(false, NULL);
-		struct OpenVDBVolumeToMeshData output_mesh;
-		OpenVDBLevelSet_mesh_to_level_set(level_set, verts,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list