geo_aid_script/
geometry.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Utility crate providing functions for geometry computations.

use std::{
    fmt::Display,
    iter::{Product, Sum},
    ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign},
};

use geo_aid_figure::Position;
use serde::Serialize;

/// Represents a complex number
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Complex {
    /// The real part
    pub real: f64,
    /// The imaginary part.
    pub imaginary: f64,
}

impl Complex {
    /// Create a new complex from its real and imaginary parts.
    #[must_use]
    #[inline]
    pub const fn new(real: f64, imaginary: f64) -> Self {
        Self { real, imaginary }
    }

    /// Convert a real to a complex.
    #[must_use]
    pub const fn real(real: f64) -> Self {
        Self::new(real, 0.0)
    }

    /// Create a new complex from its polar representation.
    #[must_use]
    pub fn polar(theta: f64, radius: f64) -> Self {
        Self::new(theta.cos(), theta.sin()) * radius
    }

    /// Complex zero.
    #[must_use]
    #[inline]
    pub const fn zero() -> Self {
        Self::new(0.0, 0.0)
    }

    /// Complex one.
    #[must_use]
    #[inline]
    pub const fn one() -> Self {
        Self::new(1.0, 0.0)
    }

    /// The imaginary unit.
    #[must_use]
    pub const fn i() -> Self {
        Self::new(0.0, 1.0)
    }

    /// Optimized multiplication by the complex unit (i).
    #[must_use]
    pub fn mul_i(self) -> Complex {
        Complex::new(-self.imaginary, self.real)
    }

    /// The magnitude of the complex, also called its modulus.
    #[must_use]
    pub fn magnitude(self) -> f64 {
        f64::sqrt(self.real.powi(2) + self.imaginary.powi(2))
    }

    /// The complex's conjugate (a - bi)
    #[must_use]
    pub fn conjugate(self) -> Complex {
        Complex::new(self.real, -self.imaginary)
    }

    /// Multiply the complex's parts by other complex's parts (ab + cdi)
    #[must_use]
    pub fn partial_mul(self, other: Complex) -> Complex {
        Complex::new(self.real * other.real, self.imaginary * other.imaginary)
    }

    /// Divide the complex's parts by other complex's parts (a/b + (c/d)i)
    #[must_use]
    pub fn partial_div(self, other: Complex) -> Complex {
        Complex::new(self.real / other.real, self.imaginary / other.imaginary)
    }

    /// Compute the complex's argument
    #[must_use]
    pub fn arg(self) -> f64 {
        f64::atan2(self.imaginary, self.real)
    }

    /// Normalize the complex by dividing it by its own magnitude.
    #[must_use]
    pub fn normalize(self) -> Complex {
        self / self.magnitude()
    }

    /// Number-theoretical norm. Simply a^2 + b^2 with self = a + bi
    #[must_use]
    pub fn len_squared(self) -> f64 {
        self.real * self.real + self.imaginary * self.imaginary
    }

    /// A square root. Chooses the one with non-negative imaginary part.
    #[must_use]
    pub fn sqrt(self) -> Complex {
        // The formula used here doesn't work for negative reals. We can use a trick here to bypass that restriction.
        // If the real part is negative, we simply negate it to get a positive part and then multiply the result by `i`.
        if self.real > 0.0 {
            // Use the generic formula (https://math.stackexchange.com/questions/44406/how-do-i-get-the-square-root-of-a-complex-number)
            let r = self.magnitude();

            r.sqrt() * (self + r).normalize()
        } else {
            (-self).sqrt().mul_i()
        }
    }

    /// Same as sqrt, but returns a normalized result.
    #[must_use]
    pub fn sqrt_norm(self) -> Complex {
        // The formula used here doesn't work for negative reals. We can use a trick here to bypass that restriction.
        // If the real part is negative, we simply negate it to get a positive part and then multiply the result by `i`.
        if self.real > 0.0 {
            // Use the generic formula (https://math.stackexchange.com/questions/44406/how-do-i-get-the-square-root-of-a-complex-number)
            let r = self.magnitude();

            // We simply don't multiply by the square root of r.
            (self + r).normalize()
        } else {
            (-self).sqrt_norm().mul_i() // Normalization isn't lost here.
        }
    }

    /// Inverse of the number.
    #[must_use]
    pub fn inverse(self) -> Self {
        self.conjugate() / self.len_squared()
    }
}

impl From<Complex> for Position {
    fn from(value: Complex) -> Self {
        Self {
            x: value.real,
            y: value.imaginary,
        }
    }
}

impl From<Complex> for geo_aid_figure::Complex {
    fn from(value: Complex) -> Self {
        Self {
            real: value.real,
            imaginary: value.imaginary,
        }
    }
}

impl From<Position> for Complex {
    fn from(value: Position) -> Self {
        Self::new(value.x, value.y)
    }
}

impl Mul for Complex {
    type Output = Complex;

    fn mul(self, rhs: Complex) -> Self::Output {
        Complex::new(
            self.real * rhs.real - self.imaginary * rhs.imaginary,
            self.real * rhs.imaginary + rhs.real * self.imaginary,
        )
    }
}

impl Mul<Complex> for f64 {
    type Output = Complex;

    fn mul(self, rhs: Complex) -> Self::Output {
        Complex::new(self * rhs.real, self * rhs.imaginary)
    }
}

impl Mul<f64> for Complex {
    type Output = Complex;

    fn mul(self, rhs: f64) -> Self::Output {
        Complex::new(self.real * rhs, self.imaginary * rhs)
    }
}

impl MulAssign<f64> for Complex {
    fn mul_assign(&mut self, rhs: f64) {
        self.real *= rhs;
        self.imaginary *= rhs;
    }
}

impl Add<f64> for Complex {
    type Output = Complex;

    fn add(self, rhs: f64) -> Self::Output {
        Complex::new(self.real + rhs, self.imaginary)
    }
}

impl Add for Complex {
    type Output = Complex;

    fn add(self, rhs: Self) -> Self::Output {
        Complex::new(self.real + rhs.real, self.imaginary + rhs.imaginary)
    }
}

impl Div<f64> for Complex {
    type Output = Complex;

    fn div(self, rhs: f64) -> Self::Output {
        Complex::new(self.real / rhs, self.imaginary / rhs)
    }
}

impl Div for Complex {
    type Output = Complex;

    fn div(self, rhs: Complex) -> Self::Output {
        (self * rhs.conjugate()) / (rhs.real * rhs.real + rhs.imaginary * rhs.imaginary)
    }
}

impl Sub<f64> for Complex {
    type Output = Complex;

    fn sub(self, rhs: f64) -> Self::Output {
        Complex::new(self.real - rhs, self.imaginary)
    }
}

impl Sub for Complex {
    type Output = Complex;

    fn sub(self, rhs: Self) -> Self::Output {
        Complex::new(self.real - rhs.real, self.imaginary - rhs.imaginary)
    }
}

impl SubAssign for Complex {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl AddAssign for Complex {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl MulAssign for Complex {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl Display for Complex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(precision) = f.precision() {
            write!(
                f,
                "{2:.*} + {3:.*}i",
                precision, precision, self.real, self.imaginary
            )
        } else {
            write!(f, "{} + {}i", self.real, self.imaginary)
        }
    }
}

impl Neg for Complex {
    type Output = Complex;

    fn neg(self) -> Self::Output {
        Complex::new(-self.real, -self.imaginary)
    }
}

impl Default for Complex {
    fn default() -> Self {
        Self {
            real: 0.0,
            imaginary: 0.0,
        }
    }
}

impl Sum for Complex {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        let mut v = Complex::zero();

        for x in iter {
            v += x;
        }

        v
    }
}

impl Product for Complex {
    fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
        let mut v = Complex::zero();

        for x in iter {
            v *= x;
        }

        v
    }
}

/// Represents a line in a 2D Euclidean space.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Line {
    /// Line's origin as a complex number.
    pub origin: Complex,
    /// A normalized direction vector.
    pub direction: Complex,
}

impl Line {
    /// Creates a new line from its origin and a direction vector
    #[must_use]
    pub fn new(origin: Complex, direction: Complex) -> Self {
        Self {
            origin,
            direction: direction.normalize(),
        }
    }
}

impl From<Line> for geo_aid_figure::Line {
    fn from(value: Line) -> Self {
        Self {
            origin: value.origin.into(),
            direction: value.direction.into(),
        }
    }
}

/// Represents a circle in a 2D euclidean space.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct Circle {
    /// Circle's center.
    pub center: Complex,
    /// Its radius
    pub radius: f64,
}

impl From<Circle> for geo_aid_figure::Circle {
    fn from(value: Circle) -> Self {
        Self {
            center: value.center.into(),
            radius: value.radius,
        }
    }
}

/// Enumerated value type for serialization.
#[derive(Debug, Clone, Copy, Serialize)]
pub enum ValueEnum {
    Complex(Complex),
    Line(Line),
    Circle(Circle),
}

impl ValueEnum {
    #[must_use]
    pub fn as_complex(self) -> Option<Complex> {
        match self {
            Self::Complex(c) => Some(c),
            _ => None,
        }
    }

    #[must_use]
    pub fn as_line(self) -> Option<Line> {
        match self {
            Self::Line(l) => Some(l),
            _ => None,
        }
    }

    #[must_use]
    pub fn as_circle(self) -> Option<Circle> {
        match self {
            Self::Circle(l) => Some(l),
            _ => None,
        }
    }
}

impl From<ValueEnum> for geo_aid_figure::Value {
    fn from(value: ValueEnum) -> Self {
        match value {
            ValueEnum::Complex(c) => Self::Complex(c.into()),
            ValueEnum::Line(ln) => Self::Line(ln.into()),
            ValueEnum::Circle(c) => Self::Circle(c.into()),
        }
    }
}

/// Get the line going through `p1` and `p2`
#[must_use]
pub fn get_line(p1: Complex, p2: Complex) -> Line {
    Line {
        origin: p1,
        direction: (p2 - p1).normalize(),
    }
}

/// Gets the intersection point of two lines.
#[must_use]
pub fn get_intersection(k_ln: Line, l_ln: Line) -> Complex {
    let Line {
        origin: a,
        direction: b,
    } = k_ln;
    let Line {
        origin: c,
        direction: d,
    } = l_ln;

    a - b * ((a - c) / d).imaginary / (b / d).imaginary
}

/// Gets the angle between two arms and the origin
#[must_use]
pub fn get_angle(arm1: Complex, origin: Complex, arm2: Complex) -> f64 {
    // Get the vectors to calculate the angle between them.
    let arm1_vec = arm1 - origin;
    let arm2_vec = arm2 - origin;

    // Get the dot product
    let dot_product = arm1_vec.real * arm2_vec.real + arm1_vec.imaginary * arm2_vec.imaginary;

    // Get the argument
    f64::acos(dot_product / (arm1_vec.magnitude() * arm2_vec.magnitude()))
}

/// Gets the directed angle between two arms and the origin
#[must_use]
pub fn get_angle_directed(arm1: Complex, origin: Complex, arm2: Complex) -> f64 {
    // Get the vectors to calculate the angle between them.
    let arm1_vec = arm1 - origin;
    let arm2_vec = arm2 - origin;

    // decrease p2's angle by p1's angle:
    let p2_rotated = arm2_vec / arm1_vec;

    // Get the argument
    p2_rotated.arg()
}

// Rotates p around origin by angle.
#[must_use]
pub fn rotate_around(p: Complex, origin: Complex, angle: f64) -> Complex {
    (p - origin) * Complex::new(angle.cos(), angle.sin()) + origin
}

// Computes Point-Line distance.
#[must_use]
pub fn distance_pt_ln(point: Complex, line: Line) -> f64 {
    // Make the point coordinates relative to the origin and rotate.
    let point_rot = (point - line.origin) / line.direction;

    // Now we can just get the imaginary part. We have to take the absolute value here.
    point_rot.imaginary.abs()
}

// Computes Point-Point distance.
#[must_use]
pub fn distance_pt_pt(p1: Complex, p2: Complex) -> f64 {
    ((p1.real - p2.real) * (p1.real - p2.real)
        + (p1.imaginary - p2.imaginary) * (p1.imaginary - p2.imaginary))
        .sqrt()
}