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.

265 lines
9.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. AffineTransform::AffineTransform() noexcept
  20. : mat00 (1.0f), mat01 (0), mat02 (0),
  21. mat10 (0), mat11 (1.0f), mat12 (0)
  22. {
  23. }
  24. AffineTransform::AffineTransform (const AffineTransform& other) noexcept
  25. : mat00 (other.mat00), mat01 (other.mat01), mat02 (other.mat02),
  26. mat10 (other.mat10), mat11 (other.mat11), mat12 (other.mat12)
  27. {
  28. }
  29. AffineTransform::AffineTransform (const float m00, const float m01, const float m02,
  30. const float m10, const float m11, const float m12) noexcept
  31. : mat00 (m00), mat01 (m01), mat02 (m02),
  32. mat10 (m10), mat11 (m11), mat12 (m12)
  33. {
  34. }
  35. AffineTransform& AffineTransform::operator= (const AffineTransform& other) noexcept
  36. {
  37. mat00 = other.mat00;
  38. mat01 = other.mat01;
  39. mat02 = other.mat02;
  40. mat10 = other.mat10;
  41. mat11 = other.mat11;
  42. mat12 = other.mat12;
  43. return *this;
  44. }
  45. bool AffineTransform::operator== (const AffineTransform& other) const noexcept
  46. {
  47. return mat00 == other.mat00
  48. && mat01 == other.mat01
  49. && mat02 == other.mat02
  50. && mat10 == other.mat10
  51. && mat11 == other.mat11
  52. && mat12 == other.mat12;
  53. }
  54. bool AffineTransform::operator!= (const AffineTransform& other) const noexcept
  55. {
  56. return ! operator== (other);
  57. }
  58. //==============================================================================
  59. bool AffineTransform::isIdentity() const noexcept
  60. {
  61. return (mat01 == 0.0f)
  62. && (mat02 == 0.0f)
  63. && (mat10 == 0.0f)
  64. && (mat12 == 0.0f)
  65. && (mat00 == 1.0f)
  66. && (mat11 == 1.0f);
  67. }
  68. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  69. const AffineTransform AffineTransform::identity;
  70. #endif
  71. //==============================================================================
  72. AffineTransform AffineTransform::followedBy (const AffineTransform& other) const noexcept
  73. {
  74. return AffineTransform (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 (const float dx, const float dy) const noexcept
  82. {
  83. return AffineTransform (mat00, mat01, mat02 + dx,
  84. mat10, mat11, mat12 + dy);
  85. }
  86. AffineTransform AffineTransform::translation (const float dx, const float dy) noexcept
  87. {
  88. return AffineTransform (1.0f, 0, dx,
  89. 0, 1.0f, dy);
  90. }
  91. AffineTransform AffineTransform::withAbsoluteTranslation (const float tx, const float ty) const noexcept
  92. {
  93. return AffineTransform (mat00, mat01, tx,
  94. mat10, mat11, ty);
  95. }
  96. AffineTransform AffineTransform::rotated (const float rad) const noexcept
  97. {
  98. const float cosRad = std::cos (rad);
  99. const float sinRad = std::sin (rad);
  100. return AffineTransform (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 (const float rad) noexcept
  108. {
  109. const float cosRad = std::cos (rad);
  110. const float sinRad = std::sin (rad);
  111. return AffineTransform (cosRad, -sinRad, 0,
  112. sinRad, cosRad, 0);
  113. }
  114. AffineTransform AffineTransform::rotation (const float rad, const float pivotX, const float pivotY) noexcept
  115. {
  116. const float cosRad = std::cos (rad);
  117. const float sinRad = std::sin (rad);
  118. return AffineTransform (cosRad, -sinRad, -cosRad * pivotX + sinRad * pivotY + pivotX,
  119. sinRad, cosRad, -sinRad * pivotX + -cosRad * pivotY + pivotY);
  120. }
  121. AffineTransform AffineTransform::rotated (const float angle, const float pivotX, const float pivotY) const noexcept
  122. {
  123. return followedBy (rotation (angle, pivotX, pivotY));
  124. }
  125. AffineTransform AffineTransform::scaled (const float factorX, const float factorY) const noexcept
  126. {
  127. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02,
  128. factorY * mat10, factorY * mat11, factorY * mat12);
  129. }
  130. AffineTransform AffineTransform::scaled (const float factor) const noexcept
  131. {
  132. return AffineTransform (factor * mat00, factor * mat01, factor * mat02,
  133. factor * mat10, factor * mat11, factor * mat12);
  134. }
  135. AffineTransform AffineTransform::scale (const float factorX, const float factorY) noexcept
  136. {
  137. return AffineTransform (factorX, 0, 0, 0, factorY, 0);
  138. }
  139. AffineTransform AffineTransform::scale (const float factor) noexcept
  140. {
  141. return AffineTransform (factor, 0, 0, 0, factor, 0);
  142. }
  143. AffineTransform AffineTransform::scaled (const float factorX, const float factorY,
  144. const float pivotX, const float pivotY) const noexcept
  145. {
  146. return AffineTransform (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 (const float factorX, const float factorY,
  150. const float pivotX, const float pivotY) noexcept
  151. {
  152. return AffineTransform (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 AffineTransform (1.0f, shearX, 0,
  158. shearY, 1.0f, 0);
  159. }
  160. AffineTransform AffineTransform::sheared (const float shearX, const float shearY) const noexcept
  161. {
  162. return AffineTransform (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 (const float height) noexcept
  170. {
  171. return AffineTransform (1.0f, 0, 0, 0, -1.0f, height);
  172. }
  173. AffineTransform AffineTransform::inverted() const noexcept
  174. {
  175. double determinant = (mat00 * mat11 - mat10 * mat01);
  176. if (determinant != 0.0)
  177. {
  178. determinant = 1.0 / determinant;
  179. const float dst00 = (float) ( mat11 * determinant);
  180. const float dst10 = (float) (-mat10 * determinant);
  181. const float dst01 = (float) (-mat01 * determinant);
  182. const float dst11 = (float) ( mat00 * determinant);
  183. return AffineTransform (dst00, dst01, -mat02 * dst00 - mat12 * dst01,
  184. dst10, dst11, -mat02 * dst10 - mat12 * dst11);
  185. }
  186. // singularity..
  187. return *this;
  188. }
  189. bool AffineTransform::isSingularity() const noexcept
  190. {
  191. return (mat00 * mat11 - mat10 * mat01) == 0.0f;
  192. }
  193. AffineTransform AffineTransform::fromTargetPoints (const float x00, const float y00,
  194. const float x10, const float y10,
  195. const float x01, const float y01) noexcept
  196. {
  197. return AffineTransform (x10 - x00, x01 - x00, x00,
  198. y10 - y00, y01 - y00, y00);
  199. }
  200. AffineTransform AffineTransform::fromTargetPoints (const float sx1, const float sy1, const float tx1, const float ty1,
  201. const float sx2, const float sy2, const float tx2, const float ty2,
  202. const float sx3, const float sy3, const float tx3, const float ty3) noexcept
  203. {
  204. return fromTargetPoints (sx1, sy1, sx2, sy2, sx3, sy3)
  205. .inverted()
  206. .followedBy (fromTargetPoints (tx1, ty1, tx2, ty2, tx3, ty3));
  207. }
  208. bool AffineTransform::isOnlyTranslation() const noexcept
  209. {
  210. return (mat01 == 0.0f)
  211. && (mat10 == 0.0f)
  212. && (mat00 == 1.0f)
  213. && (mat11 == 1.0f);
  214. }
  215. float AffineTransform::getScaleFactor() const noexcept
  216. {
  217. return (std::abs (mat00) + std::abs (mat11)) / 2.0f;
  218. }