The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

269 lines
8.3KB

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