Skip to content

Commit

Permalink
[orx-shapes] Add segmentIndex indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Oct 23, 2023
1 parent 9336bbf commit cd44428
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
14 changes: 8 additions & 6 deletions orx-shapes/src/commonMain/kotlin/adjust/ContourAdjuster.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ class ContourAdjuster(var contour: ShapeContour) {
private var vertexWorkingSet = emptyList<Int>()
private var edgeWorkingSet = emptyList<Int>()

private var vertexHead = emptyList<Int>()
private var edgeHead = emptyList<Int>()

/**
* the selected vertex
*/
val vertex: ContourAdjusterVertex
get() {
return ContourAdjusterVertex(this, vertexIndices.first())
return ContourAdjusterVertex(this, { vertexIndices.first() } )
}

val vertices: Sequence<ContourAdjusterVertex>
get() {
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() }))
}
}
}
Expand All @@ -49,17 +51,17 @@ class ContourAdjuster(var contour: ShapeContour) {
*/
val edge: ContourAdjusterEdge
get() {
return ContourAdjusterEdge(this, edgeIndices.first())
return ContourAdjusterEdge(this, { edgeIndices.first() })
}

val edges: Sequence<ContourAdjusterEdge>
get() {
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() }))
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions orx-shapes/src/commonMain/kotlin/adjust/ContourAdjusterEdge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
14 changes: 7 additions & 7 deletions orx-shapes/src/jvmDemo/kotlin/DemoAdjustContour05.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit cd44428

Please sign in to comment.