Transform2D.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef TRANSFORM2D_H
  2. #define TRANSFORM2D_H
  3. #if defined(_WIN32)
  4. # ifdef _GD_CPP_CORE_API_IMPL
  5. # define GD_CPP_CORE_API __declspec(dllexport)
  6. # else
  7. # define GD_CPP_CORE_API __declspec(dllimport)
  8. # endif
  9. #else
  10. # define GD_CPP_CORE_API
  11. #endif
  12. #include "Vector2.hpp"
  13. namespace godot {
  14. typedef Vector2 Size2;
  15. class Rect2;
  16. struct GD_CPP_CORE_API Transform2D {
  17. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  18. // M = (elements[0][0] elements[1][0])
  19. // (elements[0][1] elements[1][1])
  20. // 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].
  21. // 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.
  22. // This requires additional care when working with explicit indices.
  23. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  24. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  25. // and angle is measure from +X to +Y in a clockwise-fashion.
  26. Vector2 elements[3];
  27. inline real_t tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  28. inline real_t tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  29. inline const Vector2& operator[](int p_idx) const { return elements[p_idx]; }
  30. inline Vector2& operator[](int p_idx) { return elements[p_idx]; }
  31. inline Vector2 get_axis(int p_axis) const { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
  32. inline void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
  33. void invert();
  34. Transform2D inverse() const;
  35. void affine_invert();
  36. Transform2D affine_inverse() const;
  37. void set_rotation(real_t p_phi);
  38. real_t get_rotation() const;
  39. void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
  40. void rotate(real_t p_phi);
  41. void scale(const Size2& p_scale);
  42. void scale_basis(const Size2& p_scale);
  43. void translate( real_t p_tx, real_t p_ty);
  44. void translate( const Vector2& p_translation );
  45. real_t basis_determinant() const;
  46. Size2 get_scale() const;
  47. inline const Vector2& get_origin() const { return elements[2]; }
  48. inline void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
  49. Transform2D scaled(const Size2& p_scale) const;
  50. Transform2D basis_scaled(const Size2& p_scale) const;
  51. Transform2D translated(const Vector2& p_offset) const;
  52. Transform2D rotated(real_t p_phi) const;
  53. Transform2D untranslated() const;
  54. void orthonormalize();
  55. Transform2D orthonormalized() const;
  56. bool operator==(const Transform2D& p_transform) const;
  57. bool operator!=(const Transform2D& p_transform) const;
  58. void operator*=(const Transform2D& p_transform);
  59. Transform2D operator*(const Transform2D& p_transform) const;
  60. Transform2D interpolate_with(const Transform2D& p_transform, real_t p_c) const;
  61. Vector2 basis_xform(const Vector2& p_vec) const;
  62. Vector2 basis_xform_inv(const Vector2& p_vec) const;
  63. Vector2 xform(const Vector2& p_vec) const;
  64. Vector2 xform_inv(const Vector2& p_vec) const;
  65. Rect2 xform(const Rect2& p_vec) const;
  66. Rect2 xform_inv(const Rect2& p_vec) const;
  67. operator String() const;
  68. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy);
  69. Transform2D(real_t p_rot, const Vector2& p_pos);
  70. inline Transform2D() { elements[0][0]=1.0; elements[1][1]=1.0; }
  71. };
  72. }
  73. #endif // TRANSFORM2D_H