embree_rs/
ray_packet.rs

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