New repo for repo.or.cz
[The-Artvertiser.git] / artvertiser / MatrixTracker / ofxVec4f.h
blob4387afa52891e2ee0d21d3f4c46e2e5bf162b846
1 /*
2 Copyright 2009 openFrameworks
4 Distributed under the terms of the GNU General Public License v3.
6 This file is part of The Artvertiser.
8 The Artvertiser is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 The Artvertiser is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with The Artvertiser. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef _OFX_VEC4f
23 #define _OFX_VEC4f
25 #include "ofxVec3f.h"
27 class ofxVec4f : public ofPoint {
30 public:
32 float w;
35 ofxVec4f( float _x=0.0f,
36 float _y=0.0f,
37 float _z=0.0f,
38 float _w=0.0f );
41 // Getters and Setters.
43 void set( float _x, float _y, float _z, float _w );
44 void set( const ofxVec4f& vec );
45 float &operator[]( const int& i );
49 // Check similarity/equality.
51 bool operator==( const ofxVec4f& vec );
52 bool operator!=( const ofxVec4f& vec );
53 bool match( const ofxVec4f& vec, float tollerance=0.0001);
56 // Additions and Subtractions.
58 ofxVec4f operator+( const ofxVec4f& vec ) const;
59 ofxVec4f& operator+=( const ofxVec4f& vec );
60 ofxVec4f operator-( const float f ) const;
61 ofxVec4f& operator-=( const float f );
62 ofxVec4f operator-( const ofxVec4f& vec ) const;
63 ofxVec4f& operator-=( const ofxVec4f& vec );
64 ofxVec4f operator+( const float f ) const;
65 ofxVec4f& operator+=( const float f );
66 ofxVec4f operator-() const;
69 // Scalings
71 ofxVec4f operator*( const ofxVec4f& vec ) const;
72 ofxVec4f& operator*=( const ofxVec4f& vec );
73 ofxVec4f operator*( const float f ) const;
74 ofxVec4f& operator*=( const float f );
75 ofxVec4f operator/( const ofxVec4f& vec ) const;
76 ofxVec4f& operator/=( const ofxVec4f& vec );
77 ofxVec4f operator/( const float f ) const;
78 ofxVec4f& operator/=( const float f );
79 ofxVec4f getScaled( const float length ) const;
80 ofxVec4f& scale( const float length );
83 // Distance between two points.
85 float distance( const ofxVec4f& pnt) const;
86 float squareDistance( const ofxVec4f& pnt ) const;
89 // Linear interpolation.
91 /**
92 * p==0.0 results in this point, p==0.5 results in the
93 * midpoint, and p==1.0 results in pnt being returned.
95 ofxVec4f getInterpolated( const ofxVec4f& pnt, float p ) const;
96 ofxVec4f& interpolate( const ofxVec4f& pnt, float p );
97 ofxVec4f getMiddle( const ofxVec4f& pnt ) const;
98 ofxVec4f& middle( const ofxVec4f& pnt );
99 ofxVec4f& average( const ofxVec4f* points, int num );
102 // Normalization
104 ofxVec4f getNormalized() const;
105 ofxVec4f& normalize();
108 // Limit length.
110 ofxVec4f getLimited(float max) const;
111 ofxVec4f& limit(float max);
114 // Length
116 float length() const;
117 float squareLength() const;
119 * Dot Product.
121 float dot( const ofxVec4f& vec ) const;
126 //---------------------------------------
127 // this methods are deprecated in 006 please use:
129 // getScaled
130 ofxVec4f rescaled( const float length ) const;
132 // scale
133 ofxVec4f& rescale( const float length );
135 // getNormalized
136 ofxVec4f normalized() const;
138 // getLimited
139 ofxVec4f limited(float max) const;
141 // squareLength
142 float lengthSquared() const;
144 // use squareDistance
145 float distanceSquared( const ofxVec4f& pnt ) const;
147 // use getInterpolated
148 ofxVec4f interpolated( const ofxVec4f& pnt, float p ) const;
150 // use getMiddle
151 ofxVec4f middled( const ofxVec4f& pnt ) const;
157 // Non-Member operators
160 ofxVec4f operator+( float f, const ofxVec4f& vec );
161 ofxVec4f operator-( float f, const ofxVec4f& vec );
162 ofxVec4f operator*( float f, const ofxVec4f& vec );
163 ofxVec4f operator/( float f, const ofxVec4f& vec );
171 /////////////////
172 // Implementation
173 /////////////////
176 inline ofxVec4f::ofxVec4f( float _x,
177 float _y,
178 float _z,
179 float _w )
181 x = _x;
182 y = _y;
183 z = _z;
184 w = _w;
190 // Getters and Setters.
193 inline void ofxVec4f::set( float _x, float _y, float _z, float _w ) {
194 x = _x;
195 y = _y;
196 z = _z;
197 w = _w;
200 inline void ofxVec4f::set( const ofxVec4f& vec ) {
201 x = vec.x;
202 y = vec.y;
203 z = vec.z;
204 w = vec.w;
207 inline float& ofxVec4f::operator[]( const int& i ) {
208 switch(i) {
209 case 0: return x;
210 case 1: return y;
211 case 2: return z;
212 case 3: return w;
213 default: return x;
219 // Check similarity/equality.
222 inline bool ofxVec4f::operator==( const ofxVec4f& vec ) {
223 return (x == vec.x) && (y == vec.y) && (z == vec.z) && (w == vec.w);
226 inline bool ofxVec4f::operator!=( const ofxVec4f& vec ) {
227 return (x != vec.x) || (y != vec.y) || (z != vec.z) || (w != vec.w);
230 inline bool ofxVec4f::match( const ofxVec4f& vec, float tollerance) {
231 return (fabs(x - vec.x) < tollerance)
232 && (fabs(y - vec.y) < tollerance)
233 && (fabs(z - vec.z) < tollerance)
234 && (fabs(w - vec.w) < tollerance);
240 // Additions and Subtractions.
243 inline ofxVec4f ofxVec4f::operator+( const ofxVec4f& vec ) const {
244 return ofxVec4f( x+vec.x, y+vec.y, z+vec.z, w+vec.w);
247 inline ofxVec4f& ofxVec4f::operator+=( const ofxVec4f& vec ) {
248 x += vec.x;
249 y += vec.y;
250 z += vec.z;
251 w += vec.w;
252 return *this;
255 inline ofxVec4f ofxVec4f::operator-( const float f ) const {
256 return ofxVec4f( x-f, y-f, z-f, w-f );
259 inline ofxVec4f& ofxVec4f::operator-=( const float f ) {
260 x -= f;
261 y -= f;
262 z -= f;
263 w -= f;
264 return *this;
267 inline ofxVec4f ofxVec4f::operator-( const ofxVec4f& vec ) const {
268 return ofxVec4f( x-vec.x, y-vec.y, z-vec.z, w-vec.w );
271 inline ofxVec4f& ofxVec4f::operator-=( const ofxVec4f& vec ) {
272 x -= vec.x;
273 y -= vec.y;
274 z -= vec.z;
275 w -= vec.w;
276 return *this;
279 inline ofxVec4f ofxVec4f::operator+( const float f ) const {
280 return ofxVec4f( x+f, y+f, z+f, w+f );
283 inline ofxVec4f& ofxVec4f::operator+=( const float f ) {
284 x += f;
285 y += f;
286 z += f;
287 w += f;
288 return *this;
291 inline ofxVec4f ofxVec4f::operator-() const {
292 return ofxVec4f( -x, -y, -z, -w );
296 // Scalings
299 inline ofxVec4f ofxVec4f::operator*( const ofxVec4f& vec ) const {
300 return ofxVec4f( x*vec.x, y*vec.y, z*vec.z, w*vec.w );
303 inline ofxVec4f& ofxVec4f::operator*=( const ofxVec4f& vec ) {
304 x *= vec.x;
305 y *= vec.y;
306 z *= vec.z;
307 w *= vec.w;
308 return *this;
311 inline ofxVec4f ofxVec4f::operator*( const float f ) const {
312 return ofxVec4f( x*f, y*f, z*f, w*f );
315 inline ofxVec4f& ofxVec4f::operator*=( const float f ) {
316 x *= f;
317 y *= f;
318 z *= f;
319 w *= f;
320 return *this;
323 inline ofxVec4f ofxVec4f::operator/( const ofxVec4f& vec ) const {
324 return ofxVec4f( vec.x!=0 ? x/vec.x : x , vec.y!=0 ? y/vec.y : y, vec.z!=0 ? z/vec.z : z, vec.w!=0 ? w/vec.w : w );
327 inline ofxVec4f& ofxVec4f::operator/=( const ofxVec4f& vec ) {
328 vec.x!=0 ? x/=vec.x : x;
329 vec.y!=0 ? y/=vec.y : y;
330 vec.z!=0 ? z/=vec.z : z;
331 vec.w!=0 ? w/=vec.w : w;
332 return *this;
335 inline ofxVec4f ofxVec4f::operator/( const float f ) const {
336 if(f == 0) return ofxVec4f(x, y, z, w);
338 return ofxVec4f( x/f, y/f, z/f, w/f );
341 inline ofxVec4f& ofxVec4f::operator/=( const float f ) {
342 if(f == 0)return *this;
344 x /= f;
345 y /= f;
346 z /= f;
347 w /= f;
348 return *this;
351 inline ofxVec4f ofxVec4f::rescaled( const float length ) const {
352 return getScaled(length);
355 inline ofxVec4f ofxVec4f::getScaled( const float length ) const {
356 float l = (float)sqrt(x*x + y*y + z*z + w*w);
357 if( l > 0 )
358 return ofxVec4f( (x/l)*length, (y/l)*length,
359 (z/l)*length, (w/l)*length );
360 else
361 return ofxVec4f();
364 inline ofxVec4f& ofxVec4f::rescale( const float length ) {
365 return scale(length);
368 inline ofxVec4f& ofxVec4f::scale( const float length ) {
369 float l = (float)sqrt(x*x + y*y + z*z + w*w);
370 if (l > 0) {
371 x = (x/l)*length;
372 y = (y/l)*length;
373 z = (z/l)*length;
374 w = (w/l)*length;
376 return *this;
381 // Distance between two points.
384 inline float ofxVec4f::distance( const ofxVec4f& pnt) const {
385 float vx = x-pnt.x;
386 float vy = y-pnt.y;
387 float vz = z-pnt.z;
388 float vw = w-pnt.w;
389 return (float)sqrt( vx*vx + vy*vy + vz*vz + vw*vw );
392 inline float ofxVec4f::distanceSquared( const ofxVec4f& pnt ) const {
393 return squareDistance(pnt);
396 inline float ofxVec4f::squareDistance( const ofxVec4f& pnt ) const {
397 float vx = x-pnt.x;
398 float vy = y-pnt.y;
399 float vz = z-pnt.z;
400 float vw = w-pnt.w;
401 return vx*vx + vy*vy + vz*vz + vw*vw;
406 // Linear interpolation.
410 * p==0.0 results in this point, p==0.5 results in the
411 * midpoint, and p==1.0 results in pnt being returned.
413 inline ofxVec4f ofxVec4f::interpolated( const ofxVec4f& pnt, float p ) const{
414 return getInterpolated(pnt,p);
417 inline ofxVec4f ofxVec4f::getInterpolated( const ofxVec4f& pnt, float p ) const {
418 return ofxVec4f( x*(1-p) + pnt.x*p,
419 y*(1-p) + pnt.y*p,
420 z*(1-p) + pnt.z*p,
421 w*(1-p) + pnt.w*p );
424 inline ofxVec4f& ofxVec4f::interpolate( const ofxVec4f& pnt, float p ) {
425 x = x*(1-p) + pnt.x*p;
426 y = y*(1-p) + pnt.y*p;
427 z = z*(1-p) + pnt.z*p;
428 w = w*(1-p) + pnt.w*p;
429 return *this;
432 inline ofxVec4f ofxVec4f::middled( const ofxVec4f& pnt ) const {
433 return getMiddle(pnt);
436 inline ofxVec4f ofxVec4f::getMiddle( const ofxVec4f& pnt ) const {
437 return ofxVec4f( (x+pnt.x)/2.0f, (y+pnt.y)/2.0f,
438 (z+pnt.z)/2.0f, (w+pnt.w)/2.0f );
441 inline ofxVec4f& ofxVec4f::middle( const ofxVec4f& pnt ) {
442 x = (x+pnt.x)/2.0f;
443 y = (y+pnt.y)/2.0f;
444 z = (z+pnt.z)/2.0f;
445 w = (w+pnt.w)/2.0f;
446 return *this;
450 // Average (centroid) among points.
451 // (Addition is sometimes useful for calculating averages too)
454 inline ofxVec4f& ofxVec4f::average( const ofxVec4f* points, int num ) {
455 x = 0.f;
456 y = 0.f;
457 z = 0.f;
458 w = 0.f;
459 for( int i=0; i<num; i++) {
460 x += points[i].x;
461 y += points[i].y;
462 z += points[i].z;
463 w += points[i].w;
465 x /= num;
466 y /= num;
467 z /= num;
468 w /= num;
469 return *this;
474 // Normalization
477 inline ofxVec4f ofxVec4f::normalized() const {
478 return getNormalized();
481 inline ofxVec4f ofxVec4f::getNormalized() const {
482 float length = (float)sqrt(x*x + y*y + z*z + w*w);
483 if( length > 0 ) {
484 return ofxVec4f( x/length, y/length, z/length, w/length );
485 } else {
486 return ofxVec4f();
490 inline ofxVec4f& ofxVec4f::normalize() {
491 float lenght = (float)sqrt(x*x + y*y + z*z + w*w);
492 if( lenght > 0 ) {
493 x /= lenght;
494 y /= lenght;
495 z /= lenght;
496 w /= lenght;
498 return *this;
503 // Limit length.
506 inline ofxVec4f ofxVec4f::limited(float max) const {
507 return getLimited(max);
510 inline ofxVec4f ofxVec4f::getLimited(float max) const {
511 ofxVec4f limited;
512 float lengthSquared = (x*x + y*y + z*z + w*w);
513 if( lengthSquared > max*max && lengthSquared > 0 ) {
514 float ratio = max/(float)sqrt(lengthSquared);
515 limited.set( x*ratio, y*ratio, z*ratio, w*ratio );
516 } else {
517 limited.set(x,y,z,w);
519 return limited;
522 inline ofxVec4f& ofxVec4f::limit(float max) {
523 float lengthSquared = (x*x + y*y + z*z + w*w);
524 if( lengthSquared > max*max && lengthSquared > 0 ) {
525 float ratio = max/(float)sqrt(lengthSquared);
526 x *= ratio;
527 y *= ratio;
528 z *= ratio;
529 w *= ratio;
531 return *this;
536 // Length
539 inline float ofxVec4f::length() const {
540 return (float)sqrt( x*x + y*y + z*z + w*w );
543 inline float ofxVec4f::lengthSquared() const {
544 return squareLength();
547 inline float ofxVec4f::squareLength() const {
548 return (float)(x*x + y*y + z*z + w*w);
554 * Dot Product.
556 inline float ofxVec4f::dot( const ofxVec4f& vec ) const {
557 return x*vec.x + y*vec.y + z*vec.z + w*vec.w;
564 // Non-Member operators
567 inline ofxVec4f operator+( float f, const ofxVec4f& vec ) {
568 return ofxVec4f( f+vec.x, f+vec.y, f+vec.z, f+vec.w );
571 inline ofxVec4f operator-( float f, const ofxVec4f& vec ) {
572 return ofxVec4f( f-vec.x, f-vec.y, f-vec.z, f-vec.w );
575 inline ofxVec4f operator*( float f, const ofxVec4f& vec ) {
576 return ofxVec4f( f*vec.x, f*vec.y, f*vec.z, f*vec.w );
579 inline ofxVec4f operator/( float f, const ofxVec4f& vec ) {
580 return ofxVec4f( f/vec.x, f/vec.y, f/vec.z, f/vec.w);
587 #endif