Commit: 2e72d756c4909cd77f9313909159e368a26ccc92
Author: Bastien Montagne
Date:   Fri Jan 9 11:54:51 2015 +0100
Branches: master
https://developer.blender.org/rB2e72d756c4909cd77f9313909159e368a26ccc92

BLI_rand: add a function returning a random point whithin given 2D triangle.

Needed by transfer data.

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

M       source/blender/blenlib/BLI_rand.h
M       source/blender/blenlib/intern/rand.c

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

diff --git a/source/blender/blenlib/BLI_rand.h 
b/source/blender/blenlib/BLI_rand.h
index 879af44..7b84ddd 100644
--- a/source/blender/blenlib/BLI_rand.h
+++ b/source/blender/blenlib/BLI_rand.h
@@ -55,6 +55,9 @@ double      BLI_rng_get_double(struct RNG *rng) 
ATTR_WARN_UNUSED_RESULT ATTR_NON
 float       BLI_rng_get_float(struct RNG *rng) ATTR_WARN_UNUSED_RESULT 
ATTR_NONNULL(1);
 void        BLI_rng_get_float_unit_v2(struct RNG *rng, float v[2]) 
ATTR_NONNULL(1, 2);
 void        BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]) 
ATTR_NONNULL(1, 2);
+void        BLI_rng_get_tri_sample_float_v2(
+        struct RNG *rng, const float v1[2], const float v2[2], const float 
v3[2],
+        float r_pt[2]) ATTR_NONNULL();
 void        BLI_rng_shuffle_array(struct RNG *rng, void *data, unsigned int 
elem_size_i, unsigned int elem_tot) ATTR_NONNULL(1, 2);
 
 /** Note that skipping is as slow as generating n numbers! */
diff --git a/source/blender/blenlib/intern/rand.c 
b/source/blender/blenlib/intern/rand.c
index 59ccf38..7657cec 100644
--- a/source/blender/blenlib/intern/rand.c
+++ b/source/blender/blenlib/intern/rand.c
@@ -144,6 +144,31 @@ void BLI_rng_get_float_unit_v3(RNG *rng, float v[3])
        }
 }
 
+/**
+ * Generate a random point inside given tri.
+ */
+void BLI_rng_get_tri_sample_float_v2(
+        RNG *rng, const float v1[2], const float v2[2], const float v3[2],
+        float r_pt[2])
+{
+       float u = BLI_rng_get_float(rng);
+       float v = BLI_rng_get_float(rng);
+
+       float side_u[2], side_v[2];
+
+       if ((u + v) > 1.0f) {
+               u = 1.0f - u;
+               v = 1.0f - v;
+       }
+
+       sub_v2_v2v2(side_u, v2, v1);
+       sub_v2_v2v2(side_v, v3, v1);
+
+       copy_v2_v2(r_pt, v1);
+       madd_v2_v2fl(r_pt, side_u, u);
+       madd_v2_v2fl(r_pt, side_v, v);
+}
+
 void BLI_rng_shuffle_array(RNG *rng, void *data, unsigned int elem_size_i, 
unsigned int elem_tot)
 {
        const size_t elem_size = (unsigned int)elem_size_i;

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to