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
use super::*;

#[doc(hidden)]
pub fn double_projection<S>(
    surface0: &S,
    hint0: Option<(f64, f64)>,
    surface1: &S,
    hint1: Option<(f64, f64)>,
    mut point: Point3,
    normal: Vector3,
    trials: usize,
) -> Option<(Point3, Point2, Point2)>
where
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    #[cfg(all(test, debug_assertions))]
    let mut log = Vec::new();
    let mut uv0 = surface0.search_nearest_parameter(point, hint0, 10)?;
    let mut uv1 = surface1.search_nearest_parameter(point, hint1, 10)?;
    for _ in 0..trials {
        #[cfg(all(test, debug_assertions))]
        log.push((point, uv0, uv1));
        uv0 = surface0.search_nearest_parameter(point, Some(uv0), 10)?;
        let pt0 = surface0.subs(uv0.0, uv0.1);
        uv1 = surface1.search_nearest_parameter(point, Some(uv1), 10)?;
        let pt1 = surface1.subs(uv1.0, uv1.1);
        if point.near(&pt0) && point.near(&pt1) && pt0.near(&pt1) {
            return Some((point, Point2::from(uv0), Point2::from(uv1)));
        } else {
            let n0 = surface0.normal(uv0.0, uv0.1);
            let n1 = surface1.normal(uv1.0, uv1.1);
            let mat = Matrix3::from_cols(n0, n1, normal).transpose();
            let inv = mat.invert()?;
            let pt = inv * Vector3::new(pt0.dot(n0), pt1.dot(n1), point.dot(normal));
            point = Point3::from_vec(pt);
        }
    }
    #[cfg(all(test, debug_assertions))]
    {
        eprintln!("Newton method is not converges");
        log.into_iter().for_each(|t| eprintln!("{:?}", t));
    }
    None
}

/// Mutable editor for `IntersectionCurve`.
#[doc(hidden)]
#[derive(Debug)]
pub struct IntersectionCurveEditor<'a, C, S> {
    pub surface0: &'a mut S,
    pub surface1: &'a mut S,
    pub leader: &'a mut C,
    pub tol: &'a mut f64,
}

impl<C, S> IntersectionCurve<C, S> {
    /// This curve is a part of intersection of `self.surface0()` and `self.surface1()`.
    #[inline(always)]
    pub fn surface0(&self) -> &S { &self.surface0 }
    /// This curve is a part of intersection of `self.surface0()` and `self.surface1()`.
    #[inline(always)]
    pub fn surface1(&self) -> &S { &self.surface1 }
    /// Returns the polyline leading this curve.
    #[inline(always)]
    pub fn leader(&self) -> &C { &self.leader }
    /// Returns editor for `IntersectionCurve`. This method is only for developers, do not use.
    #[doc(hidden)]
    #[inline(always)]
    pub fn editor(&mut self) -> IntersectionCurveEditor<'_, C, S> {
        IntersectionCurveEditor {
            surface0: &mut self.surface0,
            surface1: &mut self.surface1,
            leader: &mut self.leader,
            tol: &mut self.tol,
        }
    }
    /// Change leader.
    #[doc(hidden)]
    #[inline(always)]
    pub fn change_leader<D>(self, f: impl FnOnce(C) -> D) -> IntersectionCurve<D, S> {
        IntersectionCurve {
            surface0: self.surface0,
            surface1: self.surface1,
            leader: f(self.leader),
            tol: self.tol,
        }
    }
    /// The tolerance for generating this intersection curve.
    #[inline(always)]
    pub fn tolerance(&self) -> f64 { self.tol }
    /// Creates intersection curve with unchecked bound. This method is only for developer of `truck`, deplicated for users.
    #[inline(always)]
    pub fn new_unchecked(surface0: Box<S>, surface1: Box<S>, leader: C, tol: f64) -> Self {
        Self {
            surface0,
            surface1,
            leader,
            tol,
        }
    }
}

impl<C, S> IntersectionCurve<C, S>
where
    C: ParametricCurve3D,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    /// Search triple value of the point corresponding to the parameter `t`.
    /// - the coordinate on 3D space
    /// - the uv coordinate on `self.surface0()`
    /// - the uv coordinate on `self.surface1()`
    #[inline(always)]
    pub fn search_triple(&self, t: f64) -> Option<(Point3, Point2, Point2)> {
        double_projection(
            self.surface0(),
            None,
            self.surface1(),
            None,
            self.leader.subs(t),
            self.leader.der(t),
            100,
        )
    }
}

impl<C, S> ParametricCurve for IntersectionCurve<C, S>
where
    C: ParametricCurve3D,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    type Point = Point3;
    type Vector = Vector3;
    fn subs(&self, t: f64) -> Point3 { self.search_triple(t).unwrap().0 }
    fn der(&self, t: f64) -> Vector3 {
        let n = self.leader.der(t);
        let (_, p0, p1) = self.search_triple(t).unwrap();
        let d = self
            .surface0
            .normal(p0.x, p0.y)
            .cross(self.surface1.normal(p1.x, p1.y))
            .normalize();
        d * (n.dot(n) / d.dot(n))
    }
    /// This method is unimplemented! Should panic!!
    #[inline(always)]
    fn der2(&self, _: f64) -> Vector3 {
        unimplemented!();
    }
}

impl<C, S> BoundedCurve for IntersectionCurve<C, S>
where
    C: ParametricCurve3D + BoundedCurve,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    #[inline(always)]
    fn parameter_range(&self) -> (f64, f64) { self.leader.parameter_range() }
}

impl<C, S> ParameterDivision1D for IntersectionCurve<C, S>
where
    C: ParametricCurve3D,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    type Point = Point3;
    #[inline(always)]
    fn parameter_division(&self, range: (f64, f64), tol: f64) -> (Vec<f64>, Vec<Point3>) {
        algo::curve::parameter_division(self, range, tol)
    }
}

impl<C, S> Cut for IntersectionCurve<C, S>
where
    C: Cut<Point = Point3, Vector = Vector3>,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    #[inline(always)]
    fn cut(&mut self, t: f64) -> Self {
        Self {
            surface0: self.surface0.clone(),
            surface1: self.surface1.clone(),
            leader: self.leader.cut(t),
            tol: self.tol,
        }
    }
}

impl<C: Invertible, S: Clone> Invertible for IntersectionCurve<C, S> {
    fn invert(&mut self) { self.leader.invert(); }
}

impl<C, S> SearchParameter<D1> for IntersectionCurve<C, S>
where
    C: ParametricCurve3D + SearchNearestParameter<D1, Point = Point3>,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    type Point = Point3;
    fn search_parameter<H: Into<SPHint1D>>(
        &self,
        point: Point3,
        hint: H,
        trials: usize,
    ) -> Option<f64> {
        let t = self
            .leader()
            .search_nearest_parameter(point, hint, trials)
            .unwrap();
        let pt = self.subs(t);
        match pt.near(&point) {
            true => Some(t),
            false => None,
        }
    }
}

/// Only derive from leading curve. Not precise.
impl<C, S> SearchNearestParameter<D1> for IntersectionCurve<C, S>
where
    C: ParametricCurve3D + SearchNearestParameter<D1, Point = Point3>,
    S: ParametricSurface3D + SearchNearestParameter<D2, Point = Point3>,
{
    type Point = Point3;
    fn search_nearest_parameter<H: Into<SPHint1D>>(
        &self,
        point: Point3,
        hint: H,
        trials: usize,
    ) -> Option<f64> {
        self.leader().search_nearest_parameter(point, hint, trials)
    }
}

impl<C, S> Transformed<Matrix4> for IntersectionCurve<C, S>
where
    C: Transformed<Matrix4>,
    S: Transformed<Matrix4>,
{
    fn transform_by(&mut self, trans: Matrix4) {
        self.surface0.transform_by(trans);
        self.surface1.transform_by(trans);
        self.leader.transform_by(trans);
        self.tol *= trans.norm_l2();
    }
}

impl<C: BoundedCurve> IntersectionCurve<C, Plane> {
    /// Optimizes intersection curve of [`Plane`] into [`Line`].
    #[inline]
    pub fn optimize(&self) -> Line<C::Point> {
        let (s, t) = self.leader.parameter_range();
        Line(self.leader.subs(s), self.leader.subs(t))
    }
}