Skip to content

Commit

Permalink
[orx-jumpflood] Make module multiplatform
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Jan 12, 2023
1 parent 58d15a1 commit e27f7eb
Show file tree
Hide file tree
Showing 51 changed files with 571 additions and 177 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def multiplatformModules = [
"orx-fx",
"orx-gradient-descent",
"orx-image-fit",
"orx-jumpflood",
"orx-no-clear",
"orx-noise",
"orx-parameters",
Expand Down
76 changes: 65 additions & 11 deletions orx-jumpflood/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,69 @@
import ScreenshotsHelper.collectScreenshots

plugins {
org.openrndr.extra.convention.`kotlin-jvm`
org.openrndr.extra.convention.`kotlin-multiplatform`
}

dependencies {
implementation(project(":orx-fx"))
implementation(project(":orx-parameters"))
implementation(libs.openrndr.application)
implementation(libs.openrndr.math)
demoImplementation(project(":orx-noise"))
demoImplementation(project(":orx-jvm:orx-gui"))
demoImplementation(project(":orx-compositor"))
demoImplementation(libs.openrndr.svg)
demoImplementation(libs.openrndr.ffmpeg)
val embedShaders = tasks.register<EmbedShadersTask>("embedShaders") {
inputDir.set(file("$projectDir/src/shaders/glsl"))
outputDir.set(file("$buildDir/generated/shaderKotlin"))
defaultPackage.set("org.openrndr.extra.jumpflood")
defaultVisibility.set("internal")
namePrefix.set("jf_")
}.get()


kotlin {
jvm {
@Suppress("UNUSED_VARIABLE")
val demo by compilations.getting {
// TODO: Move demos to /jvmDemo
defaultSourceSet {
kotlin.srcDir("src/demo/kotlin")
}
collectScreenshots { }
}
}

sourceSets {
val shaderKotlin by creating {
this.kotlin.srcDir(embedShaders.outputDir)
}

@Suppress("UNUSED_VARIABLE")
val commonMain by getting {
dependencies {
implementation(project(":orx-parameters"))
implementation(project(":orx-fx"))
implementation(libs.openrndr.application)
implementation(libs.openrndr.draw)
implementation(libs.openrndr.filter)
implementation(libs.kotlin.reflect)
api(shaderKotlin.kotlin)
}
dependsOn(shaderKotlin)
}


@Suppress("UNUSED_VARIABLE")
val jvmTest by getting {
dependencies {
implementation(libs.spek.dsl)
implementation(libs.kluent)
}
}

@Suppress("UNUSED_VARIABLE")
val jvmDemo by getting {
dependencies {
implementation(project(":orx-color"))
implementation(project(":orx-fx"))
implementation(project(":orx-noise"))
implementation(project(":orx-jumpflood"))
implementation(project(":orx-compositor"))
implementation(project(":orx-jvm:orx-gui"))
implementation(libs.openrndr.svg)
}
}
}
}
123 changes: 123 additions & 0 deletions orx-jumpflood/src/commonMain/kotlin/ClusteredField.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.openrndr.extra.jumpfill

import org.openrndr.draw.*
import org.openrndr.extra.fx.blend.Passthrough
import org.openrndr.extra.parameters.BooleanParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.shape.IntRectangle
import kotlin.math.ceil
import kotlin.math.log2
import kotlin.math.max
import kotlin.math.pow

@Description("Clustered field")
class ClusteredField(decodeMode: DecodeMode = DecodeMode.DIRECTION,
private val outputDistanceToContours: Boolean = true) : Filter1to1() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5

@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0

@BooleanParameter("normalized distance")
var normalizedDistance = false

@BooleanParameter("unit direction")
var unitDirection = false

@BooleanParameter("flip v direction")
var flipV = true

private val encodeFilter = EncodePoints()
private var encoded: ColorBuffer? = null
private val contourFilter = IdContourPoints()
private var contoured: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null

private val decodeFilter = PixelDirection(decodeMode)

private var fit: ColorBuffer? = null

override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
val advisedWidth = 2.0.pow(ceil(log2(source[0].effectiveWidth.toDouble()))).toInt()
val advisedHeight = 2.0.pow(ceil(log2(source[0].effectiveHeight.toDouble()))).toInt()
val advisedSize = max(advisedWidth, advisedHeight)

fit?.let {
if (it.effectiveWidth != advisedSize || it.effectiveHeight != advisedSize) {
it.destroy()
fit = null
encoded?.destroy()
encoded = null
contoured?.destroy()
contoured = null
jumpFlooder?.destroy()
jumpFlooder = null
}
}

if (fit == null) {
fit = colorBuffer(advisedSize, advisedSize, type=ColorType.FLOAT32)
}

source[0].copyTo(
fit!!,
sourceRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(
0,
advisedSize - source[0].effectiveHeight,
source[0].effectiveWidth,
source[0].effectiveHeight
)
)

if (encoded == null) {
encoded = colorBuffer(advisedSize, advisedSize, format = ColorFormat.RGBa, type = ColorType.FLOAT32)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(advisedSize, advisedSize, encodePoints = Passthrough())
}

if (outputDistanceToContours && contoured == null) {
contoured = colorBuffer(advisedSize, advisedSize, type = ColorType.FLOAT32)
}

encodeFilter.apply(fit!!, encoded!!)
var result = jumpFlooder!!.jumpFlood(encoded!!)

if (outputDistanceToContours) {
contourFilter.apply(result, contoured!!)
result = jumpFlooder!!.jumpFlood(contoured!!)
}

decodeFilter.outputIds = true
decodeFilter.originalSize = Vector2(source[0].width.toDouble(), source[0].height.toDouble())
decodeFilter.distanceScale = distanceScale
decodeFilter.normalizedDistance = normalizedDistance
decodeFilter.unitDirection = unitDirection
decodeFilter.flipV = flipV
decodeFilter.apply(arrayOf(result, encoded!!), arrayOf(result))

result.copyTo(
target[0],
sourceRectangle = IntRectangle(
0,
advisedSize - source[0].effectiveHeight,
source[0].effectiveWidth,
source[0].effectiveHeight
),
targetRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight)
)
}

override fun destroy() {
encodeFilter.destroy()
contourFilter.destroy()
fit?.destroy()
encoded?.destroy()
contoured?.destroy()
jumpFlooder?.destroy()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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.draw.*
import org.openrndr.extra.parameters.BooleanParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
Expand All @@ -14,13 +12,22 @@ import kotlin.math.max
import kotlin.math.pow

@Description("Directional field")
class DirectionalField : Filter() {
class DirectionalField : Filter1to1() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5

@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0

@BooleanParameter("normalized distance")
var normalizedDistance = false

@BooleanParameter("unit direction")
var unitDirection = false

@BooleanParameter("flip v direction")
var flipV = true

private val thresholdFilter = Threshold()
private var thresholded: ColorBuffer? = null
private val contourFilter = ContourPoints()
Expand Down Expand Up @@ -71,8 +78,11 @@ class DirectionalField : Filter() {
thresholdFilter.apply(fit!!, thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.originalSize = Vector2(advisedSize.toDouble(), advisedSize.toDouble())
decodeFilter.originalSize = Vector2(source[0].width.toDouble(), source[0].height.toDouble())
decodeFilter.distanceScale = distanceScale
decodeFilter.normalizedDistance = normalizedDistance
decodeFilter.unitDirection = unitDirection
decodeFilter.flipV = flipV
decodeFilter.apply(arrayOf(result, thresholded!!), arrayOf(result))
result.copyTo(target[0],
sourceRectangle = IntRectangle(0, advisedSize-source[0].effectiveHeight, source[0].effectiveWidth, source[0].effectiveHeight),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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.draw.*
import org.openrndr.extra.parameters.BooleanParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
Expand All @@ -15,7 +12,7 @@ import kotlin.math.max
import kotlin.math.pow

@Description("Distance field")
class DistanceField : Filter() {
class DistanceField : Filter1to1() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5

Expand Down Expand Up @@ -63,27 +60,25 @@ class DistanceField : Filter() {
)

if (thresholded == null) {
thresholded = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
thresholded = colorBuffer(advisedSize, advisedSize, format = ColorFormat.R)
}
if (contoured == null) {
contoured = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
contoured = colorBuffer(advisedSize, advisedSize, format = ColorFormat.R)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height)
jumpFlooder = JumpFlooder(advisedSize, advisedSize)
}

thresholdFilter.threshold = threshold
thresholdFilter.apply(fit!!, thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.signedDistance = signedDistance
decodeFilter.originalSize = Vector2(advisedSize.toDouble(), advisedSize.toDouble())
decodeFilter.originalSize = Vector2(source[0].width.toDouble(), source[0].height.toDouble())
decodeFilter.distanceScale = distanceScale
decodeFilter.signedBit = false
decodeFilter.apply(arrayOf(result, thresholded!!), arrayOf(result))
result.copyTo(target[0],
sourceRectangle = IntRectangle(0, advisedSize-source[0].effectiveHeight, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight)
)

}
}
Loading

0 comments on commit e27f7eb

Please sign in to comment.