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.

258 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. AffineTransform::AffineTransform() noexcept
  19. : mat00 (1.0f), mat01 (0), mat02 (0),
  20. mat10 (0), mat11 (1.0f), mat12 (0)
  21. {
  22. }
  23. AffineTransform::AffineTransform (const AffineTransform& other) noexcept
  24. : mat00 (other.mat00), mat01 (other.mat01), mat02 (other.mat02),
  25. mat10 (other.mat10), mat11 (other.mat11), mat12 (other.mat12)
  26. {
  27. }
  28. AffineTransform::AffineTransform (const float m00, const float m01, const float m02,
  29. const float m10, const float m11, const float m12) noexcept
  30. : mat00 (m00), mat01 (m01), mat02 (m02),
  31. mat10 (m10), mat11 (m11), mat12 (m12)
  32. {
  33. }
  34. AffineTransform& AffineTransform::operator= (const AffineTransform& other) noexcept
  35. {
  36. mat00 = other.mat00;
  37. mat01 = other.mat01;
  38. mat02 = other.mat02;
  39. mat10 = other.mat10;
  40. mat11 = other.mat11;
  41. mat12 = other.mat12;
  42. return *this;
  43. }
  44. bool AffineTransform::operator== (const AffineTransform& other) const noexcept
  45. {
  46. return mat00 == other.mat00
  47. && mat01 == other.mat01
  48. && mat02 == other.mat02
  49. && mat10 == other.mat10
  50. && mat11 == other.mat11
  51. && mat12 == other.mat12;
  52. }
  53. bool AffineTransform::operator!= (const AffineTransform& other) const noexcept
  54. {
  55. return ! operator== (other);
  56. }
  57. //==============================================================================
  58. bool AffineTransform::isIdentity() const noexcept
  59. {
  60. return (mat01 == 0)
  61. && (mat02 == 0)
  62. && (mat10 == 0)
  63. && (mat12 == 0)
  64. && (mat00 == 1.0f)
  65. && (mat11 == 1.0f);
  66. }
  67. const AffineTransform AffineTransform::identity;
  68. //==============================================================================
  69. AffineTransform AffineTransform::followedBy (const AffineTransform& other) const noexcept
  70. {
  71. return AffineTransform (other.mat00 * mat00 + other.mat01 * mat10,
  72. other.mat00 * mat01 + other.mat01 * mat11,
  73. other.mat00 * mat02 + other.mat01 * mat12 + other.mat02,
  74. other.mat10 * mat00 + other.mat11 * mat10,
  75. other.mat10 * mat01 + other.mat11 * mat11,
  76. other.mat10 * mat02 + other.mat11 * mat12 + other.mat12);
  77. }
  78. AffineTransform AffineTransform::translated (const float dx, const float dy) const noexcept
  79. {
  80. return AffineTransform (mat00, mat01, mat02 + dx,
  81. mat10, mat11, mat12 + dy);
  82. }
  83. AffineTransform AffineTransform::translation (const float dx, const float dy) noexcept
  84. {
  85. return AffineTransform (1.0f, 0, dx,
  86. 0, 1.0f, dy);
  87. }
  88. AffineTransform AffineTransform::withAbsoluteTranslation (const float tx, const float ty) const noexcept
  89. {
  90. return AffineTransform (mat00, mat01, tx,
  91. mat10, mat11, ty);
  92. }
  93. AffineTransform AffineTransform::rotated (const float rad) const noexcept
  94. {
  95. const float cosRad = std::cos (rad);
  96. const float sinRad = std::sin (rad);
  97. return AffineTransform (cosRad * mat00 + -sinRad * mat10,
  98. cosRad * mat01 + -sinRad * mat11,
  99. cosRad * mat02 + -sinRad * mat12,
  100. sinRad * mat00 + cosRad * mat10,
  101. sinRad * mat01 + cosRad * mat11,
  102. sinRad * mat02 + cosRad * mat12);
  103. }
  104. AffineTransform AffineTransform::rotation (const float rad) noexcept
  105. {
  106. const float cosRad = std::cos (rad);
  107. const float sinRad = std::sin (rad);
  108. return AffineTransform (cosRad, -sinRad, 0,
  109. sinRad, cosRad, 0);
  110. }
  111. AffineTransform AffineTransform::rotation (const float rad, const float pivotX, const float pivotY) noexcept
  112. {
  113. const float cosRad = std::cos (rad);
  114. const float sinRad = std::sin (rad);
  115. return AffineTransform (cosRad, -sinRad, -cosRad * pivotX + sinRad * pivotY + pivotX,
  116. sinRad, cosRad, -sinRad * pivotX + -cosRad * pivotY + pivotY);
  117. }
  118. AffineTransform AffineTransform::rotated (const float angle, const float pivotX, const float pivotY) const noexcept
  119. {
  120. return followedBy (rotation (angle, pivotX, pivotY));
  121. }
  122. AffineTransform AffineTransform::scaled (const float factorX, const float factorY) const noexcept
  123. {
  124. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02,
  125. factorY * mat10, factorY * mat11, factorY * mat12);
  126. }
  127. AffineTransform AffineTransform::scale (const float factorX, const float factorY) noexcept
  128. {
  129. return AffineTransform (factorX, 0, 0, 0, factorY, 0);
  130. }
  131. AffineTransform AffineTransform::scale (const float factor) noexcept
  132. {
  133. return AffineTransform (factor, 0, 0, 0, factor, 0);
  134. }
  135. AffineTransform AffineTransform::scaled (const float factorX, const float factorY,
  136. const float pivotX, const float pivotY) const noexcept
  137. {
  138. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02 + pivotX * (1.0f - factorX),
  139. factorY * mat10, factorY * mat11, factorY * mat12 + pivotY * (1.0f - factorY));
  140. }
  141. AffineTransform AffineTransform::scale (const float factorX, const float factorY,
  142. const float pivotX, const float pivotY) noexcept
  143. {
  144. return AffineTransform (factorX, 0, pivotX * (1.0f - factorX),
  145. 0, factorY, pivotY * (1.0f - factorY));
  146. }
  147. AffineTransform AffineTransform::shear (float shearX, float shearY) noexcept
  148. {
  149. return AffineTransform (1.0f, shearX, 0,
  150. shearY, 1.0f, 0);
  151. }
  152. AffineTransform AffineTransform::sheared (const float shearX, const float shearY) const noexcept
  153. {
  154. return AffineTransform (mat00 + shearX * mat10,
  155. mat01 + shearX * mat11,
  156. mat02 + shearX * mat12,
  157. shearY * mat00 + mat10,
  158. shearY * mat01 + mat11,
  159. shearY * mat02 + mat12);
  160. }
  161. AffineTransform AffineTransform::verticalFlip (const float height) noexcept
  162. {
  163. return AffineTransform (1.0f, 0, 0, 0, -1.0f, height);
  164. }
  165. AffineTransform AffineTransform::inverted() const noexcept
  166. {
  167. double determinant = (mat00 * mat11 - mat10 * mat01);
  168. if (determinant != 0.0)
  169. {
  170. determinant = 1.0 / determinant;
  171. const float dst00 = (float) (mat11 * determinant);
  172. const float dst10 = (float) (-mat10 * determinant);
  173. const float dst01 = (float) (-mat01 * determinant);
  174. const float dst11 = (float) (mat00 * determinant);
  175. return AffineTransform (dst00, dst01, -mat02 * dst00 - mat12 * dst01,
  176. dst10, dst11, -mat02 * dst10 - mat12 * dst11);
  177. }
  178. else
  179. {
  180. // singularity..
  181. return *this;
  182. }
  183. }
  184. bool AffineTransform::isSingularity() const noexcept
  185. {
  186. return (mat00 * mat11 - mat10 * mat01) == 0;
  187. }
  188. AffineTransform AffineTransform::fromTargetPoints (const float x00, const float y00,
  189. const float x10, const float y10,
  190. const float x01, const float y01) noexcept
  191. {
  192. return AffineTransform (x10 - x00, x01 - x00, x00,
  193. y10 - y00, y01 - y00, y00);
  194. }
  195. AffineTransform AffineTransform::fromTargetPoints (const float sx1, const float sy1, const float tx1, const float ty1,
  196. const float sx2, const float sy2, const float tx2, const float ty2,
  197. const float sx3, const float sy3, const float tx3, const float ty3) noexcept
  198. {
  199. return fromTargetPoints (sx1, sy1, sx2, sy2, sx3, sy3)
  200. .inverted()
  201. .followedBy (fromTargetPoints (tx1, ty1, tx2, ty2, tx3, ty3));
  202. }
  203. bool AffineTransform::isOnlyTranslation() const noexcept
  204. {
  205. return (mat01 == 0)
  206. && (mat10 == 0)
  207. && (mat00 == 1.0f)
  208. && (mat11 == 1.0f);
  209. }
  210. float AffineTransform::getScaleFactor() const noexcept
  211. {
  212. return juce_hypot (mat00 + mat01, mat10 + mat11);
  213. }