plane.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // Plane represents a plane in 3D space by its normal vector and a constant.
  6. // When the the normal vector is the unit vector the constant is the distance from the origin.
  7. type Plane struct {
  8. normal Vector3
  9. constant float32
  10. }
  11. // NewPlane creates and returns a new plane from a normal vector and a constant.
  12. func NewPlane(normal *Vector3, constant float32) *Plane {
  13. p := new(Plane)
  14. if normal != nil {
  15. p.normal = *normal
  16. }
  17. p.constant = constant
  18. return p
  19. }
  20. // Set sets this plane normal vector and constant.
  21. // Returns pointer to this updated plane.
  22. func (p *Plane) Set(normal *Vector3, constant float32) *Plane {
  23. p.normal = *normal
  24. p.constant = constant
  25. return p
  26. }
  27. // SetComponents sets this plane normal vector components and constant.
  28. // Returns pointer to this updated plane.
  29. func (p *Plane) SetComponents(x, y, z, w float32) *Plane {
  30. p.normal.Set(x, y, z)
  31. p.constant = w
  32. return p
  33. }
  34. // SetFromNormalAndCoplanarPoint sets this plane from a normal vector and a point on the plane.
  35. // Returns pointer to this updated plane.
  36. func (p *Plane) SetFromNormalAndCoplanarPoint(normal *Vector3, point *Vector3) *Plane {
  37. p.normal = *normal
  38. p.constant = -point.Dot(&p.normal)
  39. return p
  40. }
  41. // SetFromCoplanarPoints sets this plane from three coplanar points.
  42. // Returns pointer to this updated plane.
  43. func (p *Plane) SetFromCoplanarPoints(a, b, c *Vector3) *Plane {
  44. var v1 Vector3
  45. var v2 Vector3
  46. normal := v1.SubVectors(c, b).Cross(v2.SubVectors(a, b)).Normalize()
  47. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  48. p.SetFromNormalAndCoplanarPoint(normal, a)
  49. return p
  50. }
  51. // Copy sets this plane to a copy of other.
  52. // Returns pointer to this updated plane.
  53. func (p *Plane) Copy(other *Plane) *Plane {
  54. p.normal.Copy(&other.normal)
  55. p.constant = other.constant
  56. return p
  57. }
  58. // Normalize normalizes this plane normal vector and adjusts the constant.
  59. // Note: will lead to a divide by zero if the plane is invalid.
  60. // Returns pointer to this updated plane.
  61. func (p *Plane) Normalize() *Plane {
  62. inverseNormalLength := 1.0 / p.normal.Length()
  63. p.normal.MultiplyScalar(inverseNormalLength)
  64. p.constant *= inverseNormalLength
  65. return p
  66. }
  67. // Negate negates this plane normal.
  68. // Returns pointer to this updated plane.
  69. func (p *Plane) Negate() *Plane {
  70. p.constant *= -1
  71. p.normal.Negate()
  72. return p
  73. }
  74. // DistanceToPoint returns the distance of this plane from point.
  75. func (p *Plane) DistanceToPoint(point *Vector3) float32 {
  76. return p.normal.Dot(point) + p.constant
  77. }
  78. // DistanceToSphere returns the distance of this place from the sphere.
  79. func (p *Plane) DistanceToSphere(sphere *Sphere) float32 {
  80. return p.DistanceToPoint(&sphere.Center) - sphere.Radius
  81. }
  82. // IsIntersectionLine returns the line intersects this plane.
  83. func (p *Plane) IsIntersectionLine(line *Line3) bool {
  84. startSign := p.DistanceToPoint(&line.start)
  85. endSign := p.DistanceToPoint(&line.end)
  86. return (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0)
  87. }
  88. // IntersectLine calculates the point in the plane which intersets the specified line.
  89. // Sets the optionalTarget, if not nil to this point, and also returns it.
  90. // Returns nil if the line does not intersects the plane.
  91. func (p *Plane) IntersectLine(line *Line3, optionalTarget *Vector3) *Vector3 {
  92. var v1 Vector3
  93. var result *Vector3
  94. if optionalTarget == nil {
  95. result = NewVector3(0, 0, 0)
  96. } else {
  97. result = optionalTarget
  98. }
  99. direction := line.Delta(&v1)
  100. denominator := p.normal.Dot(direction)
  101. if denominator == 0 {
  102. // line is coplanar, return origin
  103. if p.DistanceToPoint(&line.start) == 0 {
  104. return result.Copy(&line.start)
  105. }
  106. // Unsure if this is the correct method to handle this case.
  107. return nil
  108. }
  109. var t = -(line.start.Dot(&p.normal) + p.constant) / denominator
  110. if t < 0 || t > 1 {
  111. return nil
  112. }
  113. return result.Copy(direction).MultiplyScalar(t).Add(&line.start)
  114. }
  115. // CoplanarPoint sets the optionalTarget to a point in the plane and also returns it.
  116. // The point set and returned is the closest point from the origin.
  117. func (p *Plane) CoplanarPoint(optionalTarget *Vector3) *Vector3 {
  118. var result *Vector3
  119. if optionalTarget == nil {
  120. result = NewVector3(0, 0, 0)
  121. } else {
  122. result = optionalTarget
  123. }
  124. return result.Copy(&p.normal).MultiplyScalar(-p.constant)
  125. }
  126. // Translate translates this plane in the direction of its normal by offset.
  127. // Returns pointer to this updated plane.
  128. func (p *Plane) Translate(offset *Vector3) *Plane {
  129. p.constant = p.constant - offset.Dot(&p.normal)
  130. return p
  131. }
  132. // Equals returns if this plane is equal to other
  133. func (p *Plane) Equals(other *Plane) bool {
  134. return other.normal.Equals(&p.normal) && (other.constant == p.constant)
  135. }
  136. // Clone creates and returns a pointer to a copy of this plane.
  137. func (p *Plane) Clone(plane *Plane) *Plane {
  138. return NewPlane(&plane.normal, plane.constant)
  139. }