Skip to content

Commit 41eefa8

Browse files
committed
cleanup
1 parent b436a55 commit 41eefa8

File tree

3 files changed

+89
-187
lines changed

3 files changed

+89
-187
lines changed

src/integrators/esdf.rs

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ impl EsdfIntegrator {
3939
updated_blocks: &BTreeSet<BlockIndex<VPS>>,
4040
mut callback: F,
4141
) {
42+
let start = std::time::Instant::now();
43+
4244
let mut dirty_blocks = BTreeSet::new();
4345
let mut propagate_blocks = BTreeSet::new();
4446
let mut sites_indices_to_clear = BTreeSet::new();
@@ -248,6 +250,11 @@ impl EsdfIntegrator {
248250
}
249251
}
250252
}
253+
254+
println!(
255+
"=> ESDF update finished in {:?}",
256+
std::time::Instant::now().duration_since(start)
257+
);
251258
}
252259

253260
fn sweep_block<const VPS: usize>(

src/integrators/esdf_gpu.rs

+4-160
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
1-
use nalgebra::point;
2-
31
use crate::{
42
core::{
5-
index::{BlockIndex, VoxelIndex},
3+
index::BlockIndex,
64
layer::Layer,
75
voxel::{Esdf, EsdfFlags, Tsdf},
86
},
9-
wgpu_utils::{self, GpuPropagate, GpuSweep},
7+
wgpu_utils::{GpuPropagate, GpuSweep},
108
};
119

1210
use std::{collections::BTreeSet, time::Duration};
1311

1412
#[derive(Default)]
1513
pub struct EsdfIntegratorConfig {}
1614

17-
enum OpDir {
18-
XPlus,
19-
XMinus,
20-
YPlus,
21-
YMinus,
22-
ZPlus,
23-
ZMinus,
24-
}
25-
2615
pub struct EsdfIntegrator {
2716
config: EsdfIntegratorConfig,
2817
sweep_cache: GpuSweep,
@@ -42,7 +31,7 @@ impl EsdfIntegrator {
4231
}
4332
}
4433

45-
pub async fn update_blocks<
34+
pub fn update_blocks<
4635
const VPS: usize,
4736
F: FnMut(&str, &Layer<Tsdf, VPS>, &Layer<Esdf, VPS>, &[BlockIndex<VPS>], Duration),
4837
>(
@@ -202,145 +191,11 @@ impl EsdfIntegrator {
202191
}
203192

204193
println!(
205-
"ESDF update finished in {:?}",
194+
"=> ESDF update finished in {:?}",
206195
std::time::Instant::now().duration_since(start)
207196
);
208197
}
209198

210-
fn sweep_block<const VPS: usize>(
211-
dir: OpDir,
212-
index: &BlockIndex<VPS>,
213-
esdf_layer: &mut Layer<Esdf, VPS>,
214-
) {
215-
let (step, order) = match dir {
216-
OpDir::XPlus => (1i32, [2, 1, 0]),
217-
OpDir::XMinus => (-1, [2, 1, 0]),
218-
OpDir::YPlus => (1, [2, 0, 1]),
219-
OpDir::YMinus => (-1, [2, 0, 1]),
220-
OpDir::ZPlus => (1, [1, 0, 2]),
221-
OpDir::ZMinus => (-1, [1, 0, 2]),
222-
};
223-
224-
let voxel_size = esdf_layer.voxel_size();
225-
let mut lock = esdf_layer.block_by_index(index).unwrap().write();
226-
227-
for u in 0..VPS {
228-
for v in 0..VPS {
229-
let w_range = if step > 0 {
230-
create_range_chain(1, VPS - 1)
231-
} else {
232-
create_range_chain(VPS - 2, 0)
233-
};
234-
235-
for w in w_range {
236-
let mut p = point![0, 0, 0];
237-
p[order[0]] = u;
238-
p[order[1]] = v;
239-
p[order[2]] = w;
240-
241-
let voxel_index = VoxelIndex(p);
242-
243-
p[order[2]] = (w as i32 - step) as usize;
244-
let parent_voxel_index = VoxelIndex(p);
245-
246-
let parent_voxel = lock.voxel_from_index(&parent_voxel_index);
247-
let parent_fixed = parent_voxel.flags.contains(EsdfFlags::Fixed);
248-
let parent_dist = parent_voxel.distance;
249-
let parent_site_block_index = parent_voxel.site_block_index;
250-
251-
let voxel = lock.voxel_from_index_mut(&voxel_index);
252-
253-
if parent_fixed && !voxel.flags.contains(EsdfFlags::Observed) {
254-
if !voxel.flags.contains(EsdfFlags::Fixed) {
255-
voxel.distance = parent_dist + voxel_size;
256-
voxel.flags.insert(EsdfFlags::Fixed);
257-
voxel.site_block_index = parent_site_block_index;
258-
} else if voxel.distance > parent_dist + voxel_size {
259-
voxel.distance = voxel.distance.min(parent_dist + voxel_size);
260-
voxel.site_block_index = parent_site_block_index;
261-
}
262-
}
263-
}
264-
}
265-
}
266-
}
267-
268-
fn propagate_to_neighbour<const VPS: usize>(
269-
dir: OpDir,
270-
pivot_index: &BlockIndex<VPS>,
271-
esdf_layer: &mut Layer<Esdf, VPS>,
272-
) -> Option<BlockIndex<VPS>> {
273-
let voxel_size = esdf_layer.voxel_size();
274-
275-
let nblock_index = match dir {
276-
OpDir::XPlus => BlockIndex::new(pivot_index.x + 1, pivot_index.y, pivot_index.z),
277-
OpDir::XMinus => BlockIndex::new(pivot_index.x - 1, pivot_index.y, pivot_index.z),
278-
OpDir::YPlus => BlockIndex::new(pivot_index.x, pivot_index.y + 1, pivot_index.z),
279-
OpDir::YMinus => BlockIndex::new(pivot_index.x, pivot_index.y - 1, pivot_index.z),
280-
OpDir::ZPlus => BlockIndex::new(pivot_index.x, pivot_index.y, pivot_index.z + 1),
281-
OpDir::ZMinus => BlockIndex::new(pivot_index.x, pivot_index.y, pivot_index.z - 1),
282-
};
283-
284-
let (n_index, p_index, order) = match dir {
285-
OpDir::XPlus => (0usize, VPS - 1, [2, 1, 0]),
286-
OpDir::XMinus => (VPS - 1, 0, [2, 1, 0]),
287-
OpDir::YPlus => (0, VPS - 1, [2, 0, 1]),
288-
OpDir::YMinus => (VPS - 1, 0, [2, 0, 1]),
289-
OpDir::ZPlus => (0, VPS - 1, [1, 0, 2]),
290-
OpDir::ZMinus => (VPS - 1, 0, [1, 0, 2]),
291-
};
292-
293-
let mut dirty = false;
294-
295-
if let Some(neighbour_block) = esdf_layer.block_by_index(&nblock_index) {
296-
let pivot_block = esdf_layer.block_by_index(pivot_index).unwrap().read();
297-
298-
let mut nlock = neighbour_block.write();
299-
300-
for u in 0..VPS {
301-
for v in 0..VPS {
302-
let mut p = point![0, 0, 0];
303-
p[order[0]] = u;
304-
p[order[1]] = v;
305-
p[order[2]] = p_index;
306-
307-
let p_voxel_index = VoxelIndex(p);
308-
309-
p[order[2]] = n_index;
310-
let n_voxel_index = VoxelIndex(p);
311-
312-
// propagate through the sides
313-
let pivot_voxel = pivot_block.voxel_from_index(&p_voxel_index);
314-
let pivot_fixed = pivot_voxel.flags.contains(EsdfFlags::Fixed);
315-
let pivot_dist = pivot_voxel.distance;
316-
let pivot_site_block_index = pivot_voxel.site_block_index;
317-
318-
let neighbour_voxel = nlock.voxel_from_index_mut(&n_voxel_index);
319-
let neighbour_fixed = neighbour_voxel.flags.contains(EsdfFlags::Fixed);
320-
let neighbour_observed = neighbour_voxel.flags.contains(EsdfFlags::Observed);
321-
322-
if pivot_fixed && !neighbour_observed {
323-
if neighbour_fixed {
324-
// found a shorter distance?
325-
if neighbour_voxel.distance > pivot_dist + voxel_size {
326-
neighbour_voxel.distance = pivot_dist + voxel_size;
327-
neighbour_voxel.site_block_index = pivot_site_block_index;
328-
dirty = true;
329-
}
330-
} else {
331-
neighbour_voxel.distance = pivot_dist + voxel_size;
332-
neighbour_voxel.flags |= EsdfFlags::Fixed;
333-
neighbour_voxel.site_block_index = pivot_site_block_index;
334-
dirty = true;
335-
}
336-
}
337-
}
338-
}
339-
}
340-
341-
dirty.then_some(nblock_index)
342-
}
343-
344199
fn sweep_gpu<const VPS: usize>(
345200
&mut self,
346201
esdf_layer: &mut Layer<Esdf, VPS>,
@@ -428,14 +283,3 @@ impl EsdfIntegrator {
428283
)
429284
}
430285
}
431-
432-
fn create_range_chain(a: usize, b: usize) -> impl Iterator<Item = usize> {
433-
#[allow(clippy::reversed_empty_ranges)]
434-
let (part1, part2) = if a <= b {
435-
(a..=b, 1..=0)
436-
} else {
437-
(1..=0, b..=a)
438-
};
439-
440-
part1.chain(part2.rev())
441-
}

src/main.rs

+78-27
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ fn main() {
2121
}
2222

2323
async fn run() {
24-
const RENDER: bool = true;
24+
const RENDER: bool = false;
25+
const USE_GPU: bool = true;
2526

2627
let (device, mut queue) = wgpu_utils::create_adapter().await.unwrap();
2728

@@ -35,11 +36,6 @@ async fn run() {
3536
let mut tsdf_integrator = TsdfIntegrator::new(TsdfIntegratorConfig::default());
3637

3738
let mut esdf_layer = EsdfLayer::new(1.0);
38-
let mut esdf_integrator = esdf_gpu::EsdfIntegrator::new(
39-
&device,
40-
&mut queue,
41-
esdf_gpu::EsdfIntegratorConfig::default(),
42-
);
4339

4440
let renderer = std::rc::Rc::new(std::cell::RefCell::new(Renderer::new(false)));
4541

@@ -51,8 +47,15 @@ async fn run() {
5147
{
5248
firestorm::profile_section!(esdf_full_update);
5349
let renderer_cb = renderer.clone();
54-
esdf_integrator
55-
.update_blocks(
50+
51+
if USE_GPU {
52+
let mut esdf_integrator = esdf_gpu::EsdfIntegrator::new(
53+
&device,
54+
&mut queue,
55+
esdf_gpu::EsdfIntegratorConfig::default(),
56+
);
57+
58+
esdf_integrator.update_blocks(
5659
&tsdf_layer,
5760
&mut esdf_layer,
5861
&dirty_blocks,
@@ -69,8 +72,28 @@ async fn run() {
6972
);
7073
}
7174
},
75+
);
76+
} else {
77+
let mut esdf_integrator =
78+
esdf::EsdfIntegrator::new(esdf::EsdfIntegratorConfig::default());
79+
80+
esdf_integrator.update_blocks(
81+
&tsdf_layer,
82+
&mut esdf_layer,
83+
&dirty_blocks,
84+
move |op, tsdf_layer, esdf_layer, block_indices, duration| {
85+
if RENDER {
86+
renderer_cb.borrow_mut().render_tsdf_layer(
87+
tsdf_layer,
88+
esdf_layer,
89+
block_indices,
90+
op,
91+
Some(duration),
92+
);
93+
}
94+
},
7295
)
73-
.await;
96+
}
7497
}
7598

7699
renderer.borrow_mut().render_tsdf_layer(
@@ -94,26 +117,54 @@ async fn run() {
94117
// generate esdf and render on callback
95118
{
96119
firestorm::profile_section!(esdf_partial_update);
120+
let renderer_cb = renderer.clone();
97121

98-
let mut esdf_integrator = esdf::EsdfIntegrator::new(esdf::EsdfIntegratorConfig::default());
122+
if USE_GPU {
123+
let mut esdf_integrator = esdf_gpu::EsdfIntegrator::new(
124+
&device,
125+
&mut queue,
126+
esdf_gpu::EsdfIntegratorConfig::default(),
127+
);
99128

100-
let renderer_cb = renderer.clone();
101-
esdf_integrator.update_blocks(
102-
&tsdf_layer,
103-
&mut esdf_layer,
104-
&dirty_blocks,
105-
move |op, tsdf_layer, esdf_layer, block_indices, duration| {
106-
if RENDER {
107-
renderer_cb.borrow_mut().render_tsdf_layer(
108-
tsdf_layer,
109-
esdf_layer,
110-
block_indices,
111-
op,
112-
Some(duration),
113-
);
114-
}
115-
},
116-
)
129+
esdf_integrator.update_blocks(
130+
&tsdf_layer,
131+
&mut esdf_layer,
132+
&dirty_blocks,
133+
&device,
134+
&mut queue,
135+
move |op, tsdf_layer, esdf_layer, block_indices, duration| {
136+
if RENDER {
137+
renderer_cb.borrow_mut().render_tsdf_layer(
138+
tsdf_layer,
139+
esdf_layer,
140+
block_indices,
141+
op,
142+
Some(duration),
143+
);
144+
}
145+
},
146+
);
147+
} else {
148+
let mut esdf_integrator =
149+
esdf::EsdfIntegrator::new(esdf::EsdfIntegratorConfig::default());
150+
151+
esdf_integrator.update_blocks(
152+
&tsdf_layer,
153+
&mut esdf_layer,
154+
&dirty_blocks,
155+
move |op, tsdf_layer, esdf_layer, block_indices, duration| {
156+
if RENDER {
157+
renderer_cb.borrow_mut().render_tsdf_layer(
158+
tsdf_layer,
159+
esdf_layer,
160+
block_indices,
161+
op,
162+
Some(duration),
163+
);
164+
}
165+
},
166+
)
167+
}
117168
}
118169

119170
renderer.borrow_mut().render_tsdf_layer(

0 commit comments

Comments
 (0)