rust_3d/tri_face_3d.rs
1/*
2Copyright 2019 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Face with 3 corners in 3D space
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// Face with 3 corners in 3D space
30pub struct TriFace3D {
31 a: Point3D,
32 b: Point3D,
33 c: Point3D,
34}
35
36//------------------------------------------------------------------------------
37
38impl TriFace3D {
39 pub fn new(a: Point3D, b: Point3D, c: Point3D) -> Result<Self> {
40 match BoundingBox3D::from_iterator([&a, &b, &c].iter().map(|x| *x)) {
41 Err(_) => Err(ErrorKind::TriFace3DNotSpanningVolume),
42 Ok(_) => Ok(Self { a, b, c }),
43 }
44 }
45
46 pub fn a(&self) -> &Point3D {
47 &self.a
48 }
49
50 pub fn b(&self) -> &Point3D {
51 &self.b
52 }
53
54 pub fn c(&self) -> &Point3D {
55 &self.c
56 }
57}
58
59//------------------------------------------------------------------------------
60
61impl IsSATObject for TriFace3D {
62 fn for_each_point<F>(&self, f: &mut F)
63 where
64 F: FnMut(&Point3D),
65 {
66 f(&self.a);
67 f(&self.b);
68 f(&self.c);
69 }
70
71 fn for_each_axis<F>(&self, f: &mut F)
72 where
73 F: FnMut(&Norm3D),
74 {
75 let vab = conn(&self.a, &self.b);
76 let vbc = conn(&self.b, &self.c);
77 let vca = conn(&self.c, &self.a);
78
79 let n = Norm3D::new(cross(&vab, &vbc)).unwrap_or(Norm3D::norm_z());
80 let e1 = Norm3D::new(cross(&n, &vab)).unwrap_or(Norm3D::norm_z());
81 let e2 = Norm3D::new(cross(&n, &vbc)).unwrap_or(Norm3D::norm_z());
82 let e3 = Norm3D::new(cross(&n, &vca)).unwrap_or(Norm3D::norm_z());
83
84 f(&n);
85 f(&e1);
86 f(&e2);
87 f(&e3);
88 }
89}
90
91//------------------------------------------------------------------------------
92
93impl HasBoundingBox3D for TriFace3D {
94 fn bounding_box(&self) -> BoundingBox3D {
95 BoundingBox3D::from_iterator([&self.a, &self.b, &self.c].iter().map(|x| *x)).unwrap()
96 // safe since ensured in constructor
97 }
98}
99
100//------------------------------------------------------------------------------
101
102impl HasBoundingBox3DMaybe for TriFace3D {
103 fn bounding_box_maybe(&self) -> Result<BoundingBox3D> {
104 Ok(self.bounding_box())
105 }
106}