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
use std::ops::{Add, AddAssign, Sub, SubAssign};
use serde::{Deserialize, Serialize};
pub type Vector = nalgebra::Vector3<f64>;
pub type Point = nalgebra::Point3<f64>;
pub type Matrix = nalgebra::Matrix4<f64>;
pub type LinearMatrix = nalgebra::Matrix3<f64>;
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Position(pub Point);
impl Position {
pub fn new(x: f64, y: f64, z: f64) -> Position { Position(Point::new(x, y, z)) }
pub fn x(self) -> f64 { self.0.x }
pub fn y(self) -> f64 { self.0.y }
pub fn z(self) -> f64 { self.0.z }
pub fn vector(&self) -> Vector { Vector::new(self.x(), self.y(), self.z()) }
pub fn value(&self) -> Point { self.0 }
}
impl Sub<Position> for Position {
type Output = Vector;
fn sub(self, other: Self) -> Self::Output {
Vector::new(self.x() - other.x(), self.y() - other.y(), self.z() - other.z())
}
}
impl Add<Vector> for Position {
type Output = Position;
fn add(self, other: Vector) -> Self::Output {
Position::new(self.x() + other.x, self.y() + other.y, self.z() + other.z)
}
}
impl AddAssign<Vector> for Position {
fn add_assign(&mut self, other: Vector) { *self = *self + other; }
}
impl Sub<Vector> for Position {
type Output = Position;
fn sub(self, other: Vector) -> Self::Output {
Position::new(self.x() - other.x, self.y() - other.y, self.z() - other.z)
}
}
impl SubAssign<Vector> for Position {
fn sub_assign(&mut self, other: Vector) { *self = *self - other; }
}
pub fn transform_cuboid(lower: Vector, upper: Vector) -> Matrix {
let origin = (lower + upper) / 2.;
Matrix::new_nonuniform_scaling(&(upper - origin)).append_translation(&origin)
}
pub fn transform_cylinder(x: f64, y: f64, zn: f64, zp: f64) -> Matrix {
Matrix::new_nonuniform_scaling(&Vector::new(x, y, zn + zp))
.append_translation(&Vector::new(0., 0., -zn))
}