[Bf-blender-cvs] [6ea4623ea14] functions: ability to store attribute defaults in AttributeInfo

Jacques Lucke noreply at git.blender.org
Wed Jun 26 18:19:18 CEST 2019


Commit: 6ea4623ea140f358b53e2d88f7a0deee29f5992e
Author: Jacques Lucke
Date:   Wed Jun 26 15:34:57 2019 +0200
Branches: functions
https://developer.blender.org/rB6ea4623ea140f358b53e2d88f7a0deee29f5992e

ability to store attribute defaults in AttributeInfo

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

M	source/blender/blenlib/BLI_range.hpp
M	source/blender/simulations/bparticles/attributes.cpp
M	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/particles_container.cpp
M	tests/gtests/blenlib/BLI_range_test.cc

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

diff --git a/source/blender/blenlib/BLI_range.hpp b/source/blender/blenlib/BLI_range.hpp
index 85a4c95dce3..24fc3b42531 100644
--- a/source/blender/blenlib/BLI_range.hpp
+++ b/source/blender/blenlib/BLI_range.hpp
@@ -80,6 +80,23 @@ template<typename T> class Range {
     return Range(m_start - n, m_start);
   }
 
+  T first() const
+  {
+    BLI_assert(this->size() > 0);
+    return m_start;
+  }
+
+  T last() const
+  {
+    BLI_assert(this->size() > 0);
+    return m_one_after_last - 1;
+  }
+
+  bool contains(T value) const
+  {
+    return value >= m_start && value < m_one_after_last;
+  }
+
   BLI_NOINLINE SmallVector<T> to_small_vector() const
   {
     SmallVector<T> values;
diff --git a/source/blender/simulations/bparticles/attributes.cpp b/source/blender/simulations/bparticles/attributes.cpp
index dea29f58282..7d9ff3e0c24 100644
--- a/source/blender/simulations/bparticles/attributes.cpp
+++ b/source/blender/simulations/bparticles/attributes.cpp
@@ -20,6 +20,10 @@ AttributesInfo::AttributesInfo(ArrayRef<std::string> byte_names,
   m_types.append_n_times(AttributeType::Byte, m_byte_attributes.size());
   m_types.append_n_times(AttributeType::Float, m_float_attributes.size());
   m_types.append_n_times(AttributeType::Float3, m_float3_attributes.size());
+
+  m_byte_defaults.append_n_times(0, m_byte_attributes.size());
+  m_float_defaults.append_n_times(0, m_float_attributes.size());
+  m_float3_defaults.append_n_times({0, 0, 0}, m_float3_attributes.size());
 }
 
 AttributeArraysCore::AttributeArraysCore(AttributesInfo &info, ArrayRef<void *> arrays, uint size)
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index 3928a2879ce..4876232807b 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -48,6 +48,10 @@ class AttributesInfo {
   SmallVector<AttributeType> m_types;
   SmallSetVector<std::string> m_indices;
 
+  SmallVector<uint8_t> m_byte_defaults;
+  SmallVector<float> m_float_defaults;
+  SmallVector<float3> m_float3_defaults;
+
  public:
   AttributesInfo() = default;
   AttributesInfo(ArrayRef<std::string> byte_names,
@@ -106,6 +110,22 @@ class AttributesInfo {
     return m_float3_attributes;
   }
 
+  void *default_value_ptr(uint index) const
+  {
+    BLI_assert(index < m_indices.size());
+    AttributeType type = this->type_of(index);
+    switch (type) {
+      case AttributeType::Byte:
+        return (void *)&m_byte_defaults[index - m_byte_attributes.first()];
+      case AttributeType::Float:
+        return (void *)&m_float_defaults[index - m_float_attributes.first()];
+      case AttributeType::Float3:
+        return (void *)&m_float3_defaults[index - m_float3_attributes.first()];
+    }
+    BLI_assert(false);
+    return nullptr;
+  }
+
   friend bool operator==(const AttributesInfo &a, const AttributesInfo &b)
   {
     return &a == &b;
diff --git a/source/blender/simulations/bparticles/particles_container.cpp b/source/blender/simulations/bparticles/particles_container.cpp
index 53199689024..b1f7bf1119c 100644
--- a/source/blender/simulations/bparticles/particles_container.cpp
+++ b/source/blender/simulations/bparticles/particles_container.cpp
@@ -91,7 +91,13 @@ void ParticlesContainer::update_attributes(AttributesInfo new_info)
       AttributeType type = new_info.type_of(new_index);
 
       if (old_index == -1) {
-        arrays.append(MEM_calloc_arrayN(m_block_size, size_of_attribute_type(type), __func__));
+        void *array = MEM_malloc_arrayN(m_block_size, size_of_attribute_type(type), __func__);
+        uint value_size = size_of_attribute_type(type);
+        void *default_ptr = new_info.default_value_ptr(new_index);
+        for (uint i = 0; i < m_block_size; i++) {
+          memcpy(POINTER_OFFSET(array, i * value_size), default_ptr, value_size);
+        }
+        arrays.append(array);
       }
       else {
         arrays.append(block->attributes_core().get_ptr((uint)old_index));
diff --git a/tests/gtests/blenlib/BLI_range_test.cc b/tests/gtests/blenlib/BLI_range_test.cc
index 6722f8201c7..0a5fb2fa233 100644
--- a/tests/gtests/blenlib/BLI_range_test.cc
+++ b/tests/gtests/blenlib/BLI_range_test.cc
@@ -84,3 +84,25 @@ TEST(range, ToSmallVector)
   EXPECT_EQ(vec[1], 6);
   EXPECT_EQ(vec[2], 7);
 }
+
+TEST(range, Contains)
+{
+  IntRange range = IntRange(5, 8);
+  EXPECT_TRUE(range.contains(5));
+  EXPECT_TRUE(range.contains(6));
+  EXPECT_TRUE(range.contains(7));
+  EXPECT_FALSE(range.contains(4));
+  EXPECT_FALSE(range.contains(8));
+}
+
+TEST(range, First)
+{
+  IntRange range = IntRange(5, 8);
+  EXPECT_EQ(range.first(), 5);
+}
+
+TEST(range, Last)
+{
+  IntRange range = IntRange(5, 8);
+  EXPECT_EQ(range.last(), 7);
+}



More information about the Bf-blender-cvs mailing list