rust_3d/
filter_box_3d.rs

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