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.

279 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 this technical preview, this file is not subject to commercial licensing.
  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)
  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
  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. if (errorX * errorX + errorY * errorY > toleranceSquared)
  134. {
  135. *stackPos++ = y3;
  136. *stackPos++ = x3;
  137. *stackPos++ = m2y;
  138. *stackPos++ = m2x;
  139. *stackPos++ = Path::quadMarker;
  140. *stackPos++ = m3y;
  141. *stackPos++ = m3x;
  142. *stackPos++ = m1y;
  143. *stackPos++ = m1x;
  144. *stackPos++ = Path::quadMarker;
  145. }
  146. else
  147. {
  148. *stackPos++ = y3;
  149. *stackPos++ = x3;
  150. *stackPos++ = Path::lineMarker;
  151. *stackPos++ = m3y;
  152. *stackPos++ = m3x;
  153. *stackPos++ = Path::lineMarker;
  154. }
  155. jassert (stackPos < stackBase + stackSize);
  156. }
  157. else if (isMarker (type, Path::cubicMarker))
  158. {
  159. const size_t offset = (size_t) (stackPos - stackBase);
  160. if (offset >= stackSize - 16)
  161. {
  162. stackSize <<= 1;
  163. stackBase.realloc (stackSize);
  164. stackPos = stackBase + offset;
  165. }
  166. auto m1x = (x1 + x2) * 0.5f;
  167. auto m1y = (y1 + y2) * 0.5f;
  168. auto m2x = (x3 + x2) * 0.5f;
  169. auto m2y = (y3 + y2) * 0.5f;
  170. auto m3x = (x3 + x4) * 0.5f;
  171. auto m3y = (y3 + y4) * 0.5f;
  172. auto m4x = (m1x + m2x) * 0.5f;
  173. auto m4y = (m1y + m2y) * 0.5f;
  174. auto m5x = (m3x + m2x) * 0.5f;
  175. auto m5y = (m3y + m2y) * 0.5f;
  176. auto error1X = m4x - x2;
  177. auto error1Y = m4y - y2;
  178. auto error2X = m5x - x3;
  179. auto error2Y = m5y - y3;
  180. if (error1X * error1X + error1Y * error1Y > toleranceSquared
  181. || error2X * error2X + error2Y * error2Y > toleranceSquared)
  182. {
  183. *stackPos++ = y4;
  184. *stackPos++ = x4;
  185. *stackPos++ = m3y;
  186. *stackPos++ = m3x;
  187. *stackPos++ = m5y;
  188. *stackPos++ = m5x;
  189. *stackPos++ = Path::cubicMarker;
  190. *stackPos++ = (m4y + m5y) * 0.5f;
  191. *stackPos++ = (m4x + m5x) * 0.5f;
  192. *stackPos++ = m4y;
  193. *stackPos++ = m4x;
  194. *stackPos++ = m1y;
  195. *stackPos++ = m1x;
  196. *stackPos++ = Path::cubicMarker;
  197. }
  198. else
  199. {
  200. *stackPos++ = y4;
  201. *stackPos++ = x4;
  202. *stackPos++ = Path::lineMarker;
  203. *stackPos++ = m5y;
  204. *stackPos++ = m5x;
  205. *stackPos++ = Path::lineMarker;
  206. *stackPos++ = m4y;
  207. *stackPos++ = m4x;
  208. *stackPos++ = Path::lineMarker;
  209. }
  210. }
  211. else if (isMarker (type, Path::closeSubPathMarker))
  212. {
  213. if (x2 != subPathCloseX || y2 != subPathCloseY)
  214. {
  215. x1 = x2;
  216. y1 = y2;
  217. x2 = subPathCloseX;
  218. y2 = subPathCloseY;
  219. closesSubPath = true;
  220. return true;
  221. }
  222. }
  223. else
  224. {
  225. jassert (isMarker (type, Path::moveMarker));
  226. subPathIndex = -1;
  227. subPathCloseX = x1 = x2;
  228. subPathCloseY = y1 = y2;
  229. }
  230. }
  231. }
  232. #if JUCE_MSVC && JUCE_DEBUG
  233. #pragma optimize ("", on) // resets optimisations to the project defaults
  234. #endif
  235. } // namespace juce