1use std::marker::PhantomData;
2use std::iter::{Iterator, ExactSizeIterator};
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 { self.geom_id(i) != u32::MAX }
51}
52
53pub struct SoARayRef<'a, T> {
54 ray: &'a T,
55 idx: usize,
56}
57
58impl<'a, T: SoARay + 'a> SoARayRef<'a, T> {
59 pub fn origin(&self) -> Vector3<f32> { self.ray.org(self.idx) }
60 pub fn dir(&self) -> Vector3<f32> { self.ray.dir(self.idx) }
61 pub fn tnear(&self) -> f32 { self.ray.tnear(self.idx) }
62 pub fn tfar(&self) -> f32 { self.ray.tfar(self.idx) }
63 pub fn mask(&self) -> u32 { self.ray.mask(self.idx) }
64 pub fn id(&self) -> u32 { self.ray.id(self.idx) }
65 pub fn flags(&self) -> u32 { self.ray.flags(self.idx) }
66}
67
68pub struct SoARayRefMut<'a, T> {
70 ray: *mut T,
71 idx: usize,
72 marker: PhantomData<&'a mut T>,
73}
74
75impl<'a, T: SoARay + 'a> SoARayRefMut<'a, T> {
76 pub fn origin(&self) -> Vector3<f32> {
77 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
78 ray.org(self.idx)
79 }
80 pub fn set_origin(&mut self, o: Vector3<f32>) {
81 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
82 ray.set_org(self.idx, o);
83 }
84 pub fn dir(&self) -> Vector3<f32> {
85 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
86 ray.dir(self.idx)
87 }
88 pub fn set_dir(&mut self, d: Vector3<f32>) {
89 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
90 ray.set_dir(self.idx, d);
91 }
92 pub fn tnear(&self) -> f32 {
93 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
94 ray.tnear(self.idx)
95 }
96 pub fn set_tnear(&mut self, tnear: f32) {
97 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
98 ray.set_tnear(self.idx, tnear);
99 }
100 pub fn tfar(&self) -> f32 {
101 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
102 ray.tfar(self.idx)
103 }
104 pub fn set_tfar(&mut self, tfar: f32) {
105 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
106 ray.set_tfar(self.idx, tfar);
107 }
108 pub fn mask(&self) -> u32 {
109 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
110 ray.mask(self.idx)
111 }
112 pub fn set_mask(&mut self, mask: u32) {
113 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
114 ray.set_mask(self.idx, mask);
115 }
116 pub fn id(&self) -> u32 {
117 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
118 ray.id(self.idx)
119 }
120 pub fn set_id(&mut self, id: u32) {
121 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
122 ray.set_id(self.idx, id);
123 }
124 pub fn flags(&self) -> u32 {
125 let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
126 ray.flags(self.idx)
127 }
128 pub fn set_flags(&mut self, flags: u32) {
129 let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
130 ray.set_flags(self.idx, flags);
131 }
132}
133
134pub struct SoARayIter<'a, T> {
135 ray: &'a T,
136 cur: usize,
137 len: usize,
138}
139
140impl<'a, T: SoARay + 'a> SoARayIter<'a, T> {
141 pub fn new(ray: &'a T, len: usize) -> SoARayIter<'a, T> {
142 SoARayIter { ray: ray, cur: 0, len: len }
143 }
144}
145
146impl<'a, T: SoARay + 'a> Iterator for SoARayIter<'a, T> {
147 type Item = SoARayRef<'a, T>;
148
149 fn next(&mut self) -> Option<SoARayRef<'a, T>> {
150 if self.cur >= self.len {
151 None
152 } else {
153 let i = self.cur;
154 self.cur = self.cur + 1;
155 Some(SoARayRef { ray: self.ray, idx: i })
156 }
157 }
158}
159
160impl<'a, T: SoARay + 'a> ExactSizeIterator for SoARayIter<'a, T> {
161 fn len(&self) -> usize {
162 self.len - self.cur
163 }
164}
165
166pub struct SoARayIterMut<'a, T> {
167 ray: &'a mut T,
168 cur: usize,
169 len: usize,
170}
171
172impl<'a, T: SoARay + 'a> SoARayIterMut<'a, T> {
173 pub fn new(ray: &'a mut T, len: usize) -> SoARayIterMut<'a, T> {
174 SoARayIterMut { ray: ray, cur: 0, len: len }
175 }
176}
177
178impl<'a, T: SoARay + 'a> Iterator for SoARayIterMut<'a, T> {
179 type Item = SoARayRefMut<'a, T>;
180
181 fn next(&mut self) -> Option<SoARayRefMut<'a, T>> {
182 if self.cur >= self.len {
183 None
184 } else {
185 let i = self.cur;
186 self.cur = self.cur + 1;
187 Some(SoARayRefMut {
188 ray: self.ray as *mut T,
189 idx: i,
190 marker: PhantomData
191 })
192 }
193 }
194}
195
196impl<'a, T: SoARay + 'a> ExactSizeIterator for SoARayIterMut<'a, T> {
197 fn len(&self) -> usize {
198 self.len - self.cur
199 }
200}
201
202pub struct SoAHitRef<'a, T> {
203 hit: &'a T,
204 idx: usize,
205}
206
207impl<'a, T: SoAHit + 'a> SoAHitRef<'a, T> {
208 pub fn normal(&self) -> Vector3<f32> { self.hit.normal(self.idx) }
209 pub fn uv(&self) -> (f32, f32) { self.hit.uv(self.idx) }
210 pub fn prim_id(&self) -> u32 { self.hit.prim_id(self.idx) }
211 pub fn geom_id(&self) -> u32 { self.hit.geom_id(self.idx) }
212 pub fn inst_id(&self) -> u32 { self.hit.inst_id(self.idx) }
213 pub fn hit(&self) -> bool { self.hit.hit(self.idx) }
214}
215
216pub struct SoAHitIter<'a, T> {
217 hit: &'a T,
218 cur: usize,
219 len: usize,
220}
221
222impl<'a, T: SoAHit + 'a> SoAHitIter<'a, T> {
223 pub fn new(hit: &'a T, len: usize) -> SoAHitIter<'a, T> {
224 SoAHitIter { hit: hit, cur: 0, len: len }
225 }
226}
227
228impl<'a, T: SoAHit + 'a> Iterator for SoAHitIter<'a, T> {
229 type Item = SoAHitRef<'a, T>;
230
231 fn next(&mut self) -> Option<SoAHitRef<'a, T>> {
232 if self.cur >= self.len {
233 None
234 } else {
235 let i = self.cur;
236 self.cur = self.cur + 1;
237 Some(SoAHitRef { hit: self.hit, idx: i })
238 }
239 }
240}
241
242impl<'a, T: SoAHit + 'a> ExactSizeIterator for SoAHitIter<'a, T> {
243 fn len(&self) -> usize {
244 self.len - self.cur
245 }
246}
247
248pub struct SoAHitRefMut<'a, T> {
249 hit: *mut T,
250 idx: usize,
251 marker: PhantomData<&'a mut T>,
252}
253
254impl<'a, T: SoAHit + 'a> SoAHitRefMut<'a, T> {
255 pub fn normal(&self) -> Vector3<f32> {
256 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
257 hit.normal(self.idx)
258 }
259 pub fn set_normal(&mut self, n: Vector3<f32>) {
260 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
261 hit.set_normal(self.idx, n)
262 }
263 pub fn uv(&self) -> (f32, f32) {
264 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
265 hit.uv(self.idx)
266 }
267 pub fn set_u(&mut self, u: f32) {
268 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
269 hit.set_u(self.idx, u);
270 }
271 pub fn set_v(&mut self, v: f32) {
272 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
273 hit.set_v(self.idx, v);
274 }
275 pub fn prim_id(&self) -> u32 {
276 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
277 hit.prim_id(self.idx)
278 }
279 pub fn set_prim_id(&mut self, id: u32) {
280 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
281 hit.set_prim_id(self.idx, id);
282 }
283 pub fn geom_id(&self) -> u32 {
284 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
285 hit.geom_id(self.idx)
286 }
287 pub fn set_geom_id(&mut self, id: u32) {
288 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
289 hit.set_geom_id(self.idx, id);
290 }
291 pub fn inst_id(&self) -> u32 {
292 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
293 hit.inst_id(self.idx)
294 }
295 pub fn set_inst_id(&mut self, id: u32) {
296 let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
297 hit.set_inst_id(self.idx, id);
298 }
299 pub fn hit(&self) -> bool {
300 let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
301 hit.hit(self.idx)
302 }
303}
304
305pub struct SoAHitIterMut<'a, T> {
306 hit: &'a mut T,
307 cur: usize,
308 len: usize,
309}
310
311impl<'a, T: SoAHit + 'a> SoAHitIterMut<'a, T> {
312 pub fn new(hit: &'a mut T, len: usize) -> SoAHitIterMut<'a, T> {
313 SoAHitIterMut { hit: hit, cur: 0, len: len }
314 }
315}
316
317impl<'a, T: SoAHit + 'a> Iterator for SoAHitIterMut<'a, T> {
318 type Item = SoAHitRefMut<'a, T>;
319
320 fn next(&mut self) -> Option<SoAHitRefMut<'a, T>> {
321 if self.cur >= self.len {
322 None
323 } else {
324 let i = self.cur;
325 self.cur = self.cur + 1;
326 Some(SoAHitRefMut {
327 hit: self.hit as *mut T,
328 idx: i,
329 marker: PhantomData
330 })
331 }
332 }
333}
334
335impl<'a, T: SoAHit + 'a> ExactSizeIterator for SoAHitIterMut<'a, T> {
336 fn len(&self) -> usize {
337 self.len - self.cur
338 }
339}