[orx-shapes] Add segmentIndex indirection
This commit is contained in:
@@ -22,13 +22,15 @@ class ContourAdjuster(var contour: ShapeContour) {
|
|||||||
private var vertexWorkingSet = emptyList<Int>()
|
private var vertexWorkingSet = emptyList<Int>()
|
||||||
private var edgeWorkingSet = emptyList<Int>()
|
private var edgeWorkingSet = emptyList<Int>()
|
||||||
|
|
||||||
|
private var vertexHead = emptyList<Int>()
|
||||||
|
private var edgeHead = emptyList<Int>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the selected vertex
|
* the selected vertex
|
||||||
*/
|
*/
|
||||||
val vertex: ContourAdjusterVertex
|
val vertex: ContourAdjusterVertex
|
||||||
get() {
|
get() {
|
||||||
return ContourAdjusterVertex(this, vertexIndices.first())
|
return ContourAdjusterVertex(this, { vertexIndices.first() } )
|
||||||
}
|
}
|
||||||
|
|
||||||
val vertices: Sequence<ContourAdjusterVertex>
|
val vertices: Sequence<ContourAdjusterVertex>
|
||||||
@@ -36,9 +38,9 @@ class ContourAdjuster(var contour: ShapeContour) {
|
|||||||
vertexWorkingSet = vertexIndices
|
vertexWorkingSet = vertexIndices
|
||||||
return sequence {
|
return sequence {
|
||||||
while (vertexWorkingSet.isNotEmpty()) {
|
while (vertexWorkingSet.isNotEmpty()) {
|
||||||
val head = vertexWorkingSet.first()
|
vertexHead = vertexWorkingSet.take(1)
|
||||||
vertexWorkingSet = vertexWorkingSet.drop(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
|
val edge: ContourAdjusterEdge
|
||||||
get() {
|
get() {
|
||||||
return ContourAdjusterEdge(this, edgeIndices.first())
|
return ContourAdjusterEdge(this, { edgeIndices.first() })
|
||||||
}
|
}
|
||||||
|
|
||||||
val edges: Sequence<ContourAdjusterEdge>
|
val edges: Sequence<ContourAdjusterEdge>
|
||||||
@@ -57,9 +59,9 @@ class ContourAdjuster(var contour: ShapeContour) {
|
|||||||
edgeWorkingSet = edgeIndices
|
edgeWorkingSet = edgeIndices
|
||||||
return sequence {
|
return sequence {
|
||||||
while (edgeWorkingSet.isNotEmpty()) {
|
while (edgeWorkingSet.isNotEmpty()) {
|
||||||
val head = edgeWorkingSet.first()
|
edgeHead = edgeWorkingSet.take(1)
|
||||||
edgeWorkingSet = edgeWorkingSet.drop(1)
|
edgeWorkingSet = edgeWorkingSet.drop(1)
|
||||||
yield(ContourAdjusterEdge(this@ContourAdjuster, head))
|
yield(ContourAdjusterEdge(this@ContourAdjuster, { edgeHead.first() }))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package org.openrndr.extra.shapes.adjust
|
|||||||
|
|
||||||
import org.openrndr.math.Vector2
|
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
|
* 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
|
* A [ContourAdjusterVertex] interface for the end-vertex of the edge
|
||||||
*/
|
*/
|
||||||
val end
|
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
|
* A link to the edge before this edge
|
||||||
*/
|
*/
|
||||||
val previous: ContourAdjusterEdge?
|
val previous: ContourAdjusterEdge?
|
||||||
get() = if (contourAdjuster.contour.closed) {
|
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 {
|
} else {
|
||||||
if (segmentIndex > 0) {
|
if (segmentIndex() > 0) {
|
||||||
this.copy(segmentIndex = segmentIndex - 1)
|
this.copy(segmentIndex = { segmentIndex() - 1 })
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
@@ -35,20 +35,20 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment
|
|||||||
*/
|
*/
|
||||||
val next: ContourAdjusterEdge?
|
val next: ContourAdjusterEdge?
|
||||||
get() = if (contourAdjuster.contour.closed) {
|
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 {
|
} else {
|
||||||
if (segmentIndex < contourAdjuster.contour.segments.size - 1) {
|
if (segmentIndex() < contourAdjuster.contour.segments.size - 1) {
|
||||||
this.copy(segmentIndex = segmentIndex + 1)
|
this.copy(segmentIndex = { segmentIndex() + 1 } )
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun select() {
|
fun select() {
|
||||||
contourAdjuster.selectEdge(segmentIndex)
|
contourAdjuster.selectEdge(segmentIndex())
|
||||||
}
|
}
|
||||||
private fun wrap(block: ContourEdge.() -> ContourEdge) {
|
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.contour = newEdge.contour
|
||||||
contourAdjuster.updateSelection(newEdge.adjustments)
|
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) {
|
fun sub(t0:Double, t1: Double, updateTangents: Boolean = true) {
|
||||||
contourAdjuster.contour =
|
contourAdjuster.contour =
|
||||||
ContourEdge(contourAdjuster.contour, segmentIndex)
|
ContourEdge(contourAdjuster.contour, segmentIndex())
|
||||||
.subbed(t0, t1)
|
.subbed(t0, t1)
|
||||||
.contour
|
.contour
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,17 @@ package org.openrndr.extra.shapes.adjust
|
|||||||
import org.openrndr.extra.shapes.vertex.ContourVertex
|
import org.openrndr.extra.shapes.vertex.ContourVertex
|
||||||
import org.openrndr.math.Vector2
|
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) {
|
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.contour = newVertex.contour
|
||||||
contourAdjuster.updateSelection(newVertex.adjustments)
|
contourAdjuster.updateSelection(newVertex.adjustments)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun select() {
|
fun select() {
|
||||||
contourAdjuster.selectVertex(segmentIndex)
|
contourAdjuster.selectVertex(segmentIndex())
|
||||||
}
|
}
|
||||||
|
|
||||||
fun remove(updateTangents: Boolean = true) = wrap { remove(updateTangents) }
|
fun remove(updateTangents: Boolean = true) = wrap { remove(updateTangents) }
|
||||||
fun moveBy(translation: Vector2, updateTangents: Boolean = true) = wrap { movedBy(translation, updateTangents) }
|
fun moveBy(translation: Vector2, updateTangents: Boolean = true) = wrap { movedBy(translation, updateTangents) }
|
||||||
fun rotate(rotationInDegrees: Double) = wrap { rotatedBy(rotationInDegrees) }
|
fun rotate(rotationInDegrees: Double) = wrap { rotatedBy(rotationInDegrees) }
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import org.openrndr.application
|
|||||||
import org.openrndr.color.ColorRGBa
|
import org.openrndr.color.ColorRGBa
|
||||||
import org.openrndr.extra.shapes.adjust.adjustContour
|
import org.openrndr.extra.shapes.adjust.adjustContour
|
||||||
import org.openrndr.shape.Circle
|
import org.openrndr.shape.Circle
|
||||||
|
import kotlin.math.cos
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
application {
|
application {
|
||||||
@@ -17,13 +18,12 @@ fun main() {
|
|||||||
contour = adjustContour(contour) {
|
contour = adjustContour(contour) {
|
||||||
selectEdges(0, 1, 2, 3)
|
selectEdges(0, 1, 2, 3)
|
||||||
edges.forEachIndexed { index, it ->
|
edges.forEachIndexed { index, it ->
|
||||||
it.replaceWith(0.5)
|
if (index == seconds.mod(4.0).toInt()) {
|
||||||
// if (index == seconds.mod(4.0).toInt()) {
|
it.replaceWith(0.5)
|
||||||
// it.replaceWith(0.5)
|
} else {
|
||||||
// } else {
|
val v = cos(seconds) * 0.15 + 0.25
|
||||||
// val v = cos(seconds) * 0.15 + 0.25
|
it.sub(0.5 - v, 0.5 + v)
|
||||||
// it.sub(0.5 - v, 0.5 + v)
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
drawer.stroke = ColorRGBa.RED
|
drawer.stroke = ColorRGBa.RED
|
||||||
|
|||||||
Reference in New Issue
Block a user