In many assignments, you will need to use vectors and matrices to compute various values, e.g., to rotate a point using homogeneous coordinates. Luckily, the CGL library used by assignments supports 2D/3D/4D vectors and 3x3/4x4 matrices, as well as many common operations on them.
This primer aims to help get you started on using vectors and matrices provided by CGL. However, this primer is not an exhaustive guide. To find all operations supported by CGL, you should carefully read through the following header and source files (vector2D.h/cpp
, vector3D.h/cpp
, vector4D.h/cpp
, matrix3x3.h/cpp
, and matrix4x4.h/cpp
).
While the following code example uses only 3D vectors and 3x3 matrices, the operations used naturally extend to 2D/4D vectors and 4x4 matrices.
// Initialize zero vector
Vector3D a;
// Initialize vector with given x, y, and z value
Vector3D b(1, 2, 4);
// .x, .y, and .z access and modify respective entry
// 0-based index [i] also access and modify respective entry
a.y = 1;
b[2] = 3;
cout << "(" << a.x << "," << a.y << "," << a.z << ")\n"; // should print (0,1,0)
cout << "(" << b[0] << "," << b[1] << "," << b[2] << ")\n"; // should print (1,2,3)
// Common arithmetics work as expected
// See header and source files for all arithmetic operations supported
Vector3D c = -a + 2*b - b/10;
cout << c << "\n"; // should print (1.9,2.8,5.7)
// Compute the unit vector of any vector
// Compute the norm of a vector
Vector3D u = c.unit();
cout << c.norm() << "," << u.norm() << "\n"; // should print 6.62873,1
// Compute dot product and cross product
double dp = dot(b, c);
Vector3D cr = cross(b, c);
// Initialize identity matrix
Matrix3x3 A;
// Initialize matrix with given values (first three are on the first row of matrix, etc)
Matrix3x3 B(1, 2, 3, 4, 5, 6, 7, 8, 9);
// 0-based index (i, j) access and modify respective entry
B(2, 2) = 10;
// Should print
// [ 1 0 0 ]
// [ 0 1 0 ]
// [ 0 0 1 ]
cout << A << "\n";
// Should print
// [ 1 2 3 ]
// [ 4 5 6 ]
// [ 7 8 10 ]
cout << B << "\n";
// Compute determinant and Frobenius norm
cout << B.det() << "," << B.norm() << "\n";
// Compute matrix transpose and inverse
Matrix3x3 BInvT = B.inv().T();
// Matrix-vector and matrix-matrix multiplication
Vector3D Ab = A * b;
Matrix3x3 AB = A * B;