Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Plane.cs
Go to the documentation of this file.
1using System;
5using Microsoft.Xna.Framework.Design;
6
8
10[TypeConverter(typeof(PlaneConverter))]
11public struct Plane : IEquatable<Plane>
12{
13 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
15
16 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
17 public float D;
18
19 public Plane(float a, float b, float c, float d)
20 {
21 Normal.X = a;
22 Normal.Y = b;
23 Normal.Z = c;
24 D = d;
25 }
26
27 public Plane(Vector3 normal, float d)
28 {
29 Normal = normal;
30 D = d;
31 }
32
34 {
35 Normal.X = value.X;
36 Normal.Y = value.Y;
37 Normal.Z = value.Z;
38 D = value.W;
39 }
40
41 public Plane(Vector3 point1, Vector3 point2, Vector3 point3)
42 {
43 float num = point2.X - point1.X;
44 float num2 = point2.Y - point1.Y;
45 float num3 = point2.Z - point1.Z;
46 float num4 = point3.X - point1.X;
47 float num5 = point3.Y - point1.Y;
48 float num6 = point3.Z - point1.Z;
49 float num7 = num2 * num6 - num3 * num5;
50 float num8 = num3 * num4 - num * num6;
51 float num9 = num * num5 - num2 * num4;
52 float num10 = num7 * num7 + num8 * num8 + num9 * num9;
53 float num11 = 1f / (float)Math.Sqrt(num10);
54 Normal.X = num7 * num11;
55 Normal.Y = num8 * num11;
56 Normal.Z = num9 * num11;
57 D = 0f - (Normal.X * point1.X + Normal.Y * point1.Y + Normal.Z * point1.Z);
58 }
59
60 public bool Equals(Plane other)
61 {
62 if (Normal.X == other.Normal.X && Normal.Y == other.Normal.Y && Normal.Z == other.Normal.Z)
63 {
64 return D == other.D;
65 }
66 return false;
67 }
68
69 public override bool Equals(object obj)
70 {
71 bool result = false;
72 if (obj is Plane)
73 {
74 result = Equals((Plane)obj);
75 }
76 return result;
77 }
78
79 public override int GetHashCode()
80 {
81 return Normal.GetHashCode() + D.GetHashCode();
82 }
83
84 public override string ToString()
85 {
86 CultureInfo currentCulture = CultureInfo.CurrentCulture;
87 return string.Format(currentCulture, "{{Normal:{0} D:{1}}}", new object[2]
88 {
90 D.ToString(currentCulture)
91 });
92 }
93
94 public void Normalize()
95 {
96 float num = Normal.X * Normal.X + Normal.Y * Normal.Y + Normal.Z * Normal.Z;
97 if (!(Math.Abs(num - 1f) < 1.1920929E-07f))
98 {
99 float num2 = 1f / (float)Math.Sqrt(num);
100 Normal.X *= num2;
101 Normal.Y *= num2;
102 Normal.Z *= num2;
103 D *= num2;
104 }
105 }
106
107 public static Plane Normalize(Plane value)
108 {
109 float num = value.Normal.X * value.Normal.X + value.Normal.Y * value.Normal.Y + value.Normal.Z * value.Normal.Z;
110 Plane result = default(Plane);
111 if (Math.Abs(num - 1f) < 1.1920929E-07f)
112 {
113 result.Normal = value.Normal;
114 result.D = value.D;
115 return result;
116 }
117 float num2 = 1f / (float)Math.Sqrt(num);
118 result.Normal.X = value.Normal.X * num2;
119 result.Normal.Y = value.Normal.Y * num2;
120 result.Normal.Z = value.Normal.Z * num2;
121 result.D = value.D * num2;
122 return result;
123 }
124
125 public static void Normalize(ref Plane value, out Plane result)
126 {
127 float num = value.Normal.X * value.Normal.X + value.Normal.Y * value.Normal.Y + value.Normal.Z * value.Normal.Z;
128 if (Math.Abs(num - 1f) < 1.1920929E-07f)
129 {
130 result.Normal = value.Normal;
131 result.D = value.D;
132 return;
133 }
134 float num2 = 1f / (float)Math.Sqrt(num);
135 result.Normal.X = value.Normal.X * num2;
136 result.Normal.Y = value.Normal.Y * num2;
137 result.Normal.Z = value.Normal.Z * num2;
138 result.D = value.D * num2;
139 }
140
141 public static Plane Transform(Plane plane, Matrix matrix)
142 {
143 Matrix.Invert(ref matrix, out var result);
144 float x = plane.Normal.X;
145 float y = plane.Normal.Y;
146 float z = plane.Normal.Z;
147 float d = plane.D;
148 Plane result2 = default(Plane);
149 result2.Normal.X = x * result.M11 + y * result.M12 + z * result.M13 + d * result.M14;
150 result2.Normal.Y = x * result.M21 + y * result.M22 + z * result.M23 + d * result.M24;
151 result2.Normal.Z = x * result.M31 + y * result.M32 + z * result.M33 + d * result.M34;
152 result2.D = x * result.M41 + y * result.M42 + z * result.M43 + d * result.M44;
153 return result2;
154 }
155
156 public static void Transform(ref Plane plane, ref Matrix matrix, out Plane result)
157 {
158 Matrix.Invert(ref matrix, out var result2);
159 float x = plane.Normal.X;
160 float y = plane.Normal.Y;
161 float z = plane.Normal.Z;
162 float d = plane.D;
163 result.Normal.X = x * result2.M11 + y * result2.M12 + z * result2.M13 + d * result2.M14;
164 result.Normal.Y = x * result2.M21 + y * result2.M22 + z * result2.M23 + d * result2.M24;
165 result.Normal.Z = x * result2.M31 + y * result2.M32 + z * result2.M33 + d * result2.M34;
166 result.D = x * result2.M41 + y * result2.M42 + z * result2.M43 + d * result2.M44;
167 }
168
169 public static Plane Transform(Plane plane, Quaternion rotation)
170 {
171 float num = rotation.X + rotation.X;
172 float num2 = rotation.Y + rotation.Y;
173 float num3 = rotation.Z + rotation.Z;
174 float num4 = rotation.W * num;
175 float num5 = rotation.W * num2;
176 float num6 = rotation.W * num3;
177 float num7 = rotation.X * num;
178 float num8 = rotation.X * num2;
179 float num9 = rotation.X * num3;
180 float num10 = rotation.Y * num2;
181 float num11 = rotation.Y * num3;
182 float num12 = rotation.Z * num3;
183 float num13 = 1f - num10 - num12;
184 float num14 = num8 - num6;
185 float num15 = num9 + num5;
186 float num16 = num8 + num6;
187 float num17 = 1f - num7 - num12;
188 float num18 = num11 - num4;
189 float num19 = num9 - num5;
190 float num20 = num11 + num4;
191 float num21 = 1f - num7 - num10;
192 float x = plane.Normal.X;
193 float y = plane.Normal.Y;
194 float z = plane.Normal.Z;
195 Plane result = default(Plane);
196 result.Normal.X = x * num13 + y * num14 + z * num15;
197 result.Normal.Y = x * num16 + y * num17 + z * num18;
198 result.Normal.Z = x * num19 + y * num20 + z * num21;
199 result.D = plane.D;
200 return result;
201 }
202
203 public static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result)
204 {
205 float num = rotation.X + rotation.X;
206 float num2 = rotation.Y + rotation.Y;
207 float num3 = rotation.Z + rotation.Z;
208 float num4 = rotation.W * num;
209 float num5 = rotation.W * num2;
210 float num6 = rotation.W * num3;
211 float num7 = rotation.X * num;
212 float num8 = rotation.X * num2;
213 float num9 = rotation.X * num3;
214 float num10 = rotation.Y * num2;
215 float num11 = rotation.Y * num3;
216 float num12 = rotation.Z * num3;
217 float num13 = 1f - num10 - num12;
218 float num14 = num8 - num6;
219 float num15 = num9 + num5;
220 float num16 = num8 + num6;
221 float num17 = 1f - num7 - num12;
222 float num18 = num11 - num4;
223 float num19 = num9 - num5;
224 float num20 = num11 + num4;
225 float num21 = 1f - num7 - num10;
226 float x = plane.Normal.X;
227 float y = plane.Normal.Y;
228 float z = plane.Normal.Z;
229 result.Normal.X = x * num13 + y * num14 + z * num15;
230 result.Normal.Y = x * num16 + y * num17 + z * num18;
231 result.Normal.Z = x * num19 + y * num20 + z * num21;
232 result.D = plane.D;
233 }
234
235 public float Dot(Vector4 value)
236 {
237 return Normal.X * value.X + Normal.Y * value.Y + Normal.Z * value.Z + D * value.W;
238 }
239
240 public void Dot(ref Vector4 value, out float result)
241 {
242 result = Normal.X * value.X + Normal.Y * value.Y + Normal.Z * value.Z + D * value.W;
243 }
244
246 {
247 return Normal.X * value.X + Normal.Y * value.Y + Normal.Z * value.Z + D;
248 }
249
250 public void DotCoordinate(ref Vector3 value, out float result)
251 {
252 result = Normal.X * value.X + Normal.Y * value.Y + Normal.Z * value.Z + D;
253 }
254
255 public float DotNormal(Vector3 value)
256 {
257 return Normal.X * value.X + Normal.Y * value.Y + Normal.Z * value.Z;
258 }
259
260 public void DotNormal(ref Vector3 value, out float result)
261 {
262 result = Normal.X * value.X + Normal.Y * value.Y + Normal.Z * value.Z;
263 }
264
266 {
267 Vector3 vector = default(Vector3);
268 vector.X = ((Normal.X >= 0f) ? box.Min.X : box.Max.X);
269 vector.Y = ((Normal.Y >= 0f) ? box.Min.Y : box.Max.Y);
270 vector.Z = ((Normal.Z >= 0f) ? box.Min.Z : box.Max.Z);
271 Vector3 vector2 = default(Vector3);
272 vector2.X = ((Normal.X >= 0f) ? box.Max.X : box.Min.X);
273 vector2.Y = ((Normal.Y >= 0f) ? box.Max.Y : box.Min.Y);
274 vector2.Z = ((Normal.Z >= 0f) ? box.Max.Z : box.Min.Z);
275 float num = Normal.X * vector.X + Normal.Y * vector.Y + Normal.Z * vector.Z;
276 if (num + D > 0f)
277 {
278 return PlaneIntersectionType.Front;
279 }
280 num = Normal.X * vector2.X + Normal.Y * vector2.Y + Normal.Z * vector2.Z;
281 if (num + D < 0f)
282 {
283 return PlaneIntersectionType.Back;
284 }
285 return PlaneIntersectionType.Intersecting;
286 }
287
288 public void Intersects(ref BoundingBox box, out PlaneIntersectionType result)
289 {
290 Vector3 vector = default(Vector3);
291 vector.X = ((Normal.X >= 0f) ? box.Min.X : box.Max.X);
292 vector.Y = ((Normal.Y >= 0f) ? box.Min.Y : box.Max.Y);
293 vector.Z = ((Normal.Z >= 0f) ? box.Min.Z : box.Max.Z);
294 Vector3 vector2 = default(Vector3);
295 vector2.X = ((Normal.X >= 0f) ? box.Max.X : box.Min.X);
296 vector2.Y = ((Normal.Y >= 0f) ? box.Max.Y : box.Min.Y);
297 vector2.Z = ((Normal.Z >= 0f) ? box.Max.Z : box.Min.Z);
298 float num = Normal.X * vector.X + Normal.Y * vector.Y + Normal.Z * vector.Z;
299 if (num + D > 0f)
300 {
301 result = PlaneIntersectionType.Front;
302 return;
303 }
304 num = Normal.X * vector2.X + Normal.Y * vector2.Y + Normal.Z * vector2.Z;
305 if (num + D < 0f)
306 {
307 result = PlaneIntersectionType.Back;
308 }
309 else
310 {
311 result = PlaneIntersectionType.Intersecting;
312 }
313 }
314
316 {
317 if (null == frustum)
318 {
320 }
321 return frustum.Intersects(this);
322 }
323
325 {
326 float num = sphere.Center.X * Normal.X + sphere.Center.Y * Normal.Y + sphere.Center.Z * Normal.Z;
327 float num2 = num + D;
328 if (num2 > sphere.Radius)
329 {
330 return PlaneIntersectionType.Front;
331 }
332 if (num2 < 0f - sphere.Radius)
333 {
334 return PlaneIntersectionType.Back;
335 }
336 return PlaneIntersectionType.Intersecting;
337 }
338
339 public void Intersects(ref BoundingSphere sphere, out PlaneIntersectionType result)
340 {
341 float num = sphere.Center.X * Normal.X + sphere.Center.Y * Normal.Y + sphere.Center.Z * Normal.Z;
342 float num2 = num + D;
343 if (num2 > sphere.Radius)
344 {
345 result = PlaneIntersectionType.Front;
346 }
347 else if (num2 < 0f - sphere.Radius)
348 {
349 result = PlaneIntersectionType.Back;
350 }
351 else
352 {
353 result = PlaneIntersectionType.Intersecting;
354 }
355 }
356
357 public static bool operator ==(Plane lhs, Plane rhs)
358 {
359 return lhs.Equals(rhs);
360 }
361
362 public static bool operator !=(Plane lhs, Plane rhs)
363 {
364 if (lhs.Normal.X == rhs.Normal.X && lhs.Normal.Y == rhs.Normal.Y && lhs.Normal.Z == rhs.Normal.Z)
365 {
366 return lhs.D != rhs.D;
367 }
368 return true;
369 }
370}
static CultureInfo CurrentCulture
static double Sqrt(double d)
static double Abs(double value)
static Matrix Invert(Matrix matrix)
Definition Matrix.cs:1694
override int GetHashCode()
Definition Plane.cs:79
float DotCoordinate(Vector3 value)
Definition Plane.cs:245
void Intersects(ref BoundingSphere sphere, out PlaneIntersectionType result)
Definition Plane.cs:339
float DotNormal(Vector3 value)
Definition Plane.cs:255
void Intersects(ref BoundingBox box, out PlaneIntersectionType result)
Definition Plane.cs:288
static Plane Transform(Plane plane, Matrix matrix)
Definition Plane.cs:141
void DotCoordinate(ref Vector3 value, out float result)
Definition Plane.cs:250
PlaneIntersectionType Intersects(BoundingFrustum frustum)
Definition Plane.cs:315
PlaneIntersectionType Intersects(BoundingBox box)
Definition Plane.cs:265
float Dot(Vector4 value)
Definition Plane.cs:235
static Plane Transform(Plane plane, Quaternion rotation)
Definition Plane.cs:169
bool Equals(Plane other)
Definition Plane.cs:60
static bool operator!=(Plane lhs, Plane rhs)
Definition Plane.cs:362
Plane(float a, float b, float c, float d)
Definition Plane.cs:19
Plane(Vector3 normal, float d)
Definition Plane.cs:27
void Dot(ref Vector4 value, out float result)
Definition Plane.cs:240
PlaneIntersectionType Intersects(BoundingSphere sphere)
Definition Plane.cs:324
Plane(Vector4 value)
Definition Plane.cs:33
static Plane Normalize(Plane value)
Definition Plane.cs:107
override bool Equals(object obj)
Definition Plane.cs:69
static void Normalize(ref Plane value, out Plane result)
Definition Plane.cs:125
void DotNormal(ref Vector3 value, out float result)
Definition Plane.cs:260
override string ToString()
Definition Plane.cs:84
Plane(Vector3 point1, Vector3 point2, Vector3 point3)
Definition Plane.cs:41
static bool operator==(Plane lhs, Plane rhs)
Definition Plane.cs:357
static void Transform(ref Plane plane, ref Matrix matrix, out Plane result)
Definition Plane.cs:156
static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result)
Definition Plane.cs:203
override string ToString()
Definition Vector3.cs:85