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
use na::{Scalar, Real, DefaultAllocator, Quaternion};

use traits::{Number, Alloc, Dimension};
use aliases::{Qua, TMat, TMat2, TMat3, TMat4, TVec1, TVec2, TVec3, TVec4,
              TMat2x3, TMat2x4, TMat3x2, TMat3x4, TMat4x2, TMat4x3};

/// Creates a 2x2 matrix from a slice arranged in column-major order.
pub fn make_mat2<N: Scalar>(ptr: &[N]) -> TMat2<N> {
    TMat2::from_column_slice(ptr)
}

/// Creates a 2x2 matrix from a slice arranged in column-major order.
pub fn make_mat2x2<N: Scalar>(ptr: &[N]) -> TMat2<N> {
    TMat2::from_column_slice(ptr)
}

/// Creates a 2x3 matrix from a slice arranged in column-major order.
pub fn make_mat2x3<N: Scalar>(ptr: &[N]) -> TMat2x3<N> {
    TMat2x3::from_column_slice(ptr)
}

/// Creates a 2x4 matrix from a slice arranged in column-major order.
pub fn make_mat2x4<N: Scalar>(ptr: &[N]) -> TMat2x4<N> {
    TMat2x4::from_column_slice(ptr)
}

/// Creates a 3 matrix from a slice arranged in column-major order.
pub fn make_mat3<N: Scalar>(ptr: &[N]) -> TMat3<N> {
    TMat3::from_column_slice(ptr)
}

/// Creates a 3x2 matrix from a slice arranged in column-major order.
pub fn make_mat3x2<N: Scalar>(ptr: &[N]) -> TMat3x2<N> {
    TMat3x2::from_column_slice(ptr)
}

/// Creates a 3x3 matrix from a slice arranged in column-major order.
pub fn make_mat3x3<N: Scalar>(ptr: &[N]) -> TMat3<N> {
    TMat3::from_column_slice(ptr)
}

/// Creates a 3x4 matrix from a slice arranged in column-major order.
pub fn make_mat3x4<N: Scalar>(ptr: &[N]) -> TMat3x4<N> {
    TMat3x4::from_column_slice(ptr)
}

/// Creates a 4x4 matrix from a slice arranged in column-major order.
pub fn make_mat4<N: Scalar>(ptr: &[N]) -> TMat4<N> {
    TMat4::from_column_slice(ptr)
}

/// Creates a 4x2 matrix from a slice arranged in column-major order.
pub fn make_mat4x2<N: Scalar>(ptr: &[N]) -> TMat4x2<N> {
    TMat4x2::from_column_slice(ptr)
}

/// Creates a 4x3 matrix from a slice arranged in column-major order.
pub fn make_mat4x3<N: Scalar>(ptr: &[N]) -> TMat4x3<N> {
    TMat4x3::from_column_slice(ptr)
}

/// Creates a 4x4 matrix from a slice arranged in column-major order.
pub fn make_mat4x4<N: Scalar>(ptr: &[N]) -> TMat4<N> {
    TMat4::from_column_slice(ptr)
}

/// Converts a 2x2 matrix to a 3x3 matrix.
pub fn mat2_to_mat3<N: Number>(m: &TMat2<N>) -> TMat3<N> {
    let _0 = N::zero();
    let _1 = N::one();

    TMat3::new(
        m.m11, m.m12, _0,
        m.m21, m.m22, _0,
        _0, _0, _1
    )
}

/// Converts a 3x3 matrix to a 2x2 matrix.
pub fn mat3_to_mat2<N: Scalar>(m: &TMat3<N>) -> TMat2<N> {
    TMat2::new(
        m.m11, m.m12,
        m.m21, m.m22
    )
}

/// Converts a 3x3 matrix to a 4x4 matrix.
pub fn mat3_to_mat4<N: Number>(m: &TMat3<N>) -> TMat4<N> {
    let _0 = N::zero();
    let _1 = N::one();

    TMat4::new(
        m.m11, m.m12, m.m13, _0,
        m.m21, m.m22, m.m23, _0,
        m.m31, m.m32, m.m33, _0,
        _0, _0, _0, _1,
    )
}

/// Converts a 4x4 matrix to a 3x3 matrix.
pub fn mat4_to_mat3<N: Scalar>(m: &TMat4<N>) -> TMat3<N> {
    TMat3::new(
        m.m11, m.m12, m.m13,
        m.m21, m.m22, m.m23,
        m.m31, m.m32, m.m33,
    )
}

/// Converts a 2x2 matrix to a 4x4 matrix.
pub fn mat2_to_mat4<N: Number>(m: &TMat2<N>) -> TMat4<N> {
    let _0 = N::zero();
    let _1 = N::one();

    TMat4::new(
        m.m11, m.m12, _0, _0,
        m.m21, m.m22, _0, _0,
        _0, _0, _1, _0,
        _0, _0, _0, _1,
    )
}

/// Converts a 4x4 matrix to a 2x2 matrix.
pub fn mat4_to_mat2<N: Scalar>(m: &TMat4<N>) -> TMat2<N> {
    TMat2::new(
        m.m11, m.m12,
        m.m21, m.m22,
    )
}

/// Creates a quaternion from a slice arranged as `[x, y, z, w]`.
pub fn make_quat<N: Real>(ptr: &[N]) -> Qua<N> {
    Quaternion::from_vector(TVec4::from_column_slice(ptr))
}

/// Creates a 1D vector from a slice.
pub fn make_vec1<N: Scalar>(v: &TVec1<N>) -> TVec1<N> {
    *v
}

/// Creates a 1D vector from another vector.
pub fn vec2_to_vec1<N: Scalar>(v: &TVec2<N>) -> TVec1<N> {
    TVec1::new(v.x)
}

/// Creates a 1D vector from another vector.
pub fn vec3_to_vec1<N: Scalar>(v: &TVec3<N>) -> TVec1<N> {
    TVec1::new(v.x)
}

/// Creates a 1D vector from another vector.
pub fn vec4_to_vec1<N: Scalar>(v: &TVec4<N>) -> TVec1<N> {
    TVec1::new(v.x)
}

/// Creates a 2D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn vec1_to_vec2<N: Number>(v: &TVec1<N>) -> TVec2<N> {
    TVec2::new(v.x, N::zero())
}

/// Creates a 2D vector from another vector.
pub fn vec2_to_vec2<N: Scalar>(v: &TVec2<N>) -> TVec2<N> {
    *v
}

/// Creates a 2D vector from another vector.
pub fn vec3_to_vec2<N: Scalar>(v: &TVec3<N>) -> TVec2<N> {
    TVec2::new(v.x, v.y)
}

/// Creates a 2D vector from another vector.
pub fn vec4_to_vec2<N: Scalar>(v: &TVec4<N>) -> TVec2<N> {
    TVec2::new(v.x, v.y)
}

/// Creates a 2D vector from a slice.
pub fn make_vec2<N: Scalar>(ptr: &[N]) -> TVec2<N> {
    TVec2::from_column_slice(ptr)
}

/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn vec1_to_vec3<N: Number>(v: &TVec1<N>) -> TVec3<N> {
    TVec3::new(v.x, N::zero(), N::zero())
}

/// Creates a 3D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn vec2_to_vec3<N: Number>(v: &TVec2<N>) -> TVec3<N> {
    TVec3::new(v.x, v.y, N::zero())
}

/// Creates a 3D vector from another vector.
pub fn vec3_to_vec3<N: Scalar>(v: &TVec3<N>) -> TVec3<N> {
    *v
}

/// Creates a 3D vector from another vector.
pub fn vec4_to_vec3<N: Scalar>(v: &TVec4<N>) -> TVec3<N> {
    TVec3::new(v.x, v.y, v.z)
}

/// Creates a 3D vector from another vector.
pub fn make_vec3<N: Scalar>(ptr: &[N]) -> TVec3<N> {
    TVec3::from_column_slice(ptr)
}

/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn vec1_to_vec4<N: Number>(v: &TVec1<N>) -> TVec4<N> {
    TVec4::new(v.x, N::zero(), N::zero(), N::zero())
}

/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn vec2_to_vec4<N: Number>(v: &TVec2<N>) -> TVec4<N> {
    TVec4::new(v.x, v.y, N::zero(), N::zero())
}

/// Creates a 4D vector from another vector.
///
/// Missing components, if any, are set to 0.
pub fn vec3_to_vec4<N: Number>(v: &TVec3<N>) -> TVec4<N> {
    TVec4::new(v.x, v.y, v.z, N::zero())
}

/// Creates a 4D vector from another vector.
pub fn vec4_to_vec4<N: Scalar>(v: &TVec4<N>) -> TVec4<N> {
    *v
}

/// Creates a 4D vector from another vector.
pub fn make_vec4<N: Scalar>(ptr: &[N]) -> TVec4<N> {
    TVec4::from_column_slice(ptr)
}

/// Converts a matrix or vector to a slice arranged in column-major order.
pub fn value_ptr<N: Scalar, R: Dimension, C: Dimension>(x: &TMat<N, R, C>) -> &[N]
    where DefaultAllocator: Alloc<N, R, C> {
    x.as_slice()
}

/// Converts a matrix or vector to a mutable slice arranged in column-major order.
pub fn value_ptr_mut<N: Scalar, R: Dimension, C: Dimension>(x: &mut TMat<N, R, C>) -> &mut [N]
    where DefaultAllocator: Alloc<N, R, C> {
    x.as_mut_slice()
}