4D Vectors

Overview

A 4D vector in KSL is a 3D vector with a fourth component \(w\) appended, called the homogeneous coordinate:

\[\begin{split}\begin{Bmatrix} x \\ y \\ z \\ w \end{Bmatrix}\end{split}\]

Points and directions. In homogeneous coordinates a point \(\underline{p}\) is written \(\{\underline{p}, 1\}\) and a direction (a free vector such as a velocity or a force) is written \(\{\underline{d}, 0\}\). A rigid body transformation then becomes a single 4x4 matrix product (see SE(3)):

\[\begin{split}\begin{bmatrix} R & \underline{t} \\ 0 & 1 \end{bmatrix} \begin{Bmatrix} \underline{p} \\ 1 \end{Bmatrix} = \begin{Bmatrix} R \underline{p} + \underline{t} \\ 1 \end{Bmatrix}, \qquad \begin{bmatrix} R & \underline{t} \\ 0 & 1 \end{bmatrix} \begin{Bmatrix} \underline{d} \\ 0 \end{Bmatrix} = \begin{Bmatrix} R \underline{d} \\ 0 \end{Bmatrix}\end{split}\]

so the coordinates of a point are transformed with both the rotation and the translation, while those of a direction are transformed by the rotation alone, as they should be.

Camera projection. The homogeneous coordinate is what makes perspective projection possible with matrices. A pinhole camera maps a point with coordinates \(\{x, y, z\}\) in the camera frame to the image point

\[u = f \frac{x}{z} + c_u, \qquad v = f \frac{y}{z} + c_v\]

where \(f\) is the focal length in pixels and \((c_u, c_v)\) is the image center. The division by the depth \(z\) cannot be written as a matrix product on 3D vectors. In homogeneous coordinates it can: the product

\[\begin{split}\begin{Bmatrix} w u \\ w v \\ w \end{Bmatrix} = \begin{bmatrix} f & 0 & c_u \\ 0 & f & c_v \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} R & \underline{t} \end{bmatrix} \begin{Bmatrix} \underline{p} \\ 1 \end{Bmatrix}\end{split}\]

is linear, and the image point is recovered by dividing by the last component \(w\) (the perspective divide). Any nonzero multiple of a homogeneous vector represents the same point. Graphics interfaces such as OpenGL and Vulkan use the same idea with 4x4 projection matrices acting on 4D vectors. [7] covers camera models and projective geometry in depth.

In KSL. Rigid body transformations do not need the homogeneous coordinate: the ksl_SE3_t functions compute \(R \underline{p} + \underline{t}\) directly, without storing or multiplying the extra row. ksl_vec4_t is provided for data that has a \(w\) component, for example when exchanging data with graphics or vision code.

Storage. ksl_vec4_t holds four double values, 32 bytes (ksl_vec4f_t: 16 bytes). The components can be read as x, y, z, w, as at[0] to at[3], or the first three together as a ksl_vec3_t named r:

ksl_vec4_t p = ksl_vec4(1.0, 2.0, 3.0, 1.0);
double w = p.w;
ksl_vec3_t r = p.r;   /* {1, 2, 3} */

Double precision

ksl_vec4_t

double precision 4x1 vector representing a 3x1 vector with a homogeneous coordinate (w) appended.

ksl_vec4()

vec4 constructor

ksl_vec4_alloc()

initialize n ksl_vec4_t double precision vectors datastructures on the heap

ksl_vec4_l2norm()

compute the norm of a double precision vector

ksl_vec4_normalize()

Normalize a double precision vector.

ksl_vec4_normalized()

Normalize a double precision vector leaving original datastructure unmodified; results are returned in second datastructure.

ksl_vec4_scale()

Scale a double precision vector.

ksl_vec4_scaled()

Scale a double precision vector, returning results in second vector.

ksl_vec4_copy()

Copy a double precision ksl_vec4_t vi into ksl_vec4_t vo.

ksl_vec4_swap()

Swap double precision vectors vio1 and vio2.

ksl_vec4_inverted()

Copy inverse of double precision ksl_vec4_t vi into vo.

ksl_vec4_invert()

invert a double precision vector in place

union ksl_vec4_t
#include <types.h>

double precision 4x1 vector representing a 3x1 vector with a homogeneous coordinate (w) appended.

Typically used for nonlinear transformations such as camera projections.

Public Members

double at[4]
struct ksl_vec4_t

anonymous union allows accessing vector quantities at specified index

ksl_vec3_t r
double x
double y
double z
double w

homogeneous coordinate of 4d vector

struct ksl_vec4_t

anonymous union allows x, y, z, w vector components of 4d vector by descriptive name or 3d vector component

static inline ksl_vec4_t ksl_vec4(const double x, const double y, const double z, const double w)

vec4 constructor

inline ksl_vec4_t *ksl_vec4_alloc(int)

initialize n ksl_vec4_t double precision vectors datastructures on the heap

static inline double ksl_vec4_l2norm(const ksl_vec4_t *v)

compute the norm of a double precision vector

static inline void ksl_vec4_normalize(ksl_vec4_t *v)

Normalize a double precision vector.

Parameters:
  • v – [in/out] vector to normalize

static inline void ksl_vec4_normalized(const ksl_vec4_t *v, ksl_vec4_t *vo)

Normalize a double precision vector leaving original datastructure unmodified; results are returned in second datastructure.

Parameters:
  • v – [in] vector to normalize

  • vo – [out] unit vector in the direction of v

static inline void ksl_vec4_scale(ksl_vec4_t *v, const double k)

Scale a double precision vector.

Parameters:
  • v – [in,out] vector to scale in place

  • k – [in] scale factor

static inline void ksl_vec4_scaled(const ksl_vec4_t *v, const double k, ksl_vec4_t *vo)

Scale a double precision vector, returning results in second vector.

Parameters:
  • v – [in] vector to scale

  • k – [in] scale factor

  • vo – [out] scaled vector, k * v

static inline void ksl_vec4_copy(const ksl_vec4_t *vi, ksl_vec4_t *vo)

Copy a double precision ksl_vec4_t vi into ksl_vec4_t vo.

\(V_i \rightarrow V_o\)

Parameters:
  • vi – input vector \(V_i\)

  • vo – output vector \(V_o\)

static inline void ksl_vec4_swap(ksl_vec4_t *vio1, ksl_vec4_t *vio2)

Swap double precision vectors vio1 and vio2.

\(V_io1 \rightarrow V_io2\) \(V_io2 \rightarrow V_io1\)

Parameters:
  • vio1 – input/output vector \(V_io1\)

  • vio2 – input/output vector \(V_io2\)

static inline void ksl_vec4_inverted(const ksl_vec4_t *vi, ksl_vec4_t *vo)

Copy inverse of double precision ksl_vec4_t vi into vo.

\(-V_i \rightarrow V_o\)

Parameters:
  • vi – input vector \(V_i\)

  • vo – output vector \(V_o\)

static inline void ksl_vec4_invert(ksl_vec4_t *vi)

invert a double precision vector in place

\(-V_i \rightarrow V_i\)

Parameters:
  • vi – [in/out] input vector \(V_i\)

Single precision

ksl_vec4f_t

single precision 4x1 vector representing a 3x1 vector with a homogeneous coordinate (w) appended.

ksl_vec4f()

vec4f constructor

ksl_vec4f_alloc()

initialize n ksl_vec4f_t single precision vectors datastructures on the heap

ksl_vec4f_l2norm()

compute the norm of a single precision vector

ksl_vec4f_normalize()

Normalize a single precision vector.

ksl_vec4f_normalized()

Normalize a single precision vector leaving original datastructure unmodified; results are returned in second datastructure.

ksl_vec4f_scale()

Scale a single precision vector.

ksl_vec4f_scaled()

Scale a single precision vector, returning results in second vector.

ksl_vec4f_copy()

Copy a single precision ksl_vec4f_t vi into ksl_vec4f_t vo.

ksl_vec4f_swap()

Swap single precision vectors vio1 and vio2.

ksl_vec4f_inverted()

Copy inverse of single precision ksl_vec4f_t vi into vo.

ksl_vec4f_invert()

invert a single precision vector in place

union ksl_vec4f_t
#include <types.h>

single precision 4x1 vector representing a 3x1 vector with a homogeneous coordinate (w) appended.

Typically used for nonlinear transformations such as camera projections.

Public Members

float at[4]
struct ksl_vec4f_t

anonymous union allows accessing vector quantities at specified index

ksl_vec3f_t r
float x
float y
float z
float w

homogeneous coordinate of 4d vector

struct ksl_vec4f_t

anonymous union allows x, y, z, w vector components of 4d vector by descriptive name or 3d vector component

static inline ksl_vec4f_t ksl_vec4f(const float x, const float y, const float z, const float w)

vec4f constructor

inline ksl_vec4f_t *ksl_vec4f_alloc(int)

initialize n ksl_vec4f_t single precision vectors datastructures on the heap

static inline float ksl_vec4f_l2norm(const ksl_vec4f_t *v)

compute the norm of a single precision vector

static inline void ksl_vec4f_normalize(ksl_vec4f_t *v)

Normalize a single precision vector.

Parameters:
  • v – [in/out] vector to normalize

static inline void ksl_vec4f_normalized(const ksl_vec4f_t *v, ksl_vec4f_t *vo)

Normalize a single precision vector leaving original datastructure unmodified; results are returned in second datastructure.

Parameters:
  • v – [in] vector to normalize

  • vo – [out] unit vector in the direction of v

static inline void ksl_vec4f_scale(ksl_vec4f_t *v, const float k)

Scale a single precision vector.

Parameters:
  • v – [in,out] vector to scale in place

  • k – [in] scale factor

static inline void ksl_vec4f_scaled(const ksl_vec4f_t *v, const float k, ksl_vec4f_t *vo)

Scale a single precision vector, returning results in second vector.

Parameters:
  • v – [in] vector to scale

  • k – [in] scale factor

  • vo – [out] scaled vector, k * v

static inline void ksl_vec4f_copy(const ksl_vec4f_t *vi, ksl_vec4f_t *vo)

Copy a single precision ksl_vec4f_t vi into ksl_vec4f_t vo.

\(V_i \rightarrow V_o\)

Parameters:
  • vi – input vector \(V_i\)

  • vo – output vector \(V_o\)

static inline void ksl_vec4f_swap(ksl_vec4f_t *vio1, ksl_vec4f_t *vio2)

Swap single precision vectors vio1 and vio2.

\(V_io1 \rightarrow V_io2\) \(V_io2 \rightarrow V_io1\)

Parameters:
  • vio1 – input/output vector \(V_io1\)

  • vio2 – input/output vector \(V_io2\)

static inline void ksl_vec4f_inverted(const ksl_vec4f_t *vi, ksl_vec4f_t *vo)

Copy inverse of single precision ksl_vec4f_t vi into vo.

\(-V_i \rightarrow V_o\)

Parameters:
  • vi – [in] input vector \(V_i\)

  • vo – [out] output vector \(V_o\)

static inline void ksl_vec4f_invert(ksl_vec4f_t *vi)

invert a single precision vector in place

\(-V_i \rightarrow V_i\)

Parameters:
  • vi – [in/out] input vector \(V_i\)

Integer

ksl_vec4i_t

A 4 element integer vector.

ksl_vec4i()

vec4i constructor

ksl_vec4i_alloc()

initialize n ksl_vec4i_t vectors of integers on the heap

union ksl_vec4i_t
#include <types.h>

A 4 element integer vector.

Public Members

int at[4]
struct ksl_vec4i_t

anonymous union allows accessing vector quantities at specified index

ksl_vec3i_t r
int x
int y
int z
int w

homogeneous coordinate of 4d vector

struct ksl_vec4i_t

anonymous union allows x, y, z, w vector components of 4d vector by descriptive name or 3d vector component

static inline ksl_vec4i_t ksl_vec4i(const int x, const int y, const int z, const int w)

vec4i constructor

inline ksl_vec4i_t *ksl_vec4i_alloc(int)

initialize n ksl_vec4i_t vectors of integers on the heap