diff --git a/include/vsg/core/ConstVisitor.h b/include/vsg/core/ConstVisitor.h index 5ffe597d23..3a39b9a075 100644 --- a/include/vsg/core/ConstVisitor.h +++ b/include/vsg/core/ConstVisitor.h @@ -223,6 +223,10 @@ namespace vsg virtual void apply(const uivec2Value&); virtual void apply(const uivec3Value&); virtual void apply(const uivec4Value&); + virtual void apply(const mat2Value&); + virtual void apply(const dmat2Value&); + virtual void apply(const mat3Value&); + virtual void apply(const dmat3Value&); virtual void apply(const mat4Value&); virtual void apply(const dmat4Value&); diff --git a/include/vsg/core/Value.h b/include/vsg/core/Value.h index 0dbd4b1517..64022066fe 100644 --- a/include/vsg/core/Value.h +++ b/include/vsg/core/Value.h @@ -16,6 +16,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI #include #include +#include #include #include #include @@ -231,6 +232,9 @@ namespace vsg VSG_value(uivec3Value, uivec3); VSG_value(uivec4Value, uivec4); + VSG_value(mat2Value, mat2); + VSG_value(dmat2Value, dmat2); + VSG_value(mat3Value, mat3); VSG_value(dmat3Value, dmat3); diff --git a/include/vsg/core/Visitor.h b/include/vsg/core/Visitor.h index 9185d8c22b..68eedfa9ed 100644 --- a/include/vsg/core/Visitor.h +++ b/include/vsg/core/Visitor.h @@ -223,6 +223,10 @@ namespace vsg virtual void apply(uivec2Value&); virtual void apply(uivec3Value&); virtual void apply(uivec4Value&); + virtual void apply(mat2Value&); + virtual void apply(dmat2Value&); + virtual void apply(mat3Value&); + virtual void apply(dmat3Value&); virtual void apply(mat4Value&); virtual void apply(dmat4Value&); diff --git a/include/vsg/maths/mat2.h b/include/vsg/maths/mat2.h new file mode 100644 index 0000000000..9cb1db5452 --- /dev/null +++ b/include/vsg/maths/mat2.h @@ -0,0 +1,153 @@ +#pragma once + +/* + +Copyright(c) 2018 Robert Osfield + +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: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +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. + + */ + +#include + +namespace vsg +{ + + /// t_mat2 template class that represents a 3x3 matrix. + template + struct t_mat2 + { + public: + using value_type = T; + using column_type = t_vec2; + + column_type value[2]; + + constexpr t_mat2() : + value{{1, 0}, + {0, 1}} {} + + constexpr explicit t_mat2(value_type v) : + value{{v, 0}, + {0, v}} {} + + constexpr t_mat2(value_type v0, value_type v1, /* column 0 */ + value_type v2, value_type v3) /* column 1 */ : + value{{v0, v1}, + {v2, v3}} + { + } + + constexpr explicit t_mat2(value_type v[4]) : + value{{v[0], v[1]}, + {v[2], v[3]}} {} + + constexpr t_mat2(const column_type& c0, + const column_type& c1) : + value{c0, c1} + { + } + + template + explicit t_mat2(const t_mat2& rhs) + { + value[0] = rhs[0]; + value[1] = rhs[1]; + } + + constexpr std::size_t size() const { return 4; } + constexpr std::size_t columns() const { return 2; } + constexpr std::size_t rows() const { return 2; } + + column_type& operator[](std::size_t c) { return value[c]; } + const column_type& operator[](std::size_t c) const { return value[c]; } + + value_type& operator()(std::size_t c, std::size_t r) { return value[c][r]; } + value_type operator()(std::size_t c, std::size_t r) const { return value[c][r]; } + + template + t_mat2& operator=(const t_mat2& rhs) + { + value[0] = rhs[0]; + value[1] = rhs[1]; + return *this; + } + + void set(value_type v0, value_type v1, /* column 0 */ + value_type v2, value_type v3) /* column 1 */ + { + value[0].set(v0, v1); + value[1].set(v2, v3); + } + + template + void set(const t_mat2& rhs) + { + value[0] = rhs[0]; + value[1] = rhs[1]; + } + + T* data() { return value[0].data(); } + const T* data() const { return value[0].data(); } + }; + + using mat2 = t_mat2; /// float 2x2 matrix + using dmat2 = t_mat2; /// double 2x2 matrix + + VSG_type_name(vsg::mat2); + VSG_type_name(vsg::dmat2); + + template + bool operator==(const t_mat2& lhs, const t_mat2& rhs) + { + return lhs.value[0] == rhs.value[0] && + lhs.value[1] == rhs.value[1]; + } + + template + bool operator!=(const t_mat2& lhs, const t_mat2& rhs) + { + return lhs.value[0] != rhs.value[0] || + lhs.value[1] != rhs.value[1]; + } + + template + bool operator<(const t_mat2& lhs, const t_mat2& rhs) + { + if (lhs.value[0] < rhs.value[0]) return true; + if (rhs.value[0] < lhs.value[0]) return false; + return lhs.value[1] < rhs.value[1]; + } + + template + T dot(const t_mat2& lhs, const t_mat2& rhs, int c, int r) + { + return lhs[0][r] * rhs[c][0] + + lhs[1][r] * rhs[c][1]; + } + + template + t_mat2 operator*(const t_mat2& lhs, const t_mat2& rhs) + { + return t_mat2(dot(lhs, rhs, 0, 0), dot(lhs, rhs, 0, 1), + dot(lhs, rhs, 1, 0), dot(lhs, rhs, 1, 1)); + } + + template + t_vec3 operator*(const t_mat2& lhs, const t_vec2& rhs) + { + return t_vec3((lhs[0][0] * rhs[0] + lhs[1][0] * rhs[1]), + (lhs[0][1] * rhs[0] + lhs[1][1] * rhs[1])); + } + + template + t_vec3 operator*(const t_vec2& lhs, const t_mat2& rhs) + { + return t_vec3(lhs[0] * rhs[0][0] + lhs[1] * rhs[0][1], + lhs[0] * rhs[1][0] + lhs[1] * rhs[1][1]); + } +} // namespace vsg diff --git a/src/vsg/core/ConstVisitor.cpp b/src/vsg/core/ConstVisitor.cpp index 82a576d6a2..af19479de7 100644 --- a/src/vsg/core/ConstVisitor.cpp +++ b/src/vsg/core/ConstVisitor.cpp @@ -165,6 +165,22 @@ void ConstVisitor::apply(const uivec4Value& value) { apply(static_cast(value)); } +void ConstVisitor::apply(const mat2Value& value) +{ + apply(static_cast(value)); +} +void ConstVisitor::apply(const dmat2Value& value) +{ + apply(static_cast(value)); +} +void ConstVisitor::apply(const mat3Value& value) +{ + apply(static_cast(value)); +} +void ConstVisitor::apply(const dmat3Value& value) +{ + apply(static_cast(value)); +} void ConstVisitor::apply(const mat4Value& value) { apply(static_cast(value)); diff --git a/src/vsg/core/Visitor.cpp b/src/vsg/core/Visitor.cpp index c0fb6d98a6..dd2370e45b 100644 --- a/src/vsg/core/Visitor.cpp +++ b/src/vsg/core/Visitor.cpp @@ -165,6 +165,22 @@ void Visitor::apply(uivec4Value& value) { apply(static_cast(value)); } +void Visitor::apply(mat2Value& value) +{ + apply(static_cast(value)); +} +void Visitor::apply(dmat2Value& value) +{ + apply(static_cast(value)); +} +void Visitor::apply(mat3Value& value) +{ + apply(static_cast(value)); +} +void Visitor::apply(dmat3Value& value) +{ + apply(static_cast(value)); +} void Visitor::apply(mat4Value& value) { apply(static_cast(value));