rust_3d/
filter_circle.rs

1/*
2Copyright 2016 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//! FilterCircle, a circle filter within 2D space
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29#[derive(Debug, PartialEq, PartialOrd, Default, Clone, Eq, Ord, Hash)]
30/// FilterCircle, a circle filter within 2D space
31pub struct FilterCircle {
32    circle: Circle,
33}
34impl FilterCircle {
35    /// Creates a new FilterCircle with the given parameters
36    pub fn new(circle: Circle) -> Self {
37        FilterCircle { circle }
38    }
39}
40
41//------------------------------------------------------------------------------
42
43impl IsND for FilterCircle {
44    fn n_dimensions() -> usize {
45        Circle::n_dimensions()
46    }
47
48    fn position_nd(&self, dimension: usize) -> Result<f64> {
49        self.circle.position_nd(dimension)
50    }
51}
52
53impl Is2D for FilterCircle {
54    #[inline(always)]
55    fn x(&self) -> f64 {
56        self.circle.x()
57    }
58
59    #[inline(always)]
60    fn y(&self) -> f64 {
61        self.circle.y()
62    }
63}
64
65impl IsBuildableND for FilterCircle {
66    #[inline(always)]
67    fn new_nd(coords: &[f64]) -> Result<Self> {
68        Ok(FilterCircle::new(Circle::new_nd(coords)?))
69    }
70
71    #[inline(always)]
72    fn from_nd<P>(&mut self, other: P) -> Result<()>
73    where
74        P: IsBuildableND,
75    {
76        self.circle.from_nd(other)
77    }
78}
79
80impl IsBuildable2D for FilterCircle {
81    #[inline(always)]
82    fn new(x: f64, y: f64) -> Self {
83        FilterCircle::new(Circle::new(x, y))
84    }
85
86    #[inline(always)]
87    fn from<P>(&mut self, other: &P)
88    where
89        P: Is2D,
90    {
91        self.circle.from(other)
92    }
93}
94
95impl IsEditableND for FilterCircle {
96    fn set_position(&mut self, dimension: usize, val: f64) -> Result<()> {
97        self.circle.set_position(dimension, val)
98    }
99}
100
101impl IsEditable2D for FilterCircle {
102    #[inline(always)]
103    fn set_x(&mut self, val: f64) {
104        self.circle.set_x(val);
105    }
106
107    #[inline(always)]
108    fn set_y(&mut self, val: f64) {
109        self.circle.set_y(val);
110    }
111}
112
113impl HasBoundingBox2D for FilterCircle {
114    fn bounding_box(&self) -> BoundingBox2D {
115        self.circle.bounding_box()
116    }
117}
118
119impl HasBoundingBox2DMaybe for FilterCircle {
120    fn bounding_box_maybe(&self) -> Result<BoundingBox2D> {
121        self.circle.bounding_box_maybe()
122    }
123}
124
125impl<T> IsFilter<T> for FilterCircle
126where
127    T: Is2D,
128{
129    fn is_allowed(&self, p: &T) -> bool {
130        dist_2d(p, &self.circle.center) <= self.circle.radius.get()
131    }
132}
133
134impl IsScalable for FilterCircle {
135    fn scale(&mut self, factor: Positive) {
136        self.circle.scale(factor);
137    }
138}