Ray Tracer
2020
|
Basic class for vectors. More...
#include <Vector.h>
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... | |
Vector & | operator= (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... | |
Vector & | operator+= (const Vector &vec) |
Vector addition-assignment operator. More... | |
Vector & | operator-= (const Vector &vec) |
Vector subtraction-assignment operator. More... | |
Vector & | operator*= (double s) |
Vector-scalar multiplication-assignment operator. More... | |
Vector & | operator/= (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... | |
Matrix & | operator= (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... | |
Matrix & | operator+= (const Matrix &mat) |
Matrix addition-assignment operator. More... | |
Matrix & | operator-= (const Matrix &mat) |
Matrix subtraction-assignment operator. More... | |
Matrix & | operator*= (double s) |
Matrix-scalar multiplication-assignment operator. More... | |
Matrix & | operator/= (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... | |
Related Functions inherited from Matrix | |
Matrix | operator+ (const Matrix &lhs, const Matrix &rhs) |
Matrix addition operator. More... | |
Matrix | operator- (const Matrix &lhs, const Matrix &rhs) |
Matrix subtraction operator. More... | |
Matrix | operator* (double s, const Matrix &mat) |
scalar-Matrix multiplication operator. More... | |
Matrix | operator* (const Matrix &mat, double s) |
Matrix-scalar multiplication operator. More... | |
Matrix | operator/ (const Matrix &mat, double s) |
Matrix-scalar division operator. More... | |
std::ostream & | operator<< (std::ostream &outputStream, const Matrix &mat) |
Stream insertion operator. More... | |
std::istream & | operator>> (std::istream &inputStream, Matrix &mat) |
Stream extraction 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... | |
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.
Vector::Vector | ( | size_t | size = 1 | ) |
Vector::Vector | ( | const Vector & | vec | ) |
Vector::Vector | ( | const Matrix & | mat | ) |
|
virtual |
double Vector::dot | ( | const Vector & | vec | ) | const |
double Vector::norm | ( | ) | const |
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.
ix | The element of the Vector to access. |
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.
ix | The element of the Vector to access. |
const
reference to the requested element of the Vector. Vector & Vector::operator*= | ( | double | s | ) |
Vector-scalar multiplication-assignment operator.
This scales a Vector in place.
s | The scalar multiplier to apply to this . |
this
, to allow chaining of assignment. Vector Vector::operator- | ( | ) | const |
Vector & Vector::operator/= | ( | double | s | ) |
Vector-scalar multiplication-assignment operator.
This divides a Vector in place.
s | The scalar multiplier to divide this by. |
this
, to allow chaining of assignment. 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.
this
. 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.