NGL  6.5
The NCCA Graphics Library
Vec4.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "Vec4.h"
18 #include "Vec3.h"
19 #include "Vec2.h"
20 
21 #include "NGLassert.h"
22 #include "Mat4.h"
23 #include <cmath>
24 
25 //----------------------------------------------------------------------------------------------------------------------
28 //----------------------------------------------------------------------------------------------------------------------
29 namespace ngl
30 {
31 
32 
33 
34 //----------------------------------------------------------------------------------------------------------------------
35 Real Vec4::dot(const Vec4& _v )const noexcept
36 {
37  return m_x * _v.m_x + m_y * _v.m_y + m_z * _v.m_z;
38 }
39 
40 //----------------------------------------------------------------------------------------------------------------------
41 void Vec4::set( Real _x, Real _y, Real _z, Real _w) noexcept
42 {
43  m_x=_x;
44  m_y=_y;
45  m_z=_z;
46  m_w=_w;
47 }
48 
49 //----------------------------------------------------------------------------------------------------------------------
50 void Vec4::set( const Vec4& _v) noexcept
51 {
52  m_x=_v.m_x;
53  m_y=_v.m_y;
54  m_z=_v.m_z;
55  m_w=_v.m_w;
56 }
57 //----------------------------------------------------------------------------------------------------------------------
58 void Vec4::set( const Vec3 &_v ) noexcept
59 {
60  m_x=_v.m_x;
61  m_y=_v.m_y;
62  m_z=_v.m_z;
63  m_w=1.0f;
64 }
65 
66 //----------------------------------------------------------------------------------------------------------------------
67 void Vec4::null() noexcept
68 {
69  m_x=0.0f;
70  m_y=0.0f;
71  m_z=0.0f;
72  m_w=1.0f;
73 }
74 
75 //----------------------------------------------------------------------------------------------------------------------
76 Real& Vec4::operator[]( int _i ) noexcept
77 {
78  NGL_ASSERT(_i >=0 && _i<=3);
79  return (&m_x)[_i];
80 }
81 
82 //----------------------------------------------------------------------------------------------------------------------
83 Real Vec4::length() const noexcept
84 {
85  return sqrtf((m_x*m_x)+(m_y*m_y)+(m_z*m_z));
86 }
87 
88 
89 //----------------------------------------------------------------------------------------------------------------------
90 Vec4 &Vec4::operator-() noexcept
91 {
92  m_x=-m_x;
93  m_y=-m_y;
94  m_z=-m_z;
95  return *this;
96 }
97 
98 //----------------------------------------------------------------------------------------------------------------------
99 Vec4 &Vec4::normalize() noexcept
100 {
101  Real len=sqrtf(m_x*m_x+m_y*m_y+m_z*m_z);
102  NGL_ASSERT(len!=0.0f);
103  m_x/=len;
104  m_y/=len;
105  m_z/=len;
106  return *this;
107 }
108 
109 //----------------------------------------------------------------------------------------------------------------------
110 void Vec4::cross( const Vec4& _v1, const Vec4& _v2) noexcept
111 {
112  m_x=_v1.m_y*_v2.m_z-_v1.m_z*_v2.m_y;
113  m_y=_v1.m_z*_v2.m_x-_v1.m_x*_v2.m_z;
114  m_z=_v1.m_x*_v2.m_y-_v1.m_y*_v2.m_x;
115 }
116 
117 //----------------------------------------------------------------------------------------------------------------------
118 Vec4 Vec4::cross(const Vec4& _v )const noexcept
119 {
120  return Vec4(
121  m_y*_v.m_z - m_z*_v.m_y,
122  m_z*_v.m_x - m_x*_v.m_z,
123  m_x*_v.m_y - m_y*_v.m_x,
124  0.0f
125  );
126 
127 }
128 
129 //----------------------------------------------------------------------------------------------------------------------
130 void Vec4::operator+=( const Vec4& _v) noexcept
131 {
132  m_x+=_v.m_x;
133  m_y+=_v.m_y;
134  m_z+=_v.m_z;
135 }
136 
137 //----------------------------------------------------------------------------------------------------------------------
138 void Vec4::operator/=(Real _v) noexcept
139 {
140  NGL_ASSERT(_v !=0.0f);
141  m_x/=_v;
142  m_y/=_v;
143  m_z/=_v;
144 }
145 //----------------------------------------------------------------------------------------------------------------------
146 void Vec4::operator*=(Real _v) noexcept
147 {
148  m_x*=_v;
149  m_y*=_v;
150  m_z*=_v;
151 }
152 //----------------------------------------------------------------------------------------------------------------------
153 void Vec4::operator-=( const Vec4& _v) noexcept
154 {
155  m_x-=_v.m_x;
156  m_y-=_v.m_y;
157  m_z-=_v.m_z;
158 }
159 
160 //----------------------------------------------------------------------------------------------------------------------
161 Vec4 Vec4::operator/(Real _v )const noexcept
162 {
163  return Vec4(m_x/_v,m_y/_v,m_z/_v,m_w);
164 }
165 
166 //----------------------------------------------------------------------------------------------------------------------
167 Vec4 Vec4::operator+(const Vec4& _v )const noexcept
168 {
169  return Vec4(
170  m_x+_v.m_x,
171  m_y+_v.m_y,
172  m_z+_v.m_z,
173  m_w
174  );
175 }
176 
177 //----------------------------------------------------------------------------------------------------------------------
178 Vec4 Vec4::operator-(const Vec4& _v )const noexcept
179 {
180  return Vec4(
181  m_x-_v.m_x,
182  m_y-_v.m_y,
183  m_z-_v.m_z,
184  m_w
185  );
186 }
187 
188 //----------------------------------------------------------------------------------------------------------------------
189 bool Vec4::operator==(const Vec4& _v )const noexcept
190 {
191  return (
192  FCompare(_v.m_x,m_x) &&
193  FCompare(_v.m_y,m_y) &&
194  FCompare(_v.m_z,m_z)
195  );
196 }
197 //----------------------------------------------------------------------------------------------------------------------
198 bool Vec4::operator!=( const Vec4& _v )const noexcept
199 {
200  return (
201  !FCompare(_v.m_x,m_x) ||
202  !FCompare(_v.m_y,m_y) ||
203  !FCompare(_v.m_z,m_z)
204  );
205 }
206 //----------------------------------------------------------------------------------------------------------------------
207 Vec4 Vec4::operator*( const Vec4& _v )const noexcept
208 {
209  return Vec4(
210  m_x*_v.m_x,
211  m_y*_v.m_y,
212  m_z*_v.m_z,
213  m_w
214  );
215 }
216 
217 //----------------------------------------------------------------------------------------------------------------------
218 Vec4 Vec4::operator/(const Vec4& _v )const noexcept
219 {
220  return Vec4(
221  m_x/_v.m_x,
222  m_y/_v.m_y,
223  m_z/_v.m_z,
224  m_w
225  );
226 }
227 
228 //----------------------------------------------------------------------------------------------------------------------
229 Vec4 Vec4::operator *( Real _i )const noexcept
230 {
231  return Vec4(
232  m_x*_i,
233  m_y*_i,
234  m_z*_i,
235  m_w
236  );
237 }
238 
239 //----------------------------------------------------------------------------------------------------------------------
240 Real Vec4::angleBetween( const Vec4& _v )const noexcept
241 {
242  // uses cos(t) = |a| . |b|
243 // create tmp Vec3s so we don't have to
244 // normalize existing ones
245  Vec4 v1 = _v;
246  Vec4 v2 = *this;
247  v1.normalize();
248  v2.normalize();
249  return acosf(v1.dot(v2));
250 }
251 
252 //----------------------------------------------------------------------------------------------------------------------
253 Real Vec4::inner(const Vec4& _v )const noexcept
254 {
255  return (
256  (m_x * _v.m_x) +
257  (m_y * _v.m_y) +
258  (m_z * _v.m_z)
259  );
260 }
261 
262 //----------------------------------------------------------------------------------------------------------------------
263 Vec4 Vec4::outer(const Vec4& _v ) const noexcept
264 {
265  Real x = (m_y * _v.m_z) - (m_z * _v.m_y);
266  Real y = (m_z * _v.m_x) - (m_x * _v.m_z);
267  Real z = (m_x * _v.m_y) - (m_y * _v.m_x);
268 
269  return Vec4(x,y,z,m_w);
270 }
271 
272 
273 //----------------------------------------------------------------------------------------------------------------------
274 Vec4 & Vec4::operator=( const Vec4& _v) noexcept
275 {
276  m_x = _v.m_x;
277  m_y = _v.m_y;
278  m_z = _v.m_z;
279  m_w = _v.m_w;
280  return *this;
281 }
282 
283 //----------------------------------------------------------------------------------------------------------------------
284 Vec4 & Vec4::operator=( const Vec3& _v) noexcept
285 {
286  m_x = _v.m_x;
287  m_y = _v.m_y;
288  m_z = _v.m_z;
289  m_w = 0.0f;
290  return *this;
291 }
292 
293 //----------------------------------------------------------------------------------------------------------------------
294 Vec4 & Vec4::operator=( Real _v) noexcept
295 {
296  m_x = _v;
297  m_y = _v;
298  m_z = _v;
299  m_w = 0.0f;
300  return *this;
301 }
302 
303 
304 //----------------------------------------------------------------------------------------------------------------------
305 Real Vec4::lengthSquared() const noexcept
306 {
307  return m_x * m_x+m_y * m_y+ m_z*m_z;
308 }
309 
310 //----------------------------------------------------------------------------------------------------------------------
311 Vec4 Vec4::operator*(const Mat4 &_m ) const noexcept
312 {
313 
314  Vec4 v(
315  m_x*_m.m_00 + m_y*_m.m_10 + m_z*_m.m_20+ m_w*_m.m_30,
316  m_x*_m.m_01 + m_y*_m.m_11 + m_z*_m.m_21+ m_w*_m.m_31,
317  m_x*_m.m_02 + m_y*_m.m_12 + m_z*_m.m_22+ m_w*_m.m_32,
318  m_x*_m.m_03 + m_y*_m.m_13 + m_z*_m.m_23+ m_w*_m.m_33
319  );
320 
321  return v;
322  }
323 
324 
325 } // end namspace ngl
326 
327 
328 
void operator-=(const Vec4 &_v) noexcept
-= operator this-=v
Definition: Vec4.cpp:153
GLdouble GLdouble z
Definition: glew.h:1562
Real inner(const Vec4 &_v) const noexcept
calculate the inner product of this vector and vector passed in
Definition: Vec4.cpp:253
Vec4 & normalize() noexcept
Normalize the vector using .
Definition: Vec4.cpp:99
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
GLfloat GLfloat v1
Definition: glew.h:1855
GLfloat GLfloat GLfloat v2
Definition: glew.h:1859
Vec4 & operator-() noexcept
negate the vector components
Definition: Vec4.cpp:90
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1255
Real angleBetween(const Vec4 &_v) const noexcept
calculate the angle between current vector and _v
Definition: Vec4.cpp:240
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
void operator*=(Real _v) noexcept
multiply this vector components by a scalar
Definition: Vec4.cpp:146
implementation files for RibExport class
Definition: AABB.cpp:22
Vec4()
Definition: Vec4.h:67
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
encapsulates a 4d Homogenous Point / Vector object
#define FCompare(a, b)
FCompare macro used for floating point comparision functions.
Definition: Types.h:136
Vec4 outer(const Vec4 &_v) const noexcept
compute the outer product of this vector and vector
Definition: Vec4.cpp:263
a simple 2 tuple container for compatibility with glsl
Vec4 operator/(Real _v) const noexcept
divide vector components by a scalar
Definition: Vec4.cpp:161
void set(Real _x, Real _y, Real _z, Real _w=1.0) noexcept
sets the vector component from 3 values
Definition: Vec4.cpp:41
a simple 3 tuple container for compatibility with glsl
const GLdouble * v
Definition: glew.h:1394
GLenum GLsizei len
Definition: glew.h:7780
Real m_z
z component
Definition: Vec4.h:323
bool operator!=(const Vec4 &_v) const noexcept
not equal check
Definition: Vec4.cpp:198
Real length() const noexcept
returns the length of the vector
Definition: Vec4.cpp:83
Real m_x
x component
Definition: Vec4.h:321
Real dot(const Vec4 &_b) const noexcept
return this dotted with _b
Definition: Vec4.cpp:35
GLint GLint GLint GLint GLint x
Definition: glew.h:1255
void operator/=(Real _v) noexcept
divide this vector components by a scalar
Definition: Vec4.cpp:138
#define NGL_ASSERT(X)
re-define the standard assert to work for ngl first check to see if assert is defined and undef it th...
Definition: NGLassert.h:53
bool operator==(const Vec4 &_v) const noexcept
check for equality uses FCompare (from Util.h) as float values
Definition: Vec4.cpp:189
Vec4 operator+(const Vec4 &_v) const noexcept
operator add vector+vector
Definition: Vec4.cpp:167
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
Vec4 operator*(Real _i) const noexcept
this * i for each element
Definition: Vec4.cpp:229
Real m_w
w component 0 for vector 1 for point
Definition: Vec4.h:324
Vec4 & operator=(const Vec4 &_v) noexcept
assignment operator set the current vector to rhs
Definition: Vec4.cpp:274
Real m_y
y component
Definition: Vec4.h:322
re impliment asserts so we don&#39;t exit on failure
Real & operator[](int _i) noexcept
[] index operator to access the index component of the vector
Definition: Vec4.cpp:76
void null() noexcept
clears the vector to 0,0,0,1
Definition: Vec4.cpp:67
void cross(const Vec4 &_v1, const Vec4 &_v2) noexcept
set the vector as the cross product from 2 other vectors
Definition: Vec4.cpp:110
void operator+=(const Vec4 &_v) noexcept
+= operator add vector v to current vector
Definition: Vec4.cpp:130
Real lengthSquared() const noexcept
calculate the length squared of the vector
Definition: Vec4.cpp:305
GLclampf f
Definition: glew.h:3511