-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector3.cs
More file actions
245 lines (215 loc) · 7.78 KB
/
Copy pathVector3.cs
File metadata and controls
245 lines (215 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using MathNet.Numerics.LinearAlgebra.Double;
namespace GFunctions.Mathnet
{
/// <summary>
/// A 3 dimensional (XYZ) vector
/// </summary>
public class Vector3
{
// -------------------- Properties --------------------
/// <summary>
/// X Component
/// </summary>
public double X { get; set; } = 0;
/// <summary>
/// Y Component
/// </summary>
public double Y { get; set; } = 0;
/// <summary>
/// Z Component
/// </summary>
public double Z { get; set; } = 0;
/// <summary>
/// Vector length
/// </summary>
public double Magnitude => Math.Sqrt(X * X + Y * Y + Z * Z);
// -------------------- Constructors --------------------
/// <summary>
/// Initialize to zero
/// </summary>
public Vector3() { }
/// <summary>
/// Initialize with values
/// </summary>
/// <param name="x">X Component</param>
/// <param name="y">Y Component</param>
/// <param name="z">Z Component</param>
public Vector3(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Initialize from an array
/// </summary>
/// <param name="array">An array of [x, y, z]</param>
public Vector3(double[] array)
{
if (array.Length != 3)
throw new ArgumentException("Cannot initialize Vector3 with an array other than length of 3");
X = array[0];
Y = array[1];
Z = array[2];
}
// -------------------- Public Methods --------------------
/// <summary>
/// Normalize the vector to a magnitude, while maintaining direction
/// </summary>
/// <param name="newMagnitude">Optional magnitude (defaults to 1)</param>
/// <exception cref="InvalidOperationException">Can't normalize a vector with zero length</exception>
public void Normalize(double newMagnitude = 1.0)
{
double magnitude = Magnitude;
if (magnitude == 0)
throw new InvalidOperationException("Cannot normalize a zero-length vector.");
X *= newMagnitude / magnitude;
Y *= newMagnitude / magnitude;
Z *= newMagnitude / magnitude;
}
/// <summary>
/// Gets the unit vector (length = 1) of this vector
/// </summary>
/// <returns>The unit vector</returns>
/// <exception cref="InvalidOperationException">Can't normalize a vector with zero length</exception>
public Vector3 GetUnitVector()
{
double mag = Magnitude;
if (mag == 0)
throw new InvalidOperationException("Cannot normalize a zero-length vector.");
return new Vector3(X / mag, Y / mag, Z / mag);
}
/// <summary>
/// Compute the vector dot product
/// </summary>
/// <param name="other">Other vector</param>
/// <returns>Dot product between this vector and the other</returns>
public double Dot(Vector3 other)
{
return X * other.X + Y * other.Y + Z * other.Z;
}
/// <summary>
/// Compute the length between this vector and the other one
/// </summary>
/// <param name="other">Other vector</param>
/// <returns>The distance between this vector and the other</returns>
public double LengthBetween(Vector3 other)
{
return KinematicMath.VectorLength(ToArray(), other.ToArray());
}
/// <summary>
/// Get the array implementation (X, Y, Z)
/// </summary>
/// <returns>The vector in array form</returns>
public double[] ToArray()
{
return [X, Y, Z];
}
/// <summary>
/// Get the matrix with values in a single column
/// </summary>
/// <returns>The vector in column maxtrix form</returns>
public DenseMatrix ToColumnMatrix()
{
DenseMatrix matrix = new(3, 1);
matrix.SetColumn(0, ToArray());
return matrix;
}
/// <summary>
/// Perform an arbitrary operation on all coordinates of the vector
/// </summary>
/// <param name="operation">The function to operate on each coordinate in the vector</param>
/// <returns>A vector which is the result of the operation on X, Y, and Z</returns>
public Vector3 Operate(Func<double, double> operation)
{
return new Vector3(
operation(X),
operation(Y),
operation(Z)
);
}
/// <summary>
/// Checks if this vector equals another one
/// </summary>
/// <param name="obj">Input object to test</param>
/// <returns>True of the objects are equivalent</returns>
public override bool Equals(object? obj)
{
if (obj is Vector3 v)
{
return X == v.X && Y == v.Y && Z == v.Z;
}
return false;
}
/// <summary>
/// Compute the hash code
/// </summary>
/// <returns>The object hash code</returns>
public override int GetHashCode()
{
return HashCode.Combine(X, Y, Z);
}
// -------------------- Operator overloads --------------------
/// <summary>
/// Add two vectors together
/// </summary>
/// <param name="a">Vector a</param>
/// <param name="b">Vector b</param>
/// <returns>Sum of the two vectors</returns>
public static Vector3 operator +(Vector3 a, Vector3 b)
{
return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
/// <summary>
/// Subtract two vectors
/// </summary>
/// <param name="a">Vector a</param>
/// <param name="b">Vector b</param>
/// <returns>Difference of the two vectors</returns>
public static Vector3 operator -(Vector3 a, Vector3 b)
{
return new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
/// <summary>
/// Negate a vector
/// </summary>
/// <param name="v">Input vector</param>
/// <returns>The vector, negated</returns>
public static Vector3 operator -(Vector3 v)
{
return new Vector3(-v.X, -v.Y, -v.Z);
}
/// <summary>
/// Multiply a vector by a value
/// </summary>
/// <param name="v">The vector</param>
/// <param name="scalar">The scalar</param>
/// <returns>The vector multiplied by the scalar</returns>
public static Vector3 operator *(Vector3 v, double scalar)
{
return new Vector3(v.X * scalar, v.Y * scalar, v.Z * scalar);
}
/// <summary>
/// Multiply a vector by a value
/// </summary>
/// <param name="v">The vector</param>
/// <param name="scalar">The scalar</param>
/// <returns>The vector multiplied by the scalar</returns>
public static Vector3 operator *(double scalar, Vector3 v)
{
return v * scalar;
}
/// <summary>
/// Divide a vector by a value
/// </summary>
/// <param name="v">The vector</param>
/// <param name="scalar">The scalar</param>
/// <returns>The vector divided by the scalar</returns>
public static Vector3 operator /(Vector3 v, double scalar)
{
if (scalar == 0)
throw new DivideByZeroException("Cannot divide by zero.");
return new Vector3(v.X / scalar, v.Y / scalar, v.Z / scalar);
}
}
}