cylinder.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "../common/ray.h"
  18. namespace embree
  19. {
  20. namespace isa
  21. {
  22. struct Cylinder
  23. {
  24. const Vec3fa p0; //!< start location
  25. const Vec3fa p1; //!< end position
  26. const float rr; //!< squared radius of cylinder
  27. __forceinline Cylinder(const Vec3fa& p0, const Vec3fa& p1, const float r)
  28. : p0(p0), p1(p1), rr(sqr(r)) {}
  29. __forceinline Cylinder(const Vec3fa& p0, const Vec3fa& p1, const float rr, bool)
  30. : p0(p0), p1(p1), rr(rr) {}
  31. __forceinline bool intersect(const Vec3fa& org,
  32. const Vec3fa& dir,
  33. BBox1f& t_o,
  34. float& u0_o, Vec3fa& Ng0_o,
  35. float& u1_o, Vec3fa& Ng1_o) const
  36. {
  37. /* calculate quadratic equation to solve */
  38. const float rl = rcp_length(p1-p0);
  39. const Vec3fa P0 = p0, dP = (p1-p0)*rl;
  40. const Vec3fa O = org-P0, dO = dir;
  41. const float dOdO = dot(dO,dO);
  42. const float OdO = dot(dO,O);
  43. const float OO = dot(O,O);
  44. const float dOz = dot(dP,dO);
  45. const float Oz = dot(dP,O);
  46. const float A = dOdO - sqr(dOz);
  47. const float B = 2.0f * (OdO - dOz*Oz);
  48. const float C = OO - sqr(Oz) - rr;
  49. /* we miss the cylinder if determinant is smaller than zero */
  50. const float D = B*B - 4.0f*A*C;
  51. if (D < 0.0f) {
  52. t_o = BBox1f(pos_inf,neg_inf);
  53. return false;
  54. }
  55. /* special case for rays that are parallel to the cylinder */
  56. const float eps = 16.0f*float(ulp)*max(abs(dOdO),abs(sqr(dOz)));
  57. if (abs(A) < eps)
  58. {
  59. if (C <= 0.0f) {
  60. t_o = BBox1f(neg_inf,pos_inf);
  61. return true;
  62. } else {
  63. t_o = BBox1f(pos_inf,neg_inf);
  64. return false;
  65. }
  66. }
  67. /* standard case for rays that are not parallel to the cylinder */
  68. const float Q = sqrt(D);
  69. const float rcp_2A = rcp(2.0f*A);
  70. const float t0 = (-B-Q)*rcp_2A;
  71. const float t1 = (-B+Q)*rcp_2A;
  72. /* calculates u and Ng for near hit */
  73. {
  74. u0_o = madd(t0,dOz,Oz)*rl;
  75. const Vec3fa Pr = t_o.lower*dir;
  76. const Vec3fa Pl = madd(u0_o,p1-p0,p0);
  77. Ng0_o = Pr-Pl;
  78. }
  79. /* calculates u and Ng for far hit */
  80. {
  81. u1_o = madd(t1,dOz,Oz)*rl;
  82. const Vec3fa Pr = t_o.lower*dir;
  83. const Vec3fa Pl = madd(u1_o,p1-p0,p0);
  84. Ng1_o = Pr-Pl;
  85. }
  86. t_o.lower = t0;
  87. t_o.upper = t1;
  88. return true;
  89. }
  90. __forceinline bool intersect(const Vec3fa& org_i, const Vec3fa& dir, BBox1f& t_o) const
  91. {
  92. float u0_o; Vec3fa Ng0_o;
  93. float u1_o; Vec3fa Ng1_o;
  94. return intersect(org_i,dir,t_o,u0_o,Ng0_o,u1_o,Ng1_o);
  95. }
  96. static bool verify(const size_t id, const Cylinder& cylinder, const Ray& ray, bool shouldhit, const float t0, const float t1)
  97. {
  98. float eps = 0.001f;
  99. BBox1f t; bool hit;
  100. hit = cylinder.intersect(ray.org,ray.dir,t);
  101. bool failed = hit != shouldhit;
  102. if (shouldhit) failed |= std::isinf(t0) ? t0 != t.lower : abs(t0-t.lower) > eps;
  103. if (shouldhit) failed |= std::isinf(t1) ? t1 != t.upper : abs(t1-t.upper) > eps;
  104. if (!failed) return true;
  105. std::cout << "Cylinder test " << id << " failed: cylinder = " << cylinder << ", ray = " << ray << ", hit = " << hit << ", t = " << t << std::endl;
  106. return false;
  107. }
  108. /* verify cylinder class */
  109. static bool verify()
  110. {
  111. bool passed = true;
  112. const Cylinder cylinder(Vec3fa(0.0f,0.0f,0.0f),Vec3fa(1.0f,0.0f,0.0f),1.0f);
  113. passed &= verify(0,cylinder,Ray(Vec3fa(-2.0f,1.0f,0.0f),Vec3fa( 0.0f,-1.0f,+0.0f),0.0f,float(inf)),true,0.0f,2.0f);
  114. passed &= verify(1,cylinder,Ray(Vec3fa(+2.0f,1.0f,0.0f),Vec3fa( 0.0f,-1.0f,+0.0f),0.0f,float(inf)),true,0.0f,2.0f);
  115. passed &= verify(2,cylinder,Ray(Vec3fa(+2.0f,1.0f,2.0f),Vec3fa( 0.0f,-1.0f,+0.0f),0.0f,float(inf)),false,0.0f,0.0f);
  116. passed &= verify(3,cylinder,Ray(Vec3fa(+0.0f,0.0f,0.0f),Vec3fa( 1.0f, 0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,pos_inf);
  117. passed &= verify(4,cylinder,Ray(Vec3fa(+0.0f,0.0f,0.0f),Vec3fa(-1.0f, 0.0f,+0.0f),0.0f,float(inf)),true,neg_inf,pos_inf);
  118. passed &= verify(5,cylinder,Ray(Vec3fa(+0.0f,2.0f,0.0f),Vec3fa( 1.0f, 0.0f,+0.0f),0.0f,float(inf)),false,pos_inf,neg_inf);
  119. passed &= verify(6,cylinder,Ray(Vec3fa(+0.0f,2.0f,0.0f),Vec3fa(-1.0f, 0.0f,+0.0f),0.0f,float(inf)),false,pos_inf,neg_inf);
  120. return passed;
  121. }
  122. /*! output operator */
  123. friend __forceinline std::ostream& operator<<(std::ostream& cout, const Cylinder& c) {
  124. return cout << "Cylinder { p0 = " << c.p0 << ", p1 = " << c.p1 << ", r = " << sqrtf(c.rr) << "}";
  125. }
  126. };
  127. template<int N>
  128. struct CylinderN
  129. {
  130. typedef Vec3<vfloat<N>> Vec3vfN;
  131. const Vec3vfN p0; //!< start location
  132. const Vec3vfN p1; //!< end position
  133. const vfloat<N> rr; //!< squared radius of cylinder
  134. __forceinline CylinderN(const Vec3vfN& p0, const Vec3vfN& p1, const vfloat<N>& r)
  135. : p0(p0), p1(p1), rr(sqr(r)) {}
  136. __forceinline CylinderN(const Vec3vfN& p0, const Vec3vfN& p1, const vfloat<N>& rr, bool)
  137. : p0(p0), p1(p1), rr(rr) {}
  138. __forceinline vbool<N> intersect(const Vec3fa& org, const Vec3fa& dir,
  139. BBox<vfloat<N>>& t_o,
  140. vfloat<N>& u0_o, Vec3vfN& Ng0_o,
  141. vfloat<N>& u1_o, Vec3vfN& Ng1_o) const
  142. {
  143. /* calculate quadratic equation to solve */
  144. const vfloat<N> rl = rcp_length(p1-p0);
  145. const Vec3vfN P0 = p0, dP = (p1-p0)*rl;
  146. const Vec3vfN O = Vec3vfN(org)-P0, dO = dir;
  147. const vfloat<N> dOdO = dot(dO,dO);
  148. const vfloat<N> OdO = dot(dO,O);
  149. const vfloat<N> OO = dot(O,O);
  150. const vfloat<N> dOz = dot(dP,dO);
  151. const vfloat<N> Oz = dot(dP,O);
  152. const vfloat<N> A = dOdO - sqr(dOz);
  153. const vfloat<N> B = 2.0f * (OdO - dOz*Oz);
  154. const vfloat<N> C = OO - sqr(Oz) - rr;
  155. /* we miss the cylinder if determinant is smaller than zero */
  156. const vfloat<N> D = B*B - 4.0f*A*C;
  157. vbool<N> valid = D >= 0.0f;
  158. if (none(valid)) {
  159. t_o = BBox<vfloat<N>>(empty);
  160. return valid;
  161. }
  162. /* standard case for rays that are not parallel to the cylinder */
  163. const vfloat<N> Q = sqrt(D);
  164. const vfloat<N> rcp_2A = rcp(2.0f*A);
  165. const vfloat<N> t0 = (-B-Q)*rcp_2A;
  166. const vfloat<N> t1 = (-B+Q)*rcp_2A;
  167. /* calculates u and Ng for near hit */
  168. {
  169. u0_o = madd(t0,dOz,Oz)*rl;
  170. const Vec3vfN Pr = t0*Vec3vfN(dir);
  171. const Vec3vfN Pl = madd(u0_o,p1-p0,p0);
  172. Ng0_o = Pr-Pl;
  173. }
  174. /* calculates u and Ng for far hit */
  175. {
  176. u1_o = madd(t1,dOz,Oz)*rl;
  177. const Vec3vfN Pr = t1*Vec3vfN(dir);
  178. const Vec3vfN Pl = madd(u1_o,p1-p0,p0);
  179. Ng1_o = Pr-Pl;
  180. }
  181. t_o.lower = select(valid, t0, vfloat<N>(pos_inf));
  182. t_o.upper = select(valid, t1, vfloat<N>(neg_inf));
  183. /* special case for rays that are parallel to the cylinder */
  184. const vfloat<N> eps = 16.0f*float(ulp)*max(abs(dOdO),abs(sqr(dOz)));
  185. vbool<N> validt = valid & (abs(A) < eps);
  186. if (unlikely(any(validt)))
  187. {
  188. vbool<N> inside = C <= 0.0f;
  189. t_o.lower = select(validt,select(inside,vfloat<N>(neg_inf),vfloat<N>(pos_inf)),t_o.lower);
  190. t_o.upper = select(validt,select(inside,vfloat<N>(pos_inf),vfloat<N>(neg_inf)),t_o.upper);
  191. valid &= inside;
  192. }
  193. return valid;
  194. }
  195. __forceinline vbool<N> intersect(const Vec3fa& org_i, const Vec3fa& dir, BBox<vfloat<N>>& t_o) const
  196. {
  197. vfloat<N> u0_o; Vec3vfN Ng0_o;
  198. vfloat<N> u1_o; Vec3vfN Ng1_o;
  199. return intersect(org_i,dir,t_o,u0_o,Ng0_o,u1_o,Ng1_o);
  200. }
  201. };
  202. }
  203. }