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.h 12KB

7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. Represents a 2D affine-transformation matrix.
  24. An affine transformation is a transformation such as a rotation, scale, shear,
  25. resize or translation.
  26. These are used for various 2D transformation tasks, e.g. with Path objects.
  27. @see Path, Point, Line
  28. */
  29. class JUCE_API AffineTransform
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an identity transform. */
  34. AffineTransform() noexcept;
  35. /** Creates a copy of another transform. */
  36. AffineTransform (const AffineTransform& other) noexcept;
  37. /** Creates a transform from a set of raw matrix values.
  38. The resulting matrix is:
  39. (mat00 mat01 mat02)
  40. (mat10 mat11 mat12)
  41. ( 0 0 1 )
  42. */
  43. AffineTransform (float mat00, float mat01, float mat02,
  44. float mat10, float mat11, float mat12) noexcept;
  45. /** Copies from another AffineTransform object */
  46. AffineTransform& operator= (const AffineTransform& other) noexcept;
  47. /** Compares two transforms. */
  48. bool operator== (const AffineTransform& other) const noexcept;
  49. /** Compares two transforms. */
  50. bool operator!= (const AffineTransform& other) const noexcept;
  51. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  52. /** A ready-to-use identity transform.
  53. Note that you should always avoid using a static variable like this, and
  54. prefer AffineTransform() or {} if you need a default-constructed instance.
  55. */
  56. static const AffineTransform identity;
  57. #endif
  58. //==============================================================================
  59. /** Transforms a 2D coordinate using this matrix. */
  60. template <typename ValueType>
  61. void transformPoint (ValueType& x, ValueType& y) const noexcept
  62. {
  63. const ValueType oldX = x;
  64. x = static_cast<ValueType> (mat00 * oldX + mat01 * y + mat02);
  65. y = static_cast<ValueType> (mat10 * oldX + mat11 * y + mat12);
  66. }
  67. /** Transforms two 2D coordinates using this matrix.
  68. This is just a shortcut for calling transformPoint() on each of these pairs of
  69. coordinates in turn. (And putting all the calculations into one function hopefully
  70. also gives the compiler a bit more scope for pipelining it).
  71. */
  72. template <typename ValueType>
  73. void transformPoints (ValueType& x1, ValueType& y1,
  74. ValueType& x2, ValueType& y2) const noexcept
  75. {
  76. const ValueType oldX1 = x1, oldX2 = x2;
  77. x1 = static_cast<ValueType> (mat00 * oldX1 + mat01 * y1 + mat02);
  78. y1 = static_cast<ValueType> (mat10 * oldX1 + mat11 * y1 + mat12);
  79. x2 = static_cast<ValueType> (mat00 * oldX2 + mat01 * y2 + mat02);
  80. y2 = static_cast<ValueType> (mat10 * oldX2 + mat11 * y2 + mat12);
  81. }
  82. /** Transforms three 2D coordinates using this matrix.
  83. This is just a shortcut for calling transformPoint() on each of these pairs of
  84. coordinates in turn. (And putting all the calculations into one function hopefully
  85. also gives the compiler a bit more scope for pipelining it).
  86. */
  87. template <typename ValueType>
  88. void transformPoints (ValueType& x1, ValueType& y1,
  89. ValueType& x2, ValueType& y2,
  90. ValueType& x3, ValueType& y3) const noexcept
  91. {
  92. const ValueType oldX1 = x1, oldX2 = x2, oldX3 = x3;
  93. x1 = static_cast<ValueType> (mat00 * oldX1 + mat01 * y1 + mat02);
  94. y1 = static_cast<ValueType> (mat10 * oldX1 + mat11 * y1 + mat12);
  95. x2 = static_cast<ValueType> (mat00 * oldX2 + mat01 * y2 + mat02);
  96. y2 = static_cast<ValueType> (mat10 * oldX2 + mat11 * y2 + mat12);
  97. x3 = static_cast<ValueType> (mat00 * oldX3 + mat01 * y3 + mat02);
  98. y3 = static_cast<ValueType> (mat10 * oldX3 + mat11 * y3 + mat12);
  99. }
  100. //==============================================================================
  101. /** Returns a new transform which is the same as this one followed by a translation. */
  102. AffineTransform translated (float deltaX,
  103. float deltaY) const noexcept;
  104. /** Returns a new transform which is the same as this one followed by a translation. */
  105. template <typename PointType>
  106. AffineTransform translated (PointType delta) const noexcept
  107. {
  108. return translated ((float) delta.x, (float) delta.y);
  109. }
  110. /** Returns a new transform which is a translation. */
  111. static AffineTransform translation (float deltaX,
  112. float deltaY) noexcept;
  113. /** Returns a new transform which is a translation. */
  114. template <typename PointType>
  115. static AffineTransform translation (PointType delta) noexcept
  116. {
  117. return translation ((float) delta.x, (float) delta.y);
  118. }
  119. /** Returns a copy of this transform with the specified translation matrix values. */
  120. AffineTransform withAbsoluteTranslation (float translationX,
  121. float translationY) const noexcept;
  122. /** Returns a transform which is the same as this one followed by a rotation.
  123. The rotation is specified by a number of radians to rotate clockwise, centred around
  124. the origin (0, 0).
  125. */
  126. AffineTransform rotated (float angleInRadians) const noexcept;
  127. /** Returns a transform which is the same as this one followed by a rotation about a given point.
  128. The rotation is specified by a number of radians to rotate clockwise, centred around
  129. the coordinates passed in.
  130. */
  131. AffineTransform rotated (float angleInRadians,
  132. float pivotX,
  133. float pivotY) const noexcept;
  134. /** Returns a new transform which is a rotation about (0, 0). */
  135. static AffineTransform rotation (float angleInRadians) noexcept;
  136. /** Returns a new transform which is a rotation about a given point. */
  137. static AffineTransform rotation (float angleInRadians,
  138. float pivotX,
  139. float pivotY) noexcept;
  140. /** Returns a transform which is the same as this one followed by a re-scaling.
  141. The scaling is centred around the origin (0, 0).
  142. */
  143. AffineTransform scaled (float factorX,
  144. float factorY) const noexcept;
  145. /** Returns a transform which is the same as this one followed by a re-scaling.
  146. The scaling is centred around the origin (0, 0).
  147. */
  148. AffineTransform scaled (float factor) const noexcept;
  149. /** Returns a transform which is the same as this one followed by a re-scaling.
  150. The scaling is centred around the origin provided.
  151. */
  152. AffineTransform scaled (float factorX, float factorY,
  153. float pivotX, float pivotY) const noexcept;
  154. /** Returns a new transform which is a re-scale about the origin. */
  155. static AffineTransform scale (float factorX,
  156. float factorY) noexcept;
  157. /** Returns a new transform which is a re-scale about the origin. */
  158. static AffineTransform scale (float factor) noexcept;
  159. /** Returns a new transform which is a re-scale centred around the point provided. */
  160. static AffineTransform scale (float factorX, float factorY,
  161. float pivotX, float pivotY) noexcept;
  162. /** Returns a transform which is the same as this one followed by a shear.
  163. The shear is centred around the origin (0, 0).
  164. */
  165. AffineTransform sheared (float shearX, float shearY) const noexcept;
  166. /** Returns a shear transform, centred around the origin (0, 0). */
  167. static AffineTransform shear (float shearX, float shearY) noexcept;
  168. /** Returns a transform that will flip coordinates vertically within a window of the given height.
  169. This is handy for converting between upside-down coordinate systems such as OpenGL or CoreGraphics.
  170. */
  171. static AffineTransform verticalFlip (float height) noexcept;
  172. /** Returns a matrix which is the inverse operation of this one.
  173. Some matrices don't have an inverse - in this case, the method will just return
  174. an identity transform.
  175. */
  176. AffineTransform inverted() const noexcept;
  177. /** Returns the transform that will map three known points onto three coordinates
  178. that are supplied.
  179. This returns the transform that will transform (0, 0) into (x00, y00),
  180. (1, 0) to (x10, y10), and (0, 1) to (x01, y01).
  181. */
  182. static AffineTransform fromTargetPoints (float x00, float y00,
  183. float x10, float y10,
  184. float x01, float y01) noexcept;
  185. /** Returns the transform that will map three specified points onto three target points. */
  186. static AffineTransform fromTargetPoints (float sourceX1, float sourceY1, float targetX1, float targetY1,
  187. float sourceX2, float sourceY2, float targetX2, float targetY2,
  188. float sourceX3, float sourceY3, float targetX3, float targetY3) noexcept;
  189. //==============================================================================
  190. /** Returns the result of concatenating another transformation after this one. */
  191. AffineTransform followedBy (const AffineTransform& other) const noexcept;
  192. /** Returns true if this transform has no effect on points. */
  193. bool isIdentity() const noexcept;
  194. /** Returns true if this transform maps to a singularity - i.e. if it has no inverse. */
  195. bool isSingularity() const noexcept;
  196. /** Returns true if the transform only translates, and doesn't scale or rotate the
  197. points. */
  198. bool isOnlyTranslation() const noexcept;
  199. /** If this transform is only a translation, this returns the X offset.
  200. @see isOnlyTranslation
  201. */
  202. float getTranslationX() const noexcept { return mat02; }
  203. /** If this transform is only a translation, this returns the X offset.
  204. @see isOnlyTranslation
  205. */
  206. float getTranslationY() const noexcept { return mat12; }
  207. /** Returns the approximate scale factor by which lengths will be transformed.
  208. Obviously a length may be scaled by entirely different amounts depending on its
  209. direction, so this is only appropriate as a rough guide.
  210. */
  211. float getScaleFactor() const noexcept;
  212. //==============================================================================
  213. /* The transform matrix is:
  214. (mat00 mat01 mat02)
  215. (mat10 mat11 mat12)
  216. ( 0 0 1 )
  217. */
  218. float mat00, mat01, mat02;
  219. float mat10, mat11, mat12;
  220. };
  221. } // namespace juce