Audio plugin host https://kx.studio/carla
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.

juce_AffineTransform.cpp 9.3KB

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