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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use std::cmp::Ordering;
use argmin::core::{ArgminFloat, Executor, State};
use itertools::Itertools;
use nalgebra::{
allocator::Allocator, DefaultAllocator, DimName, DimNameDiff, DimNameSub, Matrix2, OPoint,
Point2, Vector2, U1,
};
use num_traits::Float;
use crate::{
curve::NurbsCurve,
knot::KnotVector,
misc::{find_line_string_intersection, to_line_string_helper, FloatingPoint},
prelude::{BoundingBoxTraversal, CurveBoundingBoxTree, HasIntersection, Intersects},
};
use super::{
CurveCurveIntersection, CurveIntersectionBFGS, CurveIntersectionProblem,
CurveIntersectionSolverOptions,
};
impl<'a, T, D> Intersects<'a, &'a NurbsCurve<T, D>> for NurbsCurve<T, D>
where
T: FloatingPoint + ArgminFloat,
D: DimName + DimNameSub<U1>,
DefaultAllocator: Allocator<D>,
DefaultAllocator: Allocator<DimNameDiff<D, U1>>,
{
type Output = anyhow::Result<Vec<CurveCurveIntersection<OPoint<T, DimNameDiff<D, U1>>, T>>>;
type Option = Option<CurveIntersectionSolverOptions<T>>;
/// Find the intersection points with another curve by gauss-newton line search
/// * `other` - The other curve to intersect with
/// * `options` - Hyperparameters for the intersection solver
/// # Example
/// ```
/// use curvo::prelude::*;
/// use nalgebra::{Point2, Point3, Vector2};
/// use approx::assert_relative_eq;
/// let unit_circle = NurbsCurve2D::try_circle(
/// &Point2::origin(),
/// &Vector2::x(),
/// &Vector2::y(),
/// 1.
/// ).unwrap();
/// let line = NurbsCurve2D::try_new(
/// 1,
/// vec![
/// Point3::new(-2.0, 0.0, 1.),
/// Point3::new(2.0, 0.0, 1.),
/// ],
/// vec![0., 0., 1., 1.],
/// ).unwrap();
///
/// // Hyperparameters for the intersection solver
/// let options = CurveIntersectionSolverOptions {
/// minimum_distance: 1e-5, // minimum distance between intersections
/// cost_tolerance: 1e-12, // cost tolerance for the solver convergence
/// max_iters: 200, // maximum number of iterations in the solver
/// ..Default::default()
/// };
///
/// let mut intersections = unit_circle.find_intersection(&line, Some(options)).unwrap();
/// assert_eq!(intersections.len(), 2);
///
/// intersections.sort_by(|i0, i1| {
/// i0.a().0.x.partial_cmp(&i1.a().0.x).unwrap()
/// });
/// let p0 = &intersections[0];
/// assert_relative_eq!(p0.a().0, Point2::new(-1.0, 0.0), epsilon = 1e-5);
/// let p1 = &intersections[1];
/// assert_relative_eq!(p1.a().0, Point2::new(1.0, 0.0), epsilon = 1e-5);
/// ```
fn find_intersection(
&'a self,
other: &'a NurbsCurve<T, D>,
option: Self::Option,
) -> Self::Output {
if self.degree() == 1 && other.degree() == 1 && D::dim() == 3 {
// 2d polyline intersection
let p0 = self
.dehomogenized_control_points()
.iter()
.map(|p| Point2::from_slice(p.coords.as_slice()))
.collect_vec();
let p1 = other
.dehomogenized_control_points()
.iter()
.map(|p| Point2::from_slice(p.coords.as_slice()))
.collect_vec();
let l0 = to_line_string_helper(&p0);
let l1 = to_line_string_helper(&p1);
let intersections = find_line_string_intersection(&l0, &l1)?;
let find_parameter = |points: &Vec<Point2<T>>,
knots: &KnotVector<T>,
point: Point2<f64>,
index: usize|
-> anyhow::Result<T> {
anyhow::ensure!(index + 1 < points.len(), "index out of bounds");
let prev = points[index];
let next = points[index + 1];
let k0 = knots[index + 1];
let k1 = knots[index + 2];
let d = (next - prev).norm();
let d2 = (next - point.map(|x| T::from_f64(x).unwrap())).norm();
let t = T::one() - d2 / d;
Ok(k0 + t * (k1 - k0))
};
let its = intersections
.into_iter()
.map(|it| {
let pt = it.point();
let (i0, i1) = it.line_index();
let t0 = find_parameter(&p0, self.knots(), pt, i0)?;
let t1 = find_parameter(&p1, other.knots(), pt, i1)?;
let pt = OPoint::<T, DimNameDiff<D, U1>>::from_slice(
&pt.coords
.as_slice()
.iter()
.map(|x| T::from_f64(*x).unwrap())
.collect_vec(),
);
Ok(CurveCurveIntersection::new((pt.clone(), t0), (pt, t1)))
})
.collect::<anyhow::Result<Vec<_>>>()?;
return Ok(group_and_extract_closest_intersections(its));
}
let options = option.unwrap_or_default();
let ta = CurveBoundingBoxTree::new(
self,
Some(
self.knots_domain_interval() / T::from_usize(options.knot_domain_division).unwrap(),
),
);
let tb = CurveBoundingBoxTree::new(
other,
Some(
other.knots_domain_interval()
/ T::from_usize(options.knot_domain_division).unwrap(),
),
);
let traversed = BoundingBoxTraversal::try_traverse(ta, tb)?;
let a_domain = self.knots_domain();
let b_domain = other.knots_domain();
let intersections = traversed
.into_pairs_iter()
.filter_map(|(a, b)| {
let ca = a.curve_owned();
let cb = b.curve_owned();
let problem = CurveIntersectionProblem::new(&ca, &cb);
// let inv = T::from_f64(0.5).unwrap();
// let d0 = ca.knots_domain();
// let d1 = cb.knots_domain();
// Define initial parameter vector
let init_param = Vector2::<T>::new(
ca.knots_domain().0,
cb.knots_domain().0,
// (d0.0 + d0.1) * inv,
// (d1.0 + d1.1) * inv,
);
// Set up solver
let solver = CurveIntersectionBFGS::<T>::new()
.with_step_size_tolerance(options.step_size_tolerance)
.with_cost_tolerance(options.cost_tolerance);
// Run solver
let res = Executor::new(problem, solver)
.configure(|state| {
state
.param(init_param)
.inv_hessian(Matrix2::identity())
.max_iters(options.max_iters)
})
.run();
match res {
Ok(r) => {
// println!("{}", r.state().get_termination_status());
r.state().get_best_param().and_then(|param| {
if (a_domain.0..=a_domain.1).contains(¶m[0])
&& (b_domain.0..=b_domain.1).contains(¶m[1])
{
let p0 = self.point_at(param[0]);
let p1 = other.point_at(param[1]);
Some(CurveCurveIntersection::new((p0, param[0]), (p1, param[1])))
} else {
None
}
})
}
Err(_e) => {
// println!("{}", e);
None
}
}
})
.filter(|it| {
// filter out intersections that are too far away
let p0 = &it.a().0;
let p1 = &it.b().0;
let d = (p0 - p1).norm();
d < options.minimum_distance
})
.collect_vec();
let pts = group_and_extract_closest_intersections(intersections);
Ok(pts)
}
}
/// Group intersections by parameter and extract the closest intersection in each group
fn group_and_extract_closest_intersections<T, D>(
intersections: Vec<CurveCurveIntersection<OPoint<T, D>, T>>,
) -> Vec<CurveCurveIntersection<OPoint<T, D>, T>>
where
T: FloatingPoint + ArgminFloat,
D: DimName,
DefaultAllocator: Allocator<D>,
{
let sorted = intersections
.into_iter()
.sorted_by(|x, y| x.a().1.partial_cmp(&y.a().1).unwrap_or(Ordering::Equal))
.collect_vec();
let parameter_minimum_distance = T::from_f64(1e-3).unwrap();
let groups = sorted
.into_iter()
.map(|pt| vec![pt])
.coalesce(|x, y| {
let x0 = &x[x.len() - 1];
let y0 = &y[y.len() - 1];
let da = Float::abs(x0.a().1 - y0.a().1);
let db = Float::abs(x0.b().1 - y0.b().1);
if da < parameter_minimum_distance || db < parameter_minimum_distance {
// merge near parameter results
let group = [x, y].concat();
Ok(group)
} else {
Err((x, y))
}
})
.collect::<Vec<Vec<CurveCurveIntersection<OPoint<T, D>, T>>>>()
.into_iter()
.collect_vec();
groups
.into_iter()
.filter_map(|group| match group.len() {
1 => Some(group[0].clone()),
_ => {
// find the closest intersection in the group
group
.iter()
.map(|it| {
let delta = &it.a().0 - &it.b().0;
let norm = delta.norm_squared();
(it, norm)
})
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal))
.map(|closest| closest.0.clone())
}
})
.collect_vec()
}
#[cfg(test)]
mod tests {
use crate::curve::NurbsCurve2D;
use super::*;
#[test]
fn test_polyline_x_polyline_intersection_on_edge() {
let p0 = NurbsCurve2D::<f64>::polyline(
&[
Point2::new(0., 0.),
Point2::new(1., 0.),
Point2::new(1., 1.),
Point2::new(0., 1.),
],
false,
);
let p1 = NurbsCurve2D::<f64>::polyline(&[Point2::new(1., -1.), Point2::new(1., 2.)], false);
let intersections = p0.find_intersection(&p1, None).unwrap();
assert_eq!(intersections.len(), 2);
}
}