// // Basic geometric things... // // Georg Hopp // // Copyright © 2019 Georg Hopp // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // use std::fmt::Debug; use std::ops::{Add, Div, Mul, Neg, Sub}; use crate::math::transform::{TMatrix, Transformable}; use crate::math::trigonometry::Trig; use crate::math::vector::Vector; #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct Point(pub Vector, T) where T: Add + Sub + Neg + Mul + Div + PartialEq + Copy + Trig; impl Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy + From { pub fn new(x :T, y :T, z :T) -> Self { Self(Vector(x, y, z), 1.into()) } } impl Add for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy { type Output = Self; fn add(self, other :Self) -> Self { let Point(v1, w1) = self; let Point(v2, w2) = other; Self(v1 + v2, w1 + w2) } } impl Neg for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy { type Output = Self; fn neg(self) -> Self { let Point(v, w) = self; Self(-v, -w) } } impl Sub for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy { type Output = Self; fn sub(self, other :Self) -> Self { self + -other } } impl Mul for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy + From { type Output = Self; fn mul(self, other :Self) -> Self { let a :Vector = self.into(); let b :Vector = other.into(); Point(a * b, 1.into()) } } impl From> for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy + From { fn from(v :Vector) -> Self { Point(v, 1.into()) } } impl Into> for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Trig + Copy + From { fn into(self) -> Vector { let Point(v, w) = self; if w == 0.into() { v } else { v.mul(&w.recip()) } } } impl Transformable for Point where T: Add + Sub + Neg + Mul + Div + PartialEq + Debug + Trig + Copy + From { fn transform(&self, m :&TMatrix) -> Self { let Point(v, w) = *self; let (v, w) = m.apply(&v, w); if w == 0.into() { v.into() } else { v.mul(&w.recip()).into() } } }