[Bf-blender-cvs] [4de6eafba3f] functions: sample hooks on surface

Jacques Lucke noreply at git.blender.org
Thu Dec 12 13:05:12 CET 2019


Commit: 4de6eafba3fc4e82a001aa9306f78960aa2ee0d8
Author: Jacques Lucke
Date:   Thu Dec 12 13:05:08 2019 +0100
Branches: functions
https://developer.blender.org/rB4de6eafba3fc4e82a001aa9306f78960aa2ee0d8

sample hooks on surface

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

M	release/scripts/startup/nodes/function_nodes/object_mesh.py
M	source/blender/blenlib/BLI_array_ref.h
M	source/blender/blenlib/BLI_rand.h
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_cpp_type.h
M	source/blender/functions/FN_generic_vector_array.h
M	source/blender/functions/intern/cpp_types.cc
M	source/blender/functions/intern/inlined_tree_multi_function_network/mappings_nodes.cc
A	source/blender/functions/intern/multi_functions/sampling_util.cc
A	source/blender/functions/intern/multi_functions/sampling_util.h
M	source/blender/functions/intern/multi_functions/surface_hook.cc
M	source/blender/functions/intern/multi_functions/surface_hook.h

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

diff --git a/release/scripts/startup/nodes/function_nodes/object_mesh.py b/release/scripts/startup/nodes/function_nodes/object_mesh.py
index 0ab7c4d3f38..4399378833a 100644
--- a/release/scripts/startup/nodes/function_nodes/object_mesh.py
+++ b/release/scripts/startup/nodes/function_nodes/object_mesh.py
@@ -83,3 +83,14 @@ class GetImageColorOnSurfaceNode(bpy.types.Node, FunctionNode):
         builder.vectorized_input("surface_hook", "use_list__surface_hook", "Surface Hook", "Surface Hooks", "Surface Hook")
         builder.vectorized_input("image", "use_list__image", "Image", "Images", "Image")
         builder.vectorized_output("color", ["use_list__surface_hook", "use_list__image"], "Color", "Colors", "Color")
+
+
+class SampleObjectSurfaceNode(bpy.types.Node, FunctionNode):
+    bl_idname = "fn_SampleObjectSurfaceNode"
+    bl_label = "Sample Object Surface"
+
+    def declaration(self, builder: NodeBuilder):
+        builder.fixed_input("object", "Object", "Object")
+        builder.fixed_input("amount", "Amount", "Integer", default=10)
+        builder.fixed_input("seed", "Seed", "Integer")
+        builder.fixed_output("surface_hooks", "Surface Hooks", "Surface Hook List")
diff --git a/source/blender/blenlib/BLI_array_ref.h b/source/blender/blenlib/BLI_array_ref.h
index 53769d02e81..5af421066aa 100644
--- a/source/blender/blenlib/BLI_array_ref.h
+++ b/source/blender/blenlib/BLI_array_ref.h
@@ -483,6 +483,12 @@ template<typename T> class MutableArrayRef {
   {
     return IndexRange(m_size);
   }
+
+  const T &last() const
+  {
+    BLI_assert(m_size > 0);
+    return m_start[m_size - 1];
+  }
 };
 
 /**
diff --git a/source/blender/blenlib/BLI_rand.h b/source/blender/blenlib/BLI_rand.h
index be2b18b05fd..ad8a90b3977 100644
--- a/source/blender/blenlib/BLI_rand.h
+++ b/source/blender/blenlib/BLI_rand.h
@@ -20,6 +20,8 @@
 #ifndef __BLI_RAND_H__
 #define __BLI_RAND_H__
 
+#include "BLI_compiler_attrs.h"
+
 /** \file
  * \ingroup bli
  * \brief Random number functions.
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index f1e6f2a3d79..989f38b6de8 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -27,6 +27,7 @@ set(SRC
   intern/multi_functions/mixed.cc
   intern/multi_functions/network.cc
   intern/multi_functions/particles.cc
+  intern/multi_functions/sampling_util.cc
   intern/multi_functions/surface_hook.cc
   intern/multi_functions/vectorize.cc
   intern/inlined_tree_multi_function_network/builder.cc
@@ -71,6 +72,7 @@ set(SRC
   intern/multi_functions/mixed.h
   intern/multi_functions/network.h
   intern/multi_functions/particles.h
+  intern/multi_functions/sampling_util.h
   intern/multi_functions/surface_hook.h
   intern/multi_functions/util.h
   intern/multi_functions/vectorize.h
diff --git a/source/blender/functions/FN_cpp_type.h b/source/blender/functions/FN_cpp_type.h
index 0be74e4b316..6e53a2d5354 100644
--- a/source/blender/functions/FN_cpp_type.h
+++ b/source/blender/functions/FN_cpp_type.h
@@ -12,7 +12,8 @@ using BLI::StringRefNull;
 
 class CPPType {
  public:
-  using ConstructDefaultF = void (*)(const CPPType *self, void *ptr);
+  using ConstructDefaultF = void (*)(void *ptr);
+  using ConstructDefaultNF = void (*)(void *ptr, uint n);
   using DestructF = void (*)(void *ptr);
   using DestructNF = void (*)(void *ptr, uint n);
   using CopyToInitializedF = void (*)(const void *src, void *dst);
@@ -26,6 +27,7 @@ class CPPType {
           uint alignment,
           bool trivially_destructible,
           ConstructDefaultF construct_default,
+          ConstructDefaultNF construct_default_n,
           DestructF destruct,
           DestructNF destruct_n,
           CopyToInitializedF copy_to_initialized,
@@ -38,6 +40,7 @@ class CPPType {
         m_alignment(alignment),
         m_trivially_destructible(trivially_destructible),
         m_construct_default(construct_default),
+        m_construct_default_n(construct_default_n),
         m_destruct(destruct),
         m_destruct_n(destruct_n),
         m_copy_to_initialized(copy_to_initialized),
@@ -91,7 +94,14 @@ class CPPType {
   {
     BLI_assert(this->pointer_has_valid_alignment(ptr));
 
-    m_construct_default(this, ptr);
+    m_construct_default(ptr);
+  }
+
+  void construct_default_n(void *ptr, uint n) const
+  {
+    BLI_assert(this->pointer_has_valid_alignment(ptr));
+
+    m_construct_default_n(ptr, n);
   }
 
   void destruct(void *ptr) const
@@ -175,6 +185,7 @@ class CPPType {
   uint m_alignment_mask;
   bool m_trivially_destructible;
   ConstructDefaultF m_construct_default;
+  ConstructDefaultNF m_construct_default_n;
   DestructF m_destruct;
   DestructNF m_destruct_n;
   CopyToInitializedF m_copy_to_initialized;
diff --git a/source/blender/functions/FN_generic_vector_array.h b/source/blender/functions/FN_generic_vector_array.h
index 62e5ca2015c..298f879c67a 100644
--- a/source/blender/functions/FN_generic_vector_array.h
+++ b/source/blender/functions/FN_generic_vector_array.h
@@ -158,6 +158,13 @@ class GenericVectorArray : BLI::NonCopyable, BLI::NonMovable {
         this->append_single(index, value);
       }
     }
+
+    MutableArrayRef<T> allocate_and_default_construct(uint index, uint amount)
+    {
+      GenericMutableArrayRef array = m_data->allocate_single(index, amount);
+      m_data->type().construct_default_n(array.buffer(), amount);
+      return array.as_typed_ref<T>();
+    }
   };
 
   template<typename T> const TypedRef<T> as_typed_ref() const
diff --git a/source/blender/functions/intern/cpp_types.cc b/source/blender/functions/intern/cpp_types.cc
index 7d37630b109..2e639030bfd 100644
--- a/source/blender/functions/intern/cpp_types.cc
+++ b/source/blender/functions/intern/cpp_types.cc
@@ -15,24 +15,17 @@ void free_cpp_types()
 {
 }
 
-template<typename T> void ConstructDefault_CB(const CPPType *UNUSED(self), void *ptr)
+template<typename T> void ConstructDefault_CB(void *ptr)
 {
   BLI::construct_default((T *)ptr);
 }
 
-template<typename T, bool IsDefaultConstructible> struct DefaultConstructor;
-template<typename T> struct DefaultConstructor<T, true> {
-  static CPPType::ConstructDefaultF get_callback()
-  {
-    return ConstructDefault_CB<T>;
-  }
-};
-template<typename T> struct DefaultConstructor<T, false> {
-  static CPPType::ConstructDefaultF get_callback()
-  {
-    return nullptr;
+template<typename T> void ConstructDefaultN_CB(void *ptr, uint n)
+{
+  for (uint i = 0; i < n; i++) {
+    BLI::construct_default((T *)ptr + i);
   }
-};
+}
 
 template<typename T> void Destruct_CB(void *ptr)
 {
@@ -65,20 +58,20 @@ template<typename T> void RelocateToUninitializedN_CB(void *src, void *dst, uint
 
 template<typename T> static std::unique_ptr<const CPPType> create_cpp_type(StringRef name)
 {
-  const CPPType *type = new CPPType(
-      name,
-      sizeof(T),
-      alignof(T),
-      std::is_trivially_destructible<T>::value,
-      DefaultConstructor<T, std::is_default_constructible<T>::value>::get_callback(),
-      Destruct_CB<T>,
-      DestructN_CB<T>,
-      CopyToInitialized_CB<T>,
-      CopyToUninitialized_CB<T>,
-      RelocateToInitialized_CB<T>,
-      RelocateToUninitialized_CB<T>,
-      RelocateToUninitializedN_CB<T>,
-      nullptr);
+  const CPPType *type = new CPPType(name,
+                                    sizeof(T),
+                                    alignof(T),
+                                    std::is_trivially_destructible<T>::value,
+                                    ConstructDefault_CB<T>,
+                                    ConstructDefaultN_CB<T>,
+                                    Destruct_CB<T>,
+                                    DestructN_CB<T>,
+                                    CopyToInitialized_CB<T>,
+                                    CopyToUninitialized_CB<T>,
+                                    RelocateToInitialized_CB<T>,
+                                    RelocateToUninitialized_CB<T>,
+                                    RelocateToUninitializedN_CB<T>,
+                                    nullptr);
   return std::unique_ptr<const CPPType>(type);
 }
 
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 b109a716d33..6b0370d9fb5 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
@@ -481,6 +481,11 @@ static void INSERT_emitter_time_info(VNodeMFNetworkBuilder &builder)
   builder.set_constructed_matching_fn<MF_EmitterTimeInfo>();
 }
 
+static void INSERT_sample_object_surface(VNodeMFNetworkBuilder &builder)
+{
+  builder.set_constructed_matching_fn<MF_SampleObjectSurface>();
+}
+
 void add_inlined_tree_node_mapping_info(VTreeMultiFunctionMappings &mappings)
 {
   mappings.xnode_inserters.add_new("fn_CombineColorNode", INSERT_combine_color);
@@ -514,6 +519,7 @@ void add_inlined_tree_node_mapping_info(VTreeMultiFunctionMappings &mappings)
   mappings.xnode_inserters.add_new("fn_RandomFloatNode", INSERT_random_float);
   mappings.xnode_inserters.add_new("fn_ValueNode", INSERT_value);
   mappings.xnode_inserters.add_new("fn_EmitterTimeInfoNode", INSERT_emitter_time_info);
+  mappings.xnode_inserters.add_new("fn_SampleObjectSurfaceNode", INSERT_sample_object_surface);
 
   mappings.xnode_inserters.add_new("fn_AddFloatsNode", INSERT_add_floats);
   mappings.xnode_inserters.add_new("fn_MultiplyFloatsNode", INSERT_multiply_floats);
diff --git a/source/blender/functions/intern/multi_functions/sampling_util.cc b/source/blender/functions/intern/multi_functions/sampling_util.cc
new file mode 100644
index 00000000000..c7fac9e74ee
--- /dev/null
+++ b/source/blender/functions/intern/multi_functions/sampling_util.cc
@@ -0,0 +1,89 @@
+#include "sampling_util.h"
+
+#include "BLI_vector_adaptor.h"
+
+namespace FN {
+
+using BLI::VectorAdaptor;
+
+float compute_cumulative_distribution(ArrayRef<float> weights,
+                                      MutableArrayRef<float> r_cumulative_weights)
+{
+  BLI_assert(weights.size() + 1 == r_c

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list