|
| 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 |
0 commit comments