Clean-up and add simplexQuintic, simplexHermite

This commit is contained in:
Edwin Jakobs
2020-03-11 17:29:15 +01:00
parent d795ef91f3
commit 5b0775289c
13 changed files with 836 additions and 815 deletions

View File

@@ -1,7 +1,11 @@
package org.openrndr.extra.noise
fun cubicLinear(seed: Int, x: Double, y: Double) = cubic(seed, x, y, ::linear)
fun cubicQuintic(seed: Int, x: Double, y: Double) = cubic(seed, x, y, ::quintic)
fun cubicHermite(seed: Int, x: Double, y: Double) = cubic(seed, x, y, ::hermite)
private const val CUBIC_2D_BOUNDING = 1 / (1.5 * 1.5).toFloat()
fun cubic(seed: Int, x: Double, y: Double): Double {
fun cubic(seed: Int, x: Double, y: Double, interpolator: (Double) -> Double = ::linear): Double {
val x1 = x.fastFloor()
val y1 = y.fastFloor()
@@ -12,8 +16,8 @@ fun cubic(seed: Int, x: Double, y: Double): Double {
val x3 = x1 + 2
val y3 = y1 + 2
val xs = x - x1.toDouble()
val ys = y - y1.toDouble()
val xs = interpolator(x - x1.toDouble())
val ys = interpolator(y - y1.toDouble())
return cubic(
cubic(valCoord2D(seed, x0, y0), valCoord2D(seed, x1, y0), valCoord2D(seed, x2, y0), valCoord2D(seed, x3, y0),

View File

@@ -1,9 +1,13 @@
package org.openrndr.extra.noise
private const val CUBIC_3D_BOUNDING = 1 / (1.5 * 1.5 * 1.5).toFloat()
fun cubic(seed: Int, x: Double, y: Double, z: Double): Double {
fun cubic(seed: Int, x: Double, y: Double, z: Double) = cubic(seed, x, y, z, ::linear)
fun cubicLinear(seed: Int, x: Double, y: Double, z: Double) = cubic(seed, x, y, z, ::linear)
fun cubicQuintic(seed: Int, x: Double, y: Double, z: Double) = cubic(seed, x, y, z, ::quintic)
fun cubicHermite(seed: Int, x: Double, y: Double, z: Double) = perlin(seed, x, y, z, ::hermite)
fun cubic(seed: Int, x: Double, y: Double, z: Double, interpolator: (Double) -> Double): Double {
val x1 = x.fastFloor()
val y1 = y.fastFloor()
val z1 = z.fastFloor()
@@ -18,9 +22,9 @@ fun cubic(seed: Int, x: Double, y: Double, z: Double): Double {
val y3 = y1 + 2
val z3 = z1 + 2
val xs = x - x1.toFloat()
val ys = y - y1.toFloat()
val zs = z - z1.toFloat()
val xs = interpolator(x - x1.toFloat())
val ys = interpolator(y - y1.toFloat())
val zs = interpolator(z - z1.toFloat())
return cubic(
cubic(

View File

@@ -36,7 +36,7 @@ inline fun fbm(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double,
}
inline fun billow(seed: Int, x: Double, y: Double, z: Double, crossinline noise: (Int, Double, Double, Double) -> Double,
octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5) : Double {
octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double {
var sum = Math.abs(noise(seed, x, y, z) * 2.0 - 1.0)
var amp = 1.0
@@ -54,7 +54,7 @@ inline fun billow(seed: Int, x: Double, y: Double, z: Double, crossinline noise:
}
inline fun billow(seed: Int, x: Double, y: Double, crossinline noise: (Int, Double, Double) -> Double,
octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5) : Double {
octaves: Int = 8, lacunarity: Double = 0.5, gain: Double = 0.5): Double {
var sum = Math.abs(noise(seed, x, y) * 2.0 - 1.0)
var amp = 1.0

View File

@@ -3,19 +3,16 @@ package org.openrndr.extra.noise
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
private const val X_PRIME = 1619
private const val Y_PRIME = 31337
private const val Z_PRIME = 6971
private const val W_PRIME = 1013
private val GRAD_2D = arrayOf(Vector2(-1.0, -1.0), Vector2(1.0, -1.0), Vector2(-1.0, 1.0),
private val GRAD_2D = arrayOf(
Vector2(-1.0, -1.0), Vector2(1.0, -1.0), Vector2(-1.0, 1.0),
Vector2(1.0, 1.0), Vector2(0.0, -1.0), Vector2(-1.0, 0.0),
Vector2(0.0, 1.0), Vector2(1.0, 0.0))
private val GRAD_3D = arrayOf(
Vector3(1.0, 1.0, 0.0), Vector3(-1.0, 1.0, 0.0), Vector3(1.0, -1.0, 0.0), Vector3(-1.0, -1.0, 0.0),
Vector3(1.0, 0.0, 1.0), Vector3(-1.0, 0.0, 1.0), Vector3(1.0, 0.0, -1.0), Vector3(-1.0, 0.0, -1.0),
@@ -49,7 +46,6 @@ fun gradCoord3D(seed: Int, x: Int, y: Int, z: Int, xd: Double, yd: Double, zd: D
return xd * g.x + yd * g.y + zd * g.z
}
fun gradCoord4D(seed: Int, x: Int, y: Int, z: Int, w: Int, xd: Double, yd: Double, zd: Double, wd: Double): Double {
var hash = seed
hash = hash xor X_PRIME * x
@@ -124,6 +120,12 @@ fun hash4D(seed: Int, x: Int, y: Int, z: Int, w: Int): Int {
}
fun valCoord1D(seed: Int, x: Int): Double {
var n = seed
n = n xor X_PRIME * x
return n * n * n * 60493 / 2147483648.0
}
fun valCoord2D(seed: Int, x: Int, y: Int): Double {
var n = seed
n = n xor X_PRIME * x
@@ -141,7 +143,6 @@ fun valCoord3D(seed: Int, x: Int, y: Int, z: Int): Double {
return n * n * n * 60493 / 2147483648.0
}
private fun valCoord4D(seed: Int, x: Int, y: Int, z: Int, w: Int): Double {
var n = seed
n = n xor X_PRIME * x

View File

@@ -1,7 +1,5 @@
package org.openrndr.extra.noise
fun hermite(t: Double): Double {
return t * t * (3 - 2 * t)
}
@@ -10,12 +8,12 @@ fun quintic(t: Double): Double {
return t * t * t * (t * (t * 6 - 15) + 10)
}
fun cubic(a: Double, b: Double, c: Double, d: Double, t: Double) : Double {
fun cubic(a: Double, b: Double, c: Double, d: Double, t: Double): Double {
val p = d - c - (a - b)
return t * t * t * p + t * t * (a - b - p) + t * (c - a) + b
}
fun linear(x: Double) : Double {
fun linear(x: Double): Double {
return x
}

View File

@@ -1,10 +1,11 @@
package org.openrndr.extra.noise
fun perlin(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::linear)
fun perlinLinear(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::linear)
fun perlinQuintic(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::quintic)
fun perlinHermite(seed: Int, x: Double, y: Double) = perlin(seed, x, y, ::hermite)
private fun perlin(seed: Int, x: Double, y: Double, interpolator: (Double) -> Double): Double {
inline fun perlin(seed: Int, x: Double, y: Double, crossinline interpolator: (Double) -> Double): Double {
val x0 = x.fastFloor()
val y0 = y.fastFloor()
val x1 = x0 + 1
@@ -13,7 +14,6 @@ private fun perlin(seed: Int, x: Double, y: Double, interpolator: (Double) -> Do
val xs = interpolator(x - x0)
val ys = interpolator(y - y0)
val xd0 = x - x0
val yd0 = y - y0
val xd1 = xd0 - 1

View File

@@ -87,7 +87,7 @@ object Random {
var list = coll.toMutableList()
val picked = mutableListOf<T>()
while(picked.size < count) {
while (picked.size < count) {
if (list.isEmpty()) {
list = coll.toMutableList()
}
@@ -95,7 +95,7 @@ object Random {
var index = int0(list.size)
var newElem = list.elementAt(index)
while(compareAgainst.contains(newElem)) {
while (compareAgainst.contains(newElem)) {
index = int0(list.size)
newElem = list.elementAt(index)
}
@@ -238,14 +238,14 @@ object Random {
}
fun ring2d(innerRadius: Double = 0.0, outerRadius: Double = 1.0, count: Int = 1): Any {
return when(count) {
return when (count) {
1 -> Vector2.uniformRing(innerRadius, outerRadius, rnd)
else -> Vector2.uniformsRing(count, innerRadius, outerRadius, rnd)
}
}
fun ring3d(innerRadius: Double = 0.0, outerRadius: Double = 1.0, count: Int = 1): Any {
return when(count) {
return when (count) {
1 -> Vector3.uniformRing(innerRadius, outerRadius, rnd)
else -> Vector3.uniformsRing(count, innerRadius, outerRadius, rnd)
}

View File

@@ -1,10 +1,13 @@
package org.openrndr.extra.noise
private const val G2 = 1.0 / 4.0
private const val F2 = 1.0 / 2.0
private val G2 = 1.0 / 4.0
private val F2 = 1.0 / 2.0
fun simplexLinear(seed: Int, x: Double, y: Double) = simplex(seed, x, y, ::linear)
fun simplexQuintic(seed: Int, x: Double, y: Double) = simplex(seed, x, y, ::quintic)
fun simplexHermite(seed: Int, x: Double, y: Double) = simplex(seed, x, y, ::hermite)
fun simplex(seed: Int, x: Double, y: Double): Double {
fun simplex(seed: Int, x: Double, y: Double, interpolator: (Double) -> Double = ::linear): Double {
var t = (x + y) * F2
val i = (x + t).fastFloor()
val j = (y + t).fastFloor()
@@ -13,8 +16,8 @@ fun simplex(seed: Int, x: Double, y: Double): Double {
val X0 = i - t
val Y0 = j - t
val x0 = x - X0
val y0 = y - Y0
val x0 = interpolator(x - X0)
val y0 = interpolator(y - Y0)
val i1: Int
val j1: Int

View File

@@ -1,16 +1,20 @@
package org.openrndr.extra.noise
fun simplex(seed: Int, x: Double, y: Double, z: Double): Double {
fun simplexLinear(seed: Int, x: Double, y: Double, z: Double) = simplex(seed, x, y, z, ::linear)
fun simplexQuintic(seed: Int, x: Double, y: Double, z: Double) = simplex(seed, x, y, z, ::quintic)
fun simplexHermite(seed: Int, x: Double, y: Double, z: Double) = simplex(seed, x, y, z, ::hermite)
var t = (x + y + z) / 3.0
fun simplex(seed: Int, x: Double, y: Double, z: Double, interpolator: (Double) -> Double = ::linear): Double {
val t = (x + y + z) / 3.0
val i = (x + t).fastFloor()
val j = (y + t).fastFloor()
val k = (z + t).fastFloor()
val t2 = (i + j + k) / 6.0
val x0 = x - (i - t2)
val y0 = y - (j - t2)
val z0 = z - (k - t2)
val x0 = interpolator(x - (i - t2))
val y0 = interpolator(y - (j - t2))
val z0 = interpolator(z - (k - t2))
val i1: Int
val j1: Int

View File

@@ -1,20 +1,24 @@
package org.openrndr.extra.noise
private val SIMPLEX_4D = byteArrayOf(
0, 1, 2, 3, 0, 1, 3, 2, 0, 0, 0, 0, 0, 2, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0,
0, 2, 1, 3, 0, 0, 0, 0, 0, 3, 1, 2, 0, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 2, 3, 0, 2, 1, 0, 0, 0, 0, 3, 1, 2, 0,
2, 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 0, 2, 0, 0, 0, 0, 3, 2, 0, 1, 3, 2, 1, 0
)
private val F4 = ((2.23606797 - 1.0) / 4.0)
private val G4 = ((5.0 - 2.23606797) / 20.0)
private const val F4 = ((2.23606797 - 1.0) / 4.0)
private const val G4 = ((5.0 - 2.23606797) / 20.0)
fun simplex(seed: Int, x: Double, y: Double, z: Double, w: Double): Double {
fun simplexLinear(seed: Int, x: Double, y: Double, z: Double, w: Double) = simplex(seed, x, y, z, w, ::linear)
fun simplexQuintic(seed: Int, x: Double, y: Double, z: Double, w: Double) = simplex(seed, x, y, z, w, ::quintic)
fun simplexHermite(seed: Int, x: Double, y: Double, z: Double, w: Double) = simplex(seed, x, y, z, w, ::hermite)
fun simplex(seed: Int, x: Double, y: Double, z: Double, w: Double, interpolator: (Double) -> Double = ::linear): Double {
var t = (x + y + z + w) * F4
val i = (x + t).fastFloor()
@@ -23,10 +27,10 @@ fun simplex(seed: Int, x: Double, y: Double, z: Double, w: Double): Double {
val l = (w + t).fastFloor()
val t2 = (i + j + k + l) * G4
val x0 = x - (i - t2)
val y0 = y - (j - t2)
val z0 = z - (k - t2)
val w0 = w - (l - t2)
val x0 = interpolator(x - (i - t2))
val y0 = interpolator(y - (j - t2))
val z0 = interpolator(z - (k - t2))
val w0 = interpolator(w - (l - t2))
var c = if (x0 > y0) 32 else 0
c += if (x0 > z0) 16 else 0

View File

@@ -5,6 +5,10 @@ import org.openrndr.math.Vector3
import org.openrndr.math.Vector4
import kotlin.random.Random
fun random(min: Double = -1.0, max: Double = 1.0, random: Random = Random.Default): Double {
return (random.nextDouble() * (max - min)) + min
}
fun Double.Companion.uniform(min: Double = -1.0, max: Double = 1.0, random: Random = Random.Default): Double {
return (random.nextDouble() * (max - min)) + min
}

View File

@@ -1,9 +1,8 @@
package org.openrndr.extra.noise
fun valueLinear(seed: Int, x:Double, y:Double) = value(seed, x, y, ::linear)
fun valueQuintic(seed: Int, x:Double, y:Double) = value(seed, x, y, ::quintic)
fun valueHermite(seed: Int, x:Double, y:Double) = value(seed, x, y, ::hermite)
fun valueLinear(seed: Int, x: Double, y: Double) = value(seed, x, y, ::linear)
fun valueQuintic(seed: Int, x: Double, y: Double) = value(seed, x, y, ::quintic)
fun valueHermite(seed: Int, x: Double, y: Double) = value(seed, x, y, ::hermite)
inline fun value(seed: Int, x: Double, y: Double, crossinline interpolation: (Double) -> Double = ::linear): Double {
val x0 = x.fastFloor()

View File

@@ -1,10 +1,10 @@
package org.openrndr.extra.noise
fun valueLinear(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::linear)
fun valueQuintic(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::quintic)
fun valueHermite(seed: Int, x:Double, y:Double, z:Double) = value(seed, x, y, z, ::hermite)
fun valueLinear(seed: Int, x: Double, y: Double, z: Double) = value(seed, x, y, z, ::linear)
fun valueQuintic(seed: Int, x: Double, y: Double, z: Double) = value(seed, x, y, z, ::quintic)
fun valueHermite(seed: Int, x: Double, y: Double, z: Double) = value(seed, x, y, z, ::hermite)
inline fun value(seed:Int, x: Double, y: Double, z: Double, crossinline interpolation:(Double)->Double = ::linear) : Double {
inline fun value(seed: Int, x: Double, y: Double, z: Double, crossinline interpolation: (Double) -> Double = ::linear): Double {
val x0 = x.fastFloor()
val y0 = y.fastFloor()
val z0 = z.fastFloor()