num_opt/physics/
linalg.rs

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
//! Vector and Quaternion math, generic over `rug::Float` and primative floats

use std::
    ops::{Add, Div, Mul, Sub};

use crate::my_float::{Fpt, MyFloat};

/// Projects `a` onto `b`
pub fn vector_projection<T: MyFloat>(a: MyVector3<T>, b: MyVector3<T>) -> MyVector3<T> {
    b.clone() * (a.dot(&b) / b.magnitude_squared())
}

pub fn scaler_projection<T: MyFloat>(a: MyVector3<T>, b: MyVector3<T>) -> T {
    a.dot(&b) / b.magnitude()
}

pub struct Silence;

#[derive(Clone)]
pub struct MyVector3<T: MyFloat> {
    pub x: T,
    pub y: T,
    pub z: T,
}

impl<T: MyFloat> MyVector3<T> {
    pub fn new(x: T, y: T, z: T) -> Self {
        Self { x, y, z }
    }

    pub fn new_f(x: Fpt, y: Fpt, z: Fpt) -> Self {
        Self {
            x: T::from_f(x),
            y: T::from_f(y),
            z: T::from_f(z),
        }
    }

    pub fn dot(&self, other: &MyVector3<T>) -> T {
        self.x.clone() * other.x.clone()
            + self.y.clone() * other.y.clone()
            + self.z.clone() * other.z.clone()
    }

    pub fn make_ortho_to(&self, other: &MyVector3<T>) -> MyVector3<T> {
        self.clone() - vector_projection(self.clone(), other.clone())
    }

    pub fn magnitude(&self) -> T {
        self.magnitude_squared().sqrt()
    }

    pub fn magnitude_squared(&self) -> T {
        self.x.clone() * self.x.clone()
            + self.y.clone() * self.y.clone()
            + self.z.clone() * self.z.clone()
    }

    pub fn normalize(self) -> Self {
        let mag = self.magnitude();
        Self {
            x: self.x / mag.clone(),
            y: self.y / mag.clone(),
            z: self.z / mag,
        }
    }

    pub fn angle(&self, other: &MyVector3<T>) -> T {
        self.angle_dbg::<Silence>(other)
    }

    pub fn angle_dbg<Q: 'static>(&self, other: &MyVector3<T>) -> T {
        /*let dot = self.dot(other);
        let mag1 = self.magnitude();
        let mag2 = other.magnitude();
        let cos = (dot.clone() / (mag1.clone() * mag2.clone())).clamp(-1.0, 1.0);
        if TypeId::of::<Q>() == TypeId::of::<()>() {
            log::warn!("cos: {} mag1: {} mag2: {} dot: {}", cos, mag1, mag2, dot);
        } else if TypeId::of::<Q>() != TypeId::of::<Silence>() {
            panic!();
        }
        cos.acos()*/
        //log::info!("Angle!");
        let self_mag = self.magnitude();
        let other_mag = other.magnitude();

        let self_mag_other = other.clone() * self_mag;
        let other_mag_self = self.clone() * other_mag;
        
        T::from_f(2.0) * T::atan2(
            &(self_mag_other.clone() - other_mag_self.clone()).magnitude(),
            &(self_mag_other + other_mag_self).magnitude()
        )
    }

    pub fn cross(&self, other: &MyVector3<T>) -> MyVector3<T> {
        MyVector3 {
            x: (self.y.clone() * other.z.clone() - self.z.clone() * other.y.clone()),
            y: (self.z.clone() * other.x.clone() - self.x.clone() * other.z.clone()),
            z: (self.x.clone() * other.y.clone() - self.y.clone() * other.x.clone()),
        }
    }

    pub fn has_nan(&self) -> bool {
        self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
    }

    pub fn rotate_around_axis(&self, axis: &Self, angle: Fpt) -> Self {
        let axis = axis.clone().normalize();
        // For a rotation of angle θ around axis (x,y,z), the scaled axis should be:
        // (x*sin(θ/2), y*sin(θ/2), z*sin(θ/2))
        // And the quaternion should have cos(θ/2) as its scalar part
        let half_angle = angle / 2.0;
        let sin_half = T::from_f(half_angle.sin());
        let cos_half = T::from_f(half_angle.cos());
        let quat = MyQuaternion {
            q0: cos_half,
            q1: axis.x * sin_half.clone(),
            q2: axis.y * sin_half.clone(),
            q3: axis.z * sin_half,
        };
        quat.rotate_vector(self)
    }
}

impl<T: MyFloat> AsRef<MyVector3<T>> for MyVector3<T> {
    fn as_ref(&self) -> &MyVector3<T> {
        self
    }
}

impl<T: MyFloat> std::fmt::Debug for MyVector3<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MV")
            .field("x", &self.x)
            .field("y", &self.y)
            .field("z", &self.z)
            .finish()
    }
}

impl<T: MyFloat> std::ops::Add for MyVector3<T> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

impl<T: MyFloat> std::ops::Sub<&MyVector3<T>> for MyVector3<T> {
    type Output = Self;
    fn sub(self, other: &Self) -> Self {
        Self {
            x: self.x - other.x.clone(),
            y: self.y - other.y.clone(),
            z: self.z - other.z.clone(),
        }
    }
}

impl<T: MyFloat> std::ops::Sub<MyVector3<T>> for MyVector3<T> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

impl<T: MyFloat> std::ops::Neg for MyVector3<T> {
    type Output = Self;
    fn neg(self) -> Self {
        Self {
            x: -self.x,
            y: -self.y,
            z: -self.z,
        }
    }
}

impl<T: MyFloat> Div<T> for MyVector3<T> {
    type Output = MyVector3<T>;
    fn div(self, other: T) -> MyVector3<T> {
        MyVector3 {
            x: self.x / other.clone(),
            y: self.y / other.clone(),
            z: self.z / other.clone(),
        }
    }
}

impl<T: MyFloat> Mul<T> for MyVector3<T> {
    type Output = MyVector3<T>;
    fn mul(self, other: T) -> MyVector3<T> {
        MyVector3 {
            x: self.x * other.clone(),
            y: self.y * other.clone(),
            z: self.z * other,
        }
    }
}

impl<T: MyFloat> Default for MyVector3<T> {
    fn default() -> Self {
        Self {
            x: T::zero(), //float!(0.0),
            y: T::zero(), //float!(0.0),
            z: T::zero(), //float!(0.0),
        }
    }
}

/// Only suitable for rotations
#[derive(Clone, Debug)]
pub struct MyQuaternion<T: MyFloat> {
    pub q0: T,
    pub q1: T,
    pub q2: T,
    pub q3: T,
}

impl<T: MyFloat> MyQuaternion<T> {
    pub fn from_scaled_axis(x: MyVector3<T>) -> Self {
        let half_angle: T = x.magnitude() / T::from_f(2.0);
        let x = x.normalize();
        Self {
            q0: half_angle.clone().cos(),
            q1: x.x * half_angle.clone().sin(),
            q2: x.y * half_angle.clone().sin(),
            q3: x.z * half_angle.sin(),
        }
    }

    pub fn normalize(self) -> Self {
        let mag = (self.q0.clone().pow(2) + self.q1.clone().pow(2)
            + self.q2.clone().pow(2)
            + self.q3.clone().pow(2))
        .sqrt();
        Self {
            q0: self.q0 / mag.clone(),
            q1: self.q1 / mag.clone(),
            q2: self.q2 / mag.clone(),
            q3: self.q3 / mag.clone(),
        }
    }

    pub fn unit_inverse(self) -> Self {
        Self {
            q0: self.q0,
            q1: -self.q1,
            q2: -self.q2,
            q3: -self.q3,
        }
    }

    pub fn rotate_vector(self, other: &MyVector3<T>) -> MyVector3<T> {
        let other = MyQuaternion {
            q0: T::zero(),
            q1: other.x.clone(),
            q2: other.y.clone(),
            q3: other.z.clone(),
        };
        let s = self.normalize();
        let inv = s.clone().unit_inverse();
        let other = inv * other * s;
        MyVector3 {
            x: other.q1,
            y: other.q2,
            z: other.q3,
        }
    }
}

impl<T: MyFloat> Mul<MyQuaternion<T>> for MyQuaternion<T> {
    type Output = MyQuaternion<T>;
    fn mul(self, other: MyQuaternion<T>) -> MyQuaternion<T> {
        MyQuaternion {
            q0: (self.q0.clone() * other.q0.clone() - self.q1.clone() * other.q1.clone())
                - self.q2.clone() * other.q2.clone()
                - self.q3.clone() * other.q3.clone(),
            q1: (self.q0.clone() * other.q1.clone() + self.q1.clone() * other.q0.clone())
                - self.q2.clone() * other.q3.clone()
                + self.q3.clone() * other.q2.clone(),
            q2: (self.q0.clone() * other.q2.clone() + self.q1.clone() * other.q3.clone())
                + self.q2.clone() * other.q0.clone()
                - self.q3.clone() * other.q1.clone(),
            q3: (self.q0.clone() * other.q3.clone() - self.q1.clone() * other.q2.clone())
                + self.q2.clone() * other.q1.clone()
                + self.q3.clone() * other.q0.clone(),
        }
    }
}

// Typed Vectors

#[derive(Debug, Clone)]
pub struct ComDisp<T: MyFloat>(MyVector3<T>);

impl<T: MyFloat> ComDisp<T> {
    pub fn magnitude(&self) -> T {
        self.0.magnitude()
    }
}

#[derive(Debug, Clone)]
pub struct ComPos<T: MyFloat>(MyVector3<T>);

impl<T: MyFloat> Sub for ComPos<T> {
    type Output = ComDisp<T>;

    fn sub(self, rhs: Self) -> Self::Output {
        ComDisp(self.0 - rhs.0)
    }
}

impl<T: MyFloat> Add<ComDisp<T>> for ComPos<T> {
    type Output = Self;

    fn add(self, rhs: ComDisp<T>) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl<T: MyFloat> ComPos<T> {
    pub fn new(v: &MyVector3<T>) -> Self {
        Self(v.clone())
    }

    fn dist_origin(&self) -> T {
        self.0.magnitude()
    }

    pub fn dist_between(&self, other: &Self) -> T {
        (self.clone() - other.clone()).magnitude()
    }

    pub fn height(&self) -> T {
        self.0.y.clone()
    }

    pub fn inner(&self) -> MyVector3<T> {
        self.0.clone()
    }
}

#[derive(Debug, Clone)]
pub struct ComVel<T: MyFloat>(MyVector3<T>);

impl<T: MyFloat> ComVel<T> {
    pub fn new(v: &MyVector3<T>) -> Self {
        Self(v.clone())
    }

    pub fn speed(&self) -> T {
        self.0.magnitude()
    }

    pub fn to_displacement(&self, delta_t: T) -> ComDisp<T> {
        ComDisp(self.0.clone() * delta_t)
    }

    pub fn inner(&self) -> MyVector3<T> {
        self.0.clone()
    }
}