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.

254 lines
9.1KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. AffineTransform::AffineTransform() noexcept
  21. : mat00 (1.0f), mat01 (0), mat02 (0),
  22. mat10 (0), mat11 (1.0f), mat12 (0)
  23. {
  24. }
  25. AffineTransform::AffineTransform (const AffineTransform& other) noexcept
  26. : mat00 (other.mat00), mat01 (other.mat01), mat02 (other.mat02),
  27. mat10 (other.mat10), mat11 (other.mat11), mat12 (other.mat12)
  28. {
  29. }
  30. AffineTransform::AffineTransform (const float mat00_, const float mat01_, const float mat02_,
  31. const float mat10_, const float mat11_, const float mat12_) noexcept
  32. : mat00 (mat00_), mat01 (mat01_), mat02 (mat02_),
  33. mat10 (mat10_), mat11 (mat11_), mat12 (mat12_)
  34. {
  35. }
  36. AffineTransform& AffineTransform::operator= (const AffineTransform& other) noexcept
  37. {
  38. mat00 = other.mat00;
  39. mat01 = other.mat01;
  40. mat02 = other.mat02;
  41. mat10 = other.mat10;
  42. mat11 = other.mat11;
  43. mat12 = other.mat12;
  44. return *this;
  45. }
  46. bool AffineTransform::operator== (const AffineTransform& other) const noexcept
  47. {
  48. return mat00 == other.mat00
  49. && mat01 == other.mat01
  50. && mat02 == other.mat02
  51. && mat10 == other.mat10
  52. && mat11 == other.mat11
  53. && mat12 == other.mat12;
  54. }
  55. bool AffineTransform::operator!= (const AffineTransform& other) const noexcept
  56. {
  57. return ! operator== (other);
  58. }
  59. //==============================================================================
  60. bool AffineTransform::isIdentity() const noexcept
  61. {
  62. return (mat01 == 0)
  63. && (mat02 == 0)
  64. && (mat10 == 0)
  65. && (mat12 == 0)
  66. && (mat00 == 1.0f)
  67. && (mat11 == 1.0f);
  68. }
  69. const AffineTransform AffineTransform::identity;
  70. //==============================================================================
  71. AffineTransform AffineTransform::followedBy (const AffineTransform& other) const noexcept
  72. {
  73. return AffineTransform (other.mat00 * mat00 + other.mat01 * mat10,
  74. other.mat00 * mat01 + other.mat01 * mat11,
  75. other.mat00 * mat02 + other.mat01 * mat12 + other.mat02,
  76. other.mat10 * mat00 + other.mat11 * mat10,
  77. other.mat10 * mat01 + other.mat11 * mat11,
  78. other.mat10 * mat02 + other.mat11 * mat12 + other.mat12);
  79. }
  80. AffineTransform AffineTransform::translated (const float dx, const float dy) const noexcept
  81. {
  82. return AffineTransform (mat00, mat01, mat02 + dx,
  83. mat10, mat11, mat12 + dy);
  84. }
  85. AffineTransform AffineTransform::translation (const float dx, const float dy) noexcept
  86. {
  87. return AffineTransform (1.0f, 0, dx,
  88. 0, 1.0f, dy);
  89. }
  90. AffineTransform AffineTransform::rotated (const float rad) const noexcept
  91. {
  92. const float cosRad = std::cos (rad);
  93. const float sinRad = std::sin (rad);
  94. return AffineTransform (cosRad * mat00 + -sinRad * mat10,
  95. cosRad * mat01 + -sinRad * mat11,
  96. cosRad * mat02 + -sinRad * mat12,
  97. sinRad * mat00 + cosRad * mat10,
  98. sinRad * mat01 + cosRad * mat11,
  99. sinRad * mat02 + cosRad * mat12);
  100. }
  101. AffineTransform AffineTransform::rotation (const float rad) noexcept
  102. {
  103. const float cosRad = std::cos (rad);
  104. const float sinRad = std::sin (rad);
  105. return AffineTransform (cosRad, -sinRad, 0,
  106. sinRad, cosRad, 0);
  107. }
  108. AffineTransform AffineTransform::rotation (const float rad, const float pivotX, const float pivotY) noexcept
  109. {
  110. const float cosRad = std::cos (rad);
  111. const float sinRad = std::sin (rad);
  112. return AffineTransform (cosRad, -sinRad, -cosRad * pivotX + sinRad * pivotY + pivotX,
  113. sinRad, cosRad, -sinRad * pivotX + -cosRad * pivotY + pivotY);
  114. }
  115. AffineTransform AffineTransform::rotated (const float angle, const float pivotX, const float pivotY) const noexcept
  116. {
  117. return followedBy (rotation (angle, pivotX, pivotY));
  118. }
  119. AffineTransform AffineTransform::scaled (const float factorX, const float factorY) const noexcept
  120. {
  121. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02,
  122. factorY * mat10, factorY * mat11, factorY * mat12);
  123. }
  124. AffineTransform AffineTransform::scale (const float factorX, const float factorY) noexcept
  125. {
  126. return AffineTransform (factorX, 0, 0,
  127. 0, factorY, 0);
  128. }
  129. AffineTransform AffineTransform::scaled (const float factorX, const float factorY,
  130. const float pivotX, const float pivotY) const noexcept
  131. {
  132. return AffineTransform (factorX * mat00, factorX * mat01, factorX * mat02 + pivotX * (1.0f - factorX),
  133. factorY * mat10, factorY * mat11, factorY * mat12 + pivotY * (1.0f - factorY));
  134. }
  135. AffineTransform AffineTransform::scale (const float factorX, const float factorY,
  136. const float pivotX, const float pivotY) noexcept
  137. {
  138. return AffineTransform (factorX, 0, pivotX * (1.0f - factorX),
  139. 0, factorY, pivotY * (1.0f - factorY));
  140. }
  141. AffineTransform AffineTransform::shear (float shearX, float shearY) noexcept
  142. {
  143. return AffineTransform (1.0f, shearX, 0,
  144. shearY, 1.0f, 0);
  145. }
  146. AffineTransform AffineTransform::sheared (const float shearX, const float shearY) const noexcept
  147. {
  148. return AffineTransform (mat00 + shearX * mat10,
  149. mat01 + shearX * mat11,
  150. mat02 + shearX * mat12,
  151. shearY * mat00 + mat10,
  152. shearY * mat01 + mat11,
  153. shearY * mat02 + mat12);
  154. }
  155. AffineTransform AffineTransform::verticalFlip (const float height) noexcept
  156. {
  157. return AffineTransform (1.0f, 0, 0, 0, -1.0f, height);
  158. }
  159. AffineTransform AffineTransform::inverted() const noexcept
  160. {
  161. double determinant = (mat00 * mat11 - mat10 * mat01);
  162. if (determinant != 0.0)
  163. {
  164. determinant = 1.0 / determinant;
  165. const float dst00 = (float) (mat11 * determinant);
  166. const float dst10 = (float) (-mat10 * determinant);
  167. const float dst01 = (float) (-mat01 * determinant);
  168. const float dst11 = (float) (mat00 * determinant);
  169. return AffineTransform (dst00, dst01, -mat02 * dst00 - mat12 * dst01,
  170. dst10, dst11, -mat02 * dst10 - mat12 * dst11);
  171. }
  172. else
  173. {
  174. // singularity..
  175. return *this;
  176. }
  177. }
  178. bool AffineTransform::isSingularity() const noexcept
  179. {
  180. return (mat00 * mat11 - mat10 * mat01) == 0;
  181. }
  182. AffineTransform AffineTransform::fromTargetPoints (const float x00, const float y00,
  183. const float x10, const float y10,
  184. const float x01, const float y01) noexcept
  185. {
  186. return AffineTransform (x10 - x00, x01 - x00, x00,
  187. y10 - y00, y01 - y00, y00);
  188. }
  189. AffineTransform AffineTransform::fromTargetPoints (const float sx1, const float sy1, const float tx1, const float ty1,
  190. const float sx2, const float sy2, const float tx2, const float ty2,
  191. const float sx3, const float sy3, const float tx3, const float ty3) noexcept
  192. {
  193. return fromTargetPoints (sx1, sy1, sx2, sy2, sx3, sy3)
  194. .inverted()
  195. .followedBy (fromTargetPoints (tx1, ty1, tx2, ty2, tx3, ty3));
  196. }
  197. bool AffineTransform::isOnlyTranslation() const noexcept
  198. {
  199. return (mat01 == 0)
  200. && (mat10 == 0)
  201. && (mat00 == 1.0f)
  202. && (mat11 == 1.0f);
  203. }
  204. float AffineTransform::getScaleFactor() const noexcept
  205. {
  206. return juce_hypot (mat00 + mat01, mat10 + mat11);
  207. }
  208. END_JUCE_NAMESPACE