NGL  6.5
The NCCA Graphics Library
Vec2.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 "Vec2.h"
18 #include "Vec4.h"
19 #include "NGLassert.h"
20 #include <cmath>
21 //----------------------------------------------------------------------------------------------------------------------
24 //----------------------------------------------------------------------------------------------------------------------
25 namespace ngl
26 {
27 
28 //----------------------------------------------------------------------------------------------------------------------
29 void Vec2::set(Real _x, Real _y) noexcept
30 {
31  m_x=_x;
32  m_y=_y;
33 }
34 
35 //----------------------------------------------------------------------------------------------------------------------
36 void Vec2::set(const Vec2& _v ) noexcept
37 {
38  m_x=_v.m_x;
39  m_y=_v.m_y;
40 }
41 //----------------------------------------------------------------------------------------------------------------------
42 void Vec2::set(const Vec2* _v ) noexcept
43 {
44  m_x=_v->m_x;
45  m_y=_v->m_y;
46 }
47 
48 //----------------------------------------------------------------------------------------------------------------------
49 void Vec2::null() noexcept
50 {
51  m_x=0.0f;
52  m_y=0.0f;
53 }
54 
55 //----------------------------------------------------------------------------------------------------------------------
56 Real& Vec2::operator[]( int _i) noexcept
57 {
58  NGL_ASSERT(_i >=0 || _i<=2);
59  return (&m_x)[_i];
60 }
61 
62 
63 
64 //----------------------------------------------------------------------------------------------------------------------
65 Vec2 Vec2::operator-() const noexcept
66 {
67  return Vec2(-m_x,-m_y);
68 }
69 
70 
71 //----------------------------------------------------------------------------------------------------------------------
72 void Vec2::operator+=(const Vec2& _v ) noexcept
73 {
74  m_x+=_v.m_x;
75  m_y+=_v.m_y;
76 }
77 
78 //----------------------------------------------------------------------------------------------------------------------
79 void Vec2::operator/=(Real _v ) noexcept
80 {
81  NGL_ASSERT(_v !=0.0);
82  m_x/=_v;
83  m_y/=_v;
84 }
85 //----------------------------------------------------------------------------------------------------------------------
86 void Vec2::operator*=(Real _v) noexcept
87 {
88  m_x*=_v;
89  m_y*=_v;
90 }
91 //----------------------------------------------------------------------------------------------------------------------
92 void Vec2::operator-=(const Vec2& _v ) noexcept
93 {
94  m_x-=_v.m_x;
95  m_y-=_v.m_y;
96 }
97 
98 //----------------------------------------------------------------------------------------------------------------------
99 Vec2 Vec2::operator/( Real _v )const noexcept
100 {
101  return Vec2(m_x/_v,m_y/_v);
102 }
103 
104 //----------------------------------------------------------------------------------------------------------------------
105 Vec2 Vec2::operator+(const Vec2& _v )const noexcept
106 {
107  return Vec2(m_x+_v.m_x,m_y+_v.m_y);
108 }
109 
110 //----------------------------------------------------------------------------------------------------------------------
111 Vec2 Vec2::operator-( const Vec2& _v )const noexcept
112 {
113  return Vec2(m_x-_v.m_x, m_y-_v.m_y );
114 }
115 
116 //----------------------------------------------------------------------------------------------------------------------
117 bool Vec2::operator==(const Vec2& _v )const noexcept
118 {
119  return (
120  FCompare(_v.m_x,m_x) &&
121  FCompare(_v.m_y,m_y)
122  );
123 }
124 //----------------------------------------------------------------------------------------------------------------------
125 bool Vec2::operator!=(const Vec2& _v )const noexcept
126 {
127  return (
128  !FCompare(_v.m_x,m_x) ||
129  !FCompare(_v.m_y,m_y)
130  );
131 }
132 //----------------------------------------------------------------------------------------------------------------------
133 Vec2 Vec2::operator*(const Vec2& _v )const noexcept
134 {
135  return Vec2(
136  m_x*_v.m_x,
137  m_y*_v.m_y
138  );
139 }
140 
141 //----------------------------------------------------------------------------------------------------------------------
142 Vec2 Vec2::operator/( const Vec2& _v )const noexcept
143 {
144  return Vec2(m_x/_v.m_x, m_y/_v.m_y );
145 }
146 
147 //----------------------------------------------------------------------------------------------------------------------
148 Vec2 Vec2::operator *(Real _i )const noexcept
149 {
150  return Vec2(m_x*_i,m_y*_i );
151 }
152 
153 
154 
155 //----------------------------------------------------------------------------------------------------------------------
156 Vec2 & Vec2::operator=(const Vec2& _v) noexcept
157 {
158  m_x = _v.m_x;
159  m_y = _v.m_y;
160  return *this;
161 }
162 
163 //----------------------------------------------------------------------------------------------------------------------
164 void Vec2::normalize() noexcept
165 {
166  Real len=sqrtf(m_x*m_x+m_y*m_y);
167  NGL_ASSERT(len!=0.0);
168  m_x/=len;
169  m_y/=len;
170 }
171 
172 
173 
174 
175 
176 
177 Real Vec2::dot(const Vec2& _v )const noexcept
178 {
179  return m_x * _v.m_x + m_y * _v.m_y;
180 }
181 
182 
183 //----------------------------------------------------------------------------------------------------------------------
184 Real Vec2::lengthSquared() const noexcept
185 {
186  return (m_x*m_x)+(m_y*m_y);
187 }
188 
189 
190 
191 
192 
193 
194 //----------------------------------------------------------------------------------------------------------------------
195 Real Vec2::length() const noexcept
196 {
197  return sqrtf((m_x*m_x)+(m_y*m_y));
198 }
199 
200 
201 
202 } // end namspace ngl
203 
204 
205 
void operator*=(Real _v) noexcept
multiply this Vec2 components by a scalar
Definition: Vec2.cpp:86
Real & operator[](int _i) noexcept
[] index operator to access the index component of the Vec2
Definition: Vec2.cpp:56
simple Vec2 encapsulates a 3 float object like glsl Vec2 but not maths use the Vec2 class for maths a...
Definition: Vec2.h:49
bool operator==(const Vec2 &_v) const noexcept
check for equality uses FCompare (from Util.h) as float values
Definition: Vec2.cpp:117
Vec2 operator/(Real _v) const noexcept
divide Vec2 components by a scalar
Definition: Vec2.cpp:99
Vec2 operator+(const Vec2 &_v) const noexcept
operator add Vec2+Vec2
Definition: Vec2.cpp:105
Vec2 & operator=(const Vec2 &_v) noexcept
assignment operator set the current Vec2 to rhs
Definition: Vec2.cpp:156
Real m_x
x component of the Vec2
Definition: Vec2.h:256
Real dot(const Vec2 &_b) const noexcept
return this dotted with _b
Definition: Vec2.cpp:177
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
encapsulates a 4d Homogenous Point / Vector object
#define FCompare(a, b)
FCompare macro used for floating point comparision functions.
Definition: Types.h:136
void set(Real _x, Real _y) noexcept
sets the Vec2 component from 3 values
Definition: Vec2.cpp:29
a simple 2 tuple container for compatibility with glsl
Real m_y
y component of the Vec2
Definition: Vec2.h:260
GLenum GLsizei len
Definition: glew.h:7780
void normalize() noexcept
Normalize the vector using .
Definition: Vec2.cpp:164
void operator/=(Real _v) noexcept
divide this Vec2 components by a scalar
Definition: Vec2.cpp:79
void operator+=(const Vec2 &_v) noexcept
+= operator add Vec2 v to current Vec2
Definition: Vec2.cpp:72
Vec2(const Vec2 &_v) noexcept
copy ctor
Definition: Vec2.h:81
bool operator!=(const Vec2 &_v) const noexcept
not equal check
Definition: Vec2.cpp:125
void operator-=(const Vec2 &_v) noexcept
-= operator this-=v
Definition: Vec2.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
Vec2 operator-() const noexcept
negate the Vec2 components
Definition: Vec2.cpp:65
Real lengthSquared() const noexcept
returns the length squared of the vector (no sqrt so quicker)
Definition: Vec2.cpp:184
Real length() const noexcept
returns the length of the vector
Definition: Vec2.cpp:195
Vec2 operator*(const Real &_i) const noexcept
this * i for each element
re impliment asserts so we don&#39;t exit on failure
void null() noexcept
clears the Vec2 to 0,0,0
Definition: Vec2.cpp:49