Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TVector3.ipp
Go to the documentation of this file.
1#pragma once
2
3#include "Math/TVector3.h"
4#include "Math/TQuaternion.h"
5#include "Math/math.h"
6
7#include <Common/assertion.h>
8
9#include <array>
10#include <cmath>
11#include <string>
12#include <cstdlib>
13#include <algorithm>
14
15namespace ph::math
16{
17
18template<typename T>
20 const TVector3& vA, const T wA,
21 const TVector3& vB, const T wB,
22 const TVector3& vC, const T wC)
23{
24 return vA * wA + vB * wB + vC * wC;
26
27template<typename T>
28inline TVector3<T> TVector3<T>::lerp(const TVector3& vA, const TVector3& vB, const T parametricT)
29{
30 const T oneMinusT = 1 - parametricT;
31 return TVector3(vA.x() * oneMinusT + vB.x() * parametricT,
32 vA.y() * oneMinusT + vB.y() * parametricT,
33 vA.z() * oneMinusT + vB.z() * parametricT);
34}
35
36template<typename T>
37inline TVector3<T>::TVector3(const T vx, const T vy, const T vz) :
38 Base(std::array<T, 3>{vx, vy, vz})
39{}
41template<typename T>
42template<typename U>
43inline TVector3<T>::TVector3(const TVector3<U>& other) :
45 static_cast<T>(other.x()),
46 static_cast<T>(other.y()),
47 static_cast<T>(other.z()))
48{}
50template<typename T>
51inline TVector3<T> TVector3<T>::rotate(const TQuaternion<T>& rotation) const
52{
53 const TQuaternion<T>& conjugatedRotation = rotation.conjugate();
54 const TQuaternion<T> result = rotation.mul(*this).mulLocal(conjugatedRotation);
55
56 return TVector3(result.x(), result.y(), result.z());
57}
58
59template<typename T>
60inline void TVector3<T>::rotate(const TQuaternion<T>& rotation,
61 TVector3* const out_result) const
63 PH_ASSERT(out_result);
64 PH_ASSERT(out_result != this);
66 const TQuaternion<T>& conjugatedRotation = rotation.conjugate();
67 const TQuaternion<T> result = rotation.mul(*this).mulLocal(conjugatedRotation);
69 out_result->x() = result.x();
70 out_result->y() = result.y();
71 out_result->z() = result.z();
74template<typename T>
75inline TVector3<T> TVector3<T>::cross(const TVector3& rhs) const
76{
77 return TVector3(y() * rhs.z() - z() * rhs.y(),
78 z() * rhs.x() - x() * rhs.z(),
79 x() * rhs.y() - y() * rhs.x());
80}
81
82template<typename T>
83inline void TVector3<T>::cross(const TVector3& rhs,
84 TVector3* const out_result) const
85{
86 PH_ASSERT(out_result);
87 PH_ASSERT(out_result != this);
88
89 out_result->x() = y() * rhs.z() - z() * rhs.y();
90 out_result->y() = z() * rhs.x() - x() * rhs.z();
91 out_result->z() = x() * rhs.y() - y() * rhs.x();
92}
93
94template<typename T>
95inline TVector3<T>& TVector3<T>::maddLocal(const T multiplier,
96 const TVector3& adder)
97{
98 x() = x() * multiplier + adder.x();
99 y() = y() * multiplier + adder.y();
100 z() = x() * multiplier + adder.z();
101
102 return *this;
103}
104
105template<typename T>
106inline TVector3<T> TVector3<T>::reflect(const TVector3& normal) const
107{
108 TVector3 result = normal.mul(2 * normal.dot(*this));
109 return this->sub(result);
110}
111
112template<typename T>
114{
115 const T factor = 2 * normal.dot(*this);
116
117 x() -= factor * normal.x();
118 y() -= factor * normal.y();
119 z() -= factor * normal.z();
120
121 return *this;
122}
123
124template<typename T>
125inline void TVector3<T>::sort(TVector3* const out_result) const
126{
127 PH_ASSERT(out_result);
128 PH_ASSERT(out_result != this);
129
130 // Returned (x, y, z) = (min, mid, max)
131
132 if(x() > y())
133 {
134 if(x() > z())
135 {
136 out_result->z() = x();
137
138 if(y() < z())
139 {
140 out_result->x() = y();
141 out_result->y() = z();
142 }
143 else
144 {
145 out_result->x() = z();
146 out_result->y() = y();
147 }
148 }
149 else
150 {
151 out_result->z() = z();
152 out_result->y() = x();
153 out_result->x() = y();
154 }
155 }
156 else
157 {
158 if(x() < z())
159 {
160 out_result->x() = x();
161
162 if(y() > z())
163 {
164 out_result->z() = y();
165 out_result->y() = z();
166 }
167 else
168 {
169 out_result->z() = z();
170 out_result->y() = y();
171 }
172 }
173 else
174 {
175 out_result->x() = z();
176 out_result->y() = x();
177 out_result->z() = y();
178 }
179 }
180}
181
182template<typename T>
183inline T& TVector3<T>::x()
184{
185 return m[0];
186}
187
188template<typename T>
189inline T& TVector3<T>::y()
190{
191 return m[1];
192}
193
194template<typename T>
195inline T& TVector3<T>::z()
196{
197 return m[2];
198}
199
200template<typename T>
201inline const T& TVector3<T>::x() const
202{
203 return m[0];
204}
205
206template<typename T>
207inline const T& TVector3<T>::y() const
208{
209 return m[1];
210}
211
212template<typename T>
213inline const T& TVector3<T>::z() const
214{
215 return m[2];
216}
217
218template<typename T>
219inline T& TVector3<T>::r()
220{
221 return m[0];
222}
223
224template<typename T>
225inline T& TVector3<T>::g()
226{
227 return m[1];
228}
229
230template<typename T>
231inline T& TVector3<T>::b()
232{
233 return m[2];
234}
235
236template<typename T>
237inline const T& TVector3<T>::r() const
238{
239 return m[0];
240}
241
242template<typename T>
243inline const T& TVector3<T>::g() const
244{
245 return m[1];
246}
247
248template<typename T>
249inline const T& TVector3<T>::b() const
250{
251 return m[2];
252}
253
254}// end namespace ph::math
Represents a quaternion.
Definition TQuaternion.h:17
TQuaternion conjugate() const
Definition TQuaternion.ipp:173
T & x()
Definition TQuaternion.ipp:95
TQuaternion & mulLocal(const TQuaternion &rhs)
Definition TQuaternion.ipp:204
T & z()
Definition TQuaternion.ipp:107
TQuaternion mul(const TVector3< T > &xyz) const
Quaternion multiplication (treating the input's w component as 0).
Definition TQuaternion.ipp:143
T & y()
Definition TQuaternion.ipp:101
Represents a 3-D vector.
Definition TVector3.h:17
T & y()
Definition TVector3.ipp:189
TVector3 & maddLocal(T multiplier, const TVector3 &adder)
Definition TVector3.ipp:95
T & z()
Definition TVector3.ipp:195
TVector3 & reflectLocal(const TVector3 &normal)
Definition TVector3.ipp:113
T & x()
Definition TVector3.ipp:183
void sort(TVector3 *out_result) const
Definition TVector3.ipp:125
TVector3 rotate(const TQuaternion< T > &rotation) const
Definition TVector3.ipp:51
TVector3(T vx, T vy, T vz)
Definition TVector3.ipp:37
static TVector3 weightedSum(const TVector3 &vA, T wA, const TVector3 &vB, T wB, const TVector3 &vC, T wC)
Definition TVector3.ipp:19
static TVector3 lerp(const TVector3 &vA, const TVector3 &vB, T parametricT)
Definition TVector3.ipp:28
T & r()
Definition TVector3.ipp:219
TVector3 reflect(const TVector3 &normal) const
Definition TVector3.ipp:106
T & b()
Definition TVector3.ipp:231
T & g()
Definition TVector3.ipp:225
TVector3 cross(const TVector3 &rhs) const
Definition TVector3.ipp:75
Derived mul(const Derived &rhs) const
Definition TArithmeticArrayBase.ipp:98
T dot(const Derived &rhs) const
Definition TVectorNBase.ipp:14
Miscellaneous math utilities.
Math functions and utilities.
Definition TransformInfo.h:10
Definition TAABB2D.h:96