Skip to content

Commit

Permalink
Simplify builder code, fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
Articdive committed Feb 17, 2021
1 parent 30f4161 commit 7e3c5f3
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 83 deletions.
24 changes: 1 addition & 23 deletions src/main/java/de/articdive/jnoise/api/NoiseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,9 @@
import org.jetbrains.annotations.NotNull;

/**
* @param <SELF> The Class of the extending class.
* @author Lukas Mansour
*/
public abstract class NoiseBuilder<SELF extends NoiseBuilder<SELF>> {
protected long seed = 1729;

/**
* Sets the seed of the to-be-generated noise-generator.
*
* @param seed an integer to seed the noise-generator.
* @return {@link NoiseBuilder}
*/
@NotNull
public SELF setSeed(long seed) {
this.seed = seed;
return self();
}

/**
* For default methods to return the correct builder this method must be defined.
*
* @return The builder.
*/
@NotNull
protected abstract SELF self();
public abstract class NoiseBuilder {

/**
* Generates the noise-generator
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/de/articdive/jnoise/api/NoiseGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
* @author Lukas Mansour
*/
public abstract class NoiseGenerator<R extends NoiseResult> {
protected final long seed;

protected NoiseGenerator(long seed) {
this.seed = seed;
protected NoiseGenerator() {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,12 @@
/**
* @author Lukas Mansour
*/
public final class OctaveNoiseBuilder extends NoiseBuilder<OctaveNoiseBuilder> {
public final class OctaveNoiseBuilder extends NoiseBuilder {
private JNoise noise;
private int octaves = 1;
private double persistence = 1;
private double lacunarity = 1;

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

@NotNull
@Override
public OctaveNoiseBuilder setSeed(long seed) {
throw new UnsupportedOperationException(
"Octavted noise does not support a seed as it inherits it from the provided noise."
);
}

/**
* Sets the noise that will be octavated.
*
Expand Down Expand Up @@ -107,8 +93,8 @@ public OctaveNoiseBuilder setLacunarity(double lacunarity) {
@NotNull
public JNoise build() {
if (noise == null) {
throw new NullPointerException("A noise implementation to octavated has not been specified!");
throw new NullPointerException("A noise implementation to octavate has not been specified!");
}
return JNoise.build(new OctaveNoiseGenerator(seed, noise, octaves, persistence, lacunarity));
return JNoise.build(new OctaveNoiseGenerator(noise, octaves, persistence, lacunarity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public final class OctaveNoiseGenerator extends NoiseGenerator<OctaveNoiseResult
private final double persistence;
private final double lacunarity;

OctaveNoiseGenerator(long seed, @NotNull JNoise noise, int octaves, double persistence, double lacunarity) {
super(seed);
OctaveNoiseGenerator(@NotNull JNoise noise, int octaves, double persistence, double lacunarity) {
this.noise = noise;
this.octaves = octaves;
this.persistence = persistence;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@
/**
* @author Lukas Mansour
*/
public class FastSimplexBuilder extends NoiseBuilder<FastSimplexBuilder> {
public class FastSimplexBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.0;
private Simplex2DVariant variant2D = Simplex2DVariant.CLASSIC;
private Simplex3DVariant variant3D = Simplex3DVariant.CLASSIC;
private Simplex4DVariant variant4D = Simplex4DVariant.CLASSIC;

/**
* Sets the seed for the {@link FastSimplexGenerator}.
*
* @param seed the new seed for the {@link FastSimplexGenerator}.
* @return {@link FastSimplexBuilder} this
*/
@NotNull
@Override
protected FastSimplexBuilder self() {
public FastSimplexBuilder setSeed(long seed) {
this.seed = seed;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* @author Lukas Mansour
*/
public class FastSimplexGenerator extends NoiseGenerator<FastSimplexResult> {
private final double frequency;
private final OpenSimplex2F simplex;
private final double frequency;
private final Simplex2DVariant variant2D;
private final Simplex3DVariant variant3D;
private final Simplex4DVariant variant4D;
Expand All @@ -41,9 +41,8 @@ public class FastSimplexGenerator extends NoiseGenerator<FastSimplexResult> {
@NotNull Simplex3DVariant variant3D,
@NotNull Simplex4DVariant variant4D
) {
super(seed);
this.frequency = frequency;
this.simplex = new OpenSimplex2F(seed);
this.frequency = frequency;
this.variant2D = variant2D;
this.variant3D = variant3D;
this.variant4D = variant4D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@

import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.api.NoiseBuilder;
import de.articdive.jnoise.noise.worley.WorleyNoiseBuilder;
import org.jetbrains.annotations.NotNull;

/**
* @author Lukas Mansour
*/
@Deprecated
public final class OpenSimplexBuilder extends NoiseBuilder<OpenSimplexBuilder> {
public final class OpenSimplexBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.0;

/**
* Sets the seed for the {@link OpenSimplexGenerator}.
*
* @param seed the new seed for the {@link OpenSimplexGenerator}.
* @return {@link WorleyNoiseBuilder} this
*/
@NotNull
@Override
protected OpenSimplexBuilder self() {
public OpenSimplexBuilder setSeed(long seed) {
this.seed = seed;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ public final class OpenSimplexGenerator extends NoiseGenerator<OpenSimplexResult
private static final double NORM_CONSTANT_2D = 47;
private static final double NORM_CONSTANT_3D = 103;
private static final double NORM_CONSTANT_4D = 30;
private final long seed;
private final double frequency;

OpenSimplexGenerator(long seed, double frequency) {
super(seed);
this.seed = seed;
this.frequency = frequency;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@
/**
* @author Lukas Mansour
*/
public class SuperSimplexBuilder extends NoiseBuilder<SuperSimplexBuilder> {
public class SuperSimplexBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.0;
private Simplex2DVariant variant2D = Simplex2DVariant.CLASSIC;
private Simplex3DVariant variant3D = Simplex3DVariant.CLASSIC;
private Simplex4DVariant variant4D = Simplex4DVariant.CLASSIC;

/**
* Sets the seed for the {@link SuperSimplexGenerator}.
*
* @param seed the new seed for the {@link SuperSimplexGenerator}.
* @return {@link SuperSimplexBuilder} this
*/
@NotNull
@Override
protected SuperSimplexBuilder self() {
public SuperSimplexBuilder setSeed(long seed) {
this.seed = seed;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
* @author Lukas Mansour
*/
public class SuperSimplexGenerator extends NoiseGenerator<SuperSimplexResult> {
private final double frequency;
private final OpenSimplex2S simplex;
private final double frequency;
private final Simplex2DVariant variant2D;
private final Simplex3DVariant variant3D;
private final Simplex4DVariant variant4D;
Expand All @@ -41,9 +41,8 @@ public class SuperSimplexGenerator extends NoiseGenerator<SuperSimplexResult> {
@NotNull Simplex3DVariant variant3D,
@NotNull Simplex4DVariant variant4D
) {
super(seed);
this.frequency = frequency;
this.simplex = new OpenSimplex2S(seed);
this.frequency = frequency;
this.variant2D = variant2D;
this.variant3D = variant3D;
this.variant4D = variant4D;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@
package de.articdive.jnoise.noise.perlin;

import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.api.NoiseBuilder;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.interpolation.InterpolationType;
import org.jetbrains.annotations.NotNull;

/**
* @author Lukas Mansour
*/
public final class PerlinNoiseBuilder extends NoiseBuilder<PerlinNoiseBuilder> {
public final class PerlinNoiseBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.00;
private Interpolation interpolation = InterpolationType.LINEAR;

@Override
/**
* Sets the seed for the {@link PerlinNoiseGenerator}.
*
* @param seed the new seed for the {@link PerlinNoiseGenerator}.
* @return {@link PerlinNoiseBuilder} this
*/
@NotNull
protected PerlinNoiseBuilder self() {
public PerlinNoiseBuilder setSeed(long seed) {
this.seed = seed;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package de.articdive.jnoise.noise.perlin;

import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.api.NoiseGenerator;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.util.HashUtil;
import de.articdive.jnoise.util.vectors.Vector2D;
import de.articdive.jnoise.util.vectors.Vector3D;
Expand Down Expand Up @@ -76,11 +76,12 @@ public final class PerlinNoiseGenerator extends NoiseGenerator<PerlinNoiseResult
new Vector4D(-1, 1, -1, 0), new Vector4D(-1, -1, -1, 0)
};

private final long seed;
private final Interpolation interpolation;
private final double frequency;

PerlinNoiseGenerator(long seed, @NotNull Interpolation interpolation, double frequency) {
super(seed);
this.seed = seed;
this.interpolation = interpolation;
this.frequency = frequency;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@
package de.articdive.jnoise.noise.value;

import de.articdive.jnoise.JNoise;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.api.NoiseBuilder;
import de.articdive.jnoise.interpolation.Interpolation;
import de.articdive.jnoise.interpolation.InterpolationType;
import org.jetbrains.annotations.NotNull;

/**
* @author Lukas Mansour
*/
public final class ValueNoiseBuilder extends NoiseBuilder<ValueNoiseBuilder> {
public final class ValueNoiseBuilder extends NoiseBuilder {
private long seed = 1729;
private double frequency = 1.00;
private Interpolation interpolation = InterpolationType.LINEAR;

/**
* Sets the seed for the {@link ValueNoiseGenerator}.
*
* @param seed the new seed for the {@link ValueNoiseGenerator}.
* @return {@link ValueNoiseBuilder} this
*/
@NotNull
@Override
protected ValueNoiseBuilder self() {
public ValueNoiseBuilder setSeed(long seed) {
this.seed = seed;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
* @author Lukas Mansour
*/
public final class ValueNoiseGenerator extends NoiseGenerator<ValueNoiseResult> {
private final long seed;
private final Interpolation interpolation;
private final double frequency;

ValueNoiseGenerator(long seed, @NotNull Interpolation interpolation, double frequency) {
super(seed);
this.seed = seed;
this.interpolation = interpolation;
this.frequency = frequency;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@
/**
* @author Lukas Mansour
*/
public final class WhiteNoiseBuilder extends NoiseBuilder<WhiteNoiseBuilder> {
public final class WhiteNoiseBuilder extends NoiseBuilder {
private long seed = 1729;

/**
* Sets the seed for the {@link WhiteNoiseGenerator}.
*
* @param seed the new seed for the {@link WhiteNoiseGenerator}.
* @return {@link WhiteNoiseBuilder} this
*/
@NotNull
@Override
protected WhiteNoiseBuilder self() {
public WhiteNoiseBuilder setSeed(long seed) {
this.seed = seed;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
* @author Lukas Mansour
*/
public final class WhiteNoiseGenerator extends NoiseGenerator<WhiteNoiseResult> {
private final long seed;
private final double[] output = new double[]{0.0, 1.0};

WhiteNoiseGenerator(long seed) {
super(seed);
this.seed = seed;
}

@Override
Expand Down
Loading

0 comments on commit 7e3c5f3

Please sign in to comment.