Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TMatrix2.ipp
Go to the documentation of this file.
1#pragma once
2
3#include "Math/TMatrix2.h"
4#include "Math/TVector2.h"
5#include "Math/TVector3.h"
6
7#include <Common/assertion.h>
8
9#include <limits>
10#include <string>
11#include <cmath>
12
13namespace ph::math
14{
15
16template<typename T>
21
22template<typename T>
23inline TMatrix2<T>::TMatrix2(const T value) :
24 TMatrix2(value, value, value, value)
25{}
26
27template<typename T>
28inline TMatrix2<T>::TMatrix2(const T m00, const T m01, const T m10, const T m11) :
29 m{m00, m01,
30 m10, m11}
31{}
32
33template<typename T>
34inline TMatrix2<T>::TMatrix2(const TVector2<T>& m00m01, const TVector2<T>& m10m11) :
35 m{m00m01.x(), m00m01.y(),
36 m10m11.x(), m10m11.y()}
37{}
38
39template<typename T>
40template<typename U>
41inline TMatrix2<T>::TMatrix2(const TMatrix2<U>& other) :
42 m{static_cast<T>(other.m[0][0]), static_cast<T>(other.m[0][1]),
43 static_cast<T>(other.m[1][0]), static_cast<T>(other.m[1][1])}
44{}
45
46template<typename T>
48{
49 m[0][0] = static_cast<T>(1); m[0][1] = static_cast<T>(0);
50 m[1][0] = static_cast<T>(0); m[1][1] = static_cast<T>(1);
51
52 return *this;
53}
54
55template<typename T>
56inline TMatrix2<T> TMatrix2<T>::mul(const TMatrix2& rhs) const
57{
58 TMatrix2 result;
59 this->mul(rhs, &result);
60 return result;
61}
62
63template<typename T>
64inline TMatrix2<T> TMatrix2<T>::mul(const T value) const
65{
66 return TMatrix2(*this).mulLocal(value);
67}
68
69template<typename T>
70inline void TMatrix2<T>::mul(const TMatrix2& rhs, TMatrix2* const out_result) const
71{
72 out_result->m[0][0] = m[0][0] * rhs.m[0][0] + m[0][1] * rhs.m[1][0];
73 out_result->m[0][1] = m[0][0] * rhs.m[0][1] + m[0][1] * rhs.m[1][1];
74 out_result->m[1][0] = m[1][0] * rhs.m[0][0] + m[1][1] * rhs.m[1][0];
75 out_result->m[1][1] = m[1][0] * rhs.m[0][1] + m[1][1] * rhs.m[1][1];
76}
77
78template<typename T>
79inline TMatrix2<T>& TMatrix2<T>::mulLocal(const T value)
80{
81 m[0][0] *= value;
82 m[0][1] *= value;
83 m[1][0] *= value;
84 m[1][1] *= value;
85
86 return *this;
87}
88
89template<typename T>
91{
92 return TMatrix2( m[1][1], -m[0][1],
93 -m[1][0], m[0][0]).mulLocal(static_cast<T>(1) / determinant());
94}
95
96template<typename T>
98{
99 return m[0][0] * m[1][1] - m[1][0] * m[0][1];
100}
101
102template<typename T>
104 const std::array<T, 2>& b,
105 std::array<T, 2>* const out_x) const
106{
107 PH_ASSERT(out_x);
108
109 std::array<std::array<T, 2>, 1> x;
110 if(solve<1>({b}, &x))
111 {
112 (*out_x)[0] = x[0][0];
113 (*out_x)[1] = x[0][1];
114
115 return true;
116 }
117 else
118 {
119 return false;
120 }
121}
122
123template<typename T>
125 const TVector2<T>& b,
126 TVector2<T>* const out_x) const
127{
128 PH_ASSERT(out_x);
129
130 std::array<T, 2> x;
131 if(solve({b.x(), b.y()}, &x))
132 {
133 out_x->x() = x[0];
134 out_x->y() = x[1];
135
136 return true;
137 }
138 else
139 {
140 return false;
141 }
142}
143
144template<typename T>
145template<std::size_t N>
147 const std::array<std::array<T, 2>, N>& bs,
148 std::array<std::array<T, 2>, N>* const out_xs) const
149{
150 static_assert(!std::numeric_limits<T>::is_integer);
151
152 const T det = determinant();
153 if(det == 0)
154 {
155 return false;
156 }
157
158 const T rcpDet = static_cast<T>(1) / determinant();
159
160 PH_ASSERT(out_xs);
161 for(std::size_t i = 0; i < N; ++i)
162 {
163 (*out_xs)[i][0] = (m[1][1] * bs[i][0] - m[0][1] * bs[i][1]) * rcpDet;
164 (*out_xs)[i][1] = (m[0][0] * bs[i][1] - m[1][0] * bs[i][0]) * rcpDet;
165 }
166
167 for(std::size_t i = 0; i < N; ++i)
168 {
169 if(!std::isfinite((*out_xs)[i][0]) || !std::isfinite((*out_xs)[i][1]))
170 {
171 return false;
172 }
173 }
174 return true;
175}
176
177template<typename T>
178inline std::string TMatrix2<T>::toString() const
179{
180 return "[" + std::to_string(m[0][0]) + ", " + std::to_string(m[0][1]) + "]" +
181 "[" + std::to_string(m[1][0]) + ", " + std::to_string(m[1][1]) + "]";
182}
183
184}// end namespace ph::math
Represents a 2x2 matrix.
Definition TMatrix2.h:16
TMatrix2 mul(const TMatrix2 &rhs) const
Definition TMatrix2.ipp:56
std::string toString() const
Definition TMatrix2.ipp:178
static TMatrix2 makeIdentity()
Definition TMatrix2.ipp:17
Elements m
Definition TMatrix2.h:23
TMatrix2 & mulLocal(T value)
Definition TMatrix2.ipp:79
T determinant() const
Definition TMatrix2.ipp:97
TMatrix2 & initIdentity()
Definition TMatrix2.ipp:47
TMatrix2 inverse() const
Definition TMatrix2.ipp:90
bool solve(const std::array< T, 2 > &b, std::array< T, 2 > *out_x) const
Solves linear systems of the form Ax = b.
Definition TMatrix2.ipp:103
Represents a 2-D vector.
Definition TVector2.h:19
T & x()
Definition TVector2.ipp:38
T & y()
Definition TVector2.ipp:44
Math functions and utilities.
Definition TransformInfo.h:10