pbpt
Loading...
Searching...
No Matches
pbpt::math::Homogeneous< T, N > Class Template Reference

A template class for an N-dimensional homogeneous coordinate. More...

#include <homogeneous.hpp>

Public Member Functions

constexpr Homogeneous () noexcept
 Default constructor.
constexpr Homogeneous (const Point< T, N > &p) noexcept
 Constructs a homogeneous coordinate from a Point.
constexpr Homogeneous (const Vector< T, N > &v) noexcept
 Constructs a homogeneous coordinate from a Vec (vector).
template<std::convertible_to< T >... Vals>
requires (sizeof...(Vals) == N + 1)
constexpr Homogeneous (Vals... vals) noexcept
 Constructs a homogeneous coordinate from a list of values.
constexpr Homogeneous (const Vector< T, N+1 > &data) noexcept
 Explicitly constructs from a raw (N+1)-dimensional vector.
constexpr const T & w () const
 Returns the w-component of the homogeneous coordinate.
constexpr T & w ()
 Returns a reference to the w-component of the homogeneous coordinate.
constexpr const T & operator[] (int index) const
 Returns the value of the homogeneous coordinate at the specified index.
constexpr T & operator[] (int index)
 Returns the value of the homogeneous coordinate at the specified index.
constexpr const T & at (int index) const
 Returns the value of the homogeneous coordinate at the specified index.
constexpr bool is_point () const noexcept
 Checks if this homogeneous coordinate represents a point.
constexpr bool is_vector () const noexcept
 Checks if this homogeneous coordinate represents a vector.
constexpr Point< T, N > to_point () const
 Converts the homogeneous coordinate back to a Point.
constexpr Vector< T, N > to_vector () const
 Converts the homogeneous coordinate back to a Vec.
constexpr const Vector< T, N+1 > & raw () const noexcept
 Provides read-only access to the underlying raw (N+1)-dimensional vector.
constexpr Vector< T, N+1 > & raw () noexcept
 Provides mutable access to the underlying raw (N+1)-dimensional vector.

Friends

std::ostream & operator<< (std::ostream &os, const Homogeneous &h)
 Stream insertion operator for printing the homogeneous coordinate.
Homogeneous operator* (const Matrix< T, N+1, N+1 > &mat, const Homogeneous &h)
 Multiplication operator for a matrix and a homogeneous coordinate.

Detailed Description

template<typename T, int N>
class pbpt::math::Homogeneous< T, N >

A template class for an N-dimensional homogeneous coordinate.

This class unifies the mathematical treatment of points and vectors for use in projective geometry, particularly for 3D transformations. It internally stores an (N+1)-dimensional vector.

The distinction between a point and a vector is encoded in the last component, w:

  • A Point (x, y, z) is represented in homogeneous coordinates as (x, y, z, 1).
  • A Vector (vx, vy, vz) is represented as (vx, vy, vz, 0).

This class provides a type-safe way to create these coordinates from Point and Vec objects, and to convert them back, including performing the necessary perspective divide for points.

Template Parameters
TThe underlying floating-point type of the coordinate's components.
NThe original number of dimensions (e.g., 3 for 3D space). The internal storage will have N+1 dimensions.
See also
Point
Vec
Matrix

Constructor & Destructor Documentation

◆ Homogeneous() [1/5]

template<typename T, int N>
pbpt::math::Homogeneous< T, N >::Homogeneous ( )
inlineconstexprnoexcept

Default constructor.

Initializes a homogeneous coordinate representing the origin POINT (0, ..., 0, 1).

◆ Homogeneous() [2/5]

template<typename T, int N>
pbpt::math::Homogeneous< T, N >::Homogeneous ( const Point< T, N > & p)
inlineexplicitconstexprnoexcept

Constructs a homogeneous coordinate from a Point.

Copies the point's coordinates and sets the w-component to 1.

Parameters
pThe point to convert.

◆ Homogeneous() [3/5]

template<typename T, int N>
pbpt::math::Homogeneous< T, N >::Homogeneous ( const Vector< T, N > & v)
inlineexplicitconstexprnoexcept

Constructs a homogeneous coordinate from a Vec (vector).

Copies the vector's components and sets the w-component to 0.

Parameters
vThe vector to convert.

◆ Homogeneous() [4/5]

template<typename T, int N>
template<std::convertible_to< T >... Vals>
requires (sizeof...(Vals) == N + 1)
pbpt::math::Homogeneous< T, N >::Homogeneous ( Vals... vals)
inlineexplicitconstexprnoexcept

Constructs a homogeneous coordinate from a list of values.

The number of values must match the number of dimensions (N + 1).

Template Parameters
ValsThe types of the values, must be convertible to T.
Parameters
valsThe values to initialize the homogeneous coordinate.

◆ Homogeneous() [5/5]

template<typename T, int N>
pbpt::math::Homogeneous< T, N >::Homogeneous ( const Vector< T, N+1 > & data)
inlineexplicitconstexprnoexcept

Explicitly constructs from a raw (N+1)-dimensional vector.

This is an advanced constructor, primarily used internally after a matrix transformation has produced a new raw homogeneous coordinate vector.

Parameters
dataThe raw Vec<T, N + 1> of homogeneous coordinates.

Member Function Documentation

◆ is_point()

template<typename T, int N>
bool pbpt::math::Homogeneous< T, N >::is_point ( ) const
inlineconstexprnoexcept

Checks if this homogeneous coordinate represents a point.

Returns
true if the w-component is not zero, false otherwise.
Note
For floating-point types, a check against a small epsilon might be more robust in some runtime applications, but w != 0 is standard.

◆ is_vector()

template<typename T, int N>
bool pbpt::math::Homogeneous< T, N >::is_vector ( ) const
inlineconstexprnoexcept

Checks if this homogeneous coordinate represents a vector.

Returns
true if the w-component is exactly zero, false otherwise.

◆ raw() [1/2]

template<typename T, int N>
const Vector< T, N+1 > & pbpt::math::Homogeneous< T, N >::raw ( ) const
inlineconstexprnoexcept

Provides read-only access to the underlying raw (N+1)-dimensional vector.

Returns
A const reference to the internal Vec<T, N + 1>.

◆ raw() [2/2]

template<typename T, int N>
Vector< T, N+1 > & pbpt::math::Homogeneous< T, N >::raw ( )
inlineconstexprnoexcept

Provides mutable access to the underlying raw (N+1)-dimensional vector.

Returns
A mutable reference to the internal Vec<T, N + 1>.

◆ to_point()

template<typename T, int N>
Point< T, N > pbpt::math::Homogeneous< T, N >::to_point ( ) const
inlineconstexpr

Converts the homogeneous coordinate back to a Point.

This method performs the crucial "perspective divide" operation by dividing the x, y, z components by the w component.

Returns
The resulting Point<T, N> in Cartesian space.
Exceptions
std::runtime_errorThrows at runtime if this coordinate represents a vector (w=0), as this conversion is mathematically undefined.
Note
In a constexpr context, an invalid conversion will result in a compile-time error.

◆ to_vector()

template<typename T, int N>
Vector< T, N > pbpt::math::Homogeneous< T, N >::to_vector ( ) const
inlineconstexpr

Converts the homogeneous coordinate back to a Vec.

This method returns the first N components, assuming w is 0.

Returns
The resulting Vec<T, N> in Cartesian space.
Exceptions
std::runtime_errorThrows at runtime if this coordinate represents a point (w!=0), as this usually indicates a logical error in a transformation pipeline.
Note
In a constexpr context, an invalid conversion will result in a compile-time error.

◆ w() [1/2]

template<typename T, int N>
T & pbpt::math::Homogeneous< T, N >::w ( )
inlineconstexpr

Returns a reference to the w-component of the homogeneous coordinate.

Returns
A reference to the w-component, which can be used to modify it.

◆ w() [2/2]

template<typename T, int N>
const T & pbpt::math::Homogeneous< T, N >::w ( ) const
inlineconstexpr

Returns the w-component of the homogeneous coordinate.

Returns
The w-component, which is 1 for points and 0 for vectors.

◆ operator*

template<typename T, int N>
Homogeneous operator* ( const Matrix< T, N+1, N+1 > & mat,
const Homogeneous< T, N > & h )
friend

Multiplication operator for a matrix and a homogeneous coordinate.

This operator performs matrix-vector multiplication, where the matrix is of size (N+1)x(N+1) and the homogeneous coordinate is of size (N+1).

Parameters
matThe matrix to multiply.
hThe homogeneous coordinate to multiply.
Returns
The result of the matrix-vector multiplication.

◆ operator<<

template<typename T, int N>
std::ostream & operator<< ( std::ostream & os,
const Homogeneous< T, N > & h )
friend

Stream insertion operator for printing the homogeneous coordinate.

Prints a [P] or [V] tag to distinguish points from vectors, followed by the raw underlying vector data.

Parameters
osThe output stream.
hThe Homo object to print.
Returns
A reference to the output stream.

The documentation for this class was generated from the following file: