geometry_tools 0.6.0

Efficient computation of single precision geometric data
Documentation
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use std::ops::Mul;
use thiserror::Error;

use glam::{Vec2, Vec3A, Vec4};

use crate::vectors::orthonormalize;

/// The value returned when any component of the calculated tangent would be `NaN` or infinite.
pub const DEFAULT_TANGENT: Vec3A = Vec3A::X;

/// The value returned when any component of the calculated bitangent would be `NaN` or infinite.
pub const DEFAULT_BITANGENT: Vec3A = Vec3A::Y;

/// Errors that can occur while calculating tangents or bitangents.
#[derive(Error, Debug)]
pub enum TangentBitangentError {
    #[error(
        "The list sizes do not match. Positions: {}, Normals: {}, uvs: {}.",
        position_count,
        normal_count,
        uv_count
    )]
    AttributeCountMismatch {
        position_count: usize,
        normal_count: usize,
        uv_count: usize,
    },
    #[error(
        "A vertex index count of {} is not supported. Expected {} to be divisible by 3.",
        index_count,
        index_count
    )]
    InvalidIndexCont { index_count: usize },
}

// TODO: Rewrite these functions to update existing array to better support ffi.

/// Calculates smooth per-vertex tangents and bitangents by averaging over the vertices in each face.
/// `indices` is assumed to contain triangle indices for `positions`, so `indices.len()` should be a multiple of 3.
/// If either of `positions` or `indices` is empty, the result is empty.
/// # Examples
/**

```rust
use geometry_tools::vectors::calculate_tangents_bitangents;
use glam::Vec3A;

# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let positions = vec![glam::Vec3A::ZERO; 3];
# let normals = vec![glam::Vec3A::ZERO; 3];
# let uvs = vec![glam::Vec2::ZERO; 3];
# let indices = vec![0, 1, 2];
// Some applications require multiplying the resulting bitangents by -1.0.
let (tangents, bitangents) = calculate_tangents_bitangents(&positions, &normals, &uvs, &indices)?;
# Ok(())
# }
```
 */
pub fn calculate_tangents_bitangents<P, N, I>(
    positions: &[P],
    normals: &[N],
    uvs: &[Vec2],
    indices: &[I],
) -> Result<(Vec<Vec3A>, Vec<Vec3A>), TangentBitangentError>
where
    P: Into<Vec3A> + Copy,
    N: Into<Vec3A> + Copy,
    I: TryInto<usize> + Copy,
    <I as TryInto<usize>>::Error: std::fmt::Debug,
{
    // TODO: This can be generic over the face count?
    if indices.len() % 3 != 0 {
        return Err(TangentBitangentError::InvalidIndexCont {
            index_count: indices.len(),
        });
    }

    if !(positions.len() == normals.len() && normals.len() == uvs.len()) {
        return Err(TangentBitangentError::AttributeCountMismatch {
            position_count: positions.len(),
            normal_count: normals.len(),
            uv_count: uvs.len(),
        });
    }

    let mut tangents = vec![Vec3A::ZERO; positions.len()];
    let mut bitangents = vec![Vec3A::ZERO; positions.len()];

    // Calculate the vectors.
    for face in indices.chunks(3) {
        if let [v0, v1, v2] = face {
            let v0 = (*v0).try_into().unwrap();
            let v1 = (*v1).try_into().unwrap();
            let v2 = (*v2).try_into().unwrap();
            let (tangent, bitangent) = calculate_tangent_bitangent(
                &positions[v0].into(),
                &positions[v1].into(),
                &positions[v2].into(),
                &uvs[v0],
                &uvs[v1],
                &uvs[v2],
            );

            tangents[v0] += tangent;
            tangents[v1] += tangent;
            tangents[v2] += tangent;

            bitangents[v0] += bitangent;
            bitangents[v1] += bitangent;
            bitangents[v2] += bitangent;
        }
    }

    // Even if the vectors are not zero, they may still sum to zero.
    for tangent in tangents.iter_mut() {
        if tangent.length_squared() == 0.0 {
            *tangent = DEFAULT_TANGENT;
        }

        *tangent = tangent.normalize_or_zero();
    }

    for bitangent in bitangents.iter_mut() {
        if bitangent.length_squared() == 0.0 {
            *bitangent = DEFAULT_BITANGENT;
        }
    }

    for (bitangent, normal) in bitangents.iter_mut().zip(normals.iter()) {
        // Account for mirrored normal maps.
        // The default bitangent may be parallel to the normal vector.
        let normal = (*normal).into();
        if bitangent.cross(normal).length_squared() != 0.0 {
            *bitangent = orthonormalize(bitangent, &normal);
        }

        *bitangent = bitangent.normalize_or_zero();
    }

    Ok((tangents, bitangents))
}

/// Calculates smooth per-vertex tangents by averaging over the vertices in each face.
/// The 4th component contains the tangent sign and can be used to calculate the bitangent vectors.
/// This step will normally be done by shader code for the GPU.
///
/// `indices` is assumed to contain triangle indices for `positions`, so `indices.len()` should be a multiple of 3.
/// If either of `positions` or `indices` is empty, the result is empty.
/// # Examples
/**

```rust
use geometry_tools::vectors::calculate_tangents;
use glam::Vec3A;

# fn main() -> Result<(), Box<dyn std::error::Error>> {
# let positions = vec![glam::Vec3A::ZERO; 3];
# let normals = vec![glam::Vec3A::ZERO; 3];
# let uvs = vec![glam::Vec2::ZERO; 3];
# let indices = vec![0, 1, 2];
let tangents = calculate_tangents(&positions, &normals, &uvs, &indices)?;

// This step is often done by shader code for the GPU.
// Some applications require multiplying the resulting bitangents by -1.0.
let bitangents: Vec<Vec3A> = tangents
    .iter()
    .zip(normals.iter())
    .map(|(t, n)| Vec3A::from_vec4(*t).cross(*n) * t.w)
    .collect();
# Ok(())
# }
```
 */
pub fn calculate_tangents<P, N, I>(
    positions: &[P],
    normals: &[N],
    uvs: &[Vec2],
    indices: &[I],
) -> Result<Vec<Vec4>, TangentBitangentError>
where
    P: Into<Vec3A> + Copy,
    N: Into<Vec3A> + Copy,
    I: TryInto<usize> + Copy,
    <I as TryInto<usize>>::Error: std::fmt::Debug,
{
    let (tangents, bitangents) = calculate_tangents_bitangents(positions, normals, uvs, indices)?;

    // Compute the w component for each tangent.
    // TODO: Compute this without computing and immediately discarding bitangent vectors?
    let tangents_with_w = tangents
        .iter()
        .zip(bitangents.iter())
        .zip(normals.iter())
        .map(|((t, b), n)| {
            let w = calculate_tangent_w(*t, *b, (*n).into());
            Vec4::new(t.x, t.y, t.z, w)
        })
        .collect();
    Ok(tangents_with_w)
}

/// Calculates the tangent sign of 1.0 or -1.0, which is often stored in the W component for a 4 component tangent vector.
/// The tangent sign is used to flip the generated bitangent to account for mirrored (overlapping) texture coordinates.
/// Depending on the conventions of the game or application, it may be necessary to multiply the returned value by -1.0.
/// # Examples
/**

```rust
use geometry_tools::vectors::calculate_tangent_w;

# let tangent = glam::Vec3A::ZERO;
# let bitangent = glam::Vec3A::ZERO;
# let normal = glam::Vec3A::ZERO;
let tangent_w = calculate_tangent_w(tangent, bitangent, normal);

// The bitangent can be generated from the tangent and normal vector.
// This step is often done by shader code for the GPU.
let bitangent = normal.cross(tangent) * tangent_w;
```
*/
#[inline]
pub fn calculate_tangent_w(tangent: Vec3A, bitangent: Vec3A, normal: Vec3A) -> f32 {
    // 0.0 should stil return 1.0 to avoid generating black bitangents.
    if tangent.cross(bitangent).dot(normal) >= 0.0 {
        1.0
    } else {
        -1.0
    }
}

fn calculate_tangent_bitangent(
    v0: &Vec3A,
    v1: &Vec3A,
    v2: &Vec3A,
    uv0: &Vec2,
    uv1: &Vec2,
    uv2: &Vec2,
) -> (Vec3A, Vec3A) {
    let pos_a = *v1 - *v0;
    let pos_b = *v2 - *v0;

    let uv_a = *uv1 - *uv0;
    let uv_b = *uv2 - *uv0;

    let div = uv_a.x * uv_b.y - uv_b.x * uv_a.y;

    // Fix +/- infinity from division by zero.
    // TODO: Make this check less strict?
    let r = if div != 0.0 { 1.0 / div } else { 1.0 };

    let tangent = calculate_tangent(&pos_a, &pos_b, &uv_a, &uv_b, r);
    let bitangent = calculate_bitangent(&pos_a, &pos_b, &uv_a, &uv_b, r);

    // Set zero vectors to arbitrarily chosen orthogonal vectors.
    // This prevents unwanted black faces when rendering tangent space normal maps.
    let tangent = if tangent.length_squared() == 0.0 {
        DEFAULT_TANGENT
    } else {
        tangent
    };

    let bitangent = if bitangent.length_squared() == 0.0 {
        DEFAULT_BITANGENT
    } else {
        bitangent
    };

    (tangent, bitangent)
}

fn calculate_tangent(pos_a: &Vec3A, pos_b: &Vec3A, uv_a: &Vec2, uv_b: &Vec2, r: f32) -> Vec3A {
    (pos_a.mul(uv_b.y) - pos_b.mul(uv_a.y)) * r
}

fn calculate_bitangent(pos_a: &Vec3A, pos_b: &Vec3A, uv_a: &Vec2, uv_b: &Vec2, r: f32) -> Vec3A {
    (pos_b.mul(uv_a.x) - pos_a.mul(uv_b.x)) * r
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::{assert_relative_eq, relative_eq};
    use glam::Vec2;

    const EPSILON: f32 = 0.0001;

    fn cube_positions() -> Vec<Vec3A> {
        vec![
            Vec3A::new(-0.5, -0.5, 0.5),
            Vec3A::new(0.5, -0.5, 0.5),
            Vec3A::new(-0.5, 0.5, 0.5),
            Vec3A::new(0.5, 0.5, 0.5),
            Vec3A::new(-0.5, 0.5, 0.5),
            Vec3A::new(0.5, 0.5, 0.5),
            Vec3A::new(-0.5, 0.5, -0.5),
            Vec3A::new(0.5, 0.5, -0.5),
            Vec3A::new(-0.5, 0.5, -0.5),
            Vec3A::new(0.5, 0.5, -0.5),
            Vec3A::new(-0.5, -0.5, -0.5),
            Vec3A::new(0.5, -0.5, -0.5),
            Vec3A::new(-0.5, -0.5, -0.5),
            Vec3A::new(0.5, -0.5, -0.5),
            Vec3A::new(-0.5, -0.5, 0.5),
            Vec3A::new(0.5, -0.5, 0.5),
            Vec3A::new(0.5, -0.5, 0.5),
            Vec3A::new(0.5, -0.5, -0.5),
            Vec3A::new(0.5, 0.5, 0.5),
            Vec3A::new(0.5, 0.5, -0.5),
            Vec3A::new(-0.5, -0.5, -0.5),
            Vec3A::new(-0.5, -0.5, 0.5),
            Vec3A::new(-0.5, 0.5, -0.5),
            Vec3A::new(-0.5, 0.5, 0.5),
        ]
    }

    fn cube_normals() -> Vec<Vec3A> {
        vec![
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 1.0, 0.0),
            Vec3A::new(0.0, 1.0, 0.0),
            Vec3A::new(0.0, 1.0, 0.0),
            Vec3A::new(0.0, 1.0, 0.0),
            Vec3A::new(0.0, 0.0, -1.0),
            Vec3A::new(0.0, 0.0, -1.0),
            Vec3A::new(0.0, 0.0, -1.0),
            Vec3A::new(0.0, 0.0, -1.0),
            Vec3A::new(0.0, -1.0, 0.0),
            Vec3A::new(0.0, -1.0, 0.0),
            Vec3A::new(0.0, -1.0, 0.0),
            Vec3A::new(0.0, -1.0, 0.0),
            Vec3A::new(1.0, 0.0, 0.0),
            Vec3A::new(1.0, 0.0, 0.0),
            Vec3A::new(1.0, 0.0, 0.0),
            Vec3A::new(1.0, 0.0, 0.0),
            Vec3A::new(-1.0, 0.0, 0.0),
            Vec3A::new(-1.0, 0.0, 0.0),
            Vec3A::new(-1.0, 0.0, 0.0),
            Vec3A::new(-1.0, 0.0, 0.0),
        ]
    }

    fn cube_uvs() -> Vec<Vec2> {
        vec![
            Vec2::new(0.375, 1.0),
            Vec2::new(0.625, 1.0),
            Vec2::new(0.375, 0.75),
            Vec2::new(0.625, 0.75),
            Vec2::new(0.375, 0.75),
            Vec2::new(0.625, 0.75),
            Vec2::new(0.375, 0.5),
            Vec2::new(0.625, 0.5),
            Vec2::new(0.375, 0.5),
            Vec2::new(0.625, 0.5),
            Vec2::new(0.375, 0.25),
            Vec2::new(0.625, 0.25),
            Vec2::new(0.375, 0.25),
            Vec2::new(0.625, 0.25),
            Vec2::new(0.375, 0.0),
            Vec2::new(0.625, 0.0),
            Vec2::new(0.625, 1.0),
            Vec2::new(0.875, 1.0),
            Vec2::new(0.625, 0.75),
            Vec2::new(0.875, 0.75),
            Vec2::new(0.125, 1.0),
            Vec2::new(0.375, 1.0),
            Vec2::new(0.125, 0.75),
            Vec2::new(0.375, 0.75),
        ]
    }

    fn cube_indices() -> Vec<u32> {
        vec![
            0, 1, 2, 2, 1, 3, 4, 5, 6, 6, 5, 7, 8, 9, 10, 10, 9, 11, 12, 13, 14, 14, 13, 15, 16,
            17, 18, 18, 17, 19, 20, 21, 22, 22, 21, 23,
        ]
    }

    #[test]
    fn different_uvs_different_positions() {
        let v1 = Vec3A::new(1.0, 0.0, 0.0);
        let v2 = Vec3A::new(0.0, 1.0, 0.0);
        let v3 = Vec3A::new(0.0, 0.0, 1.0);
        let uv1 = Vec2::new(1.0, 0.0);
        let uv2 = Vec2::new(0.0, 1.0);
        let uv3 = Vec2::new(1.0, 1.0);

        let (tangent, bitangent) = calculate_tangent_bitangent(&v1, &v2, &v3, &uv1, &uv2, &uv3);

        assert_eq!(Vec3A::new(0.0, -1.0, 1.0), tangent);
        assert_eq!(Vec3A::new(-1.0, 0.0, 1.0), bitangent);
    }

    #[test]
    fn different_uvs_same_positions() {
        let v1 = Vec3A::new(1.0, 0.0, 0.0);
        let v2 = Vec3A::new(1.0, 0.0, 0.0);
        let v3 = Vec3A::new(1.0, 0.0, 0.0);
        let uv1 = Vec2::new(1.0, 0.0);
        let uv2 = Vec2::new(0.0, 1.0);
        let uv3 = Vec2::new(1.0, 1.0);
        let (tangent, bitangent) = calculate_tangent_bitangent(&v1, &v2, &v3, &uv1, &uv2, &uv3);

        // Make sure tangents and bitangents aren't all zero.
        assert_eq!(DEFAULT_TANGENT, tangent);
        assert_eq!(DEFAULT_BITANGENT, bitangent);
    }

    #[test]
    fn same_uvs_different_positions() {
        let v1 = Vec3A::new(1.0, 0.0, 0.0);
        let v2 = Vec3A::new(0.0, 1.0, 0.0);
        let v3 = Vec3A::new(0.0, 0.0, 1.0);
        let uv1 = Vec2::new(1.0, 1.0);
        let uv2 = Vec2::new(1.0, 1.0);
        let uv3 = Vec2::new(1.0, 1.0);
        let (tangent, bitangent) = calculate_tangent_bitangent(&v1, &v2, &v3, &uv1, &uv2, &uv3);

        // Make sure tangents and bitangents aren't all zero.
        assert_eq!(DEFAULT_TANGENT, tangent);
        assert_eq!(DEFAULT_BITANGENT, bitangent);
    }

    #[test]
    fn same_uvs_same_positions() {
        let v1 = Vec3A::new(1.0, 0.0, 0.0);
        let v2 = Vec3A::new(1.0, 0.0, 0.0);
        let v3 = Vec3A::new(1.0, 0.0, 0.0);
        let uv1 = Vec2::new(1.0, 1.0);
        let uv2 = Vec2::new(1.0, 1.0);
        let uv3 = Vec2::new(1.0, 1.0);
        let (tangent, bitangent) = calculate_tangent_bitangent(&v1, &v2, &v3, &uv1, &uv2, &uv3);

        // Make sure tangents and bitangents aren't all zero.
        assert_eq!(DEFAULT_TANGENT, tangent);
        assert_eq!(DEFAULT_BITANGENT, bitangent);
    }

    #[test]
    fn uvs_would_cause_divide_by_zero() {
        let v1 = Vec3A::new(1.0, 0.0, 0.0);
        let v2 = Vec3A::new(0.0, 1.0, 0.0);
        let v3 = Vec3A::new(0.0, 0.0, 1.0);

        // Force the divisor to be 0.
        let uv1 = Vec2::new(0.5, 0.0);
        let uv2 = Vec2::new(0.5, 0.0);
        let uv3 = Vec2::new(1.0, 1.0);

        let (tangent, bitangent) = calculate_tangent_bitangent(&v1, &v2, &v3, &uv1, &uv2, &uv3);

        // Check for division by 0.
        assert!(tangent.is_finite());
        assert!(bitangent.is_finite());
    }

    #[test]
    fn triangle_list_single_triangle() {
        let positions = vec![
            Vec3A::new(0.0, 0.0, 0.0),
            Vec3A::new(0.0, 1.0, 0.0),
            Vec3A::new(1.0, 1.0, 0.0),
        ];
        let normals = vec![
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
        ];
        let uvs = vec![
            Vec2::new(0.0, 0.0),
            Vec2::new(1.0, 0.0),
            Vec2::new(1.0, 1.0),
        ];

        let (tangents, bitangents) =
            calculate_tangents_bitangents(&positions, &normals, &uvs, &[0u16, 1u16, 2u16]).unwrap();

        assert_eq!(3, tangents.len());
        assert_eq!(3, bitangents.len());

        // The tangent should point in the direct of the U coordinate.
        for tangent in tangents {
            assert_relative_eq!(0.0, tangent.x, epsilon = EPSILON);
            assert_relative_eq!(1.0, tangent.y, epsilon = EPSILON);
            assert_relative_eq!(0.0, tangent.z, epsilon = EPSILON);
        }

        // The bitangent should be orthogonal to the tangent and normal.
        // The only option in this case is to use the x-axis.
        for bitangent in bitangents {
            assert_relative_eq!(1.0, bitangent.x, epsilon = EPSILON);
            assert_relative_eq!(0.0, bitangent.y, epsilon = EPSILON);
            assert_relative_eq!(0.0, bitangent.z, epsilon = EPSILON);
        }
    }

    #[test]
    fn triangle_list_single_triangle_with_w() {
        let positions = vec![
            Vec3A::new(0.0, 0.0, 0.0),
            Vec3A::new(0.0, 1.0, 0.0),
            Vec3A::new(1.0, 1.0, 0.0),
        ];
        let normals = vec![
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
            Vec3A::new(0.0, 0.0, 1.0),
        ];
        let uvs = vec![
            Vec2::new(0.0, 0.0),
            Vec2::new(1.0, 0.0),
            Vec2::new(1.0, 1.0),
        ];

        let tangents = calculate_tangents(&positions, &normals, &uvs, &[0u16, 1u16, 2u16]).unwrap();
        let bitangents: Vec<Vec3A> = tangents
            .iter()
            .zip(normals.iter())
            .map(|(t, n)| Vec3A::from_vec4(*t).cross(*n) * t.w)
            .collect();

        assert_eq!(3, tangents.len());
        assert_eq!(3, bitangents.len());

        // The tangent should point in the direct of the U coordinate.
        for tangent in tangents {
            assert_relative_eq!(0.0, tangent.x, epsilon = EPSILON);
            assert_relative_eq!(1.0, tangent.y, epsilon = EPSILON);
            assert_relative_eq!(0.0, tangent.z, epsilon = EPSILON);
        }

        // The bitangent should be orthogonal to the tangent and normal.
        // The only option in this case is to use the x-axis.
        for bitangent in bitangents {
            assert_relative_eq!(-1.0, bitangent.x, epsilon = EPSILON);
            assert_relative_eq!(0.0, bitangent.y, epsilon = EPSILON);
            assert_relative_eq!(0.0, bitangent.z, epsilon = EPSILON);
        }
    }

    #[test]
    fn triangle_list_basic_cube_normalized_no_weird_floats() {
        let (tangents, bitangents) = calculate_tangents_bitangents(
            &cube_positions(),
            &cube_normals(),
            &cube_uvs(),
            &cube_indices(),
        )
        .unwrap();

        assert_eq!(24, tangents.len());
        assert_eq!(24, bitangents.len());

        for (tangent, bitangent) in tangents.iter().zip(bitangents) {
            assert_relative_eq!(1.0, tangent.length(), epsilon = EPSILON);
            assert_relative_eq!(1.0, bitangent.length(), epsilon = EPSILON);
            assert!(is_good_tangent_bitangent(tangent, &bitangent));
        }
    }

    #[test]
    fn triangle_list_not_enough_indices() {
        let positions = vec![Vec3A::ZERO; 5];
        let normals = vec![Vec3A::ZERO; 5];
        let uvs = vec![Vec2::ZERO; 5];
        let indices = vec![0, 1, 2, 3, 4];

        match calculate_tangents_bitangents(&positions, &normals, &uvs, &indices) {
            Err(TangentBitangentError::InvalidIndexCont { index_count }) => {
                assert_eq!(5, index_count)
            }
            _ => panic!("Unexpected variant"),
        };
    }

    #[test]
    fn triangle_list_no_vertices() {
        let (tangents, bitangents) =
            calculate_tangents_bitangents::<Vec3A, Vec3A, u32>(&[], &[], &[], &[]).unwrap();

        assert!(tangents.is_empty());
        assert!(bitangents.is_empty());
    }

    #[test]
    #[should_panic]
    fn triangle_list_incorrect_normals_count() {
        match calculate_tangents_bitangents::<Vec3A, _, u32>(&[], &[Vec3A::ZERO], &[], &[]) {
            Err(TangentBitangentError::AttributeCountMismatch {
                position_count,
                normal_count,
                uv_count,
            }) => {
                assert_eq!(1, position_count);
                assert_eq!(0, normal_count);
                assert_eq!(0, uv_count);
            }
            _ => panic!("Unexpected variant"),
        };
    }

    #[test]
    fn triangle_list_incorrect_uvs_count() {
        match calculate_tangents_bitangents::<Vec3A, _, u32>(
            &[],
            &[Vec3A::ZERO],
            &[Vec2::ZERO],
            &[],
        ) {
            Err(TangentBitangentError::AttributeCountMismatch {
                position_count,
                normal_count,
                uv_count,
            }) => {
                assert_eq!(0, position_count);
                assert_eq!(1, normal_count);
                assert_eq!(1, uv_count);
            }
            _ => panic!("Unexpected variant"),
        };
    }

    fn is_good_tangent_bitangent(tangent: &Vec3A, bitangent: &Vec3A) -> bool {
        // Check that the values are finite and very close to being orthogonal.
        tangent.is_finite()
            && bitangent.is_finite()
            && relative_eq!(0.0, tangent.dot(*bitangent), epsilon = EPSILON)
    }

    #[test]
    fn tangent_w_should_flip() {
        // cross(tangent,bitangent) is in the opposite direction of the normal.
        // This occurs on the side with mirrored UVs.
        let tangent = Vec3A::new(0.0, 1.0, 0.0);
        let bitangent = Vec3A::new(1.0, 0.0, 0.0);
        let normal = Vec3A::new(0.0, 0.0, 1.0);
        let w = calculate_tangent_w(tangent, bitangent, normal);
        assert_eq!(-1.0, w);
    }

    #[test]
    fn tangent_w_should_not_flip() {
        // cross(tangent, bitangent) is in the same direction as the normal.
        // This occurs on the side without mirrored UVs.
        let tangent = Vec3A::new(1.0, 0.0, 0.0);
        let bitangent = Vec3A::new(0.0, 1.0, 0.0);
        let normal = Vec3A::new(0.0, 0.0, 1.0);
        let w = calculate_tangent_w(tangent, bitangent, normal);
        assert_eq!(1.0, w);
    }

    #[test]
    fn tangent_w_should_not_be_zero() {
        // cross(tangent, bitangent) is orthogonal to the normal.
        let tangent = Vec3A::new(1.0, 0.0, 0.0);
        let bitangent = Vec3A::new(0.0, 1.0, 0.0);
        let normal = Vec3A::new(1.0, 0.0, 0.0);
        let w = calculate_tangent_w(tangent, bitangent, normal);
        assert_eq!(1.0, w);
    }
}