[orx-shapes] Add WIP ContourAdjuster framework

This commit is contained in:
Edwin Jakobs
2023-10-23 12:27:45 +02:00
parent 7f41c263a7
commit e7f8add84e
17 changed files with 940 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
package org.openrndr.extra.shapes.adjust
import org.openrndr.math.Vector2
data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segmentIndex: Int) {
/**
* A [ContourAdjusterVertex] interface for the start-vertex of the edge
*/
val start
get() = ContourAdjusterVertex(contourAdjuster, segmentIndex)
/**
* A [ContourAdjusterVertex] interface for the end-vertex of the edge
*/
val end
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))
} else {
if (segmentIndex > 0) {
this.copy(segmentIndex = segmentIndex - 1)
} else {
null
}
}
/**
* A link to the edge after this edge
*/
val next: ContourAdjusterEdge?
get() = if (contourAdjuster.contour.closed) {
this.copy(segmentIndex = (segmentIndex + 1).mod(contourAdjuster.contour.segments.size))
} else {
if (segmentIndex < contourAdjuster.contour.segments.size - 1) {
this.copy(segmentIndex = segmentIndex + 1)
} else {
null
}
}
fun select() {
contourAdjuster.selectEdge(segmentIndex)
}
private fun wrap(block: ContourEdge.() -> ContourEdge) {
val newEdge = ContourEdge(contourAdjuster.contour, segmentIndex).block()
contourAdjuster.contour = newEdge.contour
contourAdjuster.updateSelection(newEdge.adjustments)
}
fun toCubic() = wrap { toCubic() }
fun splitAt(t: Double) = wrap { splitAt(t) }
fun moveBy(translation: Vector2, updateTangents: Boolean = true) = wrap { movedBy(translation, updateTangents) }
fun rotate(rotationInDegrees: Double, anchorT: Double = 0.5, updateTangents: Boolean = true) =
wrap { rotatedBy(rotationInDegrees, anchorT, updateTangents) }
fun scale(scaleFactor: Double, anchorT: Double = 0.5, updateTangents: Boolean = true) =
wrap { scaledBy(scaleFactor, anchorT, updateTangents = true) }
fun replaceWith(t:Double, updateTangents: Boolean = true) {
wrap { replacedWith(t, updateTangents) }
}
fun sub(t0:Double, t1: Double, updateTangents: Boolean = true) {
contourAdjuster.contour =
ContourEdge(contourAdjuster.contour, segmentIndex)
.subbed(t0, t1)
.contour
}
}