triangle_intersector_pluecker.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "triangle.h"
  18. #include "trianglev.h"
  19. #include "trianglev_mb.h"
  20. #include "intersector_epilog.h"
  21. /*! Modified Pluecker ray/triangle intersector. The test first shifts
  22. * the ray origin into the origin of the coordinate system and then
  23. * uses Pluecker coordinates for the intersection. Due to the shift,
  24. * the Pluecker coordinate calculation simplifies and the tests get
  25. * numerically stable. The edge equations are watertight along the
  26. * edge for neighboring triangles. */
  27. namespace embree
  28. {
  29. namespace isa
  30. {
  31. template<int M, typename UVMapper>
  32. struct PlueckerHitM
  33. {
  34. __forceinline PlueckerHitM(const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const vfloat<M>& den, const Vec3<vfloat<M>>& Ng, const UVMapper& mapUV)
  35. : U(U), V(V), T(T), den(den), mapUV(mapUV), vNg(Ng) {}
  36. __forceinline void finalize()
  37. {
  38. const vfloat<M> rcpDen = rcp(den);
  39. vt = T * rcpDen;
  40. vu = U * rcpDen;
  41. vv = V * rcpDen;
  42. mapUV(vu,vv);
  43. }
  44. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  45. __forceinline float t (const size_t i) const { return vt[i]; }
  46. __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }
  47. private:
  48. const vfloat<M> U;
  49. const vfloat<M> V;
  50. const vfloat<M> T;
  51. const vfloat<M> den;
  52. const UVMapper& mapUV;
  53. public:
  54. vfloat<M> vu;
  55. vfloat<M> vv;
  56. vfloat<M> vt;
  57. Vec3<vfloat<M>> vNg;
  58. };
  59. template<int M>
  60. struct PlueckerIntersector1
  61. {
  62. __forceinline PlueckerIntersector1() {}
  63. __forceinline PlueckerIntersector1(const Ray& ray, const void* ptr) {}
  64. template<typename UVMapper, typename Epilog>
  65. __forceinline bool intersect(Ray& ray,
  66. const Vec3<vfloat<M>>& tri_v0,
  67. const Vec3<vfloat<M>>& tri_v1,
  68. const Vec3<vfloat<M>>& tri_v2,
  69. const UVMapper& mapUV,
  70. const Epilog& epilog) const
  71. {
  72. /* calculate vertices relative to ray origin */
  73. typedef Vec3<vfloat<M>> Vec3vfM;
  74. const Vec3vfM O = Vec3vfM(ray.org);
  75. const Vec3vfM D = Vec3vfM(ray.dir);
  76. const Vec3vfM v0 = tri_v0-O;
  77. const Vec3vfM v1 = tri_v1-O;
  78. const Vec3vfM v2 = tri_v2-O;
  79. /* calculate triangle edges */
  80. const Vec3vfM e0 = v2-v0;
  81. const Vec3vfM e1 = v0-v1;
  82. const Vec3vfM e2 = v1-v2;
  83. /* perform edge tests */
  84. const vfloat<M> U = dot(cross(v2+v0,e0),D);
  85. const vfloat<M> V = dot(cross(v0+v1,e1),D);
  86. const vfloat<M> W = dot(cross(v1+v2,e2),D);
  87. #if defined(EMBREE_BACKFACE_CULLING)
  88. const vfloat<M> maxUVW = max(U,V,W);
  89. vbool<M> valid = maxUVW <= 0.0f;
  90. #else
  91. const vfloat<M> minUVW = min(U,V,W);
  92. const vfloat<M> maxUVW = max(U,V,W);
  93. vbool<M> valid = (minUVW >= 0.0f) | (maxUVW <= 0.0f);
  94. #endif
  95. if (unlikely(none(valid))) return false;
  96. /* calculate geometry normal and denominator */
  97. const Vec3vfM Ng = stable_triangle_normal(e2,e1,e0);
  98. const vfloat<M> den = twice(dot(Ng,D));
  99. const vfloat<M> absDen = abs(den);
  100. const vfloat<M> sgnDen = signmsk(den);
  101. /* perform depth test */
  102. const vfloat<M> T = twice(dot(v0,Ng));
  103. valid &= ((T^sgnDen) >= absDen*vfloat<M>(ray.tnear));
  104. valid &=(absDen*vfloat<M>(ray.tfar) >= (T^sgnDen));
  105. if (unlikely(none(valid))) return false;
  106. /* avoid division by 0 */
  107. valid &= den != vfloat<M>(zero);
  108. if (unlikely(none(valid))) return false;
  109. /* update hit information */
  110. PlueckerHitM<M,UVMapper> hit(U,V,T,den,Ng,mapUV);
  111. return epilog(valid,hit);
  112. }
  113. };
  114. template<int K, typename UVMapper>
  115. struct PlueckerHitK
  116. {
  117. __forceinline PlueckerHitK(const vfloat<K>& U, const vfloat<K>& V, const vfloat<K>& T, const vfloat<K>& den, const Vec3<vfloat<K>>& Ng, const UVMapper& mapUV)
  118. : U(U), V(V), T(T), den(den), Ng(Ng), mapUV(mapUV) {}
  119. __forceinline std::tuple<vfloat<K>,vfloat<K>,vfloat<K>,Vec3<vfloat<K>>> operator() () const
  120. {
  121. const vfloat<K> rcpDen = rcp(den);
  122. const vfloat<K> t = T * rcpDen;
  123. vfloat<K> u = U * rcpDen;
  124. vfloat<K> v = V * rcpDen;
  125. mapUV(u,v);
  126. return std::make_tuple(u,v,t,Ng);
  127. }
  128. private:
  129. const vfloat<K> U;
  130. const vfloat<K> V;
  131. const vfloat<K> T;
  132. const vfloat<K> den;
  133. const Vec3<vfloat<K>> Ng;
  134. const UVMapper& mapUV;
  135. };
  136. template<int M, int K>
  137. struct PlueckerIntersectorK
  138. {
  139. __forceinline PlueckerIntersectorK(const vbool<K>& valid, const RayK<K>& ray) {}
  140. /*! Intersects K rays with one of M triangles. */
  141. template<typename UVMapper, typename Epilog>
  142. __forceinline vbool<K> intersectK(const vbool<K>& valid0,
  143. RayK<K>& ray,
  144. const Vec3<vfloat<K>>& tri_v0,
  145. const Vec3<vfloat<K>>& tri_v1,
  146. const Vec3<vfloat<K>>& tri_v2,
  147. const UVMapper& mapUV,
  148. const Epilog& epilog) const
  149. {
  150. /* calculate vertices relative to ray origin */
  151. typedef Vec3<vfloat<K>> Vec3vfK;
  152. vbool<K> valid = valid0;
  153. const Vec3vfK O = ray.org;
  154. const Vec3vfK D = ray.dir;
  155. const Vec3vfK v0 = tri_v0-O;
  156. const Vec3vfK v1 = tri_v1-O;
  157. const Vec3vfK v2 = tri_v2-O;
  158. /* calculate triangle edges */
  159. const Vec3vfK e0 = v2-v0;
  160. const Vec3vfK e1 = v0-v1;
  161. const Vec3vfK e2 = v1-v2;
  162. /* perform edge tests */
  163. const vfloat<K> U = dot(Vec3vfK(cross(v2+v0,e0)),D);
  164. const vfloat<K> V = dot(Vec3vfK(cross(v0+v1,e1)),D);
  165. const vfloat<K> W = dot(Vec3vfK(cross(v1+v2,e2)),D);
  166. #if defined(EMBREE_BACKFACE_CULLING)
  167. const vfloat<K> maxUVW = max(U,V,W);
  168. valid &= maxUVW <= 0.0f;
  169. #else
  170. const vfloat<K> minUVW = min(U,V,W);
  171. const vfloat<K> maxUVW = max(U,V,W);
  172. valid &= (minUVW >= 0.0f) | (maxUVW <= 0.0f);
  173. #endif
  174. if (unlikely(none(valid))) return false;
  175. /* calculate geometry normal and denominator */
  176. const Vec3vfK Ng = stable_triangle_normal(e2,e1,e0);
  177. const vfloat<K> den = twice(dot(Vec3vfK(Ng),D));
  178. const vfloat<K> absDen = abs(den);
  179. const vfloat<K> sgnDen = signmsk(den);
  180. /* perform depth test */
  181. const vfloat<K> T = twice(dot(v0,Vec3vfK(Ng)));
  182. valid &= ((T^sgnDen) >= absDen*ray.tnear);
  183. valid &= (absDen*ray.tfar >= (T^sgnDen));
  184. if (unlikely(none(valid))) return false;
  185. /* avoid division by 0 */
  186. valid &= den != vfloat<K>(zero);
  187. if (unlikely(none(valid))) return false;
  188. /* calculate hit information */
  189. PlueckerHitK<K,UVMapper> hit(U,V,T,den,Ng,mapUV);
  190. return epilog(valid,hit);
  191. }
  192. /*! Intersect k'th ray from ray packet of size K with M triangles. */
  193. template<typename UVMapper, typename Epilog>
  194. __forceinline bool intersect(RayK<K>& ray, size_t k,
  195. const Vec3<vfloat<M>>& tri_v0,
  196. const Vec3<vfloat<M>>& tri_v1,
  197. const Vec3<vfloat<M>>& tri_v2,
  198. const UVMapper& mapUV,
  199. const Epilog& epilog) const
  200. {
  201. /* calculate vertices relative to ray origin */
  202. typedef Vec3<vfloat<M>> Vec3vfM;
  203. const Vec3vfM O = broadcast<vfloat<M>>(ray.org,k);
  204. const Vec3vfM D = broadcast<vfloat<M>>(ray.dir,k);
  205. const Vec3vfM v0 = tri_v0-O;
  206. const Vec3vfM v1 = tri_v1-O;
  207. const Vec3vfM v2 = tri_v2-O;
  208. /* calculate triangle edges */
  209. const Vec3vfM e0 = v2-v0;
  210. const Vec3vfM e1 = v0-v1;
  211. const Vec3vfM e2 = v1-v2;
  212. /* perform edge tests */
  213. const vfloat<M> U = dot(cross(v2+v0,e0),D);
  214. const vfloat<M> V = dot(cross(v0+v1,e1),D);
  215. const vfloat<M> W = dot(cross(v1+v2,e2),D);
  216. #if defined(EMBREE_BACKFACE_CULLING)
  217. const vfloat<M> maxUVW = max(U,V,W);
  218. vbool<M> valid = maxUVW <= 0.0f;
  219. #else
  220. const vfloat<M> minUVW = min(U,V,W);
  221. const vfloat<M> maxUVW = max(U,V,W);
  222. vbool<M> valid = (minUVW >= 0.0f) | (maxUVW <= 0.0f);
  223. #endif
  224. if (unlikely(none(valid))) return false;
  225. /* calculate geometry normal and denominator */
  226. const Vec3vfM Ng = stable_triangle_normal(e2,e1,e0);
  227. const vfloat<M> den = twice(dot(Ng,D));
  228. const vfloat<M> absDen = abs(den);
  229. const vfloat<M> sgnDen = signmsk(den);
  230. /* perform depth test */
  231. const vfloat<M> T = twice(dot(v0,Ng));
  232. valid &= ((T^sgnDen) >= absDen*vfloat<M>(ray.tnear[k]));
  233. valid &= (absDen*vfloat<M>(ray.tfar[k]) >= (T^sgnDen));
  234. if (unlikely(none(valid))) return false;
  235. /* avoid division by 0 */
  236. valid &= den != vfloat<M>(zero);
  237. if (unlikely(none(valid))) return false;
  238. /* calculate hit information */
  239. PlueckerHitM<M,UVMapper> hit(U,V,T,den,Ng,mapUV);
  240. return epilog(valid,hit);
  241. }
  242. };
  243. }
  244. }