diff --git a/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommands.kt b/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommands.kt index e4ef4505..03181362 100644 --- a/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommands.kt +++ b/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommands.kt @@ -4,6 +4,7 @@ import com.jzbrooks.vgo.core.graphic.PathElement import com.jzbrooks.vgo.core.graphic.command.* import com.jzbrooks.vgo.core.graphic.command.CommandVariant import com.jzbrooks.vgo.core.util.math.Point +import com.jzbrooks.vgo.core.util.math.computeAbsoluteCoordinates import kotlin.math.abs /** @@ -14,7 +15,9 @@ class RemoveRedundantCommands : TopDownOptimization, PathElementVisitor { val commandCount = pathElement.commands.size if (commandCount > 0) { - val commands = mutableListOf((pathElement.commands.first() as MoveTo).copy()) + val firstCommand = pathElement.commands.first() as MoveTo + + val commands = mutableListOf(firstCommand) loop@ for (current in pathElement.commands.drop(1)) { assert((current as? ParameterizedCommand<*>)?.variant != CommandVariant.ABSOLUTE) @@ -32,6 +35,17 @@ class RemoveRedundantCommands : TopDownOptimization, PathElementVisitor { commands.add(current) } + if (pathElement.commands.last() is ClosePath) { + val commandsWithoutFinalClosePath = commands.dropLast(1) + val current = computeAbsoluteCoordinates(commandsWithoutFinalClosePath) + val firstCurrentPoint = firstCommand.parameters.last() + + if (current == firstCurrentPoint) { + pathElement.commands = commandsWithoutFinalClosePath + return + } + } + pathElement.commands = commands } } diff --git a/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommandsTests.kt b/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommandsTests.kt index 2ef50738..0c8c9f59 100644 --- a/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommandsTests.kt +++ b/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/RemoveRedundantCommandsTests.kt @@ -1,6 +1,7 @@ package com.jzbrooks.vgo.core.optimization import assertk.assertThat +import assertk.assertions.containsNone import assertk.assertions.hasSize import com.jzbrooks.vgo.core.graphic.Path import com.jzbrooks.vgo.core.graphic.command.ClosePath @@ -43,4 +44,21 @@ class RemoveRedundantCommandsTests { assertThat(path.commands).hasSize(4) } + + @Test + fun testRedundantClosePathsAreRemoved() { + val path = Path( + listOf( + MoveTo(CommandVariant.ABSOLUTE, listOf(Point(100f, 1f))), + LineTo(CommandVariant.RELATIVE, listOf(Point(1f, 1f))), + LineTo(CommandVariant.RELATIVE, listOf(Point(2f, 1f))), + LineTo(CommandVariant.RELATIVE, listOf(Point(-3f, -2f))), + ClosePath + ) + ) + + RemoveRedundantCommands().visit(path) + + assertThat(path.commands).containsNone(ClosePath) + } } \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/charging_battery_optimized.xml b/vgo/src/integration-test/resources/baseline/charging_battery_optimized.xml index 1a3d1a75..c1524196 100644 --- a/vgo/src/integration-test/resources/baseline/charging_battery_optimized.xml +++ b/vgo/src/integration-test/resources/baseline/charging_battery_optimized.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.svg b/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.svg index 727b45c5..8dce9b6f 100644 --- a/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.svg +++ b/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.xml b/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.xml index fbded17c..430b1406 100644 --- a/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.xml +++ b/vgo/src/integration-test/resources/baseline/eleven_below_single_optimized.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/guacamole_optimized.svg b/vgo/src/integration-test/resources/baseline/guacamole_optimized.svg index b6c850f9..1ae45498 100644 --- a/vgo/src/integration-test/resources/baseline/guacamole_optimized.svg +++ b/vgo/src/integration-test/resources/baseline/guacamole_optimized.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/nasa_optimized.svg b/vgo/src/integration-test/resources/baseline/nasa_optimized.svg index 8cbdf615..1f961e68 100644 --- a/vgo/src/integration-test/resources/baseline/nasa_optimized.svg +++ b/vgo/src/integration-test/resources/baseline/nasa_optimized.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/nasa_optimized.xml b/vgo/src/integration-test/resources/baseline/nasa_optimized.xml index c1b9e283..f162ba35 100644 --- a/vgo/src/integration-test/resources/baseline/nasa_optimized.xml +++ b/vgo/src/integration-test/resources/baseline/nasa_optimized.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/simple_heart_optimized.svg b/vgo/src/integration-test/resources/baseline/simple_heart_optimized.svg index fec9a3ea..2ecfb50c 100644 --- a/vgo/src/integration-test/resources/baseline/simple_heart_optimized.svg +++ b/vgo/src/integration-test/resources/baseline/simple_heart_optimized.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/simple_heart_optimized.xml b/vgo/src/integration-test/resources/baseline/simple_heart_optimized.xml index 5252136b..fd8dd292 100644 --- a/vgo/src/integration-test/resources/baseline/simple_heart_optimized.xml +++ b/vgo/src/integration-test/resources/baseline/simple_heart_optimized.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/tiger_optimized.svg b/vgo/src/integration-test/resources/baseline/tiger_optimized.svg index 02fe4e2f..9082392d 100644 --- a/vgo/src/integration-test/resources/baseline/tiger_optimized.svg +++ b/vgo/src/integration-test/resources/baseline/tiger_optimized.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/tiger_optimized.xml b/vgo/src/integration-test/resources/baseline/tiger_optimized.xml index d3dfef95..cc00750b 100644 --- a/vgo/src/integration-test/resources/baseline/tiger_optimized.xml +++ b/vgo/src/integration-test/resources/baseline/tiger_optimized.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/vgo/src/integration-test/resources/baseline/visibility_strike_optimized.xml b/vgo/src/integration-test/resources/baseline/visibility_strike_optimized.xml index 4b3f302c..d4396258 100644 --- a/vgo/src/integration-test/resources/baseline/visibility_strike_optimized.xml +++ b/vgo/src/integration-test/resources/baseline/visibility_strike_optimized.xml @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file