Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TMatrixNBase.ipp
Go to the documentation of this file.
1#pragma once
2
4
5#include <Common/assertion.h>
6
7#include <utility>
8
9namespace ph::math
10{
11
12template<typename Derived, typename T, std::size_t N>
14-> Derived&
15{
16 set(static_cast<T>(0));
17
18 for(std::size_t ni = 0; ni < N; ++ni)
19 {
20 m[ni][ni] = static_cast<T>(1);
21 }
22
23 return static_cast<Derived&>(*this);
24}
25
26template<typename Derived, typename T, std::size_t N>
28-> Derived&
29{
30 set(static_cast<T>(0));
31
32 for(std::size_t ni = 0; ni < N; ++ni)
33 {
34 m[ni][ni] = scaleFactor[ni];
35 }
36
37 return static_cast<Derived&>(*this);
40template<typename Derived, typename T, std::size_t N>
41inline auto TMatrixNBase<Derived, T, N>::mul(const Derived& rhsMatrix) const
42-> Derived
44 Derived result(static_cast<const Derived&>(*this));
45 mul(rhsMatrix, &result);
46 return result;
47}
48
49template<typename Derived, typename T, std::size_t N>
50inline auto TMatrixNBase<Derived, T, N>::mulLocal(const Derived& rhsMatrix)
51-> Derived&
52{
53 Derived result(static_cast<const Derived&>(*this));
54 mul(rhsMatrix, &result);
55 Derived::operator = (result);
56
57 return static_cast<Derived&>(*this);
58}
59
60template<typename Derived, typename T, std::size_t N>
61inline auto TMatrixNBase<Derived, T, N>::mulTransposed(const Derived& rhsMatrix) const
62-> Derived
63{
64 Derived result(static_cast<const Derived&>(*this));
65 mulTransposed(rhsMatrix, &result);
66 return result;
67}
68
69template<typename Derived, typename T, std::size_t N>
70inline auto TMatrixNBase<Derived, T, N>::mulTransposedLocal(const Derived& rhsMatrix)
71-> Derived&
72{
73 Derived result(static_cast<const Derived&>(*this));
74 mulTransposed(rhsMatrix, &result);
75 Derived::operator = (result);
76
77 return static_cast<Derived&>(*this);
78}
79
80template<typename Derived, typename T, std::size_t N>
81inline void TMatrixNBase<Derived, T, N>::mul(const Derived& rhsMatrix, Derived* const out_result) const
82{
83 PH_ASSERT(out_result);
84 PH_ASSERT(out_result != this);
85
86 Base::template multiplyMatrix<N>(rhsMatrix.Self::m, &(out_result->Self::m));
87}
88
89template<typename Derived, typename T, std::size_t N>
90inline void TMatrixNBase<Derived, T, N>::mulTransposed(const Derived& rhsMatrix, Derived* const out_result) const
91{
92 PH_ASSERT(out_result);
93
94 Base::template multiplyTransposedMatrix<N>(rhsMatrix.Self::m, &(out_result->Self::m));
95}
96
97template<typename Derived, typename T, std::size_t N>
99-> Derived
100{
101 Derived result(static_cast<const Derived&>(*this));
102 result.Self::transposeLocal();
103 return result;
104}
105
106template<typename Derived, typename T, std::size_t N>
108-> Derived&
109{
110 // We did not use the general transpose from base here--as square matrices
111 // can be efficiently transposed locally.
112
113 static_assert(N >= 1);
114
115 // Skip N = 1 as it is already a transpose by itself
116 for(std::size_t ri = 0; ri < N - 1; ++ri)
117 {
118 // Skip ci = ri as diagonal elements do not change during a transpose
119 for(std::size_t ci = ri + 1; ci < N; ++ci)
120 {
121 // Enable ADL
122 using std::swap;
123
124 swap(m[ri][ci], m[ci][ri]);
125 }
126 }
127
128 return static_cast<Derived&>(*this);
129}
130
131}// end namespace ph::math
Derived mul(const Derived &rhsMatrix) const
Definition TMatrixNBase.ipp:41
Derived & setIdentity()
Sets the matrix to be an identity matrix.
Definition TMatrixNBase.ipp:13
Derived & mulTransposedLocal(const Derived &rhsMatrix)
Definition TMatrixNBase.ipp:70
Derived & transposeLocal()
Definition TMatrixNBase.ipp:107
Derived transpose() const
Definition TMatrixNBase.ipp:98
Derived & mulLocal(const Derived &rhsMatrix)
Definition TMatrixNBase.ipp:50
Derived mulTransposed(const Derived &rhsMatrix) const
Definition TMatrixNBase.ipp:61
Derived & setScale(const TRawColVector< T, N > &scaleFactor)
Definition TMatrixNBase.ipp:27
Math functions and utilities.
Definition TransformInfo.h:10
std::array< T, M > TRawColVector
Definition TMatrixMxNBase.h:20