1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
use std::iter::{ExactSizeIterator, Iterator};
use std::marker::PhantomData;
use std::u32;

use cgmath::Vector3;

pub trait SoARay {
    fn org(&self, i: usize) -> Vector3<f32>;
    fn set_org(&mut self, i: usize, o: Vector3<f32>);

    fn dir(&self, i: usize) -> Vector3<f32>;
    fn set_dir(&mut self, i: usize, d: Vector3<f32>);

    fn tnear(&self, i: usize) -> f32;
    fn set_tnear(&mut self, i: usize, near: f32);

    fn tfar(&self, i: usize) -> f32;
    fn set_tfar(&mut self, i: usize, far: f32);

    fn time(&self, i: usize) -> f32;
    fn set_time(&mut self, i: usize, time: f32);

    fn mask(&self, i: usize) -> u32;
    fn set_mask(&mut self, i: usize, mask: u32);

    fn id(&self, i: usize) -> u32;
    fn set_id(&mut self, i: usize, id: u32);

    fn flags(&self, i: usize) -> u32;
    fn set_flags(&mut self, i: usize, flags: u32);
}

pub trait SoAHit {
    fn normal(&self, i: usize) -> Vector3<f32>;
    fn set_normal(&mut self, i: usize, n: Vector3<f32>);

    fn uv(&self, i: usize) -> (f32, f32);
    fn set_u(&mut self, i: usize, u: f32);
    fn set_v(&mut self, i: usize, v: f32);

    fn prim_id(&self, i: usize) -> u32;
    fn set_prim_id(&mut self, i: usize, id: u32);

    fn geom_id(&self, i: usize) -> u32;
    fn set_geom_id(&mut self, i: usize, id: u32);

    fn inst_id(&self, i: usize) -> u32;
    fn set_inst_id(&mut self, i: usize, id: u32);

    fn hit(&self, i: usize) -> bool {
        self.geom_id(i) != u32::MAX
    }
}

pub struct SoARayRef<'a, T> {
    ray: &'a T,
    idx: usize,
}

impl<'a, T: SoARay + 'a> SoARayRef<'a, T> {
    pub fn origin(&self) -> Vector3<f32> {
        self.ray.org(self.idx)
    }
    pub fn dir(&self) -> Vector3<f32> {
        self.ray.dir(self.idx)
    }
    pub fn tnear(&self) -> f32 {
        self.ray.tnear(self.idx)
    }
    pub fn tfar(&self) -> f32 {
        self.ray.tfar(self.idx)
    }
    pub fn mask(&self) -> u32 {
        self.ray.mask(self.idx)
    }
    pub fn id(&self) -> u32 {
        self.ray.id(self.idx)
    }
    pub fn flags(&self) -> u32 {
        self.ray.flags(self.idx)
    }
}

// TODO: Is this going to work well?
pub struct SoARayRefMut<'a, T> {
    ray: *mut T,
    idx: usize,
    marker: PhantomData<&'a mut T>,
}

impl<'a, T: SoARay + 'a> SoARayRefMut<'a, T> {
    pub fn origin(&self) -> Vector3<f32> {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.org(self.idx)
    }
    pub fn set_origin(&mut self, o: Vector3<f32>) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_org(self.idx, o);
    }
    pub fn dir(&self) -> Vector3<f32> {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.dir(self.idx)
    }
    pub fn set_dir(&mut self, d: Vector3<f32>) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_dir(self.idx, d);
    }
    pub fn tnear(&self) -> f32 {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.tnear(self.idx)
    }
    pub fn set_tnear(&mut self, tnear: f32) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_tnear(self.idx, tnear);
    }
    pub fn tfar(&self) -> f32 {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.tfar(self.idx)
    }
    pub fn set_tfar(&mut self, tfar: f32) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_tfar(self.idx, tfar);
    }
    pub fn mask(&self) -> u32 {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.mask(self.idx)
    }
    pub fn set_mask(&mut self, mask: u32) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_mask(self.idx, mask);
    }
    pub fn id(&self) -> u32 {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.id(self.idx)
    }
    pub fn set_id(&mut self, id: u32) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_id(self.idx, id);
    }
    pub fn flags(&self) -> u32 {
        let ray = unsafe { self.ray.as_ref().expect("should never be null!") };
        ray.flags(self.idx)
    }
    pub fn set_flags(&mut self, flags: u32) {
        let ray = unsafe { self.ray.as_mut().expect("should never be null!") };
        ray.set_flags(self.idx, flags);
    }
}

pub struct SoARayIter<'a, T> {
    ray: &'a T,
    cur: usize,
    len: usize,
}

impl<'a, T: SoARay + 'a> SoARayIter<'a, T> {
    pub fn new(ray: &'a T, len: usize) -> SoARayIter<'a, T> {
        SoARayIter {
            ray: ray,
            cur: 0,
            len: len,
        }
    }
}

impl<'a, T: SoARay + 'a> Iterator for SoARayIter<'a, T> {
    type Item = SoARayRef<'a, T>;

    fn next(&mut self) -> Option<SoARayRef<'a, T>> {
        if self.cur >= self.len {
            None
        } else {
            let i = self.cur;
            self.cur = self.cur + 1;
            Some(SoARayRef {
                ray: self.ray,
                idx: i,
            })
        }
    }
}

impl<'a, T: SoARay + 'a> ExactSizeIterator for SoARayIter<'a, T> {
    fn len(&self) -> usize {
        self.len - self.cur
    }
}

pub struct SoARayIterMut<'a, T> {
    ray: &'a mut T,
    cur: usize,
    len: usize,
}

impl<'a, T: SoARay + 'a> SoARayIterMut<'a, T> {
    pub fn new(ray: &'a mut T, len: usize) -> SoARayIterMut<'a, T> {
        SoARayIterMut {
            ray: ray,
            cur: 0,
            len: len,
        }
    }
}

impl<'a, T: SoARay + 'a> Iterator for SoARayIterMut<'a, T> {
    type Item = SoARayRefMut<'a, T>;

    fn next(&mut self) -> Option<SoARayRefMut<'a, T>> {
        if self.cur >= self.len {
            None
        } else {
            let i = self.cur;
            self.cur = self.cur + 1;
            Some(SoARayRefMut {
                ray: self.ray as *mut T,
                idx: i,
                marker: PhantomData,
            })
        }
    }
}

impl<'a, T: SoARay + 'a> ExactSizeIterator for SoARayIterMut<'a, T> {
    fn len(&self) -> usize {
        self.len - self.cur
    }
}

pub struct SoAHitRef<'a, T> {
    hit: &'a T,
    idx: usize,
}

impl<'a, T: SoAHit + 'a> SoAHitRef<'a, T> {
    pub fn normal(&self) -> Vector3<f32> {
        self.hit.normal(self.idx)
    }
    pub fn uv(&self) -> (f32, f32) {
        self.hit.uv(self.idx)
    }
    pub fn prim_id(&self) -> u32 {
        self.hit.prim_id(self.idx)
    }
    pub fn geom_id(&self) -> u32 {
        self.hit.geom_id(self.idx)
    }
    pub fn inst_id(&self) -> u32 {
        self.hit.inst_id(self.idx)
    }
    pub fn hit(&self) -> bool {
        self.hit.hit(self.idx)
    }
}

pub struct SoAHitIter<'a, T> {
    hit: &'a T,
    cur: usize,
    len: usize,
}

impl<'a, T: SoAHit + 'a> SoAHitIter<'a, T> {
    pub fn new(hit: &'a T, len: usize) -> SoAHitIter<'a, T> {
        SoAHitIter {
            hit: hit,
            cur: 0,
            len: len,
        }
    }
}

impl<'a, T: SoAHit + 'a> Iterator for SoAHitIter<'a, T> {
    type Item = SoAHitRef<'a, T>;

    fn next(&mut self) -> Option<SoAHitRef<'a, T>> {
        if self.cur >= self.len {
            None
        } else {
            let i = self.cur;
            self.cur = self.cur + 1;
            Some(SoAHitRef {
                hit: self.hit,
                idx: i,
            })
        }
    }
}

impl<'a, T: SoAHit + 'a> ExactSizeIterator for SoAHitIter<'a, T> {
    fn len(&self) -> usize {
        self.len - self.cur
    }
}

pub struct SoAHitRefMut<'a, T> {
    hit: *mut T,
    idx: usize,
    marker: PhantomData<&'a mut T>,
}

impl<'a, T: SoAHit + 'a> SoAHitRefMut<'a, T> {
    pub fn normal(&self) -> Vector3<f32> {
        let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
        hit.normal(self.idx)
    }
    pub fn set_normal(&mut self, n: Vector3<f32>) {
        let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
        hit.set_normal(self.idx, n)
    }
    pub fn uv(&self) -> (f32, f32) {
        let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
        hit.uv(self.idx)
    }
    pub fn set_u(&mut self, u: f32) {
        let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
        hit.set_u(self.idx, u);
    }
    pub fn set_v(&mut self, v: f32) {
        let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
        hit.set_v(self.idx, v);
    }
    pub fn prim_id(&self) -> u32 {
        let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
        hit.prim_id(self.idx)
    }
    pub fn set_prim_id(&mut self, id: u32) {
        let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
        hit.set_prim_id(self.idx, id);
    }
    pub fn geom_id(&self) -> u32 {
        let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
        hit.geom_id(self.idx)
    }
    pub fn set_geom_id(&mut self, id: u32) {
        let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
        hit.set_geom_id(self.idx, id);
    }
    pub fn inst_id(&self) -> u32 {
        let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
        hit.inst_id(self.idx)
    }
    pub fn set_inst_id(&mut self, id: u32) {
        let hit = unsafe { self.hit.as_mut().expect("should never be null!") };
        hit.set_inst_id(self.idx, id);
    }
    pub fn hit(&self) -> bool {
        let hit = unsafe { self.hit.as_ref().expect("should never be null!") };
        hit.hit(self.idx)
    }
}

pub struct SoAHitIterMut<'a, T> {
    hit: &'a mut T,
    cur: usize,
    len: usize,
}

impl<'a, T: SoAHit + 'a> SoAHitIterMut<'a, T> {
    pub fn new(hit: &'a mut T, len: usize) -> SoAHitIterMut<'a, T> {
        SoAHitIterMut {
            hit: hit,
            cur: 0,
            len: len,
        }
    }
}

impl<'a, T: SoAHit + 'a> Iterator for SoAHitIterMut<'a, T> {
    type Item = SoAHitRefMut<'a, T>;

    fn next(&mut self) -> Option<SoAHitRefMut<'a, T>> {
        if self.cur >= self.len {
            None
        } else {
            let i = self.cur;
            self.cur = self.cur + 1;
            Some(SoAHitRefMut {
                hit: self.hit as *mut T,
                idx: i,
                marker: PhantomData,
            })
        }
    }
}

impl<'a, T: SoAHit + 'a> ExactSizeIterator for SoAHitIterMut<'a, T> {
    fn len(&self) -> usize {
        self.len - self.cur
    }
}