[orx-math] Add demo and readme texts.

This commit is contained in:
Abe Pazos
2025-10-05 15:29:32 +02:00
parent 58c65ab393
commit 923e64f37a
8 changed files with 137 additions and 49 deletions

View File

@@ -9,23 +9,36 @@ import org.openrndr.extra.color.spaces.OKLab
import org.openrndr.extra.color.tools.shadeLuminosity
import org.openrndr.extra.color.tools.shiftHue
import org.openrndr.extra.math.rbf.Rbf2DInterpolator
import org.openrndr.extra.math.rbf.rbfGaussian
import org.openrndr.extra.math.rbf.rbfInverseMultiQuadratic
import org.openrndr.extra.math.rbf.rbfInverseQuadratic
import org.openrndr.extra.noise.uniform
import org.openrndr.extra.shaderphrases.noise.fhash12Phrase
import org.openrndr.extra.shaderphrases.rbf.rbfGaussianPhrase
import org.openrndr.extra.shaderphrases.rbf.rbfInverseMultiQuadraticPhrase
import org.openrndr.extra.shaderphrases.rbf.rbfInverseQuadraticPhrase
import org.openrndr.math.Vector3
import kotlin.collections.indices
import kotlin.collections.map
import kotlin.collections.toTypedArray
import kotlin.random.Random
import kotlin.ranges.until
import kotlin.text.trimIndent
import kotlin.text.trimMargin
/**
* Demonstrates using a two-dimensional Radial Basis Function (RBF) interpolator
* with the user provided 2D input points, their corresponding values (colors in this demo),
* a smoothing factor, and a radial basis function kernel.
*
* The program chooses 20 random points in the window area leaving a 100 pixels
* margin around the borders and assigns a randomized color to each point.
*
* Next it creates the interpolator using those points and colors, a smoothing factor
* and the RBF function used for interpolation. This function takes a squared distance
* as input and returns a scalar value representing the influence of points at that distance.
*
* A ShadeStyle implementing the same RBF interpolation is created next, used to render
* the background gradient interpolating all points and their colors.
*
* After rendering the background, the original points and their colors are
* drawn as circles for reference.
*
* Finally, the current mouse position is used for sampling a color
* from the interpolator and displayed for comparison. Notice that even if
* the fill color is flat, it may look like a gradient due to the changing
* colors in the surrounding pixels.
*/
fun main() {
application {
configure {
@@ -36,7 +49,7 @@ fun main() {
val r = Random(0)
val points = drawer.bounds.offsetEdges(-100.0).uniform(20, r)
val colors = (0 until points.size).map {
val colors = points.map {
ColorRGBa.PINK
.shiftHue<OKHSV>(Double.uniform(-180.0, 180.0, r))
.shadeLuminosity<OKLab>(Double.uniform(0.4, 1.0, r))
@@ -54,12 +67,13 @@ fun main() {
/**
* Shader style that implements RBF interpolation in the fragment shader.
* Uses Gaussian RBF function to interpolate colors between given points.
* Uses an Inverse MultiQuadratic RBF function to interpolate colors between given points.
* Includes custom distance calculation and color interpolation functions.
*/
val ss = shadeStyle {
fragmentPreamble = """${fhash12Phrase}
|${rbfInverseMultiQuadraticPhrase}
fragmentPreamble = """
|$fhash12Phrase
|$rbfInverseMultiQuadraticPhrase
|float squaredDistance(vec2 p, vec2 q) {
| vec2 d = p - q;
| return dot(d, d);
@@ -68,9 +82,7 @@ fun main() {
| vec3 c = p_mean;
| for (int i = 0; i < p_weights_SIZE; ++i) {
| float r = rbfInverseMultiQuadratic(squaredDistance(p_points[i], p), $scale);
| c.r += p_weights[i].r * r;
| c.g += p_weights[i].g * r;
| c.b += p_weights[i].b * r;
| c += p_weights[i].rgb * r;
| }
| return c;
|}