1use core::fmt;
4use core::ops::*;
5
6#[inline(always)]
8#[must_use]
9pub const fn bvec3(x: bool, y: bool, z: bool) -> BVec3 {
10 BVec3::new(x, y, z)
11}
12
13#[derive(Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(C, align(1))]
16#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
17pub struct BVec3 {
18 pub x: bool,
19 pub y: bool,
20 pub z: bool,
21}
22
23const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
24
25impl BVec3 {
26 pub const FALSE: Self = Self::splat(false);
28
29 pub const TRUE: Self = Self::splat(true);
31
32 #[inline(always)]
34 #[must_use]
35 pub const fn new(x: bool, y: bool, z: bool) -> Self {
36 Self { x, y, z }
37 }
38
39 #[inline]
41 #[must_use]
42 pub const fn splat(v: bool) -> Self {
43 Self::new(v, v, v)
44 }
45
46 #[inline]
48 #[must_use]
49 pub const fn from_array(a: [bool; 3]) -> Self {
50 Self::new(a[0], a[1], a[2])
51 }
52
53 #[inline]
58 #[must_use]
59 pub fn bitmask(self) -> u32 {
60 (self.x as u32) | ((self.y as u32) << 1) | ((self.z as u32) << 2)
61 }
62
63 #[inline]
65 #[must_use]
66 pub fn any(self) -> bool {
67 self.x || self.y || self.z
68 }
69
70 #[inline]
72 #[must_use]
73 pub fn all(self) -> bool {
74 self.x && self.y && self.z
75 }
76
77 #[inline]
81 #[must_use]
82 pub fn test(&self, index: usize) -> bool {
83 match index {
84 0 => self.x,
85 1 => self.y,
86 2 => self.z,
87 _ => panic!("index out of bounds"),
88 }
89 }
90
91 #[inline]
95 pub fn set(&mut self, index: usize, value: bool) {
96 match index {
97 0 => self.x = value,
98 1 => self.y = value,
99 2 => self.z = value,
100 _ => panic!("index out of bounds"),
101 }
102 }
103
104 #[inline]
105 #[must_use]
106 fn into_bool_array(self) -> [bool; 3] {
107 [self.x, self.y, self.z]
108 }
109
110 #[inline]
111 #[must_use]
112 fn into_u32_array(self) -> [u32; 3] {
113 [
114 MASK[self.x as usize],
115 MASK[self.y as usize],
116 MASK[self.z as usize],
117 ]
118 }
119}
120
121impl Default for BVec3 {
122 #[inline]
123 fn default() -> Self {
124 Self::FALSE
125 }
126}
127
128impl BitAnd for BVec3 {
129 type Output = Self;
130 #[inline]
131 fn bitand(self, rhs: Self) -> Self {
132 Self {
133 x: self.x & rhs.x,
134 y: self.y & rhs.y,
135 z: self.z & rhs.z,
136 }
137 }
138}
139
140impl BitAnd<&Self> for BVec3 {
141 type Output = Self;
142 #[inline]
143 fn bitand(self, rhs: &Self) -> Self {
144 self.bitand(*rhs)
145 }
146}
147
148impl BitAnd<&BVec3> for &BVec3 {
149 type Output = BVec3;
150 #[inline]
151 fn bitand(self, rhs: &BVec3) -> BVec3 {
152 (*self).bitand(*rhs)
153 }
154}
155
156impl BitAnd<BVec3> for &BVec3 {
157 type Output = BVec3;
158 #[inline]
159 fn bitand(self, rhs: BVec3) -> BVec3 {
160 (*self).bitand(rhs)
161 }
162}
163
164impl BitAndAssign for BVec3 {
165 #[inline]
166 fn bitand_assign(&mut self, rhs: Self) {
167 *self = self.bitand(rhs);
168 }
169}
170
171impl BitAndAssign<&Self> for BVec3 {
172 #[inline]
173 fn bitand_assign(&mut self, rhs: &Self) {
174 self.bitand_assign(*rhs);
175 }
176}
177
178impl BitOr for BVec3 {
179 type Output = Self;
180 #[inline]
181 fn bitor(self, rhs: Self) -> Self {
182 Self {
183 x: self.x | rhs.x,
184 y: self.y | rhs.y,
185 z: self.z | rhs.z,
186 }
187 }
188}
189
190impl BitOr<&Self> for BVec3 {
191 type Output = Self;
192 #[inline]
193 fn bitor(self, rhs: &Self) -> Self {
194 self.bitor(*rhs)
195 }
196}
197
198impl BitOr<&BVec3> for &BVec3 {
199 type Output = BVec3;
200 #[inline]
201 fn bitor(self, rhs: &BVec3) -> BVec3 {
202 (*self).bitor(*rhs)
203 }
204}
205
206impl BitOr<BVec3> for &BVec3 {
207 type Output = BVec3;
208 #[inline]
209 fn bitor(self, rhs: BVec3) -> BVec3 {
210 (*self).bitor(rhs)
211 }
212}
213
214impl BitOrAssign for BVec3 {
215 #[inline]
216 fn bitor_assign(&mut self, rhs: Self) {
217 *self = self.bitor(rhs);
218 }
219}
220
221impl BitOrAssign<&Self> for BVec3 {
222 #[inline]
223 fn bitor_assign(&mut self, rhs: &Self) {
224 self.bitor_assign(*rhs);
225 }
226}
227
228impl BitXor for BVec3 {
229 type Output = Self;
230 #[inline]
231 fn bitxor(self, rhs: Self) -> Self {
232 Self {
233 x: self.x ^ rhs.x,
234 y: self.y ^ rhs.y,
235 z: self.z ^ rhs.z,
236 }
237 }
238}
239
240impl BitXor<&Self> for BVec3 {
241 type Output = Self;
242 #[inline]
243 fn bitxor(self, rhs: &Self) -> Self {
244 self.bitxor(*rhs)
245 }
246}
247
248impl BitXor<&BVec3> for &BVec3 {
249 type Output = BVec3;
250 #[inline]
251 fn bitxor(self, rhs: &BVec3) -> BVec3 {
252 (*self).bitxor(*rhs)
253 }
254}
255
256impl BitXor<BVec3> for &BVec3 {
257 type Output = BVec3;
258 #[inline]
259 fn bitxor(self, rhs: BVec3) -> BVec3 {
260 (*self).bitxor(rhs)
261 }
262}
263
264impl BitXorAssign for BVec3 {
265 #[inline]
266 fn bitxor_assign(&mut self, rhs: Self) {
267 *self = self.bitxor(rhs);
268 }
269}
270
271impl BitXorAssign<&Self> for BVec3 {
272 #[inline]
273 fn bitxor_assign(&mut self, rhs: &Self) {
274 self.bitxor_assign(*rhs);
275 }
276}
277
278impl Not for BVec3 {
279 type Output = Self;
280 #[inline]
281 fn not(self) -> Self {
282 Self {
283 x: !self.x,
284 y: !self.y,
285 z: !self.z,
286 }
287 }
288}
289
290impl Not for &BVec3 {
291 type Output = BVec3;
292 #[inline]
293 fn not(self) -> BVec3 {
294 (*self).not()
295 }
296}
297
298impl fmt::Debug for BVec3 {
299 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
300 let arr = self.into_u32_array();
301 write!(
302 f,
303 "{}({:#x}, {:#x}, {:#x})",
304 stringify!(BVec3),
305 arr[0],
306 arr[1],
307 arr[2]
308 )
309 }
310}
311
312impl fmt::Display for BVec3 {
313 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314 let arr = self.into_bool_array();
315 write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
316 }
317}
318
319impl From<[bool; 3]> for BVec3 {
320 #[inline]
321 fn from(a: [bool; 3]) -> Self {
322 Self::from_array(a)
323 }
324}
325
326impl From<BVec3> for [bool; 3] {
327 #[inline]
328 fn from(mask: BVec3) -> Self {
329 mask.into_bool_array()
330 }
331}
332
333impl From<BVec3> for [u32; 3] {
334 #[inline]
335 fn from(mask: BVec3) -> Self {
336 mask.into_u32_array()
337 }
338}