Skip to content

Commit 3beb56e

Browse files
Merge pull request #1437 from vsg-dev/mat2
Added vsg::mat2, vsg::mat2Value support and associated Visitor/ConstVisitor support.
2 parents 90e622d + c11a982 commit 3beb56e

6 files changed

Lines changed: 197 additions & 0 deletions

File tree

include/vsg/core/ConstVisitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ namespace vsg
223223
virtual void apply(const uivec2Value&);
224224
virtual void apply(const uivec3Value&);
225225
virtual void apply(const uivec4Value&);
226+
virtual void apply(const mat2Value&);
227+
virtual void apply(const dmat2Value&);
228+
virtual void apply(const mat3Value&);
229+
virtual void apply(const dmat3Value&);
226230
virtual void apply(const mat4Value&);
227231
virtual void apply(const dmat4Value&);
228232

include/vsg/core/Value.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
1616
#include <vsg/core/type_name.h>
1717

1818
#include <vsg/maths/box.h>
19+
#include <vsg/maths/mat2.h>
1920
#include <vsg/maths/mat3.h>
2021
#include <vsg/maths/mat4.h>
2122
#include <vsg/maths/quat.h>
@@ -231,6 +232,9 @@ namespace vsg
231232
VSG_value(uivec3Value, uivec3);
232233
VSG_value(uivec4Value, uivec4);
233234

235+
VSG_value(mat2Value, mat2);
236+
VSG_value(dmat2Value, dmat2);
237+
234238
VSG_value(mat3Value, mat3);
235239
VSG_value(dmat3Value, dmat3);
236240

include/vsg/core/Visitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ namespace vsg
223223
virtual void apply(uivec2Value&);
224224
virtual void apply(uivec3Value&);
225225
virtual void apply(uivec4Value&);
226+
virtual void apply(mat2Value&);
227+
virtual void apply(dmat2Value&);
228+
virtual void apply(mat3Value&);
229+
virtual void apply(dmat3Value&);
226230
virtual void apply(mat4Value&);
227231
virtual void apply(dmat4Value&);
228232

include/vsg/maths/mat2.h

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#pragma once
2+
3+
/* <editor-fold desc="MIT License">
4+
5+
Copyright(c) 2018 Robert Osfield
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+
13+
</editor-fold> */
14+
15+
#include <vsg/maths/vec2.h>
16+
17+
namespace vsg
18+
{
19+
20+
/// t_mat2 template class that represents a 3x3 matrix.
21+
template<typename T>
22+
struct t_mat2
23+
{
24+
public:
25+
using value_type = T;
26+
using column_type = t_vec2<T>;
27+
28+
column_type value[2];
29+
30+
constexpr t_mat2() :
31+
value{{1, 0},
32+
{0, 1}} {}
33+
34+
constexpr explicit t_mat2(value_type v) :
35+
value{{v, 0},
36+
{0, v}} {}
37+
38+
constexpr t_mat2(value_type v0, value_type v1, /* column 0 */
39+
value_type v2, value_type v3) /* column 1 */ :
40+
value{{v0, v1},
41+
{v2, v3}}
42+
{
43+
}
44+
45+
constexpr explicit t_mat2(value_type v[4]) :
46+
value{{v[0], v[1]},
47+
{v[2], v[3]}} {}
48+
49+
constexpr t_mat2(const column_type& c0,
50+
const column_type& c1) :
51+
value{c0, c1}
52+
{
53+
}
54+
55+
template<typename R>
56+
explicit t_mat2(const t_mat2<R>& rhs)
57+
{
58+
value[0] = rhs[0];
59+
value[1] = rhs[1];
60+
}
61+
62+
constexpr std::size_t size() const { return 4; }
63+
constexpr std::size_t columns() const { return 2; }
64+
constexpr std::size_t rows() const { return 2; }
65+
66+
column_type& operator[](std::size_t c) { return value[c]; }
67+
const column_type& operator[](std::size_t c) const { return value[c]; }
68+
69+
value_type& operator()(std::size_t c, std::size_t r) { return value[c][r]; }
70+
value_type operator()(std::size_t c, std::size_t r) const { return value[c][r]; }
71+
72+
template<typename R>
73+
t_mat2& operator=(const t_mat2<R>& rhs)
74+
{
75+
value[0] = rhs[0];
76+
value[1] = rhs[1];
77+
return *this;
78+
}
79+
80+
void set(value_type v0, value_type v1, /* column 0 */
81+
value_type v2, value_type v3) /* column 1 */
82+
{
83+
value[0].set(v0, v1);
84+
value[1].set(v2, v3);
85+
}
86+
87+
template<typename R>
88+
void set(const t_mat2<R>& rhs)
89+
{
90+
value[0] = rhs[0];
91+
value[1] = rhs[1];
92+
}
93+
94+
T* data() { return value[0].data(); }
95+
const T* data() const { return value[0].data(); }
96+
};
97+
98+
using mat2 = t_mat2<float>; /// float 2x2 matrix
99+
using dmat2 = t_mat2<double>; /// double 2x2 matrix
100+
101+
VSG_type_name(vsg::mat2);
102+
VSG_type_name(vsg::dmat2);
103+
104+
template<typename T>
105+
bool operator==(const t_mat2<T>& lhs, const t_mat2<T>& rhs)
106+
{
107+
return lhs.value[0] == rhs.value[0] &&
108+
lhs.value[1] == rhs.value[1];
109+
}
110+
111+
template<typename T>
112+
bool operator!=(const t_mat2<T>& lhs, const t_mat2<T>& rhs)
113+
{
114+
return lhs.value[0] != rhs.value[0] ||
115+
lhs.value[1] != rhs.value[1];
116+
}
117+
118+
template<typename T>
119+
bool operator<(const t_mat2<T>& lhs, const t_mat2<T>& rhs)
120+
{
121+
if (lhs.value[0] < rhs.value[0]) return true;
122+
if (rhs.value[0] < lhs.value[0]) return false;
123+
return lhs.value[1] < rhs.value[1];
124+
}
125+
126+
template<typename T>
127+
T dot(const t_mat2<T>& lhs, const t_mat2<T>& rhs, int c, int r)
128+
{
129+
return lhs[0][r] * rhs[c][0] +
130+
lhs[1][r] * rhs[c][1];
131+
}
132+
133+
template<typename T>
134+
t_mat2<T> operator*(const t_mat2<T>& lhs, const t_mat2<T>& rhs)
135+
{
136+
return t_mat2<T>(dot(lhs, rhs, 0, 0), dot(lhs, rhs, 0, 1),
137+
dot(lhs, rhs, 1, 0), dot(lhs, rhs, 1, 1));
138+
}
139+
140+
template<typename T>
141+
t_vec3<T> operator*(const t_mat2<T>& lhs, const t_vec2<T>& rhs)
142+
{
143+
return t_vec3<T>((lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1]),
144+
(lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1]));
145+
}
146+
147+
template<typename T>
148+
t_vec3<T> operator*(const t_vec2<T>& lhs, const t_mat2<T>& rhs)
149+
{
150+
return t_vec3<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1],
151+
lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1]);
152+
}
153+
} // namespace vsg

src/vsg/core/ConstVisitor.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ void ConstVisitor::apply(const uivec4Value& value)
165165
{
166166
apply(static_cast<const Data&>(value));
167167
}
168+
void ConstVisitor::apply(const mat2Value& value)
169+
{
170+
apply(static_cast<const Data&>(value));
171+
}
172+
void ConstVisitor::apply(const dmat2Value& value)
173+
{
174+
apply(static_cast<const Data&>(value));
175+
}
176+
void ConstVisitor::apply(const mat3Value& value)
177+
{
178+
apply(static_cast<const Data&>(value));
179+
}
180+
void ConstVisitor::apply(const dmat3Value& value)
181+
{
182+
apply(static_cast<const Data&>(value));
183+
}
168184
void ConstVisitor::apply(const mat4Value& value)
169185
{
170186
apply(static_cast<const Data&>(value));

src/vsg/core/Visitor.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ void Visitor::apply(uivec4Value& value)
165165
{
166166
apply(static_cast<Data&>(value));
167167
}
168+
void Visitor::apply(mat2Value& value)
169+
{
170+
apply(static_cast<Data&>(value));
171+
}
172+
void Visitor::apply(dmat2Value& value)
173+
{
174+
apply(static_cast<Data&>(value));
175+
}
176+
void Visitor::apply(mat3Value& value)
177+
{
178+
apply(static_cast<Data&>(value));
179+
}
180+
void Visitor::apply(dmat3Value& value)
181+
{
182+
apply(static_cast<Data&>(value));
183+
}
168184
void Visitor::apply(mat4Value& value)
169185
{
170186
apply(static_cast<Data&>(value));

0 commit comments

Comments
 (0)