You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.6 KiB
130 lines
3.6 KiB
//
|
|
// Basic geometric things...
|
|
//
|
|
// Georg Hopp <georg@steffers.org>
|
|
//
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
//
|
|
|
|
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<T>(pub Vector<T>, T)
|
|
where T: Add + Sub + Neg + Mul + Div + PartialEq + Copy + Trig;
|
|
|
|
impl<T> Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Trig + Copy + From<i32> {
|
|
pub fn new(x :T, y :T, z :T) -> Self {
|
|
Self(Vector(x, y, z), 1.into())
|
|
}
|
|
}
|
|
|
|
impl<T> Add for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ 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<T> Neg for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Trig + Copy {
|
|
type Output = Self;
|
|
|
|
fn neg(self) -> Self {
|
|
let Point(v, w) = self;
|
|
Self(-v, -w)
|
|
}
|
|
}
|
|
|
|
impl<T> Sub for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Trig + Copy {
|
|
type Output = Self;
|
|
|
|
fn sub(self, other :Self) -> Self {
|
|
self + -other
|
|
}
|
|
}
|
|
|
|
impl<T> Mul for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Trig + Copy + From<i32> {
|
|
type Output = Self;
|
|
|
|
fn mul(self, other :Self) -> Self {
|
|
let a :Vector<T> = self.into();
|
|
let b :Vector<T> = other.into();
|
|
|
|
Point(a * b, 1.into())
|
|
}
|
|
}
|
|
|
|
impl<T> From<Vector<T>> for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Trig + Copy + From<i32> {
|
|
fn from(v :Vector<T>) -> Self {
|
|
Point(v, 1.into())
|
|
}
|
|
}
|
|
|
|
impl<T> Into<Vector<T>> for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Trig + Copy + From<i32> {
|
|
fn into(self) -> Vector<T> {
|
|
let Point(v, w) = self;
|
|
|
|
if w == 0.into() {
|
|
v
|
|
} else {
|
|
v.mul(&w.recip())
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Transformable<T> for Point<T>
|
|
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
|
|
+ Mul<Output = T> + Div<Output = T>
|
|
+ PartialEq + Debug + Trig + Copy + From<i32> {
|
|
fn transform(&self, m :&TMatrix<T>) -> 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()
|
|
}
|
|
}
|
|
}
|