Transform2D.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*************************************************************************/
  2. /* Transform2D.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef TRANSFORM2D_H
  31. #define TRANSFORM2D_H
  32. #include "Vector2.hpp"
  33. namespace godot {
  34. typedef Vector2 Size2;
  35. struct Rect2;
  36. struct Transform2D {
  37. static const Transform2D IDENTITY;
  38. static const Transform2D FLIP_X;
  39. static const Transform2D FLIP_Y;
  40. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  41. // M = (elements[0][0] elements[1][0])
  42. // (elements[0][1] elements[1][1])
  43. // 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 elements[i].
  44. // Note that this is the opposite of the indices in mathematical texts, meaning: $M_{12}$ in a math book corresponds to elements[1][0] here.
  45. // This requires additional care when working with explicit indices.
  46. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  47. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  48. // and angle is measure from +X to +Y in a clockwise-fashion.
  49. Vector2 elements[3];
  50. inline real_t tdotx(const Vector2 &v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  51. inline real_t tdoty(const Vector2 &v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  52. inline const Vector2 &operator[](int p_idx) const { return elements[p_idx]; }
  53. inline Vector2 &operator[](int p_idx) { return elements[p_idx]; }
  54. inline Vector2 get_axis(int p_axis) const {
  55. ERR_FAIL_INDEX_V(p_axis, 3, Vector2());
  56. return elements[p_axis];
  57. }
  58. inline void set_axis(int p_axis, const Vector2 &p_vec) {
  59. ERR_FAIL_INDEX(p_axis, 3);
  60. elements[p_axis] = p_vec;
  61. }
  62. void invert();
  63. Transform2D inverse() const;
  64. void affine_invert();
  65. Transform2D affine_inverse() const;
  66. void set_rotation(real_t p_phi);
  67. real_t get_rotation() const;
  68. void set_rotation_and_scale(real_t p_phi, const Size2 &p_scale);
  69. void rotate(real_t p_phi);
  70. void scale(const Size2 &p_scale);
  71. void scale_basis(const Size2 &p_scale);
  72. void translate(real_t p_tx, real_t p_ty);
  73. void translate(const Vector2 &p_translation);
  74. real_t basis_determinant() const;
  75. Size2 get_scale() const;
  76. inline const Vector2 &get_origin() const { return elements[2]; }
  77. inline void set_origin(const Vector2 &p_origin) { elements[2] = p_origin; }
  78. Transform2D scaled(const Size2 &p_scale) const;
  79. Transform2D basis_scaled(const Size2 &p_scale) const;
  80. Transform2D translated(const Vector2 &p_offset) const;
  81. Transform2D rotated(real_t p_phi) const;
  82. Transform2D untranslated() const;
  83. void orthonormalize();
  84. Transform2D orthonormalized() const;
  85. bool operator==(const Transform2D &p_transform) const;
  86. bool operator!=(const Transform2D &p_transform) const;
  87. void operator*=(const Transform2D &p_transform);
  88. Transform2D operator*(const Transform2D &p_transform) const;
  89. Transform2D interpolate_with(const Transform2D &p_transform, real_t p_c) const;
  90. Vector2 basis_xform(const Vector2 &p_vec) const;
  91. Vector2 basis_xform_inv(const Vector2 &p_vec) const;
  92. Vector2 xform(const Vector2 &p_vec) const;
  93. Vector2 xform_inv(const Vector2 &p_vec) const;
  94. Rect2 xform(const Rect2 &p_vec) const;
  95. Rect2 xform_inv(const Rect2 &p_vec) const;
  96. operator String() const;
  97. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy);
  98. Transform2D(real_t p_rot, const Vector2 &p_pos);
  99. inline Transform2D() {
  100. elements[0][0] = 1.0;
  101. elements[1][1] = 1.0;
  102. }
  103. };
  104. } // namespace godot
  105. #endif // TRANSFORM2D_H