NGL  6.5
The NCCA Graphics Library
Vec3.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 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 "Vec3.h"
18 #include "Vec4.h"
19 #include "Mat3.h"
20 #include "NGLassert.h"
21 #include <cmath>
22 //----------------------------------------------------------------------------------------------------------------------
25 //----------------------------------------------------------------------------------------------------------------------
26 namespace ngl
27 {
28 
29 
30 
31 
32 //----------------------------------------------------------------------------------------------------------------------
33 void Vec3::set(Real _x, Real _y, Real _z ) noexcept
34 {
35  m_x=_x;
36  m_y=_y;
37  m_z=_z;
38 }
39 
40 //----------------------------------------------------------------------------------------------------------------------
41 void Vec3::set( const Vec3& _v ) noexcept
42 {
43  m_x=_v.m_x;
44  m_y=_v.m_y;
45  m_z=_v.m_z;
46 }
47 void Vec3::set( const Vec4& _v ) noexcept
48 {
49  m_x=_v.m_x;
50  m_y=_v.m_y;
51  m_z=_v.m_z;
52 }
53 
54 //----------------------------------------------------------------------------------------------------------------------
55 Real Vec3::dot( const Vec3& _v )const noexcept
56 {
57  return m_x * _v.m_x + m_y * _v.m_y + m_z * _v.m_z;
58 }
59 //----------------------------------------------------------------------------------------------------------------------
60 void Vec3::null() noexcept
61 {
62  m_x=0.0f;
63  m_y=0.0f;
64  m_z=0.0f;
65 }
66 
67 //----------------------------------------------------------------------------------------------------------------------
68 Real& Vec3::operator[](size_t & _i ) noexcept
69 {
70  NGL_ASSERT(_i >=0 || _i<=3);
71  return (&m_x)[_i];
72 }
73 
74 
75 
76 //----------------------------------------------------------------------------------------------------------------------
77 Vec3 Vec3::operator-() const noexcept
78 {
79  return Vec3(-m_x,-m_y,-m_z);
80 }
81 
82 
83 //----------------------------------------------------------------------------------------------------------------------
84 void Vec3::operator+=(const Vec3& _v) noexcept
85 {
86  m_x+=_v.m_x;
87  m_y+=_v.m_y;
88  m_z+=_v.m_z;
89 }
90 
91 //----------------------------------------------------------------------------------------------------------------------
92 void Vec3::operator/=(Real _v) noexcept
93 {
94  NGL_ASSERT(_v !=0.0f);
95  m_x/=_v;
96  m_y/=_v;
97  m_z/=_v;
98 }
99 //----------------------------------------------------------------------------------------------------------------------
100 void Vec3::operator*=(Real _v) noexcept
101 {
102  m_x*=_v;
103  m_y*=_v;
104  m_z*=_v;
105 }
106 //----------------------------------------------------------------------------------------------------------------------
107 void Vec3::operator-=(const Vec3& _v) noexcept
108 {
109  m_x-=_v.m_x;
110  m_y-=_v.m_y;
111  m_z-=_v.m_z;
112 }
113 
114 //----------------------------------------------------------------------------------------------------------------------
115 Vec3 Vec3::operator/(Real _v)const noexcept
116 {
117  return Vec3(m_x/_v,m_y/_v,m_z/_v);
118 }
119 
120 //----------------------------------------------------------------------------------------------------------------------
121 Vec3 Vec3::operator+( const Vec3& _v)const noexcept
122 {
123  return Vec3(m_x+_v.m_x,m_y+_v.m_y,m_z+_v.m_z);
124 }
125 
126 //----------------------------------------------------------------------------------------------------------------------
127 Vec3 Vec3::operator-(const Vec3& _v)const noexcept
128 {
129  return Vec3(m_x-_v.m_x,m_y-_v.m_y,m_z-_v.m_z);
130 }
131 
132 //----------------------------------------------------------------------------------------------------------------------
133 bool Vec3::operator==(const Vec3& _v)const noexcept
134 {
135  return (
136  FCompare(_v.m_x,m_x) &&
137  FCompare(_v.m_y,m_y) &&
138  FCompare(_v.m_z,m_z)
139  );
140 }
141 //----------------------------------------------------------------------------------------------------------------------
142 bool Vec3::operator!=(const Vec3& _v )const noexcept
143 {
144  return (
145  !FCompare(_v.m_x,m_x) ||
146  !FCompare(_v.m_y,m_y) ||
147  !FCompare(_v.m_z,m_z)
148  );
149 }
150 //----------------------------------------------------------------------------------------------------------------------
151 Vec3 Vec3::operator*( const Vec3& _v )const noexcept
152 {
153  return Vec3(m_x*_v.m_x,m_y*_v.m_y,m_z*_v.m_z );
154 }
155 
156 //----------------------------------------------------------------------------------------------------------------------
157 Vec3 Vec3::operator/( const Vec3& _v )const noexcept
158 {
159  return Vec3(m_x/_v.m_x,m_y/_v.m_y,m_z/_v.m_z);
160 }
161 
162 //----------------------------------------------------------------------------------------------------------------------
163 Vec3 Vec3::operator *(Real _i)const noexcept
164 {
165  return Vec3(m_x*_i,m_y*_i,m_z*_i);
166 }
167 
168 //----------------------------------------------------------------------------------------------------------------------
169 Vec3 & Vec3::operator=(Real _v) noexcept
170 {
171  m_x = _v;
172  m_y = _v;
173  m_z = _v;
174  return *this;
175 }
176 
177 
178 //----------------------------------------------------------------------------------------------------------------------
179 Vec3 & Vec3::operator=(const Vec4& _v) noexcept
180 {
181  m_x = _v.m_x;
182  m_y = _v.m_y;
183  m_z = _v.m_z;
184  return *this;
185 }
186 //----------------------------------------------------------------------------------------------------------------------
187 void Vec3::cross(const Vec3& _v1, const Vec3& _v2) noexcept
188 {
189  m_x=_v1.m_y*_v2.m_z-_v1.m_z*_v2.m_y;
190  m_y=_v1.m_z*_v2.m_x-_v1.m_x*_v2.m_z;
191  m_z=_v1.m_x*_v2.m_y-_v1.m_y*_v2.m_x;
192 }
193 
194 //----------------------------------------------------------------------------------------------------------------------
195 Vec3 Vec3::cross( const Vec3& _v )const noexcept
196 {
197  return Vec3(m_y*_v.m_z - m_z*_v.m_y,
198  m_z*_v.m_x - m_x*_v.m_z,
199  m_x*_v.m_y - m_y*_v.m_x
200  );
201 
202 }
203 
204 
205 //----------------------------------------------------------------------------------------------------------------------
206 void Vec3::normalize() noexcept
207 {
208  Real len=sqrtf(m_x*m_x+m_y*m_y+m_z*m_z);
209  NGL_ASSERT(len!=0.0f);
210  m_x/=len;
211  m_y/=len;
212  m_z/=len;
213 }
214 
215 //----------------------------------------------------------------------------------------------------------------------
216 Real Vec3::inner( const Vec3& _v )const noexcept
217 {
218  return ((m_x * _v.m_x) +(m_y * _v.m_y) + (m_z * _v.m_z));
219 }
220 
221 
222 
223 Mat3 Vec3::outer(const Vec3 &_v ) const noexcept
224 {
225  return Mat3(
226  m_x * _v.m_x, m_x * _v.m_y, m_x * _v.m_z,
227  m_y * _v.m_x, m_y * _v.m_y, m_y * _v.m_z,
228  m_z * _v.m_x, m_z * _v.m_y, m_z * _v.m_z
229  );
230 }
231 
232 //----------------------------------------------------------------------------------------------------------------------
233 Real Vec3::length() const noexcept
234 {
235  return sqrtf((m_x*m_x)+(m_y*m_y)+(m_z*m_z));
236 }
237 
238 
239 //----------------------------------------------------------------------------------------------------------------------
240 Real Vec3::lengthSquared() const noexcept
241 {
242  return m_x * m_x+m_y * m_y+ m_z*m_z;
243 }
244 
245 //----------------------------------------------------------------------------------------------------------------------
246 Vec3 Vec3::operator*(const Mat3 &_m) const noexcept
247 {
248 
249 
250  Vec3 v(
251  m_x*_m.m_00 + m_y*_m.m_10 + m_z*_m.m_20,
252  m_x*_m.m_01 + m_y*_m.m_11 + m_z*_m.m_21,
253  m_x*_m.m_02 + m_y*_m.m_12 + m_z*_m.m_22
254  );
255 
256 
257  return v;
258  }
259 
260 Vec3 Vec3::reflect(const Vec3 & _n) const noexcept
261 {
262  float d=this->dot(_n);
263  // I - 2.0 * dot(N, I) * N
264  return Vec3( m_x-2.0f*d*_n.m_x, m_y-2.0f*d*_n.m_y, m_z-2.0f*d*_n.m_z);
265 }
266 
267 void Vec3::clamp(float _min, float _max ) noexcept
268 {
269  m_x<_min ? m_x = _min : m_x;
270  m_x>_max ? m_x = _max : m_x;
271 
272  m_y<_min ? m_y = _min : m_y;
273  m_y>_max ? m_y = _max : m_y;
274 
275  m_z<_min ? m_z = _min : m_z;
276  m_z>_max ? m_z = _max : m_z;
277 
278 
279 }
280 void Vec3::clamp(float _max ) noexcept
281 {
282  m_x<-_max ? m_x = -_max : m_x;
283  m_x>_max ? m_x = _max : m_x;
284 
285  m_y<-_max ? m_y = -_max : m_y;
286  m_y>_max ? m_y = _max : m_y;
287 
288  m_z<-_max ? m_z = -_max : m_z;
289  m_z>_max ? m_z = _max : m_z;
290 
291 
292 }
293 
294 } // end namspace ngl
295 
296 
297 
void null() noexcept
clears the Vec3 to 0,0,0
Definition: Vec3.cpp:60
Mat3 basic 3x3 matrix for certain glsl ops.
Definition: Mat3.h:43
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
Vec3 reflect(const Vec3 &_n) const noexcept
return the vector reflected with this and N
Definition: Vec3.cpp:260
Vec3()
default ctor use default and set to (0.0f,0.0f,0.0f) as attributes are initialised ...
Definition: Vec3.h:59
Real inner(const Vec3 &_v) const noexcept
calculate the inner product of this vector and vector passed in
Definition: Vec3.cpp:216
Vec3 operator/(Real _v) const noexcept
divide Vec3 components by a scalar
Definition: Vec3.cpp:115
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
void operator+=(const Vec3 &_v) noexcept
+= operator add Vec3 v to current Vec3
Definition: Vec3.cpp:84
encapsulates a 4d Homogenous Point / Vector object
Real lengthSquared() const noexcept
returns the length squared of the vector (no sqrt so quicker)
Definition: Vec3.cpp:240
#define FCompare(a, b)
FCompare macro used for floating point comparision functions.
Definition: Types.h:136
void operator-=(const Vec3 &_v) noexcept
-= operator this-=v
Definition: Vec3.cpp:107
Vec3 operator+(const Vec3 &_v) const noexcept
operator add Vec3+Vec3
Definition: Vec3.cpp:121
Real m_y
y component
Definition: Vec3.h:311
void set(Real _x, Real _y, Real _z) noexcept
sets the Vec3 component from 3 values
Definition: Vec3.cpp:33
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_x
x component
Definition: Vec3.h:310
bool operator!=(const Vec3 &_v) const noexcept
not equal check
Definition: Vec3.cpp:142
Real dot(const Vec3 &_b) const noexcept
return this dotted with _b
Definition: Vec3.cpp:55
void clamp(float _min, float _max) noexcept
clamp the vector values between _min and _max
Definition: Vec3.cpp:267
a simple 3x3 TX matrix
Real length() const noexcept
returns the length of the vector
Definition: Vec3.cpp:233
void operator/=(Real _v) noexcept
divide this Vec3 components by a scalar
Definition: Vec3.cpp:92
#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
Real & operator[](size_t &_i) noexcept
[] index operator to access the index component of the Vec3
Definition: Vec3.cpp:68
Vec3 operator*(Real _i) const noexcept
this * i for each element
Definition: Vec3.cpp:163
void operator*=(Real _v) noexcept
multiply this Vec3 components by a scalar
Definition: Vec3.cpp:100
bool operator==(const Vec3 &_v) const noexcept
check for equality uses FCompare (from Util.h) as float values
Definition: Vec3.cpp:133
void normalize() noexcept
Normalize the vector using .
Definition: Vec3.cpp:206
re impliment asserts so we don&#39;t exit on failure
Vec3 operator-() const noexcept
negate the Vec3 components
Definition: Vec3.cpp:77
Vec3 & operator=(const Vec3 &_v)=default
assignment operator set the current Vec3 to rhs
void cross(const Vec3 &_v1, const Vec3 &_v2) noexcept
set the Vec3 as the cross product from 2 other Vec3
Definition: Vec3.cpp:187
Mat3 outer(const Vec3 &_v) const noexcept
compute the outer product of this vector and vector (requested by PJ)
Definition: Vec3.cpp:223
Real m_z
z component
Definition: Vec3.h:312
GLclampf f
Definition: glew.h:3511