| 1 | /**************************************************************************/ |
| 2 | /* transform_2d.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifndef TRANSFORM_2D_H |
| 32 | #define TRANSFORM_2D_H |
| 33 | |
| 34 | #include "core/math/math_funcs.h" |
| 35 | #include "core/math/rect2.h" |
| 36 | #include "core/math/vector2.h" |
| 37 | #include "core/templates/vector.h" |
| 38 | |
| 39 | class String; |
| 40 | |
| 41 | struct _NO_DISCARD_ Transform2D { |
| 42 | // Warning #1: basis of Transform2D is stored differently from Basis. In terms of columns array, the basis matrix looks like "on paper": |
| 43 | // M = (columns[0][0] columns[1][0]) |
| 44 | // (columns[0][1] columns[1][1]) |
| 45 | // This is such that the columns, which can be interpreted as basis vectors of the coordinate system "painted" on the object, can be accessed as columns[i]. |
| 46 | // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to columns[1][0] here. |
| 47 | // This requires additional care when working with explicit indices. |
| 48 | // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading. |
| 49 | |
| 50 | // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down, |
| 51 | // and angle is measure from +X to +Y in a clockwise-fashion. |
| 52 | |
| 53 | Vector2 columns[3]; |
| 54 | |
| 55 | _FORCE_INLINE_ real_t tdotx(const Vector2 &v) const { return columns[0][0] * v.x + columns[1][0] * v.y; } |
| 56 | _FORCE_INLINE_ real_t tdoty(const Vector2 &v) const { return columns[0][1] * v.x + columns[1][1] * v.y; } |
| 57 | |
| 58 | const Vector2 &operator[](int p_idx) const { return columns[p_idx]; } |
| 59 | Vector2 &operator[](int p_idx) { return columns[p_idx]; } |
| 60 | |
| 61 | void invert(); |
| 62 | Transform2D inverse() const; |
| 63 | |
| 64 | void affine_invert(); |
| 65 | Transform2D affine_inverse() const; |
| 66 | |
| 67 | void set_rotation(const real_t p_rot); |
| 68 | real_t get_rotation() const; |
| 69 | real_t get_skew() const; |
| 70 | void set_skew(const real_t p_angle); |
| 71 | _FORCE_INLINE_ void set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale); |
| 72 | _FORCE_INLINE_ void set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew); |
| 73 | void rotate(const real_t p_angle); |
| 74 | |
| 75 | void scale(const Size2 &p_scale); |
| 76 | void scale_basis(const Size2 &p_scale); |
| 77 | void translate_local(const real_t p_tx, const real_t p_ty); |
| 78 | void translate_local(const Vector2 &p_translation); |
| 79 | |
| 80 | real_t determinant() const; |
| 81 | |
| 82 | Size2 get_scale() const; |
| 83 | void set_scale(const Size2 &p_scale); |
| 84 | |
| 85 | _FORCE_INLINE_ const Vector2 &get_origin() const { return columns[2]; } |
| 86 | _FORCE_INLINE_ void set_origin(const Vector2 &p_origin) { columns[2] = p_origin; } |
| 87 | |
| 88 | Transform2D scaled(const Size2 &p_scale) const; |
| 89 | Transform2D scaled_local(const Size2 &p_scale) const; |
| 90 | Transform2D translated(const Vector2 &p_offset) const; |
| 91 | Transform2D translated_local(const Vector2 &p_offset) const; |
| 92 | Transform2D rotated(const real_t p_angle) const; |
| 93 | Transform2D rotated_local(const real_t p_angle) const; |
| 94 | |
| 95 | Transform2D untranslated() const; |
| 96 | |
| 97 | void orthonormalize(); |
| 98 | Transform2D orthonormalized() const; |
| 99 | bool is_equal_approx(const Transform2D &p_transform) const; |
| 100 | bool is_finite() const; |
| 101 | |
| 102 | Transform2D looking_at(const Vector2 &p_target) const; |
| 103 | |
| 104 | bool operator==(const Transform2D &p_transform) const; |
| 105 | bool operator!=(const Transform2D &p_transform) const; |
| 106 | |
| 107 | void operator*=(const Transform2D &p_transform); |
| 108 | Transform2D operator*(const Transform2D &p_transform) const; |
| 109 | void operator*=(const real_t p_val); |
| 110 | Transform2D operator*(const real_t p_val) const; |
| 111 | |
| 112 | Transform2D interpolate_with(const Transform2D &p_transform, const real_t p_c) const; |
| 113 | |
| 114 | _FORCE_INLINE_ Vector2 basis_xform(const Vector2 &p_vec) const; |
| 115 | _FORCE_INLINE_ Vector2 basis_xform_inv(const Vector2 &p_vec) const; |
| 116 | _FORCE_INLINE_ Vector2 xform(const Vector2 &p_vec) const; |
| 117 | _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const; |
| 118 | _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const; |
| 119 | _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const; |
| 120 | _FORCE_INLINE_ Vector<Vector2> xform(const Vector<Vector2> &p_array) const; |
| 121 | _FORCE_INLINE_ Vector<Vector2> xform_inv(const Vector<Vector2> &p_array) const; |
| 122 | |
| 123 | operator String() const; |
| 124 | |
| 125 | Transform2D(const real_t xx, const real_t xy, const real_t yx, const real_t yy, const real_t ox, const real_t oy) { |
| 126 | columns[0][0] = xx; |
| 127 | columns[0][1] = xy; |
| 128 | columns[1][0] = yx; |
| 129 | columns[1][1] = yy; |
| 130 | columns[2][0] = ox; |
| 131 | columns[2][1] = oy; |
| 132 | } |
| 133 | |
| 134 | Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) { |
| 135 | columns[0] = p_x; |
| 136 | columns[1] = p_y; |
| 137 | columns[2] = p_origin; |
| 138 | } |
| 139 | |
| 140 | Transform2D(const real_t p_rot, const Vector2 &p_pos); |
| 141 | |
| 142 | Transform2D(const real_t p_rot, const Size2 &p_scale, const real_t p_skew, const Vector2 &p_pos); |
| 143 | |
| 144 | Transform2D() { |
| 145 | columns[0][0] = 1.0; |
| 146 | columns[1][1] = 1.0; |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const { |
| 151 | return Vector2( |
| 152 | tdotx(p_vec), |
| 153 | tdoty(p_vec)); |
| 154 | } |
| 155 | |
| 156 | Vector2 Transform2D::basis_xform_inv(const Vector2 &p_vec) const { |
| 157 | return Vector2( |
| 158 | columns[0].dot(p_vec), |
| 159 | columns[1].dot(p_vec)); |
| 160 | } |
| 161 | |
| 162 | Vector2 Transform2D::xform(const Vector2 &p_vec) const { |
| 163 | return Vector2( |
| 164 | tdotx(p_vec), |
| 165 | tdoty(p_vec)) + |
| 166 | columns[2]; |
| 167 | } |
| 168 | |
| 169 | Vector2 Transform2D::xform_inv(const Vector2 &p_vec) const { |
| 170 | Vector2 v = p_vec - columns[2]; |
| 171 | |
| 172 | return Vector2( |
| 173 | columns[0].dot(v), |
| 174 | columns[1].dot(v)); |
| 175 | } |
| 176 | |
| 177 | Rect2 Transform2D::xform(const Rect2 &p_rect) const { |
| 178 | Vector2 x = columns[0] * p_rect.size.x; |
| 179 | Vector2 y = columns[1] * p_rect.size.y; |
| 180 | Vector2 pos = xform(p_rect.position); |
| 181 | |
| 182 | Rect2 new_rect; |
| 183 | new_rect.position = pos; |
| 184 | new_rect.expand_to(pos + x); |
| 185 | new_rect.expand_to(pos + y); |
| 186 | new_rect.expand_to(pos + x + y); |
| 187 | return new_rect; |
| 188 | } |
| 189 | |
| 190 | void Transform2D::set_rotation_and_scale(const real_t p_rot, const Size2 &p_scale) { |
| 191 | columns[0][0] = Math::cos(p_rot) * p_scale.x; |
| 192 | columns[1][1] = Math::cos(p_rot) * p_scale.y; |
| 193 | columns[1][0] = -Math::sin(p_rot) * p_scale.y; |
| 194 | columns[0][1] = Math::sin(p_rot) * p_scale.x; |
| 195 | } |
| 196 | |
| 197 | void Transform2D::set_rotation_scale_and_skew(const real_t p_rot, const Size2 &p_scale, const real_t p_skew) { |
| 198 | columns[0][0] = Math::cos(p_rot) * p_scale.x; |
| 199 | columns[1][1] = Math::cos(p_rot + p_skew) * p_scale.y; |
| 200 | columns[1][0] = -Math::sin(p_rot + p_skew) * p_scale.y; |
| 201 | columns[0][1] = Math::sin(p_rot) * p_scale.x; |
| 202 | } |
| 203 | |
| 204 | Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { |
| 205 | Vector2 ends[4] = { |
| 206 | xform_inv(p_rect.position), |
| 207 | xform_inv(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)), |
| 208 | xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)), |
| 209 | xform_inv(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)) |
| 210 | }; |
| 211 | |
| 212 | Rect2 new_rect; |
| 213 | new_rect.position = ends[0]; |
| 214 | new_rect.expand_to(ends[1]); |
| 215 | new_rect.expand_to(ends[2]); |
| 216 | new_rect.expand_to(ends[3]); |
| 217 | |
| 218 | return new_rect; |
| 219 | } |
| 220 | |
| 221 | Vector<Vector2> Transform2D::xform(const Vector<Vector2> &p_array) const { |
| 222 | Vector<Vector2> array; |
| 223 | array.resize(p_array.size()); |
| 224 | |
| 225 | const Vector2 *r = p_array.ptr(); |
| 226 | Vector2 *w = array.ptrw(); |
| 227 | |
| 228 | for (int i = 0; i < p_array.size(); ++i) { |
| 229 | w[i] = xform(r[i]); |
| 230 | } |
| 231 | return array; |
| 232 | } |
| 233 | |
| 234 | Vector<Vector2> Transform2D::xform_inv(const Vector<Vector2> &p_array) const { |
| 235 | Vector<Vector2> array; |
| 236 | array.resize(p_array.size()); |
| 237 | |
| 238 | const Vector2 *r = p_array.ptr(); |
| 239 | Vector2 *w = array.ptrw(); |
| 240 | |
| 241 | for (int i = 0; i < p_array.size(); ++i) { |
| 242 | w[i] = xform_inv(r[i]); |
| 243 | } |
| 244 | return array; |
| 245 | } |
| 246 | |
| 247 | #endif // TRANSFORM_2D_H |
| 248 | |