![JAR search and dependency download from the Maven repository](/logo.png)
commonMain.com.sudoplay.joise.noise.Util.kt Maven / Gradle / Ivy
/*
* Copyright (C) 2013 Jason Taylor.
* Released as open-source under the Apache License, Version 2.0.
*
* ============================================================================
* | Joise
* ============================================================================
*
* Copyright (C) 2013 Jason Taylor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ============================================================================
* | Accidental Noise Library
* | --------------------------------------------------------------------------
* | Joise is a derivative work based on Josua Tippetts' C++ library:
* | http://accidentalnoise.sourceforge.net/index.html
* ============================================================================
*
* Copyright (C) 2011 Joshua Tippetts
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
package com.sudoplay.joise.noise
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic
import kotlin.math.PI
import kotlin.math.floor
import kotlin.math.ln
import kotlin.math.pow
@Suppress("MagicNumber")
object Util {
const val TWO_PI = PI * 2.0
private val INV_LOG_HALF = 1.0 / ln(0.5)
@JvmStatic
fun clamp(input: Double, min: Double, max: Double): Double =
if (input < min) min else if (input > max) max else input
@JvmStatic
fun bias(b: Double, t: Double): Double = t.pow(ln(b) * INV_LOG_HALF)
@JvmStatic
fun lerp(t: Double, a: Double, b: Double): Double = a + t * (b - a)
@JvmStatic
fun gain(g: Double, t: Double): Double {
val g1 = clamp(g, 0.0, 1.0)
return if (t < 0.5) {
bias(1.0 - g1, 2.0 * t) * 0.5
} else {
1.0 - bias(1.0 - g1, 2.0 - 2.0 * t) * 0.5
}
}
@JvmStatic
fun quinticBlend(t: Double): Double = t * t * t * (t * (t * 6 - 15) + 10)
@JvmStatic
fun sawtooth(x: Double, p: Double): Double = 2.0 * (x / p - floor(0.5 + x / p)) * 0.5 + 0.5
class Vector3d @JvmOverloads constructor(var x: Double = 0.0, var y: Double = 0.0, var z: Double = 0.0)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy