Skip to content

Commit

Permalink
Update OpenSimplex, add missing annotations and improve structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Articdive committed Feb 16, 2021
1 parent 3f851e4 commit 30f4161
Show file tree
Hide file tree
Showing 29 changed files with 2,692 additions and 74 deletions.
16 changes: 16 additions & 0 deletions src/main/java/de/articdive/jnoise/JNoise.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import de.articdive.jnoise.api.NoiseGenerator;
import de.articdive.jnoise.api.NoiseResult;
import de.articdive.jnoise.noise.octaved.OctaveNoiseBuilder;
import de.articdive.jnoise.noise.opensimplex.FastSimplexBuilder;
import de.articdive.jnoise.noise.opensimplex.OpenSimplexBuilder;
import de.articdive.jnoise.noise.opensimplex.SuperSimplexBuilder;
import de.articdive.jnoise.noise.perlin.PerlinNoiseBuilder;
import de.articdive.jnoise.noise.value.ValueNoiseBuilder;
import de.articdive.jnoise.noise.white.WhiteNoiseBuilder;
Expand Down Expand Up @@ -137,11 +139,25 @@ public WhiteNoiseBuilder white() {
return new WhiteNoiseBuilder();
}

/**
* @deprecated updated: use {@link #fastSimplex()} or {@link #superSimplex()}.
*/
@Deprecated
@NotNull
public OpenSimplexBuilder openSimplex() {
return new OpenSimplexBuilder();
}

@NotNull
public FastSimplexBuilder fastSimplex() {
return new FastSimplexBuilder();
}

@NotNull
public SuperSimplexBuilder superSimplex() {
return new SuperSimplexBuilder();
}

@NotNull
public ValueNoiseBuilder value() {
return new ValueNoiseBuilder();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/articdive/jnoise/api/NoiseGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package de.articdive.jnoise.api;

import org.jetbrains.annotations.NotNull;

/**
* @author Lukas Mansour
*/
Expand All @@ -35,6 +37,7 @@ protected NoiseGenerator(long seed) {
* @param y The y value of the point.
* @return A value representing the noise at the point (x,y), its bounds are noise-type dependant!
*/
@NotNull
public abstract R evaluateNoise(double x, double y);

/**
Expand All @@ -45,6 +48,7 @@ protected NoiseGenerator(long seed) {
* @param z The z value of the point.
* @return A value representing the noise at the point (x,y,z), its bounds are noise-type dependant!
*/
@NotNull
public abstract R evaluateNoise(double x, double y, double z);

/**
Expand All @@ -56,5 +60,6 @@ protected NoiseGenerator(long seed) {
* @param w The w value of the point.
* @return A value representing the noise at the point (x,y,z,w), its bounds are noise-type dependant!
*/
@NotNull
public abstract R evaluateNoise(double x, double y, double z, double w);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.articdive.jnoise.api;
package de.articdive.jnoise.distance_functions;

/**
* @author Lukas Mansour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package de.articdive.jnoise.distance_functions;

import de.articdive.jnoise.api.DistanceFunction;

/**
* @author Lukas Mansour
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package de.articdive.jnoise.distance_functions;

import de.articdive.jnoise.api.DistanceFunction;

/**
* Interesting distance function that will be a bit more expensive to compute (especially for large values of p)
* Also to note: p = 1 is the same as Manhattan and p = 2 is the same as EuclidianSquared, so don't use this for p = 1 and p = 2!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
/*
* JNoise
* Copyright (C) 2021 Articdive (Lukas Mansour)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.articdive.jnoise.api;

/**
* @author Lukas Mansour
*/
public interface Interpolation {

/**
* Interpolates a value between two known values.
*
* @param x Position for the data to be interpolated.
* @param a The first known value.
* @param b The second known value.
* @return an interpolated value for x.
*/
double lerp(double x, double a, double b);

/**
* Interpolates between an unknown number of values.
* The amount of positions repsents the dimension
* The amount of values must be 2^(amount of position).
*
* @param positions A list of positions starting with the first stage position and going up in order.
* @param values A list of values starting with the first stage values and going up in order.
* @return an interpolated value between all the given positions.
*/
default double lerp(double[] positions, double[] values) {
if (values.length != 1 << positions.length) {
throw new IllegalArgumentException("The amount of values must be 2^(amount of fractals).");
}
for (int i = 0; i < positions.length; i++) {
for (int j = 0; j < ((1 << positions.length) - i); j += 2) {
values[j / 2] = lerp(positions[i], values[j], values[j + 1]);
}
}
// This should only have 1 element, the final value!
return values[0];
}
}
/*
* JNoise
* Copyright (C) 2021 Articdive (Lukas Mansour)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.articdive.jnoise.interpolation;

/**
* @author Lukas Mansour
*/
public interface Interpolation {

/**
* Interpolates a value between two known values.
*
* @param x Position for the data to be interpolated.
* @param a The first known value.
* @param b The second known value.
* @return an interpolated value for x.
*/
double lerp(double x, double a, double b);

/**
* Interpolates between an unknown number of values.
* The amount of positions repsents the dimension
* The amount of values must be 2^(amount of position).
*
* @param positions A list of positions starting with the first stage position and going up in order.
* @param values A list of values starting with the first stage values and going up in order.
* @return an interpolated value between all the given positions.
*/
default double lerp(double[] positions, double[] values) {
if (values.length != 1 << positions.length) {
throw new IllegalArgumentException("The amount of values must be 2^(amount of fractals).");
}
for (int i = 0; i < positions.length; i++) {
for (int j = 0; j < ((1 << positions.length) - i); j += 2) {
values[j / 2] = lerp(positions[i], values[j], values[j + 1]);
}
}
// This should only have 1 element, the final value!
return values[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package de.articdive.jnoise.interpolation;

import de.articdive.jnoise.api.Interpolation;

/**
* @author Lukas Mansour
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class OctaveNoiseGenerator extends NoiseGenerator<OctaveNoiseResult
}

@Override
@NotNull
public OctaveNoiseResult evaluateNoise(double x, double y) {
double output = 0;
double amplitude = 1;
Expand All @@ -57,6 +58,7 @@ public OctaveNoiseResult evaluateNoise(double x, double y) {
}

@Override
@NotNull
public OctaveNoiseResult evaluateNoise(double x, double y, double z) {
double output = 0;
double amplitude = 1;
Expand All @@ -74,6 +76,7 @@ public OctaveNoiseResult evaluateNoise(double x, double y, double z) {
}

@Override
@NotNull
public OctaveNoiseResult evaluateNoise(double x, double y, double z, double w) {
double output = 0;
double amplitude = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* JNoise
* Copyright (C) 2021 Articdive (Lukas Mansour)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.articdive.jnoise.noise.opensimplex;

import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.api.NoiseBuilder;
import de.articdive.jnoise.simplex_variants.Simplex2DVariant;
import de.articdive.jnoise.simplex_variants.Simplex3DVariant;
import de.articdive.jnoise.simplex_variants.Simplex4DVariant;
import org.jetbrains.annotations.NotNull;

/**
* @author Lukas Mansour
*/
public class FastSimplexBuilder extends NoiseBuilder<FastSimplexBuilder> {
private double frequency = 1.0;
private Simplex2DVariant variant2D = Simplex2DVariant.CLASSIC;
private Simplex3DVariant variant3D = Simplex3DVariant.CLASSIC;
private Simplex4DVariant variant4D = Simplex4DVariant.CLASSIC;

@NotNull
@Override
protected FastSimplexBuilder self() {
return this;
}

/**
* Sets the frequency for the {@link FastSimplexGenerator}.
*
* @param frequency the new frequency for the {@link FastSimplexGenerator}.
* @return {@link FastSimplexBuilder} this
*/
@NotNull
public FastSimplexBuilder setFrequency(double frequency) {
if (frequency <= 0) {
throw new IllegalArgumentException("Frequency must be a non-zero positive value.");
}
this.frequency = frequency;
return this;
}

/**
* Sets the variant used for 2D OpenSimplex noise
*
* @param variant2D the new {@link Simplex2DVariant} for the {@link FastSimplexGenerator}.
* @return {@link FastSimplexBuilder} this
*/
@NotNull
public FastSimplexBuilder setVariant2D(@NotNull Simplex2DVariant variant2D) {
this.variant2D = variant2D;
return this;
}

/**
* Sets the variant used for 3D OpenSimplex noise
*
* @param variant3D the new {@link Simplex3DVariant} for the {@link FastSimplexGenerator}.
* @return {@link FastSimplexBuilder} this
*/
@NotNull
public FastSimplexBuilder setVariant3D(@NotNull Simplex3DVariant variant3D) {
this.variant3D = variant3D;
return this;
}

/**
* Sets the variant used for 4D OpenSimplex noise
*
* @param variant4D the new {@link Simplex4DVariant} for the {@link FastSimplexGenerator}.
* @return {@link FastSimplexBuilder} this
*/
@NotNull
public FastSimplexBuilder setVariant4D(@NotNull Simplex4DVariant variant4D) {
this.variant4D = variant4D;
return this;
}

@Override
@NotNull
public JNoise build() {
return JNoise.build(new FastSimplexGenerator(seed, frequency, variant2D, variant3D, variant4D));
}
}
Loading

0 comments on commit 30f4161

Please sign in to comment.