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
#![allow(dead_code)]
use math::{BaseNum, BaseFloat, Matrix3, Vector3};
use crate::ops::*;
use std::ops::Neg;
use crate::Pod;

/// Generic triangle with three points
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Triangle<T>(pub Vector3<T>, pub Vector3<T>, pub Vector3<T>);

impl<T: Clone> Triangle<T> {
    /// Build a Triangle from a triplet of indices.
    #[inline]
    pub fn from_indexed_slice<V: Into<[T; 3]> + Clone>(
        indices: &[usize; 3],
        slice: &[V],
    ) -> Triangle<T> {
        Triangle(
            slice[indices[0]].clone().into().into(),
            slice[indices[1]].clone().into().into(),
            slice[indices[2]].clone().into().into(),
        )
    }

    /// Build a new triangle from an array of vertex positions.
    #[inline]
    pub fn new([a,b,c]: [[T; 3]; 3]) -> Self {
        Triangle(
            a.into(),
            b.into(),
            c.into(),
        )
    }
}

impl<T: Pod> Triangle<T> {
    /// Return this triangle as an array of vertex positions.
    ///
    /// This can be useful for performing custom arithmetic on the triangle positions.
    #[inline]
    pub fn as_array(&self) -> &[[T; 3]; 3] {
        debug_assert_eq!(
            std::mem::size_of::<[[T; 3]; 3]>(),
            std::mem::size_of::<Triangle<T>>(),
        );

        unsafe { std::mem::transmute_copy(&self) }
    }
}

impl<T> Triangle<T> {
    /// Convert this triangle into an array of vertex positions.
    #[inline]
    pub fn into_array(self) -> [[T; 3]; 3] {
        [self.0.into(), self.1.into(), self.2.into()]
    }
}

impl<T: BaseNum + Neg<Output = T>> Triangle<T> {
    /// Compute the area weighted normal of this triangle. This is the standard way to compute the
    /// normal and the area of the triangle.
    #[inline]
    pub fn area_normal(&self) -> [T; 3] {
        (self.0 - self.1).cross(self.0 - self.2).into()
    }

    /// Compute the gradient of the area weighted normal with respect to the given vertex.
    /// The returned matrix is in column-major format.
    #[inline]
    pub fn area_normal_gradient(&self, wrt: usize) -> [[T; 3]; 3] {
        match wrt {
            0 => (self.1 - self.2).skew(),
            1 => (self.2 - self.0).skew(),
            2 => (self.0 - self.1).skew(),
            _ => panic!("Triangle has only 3 vertices"),
        }.into()
    }

    /// Compute the hessian of the area weighted normal with respect to the given vertex (row
    /// index) at a given vertex (column index) multiplied by a given vector (`lambda`).
    ///
    /// The returned matrix is in column-major format.
    #[inline]
    pub fn area_normal_hessian_product(
        wrt_row: usize,
        at_col: usize,
        lambda: [T; 3],
    ) -> [[T; 3]; 3] {
        let zero = Matrix3::from([[T::zero(); 3]; 3]);
        let lambda = Vector3::from(lambda);
        match wrt_row {
            0 => match at_col {
                0 => zero,
                1 => (-lambda).skew(),
                2 => lambda.skew(),
                _ => panic!("Triangle has only 3 vertices"),
            },
            1 => match at_col {
                0 => lambda.skew(),
                1 => zero,
                2 => (-lambda).skew(),
                _ => panic!("Triangle has only 3 vertices"),
            },
            2 => match at_col {
                0 => (-lambda).skew(),
                1 => lambda.skew(),
                2 => zero,
                _ => panic!("Triangle has only 3 vertices"),
            },
            _ => panic!("Triangle has only 3 vertices"),
        }.into()
    }
}

impl<'a, T: BaseNum> Centroid<Vector3<T>> for &'a Triangle<T> {
    #[inline]
    fn centroid(self) -> Vector3<T> {
        (self.0 + self.1 + self.2) / T::from(3.0).unwrap()
    }
}

impl<'a, T: BaseFloat + Neg<Output = T>> Normal<Vector3<T>> for &'a Triangle<T> {
    #[inline]
    fn normal(self) -> Vector3<T> {
        let nml: Vector3<T> = self.area_normal().into();
        nml / nml.norm()
    }
}

impl<'a, T: BaseNum> Centroid<[T; 3]> for &'a Triangle<T> {
    #[inline]
    fn centroid(self) -> [T; 3] {
        <Self as Centroid<Vector3<T>>>::centroid(self).into()
    }
}

impl<'a, T: BaseFloat + Neg<Output = T>> Normal<[T; 3]> for &'a Triangle<T> {
    #[inline]
    fn normal(self) -> [T; 3] {
        <Self as Centroid<Vector3<T>>>::centroid(self).into()
    }
}

impl<T: BaseNum> std::ops::Index<usize> for Triangle<T> {
    type Output = Vector3<T>;

    fn index(&self, i: usize) -> &Self::Output {
        match i {
            0 => &self.0,
            1 => &self.1,
            2 => &self.2,
            _ => panic!("Index out of bounds: triangle has only 3 vertices."),
        }
    }
}

impl<T: BaseNum> std::ops::IndexMut<usize> for Triangle<T> {
    fn index_mut(&mut self, i: usize) -> &mut Self::Output {
        match i {
            0 => &mut self.0,
            1 => &mut self.1,
            2 => &mut self.2,
            _ => panic!("Index out of bounds: triangle has only 3 vertices."),
        }
    }
}

/// Returns a column major 3x2 matrix.
impl<'a, T: BaseNum> ShapeMatrix<[[T; 3]; 2]> for &'a Triangle<T> {
    #[inline]
    fn shape_matrix(self) -> [[T; 3]; 2] {
        [
            (self.0 - self.2).into(),
            (self.1 - self.2).into(),
        ]
    }
}

/// Returns a column major 3x2 matrix.
impl<T: BaseNum> ShapeMatrix<[[T; 3]; 2]> for Triangle<T> {
    #[inline]
    fn shape_matrix(self) -> [[T; 3]; 2] {
        [
            (self.0 - self.2).into(),
            (self.1 - self.2).into(),
        ]
    }
}


impl<'a, T: BaseFloat + Neg<Output = T>> Area<T> for &'a Triangle<T> {
    #[inline]
    fn area(self) -> T {
        self.signed_area()
    }
    #[inline]
    fn signed_area(self) -> T {
        Vector3::from(self.area_normal()).norm() / T::from(2.0).unwrap()
    }
}

impl<T: BaseFloat + Neg<Output = T>> Area<T> for Triangle<T> {
    #[inline]
    fn area(self) -> T {
        // A triangle in 3D space can't be inverted.
        self.signed_area()
    }
    #[inline]
    fn signed_area(self) -> T {
        Vector3::from(self.area_normal()).norm() / T::from(2.0).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use autodiff::*;
    use approx::assert_relative_eq;

    fn tri() -> Triangle<f64> {
        Triangle(
            Vector3::from([0.0, 0.0, 0.0]),
            Vector3::from([0.0, 0.0, 1.0]),
            Vector3::from([0.0, 1.0, 0.0]),
        )
    }

    #[test]
    fn area_test() {
        assert_relative_eq!(tri().area(), 0.5);
        assert_relative_eq!(tri().signed_area(), 0.5);
    }

    #[test]
    fn area_normal_gradient_test() {
        // Triangle in the XY plane.
        let trif = Triangle(
            Vector3::from([0.0, 0.0, 0.0]),
            Vector3::from([1.0, 0.0, 0.0]),
            Vector3::from([0.0, 1.0, 0.0]),
        );

        let mut tri = Triangle(
            trif[0].map(|x| F::cst(x)),
            trif[1].map(|x| F::cst(x)),
            trif[2].map(|x| F::cst(x)),
        );

        // for each vertex
        for dvtx in 0..3 {
            let grad = trif.area_normal_gradient(dvtx);
            // for each component
            for i in 0..3 {
                tri[dvtx][i] = F::var(tri[dvtx][i]); // convert to variable
                let nml = tri.area_normal();
                tri[dvtx][i] = F::cst(tri[dvtx][i]); // convert to constant
                for j in 0..3 {
                    assert_relative_eq!(nml[j].deriv(), grad[j][i], max_relative = 1e-9);
                }
            }
        }
    }

    #[test]
    fn area_normal_hessian_test() {
        // Triangle in the XY plane.
        let trif = Triangle(
            Vector3::from([0.0, 0.0, 0.0]),
            Vector3::from([1.0, 0.0, 0.0]),
            Vector3::from([0.0, 1.0, 0.0]),
        );

        let mut tri = Triangle(
            trif[0].map(|x| F::cst(x)),
            trif[1].map(|x| F::cst(x)),
            trif[2].map(|x| F::cst(x)),
        );

        let lambda = Vector3::from([0.2, 3.1, 42.0]); // some random multiplier

        // for each vertex
        for wrt_vtx in 0..3 {
            for i in 0..3 {
                tri[wrt_vtx][i] = F::var(tri[wrt_vtx][i]); // convert to variable
                for at_vtx in 0..3 {
                    let hess_prod = Matrix3::from(Triangle::area_normal_hessian_product(wrt_vtx, at_vtx, lambda.into()));
                    // for each component
                    let ad_lambda = lambda.map(|x| F::cst(x));
                    let grad_prod = Matrix3::from(tri.area_normal_gradient(at_vtx)) * ad_lambda;
                    for j in 0..3 {
                        assert_relative_eq!(
                            grad_prod[j].deriv(),
                            hess_prod[j][i],
                            max_relative = 1e-9
                        );
                    }
                }
                tri[wrt_vtx][i] = F::cst(tri[wrt_vtx][i]); // convert to constant
            }
        }
    }
}