1use crate::*;
26
27#[derive(Debug, PartialEq, PartialOrd, Default, Clone, Hash, Eq, Ord)]
30pub struct FilterBox2D {
32 box_2d: Box2D,
33}
34
35impl FilterBox2D {
36 pub fn new(box_2d: Box2D) -> Self {
38 FilterBox2D { box_2d }
39 }
40}
41
42impl 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}