line3.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Line3 struct {
  6. start Vector3
  7. end Vector3
  8. }
  9. func NewLine3(start, end *Vector3) *Line3 {
  10. this := new(Line3)
  11. this.Set(start, end)
  12. return this
  13. }
  14. func (this *Line3) Set(start, end *Vector3) *Line3 {
  15. if start != nil {
  16. this.start = *start
  17. }
  18. if end != nil {
  19. this.end = *end
  20. }
  21. return this
  22. }
  23. func (this *Line3) Copy(line *Line3) *Line3 {
  24. this.start = line.start
  25. this.end = line.end
  26. return this
  27. }
  28. func (this *Line3) Center(optionalTarget *Vector3) *Vector3 {
  29. var result *Vector3
  30. if optionalTarget == nil {
  31. result = NewVector3(0, 0, 0)
  32. } else {
  33. result = optionalTarget
  34. }
  35. return result.AddVectors(&this.start, &this.end).MultiplyScalar(0.5)
  36. }
  37. func (this *Line3) Delta(optionalTarget *Vector3) *Vector3 {
  38. var result *Vector3
  39. if optionalTarget == nil {
  40. result = NewVector3(0, 0, 0)
  41. } else {
  42. result = optionalTarget
  43. }
  44. return result.SubVectors(&this.end, &this.start).MultiplyScalar(0.5)
  45. }
  46. func (this *Line3) DistanceSq() float32 {
  47. return this.start.DistanceToSquared(&this.end)
  48. }
  49. func (this *Line3) Distance() float32 {
  50. return this.start.DistanceTo(&this.end)
  51. }
  52. func (this *Line3) At(t float32, optionalTarget *Vector3) *Vector3 {
  53. var result *Vector3
  54. if optionalTarget == nil {
  55. result = NewVector3(0, 0, 0)
  56. } else {
  57. result = optionalTarget
  58. }
  59. return this.Delta(result).MultiplyScalar(t).Add(&this.start)
  60. }
  61. func (this *Line3) ClosestPointToPointParameter() func(*Vector3, bool) float32 {
  62. startP := NewVector3(0, 0, 0)
  63. startEnd := NewVector3(0, 0, 0)
  64. return func(point *Vector3, clampToLine bool) float32 {
  65. startP.SubVectors(point, &this.start)
  66. startEnd.SubVectors(&this.end, &this.start)
  67. startEnd2 := startEnd.Dot(startEnd)
  68. startEnd_startP := startEnd.Dot(startP)
  69. t := startEnd_startP / startEnd2
  70. if clampToLine {
  71. t = Clamp(t, 0, 1)
  72. }
  73. return t
  74. }
  75. }
  76. func (this *Line3) ClosestPointToPoint(point *Vector3, clampToLine bool, optionalTarget *Vector3) *Vector3 {
  77. t := this.ClosestPointToPointParameter()(point, clampToLine)
  78. var result *Vector3
  79. if optionalTarget == nil {
  80. result = NewVector3(0, 0, 0)
  81. } else {
  82. result = optionalTarget
  83. }
  84. return this.Delta(result).MultiplyScalar(t).Add(&this.start)
  85. }
  86. func (this *Line3) ApplyMatrix4(matrix *Matrix4) *Line3 {
  87. this.start.ApplyMatrix4(matrix)
  88. this.end.ApplyMatrix4(matrix)
  89. return this
  90. }
  91. func (this *Line3) Equals(line *Line3) bool {
  92. return line.start.Equals(&this.start) && line.end.Equals(&this.end)
  93. }
  94. func (this *Line3) Clone() *Line3 {
  95. return NewLine3(&this.start, &this.end)
  96. }