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
pub mod unit;

use crate::direction::Direction;
use crate::line::Line;
use crate::pseudoscalar::PseudoScalar;
use crate::Scalar;

/// Grade-2 blade
#[derive(Debug, Copy, Clone)]
pub struct Point {
    pub e01: Scalar,
    pub e20: Scalar,
    pub e12: Scalar,
}

impl From<Direction> for Point {
    fn from(d: Direction) -> Point {
        Point {
            e01: d.e01,
            e20: d.e20,
            e12: 0f32,
        }
    }
}

impl std::ops::Mul<Scalar> for Point {
    type Output = Point;

    fn mul(self, s: Scalar) -> Point {
        Point {
            e01: self.e01 * s,
            e20: self.e20 * s,
            e12: self.e12 * s,
        }
    }
}

impl std::ops::Mul<Point> for Scalar {
    type Output = Point;

    fn mul(self, p: Point) -> Point {
        p * self
    }
}

impl std::ops::Add<Direction> for Point {
    type Output = Point;

    fn add(self, d: Direction) -> Point {
        Point {
            e01: self.e01 + d.e01,
            e20: self.e20 + d.e20,
            e12: self.e12,
        }
    }
}

impl super::Meet<Line> for Point {
    type Output = PseudoScalar;

    fn meet(self, l: Line) -> PseudoScalar {
        l.meet(self)
    }
}

impl super::Inner<Point> for Point {
    type Output = Scalar;

    fn inner(self, rhs: Point) -> Scalar {
        -self.e12 * rhs.e12
    }
}

impl super::Inner<Line> for Point {
    type Output = Line;

    fn inner(self, l: Line) -> Line {
        Line {
            e0: self.e01 * l.e1 + -self.e20 * l.e2,
            e1: self.e12 * l.e2,
            e2: -self.e12 * l.e1,
        }
    }
}

impl super::Dual for Point {
    type Output = Line;

    fn dual(self) -> Line {
        Line {
            e0: self.e12,
            e1: self.e20,
            e2: self.e01,
        }
    }
}

impl super::Join<Point> for Point {
    type Output = Line;

    fn join(self, rhs: Point) -> Line {
        use crate::{Dual, Meet};

        self.dual().meet(rhs.dual()).dual()
    }
}

impl Point {
    pub fn euc_norm(&self) -> Scalar {
        self.e12
    }

    pub fn ideal_norm(&self) -> Scalar {
        Scalar::sqrt(self.e20.powi(2) + self.e01.powi(2))
    }

    pub fn is_finite(&self) -> bool {
        self.e01.is_finite() && self.e20.is_finite() && self.e12.is_finite()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::line::Line;
    use crate::Inner;

    #[test]
    fn point_line_inner_simple_example() {
        let p = Point {
            e01: 2f32,
            e20: 3f32,
            e12: 4f32,
        };
        let l = Line {
            e0: 5f32,
            e1: 6f32,
            e2: 7f32,
        };
        let expected = Line {
            e0: 2f32 * 6f32 + -3f32 * 7f32,
            e1: 4f32 * 7f32,
            e2: -4f32 * 6f32,
        };
        let actual = p.inner(l);
        assert_eq!(actual.e0, expected.e0);
        assert_eq!(actual.e1, expected.e1);
        assert_eq!(actual.e2, expected.e2);
    }
}