sprocket_engine 0.2.1

A vulkan game engine
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use super::vec2::Vec2;
use std::ops;
/// Representation of 3D vectors and points
#[derive(Copy, Clone, PartialEq)]
pub struct Vec3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

impl Vec3 {
    /// Creates a vector given x,y,z
    pub fn new(x: f32, y: f32, z: f32) -> Self {
        Vec3 { x, y, z }
    }

    pub fn from_vec3(xy: Vec2, z: f32) -> Self {
        Vec3 {
            x: xy.x,
            y: xy.y,
            z,
        }
    }

    /// Creates a vector with all components being zero
    pub fn zero() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        }
    }

    /// Creates a vector with all components being one
    pub fn one() -> Self {
        Self {
            x: 1.0,
            y: 1.0,
            z: 1.0,
        }
    }

    /// Creates a vector with z = 1
    pub fn forward() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 1.0,
        }
    }

    /// Creates a vector with x = 1
    pub fn right() -> Self {
        Self {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        }
    }

    /// Creates a vector with y = 1
    pub fn up() -> Self {
        Self {
            x: 0.0,
            y: 1.0,
            z: 0.0,
        }
    }

    /// Returns the dot product of two vectors.
    /// The dot product is the cosine of the angle between a, b.
    ///
    /// # Example outputs assuming a, b are normalized
    /// 1: vectors are pointing in the same direction<br>
    /// 0: vectors are perpendicular<br>
    /// -1: vectors are opposite<br>
    #[inline]
    pub fn dot(a: &Self, b: &Self) -> f32 {
        a.x * b.x + a.y * b.y + a.z * b.z
    }

    /// Returns the cross product of two vectors, I.e; a vector perpendicular to both input vectors
    #[inline]
    pub fn cross(a: &Self, b: &Self) -> Self {
        Self {
            x: a.y * b.z - a.z * b.y,
            y: a.z * b.x - a.x * b.z,
            z: a.x * b.y - a.y * b.x,
        }
    }

    /// Reflects a vector about a normal
    #[inline]
    pub fn reflect(ray: Self, normal: Self) -> Self {
        let n = normal.norm();
        ray - n * Self::dot(&ray, &n) * 2.0
    }

    /// Project a vector onto another
    #[inline]
    pub fn project(a: Self, b: Self) -> Self {
        let b = b.norm();
        b * Self::dot(&a, &b)
    }

    /// Projects a vector onto a plane
    #[inline]
    pub fn project_plane(a: Self, normal: Self) -> Self {
        let normal = normal.norm();
        a - normal * Self::dot(&a, &normal)
    }

    /// Linearly interpolates between two vectors with t.<br>
    /// When t > 1, lerp(a, b) = b<br>
    /// When t = 0, lerp(a, b) = t<br>
    /// Clamps t between 0, 1
    #[inline]
    pub fn lerp(a: Self, b: Self, t: f32) -> Self {
        let t = if t < 0.0 {
            0.0
        } else if t > 1.0 {
            1.0
        } else {
            t
        };
        a * (1.0 - t) + b * t
    }

    /// Linearly interpolates between two vectors with t.<br>
    /// When t > 1, lerp(a, b) = b<br>
    /// When t = 0, lerp(a, b) = t<br>
    /// Does not clamp t between 0, 1
    #[inline]
    pub fn lerp_unclamped(a: Self, b: Self, t: f32) -> Self {
        a * (1.0 - t) + b * t
    }

    // Instance method

    /// Returns the magnitude/length of the vector
    /// Note: for comparing two vectors, sqrmag is faster
    #[inline]
    pub fn mag(&self) -> f32 {
        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
    }

    /// Returns the squared length of the vector
    /// Is faster than mag due to not using sqrt
    #[inline]
    pub fn sqrmag(&self) -> f32 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    /// Returns the normal version the vector
    #[inline]
    pub fn norm(&self) -> Vec3 {
        *self / self.mag()
    }

    /// Returns the smallest component
    pub fn smallest(&self) -> f32 {
        if self.x < self.y {
            if self.x < self.z {
                return self.x;
            }
            return self.z;
        } else if self.y < self.x {
            if self.y < self.z {
                return self.y;
            }
            return self.z;
        } else if self.z < self.x {
            if self.z < self.y {
                return self.z;
            }
            return self.y;
        }
        // All are equal
        self.x
    }

    /// Returns the largest component
    pub fn largest(&self) -> f32 {
        if self.x > self.y {
            if self.x > self.z {
                return self.x;
            }
            return self.z;
        } else if self.y > self.x {
            if self.y > self.z {
                return self.y;
            }
            return self.z;
        } else if self.z > self.x {
            if self.z > self.y {
                return self.z;
            }
            return self.y;
        }
        // All are equal
        self.x
    }

    #[inline]
    pub fn xy(&self) -> Vec2 {
        Vec2 {
            x: self.x,
            y: self.y,
        }
    }
}

impl std::fmt::Display for Vec3 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}, {}, {})", self.x, self.y, self.z)
    }
}

impl std::fmt::Debug for Vec3 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}, {}, {})", self.x, self.y, self.z)
    }
}

// Math operators

/// Adds two vectors component wise
impl ops::Add for Vec3 {
    type Output = Self;
    #[inline]
    fn add(self, other: Self) -> Self {
        Self {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}

/// Compound addition to vector component wise
impl ops::AddAssign for Vec3 {
    #[inline]
    fn add_assign(&mut self, other: Self) {
        self.x += other.x;
        self.y += other.y;
        self.z += other.z;
    }
}

/// Subtracts two vectors component wise
impl ops::Sub for Vec3 {
    type Output = Self;
    #[inline]
    fn sub(self, other: Self) -> Self {
        Self {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

/// Compound subtraction to vector component wise
impl ops::SubAssign for Vec3 {
    #[inline]
    fn sub_assign(&mut self, other: Self) {
        self.x -= other.x;
        self.y -= other.y;
        self.z -= other.z;
    }
}

/// Multiplies two vectors component wise
impl ops::Mul for Vec3 {
    type Output = Vec3;
    #[inline]
    fn mul(self, other: Self) -> Self {
        Vec3 {
            x: self.x * other.x,
            y: self.y * other.y,
            z: self.z * other.z,
        }
    }
}

// Compound multiplies two vectors component wise
impl ops::MulAssign for Vec3 {
    #[inline]
    fn mul_assign(&mut self, other: Self) {
        self.x *= other.x;
        self.y *= other.y;
        self.z *= other.z;
    }
}

/// Negates the vector
impl ops::Neg for Vec3 {
    type Output = Vec3;
    #[inline]
    fn neg(self) -> Vec3 {
        Vec3 {
            x: -self.x,
            y: -self.y,
            z: -self.z,
        }
    }
}

/// Multiplies the length of the vector by rhs
impl ops::Mul<f32> for Vec3 {
    type Output = Vec3;
    #[inline]
    fn mul(self, rhs: f32) -> Vec3 {
        Vec3 {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

/// Compound multiplies the length of the vector
impl ops::MulAssign<f32> for Vec3 {
    #[inline]
    fn mul_assign(&mut self, rhs: f32) {
        self.x *= rhs;
        self.y *= rhs;
        self.z *= rhs;
    }
}

/// Divides the length of the vector by rhs
impl ops::Div<f32> for Vec3 {
    type Output = Vec3;
    #[inline]
    fn div(self, rhs: f32) -> Self {
        Vec3 {
            x: self.x / rhs,
            y: self.y / rhs,
            z: self.z / rhs,
        }
    }
}

/// Compound divides the length of the vector by rhs
impl ops::DivAssign<f32> for Vec3 {
    #[inline]
    fn div_assign(&mut self, rhs: f32) {
        self.x /= rhs;
        self.y /= rhs;
        self.z /= rhs;
    }
}

impl From<(f32, f32, f32)> for Vec3 {
    fn from(t: (f32, f32, f32)) -> Self {
        Vec3 {
            x: t.0,
            y: t.1,
            z: t.2,
        }
    }
}

impl From<(i32, i32, i32)> for Vec3 {
    fn from(t: (i32, i32, i32)) -> Self {
        Vec3 {
            x: t.0 as f32,
            y: t.1 as f32,
            z: t.2 as f32,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn ops() {
        assert_eq!(
            Vec3::new(1.0, 2.0, 3.0) + Vec3::new(4.0, -3.0, 5.0),
            Vec3::new(5.0, -1.0, 8.0)
        );

        assert_eq!(
            Vec3::new(1.0, 2.0, 3.0) - Vec3::new(4.0, -3.0, -0.0),
            Vec3::new(-3.0, 5.0, 3.0)
        );

        assert_eq!(
            Vec3::new(1.0, 2.0, 3.0) * Vec3::new(4.0, -3.0, 0.5),
            Vec3::new(4.0, -6.0, 1.5)
        );
    }

    #[test]
    fn dot() {
        assert_eq!(
            Vec3::dot(&Vec3::new(4.0, 0.0, 0.0).norm(), &Vec3::new(0.0, 0.0, 2.0)),
            0.0
        );

        assert_eq!(
            Vec3::dot(&Vec3::new(4.0, 0.0, 4.0).norm(), &Vec3::new(1.0, 0.0, 0.0)),
            1.0 / 2_f32.sqrt()
        );
    }
}