[Bf-blender-cvs] [7df84749adf] soc-2021-porting-modifiers-to-nodes-merge-by-distance: Changes based on review bv Hans Goudey (HooglyBoogly)

Fabian Schempp noreply at git.blender.org
Fri Aug 13 08:19:07 CEST 2021


Commit: 7df84749adfe5b5b526011ea1c8e5fe190c1aae1
Author: Fabian Schempp
Date:   Fri Aug 13 08:18:58 2021 +0200
Branches: soc-2021-porting-modifiers-to-nodes-merge-by-distance
https://developer.blender.org/rB7df84749adfe5b5b526011ea1c8e5fe190c1aae1

Changes based on review bv Hans Goudey (HooglyBoogly)

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

M	source/blender/geometry/CMakeLists.txt
M	source/blender/geometry/GEO_mesh_merge_by_distance.h
A	source/blender/geometry/GEO_pointcloud_merge_by_distance.h
M	source/blender/geometry/intern/mesh_merge_by_distance.c
A	source/blender/geometry/intern/pointcloud_merge_by_distance.cc
M	source/blender/nodes/geometry/nodes/node_geo_merge_by_distance.cc

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

diff --git a/source/blender/geometry/CMakeLists.txt b/source/blender/geometry/CMakeLists.txt
index e3e78b6128a..67164aec2ed 100644
--- a/source/blender/geometry/CMakeLists.txt
+++ b/source/blender/geometry/CMakeLists.txt
@@ -24,6 +24,7 @@
          ../blenkernel
          ../blenlib
          ../blentranslation
+         ../functions
          ../makesdna
          ../makesrna
          ../../../intern/guardedalloc
@@ -33,7 +34,9 @@
 
  set(SRC
          intern/mesh_merge_by_distance.c
+         intern/pointcloud_merge_by_distance.cc
          GEO_mesh_merge_by_distance.h
+         GEO_pointcloud_merge_by_distance.h
          )
 
  set(LIB
diff --git a/source/blender/geometry/GEO_mesh_merge_by_distance.h b/source/blender/geometry/GEO_mesh_merge_by_distance.h
index 5cf34e41084..b1f5680a928 100644
--- a/source/blender/geometry/GEO_mesh_merge_by_distance.h
+++ b/source/blender/geometry/GEO_mesh_merge_by_distance.h
@@ -12,10 +12,8 @@
  * 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.
  */
+
 #pragma once
 
 /** \file
diff --git a/source/blender/geometry/GEO_mesh_merge_by_distance.h b/source/blender/geometry/GEO_pointcloud_merge_by_distance.h
similarity index 71%
copy from source/blender/geometry/GEO_mesh_merge_by_distance.h
copy to source/blender/geometry/GEO_pointcloud_merge_by_distance.h
index 5cf34e41084..c03cc08d814 100644
--- a/source/blender/geometry/GEO_mesh_merge_by_distance.h
+++ b/source/blender/geometry/GEO_pointcloud_merge_by_distance.h
@@ -12,10 +12,8 @@
  * 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.
  */
+
 #pragma once
 
 /** \file
@@ -26,15 +24,9 @@
 extern "C" {
 #endif
 
-enum {
-  WELD_MODE_ALL = 0,
-  WELD_MODE_CONNECTED = 1,
-};
-
-struct Mesh *GEO_mesh_merge_by_distance(const struct Mesh *mesh,
-               const bool *mask,
-               const float merge_distance,
-               const int weld_mode);
+PointCloud *merge_by_distance_pointcloud(const PointCloud &point_cloud,
+                                         const float merge_threshold,
+                                         blender::Span<bool> selection);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/geometry/intern/mesh_merge_by_distance.c b/source/blender/geometry/intern/mesh_merge_by_distance.c
index 244289063e4..1db2f44d370 100644
--- a/source/blender/geometry/intern/mesh_merge_by_distance.c
+++ b/source/blender/geometry/intern/mesh_merge_by_distance.c
@@ -1,3 +1,19 @@
+/*
+ * 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.
+ */
+
 //#define USE_WELD_DEBUG
 //#define USE_WELD_NORMALS
 //#define USE_BVHTREEKDOP
diff --git a/source/blender/geometry/intern/pointcloud_merge_by_distance.cc b/source/blender/geometry/intern/pointcloud_merge_by_distance.cc
new file mode 100644
index 00000000000..d91afbffc86
--- /dev/null
+++ b/source/blender/geometry/intern/pointcloud_merge_by_distance.cc
@@ -0,0 +1,120 @@
+/*
+ * 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.
+ */
+
+#include "BKE_pointcloud.h"
+
+#include "BLI_array.hh"
+#include "BLI_float3.hh"
+#include "BLI_kdtree.h"
+#include "BLI_span.hh"
+#include "BLI_vector.hh"
+
+#include "DNA_pointcloud_types.h"
+
+#include "GEO_pointcloud_merge_by_distance.h" /* Own include. */
+
+#include "FN_generic_span.hh"
+
+using blender::Array;
+using blender::float3;
+using blender::Span;
+using blender::Vector;
+
+static KDTree_3d *build_kdtree(Span<float3> positions, Span<bool> selection)
+{
+  BLI_assert(positions.size() == selection.size());
+
+  KDTree_3d *kdtree = BLI_kdtree_3d_new(selection.size());
+
+  for (const int i : positions.index_range()) {
+    if (selection[i]) {
+      BLI_kdtree_3d_insert(kdtree, i, positions[i]);
+    }
+  }
+
+  BLI_kdtree_3d_balance(kdtree);
+  return kdtree;
+}
+
+static void build_merge_map(Span<float3> &positions,
+                            Array<bool> &merge_map,
+                            int &total_merge_operations,
+                            const float merge_threshold,
+                            Span<bool> selection)
+{
+  KDTree_3d *kdtree = build_kdtree(positions, selection);
+
+  for (int i : positions.index_range()) {
+    struct CallbackData {
+      int index;
+      int &total_merge_operations;
+      Array<bool> &merge_map;
+      Span<bool> selection;
+    } callback_data = {i, total_merge_operations, merge_map, selection};
+
+    BLI_kdtree_3d_range_search_cb(
+        kdtree,
+        positions[i],
+        merge_threshold,
+        [](void *user_data,
+           int source_vertex_index,
+           const float *UNUSED(co),
+           float UNUSED(distance_squared)) {
+          CallbackData &callback_data = *static_cast<CallbackData *>(user_data);
+          int target_vertex_index = callback_data.index;
+          if (source_vertex_index != target_vertex_index &&
+              !callback_data.merge_map[source_vertex_index] &&
+              !callback_data.merge_map[target_vertex_index] &&
+              callback_data.selection[source_vertex_index] &&
+              callback_data.selection[target_vertex_index]) {
+            callback_data.merge_map[source_vertex_index] = true;
+            callback_data.total_merge_operations++;
+          }
+          return true;
+        },
+        &callback_data);
+  }
+
+  BLI_kdtree_3d_free(kdtree);
+}
+
+PointCloud *merge_by_distance_pointcloud(const PointCloud &point_cloud,
+                                         const float merge_threshold,
+                                         Span<bool> selection)
+{
+
+  Array<bool> merge_map(point_cloud.totpoint, false);
+  Span<float3> positions((const float3 *)point_cloud.co, point_cloud.totpoint);
+
+  int total_merge_operations = 0;
+
+  BLI_assert(positions.size() == merge_map.size());
+
+  build_merge_map(positions, merge_map, total_merge_operations, merge_threshold, selection);
+
+  PointCloud *result = BKE_pointcloud_new_nomain(positions.size() - total_merge_operations);
+  int offset = 0;
+  for (const int i : positions.index_range()) {
+    /* Only copy the unmerged points to new pointcloud. */
+    if (!merge_map[i]) {
+      copy_v3_v3(result->co[offset], positions[i]);
+      result->radius[offset] = point_cloud.radius[i];
+      offset++;
+    }
+  }
+
+  return result;
+}
\ No newline at end of file
diff --git a/source/blender/nodes/geometry/nodes/node_geo_merge_by_distance.cc b/source/blender/nodes/geometry/nodes/node_geo_merge_by_distance.cc
index 903e90723ab..500471637b3 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_merge_by_distance.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_merge_by_distance.cc
@@ -17,22 +17,19 @@
 #include "BKE_pointcloud.h"
 
 #include "BLI_float3.hh"
-#include "BLI_kdtree.h"
 #include "BLI_span.hh"
 
 #include "DNA_pointcloud_types.h"
 
 #include "GEO_mesh_merge_by_distance.h"
+#include "GEO_pointcloud_merge_by_distance.h"
 
 #include "UI_interface.h"
 #include "UI_resources.h"
 
 #include "node_geometry_util.hh"
 
-#include "FN_generic_span.hh"
-
-#include "node_geometry_util.hh"
-
+using blender::Array;
 using blender::float3;
 using blender::Span;
 using blender::Vector;
@@ -61,91 +58,6 @@ static void geo_merge_by_distance_init(bNodeTree *UNUSED(ntree), bNode *node)
   node->custom1 = WELD_MODE_ALL;
 }
 
-static KDTree_3d *build_kdtree(Span<float3> positions)
-{
-  KDTree_3d *kdtree = BLI_kdtree_3d_new(positions.size());
-
-  int i_point = 0;
-  for (const float3 position : positions) {
-    BLI_kdtree_3d_insert(kdtree, i_point, position);
-    i_point++;
-  }
-  BLI_kdtree_3d_balance(kdtree);
-  return kdtree;
-}
-
-static void build_merge_map(Vector<float3> &positions,
-                            Vector<int> &merge_map,
-                            int &total_merge_operations,
-                            const float merge_threshold,
-                            Span<bool> &selection)
-{
-  KDTree_3d *kdtree = build_kdtree(positions.as_span());
-
-  for (int i : positions.index_range()) {
-    struct CallbackData {
-      int index;
-      int &total_merge_operations;
-      Vector<int> &merge_map;
-      Span<bool> &selection;
-    } callback_data = {i, total_merge_operations, merge_map, selection};
-
-    BLI_kdtree_3d_range_search_cb(
-        kdtree,
-        positions[i],
-        merge_threshold,
-        [](void *user_data,
-           int source_vertex_index,
-           const float *UNUSED(co),
-           float UNUSED(distance_squared

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list