1use std::iter::{ExactSizeIterator, Iterator};
2use std::marker::PhantomData;
3use std::u32;
4
5use cgmath::Vector3;
6
7pub trait SoARay {
8 fn org(&self, i: usize) -> Vector3<f32>;
9 fn set_org(&mut self, i: usize, o: Vector3<f32>);
10
11 fn dir(&self, i: usize) -> Vector3<f32>;
12 fn set_dir(&mut self, i: usize, d: Vector3<f32>);
13
14 fn tnear(&self, i: usize) -> f32;
15 fn set_tnear(&mut self, i: usize, near: f32);
16
17 fn tfar(&self, i: usize) -> f32;
18 fn set_tfar(&mut self, i: usize, far: f32);
19
20 fn time(&self, i: usize) -> f32;
21 fn set_time(&mut self, i: usize, time: f32);
22
23 fn mask(&self, i: usize) -> u32;
24 fn set_mask(&mut self, i: usize, mask: u32);
25
26 fn id(&self, i: usize) -> u32;
27 fn set_id(&mut self, i: usize, id: u32);
28
29 fn flags(&self, i: usize) -> u32;
30 fn set_flags(&mut self, i: usize, flags: u32);
31}
32
33pub trait SoAHit {
34 fn normal(&self, i: usize) -> Vector3<f32>;
35 fn set_normal(&mut self, i: usize, n: Vector3<f32>);
36
37 fn uv(&self, i: usize) -> (f32, f32);
38 fn set_u(&mut self, i: usize, u: f32);
39 fn set_v(&mut self, i: usize, v: f32);
40
41 fn prim_id(&self, i: usize) -> u32;
42 fn set_prim_id(&mut self, i: usize, id: u32);
43
44 fn geom_id(&self, i: usize) -> u32;
45 fn set_geom_id(&mut self, i: usize, id: u32);
46
47 fn inst_id(&self, i: usize) -> u32;
48 fn set_inst_id(&mut self, i: usize, id: u32);
49
50 fn hit(&self, i: usize) -> bool {
51 self.geom_id(i) != u32::MAX
52 }
53}
54
55pub struct SoARayRef<'a, T> {
56 ray: &'a T,
57 idx: usize,
58}
59
60impl<'a, T: SoARay + 'a> SoARayRef<'a, T> {
61 pub fn origin(&self) -> Vector3<f32> {
62 self.ray.org(self.idx)
63 }
64 pub fn dir(&self) -> Vector3<f32> {
65 self.ray.dir(self.idx)
66 }
67 pub fn tnear(&self) -> f32 {
68 self.ray.tnear(self.idx)
69 }
70 pub fn tfar(&self) -> f32 {
71 self.ray.tfar(self.idx)
72 }
73 pub fn mask(&self) -> u32 {
74 self.ray.mask(self.idx)
75 }
76 pub fn id(&self) -> u32 {
77 self.ray.id(self.idx)
78 }
79 pub fn flags(&self) -> u32 {
80 self.ray.flags(self.idx)
81 }
82}
83
84pub struct SoARayRefMut<'a, T> {
86 ray: *mut T,
87 idx: usize,
88 marker: PhantomData<&'a mut T>,
89}
90
91impl<'a, T: SoARay + 'a> SoARayRefMut<'a, T> {
92 pub fn origin(&self) -> Vector3<f32> {
93 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
94 ray.org(self.idx)
95 }
96 pub fn set_origin(&mut self, o: Vector3<f32>) {
97 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
98 ray.set_org(self.idx, o);
99 }
100 pub fn dir(&self) -> Vector3<f32> {
101 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
102 ray.dir(self.idx)
103 }
104 pub fn set_dir(&mut self, d: Vector3<f32>) {
105 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
106 ray.set_dir(self.idx, d);
107 }
108 pub fn tnear(&self) -> f32 {
109 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
110 ray.tnear(self.idx)
111 }
112 pub fn set_tnear(&mut self, tnear: f32) {
113 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
114 ray.set_tnear(self.idx, tnear);
115 }
116 pub fn tfar(&self) -> f32 {
117 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
118 ray.tfar(self.idx)
119 }
120 pub fn set_tfar(&mut self, tfar: f32) {
121 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
122 ray.set_tfar(self.idx, tfar);
123 }
124 pub fn mask(&self) -> u32 {
125 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
126 ray.mask(self.idx)
127 }
128 pub fn set_mask(&mut self, mask: u32) {
129 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
130 ray.set_mask(self.idx, mask);
131 }
132 pub fn id(&self) -> u32 {
133 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
134 ray.id(self.idx)
135 }
136 pub fn set_id(&mut self, id: u32) {
137 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
138 ray.set_id(self.idx, id);
139 }
140 pub fn flags(&self) -> u32 {
141 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
142 ray.flags(self.idx)
143 }
144 pub fn set_flags(&mut self, flags: u32) {
145 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
146 ray.set_flags(self.idx, flags);
147 }
148}
149
150pub struct SoARayIter<'a, T> {
151 ray: &'a T,
152 cur: usize,
153 len: usize,
154}
155
156impl<'a, T: SoARay + 'a> SoARayIter<'a, T> {
157 pub fn new(ray: &'a T, len: usize) -> SoARayIter<'a, T> {
158 SoARayIter {
159 ray: ray,
160 cur: 0,
161 len: len,
162 }
163 }
164}
165
166impl<'a, T: SoARay + 'a> Iterator for SoARayIter<'a, T> {
167 type Item = SoARayRef<'a, T>;
168
169 fn next(&mut self) -> Option<SoARayRef<'a, T>> {
170 if self.cur >= self.len {
171 None
172 } else {
173 let i = self.cur;
174 self.cur = self.cur + 1;
175 Some(SoARayRef {
176 ray: self.ray,
177 idx: i,
178 })
179 }
180 }
181}
182
183impl<'a, T: SoARay + 'a> ExactSizeIterator for SoARayIter<'a, T> {
184 fn len(&self) -> usize {
185 self.len - self.cur
186 }
187}
188
189pub struct SoARayIterMut<'a, T> {
190 ray: &'a mut T,
191 cur: usize,
192 len: usize,
193}
194
195impl<'a, T: SoARay + 'a> SoARayIterMut<'a, T> {
196 pub fn new(ray: &'a mut T, len: usize) -> SoARayIterMut<'a, T> {
197 SoARayIterMut {
198 ray: ray,
199 cur: 0,
200 len: len,
201 }
202 }
203}
204
205impl<'a, T: SoARay + 'a> Iterator for SoARayIterMut<'a, T> {
206 type Item = SoARayRefMut<'a, T>;
207
208 fn next(&mut self) -> Option<SoARayRefMut<'a, T>> {
209 if self.cur >= self.len {
210 None
211 } else {
212 let i = self.cur;
213 self.cur = self.cur + 1;
214 Some(SoARayRefMut {
215 ray: self.ray as *mut T,
216 idx: i,
217 marker: PhantomData,
218 })
219 }
220 }
221}
222
223impl<'a, T: SoARay + 'a> ExactSizeIterator for SoARayIterMut<'a, T> {
224 fn len(&self) -> usize {
225 self.len - self.cur
226 }
227}
228
229pub struct SoAHitRef<'a, T> {
230 hit: &'a T,
231 idx: usize,
232}
233
234impl<'a, T: SoAHit + 'a> SoAHitRef<'a, T> {
235 pub fn normal(&self) -> Vector3<f32> {
236 self.hit.normal(self.idx)
237 }
238 pub fn uv(&self) -> (f32, f32) {
239 self.hit.uv(self.idx)
240 }
241 pub fn prim_id(&self) -> u32 {
242 self.hit.prim_id(self.idx)
243 }
244 pub fn geom_id(&self) -> u32 {
245 self.hit.geom_id(self.idx)
246 }
247 pub fn inst_id(&self) -> u32 {
248 self.hit.inst_id(self.idx)
249 }
250 pub fn hit(&self) -> bool {
251 self.hit.hit(self.idx)
252 }
253}
254
255pub struct SoAHitIter<'a, T> {
256 hit: &'a T,
257 cur: usize,
258 len: usize,
259}
260
261impl<'a, T: SoAHit + 'a> SoAHitIter<'a, T> {
262 pub fn new(hit: &'a T, len: usize) -> SoAHitIter<'a, T> {
263 SoAHitIter {
264 hit: hit,
265 cur: 0,
266 len: len,
267 }
268 }
269}
270
271impl<'a, T: SoAHit + 'a> Iterator for SoAHitIter<'a, T> {
272 type Item = SoAHitRef<'a, T>;
273
274 fn next(&mut self) -> Option<SoAHitRef<'a, T>> {
275 if self.cur >= self.len {
276 None
277 } else {
278 let i = self.cur;
279 self.cur = self.cur + 1;
280 Some(SoAHitRef {
281 hit: self.hit,
282 idx: i,
283 })
284 }
285 }
286}
287
288impl<'a, T: SoAHit + 'a> ExactSizeIterator for SoAHitIter<'a, T> {
289 fn len(&self) -> usize {
290 self.len - self.cur
291 }
292}
293
294pub struct SoAHitRefMut<'a, T> {
295 hit: *mut T,
296 idx: usize,
297 marker: PhantomData<&'a mut T>,
298}
299
300impl<'a, T: SoAHit + 'a> SoAHitRefMut<'a, T> {
301 pub fn normal(&self) -> Vector3<f32> {
302 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
303 hit.normal(self.idx)
304 }
305 pub fn set_normal(&mut self, n: Vector3<f32>) {
306 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
307 hit.set_normal(self.idx, n)
308 }
309 pub fn uv(&self) -> (f32, f32) {
310 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
311 hit.uv(self.idx)
312 }
313 pub fn set_u(&mut self, u: f32) {
314 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
315 hit.set_u(self.idx, u);
316 }
317 pub fn set_v(&mut self, v: f32) {
318 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
319 hit.set_v(self.idx, v);
320 }
321 pub fn prim_id(&self) -> u32 {
322 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
323 hit.prim_id(self.idx)
324 }
325 pub fn set_prim_id(&mut self, id: u32) {
326 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
327 hit.set_prim_id(self.idx, id);
328 }
329 pub fn geom_id(&self) -> u32 {
330 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
331 hit.geom_id(self.idx)
332 }
333 pub fn set_geom_id(&mut self, id: u32) {
334 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
335 hit.set_geom_id(self.idx, id);
336 }
337 pub fn inst_id(&self) -> u32 {
338 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
339 hit.inst_id(self.idx)
340 }
341 pub fn set_inst_id(&mut self, id: u32) {
342 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
343 hit.set_inst_id(self.idx, id);
344 }
345 pub fn hit(&self) -> bool {
346 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
347 hit.hit(self.idx)
348 }
349}
350
351pub struct SoAHitIterMut<'a, T> {
352 hit: &'a mut T,
353 cur: usize,
354 len: usize,
355}
356
357impl<'a, T: SoAHit + 'a> SoAHitIterMut<'a, T> {
358 pub fn new(hit: &'a mut T, len: usize) -> SoAHitIterMut<'a, T> {
359 SoAHitIterMut {
360 hit: hit,
361 cur: 0,
362 len: len,
363 }
364 }
365}
366
367impl<'a, T: SoAHit + 'a> Iterator for SoAHitIterMut<'a, T> {
368 type Item = SoAHitRefMut<'a, T>;
369
370 fn next(&mut self) -> Option<SoAHitRefMut<'a, T>> {
371 if self.cur >= self.len {
372 None
373 } else {
374 let i = self.cur;
375 self.cur = self.cur + 1;
376 Some(SoAHitRefMut {
377 hit: self.hit as *mut T,
378 idx: i,
379 marker: PhantomData,
380 })
381 }
382 }
383}
384
385impl<'a, T: SoAHit + 'a> ExactSizeIterator for SoAHitIterMut<'a, T> {
386 fn len(&self) -> usize {
387 self.len - self.cur
388 }
389}