geometry.rs
6.25 KB
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
//
// 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::convert::From;
use std::ops::{Add,Sub,Neg,Mul,Div};
use std::fmt::Debug;
use crate::easel::{Canvas,Coordinate,Coordinates,Polygon};
use crate::transform::TMatrix;
use crate::trigonometry::Trig;
use crate::vector::Vector;
#[derive(Debug)]
pub struct Polyeder<T>
where T: Add + Sub + Neg + Mul + Div + Debug + Copy + Trig {
points :Vec<Vector<T>>,
faces :Vec<Vec<usize>>,
normals :Vec<Vector<T>>,
}
pub trait Primitives<T>
where T: Add + Sub + Neg + Mul + Div + Debug + Copy + Trig + From<i32> {
fn transform(&self, m :&TMatrix<T>) -> Self;
fn project(&self, camera :&Camera<T>) -> Vec<Polygon>;
}
pub struct Camera<T>
where T: Add + Sub + Neg + Mul + Div + Debug + Copy + Trig + From<i32> {
width :T,
height :T,
project :TMatrix<T>,
}
impl<T> Camera<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Copy + Trig + From<i32> {
// This code assumes that the size of the viewport is always
// equal to the size of the physical screen… e.g. window/canvas thus some
// effects can't be done. See book for examples with different viewport
// and screen sizes.
pub fn new(c :&dyn Canvas, angle :i32) -> Self {
let width :T = (c.width() as i32).into();
let height :T = (c.height() as i32).into();
let d :T = 1.into();
let fov = T::cot(angle) * width;
let wh = width / 2.into();
let hh = height / 2.into();
Camera { width: width
, height: height
, project: TMatrix::new(
( fov, 0.into(), wh, 0.into())
, (0.into(), fov, hh, 0.into())
, (0.into(), 0.into(), d, 1.into())
, (0.into(), 0.into(), 1.into(), 0.into()) ) }
}
pub fn get_projection(&self) -> TMatrix<T> {
self.project
}
pub fn project(&self, v :Vector<T>) -> Coordinate {
let p = self.project.apply(&v);
Coordinate(T::round(&p.x()), T::round(&p.y()))
}
}
impl<T> Polyeder<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Copy + Trig + From<i32> {
// https://rechneronline.de/pi/tetrahedron.php
pub fn tetrahedron(a :T) -> Polyeder<T> {
let f0 :T = 0.into();
let f3 :T = 3.into();
let f4 :T = 4.into();
let f6 :T = 6.into();
let f12 :T = 12.into();
let yi :T = a / f12 * T::sqrt(f6).unwrap();
let yc :T = a / f4 * T::sqrt(f6).unwrap();
let zi :T = T::sqrt(f3).unwrap() / f6 * a;
let zc :T = T::sqrt(f3).unwrap() / f3 * a;
let ah :T = a / 2.into();
// half the height in y
let _yh :T = a / f6 * T::sqrt(f6).unwrap();
// half the deeps in z
let _zh :T = T::sqrt(f3).unwrap() / f4 * a;
Polyeder{ points: vec!( Vector( f0, yc, f0)
, Vector(-ah, -yi, -zi)
, Vector( ah, -yi, -zi)
, Vector( f0, -yi, zc) )
, faces: vec!( vec!(1, 2, 3)
, vec!(1, 0, 2)
, vec!(3, 0, 1)
, vec!(2, 0, 3) )}
}
pub fn cube(a :T) -> Polyeder<T> {
let ah :T = a / From::<i32>::from(2);
Polyeder{ points: vec!( Vector(-ah, ah, -ah) // 0 => front 1
, Vector(-ah, -ah, -ah) // 1 => front 2
, Vector( ah, -ah, -ah) // 2 => front 3
, Vector( ah, ah, -ah) // 3 => front 4
, Vector(-ah, ah, ah) // 4 => back 1
, Vector(-ah, -ah, ah) // 5 => back 2
, Vector( ah, -ah, ah) // 6 => back 3
, Vector( ah, ah, ah) ) // 7 => back 4
, faces: vec!( vec!(0, 1, 2, 3) // front
, vec!(7, 6, 5, 4) // back
, vec!(1, 5, 6, 2) // top
, vec!(0, 3, 7, 4) // bottom
, vec!(0, 4, 5, 1) // left
, vec!(2, 6, 7, 3) )} // right
}
}
impl<T> Primitives<T> for Polyeder<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Copy + Trig + From<i32> + From<i32> {
fn transform(&self, m :&TMatrix<T>) -> Self {
Polyeder{ points: self.points.iter().map(|p| m.apply(p)).collect()
, faces: self.faces.to_vec() }
}
// TODO for now we assume already prejected vertices (points)
// in future we need to distinguish more clear between vertex and point
// and projected_point.
fn project(&self, _camera :&Camera<T>) -> Vec<Polygon> {
fn polygon<I>(c :I) -> Polygon
where I: Iterator<Item = Coordinate> {
Polygon(Coordinates(c.collect()))
}
// this one does the projection... as the projection was the last
// matrix we do not need to do it here.
// let to_coord = |p :&usize| _camera.project(self.points[*p]);
let to_coord = |p :&usize| {
let v = self.points[*p];
Coordinate(v.x().round(), v.y().round()) };
let to_poly = |f :&Vec<usize>| polygon(f.iter().map(to_coord));
self.faces.iter().map(to_poly).collect()
}
}