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