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.

289 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. #if JUCE_MSVC && JUCE_DEBUG
  16. #pragma optimize ("t", on)
  17. #endif
  18. //==============================================================================
  19. PathFlatteningIterator::PathFlatteningIterator (const Path& pathToUse,
  20. const AffineTransform& t,
  21. float tolerance)
  22. : x2 (0),
  23. y2 (0),
  24. closesSubPath (false),
  25. subPathIndex (-1),
  26. path (pathToUse),
  27. transform (t),
  28. source (path.data.begin()),
  29. toleranceSquared (tolerance * tolerance),
  30. isIdentityTransform (t.isIdentity())
  31. {
  32. stackPos = stackBase;
  33. }
  34. PathFlatteningIterator::~PathFlatteningIterator()
  35. {
  36. }
  37. bool PathFlatteningIterator::isLastInSubpath() const noexcept
  38. {
  39. return stackPos == stackBase.get()
  40. && (source == path.data.end() || isMarker (*source, Path::moveMarker));
  41. }
  42. bool PathFlatteningIterator::next()
  43. {
  44. x1 = x2;
  45. y1 = y2;
  46. float x3 = 0;
  47. float y3 = 0;
  48. float x4 = 0;
  49. float y4 = 0;
  50. for (;;)
  51. {
  52. float type;
  53. if (stackPos == stackBase.get())
  54. {
  55. if (source == path.data.end())
  56. return false;
  57. type = *source++;
  58. if (! isMarker (type, Path::closeSubPathMarker))
  59. {
  60. x2 = *source++;
  61. y2 = *source++;
  62. if (isMarker (type, Path::quadMarker))
  63. {
  64. x3 = *source++;
  65. y3 = *source++;
  66. if (! isIdentityTransform)
  67. transform.transformPoints (x2, y2, x3, y3);
  68. }
  69. else if (isMarker (type, Path::cubicMarker))
  70. {
  71. x3 = *source++;
  72. y3 = *source++;
  73. x4 = *source++;
  74. y4 = *source++;
  75. if (! isIdentityTransform)
  76. transform.transformPoints (x2, y2, x3, y3, x4, y4);
  77. }
  78. else
  79. {
  80. if (! isIdentityTransform)
  81. transform.transformPoint (x2, y2);
  82. }
  83. }
  84. }
  85. else
  86. {
  87. type = *--stackPos;
  88. if (! isMarker (type, Path::closeSubPathMarker))
  89. {
  90. x2 = *--stackPos;
  91. y2 = *--stackPos;
  92. if (isMarker (type, Path::quadMarker))
  93. {
  94. x3 = *--stackPos;
  95. y3 = *--stackPos;
  96. }
  97. else if (isMarker (type, Path::cubicMarker))
  98. {
  99. x3 = *--stackPos;
  100. y3 = *--stackPos;
  101. x4 = *--stackPos;
  102. y4 = *--stackPos;
  103. }
  104. }
  105. }
  106. if (isMarker (type, Path::lineMarker))
  107. {
  108. ++subPathIndex;
  109. closesSubPath = stackPos == stackBase.get()
  110. && source != path.data.end()
  111. && *source == Path::closeSubPathMarker
  112. && x2 == subPathCloseX
  113. && y2 == subPathCloseY;
  114. return true;
  115. }
  116. if (isMarker (type, Path::quadMarker))
  117. {
  118. const size_t offset = (size_t) (stackPos - stackBase);
  119. if (offset >= stackSize - 10)
  120. {
  121. stackSize <<= 1;
  122. stackBase.realloc (stackSize);
  123. stackPos = stackBase + offset;
  124. }
  125. auto m1x = (x1 + x2) * 0.5f;
  126. auto m1y = (y1 + y2) * 0.5f;
  127. auto m2x = (x2 + x3) * 0.5f;
  128. auto m2y = (y2 + y3) * 0.5f;
  129. auto m3x = (m1x + m2x) * 0.5f;
  130. auto m3y = (m1y + m2y) * 0.5f;
  131. auto errorX = m3x - x2;
  132. auto errorY = m3y - y2;
  133. auto outsideTolerance = errorX * errorX + errorY * errorY > toleranceSquared;
  134. auto canBeSubdivided = (m3x != m1x && m3x != m2x)
  135. || (m3y != m1y && m3y != m2y);
  136. if (outsideTolerance && canBeSubdivided)
  137. {
  138. *stackPos++ = y3;
  139. *stackPos++ = x3;
  140. *stackPos++ = m2y;
  141. *stackPos++ = m2x;
  142. *stackPos++ = Path::quadMarker;
  143. *stackPos++ = m3y;
  144. *stackPos++ = m3x;
  145. *stackPos++ = m1y;
  146. *stackPos++ = m1x;
  147. *stackPos++ = Path::quadMarker;
  148. }
  149. else
  150. {
  151. *stackPos++ = y3;
  152. *stackPos++ = x3;
  153. *stackPos++ = Path::lineMarker;
  154. *stackPos++ = m3y;
  155. *stackPos++ = m3x;
  156. *stackPos++ = Path::lineMarker;
  157. }
  158. jassert (stackPos < stackBase + stackSize);
  159. }
  160. else if (isMarker (type, Path::cubicMarker))
  161. {
  162. const size_t offset = (size_t) (stackPos - stackBase);
  163. if (offset >= stackSize - 16)
  164. {
  165. stackSize <<= 1;
  166. stackBase.realloc (stackSize);
  167. stackPos = stackBase + offset;
  168. }
  169. auto m1x = (x1 + x2) * 0.5f;
  170. auto m1y = (y1 + y2) * 0.5f;
  171. auto m2x = (x3 + x2) * 0.5f;
  172. auto m2y = (y3 + y2) * 0.5f;
  173. auto m3x = (x3 + x4) * 0.5f;
  174. auto m3y = (y3 + y4) * 0.5f;
  175. auto m4x = (m1x + m2x) * 0.5f;
  176. auto m4y = (m1y + m2y) * 0.5f;
  177. auto m5x = (m3x + m2x) * 0.5f;
  178. auto m5y = (m3y + m2y) * 0.5f;
  179. auto error1X = m4x - x2;
  180. auto error1Y = m4y - y2;
  181. auto error2X = m5x - x3;
  182. auto error2Y = m5y - y3;
  183. auto outsideTolerance = error1X * error1X + error1Y * error1Y > toleranceSquared
  184. || error2X * error2X + error2Y * error2Y > toleranceSquared;
  185. auto canBeSubdivided = (m4x != m1x && m4x != m2x)
  186. || (m4y != m1y && m4y != m2y)
  187. || (m5x != m3x && m5x != m2x)
  188. || (m5y != m3y && m5y != m2y);
  189. if (outsideTolerance && canBeSubdivided)
  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 (isMarker (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 (isMarker (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
  243. } // namespace juce