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.

248 lines
9.0KB

  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 mat00_, const float mat01_, const float mat02_,
  29. const float mat10_, const float mat11_, const float mat12_) noexcept
  30. : mat00 (mat00_), mat01 (mat01_), mat02 (mat02_),
  31. mat10 (mat10_), mat11 (mat11_), mat12 (mat12_)
  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::rotated (const float rad) const noexcept
  89. {
  90. const float cosRad = std::cos (rad);
  91. const float sinRad = std::sin (rad);
  92. return AffineTransform (cosRad * mat00 + -sinRad * mat10,
  93. cosRad * mat01 + -sinRad * mat11,
  94. cosRad * mat02 + -sinRad * mat12,
  95. sinRad * mat00 + cosRad * mat10,
  96. sinRad * mat01 + cosRad * mat11,
  97. sinRad * mat02 + cosRad * mat12);
  98. }
  99. AffineTransform AffineTransform::rotation (const float rad) noexcept
  100. {
  101. const float cosRad = std::cos (rad);
  102. const float sinRad = std::sin (rad);
  103. return AffineTransform (cosRad, -sinRad, 0,
  104. sinRad, cosRad, 0);
  105. }
  106. AffineTransform AffineTransform::rotation (const float rad, const float pivotX, const float pivotY) noexcept
  107. {
  108. const float cosRad = std::cos (rad);
  109. const float sinRad = std::sin (rad);
  110. return AffineTransform (cosRad, -sinRad, -cosRad * pivotX + sinRad * pivotY + pivotX,
  111. sinRad, cosRad, -sinRad * pivotX + -cosRad * pivotY + pivotY);
  112. }
  113. AffineTransform AffineTransform::rotated (const float angle, const float pivotX, const float pivotY) const noexcept
  114. {
  115. return followedBy (rotation (angle, pivotX, pivotY));
  116. }
  117. AffineTransform AffineTransform::scaled (const float factorX, const float factorY) const noexcept
  118. {
  119. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02,
  120. factorY * mat10, factorY * mat11, factorY * mat12);
  121. }
  122. AffineTransform AffineTransform::scale (const float factorX, const float factorY) noexcept
  123. {
  124. return AffineTransform (factorX, 0, 0,
  125. 0, factorY, 0);
  126. }
  127. AffineTransform AffineTransform::scaled (const float factorX, const float factorY,
  128. const float pivotX, const float pivotY) const noexcept
  129. {
  130. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02 + pivotX * (1.0f - factorX),
  131. factorY * mat10, factorY * mat11, factorY * mat12 + pivotY * (1.0f - factorY));
  132. }
  133. AffineTransform AffineTransform::scale (const float factorX, const float factorY,
  134. const float pivotX, const float pivotY) noexcept
  135. {
  136. return AffineTransform (factorX, 0, pivotX * (1.0f - factorX),
  137. 0, factorY, pivotY * (1.0f - factorY));
  138. }
  139. AffineTransform AffineTransform::shear (float shearX, float shearY) noexcept
  140. {
  141. return AffineTransform (1.0f, shearX, 0,
  142. shearY, 1.0f, 0);
  143. }
  144. AffineTransform AffineTransform::sheared (const float shearX, const float shearY) const noexcept
  145. {
  146. return AffineTransform (mat00 + shearX * mat10,
  147. mat01 + shearX * mat11,
  148. mat02 + shearX * mat12,
  149. shearY * mat00 + mat10,
  150. shearY * mat01 + mat11,
  151. shearY * mat02 + mat12);
  152. }
  153. AffineTransform AffineTransform::verticalFlip (const float height) noexcept
  154. {
  155. return AffineTransform (1.0f, 0, 0, 0, -1.0f, height);
  156. }
  157. AffineTransform AffineTransform::inverted() const noexcept
  158. {
  159. double determinant = (mat00 * mat11 - mat10 * mat01);
  160. if (determinant != 0.0)
  161. {
  162. determinant = 1.0 / determinant;
  163. const float dst00 = (float) (mat11 * determinant);
  164. const float dst10 = (float) (-mat10 * determinant);
  165. const float dst01 = (float) (-mat01 * determinant);
  166. const float dst11 = (float) (mat00 * determinant);
  167. return AffineTransform (dst00, dst01, -mat02 * dst00 - mat12 * dst01,
  168. dst10, dst11, -mat02 * dst10 - mat12 * dst11);
  169. }
  170. else
  171. {
  172. // singularity..
  173. return *this;
  174. }
  175. }
  176. bool AffineTransform::isSingularity() const noexcept
  177. {
  178. return (mat00 * mat11 - mat10 * mat01) == 0;
  179. }
  180. AffineTransform AffineTransform::fromTargetPoints (const float x00, const float y00,
  181. const float x10, const float y10,
  182. const float x01, const float y01) noexcept
  183. {
  184. return AffineTransform (x10 - x00, x01 - x00, x00,
  185. y10 - y00, y01 - y00, y00);
  186. }
  187. AffineTransform AffineTransform::fromTargetPoints (const float sx1, const float sy1, const float tx1, const float ty1,
  188. const float sx2, const float sy2, const float tx2, const float ty2,
  189. const float sx3, const float sy3, const float tx3, const float ty3) noexcept
  190. {
  191. return fromTargetPoints (sx1, sy1, sx2, sy2, sx3, sy3)
  192. .inverted()
  193. .followedBy (fromTargetPoints (tx1, ty1, tx2, ty2, tx3, ty3));
  194. }
  195. bool AffineTransform::isOnlyTranslation() const noexcept
  196. {
  197. return (mat01 == 0)
  198. && (mat10 == 0)
  199. && (mat00 == 1.0f)
  200. && (mat11 == 1.0f);
  201. }
  202. float AffineTransform::getScaleFactor() const noexcept
  203. {
  204. return juce_hypot (mat00 + mat01, mat10 + mat11);
  205. }