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
use cgmath::Vector3;
use std::marker::PhantomData;
use std::{f32, u32};

use soa_ray::{
    SoAHit, SoAHitIter, SoAHitIterMut, SoAHitRef, SoARay, SoARayIter, SoARayIterMut, SoARayRef,
    SoARayRefMut,
};
use sys;

pub type Ray4 = sys::RTCRay4;
pub type Hit4 = sys::RTCHit4;
pub type RayHit4 = sys::RTCRayHit4;

impl Ray4 {
    pub fn empty() -> Ray4 {
        Ray4::segment(
            [Vector3::new(0.0, 0.0, 0.0); 4],
            [Vector3::new(0.0, 0.0, 0.0); 4],
            [0.0; 4],
            [f32::INFINITY; 4],
        )
    }
    pub fn new(origin: [Vector3<f32>; 4], dir: [Vector3<f32>; 4]) -> Ray4 {
        Ray4::segment(origin, dir, [0.0; 4], [f32::INFINITY; 4])
    }
    pub fn segment(
        origin: [Vector3<f32>; 4],
        dir: [Vector3<f32>; 4],
        tnear: [f32; 4],
        tfar: [f32; 4],
    ) -> Ray4 {
        sys::RTCRay4 {
            org_x: [origin[0].x, origin[1].x, origin[2].x, origin[3].x],
            org_y: [origin[0].y, origin[1].y, origin[2].y, origin[3].y],
            org_z: [origin[0].z, origin[1].z, origin[2].z, origin[3].z],
            dir_x: [dir[0].x, dir[1].x, dir[2].x, dir[3].x],
            dir_y: [dir[0].y, dir[1].y, dir[2].y, dir[3].y],
            dir_z: [dir[0].z, dir[1].z, dir[2].z, dir[3].z],
            tnear: tnear,
            tfar: tfar,
            time: [0.0; 4],
            mask: [u32::MAX; 4],
            id: [0; 4],
            flags: [0; 4],
        }
    }
    pub fn iter(&self) -> SoARayIter<Ray4> {
        SoARayIter::new(self, 4)
    }
    pub fn iter_mut(&mut self) -> SoARayIterMut<Ray4> {
        SoARayIterMut::new(self, 4)
    }
}

impl SoARay for Ray4 {
    fn org(&self, i: usize) -> Vector3<f32> {
        Vector3::new(self.org_x[i], self.org_y[i], self.org_z[i])
    }
    fn set_org(&mut self, i: usize, o: Vector3<f32>) {
        self.org_x[i] = o.x;
        self.org_y[i] = o.y;
        self.org_z[i] = o.z;
    }

    fn dir(&self, i: usize) -> Vector3<f32> {
        Vector3::new(self.dir_x[i], self.dir_y[i], self.dir_z[i])
    }
    fn set_dir(&mut self, i: usize, d: Vector3<f32>) {
        self.dir_x[i] = d.x;
        self.dir_y[i] = d.y;
        self.dir_z[i] = d.z;
    }

    fn tnear(&self, i: usize) -> f32 {
        self.tnear[i]
    }
    fn set_tnear(&mut self, i: usize, near: f32) {
        self.tnear[i] = near;
    }

    fn tfar(&self, i: usize) -> f32 {
        self.tfar[i]
    }
    fn set_tfar(&mut self, i: usize, far: f32) {
        self.tfar[i] = far;
    }

    fn time(&self, i: usize) -> f32 {
        self.time[i]
    }
    fn set_time(&mut self, i: usize, time: f32) {
        self.time[i] = time;
    }

    fn mask(&self, i: usize) -> u32 {
        self.mask[i]
    }
    fn set_mask(&mut self, i: usize, mask: u32) {
        self.mask[i] = mask;
    }

    fn id(&self, i: usize) -> u32 {
        self.id[i]
    }
    fn set_id(&mut self, i: usize, id: u32) {
        self.id[i] = id;
    }

    fn flags(&self, i: usize) -> u32 {
        self.flags[i]
    }
    fn set_flags(&mut self, i: usize, flags: u32) {
        self.flags[i] = flags;
    }
}

impl Hit4 {
    pub fn new() -> Hit4 {
        sys::RTCHit4 {
            Ng_x: [0.0; 4],
            Ng_y: [0.0; 4],
            Ng_z: [0.0; 4],
            u: [0.0; 4],
            v: [0.0; 4],
            primID: [u32::MAX; 4],
            geomID: [u32::MAX; 4],
            instID: [[u32::MAX; 4]],
        }
    }
    pub fn any_hit(&self) -> bool {
        self.hits().fold(false, |acc, g| acc || g)
    }
    pub fn hits<'a>(&'a self) -> impl Iterator<Item = bool> + 'a {
        self.geomID.iter().map(|g| *g != u32::MAX)
    }
    pub fn iter(&self) -> SoAHitIter<Hit4> {
        SoAHitIter::new(self, 4)
    }
    pub fn iter_hits<'a>(&'a self) -> impl Iterator<Item = SoAHitRef<Hit4>> + 'a {
        SoAHitIter::new(self, 4).filter(|h| h.hit())
    }
}

impl SoAHit for Hit4 {
    fn normal(&self, i: usize) -> Vector3<f32> {
        Vector3::new(self.Ng_x[i], self.Ng_y[i], self.Ng_z[i])
    }
    fn set_normal(&mut self, i: usize, n: Vector3<f32>) {
        self.Ng_x[i] = n.x;
        self.Ng_y[i] = n.y;
        self.Ng_z[i] = n.z;
    }

    fn uv(&self, i: usize) -> (f32, f32) {
        (self.u[i], self.v[i])
    }
    fn set_u(&mut self, i: usize, u: f32) {
        self.u[i] = u;
    }
    fn set_v(&mut self, i: usize, v: f32) {
        self.v[i] = v;
    }

    fn prim_id(&self, i: usize) -> u32 {
        self.primID[i]
    }
    fn set_prim_id(&mut self, i: usize, id: u32) {
        self.primID[i] = id;
    }

    fn geom_id(&self, i: usize) -> u32 {
        self.geomID[i]
    }
    fn set_geom_id(&mut self, i: usize, id: u32) {
        self.geomID[i] = id;
    }

    fn inst_id(&self, i: usize) -> u32 {
        self.instID[0][i]
    }
    fn set_inst_id(&mut self, i: usize, id: u32) {
        self.instID[0][i] = id;
    }
}

impl RayHit4 {
    pub fn new(ray: Ray4) -> RayHit4 {
        sys::RTCRayHit4 {
            ray: ray,
            hit: Hit4::new(),
        }
    }
    pub fn iter(&self) -> std::iter::Zip<SoARayIter<Ray4>, SoAHitIter<Hit4>> {
        self.ray.iter().zip(self.hit.iter())
    }
}