CUGL 2.1
Cornell University Game Library
|
#include <CUVec4.h>
Public Member Functions | |
Vec4 () | |
Vec4 (float x, float y, float z, float w) | |
Vec4 (const float *array) | |
Vec4 (const Vec4 p1, const Vec4 p2) | |
Vec4 & | operator= (const float *array) |
Vec4 & | set (float x, float y, float z, float w) |
Vec4 & | set (const float *array) |
Vec4 & | set (const Vec4 v) |
Vec4 & | set (const Vec4 p1, const Vec4 p2) |
Vec4 & | setZero () |
Vec4 & | clamp (const Vec4 min, const Vec4 max) |
Vec4 | getClamp (const Vec4 min, const Vec4 max) const |
Vec4 & | add (const Vec4 v) |
Vec4 & | add (float x, float y, float z, float w) |
Vec4 & | subtract (const Vec4 v) |
Vec4 & | subtract (float x, float y, float z, float w) |
Vec4 & | scale (float s) |
Vec4 & | scale (float sx, float sy, float sz, float sw) |
Vec4 & | scale (const Vec4 v) |
Vec4 & | divide (float s) |
Vec4 & | divide (float sx, float sy, float sz, float sw) |
Vec4 & | divide (const Vec4 v) |
Vec4 & | negate () |
Vec4 & | reciprocate () |
Vec4 | getNegation () const |
Vec4 | getReciprocal () const |
Vec4 & | map (std::function< float(float)> func) |
Vec4 | getMap (std::function< float(float)> func) const |
Vec4 & | operator+= (const Vec4 v) |
Vec4 & | operator-= (const Vec4 v) |
Vec4 & | operator*= (float s) |
Vec4 & | operator*= (const Vec4 v) |
Vec4 & | operator/= (float s) |
Vec4 & | operator/= (const Vec4 v) |
const Vec4 | operator+ (const Vec4 v) const |
const Vec4 | operator- (const Vec4 v) const |
const Vec4 | operator- () const |
const Vec4 | operator* (float s) const |
const Vec4 | operator* (const Vec4 v) const |
const Vec4 | operator/ (float s) const |
const Vec4 | operator/ (const Vec4 v) const |
bool | operator< (const Vec4 v) const |
bool | operator<= (const Vec4 v) const |
bool | operator> (const Vec4 v) const |
bool | operator>= (const Vec4 v) const |
bool | operator== (const Vec4 v) const |
bool | operator!= (const Vec4 v) const |
bool | under (const Vec4 v) const |
bool | over (const Vec4 v) const |
bool | equals (const Vec4 v, float variance=CU_MATH_EPSILON) const |
bool | isZero () const |
bool | isNearZero (float variance=CU_MATH_EPSILON) const |
bool | isOne () const |
bool | isInvertible () const |
bool | isUnit (float variance=CU_MATH_EPSILON) const |
bool | isHomogenous () const |
float | getAngle (const Vec4 other) const |
float | distance (const Vec4 v) const |
float | distanceSquared (const Vec4 v) const |
float | length () const |
float | lengthSquared () const |
float | dot (const Vec4 v) const |
Vec4 | cross (const Vec4 v) |
Vec4 & | normalize () |
Vec4 | getNormalization () const |
Vec4 | getMidpoint (const Vec4 other) const |
Vec4 & | project (const Vec4 other) |
Vec4 | getProjection (const Vec4 other) const |
Vec4 & | homogenize () |
Vec4 | getHomogenized () |
Vec4 & | lerp (const Vec4 other, float alpha) |
Vec4 | getLerp (const Vec4 other, float alpha) |
void | smooth (const Vec4 target, float elapsed, float response) |
std::string | toString (bool verbose=false) const |
operator std::string () const | |
operator Color4 () const | |
Vec4 (Color4 color) | |
Vec4 & | operator= (Color4 color) |
operator Color4f () const | |
Vec4 (const Color4f color) | |
Vec4 & | operator= (const Color4f color) |
operator Vec2 () const | |
Vec4 (const Vec2 v) | |
Vec4 (const Vec2 v, float z, float w) | |
Vec4 & | operator= (const Vec2 size) |
operator Vec3 () const | |
Vec4 (const Vec3 v) | |
Vec4 (const Vec3 v, float w) | |
Vec4 & | operator= (const Vec3 v) |
Vec4 & | set (const Vec3 v, float w) |
Static Public Member Functions | |
static Vec4 * | clamp (const Vec4 v, const Vec4 min, const Vec4 max, Vec4 *dst) |
static float | angle (const Vec4 v1, const Vec4 v2) |
static Vec4 * | add (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | subtract (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | scale (const Vec4 v, float s, Vec4 *dst) |
static Vec4 * | scale (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | divide (const Vec4 v, float s, Vec4 *dst) |
static Vec4 * | divide (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | reciprocate (const Vec4 v, Vec4 *dst) |
static Vec4 * | negate (const Vec4 v, Vec4 *dst) |
static float | dot (const Vec4 v1, const Vec4 v2) |
static Vec4 * | cross (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | normalize (const Vec4 v, Vec4 *dst) |
static Vec4 * | homogenize (const Vec4 v, Vec4 *dst) |
static Vec4 * | midpoint (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | project (const Vec4 v1, const Vec4 v2, Vec4 *dst) |
static Vec4 * | lerp (const Vec4 v1, const Vec4 v2, float alpha, Vec4 *dst) |
Public Attributes | |
float | x |
float | y |
float | z |
float | w |
Static Public Attributes | |
static const Vec4 | ZERO |
static const Vec4 | ONE |
static const Vec4 | UNIT_X |
static const Vec4 | UNIT_Y |
static const Vec4 | UNIT_Z |
static const Vec4 | UNIT_W |
static const Vec4 | HOMOG_ORIGIN |
static const Vec4 | HOMOG_X |
static const Vec4 | HOMOG_Y |
static const Vec4 | HOMOG_Z |
This class defines a 3-element floating point vector.
This class may be used to represent either a normal, a direction or a point interchangeably without casting. In addition, instances of this class may be freely cast to Color4 and vectors of other sizes.
This class is in standard layout with fields of uniform type. This means that it is safe to reinterpret_cast objects to float arrays.
|
inline |
Constructs a new vector initialized to all zeros.
|
inline |
Constructs a new vector initialized to the specified values.
x | The x coordinate. |
y | The y coordinate. |
z | The z coordinate. |
w | The w coordinate. |
|
inline |
Constructs a new vector from the values in the specified array.
The elements of the arra are in the order x, y, z, and w.
array | An array containing the elements of the vector. |
Constructs a vector that describes the direction between the specified points.
p1 | The first point. |
p2 | The second point. |
|
explicit |
Creates a vector from the given color.
The attributes are read in the order r,g,b,a. Values are all divided uniformly by 255.0.
color | The color to convert |
|
explicit |
Creates a vector from the given color.
The attributes are read in the order r,g,b,a.
color | The color to convert |
|
explicit |
Creates a homogenous vector from the given 2d one.
The z-value is set to 0. However, the w-value is set to 1, to support homogenous coordinates.
v | The vector to convert |
cugl::Vec4::Vec4 | ( | const Vec2 | v, |
float | z, | ||
float | w | ||
) |
Creates a 4d vector from the given 2d one
The z-coordinate and w-coordinate are set to given values.
v | The vector to convert |
z | The z-coordinate |
w | The w-coordinate |
|
explicit |
Creates a homogenous vector from the given 3d one.
The w-value is set to 1. That is because the standard usage of Vec4 objects is homogenous coords.
v | The vector to convert |
cugl::Vec4::Vec4 | ( | const Vec3 | v, |
float | w | ||
) |
Creates a 4d vector from the given 3d one
The w-value is set to given value.
v | The vector to convert |
w | The w-coordinate |
Adds the given vector to this one in place.
v | The vector to add |
Adds the specified vectors and stores the result in dst.
v1 | The first vector. |
v2 | The second vector. |
dst | A vector to store the result in |
|
inline |
Adds the given values to this vector.
x | The x coordinate to add. |
y | The y coordinate to add. |
z | The z coordinate to add. |
w | The w coordinate to add. |
Returns the angle (in radians) between the specified vectors.
This computes the angle between the vectors in 4d space. It does not treat the vectors as homogenous coordinates. To get the angle between two homogenous vectors, cast them to Vec3 and use Vec3::angle.
The angle returned is unsigned, as there is no clear sense of direction. If either vector is zero, this method returns zero.
v1 | The first vector. |
v2 | The second vector. |
Clamps this vector within the given range.
min | The minimum value. |
max | The maximum value. |
Clamps the specified vector within the given range and returns it in dst.
v | The vector to clamp. |
min | The minimum value. |
max | The maximum value. |
dst | A vector to store the result in. |
Return the cross product of this vector and the specified vector.
This method assumes that this vector and v are homogenous vectors (w=1). It returns a projection vector (w=0).
v | The vector to cross. |
Returns the cross product between the specified vectors.
The vectors are treated as 3-dimensional vectors, with the w-attribute ignored.
v1 | The first vector. |
v2 | The second vector. |
dst | The destination vector. |
|
inline |
Returns the distance between this vector and v.
v | The other vector. |
{
float cugl::Vec4::distanceSquared | ( | const Vec4 | v | ) | const |
Returns the squared distance between this vector and v.
This method is faster than distance because it does not need to compute a square root. Hence it is best to us this method when it is not necessary to get the exact distance between two vectors (e.g. when simply comparing the distance between different vectors).
v | The other vector. |
{
Divides this vector in place by the given vector.
This method is provided to support non-uniform scaling.
v | The vector to divide by |
Divides the specified vector and stores the result in dst.
The division is uniform by the constant s.
v | The vector to divide. |
s | The uniform division factor. |
dst | The destination vector. |
Divides the specified vector and stores the result in dst.
The division is nonuniform by the attributes in v2.
v1 | The vector to divide. |
v2 | The nonuniform division factor. |
dst | The destination vector. |
Vec4& cugl::Vec4::divide | ( | float | s | ) |
Divides this vector in place by the given factor.
s | The scalar to divide by |
Vec4& cugl::Vec4::divide | ( | float | sx, |
float | sy, | ||
float | sz, | ||
float | sw | ||
) |
Divides this vector nonuniformly by the given factors.
sx | The scalar to divide the x-axis |
sy | The scalar to divide the y-axis |
sz | The scalar to divide the z-axis |
sw | The scalar to divide the w-axis |
|
inline |
Returns the dot product of this vector and the specified vector.
v | The vector to compute the dot product with. |
Returns the dot product between the specified vectors.
v1 | The first vector. |
v2 | The second vector. |
bool cugl::Vec4::equals | ( | const Vec4 | v, |
float | variance = CU_MATH_EPSILON |
||
) | const |
Returns true if the vectors are within tolerance of each other.
The tolerance bounds the traditional Euclidean difference between the two vectors (treated as points)
v | The vector to compare against. |
variance | The comparison tolerance. |
float cugl::Vec4::getAngle | ( | const Vec4 | other | ) | const |
Returns the angle between this vector and other.
The angle is measured starting at this one. If either vector is zero, the result is undefined.
other | The vector to sweep towards. |
Returns a copy of this vector clamped within the given range.
Note: this does not modify this vector.
min | The minimum value. |
max | The maximum value. |
|
inline |
Returns a normalized homogenous copy of this vector.
If w is 0, the copy has a w value to 1. Otherwise, the copy has all of its attributes divided by w.
Note: this does not modify this vector.
Returns the linear interpolation of this vector with other.
If alpha is 0, the vector is unchanged. If alpha is 1, the vector is other. Otherwise it is a value on the line ab. This method supports alpha outside of the range 0..1.
other | The other end of the line segment. |
alpha | The interpolation value |
|
inline |
Returns a copy of this vector with func applied to each component.
This method supports any function that has the signature float func(float); This includes many of the functions in math.h.
func | The function to map on the coordinates. |
Returns the midpoint between this point and another.
Note: this does not modify this vector.
other | The other end of the line segment. |
|
inline |
Returns a negated copy of this vector.
Note: This method does not modify the vector
|
inline |
Returns a normalized copy of this vector.
This method creates a copy of this vector that is of unit length (i.e. the length of the vector after calling this method will be 1.0f). If the vector already has unit length or if the length of the vector is zero, this method does nothing. Note: this does not modify this vector.
Returns the projection of this vector on to the other one.
Note: this does not modify this vector.
other | The vector to project on. |
|
inline |
Returns a reciprocated copy of this vector.
The reciprocal is computed for each element invidiually. This method does not check that all elements are non-zero. If any element is zero, the result will be system-dependent.
Note: This method does not modify the vector
|
inline |
Normalizes the homogenous representation of this vector.
If w is 0, it sets the value to 1. Otherwise, it divides all attributes by w.
Homogenizes the specified vector and stores the result in dst.
If w is 0, it sets the value to 1. Otherwise, it divides all attributes by w.
v | The vector to homogenize. |
dst | The destination vector. |
|
inline |
Returns true if this vector is in homogenous coordinates (w=1).
bool cugl::Vec4::isInvertible | ( | ) | const |
Returns true if this vector contains no zeroes.
|
inline |
Returns true if this vector is with tolerance of the origin.
variance | The comparison tolerance |
bool cugl::Vec4::isOne | ( | ) | const |
Returns true if this vector contains all ones.
|
inline |
Returns true if this vector is a unit vector.
variance | The comparison tolerance |
bool cugl::Vec4::isZero | ( | ) | const |
Returns true this vector contains all zeros.
|
inline |
|
inline |
Returns the squared length of this vector.
This method is faster than length because it does not need to compute a square root. Hence it is best to us this method when it is not necessary to get the exact length of a vectors (e.g. when simply comparing the length to a threshold value).
{
Modifies this vector to be the linear interpolation with other.
If alpha is 0, the vector is unchanged. If alpha is 1, the vector is other. Otherwise it is a value on the line ab. This method supports alpha outside of the range 0..1.
other | The other end of the line segment. |
alpha | The interpolation value |
Computes the linear interpolation of two vectors and stores it in dst.
If alpha is 0, the vector is a copy of v1. If alpha is 1, the vector is a copy of v2. Otherwise it is a value on the line v1v2. This method supports alpha outside of the range 0..1.
v1 | The first point. |
v2 | The second point. |
alpha | The interpolation value. |
dst | The destination vector. |
|
inline |
Maps the given function to the vector coordinates in place.
This method supports any function that has the signature float func(float); This includes many of the functions in math.h.
func | The function to map on the coordinates. |
Computes the midpoint between two points and stores it in dst.
v1 | The first point. |
v2 | The second point. |
dst | The destination vector. |
|
inline |
Negates this vector in place.
Negates the specified vector and stores the result in dst.
v | The vector to negate. |
dst | The destination vector. |
Vec4& cugl::Vec4::normalize | ( | ) |
Normalizes this vector.
This method normalizes the vector so that it is of unit length (i.e. the length of the vector after calling this method will be 1.0f). If the vector already has unit length or if the length of the vector is zero, this method does nothing.
Normalizes the specified vector and stores the result in dst.
If the vector already has unit length or if the length of the vector is zero, this method copies v into dst.
v | The vector to normalize. |
dst | The destination vector. |
|
inline |
Cast from Vec4 to a string.
cugl::Vec4::operator Vec2 | ( | ) | const |
Casts from a homogenous vector to Vec4.
All coordinates are divided by the w-coordinate (assuming it is not zero) before this conversion. Afterwards, both z and w are dropped.
cugl::Vec4::operator Vec3 | ( | ) | const |
bool cugl::Vec4::operator!= | ( | const Vec4 | v | ) | const |
Returns true if this vector is not equal to the given vector.
Comparison is exact, which may be unreliable given that the attributes are floats.
v | The vector to compare against. |
Returns the scalar product of this vector with the given vector.
This method is provided to support non-uniform scaling. Note: this does not modify this vector.
v | The vector to scale by. |
|
inline |
Returns the scalar product of this vector with the given value.
Note: this does not modify this vector.
s | The value to scale by. |
Scales this vector nonuniformly by the given vector.
v | The vector to scale by |
|
inline |
Scales this vector in place by the given factor.
s | The value to scale by |
Returns the sum of this vector with the given vector.
Note: this does not modify this vector.
v | The vector to add. |
Adds the given vector to this one in place.
v | The vector to add |
|
inline |
Returns the negation of this vector.
Note: this does not modify this vector.
Returns the difference of this vector with the given vector.
Note: this does not modify this vector.
v | The vector to subtract. |
Subtracts the given vector from this one in place.
v | The vector to subtract |
Returns a copy of this vector divided by the given vector.
This method is provided to support non-uniform scaling. Note: this does not modify this vector.
v | the vector to divide this vector with |
|
inline |
Returns a copy of this vector divided by the given constant
Note: this does not modify this vector.
s | the constant to divide this vector with |
Divides this vector in place by the given vector.
This method is provided to support non-uniform scaling.
v | The vector to divide by |
|
inline |
Divides this vector in place by the given factor.
s | The scalar to divide by |
bool cugl::Vec4::operator< | ( | const Vec4 | v | ) | const |
Returns true if this vector is less than the given vector.
This comparison uses the lexicographical order. To test if all components in this vector are less than those of v, use the method under().
v | The vector to compare against. |
bool cugl::Vec4::operator<= | ( | const Vec4 | v | ) | const |
Returns true if this vector is less than or equal the given vector.
This comparison uses the lexicographical order. To test if all components in this vector are less than those of v, use the method under().
v | The vector to compare against. |
Sets the coordinates of this vector to those of the given color.
The attributes are read in the order r,g,b,a. Values are all divided uniformly by 255.0.
color | The color to convert. |
Sets the coordinates of this vector to those of the given color.
The attributes are read in the order r,g,b,a.
color | The color to convert. |
|
inline |
Sets the elements of this vector from the values in the specified array.
The elements of the array are in the order x, y, z, and w.
array | An array containing the elements of the vector. |
Sets the coordinates of this vector to those of the given 2d vector.
The z-value is set to 0. However, the w-value is set to 1, to support homogenous coordinates.
size | The vector to convert |
bool cugl::Vec4::operator== | ( | const Vec4 | v | ) | const |
Returns true if this vector is equal to the given vector.
Comparison is exact, which may be unreliable given that the attributes are floats.
v | The vector to compare against. |
bool cugl::Vec4::operator> | ( | const Vec4 | v | ) | const |
Determines if this vector is greater than the given vector.
This comparison uses the lexicographical order. To test if all components in this vector are greater than those of v, use the method over().
v | The vector to compare against. |
bool cugl::Vec4::operator>= | ( | const Vec4 | v | ) | const |
Determines if this vector is greater than or equal the given vector.
This comparison uses the lexicographical order. To test if all components in this vector are greater than those of v, use the method over().
v | The vector to compare against. |
bool cugl::Vec4::over | ( | const Vec4 | v | ) | const |
Returns true if this vector dominates the given vector.
Domination means that all components of this vector are greater than or equal to the components of the given vector.
v | The vector to compare against. |
Modifies this vector to be its projection on to the other one.
other | The vector to project on. |
Computes the projection of one vector on to another and stores it in dst.
v1 | The original vector. |
v2 | The vector to project on. |
dst | The destination vector. |
Vec4& cugl::Vec4::reciprocate | ( | ) |
Reciprovates this vector in place.
The reciprocal is computed for each element invidiually. This method does not check that all elements are non-zero. If any element is zero, the result will be system-dependent.
Reciprocates the specified quaternion and stores the result in dst.
Reciprocation is applied to each element invidiually. This method does not check that all elements are non-zero. If any element is zero, the result will be system-dependent.
v | The vector to reciprocate. |
dst | The destination vector. |
Scales this vector nonuniformly by the given vector.
v | The vector to scale by |
Scales the specified vector and stores the result in dst.
The scale is uniform by the constant s.
v | The vector to scale. |
s | The uniform scaling factor. |
dst | The destination vector. |
Scales the specified vector and stores the result in dst.
The scale is nonuniform by the attributes in v2.
v1 | The vector to scale. |
v2 | The nonuniform scaling factor. |
dst | The destination vector. |
|
inline |
Scales this vector in place by the given factor.
s | The scalar to multiply by |
|
inline |
Scales this vector nonuniformly by the given factors.
sx | The scalar to multiply the x-axis |
sy | The scalar to multiply the y-axis |
sz | The scalar to multiply the z-axis |
sw | The scalar to divide the w-axis |
|
inline |
Sets the elements of this vector from the values in the specified array.
The elements of the arra are in the order x, y, z, and w.
array | An array containing the elements of the vector. |
Sets the coordinates of this vector to those of the given 3d vector.
The w-value is set to given value.
v | The vector to convert |
w | The w-coordinate |
Sets this vector to the directional vector between the specified points.
p1 | The initial point of the vector. |
p2 | The terminal point of the vector. |
Sets the elements of this vector to those in the specified vector.
v | The vector to copy. |
|
inline |
Sets the elements of this vector to the specified values.
x | The new x coordinate. |
y | The new y coordinate. |
z | The new z coordinate. |
w | The new w coordinate. |
|
inline |
Sets the elements of this vector to zero.
void cugl::Vec4::smooth | ( | const Vec4 | target, |
float | elapsed, | ||
float | response | ||
) |
Updates this vector towards the given target using a smoothing function.
The given response time determines the amount of smoothing (lag). A longer response time yields a smoother result and more lag. To force this vector to follow the target closely, provide a response time that is very small relative to the given elapsed time.
target | The target value. |
elapsed | The elapsed time between calls. |
response | The response time (in the same units as elapsed). |
Subtracts the given vector from this one in place.
v | The vector to subtract |
Subtracts the specified vectors and stores the result in dst.
The resulting vector is computed as (v1 - v2).
v1 | The first vector. |
v2 | The second vector. |
dst | The destination vector. |
|
inline |
Subtracts the given values from this vector.
x | The x coordinate to subtract. |
y | The y coordinate to subtract. |
z | The z coordinate to subtract. |
w | The w coordinate to subtract. |
std::string cugl::Vec4::toString | ( | bool | verbose = false | ) | const |
Returns a string representation of this vector for debugging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
verbose | Whether to include class information |
bool cugl::Vec4::under | ( | const Vec4 | v | ) | const |
Returns true if this vector is dominated by the given vector.
Domination means that all components of the given vector are greater than or equal to the components of this one.
v | The vector to compare against. |
|
static |
The homogenous origin Vec4(0,0,0,1)
|
static |
The homogenous x-axis Vec4(1,0,0,1)
|
static |
The homogenous y-axis Vec4(0,1,0,1)
|
static |
The homogenous z-axis Vec4(0,0,1,1)
|
static |
The ones vector Vec4(1,1,1,1)
|
static |
The w-axis Vec4(0,0,0,1)
|
static |
The x-axis Vec4(1,0,0,0)
|
static |
The y-axis Vec4(0,1,0,0)
|
static |
The z-axis Vec4(0,0,1,0)
float cugl::Vec4::w |
The w-coordinate.
float cugl::Vec4::x |
The x-coordinate.
float cugl::Vec4::y |
The y-coordinate.
float cugl::Vec4::z |
The z-coordinate.
|
static |
The zero vector Vec4(0,0,0,0)