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_PathIterator.cpp 8.7KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #if JUCE_MSVC && JUCE_DEBUG
  18. #pragma optimize ("t", on)
  19. #endif
  20. const float PathFlatteningIterator::defaultTolerance = 0.6f;
  21. //==============================================================================
  22. PathFlatteningIterator::PathFlatteningIterator (const Path& path_,
  23. const AffineTransform& transform_,
  24. const float tolerance)
  25. : x2 (0),
  26. y2 (0),
  27. closesSubPath (false),
  28. subPathIndex (-1),
  29. path (path_),
  30. transform (transform_),
  31. points (path_.data.elements),
  32. toleranceSquared (tolerance * tolerance),
  33. subPathCloseX (0),
  34. subPathCloseY (0),
  35. isIdentityTransform (transform_.isIdentity()),
  36. stackBase (32),
  37. index (0),
  38. stackSize (32)
  39. {
  40. stackPos = stackBase;
  41. }
  42. PathFlatteningIterator::~PathFlatteningIterator()
  43. {
  44. }
  45. bool PathFlatteningIterator::isLastInSubpath() const noexcept
  46. {
  47. return stackPos == stackBase.getData()
  48. && (index >= path.numElements || points [index] == Path::moveMarker);
  49. }
  50. bool PathFlatteningIterator::next()
  51. {
  52. x1 = x2;
  53. y1 = y2;
  54. float x3 = 0;
  55. float y3 = 0;
  56. float x4 = 0;
  57. float y4 = 0;
  58. for (;;)
  59. {
  60. float type;
  61. if (stackPos == stackBase)
  62. {
  63. if (index >= path.numElements)
  64. return false;
  65. type = points [index++];
  66. if (type != Path::closeSubPathMarker)
  67. {
  68. x2 = points [index++];
  69. y2 = points [index++];
  70. if (type == Path::quadMarker)
  71. {
  72. x3 = points [index++];
  73. y3 = points [index++];
  74. if (! isIdentityTransform)
  75. transform.transformPoints (x2, y2, x3, y3);
  76. }
  77. else if (type == Path::cubicMarker)
  78. {
  79. x3 = points [index++];
  80. y3 = points [index++];
  81. x4 = points [index++];
  82. y4 = points [index++];
  83. if (! isIdentityTransform)
  84. transform.transformPoints (x2, y2, x3, y3, x4, y4);
  85. }
  86. else
  87. {
  88. if (! isIdentityTransform)
  89. transform.transformPoint (x2, y2);
  90. }
  91. }
  92. }
  93. else
  94. {
  95. type = *--stackPos;
  96. if (type != Path::closeSubPathMarker)
  97. {
  98. x2 = *--stackPos;
  99. y2 = *--stackPos;
  100. if (type == Path::quadMarker)
  101. {
  102. x3 = *--stackPos;
  103. y3 = *--stackPos;
  104. }
  105. else if (type == Path::cubicMarker)
  106. {
  107. x3 = *--stackPos;
  108. y3 = *--stackPos;
  109. x4 = *--stackPos;
  110. y4 = *--stackPos;
  111. }
  112. }
  113. }
  114. if (type == Path::lineMarker)
  115. {
  116. ++subPathIndex;
  117. closesSubPath = (stackPos == stackBase)
  118. && (index < path.numElements)
  119. && (points [index] == Path::closeSubPathMarker)
  120. && x2 == subPathCloseX
  121. && y2 == subPathCloseY;
  122. return true;
  123. }
  124. if (type == Path::quadMarker)
  125. {
  126. const size_t offset = (size_t) (stackPos - stackBase);
  127. if (offset >= stackSize - 10)
  128. {
  129. stackSize <<= 1;
  130. stackBase.realloc (stackSize);
  131. stackPos = stackBase + offset;
  132. }
  133. const float m1x = (x1 + x2) * 0.5f;
  134. const float m1y = (y1 + y2) * 0.5f;
  135. const float m2x = (x2 + x3) * 0.5f;
  136. const float m2y = (y2 + y3) * 0.5f;
  137. const float m3x = (m1x + m2x) * 0.5f;
  138. const float m3y = (m1y + m2y) * 0.5f;
  139. const float errorX = m3x - x2;
  140. const float errorY = m3y - y2;
  141. if (errorX * errorX + errorY * errorY > toleranceSquared)
  142. {
  143. *stackPos++ = y3;
  144. *stackPos++ = x3;
  145. *stackPos++ = m2y;
  146. *stackPos++ = m2x;
  147. *stackPos++ = Path::quadMarker;
  148. *stackPos++ = m3y;
  149. *stackPos++ = m3x;
  150. *stackPos++ = m1y;
  151. *stackPos++ = m1x;
  152. *stackPos++ = Path::quadMarker;
  153. }
  154. else
  155. {
  156. *stackPos++ = y3;
  157. *stackPos++ = x3;
  158. *stackPos++ = Path::lineMarker;
  159. *stackPos++ = m3y;
  160. *stackPos++ = m3x;
  161. *stackPos++ = Path::lineMarker;
  162. }
  163. jassert (stackPos < stackBase + stackSize);
  164. }
  165. else if (type == Path::cubicMarker)
  166. {
  167. const size_t offset = (size_t) (stackPos - stackBase);
  168. if (offset >= stackSize - 16)
  169. {
  170. stackSize <<= 1;
  171. stackBase.realloc (stackSize);
  172. stackPos = stackBase + offset;
  173. }
  174. const float m1x = (x1 + x2) * 0.5f;
  175. const float m1y = (y1 + y2) * 0.5f;
  176. const float m2x = (x3 + x2) * 0.5f;
  177. const float m2y = (y3 + y2) * 0.5f;
  178. const float m3x = (x3 + x4) * 0.5f;
  179. const float m3y = (y3 + y4) * 0.5f;
  180. const float m4x = (m1x + m2x) * 0.5f;
  181. const float m4y = (m1y + m2y) * 0.5f;
  182. const float m5x = (m3x + m2x) * 0.5f;
  183. const float m5y = (m3y + m2y) * 0.5f;
  184. const float error1X = m4x - x2;
  185. const float error1Y = m4y - y2;
  186. const float error2X = m5x - x3;
  187. const float error2Y = m5y - y3;
  188. if (error1X * error1X + error1Y * error1Y > toleranceSquared
  189. || error2X * error2X + error2Y * error2Y > toleranceSquared)
  190. {
  191. *stackPos++ = y4;
  192. *stackPos++ = x4;
  193. *stackPos++ = m3y;
  194. *stackPos++ = m3x;
  195. *stackPos++ = m5y;
  196. *stackPos++ = m5x;
  197. *stackPos++ = Path::cubicMarker;
  198. *stackPos++ = (m4y + m5y) * 0.5f;
  199. *stackPos++ = (m4x + m5x) * 0.5f;
  200. *stackPos++ = m4y;
  201. *stackPos++ = m4x;
  202. *stackPos++ = m1y;
  203. *stackPos++ = m1x;
  204. *stackPos++ = Path::cubicMarker;
  205. }
  206. else
  207. {
  208. *stackPos++ = y4;
  209. *stackPos++ = x4;
  210. *stackPos++ = Path::lineMarker;
  211. *stackPos++ = m5y;
  212. *stackPos++ = m5x;
  213. *stackPos++ = Path::lineMarker;
  214. *stackPos++ = m4y;
  215. *stackPos++ = m4x;
  216. *stackPos++ = Path::lineMarker;
  217. }
  218. }
  219. else if (type == Path::closeSubPathMarker)
  220. {
  221. if (x2 != subPathCloseX || y2 != subPathCloseY)
  222. {
  223. x1 = x2;
  224. y1 = y2;
  225. x2 = subPathCloseX;
  226. y2 = subPathCloseY;
  227. closesSubPath = true;
  228. return true;
  229. }
  230. }
  231. else
  232. {
  233. jassert (type == Path::moveMarker);
  234. subPathIndex = -1;
  235. subPathCloseX = x1 = x2;
  236. subPathCloseY = y1 = y2;
  237. }
  238. }
  239. }
  240. #if JUCE_MSVC && JUCE_DEBUG
  241. #pragma optimize ("", on) // resets optimisations to the project defaults
  242. #endif