1use crate::device::workgroup_grid;
4use crate::spatial::{GpuKdTree, KD_TREE_WGSL};
5use crate::GpuContext;
6use threecrate_core::{Point3f, PointCloud, Result};
7
8fn radius_outlier_shader() -> String {
12 format!(
13 "{}{}",
14 KD_TREE_WGSL,
15 r#"
16struct RadiusOutlierParams {
17 num_nodes: u32,
18 radius_sq: f32,
19 min_neighbors: u32,
20 _padding: u32,
21}
22
23@group(0) @binding(1) var<storage, read_write> is_outlier: array<u32>;
24@group(0) @binding(2) var<uniform> params: RadiusOutlierParams;
25
26@compute @workgroup_size(64)
27fn radius_outlier(@builtin(global_invocation_id) gid: vec3<u32>, @builtin(num_workgroups) nwg: vec3<u32>) {
28 let node_id = gid.x + gid.y * nwg.x * 64u;
29 if (node_id >= params.num_nodes) {
30 return;
31 }
32 let count = count_within(
33 nodes[node_id].pos,
34 params.radius_sq,
35 node_id,
36 params.min_neighbors,
37 );
38 is_outlier[nodes[node_id].index] = select(0u, 1u, count < params.min_neighbors);
39}
40"#
41 )
42}
43
44impl GpuContext {
45 pub async fn remove_statistical_outliers(
49 &self,
50 points: &[Point3f],
51 k_neighbors: usize,
52 std_dev_multiplier: f32,
53 ) -> Result<Vec<Point3f>> {
54 let cloud = PointCloud::from_points(points.to_vec());
55 let kept = threecrate_algorithms::statistical_outlier_removal(
56 &cloud,
57 k_neighbors,
58 std_dev_multiplier,
59 )?;
60 Ok(kept.points)
61 }
62
63 pub async fn remove_radius_outliers(
69 &self,
70 points: &[Point3f],
71 radius: f32,
72 min_neighbors: usize,
73 ) -> Result<Vec<Point3f>> {
74 if points.is_empty() {
75 return Ok(Vec::new());
76 }
77
78 let tree = GpuKdTree::new(self, points)?;
79 let initial_flag = (min_neighbors > 0) as u32;
82 let outlier_buffer = self.create_buffer_init(
83 "Outlier Flags",
84 &vec![initial_flag; points.len()],
85 wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
86 );
87
88 if !tree.is_empty() {
89 #[repr(C)]
90 #[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
91 struct RadiusOutlierParams {
92 num_nodes: u32,
93 radius_sq: f32,
94 min_neighbors: u32,
95 _padding: u32,
96 }
97 let params = RadiusOutlierParams {
98 num_nodes: tree.node_to_index.len() as u32,
99 radius_sq: if radius < 0.0 { -1.0 } else { radius * radius },
102 min_neighbors: min_neighbors.min(u32::MAX as usize) as u32,
103 _padding: 0,
104 };
105 let params_buffer = self.create_buffer_init(
106 "Radius Outlier Params",
107 &[params],
108 wgpu::BufferUsages::UNIFORM,
109 );
110
111 let pipeline = self.cached_pipeline(
112 &self.pipelines.radius_outlier,
113 "Radius Outlier Filter",
114 radius_outlier_shader,
115 "radius_outlier",
116 );
117 let bind_group = self.create_bind_group(
118 "Radius Outlier Filter",
119 &pipeline.get_bind_group_layout(0),
120 &[
121 wgpu::BindGroupEntry {
122 binding: 0,
123 resource: tree.nodes.as_entire_binding(),
124 },
125 wgpu::BindGroupEntry {
126 binding: 1,
127 resource: outlier_buffer.as_entire_binding(),
128 },
129 wgpu::BindGroupEntry {
130 binding: 2,
131 resource: params_buffer.as_entire_binding(),
132 },
133 ],
134 );
135
136 let mut encoder = self
137 .device
138 .create_command_encoder(&wgpu::CommandEncoderDescriptor {
139 label: Some("Radius Outlier Filter"),
140 });
141 {
142 let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
143 label: Some("Radius Outlier Filter"),
144 timestamp_writes: None,
145 });
146 pass.set_pipeline(pipeline);
147 pass.set_bind_group(0, &bind_group, &[]);
148 let (x, y) = workgroup_grid(tree.node_to_index.len().div_ceil(64));
149 pass.dispatch_workgroups(x, y, 1);
150 }
151 self.queue.submit(std::iter::once(encoder.finish()));
152 }
153
154 let outlier_flags: Vec<u32> = self.read_buffer(&outlier_buffer)?;
155 Ok(points
156 .iter()
157 .zip(&outlier_flags)
158 .filter(|&(_, &is_outlier)| is_outlier == 0)
159 .map(|(point, _)| *point)
160 .collect())
161 }
162
163 pub async fn voxel_grid_filter(
167 &self,
168 points: &[Point3f],
169 voxel_size: f32,
170 ) -> Result<Vec<Point3f>> {
171 let cloud = PointCloud::from_points(points.to_vec());
172 Ok(threecrate_algorithms::voxel_grid_filter(&cloud, voxel_size)?.points)
173 }
174}
175
176pub async fn gpu_remove_statistical_outliers(
178 gpu_context: &GpuContext,
179 cloud: &PointCloud<Point3f>,
180 k_neighbors: usize,
181 std_dev_multiplier: f32,
182) -> Result<PointCloud<Point3f>> {
183 let filtered_points = gpu_context
184 .remove_statistical_outliers(&cloud.points, k_neighbors, std_dev_multiplier)
185 .await?;
186 Ok(PointCloud::from_points(filtered_points))
187}
188
189pub async fn gpu_radius_outlier_removal(
191 gpu_context: &GpuContext,
192 cloud: &PointCloud<Point3f>,
193 radius: f32,
194 min_neighbors: usize,
195) -> Result<PointCloud<Point3f>> {
196 let filtered_points = gpu_context
197 .remove_radius_outliers(&cloud.points, radius, min_neighbors)
198 .await?;
199 Ok(PointCloud::from_points(filtered_points))
200}
201
202pub async fn gpu_voxel_grid_filter(
204 gpu_context: &GpuContext,
205 cloud: &PointCloud<Point3f>,
206 voxel_size: f32,
207) -> Result<PointCloud<Point3f>> {
208 let filtered_points = gpu_context
209 .voxel_grid_filter(&cloud.points, voxel_size)
210 .await?;
211 Ok(PointCloud::from_points(filtered_points))
212}
213
214#[cfg(test)]
215mod tests {
216 use super::*;
217 use std::time::Instant;
218 use threecrate_core::{Point3f, PointCloud};
219
220 async fn try_create_gpu_context() -> Option<GpuContext> {
221 GpuContext::new().await.ok()
222 }
223
224 fn create_test_point_cloud_with_outliers() -> PointCloud<Point3f> {
225 let mut points = Vec::new();
226
227 for i in 0..50 {
229 for j in 0..50 {
230 let x = (i as f32 - 25.0) * 0.1;
231 let y = (j as f32 - 25.0) * 0.1;
232 let z = 0.0;
233 points.push(Point3f::new(x, y, z));
234 }
235 }
236
237 for i in 0..20 {
239 let x = 50.0 + (i as f32 * 2.0);
240 let y = 50.0 + (i as f32 * 2.0);
241 let z = 50.0 + (i as f32 * 2.0);
242 points.push(Point3f::new(x, y, z));
243 }
244
245 PointCloud::from_points(points)
246 }
247
248 fn create_test_point_cloud_no_outliers() -> PointCloud<Point3f> {
249 let mut points = Vec::new();
250
251 for i in 0..25 {
253 for j in 0..25 {
254 for k in 0..5 {
255 let x = (i as f32 - 12.5) * 0.1;
256 let y = (j as f32 - 12.5) * 0.1;
257 let z = (k as f32 - 2.5) * 0.1;
258 points.push(Point3f::new(x, y, z));
259 }
260 }
261 }
262
263 PointCloud::from_points(points)
264 }
265
266 #[tokio::test]
269 async fn test_gpu_filters_match_cpu_on_216_cases() {
270 let Some(gpu_context) = try_create_gpu_context().await else {
271 println!("Skipping GPU test - no GPU available");
272 return;
273 };
274 let mut grid: Vec<Point3f> = (0..400)
275 .map(|i| Point3f::new((i % 20) as f32, (i / 20) as f32, 0.0))
276 .collect();
277 grid.push(Point3f::new(100.0, 100.0, 100.0));
278 let grid = PointCloud::from_points(grid);
279 for k in [8, 10, 64] {
280 let gpu = gpu_remove_statistical_outliers(&gpu_context, &grid, k, 1.0)
281 .await
282 .unwrap();
283 let cpu = threecrate_algorithms::statistical_outlier_removal(&grid, k, 1.0).unwrap();
284 assert_eq!(gpu.len(), cpu.len(), "k = {k}");
285 assert_eq!(gpu.len(), 400, "k = {k}: only the far point should go");
286 }
287
288 let line =
289 PointCloud::from_points((0..100).map(|x| Point3f::new(x as f32, 0.0, 0.0)).collect());
290 let kept = gpu_voxel_grid_filter(&gpu_context, &line, 0.5)
291 .await
292 .unwrap();
293 assert_eq!(kept.len(), 100);
294 }
295
296 #[tokio::test]
297 async fn test_gpu_statistical_outlier_removal_basic() {
298 let gpu_context = match try_create_gpu_context().await {
299 Some(ctx) => ctx,
300 None => {
301 println!("Skipping GPU test - no GPU available");
302 return;
303 }
304 };
305
306 let cloud = create_test_point_cloud_with_outliers();
307 let original_count = cloud.len();
308
309 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 10, 1.0)
310 .await
311 .unwrap();
312 let filtered_count = filtered.len();
313
314 assert!(filtered_count < original_count);
316 assert!(filtered_count > 0);
317
318 println!(
319 "GPU outlier removal: {} -> {} points",
320 original_count, filtered_count
321 );
322 }
323
324 #[tokio::test]
325 async fn test_gpu_statistical_outlier_removal_no_outliers() {
326 let gpu_context = match try_create_gpu_context().await {
327 Some(ctx) => ctx,
328 None => {
329 println!("Skipping GPU test - no GPU available");
330 return;
331 }
332 };
333
334 let cloud = create_test_point_cloud_no_outliers();
335 let original_count = cloud.len();
336
337 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 10, 1.0)
338 .await
339 .unwrap();
340 let filtered_count = filtered.len();
341
342 println!(
343 "GPU outlier removal (no outliers): {} -> {} points",
344 original_count, filtered_count
345 );
346
347 assert!(filtered_count > 0);
350 assert!(filtered_count < original_count);
351 }
352
353 #[tokio::test]
354 async fn test_gpu_statistical_outlier_removal_empty_cloud() {
355 let gpu_context = match try_create_gpu_context().await {
356 Some(ctx) => ctx,
357 None => {
358 println!("Skipping GPU test - no GPU available");
359 return;
360 }
361 };
362
363 let cloud = PointCloud::<Point3f>::new();
364 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 10, 1.0)
365 .await
366 .unwrap();
367
368 assert_eq!(filtered.len(), 0);
369 }
370
371 #[tokio::test]
372 async fn test_gpu_statistical_outlier_removal_single_point() {
373 let gpu_context = match try_create_gpu_context().await {
374 Some(ctx) => ctx,
375 None => {
376 println!("Skipping GPU test - no GPU available");
377 return;
378 }
379 };
380
381 let cloud = PointCloud::from_points(vec![Point3f::new(0.0, 0.0, 0.0)]);
382 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 1, 1.0)
383 .await
384 .unwrap();
385
386 assert_eq!(filtered.len(), 1);
388 }
389
390 #[tokio::test]
391 async fn test_gpu_vs_cpu_statistical_outlier_removal() {
392 let gpu_context = match try_create_gpu_context().await {
393 Some(ctx) => ctx,
394 None => {
395 println!("Skipping GPU vs CPU test - no GPU available");
396 return;
397 }
398 };
399
400 let cloud = create_test_point_cloud_with_outliers();
401
402 let cpu_start = Instant::now();
404 let cpu_filtered_count = {
405 let points = &cloud.points;
407 let k = 10;
408 let std_dev_multiplier = 1.0;
409
410 if points.is_empty() {
411 0
412 } else {
413 let mut mean_distances = Vec::new();
415 for point in points {
416 let mut distances = Vec::new();
417 for other_point in points {
418 if other_point != point {
419 let dx = point.x - other_point.x;
420 let dy = point.y - other_point.y;
421 let dz = point.z - other_point.z;
422 let distance = (dx * dx + dy * dy + dz * dz).sqrt();
423 distances.push(distance);
424 }
425 }
426 distances.sort_by(|a, b| a.partial_cmp(b).unwrap());
427 let k_neighbors = k.min(distances.len());
428 if k_neighbors > 0 {
429 let mean =
430 distances[..k_neighbors].iter().sum::<f32>() / k_neighbors as f32;
431 mean_distances.push(mean);
432 } else {
433 mean_distances.push(0.0);
434 }
435 }
436
437 let global_mean = mean_distances.iter().sum::<f32>() / mean_distances.len() as f32;
438 let variance = mean_distances
439 .iter()
440 .map(|&d| (d - global_mean).powi(2))
441 .sum::<f32>()
442 / mean_distances.len() as f32;
443 let global_std_dev = variance.sqrt();
444 let threshold = global_mean + std_dev_multiplier * global_std_dev;
445
446 mean_distances.iter().filter(|&&d| d <= threshold).count()
447 }
448 };
449 let cpu_time = cpu_start.elapsed();
450
451 let gpu_start = Instant::now();
453 let gpu_filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 10, 1.0)
454 .await
455 .unwrap();
456 let gpu_time = gpu_start.elapsed();
457
458 println!(
459 "CPU outlier removal: {} -> {} points in {:?}",
460 cloud.len(),
461 cpu_filtered_count,
462 cpu_time
463 );
464 println!(
465 "GPU outlier removal: {} -> {} points in {:?}",
466 cloud.len(),
467 gpu_filtered.len(),
468 gpu_time
469 );
470
471 assert!(cpu_filtered_count < cloud.len());
473 assert!(gpu_filtered.len() < cloud.len());
474
475 if cloud.len() > 1000 {
477 println!(
478 "GPU speedup: {:.2}x",
479 cpu_time.as_secs_f32() / gpu_time.as_secs_f32()
480 );
481 }
482 }
483
484 #[tokio::test]
485 async fn test_gpu_statistical_outlier_removal_different_parameters() {
486 let gpu_context = match try_create_gpu_context().await {
487 Some(ctx) => ctx,
488 None => {
489 println!("Skipping GPU test - no GPU available");
490 return;
491 }
492 };
493
494 let cloud = create_test_point_cloud_with_outliers();
495 let original_count = cloud.len();
496
497 for k in [5, 10, 20] {
499 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, k, 1.0)
500 .await
501 .unwrap();
502 println!("k={}: {} -> {} points", k, original_count, filtered.len());
503 assert!(filtered.len() > 0);
504 }
505
506 for std_dev in [0.5, 1.0, 2.0] {
508 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 10, std_dev)
509 .await
510 .unwrap();
511 println!(
512 "std_dev={}: {} -> {} points",
513 std_dev,
514 original_count,
515 filtered.len()
516 );
517 assert!(filtered.len() > 0);
518 }
519 }
520
521 #[tokio::test]
522 async fn test_gpu_statistical_outlier_removal_large_dataset() {
523 let gpu_context = match try_create_gpu_context().await {
524 Some(ctx) => ctx,
525 None => {
526 println!("Skipping GPU test - no GPU available");
527 return;
528 }
529 };
530
531 let mut points = Vec::new();
533 for i in 0..50 {
534 for j in 0..50 {
535 let x = (i as f32 - 25.0) * 0.1;
536 let y = (j as f32 - 25.0) * 0.1;
537 let z = 0.0;
538 points.push(Point3f::new(x, y, z));
539 }
540 }
541
542 for i in 0..10 {
544 let x = 25.0 + (i as f32 * 2.0);
545 let y = 25.0 + (i as f32 * 2.0);
546 let z = 25.0 + (i as f32 * 2.0);
547 points.push(Point3f::new(x, y, z));
548 }
549
550 let cloud = PointCloud::from_points(points);
551 let original_count = cloud.len();
552
553 let start = Instant::now();
554 let filtered = gpu_remove_statistical_outliers(&gpu_context, &cloud, 5, 1.0)
555 .await
556 .unwrap();
557 let elapsed = start.elapsed();
558
559 println!(
560 "Large dataset GPU outlier removal: {} -> {} points in {:?}",
561 original_count,
562 filtered.len(),
563 elapsed
564 );
565
566 assert!(filtered.len() < original_count);
567 assert!(filtered.len() > 0);
568
569 assert!(elapsed.as_secs() < 10);
571 }
572
573 fn create_test_point_cloud_for_radius_outlier() -> PointCloud<Point3f> {
574 let mut points = Vec::new();
575
576 for i in 0..20 {
578 for j in 0..20 {
579 let x = (i as f32 - 10.0) * 0.1;
580 let y = (j as f32 - 10.0) * 0.1;
581 let z = 0.0;
582 points.push(Point3f::new(x, y, z));
583 }
584 }
585
586 for i in 0..5 {
588 let x = 10.0 + (i as f32 * 2.0);
589 let y = 10.0 + (i as f32 * 2.0);
590 let z = 10.0 + (i as f32 * 2.0);
591 points.push(Point3f::new(x, y, z));
592 }
593
594 PointCloud::from_points(points)
595 }
596
597 #[tokio::test]
598 async fn test_gpu_radius_outlier_removal_matches_cpu() {
599 let Some(gpu_context) = try_create_gpu_context().await else {
600 return;
601 };
602
603 let mut state = 7u32;
605 let mut next = || {
606 state = state.wrapping_mul(1664525).wrapping_add(1013904223);
607 (state >> 8) as f32 / (1u32 << 24) as f32
608 };
609 let mut cloud = PointCloud::new();
610 for _ in 0..15_000 {
611 cloud.push(Point3f::new(next(), next(), 0.1 * next()));
612 }
613 for _ in 0..3_000 {
614 cloud.push(Point3f::new(2.0 + 3.0 * next(), 3.0 * next(), next()));
615 }
616 cloud.push(Point3f::new(f32::NAN, 0.0, 0.0));
617
618 let gpu = gpu_radius_outlier_removal(&gpu_context, &cloud, 0.05, 4)
619 .await
620 .unwrap();
621 let finite = PointCloud::from_points(
622 cloud
623 .points
624 .iter()
625 .copied()
626 .filter(|p| p.coords.iter().all(|c| c.is_finite()))
627 .collect(),
628 );
629 let cpu = threecrate_algorithms::radius_outlier_removal(&finite, 0.05, 4).unwrap();
630
631 assert_eq!(gpu.len(), cpu.len());
632 assert!(gpu.points.iter().zip(&cpu.points).all(|(g, c)| g == c));
633 }
634
635 #[tokio::test]
636 async fn test_gpu_radius_outlier_removal_negative_radius() {
637 let Some(gpu_context) = try_create_gpu_context().await else {
638 return;
639 };
640 let cloud = PointCloud::from_points(
641 (0..50)
642 .map(|i| Point3f::new(i as f32 * 0.01, 0.0, 0.0))
643 .collect(),
644 );
645 let filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, -0.2, 3)
647 .await
648 .unwrap();
649 assert!(filtered.is_empty());
650 }
651
652 #[tokio::test]
653 async fn test_gpu_radius_outlier_removal_basic() {
654 let gpu_context = match try_create_gpu_context().await {
655 Some(ctx) => ctx,
656 None => {
657 println!("Skipping GPU test - no GPU available");
658 return;
659 }
660 };
661
662 let cloud = create_test_point_cloud_for_radius_outlier();
663 let original_count = cloud.len();
664
665 let filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, 0.5, 3)
666 .await
667 .unwrap();
668 let filtered_count = filtered.len();
669
670 assert!(filtered_count < original_count);
672 assert!(filtered_count > 0);
673
674 println!(
675 "GPU radius outlier removal: {} -> {} points",
676 original_count, filtered_count
677 );
678 }
679
680 #[tokio::test]
681 async fn test_gpu_radius_outlier_removal_empty_cloud() {
682 let gpu_context = match try_create_gpu_context().await {
683 Some(ctx) => ctx,
684 None => {
685 println!("Skipping GPU test - no GPU available");
686 return;
687 }
688 };
689
690 let cloud = PointCloud::<Point3f>::new();
691 let filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, 0.5, 3)
692 .await
693 .unwrap();
694
695 assert_eq!(filtered.len(), 0);
696 }
697
698 #[tokio::test]
699 async fn test_gpu_radius_outlier_removal_single_point() {
700 let gpu_context = match try_create_gpu_context().await {
701 Some(ctx) => ctx,
702 None => {
703 println!("Skipping GPU test - no GPU available");
704 return;
705 }
706 };
707
708 let cloud = PointCloud::from_points(vec![Point3f::new(0.0, 0.0, 0.0)]);
709 let filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, 0.5, 1)
710 .await
711 .unwrap();
712
713 assert_eq!(filtered.len(), 0);
715 }
716
717 #[tokio::test]
718 async fn test_gpu_radius_outlier_removal_different_parameters() {
719 let gpu_context = match try_create_gpu_context().await {
720 Some(ctx) => ctx,
721 None => {
722 println!("Skipping GPU test - no GPU available");
723 return;
724 }
725 };
726
727 let cloud = create_test_point_cloud_for_radius_outlier();
728 let original_count = cloud.len();
729
730 for radius in [0.2, 0.5, 1.0] {
732 let filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, radius, 3)
733 .await
734 .unwrap();
735 println!(
736 "radius={}: {} -> {} points",
737 radius,
738 original_count,
739 filtered.len()
740 );
741 assert!(filtered.len() > 0);
742 }
743
744 for min_neighbors in [1, 3, 5] {
746 let filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, 0.5, min_neighbors)
747 .await
748 .unwrap();
749 println!(
750 "min_neighbors={}: {} -> {} points",
751 min_neighbors,
752 original_count,
753 filtered.len()
754 );
755 assert!(filtered.len() > 0);
756 }
757 }
758
759 fn create_test_point_cloud_for_voxel_grid() -> PointCloud<Point3f> {
760 let mut points = Vec::new();
761
762 for i in 0..10 {
764 for j in 0..10 {
765 for k in 0..5 {
766 let x = (i as f32 - 5.0) * 0.1;
767 let y = (j as f32 - 5.0) * 0.1;
768 let z = (k as f32 - 2.5) * 0.1;
769 points.push(Point3f::new(x, y, z));
770 }
771 }
772 }
773
774 for i in 0..5 {
776 for j in 0..5 {
777 let x = (i as f32 - 2.5) * 0.1;
778 let y = (j as f32 - 2.5) * 0.1;
779 let z = 0.0;
780 points.push(Point3f::new(x, y, z));
781 points.push(Point3f::new(x, y, z)); }
783 }
784
785 PointCloud::from_points(points)
786 }
787
788 #[tokio::test]
789 async fn test_gpu_voxel_grid_filter_basic() {
790 let gpu_context = match try_create_gpu_context().await {
791 Some(ctx) => ctx,
792 None => {
793 println!("Skipping GPU test - no GPU available");
794 return;
795 }
796 };
797
798 let cloud = create_test_point_cloud_for_voxel_grid();
799 let original_count = cloud.len();
800
801 let filtered = gpu_voxel_grid_filter(&gpu_context, &cloud, 0.1)
802 .await
803 .unwrap();
804 let filtered_count = filtered.len();
805
806 assert!(filtered_count < original_count);
808 assert!(filtered_count > 0);
809
810 println!(
811 "GPU voxel grid filter: {} -> {} points",
812 original_count, filtered_count
813 );
814 }
815
816 #[tokio::test]
817 async fn test_gpu_voxel_grid_filter_empty_cloud() {
818 let gpu_context = match try_create_gpu_context().await {
819 Some(ctx) => ctx,
820 None => {
821 println!("Skipping GPU test - no GPU available");
822 return;
823 }
824 };
825
826 let cloud = PointCloud::<Point3f>::new();
827 let filtered = gpu_voxel_grid_filter(&gpu_context, &cloud, 0.1)
828 .await
829 .unwrap();
830
831 assert_eq!(filtered.len(), 0);
832 }
833
834 #[tokio::test]
835 async fn test_gpu_voxel_grid_filter_single_point() {
836 let gpu_context = match try_create_gpu_context().await {
837 Some(ctx) => ctx,
838 None => {
839 println!("Skipping GPU test - no GPU available");
840 return;
841 }
842 };
843
844 let cloud = PointCloud::from_points(vec![Point3f::new(0.0, 0.0, 0.0)]);
845 let filtered = gpu_voxel_grid_filter(&gpu_context, &cloud, 0.1)
846 .await
847 .unwrap();
848
849 assert_eq!(filtered.len(), 1);
851 }
852
853 #[tokio::test]
854 async fn test_gpu_voxel_grid_filter_different_voxel_sizes() {
855 let gpu_context = match try_create_gpu_context().await {
856 Some(ctx) => ctx,
857 None => {
858 println!("Skipping GPU test - no GPU available");
859 return;
860 }
861 };
862
863 let cloud = create_test_point_cloud_for_voxel_grid();
864 let original_count = cloud.len();
865
866 for voxel_size in [0.05, 0.1, 0.2] {
868 let filtered = gpu_voxel_grid_filter(&gpu_context, &cloud, voxel_size)
869 .await
870 .unwrap();
871 println!(
872 "voxel_size={}: {} -> {} points",
873 voxel_size,
874 original_count,
875 filtered.len()
876 );
877 assert!(filtered.len() > 0);
878 assert!(filtered.len() <= original_count);
879 }
880 }
881
882 #[tokio::test]
883 async fn test_gpu_voxel_grid_filter_invalid_voxel_size() {
884 let gpu_context = match try_create_gpu_context().await {
885 Some(ctx) => ctx,
886 None => {
887 println!("Skipping GPU test - no GPU available");
888 return;
889 }
890 };
891
892 let cloud = PointCloud::from_points(vec![Point3f::new(0.0, 0.0, 0.0)]);
893 let result = gpu_voxel_grid_filter(&gpu_context, &cloud, 0.0).await;
894 assert!(result.is_err());
895
896 let result = gpu_voxel_grid_filter(&gpu_context, &cloud, -1.0).await;
897 assert!(result.is_err());
898 }
899
900 #[tokio::test]
901 async fn test_gpu_vs_cpu_performance_comparison() {
902 let gpu_context = match try_create_gpu_context().await {
903 Some(ctx) => ctx,
904 None => {
905 println!("Skipping GPU vs CPU test - no GPU available");
906 return;
907 }
908 };
909
910 let cloud = create_test_point_cloud_for_radius_outlier();
911
912 let gpu_start = Instant::now();
914 let gpu_filtered = gpu_radius_outlier_removal(&gpu_context, &cloud, 0.5, 3)
915 .await
916 .unwrap();
917 let gpu_time = gpu_start.elapsed();
918
919 let gpu_voxel_start = Instant::now();
921 let gpu_voxel_filtered = gpu_voxel_grid_filter(&gpu_context, &cloud, 0.1)
922 .await
923 .unwrap();
924 let gpu_voxel_time = gpu_voxel_start.elapsed();
925
926 println!(
927 "GPU radius outlier removal: {} -> {} points in {:?}",
928 cloud.len(),
929 gpu_filtered.len(),
930 gpu_time
931 );
932 println!(
933 "GPU voxel grid filter: {} -> {} points in {:?}",
934 cloud.len(),
935 gpu_voxel_filtered.len(),
936 gpu_voxel_time
937 );
938
939 assert!(gpu_filtered.len() > 0);
941 assert!(gpu_voxel_filtered.len() > 0);
942
943 assert!(gpu_time.as_secs() < 10);
945 assert!(gpu_voxel_time.as_secs() < 10);
946 }
947}