reference

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

ofVec3f Class Reference

ofVec3f is a class for storing a three dimensional vector. More...

#include <ofVec3f.h>

Public Member Functions

Construct a 3D vector
 ofVec3f ()
 Construct a 3D vector. Defaults to (0,0,0).
 
 ofVec3f (float x, float y, float z=0)
 Construt a 3D vector with x, y and z specified.
 
 ofVec3f (float scalar)
 Construct a 3D vector with x, y and z set to scalar
 
 ofVec3f (const ofVec2f &vec)
 
 ofVec3f (const ofVec4f &vec)
 Construct a new 3D vector from a 4D vector by throwing away the 'w' component.
 
 ofVec3f (const glm::vec2 &vec)
 
 ofVec3f (const glm::vec3 &vec)
 
 ofVec3f (const glm::vec4 &vec)
 
 operator glm::vec3 () const
 
Access components
float * getPtr ()
 Returns a pointer to the memory position of the first element of the vector ('x'); the other elements ('y' and 'z') immediately follow it in memory.
 
const float * getPtr () const
 
float & operator[] (int n)
 Allows to access the individual components of an 'ofVec3f' as though it is an array.
 
float operator[] (int n) const
 
void set (float x, float y, float z=0)
 Set 'x', 'y' and 'z' components of this vector with just one function call. 'z' is optional, it defaults to 0.
 
void set (const ofVec3f &vec)
 Setting the values by using other 3 dimension vector ofVec3f.
 
void set (float _scalar)
 
Comparison
bool operator== (const ofVec3f &vec) const
 Check for equality between two ofVec3f.
 
bool operator!= (const ofVec3f &vec) const
 Returns 'true' if any component is different to its corresponding component in 'vec', ie if 'x != vec.x' or 'y != vec.y' or 'z != vec.z'; otherwise returns 'false'.
 
bool match (const ofVec3f &vec, float tolerance=0.0001f) const
 Let you check if two vectors are similar given a tolerance threshold 'tolerance' (default = 0.0001).
 
bool isAligned (const ofVec3f &vec, float tolerance=0.0001f) const
 
bool isAlignedRad (const ofVec3f &vec, float tolerance=0.0001f) const
 
bool align (const ofVec3f &vec, float tolerance=0.0001f) const
 Returns 'true' if this vector is pointing in the same direction as 'vec', with an angle error threshold 'tolerance' in degrees (default 0.0001 degrees).

 
bool alignRad (const ofVec3f &vec, float tolerance=0.0001f) const
 Returns 'true' if this vector is pointing in the same direction as 'vec', with an angle error threshold 'tolerance' in radians (default 0.0001).
 
Operators
ofVec3f operator+ (const ofVec3f &pnt) const
 
ofVec3f operator+ (const float f) const
 
ofVec3foperator+= (const ofVec3f &pnt)
 
ofVec3foperator+= (const float f)
 
ofVec3f operator- (const ofVec3f &vec) const
 
ofVec3f operator- (const float f) const
 
ofVec3f operator- () const
 
ofVec3foperator-= (const ofVec3f &vec)
 
ofVec3foperator-= (const float f)
 
ofVec3f operator* (const ofVec3f &vec) const
 
ofVec3f operator* (const float f) const
 
ofVec3foperator*= (const ofVec3f &vec)
 
ofVec3foperator*= (const float f)
 
ofVec3f operator/ (const ofVec3f &vec) const
 
ofVec3f operator/ (const float f) const
 
ofVec3foperator/= (const ofVec3f &vec)
 
ofVec3foperator/= (const float f)
 
Simple manipulations
ofVec3f getScaled (const float length) const
 Return a new 'ofVec3f' that is the result of scaling this vector up or down so that it has the requested length.

 
ofVec3fscale (const float length)
 Scales this vector up or down so that it has the requested length.
 
ofVec3f getRotated (float angle, const ofVec3f &axis) const
 Returns a new vector that is the result of rotating this vector by 'angle' degrees about the given axis.
 
ofVec3f getRotated (float ax, float ay, float az) const
 Returns a new vector that is the result of performing an Euler rotation of this vector by 'ax' degrees about the x axis, 'ay' degrees about the y axis, and 'az' degrees about the z axis.
 
ofVec3f getRotated (float angle, const ofVec3f &pivot, const ofVec3f &axis) const
 Returns a new vector that is the result of rotating this vector by 'angle' degrees about the axis specified by 'axis', using 'pivot' as the origin of rotation.
 
ofVec3f getRotatedRad (float angle, const ofVec3f &axis) const
 Returns a new vector that is the result of rotating this vector by 'angle' radians about the given axis.
 
ofVec3f getRotatedRad (float ax, float ay, float az) const
 Returns a new vector that is the result of performing an Euler rotation of this vector by 'ax' radians about the x axis, 'ay' radians about the y axis, and 'az' radians about the z axis.
 
ofVec3f getRotatedRad (float angle, const ofVec3f &pivot, const ofVec3f &axis) const
 Returns a new vector that is the result of rotating this vector by 'angle' radians about the axis specified by 'axis', using 'pivot' as the origin of rotation.
 
ofVec3frotate (float angle, const ofVec3f &axis)
 Rotates this vector by 'angle' degrees around the given axis.
 
ofVec3frotate (float ax, float ay, float az)
 Performs an Euler rotation of this vector by 'ax' degrees about the x axis, 'ay' degrees about the y axis, and 'az' degrees about the z axis.
 
ofVec3frotate (float angle, const ofVec3f &pivot, const ofVec3f &axis)
 Rotates this vector by 'angle' degrees about the axis specified by 'axis', using 'pivot' as the origin of rotation.
 
ofVec3frotateRad (float angle, const ofVec3f &axis)
 Rotates this vector by 'angle' radians about the given axis.
 
ofVec3frotateRad (float ax, float ay, float az)
 Performs an Euler rotation of this vector by 'ax' radians about the x axis, 'ay' radians about the y axis, and 'az' radians about the z axis.
 
ofVec3frotateRad (float angle, const ofVec3f &pivot, const ofVec3f &axis)
 Rotates this vector by 'angle' radians around the axis specified by 'axis', using 'pivot' as the origin of rotation.
 
ofVec3f getMapped (const ofVec3f &origin, const ofVec3f &vx, const ofVec3f &vy, const ofVec3f &vz) const
 Return a new 'ofVec3f' calculated by copying this vector and then mapping from its default coordinate system – origin (0,0,0), X direction (1,0,0), Y direction (0,1,0), Z direction (0,0,1) – to a new coordinate system defined with origin at 'origin', X direction 'vx', and Y direction 'vy', and Z direction 'vz'.
 
ofVec3fmap (const ofVec3f &origin, const ofVec3f &vx, const ofVec3f &vy, const ofVec3f &vz)
 Map this vector from its default coordinate system – origin (0,0,0), X direction (1,0,0), Y direction (0,1,0), Z direction (0,0,1) – to a new coordinate system defined with origin at 'origin', X direction 'vx', and Y direction 'vy', and Z direction 'vz'.
 
Distance
float distance (const ofVec3f &pnt) const
 Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the distance between them.

 
float squareDistance (const ofVec3f &pnt) const
 Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the squared distance between them.
 
Interpolation
ofVec3f getInterpolated (const ofVec3f &pnt, float p) const
 Perform a linear interpolation of this vector's position towards 'pnt' and return the interpolated vector without altering the original. 'p' controls the amount to move towards 'pnt'. 'p' is normally between 0 and 1 and where 0 means stay the original position and 1 means move all the way to 'pnt', but you can also have 'p' greater than 1 overshoot 'pnt', or less than 0 to move backwards away from 'pnt'.

 
ofVec3finterpolate (const ofVec3f &pnt, float p)
 Perform a linear interpolation of this vector's position towards 'pnt'. 'p' controls the amount to move towards 'pnt'. 'p' is normally between 0 and 1 and where 0 means stay the original position and 1 means move all the way to 'pnt', but you can also have 'p' greater than 1 overshoot 'pnt', or less than 0 to move backwards away from 'pnt'.

 
ofVec3f getMiddle (const ofVec3f &pnt) const
 Calculate and return the midpoint between this vector and 'pnt'.
 
ofVec3fmiddle (const ofVec3f &pnt)
 
ofVec3faverage (const ofVec3f *points, int num)
 Sets this vector to be the average (centre of gravity or centroid) of a given array of 'ofVec3f's. 'points' is the array of 'ofVec3f's and 'num' specifies the number of 'ofVec3f's in the array.

 
Limit
ofVec3f getNormalized () const
 Return a normalized copy of this vector.
 
ofVec3fnormalize ()
 Normalize the vector.
 
ofVec3f getLimited (float max) const
 Return a copy of this vector with its length (magnitude) restricted to a maximum of 'max' units by scaling down if necessary.
 
ofVec3flimit (float max)
 Restrict the length (magnitude) of this vector to a maximum of 'max' units by scaling down if necessary.
 
Measurement
float length () const
 
float lengthSquared () const
 Return the squared length (squared magnitude) of this vector.
 
float angle (const ofVec3f &vec) const
 Calculate and return the coplanar angle in degrees between this vector and 'vec'.
 
float angleRad (const ofVec3f &vec) const
 Calculate and return the coplanar angle in radians between this vector and 'vec'.
 
Perpendicular
ofVec3f getPerpendicular (const ofVec3f &vec) const
 Construct a plane using this vector and 'vec' (by finding the plane that both lectors lie on), and return the vector that is perpendicular to that plane (the normal to that plane).
 
ofVec3fperpendicular (const ofVec3f &vec)
 Construct a plane using this vector and 'vec' (by finding the plane that both lie on), and set our 'x', 'y' and 'z' to be the vector that is perpendicular to the constructed plane (the normal to the plane).
 
ofVec3f getCrossed (const ofVec3f &vec) const
 Returns the cross product (vector product) of this vector and 'vec'. This is a binary operation on two vectors in three-dimensional space, which results in a vector that is perpendicular to both of the vectors being multiplied, and normal to the plane containing them. The name cross product is derived from the cross symbol X that is often used to designate this operation; the alternative name vector product emphasizes the vector (rather than scalar) nature of the result.
 
ofVec3fcross (const ofVec3f &vec)
 
float dot (const ofVec3f &vec) const
 Returns the dot product of this vector with 'vec'.
 

Public Attributes

float x
 Stores the X component of this vector.
 
float y
 Stores the Y component of this vector.
 
float z
 Stores the Z component of this vector.
 

Detailed Description

ofVec3f is a class for storing a three dimensional vector.

Moving through space requires knowledge of where things are and where they are going. Vector Maths is the class of mathematics that gives us control over these things in space, allowing for elegant and intuitive descriptions of complex structures and movement. Vectors are at the heart of animations, particle systems, and 2D and 3D graphics.

Vectors in mathematics in general are entities with magnitude (also called length) and direction. A vector whose magnitude is 1 (ie a vector that is normalized) is called a unit vector. Unit vectors are very handy for storing directions as they can be easily scaled up (or down) to represent motion in a particular direction with a particular length.

You will also see the term 'vector' used to describe an array of objects in C++ (such as text strings). Don't let this confuse you, they are quite different: one of them is a mathematical term for a fixed-length list of numbers that you can do mathematical operations on, the other is a C++-specific term that means 'dynamically sizeable array'.

'ofVec3f has three member variables, x, y, and z, which allow to conveniently 'store 3D properties of an object such as its position, velocity, or 'acceleration.

ofVec3f v1; // v1.x is 0, v1.y is 0, v1.z is 0
v1.set(10, 50, 80); // now v1.x is 10, v1.y is 50, v1.z is 80
ofVec3f is a class for storing a three dimensional vector.
Definition ofVec3f.h:79
void set(float x, float y, float z=0)
Set 'x', 'y' and 'z' components of this vector with just one function call. 'z' is optional,...

Using 'ofVec3f' greatly simplifies arithmetic operations in three dimensions. For example if you have two vectors 'v1' and 'v2', both of which represent a 3D change in position, you can find the total change of position of both of them just by doing an addition 'v1 + v2':

ofVec3f v1(5, 2, 1);
// so now v1 represents walking 5 steps forward then 2 steps
// sideways then 1 step upwards
v2.set(1, 1, 1);
// so now v2 represents walking 1 step forward then 1 step
// sideways then 1 step upwards
// what happens if you do v1 followed by v2?
// to find out just add v1 and v2 together:
ofVec3f result = v1 + v2;
// result is (6, 3, 1), or 6 steps forward then 3 steps sideways
// then 2 steps upwards

You can scale an 'ofVec3f' by multiplying it with a float:

// walk 5 steps forward then 2 steps sideways then 1 step upwards
ofVec3f v1(5, 2, 1);
// what happens if we do v1 three times?
ofVec3f result = v1 * 3; // result is (15, 6, 3), or
// 15 steps forward, 6 steps sideways and 3 steps upwards

This also works for subtraction and division.

As you can see this really makes dealing with vectors as easy as dealing with single 'float's or 'int's, and can reduce the number of lines of code you have to write by half, at the same time making your code much easier to read and understand!

See also
ofVec2f for 2D vectors
ofVec4f for 4D vectors

Constructor & Destructor Documentation

◆ ofVec3f() [1/8]

ofVec3f::ofVec3f ( )

Construct a 3D vector. Defaults to (0,0,0).

ofVec3f v1;
// default: v1.x is 0, v1.y is 0, v1.z is 0
ofVec3f v2 = ofVec3f(40, 20, 10);
// v2.x is 40, v2.y is 20, v2.z is 10
ofVec3f v3(0.1, 0.3, -1.5);
// v3.x is 0.1, v3.y is 0.3, v3.z is -1.5
ofVec3f()
Construct a 3D vector. Defaults to (0,0,0).

◆ ofVec3f() [2/8]

ofVec3f::ofVec3f ( float  x,
float  y,
float  z = 0 
)

Construt a 3D vector with x, y and z specified.

◆ ofVec3f() [3/8]

ofVec3f::ofVec3f ( float  scalar)
explicit

Construct a 3D vector with x, y and z set to scalar

◆ ofVec3f() [4/8]

ofVec3f::ofVec3f ( const ofVec2f vec)

◆ ofVec3f() [5/8]

ofVec3f::ofVec3f ( const ofVec4f vec)

Construct a new 3D vector from a 4D vector by throwing away the 'w' component.

ofVec3f mom = ofVec4f(40, 20, 10, 100);
ofVec3f v(mom); // v is (40, 20, 10)
Definition ofVec4f.h:11

◆ ofVec3f() [6/8]

ofVec3f::ofVec3f ( const glm::vec2 &  vec)

◆ ofVec3f() [7/8]

ofVec3f::ofVec3f ( const glm::vec3 &  vec)

◆ ofVec3f() [8/8]

ofVec3f::ofVec3f ( const glm::vec4 &  vec)

Member Function Documentation

◆ align()

bool ofVec3f::align ( const ofVec3f vec,
float  tolerance = 0.0001f 
) const

Returns 'true' if this vector is pointing in the same direction as 'vec', with an angle error threshold 'tolerance' in degrees (default 0.0001 degrees).

ofVec3f v1 = ofVec3f(40, 20, 70);
ofVec3f v2 = ofVec3f(4, 2, 7);
// v1.align(v2, 0.0) is true

◆ alignRad()

bool ofVec3f::alignRad ( const ofVec3f vec,
float  tolerance = 0.0001f 
) const

Returns 'true' if this vector is pointing in the same direction as 'vec', with an angle error threshold 'tolerance' in radians (default 0.0001).

ofVec3f v1 = ofVec3f(40, 20, 70);
ofVec3f v2 = ofVec3f(4, 2, 7);
// v1.align(v2, 0.0) is true

◆ angle()

float ofVec3f::angle ( const ofVec3f vec) const

Calculate and return the coplanar angle in degrees between this vector and 'vec'.

ofVec3f v1(1,0,0);
ofVec3f v2(0,1,0);
float angle = v1.angle(v2); // angle is 90
float angle(const ofVec3f &vec) const
Calculate and return the coplanar angle in degrees between this vector and 'vec'.

◆ angleRad()

float ofVec3f::angleRad ( const ofVec3f vec) const

Calculate and return the coplanar angle in radians between this vector and 'vec'.

ofVec3f v1(1,0,0);
ofVec3f v2(0,1,0);
float angle = v1.angle(v2); // angle is 90

◆ average()

ofVec3f & ofVec3f::average ( const ofVec3f points,
int  num 
)

Sets this vector to be the average (centre of gravity or centroid) of a given array of 'ofVec3f's. 'points' is the array of 'ofVec3f's and 'num' specifies the number of 'ofVec3f's in the array.

int numPoints = 10;
ofVec3f points[numPoints];
for ( int i=0; i<numPoints; i++ ) {
points[i].set( ofRandom(0,100), ofRandom(0,100), ofRandom(0,100) );
}
ofVec3f centroid;
centroid.average( points, numPoints );
// centroid now is the centre of gravity/average of all the random points
ofVec3f & average(const ofVec3f *points, int num)
Sets this vector to be the average (centre of gravity or centroid) of a given array of 'ofVec3f's....
float ofRandom(float max)
Get a random floating point number between 0 and max.
Definition ofMath.cpp:47

◆ cross()

ofVec3f & ofVec3f::cross ( const ofVec3f vec)

Set this vector to the cross product (vector product) of itself and 'vec'. This is a binary operation on two vectors in three-dimensional space, which results in a vector that is perpendicular to both of the vectors being multiplied, and normal to the plane containing them. The name cross product is derived from the cross symbol X that is often used to designate this operation; the alternative name vector product emphasizes the vector (rather than scalar) nature of the result.

◆ distance()

float ofVec3f::distance ( const ofVec3f pnt) const

Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the distance between them.

ofVec3f p1(3, 4, 2);
ofVec3f p2(6, 8, 5);
float distance = p1.distance( p2 ); // distance is 5.8310
float distance(const ofVec3f &pnt) const
Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the distance betw...

'distance' involves a square root calculation, which is one of the slowest things you can do in programming. If you don't need an exact number but rather just a rough idea of distance (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the distances are, you just want the shortest), you can use squareDistance() instead.

◆ dot()

float ofVec3f::dot ( const ofVec3f vec) const

Returns the dot product of this vector with 'vec'.

The dot product of two vectors, also known as the scalar product, is the product of the magnitude of the two vectors and the cosine of the angle between them.

One interpretation of the dot product is as a measure of how closely two vectors align with each other. If they point in exactly the same direction, their dot product will simply be the product of their magnitudes, if they are perpendicular, their dot product will be 0, and if they point in opposite directions, their dot product will be negative.

The dot product is in contrast to the cross product, which returns a vector rather than a scalar.

ofVec3f a1(2, 0, 0); // magnitude 2, parallel to x-axis
ofVec3f b1(3, 4, 0); // magnitude 5, 53.13 degree angle to a1
float dot = a1.dot(b1); // dot is 2 * 5 * cos(53.13) = 6.0
ofVec3f a2(1, 0, 0); // magnitude 1, parallel to x-axis
ofVec3f b2(0, 0, 1); // magnitude 1, 90 degree angle to a2
dot = a2.dot(b2); // dot is 1 * 1 * cos(90) = 0.0
ofVec3f a3(0, 1, 0); // magnitude 1, parallel to y-axis
ofVec3f b3(0, -1, 0); // magnitude 1, 180 degree angle to a3
dot = a3.dot(b3); // dot is 1 * 1 * cos(180) = -1.0
float dot(const ofVec3f &vec) const
Returns the dot product of this vector with 'vec'.

◆ getCrossed()

ofVec3f ofVec3f::getCrossed ( const ofVec3f vec) const

Returns the cross product (vector product) of this vector and 'vec'. This is a binary operation on two vectors in three-dimensional space, which results in a vector that is perpendicular to both of the vectors being multiplied, and normal to the plane containing them. The name cross product is derived from the cross symbol X that is often used to designate this operation; the alternative name vector product emphasizes the vector (rather than scalar) nature of the result.

CROSS Image courtesy of Wikipedia

◆ getInterpolated()

ofVec3f ofVec3f::getInterpolated ( const ofVec3f pnt,
float  p 
) const

Perform a linear interpolation of this vector's position towards 'pnt' and return the interpolated vector without altering the original. 'p' controls the amount to move towards 'pnt'. 'p' is normally between 0 and 1 and where 0 means stay the original position and 1 means move all the way to 'pnt', but you can also have 'p' greater than 1 overshoot 'pnt', or less than 0 to move backwards away from 'pnt'.

ofVec3f v1(0, 5, 0);
ofVec3f v2(10, 10, 20);
ofVec3f v3 = v1.getInterpolated(p2, 0.5); // v3 is (5, 7.5, 10)
ofVec3f v4 = v1.getInterpolated(p2, 0.8); // v4 is (8, 9, 16)
ofVec3f getInterpolated(const ofVec3f &pnt, float p) const
Perform a linear interpolation of this vector's position towards 'pnt' and return the interpolated ve...

◆ getLimited()

ofVec3f ofVec3f::getLimited ( float  max) const

Return a copy of this vector with its length (magnitude) restricted to a maximum of 'max' units by scaling down if necessary.

ofVec3f v1(5, 0, 1); // length is about 5.1
ofVec3f v2(2, 0, 1); // length is about 2.2
ofVec3f v1Limited = v1.getLimited(3);
// v1Limited is (2.9417, 0, 0.58835) which has length of 3 in the same direction as v1
ofVec3f v2Limited = v2.getLimited(3);
// v2Limited is (2, 0, 1) (same as v2)
ofVec3f getLimited(float max) const
Return a copy of this vector with its length (magnitude) restricted to a maximum of 'max' units by sc...

◆ getMapped()

ofVec3f ofVec3f::getMapped ( const ofVec3f origin,
const ofVec3f vx,
const ofVec3f vy,
const ofVec3f vz 
) const

Return a new 'ofVec3f' calculated by copying this vector and then mapping from its default coordinate system – origin (0,0,0), X direction (1,0,0), Y direction (0,1,0), Z direction (0,0,1) – to a new coordinate system defined with origin at 'origin', X direction 'vx', and Y direction 'vy', and Z direction 'vz'.

In most cases you want 'vx', 'vy', and 'vz' to be perpendicular and of unit length; if they are not perpendicular you will have shearing as part of the mapping, and if they are not of unit length you will have scaling as part of the mapping.

◆ getMiddle()

ofVec3f ofVec3f::getMiddle ( const ofVec3f pnt) const

Calculate and return the midpoint between this vector and 'pnt'.

ofVec3f v1(5, 0, 0);
ofVec3f v2(10, 10, 20);
ofVec3f mid = v1.getMiddle(v2); // mid gets (7.5, 5, 10)
ofVec3f getMiddle(const ofVec3f &pnt) const
Calculate and return the midpoint between this vector and 'pnt'.

◆ getNormalized()

ofVec3f ofVec3f::getNormalized ( ) const

Return a normalized copy of this vector.

Normalization means to scale the vector so that its length (magnitude) is exactly 1, at which stage all that is left is the direction. A normalized vector is usually called a unit vector, and can be used to represent a pure direction (heading).

ofVec3f v1(5, 0, 0);
ofVec3f v1Normalized = v1.getNormalized(); // (1, 0, 0)
ofVec3f v2(5, 0, 5);
ofVec3f v2Normalized = v2.getNormalized(); // (√2, 0, √2)
ofVec3f getNormalized() const
Return a normalized copy of this vector.

◆ getPerpendicular()

ofVec3f ofVec3f::getPerpendicular ( const ofVec3f vec) const

Construct a plane using this vector and 'vec' (by finding the plane that both lectors lie on), and return the vector that is perpendicular to that plane (the normal to that plane).

ofVec3f v1(1,0,0);
ofVec3f v2(0,1,0);
ofLog(OF_LOG_NOTICE, "%1.1f, %1.1f, %1.1f\n", p.x, p.y, p.z);
// prints "0.0, 0.0, 1.0"
A C++ stream-style logging interface.
Definition ofLog.h:299
float x
Stores the X component of this vector.
Definition ofVec3f.h:86
float y
Stores the Y component of this vector.
Definition ofVec3f.h:89
float z
Stores the Z component of this vector.
Definition ofVec3f.h:92
ofVec3f getPerpendicular(const ofVec3f &vec) const
Construct a plane using this vector and 'vec' (by finding the plane that both lectors lie on),...
void ofSetLogLevel(ofLogLevel level)
Sets the logging level to selectively show log messages.
Definition ofLog.cpp:40
@ OF_LOG_NOTICE
Definition ofLog.h:118

This method is usually used to calculate a normal vector to a surface, which can then be used to calculate lighting, collisions, and other 3D effects.

◆ getPtr() [1/2]

float * ofVec3f::getPtr ( )
inline

Returns a pointer to the memory position of the first element of the vector ('x'); the other elements ('y' and 'z') immediately follow it in memory.

ofVec3f v1 = ofVec3f(40, 20, 10);
float * v1Ptr = v1.getPtr();
float x = *(v1Ptr); // x is 40
float y = *(v1Ptr+1); // y is 20
float z = *(v1Ptr+2); // z is 10
float * getPtr()
Returns a pointer to the memory position of the first element of the vector ('x'); the other elements...
Definition ofVec3f.h:156

This is very useful when using arrays of 'ofVec3f's to store geometry information, as it allows the vector to be treated as a simple C array of 'float's that can be passed verbatim to OpenGL.

◆ getPtr() [2/2]

const float * ofVec3f::getPtr ( ) const
inline

◆ getRotated() [1/3]

ofVec3f ofVec3f::getRotated ( float  angle,
const ofVec3f axis 
) const

Returns a new vector that is the result of rotating this vector by 'angle' degrees about the given axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2 = v1.getRotated(45, ofVec3f(0, 0, 1)); // v2 is (0.707, 0.707, 0)
ofVec3f v3 = v1.getRotated(45, ofVec3f(0, 1, 0)); // v3 is (0.707, 0, -0.707)
ofVec3f getRotated(float angle, const ofVec3f &axis) const
Returns a new vector that is the result of rotating this vector by 'angle' degrees about the given ax...

◆ getRotated() [2/3]

ofVec3f ofVec3f::getRotated ( float  angle,
const ofVec3f pivot,
const ofVec3f axis 
) const

Returns a new vector that is the result of rotating this vector by 'angle' degrees about the axis specified by 'axis', using 'pivot' as the origin of rotation.

◆ getRotated() [3/3]

ofVec3f ofVec3f::getRotated ( float  ax,
float  ay,
float  az 
) const

Returns a new vector that is the result of performing an Euler rotation of this vector by 'ax' degrees about the x axis, 'ay' degrees about the y axis, and 'az' degrees about the z axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2 = v1.getRotated(0, 0, 45); // v2 is (0.707, 0.707, 0)
ofVec3f v3 = v1.getRotated(0, 45, 0); // v3 is (0.707, 0, -0.707)

Watch out for gimbal lock when specifying multiple rotations in the same call.

◆ getRotatedRad() [1/3]

ofVec3f ofVec3f::getRotatedRad ( float  angle,
const ofVec3f axis 
) const

Returns a new vector that is the result of rotating this vector by 'angle' radians about the given axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2 = v1.getRotated(PI / 4, ofVec3f(0, 0, 1)); // v2 is (0.707, 0.707, 0)
ofVec3f v3 = v1.getRotated(PI / 4, ofVec3f(0, 1, 0)); // v3 is (0.707, 0, -0.707)
#define PI
Definition ofMathConstants.h:21

◆ getRotatedRad() [2/3]

ofVec3f ofVec3f::getRotatedRad ( float  angle,
const ofVec3f pivot,
const ofVec3f axis 
) const

Returns a new vector that is the result of rotating this vector by 'angle' radians about the axis specified by 'axis', using 'pivot' as the origin of rotation.

◆ getRotatedRad() [3/3]

ofVec3f ofVec3f::getRotatedRad ( float  ax,
float  ay,
float  az 
) const

Returns a new vector that is the result of performing an Euler rotation of this vector by 'ax' radians about the x axis, 'ay' radians about the y axis, and 'az' radians about the z axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2 = v1.getRotatedRad(0, 0, PI / 4); // v2 is (0.707, 0.707, 0)
ofVec3f v3 = v1.getRotatedRad(0, PI / 4, 0); // v3 is (0.707, 0, -0.707)
ofVec3f getRotatedRad(float angle, const ofVec3f &axis) const
Returns a new vector that is the result of rotating this vector by 'angle' radians about the given ax...

Watch out for gimbal lock when specifying multiple rotations in the same call.

◆ getScaled()

ofVec3f ofVec3f::getScaled ( const float  length) const

Return a new 'ofVec3f' that is the result of scaling this vector up or down so that it has the requested length.

ofVec3f v1(3, 4); // length is 5
ofVec3f v2 = v1.getScaled(15); // v2 is (9, 12), which has length of 15
ofVec3f getScaled(const float length) const
Return a new 'ofVec3f' that is the result of scaling this vector up or down so that it has the reques...

◆ interpolate()

ofVec3f & ofVec3f::interpolate ( const ofVec3f pnt,
float  p 
)

Perform a linear interpolation of this vector's position towards 'pnt'. 'p' controls the amount to move towards 'pnt'. 'p' is normally between 0 and 1 and where 0 means stay the original position and 1 means move all the way to 'pnt', but you can also have 'p' greater than 1 overshoot 'pnt', or less than 0 to move backwards away from 'pnt'.

ofVec3f v1( 0, 5, 0 );
ofVec3f v2( 10, 10, 20 );
// go go gadget zeno
v1.interpolate( v2, 0.5 ); // v1 is now (5, 7.5, 10)
v1.interpolate( v2, 0.5 ); // v1 is now (7.5, 8.75, 15)
v1.interpolate( v2, 0.5 ); // v1 is now (8.75, 9.375, 17.5)
v1.interpolate( v2, 0.5 ); // v1 is now (9.375, 9.6875, 18.75)

◆ isAligned()

bool ofVec3f::isAligned ( const ofVec3f vec,
float  tolerance = 0.0001f 
) const

Checks if vectors look in the same direction.

◆ isAlignedRad()

bool ofVec3f::isAlignedRad ( const ofVec3f vec,
float  tolerance = 0.0001f 
) const

◆ length()

float ofVec3f::length ( ) const

Return the length (magnitude) of this vector.

ofVec3f v(3, 4, 1);
float len = v.length(); // len is 5.0990

‘length’ involves a square root calculation, which is one of the slowest things you can do in programming. If you don't need an exact number but rather just a rough idea of a length (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the lengths are, you just want the shortest), you can use lengthSquared() instead.

◆ lengthSquared()

float ofVec3f::lengthSquared ( ) const

Return the squared length (squared magnitude) of this vector.

ofVec3f v(3, 4, 1);
float len = v.length(); // len is 5.0990

Use as a much faster alternative to length() if you don't need to know an accurate length but rather just a rough idea of a length (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the lengths are, you just want the shortest). It avoids the square root calculation that is ordinarily required to calculate a length.

◆ limit()

ofVec3f & ofVec3f::limit ( float  max)

Restrict the length (magnitude) of this vector to a maximum of 'max' units by scaling down if necessary.

ofVec3f v1(5, 0, 1); // length is about 5.1
ofVec3f v2(2, 0, 1); // length is about 2.2
v1.limit(3);
// v1 is now (2.9417, 0, 0.58835) which has length of 3 in the same direction as at initialization
v2.limit(3);
// v2 is unchanged

◆ map()

ofVec3f & ofVec3f::map ( const ofVec3f origin,
const ofVec3f vx,
const ofVec3f vy,
const ofVec3f vz 
)

Map this vector from its default coordinate system – origin (0,0,0), X direction (1,0,0), Y direction (0,1,0), Z direction (0,0,1) – to a new coordinate system defined with origin at 'origin', X direction 'vx', and Y direction 'vy', and Z direction 'vz'.

In most cases you want 'vx', 'vy', and 'vz' to be perpendicular and of unit length; if they are not perpendicular you will have shearing as part of the mapping, and if they are not of unit length you will have scaling as part of the mapping.

◆ match()

bool ofVec3f::match ( const ofVec3f vec,
float  tolerance = 0.0001f 
) const

Let you check if two vectors are similar given a tolerance threshold 'tolerance' (default = 0.0001).

ofVec3f v1 = ofVec3f(40, 20, 70);
ofVec3f v2 = ofVec3f(40.01, 19.999, 70.05);
// v1.match(v2, 0.1) is true
// v1.match(v2, 0.01) is false (because (70.5-70) > 0.01)

◆ middle()

ofVec3f & ofVec3f::middle ( const ofVec3f pnt)

Set this vector to the midpoint between itself and 'pnt'.

ofVec3f v1( 0, 5, 0 );
ofVec3f v2( 10, 10, 20);
// go go gadget zeno
v1.middle( v2 ); // v1 is now (5, 7.5, 10)
v1.middle( v2 ); // v1 is now (7.5, 8.75, 15)
v1.middle( v2 ); // v1 is now (8.75, 9.375, 17.5)
v1.middle( v2 ); // v1 is now (9.375, 9.6875, 18.75)

◆ normalize()

ofVec3f & ofVec3f::normalize ( )

Normalize the vector.

Normalizing means to scale the vector so that its length (magnitude) is exactly 1, at which stage all that is left is the direction. A normalized vector is usually called a unit vector, and can be used to represent a pure direction (heading).

ofVec3f v1(5, 0, 0);
v1.normalize(); // v2 is now (1, 0, 0)
ofVec3f v2(5, 0, 5);
v2.normalize(); // v2 is now (√2, 0, √2)

◆ operator glm::vec3()

ofVec3f::operator glm::vec3 ( ) const

◆ operator!=()

bool ofVec3f::operator!= ( const ofVec3f vec) const

Returns 'true' if any component is different to its corresponding component in 'vec', ie if 'x != vec.x' or 'y != vec.y' or 'z != vec.z'; otherwise returns 'false'.

ofVec3f v1(40, 20, 10);
ofVec3f v2(50, 20, 40);
ofVec3f v3(40, 20, 10);
// ( v1 != v2 ) is true
// ( v1 != v3 ) is false

◆ operator*() [1/2]

ofVec3f ofVec3f::operator* ( const float  f) const

Return a new 'ofVec3f' that is this vector scaled by multiplying 'x', 'y', 'z' members by 'f'.

ofVec3f v1(2, 5, 1);
ofVec3f v2 = v1 * 4; // (8, 20, 4)

◆ operator*() [2/2]

ofVec3f ofVec3f::operator* ( const ofVec3f vec) const

Returns a new vector ('x'*'vec.x','y'*'vec.y','z'*'vec.z').

ofVec3f v1 = ofVec3f(40, 20, 10); inline
ofVec3f v2 = ofVec3f(2, 4, 10);
ofVec3f v3 = v1 * v2; // (80, 80, 100)

Useful for scaling a 3D point by a non-uniform scale.

◆ operator*=() [1/2]

ofVec3f & ofVec3f::operator*= ( const float  f)

Scale this vector by multiplying 'x', 'y' and 'z' members by 'f'.

ofVec3f v1(2, 5, 1);
v1 *= 4; // (8, 20, 4)

◆ operator*=() [2/2]

ofVec3f & ofVec3f::operator*= ( const ofVec3f vec)

Multiplies 'x' by 'vec.x', and multiplies 'y' by 'vec.y', and multiplies 'z' by 'vec.z'.

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(2, 4, 10);
v1 *= v2; // v1 is now (80, 80, 100)

Useful for scaling a 3D point by a non-uniform scale.

◆ operator+() [1/2]

ofVec3f ofVec3f::operator+ ( const float  f) const

Returns a new vector with a float value 'f' added to 'x', 'y' and 'z' members.

ofVec3f v1(2, 5, 1);
ofVec3f v2 = v1 + 10; // (12, 15, 11)

◆ operator+() [2/2]

ofVec3f ofVec3f::operator+ ( const ofVec3f pnt) const

Super easy vector addition. Returns a new vector ('x'+'vec.x','y'+'vec.y','z'+'vec.z').

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(25, 50, 10);
ofVec3f v3 = v1 + v2; // v3 is (65, 70, 20)

◆ operator+=() [1/2]

ofVec3f & ofVec3f::operator+= ( const float  f)

Adds a float value 'f' to 'x', 'y' and 'z' members.

ofVec3f v1(2, 5, 1);
v1 += 10; // (12, 15, 11)

◆ operator+=() [2/2]

ofVec3f & ofVec3f::operator+= ( const ofVec3f pnt)

Super easy addition assignment. Adds 'vec.x' to 'x', adds 'vec.y' to 'y' and adds 'vec.z' to 'z'.

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(25, 50, 10);
v1 += v2; // v1 is (65, 70, 20)

◆ operator-() [1/3]

ofVec3f ofVec3f::operator- ( ) const

Returns a new 'ofVec3f' that is the inverted version (mirrored in X, Y and Z) of this vector.

ofVec3f v1(2, 5, 1);
ofVec3f v2 = -v1; // (-2, -5, -1)

◆ operator-() [2/3]

ofVec3f ofVec3f::operator- ( const float  f) const

Returns a new vector with a float value 'f' subtracted from 'x', 'y' and 'z' members.

ofVec3f v1(2, 5, 1);
ofVec3f v2 = v1 - 10; // (-8, -5, -9)

◆ operator-() [3/3]

ofVec3f ofVec3f::operator- ( const ofVec3f vec) const

Super easy vector subtraction. Returns a new vector ('x'-'vec.x','y'-'vec.y','z'-'vec.z').

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(25, 50, 10);
ofVec3f v3 = v1 - v2; // v3 is (15, -30, 0)

◆ operator-=() [1/2]

ofVec3f & ofVec3f::operator-= ( const float  f)

Subtract a float value 'f' from 'x', 'y', and 'z' members.

ofVec3f v1(2, 5, 1);
v1 -= 10; // (-8, -5, -9)

◆ operator-=() [2/2]

ofVec3f & ofVec3f::operator-= ( const ofVec3f vec)

Super easy subtraction assignment. Subtracts 'vec.x' from 'x', subtracts 'vec.y' from 'y' and subtracts 'vec.z' from 'z'.

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(25, 50, 10);
v1 -= v2; // v1 is (15, -30, 0)

◆ operator/() [1/2]

ofVec3f ofVec3f::operator/ ( const float  f) const

Return a new 'ofVec3f' that is this vector scaled by dividing 'x', 'y' and 'z' members by 'f'.

ofVec3f v1(2, 5, 1);
ofVec3f v2 = v1 / 4; // (0.5, 1.25, 0.25)

◆ operator/() [2/2]

ofVec3f ofVec3f::operator/ ( const ofVec3f vec) const

Returns a new vector ('x'/'vec.x','y'/'vec.y','z'/'vec.z').

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(2, 4, 10);
ofVec3f v3 = v1 / v2; // (20, 5, 1)

Useful for scaling a 3D point by a non-uniform scale.

◆ operator/=() [1/2]

ofVec3f & ofVec3f::operator/= ( const float  f)

Scale this vector by dividing 'x', 'y' and 'z' members by 'f'.

ofVec3f v1(2, 5, 1);
v1 /= 4; // (0.5, 1.25, 0.25)

◆ operator/=() [2/2]

ofVec3f & ofVec3f::operator/= ( const ofVec3f vec)

Divides 'x' by 'vec.x', divides 'y' by 'vec.y', and divides 'z' by 'vec.z'.

ofVec3f v1 = ofVec3f(40, 20, 10);
ofVec3f v2 = ofVec3f(2, 4, 10);
v1 *= v2; // v1 is now (20, 5, 1)

Useful for scaling a 3D point by a non-uniform scale.

◆ operator==()

bool ofVec3f::operator== ( const ofVec3f vec) const

Check for equality between two ofVec3f.

Returns 'true' if each component is the same as the corresponding component in 'vec', ie if 'x == vec.x' and 'y == vec.y' and 'z == vec.z'; otherwise returns 'false'. But you should probably be using 'match' instead.

ofVec3f v1(40, 20, 10);
ofVec3f v2(50, 30, 10);
ofVec3f v3(40, 20, 10);
// ( v1 == v2 ) is false
// ( v1 == v3 ) is true

◆ operator[]() [1/2]

float & ofVec3f::operator[] ( int  n)
inline

Allows to access the individual components of an 'ofVec3f' as though it is an array.

ofVec3f v1 = ofVec3f(40, 20, 10);
float x = v1[0]; // x is 40
float y = v1[1]; // y is 20
float z = v1[2]; // z is 10

This function can be handy if you want to do the same operation to all 'x', 'y' and 'z' components, as it means you can just make a 'for' loop that repeats 3 times.

◆ operator[]() [2/2]

float ofVec3f::operator[] ( int  n) const
inline

◆ perpendicular()

ofVec3f & ofVec3f::perpendicular ( const ofVec3f vec)

Construct a plane using this vector and 'vec' (by finding the plane that both lie on), and set our 'x', 'y' and 'z' to be the vector that is perpendicular to the constructed plane (the normal to the plane).

ofVec3f v1(1,0,0);
ofVec3f v2(0,1,0);
v1.perpendicular(v2);
ofLog(OF_LOG_NOTICE, "%1.1f, %1.1f, %1.1f\n", v1.x, v1.y, v1.z);
// prints "0.0, 0.0, 1.0'

This method is usually used to calculate a normal vector to a surface, which can then be used to calculate lighting, collisions, and other 3D effects.

◆ rotate() [1/3]

ofVec3f & ofVec3f::rotate ( float  angle,
const ofVec3f axis 
)

Rotates this vector by 'angle' degrees around the given axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2(1, 0, 0);
v1.rotate(45, ofVec3f(0, 0, 1)); // v1 is now (0.707, 0.707, 0)
v2.rotate(45, ofVec3f(0, 1, 0)); // v2 is now (0.707, 0, -0.707)

◆ rotate() [2/3]

ofVec3f & ofVec3f::rotate ( float  angle,
const ofVec3f pivot,
const ofVec3f axis 
)

Rotates this vector by 'angle' degrees about the axis specified by 'axis', using 'pivot' as the origin of rotation.

◆ rotate() [3/3]

ofVec3f & ofVec3f::rotate ( float  ax,
float  ay,
float  az 
)

Performs an Euler rotation of this vector by 'ax' degrees about the x axis, 'ay' degrees about the y axis, and 'az' degrees about the z axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2(1, 0, 0);
v1.rotate(0, 0, 45); // v1 is now (0.707, 0.707, 0)
v2.rotate(0, 45, 0); // v2 is now (0.707, 0, -0.707)

Watch out for gimbal lock when specifying multiple rotations in the same call.

◆ rotateRad() [1/3]

ofVec3f & ofVec3f::rotateRad ( float  angle,
const ofVec3f axis 
)

Rotates this vector by 'angle' radians about the given axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2(1, 0, 0)
v1.rotateRad(PI / 4, ofVec3f(0, 0, 1)); // v1 is now (0.707, 0.707, 0)
v2.rotateRad(PI / 4, ofVec3f(0, 1, 0)); // v2 is now (0.707, 0, -0.707)
ofVec3f & rotateRad(float angle, const ofVec3f &axis)
Rotates this vector by 'angle' radians about the given axis.

◆ rotateRad() [2/3]

ofVec3f & ofVec3f::rotateRad ( float  angle,
const ofVec3f pivot,
const ofVec3f axis 
)

Rotates this vector by 'angle' radians around the axis specified by 'axis', using 'pivot' as the origin of rotation.

◆ rotateRad() [3/3]

ofVec3f & ofVec3f::rotateRad ( float  ax,
float  ay,
float  az 
)

Performs an Euler rotation of this vector by 'ax' radians about the x axis, 'ay' radians about the y axis, and 'az' radians about the z axis.

ofVec3f v1(1, 0, 0);
ofVec3f v2(1, 0, 0);
v1.rotateRad(0, 0, PI / 4); // v1 is now (0.707, 0.707, 0)
v2.rotateRad(0, PI / 4, 0); // v2 is now (0.707, 0, -0.707)

Watch out for gimbal lock when specifying multiple rotations in the same call.

◆ scale()

ofVec3f & ofVec3f::scale ( const float  length)

Scales this vector up or down so that it has the requested length.

ofVec3f v1(3, 4); // length is 5
v1.scale(15); // v1 is now (9, 12), which has length of 15

◆ set() [1/3]

void ofVec3f::set ( const ofVec3f vec)

Setting the values by using other 3 dimension vector ofVec3f.

v1.set(40, 20, 70);
v2.set(v1);

◆ set() [2/3]

void ofVec3f::set ( float  _scalar)

◆ set() [3/3]

void ofVec3f::set ( float  x,
float  y,
float  z = 0 
)

Set 'x', 'y' and 'z' components of this vector with just one function call. 'z' is optional, it defaults to 0.

v1.set(40, 20, 70);

◆ squareDistance()

float ofVec3f::squareDistance ( const ofVec3f pnt) const

Treats both this vector and 'pnt' as points in 3D space, and calculates and returns the squared distance between them.

ofVec3f p1(3, 4, 2);
ofVec3f p2(6, 8, 5);
float distance = p1.distance( p2 ); // distance is 5.8310

Use as a much faster alternative to distance() if you don't need to know an exact number but rather just a rough idea of distance (for example when finding the shortest distance of a bunch of points to a reference point, where it doesn't matter exactly what the distances are, you just want the shortest). It avoids the square root calculation that is ordinarily required to calculate a length.

Member Data Documentation

◆ x

float ofVec3f::x

Stores the X component of this vector.

◆ y

float ofVec3f::y

Stores the Y component of this vector.

◆ z

float ofVec3f::z

Stores the Z component of this vector.


The documentation for this class was generated from the following file:
  • /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/math/ofVec3f.h