Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit 3488766

Browse files
authored
Feature/improve.curves (#693)
* Improve Curves to support multiple separated paths * Small optimization
1 parent 5d5bd23 commit 3488766

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

korma/src/commonMain/kotlin/com/soywiz/korma/geom/BoundsBuilder.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ class BoundsBuilder {
5555
}
5656
fun add(rect: Rectangle): BoundsBuilder {
5757
if (rect.isNotEmpty) {
58-
add(rect.left, rect.top)
59-
add(rect.right, rect.bottom)
58+
addEvenEmpty(rect)
6059
}
6160
return this
6261
}
62+
fun addEvenEmpty(rect: Rectangle): BoundsBuilder {
63+
add(rect.left, rect.top)
64+
add(rect.right, rect.bottom)
65+
return this
66+
}
6367

6468
fun add(ps: Iterable<IPoint>, transform: Matrix): BoundsBuilder {
6569
for (p in ps) add(p, transform)

korma/src/commonMain/kotlin/com/soywiz/korma/geom/bezier/Bezier.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import com.soywiz.korma.geom.PointPool
1010
import com.soywiz.korma.geom.Rectangle
1111
import com.soywiz.korma.interpolation.interpolate
1212
import com.soywiz.korma.math.clamp
13+
import kotlin.jvm.JvmName
1314
import kotlin.math.hypot
1415
import kotlin.math.max
1516
import kotlin.math.min
1617

18+
@JvmName("ListCurves_toCurves")
19+
fun List<Curves>.toCurves() = Curves(this.flatMap { it.curves })
20+
@JvmName("ListCurve_toCurves")
1721
fun List<Curve>.toCurves() = Curves(this)
1822

1923
data class Curves(val curves: List<Curve>) : Curve {
@@ -41,7 +45,7 @@ data class Curves(val curves: List<Curve>) : Curve {
4145

4246
override fun getBounds(target: Rectangle): Rectangle {
4347
bb.reset()
44-
infos.fastForEach { bb.add(it.bounds) }
48+
infos.fastForEach { bb.addEvenEmpty(it.bounds) }
4549
return bb.getBounds(target)
4650
}
4751

korma/src/commonMain/kotlin/com/soywiz/korma/geom/vector/VectorPath.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ open class VectorPath(
7979
line: (x0: Double, y0: Double, x1: Double, y1: Double) -> Unit,
8080
quad: (x0: Double, y0: Double, x1: Double, y1: Double, x2: Double, y2: Double) -> Unit,
8181
cubic: (x0: Double, y0: Double, x1: Double, y1: Double, x2: Double, y2: Double, x3: Double, y3: Double) -> Unit,
82-
close: () -> Unit = {}
82+
close: () -> Unit = {},
83+
move: (x: Double, y: Double) -> Unit = { x, y -> },
84+
dummy: Unit = Unit // Prevents tailing lambda
8385
) {
8486
var mx = 0.0
8587
var my = 0.0
@@ -89,6 +91,7 @@ open class VectorPath(
8991
moveTo = { x, y ->
9092
mx = x; my = y
9193
lx = x; ly = y
94+
move(x, y)
9295
},
9396
lineTo = { x, y ->
9497
line(lx, ly, x, y)
@@ -548,10 +551,20 @@ fun VectorPath.applyTransform(m: Matrix?): VectorPath {
548551
return this
549552
}
550553

551-
fun VectorPath.getCurves(): Curves = arrayListOf<Curve>().also { out ->
554+
fun VectorPath.getCurvesLists(): List<Curves> = arrayListOf<List<Curve>>().also { out ->
555+
var current = arrayListOf<Curve>()
556+
fun flush() {
557+
if (current.isEmpty()) return
558+
out.add(current)
559+
current = arrayListOf()
560+
}
552561
visitEdges(
553-
line = { x0, y0, x1, y1 -> out += Curve.Line(x0, y0, x1, y1) },
554-
quad = { x0, y0, x1, y1, x2, y2 -> out += Bezier.Quad(x0, y0, x1, y1, x2, y2) },
555-
cubic = { x0, y0, x1, y1, x2, y2, x3, y3 -> out += Bezier.Cubic(x0, y0, x1, y1, x2, y2, x3, y3) },
562+
line = { x0, y0, x1, y1 -> current += Curve.Line(x0, y0, x1, y1) },
563+
quad = { x0, y0, x1, y1, x2, y2 -> current += Bezier.Quad(x0, y0, x1, y1, x2, y2) },
564+
cubic = { x0, y0, x1, y1, x2, y2, x3, y3 -> current += Bezier.Cubic(x0, y0, x1, y1, x2, y2, x3, y3) },
565+
move = { x, y -> flush() }
556566
)
557-
}.toCurves()
567+
flush()
568+
}.map { it.toCurves() }
569+
570+
fun VectorPath.getCurves(): Curves = getCurvesLists().flatMap { it.curves }.toCurves()

korma/src/commonTest/kotlin/com/soywiz/korma/geom/bezier/BezierTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.soywiz.korma.geom.Rectangle
55
import com.soywiz.korma.geom.map
66
import com.soywiz.korma.geom.shape.buildVectorPath
77
import com.soywiz.korma.geom.vector.getCurves
8+
import com.soywiz.korma.geom.vector.getCurvesLists
9+
import com.soywiz.korma.geom.vector.rect
810
import kotlin.math.roundToInt
911
import kotlin.test.Test
1012
import kotlin.test.assertEquals
@@ -51,4 +53,17 @@ class BezierTest {
5153
curves.getPoints(20).map { x, y -> "(${x.roundToInt()}, ${y.roundToInt()})" }.joinToString("\n")
5254
)
5355
}
56+
57+
@Test
58+
fun testCurveList() {
59+
val path = buildVectorPath {
60+
rect(0, 0, 100, 100)
61+
rect(300, 0, 100, 100)
62+
}
63+
val curves = path.getCurvesLists()
64+
assertEquals(2, curves.size)
65+
assertEquals(Rectangle(0, 0, 100, 100), curves[0].getBounds())
66+
assertEquals(Rectangle(300, 0, 100, 100), curves[1].getBounds())
67+
assertEquals(Rectangle(0, 0, 400, 100), curves.toCurves().getBounds())
68+
}
5469
}

0 commit comments

Comments
 (0)