rust_3d/
polygon_3d.rs

1/*
2Copyright 2018 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//! Polygon3D, a polygon within 3D space
24
25use std::fmt;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Hash)]
32/// Polygon3D, a polygon within 3D space
33pub struct Polygon3D<P>
34where
35    P: Is3D,
36{
37    pc: PointCloud3D<P>,
38}
39
40impl<P> IsPolygon<P> for Polygon3D<P>
41where
42    P: Is3D + Clone,
43{
44    fn num_segments(&self) -> usize {
45        self.pc.len()
46    }
47
48    fn segment_vertex_ids(&self, segmentid: SId) -> Result<(VId, VId)> {
49        if segmentid.val >= self.pc.len() {
50            Err(ErrorKind::IncorrectSegmentID)
51        } else if segmentid.val == self.pc.len() - 1 {
52            Ok((VId { val: segmentid.val }, VId { val: 0 }))
53        } else {
54            Ok((
55                VId { val: segmentid.val },
56                VId {
57                    val: segmentid.val + 1,
58                },
59            ))
60        }
61    }
62
63    fn segment_vertices(&self, segmentid: SId) -> Result<(P, P)> {
64        let (vid1, vid2) = self.segment_vertex_ids(segmentid)?;
65        Ok((self.pc[vid1.val].clone(), self.pc[vid2.val].clone()))
66    }
67
68    fn vertex(&self, vertexid: VId) -> Result<P> {
69        if vertexid.val >= self.pc.len() {
70            Err(ErrorKind::IncorrectVertexID)
71        } else {
72            Ok(self.pc[vertexid.val].clone())
73        }
74    }
75}
76
77impl<P> IsEditablePolygon<P> for Polygon3D<P>
78where
79    P: Is3D + Clone,
80{
81    fn add_vertex(&mut self, vertex: P) -> VId {
82        self.pc.data.push(vertex);
83        VId {
84            val: self.pc.len() - 1,
85        }
86    }
87
88    fn change_vertex(&mut self, vertexid: VId, vertex: P) -> Result<()> {
89        if vertexid.val >= self.pc.len() {
90            return Err(ErrorKind::IncorrectVertexID);
91        }
92
93        self.pc[vertexid.val] = vertex;
94        Ok(())
95    }
96}
97
98impl<P> IsMovable3D for Polygon3D<P>
99where
100    P: Is3D + IsMovable3D,
101{
102    fn move_by(&mut self, x: f64, y: f64, z: f64) {
103        self.pc.move_by(x, y, z)
104    }
105}
106
107impl<P> HasBoundingBox3DMaybe for Polygon3D<P>
108where
109    P: Is3D,
110{
111    fn bounding_box_maybe(&self) -> Result<BoundingBox3D> {
112        self.pc.bounding_box_maybe()
113    }
114}
115
116impl<P> HasCenterOfGravity3D for Polygon3D<P>
117where
118    P: Is3D,
119{
120    fn center_of_gravity(&self) -> Result<Point3D> {
121        self.pc.center_of_gravity()
122    }
123}
124
125impl<P> HasLength for Polygon3D<P>
126where
127    P: Is3D,
128{
129    fn length(&self) -> f64 {
130        let mut length = self.pc.length();
131
132        if self.pc.data.len() > 0 {
133            length += dist_3d(&self.pc.data[self.pc.data.len() - 1], &self.pc.data[0]);
134        }
135
136        length
137    }
138}
139
140impl<P> IsScalable for Polygon3D<P>
141where
142    P: IsEditable3D,
143{
144    fn scale(&mut self, factor: Positive) {
145        self.pc.scale(factor)
146    }
147}
148
149impl<P> IsMatrix4Transformable for Polygon3D<P>
150where
151    P: Is3D + IsMatrix4Transformable + Clone,
152{
153    fn transformed(&self, m: &Matrix4) -> Self {
154        let mut new = self.clone();
155        new.transform(m);
156        new
157    }
158
159    fn transform(&mut self, m: &Matrix4) {
160        self.pc.transform(m);
161    }
162}
163
164impl<P> Default for Polygon3D<P>
165where
166    //https://github.com/rust-lang/rust/issues/26925
167    P: Is3D,
168{
169    fn default() -> Self {
170        let pc = PointCloud3D::default();
171        Polygon3D { pc }
172    }
173}
174
175impl<P> fmt::Display for Polygon3D<P>
176where
177    P: Is3D + fmt::Display,
178{
179    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180        self.pc.fmt(f)
181    }
182}
183
184impl<P> From<PointCloud3D<P>> for Polygon3D<P>
185where
186    P: Is3D,
187{
188    fn from(pc: PointCloud3D<P>) -> Self {
189        Polygon3D { pc }
190    }
191}