Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid merging intersecting paths #109

Merged
merged 28 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
af3c863
Pull out logic for absolutizing commands
jzbrooks Oct 13, 2024
290aa64
Start bounding box code
jzbrooks Oct 30, 2024
f8ec9e2
Require interpolation to happen on full cubic curves
jzbrooks Oct 31, 2024
b75f9a0
Add interpolation functions
jzbrooks Oct 31, 2024
6139a80
Work on bounding box approach
jzbrooks Nov 15, 2024
38ba80f
Make curve boxes work
jzbrooks Nov 16, 2024
de1393f
Fix bounding box for smooth cubics
jzbrooks Nov 18, 2024
607a0d1
Fix current point tracking
jzbrooks Nov 18, 2024
f558493
Support polycommands for lines
jzbrooks Nov 24, 2024
7d37a9a
Handle cubic polycommands
jzbrooks Nov 24, 2024
3bf169e
Support smooth polycubics
jzbrooks Nov 24, 2024
77bb16a
Support polyquadratic curves
jzbrooks Nov 24, 2024
4466e9e
Support smooth polyquadratic curves
jzbrooks Nov 24, 2024
654a5b3
Add some helpful arc code - WIP
jzbrooks Nov 25, 2024
aa4e3a7
Restructure endpoint parameterization code
jzbrooks Nov 25, 2024
ffbef7d
Handle arcs with endpoint-stretched radii
jzbrooks Nov 25, 2024
ea6ecd1
Handle rotated arcs
jzbrooks Nov 26, 2024
662a600
Add bounding box tests for real art
jzbrooks Nov 27, 2024
8a128e7
Cleanup
jzbrooks Nov 27, 2024
3373b81
Add rectangle intersection logic
jzbrooks Nov 27, 2024
475745f
Avoid merging intersecting paths
jzbrooks Nov 27, 2024
f59c4da
Update tests
jzbrooks Nov 27, 2024
0d1e390
Add a new test
jzbrooks Nov 27, 2024
c8819ba
Update api
jzbrooks Nov 27, 2024
4916031
Update changelog
jzbrooks Nov 27, 2024
de77de2
Clarify comment
jzbrooks Nov 28, 2024
6265307
Remove interpolation resolution from public api
jzbrooks Nov 28, 2024
f9d8022
Tidy comment
jzbrooks Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
## Unreleased

### Added
- `com.jzbrooks.vgo.core.util.math.Surveyor`, which computes the bounding box of an arbitrary list of commands
- Bezier curve interpolation for all variants and elliptical arc bounding box functions

### Changed

- `vgo-plugin` (`com.jzbrooks.vgo.plugin`) no longer requires a particular version of Android Gradle Plugin.
Note: `:vgo` is now an abstract implementation of the tool which does not assume either a cli or plugin context. CLI related logic has been relocated into `:vgo-cli`.
Note: `:vgo` is an abstract implementation of the tool which does not assume either a cli or plugin context. CLI related logic has been relocated into `:vgo-cli`.
- **Breaking:** `CubicCurve<*>.interpolate` has been split into `CubicBezierCurve.interpolate` and `SmoothCubicBezierCurve.interpolate`
- Paths with an even odd fill rule can be merged

### Deprecated

### Removed

### Fixed

- Overlapping paths are no longer merged, which avoids some image warping issues (#88, #101)
- Conversions without a specified output file will write a file the file extension corresponding to the format.
- Decimal separators are locale-invariant.
- Crash when using the cli to convert an svg containing a clip path to vector drawable.
- Crash when using the cli to convert an svg containing a clip path to vector drawable.

### Security

Expand Down
63 changes: 56 additions & 7 deletions vgo-core/api/vgo-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,23 @@ public final class com/jzbrooks/vgo/core/util/element/TraverseKt {
public static final fun traverseTopDown (Lcom/jzbrooks/vgo/core/graphic/Element;Lkotlin/jvm/functions/Function1;)Lcom/jzbrooks/vgo/core/graphic/Element;
}

public final class com/jzbrooks/vgo/core/util/math/CenterParameterization {
public fun <init> (Lcom/jzbrooks/vgo/core/util/math/Point;FFD)V
public final fun component1 ()Lcom/jzbrooks/vgo/core/util/math/Point;
public final fun component2 ()F
public final fun component3 ()F
public final fun component4 ()D
public final fun copy (Lcom/jzbrooks/vgo/core/util/math/Point;FFD)Lcom/jzbrooks/vgo/core/util/math/CenterParameterization;
public static synthetic fun copy$default (Lcom/jzbrooks/vgo/core/util/math/CenterParameterization;Lcom/jzbrooks/vgo/core/util/math/Point;FFDILjava/lang/Object;)Lcom/jzbrooks/vgo/core/util/math/CenterParameterization;
public fun equals (Ljava/lang/Object;)Z
public final fun getCenter ()Lcom/jzbrooks/vgo/core/util/math/Point;
public final fun getPhi ()D
public final fun getRadiusX ()F
public final fun getRadiusY ()F
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/jzbrooks/vgo/core/util/math/Circle {
public fun <init> (Lcom/jzbrooks/vgo/core/util/math/Point;F)V
public final fun component1 ()Lcom/jzbrooks/vgo/core/util/math/Point;
Expand All @@ -645,14 +662,20 @@ public final class com/jzbrooks/vgo/core/util/math/CommandsKt {
}

public final class com/jzbrooks/vgo/core/util/math/CurvesKt {
public static final fun computeBoundingBox (Lcom/jzbrooks/vgo/core/graphic/command/EllipticalArcCurve$Parameter;Lcom/jzbrooks/vgo/core/graphic/command/CommandVariant;Lcom/jzbrooks/vgo/core/util/math/Point;)Lcom/jzbrooks/vgo/core/util/math/Rectangle;
public static final fun computeCenterParameterization (Lcom/jzbrooks/vgo/core/graphic/command/EllipticalArcCurve$Parameter;Lcom/jzbrooks/vgo/core/graphic/command/CommandVariant;Lcom/jzbrooks/vgo/core/util/math/Point;)Lcom/jzbrooks/vgo/core/util/math/CenterParameterization;
public static final fun findArcAngle (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;Lcom/jzbrooks/vgo/core/util/math/Circle;)F
public static final fun fitCircle (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;F)Lcom/jzbrooks/vgo/core/util/math/Circle;
public static synthetic fun fitCircle$default (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;FILjava/lang/Object;)Lcom/jzbrooks/vgo/core/util/math/Circle;
public static final fun interpolate (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;F)Lcom/jzbrooks/vgo/core/util/math/Point;
public static final fun isConvex (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;F)Z
public static synthetic fun isConvex$default (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;FILjava/lang/Object;)Z
public static final fun liesOnCircle (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;Lcom/jzbrooks/vgo/core/util/math/Circle;F)Z
public static synthetic fun liesOnCircle$default (Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;Lcom/jzbrooks/vgo/core/util/math/Circle;FILjava/lang/Object;)Z
public static final fun fitCircle (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;F)Lcom/jzbrooks/vgo/core/util/math/Circle;
public static synthetic fun fitCircle$default (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;FILjava/lang/Object;)Lcom/jzbrooks/vgo/core/util/math/Circle;
public static final fun interpolate (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve$Parameter;Lcom/jzbrooks/vgo/core/util/math/Point;F)Lcom/jzbrooks/vgo/core/util/math/Point;
public static final fun interpolate (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;Lcom/jzbrooks/vgo/core/util/math/Point;F)Lcom/jzbrooks/vgo/core/util/math/Point;
public static final fun interpolate (Lcom/jzbrooks/vgo/core/graphic/command/QuadraticBezierCurve$Parameter;Lcom/jzbrooks/vgo/core/util/math/Point;F)Lcom/jzbrooks/vgo/core/util/math/Point;
public static final fun interpolate (Lcom/jzbrooks/vgo/core/graphic/command/SmoothCubicBezierCurve$Parameter;Lcom/jzbrooks/vgo/core/util/math/Point;Lcom/jzbrooks/vgo/core/util/math/Point;F)Lcom/jzbrooks/vgo/core/util/math/Point;
public static final fun interpolateSmoothQuadraticBezierCurve (Lcom/jzbrooks/vgo/core/util/math/Point;Lcom/jzbrooks/vgo/core/util/math/Point;Lcom/jzbrooks/vgo/core/util/math/Point;F)Lcom/jzbrooks/vgo/core/util/math/Point;
public static final fun isConvex (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;F)Z
public static synthetic fun isConvex$default (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;FILjava/lang/Object;)Z
public static final fun liesOnCircle (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;Lcom/jzbrooks/vgo/core/util/math/Circle;F)Z
public static synthetic fun liesOnCircle$default (Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;Lcom/jzbrooks/vgo/core/util/math/Circle;FILjava/lang/Object;)Z
public static final fun toCubicBezierCurve (Lcom/jzbrooks/vgo/core/graphic/command/SmoothCubicBezierCurve;Lcom/jzbrooks/vgo/core/graphic/command/CubicCurve;)Lcom/jzbrooks/vgo/core/graphic/command/CubicBezierCurve;
}

Expand Down Expand Up @@ -729,6 +752,32 @@ public final class com/jzbrooks/vgo/core/util/math/Point$Companion {
public final fun getZERO ()Lcom/jzbrooks/vgo/core/util/math/Point;
}

public final class com/jzbrooks/vgo/core/util/math/Rectangle {
public fun <init> (FFFF)V
public final fun component1 ()F
public final fun component2 ()F
public final fun component3 ()F
public final fun component4 ()F
public final fun copy (FFFF)Lcom/jzbrooks/vgo/core/util/math/Rectangle;
public static synthetic fun copy$default (Lcom/jzbrooks/vgo/core/util/math/Rectangle;FFFFILjava/lang/Object;)Lcom/jzbrooks/vgo/core/util/math/Rectangle;
public fun equals (Ljava/lang/Object;)Z
public final fun getBottom ()F
public final fun getLeft ()F
public final fun getRight ()F
public final fun getTop ()F
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/jzbrooks/vgo/core/util/math/RectangleKt {
public static final fun intersects (Lcom/jzbrooks/vgo/core/util/math/Rectangle;Lcom/jzbrooks/vgo/core/util/math/Rectangle;)Z
}

public final class com/jzbrooks/vgo/core/util/math/Surveyor {
public fun <init> ()V
public final fun findBoundingBox (Ljava/util/List;)Lcom/jzbrooks/vgo/core/util/math/Rectangle;
}

public final class com/jzbrooks/vgo/core/util/math/Vector3 {
public fun <init> (FFF)V
public fun <init> (Lcom/jzbrooks/vgo/core/util/math/Point;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.jzbrooks.vgo.core.graphic.command.CubicBezierCurve
import com.jzbrooks.vgo.core.graphic.command.CubicCurve
import com.jzbrooks.vgo.core.graphic.command.EllipticalArcCurve
import com.jzbrooks.vgo.core.graphic.command.SmoothCubicBezierCurve
import com.jzbrooks.vgo.core.util.math.Circle
import com.jzbrooks.vgo.core.util.math.computeAbsoluteCoordinates
import com.jzbrooks.vgo.core.util.math.findArcAngle
import com.jzbrooks.vgo.core.util.math.fitCircle
Expand Down Expand Up @@ -101,52 +102,53 @@ class ConvertCurvesToArcs(

val previous = computeAbsoluteCoordinates(commands.take(i))

while (nextCommand is CubicCurve<*> && nextCommand.isConvex() && nextCommand.liesOnCircle(relativeCircle)) {
val normalizedNext =
if (nextCommand is SmoothCubicBezierCurve) {
nextCommand.toCubicBezierCurve(pendingCurves.last())
} else {
nextCommand
}
if (nextCommand != null) {
var currentCurve = currentCommand
var nextCurve = convertToCircularCubicCurve(nextCommand, relativeCircle, currentCurve)

check(normalizedNext is CubicBezierCurve)

pendingCurves.add(nextCommand)
while (nextCommand is CubicCurve<*> && nextCurve != null) {
pendingCurves.add(nextCommand)

val next = computeAbsoluteCoordinates(commands.take(j + 1))
val next = computeAbsoluteCoordinates(commands.take(j + 1))

angle += normalizedNext.findArcAngle(relativeCircle)
angle += nextCurve.findArcAngle(relativeCircle)

if (angle - 2 * Math.PI > 1e-3) {
break
}
if (angle - 2 * Math.PI > 1e-3) {
break
}

if (angle > Math.PI) arc.parameters[0].arc = EllipticalArcCurve.ArcFlag.LARGE
if (angle > Math.PI) arc.parameters[0].arc = EllipticalArcCurve.ArcFlag.LARGE

if (2 * Math.PI - angle > 1e-3) {
arc.parameters[0].end = next - previous
} else {
arc.parameters[0].end = (relativeCircle.center - normalizedNext.parameters[0].end) * 2f
ellipticalArcs.add(
EllipticalArcCurve(
CommandVariant.RELATIVE,
listOf(
EllipticalArcCurve.Parameter(
radius,
radius,
0f,
EllipticalArcCurve.ArcFlag.SMALL,
sweep,
next - (previous + arc.parameters[0].end),
if (2 * Math.PI - angle > 1e-3) {
arc.parameters[0].end = next - previous
} else {
arc.parameters[0].end = (relativeCircle.center - nextCurve.parameters[0].end) * 2f
ellipticalArcs.add(
EllipticalArcCurve(
CommandVariant.RELATIVE,
listOf(
EllipticalArcCurve.Parameter(
radius,
radius,
0f,
EllipticalArcCurve.ArcFlag.SMALL,
sweep,
next - (previous + arc.parameters[0].end),
),
),
),
),
)
break
}
)
break
}

relativeCircle.center += normalizedNext.parameters[0].end
nextCommand = commands.getOrNull(++j)
relativeCircle.center += nextCurve.parameters[0].end
nextCommand = commands.getOrNull(++j)
currentCurve = nextCurve
nextCurve =
nextCommand?.let {
convertToCircularCubicCurve(it, relativeCircle, currentCurve)
}
}
}

// If the next curve is a shorthand, it must be converted
Expand Down Expand Up @@ -245,4 +247,15 @@ class ConvertCurvesToArcs(

return newCommands
}

private fun convertToCircularCubicCurve(
command: Command,
relativeCircle: Circle,
previousCurve: CubicBezierCurve?,
): CubicBezierCurve? =
when (command) {
is CubicBezierCurve -> command
is SmoothCubicBezierCurve -> command.toCubicBezierCurve(previousCurve!!)
else -> null
}?.takeIf { it.isConvex() && it.liesOnCircle(relativeCircle) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import com.jzbrooks.vgo.core.graphic.Extra
import com.jzbrooks.vgo.core.graphic.Graphic
import com.jzbrooks.vgo.core.graphic.Group
import com.jzbrooks.vgo.core.graphic.Path
import com.jzbrooks.vgo.core.util.math.Surveyor
import com.jzbrooks.vgo.core.util.math.intersects

/**
* Merges multiple paths into a single path where possible
*/
class MergePaths : BottomUpOptimization {
private val surveyor = Surveyor()

override fun visit(graphic: Graphic) = merge(graphic)

override fun visit(group: Group) = merge(group)
Expand Down Expand Up @@ -57,14 +61,10 @@ class MergePaths : BottomUpOptimization {
for (current in paths.drop(1)) {
val previous = mergedPaths.last()

// Avoid merging paths with even odd fill rule, because
// the merge might cause some paths to be considered 'interior'
// according to those rules when they were previously exterior
// in their own paths.
//
// There might be a reasonable way to deduce that situation more
// specifically, which could enable merging of some even odd paths.
if (!haveSameAttributes(current, previous) || current.fillRule == Path.FillRule.EVEN_ODD) {
// Intersecting paths can cause problems with path fill rules and with transparency.
if (!haveSameAttributes(current, previous) ||
surveyor.findBoundingBox(previous.commands) intersects surveyor.findBoundingBox(current.commands)
) {
mergedPaths.add(current)
} else {
previous.commands += current.commands
Expand Down
Loading