ray.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math32
  5. // Ray represents an oriented 3D line segment defined by an origin point and a direction vector.
  6. type Ray struct {
  7. origin Vector3
  8. direction Vector3
  9. }
  10. // NewRay creates and returns a pointer to a Ray object with
  11. // the specified origin and direction vectors.
  12. // If a nil pointer is supplied for any of the parameters,
  13. // the zero vector will be used.
  14. func NewRay(origin *Vector3, direction *Vector3) *Ray {
  15. ray := new(Ray)
  16. if origin != nil {
  17. ray.origin = *origin
  18. }
  19. if direction != nil {
  20. ray.direction = *direction
  21. }
  22. return ray
  23. }
  24. // Set sets the origin and direction vectors of this Ray.
  25. func (ray *Ray) Set(origin, direction *Vector3) *Ray {
  26. ray.origin = *origin
  27. ray.direction = *direction
  28. return ray
  29. }
  30. // Copy copies other ray into this one.
  31. func (ray *Ray) Copy(other *Ray) *Ray {
  32. *ray = *other
  33. return ray
  34. }
  35. // Origin returns a copy of this ray current origin.
  36. func (ray *Ray) Origin() Vector3 {
  37. return ray.origin
  38. }
  39. // Direction returns a copy of this ray current direction.
  40. func (ray *Ray) Direction() Vector3 {
  41. return ray.direction
  42. }
  43. // At calculates the point in the ray which is at the specified t distance from the origin
  44. // along its direction.
  45. // The calculated point is stored in optionalTarget, if not nil, and also returned.
  46. func (ray *Ray) At(t float32, optionalTarget *Vector3) *Vector3 {
  47. var result *Vector3
  48. if optionalTarget != nil {
  49. result = optionalTarget
  50. } else {
  51. result = &Vector3{}
  52. }
  53. return result.Copy(&ray.direction).MultiplyScalar(t).Add(&ray.origin)
  54. }
  55. // Recast sets the new origin of the ray at the specified distance t
  56. // from its origin along its direction.
  57. func (ray *Ray) Recast(t float32) *Ray {
  58. var v1 Vector3
  59. ray.origin.Copy(ray.At(t, &v1))
  60. return ray
  61. }
  62. // ClosestPointToPoint calculates the point in the ray which is closest to the specified point.
  63. // The calculated point is stored in optionalTarget, if not nil, and also returned.
  64. func (ray *Ray) ClosestPointToPoint(point, optionalTarget *Vector3) *Vector3 {
  65. var result *Vector3
  66. if optionalTarget != nil {
  67. result = optionalTarget
  68. } else {
  69. result = NewVector3(0, 0, 0)
  70. }
  71. result.SubVectors(point, &ray.origin)
  72. directionDistance := result.Dot(&ray.direction)
  73. if directionDistance < 0 {
  74. return result.Copy(&ray.origin)
  75. }
  76. return result.Copy(&ray.direction).MultiplyScalar(directionDistance).Add(&ray.origin)
  77. }
  78. // DistanceToPoint returns the smallest distance
  79. // from the ray direction vector to the specified point.
  80. func (ray *Ray) DistanceToPoint(point *Vector3) float32 {
  81. return Sqrt(ray.DistanceSqToPoint(point))
  82. }
  83. // DistanceSqToPoint returns the smallest squared distance
  84. // from the ray direction vector to the specified point.
  85. // If the ray was pointed directly at the point this distance would be 0.
  86. func (ray *Ray) DistanceSqToPoint(point *Vector3) float32 {
  87. var v1 Vector3
  88. directionDistance := v1.SubVectors(point, &ray.origin).Dot(&ray.direction)
  89. // point behind the ray
  90. if directionDistance < 0 {
  91. return ray.origin.DistanceTo(point)
  92. }
  93. v1.Copy(&ray.direction).MultiplyScalar(directionDistance).Add(&ray.origin)
  94. return v1.DistanceToSquared(point)
  95. }
  96. // DistanceSqToSegment returns the smallest squared distance
  97. // from this ray to the line segment from v0 to v1.
  98. // If optionalPointOnRay Vector3 is not nil,
  99. // it is set with the coordinates of the point on the ray.
  100. // if optionalPointOnSegment Vector3 is not nil,
  101. // it is set with the coordinates of the point on the segment.
  102. func (ray *Ray) DistanceSqToSegment(v0, v1, optionalPointOnRay, optionalPointOnSegment *Vector3) float32 {
  103. var segCenter Vector3
  104. var segDir Vector3
  105. var diff Vector3
  106. segCenter.Copy(v0).Add(v1).MultiplyScalar(0.5)
  107. segDir.Copy(v1).Sub(v0).Normalize()
  108. diff.Copy(&ray.origin).Sub(&segCenter)
  109. segExtent := v0.DistanceTo(v1) * 0.5
  110. a01 := -ray.direction.Dot(&segDir)
  111. b0 := diff.Dot(&ray.direction)
  112. b1 := -diff.Dot(&segDir)
  113. c := diff.LengthSq()
  114. det := Abs(1 - a01*a01)
  115. var s0, s1, sqrDist, extDet float32
  116. if det > 0 {
  117. // The ray and segment are not parallel.
  118. s0 = a01*b1 - b0
  119. s1 = a01*b0 - b1
  120. extDet = segExtent * det
  121. if s0 >= 0 {
  122. if s1 >= -extDet {
  123. if s1 <= extDet {
  124. // region 0
  125. // Minimum at interior points of ray and segment.
  126. invDet := 1 / det
  127. s0 *= invDet
  128. s1 *= invDet
  129. sqrDist = s0*(s0+a01*s1+2*b0) + s1*(a01*s0+s1+2*b1) + c
  130. } else {
  131. // region 1
  132. s1 = segExtent
  133. s0 = Max(0, -(a01*s1 + b0))
  134. sqrDist = -s0*s0 + s1*(s1+2*b1) + c
  135. }
  136. } else {
  137. // region 5
  138. s1 = -segExtent
  139. s0 = Max(0, -(a01*s1 + b0))
  140. sqrDist = -s0*s0 + s1*(s1+2*b1) + c
  141. }
  142. } else {
  143. if s1 <= -extDet {
  144. // region 4
  145. s0 = Max(0, -(-a01*segExtent + b0))
  146. if s0 > 0 {
  147. s1 = -segExtent
  148. } else {
  149. s1 = Min(Max(-segExtent, -b1), segExtent)
  150. }
  151. sqrDist = -s0*s0 + s1*(s1+2*b1) + c
  152. } else if s1 <= extDet {
  153. // region 3
  154. s0 = 0
  155. s1 = Min(Max(-segExtent, -b1), segExtent)
  156. sqrDist = s1*(s1+2*b1) + c
  157. } else {
  158. // region 2
  159. s0 = Max(0, -(a01*segExtent + b0))
  160. if s0 > 0 {
  161. s1 = segExtent
  162. } else {
  163. s1 = Min(Max(-segExtent, -b1), segExtent)
  164. }
  165. sqrDist = -s0*s0 + s1*(s1+2*b1) + c
  166. }
  167. }
  168. } else {
  169. // Ray and segment are parallel.
  170. if a01 > 0 {
  171. s1 = -segExtent
  172. } else {
  173. s1 = segExtent
  174. }
  175. s0 = Max(0, -(a01*s1 + b0))
  176. sqrDist = -s0*s0 + s1*(s1+2*b1) + c
  177. }
  178. if optionalPointOnRay != nil {
  179. optionalPointOnRay.Copy(&ray.direction).MultiplyScalar(s0).Add(&ray.origin)
  180. }
  181. if optionalPointOnSegment != nil {
  182. optionalPointOnSegment.Copy(&segDir).MultiplyScalar(s1).Add(&segCenter)
  183. }
  184. return sqrDist
  185. }
  186. // IsIntersectionSphere returns if this ray intersects with the specified sphere.
  187. func (ray *Ray) IsIntersectionSphere(sphere *Sphere) bool {
  188. if ray.DistanceToPoint(&sphere.Center) <= sphere.Radius {
  189. return true
  190. }
  191. return false
  192. }
  193. // IntersectSphere calculates the point which is the intersection of this ray with the specified sphere.
  194. // The calculated point is stored in optionalTarget, it not nil, and also returned.
  195. // If no intersection is found the calculated point is set to nil.
  196. func (ray *Ray) IntersectSphere(sphere *Sphere, optionalTarget *Vector3) *Vector3 {
  197. var v1 Vector3
  198. v1.SubVectors(&sphere.Center, &ray.origin)
  199. tca := v1.Dot(&ray.direction)
  200. d2 := v1.Dot(&v1) - tca*tca
  201. radius2 := sphere.Radius * sphere.Radius
  202. if d2 > radius2 {
  203. return nil
  204. }
  205. thc := Sqrt(radius2 - d2)
  206. // t0 = first intersect point - entrance on front of sphere
  207. t0 := tca - thc
  208. // t1 = second intersect point - exit point on back of sphere
  209. t1 := tca + thc
  210. // test to see if both t0 and t1 are behind the ray - if so, return null
  211. if t0 < 0 && t1 < 0 {
  212. return nil
  213. }
  214. // test to see if t0 is behind the ray:
  215. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  216. // in order to always return an intersect point that is in front of the ray.
  217. if t0 < 0 {
  218. return ray.At(t1, optionalTarget)
  219. }
  220. // else t0 is in front of the ray, so return the first collision point scaled by t0
  221. return ray.At(t0, optionalTarget)
  222. }
  223. // IsIntersectPlane returns if this ray intersects the specified plane.
  224. func (ray *Ray) IsIntersectPlane(plane *Plane) bool {
  225. distToPoint := plane.DistanceToPoint(&ray.origin)
  226. if distToPoint == 0 {
  227. return true
  228. }
  229. denominator := plane.normal.Dot(&ray.direction)
  230. if denominator*distToPoint < 0 {
  231. return true
  232. }
  233. // ray origin is behind the plane (and is pointing behind it)
  234. return false
  235. }
  236. // DistanceToPlane returns the distance of this ray origin to its intersection point in the plane.
  237. // If the ray does not intersects the plane, returns NaN.
  238. func (ray *Ray) DistanceToPlane(plane *Plane) float32 {
  239. denominator := plane.normal.Dot(&ray.direction)
  240. if denominator == 0 {
  241. // line is coplanar, return origin
  242. if plane.DistanceToPoint(&ray.origin) == 0 {
  243. return 0
  244. }
  245. return NaN()
  246. }
  247. t := -(ray.origin.Dot(&plane.normal) + plane.constant) / denominator
  248. // Return if the ray never intersects the plane
  249. if t >= 0 {
  250. return t
  251. }
  252. return NaN()
  253. }
  254. // IntersectPlane calculates the point which is the intersection of this ray with the specified plane.
  255. // The calculated point is stored in optionalTarget, if not nil, and also returned.
  256. // If no intersection is found the calculated point is set to nil.
  257. func (ray *Ray) IntersectPlane(plane *Plane, optionalTarget *Vector3) *Vector3 {
  258. t := ray.DistanceToPlane(plane)
  259. if t == NaN() {
  260. return nil
  261. }
  262. return ray.At(t, optionalTarget)
  263. }
  264. // IsIntersectionBox returns if this ray intersects the specified box.
  265. func (ray *Ray) IsIntersectionBox(box *Box3) bool {
  266. var v Vector3
  267. if ray.IntersectBox(box, &v) != nil {
  268. return true
  269. }
  270. return false
  271. }
  272. // IntersectBox calculates the point which is the intersection of this ray with the specified box.
  273. // The calculated point is stored in optionalTarget, it not nil, and also returned.
  274. // If no intersection is found the calculated point is set to nil.
  275. func (ray *Ray) IntersectBox(box *Box3, optionalTarget *Vector3) *Vector3 {
  276. // http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
  277. var tmin, tmax, tymin, tymax, tzmin, tzmax float32
  278. invdirx := 1 / ray.direction.X
  279. invdiry := 1 / ray.direction.Y
  280. invdirz := 1 / ray.direction.Z
  281. var origin = ray.origin
  282. if invdirx >= 0 {
  283. tmin = (box.Min.X - origin.X) * invdirx
  284. tmax = (box.Max.X - origin.X) * invdirx
  285. } else {
  286. tmin = (box.Max.X - origin.X) * invdirx
  287. tmax = (box.Min.X - origin.X) * invdirx
  288. }
  289. if invdiry >= 0 {
  290. tymin = (box.Min.Y - origin.Y) * invdiry
  291. tymax = (box.Max.Y - origin.Y) * invdiry
  292. } else {
  293. tymin = (box.Max.Y - origin.Y) * invdiry
  294. tymax = (box.Min.Y - origin.Y) * invdiry
  295. }
  296. if (tmin > tymax) || (tymin > tmax) {
  297. return nil
  298. }
  299. // These lines also handle the case where tmin or tmax is NaN
  300. // (result of 0 * Infinity). x !== x returns true if x is NaN
  301. if tymin > tmin || tmin != tmin {
  302. tmin = tymin
  303. }
  304. if tymax < tmax || tmax != tmax {
  305. tmax = tymax
  306. }
  307. if invdirz >= 0 {
  308. tzmin = (box.Min.Z - origin.Z) * invdirz
  309. tzmax = (box.Max.Z - origin.Z) * invdirz
  310. } else {
  311. tzmin = (box.Max.Z - origin.Z) * invdirz
  312. tzmax = (box.Min.Z - origin.Z) * invdirz
  313. }
  314. if (tmin > tzmax) || (tzmin > tmax) {
  315. return nil
  316. }
  317. if tzmin > tmin || tmin != tmin {
  318. tmin = tzmin
  319. }
  320. if tzmax < tmax || tmax != tmax {
  321. tmax = tzmax
  322. }
  323. //return point closest to the ray (positive side)
  324. if tmax < 0 {
  325. return nil
  326. }
  327. if tmin >= 0 {
  328. return ray.At(tmin, optionalTarget)
  329. }
  330. return ray.At(tmax, optionalTarget)
  331. }
  332. // IntersectTriangle returns if this ray intersects the triangle with the face
  333. // defined by points a, b, c. Returns true if it intersects and sets the point
  334. // parameter with the intersected point coordinates.
  335. // If backfaceCulling is false it ignores the intersection if the face is not oriented
  336. // in the ray direction.
  337. func (ray *Ray) IntersectTriangle(a, b, c *Vector3, backfaceCulling bool, point *Vector3) bool {
  338. var diff Vector3
  339. var edge1 Vector3
  340. var edge2 Vector3
  341. var normal Vector3
  342. edge1.SubVectors(b, a)
  343. edge2.SubVectors(c, a)
  344. normal.CrossVectors(&edge1, &edge2)
  345. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  346. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  347. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  348. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  349. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  350. DdN := ray.direction.Dot(&normal)
  351. var sign float32
  352. if DdN > 0 {
  353. if backfaceCulling {
  354. return false
  355. }
  356. sign = 1
  357. } else if DdN < 0 {
  358. sign = -1
  359. DdN = -DdN
  360. } else {
  361. return false
  362. }
  363. diff.SubVectors(&ray.origin, a)
  364. DdQxE2 := sign * ray.direction.Dot(edge2.CrossVectors(&diff, &edge2))
  365. // b1 < 0, no intersection
  366. if DdQxE2 < 0 {
  367. return false
  368. }
  369. DdE1xQ := sign * ray.direction.Dot(edge1.Cross(&diff))
  370. // b2 < 0, no intersection
  371. if DdE1xQ < 0 {
  372. return false
  373. }
  374. // b1+b2 > 1, no intersection
  375. if DdQxE2+DdE1xQ > DdN {
  376. return false
  377. }
  378. // Line intersects triangle, check if ray does.
  379. QdN := -sign * diff.Dot(&normal)
  380. // t < 0, no intersection
  381. if QdN < 0 {
  382. return false
  383. }
  384. // Ray intersects triangle.
  385. ray.At(QdN/DdN, point)
  386. return true
  387. }
  388. // ApplyMatrix4 multiplies this ray origin and direction
  389. // by the specified matrix4, basically transforming this ray coordinates.
  390. func (ray *Ray) ApplyMatrix4(matrix4 *Matrix4) *Ray {
  391. ray.direction.Add(&ray.origin).ApplyMatrix4(matrix4)
  392. ray.origin.ApplyMatrix4(matrix4)
  393. ray.direction.Sub(&ray.origin)
  394. ray.direction.Normalize()
  395. return ray
  396. }
  397. // Equals returns if this ray is equal to other
  398. func (ray *Ray) Equals(other *Ray) bool {
  399. return ray.origin.Equals(&other.origin) && ray.direction.Equals(&other.direction)
  400. }
  401. // Clone creates and returns a pointer to copy of this ray.
  402. func (ray *Ray) Clone() *Ray {
  403. return NewRay(&ray.origin, &ray.direction)
  404. }