[Bf-blender-cvs] [6e5e1e7ee7e] functions: store Range in AttributeArrays

Jacques Lucke noreply at git.blender.org
Wed Aug 28 18:01:15 CEST 2019


Commit: 6e5e1e7ee7e8d2966cf4adf2862d70219468d3d0
Author: Jacques Lucke
Date:   Wed Aug 28 10:50:55 2019 +0200
Branches: functions
https://developer.blender.org/rB6e5e1e7ee7e8d2966cf4adf2862d70219468d3d0

store Range in AttributeArrays

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

M	source/blender/blenlib/BLI_range.hpp
M	source/blender/simulations/bparticles/action_interface.hpp
M	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/particles_container.hpp
M	source/blender/simulations/bparticles/simulate.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 de9062e18aa..064444cfaa1 100644
--- a/source/blender/blenlib/BLI_range.hpp
+++ b/source/blender/blenlib/BLI_range.hpp
@@ -148,13 +148,21 @@ template<typename T> class Range {
   }
 
   /**
-   * Get the element one after the end. Do not depend this value when the range is empty.
+   * Get the element one after the end. The returned value is undefined when the range is empty.
    */
   T one_after_last() const
   {
     return m_one_after_last;
   }
 
+  /**
+   * Get the first element in the range. The returned value is undefined when the range is empty.
+   */
+  T start() const
+  {
+    return m_start;
+  }
+
   /**
    * Returns true when the range contains a certain number, otherwise false.
    */
@@ -163,6 +171,13 @@ template<typename T> class Range {
     return value >= m_start && value < m_one_after_last;
   }
 
+  Range<T> slice(uint start, uint size) const
+  {
+    uint new_start = m_start + start;
+    BLI_assert(new_start + size <= m_one_after_last);
+    return Range<T>(new_start, new_start + size);
+  }
+
   /**
    * Get read-only access to a memory buffer that contains the range as actual numbers. This only
    * works for some ranges. The range must be within [0, RANGE_AS_ARRAY_REF_MAX_LEN].
diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index ae27995f628..c9fdc40120d 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -98,7 +98,7 @@ inline void Action::execute_from_emitter(ParticleSets &particle_sets,
 
   for (ParticleSet particles : particle_sets.sets()) {
     uint min_array_size = particles.attributes().size();
-    AttributeArrays offsets(info, buffers, 0, min_array_size);
+    AttributeArrays offsets(info, buffers, min_array_size);
     TemporaryArray<float> durations(min_array_size);
     durations.fill_indices(particles.pindices(), 0);
     ActionInterface action_interface(emitter_interface.particle_allocator(),
@@ -149,7 +149,7 @@ inline void Action::execute_for_new_particles(ParticleSets &particle_sets,
 
   for (ParticleSet particles : particle_sets.sets()) {
     uint min_array_size = particles.attributes().size();
-    AttributeArrays offsets(info, buffers, 0, min_array_size);
+    AttributeArrays offsets(info, buffers, min_array_size);
     TemporaryArray<float> durations(min_array_size);
     durations.fill_indices(particles.pindices(), 0);
     ActionInterface new_interface(action_interface.particle_allocator(),
@@ -172,7 +172,7 @@ inline void Action::execute_for_new_particles(ParticleSets &particle_sets,
 
   for (ParticleSet particles : particle_sets.sets()) {
     uint min_array_size = particles.attributes().size();
-    AttributeArrays offsets(info, buffers, 0, min_array_size);
+    AttributeArrays offsets(info, buffers, min_array_size);
     TemporaryArray<float> durations(min_array_size);
     durations.fill_indices(particles.pindices(), 0);
     ActionInterface new_interface(offset_handler_interface.particle_allocator(),
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index 5ca8c9d4dd0..98ae2ad096e 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -267,11 +267,12 @@ class AttributeArrays;
 class AttributeArrays {
  private:
   AttributesInfo *m_info;
-  uint m_start, m_size;
   ArrayRef<void *> m_buffers;
+  Range<uint> m_range;
 
  public:
-  AttributeArrays(AttributesInfo &info, ArrayRef<void *> buffers, uint start, uint size);
+  AttributeArrays(AttributesInfo &info, ArrayRef<void *> buffers, uint size);
+  AttributeArrays(AttributesInfo &info, ArrayRef<void *> buffers, Range<uint> range);
 
   /**
    * Get the number of referenced elements.
@@ -312,7 +313,7 @@ class AttributeArrays {
   {
     BLI_assert(attribute_type_by_type<T>::value == m_info->type_of(index));
     void *ptr = this->get_ptr(index);
-    return MutableArrayRef<T>((T *)ptr, m_size);
+    return MutableArrayRef<T>((T *)ptr, m_range.size());
   }
   template<typename T> MutableArrayRef<T> get(StringRef name)
   {
@@ -349,17 +350,21 @@ class AttributeArrays {
 /* Attribute Arrays
  ******************************************/
 
+inline AttributeArrays::AttributeArrays(AttributesInfo &info, ArrayRef<void *> buffers, uint size)
+    : AttributeArrays(info, buffers, Range<uint>(0, size))
+{
+}
+
 inline AttributeArrays::AttributeArrays(AttributesInfo &info,
                                         ArrayRef<void *> buffers,
-                                        uint start,
-                                        uint size)
-    : m_info(&info), m_start(start), m_size(size), m_buffers(buffers)
+                                        Range<uint> range)
+    : m_info(&info), m_buffers(buffers), m_range(range)
 {
 }
 
 inline uint AttributeArrays::size() const
 {
-  return m_size;
+  return m_range.size();
 }
 
 inline AttributesInfo &AttributeArrays::info()
@@ -382,7 +387,7 @@ inline void *AttributeArrays::get_ptr(uint index) const
   void *ptr = m_buffers[index];
   AttributeType type = m_info->type_of(index);
   uint size = size_of_attribute_type(type);
-  return POINTER_OFFSET(ptr, m_start * size);
+  return POINTER_OFFSET(ptr, m_range.start() * size);
 }
 
 inline void AttributeArrays::init_default(uint index)
@@ -392,7 +397,7 @@ inline void AttributeArrays::init_default(uint index)
   AttributeType type = m_info->type_of(index);
   uint element_size = size_of_attribute_type(type);
 
-  for (uint i = 0; i < m_size; i++) {
+  for (uint i : m_range) {
     memcpy(POINTER_OFFSET(dst, element_size * i), default_value, element_size);
   }
 }
@@ -404,15 +409,12 @@ inline void AttributeArrays::init_default(StringRef name)
 
 inline AttributeArrays AttributeArrays::slice(uint start, uint size) const
 {
-  BLI_assert(start >= m_start);
-  BLI_assert(start + size <= m_size);
-  return AttributeArrays(*m_info, m_buffers, m_start + start, size);
+  return AttributeArrays(*m_info, m_buffers, m_range.slice(start, size));
 }
 
 inline AttributeArrays AttributeArrays::take_front(uint n) const
 {
-  BLI_assert(n <= m_size);
-  return AttributeArrays(*m_info, m_buffers, m_start, n);
+  return AttributeArrays(*m_info, m_buffers, m_range.slice(0, n));
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/particles_container.hpp b/source/blender/simulations/bparticles/particles_container.hpp
index fa6b61e3226..6f21d7ef231 100644
--- a/source/blender/simulations/bparticles/particles_container.hpp
+++ b/source/blender/simulations/bparticles/particles_container.hpp
@@ -302,26 +302,22 @@ inline ParticlesContainer &ParticlesBlock::container()
 
 inline AttributeArrays ParticlesBlock::attributes_slice(Range<uint> range)
 {
-  if (range.size() == 0) {
-    return this->attributes_slice(0, 0);
-  }
-  return this->attributes_slice(range.first(), range.size());
+  return AttributeArrays(m_container.attributes_info(), m_attribute_buffers, range);
 }
 
 inline AttributeArrays ParticlesBlock::attributes_slice(uint start, uint length)
 {
-  return AttributeArrays(m_container.attributes_info(), m_attribute_buffers, start, length);
+  return this->attributes_slice(Range<uint>(start, start + length));
 }
 
 inline AttributeArrays ParticlesBlock::attributes_all()
 {
-  return AttributeArrays(
-      m_container.attributes_info(), m_attribute_buffers, 0, m_container.block_size());
+  return this->attributes_slice(Range<uint>(0, m_container.block_size()));
 }
 
 inline AttributeArrays ParticlesBlock::attributes()
 {
-  return this->attributes_slice(0, m_active_amount);
+  return this->attributes_slice(Range<uint>(0, m_active_amount));
 }
 
 }  // namespace BParticles
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 2ad036ba1f9..ec26525daf6 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -345,7 +345,7 @@ BLI_NOINLINE static void simulate_block(ParticleAllocator &particle_allocator,
     void *ptr = BLI_temporary_allocate(size_of_attribute_type(type) * amount);
     offset_buffers.append(ptr);
   }
-  AttributeArrays attribute_offsets(offsets_info, offset_buffers, 0, amount);
+  AttributeArrays attribute_offsets(offsets_info, offset_buffers, amount);
 
   BlockStepData step_data = {
       particle_allocator, block.attributes(), attribute_offsets, remaining_durations, end_time};
diff --git a/tests/gtests/blenlib/BLI_range_test.cc b/tests/gtests/blenlib/BLI_range_test.cc
index f6a5f3058a7..26b99fed57c 100644
--- a/tests/gtests/blenlib/BLI_range_test.cc
+++ b/tests/gtests/blenlib/BLI_range_test.cc
@@ -105,6 +105,21 @@ TEST(range, OneAfterEnd)
   EXPECT_EQ(range.one_after_last(), 8);
 }
 
+TEST(range, Start)
+{
+  IntRange range = IntRange(6, 8);
+  EXPECT_EQ(range.start(), 6);
+}
+
+TEST(range, Slice)
+{
+  IntRange range = IntRange(5, 20);
+  IntRange slice = range.slice(2, 6);
+  EXPECT_EQ(slice.size(), 6);
+  EXPECT_EQ(slice.first(), 7);
+  EXPECT_EQ(slice.last(), 12);
+}
+
 TEST(range, AsArrayRef)
 {
   IntRange range = IntRange(4, 10);



More information about the Bf-blender-cvs mailing list