Browse Source

random point sampling done right

Former-commit-id: 0da87dcb0f885e9df75ce58734d7bc88f3683982
Kenshi Takayama (kenshi 11 years ago
parent
commit
5f7b333f66
2 changed files with 16 additions and 10 deletions
  1. 7 0
      include/igl/Camera.h
  2. 9 10
      include/igl/embree/orient_outward_ao.cpp

+ 7 - 0
include/igl/Camera.h

@@ -1,5 +1,12 @@
 #ifndef IGL_CAMERA_H
 #define IGL_CAMERA_H
+
+// you're idiot, M$!
+#if defined(_WIN32)
+#undef far
+#undef near
+#endif
+
 #include <Eigen/Geometry>
 #include <Eigen/Core>
 

+ 9 - 10
include/igl/embree/orient_outward_ao.cpp

@@ -110,16 +110,15 @@ IGL_INLINE void igl::orient_outward_ao(
     for (int i = 0; i < num_rays_per_component[c]; ++i)
     {
       int f    = CF[ddist(prng)];    // select face with probability proportional to face area
-      float t0 = rdist(prng);        // random barycentric coordinate
-      float t1 = rdist(prng);
-      float t2 = rdist(prng);
-      float t_sum = t0 + t1 + t2;
-      t0 /= t_sum;
-      t1 /= t_sum;
-      t2 /= t_sum;
-      Vector3f p = t0 * V.row(F(f,0)).template cast<float>().eval()       // be careful with the index!!!
-                 + t1 * V.row(F(f,1)).template cast<float>().eval()
-                 + t2 * V.row(F(f,2)).template cast<float>().eval();
+      float s = rdist(prng);            // random barycentric coordinate (reference: Generating Random Points in Triangles [Turk, Graphics Gems I 1990])
+      float t = rdist(prng);
+      float sqrt_t = sqrtf(t);
+      float a = 1 - sqrt_t;
+      float b = (1 - s) * sqrt_t;
+      float c = s * sqrt_t;
+      Vector3f p = a * V.row(F(f,0)).template cast<float>().eval()       // be careful with the index!!!
+                 + b * V.row(F(f,1)).template cast<float>().eval()
+                 + c * V.row(F(f,2)).template cast<float>().eval();
       Vector3f n = N.row(f).cast<float>();
       // random direction in hemisphere around n (avoid too grazing angle)
       Vector3f d;