Skip to content

Commit bfd2309

Browse files
committed
Move Vec3 into a math module
1 parent 1284ddd commit bfd2309

File tree

2 files changed

+159
-155
lines changed

2 files changed

+159
-155
lines changed

src/lib.rs

Lines changed: 4 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ use nom::{
8282
IResult,
8383
};
8484

85+
mod math;
86+
87+
use math::Vec3;
88+
8589
// LDraw File Format Specification
8690
// https://www.ldraw.org/article/218.html
8791

@@ -436,161 +440,6 @@ pub struct Comment {
436440
text: str
437441
}
438442

439-
/// Generic 3-component vector.
440-
#[derive(Debug, Copy, Clone, PartialEq)]
441-
pub struct Vec3 {
442-
pub x: f32,
443-
pub y: f32,
444-
pub z: f32
445-
}
446-
447-
impl Vec3 {
448-
pub fn dot(&self, other: &Vec3) -> f32 {
449-
self.x * other.x + self.y * other.y + self.z * other.z
450-
}
451-
452-
pub fn normalized(&self) -> Vec3 {
453-
let invsqrt : f32 = self.dot(self).sqrt().recip();
454-
self * invsqrt
455-
}
456-
}
457-
458-
impl std::ops::Add<Vec3> for Vec3 {
459-
type Output = Vec3;
460-
461-
fn add(self, other: Vec3) -> Vec3 {
462-
Vec3 {
463-
x: self.x + other.x,
464-
y: self.y + other.y,
465-
z: self.z + other.z
466-
}
467-
}
468-
}
469-
470-
impl std::ops::Add<&Vec3> for Vec3 {
471-
type Output = Vec3;
472-
473-
fn add(self, other: &Vec3) -> Vec3 {
474-
Vec3 {
475-
x: self.x + other.x,
476-
y: self.y + other.y,
477-
z: self.z + other.z
478-
}
479-
}
480-
}
481-
482-
impl<'a> std::ops::Add<&Vec3> for &'a Vec3 {
483-
type Output = Vec3;
484-
485-
fn add(self, other: &Vec3) -> Vec3 {
486-
Vec3 {
487-
x: self.x + other.x,
488-
y: self.y + other.y,
489-
z: self.z + other.z
490-
}
491-
}
492-
}
493-
494-
impl<'a> std::ops::Add<Vec3> for &'a Vec3 {
495-
type Output = Vec3;
496-
497-
fn add(self, other: Vec3) -> Vec3 {
498-
Vec3 {
499-
x: self.x + other.x,
500-
y: self.y + other.y,
501-
z: self.z + other.z
502-
}
503-
}
504-
}
505-
506-
impl std::ops::Add<f32> for Vec3 {
507-
type Output = Vec3;
508-
509-
fn add(self, other: f32) -> Vec3 {
510-
Vec3 {
511-
x: self.x + other,
512-
y: self.y + other,
513-
z: self.z + other
514-
}
515-
}
516-
}
517-
518-
impl<'a> std::ops::Add<f32> for &'a Vec3 {
519-
type Output = Vec3;
520-
521-
fn add(self, other: f32) -> Vec3 {
522-
Vec3 {
523-
x: self.x + other,
524-
y: self.y + other,
525-
z: self.z + other
526-
}
527-
}
528-
}
529-
530-
impl std::ops::AddAssign<Vec3> for Vec3 {
531-
fn add_assign(&mut self, other: Vec3) {
532-
self.x += other.x;
533-
self.y += other.y;
534-
self.z += other.z;
535-
}
536-
}
537-
538-
impl std::ops::AddAssign<&Vec3> for Vec3 {
539-
fn add_assign(&mut self, other: &Vec3) {
540-
self.x += other.x;
541-
self.y += other.y;
542-
self.z += other.z;
543-
}
544-
}
545-
546-
impl<'a> std::ops::Sub<&Vec3> for &'a Vec3 {
547-
type Output = Vec3;
548-
549-
fn sub(self, other: &Vec3) -> Vec3 {
550-
Vec3 {
551-
x: self.x - other.x,
552-
y: self.y - other.y,
553-
z: self.z - other.z
554-
}
555-
}
556-
}
557-
558-
impl<'a> std::ops::Mul<&Vec3> for &'a Vec3 {
559-
type Output = Vec3;
560-
561-
fn mul(self, other: &Vec3) -> Vec3 {
562-
Vec3 {
563-
x: self.x * other.x,
564-
y: self.y * other.y,
565-
z: self.z * other.z
566-
}
567-
}
568-
}
569-
570-
impl std::ops::Mul<f32> for Vec3 {
571-
type Output = Vec3;
572-
573-
fn mul(self, other: f32) -> Vec3 {
574-
Vec3 {
575-
x: self.x * other,
576-
y: self.y * other,
577-
z: self.z * other
578-
}
579-
}
580-
}
581-
582-
impl<'a> std::ops::Mul<f32> for &'a Vec3 {
583-
type Output = Vec3;
584-
585-
fn mul(self, other: f32) -> Vec3 {
586-
Vec3 {
587-
x: self.x * other,
588-
y: self.y * other,
589-
z: self.z * other
590-
}
591-
}
592-
}
593-
594443
#[derive(Debug, PartialEq)]
595444
pub struct SourceFile {
596445
pub filename: String,

src/math.rs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
/// Generic 3-component vector.
3+
#[derive(Debug, Copy, Clone, PartialEq)]
4+
pub struct Vec3 {
5+
pub x: f32,
6+
pub y: f32,
7+
pub z: f32
8+
}
9+
10+
impl Vec3 {
11+
pub fn dot(&self, other: &Vec3) -> f32 {
12+
self.x * other.x + self.y * other.y + self.z * other.z
13+
}
14+
15+
pub fn normalized(&self) -> Vec3 {
16+
let invsqrt : f32 = self.dot(self).sqrt().recip();
17+
self * invsqrt
18+
}
19+
}
20+
21+
impl std::ops::Add<Vec3> for Vec3 {
22+
type Output = Vec3;
23+
24+
fn add(self, other: Vec3) -> Vec3 {
25+
Vec3 {
26+
x: self.x + other.x,
27+
y: self.y + other.y,
28+
z: self.z + other.z
29+
}
30+
}
31+
}
32+
33+
impl std::ops::Add<&Vec3> for Vec3 {
34+
type Output = Vec3;
35+
36+
fn add(self, other: &Vec3) -> Vec3 {
37+
Vec3 {
38+
x: self.x + other.x,
39+
y: self.y + other.y,
40+
z: self.z + other.z
41+
}
42+
}
43+
}
44+
45+
impl<'a> std::ops::Add<&Vec3> for &'a Vec3 {
46+
type Output = Vec3;
47+
48+
fn add(self, other: &Vec3) -> Vec3 {
49+
Vec3 {
50+
x: self.x + other.x,
51+
y: self.y + other.y,
52+
z: self.z + other.z
53+
}
54+
}
55+
}
56+
57+
impl<'a> std::ops::Add<Vec3> for &'a Vec3 {
58+
type Output = Vec3;
59+
60+
fn add(self, other: Vec3) -> Vec3 {
61+
Vec3 {
62+
x: self.x + other.x,
63+
y: self.y + other.y,
64+
z: self.z + other.z
65+
}
66+
}
67+
}
68+
69+
impl std::ops::Add<f32> for Vec3 {
70+
type Output = Vec3;
71+
72+
fn add(self, other: f32) -> Vec3 {
73+
Vec3 {
74+
x: self.x + other,
75+
y: self.y + other,
76+
z: self.z + other
77+
}
78+
}
79+
}
80+
81+
impl<'a> std::ops::Add<f32> for &'a Vec3 {
82+
type Output = Vec3;
83+
84+
fn add(self, other: f32) -> Vec3 {
85+
Vec3 {
86+
x: self.x + other,
87+
y: self.y + other,
88+
z: self.z + other
89+
}
90+
}
91+
}
92+
93+
impl std::ops::AddAssign<Vec3> for Vec3 {
94+
fn add_assign(&mut self, other: Vec3) {
95+
self.x += other.x;
96+
self.y += other.y;
97+
self.z += other.z;
98+
}
99+
}
100+
101+
impl std::ops::AddAssign<&Vec3> for Vec3 {
102+
fn add_assign(&mut self, other: &Vec3) {
103+
self.x += other.x;
104+
self.y += other.y;
105+
self.z += other.z;
106+
}
107+
}
108+
109+
impl<'a> std::ops::Sub<&Vec3> for &'a Vec3 {
110+
type Output = Vec3;
111+
112+
fn sub(self, other: &Vec3) -> Vec3 {
113+
Vec3 {
114+
x: self.x - other.x,
115+
y: self.y - other.y,
116+
z: self.z - other.z
117+
}
118+
}
119+
}
120+
121+
impl<'a> std::ops::Mul<&Vec3> for &'a Vec3 {
122+
type Output = Vec3;
123+
124+
fn mul(self, other: &Vec3) -> Vec3 {
125+
Vec3 {
126+
x: self.x * other.x,
127+
y: self.y * other.y,
128+
z: self.z * other.z
129+
}
130+
}
131+
}
132+
133+
impl std::ops::Mul<f32> for Vec3 {
134+
type Output = Vec3;
135+
136+
fn mul(self, other: f32) -> Vec3 {
137+
Vec3 {
138+
x: self.x * other,
139+
y: self.y * other,
140+
z: self.z * other
141+
}
142+
}
143+
}
144+
145+
impl<'a> std::ops::Mul<f32> for &'a Vec3 {
146+
type Output = Vec3;
147+
148+
fn mul(self, other: f32) -> Vec3 {
149+
Vec3 {
150+
x: self.x * other,
151+
y: self.y * other,
152+
z: self.z * other
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)