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
use crate::math::{Point, Real};
#[cfg(feature = "dim2")]
use crate::{math::Vector, utils};

// Features in clipping points are:
// 0 = First vertex.
// 1 = On the face.
// 2 = Second vertex.
pub type ClippingPoints = (Point<Real>, Point<Real>, usize, usize);

/// Projects two segments on one another towards the direction `normal`,
/// and compute their intersection.
#[cfg(feature = "dim2")]
pub fn clip_segment_segment_with_normal(
    mut seg1: (Point<Real>, Point<Real>),
    mut seg2: (Point<Real>, Point<Real>),
    normal: Vector<Real>,
) -> Option<(ClippingPoints, ClippingPoints)> {
    use crate::utils::WBasis;
    let tangent = normal.orthonormal_basis()[0];

    let mut range1 = [seg1.0.coords.dot(&tangent), seg1.1.coords.dot(&tangent)];
    let mut range2 = [seg2.0.coords.dot(&tangent), seg2.1.coords.dot(&tangent)];
    let mut features1 = [0, 2];
    let mut features2 = [0, 2];

    if range1[1] < range1[0] {
        range1.swap(0, 1);
        features1.swap(0, 1);
        std::mem::swap(&mut seg1.0, &mut seg1.1);
    }

    if range2[1] < range2[0] {
        range2.swap(0, 1);
        features2.swap(0, 1);
        std::mem::swap(&mut seg2.0, &mut seg2.1);
    }

    if range2[0] > range1[1] || range1[0] > range2[1] {
        // No clip point.
        return None;
    }

    let ca = if range2[0] > range1[0] {
        let bcoord = (range2[0] - range1[0]) * utils::inv(range1[1] - range1[0]);
        let p1 = seg1.0 + (seg1.1 - seg1.0) * bcoord;
        let p2 = seg2.0;

        (p1, p2, 1, features2[0])
    } else {
        let bcoord = (range1[0] - range2[0]) * utils::inv(range2[1] - range2[0]);
        let p1 = seg1.0;
        let p2 = seg2.0 + (seg2.1 - seg2.0) * bcoord;

        (p1, p2, features1[0], 1)
    };

    let cb = if range2[1] < range1[1] {
        let bcoord = (range2[1] - range1[0]) * utils::inv(range1[1] - range1[0]);
        let p1 = seg1.0 + (seg1.1 - seg1.0) * bcoord;
        let p2 = seg2.1;

        (p1, p2, 1, features2[1])
    } else {
        let bcoord = (range1[1] - range2[0]) * utils::inv(range2[1] - range2[0]);
        let p1 = seg1.1;
        let p2 = seg2.0 + (seg2.1 - seg2.0) * bcoord;

        (p1, p2, features1[1], 1)
    };

    Some((ca, cb))
}

/// Projects two segments on one another and compute their intersection.
pub fn clip_segment_segment(
    mut seg1: (Point<Real>, Point<Real>),
    mut seg2: (Point<Real>, Point<Real>),
) -> Option<(ClippingPoints, ClippingPoints)> {
    // NOTE: no need to normalize the tangent.
    let tangent1 = seg1.1 - seg1.0;
    let sqnorm_tangent1 = tangent1.norm_squared();

    let mut range1 = [0.0, sqnorm_tangent1];
    let mut range2 = [
        (seg2.0 - seg1.0).dot(&tangent1),
        (seg2.1 - seg1.0).dot(&tangent1),
    ];
    let mut features1 = [0, 2];
    let mut features2 = [0, 2];

    if range1[1] < range1[0] {
        range1.swap(0, 1);
        features1.swap(0, 1);
        std::mem::swap(&mut seg1.0, &mut seg1.1);
    }

    if range2[1] < range2[0] {
        range2.swap(0, 1);
        features2.swap(0, 1);
        std::mem::swap(&mut seg2.0, &mut seg2.1);
    }

    if range2[0] > range1[1] || range1[0] > range2[1] {
        // No clip point.
        return None;
    }

    let length1 = range1[1] - range1[0];
    let length2 = range2[1] - range2[0];

    let ca = if range2[0] > range1[0] {
        let bcoord = (range2[0] - range1[0]) / length1;
        let p1 = seg1.0 + tangent1 * bcoord;
        let p2 = seg2.0;

        (p1, p2, 1, features2[0])
    } else {
        let bcoord = (range1[0] - range2[0]) / length2;
        let p1 = seg1.0;
        let p2 = seg2.0 + (seg2.1 - seg2.0) * bcoord;

        (p1, p2, features1[0], 1)
    };

    let cb = if range2[1] < range1[1] {
        let bcoord = (range2[1] - range1[0]) / length1;
        let p1 = seg1.0 + tangent1 * bcoord;
        let p2 = seg2.1;

        (p1, p2, 1, features2[1])
    } else {
        let bcoord = (range1[1] - range2[0]) / length2;
        let p1 = seg1.1;
        let p2 = seg2.0 + (seg2.1 - seg2.0) * bcoord;

        (p1, p2, features1[1], 1)
    };

    Some((ca, cb))
}