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/>.
27 class ofxVec4f
: public ofPoint
{
35 ofxVec4f( float _x
=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;
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.
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
);
104 ofxVec4f
getNormalized() const;
105 ofxVec4f
& normalize();
110 ofxVec4f
getLimited(float max
) const;
111 ofxVec4f
& limit(float max
);
116 float length() const;
117 float squareLength() const;
121 float dot( const ofxVec4f
& vec
) const;
126 //---------------------------------------
127 // this methods are deprecated in 006 please use:
130 ofxVec4f
rescaled( const float length
) const;
133 ofxVec4f
& rescale( const float length
);
136 ofxVec4f
normalized() const;
139 ofxVec4f
limited(float max
) const;
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;
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
);
176 inline ofxVec4f::ofxVec4f( float _x
,
190 // Getters and Setters.
193 inline void ofxVec4f::set( float _x
, float _y
, float _z
, float _w
) {
200 inline void ofxVec4f::set( const ofxVec4f
& vec
) {
207 inline float& ofxVec4f::operator[]( const int& i
) {
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
) {
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
) {
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
) {
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
) {
291 inline ofxVec4f
ofxVec4f::operator-() const {
292 return ofxVec4f( -x
, -y
, -z
, -w
);
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
) {
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
) {
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
;
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;
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
);
358 return ofxVec4f( (x
/l
)*length
, (y
/l
)*length
,
359 (z
/l
)*length
, (w
/l
)*length
);
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
);
381 // Distance between two points.
384 inline float ofxVec4f::distance( const ofxVec4f
& pnt
) const {
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 {
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
,
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
;
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
) {
450 // Average (centroid) among points.
451 // (Addition is sometimes useful for calculating averages too)
454 inline ofxVec4f
& ofxVec4f::average( const ofxVec4f
* points
, int num
) {
459 for( int i
=0; i
<num
; i
++) {
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
);
484 return ofxVec4f( x
/length
, y
/length
, z
/length
, w
/length
);
490 inline ofxVec4f
& ofxVec4f::normalize() {
491 float lenght
= (float)sqrt(x
*x
+ y
*y
+ z
*z
+ w
*w
);
506 inline ofxVec4f
ofxVec4f::limited(float max
) const {
507 return getLimited(max
);
510 inline ofxVec4f
ofxVec4f::getLimited(float max
) const {
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
);
517 limited
.set(x
,y
,z
,w
);
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
);
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
);
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
);