All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.sudoplay.joise.module.ModuleRotateDomain.kt Maven / Gradle / Ivy

The newest version!
/*
 * 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.module

import com.sudoplay.joise.noise.Util
import kotlin.math.cos
import kotlin.math.sin

class ModuleRotateDomain : SourcedModule() {

    var rotmatrix = Array(3) { DoubleArray(3) }
    var ax: ScalarParameter = ScalarParameter(0.0)
    var ay: ScalarParameter = ScalarParameter(0.0)
    var az: ScalarParameter = ScalarParameter(0.0)
    var axisangle: ScalarParameter = ScalarParameter(0.0)

    fun setAxis(ax: Double, ay: Double, az: Double) {
        this.ax.value = ax
        this.ay.value = ay
        this.az.value = az
    }

    fun setAxis(
        ax: Module?,
        ay: Module?,
        az: Module?
    ) {
        this.ax.module = ax
        this.ay.module = ay
        this.az.module = az
    }

    fun setAxisX(ax: Double) {
        this.ax.value = ax
    }

    fun setAxisX(ax: Module?) {
        this.ax.module = ax
    }

    fun setAxisY(ay: Double) {
        this.ay.value = ay
    }

    fun setAxisY(ay: Module?) {
        this.ay.module = ay
    }

    fun setAxisZ(az: Double) {
        this.az.value = az
    }

    fun setAxisZ(az: Module?) {
        this.az.module = az
    }

    fun setAngle(a: Double) {
        axisangle.value = a
    }

    fun setAngle(a: Module?) {
        axisangle.module = a
    }

    override fun get(x: Double, y: Double): Double {
        val nx: Double
        val ny: Double
        val angle = axisangle[x, y] * Util.TWO_PI
        val cos2d = cos(angle)
        val sin2d = sin(angle)
        nx = x * cos2d - y * sin2d
        ny = y * cos2d + x * sin2d
        return source[nx, ny]
    }

    override fun get(x: Double, y: Double, z: Double): Double {
        calculateRotMatrix(x, y, z)
        val nx: Double
        val ny: Double
        val nz: Double
        nx = rotmatrix[0][0] * x + rotmatrix[1][0] * y + rotmatrix[2][0] * z
        ny = rotmatrix[0][1] * x + rotmatrix[1][1] * y + rotmatrix[2][1] * z
        nz = rotmatrix[0][2] * x + rotmatrix[1][2] * y + rotmatrix[2][2] * z
        return source[nx, ny, nz]
    }

    override fun get(x: Double, y: Double, z: Double, w: Double): Double {
        calculateRotMatrix(x, y, z, w)
        val nx: Double
        val ny: Double
        val nz: Double
        nx = rotmatrix[0][0] * x + rotmatrix[1][0] * y + rotmatrix[2][0] * z
        ny = rotmatrix[0][1] * x + rotmatrix[1][1] * y + rotmatrix[2][1] * z
        nz = rotmatrix[0][2] * x + rotmatrix[1][2] * y + rotmatrix[2][2] * z
        return source[nx, ny, nz, w]
    }

    override fun get(
        x: Double,
        y: Double,
        z: Double,
        w: Double,
        u: Double,
        v: Double
    ): Double {
        calculateRotMatrix(x, y, z, w, u, v)
        val nx: Double
        val ny: Double
        val nz: Double
        nx = rotmatrix[0][0] * x + rotmatrix[1][0] * y + rotmatrix[2][0] * z
        ny = rotmatrix[0][1] * x + rotmatrix[1][1] * y + rotmatrix[2][1] * z
        nz = rotmatrix[0][2] * x + rotmatrix[1][2] * y + rotmatrix[2][2] * z
        return source[nx, ny, nz, w, u, v]
    }

    fun calculateRotMatrix(x: Double, y: Double) {
        val angle = axisangle[x, y] * Util.TWO_PI
        val ax = ax[x, y]
        val ay = ay[x, y]
        val az = az[x, y]
        calc(angle, ax, ay, az)
    }

    fun calculateRotMatrix(x: Double, y: Double, z: Double) {
        val angle = axisangle[x, y, z] * Util.TWO_PI
        val ax = ax[x, y, z]
        val ay = ay[x, y, z]
        val az = az[x, y, z]
        calc(angle, ax, ay, az)
    }

    fun calculateRotMatrix(
        x: Double,
        y: Double,
        z: Double,
        w: Double
    ) {
        val angle = axisangle[x, y, z, w] * Util.TWO_PI
        val ax = ax[x, y, z, w]
        val ay = ay[x, y, z, w]
        val az = az[x, y, z, w]
        calc(angle, ax, ay, az)
    }

    fun calculateRotMatrix(
        x: Double, y: Double, z: Double, w: Double,
        u: Double, v: Double
    ) {
        val angle = axisangle[x, y, z, w, u, v] * Util.TWO_PI
        val ax = ax[x, y, z, w, u, v]
        val ay = ay[x, y, z, w, u, v]
        val az = az[x, y, z, w, u, v]
        calc(angle, ax, ay, az)
    }

    fun calc(angle: Double, ax: Double, ay: Double, az: Double) {
        val cosangle = cos(angle)
        val sinangle = sin(angle)
        rotmatrix[0][0] = 1.0 + (1.0 - cosangle) * (ax * ax - 1.0)
        rotmatrix[1][0] = -az * sinangle + (1.0 - cosangle) * ax * ay
        rotmatrix[2][0] = ay * sinangle + (1.0 - cosangle) * ax * az
        rotmatrix[0][1] = az * sinangle + (1.0 - cosangle) * ax * ay
        rotmatrix[1][1] = 1.0 + (1.0 - cosangle) * (ay * ay - 1.0)
        rotmatrix[2][1] = -ax * sinangle + (1.0 - cosangle) * ay * az
        rotmatrix[0][2] = -ay * sinangle + (1.0 - cosangle) * ax * az
        rotmatrix[1][2] = ax * sinangle + (1.0 - cosangle) * ay * az
        rotmatrix[2][2] = 1.0 + (1.0 - cosangle) * (az * az - 1.0)
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy