Plane.cs 5.7 KB

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