reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofVec4f.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofConstants.h"
4#include "glm/vec4.hpp"
5#include "glm/fwd.hpp"
6#include <cmath>
7
8class ofVec2f;
9class ofVec3f;
10
11class ofVec4f {
12public:
14 static const int DIM = 4;
16
17 float x;
18 float y;
19 float z;
20 float w;
21
22 //---------------------
25
27 explicit ofVec4f( float _scalar );
28 ofVec4f( float _x, float _y, float _z, float _w );
29 ofVec4f( const ofVec2f& vec);
30 ofVec4f( const ofVec3f& vec);
31 ofVec4f( const glm::vec2& vec);
32 ofVec4f( const glm::vec3& vec);
33 ofVec4f( const glm::vec4& vec);
34 operator glm::vec4() const;
35
37
38 //---------------------
41
42
43 float * getPtr() {
44 return (float*)&x;
45 }
46 const float * getPtr() const {
47 return (const float *)&x;
48 }
49
50 float& operator[]( int n ){
51 return getPtr()[n];
52 }
53
54 float operator[]( int n ) const {
55 return getPtr()[n];
56 }
57
58 void set( float _scalar );
59 void set( float _x, float _y, float _z, float _w );
60 void set( const ofVec4f& vec );
61
62
64
65 //---------------------
68
69
70 bool operator==( const ofVec4f& vec ) const;
71 bool operator!=( const ofVec4f& vec ) const;
72 bool match( const ofVec4f& vec, float tolerance = 0.0001f) const;
73
75
76 //---------------------
79
80 ofVec4f operator+( const ofVec4f& vec ) const;
81 ofVec4f operator+( const float f ) const;
82 ofVec4f& operator+=( const ofVec4f& vec );
83 ofVec4f& operator+=( const float f );
84 ofVec4f operator-( const float f ) const;
85 ofVec4f operator-( const ofVec4f& vec ) const;
87 ofVec4f& operator-=( const float f );
88 ofVec4f& operator-=( const ofVec4f& vec );
89
90
91 ofVec4f operator*( const ofVec4f& vec ) const;
92 ofVec4f operator*( const float f ) const;
93 ofVec4f& operator*=( const ofVec4f& vec );
94 ofVec4f& operator*=( const float f );
95 ofVec4f operator/( const ofVec4f& vec ) const;
96 ofVec4f operator/( const float f ) const;
97 ofVec4f& operator/=( const ofVec4f& vec );
98 ofVec4f& operator/=( const float f );
99
101 friend std::ostream& operator<<(std::ostream& os, const ofVec4f& vec);
102 friend std::istream& operator>>(std::istream& is, const ofVec4f& vec);
104
106
107 //---------------------
110
115 ofVec4f getScaled( const float length ) const;
116
120 ofVec4f& scale( const float length );
121
123
124
125 //---------------------
128
133 float distance( const ofVec4f& pnt) const;
134 float squareDistance( const ofVec4f& pnt ) const;
135
137
138 //---------------------
141
142
148 ofVec4f getInterpolated( const ofVec4f& pnt, float p ) const;
149
154 ofVec4f& interpolate( const ofVec4f& pnt, float p );
155
160 ofVec4f getMiddle( const ofVec4f& pnt ) const;
161
166 ofVec4f& middle( const ofVec4f& pnt );
167
172 ofVec4f& average( const ofVec4f* points, int num );
173
175
176 //---------------------
179
188
195
196
201 ofVec4f getLimited(float max) const;
205 ofVec4f& limit(float max);
206
208
209 //---------------------
212
216 float length() const;
217 float lengthSquared() const;
218
219
221
222 //---------------------
225
226
237 float dot( const ofVec4f& vec ) const;
238
240
241
242
243
244 //---------------------------------------
245 // this methods are deprecated in 006 please use:
247
248 // getScaled
249 OF_DEPRECATED_MSG("Use member method getScaled() instead.", ofVec4f rescaled( const float length ) const);
250
251 // scale
252 OF_DEPRECATED_MSG("Use member method scale() instead.", ofVec4f& rescale( const float length ));
253
254 // getNormalized
255 OF_DEPRECATED_MSG("Use member method getNormalized() instead.", ofVec4f normalized() const);
256
257 // getLimited
258 OF_DEPRECATED_MSG("Use member method getLimited() instead.", ofVec4f limited(float max) const);
259
260 // use squareDistance
261 OF_DEPRECATED_MSG("Use member method squareDistance() instead.", float distanceSquared( const ofVec4f& pnt ) const);
262
263 // use getInterpolated
264 OF_DEPRECATED_MSG("Use member method getInterpolated() instead.", ofVec4f interpolated( const ofVec4f& pnt, float p ) const);
265
266 // use getMiddle
267 OF_DEPRECATED_MSG("Use member method getMiddle() instead.", ofVec4f middled( const ofVec4f& pnt ) const);
268
269 // return all zero vector
270 static ofVec4f zero() { return ofVec4f(0, 0, 0, 0); }
271
272 // return all one vector
273 static ofVec4f one() { return ofVec4f(1, 1, 1, 1); }
275};
276
277
279
280
281// Non-Member operators
282//
283//
284ofVec4f operator+( float f, const ofVec4f& vec );
285ofVec4f operator-( float f, const ofVec4f& vec );
286ofVec4f operator*( float f, const ofVec4f& vec );
287ofVec4f operator/( float f, const ofVec4f& vec );
288
289
290
291
292
293
294
296// Implementation
298
299inline ofVec4f::ofVec4f(): x(0), y(0), z(0), w(0) {}
300inline ofVec4f::ofVec4f(float _s): x(_s), y(_s), z(_s), w(_s) {}
301inline ofVec4f::ofVec4f( float _x,
302 float _y,
303 float _z,
304 float _w ):x(_x), y(_y), z(_z), w(_w) {}
305
306
307inline ofVec4f::operator glm::vec4() const{
308 return glm::vec4(x,y,z,w);
309}
310// Getters and Setters.
311//
312//
313inline void ofVec4f::set( float _scalar) {
314 x = _scalar;
315 y = _scalar;
316 z = _scalar;
317 w = _scalar;
318}
319
320inline void ofVec4f::set( float _x, float _y, float _z, float _w ) {
321 x = _x;
322 y = _y;
323 z = _z;
324 w = _w;
325}
326
327inline void ofVec4f::set( const ofVec4f& vec ) {
328 x = vec.x;
329 y = vec.y;
330 z = vec.z;
331 w = vec.w;
332}
333
334
335// Check similarity/equality.
336//
337//
338inline bool ofVec4f::operator==( const ofVec4f& vec ) const {
339 return (x == vec.x) && (y == vec.y) && (z == vec.z) && (w == vec.w);
340}
341
342inline bool ofVec4f::operator!=( const ofVec4f& vec ) const {
343 return (x != vec.x) || (y != vec.y) || (z != vec.z) || (w != vec.w);
344}
345
346inline bool ofVec4f::match( const ofVec4f& vec, float tolerance) const {
347 return (fabs(x - vec.x) < tolerance)
348 && (fabs(y - vec.y) < tolerance)
349 && (fabs(z - vec.z) < tolerance)
350 && (fabs(w - vec.w) < tolerance);
351}
352
353
354
355
356// Additions and Subtractions.
357//
358//
359inline ofVec4f ofVec4f::operator+( const ofVec4f& vec ) const {
360 return ofVec4f( x+vec.x, y+vec.y, z+vec.z, w+vec.w);
361}
362
363inline ofVec4f& ofVec4f::operator+=( const ofVec4f& vec ) {
364 x += vec.x;
365 y += vec.y;
366 z += vec.z;
367 w += vec.w;
368 return *this;
369}
370
371inline ofVec4f ofVec4f::operator-( const float f ) const {
372 return ofVec4f( x-f, y-f, z-f, w-f );
373}
374
375inline ofVec4f& ofVec4f::operator-=( const float f ) {
376 x -= f;
377 y -= f;
378 z -= f;
379 w -= f;
380 return *this;
381}
382
383inline ofVec4f ofVec4f::operator-( const ofVec4f& vec ) const {
384 return ofVec4f( x-vec.x, y-vec.y, z-vec.z, w-vec.w );
385}
386
387inline ofVec4f& ofVec4f::operator-=( const ofVec4f& vec ) {
388 x -= vec.x;
389 y -= vec.y;
390 z -= vec.z;
391 w -= vec.w;
392 return *this;
393}
394
395inline ofVec4f ofVec4f::operator+( const float f ) const {
396 return ofVec4f( x+f, y+f, z+f, w+f );
397}
398
399inline ofVec4f& ofVec4f::operator+=( const float f ) {
400 x += f;
401 y += f;
402 z += f;
403 w += f;
404 return *this;
405}
406
407inline ofVec4f ofVec4f::operator-() const {
408 return ofVec4f( -x, -y, -z, -w );
409}
410
411
412// Scalings
413//
414//
415inline ofVec4f ofVec4f::operator*( const ofVec4f& vec ) const {
416 return ofVec4f( x*vec.x, y*vec.y, z*vec.z, w*vec.w );
417}
418
419inline ofVec4f& ofVec4f::operator*=( const ofVec4f& vec ) {
420 x *= vec.x;
421 y *= vec.y;
422 z *= vec.z;
423 w *= vec.w;
424 return *this;
425}
426
427inline ofVec4f ofVec4f::operator*( const float f ) const {
428 return ofVec4f( x*f, y*f, z*f, w*f );
429}
430
431inline ofVec4f& ofVec4f::operator*=( const float f ) {
432 x *= f;
433 y *= f;
434 z *= f;
435 w *= f;
436 return *this;
437}
438
439inline ofVec4f ofVec4f::operator/( const ofVec4f& vec ) const {
440 return ofVec4f( 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 );
441}
442
443inline ofVec4f& ofVec4f::operator/=( const ofVec4f& vec ) {
444 vec.x!=0 ? x/=vec.x : x;
445 vec.y!=0 ? y/=vec.y : y;
446 vec.z!=0 ? z/=vec.z : z;
447 vec.w!=0 ? w/=vec.w : w;
448 return *this;
449}
450
451inline ofVec4f ofVec4f::operator/( const float f ) const {
452 if(f == 0) return ofVec4f(x, y, z, w);
453
454 return ofVec4f( x/f, y/f, z/f, w/f );
455}
456
457inline ofVec4f& ofVec4f::operator/=( const float f ) {
458 if(f == 0)return *this;
459
460 x /= f;
461 y /= f;
462 z /= f;
463 w /= f;
464 return *this;
465}
466
467
468inline std::ostream& operator<<(std::ostream& os, const ofVec4f& vec) {
469 os << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w;
470 return os;
471}
472
473inline std::istream& operator>>(std::istream& is, ofVec4f& vec) {
474 is >> vec.x;
475 is.ignore(2);
476 is >> vec.y;
477 is.ignore(2);
478 is >> vec.z;
479 is.ignore(2);
480 is >> vec.w;
481 return is;
482}
483
484
485inline ofVec4f ofVec4f::rescaled( const float length ) const {
486 return getScaled(length);
487}
488
489inline ofVec4f ofVec4f::getScaled( const float length ) const {
490 float l = (float)sqrt(x*x + y*y + z*z + w*w);
491 if( l > 0 )
492 return ofVec4f( (x/l)*length, (y/l)*length,
493 (z/l)*length, (w/l)*length );
494 else
495 return ofVec4f();
496}
497
498inline ofVec4f& ofVec4f::rescale( const float length ) {
499 return scale(length);
500}
501
502inline ofVec4f& ofVec4f::scale( const float length ) {
503 float l = (float)sqrt(x*x + y*y + z*z + w*w);
504 if (l > 0) {
505 x = (x/l)*length;
506 y = (y/l)*length;
507 z = (z/l)*length;
508 w = (w/l)*length;
509 }
510 return *this;
511}
512
513
514
515// Distance between two points.
516//
517//
518inline float ofVec4f::distance( const ofVec4f& pnt) const {
519 float vx = x-pnt.x;
520 float vy = y-pnt.y;
521 float vz = z-pnt.z;
522 float vw = w-pnt.w;
523 return (float)sqrt( vx*vx + vy*vy + vz*vz + vw*vw );
524}
525
526inline float ofVec4f::distanceSquared( const ofVec4f& pnt ) const {
527 return squareDistance(pnt);
528}
529
530inline float ofVec4f::squareDistance( const ofVec4f& pnt ) const {
531 float vx = x-pnt.x;
532 float vy = y-pnt.y;
533 float vz = z-pnt.z;
534 float vw = w-pnt.w;
535 return vx*vx + vy*vy + vz*vz + vw*vw;
536}
537
538
539
540// Linear interpolation.
541//
542//
547inline ofVec4f ofVec4f::interpolated( const ofVec4f& pnt, float p ) const{
548 return getInterpolated(pnt,p);
549}
550
551inline ofVec4f ofVec4f::getInterpolated( const ofVec4f& pnt, float p ) const {
552 return ofVec4f( x*(1-p) + pnt.x*p,
553 y*(1-p) + pnt.y*p,
554 z*(1-p) + pnt.z*p,
555 w*(1-p) + pnt.w*p );
556}
557
558inline ofVec4f& ofVec4f::interpolate( const ofVec4f& pnt, float p ) {
559 x = x*(1-p) + pnt.x*p;
560 y = y*(1-p) + pnt.y*p;
561 z = z*(1-p) + pnt.z*p;
562 w = w*(1-p) + pnt.w*p;
563 return *this;
564}
565
566inline ofVec4f ofVec4f::middled( const ofVec4f& pnt ) const {
567 return getMiddle(pnt);
568}
569
570inline ofVec4f ofVec4f::getMiddle( const ofVec4f& pnt ) const {
571 return ofVec4f( (x+pnt.x)/2.0f, (y+pnt.y)/2.0f,
572 (z+pnt.z)/2.0f, (w+pnt.w)/2.0f );
573}
574
575inline ofVec4f& ofVec4f::middle( const ofVec4f& pnt ) {
576 x = (x+pnt.x)/2.0f;
577 y = (y+pnt.y)/2.0f;
578 z = (z+pnt.z)/2.0f;
579 w = (w+pnt.w)/2.0f;
580 return *this;
581}
582
583
584// Average (centroid) among points.
585// (Addition is sometimes useful for calculating averages too)
586//
587//
588inline ofVec4f& ofVec4f::average( const ofVec4f* points, int num ) {
589 x = 0.f;
590 y = 0.f;
591 z = 0.f;
592 w = 0.f;
593 for( int i=0; i<num; i++) {
594 x += points[i].x;
595 y += points[i].y;
596 z += points[i].z;
597 w += points[i].w;
598 }
599 x /= num;
600 y /= num;
601 z /= num;
602 w /= num;
603 return *this;
604}
605
606
607
608// Normalization
609//
610//
611inline ofVec4f ofVec4f::normalized() const {
612 return getNormalized();
613}
614
615inline ofVec4f ofVec4f::getNormalized() const {
616 float length = (float)sqrt(x*x + y*y + z*z + w*w);
617 if( length > 0 ) {
618 return ofVec4f( x/length, y/length, z/length, w/length );
619 } else {
620 return ofVec4f();
621 }
622}
623
624inline ofVec4f& ofVec4f::normalize() {
625 float lenght = (float)sqrt(x*x + y*y + z*z + w*w);
626 if( lenght > 0 ) {
627 x /= lenght;
628 y /= lenght;
629 z /= lenght;
630 w /= lenght;
631 }
632 return *this;
633}
634
635
636
637// Limit length.
638//
639//
640inline ofVec4f ofVec4f::limited(float max) const {
641 return getLimited(max);
642}
643
644inline ofVec4f ofVec4f::getLimited(float max) const {
645 ofVec4f limited;
646 float lengthSquared = (x*x + y*y + z*z + w*w);
647 if( lengthSquared > max*max && lengthSquared > 0 ) {
648 float ratio = max/(float)sqrt(lengthSquared);
649 limited.set( x*ratio, y*ratio, z*ratio, w*ratio );
650 } else {
651 limited.set(x,y,z,w);
652 }
653 return limited;
654}
655
656inline ofVec4f& ofVec4f::limit(float max) {
657 float lengthSquared = (x*x + y*y + z*z + w*w);
658 if( lengthSquared > max*max && lengthSquared > 0 ) {
659 float ratio = max/(float)sqrt(lengthSquared);
660 x *= ratio;
661 y *= ratio;
662 z *= ratio;
663 w *= ratio;
664 }
665 return *this;
666}
667
668
669
670// Length
671//
672//
673inline float ofVec4f::length() const {
674 return (float)sqrt( x*x + y*y + z*z + w*w );
675}
676
677inline float ofVec4f::lengthSquared() const {
678 return (float)(x*x + y*y + z*z + w*w);
679}
680
681
682
683
687inline float ofVec4f::dot( const ofVec4f& vec ) const {
688 return x*vec.x + y*vec.y + z*vec.z + w*vec.w;
689}
690
691
692
693
694
695// Non-Member operators
696//
697//
698inline ofVec4f operator+( float f, const ofVec4f& vec ) {
699 return ofVec4f( f+vec.x, f+vec.y, f+vec.z, f+vec.w );
700}
701
702inline ofVec4f operator-( float f, const ofVec4f& vec ) {
703 return ofVec4f( f-vec.x, f-vec.y, f-vec.z, f-vec.w );
704}
705
706inline ofVec4f operator*( float f, const ofVec4f& vec ) {
707 return ofVec4f( f*vec.x, f*vec.y, f*vec.z, f*vec.w );
708}
709
710inline ofVec4f operator/( float f, const ofVec4f& vec ) {
711 return ofVec4f( f/vec.x, f/vec.y, f/vec.z, f/vec.w);
712}
713
714
716
ofVec2f is a class for storing a two dimensional vector.
Definition ofVec2f.h:72
ofVec3f is a class for storing a three dimensional vector.
Definition ofVec3f.h:79
Definition ofVec4f.h:11
ofVec4f & limit(float max)
Restrict the length (magnitude) of this vector to a maximum of 'max' units by scaling down if necessa...
ofVec4f(float _scalar)
ofVec4f & normalize()
Normalizes the vector. This changes the current vector to its normalized value.
float w
Definition ofVec4f.h:20
ofVec4f operator-() const
ofVec4f & scale(const float length)
Scales this vector up or down so that it has the requested length.
float x
Definition ofVec4f.h:17
float length() const
Returns the length (magnitude) of this vector.
float z
Definition ofVec4f.h:19
float squareDistance(const ofVec4f &pnt) const
void set(float _x, float _y, float _z, float _w)
float dot(const ofVec4f &vec) const
Calculates and returns the dot product of this vector with 'vec'.
float distance(const ofVec4f &pnt) const
Treats this vector and 'pnt' as points in 4D space and calculates the distance between them.
ofVec4f & operator*=(const ofVec4f &vec)
ofVec4f getLimited(float max) const
Returns a copy of this vector with its length (magnitude) restricted to a maximum of 'max' units by s...
ofVec4f operator+(const float f) const
ofVec4f & average(const ofVec4f *points, int num)
Sets this vector to be the average (center of gravity or centroid) of a given array of 'ofVec4f's.
ofVec4f getScaled(const float length) const
Returns a new ofVec4f that is the result of scaling this vector up or down so that it has the request...
ofVec4f getNormalized() const
Returns a normalized copy of this vector.
const float * getPtr() const
Definition ofVec4f.h:46
ofVec4f operator-(const float f) const
ofVec4f operator-(const ofVec4f &vec) const
float y
Definition ofVec4f.h:18
ofVec4f & operator/=(const ofVec4f &vec)
ofVec4f operator/(const float f) const
ofVec4f & operator/=(const float f)
ofVec4f & operator+=(const float f)
ofVec4f & operator*=(const float f)
void set(const ofVec4f &vec)
float * getPtr()
Definition ofVec4f.h:43
ofVec4f getInterpolated(const ofVec4f &pnt, float p) const
Performs a linear interpolation of this vector towards 'pnt'.
void set(float _scalar)
ofVec4f getMiddle(const ofVec4f &pnt) const
Calculates and returns the midpoint (as a vector) between this vector and 'pnt'.
ofVec4f operator/(const ofVec4f &vec) const
ofVec4f & middle(const ofVec4f &pnt)
Calculates and returns the midpoint (as a vector) between this vector and 'pnt'. This modifies the cu...
float operator[](int n) const
Definition ofVec4f.h:54
float & operator[](int n)
Definition ofVec4f.h:50
bool operator!=(const ofVec4f &vec) const
ofVec4f & operator-=(const float f)
ofVec4f(float _x, float _y, float _z, float _w)
bool match(const ofVec4f &vec, float tolerance=0.0001f) const
ofVec4f & interpolate(const ofVec4f &pnt, float p)
Performs a linear interpolation of this vector towards 'pnt'. This modifies the current vector to the...
ofVec4f operator*(const ofVec4f &vec) const
ofVec4f & operator-=(const ofVec4f &vec)
ofVec4f operator+(const ofVec4f &vec) const
ofVec4f & operator+=(const ofVec4f &vec)
ofVec4f operator*(const float f) const
bool operator==(const ofVec4f &vec) const
float lengthSquared() const
#define OF_DEPRECATED_MSG(message,...)
Definition ofConstants.h:78
std::ostream & operator<<(std::ostream &os, const ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:304
std::istream & operator>>(std::istream &is, ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:323
glm::vec3 operator+(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:292
glm::vec3 operator/(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:307
glm::vec3 operator*(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:302
glm::vec3 operator-(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:297