alloy/math/mat/
col4.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
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
use std::ops::{Deref, DerefMut, Index, IndexMut};

use crate::core::StridedSlice;
use crate::math::vec::{Vec4, Vector4};
use crate::ops::Dot;

/// A column of a 4x4 matrix.
#[repr(transparent)]
pub struct Col4(StridedSlice<f32, 4>);

// Constructors

impl Col4 {
  /// Creates a new column from a slice.
  ///
  /// # Safety
  ///
  /// This function is unsafe because the caller must ensure that the slice is
  /// at least 16 elements long.
  ///
  /// # Panics
  ///
  /// This function will panic if the slice is not at least 4 elements long.
  #[inline(always)]
  #[allow(unused)] // This is used in tests
  pub(crate) unsafe fn from_slice(slice: &[f32]) -> &Self {
    std::mem::transmute(StridedSlice::<f32, 4>::from_slice(slice))
  }

  /// Creates a new column from a raw pointer and a length.
  ///
  /// # Safety
  ///
  /// This function is unsafe because the caller must ensure that the pointer is
  /// valid for `len` elements.
  ///
  /// # Parameters
  ///
  /// * `ptr` - A raw pointer to the start of the column.
  /// * `len` - The number of elements in the column.
  #[inline(always)]
  pub(crate) const unsafe fn from_raw_parts<'a>(ptr: *const f32, len: usize) -> &'a Self {
    std::mem::transmute(StridedSlice::<f32, 4>::from_raw_parts(ptr, len))
  }

  /// Creates a new column from a raw pointer and a length.
  /// This function will return a mutable column.
  ///
  /// # Safety
  ///
  /// This function is unsafe because the caller must ensure that the pointer is
  /// valid for `len` elements.
  ///
  /// # Parameters
  ///
  /// * `ptr` - A raw pointer to the start of the column.
  /// * `len` - The number of elements in the column.
  #[inline(always)]
  pub(crate) unsafe fn from_raw_parts_mut<'a>(ptr: *mut f32, len: usize) -> &'a mut Self {
    std::mem::transmute(StridedSlice::<f32, 4>::from_raw_parts_mut(ptr, len))
  }
}

// Conversions

impl Col4 {
  /// Converts the column into a 4D owning vector.
  pub const fn to_vector4(&self) -> Vector4 {
    // Safety: every validly-constructed column has exactly 4 addressable elements.
    unsafe {
      Vector4::new(
        *self.index_unchecked(0),
        *self.index_unchecked(1),
        *self.index_unchecked(2),
        *self.index_unchecked(3),
      )
    }
  }
}

impl Deref for Col4 {
  type Target = StridedSlice<f32, 4>;

  #[inline(always)]
  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl DerefMut for Col4 {
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

// Modifiers

impl Col4 {
  /// Sets the column to the content of the other vector.
  ///
  /// # Parameters
  ///
  /// * `vec` - The vector to set the column to.
  pub fn set_vec4(&mut self, vec: &Vec4) {
    self.0[0] = vec.x();
    self.0[1] = vec.y();
    self.0[2] = vec.z();
    self.0[3] = vec.w();
  }
}

// Indexing

impl Col4 {
  /// Returns the value at the specified index without doing any bounds checking.
  ///
  /// # Safety
  ///
  /// This function is unsafe because it does not check that the index is less
  /// than the length of the column.
  ///
  /// # Parameters
  ///
  /// * `index` - The index of the value to return.
  pub const unsafe fn index_unchecked(&self, index: usize) -> &f32 {
    self.0.index_unchecked(index)
  }

  /// Returns a mutable reference to the value at the specified index without doing
  /// any bounds checking.
  ///
  /// # Safety
  ///
  /// This function is unsafe because it does not check that the index is less than
  /// the length of the column.
  ///
  /// # Parameters
  ///
  /// * `index` - The index of the value to return.
  pub unsafe fn index_mut_unchecked(&mut self, index: usize) -> &mut f32 {
    self.0.index_mut_unchecked(index)
  }
}

impl Index<usize> for Col4 {
  type Output = f32;

  #[inline(always)]
  fn index(&self, index: usize) -> &Self::Output {
    &self.0[index]
  }
}

impl IndexMut<usize> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, index: usize) -> &mut Self::Output {
    &mut self.0[index]
  }
}

impl Index<core::ops::Range<usize>> for Col4 {
  type Output = Col4;

  #[inline(always)]
  fn index(&self, index: core::ops::Range<usize>) -> &Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&self.0[index]) }
  }
}

impl IndexMut<core::ops::Range<usize>> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, index: core::ops::Range<usize>) -> &mut Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&mut self.0[index]) }
  }
}

impl Index<core::ops::RangeTo<usize>> for Col4 {
  type Output = Col4;

  #[inline(always)]
  fn index(&self, index: core::ops::RangeTo<usize>) -> &Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&self.0[index]) }
  }
}

impl IndexMut<core::ops::RangeTo<usize>> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, index: core::ops::RangeTo<usize>) -> &mut Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&mut self.0[index]) }
  }
}

impl Index<core::ops::RangeFrom<usize>> for Col4 {
  type Output = Col4;

  #[inline(always)]
  fn index(&self, index: core::ops::RangeFrom<usize>) -> &Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&self.0[index]) }
  }
}

impl IndexMut<core::ops::RangeFrom<usize>> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, index: core::ops::RangeFrom<usize>) -> &mut Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&mut self.0[index]) }
  }
}

impl Index<core::ops::RangeInclusive<usize>> for Col4 {
  type Output = Col4;

  #[inline(always)]
  fn index(&self, index: core::ops::RangeInclusive<usize>) -> &Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&self.0[index]) }
  }
}

impl IndexMut<core::ops::RangeInclusive<usize>> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, index: core::ops::RangeInclusive<usize>) -> &mut Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&mut self.0[index]) }
  }
}

impl Index<core::ops::RangeFull> for Col4 {
  type Output = Col4;

  #[inline(always)]
  fn index(&self, _: core::ops::RangeFull) -> &Self::Output {
    self
  }
}

impl IndexMut<core::ops::RangeFull> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, _: core::ops::RangeFull) -> &mut Self::Output {
    self
  }
}

impl Index<core::ops::RangeToInclusive<usize>> for Col4 {
  type Output = Col4;

  #[inline(always)]
  fn index(&self, index: core::ops::RangeToInclusive<usize>) -> &Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&self.0[index]) }
  }
}

impl IndexMut<core::ops::RangeToInclusive<usize>> for Col4 {
  #[inline(always)]
  fn index_mut(&mut self, index: core::ops::RangeToInclusive<usize>) -> &mut Self::Output {
    // Safety: the column is guaranteed to have 4 elements.
    unsafe { std::mem::transmute(&mut self.0[index]) }
  }
}

// Operation

impl Dot for Col4 {
  type Output = f32;

  #[inline(always)]
  fn dot(&self, other: &Self) -> Self::Output {
    self.iter().zip(other.iter()).map(|(a, b)| a * b).sum()
  }
}

impl Dot<Vec4> for Col4 {
  type Output = f32;

  #[inline(always)]
  fn dot(&self, other: &Vec4) -> Self::Output {
    self.iter().zip(other.iter()).map(|(a, b)| a * b).sum()
  }
}

impl Dot<Col4> for Vec4 {
  type Output = f32;

  #[inline(always)]
  fn dot(&self, other: &Col4) -> Self::Output {
    self.iter().zip(other.iter()).map(|(a, b)| a * b).sum()
  }
}

#[cfg(test)]
#[path = "col4.test.rs"]
mod test;