Transform.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <array>
  9. #include <folly/Hash.h>
  10. #include <react/graphics/Float.h>
  11. #include <react/graphics/Geometry.h>
  12. namespace facebook {
  13. namespace react {
  14. /*
  15. * Defines transform matrix to apply affine transformations.
  16. */
  17. struct Transform {
  18. std::array<Float, 16> matrix{
  19. {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}};
  20. /*
  21. * Returns the identity transform (`[1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]`).
  22. */
  23. static Transform Identity();
  24. /*
  25. * Returns a Perspective transform.
  26. */
  27. static Transform Perspective(Float perspective);
  28. /*
  29. * Returns a Scale transform.
  30. */
  31. static Transform Scale(Float factorX, Float factorY, Float factorZ);
  32. /*
  33. * Returns a Translate transform.
  34. */
  35. static Transform Translate(Float x, Float y, Float z);
  36. /*
  37. * Returns a Skew transform.
  38. */
  39. static Transform Skew(Float x, Float y);
  40. /*
  41. * Returns a transform that rotates by `angle` radians along the given axis.
  42. */
  43. static Transform RotateX(Float angle);
  44. static Transform RotateY(Float angle);
  45. static Transform RotateZ(Float angle);
  46. static Transform Rotate(Float angleX, Float angleY, Float angleZ);
  47. /*
  48. * Equality operators.
  49. */
  50. bool operator==(Transform const &rhs) const;
  51. bool operator!=(Transform const &rhs) const;
  52. /*
  53. * Matrix subscript.
  54. */
  55. Float &at(int x, int y);
  56. Float const &at(int x, int y) const;
  57. /*
  58. * Concatenates (multiplies) transform matrices.
  59. */
  60. Transform operator*(Transform const &rhs) const;
  61. };
  62. /*
  63. * Applies tranformation to the given point.
  64. */
  65. Point operator*(Point const &point, Transform const &transform);
  66. /*
  67. * Applies tranformation to the given size.
  68. */
  69. Size operator*(Size const &size, Transform const &transform);
  70. /*
  71. * Applies tranformation to the given rect.
  72. * ONLY SUPPORTS scale and translation transformation.
  73. */
  74. Rect operator*(Rect const &rect, Transform const &transform);
  75. Vector operator*(Transform const &transform, Vector const &vector);
  76. } // namespace react
  77. } // namespace facebook
  78. namespace std {
  79. template <>
  80. struct hash<facebook::react::Transform> {
  81. size_t operator()(const facebook::react::Transform &transform) const {
  82. return folly::hash::hash_combine(
  83. 0,
  84. transform.matrix[0],
  85. transform.matrix[1],
  86. transform.matrix[2],
  87. transform.matrix[3],
  88. transform.matrix[4],
  89. transform.matrix[5],
  90. transform.matrix[6],
  91. transform.matrix[7],
  92. transform.matrix[8],
  93. transform.matrix[9],
  94. transform.matrix[10],
  95. transform.matrix[11],
  96. transform.matrix[12],
  97. transform.matrix[13],
  98. transform.matrix[14],
  99. transform.matrix[15]);
  100. }
  101. };
  102. } // namespace std