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.5KB

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