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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use super::*;

/// A 2-dimensional vector.
///
/// This is two `f32` values, `x` and `y`.
#[derive(Clone, Copy, Default, PartialEq)]
#[repr(C)]
pub struct Vec2 {
  pub(crate) x: f32,
  pub(crate) y: f32,
}
unsafe impl Zeroable for Vec2 {}
unsafe impl Pod for Vec2 {}

impl core::fmt::Debug for Vec2 {
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// Debug format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    f.write_str("Vec2 { x: ")?;
    core::fmt::Debug::fmt(&self.x, f)?;
    f.write_str(", y: ")?;
    core::fmt::Debug::fmt(&self.y, f)?;
    f.write_str(" }")
  }
}

impl core::fmt::Display for Vec2 {
  /// Display formats without labels like a 2-tuple.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// Display format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    f.write_str("(")?;
    core::fmt::Display::fmt(&self.x, f)?;
    f.write_str(", ")?;
    core::fmt::Display::fmt(&self.y, f)?;
    f.write_str(")")
  }
}

impl core::fmt::LowerExp for Vec2 {
  /// LowerExp formats like Display, but with the lower exponent.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// LowerExp format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    f.write_str("(")?;
    core::fmt::LowerExp::fmt(&self.x, f)?;
    f.write_str(", ")?;
    core::fmt::LowerExp::fmt(&self.y, f)?;
    f.write_str(")")
  }
}

impl core::fmt::UpperExp for Vec2 {
  /// UpperExp formats like Display, but with the upper exponent.
  ///
  /// Passes the formatter along to the fields, so you can use any normal `f32`
  /// UpperExp format arguments that you like.
  fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
    f.write_str("(")?;
    core::fmt::UpperExp::fmt(&self.x, f)?;
    f.write_str(", ")?;
    core::fmt::UpperExp::fmt(&self.y, f)?;
    f.write_str(")")
  }
}

impl Index<usize> for Vec2 {
  type Output = f32;
  #[inline(always)]
  fn index(&self, index: usize) -> &f32 {
    match index {
      0 => &self.x,
      1 => &self.y,
      otherwise => panic!("Vec2 index out of bounds: {}", otherwise),
    }
  }
}
impl IndexMut<usize> for Vec2 {
  #[inline(always)]
  fn index_mut(&mut self, index: usize) -> &mut f32 {
    match index {
      0 => &mut self.x,
      1 => &mut self.y,
      otherwise => panic!("Vec2 index out of bounds: {}", otherwise),
    }
  }
}

impl AsRef<[f32; 2]> for Vec2 {
  #[inline(always)]
  fn as_ref(&self) -> &[f32; 2] {
    cast_ref(self)
  }
}
impl AsMut<[f32; 2]> for Vec2 {
  #[inline(always)]
  fn as_mut(&mut self) -> &mut [f32; 2] {
    cast_mut(self)
  }
}

impl From<[f32; 2]> for Vec2 {
  #[inline]
  fn from([x, y]: [f32; 2]) -> Self {
    Self { x, y }
  }
}
impl From<Vec2> for [f32; 2] {
  #[inline]
  fn from(Vec2 { x, y }: Vec2) -> Self {
    [x, y]
  }
}

#[cfg(feature = "mint")]
impl From<mint::Vector2<f32>> for Vec2 {
  #[inline]
  fn from(mint::Vector2 { x, y }: mint::Vector2<f32>) -> Self {
    Self { x, y }
  }
}
#[cfg(feature = "mint")]
impl From<Vec2> for mint::Vector2<f32> {
  #[inline]
  fn from(Vec2 { x, y }: Vec2) -> Self {
    Self { x, y }
  }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Vec2 {
  #[inline]
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: serde::Serializer,
  {
    <[f32; 2]>::from(*self).serialize(serializer)
  }
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Vec2 {
  #[inline]
  fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  where
    D: serde::Deserializer<'de>,
  {
    Ok(Self::from(<[f32; 2]>::deserialize(deserializer)?))
  }
}

impl Add for Vec2 {
  type Output = Self;
  #[inline]
  fn add(self, rhs: Self) -> Self {
    Self {
      x: self.x + rhs.x,
      y: self.y + rhs.y,
    }
  }
}
impl Add<f32> for Vec2 {
  type Output = Self;
  #[inline]
  fn add(self, rhs: f32) -> Self {
    Self {
      x: self.x + rhs,
      y: self.y + rhs,
    }
  }
}
impl Add<Vec2> for f32 {
  type Output = Vec2;
  #[inline]
  fn add(self, rhs: Vec2) -> Vec2 {
    Vec2::splat(self) + rhs
  }
}
impl AddAssign for Vec2 {
  #[inline]
  fn add_assign(&mut self, rhs: Self) {
    *self = *self + rhs
  }
}
impl AddAssign<f32> for Vec2 {
  #[inline]
  fn add_assign(&mut self, rhs: f32) {
    *self = *self + rhs
  }
}

impl Sub for Vec2 {
  type Output = Self;
  #[inline]
  fn sub(self, rhs: Self) -> Self {
    Self {
      x: self.x - rhs.x,
      y: self.y - rhs.y,
    }
  }
}
impl Sub<f32> for Vec2 {
  type Output = Self;
  #[inline]
  fn sub(self, rhs: f32) -> Self {
    Self {
      x: self.x - rhs,
      y: self.y - rhs,
    }
  }
}
impl Sub<Vec2> for f32 {
  type Output = Vec2;
  #[inline]
  fn sub(self, rhs: Vec2) -> Vec2 {
    Vec2::splat(self) - rhs
  }
}
impl SubAssign for Vec2 {
  #[inline]
  fn sub_assign(&mut self, rhs: Self) {
    *self = *self - rhs
  }
}
impl SubAssign<f32> for Vec2 {
  #[inline]
  fn sub_assign(&mut self, rhs: f32) {
    *self = *self - rhs
  }
}

impl Neg for Vec2 {
  type Output = Self;
  #[inline]
  fn neg(self) -> Self {
    Self {
      x: -self.x,
      y: -self.y,
    }
  }
}

impl Mul<f32> for Vec2 {
  type Output = Self;
  #[inline]
  fn mul(self, rhs: f32) -> Self {
    Self {
      x: self.x * rhs,
      y: self.y * rhs,
    }
  }
}
impl Mul<Vec2> for f32 {
  type Output = Vec2;
  #[inline]
  fn mul(self, rhs: Vec2) -> Vec2 {
    rhs * self
  }
}
impl MulAssign<f32> for Vec2 {
  #[inline]
  fn mul_assign(&mut self, rhs: f32) {
    *self = *self * rhs;
  }
}

impl Mul for Vec2 {
  type Output = Self;
  /// Non-mathematical component-wise multiplication (GLSL-style)
  #[inline]
  fn mul(self, rhs: Self) -> Self {
    Self {
      x: self.x * rhs.x,
      y: self.y * rhs.y,
    }
  }
}
impl MulAssign for Vec2 {
  #[inline]
  fn mul_assign(&mut self, rhs: Self) {
    *self = *self * rhs;
  }
}

/// ## Accessors
impl Vec2 {
  /// Gets the `x` component of this vector.
  #[inline(always)]
  pub fn x(self) -> f32 {
    self.x
  }
  /// Gets the `y` component of this vector.
  #[inline(always)]
  pub fn y(self) -> f32 {
    self.y
  }
  /// `&mut` to the `x` component of this vector.
  #[inline(always)]
  pub fn x_mut(&mut self) -> &mut f32 {
    &mut self.x
  }
  /// `&mut` to the `y` component of this vector.
  #[inline(always)]
  pub fn y_mut(&mut self) -> &mut f32 {
    &mut self.y
  }
}

/// ## Constructors
impl Vec2 {
  /// Makes a new `Vec2`
  #[inline(always)]
  pub fn new(x: f32, y: f32) -> Self {
    Self { x, y }
  }

  /// Splats the given value across all components.
  #[inline]
  pub fn splat(v: f32) -> Self {
    Self { x: v, y: v }
  }

  /// Extends this 2d vec into a 3d vec with the `z` given.
  #[inline]
  pub fn to_vec3(self, z: f32) -> Vec3 {
    Vec3::from([self.x, self.y, z])
  }
}

/// ## Operations
impl Vec2 {
  /// Dot product.
  ///
  /// This is the sum of the component-wise multiplication of the two values.
  /// Order doesn't matter. Positive dot product means the vectors are pointing
  /// in the same general direction, zero dot product means they're
  /// perpendicular, and negative dot product means they have opposite general
  /// direction.
  #[inline]
  pub fn dot(self, rhs: Self) -> f32 {
    let t = self * rhs;
    t.x() + t.y()
  }

  /// The length / magnitude of the vector.
  ///
  /// * `sqrt(x^2 + y^2)`
  #[inline]
  pub fn length(self) -> f32 {
    lokacore::sqrt_f32(self.length2())
  }

  /// The squared length / magnitude of the vector.
  ///
  /// * `x^2 + y^2 + z^2`
  #[inline]
  pub fn length2(self) -> f32 {
    let sq = self * self;
    sq.x() + sq.y()
  }

  /// Generates a new vector where the length is 1.0
  ///
  /// Or, well, as close as it can get. Floating point, and all that.
  #[inline]
  pub fn normalize(self) -> Self {
    let len = self.length();
    Self::new(self.x() / len, self.y() / len)
  }
}