ray.go 11 KB

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