Commit: 945a75290299794f941bcc25e5f1bc341270e4b5
Author: Jacques Lucke
Date:   Wed Nov 27 18:15:48 2019 +0100
Branches: functions
https://developer.blender.org/rB945a75290299794f941bcc25e5f1bc341270e4b5

update closest location on object node

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

M       release/scripts/startup/nodes/function_nodes/object_mesh.py
M       source/blender/blenkernel/BKE_surface_location.h
M       source/blender/blenkernel/CMakeLists.txt
A       source/blender/blenkernel/intern/surface_location.cc
M       source/blender/functions/intern/cpp_types.cc
M       
source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
M       
source/blender/functions/intern/inlined_tree_multi_function_network/mappings_sockets.cc
M       source/blender/functions/intern/multi_functions/mixed.cc
M       source/blender/functions/intern/multi_functions/mixed.h

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

diff --git a/release/scripts/startup/nodes/function_nodes/object_mesh.py 
b/release/scripts/startup/nodes/function_nodes/object_mesh.py
index a42ac8f0a15..367260a7f3c 100644
--- a/release/scripts/startup/nodes/function_nodes/object_mesh.py
+++ b/release/scripts/startup/nodes/function_nodes/object_mesh.py
@@ -19,11 +19,11 @@ class VertexInfo(bpy.types.Node, FunctionNode):
         builder.fixed_output("position", "Position", "Vector")
 
 
-class ClosestPointOnObjectNode(bpy.types.Node, FunctionNode):
-    bl_idname = "fn_ClosestPointOnObjectNode"
-    bl_label = "Closest Point on Object"
+class ClosestLocationOnObjectNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_ClosestLocationOnObjectNode"
+    bl_label = "Closest Location on Object"
 
     def declaration(self, builder):
         builder.fixed_input("object", "Object", "Object")
         builder.fixed_input("position", "Position", "Vector")
-        builder.fixed_output("closest_point", "Closest Point", "Vector")
+        builder.fixed_output("closest_point", "Closest Location", "Surface 
Location")
diff --git a/source/blender/blenkernel/BKE_surface_location.h 
b/source/blender/blenkernel/BKE_surface_location.h
index 9093ca0779e..a56087af078 100644
--- a/source/blender/blenkernel/BKE_surface_location.h
+++ b/source/blender/blenkernel/BKE_surface_location.h
@@ -2,10 +2,15 @@
 #define __BKE_SURFACE_LOCATION_H__
 
 #include "BLI_utildefines.h"
+#include "BLI_math_cxx.h"
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include "DNA_object_types.h"
+
+#include "BLI_hash.h"
+
+namespace BKE {
+
+using BLI::float3;
 
 /**
  * References a point on a surface. If the surface moves, the point moves with 
it. The surface is
@@ -13,7 +18,7 @@ extern "C" {
  *
  * For now, only points on triangle meshes are supported, support for curves 
could be added too.
  */
-typedef struct SurfaceLocation {
+struct SurfaceLocation {
   /**
    * Identifies the surface that is being referenced. This is usually a hash 
of the name of an
    * object. The location is invalid, if this id is negative.
@@ -24,12 +29,26 @@ typedef struct SurfaceLocation {
   uint32_t triangle_index;
 
   /* Barycentric coordinates of the referenced location inside the triangle. */
-  float weight1, weight2, weight3;
+  float3 bary_coords;
+
+  SurfaceLocation() : surface_id(-1)
+  {
+  }
+
+  SurfaceLocation(int32_t surface_id, uint32_t triangle_index, float3 
bary_coords)
+      : surface_id(surface_id), triangle_index(triangle_index), 
bary_coords(bary_coords)
+  {
+  }
+
+  static int32_t ComputeObjectSurfaceID(const Object *ob)
+  {
+    BLI_assert(ob != nullptr);
 
-} SurfaceLocation;
+    /* Set the highest bit to zero, to make the number positive. */
+    return BLI_hash_string(ob->id.name) & ~(1 << 31);
+  }
+};
 
-#ifdef __cplusplus
-}
-#endif
+}  // namespace BKE
 
 #endif /* __BKE_SURFACE_LOCATION_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt 
b/source/blender/blenkernel/CMakeLists.txt
index 01120ea369c..e550fb94757 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -218,6 +218,7 @@ set(SRC
   intern/subdiv_stats.c
   intern/subdiv_topology.c
   intern/subsurf_ccg.c
+  intern/surface_location.cc
   intern/text.c
   intern/text_suggestions.c
   intern/texture.c
diff --git a/source/blender/blenkernel/intern/surface_location.cc 
b/source/blender/blenkernel/intern/surface_location.cc
new file mode 100644
index 00000000000..2588c19c30b
--- /dev/null
+++ b/source/blender/blenkernel/intern/surface_location.cc
@@ -0,0 +1,7 @@
+#include "BKE_surface_location.h"
+
+#include "BLI_hash.h"
+
+namespace BKE {
+
+}  // namespace BKE
diff --git a/source/blender/functions/intern/cpp_types.cc 
b/source/blender/functions/intern/cpp_types.cc
index 55838287076..95f13090e23 100644
--- a/source/blender/functions/intern/cpp_types.cc
+++ b/source/blender/functions/intern/cpp_types.cc
@@ -101,6 +101,6 @@ MAKE_CPP_TYPE(int32, int32_t)
 MAKE_CPP_TYPE(rgba_f, BLI::rgba_f)
 MAKE_CPP_TYPE(float3, BLI::float3)
 MAKE_CPP_TYPE(string, std::string)
-MAKE_CPP_TYPE(SurfaceLocation, SurfaceLocation)
+MAKE_CPP_TYPE(SurfaceLocation, BKE::SurfaceLocation)
 
 }  // namespace FN
diff --git 
a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
 
b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
index f3990173ce1..741fbb3ec82 100644
--- 
a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
+++ 
b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
@@ -329,9 +329,9 @@ static void INSERT_particle_info(VNodeMFNetworkBuilder 
&builder)
   }
 }
 
-static void INSERT_closest_point_on_object(VNodeMFNetworkBuilder &builder)
+static void INSERT_closest_location_on_object(VNodeMFNetworkBuilder &builder)
 {
-  builder.set_constructed_matching_fn<MF_ClosestPointOnObject>();
+  builder.set_constructed_matching_fn<MF_ClosestLocationOnObject>();
 }
 
 static void INSERT_clamp_float(VNodeMFNetworkBuilder &builder)
@@ -369,7 +369,8 @@ void 
add_inlined_tree_node_mapping_info(VTreeMultiFunctionMappings &mappings)
   mappings.xnode_inserters.add_new("fn_CompareNode", INSERT_compare);
   mappings.xnode_inserters.add_new("fn_PerlinNoiseNode", INSERT_perlin_noise);
   mappings.xnode_inserters.add_new("fn_ParticleInfoNode", 
INSERT_particle_info);
-  mappings.xnode_inserters.add_new("fn_ClosestPointOnObjectNode", 
INSERT_closest_point_on_object);
+  mappings.xnode_inserters.add_new("fn_ClosestLocationOnObjectNode",
+                                   INSERT_closest_location_on_object);
   mappings.xnode_inserters.add_new("fn_MapRangeNode", INSERT_map_range);
   mappings.xnode_inserters.add_new("fn_FloatClampNode", INSERT_clamp_float);
   mappings.xnode_inserters.add_new("fn_RandomFloatNode", INSERT_random_float);
diff --git 
a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_sockets.cc
 
b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_sockets.cc
index 883f57e9b87..8f3d0c69999 100644
--- 
a/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_sockets.cc
+++ 
b/source/blender/functions/intern/inlined_tree_multi_function_network/mappings_sockets.cc
@@ -60,9 +60,7 @@ static void INSERT_text_socket(VSocketMFNetworkBuilder 
&builder)
 
 static void INSERT_surface_location_socket(VSocketMFNetworkBuilder &builder)
 {
-  SurfaceLocation location;
-  location.surface_id = -1;
-  builder.set_constant_value(location);
+  builder.set_constant_value(BKE::SurfaceLocation());
 }
 
 template<typename T> static void 
INSERT_empty_list_socket(VSocketMFNetworkBuilder &builder)
@@ -164,7 +162,7 @@ void 
add_inlined_tree_socket_mapping_info(VTreeMultiFunctionMappings &mappings)
   add_basic_type<std::string>(mappings, "Text", INSERT_text_socket);
   add_basic_type<bool>(mappings, "Boolean", INSERT_bool_socket);
   add_basic_type<BLI::rgba_f>(mappings, "Color", INSERT_color_socket);
-  add_basic_type<SurfaceLocation>(
+  add_basic_type<BKE::SurfaceLocation>(
       mappings, "Surface Location", "SurfaceLocation", 
INSERT_surface_location_socket);
 
   add_bidirectional_implicit_conversion<float, int32_t>(mappings);
diff --git a/source/blender/functions/intern/multi_functions/mixed.cc 
b/source/blender/functions/intern/multi_functions/mixed.cc
index e0912f38b13..21a064f4f03 100644
--- a/source/blender/functions/intern/multi_functions/mixed.cc
+++ b/source/blender/functions/intern/multi_functions/mixed.cc
@@ -15,8 +15,12 @@
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 
+#include "BKE_surface_location.h"
+#include "BKE_mesh_runtime.h"
+
 namespace FN {
 
+using BKE::SurfaceLocation;
 using BLI::float3;
 using BLI::float4x4;
 using BLI::rgba_f;
@@ -597,12 +601,12 @@ void MF_ParticleAttributes::call(MFMask mask, MFParams 
params, MFContext context
   }
 }
 
-MF_ClosestPointOnObject::MF_ClosestPointOnObject()
+MF_ClosestLocationOnObject::MF_ClosestLocationOnObject()
 {
   MFSignatureBuilder signature("Closest Point on Object");
   signature.single_input<Object *>("Object");
   signature.single_input<float3>("Position");
-  signature.single_output<float3>("Closest Point");
+  signature.single_output<SurfaceLocation>("Closest Location");
   this->set_signature(signature);
 }
 
@@ -616,66 +620,90 @@ static BVHTreeNearest get_nearest_point(BVHTreeFromMesh 
*bvhtree_data, float3 po
   return nearest;
 }
 
-void MF_ClosestPointOnObject::call(MFMask mask, MFParams params, MFContext 
context) const
+static float3 get_barycentric_coords(Mesh *mesh,
+                                     const MLoopTri *triangles,
+                                     float3 position,
+                                     uint triangle_index)
+{
+  const MLoopTri &triangle = triangles[triangle_index];
+
+  float3 v1 = mesh->mvert[mesh->mloop[triangle.tri[0]].v].co;
+  float3 v2 = mesh->mvert[mesh->mloop[triangle.tri[1]].v].co;
+  float3 v3 = mesh->mvert[mesh->mloop[triangle.tri[2]].v].co;
+
+  float3 weights;
+  interp_weights_tri_v3(weights, v1, v2, v3, position);
+  return weights;
+}
+
+void MF_ClosestLocationOnObject::call(MFMask mask, MFParams params, MFContext 
context) const
 {
   auto context_data = 
context.element_contexts().find_first<ExternalDataCacheContext>();
 
   VirtualListRef<Object *> objects = params.readonly_single_input<Object *>(0, 
"Object");
   VirtualListRef<float3> positions = params.readonly_single_input<float3>(1, 
"Position");
-  MutableArrayRef<float3> r_points = 
params.uninitialized_single_output<float3>(2,
-                                                                               
 "Closest Point");
+  MutableArrayRef<SurfaceLocation> r_surface_locations =
+      params.uninitialized_single_output<SurfaceLocation>(2, "Closest Point");
 
   if (!context_data.has_value()) {
-    r_points.fill_indices(mask.indices(), {0, 0, 0});
+    r_surface_locations.fill_indices(mask.indices(), {});
     return;
   }
 
   if (mask.indices().size() > 0 && objects.all_equal(mask.indices())) {
     Object *object = objects[mask.indices()[0]];
     if (object == nullptr) {
-      r_points.fill_indices(mask.indices(), {0, 0, 0});
+      r_surface_locations.fill

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to