pbpt
Loading...
Searching...
No Matches
homogeneous.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "point.hpp"
4#include "matrix.hpp"
5#include <concepts>
6#include <stdexcept>
7#include <type_traits>
8#include <iostream>
9
18
19namespace pbpt::math {
20
43template<typename T, int N>
45private:
49 Vector<T, N + 1> m_data{};
50
51public:
52
53 // --- 构造函数 (Constructors) ---
54
59 constexpr Homogeneous() noexcept { m_data[N] = 1; }
60
66 constexpr explicit Homogeneous(const Point<T, N>& p) noexcept {
67 for (int i = 0; i < N; ++i) m_data[i] = p[i];
68 m_data[N] = 1;
69 }
70
76 constexpr explicit Homogeneous(const Vector<T, N>& v) noexcept {
77 for (int i = 0; i < N; ++i) m_data[i] = v[i];
78 m_data[N] = 0;
79 }
80
87
88 template<std::convertible_to<T>... Vals>
89 constexpr explicit Homogeneous(Vals... vals) noexcept requires(sizeof...(Vals) == N + 1) {
90 int i = 0;
91 ((m_data[i++] = vals), ...);
92 }
93
100 constexpr explicit Homogeneous(const Vector<T, N + 1>& data) noexcept : m_data(data) {}
101
106 constexpr const T& w() const { return m_data[N]; }
107
112 constexpr T& w() { return m_data[N]; }
113
114 // 访问运算符
116 constexpr const T& operator[](int index) const { return m_data[index]; }
117
119 constexpr T& operator[](int index) { return m_data[index]; }
120
122 constexpr const T& at(int index) const { return m_data.at(index); }
123
124
125 // --- 状态检查 (State Checks) ---
126
133 constexpr bool is_point() const noexcept { return m_data[N] != 0; }
134
139 constexpr bool is_vector() const noexcept { return m_data[N] == 0; }
140
141 // --- 转换 (Conversions) ---
142
152 constexpr Point<T, N> to_point() const {
153 if (is_vector()) {
154 if (std::is_constant_evaluated()) throw "Compile-time error: Cannot convert a homogeneous vector (w=0) to a Point.";
155 else throw std::runtime_error("Cannot convert a homogeneous vector (w=0) to a Point.");
156 }
157 Vector<T, N> result_coords;
158 const T inv_w = 1.0 / m_data[N];
159 for (int i = 0; i < N; ++i) result_coords[i] = m_data[i] * inv_w;
160 return Point<T, N>(result_coords);
161 }
162
171 constexpr Vector<T, N> to_vector() const {
172 if (is_point()) {
173 if (std::is_constant_evaluated()) throw "Compile-time error: Cannot convert a homogeneous point (w!=0) to a Vec.";
174 else throw std::runtime_error("Logical error: Cannot convert a homogeneous point (w!=0) to a Vec.");
175 }
176 Vector<T, N> result_coords;
177 for (int i = 0; i < N; ++i) result_coords[i] = m_data[i];
178 return result_coords;
179 }
180
181 // --- 访问器 (Accessors) ---
182
187 constexpr const Vector<T, N + 1>& raw() const noexcept { return m_data; }
188
193 constexpr Vector<T, N + 1>& raw() noexcept { return m_data; }
194
195 // --- 流输出 (Stream Output) ---
196
205 friend std::ostream& operator<<(std::ostream& os, const Homogeneous& h) {
206 os << "HCoord" << N << (h.is_point() ? "[P] " : "[V] ") << h.raw();
207 return os;
208 }
209
219 return Homogeneous(mat * h.raw());
220 }
221};
222
223// --- 类型别名 (Type Aliases) ---
224
227/*** @brief A 2-dimensional homogeneous coordinate using the library's default `Float` type.*/
228using Homo2 = Homogeneous<Float, 2>;
229
230} // namespace homogeneous
A template class for an N-dimensional homogeneous coordinate.
Definition homogeneous.hpp:44
constexpr const Vector< T, N+1 > & raw() const noexcept
Provides read-only access to the underlying raw (N+1)-dimensional vector.
Definition homogeneous.hpp:187
constexpr Homogeneous(const Vector< T, N > &v) noexcept
Constructs a homogeneous coordinate from a Vec (vector).
Definition homogeneous.hpp:76
friend Homogeneous operator*(const Matrix< T, N+1, N+1 > &mat, const Homogeneous &h)
Multiplication operator for a matrix and a homogeneous coordinate.
Definition homogeneous.hpp:218
constexpr Vector< T, N > to_vector() const
Converts the homogeneous coordinate back to a Vec.
Definition homogeneous.hpp:171
constexpr bool is_vector() const noexcept
Checks if this homogeneous coordinate represents a vector.
Definition homogeneous.hpp:139
constexpr T & operator[](int index)
Returns the value of the homogeneous coordinate at the specified index.
Definition homogeneous.hpp:119
friend std::ostream & operator<<(std::ostream &os, const Homogeneous &h)
Stream insertion operator for printing the homogeneous coordinate.
Definition homogeneous.hpp:205
constexpr T & w()
Returns a reference to the w-component of the homogeneous coordinate.
Definition homogeneous.hpp:112
constexpr Homogeneous(const Point< T, N > &p) noexcept
Constructs a homogeneous coordinate from a Point.
Definition homogeneous.hpp:66
constexpr Vector< T, N+1 > & raw() noexcept
Provides mutable access to the underlying raw (N+1)-dimensional vector.
Definition homogeneous.hpp:193
constexpr bool is_point() const noexcept
Checks if this homogeneous coordinate represents a point.
Definition homogeneous.hpp:133
constexpr Homogeneous(Vals... vals) noexcept
Constructs a homogeneous coordinate from a list of values.
Definition homogeneous.hpp:89
constexpr const T & at(int index) const
Returns the value of the homogeneous coordinate at the specified index.
Definition homogeneous.hpp:122
constexpr const T & operator[](int index) const
Returns the value of the homogeneous coordinate at the specified index.
Definition homogeneous.hpp:116
constexpr Homogeneous() noexcept
Default constructor.
Definition homogeneous.hpp:59
constexpr Homogeneous(const Vector< T, N+1 > &data) noexcept
Explicitly constructs from a raw (N+1)-dimensional vector.
Definition homogeneous.hpp:100
constexpr const T & w() const
Returns the w-component of the homogeneous coordinate.
Definition homogeneous.hpp:106
constexpr Point< T, N > to_point() const
Converts the homogeneous coordinate back to a Point.
Definition homogeneous.hpp:152
A template class for RxC dimensional mathematical matrices.
Definition matrix.hpp:267
A template class for an N-dimensional point in space.
Definition point.hpp:37
A template class for N-dimensional mathematical vectors.
Definition vector.hpp:35
Defines a generic, RxC-dimensional, constexpr-friendly matrix class and its views.
The namespace for math library implementation.
Definition bounding_box.hpp:9
Homogeneous< Float, 3 > Homo3
A 3-dimensional homogeneous coordinate using the library's default Float type.
Definition homogeneous.hpp:226
Defines a generic, N-dimensional Point class and its geometric operations.