Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TArithmeticArrayBase.h
Go to the documentation of this file.
1#pragma once
2
3#include "Math/math_fwd.h"
4#include "Utility/utility.h"
5#include "Utility/TSpan.h"
6
7#include <Common/primitive_type.h>
8
9#include <cstddef>
10#include <array>
11#include <type_traits>
12#include <string>
13#include <vector>
14
15namespace ph::math
16{
17
18template<typename Derived, typename T, std::size_t N>
21private:
23
24public:
25 using Elements = std::array<T, N>;
26
27 explicit TArithmeticArrayBase(T value);
28 explicit TArithmeticArrayBase(Elements values);
29
30// Hide special members as this class is not intended to be used polymorphically.
31// It is derived class's choice to expose them (by defining them in public) or not.
32protected:
34
35protected:
36 inline static constexpr auto NUM_ELEMENTS = N;
37
40 constexpr std::size_t size() const noexcept;
41
42 Derived add(const Derived& rhs) const;
43 Derived add(T rhs) const;
44 Derived& addLocal(const Derived& rhs);
45 Derived& addLocal(T rhs);
46
47 Derived sub(const Derived& rhs) const;
48 Derived sub(T rhs) const;
49 Derived& subLocal(const Derived& rhs);
50 Derived& subLocal(T rhs);
52 Derived mul(const Derived& rhs) const;
53 Derived mul(T rhs) const;
54 Derived& mulLocal(const Derived& rhs);
55 Derived& mulLocal(T rhs);
56
57 Derived div(const Derived& rhs) const;
58 Derived div(T rhs) const;
59 Derived& divLocal(const Derived& rhs);
60 Derived& divLocal(T rhs);
61
62 template<typename U>
63 Derived pow(U exponent) const;
64
65 template<typename U>
66 Derived& powLocal(U exponent);
68 Derived pow(const Derived& exponent) const;
69 Derived& powLocal(const Derived& exponent);
74 template<typename U>
75 Derived exp(U exponent) const;
76
77 template<typename U>
78 Derived& expLocal(U exponent);
80
84 Derived exp(const Derived& exponent) const;
85 Derived& expLocal(const Derived& exponent);
87
88 Derived sqrt() const;
89 Derived& sqrtLocal();
90
91 // TODO: supply clamp methods for NaN-safe and NaN-propagative versions
92
97 Derived clamp(T lowerBound, T upperBound) const;
98 Derived& clampLocal(T lowerBound, T upperBound);
99 Derived clamp(const Derived& lowerBound, const Derived& upperBound) const;
100 Derived& clampLocal(const Derived& lowerBound, const Derived& upperBound);
102
108 Derived safeClamp(T lowerBound, T upperBound) const;
109 Derived& safeClampLocal(T lowerBound, T upperBound);
110 Derived safeClamp(const Derived& lowerBound, const Derived& upperBound) const;
111 Derived& safeClampLocal(const Derived& lowerBound, const Derived& upperBound);
113
114 Derived abs() const;
115 Derived& absLocal();
116
117 Derived rcp() const;
118 Derived& rcpLocal();
119
124 Derived complement() const;
125 Derived& complementLocal();
127
132 Derived negate() const
133 requires std::is_signed_v<T>;
134
135 Derived& negateLocal()
136 requires std::is_signed_v<T>;
138
139 T sum() const;
140 T avg() const;
141 T product() const;
142
146 T min() const;
147
151 Derived min(const Derived& other) const;
152
156 std::size_t minIndex() const;
157
158 T max() const;
159 Derived max(const Derived& other) const;
160 std::size_t maxIndex() const;
161
162 Derived ceil() const;
163 Derived floor() const;
164
165 template<typename U>
166 Derived lerp(const Derived& rhs, U factor) const;
167
168 bool isZero() const;
169 bool isNonNegative() const;
170 bool isFinite() const;
171
172 Derived& set(T value);
173 Derived& set(std::size_t index, T value);
174 Derived& set(const std::array<T, N>& values);
175
176 T& operator [] (std::size_t index);
177 const T& operator [] (std::size_t index) const;
178
179 bool isEqual(const Derived& other) const;
180 bool isNear(const Derived& other, T margin) const;
181 bool operator == (const Derived& other) const;
182 bool operator != (const Derived& other) const;
183
184 Derived operator + (const Derived& rhs) const;
185 Derived operator + (T rhs) const;
186 Derived operator - (const Derived& rhs) const;
187 Derived operator - (T rhs) const;
188 Derived operator * (const Derived& rhs) const;
189 Derived operator * (T rhs) const;
190 Derived operator / (const Derived& rhs) const;
191 Derived operator / (T rhs) const;
192
193 Derived& operator += (const Derived& rhs);
194 Derived& operator += (T rhs);
195 Derived& operator -= (const Derived& rhs);
196 Derived& operator -= (T rhs);
197 Derived& operator *= (const Derived& rhs);
198 Derived& operator *= (T rhs);
199 Derived& operator /= (const Derived& rhs);
200 Derived& operator /= (T rhs);
201
202 Derived operator - () const
203 requires std::is_signed_v<T>;
204
205 auto begin() noexcept -> typename std::array<T, N>::iterator;
206 auto begin() const noexcept -> typename std::array<T, N>::const_iterator;
207 auto end() noexcept -> typename std::array<T, N>::iterator;
208 auto end() const noexcept -> typename std::array<T, N>::const_iterator;
209
210 std::string toString() const;
211 std::vector<T> toVector() const;
212 std::array<T, N> toArray() const;
213 TSpan<T, N> toSpan();
214 TSpanView<T, N> toView() const;
215
216protected:
217 std::array<T, N> m;
218};
219
220}// end namespace ph::math
221
222#include "Math/General/TArithmeticArrayBase.ipp"
Definition TArithmeticArrayBase.h:20
auto begin() noexcept -> typename std::array< T, N >::iterator
Definition TArithmeticArrayBase.ipp:797
Derived exp(U exponent) const
Sets the array to .
bool isZero() const
Definition TArithmeticArrayBase.ipp:549
T sum() const
Definition TArithmeticArrayBase.ipp:336
Derived mul(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:98
Derived pow(U exponent) const
std::array< T, N > m
Definition TArithmeticArrayBase.h:217
Derived sqrt() const
Definition TArithmeticArrayBase.ipp:246
std::vector< T > toVector() const
Definition TArithmeticArrayBase.ipp:842
T avg() const
Definition TArithmeticArrayBase.ipp:342
Derived complement() const
Complements the array's elements. Effectively performing 1 - (*this)[i] for each element.
Definition TArithmeticArrayBase.ipp:502
Derived floor() const
Definition TArithmeticArrayBase.ipp:447
std::array< T, N > toArray() const
Definition TArithmeticArrayBase.ipp:855
T product() const
Definition TArithmeticArrayBase.ipp:358
Derived & sqrtLocal()
Definition TArithmeticArrayBase.ipp:253
Derived & safeClampLocal(T lowerBound, T upperBound)
Definition TArithmeticArrayBase.ipp:307
Derived & expLocal(U exponent)
Derived safeClamp(T lowerBound, T upperBound) const
Clamps current array's elements to specific range. If a floating-point value is non-finite (e....
Definition TArithmeticArrayBase.ipp:300
TSpanView< T, N > toView() const
Definition TArithmeticArrayBase.ipp:869
Derived & divLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:148
Derived & set(T value)
Definition TArithmeticArrayBase.ipp:604
auto end() noexcept -> typename std::array< T, N >::iterator
Definition TArithmeticArrayBase.ipp:811
Derived & absLocal()
Definition TArithmeticArrayBase.ipp:473
Derived clamp(T lowerBound, T upperBound) const
Clamps current array's elements to specific range. None of value, lowerBound and upperBound can be Na...
Definition TArithmeticArrayBase.ipp:264
Derived & addLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:40
Derived lerp(const Derived &rhs, U factor) const
Derived sub(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:62
Derived div(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:134
std::string toString() const
Definition TArithmeticArrayBase.ipp:825
Derived & negateLocal()
Definition TArithmeticArrayBase.ipp:528
Derived & rcpLocal()
Definition TArithmeticArrayBase.ipp:491
T min() const
Definition TArithmeticArrayBase.ipp:364
Derived abs() const
Definition TArithmeticArrayBase.ipp:466
bool isFinite() const
Definition TArithmeticArrayBase.ipp:585
std::size_t maxIndex() const
Definition TArithmeticArrayBase.ipp:414
Derived negate() const
Applies a negative sign to the array's elements. These methods is only defined for signed element typ...
Definition TArithmeticArrayBase.ipp:520
Derived & mulLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:112
TSpan< T, N > toSpan()
Definition TArithmeticArrayBase.ipp:862
bool isNear(const Derived &other, T margin) const
Definition TArithmeticArrayBase.ipp:652
std::size_t minIndex() const
Definition TArithmeticArrayBase.ipp:382
TArithmeticArrayBase(T value)
Definition TArithmeticArrayBase.ipp:15
T max() const
Definition TArithmeticArrayBase.ipp:396
bool isNonNegative() const
Definition TArithmeticArrayBase.ipp:565
static constexpr auto NUM_ELEMENTS
Definition TArithmeticArrayBase.h:36
Derived & clampLocal(T lowerBound, T upperBound)
Definition TArithmeticArrayBase.ipp:271
Derived add(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:26
Derived & subLocal(const Derived &rhs)
Definition TArithmeticArrayBase.ipp:76
constexpr std::size_t size() const noexcept
Number of elements of the array.
Definition TArithmeticArrayBase.ipp:598
bool isEqual(const Derived &other) const
Definition TArithmeticArrayBase.ipp:646
Derived & powLocal(U exponent)
std::array< T, N > Elements
Definition TArithmeticArrayBase.h:25
Derived ceil() const
Definition TArithmeticArrayBase.ipp:428
Derived & complementLocal()
Definition TArithmeticArrayBase.ipp:509
Derived rcp() const
Definition TArithmeticArrayBase.ipp:484
Definition TVectorNBase.h:14
Math functions and utilities.
Definition TransformInfo.h:10
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19
std::span< T, EXTENT > TSpan
A contiguous sequence of objects of type T. Effectively the same as std::span.
Definition TSpan.h:12
Definition TAABB2D.h:96
#define PH_DEFINE_INLINE_RULE_OF_5_MEMBERS(ClassType)
Helper to define defaulted rule of 5 special class members (inlined).
Definition utility.h:64