Simplify vec2d uses
diff --git a/src/constraints/ik_constraint.cpp b/src/constraints/ik_constraint.cpp index ed049ec..7985526 100644 --- a/src/constraints/ik_constraint.cpp +++ b/src/constraints/ik_constraint.cpp
@@ -6,6 +6,10 @@ using namespace rive; +static float atan2(Vec2D v) { + return std::atan2(v.y(), v.x()); +} + void IKConstraint::buildDependencies() { Super::buildDependencies(); @@ -92,7 +96,7 @@ // Note this is directional, hence not transformMat2d Vec2D toTargetLocal = Vec2D::transformDir(toTarget, iworld); - float r = std::atan2(toTargetLocal[1], toTargetLocal[0]); + float r = atan2(toTargetLocal); constrainRotation(*fk1, r); fk1->angle = r; @@ -135,20 +139,20 @@ b2->tipWorldTranslation(pB); Vec2D avLocal = Vec2D::transformDir(pB - pC, secondChildWorldInverse); - float angleCorrection = -std::atan2(avLocal[1], avLocal[0]); + float angleCorrection = -atan2(avLocal); if (invertDirection()) { - r1 = std::atan2(cv[1], cv[0]) - A; + r1 = atan2(cv) - A; r2 = -C + math::PI + angleCorrection; } else { - r1 = A + std::atan2(cv[1], cv[0]); + r1 = A + atan2(cv); r2 = C - math::PI + angleCorrection; } } else if (invertDirection()) { - r1 = std::atan2(cv[1], cv[0]) - A; + r1 = atan2(cv) - A; r2 = -C + math::PI; } else { - r1 = A + std::atan2(cv[1], cv[0]); + r1 = A + atan2(cv); r2 = C - math::PI; }
diff --git a/src/shapes/metrics_path.cpp b/src/shapes/metrics_path.cpp index d6d5639..0190008 100644 --- a/src/shapes/metrics_path.cpp +++ b/src/shapes/metrics_path.cpp
@@ -229,16 +229,13 @@ const PathPart& part = m_Parts[i]; switch (part.type) { case PathPart::line: { - const Vec2D& point = m_TransformedPoints[part.offset]; - result->lineTo(point[0], point[1]); + result->line(m_TransformedPoints[part.offset]); break; } default: { - const Vec2D& point1 = m_TransformedPoints[part.offset]; - const Vec2D& point2 = m_TransformedPoints[part.offset + 1]; - const Vec2D& point3 = m_TransformedPoints[part.offset + 2]; - result->cubicTo( - point1[0], point1[1], point2[0], point2[1], point3[0], point3[1]); + result->cubic(m_TransformedPoints[part.offset + 0], + m_TransformedPoints[part.offset + 1], + m_TransformedPoints[part.offset + 2]); break; } } @@ -253,15 +250,13 @@ const PathPart& part = m_Parts[index]; switch (part.type) { case PathPart::line: { - const Vec2D& from = m_TransformedPoints[part.offset - 1]; - const Vec2D& to = m_TransformedPoints[part.offset]; - Vec2D dir = to - from; + const Vec2D from = m_TransformedPoints[part.offset - 1]; + const Vec2D to = m_TransformedPoints[part.offset]; + const Vec2D dir = to - from; if (moveTo) { - Vec2D point = from + dir * startT; - result->moveTo(point[0], point[1]); + result->move(from + dir * startT); } - dir = from + dir * endT; - result->lineTo(dir[0], dir[1]); + result->line(from + dir * endT); break; } @@ -325,29 +320,27 @@ // Start is 0, so split at end and keep the left side. computeHull(from, fromOut, toIn, to, endT, hull); if (moveTo) { - result->moveTo(from[0], from[1]); + result->move(from); } - result->cubicTo( - hull[0][0], hull[0][1], hull[3][0], hull[3][1], hull[5][0], hull[5][1]); + result->cubic(hull[0], hull[3], hull[5]); } else { // Split at start since it's non 0. computeHull(from, fromOut, toIn, to, startT, hull); if (moveTo) { // Move to first point on the right side. - result->moveTo(hull[5][0], hull[5][1]); + result->move(hull[5]); } if (endT == 1.0f) { // End is 1, so no further split is necessary just cubicTo // the remaining right side. - result->cubicTo(hull[4][0], hull[4][1], hull[2][0], hull[2][1], to[0], to[1]); + result->cubic(hull[4], hull[2], to); } else { // End is not 1, so split again and cubic to the left side // of the split and remap endT to the new curve range computeHull( hull[5], hull[4], hull[2], to, (endT - startT) / (1.0f - startT), hull); - result->cubicTo( - hull[0][0], hull[0][1], hull[3][0], hull[3][1], hull[5][0], hull[5][1]); + result->cubic(hull[0], hull[3], hull[5]); } } break;
diff --git a/src/shapes/path.cpp b/src/shapes/path.cpp index 520c88b..0b11b30 100644 --- a/src/shapes/path.cpp +++ b/src/shapes/path.cpp
@@ -54,24 +54,19 @@ auto firstPoint = vertices[0]; // Init out to translation - float outX, outY; + Vec2D out; bool prevIsCubic; - float startX, startY; - float startInX, startInY; + Vec2D start, startIn; bool startIsCubic; if (firstPoint->is<CubicVertex>()) { auto cubic = firstPoint->as<CubicVertex>(); startIsCubic = prevIsCubic = true; - auto inPoint = cubic->renderIn(); - startInX = inPoint[0]; - startInY = inPoint[1]; - auto outPoint = cubic->renderOut(); - outX = outPoint[0]; - outY = outPoint[1]; - auto translation = cubic->renderTranslation(); - commandPath.moveTo(startX = translation[0], startY = translation[1]); + startIn = cubic->renderIn(); + out = cubic->renderOut(); + start = cubic->renderTranslation(); + commandPath.move(start); } else { startIsCubic = prevIsCubic = false; auto point = *firstPoint->as<StraightVertex>(); @@ -100,24 +95,17 @@ float renderRadius = std::min(toPrevLength, std::min(toNextLength, radius)); - Vec2D translation = Vec2D::scaleAndAdd(pos, toPrev, renderRadius); - commandPath.moveTo(startInX = startX = translation[0], - startInY = startY = translation[1]); + startIn = start = Vec2D::scaleAndAdd(pos, toPrev, renderRadius); + commandPath.move(startIn); Vec2D outPoint = Vec2D::scaleAndAdd(pos, toPrev, icircleConstant * renderRadius); Vec2D inPoint = Vec2D::scaleAndAdd(pos, toNext, icircleConstant * renderRadius); - Vec2D posNext = Vec2D::scaleAndAdd(pos, toNext, renderRadius); - commandPath.cubicTo(outPoint[0], - outPoint[1], - inPoint[0], - inPoint[1], - outX = posNext[0], - outY = posNext[1]); + out = Vec2D::scaleAndAdd(pos, toNext, renderRadius); + commandPath.cubic(outPoint, inPoint, out); prevIsCubic = false; } else { - auto translation = point.renderTranslation(); - commandPath.moveTo(startInX = startX = outX = translation[0], - startInY = startY = outY = translation[1]); + startIn = start = out = point.renderTranslation(); + commandPath.move(out); } } @@ -129,18 +117,16 @@ auto inPoint = cubic->renderIn(); auto translation = cubic->renderTranslation(); - commandPath.cubicTo(outX, outY, inPoint[0], inPoint[1], translation[0], translation[1]); + commandPath.cubic(out, inPoint, translation); prevIsCubic = true; - auto outPoint = cubic->renderOut(); - outX = outPoint[0]; - outY = outPoint[1]; + out = cubic->renderOut(); } else { auto point = *vertex->as<StraightVertex>(); Vec2D pos = point.renderTranslation(); if (auto radius = point.radius(); radius > 0.0f) { - Vec2D toPrev = Vec2D(outX, outY) - pos; + Vec2D toPrev = out - pos; auto toPrevLength = toPrev.length(); toPrev[0] /= toPrevLength; toPrev[1] /= toPrevLength; @@ -158,40 +144,32 @@ Vec2D translation = Vec2D::scaleAndAdd(pos, toPrev, renderRadius); if (prevIsCubic) { - commandPath.cubicTo( - outX, outY, translation[0], translation[1], translation[0], translation[1]); + commandPath.cubic(out, translation, translation); } else { - commandPath.lineTo(translation[0], translation[1]); + commandPath.line(translation); } Vec2D outPoint = Vec2D::scaleAndAdd(pos, toPrev, icircleConstant * renderRadius); Vec2D inPoint = Vec2D::scaleAndAdd(pos, toNext, icircleConstant * renderRadius); - Vec2D posNext = Vec2D::scaleAndAdd(pos, toNext, renderRadius); - commandPath.cubicTo(outPoint[0], - outPoint[1], - inPoint[0], - inPoint[1], - outX = posNext[0], - outY = posNext[1]); + out = Vec2D::scaleAndAdd(pos, toNext, renderRadius); + commandPath.cubic(outPoint, inPoint, out); prevIsCubic = false; } else if (prevIsCubic) { - float x = pos[0]; - float y = pos[1]; - commandPath.cubicTo(outX, outY, x, y, x, y); + commandPath.cubic(out, pos, pos); prevIsCubic = false; - outX = x; - outY = y; + out = pos; } else { - commandPath.lineTo(outX = pos[0], outY = pos[1]); + out = pos; + commandPath.line(out); } } } if (isClosed) { if (prevIsCubic || startIsCubic) { - commandPath.cubicTo(outX, outY, startInX, startInY, startX, startY); + commandPath.cubic(out, startIn, start); } else { - commandPath.lineTo(startX, startY); + commandPath.line(start); } commandPath.close(); }