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
use na::{RealField, Unit};
use crate::interpolation::RigidMotion;
use crate::math::{Isometry, Point, Vector};
use crate::query::{self, ClosestPoints, TOIStatus, TOI};
use crate::shape::SupportMap;
use crate::utils::IsometryOps;
pub fn nonlinear_time_of_impact_support_map_support_map<N, G1: ?Sized, G2: ?Sized>(
motion1: &(impl RigidMotion<N> + ?Sized),
g1: &G1,
motion2: &(impl RigidMotion<N> + ?Sized),
g2: &G2,
max_toi: N,
target_distance: N,
) -> Option<TOI<N>>
where
N: RealField,
G1: SupportMap<N>,
G2: SupportMap<N>,
{
nonlinear_time_of_impact_support_map_support_map_with_closest_points_function(
motion1,
g1,
motion2,
g2,
max_toi,
target_distance,
query::closest_points_support_map_support_map,
)
}
pub fn nonlinear_time_of_impact_support_map_support_map_with_closest_points_function<
N,
G1: ?Sized,
G2: ?Sized,
>(
motion1: &(impl RigidMotion<N> + ?Sized),
g1: &G1,
motion2: &(impl RigidMotion<N> + ?Sized),
g2: &G2,
max_toi: N,
target_distance: N,
closest_points: impl Fn(&Isometry<N>, &G1, &Isometry<N>, &G2, N) -> ClosestPoints<N>,
) -> Option<TOI<N>>
where
N: RealField,
G1: SupportMap<N>,
G2: SupportMap<N>,
{
let _0_5 = na::convert(0.5);
let mut min_t = N::zero();
let mut prev_min_t = min_t;
let abs_tol: N = query::algorithms::gjk::eps_tol();
let rel_tol = abs_tol.sqrt();
let mut result = TOI {
toi: N::zero(),
normal1: Vector::x_axis(),
normal2: Vector::x_axis(),
witness1: Point::origin(),
witness2: Point::origin(),
status: TOIStatus::Penetrating,
};
loop {
let pos1 = motion1.position_at_time(result.toi);
let pos2 = motion2.position_at_time(result.toi);
match closest_points(&pos1, g1, &pos2, g2, N::max_value()) {
ClosestPoints::Intersecting => {
if result.toi == N::zero() {
result.status = TOIStatus::Penetrating
} else {
result.status = TOIStatus::Failed;
}
break;
}
ClosestPoints::WithinMargin(p1, p2) => {
result.witness1 = pos1.inverse_transform_point(&p1);
result.witness2 = pos2.inverse_transform_point(&p2);
if let Some((dir, mut dist)) = Unit::try_new_and_get(p2 - p1, N::default_epsilon())
{
result.normal1 = pos1.inverse_transform_unit_vector(&dir);
result.normal2 = pos2.inverse_transform_unit_vector(&-dir);
let mut niter = 0;
min_t = result.toi;
let mut max_t = max_toi;
let min_target_distance = (target_distance - rel_tol).max(N::zero());
let max_target_distance = target_distance + rel_tol;
loop {
if dist < min_target_distance {
max_t = result.toi;
result.toi = (min_t + result.toi) * _0_5;
} else if dist > max_target_distance {
min_t = result.toi;
result.toi = (result.toi + max_t) * _0_5;
} else {
break;
}
if max_t - min_t < abs_tol {
result.toi = min_t;
break;
}
let pos1 = motion1.position_at_time(result.toi);
let pos2 = motion2.position_at_time(result.toi);
let pt1 = g1.support_point_toward(&pos1, &dir);
let pt2 = g2.support_point_toward(&pos2, &-dir);
dist = (pt2 - pt1).dot(&dir);
niter += 1;
}
if min_t - prev_min_t < abs_tol {
if max_t == max_toi {
let pos1 = motion1.position_at_time(max_t);
let pos2 = motion2.position_at_time(max_t);
let pt1 = g1.support_point_toward(&pos1, &dir);
let pt2 = g2.support_point_toward(&pos2, &-dir);
if (pt2 - pt1).dot(&dir) > target_distance {
return None;
}
}
result.status = TOIStatus::Converged;
break;
}
prev_min_t = min_t;
if niter == 0 {
result.status = TOIStatus::Converged;
break;
}
} else {
result.status = TOIStatus::Failed;
break;
}
}
ClosestPoints::Disjoint => unreachable!(),
}
}
Some(result)
}