rust_3d/
polygon_2d.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//! Polygon2D, a polygon within 2D space
24
25use std::fmt;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone, Hash)]
32/// Polygon2D, a polygon within 2D space
33pub struct Polygon2D<P>
34where
35    P: Is2D,
36{
37    pc: PointCloud2D<P>,
38}
39
40impl<P> IsPolygon<P> for Polygon2D<P>
41where
42    P: Is2D + 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 Polygon2D<P>
78where
79    P: Is2D + 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> IsMovable2D for Polygon2D<P>
99where
100    P: Is2D + IsMovable2D,
101{
102    fn move_by(&mut self, x: f64, y: f64) {
103        self.pc.move_by(x, y)
104    }
105}
106
107impl<P> HasBoundingBox2DMaybe for Polygon2D<P>
108where
109    P: Is2D,
110{
111    fn bounding_box_maybe(&self) -> Result<BoundingBox2D> {
112        self.pc.bounding_box_maybe()
113    }
114}
115
116impl<P> HasCenterOfGravity2D for Polygon2D<P>
117where
118    P: Is2D,
119{
120    fn center_of_gravity(&self) -> Result<Point2D> {
121        self.pc.center_of_gravity()
122    }
123}
124
125impl<P> HasLength for Polygon2D<P>
126where
127    P: Is2D,
128{
129    fn length(&self) -> f64 {
130        let mut length = self.pc.length();
131
132        if self.pc.data.len() > 0 {
133            length += dist_2d(&self.pc.data[self.pc.data.len() - 1], &self.pc.data[0]);
134        }
135
136        length
137    }
138}
139
140impl<P> IsScalable for Polygon2D<P>
141where
142    P: IsEditable2D,
143{
144    fn scale(&mut self, factor: Positive) {
145        self.pc.scale(factor)
146    }
147}
148
149impl<P> IsMatrix3Transformable for Polygon2D<P>
150where
151    P: Is2D + IsMatrix3Transformable + Clone,
152{
153    fn transformed(&self, m: &Matrix3) -> Self {
154        let mut new = self.clone();
155        new.transform(m);
156        new
157    }
158
159    fn transform(&mut self, m: &Matrix3) {
160        self.pc.transform(m);
161    }
162}
163
164impl<P> fmt::Display for Polygon2D<P>
165where
166    P: Is2D + fmt::Display,
167{
168    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169        self.pc.fmt(f)
170    }
171}
172
173impl<P> Default for Polygon2D<P>
174where
175    //https://github.com/rust-lang/rust/issues/26925
176    P: Is2D,
177{
178    fn default() -> Self {
179        let pc = PointCloud2D::default();
180        Polygon2D { pc }
181    }
182}
183
184impl<P> From<PointCloud2D<P>> for Polygon2D<P>
185where
186    P: Is2D,
187{
188    fn from(pc: PointCloud2D<P>) -> Self {
189        Polygon2D { pc }
190    }
191}