From cd44428c54a7f00de7e1a2a5d7cf03678280e479 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Mon, 23 Oct 2023 22:49:36 +0200 Subject: [PATCH] [orx-shapes] Add segmentIndex indirection --- .../kotlin/adjust/ContourAdjuster.kt | 14 +++++++----- .../kotlin/adjust/ContourAdjusterEdge.kt | 22 +++++++++---------- .../kotlin/adjust/ContourAdjusterVertex.kt | 8 ++++--- .../src/jvmDemo/kotlin/DemoAdjustContour05.kt | 14 ++++++------ 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjuster.kt b/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjuster.kt index 65ec6d94e..034ef79a7 100644 --- a/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjuster.kt +++ b/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjuster.kt @@ -22,13 +22,15 @@ class ContourAdjuster(var contour: ShapeContour) { private var vertexWorkingSet = emptyList() private var edgeWorkingSet = emptyList() + private var vertexHead = emptyList() + private var edgeHead = emptyList() /** * the selected vertex */ val vertex: ContourAdjusterVertex get() { - return ContourAdjusterVertex(this, vertexIndices.first()) + return ContourAdjusterVertex(this, { vertexIndices.first() } ) } val vertices: Sequence @@ -36,9 +38,9 @@ class ContourAdjuster(var contour: ShapeContour) { vertexWorkingSet = vertexIndices return sequence { while (vertexWorkingSet.isNotEmpty()) { - val head = vertexWorkingSet.first() + vertexHead = vertexWorkingSet.take(1) vertexWorkingSet = vertexWorkingSet.drop(1) - yield(ContourAdjusterVertex(this@ContourAdjuster, head)) + yield(ContourAdjusterVertex(this@ContourAdjuster, { vertexHead.first() })) } } } @@ -49,7 +51,7 @@ class ContourAdjuster(var contour: ShapeContour) { */ val edge: ContourAdjusterEdge get() { - return ContourAdjusterEdge(this, edgeIndices.first()) + return ContourAdjusterEdge(this, { edgeIndices.first() }) } val edges: Sequence @@ -57,9 +59,9 @@ class ContourAdjuster(var contour: ShapeContour) { edgeWorkingSet = edgeIndices return sequence { while (edgeWorkingSet.isNotEmpty()) { - val head = edgeWorkingSet.first() + edgeHead = edgeWorkingSet.take(1) edgeWorkingSet = edgeWorkingSet.drop(1) - yield(ContourAdjusterEdge(this@ContourAdjuster, head)) + yield(ContourAdjusterEdge(this@ContourAdjuster, { edgeHead.first() })) } } } diff --git a/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterEdge.kt b/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterEdge.kt index db80a6a4b..94329cce8 100644 --- a/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterEdge.kt +++ b/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterEdge.kt @@ -2,7 +2,7 @@ package org.openrndr.extra.shapes.adjust import org.openrndr.math.Vector2 -data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segmentIndex: Int) { +data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segmentIndex: () -> Int) { /** * A [ContourAdjusterVertex] interface for the start-vertex of the edge @@ -14,17 +14,17 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment * A [ContourAdjusterVertex] interface for the end-vertex of the edge */ val end - get() = ContourAdjusterVertex(contourAdjuster, (segmentIndex + 1).mod(contourAdjuster.contour.segments.size)) + get() = ContourAdjusterVertex(contourAdjuster, { (segmentIndex() + 1).mod(contourAdjuster.contour.segments.size) } ) /** * A link to the edge before this edge */ val previous: ContourAdjusterEdge? get() = if (contourAdjuster.contour.closed) { - this.copy(segmentIndex = (segmentIndex - 1).mod(contourAdjuster.contour.segments.size)) + this.copy(segmentIndex = { (segmentIndex() - 1).mod(contourAdjuster.contour.segments.size) }) } else { - if (segmentIndex > 0) { - this.copy(segmentIndex = segmentIndex - 1) + if (segmentIndex() > 0) { + this.copy(segmentIndex = { segmentIndex() - 1 }) } else { null } @@ -35,20 +35,20 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment */ val next: ContourAdjusterEdge? get() = if (contourAdjuster.contour.closed) { - this.copy(segmentIndex = (segmentIndex + 1).mod(contourAdjuster.contour.segments.size)) + this.copy(segmentIndex = { (segmentIndex() + 1).mod(contourAdjuster.contour.segments.size) }) } else { - if (segmentIndex < contourAdjuster.contour.segments.size - 1) { - this.copy(segmentIndex = segmentIndex + 1) + if (segmentIndex() < contourAdjuster.contour.segments.size - 1) { + this.copy(segmentIndex = { segmentIndex() + 1 } ) } else { null } } fun select() { - contourAdjuster.selectEdge(segmentIndex) + contourAdjuster.selectEdge(segmentIndex()) } private fun wrap(block: ContourEdge.() -> ContourEdge) { - val newEdge = ContourEdge(contourAdjuster.contour, segmentIndex).block() + val newEdge = ContourEdge(contourAdjuster.contour, segmentIndex()).block() contourAdjuster.contour = newEdge.contour contourAdjuster.updateSelection(newEdge.adjustments) } @@ -66,7 +66,7 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment fun sub(t0:Double, t1: Double, updateTangents: Boolean = true) { contourAdjuster.contour = - ContourEdge(contourAdjuster.contour, segmentIndex) + ContourEdge(contourAdjuster.contour, segmentIndex()) .subbed(t0, t1) .contour } diff --git a/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterVertex.kt b/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterVertex.kt index f7de6494d..c70ea530b 100644 --- a/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterVertex.kt +++ b/orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterVertex.kt @@ -3,15 +3,17 @@ package org.openrndr.extra.shapes.adjust import org.openrndr.extra.shapes.vertex.ContourVertex import org.openrndr.math.Vector2 -class ContourAdjusterVertex(val contourAdjuster: ContourAdjuster, val segmentIndex: Int) { +class ContourAdjusterVertex(val contourAdjuster: ContourAdjuster, val segmentIndex: () -> Int) { private fun wrap(block: ContourVertex.() -> ContourVertex) { - val newVertex = ContourVertex(contourAdjuster.contour, segmentIndex).block() + val newVertex = ContourVertex(contourAdjuster.contour, segmentIndex()).block() contourAdjuster.contour = newVertex.contour contourAdjuster.updateSelection(newVertex.adjustments) } + fun select() { - contourAdjuster.selectVertex(segmentIndex) + contourAdjuster.selectVertex(segmentIndex()) } + fun remove(updateTangents: Boolean = true) = wrap { remove(updateTangents) } fun moveBy(translation: Vector2, updateTangents: Boolean = true) = wrap { movedBy(translation, updateTangents) } fun rotate(rotationInDegrees: Double) = wrap { rotatedBy(rotationInDegrees) } diff --git a/orx-shapes/src/jvmDemo/kotlin/DemoAdjustContour05.kt b/orx-shapes/src/jvmDemo/kotlin/DemoAdjustContour05.kt index 95b4717a4..6a58392be 100644 --- a/orx-shapes/src/jvmDemo/kotlin/DemoAdjustContour05.kt +++ b/orx-shapes/src/jvmDemo/kotlin/DemoAdjustContour05.kt @@ -2,6 +2,7 @@ import org.openrndr.application import org.openrndr.color.ColorRGBa import org.openrndr.extra.shapes.adjust.adjustContour import org.openrndr.shape.Circle +import kotlin.math.cos fun main() { application { @@ -17,13 +18,12 @@ fun main() { contour = adjustContour(contour) { selectEdges(0, 1, 2, 3) edges.forEachIndexed { index, it -> - it.replaceWith(0.5) -// if (index == seconds.mod(4.0).toInt()) { -// it.replaceWith(0.5) -// } else { -// val v = cos(seconds) * 0.15 + 0.25 -// it.sub(0.5 - v, 0.5 + v) -// } + if (index == seconds.mod(4.0).toInt()) { + it.replaceWith(0.5) + } else { + val v = cos(seconds) * 0.15 + 0.25 + it.sub(0.5 - v, 0.5 + v) + } } } drawer.stroke = ColorRGBa.RED