From 64ac42913cd1ab65d3edaa87dacfe2c9c73c57dd Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 2 Feb 2025 11:27:14 +0100 Subject: [PATCH] [orx-noise] Refactor LinearRangeExtensions, add SimplexRangeExtensions --- orx-noise/build.gradle.kts | 1 + .../linearrange/LinearRangeExtensions.kt | 3 +- .../simplexrange/SimplexRangeExtensions.kt | 37 +++++++++++ .../kotlin/linearrange/DemoLinearRange01.kt | 2 +- .../simplexrange/DemoSimplexRange2D01.kt | 61 +++++++++++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 orx-noise/src/commonMain/kotlin/simplexrange/SimplexRangeExtensions.kt create mode 100644 orx-noise/src/jvmDemo/kotlin/simplexrange/DemoSimplexRange2D01.kt diff --git a/orx-noise/build.gradle.kts b/orx-noise/build.gradle.kts index f7a78015..af6f33db 100644 --- a/orx-noise/build.gradle.kts +++ b/orx-noise/build.gradle.kts @@ -22,6 +22,7 @@ kotlin { implementation(project(":orx-hash-grid")) implementation(project(":orx-parameters")) implementation(project(":orx-shader-phrases")) + api(project(":orx-math")) } } diff --git a/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt b/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt index 31ed49aa..107cd5e8 100644 --- a/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt +++ b/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt @@ -1,7 +1,8 @@ package org.openrndr.extra.noise.linearrange import org.openrndr.extra.noise.fhash1D -import org.openrndr.math.* +import org.openrndr.extra.math.linearrange.* +import org.openrndr.math.LinearType import kotlin.random.Random /** diff --git a/orx-noise/src/commonMain/kotlin/simplexrange/SimplexRangeExtensions.kt b/orx-noise/src/commonMain/kotlin/simplexrange/SimplexRangeExtensions.kt new file mode 100644 index 00000000..091d2922 --- /dev/null +++ b/orx-noise/src/commonMain/kotlin/simplexrange/SimplexRangeExtensions.kt @@ -0,0 +1,37 @@ +package org.openrndr.extra.noise.simplexrange + +import org.openrndr.math.LinearType +import org.openrndr.extra.math.simplexrange.SimplexRange4D +import org.openrndr.extra.math.simplexrange.SimplexRange3D +import org.openrndr.extra.math.simplexrange.SimplexRange2D +import kotlin.random.Random + +/** + * Generates a uniformly distributed value within the SimplexRange2D. + * + * @param random the random number generator used to produce random values. + * @return a value of type T sampled uniformly within the 2D simplex range. + */ +fun > SimplexRange2D.uniform(random: Random): T { + return value(random.nextDouble(), random.nextDouble()) +} + +/** + * Generates a uniformly distributed value within the 3D simplex range. + * + * @param random the random number generator to produce the distribution. + * @return a value of type [T] sampled uniformly within the range. + */ +fun > SimplexRange3D.uniform(random: Random): T { + return value(random.nextDouble(), random.nextDouble(), random.nextDouble()) +} + +/** + * Generates a uniformly distributed value within the 4D simplex range using a random generator. + * + * @param random an instance of the random number generator used to produce random values + * @return a value of type `T` that represents a point within the simplex range + */ +fun > SimplexRange4D.uniform(random: Random): T { + return value(random.nextDouble(), random.nextDouble(), random.nextDouble(), random.nextDouble()) +} \ No newline at end of file diff --git a/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt b/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt index 4e810fc3..2517caa2 100644 --- a/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt +++ b/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt @@ -3,7 +3,7 @@ package linearrange import org.openrndr.application import org.openrndr.color.ColorRGBa import org.openrndr.extra.noise.linearrange.uniform -import org.openrndr.math.rangeTo +import org.openrndr.extra.math.linearrange.rangeTo import kotlin.random.Random fun main() { diff --git a/orx-noise/src/jvmDemo/kotlin/simplexrange/DemoSimplexRange2D01.kt b/orx-noise/src/jvmDemo/kotlin/simplexrange/DemoSimplexRange2D01.kt new file mode 100644 index 00000000..d0caa1f0 --- /dev/null +++ b/orx-noise/src/jvmDemo/kotlin/simplexrange/DemoSimplexRange2D01.kt @@ -0,0 +1,61 @@ +package simplexrange + +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.extra.math.linearrange.rangeTo +import org.openrndr.extra.math.simplexrange.SimplexRange3D +import kotlin.random.Random +import org.openrndr.extra.noise.simplexrange.uniform +import org.openrndr.extra.noise.linearrange.* + +/** + * This demo creates a dynamic graphical output utilizing simplex and + * linear interpolation-based color ranges. + * + * Functionalities: + * - Defines a list of base colors converted to LAB color space for smooth interpolation. + * - Constructs a 3D simplex range and a 2D linear range for color sampling. + * - Randomly populates two sections of the screen with rectangles filled with colors + * sampled from simplex and linear ranges respectively. + * - Draws a vertical divider line in the middle of the application window. + */ +fun main() { + application { + configure { + width = 720 + height = 720 + } + program { + extend { + val colors = listOf(ColorRGBa.BLACK, ColorRGBa.RED, ColorRGBa.GREEN, ColorRGBa.BLUE).map { it.toLABa() } + drawer.stroke = null + val sr = SimplexRange3D(colors[0], colors[1], colors[2], colors[3]) + val lr = (colors[0]..colors[1])..(colors[2]..colors[3]) + + val r = Random((seconds * 2).toInt()) + + // Draw the simplex sampling on the left + drawer.rectangles { + for (y in 0 until 40) { + for (x in 0 until 20) { + fill = sr.uniform(r).toRGBa() + rectangle(x * width / 40.0, y * height / 40.0, width / 40.0, height / 40.0) + } + } + } + + // Draw the bilinear sampling on the right + drawer.rectangles { + for (y in 0 until 40) { + for (x in 20 until 40) { + fill = lr.uniform(r).toRGBa() + rectangle(x * width / 40.0, y * height / 40.0, width / 40.0, height / 40.0) + } + } + } + drawer.stroke = ColorRGBa.BLACK + drawer.lineSegment(drawer.bounds.vertical(0.5)) + } + } + } +} \ No newline at end of file