[Bf-blender-cvs] [f78e2837ffe] functions: change get_T to get<T> when accessing attributes

Jacques Lucke noreply at git.blender.org
Mon Jul 29 17:57:50 CEST 2019


Commit: f78e2837ffe06b36fd689f8ef29eacc92e986f5b
Author: Jacques Lucke
Date:   Mon Jul 29 17:03:32 2019 +0200
Branches: functions
https://developer.blender.org/rBf78e2837ffe06b36fd689f8ef29eacc92e986f5b

change get_T to get<T> when accessing attributes

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

M	source/blender/simulations/bparticles/action_interface.hpp
M	source/blender/simulations/bparticles/actions.cpp
M	source/blender/simulations/bparticles/attributes.hpp
M	source/blender/simulations/bparticles/events.cpp
M	source/blender/simulations/bparticles/forces.cpp
M	source/blender/simulations/bparticles/integrator.cpp
M	source/blender/simulations/bparticles/offset_handlers.cpp
M	source/blender/simulations/bparticles/particle_allocator.cpp
M	source/blender/simulations/bparticles/particle_function_builder.cpp
M	source/blender/simulations/bparticles/simulate.cpp

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

diff --git a/source/blender/simulations/bparticles/action_interface.hpp b/source/blender/simulations/bparticles/action_interface.hpp
index 2ea5b7da668..cebe829740b 100644
--- a/source/blender/simulations/bparticles/action_interface.hpp
+++ b/source/blender/simulations/bparticles/action_interface.hpp
@@ -103,7 +103,7 @@ inline void Action::execute_from_emitter(ParticleSets &particle_sets,
                                      emitter_interface.array_allocator(),
                                      particles,
                                      offsets,
-                                     particles.attributes().get_float("Birth Time"),
+                                     particles.attributes().get<float>("Birth Time"),
                                      durations,
                                      used_action_context);
     this->execute(action_interface);
@@ -171,7 +171,7 @@ inline ArrayRef<float> ActionInterface::current_times()
 
 inline void ActionInterface::kill(ArrayRef<uint> pindices)
 {
-  auto kill_states = m_particles.attributes().get_byte("Kill State");
+  auto kill_states = m_particles.attributes().get<uint8_t>("Kill State");
   for (uint pindex : pindices) {
     kill_states[pindex] = 1;
   }
diff --git a/source/blender/simulations/bparticles/actions.cpp b/source/blender/simulations/bparticles/actions.cpp
index 1a633f1e643..26daafb00f2 100644
--- a/source/blender/simulations/bparticles/actions.cpp
+++ b/source/blender/simulations/bparticles/actions.cpp
@@ -11,7 +11,7 @@ void NoneAction::execute(ActionInterface &UNUSED(interface))
 void ChangeDirectionAction::execute(ActionInterface &interface)
 {
   ParticleSet particles = interface.particles();
-  auto velocities = particles.attributes().get_float3("Velocity");
+  auto velocities = particles.attributes().get<float3>("Velocity");
   auto position_offsets = interface.attribute_offsets().try_get_float3("Position");
   auto velocity_offsets = interface.attribute_offsets().try_get_float3("Velocity");
 
@@ -54,7 +54,7 @@ void ExplodeAction::execute(ActionInterface &interface)
 {
   ParticleSet &particles = interface.particles();
 
-  auto positions = particles.attributes().get_float3("Position");
+  auto positions = particles.attributes().get<float3>("Position");
 
   Vector<float3> new_positions;
   Vector<float3> new_velocities;
diff --git a/source/blender/simulations/bparticles/attributes.hpp b/source/blender/simulations/bparticles/attributes.hpp
index 8655d5a39a2..97f53e5e80d 100644
--- a/source/blender/simulations/bparticles/attributes.hpp
+++ b/source/blender/simulations/bparticles/attributes.hpp
@@ -34,6 +34,21 @@ enum AttributeType {
   Float3,
 };
 
+template<typename T> struct attribute_type_by_type {
+};
+
+#define ATTRIBUTE_TYPE_BY_TYPE(CPP_TYPE, ATTRIBUTE_TYPE) \
+  template<> struct attribute_type_by_type<CPP_TYPE> { \
+    static const AttributeType value = AttributeType::ATTRIBUTE_TYPE; \
+  }
+
+ATTRIBUTE_TYPE_BY_TYPE(uint8_t, Byte);
+ATTRIBUTE_TYPE_BY_TYPE(int32_t, Integer);
+ATTRIBUTE_TYPE_BY_TYPE(float, Float);
+ATTRIBUTE_TYPE_BY_TYPE(float3, Float3);
+
+#undef ATTRIBUTE_TYPE_BY_TYPE
+
 /**
  * Get the size of an attribute type.
  *
@@ -394,14 +409,17 @@ class AttributeArrays {
    * Get access to the underlying attribute arrays.
    * Asserts when the attribute does not exists.
    */
-  ArrayRef<uint8_t> get_byte(uint index) const;
-  ArrayRef<uint8_t> get_byte(StringRef name);
-  ArrayRef<int32_t> get_integer(uint index) const;
-  ArrayRef<int32_t> get_integer(StringRef name);
-  ArrayRef<float> get_float(uint index) const;
-  ArrayRef<float> get_float(StringRef name);
-  ArrayRef<float3> get_float3(uint index) const;
-  ArrayRef<float3> get_float3(StringRef name);
+  template<typename T> ArrayRef<T> get(uint index) const
+  {
+    BLI_assert(attribute_type_by_type<T>::value == m_core.info().type_of(index));
+    void *ptr = this->get_ptr(index);
+    return ArrayRef<T>((T *)ptr, m_size);
+  }
+  template<typename T> ArrayRef<T> get(StringRef name)
+  {
+    uint index = this->attribute_index(name);
+    return this->get<T>(index);
+  }
 
   /**
    * Get access to the arrays.
@@ -541,50 +559,6 @@ inline void AttributeArrays::init_default(StringRef name)
   this->init_default(this->attribute_index(name));
 }
 
-inline ArrayRef<uint8_t> AttributeArrays::get_byte(uint index) const
-{
-  BLI_assert(m_core.get_type(index) == AttributeType::Byte);
-  return ArrayRef<uint8_t>((uint8_t *)m_core.get_ptr(index) + m_start, m_size);
-}
-
-inline ArrayRef<uint8_t> AttributeArrays::get_byte(StringRef name)
-{
-  return this->get_byte(this->attribute_index(name));
-}
-
-inline ArrayRef<int32_t> AttributeArrays::get_integer(uint index) const
-{
-  BLI_assert(m_core.get_type(index) == AttributeType::Integer);
-  return ArrayRef<int32_t>((int32_t *)m_core.get_ptr(index) + m_start, m_size);
-}
-
-inline ArrayRef<int32_t> AttributeArrays::get_integer(StringRef name)
-{
-  return this->get_integer(this->attribute_index(name));
-}
-
-inline ArrayRef<float> AttributeArrays::get_float(uint index) const
-{
-  BLI_assert(m_core.get_type(index) == AttributeType::Float);
-  return ArrayRef<float>((float *)m_core.get_ptr(index) + m_start, m_size);
-}
-
-inline ArrayRef<float> AttributeArrays::get_float(StringRef name)
-{
-  return this->get_float(this->attribute_index(name));
-}
-
-inline ArrayRef<float3> AttributeArrays::get_float3(uint index) const
-{
-  BLI_assert(m_core.get_type(index) == AttributeType::Float3);
-  return ArrayRef<float3>((float3 *)m_core.get_ptr(index) + m_start, m_size);
-}
-
-inline ArrayRef<float3> AttributeArrays::get_float3(StringRef name)
-{
-  return this->get_float3(this->attribute_index(name));
-}
-
 inline Optional<ArrayRef<uint8_t>> AttributeArrays::try_get_byte(StringRef name)
 {
   int index = this->info().attribute_index_try(name, AttributeType::Byte);
@@ -592,7 +566,7 @@ inline Optional<ArrayRef<uint8_t>> AttributeArrays::try_get_byte(StringRef name)
     return {};
   }
   else {
-    return this->get_byte((uint)index);
+    return this->get<uint8_t>((uint)index);
   }
 }
 
@@ -603,7 +577,7 @@ inline Optional<ArrayRef<int32_t>> AttributeArrays::try_get_integer(StringRef na
     return {};
   }
   else {
-    return this->get_integer((uint)index);
+    return this->get<int32_t>((uint)index);
   }
 }
 
@@ -614,7 +588,7 @@ inline Optional<ArrayRef<float>> AttributeArrays::try_get_float(StringRef name)
     return {};
   }
   else {
-    return this->get_float((uint)index);
+    return this->get<float>((uint)index);
   }
 }
 
@@ -625,7 +599,7 @@ inline Optional<ArrayRef<float3>> AttributeArrays::try_get_float3(StringRef name
     return {};
   }
   else {
-    return this->get_float3((uint)index);
+    return this->get<float3>((uint)index);
   }
 }
 
diff --git a/source/blender/simulations/bparticles/events.cpp b/source/blender/simulations/bparticles/events.cpp
index 0ffc1ad7880..59e4e6493b9 100644
--- a/source/blender/simulations/bparticles/events.cpp
+++ b/source/blender/simulations/bparticles/events.cpp
@@ -13,8 +13,8 @@ void AgeReachedEvent::attributes(AttributesDeclaration &builder)
 void AgeReachedEvent::filter(EventFilterInterface &interface)
 {
   ParticleSet particles = interface.particles();
-  auto birth_times = particles.attributes().get_float("Birth Time");
-  auto was_activated_before = particles.attributes().get_byte(m_identifier);
+  auto birth_times = particles.attributes().get<float>("Birth Time");
+  auto was_activated_before = particles.attributes().get<uint8_t>(m_identifier);
 
   float end_time = interface.step_end_time();
 
@@ -50,7 +50,7 @@ void AgeReachedEvent::execute(EventExecuteInterface &interface)
 {
   ParticleSet particles = interface.particles();
 
-  auto was_activated_before = particles.attributes().get_byte(m_identifier);
+  auto was_activated_before = particles.attributes().get<uint8_t>(m_identifier);
   for (uint pindex : particles.pindices()) {
     was_activated_before[pindex] = true;
   }
@@ -74,9 +74,9 @@ uint MeshCollisionEvent::storage_size()
 void MeshCollisionEvent::filter(EventFilterInterface &interface)
 {
   ParticleSet particles = interface.particles();
-  auto positions = particles.attributes().get_float3("Position");
-  auto last_collision_times = particles.attributes().get_float(m_identifier);
-  auto position_offsets = interface.attribute_offsets().get_float3("Position");
+  auto positions = particles.attributes().get<float3>("Position");
+  auto last_collision_times = particles.attributes().get<float>(m_identifier);
+  auto position_offsets = interface.attribute_offsets().get<float3>("Position");
 
   for (uint pindex : particles.pindices()) {
     float3 ray_start = m_world_to_local.transform_position(positions[pindex]);
@@ -121,7 +121,7 @@ void MeshCollisionEvent::execute(EventExecuteInterface &interface)
 {
   ParticleSet particles = interface.particles();
   Vector<float3> normals(particles.block().active_amount());
-  auto last_collision_times = particles.attributes().get_float(m_identifier);
+  auto last_collision_times = particles.attributes().get<float>(m_identifier);
 
   for (uint pindex : particles.pindices()) {
     auto storage = interface.get_storage<EventStorage>(pindex);
@@ -136,7 +136,7 @@ void MeshCollisionEvent::execute(EventExecuteInterface &interface)
 void CloseByPointsEvent::filter(EventFilterInterface &interface)
 {
   ParticleSet particles = interface.particles();
-  auto positions = particles.attributes().get_float3("Position");
+  auto positions = particles.attributes().get<float3>("Position");
 
   for (uint pindex : particles.pindices()) {
     KDTreeNearest_3d nearest;
diff --git a/source/blender/simulations/bparticles/forces.cpp b/source/blender/simulations/bparticles/forces.cpp
index d5b8a9d3c9a..8c2a7db7feb 100644
--- a/source/blender/simulations/bparticles/forces.cpp
+++ b/source/blender/simulations/bparticles/forces.cpp
@@ -26,7 +26,7 @@ void TurbulenceForce::add_force(ForceInterface &interface)
   ParticlesBlock &block = interface.block();
   ArrayRef<float3> destination = interface.combined_destination();
 
-  auto positions = block.attributes().get_float3("Position");
+  auto positions = block.attributes().get<float3>("Position");
 
   auto inputs = m_co

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list