Ray Tracer  2020
Public Member Functions | Related Functions | List of all members
Vector Class Reference

Basic class for vectors. More...

#include <Vector.h>

Inheritance diagram for Vector:
Matrix Direction Normal Point

Public Member Functions

 Vector (size_t size=1)
 Vector constructor. More...
 
 Vector (const Vector &vec)
 Vector copy constructor. More...
 
 Vector (const Matrix &mat)
 Vector from Matrix constructor. More...
 
virtual ~Vector ()
 Vector destructor. More...
 
Vectoroperator= (const Vector &vec)
 Vector assignment operator. More...
 
double & operator() (size_t ix)
 Vector element access. More...
 
const double & operator() (size_t ix) const
 Vector element access (const version). More...
 
Vector operator- () const
 Unary minus. More...
 
Vectoroperator+= (const Vector &vec)
 Vector addition-assignment operator. More...
 
Vectoroperator-= (const Vector &vec)
 Vector subtraction-assignment operator. More...
 
Vectoroperator*= (double s)
 Vector-scalar multiplication-assignment operator. More...
 
Vectoroperator/= (double s)
 Vector-scalar multiplication-assignment operator. More...
 
double dot (const Vector &vec) const
 Vector dot product. More...
 
Vector cross (const Vector &vec) const
 Vector cross product. More...
 
double norm () const
 Vector norm. More...
 
double squaredNorm () const
 SquaredVector norm. More...
 
- Public Member Functions inherited from Matrix
 Matrix (size_t size=1)
 Square Matrix constructor. More...
 
 Matrix (size_t rows, size_t cols)
 General Matrix constructor. More...
 
 Matrix (const Matrix &mat)
 Matrix copy constructor. More...
 
virtual ~Matrix ()
 Matrix destructor. More...
 
Matrixoperator= (const Matrix &mat)
 Matrix assignment operator. More...
 
double & operator() (size_t row, size_t col)
 Matrix element access. More...
 
const double & operator() (size_t row, size_t col) const
 Matrix element access (const version). More...
 
size_t numRows () const
 Number of rows in a Matrix. More...
 
size_t numCols () const
 Number of columns in a Matrix. More...
 
size_t numElements () const
 Number of elements in a Matrix. More...
 
Matrix operator- () const
 Unary minus. More...
 
Matrixoperator+= (const Matrix &mat)
 Matrix addition-assignment operator. More...
 
Matrixoperator-= (const Matrix &mat)
 Matrix subtraction-assignment operator. More...
 
Matrixoperator*= (double s)
 Matrix-scalar multiplication-assignment operator. More...
 
Matrixoperator/= (double s)
 Matrix-scalar multiplication-assignment operator. More...
 
Matrix transpose () const
 Matrix transpose. More...
 

Related Functions

(Note that these are not member functions.)

Vector operator+ (const Vector &lhs, const Vector &rhs)
 Vector addition operator. More...
 
Vector operator- (const Vector &lhs, const Vector &rhs)
 Vector subtraction operator. More...
 
Vector operator* (double s, const Vector &vec)
 scalar-Vector multiplication operator. More...
 
Vector operator* (const Vector &vec, double s)
 scalar-Vector multiplication operator. More...
 
Vector operator/ (const Vector &vec, double s)
 Vector-scalar division operator. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Matrix
static Matrix identity (size_t rows, size_t cols)
 Factory method for Identity Matrices. More...
 
static Matrix zero (size_t rows, size_t cols)
 Factory method for Zero Matrices. More...
 
- Protected Attributes inherited from Matrix
size_t rows_
 Number of rows in the Matrix. More...
 
size_t cols_
 Number of columns in the Matrix. More...
 
std::vector< double > data_
 Storage for Matrix data elements. More...
 

Detailed Description

Basic class for vectors.

A Vector is a Matrix with exactly one column, and in many cases can be dealt with as such. However, having a separate Vector class means that we can implement Vector-specific behaviour like dot and cross products. Since all Vectors are Matrices, Vector is a subclass of Matrix.

Many Matrix operations (such as + and *) are overloaded in cases when a Vector is gauranteed to be the result. Thus Matrix*Vector is overloaded (it is always a Vector), but Vector*Matrix is not - we can just use Matrix*Matrix for that. Most of these just call the Matrix equivalents, so this provides some convenience at the cost of a layer of redirecton.

Note that a Vector as a mathematical object, is quite distinct from a std:vector, which is essentially an array.

Constructor & Destructor Documentation

◆ Vector() [1/3]

Vector::Vector ( size_t  size = 1)

Vector constructor.

This constructor creates a Vector of the specified size. If no size is provided, it creates a 1-Vector, and so also provides a default constructor.

Parameters
sizeThe size of the resulting Vector.

◆ Vector() [2/3]

Vector::Vector ( const Vector vec)

Vector copy constructor.

This constructor creates a new Vector which is a copy of an existing one. The new Vector has independent storage etc.

Parameters
vecThe Vector to copy.

◆ Vector() [3/3]

Vector::Vector ( const Matrix mat)

Vector from Matrix constructor.

This constructor creates a new Vector which is a copy of an existing Matrix. The Matrix must have exactly one column. This form allows us to turn a Matrix into a Vector when needed.

Parameters
matThe Matrix to copy.

◆ ~Vector()

Vector::~Vector ( )
virtual

Vector destructor.

As with Matrices, there's not a lot to do here. The destructor is virtual in case we want to make subclasses of Vector (like Point or Normal)

Member Function Documentation

◆ cross()

Vector Vector::cross ( const Vector vec) const

Vector cross product.

This computes the cross product of two vectors, u.cross(v). The Vectors must both be 3-vectors.

Parameters
vecThe Vector to take the cross product with.
Returns
The cross product of this and vec.

◆ dot()

double Vector::dot ( const Vector vec) const

Vector dot product.

This computes the dot product of two vectors, u.dot(v). The two Vectors must have the same size.

Parameters
vecThe Vector to take the dot product with.
Returns
The dot product of vec and this

◆ norm()

double Vector::norm ( ) const

Vector norm.

This computes the norm or length of a Vector. Technically, this is the L2- or Euclidean norm. Other norms exist, but this is the one that is concerns us most of the time.

Returns
The norm (length) of of this.

◆ operator()() [1/2]

double & Vector::operator() ( size_t  ix)

Vector element access.

This method provides access to the elements of a Vector using the syntax v(i). This notation is equivalent to v.operator()(i). Since a reference is returned, this can be used for assignment, eg: v(1)=3.1415; The value of ix must less than the size of the Vector.

Parameters
ixThe element of the Vector to access.
Returns
A reference to the requested element of the Vector.

◆ operator()() [2/2]

const double & Vector::operator() ( size_t  ix) const

Vector element access (const version).

This method provides access to the elements of a Vector using the syntax v(i). Since a const reference is returned, this cannot be used for assignment, but it can be applied to const Vector. The value of ix must less than the size of the Vector.

Parameters
ixThe element of the Vector to access.
Returns
A const reference to the requested element of the Vector.

◆ operator*=()

Vector & Vector::operator*= ( double  s)

Vector-scalar multiplication-assignment operator.

This scales a Vector in place.

Parameters
sThe scalar multiplier to apply to this.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator+=()

Vector & Vector::operator+= ( const Vector vec)

Vector addition-assignment operator.

This adds a Vector to an existing one in place. The two Vectors must have the same size.

Parameters
vecThe Vector to add to this.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator-()

Vector Vector::operator- ( ) const

Unary minus.

This provides the ability to negate a Vector. -v is equivalent to v.operator-()

Returns
A negated version of the Vector.

◆ operator-=()

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

Vector subtraction-assignment operator.

This subtracts a Vector from an existing one in place. The two Vectors must have the same size.

Parameters
vecThe Vector to subtract from this.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator/=()

Vector & Vector::operator/= ( double  s)

Vector-scalar multiplication-assignment operator.

This divides a Vector in place.

Parameters
sThe scalar multiplier to divide this by.
Returns
A reference to the updated this, to allow chaining of assignment.

◆ operator=()

Vector & Vector::operator= ( const Vector vec)

Vector assignment operator.

This allows expressions like u = v; where u and v are Vectors. It also allows chained assignment, like u = v = w;

Parameters
vecThe Vector on the right hand side of the assignment.
Returns
a reference to this, to allow chaining of assignment.

◆ squaredNorm()

double Vector::squaredNorm ( ) const

SquaredVector norm.

This computes the squared norm or length of a Vector. Technically, this is the squared L2- or Euclidean norm. Other norms exist, but this is the one that is concerns us most of the time.

Computing the squared norm (rather than the norm itself) is often sufficient for a task (eg: to see which of two Vectors is shorter), and avoids a sqrt operation.

Returns
The norm (length) of of this.

Friends And Related Function Documentation

◆ operator*() [1/2]

Vector operator* ( const Vector vec,
double  s 
)
related

scalar-Vector multiplication operator.

This creates the product of a Vector and a scalar.

Parameters
vecThe Vector to be scaled.
sThe scalar value to multiply the Matrix by.
Returns
The Vector formed by vec*s.

◆ operator*() [2/2]

Vector operator* ( double  s,
const Vector vec 
)
related

scalar-Vector multiplication operator.

This creates the product of a scalar and a Vector.

Parameters
sThe scalar value to multiply the Matrix by.
vecThe Vector to be scaled.
Returns
The Vector formed by s*vec.

◆ operator+()

Vector operator+ ( const Vector lhs,
const Vector rhs 
)
related

Vector addition operator.

This performs addition of two Vectors. The two Vectors must have the same size

Parameters
lhsThe Vector on the left hand side of the +.
rhsThe Vector on the right hand side of the +.
Returns
The Vector formed by lhs + rhs.

◆ operator-()

Vector operator- ( const Vector lhs,
const Vector rhs 
)
related

Vector subtraction operator.

This performs subtraction of two Vectors. The two Vectors must have the same size

Parameters
lhsThe Vector on the left hand side of the -.
rhsThe Vector on the right hand side of the -.
Returns
Vector formed by lhs - rhs.

◆ operator/()

Vector operator/ ( const Vector vec,
double  s 
)
related

Vector-scalar division operator.

This creates the result of a Vector divided by a scalar. Division by a scalar, s, is essentially the same as multiplcation (or scaling) by 1/s. Division by numbers close to 0 will result in NaN and Inf values and wildly unstable results as normal.

Parameters
vecThe Vector to be scaled.
sThe scalar value to divide the Matrix by.
Returns
The Vector formed by vec/s.

The documentation for this class was generated from the following files: