assimp/scene/
mesh.rs

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
use ffi::{AiMesh, AiVector3D, AiBone, AiVertexWeight, AiColor4D};

use math::vector3::{Vector3D, Vector3DIter};
use math::color4::{Color4D, Color4DIter};
use super::face::{Face, FaceIter};

use math::Matrix4x4;

define_type_and_iterator_indirect! {
    /// Mesh type (incomplete)
    struct Mesh(&AiMesh)
    /// Mesh iterator type.
    struct MeshIter
}

define_type_and_iterator_indirect! {
    /// Bone type
    struct Bone(&AiBone)
    /// Bone iterator type.
    struct BoneIter
}

define_type_and_iterator_indirect! {
    /// Vertex weight type
    struct VertexWeight(&AiVertexWeight)
    /// Vertex weight iterator type.
    struct VertexWeightIter
}

impl<'a> Mesh<'a> {
    // TODO return as PrimitiveType enum
    pub fn primitive_types(&self) -> u32 {
        self.primitive_types
    }

    pub fn num_vertices(&self) -> u32 {
        self.num_vertices
    }

    pub fn vertex_iter(&self) -> Vector3DIter {
        Vector3DIter::new(self.vertices,
                          self.num_vertices as usize)
    }

    pub fn get_vertex(&self, id: u32) -> Option<Vector3D> {
        self.vertex_data(self.vertices, id)
    }

    pub fn normal_iter(&self) -> Vector3DIter {
        Vector3DIter::new(self.normals,
                          self.num_vertices as usize)
    }

    pub fn get_normal(&self, id: u32) -> Option<Vector3D> {
        self.vertex_data(self.normals, id)
    }

    pub fn tangent_iter(&self) -> Vector3DIter {
        Vector3DIter::new(self.tangents,
                          self.num_vertices as usize)
    }

    pub fn get_tangent(&self, id: u32) -> Option<Vector3D> {
        self.vertex_data(self.tangents, id)
    }

    pub fn bitangent_iter(&self) -> Vector3DIter {
        Vector3DIter::new(self.bitangents,
                          self.num_vertices as usize)
    }

    pub fn get_bitangent(&self, id: u32) -> Option<Vector3D> {
        self.vertex_data(self.bitangents, id)
    }

    pub fn vertex_color_iter(&self, set_id: usize) -> Color4DIter {
        Color4DIter::new(self.colors[set_id],
                         self.num_vertices as usize)
    }

    pub fn get_vertex_color(&self, set_id: usize, id: u32) -> Option<Color4D> {
        self.color_data(self.colors[set_id], id)
    }

    pub fn texture_coords_iter(&self, channel_id: usize) -> Vector3DIter {
        Vector3DIter::new(self.texture_coords[channel_id],
                          self.num_vertices as usize)
    }

    pub fn get_texture_coord(&self, channel_id: usize, id: u32) -> Option<Vector3D> {
        self.vertex_data(self.texture_coords[channel_id], id)
    }

    pub fn num_faces(&self) -> u32 {
        self.num_faces
    }

    pub fn face_iter(&self) -> FaceIter {
        FaceIter::new(self.faces,
                      self.num_faces as usize)
    }

    pub fn get_face(&self, id: u32) -> Option<Face> {
        if id < self.num_faces {
            unsafe { Some(Face::from_raw(self.faces.offset(id as isize))) }
        } else {
            None
        }
    }

    pub fn num_bones(&self) -> u32 {
        self.num_bones
    }

    pub fn bone_iter(&self) -> BoneIter {
        BoneIter::new(self.bones as *const *const AiBone,
                      self.num_bones as usize)
    }

    pub fn get_bone(&self, id: u32) -> Option<Bone> {
        if id < self.num_bones {
            unsafe { Some(Bone::from_raw(*(self.bones.offset(id as isize)))) }
        } else {
            None
        }
    }

    #[inline]
    fn vertex_data(&self, array: *mut AiVector3D, id: u32) -> Option<Vector3D> {
        if id < self.num_vertices {
            unsafe { Some(Vector3D::from_raw(array.offset(id as isize))) }
        } else {
            None
        }
    }

    #[inline]
    fn color_data(&self, array: *mut AiColor4D, id: u32) -> Option<Color4D> {
        if id < self.num_vertices {
            unsafe { Some(Color4D::from_raw(array.offset(id as isize))) }
        } else {
            None
        }
    }
}

impl<'a> Bone<'a> {
    /// Returns the name of the bone.
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }

    /// Returns the bones's offset transformation matrix.
    pub fn offset_matrix(&self) -> Matrix4x4 {
        Matrix4x4::from_raw(&self.offset_matrix)
    }

    pub fn num_weights(&self) -> u32 {
        self.num_weights
    }

    pub fn weight_iter(&self) -> VertexWeightIter {
        VertexWeightIter::new(self.weights as *const *const AiVertexWeight,
                      self.num_weights as usize)
    }

    pub fn get_weight(&self, id: u32) -> Option<VertexWeight> {
        if id < self.num_weights {
            unsafe { Some(VertexWeight::from_raw(self.weights.offset(id as isize))) }
        } else {
            None
        }
    }
}