-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmod.rs
293 lines (263 loc) · 9.96 KB
/
mod.rs
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
mod environment;
mod importance_map;
pub use environment::EnvironmentMap;
pub use importance_map::ImportanceMap;
use crate::prelude::*;
use crate::hittable::*;
use crate::mediums::MediumTable;
pub use crate::accelerator::{Accelerator, AcceleratorType};
pub use crate::geometry::*;
pub use crate::materials::*;
#[derive(Clone, Debug)]
pub struct World {
pub accelerator: Accelerator,
pub lights: Vec<InstanceId>,
pub cameras: Vec<CameraEnum>,
pub materials: MaterialTable,
pub mediums: MediumTable,
pub environment: EnvironmentMap,
env_sampling_probability: f32,
pub radius: f32,
pub center: Point3,
}
impl World {
pub fn new(
instances: Vec<Instance>,
materials: MaterialTable,
mediums: MediumTable,
environment: EnvironmentMap,
cameras: Vec<CameraEnum>,
mut env_sampling_probability: f32,
accelerator_type: AcceleratorType,
) -> Self {
// TODO: add light accelerator data structure, to prevent sampling very distant (and small) lights when there are lights closer by
let mut lights = Vec::new();
for instance in instances.iter() {
match &instance.aggregate {
Aggregate::Mesh(mesh) => {
for tri in mesh.triangles.as_ref().unwrap() {
if let MaterialId::Light(id) = tri.get_material_id() {
info!(
"adding light with mat id Light({:?}) and instance id {:?} to lights list",
id, instance.instance_id
);
lights.push(instance.instance_id as InstanceId);
}
}
}
_ => {
if let MaterialId::Light(id) = instance.get_material_id() {
info!(
"adding light with mat id Light({:?}) and instance id {:?} to lights list",
id, instance.instance_id
);
lights.push(instance.instance_id as InstanceId);
}
}
}
}
let accelerator = Accelerator::new(instances, accelerator_type);
let world_aabb = accelerator.aabb();
let span = world_aabb.max - world_aabb.min;
let center: Point3 = world_aabb.min + span / 2.0;
let radius = span.norm() / 2.0;
info!(
"world radius is {:?} meters, world center is at {:?}",
radius, center
);
if lights.is_empty() {
warn!("the world had no lights, so force-setting env_sampling_probability to 1.0");
env_sampling_probability = 1.0;
}
let world = World {
accelerator,
lights,
cameras,
materials,
mediums,
environment,
env_sampling_probability,
radius,
center,
};
if env_sampling_probability == 1.0 || env_sampling_probability == 0.0 {
warn!(
"env sampling probability is at an extrema of {}",
env_sampling_probability
);
}
world
}
pub fn pick_random_light(&self, s: Sample1D) -> Option<(&Instance, PDF<f32, Uniform01>)> {
// currently just uniform sampling
// TODO: change method to take into account the location from which the light is being picked, to allow light trees or other heuristics
// i.e. a projected solid angle * power heuristic and pdf
// maybe use reservoir sampling?
let length = self.lights.len();
if length == 0 {
None
} else {
let x = s.x;
let idx = (length as f32 * x).clamp(0.0, length as f32 - 1.0) as usize;
debug_assert!(
idx < self.lights.len(),
"{}, {}, {}, {}",
x,
length as f32 * x,
idx,
length
);
Some((
self.accelerator.get_primitive(self.lights[idx]),
PDF::from(1.0 / length as f32),
))
}
}
pub fn pick_random_camera(
&self,
s: Sample1D,
) -> Option<(&CameraEnum, usize, PDF<f32, Uniform01>)> {
// currently just uniform sampling
let length = self.cameras.len();
if length == 0 {
None
} else {
let x = s.x;
let idx = (length as f32 * x).clamp(0.0, length as f32 - 1.0) as usize;
debug_assert!(
idx < self.cameras.len(),
"{}, {}, {}, {}",
x,
length as f32 * x,
idx,
length
);
Some((self.get_camera(idx), idx, PDF::from(1.0 / length as f32)))
}
}
pub fn instance_is_light(&self, instance_id: InstanceId) -> bool {
self.lights.contains(&instance_id)
}
pub fn get_material(&self, mat_id: MaterialId) -> &MaterialEnum {
let id: usize = mat_id.into();
&self.materials[id]
}
pub fn get_primitive(&self, index: InstanceId) -> &Instance {
self.accelerator.get_primitive(index)
}
pub fn get_camera(&self, index: usize) -> &CameraEnum {
&self.cameras[index]
}
pub fn hit(&self, r: Ray, t0: f32, t1: f32) -> Option<HitRecord> {
self.accelerator.hit(r, t0, t1)
}
pub fn get_env_sampling_probability(&self) -> f32 {
if !self.lights.is_empty() {
self.env_sampling_probability
} else {
1.0
}
}
#[deprecated]
pub fn assign_cameras(&mut self, _: Vec<CameraEnum>, _: bool) {
unimplemented!()
// // reconfigures the scene's cameras and rebuilds the scene accelerator if specified
// if add_and_rebuild_scene {
// match &mut self.accelerator {
// Accelerator::List { ref mut instances } => {
// for camera in self.cameras.iter() {
// if let Some(camera_surface) = camera.get_surface() {
// println!("removing camera surface {:?}", &camera_surface);
// // instances.remove_item(&camera_surface);
// let maybe_id = instances.binary_search(camera_surface);
// if let Ok(id) = maybe_id {
// instances.remove(id);
// }
// }
// }
// }
// Accelerator::BVH {
// ref mut instances,
// bvh: _,
// } => {
// for camera in self.cameras.iter() {
// if let Some(camera_surface) = camera.get_surface() {
// println!("removing camera surface {:?}", &camera_surface);
// // instances.remove_item(&camera_surface);
// let maybe_id = instances.binary_search(camera_surface);
// if let Ok(id) = maybe_id {
// instances.remove(id);
// }
// }
// }
// }
// }
// }
// self.cameras = cameras;
// if add_and_rebuild_scene {
// match &mut self.accelerator {
// Accelerator::List { ref mut instances } => {
// for (cam_id, cam) in self.cameras.iter().enumerate() {
// if let Some(surface) = cam.get_surface() {
// let mut surface = surface.clone();
// let id = instances.len() as InstanceId;
// surface.instance_id = id;
// surface.material_id = Some(MaterialId::Camera(cam_id as u16));
// println!("adding camera {:?} with id {}", &surface, cam_id);
// instances.push(surface);
// }
// }
// }
// Accelerator::BVH {
// ref mut instances,
// bvh: _,
// } => {
// for (cam_id, cam) in self.cameras.iter().enumerate() {
// if let Some(surface) = cam.get_surface() {
// let mut surface = surface.clone();
// let id = instances.len() as InstanceId;
// surface.instance_id = id;
// surface.material_id = Some(MaterialId::Camera(cam_id as u16));
// println!("adding camera {:?} with id {}", &surface, cam_id);
// instances.push(surface);
// }
// }
// }
// }
// self.accelerator.rebuild();
// }
}
}
impl HasBoundingBox for World {
fn aabb(&self) -> AABB {
self.accelerator.aabb()
}
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::parsing::{config::Config, construct_world};
use super::*;
#[test]
fn test_world_intersection() {
crate::log_test_setup();
let mut default_config = Config::load_default();
let mut handles = Vec::new();
let world = construct_world(
&mut default_config,
PathBuf::from("data/scenes/test_lighting_north.toml"),
&mut handles,
)
.unwrap();
for handle in handles {
let _ = handle.join();
}
let ray = Ray::new(Point3::new(0.0, 0.0, 7.0), -Vec3::Z);
let aabb = world.aabb();
println!("{:?}", aabb);
let maybe_hit = world.hit(ray, 0.0, f32::INFINITY);
assert!(maybe_hit.is_some());
let intersection = maybe_hit.unwrap();
println!("{:?}", intersection);
}
}