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
use crate::{ffi, Error, Result};

/// Converts a previously vertex cache optimized triangle list to triangle
/// strip, stitching strips using restart index.
///
/// For maximum efficiency the index buffer being converted has to be
/// optimized for vertex cache first.
///
/// The `restart_index` should be 0xffff or 0xffffffff depending on index size,
/// or 0 to use degenerate triangles.
pub fn stripify(indices: &[u32], vertex_count: usize, restart_index: u32) -> Result<Vec<u32>> {
    let mut result: Vec<u32> = vec![0; indices.len() / 3 * 4];
    let index_count = unsafe {
        ffi::meshopt_stripify(
            result.as_mut_ptr() as *mut ::std::os::raw::c_uint,
            indices.as_ptr() as *const ::std::os::raw::c_uint,
            indices.len(),
            vertex_count,
            restart_index,
        )
    };
    if index_count <= result.len() {
        result.resize(index_count, 0u32);
        Ok(result)
    } else {
        Err(Error::memory("index count is larger than result"))
    }
}

/// Converts a triangle strip to a triangle list
pub fn unstripify(indices: &[u32], restart_index: u32) -> Result<Vec<u32>> {
    let mut result: Vec<u32> = vec![0; (indices.len() - 2) * 3];
    let index_count = unsafe {
        ffi::meshopt_unstripify(
            result.as_mut_ptr() as *mut ::std::os::raw::c_uint,
            indices.as_ptr() as *const ::std::os::raw::c_uint,
            indices.len(),
            restart_index,
        )
    };
    if index_count <= result.len() {
        result.resize(index_count, 0u32);
        Ok(result)
    } else {
        Err(Error::memory("index count is larger than result"))
    }
}