box3.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Box3 struct {
  6. Min Vector3
  7. Max Vector3
  8. }
  9. func NewBox3(min, max *Vector3) *Box3 {
  10. this := new(Box3)
  11. this.Set(min, max)
  12. return this
  13. }
  14. func (this *Box3) Set(min, max *Vector3) *Box3 {
  15. if min != nil {
  16. this.Min = *min
  17. } else {
  18. this.Min.Set(Infinity, Infinity, Infinity)
  19. }
  20. if max != nil {
  21. this.Max = *max
  22. } else {
  23. this.Max.Set(-Infinity, -Infinity, -Infinity)
  24. }
  25. return this
  26. }
  27. func (this *Box3) SetFromPoints(points []Vector3) *Box3 {
  28. this.MakeEmpty()
  29. for i := 0; i < len(points); i++ {
  30. this.ExpandByPoint(&points[i])
  31. }
  32. return this
  33. }
  34. func (this *Box3) SetFromCenterAndSize(center, size *Vector3) *Box3 {
  35. v1 := NewVector3(0, 0, 0)
  36. halfSize := v1.Copy(size).MultiplyScalar(0.5)
  37. this.Min.Copy(center).Sub(halfSize)
  38. this.Max.Copy(center).Add(halfSize)
  39. return this
  40. }
  41. //func (this *Box3) SetFromObject(object *Object3D) *Box3 {
  42. //
  43. // // TODO object.UpdateMatrixWorld(true)
  44. //
  45. // return this
  46. //}
  47. func (this *Box3) Copy(box *Box3) *Box3 {
  48. this.Min = box.Min
  49. this.Max = box.Max
  50. return this
  51. }
  52. func (this *Box3) MakeEmpty() *Box3 {
  53. this.Min.X = Infinity
  54. this.Min.Y = Infinity
  55. this.Min.Z = Infinity
  56. this.Max.X = -Infinity
  57. this.Max.Y = -Infinity
  58. this.Max.Z = -Infinity
  59. return this
  60. }
  61. func (this *Box3) Empty() bool {
  62. return (this.Max.X < this.Min.X) || (this.Max.Y < this.Min.Y) || (this.Max.Z < this.Min.Z)
  63. }
  64. func (this *Box3) Center(optionalTarget *Vector3) *Vector3 {
  65. var result *Vector3
  66. if optionalTarget == nil {
  67. result = NewVector3(0, 0, 0)
  68. } else {
  69. result = optionalTarget
  70. }
  71. return result.AddVectors(&this.Min, &this.Max).MultiplyScalar(0.5)
  72. }
  73. func (this *Box3) Size(optionalTarget *Vector3) *Vector3 {
  74. var result *Vector3
  75. if optionalTarget == nil {
  76. result = NewVector3(0, 0, 0)
  77. } else {
  78. result = optionalTarget
  79. }
  80. return result.SubVectors(&this.Min, &this.Max)
  81. }
  82. func (this *Box3) ExpandByPoint(point *Vector3) *Box3 {
  83. this.Min.Min(point)
  84. this.Max.Max(point)
  85. return this
  86. }
  87. func (this *Box3) ExpandByVector(vector *Vector3) *Box3 {
  88. this.Min.Sub(vector)
  89. this.Max.Add(vector)
  90. return this
  91. }
  92. func (this *Box3) ExpandByScalar(scalar float32) *Box3 {
  93. this.Min.AddScalar(-scalar)
  94. this.Max.AddScalar(scalar)
  95. return this
  96. }
  97. func (this *Box3) ContainsPoint(point *Vector3) bool {
  98. if point.X < this.Min.X || point.X > this.Max.X ||
  99. point.Y < this.Min.Y || point.Y > this.Max.Y ||
  100. point.Z < this.Min.Z || point.Z > this.Max.Z {
  101. return false
  102. }
  103. return true
  104. }
  105. func (this *Box3) ContainsBox(box *Box3) bool {
  106. if (this.Min.X <= box.Max.X) && (box.Max.X <= this.Max.X) &&
  107. (this.Min.Y <= box.Min.Y) && (box.Max.Y <= this.Max.Y) &&
  108. (this.Min.Z <= box.Min.Z) && (box.Max.Z <= this.Max.Z) {
  109. return true
  110. }
  111. return false
  112. }
  113. func (this *Box3) GetParameter(point *Vector3, optionalTarget *Vector3) *Vector3 {
  114. // This can potentially have a divide by zero if the box
  115. // has a size dimension of 0.
  116. var result *Vector3
  117. if optionalTarget == nil {
  118. result = NewVector3(0, 0, 0)
  119. } else {
  120. result = optionalTarget
  121. }
  122. return result.Set(
  123. (point.X-this.Min.X)/(this.Max.X-this.Min.X),
  124. (point.Y-this.Min.Y)/(this.Max.Y-this.Min.Y),
  125. (point.Z-this.Min.Z)/(this.Max.Z-this.Min.Z),
  126. )
  127. }
  128. func (this *Box3) IsIntersectionBox(box *Box3) bool {
  129. // using 6 splitting planes to rule out intersections.
  130. if box.Max.X < this.Min.X || box.Min.X > this.Max.X ||
  131. box.Max.Y < this.Min.Y || box.Min.Y > this.Max.Y ||
  132. box.Max.Z < this.Min.Z || box.Min.Z > this.Max.Z {
  133. return false
  134. }
  135. return true
  136. }
  137. func (this *Box3) ClampPoint(point *Vector3, optionalTarget *Vector3) *Vector3 {
  138. var result *Vector3
  139. if optionalTarget == nil {
  140. result = NewVector3(0, 0, 0)
  141. } else {
  142. result = optionalTarget
  143. }
  144. return result.Copy(point).Clamp(&this.Min, &this.Max)
  145. }
  146. func (this *Box3) DistanceToPoint(point *Vector3) float32 {
  147. var v1 Vector3
  148. clampedPoint := v1.Copy(point).Clamp(&this.Min, &this.Max)
  149. return clampedPoint.Sub(point).Length()
  150. }
  151. func (this *Box3) GetBoundingSphere(optionalTarget *Sphere) *Sphere {
  152. var v1 Vector3
  153. var result *Sphere
  154. if optionalTarget == nil {
  155. result = NewSphere(nil, 0)
  156. } else {
  157. result = optionalTarget
  158. }
  159. result.Center = *this.Center(nil)
  160. result.Radius = this.Size(&v1).Length() * 0.5
  161. return result
  162. }
  163. func (this *Box3) Intersect(box *Box3) *Box3 {
  164. this.Min.Max(&box.Min)
  165. this.Max.Min(&box.Max)
  166. return this
  167. }
  168. func (this *Box3) Union(box *Box3) *Box3 {
  169. this.Min.Min(&box.Min)
  170. this.Max.Max(&box.Max)
  171. return this
  172. }
  173. func (this *Box3) ApplyMatrix4(matrix *Matrix4) *Box3 {
  174. points := []Vector3{
  175. Vector3{},
  176. Vector3{},
  177. Vector3{},
  178. Vector3{},
  179. Vector3{},
  180. Vector3{},
  181. Vector3{},
  182. Vector3{},
  183. }
  184. points[0].Set(this.Min.X, this.Min.Y, this.Min.Z).ApplyMatrix4(matrix) // 000
  185. points[1].Set(this.Min.X, this.Min.Y, this.Max.Z).ApplyMatrix4(matrix) // 001
  186. points[2].Set(this.Min.X, this.Max.Y, this.Min.Z).ApplyMatrix4(matrix) // 010
  187. points[3].Set(this.Min.X, this.Max.Y, this.Max.Z).ApplyMatrix4(matrix) // 011
  188. points[4].Set(this.Max.X, this.Min.Y, this.Min.Z).ApplyMatrix4(matrix) // 100
  189. points[5].Set(this.Max.X, this.Min.Y, this.Max.Z).ApplyMatrix4(matrix) // 101
  190. points[6].Set(this.Max.X, this.Max.Y, this.Min.Z).ApplyMatrix4(matrix) // 110
  191. points[7].Set(this.Max.X, this.Max.Y, this.Max.Z).ApplyMatrix4(matrix) // 111
  192. this.MakeEmpty()
  193. this.SetFromPoints(points)
  194. return this
  195. }
  196. func (this *Box3) Translate(offset *Vector3) *Box3 {
  197. this.Min.Add(offset)
  198. this.Max.Add(offset)
  199. return this
  200. }
  201. func (this *Box3) Equals(box *Box3) bool {
  202. return box.Min.Equals(&this.Min) && box.Max.Equals(&this.Max)
  203. }
  204. func (this *Box3) Clone() *Box3 {
  205. return NewBox3(&this.Min, &this.Max)
  206. }