| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #include "vpath.h" |
| 20 | #include <cassert> |
| 21 | #include <iterator> |
| 22 | #include <vector> |
| 23 | #include "vbezier.h" |
| 24 | #include "vdebug.h" |
| 25 | #include "vline.h" |
| 26 | #include "vrect.h" |
| 27 |
| 28 | V_BEGIN_NAMESPACE |
| 29 |
| 30 | void VPath::VPathData::transform(const VMatrix &m) |
| 31 | { |
| 32 | for (auto &i : m_points) { |
| 33 | i = m.map(i); |
| 34 | } |
| 35 | mLengthDirty = true; |
| 36 | } |
| 37 |
| 38 | float VPath::VPathData::length() const |
| 39 | { |
| 40 | if (!mLengthDirty) return mLength; |
| 41 |
| 42 | mLengthDirty = false; |
| 43 | mLength = 0.0; |
| 44 |
| 45 | size_t i = 0; |
| 46 | for (auto e : m_elements) { |
| 47 | switch (e) { |
| 48 | case VPath::Element::MoveTo: |
| 49 | i++; |
| 50 | break; |
| 51 | case VPath::Element::LineTo: { |
| 52 | mLength += VLine(m_points[i - 1], m_points[i]).length(); |
| 53 | i++; |
| 54 | break; |
| 55 | } |
| 56 | case VPath::Element::CubicTo: { |
| 57 | mLength += VBezier::fromPoints(m_points[i - 1], m_points[i], |
| 58 | m_points[i + 1], m_points[i + 2]) |
| 59 | .length(); |
| 60 | i += 3; |
| 61 | break; |
| 62 | } |
| 63 | case VPath::Element::Close: |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 |
| 68 | return mLength; |
| 69 | } |
| 70 |
| 71 | void VPath::VPathData::checkNewSegment() |
| 72 | { |
| 73 | if (mNewSegment) { |
| 74 | moveTo(0, 0); |
| 75 | mNewSegment = false; |
| 76 | } |
| 77 | } |
| 78 |
| 79 | void VPath::VPathData::moveTo(float x, float y) |
| 80 | { |
| 81 | mStartPoint = {x, y}; |
| 82 | mNewSegment = false; |
| 83 | m_elements.emplace_back(VPath::Element::MoveTo); |
| 84 | m_points.emplace_back(x, y); |
| 85 | m_segments++; |
| 86 | mLengthDirty = true; |
| 87 | } |
| 88 |
| 89 | void VPath::VPathData::lineTo(float x, float y) |
| 90 | { |
| 91 | checkNewSegment(); |
| 92 | m_elements.emplace_back(VPath::Element::LineTo); |
| 93 | m_points.emplace_back(x, y); |
| 94 | mLengthDirty = true; |
| 95 | } |
| 96 |
| 97 | void VPath::VPathData::cubicTo(float cx1, float cy1, float cx2, float cy2, |
| 98 | float ex, float ey) |
| 99 | { |
| 100 | checkNewSegment(); |
| 101 | m_elements.emplace_back(VPath::Element::CubicTo); |
| 102 | m_points.emplace_back(cx1, cy1); |
| 103 | m_points.emplace_back(cx2, cy2); |
| 104 | m_points.emplace_back(ex, ey); |
| 105 | mLengthDirty = true; |
| 106 | } |
| 107 |
| 108 | void VPath::VPathData::close() |
| 109 | { |
| 110 | if (empty()) return; |
| 111 |
| 112 | const VPointF &lastPt = m_points.back(); |
| 113 | if (!fuzzyCompare(mStartPoint, lastPt)) { |
| 114 | lineTo(mStartPoint.x(), mStartPoint.y()); |
| 115 | } |
| 116 | m_elements.push_back(VPath::Element::Close); |
| 117 | mNewSegment = true; |
| 118 | mLengthDirty = true; |
| 119 | } |
| 120 |
| 121 | void VPath::VPathData::reset() |
| 122 | { |
| 123 | if (empty()) return; |
| 124 |
| 125 | m_elements.clear(); |
| 126 | m_points.clear(); |
| 127 | m_segments = 0; |
| 128 | mLength = 0; |
| 129 | mLengthDirty = false; |
| 130 | } |
| 131 |
| 132 | size_t VPath::VPathData::segments() const |
| 133 | { |
| 134 | return m_segments; |
| 135 | } |
| 136 |
| 137 | void VPath::VPathData::reserve(size_t pts, size_t elms) |
| 138 | { |
| 139 | if (m_points.capacity() < m_points.size() + pts) |
| 140 | m_points.reserve(m_points.size() + pts); |
| 141 | if (m_elements.capacity() < m_elements.size() + elms) |
| 142 | m_elements.reserve(m_elements.size() + elms); |
| 143 | } |
| 144 |
| 145 | static VPointF curvesForArc(const VRectF &, float, float, VPointF *, size_t *); |
| 146 | static constexpr float PATH_KAPPA = 0.5522847498f; |
| 147 | static constexpr float K_PI = float(M_PI); |
| 148 |
| 149 | void VPath::VPathData::arcTo(const VRectF &rect, float startAngle, |
| 150 | float sweepLength, bool forceMoveTo) |
| 151 | { |
| 152 | size_t point_count = 0; |
| 153 | VPointF pts[15]; |
| 154 | VPointF curve_start = |
| 155 | curvesForArc(rect, startAngle, sweepLength, pts, &point_count); |
| 156 |
| 157 | reserve(point_count + 1, point_count / 3 + 1); |
| 158 | if (empty() || forceMoveTo) { |
| 159 | moveTo(curve_start.x(), curve_start.y()); |
| 160 | } else { |
| 161 | lineTo(curve_start.x(), curve_start.y()); |
| 162 | } |
| 163 | for (size_t i = 0; i < point_count; i += 3) { |
| 164 | cubicTo(pts[i].x(), pts[i].y(), pts[i + 1].x(), pts[i + 1].y(), |
| 165 | pts[i + 2].x(), pts[i + 2].y()); |
| 166 | } |
| 167 | } |
| 168 |
| 169 | void VPath::VPathData::addCircle(float cx, float cy, float radius, |
| 170 | VPath::Direction dir) |
| 171 | { |
| 172 | addOval(VRectF(cx - radius, cy - radius, 2 * radius, 2 * radius), dir); |
| 173 | } |
| 174 |
| 175 | void VPath::VPathData::addOval(const VRectF &rect, VPath::Direction dir) |
| 176 | { |
| 177 | if (rect.empty()) return; |
| 178 |
| 179 | float x = rect.x(); |
| 180 | float y = rect.y(); |
| 181 |
| 182 | float w = rect.width(); |
| 183 | float w2 = rect.width() / 2; |
| 184 | float w2k = w2 * PATH_KAPPA; |
| 185 |
| 186 | float h = rect.height(); |
| 187 | float h2 = rect.height() / 2; |
| 188 | float h2k = h2 * PATH_KAPPA; |
| 189 |
| 190 | reserve(13, 6); // 1Move + 4Cubic + 1Close |
| 191 | if (dir == VPath::Direction::CW) { |
| 192 | // moveto 12 o'clock. |
| 193 | moveTo(x + w2, y); |
| 194 | // 12 -> 3 o'clock |
| 195 | cubicTo(x + w2 + w2k, y, x + w, y + h2 - h2k, x + w, y + h2); |
| 196 | // 3 -> 6 o'clock |
| 197 | cubicTo(x + w, y + h2 + h2k, x + w2 + w2k, y + h, x + w2, y + h); |
| 198 | // 6 -> 9 o'clock |
| 199 | cubicTo(x + w2 - w2k, y + h, x, y + h2 + h2k, x, y + h2); |
| 200 | // 9 -> 12 o'clock |
| 201 | cubicTo(x, y + h2 - h2k, x + w2 - w2k, y, x + w2, y); |
| 202 | } else { |
| 203 | // moveto 12 o'clock. |
| 204 | moveTo(x + w2, y); |
| 205 | // 12 -> 9 o'clock |
| 206 | cubicTo(x + w2 - w2k, y, x, y + h2 - h2k, x, y + h2); |
| 207 | // 9 -> 6 o'clock |
| 208 | cubicTo(x, y + h2 + h2k, x + w2 - w2k, y + h, x + w2, y + h); |
| 209 | // 6 -> 3 o'clock |
| 210 | cubicTo(x + w2 + w2k, y + h, x + w, y + h2 + h2k, x + w, y + h2); |
| 211 | // 3 -> 12 o'clock |
| 212 | cubicTo(x + w, y + h2 - h2k, x + w2 + w2k, y, x + w2, y); |
| 213 | } |
| 214 | close(); |
| 215 | } |
| 216 |
| 217 | void VPath::VPathData::addRect(const VRectF &rect, VPath::Direction dir) |
| 218 | { |
| 219 | if (rect.empty()) return; |
| 220 |
| 221 | float x = rect.x(); |
| 222 | float y = rect.y(); |
| 223 | float w = rect.width(); |
| 224 | float h = rect.height(); |
| 225 |
| 226 | reserve(5, 6); // 1Move + 4Line + 1Close |
| 227 | if (dir == VPath::Direction::CW) { |
| 228 | moveTo(x + w, y); |
| 229 | lineTo(x + w, y + h); |
| 230 | lineTo(x, y + h); |
| 231 | lineTo(x, y); |
| 232 | close(); |
| 233 | } else { |
| 234 | moveTo(x + w, y); |
| 235 | lineTo(x, y); |
| 236 | lineTo(x, y + h); |
| 237 | lineTo(x + w, y + h); |
| 238 | close(); |
| 239 | } |
| 240 | } |
| 241 |
| 242 | void VPath::VPathData::addRoundRect(const VRectF &rect, float roundness, |
| 243 | VPath::Direction dir) |
| 244 | { |
| 245 | if (2 * roundness > rect.width()) roundness = rect.width() / 2.0f; |
| 246 | if (2 * roundness > rect.height()) roundness = rect.height() / 2.0f; |
| 247 | addRoundRect(rect, roundness, roundness, dir); |
| 248 | } |
| 249 |
| 250 | void VPath::VPathData::addRoundRect(const VRectF &rect, float rx, float ry, |
| 251 | VPath::Direction dir) |
| 252 | { |
| 253 | if (vCompare(rx, 0.f) || vCompare(ry, 0.f)) { |
| 254 | addRect(rect, dir); |
| 255 | return; |
| 256 | } |
| 257 |
| 258 | float x = rect.x(); |
| 259 | float y = rect.y(); |
| 260 | float w = rect.width(); |
| 261 | float h = rect.height(); |
| 262 | // clamp the rx and ry radius value. |
| 263 | rx = 2 * rx; |
| 264 | ry = 2 * ry; |
| 265 | if (rx > w) rx = w; |
| 266 | if (ry > h) ry = h; |
| 267 |
| 268 | reserve(17, 10); // 1Move + 4Cubic + 1Close |
| 269 | if (dir == VPath::Direction::CW) { |
| 270 | moveTo(x + w, y + ry / 2.f); |
| 271 | arcTo(VRectF(x + w - rx, y + h - ry, rx, ry), 0, -90, false); |
| 272 | arcTo(VRectF(x, y + h - ry, rx, ry), -90, -90, false); |
| 273 | arcTo(VRectF(x, y, rx, ry), -180, -90, false); |
| 274 | arcTo(VRectF(x + w - rx, y, rx, ry), -270, -90, false); |
| 275 | close(); |
| 276 | } else { |
| 277 | moveTo(x + w, y + ry / 2.f); |
| 278 | arcTo(VRectF(x + w - rx, y, rx, ry), 0, 90, false); |
| 279 | arcTo(VRectF(x, y, rx, ry), 90, 90, false); |
| 280 | arcTo(VRectF(x, y + h - ry, rx, ry), 180, 90, false); |
| 281 | arcTo(VRectF(x + w - rx, y + h - ry, rx, ry), 270, 90, false); |
| 282 | close(); |
| 283 | } |
| 284 | } |
| 285 |
| 286 | static float tForArcAngle(float angle); |
| 287 | void findEllipseCoords(const VRectF &r, float angle, float length, |
| 288 | VPointF *startPoint, VPointF *endPoint) |
| 289 | { |
| 290 | if (r.empty()) { |
| 291 | if (startPoint) *startPoint = VPointF(); |
| 292 | if (endPoint) *endPoint = VPointF(); |
| 293 | return; |
| 294 | } |
| 295 |
| 296 | float w2 = r.width() / 2; |
| 297 | float h2 = r.height() / 2; |
| 298 |
| 299 | float angles[2] = {angle, angle + length}; |
| 300 | VPointF *points[2] = {startPoint, endPoint}; |
| 301 |
| 302 | for (int i = 0; i < 2; ++i) { |
| 303 | if (!points[i]) continue; |
| 304 |
| 305 | float theta = angles[i] - 360 * floorf(angles[i] / 360); |
| 306 | float t = theta / 90; |
| 307 | // truncate |
| 308 | int quadrant = int(t); |
| 309 | t -= quadrant; |
| 310 |
| 311 | t = tForArcAngle(90 * t); |
| 312 |
| 313 | // swap x and y? |
| 314 | if (quadrant & 1) t = 1 - t; |
| 315 |
| 316 | float a, b, c, d; |
| 317 | VBezier::coefficients(t, a, b, c, d); |
| 318 | VPointF p(a + b + c * PATH_KAPPA, d + c + b * PATH_KAPPA); |
| 319 |
| 320 | // left quadrants |
| 321 | if (quadrant == 1 || quadrant == 2) p.rx() = -p.x(); |
| 322 |
| 323 | // top quadrants |
| 324 | if (quadrant == 0 || quadrant == 1) p.ry() = -p.y(); |
| 325 |
| 326 | *points[i] = r.center() + VPointF(w2 * p.x(), h2 * p.y()); |
| 327 | } |
| 328 | } |
| 329 |
| 330 | static float tForArcAngle(float angle) |
| 331 | { |
| 332 | float radians, cos_angle, sin_angle, tc, ts, t; |
| 333 |
| 334 | if (vCompare(angle, 0.f)) return 0; |
| 335 | if (vCompare(angle, 90.0f)) return 1; |
| 336 |
| 337 | radians = (angle / 180) * K_PI; |
| 338 |
| 339 | cos_angle = cosf(radians); |
| 340 | sin_angle = sinf(radians); |
| 341 |
| 342 | // initial guess |
| 343 | tc = angle / 90; |
| 344 |
| 345 | // do some iterations of newton's method to approximate cos_angle |
| 346 | // finds the zero of the function b.pointAt(tc).x() - cos_angle |
| 347 | tc -= ((((2 - 3 * PATH_KAPPA) * tc + 3 * (PATH_KAPPA - 1)) * tc) * tc + 1 - |
| 348 | cos_angle) // value |
| 349 | / (((6 - 9 * PATH_KAPPA) * tc + 6 * (PATH_KAPPA - 1)) * |
| 350 | tc); // derivative |
| 351 | tc -= ((((2 - 3 * PATH_KAPPA) * tc + 3 * (PATH_KAPPA - 1)) * tc) * tc + 1 - |
| 352 | cos_angle) // value |
| 353 | / (((6 - 9 * PATH_KAPPA) * tc + 6 * (PATH_KAPPA - 1)) * |
| 354 | tc); // derivative |
| 355 |
| 356 | // initial guess |
| 357 | ts = tc; |
| 358 | // do some iterations of newton's method to approximate sin_angle |
| 359 | // finds the zero of the function b.pointAt(tc).y() - sin_angle |
| 360 | ts -= ((((3 * PATH_KAPPA - 2) * ts - 6 * PATH_KAPPA + 3) * ts + |
| 361 | 3 * PATH_KAPPA) * |
| 362 | ts - |
| 363 | sin_angle) / |
| 364 | (((9 * PATH_KAPPA - 6) * ts + 12 * PATH_KAPPA - 6) * ts + |
| 365 | 3 * PATH_KAPPA); |
| 366 | ts -= ((((3 * PATH_KAPPA - 2) * ts - 6 * PATH_KAPPA + 3) * ts + |
| 367 | 3 * PATH_KAPPA) * |
| 368 | ts - |
| 369 | sin_angle) / |
| 370 | (((9 * PATH_KAPPA - 6) * ts + 12 * PATH_KAPPA - 6) * ts + |
| 371 | 3 * PATH_KAPPA); |
| 372 |
| 373 | // use the average of the t that best approximates cos_angle |
| 374 | // and the t that best approximates sin_angle |
| 375 | t = 0.5f * (tc + ts); |
| 376 | return t; |
| 377 | } |
| 378 |
| 379 | // The return value is the starting point of the arc |
| 380 | static VPointF curvesForArc(const VRectF &rect, float startAngle, |
| 381 | float sweepLength, VPointF *curves, |
| 382 | size_t *point_count) |
| 383 | { |
| 384 | if (rect.empty()) { |
| 385 | return {}; |
| 386 | } |
| 387 |
| 388 | float x = rect.x(); |
| 389 | float y = rect.y(); |
| 390 |
| 391 | float w = rect.width(); |
| 392 | float w2 = rect.width() / 2; |
| 393 | float w2k = w2 * PATH_KAPPA; |
| 394 |
| 395 | float h = rect.height(); |
| 396 | float h2 = rect.height() / 2; |
| 397 | float h2k = h2 * PATH_KAPPA; |
| 398 |
| 399 | VPointF points[16] = { |
| 400 | // start point |
| 401 | VPointF(x + w, y + h2), |
| 402 |
| 403 | // 0 -> 270 degrees |
| 404 | VPointF(x + w, y + h2 + h2k), VPointF(x + w2 + w2k, y + h), |
| 405 | VPointF(x + w2, y + h), |
| 406 |
| 407 | // 270 -> 180 degrees |
| 408 | VPointF(x + w2 - w2k, y + h), VPointF(x, y + h2 + h2k), |
| 409 | VPointF(x, y + h2), |
| 410 |
| 411 | // 180 -> 90 degrees |
| 412 | VPointF(x, y + h2 - h2k), VPointF(x + w2 - w2k, y), VPointF(x + w2, y), |
| 413 |
| 414 | // 90 -> 0 degrees |
| 415 | VPointF(x + w2 + w2k, y), VPointF(x + w, y + h2 - h2k), |
| 416 | VPointF(x + w, y + h2)}; |
| 417 |
| 418 | if (sweepLength > 360) |
| 419 | sweepLength = 360; |
| 420 | else if (sweepLength < -360) |
| 421 | sweepLength = -360; |
| 422 |
| 423 | // Special case fast paths |
| 424 | if (startAngle == 0.0f) { |
| 425 | if (vCompare(sweepLength, 360)) { |
| 426 | for (int i = 11; i >= 0; --i) curves[(*point_count)++] = points[i]; |
| 427 | return points[12]; |
| 428 | } else if (vCompare(sweepLength, -360)) { |
| 429 | for (int i = 1; i <= 12; ++i) curves[(*point_count)++] = points[i]; |
| 430 | return points[0]; |
| 431 | } |
| 432 | } |
| 433 |
| 434 | int startSegment = int(floorf(startAngle / 90.0f)); |
| 435 | int endSegment = int(floorf((startAngle + sweepLength) / 90.0f)); |
| 436 |
| 437 | float startT = (startAngle - startSegment * 90) / 90; |
| 438 | float endT = (startAngle + sweepLength - endSegment * 90) / 90; |
| 439 |
| 440 | int delta = sweepLength > 0 ? 1 : -1; |
| 441 | if (delta < 0) { |
| 442 | startT = 1 - startT; |
| 443 | endT = 1 - endT; |
| 444 | } |
| 445 |
| 446 | // avoid empty start segment |
| 447 | if (vIsZero(startT - float(1))) { |
| 448 | startT = 0; |
| 449 | startSegment += delta; |
| 450 | } |
| 451 |
| 452 | // avoid empty end segment |
| 453 | if (vIsZero(endT)) { |
| 454 | endT = 1; |
| 455 | endSegment -= delta; |
| 456 | } |
| 457 |
| 458 | startT = tForArcAngle(startT * 90); |
| 459 | endT = tForArcAngle(endT * 90); |
| 460 |
| 461 | const bool splitAtStart = !vIsZero(startT); |
| 462 | const bool splitAtEnd = !vIsZero(endT - float(1)); |
| 463 |
| 464 | const int end = endSegment + delta; |
| 465 |
| 466 | // empty arc? |
| 467 | if (startSegment == end) { |
| 468 | const int quadrant = 3 - ((startSegment % 4) + 4) % 4; |
| 469 | const int j = 3 * quadrant; |
| 470 | return delta > 0 ? points[j + 3] : points[j]; |
| 471 | } |
| 472 |
| 473 | VPointF startPoint, endPoint; |
| 474 | findEllipseCoords(rect, startAngle, sweepLength, &startPoint, &endPoint); |
| 475 |
| 476 | for (int i = startSegment; i != end; i += delta) { |
| 477 | const int quadrant = 3 - ((i % 4) + 4) % 4; |
| 478 | const int j = 3 * quadrant; |
| 479 |
| 480 | VBezier b; |
| 481 | if (delta > 0) |
| 482 | b = VBezier::fromPoints(points[j + 3], points[j + 2], points[j + 1], |
| 483 | points[j]); |
| 484 | else |
| 485 | b = VBezier::fromPoints(points[j], points[j + 1], points[j + 2], |
| 486 | points[j + 3]); |
| 487 |
| 488 | // empty arc? |
| 489 | if (startSegment == endSegment && vCompare(startT, endT)) |
| 490 | return startPoint; |
| 491 |
| 492 | if (i == startSegment) { |
| 493 | if (i == endSegment && splitAtEnd) |
| 494 | b = b.onInterval(startT, endT); |
| 495 | else if (splitAtStart) |
| 496 | b = b.onInterval(startT, 1); |
| 497 | } else if (i == endSegment && splitAtEnd) { |
| 498 | b = b.onInterval(0, endT); |
| 499 | } |
| 500 |
| 501 | // push control points |
| 502 | curves[(*point_count)++] = b.pt2(); |
| 503 | curves[(*point_count)++] = b.pt3(); |
| 504 | curves[(*point_count)++] = b.pt4(); |
| 505 | } |
| 506 |
| 507 | curves[*(point_count)-1] = endPoint; |
| 508 |
| 509 | return startPoint; |
| 510 | } |
| 511 |
| 512 | void VPath::VPathData::addPolystar(float points, float innerRadius, |
| 513 | float outerRadius, float innerRoundness, |
| 514 | float outerRoundness, float startAngle, |
| 515 | float cx, float cy, VPath::Direction dir) |
| 516 | { |
| 517 | const static float POLYSTAR_MAGIC_NUMBER = 0.47829f / 0.28f; |
| 518 | float currentAngle = (startAngle - 90.0f) * K_PI / 180.0f; |
| 519 | float x; |
| 520 | float y; |
| 521 | float partialPointRadius = 0; |
| 522 | float anglePerPoint = (2.0f * K_PI / points); |
| 523 | float halfAnglePerPoint = anglePerPoint / 2.0f; |
| 524 | float partialPointAmount = points - floorf(points); |
| 525 | bool longSegment = false; |
| 526 | size_t numPoints = size_t(ceilf(points) * 2); |
| 527 | float angleDir = ((dir == VPath::Direction::CW) ? 1.0 : -1.0); |
| 528 | bool hasRoundness = false; |
| 529 |
| 530 | innerRoundness /= 100.0f; |
| 531 | outerRoundness /= 100.0f; |
| 532 |
| 533 | if (!vCompare(partialPointAmount, 0)) { |
| 534 | currentAngle += |
| 535 | halfAnglePerPoint * (1.0f - partialPointAmount) * angleDir; |
| 536 | } |
| 537 |
| 538 | if (!vCompare(partialPointAmount, 0)) { |
| 539 | partialPointRadius = |
| 540 | innerRadius + partialPointAmount * (outerRadius - innerRadius); |
| 541 | x = partialPointRadius * cosf(currentAngle); |
| 542 | y = partialPointRadius * sinf(currentAngle); |
| 543 | currentAngle += anglePerPoint * partialPointAmount / 2.0f * angleDir; |
| 544 | } else { |
| 545 | x = outerRadius * cosf(currentAngle); |
| 546 | y = outerRadius * sinf(currentAngle); |
| 547 | currentAngle += halfAnglePerPoint * angleDir; |
| 548 | } |
| 549 |
| 550 | if (vIsZero(innerRoundness) && vIsZero(outerRoundness)) { |
| 551 | reserve(numPoints + 2, numPoints + 3); |
| 552 | } else { |
| 553 | reserve(numPoints * 3 + 2, numPoints + 3); |
| 554 | hasRoundness = true; |
| 555 | } |
| 556 |
| 557 | moveTo(x + cx, y + cy); |
| 558 |
| 559 | for (size_t i = 0; i < numPoints; i++) { |
| 560 | float radius = longSegment ? outerRadius : innerRadius; |
| 561 | float dTheta = halfAnglePerPoint; |
| 562 | if (!vIsZero(partialPointRadius) && i == numPoints - 2) { |
| 563 | dTheta = anglePerPoint * partialPointAmount / 2.0f; |
| 564 | } |
| 565 | if (!vIsZero(partialPointRadius) && i == numPoints - 1) { |
| 566 | radius = partialPointRadius; |
| 567 | } |
| 568 | float previousX = x; |
| 569 | float previousY = y; |
| 570 | x = radius * cosf(currentAngle); |
| 571 | y = radius * sinf(currentAngle); |
| 572 |
| 573 | if (hasRoundness) { |
| 574 | float cp1Theta = |
| 575 | (atan2f(previousY, previousX) - K_PI / 2.0f * angleDir); |
| 576 | float cp1Dx = cosf(cp1Theta); |
| 577 | float cp1Dy = sinf(cp1Theta); |
| 578 | float cp2Theta = (atan2f(y, x) - K_PI / 2.0f * angleDir); |
| 579 | float cp2Dx = cosf(cp2Theta); |
| 580 | float cp2Dy = sinf(cp2Theta); |
| 581 |
| 582 | float cp1Roundness = longSegment ? innerRoundness : outerRoundness; |
| 583 | float cp2Roundness = longSegment ? outerRoundness : innerRoundness; |
| 584 | float cp1Radius = longSegment ? innerRadius : outerRadius; |
| 585 | float cp2Radius = longSegment ? outerRadius : innerRadius; |
| 586 |
| 587 | float cp1x = cp1Radius * cp1Roundness * POLYSTAR_MAGIC_NUMBER * |
| 588 | cp1Dx / points; |
| 589 | float cp1y = cp1Radius * cp1Roundness * POLYSTAR_MAGIC_NUMBER * |
| 590 | cp1Dy / points; |
| 591 | float cp2x = cp2Radius * cp2Roundness * POLYSTAR_MAGIC_NUMBER * |
| 592 | cp2Dx / points; |
| 593 | float cp2y = cp2Radius * cp2Roundness * POLYSTAR_MAGIC_NUMBER * |
| 594 | cp2Dy / points; |
| 595 |
| 596 | if (!vIsZero(partialPointAmount) && |
| 597 | ((i == 0) || (i == numPoints - 1))) { |
| 598 | cp1x *= partialPointAmount; |
| 599 | cp1y *= partialPointAmount; |
| 600 | cp2x *= partialPointAmount; |
| 601 | cp2y *= partialPointAmount; |
| 602 | } |
| 603 |
| 604 | cubicTo(previousX - cp1x + cx, previousY - cp1y + cy, x + cp2x + cx, |
| 605 | y + cp2y + cy, x + cx, y + cy); |
| 606 | } else { |
| 607 | lineTo(x + cx, y + cy); |
| 608 | } |
| 609 |
| 610 | currentAngle += dTheta * angleDir; |
| 611 | longSegment = !longSegment; |
| 612 | } |
| 613 |
| 614 | close(); |
| 615 | } |
| 616 |
| 617 | void VPath::VPathData::addPolygon(float points, float radius, float roundness, |
| 618 | float startAngle, float cx, float cy, |
| 619 | VPath::Direction dir) |
| 620 | { |
| 621 | // TODO: Need to support floating point number for number of points |
| 622 | const static float POLYGON_MAGIC_NUMBER = 0.25; |
| 623 | float currentAngle = (startAngle - 90.0f) * K_PI / 180.0f; |
| 624 | float x; |
| 625 | float y; |
| 626 | float anglePerPoint = 2.0f * K_PI / floorf(points); |
| 627 | size_t numPoints = size_t(floorf(points)); |
| 628 | float angleDir = ((dir == VPath::Direction::CW) ? 1.0 : -1.0); |
| 629 | bool hasRoundness = false; |
| 630 |
| 631 | roundness /= 100.0f; |
| 632 |
| 633 | currentAngle = (currentAngle - 90.0f) * K_PI / 180.0f; |
| 634 | x = radius * cosf(currentAngle); |
| 635 | y = radius * sinf(currentAngle); |
| 636 | currentAngle += anglePerPoint * angleDir; |
| 637 |
| 638 | if (vIsZero(roundness)) { |
| 639 | reserve(numPoints + 2, numPoints + 3); |
| 640 | } else { |
| 641 | reserve(numPoints * 3 + 2, numPoints + 3); |
| 642 | hasRoundness = true; |
| 643 | } |
| 644 |
| 645 | moveTo(x + cx, y + cy); |
| 646 |
| 647 | for (size_t i = 0; i < numPoints; i++) { |
| 648 | float previousX = x; |
| 649 | float previousY = y; |
| 650 | x = (radius * cosf(currentAngle)); |
| 651 | y = (radius * sinf(currentAngle)); |
| 652 |
| 653 | if (hasRoundness) { |
| 654 | float cp1Theta = |
| 655 | (atan2f(previousY, previousX) - K_PI / 2.0f * angleDir); |
| 656 | float cp1Dx = cosf(cp1Theta); |
| 657 | float cp1Dy = sinf(cp1Theta); |
| 658 | float cp2Theta = atan2f(y, x) - K_PI / 2.0f * angleDir; |
| 659 | float cp2Dx = cosf(cp2Theta); |
| 660 | float cp2Dy = sinf(cp2Theta); |
| 661 |
| 662 | float cp1x = radius * roundness * POLYGON_MAGIC_NUMBER * cp1Dx; |
| 663 | float cp1y = radius * roundness * POLYGON_MAGIC_NUMBER * cp1Dy; |
| 664 | float cp2x = radius * roundness * POLYGON_MAGIC_NUMBER * cp2Dx; |
| 665 | float cp2y = radius * roundness * POLYGON_MAGIC_NUMBER * cp2Dy; |
| 666 |
| 667 | cubicTo(previousX - cp1x + cx, previousY - cp1y + cy, x + cp2x + cx, |
| 668 | y + cp2y + cy, x, y); |
| 669 | } else { |
| 670 | lineTo(x + cx, y + cy); |
| 671 | } |
| 672 |
| 673 | currentAngle += anglePerPoint * angleDir; |
| 674 | } |
| 675 |
| 676 | close(); |
| 677 | } |
| 678 |
| 679 | void VPath::VPathData::addPath(const VPathData &path) |
| 680 | { |
| 681 | size_t segment = path.segments(); |
| 682 |
| 683 | // make sure enough memory available |
| 684 | if (m_points.capacity() < m_points.size() + path.m_points.size()) |
| 685 | m_points.reserve(m_points.size() + path.m_points.size()); |
| 686 |
| 687 | if (m_elements.capacity() < m_elements.size() + path.m_elements.size()) |
| 688 | m_elements.reserve(m_elements.size() + path.m_elements.size()); |
| 689 |
| 690 | std::copy(path.m_points.begin(), path.m_points.end(), |
| 691 | back_inserter(m_points)); |
| 692 | std::copy(path.m_elements.begin(), path.m_elements.end(), |
| 693 | back_inserter(m_elements)); |
| 694 |
| 695 | m_segments += segment; |
| 696 | mLengthDirty = true; |
| 697 | } |
| 698 |
| 699 | V_END_NAMESPACE |
| 700 |