vsg 1.1.3
VulkanSceneGraph library
Loading...
Searching...
No Matches
mat3.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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/vec3.h>
16
17namespace vsg
18{
19
21 template<typename T>
22 struct t_mat3
23 {
24 public:
25 using value_type = T;
26 using column_type = t_vec3<T>;
27
28 column_type value[3];
29
30 constexpr t_mat3() :
31 value{{1, 0, 0},
32 {0, 1, 0},
33 {0, 0, 1}} {}
34
35 constexpr explicit t_mat3(value_type v) :
36 value{{v, 0, 0},
37 {0, v, 0},
38 {0, 0, v}} {}
39
40 constexpr t_mat3(value_type v0, value_type v1, value_type v2, /* column 0 */
41 value_type v3, value_type v4, value_type v5, /* column 1 */
42 value_type v6, value_type v7, value_type v8) /* column 2 */ :
43 value{{v0, v1, v2},
44 {v3, v4, v5},
45 {v6, v7, v8}}
46 {
47 }
48
49 constexpr explicit t_mat3(value_type v[9]) :
50 value{{v[0], v[1], v[2]},
51 {v[3], v[4], v[5]},
52 {v[6], v[7], v[8]}} {}
53
54 constexpr t_mat3(const column_type& c0,
55 const column_type& c1,
56 const column_type& c2) :
57 value{c0, c1, c2}
58 {
59 }
60
61 template<typename R>
62 explicit t_mat3(const t_mat3<R>& rhs)
63 {
64 value[0] = rhs[0];
65 value[1] = rhs[1];
66 value[2] = rhs[2];
67 }
68
69 constexpr std::size_t size() const { return 9; }
70 constexpr std::size_t columns() const { return 3; }
71 constexpr std::size_t rows() const { return 3; }
72
73 column_type& operator[](std::size_t c) { return value[c]; }
74 const column_type& operator[](std::size_t c) const { return value[c]; }
75
76 value_type& operator()(std::size_t c, std::size_t r) { return value[c][r]; }
77 value_type operator()(std::size_t c, std::size_t r) const { return value[c][r]; }
78
79 template<typename R>
80 t_mat3& operator=(const t_mat3<R>& rhs)
81 {
82 value[0] = rhs[0];
83 value[1] = rhs[1];
84 value[2] = rhs[2];
85 return *this;
86 }
87
88 void set(value_type v0, value_type v1, value_type v2, /* column 0 */
89 value_type v3, value_type v4, value_type v5, /* column 1 */
90 value_type v6, value_type v7, value_type v8) /* column 2 */
91 {
92 value[0].set(v0, v1, v2);
93 value[1].set(v3, v4, v5);
94 value[2].set(v6, v7, v8);
95 }
96
97 template<typename R>
98 void set(const t_mat3<R>& rhs)
99 {
100 value[0] = rhs[0];
101 value[1] = rhs[1];
102 value[2] = rhs[2];
103 }
104
105 T* data() { return value[0].data(); }
106 const T* data() const { return value[0].data(); }
107 };
108
109 using mat3 = t_mat3<float>;
110 using dmat3 = t_mat3<double>;
111
112 VSG_type_name(vsg::mat3);
113 VSG_type_name(vsg::dmat3);
114
115 template<typename T>
116 bool operator==(const t_mat3<T>& lhs, const t_mat3<T>& rhs)
117 {
118 return lhs.value[0] == rhs.value[0] &&
119 lhs.value[1] == rhs.value[1] &&
120 lhs.value[2] == rhs.value[2];
121 }
122
123 template<typename T>
124 bool operator!=(const t_mat3<T>& lhs, const t_mat3<T>& rhs)
125 {
126 return lhs.value[0] != rhs.value[0] ||
127 lhs.value[1] != rhs.value[1] ||
128 lhs.value[2] != rhs.value[2];
129 }
130
131 template<typename T>
132 bool operator<(const t_mat3<T>& lhs, const t_mat3<T>& rhs)
133 {
134 if (lhs.value[0] < rhs.value[0]) return true;
135 if (rhs.value[0] < lhs.value[0]) return false;
136 if (lhs.value[1] < rhs.value[1]) return true;
137 if (rhs.value[1] < lhs.value[1]) return false;
138 return lhs.value[2] < rhs.value[2];
139 }
140
141 template<typename T>
142 T dot(const t_mat3<T>& lhs, const t_mat3<T>& rhs, int c, int r)
143 {
144 return lhs[0][r] * rhs[c][0] +
145 lhs[1][r] * rhs[c][1] +
146 lhs[2][r] * rhs[c][2];
147 }
148
149 template<typename T>
150 t_mat3<T> operator*(const t_mat3<T>& lhs, const t_mat3<T>& rhs)
151 {
152 return t_mat3<T>(dot(lhs, rhs, 0, 0), dot(lhs, rhs, 0, 1), dot(lhs, rhs, 0, 2),
153 dot(lhs, rhs, 1, 0), dot(lhs, rhs, 1, 1), dot(lhs, rhs, 1, 2),
154 dot(lhs, rhs, 2, 0), dot(lhs, rhs, 2, 1), dot(lhs, rhs, 2, 2));
155 }
156
157 template<typename T>
158 t_vec3<T> operator*(const t_mat3<T>& lhs, const t_vec3<T>& rhs)
159 {
160 return t_vec3<T>((lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1] + lhs[2][0] * rhs[2]),
161 (lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1] + lhs[2][1] * rhs[2]),
162 (lhs[0][2] * rhs[0] + lhs[1][2] * rhs[1] + lhs[2][2] * rhs[2]));
163 }
164
165 template<typename T>
166 t_vec3<T> operator*(const t_vec3<T>& lhs, const t_mat3<T>& rhs)
167 {
168 return t_vec3<T>(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1] + lhs[2] * rhs[0][2],
169 lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1] + lhs[2] * rhs[1][2],
170 lhs[0] * rhs[2][0] + lhs[1] * rhs[2][1] + lhs[2] * rhs[2][2]);
171 }
172} // namespace vsg
t_mat3 template class that represents a 3x3 matrix.
Definition mat3.h:23
t_vec3 template class that represents a 3D vector
Definition vec3.h:34