Skip to content

Commit

Permalink
[orx-shapes] Handle RectifiedPath3D.path better
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed May 9, 2024
1 parent 15cdba1 commit 8f68c91
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 45 deletions.
4 changes: 2 additions & 2 deletions orx-shapes/src/commonMain/kotlin/blend/ContourBlend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fun ContourBlend(a: ShapeContour, b: ShapeContour): ContourBlend {
val rb = b.rectified()
val sa = ra.splitForBlend(rb)
val sb = rb.splitForBlend(ra)
require(sa.path.segments.size == sb.path.segments.size) {
"preprocessing for contours failed to produce equal number of segments. ${sa.path.segments.size}, ${sb.path.segments.size}"
require(sa.originalPath.segments.size == sb.originalPath.segments.size) {
"preprocessing for contours failed to produce equal number of segments. ${sa.originalPath.segments.size}, ${sb.originalPath.segments.size}"
}
return ContourBlend(sa, sb)
}
6 changes: 2 additions & 4 deletions orx-shapes/src/commonMain/kotlin/blend/Path3DBlend.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.openrndr.extra.shapes.blend

import org.openrndr.extra.shapes.rectify.RectifiedContour
import org.openrndr.extra.shapes.rectify.RectifiedPath3D
import org.openrndr.extra.shapes.rectify.rectified
import org.openrndr.shape.Path3D
import org.openrndr.shape.ShapeContour

/**
* ContourBlend holds two rectified contours with an equal amount of segments
Expand All @@ -30,8 +28,8 @@ fun Path3DBlend(a: Path3D, b: Path3D): Path3DBlend {
val rb = b.rectified()
val sa = ra.splitForBlend(rb)
val sb = rb.splitForBlend(ra)
require(sa.path.segments.size == sb.path.segments.size) {
"preprocessing for contours failed to produce equal number of segments. ${sa.path.segments.size}, ${sb.path.segments.size}"
require(sa.originalPath.segments.size == sb.originalPath.segments.size) {
"preprocessing for contours failed to produce equal number of segments. ${sa.originalPath.segments.size}, ${sb.originalPath.segments.size}"
}
return Path3DBlend(sa, sb)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import org.openrndr.shape.ShapeContour
* Split for blending with [other]
*/
fun RectifiedContour.splitForBlend(other: RectifiedContour): RectifiedContour {
val ts = (0 until other.path.segments.size + 1).map { it.toDouble() / other.path.segments.size }
val ts = (0 until other.originalPath.segments.size + 1).map { it.toDouble() / other.originalPath.segments.size }
val rts = ts.map { other.inverseRectify(it) }

return ShapeContour.fromContours(splitAt(rts), path.closed && other.path.closed).rectified()
return ShapeContour.fromContours(splitAt(rts), originalPath.closed && other.originalPath.closed).rectified()
}

fun RectifiedContour.mix(other: RectifiedContour, blendFunction: (Double) -> Double): ShapeContour {
val n = this.path.segments.size.toDouble()
val segs = (this.path.segments zip other.path.segments).mapIndexed { index, it ->
val n = this.originalPath.segments.size.toDouble()
val segs = (this.originalPath.segments zip other.originalPath.segments).mapIndexed { index, it ->
val t0 = inverseRectify(index / n)
val t1 = inverseRectify((index + 1 / 3.0) / n)
val t2 = inverseRectify((index + 2 / 3.0) / n)
val t3 = inverseRectify((index + 1) / n)
(it.first as Segment2D).mix(it.second as Segment2D, blendFunction(t0), blendFunction(t1), blendFunction(t2), blendFunction(t3))
}
return ShapeContour.fromSegments(segs, path.closed && other.path.closed)
return ShapeContour.fromSegments(segs, originalPath.closed && other.originalPath.closed)
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import org.openrndr.shape.Segment3D
* Split for blending with [other]
*/
fun RectifiedPath3D.splitForBlend(other: RectifiedPath3D): RectifiedPath3D {
val ts = (0 until other.path.segments.size + 1).map { it.toDouble() / other.path.segments.size }
val ts = (0 until other.originalPath.segments.size + 1).map { it.toDouble() / other.originalPath.segments.size }
val rts = ts.map { other.inverseRectify(it) }

return Path3D.fromPaths(splitAt(rts), path.closed && other.path.closed).rectified()
return Path3D.fromPaths(splitAt(rts), originalPath.closed && other.originalPath.closed).rectified()
}

fun RectifiedPath3D.mix(other: RectifiedPath3D, blendFunction: (Double) -> Double): Path3D {
val n = this.path.segments.size.toDouble()
val segs = (this.path.segments zip other.path.segments).mapIndexed { index, it ->
val n = this.originalPath.segments.size.toDouble()
val segs = (this.originalPath.segments zip other.originalPath.segments).mapIndexed { index, it ->
val t0 = inverseRectify(index / n)
val t1 = inverseRectify((index + 1 / 3.0) / n)
val t2 = inverseRectify((index + 2 / 3.0) / n)
val t3 = inverseRectify((index + 1) / n)
(it.first as Segment3D).mix(it.second as Segment3D, blendFunction(t0), blendFunction(t1), blendFunction(t2), blendFunction(t3))
}
return Path3D.fromSegments(segs, path.closed && other.path.closed)
return Path3D.fromSegments(segs, originalPath.closed && other.originalPath.closed)
}

28 changes: 14 additions & 14 deletions orx-shapes/src/commonMain/kotlin/rectify/RectifiedContour.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@ import kotlin.math.floor
class RectifiedContour(contour: ShapeContour, distanceTolerance: Double = 0.5, lengthScale: Double = 1.0) :
RectifiedPath<Vector2>(contour, distanceTolerance, lengthScale) {
fun velocity(t: Double): Vector2 {
return if (path.empty) {
return if (originalPath.empty) {
Vector2.ZERO
} else {
val (segment, st) = path.segment(rectify(safe(t)))
path.segments[segment].direction(st)
val (segment, st) = originalPath.segment(rectify(safe(t)))
originalPath.segments[segment].direction(st)
}
}

fun normal(t: Double): Vector2 {
return if (path.empty) {
return if (originalPath.empty) {
Vector2.UNIT_Y
} else {
(path as ShapeContour).normal(rectify(safe(t)))
(originalPath as ShapeContour).normal(rectify(safe(t)))
}
}

fun pose(t: Double): Matrix44 {
path as ShapeContour
return if (path.empty) {
originalPath as ShapeContour
return if (originalPath.empty) {
Matrix44.IDENTITY
} else {
path.pose(rectify(safe(t)))
originalPath.pose(rectify(safe(t)))
}
}

override fun sub(t0: Double, t1: Double): ShapeContour {
path as ShapeContour
if (path.empty) {
originalPath as ShapeContour
if (originalPath.empty) {
return ShapeContour.EMPTY
}

return if (path.closed) {
path.sub(rectify(t0.mod(1.0)) + floor(t0), rectify(t1.mod(1.0)) + floor(t1))
return if (originalPath.closed) {
originalPath.sub(rectify(t0.mod(1.0)) + floor(t0), rectify(t1.mod(1.0)) + floor(t1))
} else {
path.sub(rectify(t0), rectify(t1))
originalPath.sub(rectify(t0), rectify(t1))
}
}

Expand All @@ -51,5 +51,5 @@ class RectifiedContour(contour: ShapeContour, distanceTolerance: Double = 0.5, l
return super.splitAt(ascendingTs, weldEpsilon) as List<ShapeContour>
}

val contour: ShapeContour get() = path as ShapeContour
val contour: ShapeContour get() = originalPath as ShapeContour
}
27 changes: 13 additions & 14 deletions orx-shapes/src/commonMain/kotlin/rectify/RectifiedPath.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.openrndr.extra.shapes.rectify

import org.openrndr.extra.shapes.utilities.splitAt
import org.openrndr.extra.shapes.utilities.splitAtBase
import org.openrndr.math.EuclideanVector
import org.openrndr.math.clamp
Expand All @@ -11,12 +10,12 @@ import org.openrndr.shape.ShapeContour
* RectifiedContour provides an approximately uniform parameterization for [ShapeContour]
*/
abstract class RectifiedPath<T : EuclideanVector<T>>(
open val path: Path<T>,
val originalPath: Path<T>,
distanceTolerance: Double = 0.5,
lengthScale: Double = 1.0
) {
val points =
path.equidistantPositionsWithT((path.length * lengthScale).toInt().coerceAtLeast(2), distanceTolerance)
originalPath.equidistantPositionsWithT((originalPath.length * lengthScale).toInt().coerceAtLeast(2), distanceTolerance)

val intervals by lazy {
points.zipWithNext().map {
Expand All @@ -25,18 +24,18 @@ abstract class RectifiedPath<T : EuclideanVector<T>>(
}

internal fun safe(t: Double): Double {
return if (path.closed) {
return if (originalPath.closed) {
t.mod(1.0)
} else {
t.clamp(0.0, 1.0)
}
}

/**
* computes a rectified t-value for [path]
* computes a rectified t-value for [originalPath]
*/
fun rectify(t: Double): Double {
if (path.empty) {
if (originalPath.empty) {
return 0.0
} else {
if (t <= 0.0) {
Expand All @@ -56,7 +55,7 @@ abstract class RectifiedPath<T : EuclideanVector<T>>(
}

fun inverseRectify(t: Double): Double {
if (path.empty) {
if (originalPath.empty) {
return 0.0
} else {
if (t <= 0.0) {
Expand Down Expand Up @@ -85,18 +84,18 @@ abstract class RectifiedPath<T : EuclideanVector<T>>(
}

fun position(t: Double): T {
return if (path.empty) {
path.infinity
return if (originalPath.empty) {
originalPath.infinity
} else {
path.position(rectify(safe(t)))
originalPath.position(rectify(safe(t)))
}
}

fun direction(t: Double): T {
return if (path.empty) {
path.infinity
return if (originalPath.empty) {
originalPath.infinity
} else {
path.direction(rectify(safe(t)))
originalPath.direction(rectify(safe(t)))
}
}

Expand All @@ -107,6 +106,6 @@ abstract class RectifiedPath<T : EuclideanVector<T>>(
* @since orx 0.4.4
*/
open fun splitAt(ascendingTs: List<Double>, weldEpsilon: Double = 1E-6): List<Path<T>> {
return path.splitAtBase(ascendingTs.map { rectify(it) }, weldEpsilon)
return originalPath.splitAtBase(ascendingTs.map { rectify(it) }, weldEpsilon)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.math.floor
class RectifiedPath3D(contour: Path3D, distanceTolerance: Double = 0.5, lengthScale: Double = 1.0) :
RectifiedPath<Vector3>(contour, distanceTolerance, lengthScale) {

override val path: Path3D = super.path as Path3D
val path: Path3D get() = originalPath as Path3D

override fun sub(t0: Double, t1: Double): Path3D {
if (path.empty) {
Expand Down
2 changes: 2 additions & 0 deletions orx-shapes/src/jvmDemo/kotlin/frames/DemoFrames01.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package frames

import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.DrawPrimitive
Expand All @@ -19,6 +20,7 @@ fun main() {
configure {
width = 720
height = 720
multisample = WindowMultisample.SampleCount(4)
}
program {
val random = Random(0)
Expand Down

0 comments on commit 8f68c91

Please sign in to comment.