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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use std::ops::{Add, Sub};
pub mod lib32;
#[derive(Debug, Copy, Clone)]
pub struct Point {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Point {
pub fn cross(&self, pt: &Point) -> Point {
Point {
x: self.y * pt.z - pt.y * self.z,
y: self.z * pt.x - pt.z * self.x,
z: self.x * pt.y - pt.x * self.y,
}
}
pub fn dot(&self, pt: &Point) -> f64 {
self.x * pt.x + self.y * pt.y + self.z * pt.z
}
pub fn distance_to(&self, pt: &Point) -> f64 {
((self.x - pt.x).powi(2) + (self.y - pt.y).powi(2) + (self.z - pt.z).powi(2)).sqrt()
}
pub fn normalized(&self) -> Point {
let mut n = self.distance_to(&Point {
x: 0.0,
y: 0.0,
z: 0.0,
});
if n == 0.0 {
n = 1.0;
}
Point {
x: self.x / n,
y: self.y / n,
z: self.z / n,
}
}
}
impl Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
impl Sub for Point {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct Triangle {
pub a: Point,
pub b: Point,
pub c: Point,
}
impl Triangle {
pub fn new(a: Point, b: Point, c: Point) -> Triangle {
Triangle { a, b, c }
}
pub fn from_array(points: [Point; 3]) -> Triangle {
Triangle {
a: points[0],
b: points[1],
c: points[2],
}
}
pub fn aabb(&self) -> [Point; 2] {
let mut c_x = [self.a.x, self.b.x, self.c.x];
let mut c_y = [self.a.y, self.b.y, self.c.y];
let mut c_z = [self.a.z, self.b.z, self.c.z];
c_x.sort_by(|i, j| i.partial_cmp(j).unwrap());
c_y.sort_by(|i, j| i.partial_cmp(j).unwrap());
c_z.sort_by(|i, j| i.partial_cmp(j).unwrap());
[
Point {
x: c_x[0],
y: c_y[0],
z: c_z[0],
},
Point {
x: c_x[2],
y: c_y[2],
z: c_z[2],
},
]
}
pub fn angles(&self) -> Option<[f64; 3]> {
if self.is_collinear() {
return None;
}
let [la, lb, lc] = self.sides();
let alpha = ((lb.powi(2) + lc.powi(2) - la.powi(2)) / (2.0 * lb * lc)).acos();
let beta = ((la.powi(2) + lc.powi(2) - lb.powi(2)) / (2.0 * la * lc)).acos();
let gamma = std::f64::consts::PI - alpha - beta;
Some([alpha, beta, gamma])
}
pub fn area(&self) -> f64 {
let s = self.semiperimeter();
let [la, lb, lc] = self.sides();
(s * (s - la) * (s - lb) * (s - lc)).sqrt()
}
pub fn barycentric_to_cartesian(&self, pt: &Point) -> Point {
let x = pt.x * self.a.x + pt.y * self.b.x + pt.z * self.c.x;
let y = pt.x * self.a.y + pt.y * self.b.y + pt.z * self.c.y;
let z = pt.x * self.a.z + pt.y * self.b.z + pt.z * self.c.z;
Point { x, y, z }
}
pub fn cartesian_to_barycentric(&self, pt: &Point) -> Point {
let v0 = Point {
x: self.b.x - self.a.x,
y: self.b.y - self.a.y,
z: self.b.z - self.a.z,
};
let v1 = Point {
x: self.c.x - self.a.x,
y: self.c.y - self.a.y,
z: self.c.z - self.a.z,
};
let v2 = Point {
x: pt.x - self.a.x,
y: pt.y - self.a.y,
z: pt.z - self.a.z,
};
let den = 1.0 / (v0.x * v1.y - v1.x * v0.y);
let v = (v2.x * v1.y - v1.x * v2.y) * den;
let w = (v0.x * v2.y - v2.x * v0.y) * den;
let u = 1.0 - v - w;
Point { x: u, y: v, z: w }
}
pub fn centroid(&self) -> Point {
Point {
x: (self.a.x + self.b.x + self.c.x) / 3.0,
y: (self.a.y + self.b.y + self.c.y) / 3.0,
z: (self.a.z + self.b.z + self.c.z) / 3.0,
}
}
pub fn circumradius(&self) -> Option<f64> {
if self.is_collinear() {
return None;
}
Some(self.sides().iter().product::<f64>() / (4.0 * self.area()))
}
pub fn has_point(&self, pt: Point) -> bool {
fn sign(a: &Point, b: &Point, c: &Point) -> f64 {
((a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y)) as f64
}
let d1 = sign(&pt, &self.a, &self.b);
let d2 = sign(&pt, &self.b, &self.c);
let d3 = sign(&pt, &self.c, &self.a);
let has_neg = (d1 < 0.0) || (d2 < 0.0) || (d3 < 0.0);
let has_pos = (d1 > 0.0) || (d2 > 0.0) || (d3 > 0.0);
!(has_neg && has_pos)
}
pub fn heights(&self) -> Option<[f64; 3]> {
if self.is_collinear() {
return None;
}
let double_area = 2.0 * self.area();
let [la, lb, lc] = self.sides();
Some([double_area / la, double_area / lb, double_area / lc])
}
pub fn inradius(&self) -> Option<f64> {
if self.is_collinear() {
return None;
}
Some(self.area() / self.semiperimeter())
}
pub fn is_collinear(&self) -> bool {
self.area().eq(&0.0)
}
pub fn is_equilateral(&self) -> bool {
let sides = self.sides();
sides[0].eq(&sides[1]) && sides[1].eq(&sides[2])
}
pub fn is_golden(&self) -> bool {
if !self.is_isosceles() {
return false;
}
let mut sides = self.sides();
sides.sort_by(|a, b| a.partial_cmp(b).unwrap());
let min = sides[0];
let max = sides[2];
(max / min).eq(&((1.0 + 5.0_f64.sqrt()) / 2.0))
}
pub fn is_isosceles(&self) -> bool {
let sides = self.sides();
sides[0].eq(&sides[1]) || sides[1].eq(&sides[2]) || sides[2].eq(&sides[0])
}
pub fn is_right(&self) -> bool {
if self.is_collinear() {
return false;
}
let angles = self.angles().unwrap();
let half_pi = std::f64::consts::PI / 2.0;
angles[0].eq(&half_pi) || angles[1].eq(&half_pi) || angles[2].eq(&half_pi)
}
pub fn medians(&self) -> [f64; 3] {
let [la, lb, lc] = self.sides();
let ma = (2.0 * lb.powi(2) + 2.0 * lc.powi(2) - la.powi(2)).sqrt() / 2.0;
let mb = (2.0 * lc.powi(2) + 2.0 * la.powi(2) - lb.powi(2)).sqrt() / 2.0;
let mc = (2.0 * la.powi(2) + 2.0 * lb.powi(2) - lc.powi(2)).sqrt() / 2.0;
[ma, mb, mc]
}
pub fn normal(&self) -> Option<Point> {
if self.is_collinear() {
return None;
}
let u = self.b - self.a;
let v = self.c - self.a;
let n = Point {
x: u.y * v.z - u.z * v.y,
y: u.z * v.x - u.x * v.z,
z: u.x * v.y - u.y * v.x,
};
Some(n.normalized())
}
pub fn perimeter(&self) -> f64 {
return self.sides().iter().sum();
}
pub fn ray_intersection(&self, ray_orig: &Point, ray_dir: &Point) -> Option<f64> {
if self.is_collinear() {
return None;
}
let e1 = self.b - self.a;
let e2 = self.c - self.a;
let pvec = ray_dir.cross(&e2);
let det = e1.dot(&pvec);
if det.abs() < f64::MIN {
return None;
}
let inv_det = 1.0 / det;
let tvec = *ray_orig - self.a;
let u = tvec.dot(&pvec) * inv_det;
if u < 0.0 || u > 1.0 {
return None;
}
let qvec = tvec.cross(&e1);
let v = ray_dir.dot(&qvec) * inv_det;
if v < 0.0 || (u + v) > 1.0 {
return None;
}
Some(e2.dot(&qvec) * inv_det)
}
pub fn semiperimeter(&self) -> f64 {
self.perimeter() / 2.0
}
pub fn sides(&self) -> [f64; 3] {
[
self.b.distance_to(&self.c),
self.c.distance_to(&self.a),
self.a.distance_to(&self.b),
]
}
pub fn is_sorted_by(self, axis_name: char) -> bool {
match axis_name {
'x' | 'X' | '0' => self.a.x <= self.b.x && self.b.x <= self.c.x,
'z' | 'Z' | '2' => self.a.z <= self.b.z && self.b.z <= self.c.z,
_ => self.a.y <= self.b.y && self.b.y <= self.c.y,
}
}
pub fn sorted_by(self, axis_name: char) -> Triangle {
let mut sorted = [self.a, self.b, self.c];
match axis_name {
'x' | 'X' | '0' => sorted.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap()),
'z' | 'Z' | '2' => sorted.sort_by(|a, b| a.z.partial_cmp(&b.z).unwrap()),
_ => sorted.sort_by(|a, b| a.y.partial_cmp(&b.y).unwrap()),
};
Triangle::from_array(sorted)
}
}