microcad_core/geo3d/
triangle.rs1use cgmath::InnerSpace;
7
8use crate::*;
9
10impl Triangle<Vertex> {
11 pub fn normal(&self) -> Vec3 {
13 (self.2.pos - self.0.pos).cross(self.1.pos - self.0.pos)
14 }
15}
16
17impl Triangle<&Vertex> {
18 pub fn normal(&self) -> Vec3 {
20 (self.2.pos - self.0.pos).cross(self.1.pos - self.0.pos)
21 }
22
23 pub fn area(&self) -> Scalar {
25 self.normal().magnitude()
26 }
27
28 pub fn signed_volume(&self) -> f64 {
32 let v210 = self.2.pos.x * self.1.pos.y * self.0.pos.z;
33 let v120 = self.1.pos.x * self.2.pos.y * self.0.pos.z;
34 let v201 = self.2.pos.x * self.0.pos.y * self.1.pos.z;
35 let v021 = self.0.pos.x * self.2.pos.y * self.1.pos.z;
36 let v102 = self.1.pos.x * self.0.pos.y * self.2.pos.z;
37 let v012 = self.0.pos.x * self.1.pos.y * self.2.pos.z;
38
39 (1.0 / 6.0) * (-v210 + v120 + v201 - v021 - v102 + v012)
40 }
41}