Files
orx/orx-jumpflood/src/main/kotlin/DirectionalField.kt
2020-02-29 14:01:32 +01:00

46 lines
1.7 KiB
Kotlin

package org.openrndr.extra.jumpfill
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.ColorFormat
import org.openrndr.draw.Filter
import org.openrndr.draw.colorBuffer
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
@Description("Directional field")
class DirectionalField : Filter() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5
@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0
private val thresholdFilter = Threshold()
private var thresholded: ColorBuffer? = null
private val contourFilter = ContourPoints()
private var contoured: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (thresholded == null) {
thresholded = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
}
if (contoured == null) {
contoured = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height)
}
thresholdFilter.threshold = threshold
thresholdFilter.apply(source[0], thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = distanceScale
decodeFilter.apply(result, result)
result.copyTo(target[0])
}
}