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