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
use na::{ComplexField, Unit};
use crate::math::{Isometry, Point, Real, Vector};
use crate::motion::RigidMotion;
use crate::query::{self, ClosestPoints, TOIStatus, TOI};
use crate::shape::SupportMap;
use num::Bounded;
pub fn nonlinear_time_of_impact_support_map_support_map<G1: ?Sized, G2: ?Sized>(
motion12: &(impl RigidMotion + ?Sized),
g1: &G1,
g2: &G2,
max_toi: Real,
target_distance: Real,
) -> Option<TOI>
where
G1: SupportMap,
G2: SupportMap,
{
nonlinear_time_of_impact_support_map_support_map_with_closest_points_function(
motion12,
g1,
g2,
max_toi,
target_distance,
query::details::closest_points_support_map_support_map,
)
}
pub fn nonlinear_time_of_impact_support_map_support_map_with_closest_points_function<
G1: ?Sized,
G2: ?Sized,
>(
motion12: &(impl RigidMotion + ?Sized),
g1: &G1,
g2: &G2,
max_toi: Real,
target_distance: Real,
closest_points: impl Fn(&Isometry<Real>, &G1, &G2, Real) -> ClosestPoints,
) -> Option<TOI>
where
G1: SupportMap,
G2: SupportMap,
{
let _0_5: Real = 0.5;
let mut min_t = 0.0;
let mut prev_min_t = min_t;
let abs_tol: Real = query::gjk::eps_tol();
let rel_tol = ComplexField::sqrt(abs_tol);
let mut result = TOI {
toi: 0.0,
normal1: Vector::<Real>::x_axis(),
normal2: Vector::<Real>::x_axis(),
witness1: Point::<Real>::origin(),
witness2: Point::<Real>::origin(),
status: TOIStatus::Penetrating,
};
loop {
let pos12 = motion12.position_at_time(result.toi);
match closest_points(&pos12, g1, g2, Real::max_value()) {
ClosestPoints::Intersecting => {
if result.toi == 0.0 {
result.status = TOIStatus::Penetrating
} else {
result.status = TOIStatus::Failed;
}
break;
}
ClosestPoints::WithinMargin(p1, p2) => {
result.witness1 = p1;
result.witness2 = p2;
if let Some((dir, mut dist)) =
Unit::try_new_and_get(pos12 * p2 - p1, crate::math::DEFAULT_EPSILON)
{
result.normal1 = dir;
result.normal2 = pos12.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(0.0);
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 pos12 = motion12.position_at_time(result.toi);
let pt1 = g1.local_support_point_toward(&dir);
let pt2 = g2.support_point_toward(&pos12, &-dir);
dist = (pt2 - pt1).dot(&dir);
niter += 1;
}
if min_t - prev_min_t < abs_tol {
if max_t == max_toi {
let pos12 = motion12.position_at_time(max_t);
let pt1 = g1.local_support_point_toward(&dir);
let pt2 = g2.support_point_toward(&pos12, &-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)
}