Plane.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. #if REAL_T_IS_DOUBLE
  3. using real_t = System.Double;
  4. #else
  5. using real_t = System.Single;
  6. #endif
  7. namespace Godot
  8. {
  9. public struct Plane : IEquatable<Plane>
  10. {
  11. private Vector3 _normal;
  12. public Vector3 Normal
  13. {
  14. get { return _normal; }
  15. set { _normal = value; }
  16. }
  17. public real_t x
  18. {
  19. get
  20. {
  21. return _normal.x;
  22. }
  23. set
  24. {
  25. _normal.x = value;
  26. }
  27. }
  28. public real_t y
  29. {
  30. get
  31. {
  32. return _normal.y;
  33. }
  34. set
  35. {
  36. _normal.y = value;
  37. }
  38. }
  39. public real_t z
  40. {
  41. get
  42. {
  43. return _normal.z;
  44. }
  45. set
  46. {
  47. _normal.z = value;
  48. }
  49. }
  50. public real_t D { get; set; }
  51. public Vector3 Center
  52. {
  53. get
  54. {
  55. return _normal * D;
  56. }
  57. }
  58. public real_t DistanceTo(Vector3 point)
  59. {
  60. return _normal.Dot(point) - D;
  61. }
  62. public Vector3 GetAnyPoint()
  63. {
  64. return _normal * D;
  65. }
  66. public bool HasPoint(Vector3 point, real_t epsilon = Mathf.Epsilon)
  67. {
  68. real_t dist = _normal.Dot(point) - D;
  69. return Mathf.Abs(dist) <= epsilon;
  70. }
  71. public Vector3 Intersect3(Plane b, Plane c)
  72. {
  73. real_t denom = _normal.Cross(b._normal).Dot(c._normal);
  74. if (Mathf.Abs(denom) <= Mathf.Epsilon)
  75. return new Vector3();
  76. Vector3 result = b._normal.Cross(c._normal) * D +
  77. c._normal.Cross(_normal) * b.D +
  78. _normal.Cross(b._normal) * c.D;
  79. return result / denom;
  80. }
  81. public Vector3 IntersectRay(Vector3 from, Vector3 dir)
  82. {
  83. real_t den = _normal.Dot(dir);
  84. if (Mathf.Abs(den) <= Mathf.Epsilon)
  85. return new Vector3();
  86. real_t dist = (_normal.Dot(from) - D) / den;
  87. // This is a ray, before the emitting pos (from) does not exist
  88. if (dist > Mathf.Epsilon)
  89. return new Vector3();
  90. return from + dir * -dist;
  91. }
  92. public Vector3 IntersectSegment(Vector3 begin, Vector3 end)
  93. {
  94. Vector3 segment = begin - end;
  95. real_t den = _normal.Dot(segment);
  96. if (Mathf.Abs(den) <= Mathf.Epsilon)
  97. return new Vector3();
  98. real_t dist = (_normal.Dot(begin) - D) / den;
  99. if (dist < -Mathf.Epsilon || dist > 1.0f + Mathf.Epsilon)
  100. return new Vector3();
  101. return begin + segment * -dist;
  102. }
  103. public bool IsPointOver(Vector3 point)
  104. {
  105. return _normal.Dot(point) > D;
  106. }
  107. public Plane Normalized()
  108. {
  109. real_t len = _normal.Length();
  110. if (len == 0)
  111. return new Plane(0, 0, 0, 0);
  112. return new Plane(_normal / len, D / len);
  113. }
  114. public Vector3 Project(Vector3 point)
  115. {
  116. return point - _normal * DistanceTo(point);
  117. }
  118. // Constants
  119. private static readonly Plane _planeYZ = new Plane(1, 0, 0, 0);
  120. private static readonly Plane _planeXZ = new Plane(0, 1, 0, 0);
  121. private static readonly Plane _planeXY = new Plane(0, 0, 1, 0);
  122. public static Plane PlaneYZ { get { return _planeYZ; } }
  123. public static Plane PlaneXZ { get { return _planeXZ; } }
  124. public static Plane PlaneXY { get { return _planeXY; } }
  125. // Constructors
  126. public Plane(real_t a, real_t b, real_t c, real_t d)
  127. {
  128. _normal = new Vector3(a, b, c);
  129. this.D = d;
  130. }
  131. public Plane(Vector3 normal, real_t d)
  132. {
  133. this._normal = normal;
  134. this.D = d;
  135. }
  136. public Plane(Vector3 v1, Vector3 v2, Vector3 v3)
  137. {
  138. _normal = (v1 - v3).Cross(v1 - v2);
  139. _normal.Normalize();
  140. D = _normal.Dot(v1);
  141. }
  142. public static Plane operator -(Plane plane)
  143. {
  144. return new Plane(-plane._normal, -plane.D);
  145. }
  146. public static bool operator ==(Plane left, Plane right)
  147. {
  148. return left.Equals(right);
  149. }
  150. public static bool operator !=(Plane left, Plane right)
  151. {
  152. return !left.Equals(right);
  153. }
  154. public override bool Equals(object obj)
  155. {
  156. if (obj is Plane)
  157. {
  158. return Equals((Plane)obj);
  159. }
  160. return false;
  161. }
  162. public bool Equals(Plane other)
  163. {
  164. return _normal == other._normal && Mathf.IsEqualApprox(D, other.D);
  165. }
  166. public override int GetHashCode()
  167. {
  168. return _normal.GetHashCode() ^ D.GetHashCode();
  169. }
  170. public override string ToString()
  171. {
  172. return String.Format("({0}, {1})", new object[]
  173. {
  174. _normal.ToString(),
  175. D.ToString()
  176. });
  177. }
  178. public string ToString(string format)
  179. {
  180. return String.Format("({0}, {1})", new object[]
  181. {
  182. _normal.ToString(format),
  183. D.ToString(format)
  184. });
  185. }
  186. }
  187. }