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
pub mod astar;
pub mod log;

use std::marker::PhantomData;
use crate::{
    scene::{mesh::Mesh, base::AsBase},
    physics::static_geometry::{StaticGeometry, StaticTriangle},
};

pub struct UnsafeCollectionView<T> {
    items: *const T,
    len: usize,
}

impl<T> UnsafeCollectionView<T> {
    pub fn empty() -> UnsafeCollectionView<T> {
        UnsafeCollectionView {
            items: std::ptr::null(),
            len: 0,
        }
    }

    pub fn from_slice(vec: &[T]) -> UnsafeCollectionView<T> {
        UnsafeCollectionView {
            items: vec.as_ptr(),
            len: vec.len(),
        }
    }

    pub fn iter(&self) -> UnsafeCollectionViewIterator<T> {
        unsafe {
            UnsafeCollectionViewIterator {
                current: self.items,
                end: self.items.add(self.len),
                marker: PhantomData,
            }
        }
    }
}

pub struct UnsafeCollectionViewIterator<'a, T> {
    current: *const T,
    end: *const T,
    marker: PhantomData<&'a T>,
}

impl<'a, T> Iterator for UnsafeCollectionViewIterator<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<&'a T> {
        unsafe {
            if self.current != self.end {
                let value = self.current;
                self.current = self.current.offset(1);
                Some(&*value)
            } else {
                None
            }
        }
    }
}

/// Small helper that creates static physics geometry from given mesh.
///
/// # Notes
///
/// This method *bakes* global transform of given mesh into static geometry
/// data. So if given mesh was at some position with any rotation and scale
/// resulting static geometry will have vertices that exactly matches given
/// mesh.
pub fn mesh_to_static_geometry(mesh: &Mesh) -> StaticGeometry {
    let mut triangles = Vec::new();
    let global_transform = mesh.base().get_global_transform();
    for surface in mesh.get_surfaces() {
        let data_rc = surface.get_data();
        let shared_data = data_rc.lock().unwrap();

        let vertices = shared_data.get_vertices();
        let indices = shared_data.get_indices();

        let last = indices.len() - indices.len() % 3;
        let mut i: usize = 0;
        while i < last {
            let a = global_transform.transform_vector(vertices[indices[i] as usize].position);
            let b = global_transform.transform_vector(vertices[indices[i + 1] as usize].position);
            let c = global_transform.transform_vector(vertices[indices[i + 2] as usize].position);

            // Silently ignore degenerated triangles.
            if let Some(triangle) = StaticTriangle::from_points(&a, &b, &c) {
                triangles.push(triangle);
            }

            i += 3;
        }
    }
    StaticGeometry::new(triangles)
}