Week 3: Linear Algebra Tools
Reading Recap
The Immersive Linear Algebra website has good introductory material on linear algebra topics related to computer graphics
-
Chapter 1: Introduction
-
Chapter 2: Vectors (Sections 2.1-2.5)
-
Chapter 6: The Matrix (6.1-6.4, 6.8)
-
Chapter 3: Dot Product (Sections 3.1-3.3, Ex 3.6., 3.7)
The Learn OpenGL website has a good summaries using glm and C++ syntax in an OpenGL context. Similar features are available in TWGL.
The term vector gets overused in computer science, but for graphics, it will be important to distinguish at times between something like a generic type vec3
and the geometric concept of vector. Theorem 2.1 in Chapter 2: Vectors covers the basic rules for vector, scalar operations.
With points \(P,Q\) and vector \(\vec{v}\), we allow the following operations
-
\(Q=P+\vec{v}\) (point displacement)
-
\(Q-P=\vec{v}\) (a variant of above, \(\vec{v}\) points from \(P\) to \(Q\))
-
\(1 \cdot P = P\)
-
\(0 \cdot P = \vec{0}\)
We will typically use the vec[234]
GLSL types to store points, colors, and vectors. Even though GLSL will always support vec3
addition, it does not make sense geometrically to add two points.
Change of Basis/Frame
In 3D, a set of three linearly independent vectors, \(\vec{v_1},\vec{v_2}, \vec{v_3}\) form a basis allowing us to express any other vector \(\vec{w}\) as a linear combination of the basis, e.g., \(\vec{w}=a_1\vec{v_1}+a_2\vec{v_2}+a_3\vec{v_3}\). We can write this in matrix form as:
When the underlying basis is understood, we can express \(\vec{w}\) by just its three coordinates \(\mathbf{a}^T = (a_1, a_2, a_3)\). We will use \(\mathbf{a}^T\) to express coordinates in row form, and the transpose,
to express coordinates in column form.
By adding an origin point \(Q_0\) to our basis, we can define a frame in which we can assign coordinates to both vectors and points. In 3D, we do this by assigning a fourth coordinate that is set to 0 for vectors and 1 for points.
Our vector \(\vec{w}\) is now:
While a point \(P=b_1\vec{v_1}+b_2\vec{v_2}+b_3\vec{v_3}+Q_0\) is defined as:
which again can be simplified to \(P=(b_1,b_2,b_3,1)\) if the frame is understood.
A frame is not unique. Any set of linearly independent vectors and and origin point can form a frame and coordinate system for measuring points and vectors. In graphics, this is a feature, not a bug, and it will often be convenient to work in one frame rather than another at various points. But since a frame is just a way of assigning coordinates to the underlying concept (a point location in space, or a vector magnitude and direction), there must be a way to change between two frames.
Let \((\vec{v_1},\vec{v_2}, \vec{v_3}, Q_0)\) be one frame, and let \((\vec{u_1},\vec{u_2}, \vec{u_3}, P_0)\) be another. Since each frame forms a basis, we can write each vector in the other frame as linear combination of the basis vectors, e.g.,
\(\vec{u_1}=m_{11}\vec{v_1}+m_{12}\vec{v_2}+m_{13}\vec{v_3}\)
\(\vec{u_2}=m_{21}\vec{v_1}+m_{22}\vec{v_2}+m_{23}\vec{v_3}\)
\(\vec{u_3}=m_{31}\vec{v_1}+m_{32}\vec{v_2}+m_{33}\vec{v_3}\)
Similarly we can express the origin of one frame in the coordinates of the other.
\(P_0=m_{41}\vec{v_1}+m_{42}\vec{v_2}+m_{43}\vec{v_3}+Q_0\)
In matrix form we can write this as
If a point/vector is expressed with coordinates \(\mathbf{a}^T=(a_1, a_2, a_3, a_4)\) in the \(\mathbf{v}\) frame and coordinates \(\mathbf{b}^T=(b_1, b_2, b_3, b_4)\) in \(\mathbf{u}\) frame, we can convert back and forth using the matrix \(\mathbf{M}^T\) and its inverse as follows:
\(\mathbf{a}^T\mathbf{v} =\mathbf{b}^T\mathbf{u} = \mathbf{b}^T\mathbf{M}\mathbf{v} \implies \mathbf{a}^T= \mathbf{b}^T\mathbf{M}\)
To convert to \(\mathbf{v}\) coordinates from \(\mathbf{u}\) coordinates , use
\(\mathbf{a} = \mathbf{M}^T\mathbf{b}\).
To convert the other way, use
\(\mathbf{b} = \mathbf{M^{-1}}^T\mathbf{a}\)
Exercise
Suppose you have coordinates in the blue frame below and you want to convert to coordinates in the red frame. Try rewriting the basis vectors and origin of the blue frame vectors/origin in terms of the red frame to compute the matrix \(\mathbf{M}\) and its transpose.
Now go the other way and compute the matrix to convert from red coordinates to blue coordinates. You can either invert the matrix, or swap the roles of \(\mathbf{u}\) and \(\mathbf{v}\) and repeat the first exercise.