Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Plane.cs
Go to the documentation of this file.
3
4namespace System.Numerics;
5
6[Intrinsic]
7public struct Plane : IEquatable<Plane>
8{
9 public Vector3 Normal;
10
11 public float D;
12
13 public Plane(float x, float y, float z, float d)
14 {
15 Normal = new Vector3(x, y, z);
16 D = d;
17 }
18
19 public Plane(Vector3 normal, float d)
20 {
21 Normal = normal;
22 D = d;
23 }
24
26 {
27 Normal = new Vector3(value.X, value.Y, value.Z);
28 D = value.W;
29 }
30
31 [MethodImpl(MethodImplOptions.AggressiveInlining)]
32 public static Plane CreateFromVertices(Vector3 point1, Vector3 point2, Vector3 point3)
33 {
35 {
36 Vector3 vector = point2 - point1;
37 Vector3 vector2 = point3 - point1;
38 Vector3 value = Vector3.Cross(vector, vector2);
39 Vector3 vector3 = Vector3.Normalize(value);
40 float d = 0f - Vector3.Dot(vector3, point1);
41 return new Plane(vector3, d);
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 x = num7 * num7 + num8 * num8 + num9 * num9;
53 float num10 = 1f / MathF.Sqrt(x);
54 Vector3 normal = new Vector3(num7 * num10, num8 * num10, num9 * num10);
55 return new Plane(normal, 0f - (normal.X * point1.X + normal.Y * point1.Y + normal.Z * point1.Z));
56 }
57
58 [MethodImpl(MethodImplOptions.AggressiveInlining)]
59 public static float Dot(Plane plane, Vector4 value)
60 {
61 return plane.Normal.X * value.X + plane.Normal.Y * value.Y + plane.Normal.Z * value.Z + plane.D * value.W;
62 }
63
64 [MethodImpl(MethodImplOptions.AggressiveInlining)]
65 public static float DotCoordinate(Plane plane, Vector3 value)
66 {
68 {
69 return Vector3.Dot(plane.Normal, value) + plane.D;
70 }
71 return plane.Normal.X * value.X + plane.Normal.Y * value.Y + plane.Normal.Z * value.Z + plane.D;
72 }
73
74 [MethodImpl(MethodImplOptions.AggressiveInlining)]
75 public static float DotNormal(Plane plane, Vector3 value)
76 {
78 {
79 return Vector3.Dot(plane.Normal, value);
80 }
81 return plane.Normal.X * value.X + plane.Normal.Y * value.Y + plane.Normal.Z * value.Z;
82 }
83
84 [MethodImpl(MethodImplOptions.AggressiveInlining)]
85 public static Plane Normalize(Plane value)
86 {
88 {
89 float num = value.Normal.LengthSquared();
90 if (MathF.Abs(num - 1f) < 1.1920929E-07f)
91 {
92 return value;
93 }
94 float num2 = MathF.Sqrt(num);
95 return new Plane(value.Normal / num2, value.D / num2);
96 }
97 float num3 = value.Normal.X * value.Normal.X + value.Normal.Y * value.Normal.Y + value.Normal.Z * value.Normal.Z;
98 if (MathF.Abs(num3 - 1f) < 1.1920929E-07f)
99 {
100 return value;
101 }
102 float num4 = 1f / MathF.Sqrt(num3);
103 return new Plane(value.Normal.X * num4, value.Normal.Y * num4, value.Normal.Z * num4, value.D * num4);
104 }
105
106 [MethodImpl(MethodImplOptions.AggressiveInlining)]
107 public static Plane Transform(Plane plane, Matrix4x4 matrix)
108 {
109 Matrix4x4.Invert(matrix, out var result);
110 float x = plane.Normal.X;
111 float y = plane.Normal.Y;
112 float z = plane.Normal.Z;
113 float d = plane.D;
114 return new Plane(x * result.M11 + y * result.M12 + z * result.M13 + d * result.M14, x * result.M21 + y * result.M22 + z * result.M23 + d * result.M24, x * result.M31 + y * result.M32 + z * result.M33 + d * result.M34, x * result.M41 + y * result.M42 + z * result.M43 + d * result.M44);
115 }
116
117 [MethodImpl(MethodImplOptions.AggressiveInlining)]
118 public static Plane Transform(Plane plane, Quaternion rotation)
119 {
120 float num = rotation.X + rotation.X;
121 float num2 = rotation.Y + rotation.Y;
122 float num3 = rotation.Z + rotation.Z;
123 float num4 = rotation.W * num;
124 float num5 = rotation.W * num2;
125 float num6 = rotation.W * num3;
126 float num7 = rotation.X * num;
127 float num8 = rotation.X * num2;
128 float num9 = rotation.X * num3;
129 float num10 = rotation.Y * num2;
130 float num11 = rotation.Y * num3;
131 float num12 = rotation.Z * num3;
132 float num13 = 1f - num10 - num12;
133 float num14 = num8 - num6;
134 float num15 = num9 + num5;
135 float num16 = num8 + num6;
136 float num17 = 1f - num7 - num12;
137 float num18 = num11 - num4;
138 float num19 = num9 - num5;
139 float num20 = num11 + num4;
140 float num21 = 1f - num7 - num10;
141 float x = plane.Normal.X;
142 float y = plane.Normal.Y;
143 float z = plane.Normal.Z;
144 return new Plane(x * num13 + y * num14 + z * num15, x * num16 + y * num17 + z * num18, x * num19 + y * num20 + z * num21, plane.D);
145 }
146
147 [MethodImpl(MethodImplOptions.AggressiveInlining)]
148 public static bool operator ==(Plane value1, Plane value2)
149 {
150 if (value1.Normal.X == value2.Normal.X && value1.Normal.Y == value2.Normal.Y && value1.Normal.Z == value2.Normal.Z)
151 {
152 return value1.D == value2.D;
153 }
154 return false;
155 }
156
157 [MethodImpl(MethodImplOptions.AggressiveInlining)]
158 public static bool operator !=(Plane value1, Plane value2)
159 {
160 return !(value1 == value2);
161 }
162
163 [MethodImpl(MethodImplOptions.AggressiveInlining)]
164 public override readonly bool Equals([NotNullWhen(true)] object? obj)
165 {
166 if (obj is Plane other)
167 {
168 return Equals(other);
169 }
170 return false;
171 }
172
173 [MethodImpl(MethodImplOptions.AggressiveInlining)]
174 public readonly bool Equals(Plane other)
175 {
177 {
178 if (Normal.Equals(other.Normal))
179 {
180 return D == other.D;
181 }
182 return false;
183 }
184 if (Normal.X == other.Normal.X && Normal.Y == other.Normal.Y && Normal.Z == other.Normal.Z)
185 {
186 return D == other.D;
187 }
188 return false;
189 }
190
191 public override readonly int GetHashCode()
192 {
193 return Normal.GetHashCode() + D.GetHashCode();
194 }
195
196 public override readonly string ToString()
197 {
198 return $"{{Normal:{Normal} D:{D}}}";
199 }
200}
static float Abs(float x)
Definition MathF.cs:130
static float Sqrt(float x)
static bool IsHardwareAccelerated
Definition Vector.cs:14
static bool Invert(Matrix4x4 matrix, out Matrix4x4 result)
Definition Matrix4x4.cs:855
static float DotNormal(Plane plane, Vector3 value)
Definition Plane.cs:75
static bool operator!=(Plane value1, Plane value2)
Definition Plane.cs:158
static float DotCoordinate(Plane plane, Vector3 value)
Definition Plane.cs:65
static Plane CreateFromVertices(Vector3 point1, Vector3 point2, Vector3 point3)
Definition Plane.cs:32
override readonly bool Equals([NotNullWhen(true)] object? obj)
Definition Plane.cs:164
Plane(Vector4 value)
Definition Plane.cs:25
static Plane Normalize(Plane value)
Definition Plane.cs:85
Plane(float x, float y, float z, float d)
Definition Plane.cs:13
static Plane Transform(Plane plane, Quaternion rotation)
Definition Plane.cs:118
override readonly int GetHashCode()
Definition Plane.cs:191
static float Dot(Plane plane, Vector4 value)
Definition Plane.cs:59
static bool operator==(Plane value1, Plane value2)
Definition Plane.cs:148
readonly bool Equals(Plane other)
Definition Plane.cs:174
override readonly string ToString()
Definition Plane.cs:196
static Plane Transform(Plane plane, Matrix4x4 matrix)
Definition Plane.cs:107
Plane(Vector3 normal, float d)
Definition Plane.cs:19
static Vector3 Cross(Vector3 vector1, Vector3 vector2)
Definition Vector3.cs:162
override readonly int GetHashCode()
Definition Vector3.cs:362
static float Dot(Vector3 vector1, Vector3 vector2)
Definition Vector3.cs:195
override readonly bool Equals([NotNullWhen(true)] object? obj)
Definition Vector3.cs:347
static Vector3 Normalize(Vector3 value)
Definition Vector3.cs:244