pbpt
Loading...
Searching...
No Matches
point.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "math/function.hpp"
4#include "vector.hpp" // Presumed to define Vec, Vec2, Vec3, Vec4, Float
5
15
16namespace pbpt::math {
36template<typename T, int N>
37class Point {
38private:
42 Vector<T, N> m_coords{};
43
44public:
45 // --- 静态工厂函数 (Static Factory Functions) ---
46
52 constexpr static Point filled(T value) noexcept { return Point(Vector<T, N>::filled(value)); }
53
58 constexpr static Point zeros() noexcept { return Point(Vector<T, N>::zeros()); }
59
64 constexpr static Point ones() noexcept { return Point(Vector<T, N>::ones()); }
65
66 // --- 构造函数 (Constructors) ---
67
71 constexpr Point() noexcept : m_coords(Vector<T, N>::zeros()) {}
72
77 constexpr explicit Point(const Vector<T, N>& vec) noexcept : m_coords(vec) {}
78
86 template<std::convertible_to<T>... Args>
87 requires(sizeof...(Args) == N)
88 constexpr explicit Point(Args&&... args) noexcept
89 : m_coords(std::forward<Args>(args)...) {}
90
91 // --- 访问器 (Accessors) ---
92
94 constexpr T& x() noexcept requires(N > 0) { return m_coords.x(); }
96 constexpr T& y() noexcept requires(N > 1) { return m_coords.y(); }
98 constexpr T& z() noexcept requires(N > 2) { return m_coords.z(); }
100 constexpr T& w() noexcept requires(N > 3) { return m_coords.w(); }
101
103 constexpr T x() const noexcept requires(N > 0) { return m_coords.x(); }
105 constexpr T y() const noexcept requires(N > 1) { return m_coords.y(); }
107 constexpr T z() const noexcept requires(N > 2) { return m_coords.z(); }
109 constexpr T w() const noexcept requires(N > 3) { return m_coords.w(); }
110
112 constexpr int dims() const noexcept { return N; }
113
114 // --- 下标访问 (Subscript Access) ---
115
117 constexpr const T& operator[](int index) const { return m_coords[index]; }
119 constexpr T& operator[](int index) { return m_coords[index]; }
121 constexpr const T& at(int index) const { return m_coords.at(index); }
122
123 // --- 显式转换 (Explicit Conversion) ---
124
131 constexpr Vector<T, N> to_vector() const noexcept { return m_coords; }
132
133 // --- 复合赋值运算符 (Compound Assignment Operators) ---
134
140 constexpr Point& operator+=(const Vector<T, N>& rhs) noexcept {
141 m_coords += rhs;
142 return *this;
143 }
144
145 /*** @brief less than */
146 constexpr bool operator<(const Point& rhs) const {
147 for (int i = 0; i < N; i ++) {
148 if (!is_less(m_coords[i], rhs.m_coords[i]))
149 return false;
150 }
151 return true;
152 }
153
154 /*** @brief less than or equal*/
155 constexpr bool operator<=(const Point& rhs) const {
156 for (int i = 0; i < N; i ++) {
157 if (is_greater(m_coords[i], rhs.m_coords[i]))
158 return false;
159 }
160 return true;
161 }
162
166 constexpr bool operator>(const Point& rhs) const {
167 for (int i = 0; i < N; i ++) {
168 if (!is_greater(m_coords[i], rhs.m_coords[i]))
169 return false;
170 }
171 return true;
172 }
173
177 constexpr bool operator>=(const Point& rhs) const {
178 for (int i = 0; i < N; i ++) {
179 if (is_less(m_coords[i], rhs.m_coords[i]))
180 return false;
181 }
182 return true;
183 }
184
192 constexpr bool operator==(const Point& rhs) const {
193 for (int i = 0; i < N; i ++) {
194 if (!is_equal(m_coords[i], rhs.m_coords[i]))
195 return false;
196 }
197 return true;
198 }
199
205 constexpr Point& operator-=(const Vector<T, N>& rhs) noexcept {
206 m_coords -= rhs;
207 return *this;
208 }
209
210 // --- 流输出 (Stream Output) ---
211
218 friend std::ostream& operator<<(std::ostream& os, const Point& point) {
219 os << "Point" << N << "(";
220 for (int i = 0; i < N; ++i) {
221 os << point[i] << (i == N - 1 ? "" : ", ");
222 }
223 os << ")";
224 return os;
225 }
226
232 constexpr Vector<T, N> operator-(const Point& rhs) const noexcept {
233 return this->to_vector() - rhs.to_vector();
234 }
235
241 constexpr Point operator+(const Vector<T, N>& rhs) const noexcept {
242 auto result = *this;
243 result += rhs;
244 return result;
245 }
246
252 constexpr Point operator-(const Vector<T, N>& rhs) const noexcept {
253 auto result = *this;
254 result -= rhs;
255 return result;
256 }
257
263 constexpr Point mid(const Point<T, N>& rhs) const noexcept {
264 // Conceptually: start at lhs, and move halfway towards rhs.
265 // (lhs + (rhs - lhs) * 0.5) which simplifies to (lhs + rhs) * 0.5
266 return Point<T, N>((this->to_vector() + rhs.to_vector()) * 0.5);
267 }
268};
269
270// --- 类型别名 (Type Aliases) ---
277
278} // namespace math
A template class for an N-dimensional point in space.
Definition point.hpp:37
constexpr T w() const noexcept
Const access to the fourth coordinate (w-axis).
Definition point.hpp:109
constexpr Point mid(const Point< T, N > &rhs) const noexcept
Calculates the midpoint between two points.
Definition point.hpp:263
constexpr bool operator==(const Point &rhs) const
equal
Definition point.hpp:192
constexpr Point & operator+=(const Vector< T, N > &rhs) noexcept
Translates the point by adding a vector.
Definition point.hpp:140
constexpr Point operator+(const Vector< T, N > &rhs) const noexcept
Rule 2: Translates a point by a vector, resulting in a new point (Point + Vector = Point).
Definition point.hpp:241
constexpr bool operator>=(const Point &rhs) const
greater than or equal
Definition point.hpp:177
constexpr T x() const noexcept
Const access to the first coordinate (x-axis).
Definition point.hpp:103
constexpr T & x() noexcept
Accesses the first coordinate (x-axis).
Definition point.hpp:94
constexpr T y() const noexcept
Const access to the second coordinate (y-axis).
Definition point.hpp:105
constexpr T z() const noexcept
Const access to the third coordinate (z-axis).
Definition point.hpp:107
constexpr Point() noexcept
Default constructor. Initializes the point at the origin.
Definition point.hpp:71
constexpr Vector< T, N > operator-(const Point &rhs) const noexcept
Rule 1: Calculates the displacement vector between two points (Point - Point = Vector).
Definition point.hpp:232
constexpr T & w() noexcept
Accesses the fourth coordinate (w-axis).
Definition point.hpp:100
static constexpr Point zeros() noexcept
Creates a point at the origin (0, 0, ...).
Definition point.hpp:58
static constexpr Point filled(T value) noexcept
Creates a point filled with a specified value.
Definition point.hpp:52
friend std::ostream & operator<<(std::ostream &os, const Point &point)
Stream insertion operator for printing the point's coordinates.
Definition point.hpp:218
constexpr Point operator-(const Vector< T, N > &rhs) const noexcept
Rule 3: Translates a point by the negative of a vector (Point - Vector = Point).
Definition point.hpp:252
constexpr Vector< T, N > to_vector() const noexcept
Explicitly converts the point to its underlying coordinate vector.
Definition point.hpp:131
constexpr T & z() noexcept
Accesses the third coordinate (z-axis).
Definition point.hpp:98
constexpr Point(const Vector< T, N > &vec) noexcept
Constructs a point from a coordinate vector.
Definition point.hpp:77
constexpr const T & operator[](int index) const
Provides const access to the point's coordinates by index.
Definition point.hpp:117
constexpr T & operator[](int index)
Provides mutable access to the point's coordinates by index.
Definition point.hpp:119
static constexpr Point ones() noexcept
Creates a point with all coordinates set to one.
Definition point.hpp:64
constexpr int dims() const noexcept
Returns the number of dimensions of the point.
Definition point.hpp:112
constexpr Point(Args &&... args) noexcept
Constructs a point from a list of individual coordinate values.
Definition point.hpp:88
constexpr bool operator>(const Point &rhs) const
greater than
Definition point.hpp:166
constexpr const T & at(int index) const
Provides safe const access to the point's coordinates by index.
Definition point.hpp:121
constexpr Point & operator-=(const Vector< T, N > &rhs) noexcept
Translates the point by subtracting a vector.
Definition point.hpp:205
constexpr T & y() noexcept
Accesses the second coordinate (y-axis).
Definition point.hpp:96
A template class for N-dimensional mathematical vectors.
Definition vector.hpp:35
static constexpr Vector zeros() noexcept
Creates a vector with all components set to zero.
Definition vector.hpp:56
static constexpr Vector ones() noexcept
Creates a vector with all components set to one.
Definition vector.hpp:62
static constexpr Vector filled(T value) noexcept
Creates a vector with all components set to a single scalar value.
Definition vector.hpp:46
Provides basic, constexpr-aware mathematical functions.
The namespace for math library implementation.
Definition bounding_box.hpp:9
Point< Float, 2 > Pt2
A 2-dimensional point of type Float.
Definition point.hpp:272
constexpr bool is_greater(T a, T b)
Compares if a is greater than b.
Definition function.hpp:301
Point< Float, 3 > Pt3
A 3-dimensional point of type Float.
Definition point.hpp:274
constexpr bool is_equal(T a, T b)
Compares two floating-point values for equality.
Definition function.hpp:287
constexpr bool is_less(T a, T b)
Compares if a is less than b.
Definition function.hpp:329
Point< Float, 4 > Pt4
A 4-dimensional point of type Float.
Definition point.hpp:276
Defines a generic, N-dimensional, constexpr-friendly vector class.