1use crate::conversion::Conversion;
2
3#[repr(C)]
4#[derive(Copy, Clone)]
5pub struct Vector4 {
6 v: [f32; 4]
7}
8
9impl Vector4 {
10 pub fn new(x: f32, y: f32, z: f32, w: f32) -> Vector4 {
11 Self {
12 v: [
13 x, y, z, w
14 ]
15 }
16 }
17
18 pub fn get_vector(&self) -> [f32; 4] {
19 self.v
20 }
21
22 pub fn get_vector_mut(&mut self) -> &mut [f32; 4] {
23 &mut self.v
24 }
25
26 pub fn x(&self) -> f32 {
27 self.v[0]
28 }
29
30 pub fn y(&self) -> f32 {
31 self.v[1]
32 }
33
34 pub fn z(&self) -> f32 {
35 self.v[2]
36 }
37
38 pub fn w(&self) -> f32 {
39 self.v[3]
40 }
41
42 pub fn length(&self) -> f32 {
43 (self.x() * self.x() + self.y() * self.y() + self.z() * self.z()).sqrt()
44 }
45
46 pub fn dot(&self, v: &Vector4) -> f32 {
47 self.x() * v.x() + self.y() * v.y() + self.z() * v.z() + self.w() * v.w()
48 }
49
50 pub fn normalize(&self) -> Vector4 {
51 if self.length() <= 0.0 {
52 return 0.0.convert()
53 }
54 return Vector4::new(
55 self.x() / self.length(),
56 self.y() / self.length(),
57 self.z() / self.length(),
58 self.w() / self.length()
59 )
60 }
61}
62
63impl std::ops::Add for Vector4 {
64 type Output = Self;
65
66 fn add(self, rhs: Self) -> Self::Output {
67 Self {
68 v: [
69 self.x() + rhs.x(),
70 self.y() + rhs.y(),
71 self.z() + rhs.z(),
72 self.w() + rhs.w(),
73 ]
74 }
75 }
76}
77
78impl std::ops::AddAssign for Vector4 {
79 fn add_assign(&mut self, rhs: Self) {
80 *self = Self {
81 v: [
82 self.x() + rhs.x(),
83 self.y() + rhs.y(),
84 self.z() + rhs.z(),
85 self.w() + rhs.w()
86 ]
87 }
88 }
89}
90
91impl std::ops::Sub for Vector4 {
92 type Output = Self;
93
94 fn sub(self, rhs: Self) -> Self::Output {
95 Self {
96 v: [
97 self.x() - rhs.x(),
98 self.y() - rhs.y(),
99 self.z() - rhs.z(),
100 self.w() - rhs.w()
101 ]
102 }
103 }
104}
105
106impl std::ops::SubAssign for Vector4 {
107 fn sub_assign(&mut self, rhs: Self) {
108 *self = Self {
109 v: [
110 self.x() - rhs.x(),
111 self.y() - rhs.y(),
112 self.z() - rhs.z(),
113 self.w() - rhs.w()
114 ]
115 }
116 }
117}
118
119impl std::ops::Mul for Vector4 {
120 type Output = Self;
121
122 fn mul(self, rhs: Self) -> Self::Output {
123 Self {
124 v: [
125 self.x() * rhs.x(),
126 self.y() * rhs.y(),
127 self.z() * rhs.z(),
128 self.w() * rhs.w()
129 ]
130 }
131 }
132}
133
134impl std::ops::MulAssign for Vector4 {
135 fn mul_assign(&mut self, rhs: Self) {
136 *self = Self {
137 v: [
138 self.x() * rhs.x(),
139 self.y() * rhs.y(),
140 self.z() * rhs.z(),
141 self.w() * rhs.w()
142 ]
143 }
144 }
145}
146
147impl std::ops::Div for Vector4 {
148 type Output = Self;
149
150 fn div(self, rhs: Self) -> Self::Output {
151 Self {
152 v: [
153 self.x() / rhs.x(),
154 self.y() / rhs.y(),
155 self.z() / rhs.z(),
156 self.w() / rhs.w()
157 ]
158 }
159 }
160}
161
162impl std::ops::DivAssign for Vector4 {
163 fn div_assign(&mut self, rhs: Self) {
164 *self = Self {
165 v: [
166 self.x() / rhs.x(),
167 self.y() / rhs.y(),
168 self.z() / rhs.z(),
169 self.w() / rhs.w()
170 ]
171 }
172 }
173}