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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/// A 3-element vector.
///
/// [Vec3] implements many traits, including [Add](::std::ops::Add), [Sub](::std::ops::Sub),
/// [Mul](::std::ops::Mul) and [Div](::std::ops::Div), among others.
#[derive(PartialEq, Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] // codecov:ignore:this
pub struct Vec3 {
x: f64,
y: f64,
z: f64,
}
impl Vec3 {
/// (1.0, 0.0, 0.0).
pub const UNIT_X: Vec3 = Vec3 {
x: 1.0,
y: 0.0,
z: 0.0,
};
/// (0.0, 1.0, 0.0).
pub const UNIT_Y: Vec3 = Vec3 {
x: 0.0,
y: 1.0,
z: 0.0,
};
/// (0.0, 0.0, 1.0).
pub const UNIT_Z: Vec3 = Vec3 {
x: 0.0,
y: 0.0,
z: 1.0,
};
/// (-1.0, 0.0, 0.0).
pub const NEG_UNIT_X: Vec3 = Vec3 {
x: -1.0,
y: 0.0,
z: 0.0,
};
/// (0.0, -1.0, 0.0).
pub const NEG_UNIT_Y: Vec3 = Vec3 {
x: 0.0,
y: -1.0,
z: 0.0,
};
/// (0.0, 0.0, -1.0).
pub const NEG_UNIT_Z: Vec3 = Vec3 {
x: 0.0,
y: 0.0,
z: -1.0,
};
/// Origin: (0.0, 0.0, 0.0).
pub const ZERO: Vec3 = Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
};
/// Creates a 3-dimensional vector from the given x, y, z components.
pub fn new(vx: f64, vy: f64, vz: f64) -> Self {
Vec3 {
x: 0.0 + vx,
y: 0.0 + vy,
z: 0.0 + vz,
}
}
/// Creates a 3-dimensional vector of unit length from the given x, y, z components.
/// If the given components are all `0.0` then `Vec3::ZERO` is returned.
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// assert_eq!(Vec3::new_unit(2.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0));
/// ```
pub fn new_unit(vx: f64, vy: f64, vz: f64) -> Self {
let n = (vx * vx + vy * vy + vz * vz).sqrt();
if n == 0.0 {
Vec3::ZERO
} else {
let s = 1.0 / n;
Vec3::new(s * vx, s * vy, s * vz)
}
}
/// Creates a 3-dimensional vector of unit length which is the mean of all given vectors: unit length vector of
/// the sum of all vectors.
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// let vs = vec![Vec3::UNIT_X, Vec3::UNIT_Y, Vec3::UNIT_Z];
/// let c = 1.0 / 3.0_f64.sqrt();
/// assert_eq!(Vec3::new(c, c, c), Vec3::mean(&vs));
/// ```
pub fn mean(vs: &[Self]) -> Self {
let mut x = 0.0;
let mut y = 0.0;
let mut z = 0.0;
for v in vs {
x += v.x();
y += v.y();
z += v.z();
}
Vec3::new_unit(x, y, z)
}
/// Returns the x component of this vector.
#[inline]
pub fn x(self) -> f64 {
self.x
}
/// Returns the y component of this vector.
#[inline]
pub fn y(self) -> f64 {
self.y
}
/// Returns the z component of this vector.
#[inline]
pub fn z(self) -> f64 {
self.z
}
/// Returns the vector perpendicular to this vector and the given vector (cross product).
/// Note that cross product is unstable for nearly parallel (coincidental or opposite) vectors.
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// let v1 = Vec3::new(1.0, 5.0, 4.0);
/// let v2 = Vec3::new(2.0, 6.0, 5.0);
///
/// assert_eq!(v1.cross_prod(v2), Vec3::new(1.0, 3.0, -4.0));
/// ```
pub fn cross_prod(self, o: Self) -> Self {
let x = self.y() * o.z() - self.z() * o.y();
let y = self.z() * o.x() - self.x() * o.z();
let z = self.x() * o.y() - self.y() * o.x();
Vec3::new(x, y, z)
}
/// Returns the unit length vector perpendicular to this vector and the given vector (normalised cross product).
/// See also [crate::Vec3::cross_prod] and [crate::Vec3::unit].
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// let v1 = Vec3::new(2.0, 0.0, 0.0);
/// let v2 = Vec3::new(0.0, 2.0, 0.0);
///
/// assert_eq!(v1.cross_prod_unit(v2), Vec3::new(0.0, 0.0, 1.0));
/// ```
pub fn cross_prod_unit(self, o: Self) -> Self {
let x = self.y() * o.z() - self.z() * o.y();
let y = self.z() * o.x() - self.x() * o.z();
let z = self.x() * o.y() - self.y() * o.x();
Vec3::new_unit(x, y, z)
}
/// Returns the dot product of this vector and the given vector. Equivalently the dot product of 2 vectors
/// is the product of their magnitudes, times the cosine of the angle between them.
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// let v1 = Vec3::new(1.0, 5.0, 4.0);
/// let v2 = Vec3::new(2.0, 6.0, 5.0);
///
/// assert_eq!(v1.dot_prod(v2), 52.0);
/// ```
pub fn dot_prod(self, o: Self) -> f64 {
self.x() * o.x() + self.y() * o.y() + self.z() * o.z()
}
/// Returns a unit-length vector that is orthogonal to this vector and the given one.
///
/// This function is similar to [v1 x v2](crate::Vec3::cross_prod_unit) except that it does a better job of ensuring
/// orthogonality when both vectors are nearly parallel and it returns a non-zero result even when
/// both vectors are equal or opposite.
///
/// See [S2Geometry GetStableCrossProd](https://github.com/google/s2geometry/blob/master/src/s2/s2edge_crossings_internal.h).
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// assert_eq!(
/// Vec3::new(0.0, 0.0, 1.0),
/// Vec3::new(1.0, 0.0, 0.0).orthogonal_to(Vec3::new(0.0, 1.0, 0.0))
/// );
/// assert_eq!(
/// Vec3::new(0.0, -1.0, 0.0),
/// Vec3::new(1.0, 0.0, 0.0).orthogonal_to(Vec3::new(1.0, 0.0, 0.0))
/// );
/// assert_eq!(
/// Vec3::new(0.0, -1.0, 0.0),
/// Vec3::new(1.0, 0.0, 0.0).orthogonal_to(Vec3::new(-1.0, 0.0, 0.0))
/// );
/// ```
pub fn orthogonal_to(&self, o: Self) -> Self {
// The direction of v1 x v2 is unstable as v2 + v1 or v2 - v1 approaches 0. To avoid this,
// we just compute (v2 + v1) x (v2 - v1) which is twice the cross product of v2 and v1, but
// is always perpendicular (since both v1 and v2 are unit-length vectors).
let r = self.stable_cross_prod_unit(o);
if r == Vec3::ZERO {
// return an arbitrary orthogonal vector.
self.orthogonal()
} else {
r
}
}
/// Returns a unit length vector orthogonal to this vector.
///
/// # Examples:
/// ```
/// use jord::Vec3;
///
/// let v = Vec3::new(3.0, 2.0, 1.0);
/// let o = v.orthogonal();
///
/// assert_eq!(o.z(), 0.0);
/// ```
pub fn orthogonal(self) -> Self {
let ax = self.x().abs();
let ay = self.y().abs();
let az = self.z().abs();
let tmp;
if ax > ay {
if ax > az {
// largest is x: select z axis.
tmp = Vec3::UNIT_Z;
} else {
// largest is z: select y axis.
tmp = Vec3::UNIT_Y;
}
} else if ay > az {
// largest is y: select x axis.
tmp = Vec3::UNIT_X;
} else {
// largest is z: select y axis.
tmp = Vec3::UNIT_Y;
}
self.cross_prod_unit(tmp)
}
/// Euclidean norm of this vector (square root of the dot product with itself).
pub fn norm(self) -> f64 {
self.squared_norm().sqrt()
}
/// Similar to [stable_cross_prod](crate::Vec3::stable_cross_prod), but returns a unit vector (without creating an intermediate
/// [Vec3].
///
/// #Examples:
///
/// ```
/// use jord::Vec3;
///
/// let v1 = Vec3::new(2.0, 0.0, 0.0);
/// let v2 = Vec3::new(0.0, 2.0, 0.0);
/// assert_eq!(Vec3::new(0.0, 0.0, 1.0), v1.stable_cross_prod_unit(v2));
/// ```
pub fn stable_cross_prod_unit(self, o: Self) -> Self {
// a = v2 + v1
let xa = o.x() + self.x();
let ya = o.y() + self.y();
let za = o.z() + self.z();
// b = v2 - v1
let xb = o.x() - self.x();
let yb = o.y() - self.y();
let zb = o.z() - self.z();
// a x b
let x = ya * zb - za * yb;
let y = za * xb - xa * zb;
let z = xa * yb - ya * xb;
Vec3::new_unit(x, y, z)
}
/// Calculates the vector perpendicular to given unit vectors in a numerically stable way. The direction of v1 x
/// v2 is unstable as v2 + v1 or v2 - v1 approaches 0. In order to workaround this, this method computes (v2 +
/// v1) x (v2 - v1) which is twice the cross product of v2 and v1, but is always perpendicular (since both v1
/// and v2 are unit-length vectors).
///
/// #Examples:
///
/// ```
/// use jord::Vec3;
///
/// let v1 = Vec3::new(2.0, 0.0, 0.0);
/// let v2 = Vec3::new(0.0, 2.0, 0.0);
/// assert_eq!(Vec3::new(0.0, 0.0, 8.0), v1.stable_cross_prod(v2));
/// ```
pub fn stable_cross_prod(self, o: Self) -> Self {
// a = v2 + v1
let xa = o.x() + self.x();
let ya = o.y() + self.y();
let za = o.z() + self.z();
// b = v2 - v1
let xb = o.x() - self.x();
let yb = o.y() - self.y();
let zb = o.z() - self.z();
// a x b
let x = ya * zb - za * yb;
let y = za * xb - xa * zb;
let z = xa * yb - ya * xb;
Vec3::new(x, y, z)
}
/// Squared Euclidean norm of this vector (the dot product with itself).
pub fn squared_norm(self) -> f64 {
self.dot_prod(self)
}
/// Normalised vector (or unit length vector) if the norm of this vector is nonzero.
///
/// # Examples
///
/// ```
/// use jord::Vec3;
///
/// let v = Vec3::new(2.0, 0.0, 0.0);
/// assert_eq!(v.unit(), Vec3::new(1.0, 0.0, 0.0));
/// ```
pub fn unit(self) -> Self {
let n = self.norm();
if n == 0.0 {
Vec3::ZERO
} else {
let s = 1.0 / n;
s * self
}
}
}
impl ::std::ops::Add for Vec3 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Vec3::new(self.x() + rhs.x(), self.y() + rhs.y(), self.z() + rhs.z())
}
}
impl ::std::ops::Sub for Vec3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Vec3::new(self.x() - rhs.x(), self.y() - rhs.y(), self.z() - rhs.z())
}
}
impl ::std::ops::Mul<f64> for Vec3 {
type Output = Self;
fn mul(self, rhs: f64) -> Self {
Vec3::new(self.x() * rhs, self.y() * rhs, self.z() * rhs)
}
}
impl ::std::ops::Mul<Vec3> for f64 {
type Output = Vec3;
fn mul(self, rhs: Vec3) -> Self::Output {
Vec3::new(rhs.x() * self, rhs.y() * self, rhs.z() * self)
}
}
impl ::std::ops::Div<f64> for Vec3 {
type Output = Self;
fn div(self, rhs: f64) -> Self {
Vec3::new(self.x() / rhs, self.y() / rhs, self.z() / rhs)
}
}
impl From<(f64, f64, f64)> for Vec3 {
fn from(tuple: (f64, f64, f64)) -> Self {
Vec3::new(tuple.0, tuple.1, tuple.2)
}
}
impl ::std::ops::Neg for Vec3 {
type Output = Self;
fn neg(self) -> Self::Output {
Vec3::new(-self.x, -self.y, -self.z)
}
}
#[cfg(test)]
mod tests {
use crate::Vec3;
#[test]
fn cross_prod() {
let v1: Vec3 = (1.0, 5.0, 4.0).into();
let v2: Vec3 = (2.0, 6.0, 5.0).into();
assert_eq!(Vec3::new(1.0, 3.0, -4.0), v1.cross_prod(v2));
let v3: Vec3 = (2.0, 0.0, 0.0).into();
let v4 = (0.0, 2.0, 0.0).into();
assert_eq!(Vec3::new(0.0, 0.0, 8.0), v3.stable_cross_prod(v4));
assert_eq!(Vec3::new(0.0, 0.0, 1.0), v3.stable_cross_prod_unit(v4));
}
#[test]
fn unit() {
assert_eq!(Vec3::ZERO, Vec3::ZERO.unit());
assert_eq!(Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 8.0).unit());
}
#[test]
fn orthogonal_x_largest() {
let v = Vec3::new_unit(3.0, 2.0, 1.0);
let o = v.orthogonal();
assert_eq!(o.z(), 0.0);
assert_eq!(v.dot_prod(o), 0.0);
}
#[test]
fn orthogonal_y_largest() {
let v = Vec3::new_unit(2.0, 3.0, 1.0);
let o = v.orthogonal();
assert_eq!(o.x(), 0.0);
assert_eq!(v.dot_prod(o), 0.0);
}
#[test]
fn orthogonal_z_largest() {
let v = Vec3::new_unit(1.0, 2.0, 3.0);
let o = v.orthogonal();
assert_eq!(o.y(), 0.0);
assert_eq!(v.dot_prod(o), 0.0);
}
#[test]
fn orthogonal_z_largest_2() {
let v = Vec3::new_unit(2.0, 1.0, 3.0);
let o = v.orthogonal();
assert_eq!(o.y(), 0.0);
assert_eq!(v.dot_prod(o), 0.0);
}
}