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

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