1use cgmath::Vector3;
2use std::marker::PhantomData;
3use std::{f32, u32};
4
5use soa_ray::{
6 SoAHit, SoAHitIter, SoAHitIterMut, SoAHitRef, SoARay, SoARayIter, SoARayIterMut, SoARayRef,
7 SoARayRefMut,
8};
9use sys;
10
11pub type Ray4 = sys::RTCRay4;
12pub type Hit4 = sys::RTCHit4;
13pub type RayHit4 = sys::RTCRayHit4;
14
15impl Ray4 {
16 pub fn empty() -> Ray4 {
17 Ray4::segment(
18 [Vector3::new(0.0, 0.0, 0.0); 4],
19 [Vector3::new(0.0, 0.0, 0.0); 4],
20 [0.0; 4],
21 [f32::INFINITY; 4],
22 )
23 }
24 pub fn new(origin: [Vector3<f32>; 4], dir: [Vector3<f32>; 4]) -> Ray4 {
25 Ray4::segment(origin, dir, [0.0; 4], [f32::INFINITY; 4])
26 }
27 pub fn segment(
28 origin: [Vector3<f32>; 4],
29 dir: [Vector3<f32>; 4],
30 tnear: [f32; 4],
31 tfar: [f32; 4],
32 ) -> Ray4 {
33 sys::RTCRay4 {
34 org_x: [origin[0].x, origin[1].x, origin[2].x, origin[3].x],
35 org_y: [origin[0].y, origin[1].y, origin[2].y, origin[3].y],
36 org_z: [origin[0].z, origin[1].z, origin[2].z, origin[3].z],
37 dir_x: [dir[0].x, dir[1].x, dir[2].x, dir[3].x],
38 dir_y: [dir[0].y, dir[1].y, dir[2].y, dir[3].y],
39 dir_z: [dir[0].z, dir[1].z, dir[2].z, dir[3].z],
40 tnear: tnear,
41 tfar: tfar,
42 time: [0.0; 4],
43 mask: [u32::MAX; 4],
44 id: [0; 4],
45 flags: [0; 4],
46 }
47 }
48 pub fn iter(&self) -> SoARayIter<Ray4> {
49 SoARayIter::new(self, 4)
50 }
51 pub fn iter_mut(&mut self) -> SoARayIterMut<Ray4> {
52 SoARayIterMut::new(self, 4)
53 }
54}
55
56impl SoARay for Ray4 {
57 fn org(&self, i: usize) -> Vector3<f32> {
58 Vector3::new(self.org_x[i], self.org_y[i], self.org_z[i])
59 }
60 fn set_org(&mut self, i: usize, o: Vector3<f32>) {
61 self.org_x[i] = o.x;
62 self.org_y[i] = o.y;
63 self.org_z[i] = o.z;
64 }
65
66 fn dir(&self, i: usize) -> Vector3<f32> {
67 Vector3::new(self.dir_x[i], self.dir_y[i], self.dir_z[i])
68 }
69 fn set_dir(&mut self, i: usize, d: Vector3<f32>) {
70 self.dir_x[i] = d.x;
71 self.dir_y[i] = d.y;
72 self.dir_z[i] = d.z;
73 }
74
75 fn tnear(&self, i: usize) -> f32 {
76 self.tnear[i]
77 }
78 fn set_tnear(&mut self, i: usize, near: f32) {
79 self.tnear[i] = near;
80 }
81
82 fn tfar(&self, i: usize) -> f32 {
83 self.tfar[i]
84 }
85 fn set_tfar(&mut self, i: usize, far: f32) {
86 self.tfar[i] = far;
87 }
88
89 fn time(&self, i: usize) -> f32 {
90 self.time[i]
91 }
92 fn set_time(&mut self, i: usize, time: f32) {
93 self.time[i] = time;
94 }
95
96 fn mask(&self, i: usize) -> u32 {
97 self.mask[i]
98 }
99 fn set_mask(&mut self, i: usize, mask: u32) {
100 self.mask[i] = mask;
101 }
102
103 fn id(&self, i: usize) -> u32 {
104 self.id[i]
105 }
106 fn set_id(&mut self, i: usize, id: u32) {
107 self.id[i] = id;
108 }
109
110 fn flags(&self, i: usize) -> u32 {
111 self.flags[i]
112 }
113 fn set_flags(&mut self, i: usize, flags: u32) {
114 self.flags[i] = flags;
115 }
116}
117
118impl Hit4 {
119 pub fn new() -> Hit4 {
120 sys::RTCHit4 {
121 Ng_x: [0.0; 4],
122 Ng_y: [0.0; 4],
123 Ng_z: [0.0; 4],
124 u: [0.0; 4],
125 v: [0.0; 4],
126 primID: [u32::MAX; 4],
127 geomID: [u32::MAX; 4],
128 instID: [[u32::MAX; 4]],
129 }
130 }
131 pub fn any_hit(&self) -> bool {
132 self.hits().fold(false, |acc, g| acc || g)
133 }
134 pub fn hits<'a>(&'a self) -> impl Iterator<Item = bool> + 'a {
135 self.geomID.iter().map(|g| *g != u32::MAX)
136 }
137 pub fn iter(&self) -> SoAHitIter<Hit4> {
138 SoAHitIter::new(self, 4)
139 }
140 pub fn iter_hits<'a>(&'a self) -> impl Iterator<Item = SoAHitRef<Hit4>> + 'a {
141 SoAHitIter::new(self, 4).filter(|h| h.hit())
142 }
143}
144
145impl SoAHit for Hit4 {
146 fn normal(&self, i: usize) -> Vector3<f32> {
147 Vector3::new(self.Ng_x[i], self.Ng_y[i], self.Ng_z[i])
148 }
149 fn set_normal(&mut self, i: usize, n: Vector3<f32>) {
150 self.Ng_x[i] = n.x;
151 self.Ng_y[i] = n.y;
152 self.Ng_z[i] = n.z;
153 }
154
155 fn uv(&self, i: usize) -> (f32, f32) {
156 (self.u[i], self.v[i])
157 }
158 fn set_u(&mut self, i: usize, u: f32) {
159 self.u[i] = u;
160 }
161 fn set_v(&mut self, i: usize, v: f32) {
162 self.v[i] = v;
163 }
164
165 fn prim_id(&self, i: usize) -> u32 {
166 self.primID[i]
167 }
168 fn set_prim_id(&mut self, i: usize, id: u32) {
169 self.primID[i] = id;
170 }
171
172 fn geom_id(&self, i: usize) -> u32 {
173 self.geomID[i]
174 }
175 fn set_geom_id(&mut self, i: usize, id: u32) {
176 self.geomID[i] = id;
177 }
178
179 fn inst_id(&self, i: usize) -> u32 {
180 self.instID[0][i]
181 }
182 fn set_inst_id(&mut self, i: usize, id: u32) {
183 self.instID[0][i] = id;
184 }
185}
186
187impl RayHit4 {
188 pub fn new(ray: Ray4) -> RayHit4 {
189 sys::RTCRayHit4 {
190 ray: ray,
191 hit: Hit4::new(),
192 }
193 }
194 pub fn iter(&self) -> std::iter::Zip<SoARayIter<Ray4>, SoAHitIter<Hit4>> {
195 self.ray.iter().zip(self.hit.iter())
196 }
197}