[Bf-blender-cvs] [907f72d4bc7] functions: initial IDHandle implementation

Jacques Lucke noreply at git.blender.org
Wed Dec 4 14:28:15 CET 2019


Commit: 907f72d4bc7743991187a48b405f9f1ea67621e3
Author: Jacques Lucke
Date:   Wed Dec 4 13:21:05 2019 +0100
Branches: functions
https://developer.blender.org/rB907f72d4bc7743991187a48b405f9f1ea67621e3

initial IDHandle implementation

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

A	source/blender/blenkernel/BKE_id_handle.h
M	source/blender/blenkernel/BKE_surface_location.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/id_handle.cc
M	source/blender/functions/FN_multi_function_common_contexts.h
M	source/blender/functions/FN_multi_function_context.h
M	source/blender/functions/intern/multi_functions/mixed.cc
M	source/blender/simulations/bparticles/node_frontend.cpp
M	source/blender/simulations/bparticles/particle_function.cpp
M	source/blender/simulations/bparticles/particle_function.hpp

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

diff --git a/source/blender/blenkernel/BKE_id_handle.h b/source/blender/blenkernel/BKE_id_handle.h
new file mode 100644
index 00000000000..ce45ccb1eb3
--- /dev/null
+++ b/source/blender/blenkernel/BKE_id_handle.h
@@ -0,0 +1,108 @@
+#ifndef __BKE_ID_HANDLE_H__
+#define __BKE_ID_HANDLE_H__
+
+#include "BLI_utildefines.h"
+
+#include "BLI_map.h"
+
+extern "C" {
+struct ID;
+struct Object;
+struct Image;
+}
+
+namespace BKE {
+
+using BLI::Map;
+
+/**
+ * This is a weak reference to an ID data-block. It does not contain a pointer to the actual data.
+ * It can happen that the IDHandle references data, that does not exist anymore. The handle does
+ * not know that.
+ */
+class IDHandle {
+ private:
+  uint32_t m_identifier;
+
+ public:
+  IDHandle() : m_identifier((uint32_t)-1)
+  {
+  }
+
+  IDHandle(struct ID *id);
+
+  friend bool operator==(IDHandle a, IDHandle b)
+  {
+    return a.m_identifier == b.m_identifier;
+  }
+
+  friend bool operator!=(IDHandle a, IDHandle b)
+  {
+    return !(a == b);
+  }
+
+  uint32_t internal_identifier() const
+  {
+    return m_identifier;
+  }
+};
+
+class ObjectIDHandle : public IDHandle {
+ public:
+  ObjectIDHandle() : IDHandle()
+  {
+  }
+
+  ObjectIDHandle(struct Object *object);
+};
+
+class ImageIDHandle : public IDHandle {
+ public:
+  ImageIDHandle() : IDHandle()
+  {
+  }
+
+  ImageIDHandle(struct Image *image);
+};
+
+class IDHandleLookup {
+ private:
+  Map<IDHandle, ID *> m_handle_to_id_map;
+
+ public:
+  void add(ID &id)
+  {
+    IDHandle handle(&id);
+    m_handle_to_id_map.add(handle, &id);
+  }
+
+  ID *lookup(IDHandle handle) const
+  {
+    return m_handle_to_id_map.lookup_default(handle, nullptr);
+  }
+
+  struct Object *lookup(ObjectIDHandle handle) const
+  {
+    return reinterpret_cast<struct Object *>(this->lookup((IDHandle)handle));
+  }
+
+  struct Image *lookup(ImageIDHandle handle) const
+  {
+    return reinterpret_cast<struct Image *>(this->lookup((IDHandle)handle));
+  }
+
+  static const IDHandleLookup &Empty();
+};
+
+}  // namespace BKE
+
+namespace BLI {
+template<> struct DefaultHash<BKE::IDHandle> {
+  uint32_t operator()(const BKE::IDHandle &value) const
+  {
+    return value.internal_identifier();
+  }
+};
+}  // namespace BLI
+
+#endif /* __BKE_ID_HANDLE_H__ */
\ No newline at end of file
diff --git a/source/blender/blenkernel/BKE_surface_location.h b/source/blender/blenkernel/BKE_surface_location.h
index 217c8579f1e..8d55f0e2216 100644
--- a/source/blender/blenkernel/BKE_surface_location.h
+++ b/source/blender/blenkernel/BKE_surface_location.h
@@ -4,9 +4,7 @@
 #include "BLI_utildefines.h"
 #include "BLI_math_cxx.h"
 
-#include "DNA_object_types.h"
-
-#include "BLI_hash.h"
+#include "BKE_id_handle.h"
 
 namespace BKE {
 
@@ -20,20 +18,16 @@ enum Enum {
 }
 
 /**
- * References a point on a surface. If the surface moves, the point moves with it. The surface is
- * identified by an integer.
- *
- * For now, only points on triangle meshes are supported, support for curves could be added too.
+ * References a point on a surface. If the surface moves, the point moves with it.
  */
 class SurfaceLocation {
  private:
   SurfaceLocationType::Enum m_type;
 
   /**
-   * Identifies the surface that is being referenced. This can e.g. be a hash of the name of an
-   * object.
+   * Used to identify the object if m_type is MeshObject.
    */
-  uint32_t m_surface_id;
+  ObjectIDHandle m_object_handle;
 
   /* Index of the triangle that contains the referenced location. */
   uint32_t m_triangle_index;
@@ -46,9 +40,9 @@ class SurfaceLocation {
   {
   }
 
-  SurfaceLocation(uint32_t surface_id, uint32_t triangle_index, float3 bary_coords)
+  SurfaceLocation(ObjectIDHandle object_handle, uint32_t triangle_index, float3 bary_coords)
       : m_type(SurfaceLocationType::MeshObject),
-        m_surface_id(surface_id),
+        m_object_handle(object_handle),
         m_triangle_index(triangle_index),
         m_bary_coords(bary_coords)
   {
@@ -64,10 +58,10 @@ class SurfaceLocation {
     return m_type != SurfaceLocationType::None;
   }
 
-  uint32_t surface_id() const
+  ObjectIDHandle object_handle() const
   {
-    BLI_assert(this->is_valid());
-    return m_surface_id;
+    BLI_assert(m_type == SurfaceLocationType::MeshObject);
+    return m_object_handle;
   }
 
   uint32_t triangle_index() const
@@ -81,13 +75,6 @@ class SurfaceLocation {
     BLI_assert(m_type == SurfaceLocationType::MeshObject);
     return m_bary_coords;
   }
-
-  static uint32_t ComputeObjectSurfaceID(const Object *ob)
-  {
-    BLI_assert(ob != nullptr);
-
-    return BLI_hash_string(ob->id.name);
-  }
 };
 
 }  // namespace BKE
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 14229bcb016..a5a453bfc43 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -125,6 +125,7 @@ set(SRC
   intern/idcode.c
   intern/idprop.c
   intern/idprop_utils.c
+  intern/id_handle.cc
   intern/image.c
   intern/image_gen.c
   intern/image_save.c
@@ -293,6 +294,7 @@ set(SRC
   BKE_icons.h
   BKE_idcode.h
   BKE_idprop.h
+  BKE_id_handle.h
   BKE_image.h
   BKE_image_save.h
   BKE_inlined_node_tree.h
diff --git a/source/blender/blenkernel/intern/id_handle.cc b/source/blender/blenkernel/intern/id_handle.cc
new file mode 100644
index 00000000000..81298dbf86c
--- /dev/null
+++ b/source/blender/blenkernel/intern/id_handle.cc
@@ -0,0 +1,33 @@
+#include "BLI_utildefines.h"
+#include "BLI_hash.h"
+
+#include "BKE_id_handle.h"
+
+#include "DNA_ID.h"
+#include "DNA_object_types.h"
+#include "DNA_image_types.h"
+
+namespace BKE {
+
+IDHandle::IDHandle(ID *id)
+{
+  BLI_assert(id != nullptr);
+  m_identifier = BLI_hash_string(id->name);
+}
+
+ObjectIDHandle::ObjectIDHandle(Object *object) : IDHandle(&object->id)
+{
+}
+
+ImageIDHandle::ImageIDHandle(Image *image) : IDHandle(&image->id)
+{
+}
+
+static IDHandleLookup empty_id_handle_lookup;
+
+const IDHandleLookup &IDHandleLookup::Empty()
+{
+  return empty_id_handle_lookup;
+}
+
+}  // namespace BKE
\ No newline at end of file
diff --git a/source/blender/functions/FN_multi_function_common_contexts.h b/source/blender/functions/FN_multi_function_common_contexts.h
index b2a749867b5..8d41e9c888e 100644
--- a/source/blender/functions/FN_multi_function_common_contexts.h
+++ b/source/blender/functions/FN_multi_function_common_contexts.h
@@ -40,22 +40,6 @@ class ParticleAttributesContext : public MFElementContext {
   }
 };
 
-class PersistentSurfacesLookupContext : public MFElementContext {
- private:
-  Map<uint32_t, Object *> m_object_by_id;
-
- public:
-  PersistentSurfacesLookupContext(Map<uint32_t, Object *> object_by_id)
-      : m_object_by_id(object_by_id)
-  {
-  }
-
-  Object *lookup(uint32_t id) const
-  {
-    return m_object_by_id.lookup_default(id, nullptr);
-  }
-};
-
 class ExternalDataCacheContext : public MFElementContext {
  private:
   mutable Map<Object *, BVHTreeFromMesh *> m_bvh_trees;
diff --git a/source/blender/functions/FN_multi_function_context.h b/source/blender/functions/FN_multi_function_context.h
index 8d7e15772f6..00b8b8b3b0f 100644
--- a/source/blender/functions/FN_multi_function_context.h
+++ b/source/blender/functions/FN_multi_function_context.h
@@ -8,8 +8,11 @@
 #include "BLI_utility_mixins.h"
 #include "BLI_index_range.h"
 
+#include "BKE_id_handle.h"
+
 namespace FN {
 
+using BKE::IDHandleLookup;
 using BLI::ArrayRef;
 using BLI::IndexRange;
 using BLI::Optional;
@@ -52,11 +55,14 @@ class MFElementContexts {
 class MFContextBuilder : BLI::NonCopyable, BLI::NonMovable {
  private:
   MFElementContexts m_element_contexts;
+  const IDHandleLookup &m_id_handle_lookup;
 
   friend class MFContext;
 
  public:
-  MFContextBuilder()
+  MFContextBuilder(const IDHandleLookup *id_handle_lookup = nullptr)
+      : m_id_handle_lookup((id_handle_lookup == nullptr) ? IDHandleLookup::Empty() :
+                                                           *id_handle_lookup)
   {
   }
 
@@ -88,6 +94,11 @@ class MFContext {
   {
   }
 
+  const IDHandleLookup &id_handle_lookup() const
+  {
+    return m_builder->m_id_handle_lookup;
+  }
+
   const MFElementContexts &element_contexts() const
   {
     return m_builder->m_element_contexts;
diff --git a/source/blender/functions/intern/multi_functions/mixed.cc b/source/blender/functions/intern/multi_functions/mixed.cc
index c63fa8aeaab..634e170aa06 100644
--- a/source/blender/functions/intern/multi_functions/mixed.cc
+++ b/source/blender/functions/intern/multi_functions/mixed.cc
@@ -25,6 +25,7 @@
 
 namespace FN {
 
+using BKE::ObjectIDHandle;
 using BKE::SurfaceLocation;
 using BLI::float2;
 using BLI::float3;
@@ -288,13 +289,6 @@ void MF_GetPositionOnSurface::call(MFMask mask, MFParams params, MFContext conte
       0, "Surface Location");
   MutableArrayRef<float3> r_positions = params.uninitialized_single_output<float3>(1, "Position");
 
-  auto persistent_surfaces_opt =
-      context.element_contexts().find_first<PersistentSurfacesLookupContext>();
-  if (!persistent_surfaces_opt.has_value()) {
-    r_positions.fill_indices(mask.indices(), {0, 0, 0});
-    return;
-  }
-
   for (uint i : mask.indices()) {
     SurfaceLocation location = locations[i];
     if (location.type() != BKE::SurfaceLocationType::MeshObject) {
@@ -302,7 +296,7 @@ void MF_GetPositionOnSurface::call(MFMask mask, MFParams params, MFContext conte
       continue;
     }
 
-    Object *object = persistent_surfaces_opt->data->lookup(location.surface_id());
+    Object *object = context.id_handle_lookup().lookup(location.object_handle());
     if (object == nullptr) {
       r_positions[i] = {0, 0, 0};
       continue;
@@ -351,13 +345,6 @@ void MF_GetNormalOnSurface::call(MFMask mask, MFParams params, MFContext context
       0, "Surface Location");
   MutableArrayRef<float3> r_normals = params.uninitialized_single_output<float3>(1, "Normal");
 
-  auto persistent_surfaces_opt =
-      context.element_contexts().find_first<PersistentSurfacesLookupContext>();
-  if (!persistent_surfaces_opt.has_value()) {
-    r_normals.fill_indices(mask.indices(), {0, 0, 1});
-    return;
-  }
-
   for (uint i : mask.indices()) {
     SurfaceLocation location = locations[i

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list