Core 1.0
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | Friends | List of all members
Vector Class Reference

Vectors in three dimensions. More...

#include <evector.h>

Public Member Functions

 Vector ()
 Empty.
 
 Vector (const double &)
 Create a vector with the same coordinates. More...
 
 Vector (const double &, const double &, const double &)
 Create a vector with argument coordinates. More...
 
constexpr double & operator[] (int)
 Gets the i-th coordinate of vector.
 
constexpr double operator[] (int) const
 Returns the i-th coordinate of vector.
 
Vector operator+ () const
 Overloaded.
 
Vector operator- () const
 Overloaded.
 
Vectoroperator+= (const Vector &)
 Destructive addition.
 
Vectoroperator-= (const Vector &)
 Destructive subtraction.
 
Vectoroperator*= (const Vector &)
 Destructively scale a vector by another vector. More...
 
Vectoroperator/= (const Vector &)
 Destructively divide the components of a vector by another vector.
 
Vectoroperator*= (const double &)
 Destructive scalar multiply.
 
Vectoroperator/= (const double &)
 Destructive division by a scalar.
 
double Max () const
 Compute the maximum component of a vector. More...
 
int MaxIndex () const
 Compute the index of the maximum component of a vector. More...
 
int MinIndex () const
 Compute the index of the minimum component of a vector. More...
 
Vector Orthogonal () const
 Returns a vector orthogonal to the argument vector. More...
 
void RandomOrthonormal (Vector &, Vector &) const
 Computes two random orthonormal vectors to the argument vector. More...
 
void Orthonormal (Vector &, Vector &) const
 Given a vector, creates two vectors xand y that form an orthogonal basis. More...
 
int Octant (const Vector &) const
 Computes octant index of a vector with respect to the vector object. More...
 
Vector Scaled (const Vector &) const
 Scale a vector. More...
 
Vector Inverse () const
 Inverse of a vector. More...
 
Vector Sort () const
 Sort the terms of the vector into ascending order.
 
QString ToString (int=6) const
 Create a QString form a vector. More...
 
double Angle (const Vector &) const
 Compute the angle between two vectors. More...
 
Vector Fract () const
 Compute the fractional part of the coordinates. More...
 
Vector Floor () const
 Compute the numerator part of the coordinates. More...
 

Static Public Member Functions

static bool Equal (const Vector &, const Vector &, const double &=0.0001)
 Test if two vectors are almost equal. More...
 
static Vector Min (const Vector &, const Vector &)
 Return a vector with coordinates set to the minimum coordinates of the two argument vectors.
 
static Vector Max (const Vector &, const Vector &)
 Return a vector with coordinates set to the maximum coordinates of the two argument vectors.
 
static void SetMinMax (const Vector &, Vector &, Vector &)
 Update the minimum and maximum values given a vector. More...
 
static Vector Mod (const Vector &, const Vector &)
 Modulo of two Vectors. More...
 
static bool Coplanar (const Vector &, const Vector &, const Vector &, const double &=1.0e-6)
 Check if three vectors are coplanar. More...
 
static bool Coplanar (const Vector &, const Vector &, const Vector &, const Vector &, const double &=1.0e-6)
 Check if four points are coplanar. More...
 
static Vector Bilinear (const Vector &, const Vector &, const Vector &, const Vector &, const double &, const double &)
 Bi-linear interpolation between four vectors. More...
 
static Vector Trilinear (const Vector &, const Vector &, const Vector &, const Vector &, const Vector &, const Vector &, const Vector &, const Vector &, const double &, const double &, const double &)
 Trilinear interpolation between eight vectors. More...
 
static double Slope (const Vector &, const Vector &)
 Compute the vertical slope between two vectors. More...
 
static Vector FromString (QString)
 Creates a vector from a QString. More...
 
static Vector Polar (const double &, const double &)
 Creates a vector given polar coordinates. More...
 
static Vector Solve (const Vector &, const Vector &, const double &, const double &)
 Compute the point on a segment such that the linear function satisfies f(a)=va and f(b)=vb. More...
 

Static Public Attributes

static const Vector Null = Vector(0.0, 0.0, 0.0)
 Null vector.
 
static const Vector X = Vector(1.0, 0.0, 0.0)
 Vector(1,0,0).
 
static const Vector Y = Vector(0.0, 1.0, 0.0)
 Vector(0,1,0).
 
static const Vector Z = Vector(0.0, 0.0, 1.0)
 Vector(0,0,1).
 

Protected Attributes

double c [3]
 Components.
 

Friends

bool operator> (const Vector &, const Vector &)
 Compare two vectors.
 
bool operator< (const Vector &, const Vector &)
 Compare two vectors.
 
bool operator>= (const Vector &, const Vector &)
 Overloaded.
 
bool operator<= (const Vector &, const Vector &)
 Overloaded.
 
Vector operator+ (const Vector &, const Vector &)
 Adds up two vectors.
 
Vector operator- (const Vector &, const Vector &)
 Difference between two vectors.
 
constexpr friend double operator* (const Vector &, const Vector &)
 Scalar product.
 
Vector operator* (const Vector &, double)
 Right multiply by a scalar.
 
Vector operator* (const double &, const Vector &)
 Left multiply by a scalar.
 
Vector operator/ (const Vector &, double)
 Left multiply by a scalar.
 
Vector operator/ (const Vector &, const Vector &)
 Cross product.
 
bool operator== (const Vector &, const Vector &)
 Strong equality test.
 
bool operator!= (const Vector &, const Vector &)
 Strong difference test.
 
double Norm (const Vector &)
 Compute the Euclidean norm of a vector. More...
 
double SquaredNorm (const Vector &)
 Compute the squared Euclidean norm of a vector. More...
 
double NormInfinity (const Vector &)
 Compute the norm infinity of a vector. More...
 
void Normalize (Vector &)
 Normalize a vector, computing the inverse of its norm and scaling the components. More...
 
Vector Normalized (const Vector &)
 Return a normalized vector. More...
 
double Sine (const Vector &, const Vector &)
 Returns the positive sine of two vectors. More...
 
double Cosine (const Vector &, const Vector &)
 Returns the positive cosine of two vectors. More...
 
Vector Abs (const Vector &)
 Computes the absolute value of a vector. More...
 
void Swap (Vector &, Vector &)
 Swap two vectors. More...
 
void Swap (Vector *&, Vector *&)
 Swap two pointers to (arrays) vectors. More...
 
int Aligned (const Vector &, const Vector &)
 Check if two vectors are aligned. More...
 
Vector Clamp (const Vector &, const Vector &, const Vector &)
 Clamp a vector between two bounds. More...
 
Vector Lerp (const Vector &, const Vector &, const double &)
 Linear interpolation between two vectors. More...
 
std::ostream & operator<< (std::ostream &, const Vector &)
 Overloaded output-stream operator. More...
 

Detailed Description

Vectors in three dimensions.

Most binary operators have been overloaded as expected, destructive operators, such as addition and subtraction have been implemented and behave as one could expect.

How do I compute the cross product of two vectors?
Simply use the overloaded Vector::operator/, for instance

Vector c=a/b; // Cross product
Vectors in three dimensions.
Definition: evector.h:21
double c[3]
Components.
Definition: evector.h:23

computes the cross product of a and b.

How do I compute the sine of the angle between two vectors?
Simply use the Sine(const Vector&,const Vector&) function, which internally computes the norm of the cross product divided by the norm of the argument vectors.

double s=Sine(a,b); // Equivalent to Norm(a/b)/(Norm(a)*Norm(b));
friend double Sine(const Vector &, const Vector &)
Returns the positive sine of two vectors.
Definition: evector.cpp:111

How can I get access to the x, y and z components of a vector?
Use v[0], v[1] and v[2] to get access to the x, y and z components of a vector v respectively.

How do I compute the normal of a triangle?
Let a,b,c the vertices of the triangle, simply compute the cross product

Vector n=(a-b)/(a-c); // Cross product

or use the member function of the Triangle class:

Vector n=Triangle(a,b,c).Normal(); // Compute the normal
Base minimum storage triangle class.
Definition: triangle.h:17
Vector Normal() const
Compute the normal vector of the triangle.
Definition: triangle.cpp:64

How can I sort the three elements in a vector?
Use Vector::Sort() as follows:

Vector s=Vector(2.0,3.0,-1.0).Sort(); // Sort components in ascending order
Vector()
Empty.
Definition: evector.h:26

How do I perform bi-linear interpolation on vectors?
Use Vector::Bilinear() with four vectors and bilinear coordinates. Alternatively, some geometric classes implement bilinear interpolation, such as Quadrangle::Vertex().

Constructor & Destructor Documentation

◆ Vector() [1/2]

Vector::Vector ( const double &  a)
inlineexplicit

Create a vector with the same coordinates.

Parameters
aReal.

◆ Vector() [2/2]

Vector::Vector ( const double &  a,
const double &  b,
const double &  c 
)
inlineexplicit

Create a vector with argument coordinates.

Parameters
a,b,cCoordinates.

Member Function Documentation

◆ Angle()

double Vector::Angle ( const Vector b) const

Compute the angle between two vectors.

The two input vectors implitly define a frame

Parameters
bArgument vector forming the angle.

◆ Bilinear()

Vector Vector::Bilinear ( const Vector a00,
const Vector a10,
const Vector a11,
const Vector a01,
const double &  u,
const double &  v 
)
inlinestatic

Bi-linear interpolation between four vectors.

The values are given in trigonometric order.

Parameters
a00,a10,a11,a01Interpolated vectors.
u,vInterpolation coefficients.
See also
Math::Bilinear

◆ Coplanar() [1/2]

bool Vector::Coplanar ( const Vector a,
const Vector b,
const Vector c,
const double &  epsilon = 1.0e-6 
)
static

Check if three vectors are coplanar.

Simply compute the cross product of a and b, and the dot product with c. Compare the result with a given tolerance.

Parameters
a,b,cVectors.
epsilonTolerance parameter.

◆ Coplanar() [2/2]

bool Vector::Coplanar ( const Vector a,
const Vector b,
const Vector c,
const Vector d,
const double &  epsilon = 1.0e-6 
)
static

Check if four points are coplanar.

Parameters
a,b,c,dFour points.
epsilonTolerance parameter.

◆ Equal()

bool Vector::Equal ( const Vector a,
const Vector b,
const double &  epsilon = 0.0001 
)
static

Test if two vectors are almost equal.

This function computes the difference between the two argument vectors, then the norm infinity of this difference and check the result againt the epsilon threshold value. This is a convenience function which is the same as:

Vector a,b; // Two vectors
double e; // Epsilon value
bool e=NormInfinity(Abs(b-a))<e?true:false;
friend Vector Abs(const Vector &)
Computes the absolute value of a vector.
Definition: evector.h:512
friend double NormInfinity(const Vector &)
Compute the norm infinity of a vector.
Definition: evector.h:405

◆ Floor()

Vector Vector::Floor ( ) const
inline

Compute the numerator part of the coordinates.

See also
Math::Floor

◆ Fract()

Vector Vector::Fract ( ) const
inline

Compute the fractional part of the coordinates.

See also
Math::Fract

◆ FromString()

Vector Vector::FromString ( QString  n)
static

Creates a vector from a QString.

Parameters
nString.

◆ Inverse()

Vector Vector::Inverse ( ) const
inline

Inverse of a vector.

This function inverses the components of the vector. This is the same as:

Vector v=Vector(1.0/u[0],1.0/u[1],1.0/u[2]);

◆ Max()

double Vector::Max ( ) const
inline

Compute the maximum component of a vector.

Note that this function is not the same as NormInfinity which computes the maximum of the absolute values of components. The codes are equivalent:

Vector a(-1.0,-3.0,2.0);
double s=NormInfinity(a);
double t=Max(Abs(a));
double Max() const
Compute the maximum component of a vector.
Definition: evector.h:423
See also
NormInfinity

◆ MaxIndex()

int Vector::MaxIndex ( ) const
inline

Compute the index of the maximum component of a vector.

Vector a(-1.0,-3.0,2.0);
int i=MaxIndex(Abs(a)); // Should be 1
int MaxIndex() const
Compute the index of the maximum component of a vector.
Definition: evector.h:446

This function can be used to find the most stretched axis of a bounding box, for instance to cut the box in the middle of this stretched axis:

Box box;
int axis = box.Diagonal().MaxIndex();
An axis aligned box.
Definition: box.h:23
Vector Diagonal() const
Returns the diagonal of the box.
Definition: box.h:199
See also
Max

◆ MinIndex()

int Vector::MinIndex ( ) const
inline

Compute the index of the minimum component of a vector.

Vector a(-1.0,4.0,2.0);
int i = MinIndex(a); // Should be 0
int MinIndex() const
Compute the index of the minimum component of a vector.
Definition: evector.h:482
See also
MaxIndex

◆ Mod()

Vector Vector::Mod ( const Vector a,
const Vector b 
)
inlinestatic

Modulo of two Vectors.

Parameters
a,bArgument vectors.

◆ Octant()

int Vector::Octant ( const Vector p) const

Computes octant index of a vector with respect to the vector object.

See also
Box::Octant(const Vector&)
Parameters
pVector.

◆ operator*=()

Vector & Vector::operator*= ( const Vector u)
inline

Destructively scale a vector by another vector.

This is the same as Scale:

Vector u(2.0,-1.0,1.0);
u=u.Scaled(Vector(3.0,1.0,2.0)); // u*=Vector(3.0,1.0,2.0);

◆ Orthogonal()

Vector Vector::Orthogonal ( ) const

Returns a vector orthogonal to the argument vector.

The returned orthogonal vector is not computed randomly. First, we find the two coordinates of the argument vector with maximum absolute value. The orthogonal vector is defined by swapping those two coordinates and changing one sign, whereas the third coordinate is set to 0.

The returned orthogonal vector lies in the plane orthogonal to the first vector.

◆ Orthonormal()

void Vector::Orthonormal ( Vector x,
Vector y 
) const

Given a vector, creates two vectors xand y that form an orthogonal basis.

This algorithm pickes the minor axis in order to reduce numerical instability

Parameters
x,yReturned vectors such that (x,y,n) form an orthonormal basis (provided n is normalized).

◆ Polar()

Vector Vector::Polar ( const double &  t,
const double &  p 
)
inlinestatic

Creates a vector given polar coordinates.

Parameters
tTheta.
pPhi.

◆ RandomOrthonormal()

void Vector::RandomOrthonormal ( Vector i,
Vector j 
) const

Computes two random orthonormal vectors to the argument vector.

This function is expensive as it requires the computation of normalized vectors and the evaluation of a random number.

Parameters
i,jOrthonormal vectors.

◆ Scaled()

Vector Vector::Scaled ( const Vector a) const
inline

Scale a vector.

Parameters
aScaling vector.

◆ SetMinMax()

void Vector::SetMinMax ( const Vector x,
Vector a,
Vector b 
)
static

Update the minimum and maximum values given a vector.

Parameters
xInput vector.
a,bLower and upper vectors that will be updated.

◆ Slope()

double Vector::Slope ( const Vector a,
const Vector b 
)
inlinestatic

Compute the vertical slope between two vectors.

This is a convenience function.

Parameters
a,bArgument vectors.

◆ Solve()

Vector Vector::Solve ( const Vector a,
const Vector b,
const double &  va,
const double &  vb 
)
inlinestatic

Compute the point on a segment such that the linear function satisfies f(a)=va and f(b)=vb.

This function can be used as a first approximation for computing the intersection between a segment and an implicit surface.

See also
Linear::Solve(const double& a, const double& b, const Vector&, const Vector&);

◆ ToString()

QString Vector::ToString ( int  n = 6) const

Create a QString form a vector.

The implementation currently uses:

return QString("Vector(%1,%2,%3)").arg(c[0],0,'f',n).arg(c[1],0,'f',n).arg(c[2],0,'f',n);

Another possible implementation could have been:

return "Vector("+QString::number(c[0],n)+","+QString::number(c[1],n)+","+QString::number(c[2],n)+")";
Parameters
nNumber of digits.

◆ Trilinear()

Vector Vector::Trilinear ( const Vector a,
const Vector b,
const Vector c,
const Vector d,
const Vector e,
const Vector f,
const Vector g,
const Vector h,
const double &  u,
const double &  v,
const double &  w 
)
static

Trilinear interpolation between eight vectors.

See also
Math::Bilinear()
Parameters
a,b,c,d,e,f,g,hInterpolated values.
u,v,wInterpolation coefficients.

Friends And Related Function Documentation

◆ Abs

Vector Abs ( const Vector u)
friend

Computes the absolute value of a vector.

Parameters
uVector.

◆ Aligned

int Aligned ( const Vector u,
const Vector v 
)
friend

Check if two vectors are aligned.

Computes the cosine of the two vectors, and checks for unity.

Parameters
u,vVectors.

◆ Clamp

Vector Clamp ( const Vector x,
const Vector a,
const Vector b 
)
friend

Clamp a vector between two bounds.

Parameters
xInput vector
a,bVector bounds.

◆ Cosine

double Cosine ( const Vector u,
const Vector v 
)
friend

Returns the positive cosine of two vectors.

Basically computes the dot product of the normalized vectors.

Parameters
u,vVectors.

◆ Lerp

Vector Lerp ( const Vector a,
const Vector b,
const double &  t 
)
friend

Linear interpolation between two vectors.

Parameters
a,bInterpolated points.
tInterpolant.

◆ Norm

double Norm ( const Vector u)
friend

Compute the Euclidean norm of a vector.

This function involves a square root computation, it is in general more efficient to rely on the squared norm of a vector instead.

Parameters
uVector.
See also
SquaredNorm

◆ Normalize

void Normalize ( Vector u)
friend

Normalize a vector, computing the inverse of its norm and scaling the components.

This function does not check if the vector is null, which might resulting in errors.

◆ Normalized

Vector Normalized ( const Vector u)
friend

Return a normalized vector.

Compute the inverse of its norm and scale the components.

This function does not check if the vector is null.

Parameters
uVector.

◆ NormInfinity

double NormInfinity ( const Vector u)
friend

Compute the norm infinity of a vector.

See also
Max
Parameters
uVector.

◆ operator<<

std::ostream & operator<< ( std::ostream &  s,
const Vector u 
)
friend

Overloaded output-stream operator.

Parameters
uVector.
sStream.

◆ Sine

double Sine ( const Vector u,
const Vector v 
)
friend

Returns the positive sine of two vectors.

Computes the cross product of the vectors and normalizes the result.

Parameters
u,vVectors.

◆ SquaredNorm

double SquaredNorm ( const Vector u)
friend

Compute the squared Euclidean norm of a vector.

Parameters
uVector.
See also
Norm

◆ Swap [1/2]

void Swap ( Vector a,
Vector b 
)
friend

Swap two vectors.

Parameters
a,bVectors.

◆ Swap [2/2]

void Swap ( Vector *&  a,
Vector *&  b 
)
friend

Swap two pointers to (arrays) vectors.

Parameters
a,bVectors.