[Bf-blender-cvs] [9d03990e32a] master: Fix T92160: Geometry Proximity node can produce invalid values

Hans Goudey noreply at git.blender.org
Tue Oct 12 22:28:17 CEST 2021


Commit: 9d03990e32ac36a4b9c6f958e9b1bea7288b0bbc
Author: Hans Goudey
Date:   Tue Oct 12 15:28:11 2021 -0500
Branches: master
https://developer.blender.org/rB9d03990e32ac36a4b9c6f958e9b1bea7288b0bbc

Fix T92160: Geometry Proximity node can produce invalid values

Check when the node fails to create BVH trees, and fill the result
with zero in that case, which is most likely the expected value when
the node encounters an error. Warnings will be added with a separate
patch.

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

M	source/blender/nodes/geometry/nodes/node_geo_proximity.cc

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

diff --git a/source/blender/nodes/geometry/nodes/node_geo_proximity.cc b/source/blender/nodes/geometry/nodes/node_geo_proximity.cc
index 9f357ce2b1c..30d025953af 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_proximity.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_proximity.cc
@@ -50,7 +50,7 @@ static void geo_proximity_init(bNodeTree *UNUSED(ntree), bNode *node)
   node->storage = node_storage;
 }
 
-static void calculate_mesh_proximity(const VArray<float3> &positions,
+static bool calculate_mesh_proximity(const VArray<float3> &positions,
                                      const IndexMask mask,
                                      const Mesh &mesh,
                                      const GeometryNodeProximityTargetType type,
@@ -71,7 +71,7 @@ static void calculate_mesh_proximity(const VArray<float3> &positions,
   }
 
   if (bvh_data.tree == nullptr) {
-    return;
+    return false;
   }
 
   threading::parallel_for(mask.index_range(), 512, [&](IndexRange range) {
@@ -97,18 +97,19 @@ static void calculate_mesh_proximity(const VArray<float3> &positions,
   });
 
   free_bvhtree_from_mesh(&bvh_data);
+  return true;
 }
 
-static void calculate_pointcloud_proximity(const VArray<float3> &positions,
+static bool calculate_pointcloud_proximity(const VArray<float3> &positions,
                                            const IndexMask mask,
                                            const PointCloud &pointcloud,
-                                           const MutableSpan<float> r_distances,
-                                           const MutableSpan<float3> r_locations)
+                                           MutableSpan<float> r_distances,
+                                           MutableSpan<float3> r_locations)
 {
   BVHTreeFromPointCloud bvh_data;
   BKE_bvhtree_from_pointcloud_get(&bvh_data, &pointcloud, 2);
   if (bvh_data.tree == nullptr) {
-    return;
+    return false;
   }
 
   threading::parallel_for(mask.index_range(), 512, [&](IndexRange range) {
@@ -136,6 +137,7 @@ static void calculate_pointcloud_proximity(const VArray<float3> &positions,
   });
 
   free_bvhtree_from_pointcloud(&bvh_data);
+  return true;
 }
 
 class ProximityFunction : public fn::MultiFunction {
@@ -174,16 +176,23 @@ class ProximityFunction : public fn::MultiFunction {
 
     distances.fill(FLT_MAX);
 
+    bool success = false;
     if (target_.has_mesh()) {
-      calculate_mesh_proximity(
+      success |= calculate_mesh_proximity(
           src_positions, mask, *target_.get_mesh_for_read(), type_, distances, positions);
     }
 
     if (target_.has_pointcloud() && type_ == GEO_NODE_PROX_TARGET_POINTS) {
-      calculate_pointcloud_proximity(
+      success |= calculate_pointcloud_proximity(
           src_positions, mask, *target_.get_pointcloud_for_read(), distances, positions);
     }
 
+    if (!success) {
+      positions.fill(float3(0));
+      distances.fill(0.0f);
+      return;
+    }
+
     if (params.single_output_is_required(2, "Distance")) {
       threading::parallel_for(mask.index_range(), 2048, [&](IndexRange range) {
         for (const int i : range) {
@@ -199,10 +208,13 @@ static void geo_node_proximity_exec(GeoNodeExecParams params)
 {
   GeometrySet geometry_set_target = params.extract_input<GeometrySet>("Target");
 
-  if (!geometry_set_target.has_mesh() && !geometry_set_target.has_pointcloud()) {
+  auto return_default = [&]() {
     params.set_output("Position", fn::make_constant_field<float3>({0.0f, 0.0f, 0.0f}));
     params.set_output("Distance", fn::make_constant_field<float>(0.0f));
-    return;
+  };
+
+  if (!geometry_set_target.has_mesh() && !geometry_set_target.has_pointcloud()) {
+    return return_default();
   }
 
   const NodeGeometryProximity &storage = *(const NodeGeometryProximity *)params.node().storage;



More information about the Bf-blender-cvs mailing list