Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. AffineTransform::AffineTransform (float m00, float m01, float m02,
  16. float m10, float m11, float m12) noexcept
  17. : mat00 (m00), mat01 (m01), mat02 (m02),
  18. mat10 (m10), mat11 (m11), mat12 (m12)
  19. {
  20. }
  21. bool AffineTransform::operator== (const AffineTransform& other) const noexcept
  22. {
  23. return mat00 == other.mat00
  24. && mat01 == other.mat01
  25. && mat02 == other.mat02
  26. && mat10 == other.mat10
  27. && mat11 == other.mat11
  28. && mat12 == other.mat12;
  29. }
  30. bool AffineTransform::operator!= (const AffineTransform& other) const noexcept
  31. {
  32. return ! operator== (other);
  33. }
  34. //==============================================================================
  35. bool AffineTransform::isIdentity() const noexcept
  36. {
  37. return mat01 == 0.0f
  38. && mat02 == 0.0f
  39. && mat10 == 0.0f
  40. && mat12 == 0.0f
  41. && mat00 == 1.0f
  42. && mat11 == 1.0f;
  43. }
  44. const AffineTransform AffineTransform::identity (1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  45. //==============================================================================
  46. AffineTransform AffineTransform::followedBy (const AffineTransform& other) const noexcept
  47. {
  48. return { other.mat00 * mat00 + other.mat01 * mat10,
  49. other.mat00 * mat01 + other.mat01 * mat11,
  50. other.mat00 * mat02 + other.mat01 * mat12 + other.mat02,
  51. other.mat10 * mat00 + other.mat11 * mat10,
  52. other.mat10 * mat01 + other.mat11 * mat11,
  53. other.mat10 * mat02 + other.mat11 * mat12 + other.mat12 };
  54. }
  55. AffineTransform AffineTransform::translated (float dx, float dy) const noexcept
  56. {
  57. return { mat00, mat01, mat02 + dx,
  58. mat10, mat11, mat12 + dy };
  59. }
  60. AffineTransform AffineTransform::translation (float dx, float dy) noexcept
  61. {
  62. return { 1.0f, 0.0f, dx,
  63. 0.0f, 1.0f, dy };
  64. }
  65. AffineTransform AffineTransform::withAbsoluteTranslation (float tx, float ty) const noexcept
  66. {
  67. return { mat00, mat01, tx,
  68. mat10, mat11, ty };
  69. }
  70. AffineTransform AffineTransform::rotated (float rad) const noexcept
  71. {
  72. auto cosRad = std::cos (rad);
  73. auto sinRad = std::sin (rad);
  74. return { cosRad * mat00 - sinRad * mat10,
  75. cosRad * mat01 - sinRad * mat11,
  76. cosRad * mat02 - sinRad * mat12,
  77. sinRad * mat00 + cosRad * mat10,
  78. sinRad * mat01 + cosRad * mat11,
  79. sinRad * mat02 + cosRad * mat12 };
  80. }
  81. AffineTransform AffineTransform::rotation (float rad) noexcept
  82. {
  83. auto cosRad = std::cos (rad);
  84. auto sinRad = std::sin (rad);
  85. return { cosRad, -sinRad, 0,
  86. sinRad, cosRad, 0 };
  87. }
  88. AffineTransform AffineTransform::rotation (float rad, float pivotX, float pivotY) noexcept
  89. {
  90. auto cosRad = std::cos (rad);
  91. auto sinRad = std::sin (rad);
  92. return { cosRad, -sinRad, -cosRad * pivotX + sinRad * pivotY + pivotX,
  93. sinRad, cosRad, -sinRad * pivotX + -cosRad * pivotY + pivotY };
  94. }
  95. AffineTransform AffineTransform::rotated (float angle, float pivotX, float pivotY) const noexcept
  96. {
  97. return followedBy (rotation (angle, pivotX, pivotY));
  98. }
  99. AffineTransform AffineTransform::scaled (float factorX, float factorY) const noexcept
  100. {
  101. return { factorX * mat00, factorX * mat01, factorX * mat02,
  102. factorY * mat10, factorY * mat11, factorY * mat12 };
  103. }
  104. AffineTransform AffineTransform::scaled (float factor) const noexcept
  105. {
  106. return { factor * mat00, factor * mat01, factor * mat02,
  107. factor * mat10, factor * mat11, factor * mat12 };
  108. }
  109. AffineTransform AffineTransform::scale (float factorX, float factorY) noexcept
  110. {
  111. return { factorX, 0, 0, 0, factorY, 0 };
  112. }
  113. AffineTransform AffineTransform::scale (float factor) noexcept
  114. {
  115. return { factor, 0, 0, 0, factor, 0 };
  116. }
  117. AffineTransform AffineTransform::scaled (float factorX, float factorY,
  118. float pivotX, float pivotY) const noexcept
  119. {
  120. return { factorX * mat00, factorX * mat01, factorX * mat02 + pivotX * (1.0f - factorX),
  121. factorY * mat10, factorY * mat11, factorY * mat12 + pivotY * (1.0f - factorY) };
  122. }
  123. AffineTransform AffineTransform::scale (float factorX, float factorY,
  124. float pivotX, float pivotY) noexcept
  125. {
  126. return { factorX, 0, pivotX * (1.0f - factorX),
  127. 0, factorY, pivotY * (1.0f - factorY) };
  128. }
  129. AffineTransform AffineTransform::shear (float shearX, float shearY) noexcept
  130. {
  131. return { 1.0f, shearX, 0,
  132. shearY, 1.0f, 0 };
  133. }
  134. AffineTransform AffineTransform::sheared (float shearX, float shearY) const noexcept
  135. {
  136. return { mat00 + shearX * mat10,
  137. mat01 + shearX * mat11,
  138. mat02 + shearX * mat12,
  139. mat10 + shearY * mat00,
  140. mat11 + shearY * mat01,
  141. mat12 + shearY * mat02 };
  142. }
  143. AffineTransform AffineTransform::verticalFlip (float height) noexcept
  144. {
  145. return { 1.0f, 0.0f, 0.0f,
  146. 0.0f, -1.0f, height };
  147. }
  148. AffineTransform AffineTransform::inverted() const noexcept
  149. {
  150. double determinant = getDeterminant();
  151. if (! approximatelyEqual (determinant, 0.0))
  152. {
  153. determinant = 1.0 / determinant;
  154. auto dst00 = (float) ( mat11 * determinant);
  155. auto dst10 = (float) (-mat10 * determinant);
  156. auto dst01 = (float) (-mat01 * determinant);
  157. auto dst11 = (float) ( mat00 * determinant);
  158. return { dst00, dst01, -mat02 * dst00 - mat12 * dst01,
  159. dst10, dst11, -mat02 * dst10 - mat12 * dst11 };
  160. }
  161. // singularity..
  162. return *this;
  163. }
  164. bool AffineTransform::isSingularity() const noexcept
  165. {
  166. return (mat00 * mat11 - mat10 * mat01) == 0.0f;
  167. }
  168. AffineTransform AffineTransform::fromTargetPoints (float x00, float y00,
  169. float x10, float y10,
  170. float x01, float y01) noexcept
  171. {
  172. return { x10 - x00, x01 - x00, x00,
  173. y10 - y00, y01 - y00, y00 };
  174. }
  175. AffineTransform AffineTransform::fromTargetPoints (float sx1, float sy1, float tx1, float ty1,
  176. float sx2, float sy2, float tx2, float ty2,
  177. float sx3, float sy3, float tx3, float ty3) noexcept
  178. {
  179. return fromTargetPoints (sx1, sy1, sx2, sy2, sx3, sy3)
  180. .inverted()
  181. .followedBy (fromTargetPoints (tx1, ty1, tx2, ty2, tx3, ty3));
  182. }
  183. bool AffineTransform::isOnlyTranslation() const noexcept
  184. {
  185. return mat01 == 0.0f
  186. && mat10 == 0.0f
  187. && mat00 == 1.0f
  188. && mat11 == 1.0f;
  189. }
  190. float AffineTransform::getDeterminant() const noexcept
  191. {
  192. return (mat00 * mat11) - (mat01 * mat10);
  193. }
  194. float AffineTransform::getScaleFactor() const noexcept
  195. {
  196. return (std::abs (mat00) + std::abs (mat11)) / 2.0f;
  197. }
  198. //==============================================================================
  199. //==============================================================================
  200. #if JUCE_UNIT_TESTS
  201. class AffineTransformTests : public UnitTest
  202. {
  203. public:
  204. AffineTransformTests()
  205. : UnitTest ("AffineTransform", UnitTestCategories::maths)
  206. {}
  207. void runTest() override
  208. {
  209. beginTest ("Determinant");
  210. {
  211. constexpr float scale1 = 1.5f, scale2 = 1.3f;
  212. auto transform = AffineTransform::scale (scale1)
  213. .followedBy (AffineTransform::rotation (degreesToRadians (72.0f)))
  214. .followedBy (AffineTransform::translation (100.0f, 20.0f))
  215. .followedBy (AffineTransform::scale (scale2));
  216. expect (approximatelyEqual (std::sqrt (std::abs (transform.getDeterminant())), scale1 * scale2));
  217. }
  218. }
  219. };
  220. static AffineTransformTests timeTests;
  221. #endif
  222. } // namespace juce