4D Vectors¶
Overview¶
A 4D vector in KSL is a 3D vector with a fourth component \(w\) appended, called the homogeneous coordinate:
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)):
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
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
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¶
double precision 4x1 vector representing a 3x1 vector with a homogeneous coordinate (w) appended. |
|
vec4 constructor |
|
initialize n ksl_vec4_t double precision vectors datastructures on the heap |
|
compute the norm of a double precision vector |
|
Normalize a double precision vector. |
|
Normalize a double precision vector leaving original datastructure unmodified; results are returned in second datastructure. |
|
Scale a double precision vector. |
|
Scale a double precision vector, returning results in second vector. |
|
Copy a double precision ksl_vec4_t vi into ksl_vec4_t vo. |
|
Swap double precision vectors vio1 and vio2. |
|
Copy inverse of double precision ksl_vec4_t vi into vo. |
|
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
-
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
-
double at[4]¶
-
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¶
single precision 4x1 vector representing a 3x1 vector with a homogeneous coordinate (w) appended. |
|
vec4f constructor |
|
initialize n ksl_vec4f_t single precision vectors datastructures on the heap |
|
compute the norm of a single precision vector |
|
Normalize a single precision vector. |
|
Normalize a single precision vector leaving original datastructure unmodified; results are returned in second datastructure. |
|
Scale a single precision vector. |
|
Scale a single precision vector, returning results in second vector. |
|
Copy a single precision ksl_vec4f_t vi into ksl_vec4f_t vo. |
|
Swap single precision vectors vio1 and vio2. |
|
Copy inverse of single precision ksl_vec4f_t vi into vo. |
|
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
-
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
-
float at[4]¶
-
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¶
A 4 element integer vector. |
|
vec4i constructor |
|
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
-
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
-
int at[4]¶
-
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