Transform2D.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #ifndef TRANSFORM2D_H
  2. #define TRANSFORM2D_H
  3. #include "Vector2.h"
  4. // @Todo
  5. // error handling plllls
  6. #ifndef ERR_FAIL_INDEX_V
  7. #define ERR_FAIL_INDEX_V(a, b, c)
  8. #endif
  9. #ifndef ERR_FAIL_INDEX
  10. #define ERR_FAIL_INDEX(a, b)
  11. #endif
  12. #ifndef ERR_FAIL_COND
  13. #define ERR_FAIL_COND(a)
  14. #endif
  15. namespace godot {
  16. typedef Vector2 Size2;
  17. class Rect2;
  18. struct Transform2D {
  19. // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
  20. // M = (elements[0][0] elements[1][0])
  21. // (elements[0][1] elements[1][1])
  22. // 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].
  23. // 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.
  24. // This requires additional care when working with explicit indices.
  25. // See https://en.wikipedia.org/wiki/Row-_and_column-major_order for further reading.
  26. // Warning #2: 2D be aware that unlike 3D code, 2D code uses a left-handed coordinate system: Y-axis points down,
  27. // and angle is measure from +X to +Y in a clockwise-fashion.
  28. Vector2 elements[3];
  29. real_t tdotx(const Vector2& v) const { return elements[0][0] * v.x + elements[1][0] * v.y; }
  30. real_t tdoty(const Vector2& v) const { return elements[0][1] * v.x + elements[1][1] * v.y; }
  31. const Vector2& operator[](int p_idx) const { return elements[p_idx]; }
  32. Vector2& operator[](int p_idx) { return elements[p_idx]; }
  33. Vector2 get_axis(int p_axis) const { ERR_FAIL_INDEX_V(p_axis,3,Vector2()); return elements[p_axis]; }
  34. void set_axis(int p_axis,const Vector2& p_vec) { ERR_FAIL_INDEX(p_axis,3); elements[p_axis]=p_vec; }
  35. void invert();
  36. Transform2D inverse() const;
  37. void affine_invert();
  38. Transform2D affine_inverse() const;
  39. void set_rotation(real_t p_phi);
  40. real_t get_rotation() const;
  41. void set_rotation_and_scale(real_t p_phi,const Size2& p_scale);
  42. void rotate(real_t p_phi);
  43. void scale(const Size2& p_scale);
  44. void scale_basis(const Size2& p_scale);
  45. void translate( real_t p_tx, real_t p_ty);
  46. void translate( const Vector2& p_translation );
  47. real_t basis_determinant() const;
  48. Size2 get_scale() const;
  49. const Vector2& get_origin() const { return elements[2]; }
  50. void set_origin(const Vector2& p_origin) { elements[2]=p_origin; }
  51. Transform2D scaled(const Size2& p_scale) const;
  52. Transform2D basis_scaled(const Size2& p_scale) const;
  53. Transform2D translated(const Vector2& p_offset) const;
  54. Transform2D rotated(real_t p_phi) const;
  55. Transform2D untranslated() const;
  56. void orthonormalize();
  57. Transform2D orthonormalized() const;
  58. bool operator==(const Transform2D& p_transform) const;
  59. bool operator!=(const Transform2D& p_transform) const;
  60. void operator*=(const Transform2D& p_transform);
  61. Transform2D operator*(const Transform2D& p_transform) const;
  62. Transform2D interpolate_with(const Transform2D& p_transform, real_t p_c) const;
  63. Vector2 basis_xform(const Vector2& p_vec) const;
  64. Vector2 basis_xform_inv(const Vector2& p_vec) const;
  65. Vector2 xform(const Vector2& p_vec) const;
  66. Vector2 xform_inv(const Vector2& p_vec) const;
  67. Rect2 xform(const Rect2& p_vec) const;
  68. Rect2 xform_inv(const Rect2& p_vec) const;
  69. operator String() const;
  70. Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy) {
  71. elements[0][0] = xx;
  72. elements[0][1] = xy;
  73. elements[1][0] = yx;
  74. elements[1][1] = yy;
  75. elements[2][0] = ox;
  76. elements[2][1] = oy;
  77. }
  78. Transform2D(real_t p_rot, const Vector2& p_pos);
  79. Transform2D() { elements[0][0]=1.0; elements[1][1]=1.0; }
  80. };
  81. }
  82. #include "Rect2.h"
  83. namespace godot {
  84. Vector2 Transform2D::basis_xform(const Vector2& v) const {
  85. return Vector2(
  86. tdotx(v),
  87. tdoty(v)
  88. );
  89. }
  90. Vector2 Transform2D::basis_xform_inv(const Vector2& v) const{
  91. return Vector2(
  92. elements[0].dot(v),
  93. elements[1].dot(v)
  94. );
  95. }
  96. Vector2 Transform2D::xform(const Vector2& v) const {
  97. return Vector2(
  98. tdotx(v),
  99. tdoty(v)
  100. ) + elements[2];
  101. }
  102. Vector2 Transform2D::xform_inv(const Vector2& p_vec) const {
  103. Vector2 v = p_vec - elements[2];
  104. return Vector2(
  105. elements[0].dot(v),
  106. elements[1].dot(v)
  107. );
  108. }
  109. Rect2 Transform2D::xform(const Rect2& p_rect) const {
  110. Vector2 x=elements[0]*p_rect.size.x;
  111. Vector2 y=elements[1]*p_rect.size.y;
  112. Vector2 pos = xform( p_rect.pos );
  113. Rect2 new_rect;
  114. new_rect.pos=pos;
  115. new_rect.expand_to( pos+x );
  116. new_rect.expand_to( pos+y );
  117. new_rect.expand_to( pos+x+y );
  118. return new_rect;
  119. }
  120. void Transform2D::set_rotation_and_scale(real_t p_rot,const Size2& p_scale) {
  121. elements[0][0]=::cos(p_rot)*p_scale.x;
  122. elements[1][1]=::cos(p_rot)*p_scale.y;
  123. elements[1][0]=-::sin(p_rot)*p_scale.y;
  124. elements[0][1]=::sin(p_rot)*p_scale.x;
  125. }
  126. Rect2 Transform2D::xform_inv(const Rect2& p_rect) const {
  127. Vector2 ends[4]={
  128. xform_inv( p_rect.pos ),
  129. xform_inv( Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y ) ),
  130. xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y ) ),
  131. xform_inv( Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y ) )
  132. };
  133. Rect2 new_rect;
  134. new_rect.pos=ends[0];
  135. new_rect.expand_to(ends[1]);
  136. new_rect.expand_to(ends[2]);
  137. new_rect.expand_to(ends[3]);
  138. return new_rect;
  139. }
  140. void Transform2D::invert() {
  141. // FIXME: this function assumes the basis is a rotation matrix, with no scaling.
  142. // Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
  143. std::swap(elements[0][1],elements[1][0]);
  144. elements[2] = basis_xform(-elements[2]);
  145. }
  146. Transform2D Transform2D::inverse() const {
  147. Transform2D inv=*this;
  148. inv.invert();
  149. return inv;
  150. }
  151. void Transform2D::affine_invert() {
  152. real_t det = basis_determinant();
  153. ERR_FAIL_COND(det==0);
  154. real_t idet = 1.0 / det;
  155. std::swap( elements[0][0],elements[1][1] );
  156. elements[0]*=Vector2(idet,-idet);
  157. elements[1]*=Vector2(-idet,idet);
  158. elements[2] = basis_xform(-elements[2]);
  159. }
  160. Transform2D Transform2D::affine_inverse() const {
  161. Transform2D inv=*this;
  162. inv.affine_invert();
  163. return inv;
  164. }
  165. void Transform2D::rotate(real_t p_phi) {
  166. *this = Transform2D(p_phi,Vector2()) * (*this);
  167. }
  168. real_t Transform2D::get_rotation() const {
  169. real_t det = basis_determinant();
  170. Transform2D m = orthonormalized();
  171. if (det < 0) {
  172. m.scale_basis(Size2(-1,-1));
  173. }
  174. return ::atan2(m[0].y,m[0].x);
  175. }
  176. void Transform2D::set_rotation(real_t p_rot) {
  177. real_t cr = ::cos(p_rot);
  178. real_t sr = ::sin(p_rot);
  179. elements[0][0]=cr;
  180. elements[0][1]=sr;
  181. elements[1][0]=-sr;
  182. elements[1][1]=cr;
  183. }
  184. Transform2D::Transform2D(real_t p_rot, const Vector2& p_pos) {
  185. real_t cr = ::cos(p_rot);
  186. real_t sr = ::sin(p_rot);
  187. elements[0][0]=cr;
  188. elements[0][1]=sr;
  189. elements[1][0]=-sr;
  190. elements[1][1]=cr;
  191. elements[2]=p_pos;
  192. }
  193. Size2 Transform2D::get_scale() const {
  194. real_t det_sign = basis_determinant() > 0 ? 1 : -1;
  195. return det_sign * Size2( elements[0].length(), elements[1].length() );
  196. }
  197. void Transform2D::scale(const Size2& p_scale) {
  198. scale_basis(p_scale);
  199. elements[2]*=p_scale;
  200. }
  201. void Transform2D::scale_basis(const Size2& p_scale) {
  202. elements[0][0]*=p_scale.x;
  203. elements[0][1]*=p_scale.y;
  204. elements[1][0]*=p_scale.x;
  205. elements[1][1]*=p_scale.y;
  206. }
  207. void Transform2D::translate( real_t p_tx, real_t p_ty) {
  208. translate(Vector2(p_tx,p_ty));
  209. }
  210. void Transform2D::translate( const Vector2& p_translation ) {
  211. elements[2]+=basis_xform(p_translation);
  212. }
  213. void Transform2D::orthonormalize() {
  214. // Gram-Schmidt Process
  215. Vector2 x=elements[0];
  216. Vector2 y=elements[1];
  217. x.normalize();
  218. y = (y-x*(x.dot(y)));
  219. y.normalize();
  220. elements[0]=x;
  221. elements[1]=y;
  222. }
  223. Transform2D Transform2D::orthonormalized() const {
  224. Transform2D on=*this;
  225. on.orthonormalize();
  226. return on;
  227. }
  228. bool Transform2D::operator==(const Transform2D& p_transform) const {
  229. for(int i=0;i<3;i++) {
  230. if (elements[i]!=p_transform.elements[i])
  231. return false;
  232. }
  233. return true;
  234. }
  235. bool Transform2D::operator!=(const Transform2D& p_transform) const {
  236. for(int i=0;i<3;i++) {
  237. if (elements[i]!=p_transform.elements[i])
  238. return true;
  239. }
  240. return false;
  241. }
  242. void Transform2D::operator*=(const Transform2D& p_transform) {
  243. elements[2] = xform(p_transform.elements[2]);
  244. real_t x0,x1,y0,y1;
  245. x0 = tdotx(p_transform.elements[0]);
  246. x1 = tdoty(p_transform.elements[0]);
  247. y0 = tdotx(p_transform.elements[1]);
  248. y1 = tdoty(p_transform.elements[1]);
  249. elements[0][0]=x0;
  250. elements[0][1]=x1;
  251. elements[1][0]=y0;
  252. elements[1][1]=y1;
  253. }
  254. Transform2D Transform2D::operator*(const Transform2D& p_transform) const {
  255. Transform2D t = *this;
  256. t*=p_transform;
  257. return t;
  258. }
  259. Transform2D Transform2D::scaled(const Size2& p_scale) const {
  260. Transform2D copy=*this;
  261. copy.scale(p_scale);
  262. return copy;
  263. }
  264. Transform2D Transform2D::basis_scaled(const Size2& p_scale) const {
  265. Transform2D copy=*this;
  266. copy.scale_basis(p_scale);
  267. return copy;
  268. }
  269. Transform2D Transform2D::untranslated() const {
  270. Transform2D copy=*this;
  271. copy.elements[2]=Vector2();
  272. return copy;
  273. }
  274. Transform2D Transform2D::translated(const Vector2& p_offset) const {
  275. Transform2D copy=*this;
  276. copy.translate(p_offset);
  277. return copy;
  278. }
  279. Transform2D Transform2D::rotated(real_t p_phi) const {
  280. Transform2D copy=*this;
  281. copy.rotate(p_phi);
  282. return copy;
  283. }
  284. real_t Transform2D::basis_determinant() const {
  285. return elements[0].x * elements[1].y - elements[0].y * elements[1].x;
  286. }
  287. Transform2D Transform2D::interpolate_with(const Transform2D& p_transform, real_t p_c) const {
  288. //extract parameters
  289. Vector2 p1 = get_origin();
  290. Vector2 p2 = p_transform.get_origin();
  291. real_t r1 = get_rotation();
  292. real_t r2 = p_transform.get_rotation();
  293. Size2 s1 = get_scale();
  294. Size2 s2 = p_transform.get_scale();
  295. //slerp rotation
  296. Vector2 v1(::cos(r1), ::sin(r1));
  297. Vector2 v2(::cos(r2), ::sin(r2));
  298. real_t dot = v1.dot(v2);
  299. dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1]
  300. Vector2 v;
  301. if (dot > 0.9995) {
  302. v = Vector2::linear_interpolate(v1, v2, p_c).normalized(); //linearly interpolate to avoid numerical precision issues
  303. } else {
  304. real_t angle = p_c*::acos(dot);
  305. Vector2 v3 = (v2 - v1*dot).normalized();
  306. v = v1*::cos(angle) + v3*::sin(angle);
  307. }
  308. //construct matrix
  309. Transform2D res(::atan2(v.y, v.x), Vector2::linear_interpolate(p1, p2, p_c));
  310. res.scale_basis(Vector2::linear_interpolate(s1, s2, p_c));
  311. return res;
  312. }
  313. Transform2D::operator String() const {
  314. //return String(String()+elements[0]+", "+elements[1]+", "+elements[2]);
  315. return String(); // @Todo
  316. }
  317. }
  318. #endif // TRANSFORM2D_H