Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: skeeto/perlin-noise
base: 4471844b97
...
head fork: skeeto/perlin-noise
compare: def5f914f4
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
4 src/com/nullprogram/noise/Launcher.java
@@ -15,8 +15,10 @@ public static void main(String[] args) {
int size = 32;
int depth = 4;
float step = 0.04f;
- //Runnable calc = new ClSlice3d(size, size, depth, step);
Runnable calc = new Slice3d(size, size, depth, step);
+ if (args.length > 0 && args[0].equals("opencl")) {
+ calc = new ClSlice3d(size, size, depth, step);
+ }
System.out.println(time(calc) + " seconds");
System.exit(0);
}
View
2  src/com/nullprogram/noise/Noise.java
@@ -10,5 +10,5 @@
* @param p the position to query
* @return the noise value at the selected position
*/
- double sample(Vector p);
+ float sample(Vector p);
}
View
12 src/com/nullprogram/noise/PerlinNoise.java
@@ -33,7 +33,7 @@ public PerlinNoise(int seed, int dimension) {
/* Pre-calulate vectors to find corners. */
for (int i = 0; i < 1 << dimension; i++) {
- double[] v = new double[dimension];
+ float[] v = new float[dimension];
for (int n = 0; n < dimension; n++) {
if ((i & (1 << n)) != 0) {
v[n] = 1;
@@ -46,13 +46,13 @@ public PerlinNoise(int seed, int dimension) {
}
@Override
- public double sample(Vector p) {
- double sum = 0;
+ public float sample(Vector p) {
+ float sum = 0;
Vector pfloor = p.floor();
for (Vector c : corners) {
Vector q = pfloor.add(c);
Vector g = gradient(q);
- double m = g.dot(p.subtract(q));
+ float m = g.dot(p.subtract(q));
Vector t = p.subtract(q).abs().multiply(-1).add(1);
Vector w = t.pow(2).multiply(3).subtract(t.pow(3).multiply(2));
sum += w.prod() * m;
@@ -68,9 +68,9 @@ public double sample(Vector p) {
private Vector gradient(Vector p) {
Vector gradient = gradients.get(p);
if (gradient == null) {
- double[] vector = new double[dimension];
+ float[] vector = new float[dimension];
for (int i = 0; i < vector.length; i++) {
- vector[i] = rng.nextDouble() - 0.5;
+ vector[i] = rng.nextFloat() - 0.5f;
}
gradient = new Vector(vector).unitize();
gradients.put(p, gradient);
View
8 src/com/nullprogram/noise/Slice3d.java
@@ -25,12 +25,12 @@ public void run() {
int count = 0;
for (float z = 0; z < depth; z += step, count++) {
for (int i = 0; i < im.getWidth(); i++) {
- double x = i * step;
+ float x = i * step;
for (int j = 0; j < im.getHeight(); j++) {
- double y = j * step;
+ float y = j * step;
Vector p = new Vector(x, y, z);
- double m = noise.sample(p);
- float v = (float) ((m + 2f / 3f) * 2f / 3f);
+ float m = noise.sample(p);
+ float v = ((m + 2f / 3f) * 2f / 3f);
int c = new Color(v, v, v).getRGB();
im.setRGB(i, j, c);
}
View
46 src/com/nullprogram/noise/Vector.java
@@ -16,13 +16,13 @@
public final class Vector implements Serializable, Cloneable {
/** The actual vector. */
- private final double[] vector;
+ private final float[] vector;
/**
* Create a new vector from values.
* @param values the values of the new vector
*/
- public Vector(double... values) {
+ public Vector(float... values) {
vector = values.clone();
}
@@ -32,7 +32,7 @@ public Vector(double... values) {
* @param clone true if a defensive copy should be made
* @param values the array to be used directly (if clone is false)
*/
- private Vector(boolean clone, double[] values) {
+ private Vector(boolean clone, float[] values) {
if (!clone) {
vector = values;
} else {
@@ -45,7 +45,7 @@ private Vector(boolean clone, double[] values) {
* @param index the index to query
* @return the value at the index
*/
- public double get(int index) {
+ public float get(int index) {
return vector[index];
}
@@ -61,12 +61,12 @@ public int dimension() {
* Compute the magnitude of this vector.
* @return the magnitude of this vector
*/
- public double magnitude() {
- double sum = 0;
+ public float magnitude() {
+ float sum = 0;
for (int i = 0; i < vector.length; i++) {
sum += vector[i] * vector[i];
}
- return Math.sqrt(sum);
+ return (float) Math.sqrt(sum);
}
/**
@@ -74,9 +74,9 @@ public double magnitude() {
* @param v the other vector
* @return the dot product
*/
- public double dot(Vector v) {
+ public float dot(Vector v) {
check(v);
- double sum = 0;
+ float sum = 0;
for (int i = 0; i < vector.length; i++) {
sum += vector[i] * v.vector[i];
}
@@ -90,7 +90,7 @@ public double dot(Vector v) {
*/
public Vector subtract(Vector v) {
check(v);
- double[] diff = new double[vector.length];
+ float[] diff = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
diff[i] = vector[i] - v.vector[i];
}
@@ -104,7 +104,7 @@ public Vector subtract(Vector v) {
*/
public Vector add(Vector v) {
check(v);
- double[] sum = new double[vector.length];
+ float[] sum = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
sum[i] = vector[i] + v.vector[i];
}
@@ -116,9 +116,9 @@ public Vector add(Vector v) {
* @return a new vector with each element floor()ed.
*/
public Vector floor() {
- double[] floor = new double[vector.length];
+ float[] floor = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
- floor[i] = Math.floor(vector[i]);
+ floor[i] = (float) Math.floor(vector[i]);
}
return new Vector(false, floor);
}
@@ -128,7 +128,7 @@ public Vector floor() {
* @return a new vector with each element abs()ed.
*/
public Vector abs() {
- double[] abs = new double[vector.length];
+ float[] abs = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
abs[i] = Math.abs(vector[i]);
}
@@ -140,10 +140,10 @@ public Vector abs() {
* @param e the exponent
* @return a new vector with each element pow()ed.
*/
- public Vector pow(double e) {
- double[] pow = new double[vector.length];
+ public Vector pow(float e) {
+ float[] pow = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
- pow[i] = Math.pow(vector[i], e);
+ pow[i] = (float) Math.pow(vector[i], e);
}
return new Vector(false, pow);
}
@@ -153,8 +153,8 @@ public Vector pow(double e) {
* @param e the scalar
* @return a new vector
*/
- public Vector multiply(double s) {
- double[] mult = new double[vector.length];
+ public Vector multiply(float s) {
+ float[] mult = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
mult[i] = vector[i] * s;
}
@@ -166,8 +166,8 @@ public Vector multiply(double s) {
* @param s the scalar
* @return a new vector
*/
- public Vector add(double s) {
- double[] add = new double[vector.length];
+ public Vector add(float s) {
+ float[] add = new float[vector.length];
for (int i = 0; i < vector.length; i++) {
add[i] = vector[i] + s;
}
@@ -178,8 +178,8 @@ public Vector add(double s) {
* Calculate the product of the elements of this vector.
* @return the product of the elements
*/
- public double prod() {
- double prod = 1;
+ public float prod() {
+ float prod = 1;
for (int i = 0; i < vector.length; i++) {
prod *= vector[i];
}
View
70 test/com/nullprogram/noise/VectorTest.java
@@ -14,39 +14,39 @@ public void lengthTest() {
@Test
public void getTest() {
- Vector v = new Vector(3.0, 2.1);
- assertEquals(v.get(0), 3.0, 1e-15);
- assertEquals(v.get(1), 2.1, 1e-15);
+ Vector v = new Vector(3.0f, 2.1f);
+ assertEquals(v.get(0), 3.0, 1e-7);
+ assertEquals(v.get(1), 2.1, 1e-7);
}
@Test
public void equalsTest() {
- Vector a = new Vector(1.1, 2.5, 0.9);
- Vector b = new Vector(1.1, 2.5, 0.9);
+ Vector a = new Vector(1.1f, 2.5f, 0.9f);
+ Vector b = new Vector(1.1f, 2.5f, 0.9f);
assertEquals(a, b);
assertEquals(a.hashCode(), b.hashCode());
}
@Test
public void cloneTest() {
- Vector a = new Vector(-1.0, 2.0, 9.9);
+ Vector a = new Vector(-1.0f, 2.0f, 9.9f);
Vector b = (Vector) a.clone();
assertEquals(a, b);
}
@Test
public void dotTest() {
- Vector a = new Vector( 4.2, -1.7, 3.9);
- Vector b = new Vector(-5.6, -4.0, 4.0);
- assertEquals(-1.12, a.dot(b), 1e-15);
+ Vector a = new Vector( 4.2f, -1.7f, 3.9f);
+ Vector b = new Vector(-5.6f, -4.0f, 4.0f);
+ assertEquals(-1.12, a.dot(b), 1e-5);
}
@Test
public void subtractTest() {
- Vector a = new Vector(9.3181, 4.5509, 5.3475);
- Vector b = new Vector(9.2859, 8.9444, 5.6423);
+ Vector a = new Vector(9.3181f, 4.5509f, 5.3475f);
+ Vector b = new Vector(9.2859f, 8.9444f, 5.6423f);
Vector result = a.subtract(b);
- double delta = 1e-15;
+ double delta = 1e-6;
assertEquals( 0.0322, result.get(0), delta);
assertEquals(-4.3935, result.get(1), delta);
assertEquals(-0.2948, result.get(2), delta);
@@ -54,10 +54,10 @@ public void subtractTest() {
@Test
public void addTest() {
- Vector a = new Vector(9.3181, 4.5509, 5.3475);
- Vector b = new Vector(9.2859, 8.9444, 5.6423);
+ Vector a = new Vector(9.3181f, 4.5509f, 5.3475f);
+ Vector b = new Vector(9.2859f, 8.9444f, 5.6423f);
Vector result = a.add(b);
- double delta = 1e-14;
+ double delta = 1e-6;
assertEquals(18.6040, result.get(0), delta);
assertEquals(13.4953, result.get(1), delta);
assertEquals(10.9898, result.get(2), delta);
@@ -65,9 +65,9 @@ public void addTest() {
@Test
public void floorTest() {
- Vector v = new Vector(2.1388, -42.1785, 17.0530);
+ Vector v = new Vector(2.1388f, -42.1785f, 17.0530f);
Vector floor = v.floor();
- double delta = 1e-15;
+ double delta = 1e-7;
assertEquals( 2.0, floor.get(0), delta);
assertEquals(-43.0, floor.get(1), delta);
assertEquals( 17.0, floor.get(2), delta);
@@ -75,9 +75,9 @@ public void floorTest() {
@Test
public void absTest() {
- Vector v = new Vector(2.1, -42.1, 17.0);
+ Vector v = new Vector(2.1f, -42.1f, 17.0f);
Vector abs = v.abs();
- double delta = 1e-15;
+ double delta = 1e-5;
assertEquals( 2.1, abs.get(0), delta);
assertEquals(42.1, abs.get(1), delta);
assertEquals(17.0, abs.get(2), delta);
@@ -85,9 +85,9 @@ public void absTest() {
@Test
public void powTest() {
- Vector v = new Vector(2.0, -3.0, 2.5);
- Vector pow = v.pow(2.0);
- double delta = 1e-15;
+ Vector v = new Vector(2.0f, -3.0f, 2.5f);
+ Vector pow = v.pow(2.0f);
+ double delta = 1e-7;
assertEquals(4.0, pow.get(0), delta);
assertEquals(9.0, pow.get(1), delta);
assertEquals(6.25, pow.get(2), delta);
@@ -95,9 +95,9 @@ public void powTest() {
@Test
public void multiplyTest() {
- Vector v = new Vector(2.0, -3.0, 2.5);
- Vector mult = v.multiply(2.0);
- double delta = 1e-15;
+ Vector v = new Vector(2.0f, -3.0f, 2.5f);
+ Vector mult = v.multiply(2.0f);
+ double delta = 1e-7;
assertEquals( 4.0, mult.get(0), delta);
assertEquals(-6.0, mult.get(1), delta);
assertEquals( 5.0, mult.get(2), delta);
@@ -105,9 +105,9 @@ public void multiplyTest() {
@Test
public void scalarAddTest() {
- Vector v = new Vector(2.0, -3.0, 2.5);
- Vector add = v.add(2.5);
- double delta = 1e-15;
+ Vector v = new Vector(2.0f, -3.0f, 2.5f);
+ Vector add = v.add(2.5f);
+ double delta = 1e-7;
assertEquals( 4.5, add.get(0), delta);
assertEquals(-0.5, add.get(1), delta);
assertEquals( 5.0, add.get(2), delta);
@@ -115,20 +115,20 @@ public void scalarAddTest() {
@Test
public void productTest() {
- Vector v = new Vector(2.5, 3.0, -3.5);
- double prod = v.prod();
- assertEquals(2.5 * 3.0 * -3.5, prod, 1e-15);
+ Vector v = new Vector(2.5f, 3.0f, -3.5f);
+ float prod = v.prod();
+ assertEquals(2.5 * 3.0 * -3.5, prod, 1e-7);
}
@Test
public void magnitudeTest() {
- Vector v = new Vector(3.43178, 0.27874, 4.32300);
- assertEquals(5.52658474611581, v.magnitude(), 1e-14);
+ Vector v = new Vector(3.43178f, 0.27874f, 4.32300f);
+ assertEquals(5.52658474611581, v.magnitude(), 1e-6);
}
@Test
public void unitizeTest() {
- Vector v = new Vector(5.703441, 8.076563, 7.470319);
- assertEquals(1.0, v.unitize().magnitude(), 1e-15);
+ Vector v = new Vector(5.703441f, 8.076563f, 7.470319f);
+ assertEquals(1.0, v.unitize().magnitude(), 1e-7);
}
}

No commit comments for this range

Something went wrong with that request. Please try again.