ncollide3d/transformation/
convex_hull_utils.rs
1use crate::bounding_volume;
2use crate::math::Point;
3use na::{self, RealField};
4
5pub fn support_point_id<N: RealField + Copy, const D: usize>(
7 direction: &na::SVector<N, D>,
8 points: &[na::Point<N, D>],
9) -> Option<usize> {
10 let mut argmax = None;
11 let _max: N = N::max_value().unwrap();
12 let mut max = -_max;
13
14 for (id, pt) in points.iter().enumerate() {
15 let dot = direction.dot(&pt.coords);
16
17 if dot > max {
18 argmax = Some(id);
19 max = dot;
20 }
21 }
22
23 argmax
24}
25
26pub fn indexed_support_point_id<N: RealField + Copy, const D: usize>(
28 direction: &na::SVector<N, D>,
29 points: &[na::Point<N, D>],
30 idx: &[usize],
31) -> Option<usize> {
32 let mut argmax = None;
33 let _max: N = N::max_value().unwrap();
34 let mut max = -_max;
35
36 for i in idx.iter() {
37 let dot = direction.dot(&points[*i].coords);
38
39 if dot > max {
40 argmax = Some(*i);
41 max = dot;
42 }
43 }
44
45 argmax
46}
47
48pub fn normalize<N: RealField + Copy>(coords: &mut [Point<N>]) -> (Point<N>, N) {
50 let aabb = bounding_volume::local_point_cloud_aabb(&coords[..]);
51 let diag = na::distance(&aabb.mins, &aabb.maxs);
52 let center = aabb.center();
53
54 for c in coords.iter_mut() {
55 *c = (*c + (-center.coords)) / diag;
56 }
57
58 (center, diag)
59}
60
61pub fn denormalize<N: RealField + Copy>(coords: &mut [Point<N>], center: &Point<N>, diag: N) {
63 for c in coords.iter_mut() {
64 *c = *c * diag + center.coords;
65 }
66}