aboutsummaryrefslogtreecommitdiff
path: root/src/geometry/polyhedron_feature3d.rs
blob: dfeee29be281e6509c58a73fe54da89d1c6f37ab (plain)
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
use crate::geometry::{Contact, ContactManifold, CuboidFeatureFace, Triangle};
use crate::math::{Isometry, Point, Vector};
use crate::utils::WBasis;
use na::Point2;
use ncollide::shape::Segment;

#[derive(Debug)]
pub struct PolyhedronFace {
    pub vertices: [Point<f32>; 4],
    pub vids: [u8; 4], // Feature ID of the vertices.
    pub eids: [u8; 4], // Feature ID of the edges.
    pub fid: u8,       // Feature ID of the face.
    pub num_vertices: usize,
}

impl From<CuboidFeatureFace> for PolyhedronFace {
    fn from(face: CuboidFeatureFace) -> Self {
        Self {
            vertices: face.vertices,
            vids: face.vids,
            eids: face.eids,
            fid: face.fid,
            num_vertices: 4,
        }
    }
}

impl From<Triangle> for PolyhedronFace {
    fn from(tri: Triangle) -> Self {
        Self {
            vertices: [tri.a, tri.b, tri.c, tri.c],
            vids: [0, 2, 4, 4],
            eids: [1, 3, 5, 5],
            fid: 0,
            num_vertices: 3,
        }
    }
}

impl From<Segment<f32>> for PolyhedronFace {
    fn from(seg: Segment<f32>) -> Self {
        Self {
            vertices: [seg.a, seg.b, seg.b, seg.b],
            vids: [0, 2, 2, 2],
            eids: [1, 1, 1, 1],
            fid: 0,
            num_vertices: 2,
        }
    }
}

impl PolyhedronFace {
    pub fn transform_by(&mut self, iso: &Isometry<f32>) {
        for v in &mut self.vertices[0..self.num_vertices] {
            *v = iso * *v;
        }
    }

    pub fn contacts(
        prediction_distance: f32,
        face1: &PolyhedronFace,
        sep_axis1: &Vector<f32>,
        face2: &PolyhedronFace,
        pos21: &Isometry<f32>,
        manifold: &mut ContactManifold,
    ) {
        // Project the faces to a 2D plane for contact clipping.
        // The plane they are projected onto has normal sep_axis1
        // and contains the origin (this is numerically OK because
        // we are not working in world-space here).
        let basis = sep_axis1.orthonormal_basis();
        let projected_face1 = [
            Point2::new(
                face1.vertices[0].coords.dot(&basis[0]),
                face1.vertices[0].coords.dot(&basis[1]),
            ),
            Point2::new(
                face1.vertices[1].coords.dot(&basis[0]),
                face1.vertices[1].coords.dot(&basis[1]),
            ),
            Point2::new(
                face1.vertices[2].coords.dot(&basis[0]),
                face1.vertices[2].coords.dot(&basis[1]),
            ),
            Point2::new(
                face1.vertices[3].coords.dot(&basis[0]),
                face1.vertices[3].coords.dot(&basis[1]),
            ),
        ];
        let projected_face2 = [
            Point2::new(
                face2.vertices[0].coords.dot(&basis[0]),
                face2.vertices[0].coords.dot(&basis[1]),
            ),
            Point2::new(
                face2.vertices[1].coords.dot(&basis[0]),
                face2.vertices[1].coords.dot(&basis[1]),
            ),
            Point2::new(
                face2.vertices[2].coords.dot(&basis[0]),
                face2.vertices[2].coords.dot(&basis[1]),
            ),
            Point2::new(
                face2.vertices[3].coords.dot(&basis[0]),
                face2.vertices[3].coords.dot(&basis[1]),
            ),
        ];

        // Also find all the vertices located inside of the other projected face.
        if face2.num_vertices > 2 {
            let normal2 = (face2.vertices[2] - face2.vertices[1])
                .cross(&(face2.vertices[0] - face2.vertices[1]));
            let denom = normal2.dot(&sep_axis1);

            if !relative_eq!(denom, 0.0) {
                let last_index2 = face2.num_vertices as usize - 1;
                'point_loop1: for i in 0..face1.num_vertices as usize {
                    let p1 = projected_face1[i];

                    let sign = (projected_face2[0] - projected_face2[last_index2])
                        .perp(&(p1 - projected_face2[last_index2]));
                    for j in 0..last_index2 {
                        let new_sign = (projected_face2[j + 1] - projected_face2[j])
                            .perp(&(p1 - projected_face2[j]));
                        if new_sign * sign < 0.0 {
                            // The point lies outside.
                            continue 'point_loop1;
                        }
                    }

                    // All the perp had the same sign: the point is inside of the other shapes projection.
                    // Output the contact.
                    let dist = (face2.vertices[0] - face1.vertices[i]).dot(&normal2) / denom;
                    let local_p1 = face1.vertices[i];
                    let local_p2 = face1.vertices[i] + dist * sep_axis1;

                    if dist <= prediction_distance {
                        manifold.points.push(Contact {
                            local_p1,
                            local_p2: pos21 * local_p2,
                            impulse: 0.0,
                            tangent_impulse: Contact::zero_tangent_impulse(),
                            fid1: face1.vids[i],
                            fid2: face2.fid,
                            dist,
                        });
                    }
                }
            }
        }

        if face1.num_vertices > 2 {
            let normal1 = (face1.vertices[2] - face1.vertices[1])
                .cross(&(face1.vertices[0] - face1.vertices[1]));

            let denom = -normal1.dot(&sep_axis1);
            if !relative_eq!(denom, 0.0) {
                let last_index1 = face1.num_vertices as usize - 1;
                'point_loop2: for i in 0..face2.num_vertices as usize {
                    let p2 = projected_face2[i];

                    let sign = (projected_face1[0] - projected_face1[last_index1])
                        .perp(&(p2 - projected_face1[last_index1]));
                    for j in 0..last_index1 {
                        let new_sign = (projected_face1[j + 1] - projected_face1[j])
                            .perp(&(p2 - projected_face1[j]));

                        if new_sign * sign < 0.0 {
                            // The point lies outside.
                            continue 'point_loop2;
                        }
                    }

                    // All the perp had the same sign: the point is inside of the other shapes projection.
                    // Output the contact.
                    let dist = (face1.vertices[0] - face2.vertices[i]).dot(&normal1) / denom;
                    let local_p2 = face2.vertices[i];
                    let local_p1 = face2.vertices[i] - dist * sep_axis1;

                    if true {
                        // dist <= prediction_distance {
                        manifold.points.push(Contact {
                            local_p1,
                            local_p2: pos21 * local_p2,
                            impulse: 0.0,
                            tangent_impulse: Contact::zero_tangent_impulse(),
                            fid1: face1.fid,
                            fid2: face2.vids[i],
                            dist,
                        });
                    }
                }
            }
        }

        // Now we have to compute the intersection between all pairs of
        // edges from the face 1 and from the face2.
        for j in 0..face2.num_vertices {
            let projected_edge2 = [
                projected_face2[j],