[Bf-blender-cvs] [e097116072e] geometry-nodes: Geometry Nodes: Add utility to position point randomly on 3D triangle

Hans Goudey noreply at git.blender.org
Tue Oct 27 05:00:40 CET 2020


Commit: e097116072e90f0501a2e70c1d2e29fbeba72ead
Author: Hans Goudey
Date:   Mon Oct 26 22:44:35 2020 -0500
Branches: geometry-nodes
https://developer.blender.org/rBe097116072e90f0501a2e70c1d2e29fbeba72ead

Geometry Nodes: Add utility to position point randomly on 3D triangle

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

M	source/blender/blenlib/BLI_rand.h
M	source/blender/blenlib/BLI_rand.hh
M	source/blender/blenlib/intern/rand.cc

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

diff --git a/source/blender/blenlib/BLI_rand.h b/source/blender/blenlib/BLI_rand.h
index 78a323c2431..ec74cef9311 100644
--- a/source/blender/blenlib/BLI_rand.h
+++ b/source/blender/blenlib/BLI_rand.h
@@ -60,6 +60,12 @@ void BLI_rng_get_tri_sample_float_v2(struct RNG *rng,
                                      const float v2[2],
                                      const float v3[2],
                                      float r_pt[2]) ATTR_NONNULL();
+void BLI_rng_get_tri_sample_float_v3(RNG *rng,
+                                     const float v1[3],
+                                     const float v2[3],
+                                     const float v3[3],
+                                     float r_pt[3]) ATTR_NONNULL();
+
 void BLI_rng_shuffle_array(struct RNG *rng,
                            void *data,
                            unsigned int elem_size_i,
diff --git a/source/blender/blenlib/BLI_rand.hh b/source/blender/blenlib/BLI_rand.hh
index 1b321bd7bcd..0c0dd464d4d 100644
--- a/source/blender/blenlib/BLI_rand.hh
+++ b/source/blender/blenlib/BLI_rand.hh
@@ -118,6 +118,7 @@ class RandomNumberGenerator {
   float2 get_unit_float2();
   float3 get_unit_float3();
   float2 get_triangle_sample(float2 v1, float2 v2, float2 v3);
+  float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3);
   void get_bytes(MutableSpan<char> r_bytes);
 
   /**
diff --git a/source/blender/blenlib/intern/rand.cc b/source/blender/blenlib/intern/rand.cc
index 224db31471d..c61e17e6627 100644
--- a/source/blender/blenlib/intern/rand.cc
+++ b/source/blender/blenlib/intern/rand.cc
@@ -141,6 +141,12 @@ void BLI_rng_get_tri_sample_float_v2(
   copy_v2_v2(r_pt, rng->rng.get_triangle_sample(v1, v2, v3));
 }
 
+void BLI_rng_get_tri_sample_float_v3(
+    RNG *rng, const float v1[3], const float v2[3], const float v3[3], float r_pt[3])
+{
+  copy_v3_v3(r_pt, rng->rng.get_triangle_sample_3d(v1, v2, v3));
+}
+
 void BLI_rng_shuffle_array(RNG *rng, void *data, unsigned int elem_size_i, unsigned int elem_tot)
 {
   const uint elem_size = elem_size_i;
@@ -425,6 +431,25 @@ float2 RandomNumberGenerator::get_triangle_sample(float2 v1, float2 v2, float2 v
   return sample;
 }
 
+float3 RandomNumberGenerator::get_triangle_sample_3d(float3 v1, float3 v2, float3 v3)
+{
+  float u = this->get_float();
+  float v = this->get_float();
+
+  if (u + v > 1.0f) {
+    u = 1.0f - u;
+    v = 1.0f - v;
+  }
+
+  float3 side_u = v2 - v1;
+  float3 side_v = v3 - v1;
+
+  float3 sample = v1;
+  sample += side_u * u;
+  sample += side_v * v;
+  return sample;
+}
+
 void RandomNumberGenerator::get_bytes(MutableSpan<char> r_bytes)
 {
   constexpr int64_t mask_bytes = 2;



More information about the Bf-blender-cvs mailing list