[Bf-blender-cvs] [a8892549741] attribute-accessor: use simpler accessor

Jacques Lucke noreply at git.blender.org
Tue Nov 17 13:55:38 CET 2020


Commit: a88925497412f3634e4159122781383499a9dae3
Author: Jacques Lucke
Date:   Tue Nov 17 12:45:03 2020 +0100
Branches: attribute-accessor
https://developer.blender.org/rBa88925497412f3634e4159122781383499a9dae3

use simpler accessor

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

M	source/blender/blenkernel/BKE_attribute_accessor.hh
M	source/blender/blenkernel/intern/attribute_accessor.cc
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc

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

diff --git a/source/blender/blenkernel/BKE_attribute_accessor.hh b/source/blender/blenkernel/BKE_attribute_accessor.hh
index 561777511ac..ce744baa762 100644
--- a/source/blender/blenkernel/BKE_attribute_accessor.hh
+++ b/source/blender/blenkernel/BKE_attribute_accessor.hh
@@ -28,7 +28,7 @@ namespace blender::bke {
 using fn::CPPType;
 
 class AttributeAccessor {
- private:
+ protected:
   AttributeDomain domain_;
   const CPPType *cpp_type_;
   int64_t size_;
@@ -78,11 +78,18 @@ class AttributeAccessor {
 
 using AttributeAccessorPtr = std::unique_ptr<AttributeAccessor>;
 
-AttributeAccessorPtr get_raw_mesh_attribute_accessor(const MeshComponent &mesh_component,
-                                                     const StringRef attribute_name);
+AttributeAccessorPtr mesh_attribute_get_accessor(const MeshComponent &mesh_component,
+                                                 const StringRef attribute_name);
 
-AttributeAccessorPtr adapt_mesh_attribute_accessor_domain(const MeshComponent &mesh_component,
+AttributeAccessorPtr mesh_attribute_adapt_accessor_domain(const MeshComponent &mesh_component,
                                                           AttributeAccessorPtr attribute_accessor,
                                                           const AttributeDomain to_domain);
 
+AttributeAccessorPtr mesh_attribute_get_accessor_for_domain_with_type(
+    const MeshComponent &mesh_component,
+    const StringRef attribute_name,
+    const AttributeDomain domain,
+    const CPPType &cpp_type,
+    const void *default_value = nullptr);
+
 }  // namespace blender::bke
diff --git a/source/blender/blenkernel/intern/attribute_accessor.cc b/source/blender/blenkernel/intern/attribute_accessor.cc
index 990ad1872e7..66e125398c4 100644
--- a/source/blender/blenkernel/intern/attribute_accessor.cc
+++ b/source/blender/blenkernel/intern/attribute_accessor.cc
@@ -96,19 +96,24 @@ class DerivedArrayAttributeAccessor final : public AttributeAccessor {
   }
 };
 
-template<typename T> class ConstantAttributeAccessor final : public AttributeAccessor {
+class ConstantAttributeAccessor final : public AttributeAccessor {
  private:
-  T value_;
+  void *value_;
 
  public:
-  ConstantAttributeAccessor(AttributeDomain domain, T value, const int64_t size)
-      : AttributeAccessor(domain, CPPType::get<T>(), size), value_(std::move(value))
+  ConstantAttributeAccessor(AttributeDomain domain,
+                            const int64_t size,
+                            const CPPType &type,
+                            const void *value)
+      : AttributeAccessor(domain, type, size)
   {
+    value_ = MEM_mallocN_aligned(type.size(), type.alignment(), __func__);
+    type.copy_to_uninitialized(value, value_);
   }
 
-  void access_single(const int64_t index, void *r_value) const override
+  void access_single(const int64_t UNUSED(index), void *r_value) const override
   {
-    new (r_value) T(value_);
+    this->cpp_type_->copy_to_uninitialized(value_, r_value);
   }
 };
 
@@ -242,8 +247,8 @@ static AttributeAccessorPtr get_mesh_attribute_accessor__polygon(
       mesh->pdata, mesh->totpoly, attribute_name, ATTR_DOMAIN_POLYGON);
 }
 
-AttributeAccessorPtr get_raw_mesh_attribute_accessor(const MeshComponent &mesh_component,
-                                                     const StringRef attribute_name)
+AttributeAccessorPtr mesh_attribute_get_accessor(const MeshComponent &mesh_component,
+                                                 const StringRef attribute_name)
 {
   AttributeAccessorPtr corner_level = get_mesh_attribute_accessor__corner(mesh_component,
                                                                           attribute_name);
@@ -283,11 +288,9 @@ static AttributeAccessorPtr adapt_mesh_attribute_accessor_to_corner(
       return std::make_unique<VertexToCornerAccessor>(std::move(attribute_accessor),
                                                       Span(mesh.mloop, mesh.totloop));
     case ATTR_DOMAIN_EDGE: {
-      BLI_assert(!"currently not implemented");
       break;
     }
     case ATTR_DOMAIN_POLYGON: {
-      BLI_assert(!"currently not implemented");
       break;
     }
     default: {
@@ -300,25 +303,22 @@ static AttributeAccessorPtr adapt_mesh_attribute_accessor_to_corner(
 static AttributeAccessorPtr adapt_mesh_attribute_accessor_to_vertex(
     const MeshComponent &UNUSED(mesh_component), AttributeAccessorPtr UNUSED(attribute_accessor))
 {
-  BLI_assert(!"currently not implemented");
   return {};
 }
 
 static AttributeAccessorPtr adapt_mesh_attribute_accessor_to_edge(
     const MeshComponent &UNUSED(mesh_component), AttributeAccessorPtr UNUSED(attribute_accessor))
 {
-  BLI_assert(!"currently not implemented");
   return {};
 }
 
 static AttributeAccessorPtr adapt_mesh_attribute_accessor_to_polygon(
     const MeshComponent &UNUSED(mesh_component), AttributeAccessorPtr UNUSED(attribute_accessor))
 {
-  BLI_assert(!"currently not implemented");
   return {};
 }
 
-AttributeAccessorPtr adapt_mesh_attribute_accessor_domain(const MeshComponent &mesh_component,
+AttributeAccessorPtr mesh_attribute_adapt_accessor_domain(const MeshComponent &mesh_component,
                                                           AttributeAccessorPtr attribute_accessor,
                                                           const AttributeDomain to_domain)
 {
@@ -347,4 +347,68 @@ AttributeAccessorPtr adapt_mesh_attribute_accessor_domain(const MeshComponent &m
   }
 }
 
+static int get_domain_length(const MeshComponent &mesh_component, const AttributeDomain domain)
+{
+  const Mesh *mesh = mesh_component.get_for_read();
+  if (mesh == nullptr) {
+    return 0;
+  }
+  switch (domain) {
+    case ATTR_DOMAIN_CORNER:
+      return mesh->totloop;
+    case ATTR_DOMAIN_VERTEX:
+      return mesh->totvert;
+    case ATTR_DOMAIN_EDGE:
+      return mesh->totedge;
+    case ATTR_DOMAIN_POLYGON:
+      return mesh->totpoly;
+    default:
+      break;
+  }
+  return 0;
+}
+
+static AttributeAccessorPtr make_default_accessor(const MeshComponent &mesh_component,
+                                                  const AttributeDomain domain,
+                                                  const CPPType &cpp_type,
+                                                  const void *default_value)
+{
+
+  const int length = get_domain_length(mesh_component, domain);
+  return std::make_unique<ConstantAttributeAccessor>(domain, length, cpp_type, default_value);
+}
+
+AttributeAccessorPtr mesh_attribute_get_accessor_for_domain_with_type(
+    const MeshComponent &mesh_component,
+    const StringRef attribute_name,
+    const AttributeDomain domain,
+    const CPPType &cpp_type,
+    const void *default_value)
+{
+  AttributeAccessorPtr attribute_accessor = mesh_attribute_get_accessor(mesh_component,
+                                                                        attribute_name);
+  auto get_default_or_empty = [&]() -> AttributeAccessorPtr {
+    if (default_value != nullptr) {
+      return make_default_accessor(mesh_component, domain, cpp_type, default_value);
+    }
+    return {};
+  };
+
+  if (!attribute_accessor) {
+    return get_default_or_empty();
+  }
+  if (attribute_accessor->domain() != domain) {
+    attribute_accessor = mesh_attribute_adapt_accessor_domain(
+        mesh_component, std::move(attribute_accessor), domain);
+  }
+  if (!attribute_accessor) {
+    return get_default_or_empty();
+  }
+  if (attribute_accessor->cpp_type() != cpp_type) {
+    /* TODO: Support some type conversions. */
+    return get_default_or_empty();
+  }
+  return attribute_accessor;
+}
+
 }  // namespace blender::bke
diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh
index 6ef39e0c698..fe71f47428b 100644
--- a/source/blender/nodes/NOD_geometry_exec.hh
+++ b/source/blender/nodes/NOD_geometry_exec.hh
@@ -26,6 +26,7 @@
 
 namespace blender::nodes {
 
+using bke::AttributeAccessor;
 using bke::AttributeAccessorPtr;
 using bke::PersistentDataHandleMap;
 using bke::PersistentObjectHandle;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc b/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
index d39f44f21f8..98013d5ac03 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
@@ -47,20 +47,15 @@ namespace blender::nodes {
 
 static Vector<float3> scatter_points_from_mesh(const Mesh *mesh,
                                                const float density,
-                                               const AttributeAccessorPtr &density_factors)
+                                               const AttributeAccessor &density_factors)
 {
   /* This only updates a cache and can be considered to be logically const. */
   const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(const_cast<Mesh *>(mesh));
   const int looptris_len = BKE_mesh_runtime_looptri_len(mesh);
 
   Array<float> vertex_density_factors(mesh->totvert);
-  if (!density_factors) {
-    vertex_density_factors.fill(1.0f);
-  }
-  else {
-    for (const int i : IndexRange(mesh->totvert)) {
-      vertex_density_factors[i] = density_factors->get<float>(i);
-    }
+  for (const int i : IndexRange(mesh->totvert)) {
+    vertex_density_factors[i] = density_factors.get<float>(i);
   }
 
   Vector<float3> points;
@@ -122,13 +117,15 @@ static void geo_point_distribute_exec(GeoNodeExecParams params)
   const MeshComponent &mesh_component = *geometry_set.get_component_for_read<MeshComponent>();
   const Mesh *mesh_in = mesh_component.get_for_read();
 
-  AttributeAccessorPtr density_factors = bke::get_raw_mesh_attribute_accessor(mesh_component,
-                                                                              density_attribute);
-  if (density_factors && density_factors->domain() != ATTR_DOMAIN_VERTEX) {
-    density_factors.reset();
-  }
+  const float default_factor = 1.0f;
+  AttributeAccessorPtr density_factors = bke::mesh_attribute_get_accessor_for_domain_with_type(
+      mesh_component,
+      density_attribute,
+      ATTR_DOMAIN_VERTEX,
+      CPPType::get<float>(),
+      &default_f

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list