graphene

view src/gmath/vector.inl @ 4:d30e24132b6e

more gmath
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 07:42:30 +0300
parents
children 2ce58d5309f0
line source
1 #include <math.h>
3 namespace gph {
5 // ---- Vector3 ----
7 inline void Vector3::normalize()
8 {
9 float len = (float)sqrt(x * x + y * y + z * z);
10 if(len != 0.0f) {
11 x /= len;
12 y /= len;
13 z /= len;
14 }
15 }
17 inline float &Vector3::operator[] (int idx)
18 {
19 return idx == 0 ? x : (idx == 1 ? y : z);
20 }
22 inline const float &Vector3::operator[] (int idx) const
23 {
24 return idx == 0 ? x : (idx == 1 ? y : z);
25 }
27 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
28 {
29 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
30 }
32 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
33 {
34 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
35 }
37 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
38 {
39 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
40 }
42 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
43 {
44 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
45 }
47 inline Vector3 operator *(const Vector3 &v, float s)
48 {
49 return Vector3(v.x * s, v.y * s, v.z * s);
50 }
52 inline Vector3 operator *(float s, const Vector3 &v)
53 {
54 return Vector3(s * v.x, s * v.y, s * v.z);
55 }
57 inline Vector3 operator /(const Vector3 &v, float s)
58 {
59 return Vector3(v.x / s, v.y / s, v.z / s);
60 }
62 inline Vector3 operator /(float s, const Vector3 &v)
63 {
64 return Vector3(s / v.x, s / v.y, s / v.z);
65 }
67 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
68 {
69 a.x += b.x;
70 a.y += b.y;
71 a.z += b.z;
72 return a;
73 }
75 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
76 {
77 a.x -= b.x;
78 a.y -= b.y;
79 a.z -= b.z;
80 return a;
81 }
83 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
84 {
85 a.x *= b.x;
86 a.y *= b.y;
87 a.z *= b.z;
88 return a;
89 }
91 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
92 {
93 a.x /= b.x;
94 a.y /= b.y;
95 a.z /= b.z;
96 return a;
97 }
99 inline Vector3 &operator *=(Vector3 &v, float s)
100 {
101 v.x *= s;
102 v.y *= s;
103 v.z *= s;
104 return v;
105 }
107 inline Vector3 &operator /=(Vector3 &v, float s)
108 {
109 v.x /= s;
110 v.y /= s;
111 v.z /= s;
112 return v;
113 }
115 inline bool operator ==(const Vector3 &a, const Vector3 &b)
116 {
117 return a.x == b.x && a.y == b.y && a.z == b.z;
118 }
120 inline bool operator !=(const Vector3 &a, const Vector3 &b)
121 {
122 return !(a == b);
123 }
125 inline float dot(const Vector3 &a, const Vector3 &b)
126 {
127 return a.x * b.x + a.y * b.y + a.z * b.z;
128 }
130 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
131 {
132 return Vector3(a.y * b.z - a.z * b.y,
133 a.z * b.x - a.x * b.z,
134 a.x * b.y - a.y * b.x);
135 }
137 inline float length(const Vector3 &v)
138 {
139 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
140 }
142 inline float length_sq(const Vector3 &v)
143 {
144 return v.x * v.x + v.y * v.y + v.z * v.z;
145 }
147 inline Vector3 normalize(const Vector3 &v)
148 {
149 float len = length(v);
150 if(len == 0.0f) {
151 return v;
152 }
154 return Vector3(v.x / len, v.y / len, v.z / len);
155 }
157 inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
158 {
159 return v - n * dot(n, v) * 2.0;
160 }
162 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
163 {
164 float ndotv = dot(n, v);
165 float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
166 if(k < 0.0f) {
167 return Vector3();
168 }
169 return ior * v - (ior * ndotv + sqrt(k)) * n;
170 }
172 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior)
173 {
174 if(to_ior == 0.0f) to_ior = 1.0f;
175 return refract(v, n, from_ior / to_ior);
176 }
178 inline Vector3 distance(const Vector3 &a, const Vector3 &b)
179 {
180 return length(a - b);
181 }
183 inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b)
184 {
185 return length_sq(a - b);
186 }
188 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng)
189 {
190 return dot(ng, i) < 0.0f ? n : -n;
191 }
193 // ---- Vector4 ----
196 inline void Vector4::normalize()
197 {
198 float len = (float)sqrt(x * x + y * y + z * z + w * w);
199 if(len != 0.0f) {
200 x /= len;
201 y /= len;
202 z /= len;
203 w /= len;
204 }
205 }
207 inline float &Vector4::operator[] (int idx)
208 {
209 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
210 }
212 inline const float &Vector4::operator[] (int idx) const
213 {
214 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
215 }
217 } // namespace gph