From e458782c200bd61a5ad18eeaad7443e2d82edefd Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Sun, 5 Feb 2012 12:14:18 -0800 Subject: [PATCH 01/12] Removed unicode copyright symbol from statement (did not change any actual copyright information). It was having problems with Intellij on Windows. I know it sucks, sorry guys. --- src/libnoiseforjava/Interp.java | 214 +-- src/libnoiseforjava/Misc.java | 102 +- src/libnoiseforjava/NoiseGen.java | 524 +++--- .../exception/ExceptionInvalidParam.java | 84 +- .../exception/ExceptionNoModule.java | 82 +- src/libnoiseforjava/model/Cylinder.java | 230 +-- src/libnoiseforjava/model/Line.java | 528 +++--- src/libnoiseforjava/model/Plane.java | 210 +-- src/libnoiseforjava/model/Sphere.java | 236 +-- src/libnoiseforjava/module/Abs.java | 104 +- src/libnoiseforjava/module/Add.java | 106 +- src/libnoiseforjava/module/Billow.java | 368 ++-- src/libnoiseforjava/module/Blend.java | 158 +- src/libnoiseforjava/module/Cached.java | 194 +- src/libnoiseforjava/module/Checkerboard.java | 124 +- src/libnoiseforjava/module/Clamp.java | 180 +- src/libnoiseforjava/module/Const.java | 130 +- src/libnoiseforjava/module/Curve.java | 404 ++-- src/libnoiseforjava/module/Cylinders.java | 206 +-- src/libnoiseforjava/module/Displace.java | 476 ++--- src/libnoiseforjava/module/Exponent.java | 158 +- src/libnoiseforjava/module/Invert.java | 96 +- src/libnoiseforjava/module/Max.java | 108 +- src/libnoiseforjava/module/Min.java | 112 +- src/libnoiseforjava/module/ModuleBase.java | 342 ++-- src/libnoiseforjava/module/Multiply.java | 110 +- src/libnoiseforjava/module/Perlin.java | 714 ++++---- src/libnoiseforjava/module/Power.java | 112 +- src/libnoiseforjava/module/RidgedMulti.java | 714 ++++---- src/libnoiseforjava/module/RotatePoint.java | 432 ++--- src/libnoiseforjava/module/ScaleBias.java | 248 +-- src/libnoiseforjava/module/ScalePoint.java | 364 ++-- src/libnoiseforjava/module/Select.java | 600 +++--- src/libnoiseforjava/module/Spheres.java | 204 +-- src/libnoiseforjava/module/Terrace.java | 652 +++---- .../module/TranslatePoint.java | 384 ++-- src/libnoiseforjava/module/Turbulence.java | 564 +++--- src/libnoiseforjava/module/Voronoi.java | 566 +++--- src/libnoiseforjava/util/ColorCafe.java | 210 +-- src/libnoiseforjava/util/GradientColor.java | 502 ++--- src/libnoiseforjava/util/GradientPoint.java | 148 +- src/libnoiseforjava/util/ImageCafe.java | 346 ++-- src/libnoiseforjava/util/MiscUtilities.java | 104 +- src/libnoiseforjava/util/NoiseMap.java | 300 +-- src/libnoiseforjava/util/NoiseMapBuilder.java | 388 ++-- .../util/NoiseMapBuilderCylinder.java | 426 ++--- .../util/NoiseMapBuilderPlane.java | 490 ++--- .../util/NoiseMapBuilderSphere.java | 390 ++-- src/libnoiseforjava/util/RendererImage.java | 1626 ++++++++--------- .../util/RendererNormalMap.java | 632 +++---- 50 files changed, 8351 insertions(+), 8351 deletions(-) diff --git a/src/libnoiseforjava/Interp.java b/src/libnoiseforjava/Interp.java index 8a630cf..53ed56d 100644 --- a/src/libnoiseforjava/Interp.java +++ b/src/libnoiseforjava/Interp.java @@ -1,107 +1,107 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava; - -public class Interp -{ - - /// Performs cubic interpolation between two values bound between two other - /// values. - /// - /// @param n0 The value before the first value. - /// @param n1 The first value. - /// @param n2 The second value. - /// @param n3 The value after the second value. - /// @param a The alpha value. - /// - /// @returns The interpolated value. - /// - /// The alpha value should range from 0.0 to 1.0. If the alpha value is - /// 0.0, this function returns @a n1. If the alpha value is 1.0, this - /// function returns @a n2. - public static double cubicInterp (double n0, double n1, double n2, double n3, - double a) - { - double p = (n3 - n2) - (n0 - n1); - double q = (n0 - n1) - p; - double r = n2 - n0; - double s = n1; - return p * a * a * a + q * a * a + r * a + s; - } - - /// Performs linear interpolation between two values. - /// - /// @param n0 The first value. - /// @param n1 The second value. - /// @param a The alpha value. - /// - /// @returns The interpolated value. - /// - /// The alpha value should range from 0.0 to 1.0. If the alpha value is - /// 0.0, this function returns @a n0. If the alpha value is 1.0, this - /// function returns @a n1. - public static double linearInterp (double n0, double n1, double a) - { - return ((1.0 - a) * n0) + (a * n1); - } - - /// Maps a value onto a cubic S-curve. - /// - /// @param a The value to map onto a cubic S-curve. - /// - /// @returns The mapped value. - /// - /// @a a should range from 0.0 to 1.0. - /// - /// The derivative of a cubic S-curve is zero at @a a = 0.0 and @a a = - /// 1.0 - public static double SCurve3 (double a) - { - return (a * a * (3.0 - 2.0 * a)); - } - - /// Maps a value onto a quintic S-curve. - /// - /// @param a The value to map onto a quintic S-curve. - /// - /// @returns The mapped value. - /// - /// @a a should range from 0.0 to 1.0. - /// - /// The first derivative of a quintic S-curve is zero at @a a = 0.0 and - /// @a a = 1.0 - /// - /// The second derivative of a quintic S-curve is zero at @a a = 0.0 and - /// @a a = 1.0 - static double SCurve5 (double a) - { - double a3 = a * a * a; - double a4 = a3 * a; - double a5 = a4 * a; - return (6.0 * a5) - (15.0 * a4) + (10.0 * a3); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava; + +public class Interp +{ + + /// Performs cubic interpolation between two values bound between two other + /// values. + /// + /// @param n0 The value before the first value. + /// @param n1 The first value. + /// @param n2 The second value. + /// @param n3 The value after the second value. + /// @param a The alpha value. + /// + /// @returns The interpolated value. + /// + /// The alpha value should range from 0.0 to 1.0. If the alpha value is + /// 0.0, this function returns @a n1. If the alpha value is 1.0, this + /// function returns @a n2. + public static double cubicInterp (double n0, double n1, double n2, double n3, + double a) + { + double p = (n3 - n2) - (n0 - n1); + double q = (n0 - n1) - p; + double r = n2 - n0; + double s = n1; + return p * a * a * a + q * a * a + r * a + s; + } + + /// Performs linear interpolation between two values. + /// + /// @param n0 The first value. + /// @param n1 The second value. + /// @param a The alpha value. + /// + /// @returns The interpolated value. + /// + /// The alpha value should range from 0.0 to 1.0. If the alpha value is + /// 0.0, this function returns @a n0. If the alpha value is 1.0, this + /// function returns @a n1. + public static double linearInterp (double n0, double n1, double a) + { + return ((1.0 - a) * n0) + (a * n1); + } + + /// Maps a value onto a cubic S-curve. + /// + /// @param a The value to map onto a cubic S-curve. + /// + /// @returns The mapped value. + /// + /// @a a should range from 0.0 to 1.0. + /// + /// The derivative of a cubic S-curve is zero at @a a = 0.0 and @a a = + /// 1.0 + public static double SCurve3 (double a) + { + return (a * a * (3.0 - 2.0 * a)); + } + + /// Maps a value onto a quintic S-curve. + /// + /// @param a The value to map onto a quintic S-curve. + /// + /// @returns The mapped value. + /// + /// @a a should range from 0.0 to 1.0. + /// + /// The first derivative of a quintic S-curve is zero at @a a = 0.0 and + /// @a a = 1.0 + /// + /// The second derivative of a quintic S-curve is zero at @a a = 0.0 and + /// @a a = 1.0 + static double SCurve5 (double a) + { + double a3 = a * a * a; + double a4 = a3 * a; + double a5 = a4 * a; + return (6.0 * a5) - (15.0 * a4) + (10.0 * a3); + } + +} diff --git a/src/libnoiseforjava/Misc.java b/src/libnoiseforjava/Misc.java index 101aefd..11d2a7f 100644 --- a/src/libnoiseforjava/Misc.java +++ b/src/libnoiseforjava/Misc.java @@ -1,51 +1,51 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava; - -public class Misc -{ - /// Clamps a value onto a clamping range. - /// - /// @param value The value to clamp. - /// @param lowerBound The lower bound of the clamping range. - /// @param upperBound The upper bound of the clamping range. - /// - /// @returns - /// - @a value if @a value lies between @a lowerBound and @a upperBound. - /// - @a lowerBound if @a value is less than @a lowerBound. - /// - @a upperBound if @a value is greater than @a upperBound. - /// - /// This function does not modify any parameters. - public static int ClampValue (int value, int lowerBound, int upperBound) - { - if (value < lowerBound) - return lowerBound; - else if (value > upperBound) - return upperBound; - else - return value; - } -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava; + +public class Misc +{ + /// Clamps a value onto a clamping range. + /// + /// @param value The value to clamp. + /// @param lowerBound The lower bound of the clamping range. + /// @param upperBound The upper bound of the clamping range. + /// + /// @returns + /// - @a value if @a value lies between @a lowerBound and @a upperBound. + /// - @a lowerBound if @a value is less than @a lowerBound. + /// - @a upperBound if @a value is greater than @a upperBound. + /// + /// This function does not modify any parameters. + public static int ClampValue (int value, int lowerBound, int upperBound) + { + if (value < lowerBound) + return lowerBound; + else if (value > upperBound) + return upperBound; + else + return value; + } +} diff --git a/src/libnoiseforjava/NoiseGen.java b/src/libnoiseforjava/NoiseGen.java index 4996fde..712d864 100644 --- a/src/libnoiseforjava/NoiseGen.java +++ b/src/libnoiseforjava/NoiseGen.java @@ -1,262 +1,262 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava; - -public class NoiseGen -{ - - /// Enumerates the noise quality. - public enum NoiseQuality - { - - /// Generates coherent noise quickly. When a coherent-noise function with - /// this quality setting is used to generate a bump-map image, there are - /// noticeable "creasing" artifacts in the resulting image. This is - /// because the derivative of that function is discontinuous at integer - /// boundaries. - QUALITY_FAST, - - /// Generates standard-quality coherent noise. When a coherent-noise - /// function with this quality setting is used to generate a bump-map - /// image, there are some minor "creasing" artifacts in the resulting - /// image. This is because the second derivative of that function is - /// discontinuous at integer boundaries. - QUALITY_STD, - - /// Generates the best-quality coherent noise. When a coherent-noise - /// function with this quality setting is used to generate a bump-map - /// image, there are no "creasing" artifacts in the resulting image. This - /// is because the first and second derivatives of that function are - /// continuous at integer boundaries. - QUALITY_BEST - } - - static final int X_NOISE_GEN = 1619; - static final int Y_NOISE_GEN = 31337; - static final int Z_NOISE_GEN = 6971; - static final int SEED_NOISE_GEN = 1013; - static final int SHIFT_NOISE_GEN = 8; - - public static double GradientCoherentNoise3D (double x, double y, double z, int seed, - NoiseQuality noiseQuality) - { - - // Create a unit-length cube aligned along an integer boundary. This cube - // surrounds the input point. - int x0 = (x > 0.0? (int)x: (int)x - 1); - int x1 = x0 + 1; - int y0 = (y > 0.0? (int)y: (int)y - 1); - int y1 = y0 + 1; - int z0 = (z > 0.0? (int)z: (int)z - 1); - int z1 = z0 + 1; - - // Map the difference between the coordinates of the input value and the - // coordinates of the cube's outer-lower-left vertex onto an S-curve. - double xs = 0, ys = 0, zs = 0; - switch (noiseQuality) - { - case QUALITY_FAST: - xs = (x - (double)x0); - ys = (y - (double)y0); - zs = (z - (double)z0); - break; - case QUALITY_STD: - xs = Interp.SCurve3 (x - (double)x0); - ys = Interp.SCurve3 (y - (double)y0); - zs = Interp.SCurve3 (z - (double)z0); - break; - case QUALITY_BEST: - xs = Interp.SCurve5 (x - (double)x0); - ys = Interp.SCurve5 (y - (double)y0); - zs = Interp.SCurve5 (z - (double)z0); - break; - } - - // Now calculate the noise values at each vertex of the cube. To generate - // the coherent-noise value at the input point, interpolate these eight - // noise values using the S-curve value as the interpolant (trilinear - // interpolation.) - double n0, n1, ix0, ix1, iy0, iy1; - n0 = GradientNoise3D (x, y, z, x0, y0, z0, seed); - n1 = GradientNoise3D (x, y, z, x1, y0, z0, seed); - ix0 = Interp.linearInterp (n0, n1, xs); - n0 = GradientNoise3D (x, y, z, x0, y1, z0, seed); - n1 = GradientNoise3D (x, y, z, x1, y1, z0, seed); - ix1 = Interp.linearInterp (n0, n1, xs); - iy0 = Interp.linearInterp (ix0, ix1, ys); - n0 = GradientNoise3D (x, y, z, x0, y0, z1, seed); - n1 = GradientNoise3D (x, y, z, x1, y0, z1, seed); - ix0 = Interp.linearInterp (n0, n1, xs); - n0 = GradientNoise3D (x, y, z, x0, y1, z1, seed); - n1 = GradientNoise3D (x, y, z, x1, y1, z1, seed); - ix1 = Interp.linearInterp (n0, n1, xs); - iy1 = Interp.linearInterp (ix0, ix1, ys); - - return Interp.linearInterp (iy0, iy1, zs); - } - - public static double GradientNoise3D (double fx, double fy, double fz, int ix, - int iy, int iz, int seed) - { - - VectorTable vectorTable = new VectorTable(); - // Randomly generate a gradient vector given the integer coordinates of the - // input value. This implementation generates a random number and uses it - // as an index into a normalized-vector lookup table. - int vectorIndex = (X_NOISE_GEN * ix - + Y_NOISE_GEN * iy - + Z_NOISE_GEN * iz - + SEED_NOISE_GEN * seed) - & 0xffffffff; - - vectorIndex ^= (vectorIndex >> SHIFT_NOISE_GEN); - vectorIndex &= 0xff; - - double xvGradient = vectorTable.getRandomVectors(vectorIndex, 0); - double yvGradient = vectorTable.getRandomVectors(vectorIndex, 1); - double zvGradient = vectorTable.getRandomVectors(vectorIndex, 2); - // array size too large when using this original, changed to above for all 3 - // double zvGradient = vectorTable.getRandomVectors(vectorIndex << 2, 2); - - // Set up us another vector equal to the distance between the two vectors - // passed to this function. - double xvPoint = (fx - (double)ix); - double yvPoint = (fy - (double)iy); - double zvPoint = (fz - (double)iz); - - // Now compute the dot product of the gradient vector with the distance - // vector. The resulting value is gradient noise. Apply a scaling value - // so that this noise value ranges from -1.0 to 1.0. - return ((xvGradient * xvPoint) - + (yvGradient * yvPoint) - + (zvGradient * zvPoint)) * 2.12; - } - - public static int IntValueNoise3D (int x, int y, int z, int seed) - { - // All constants are primes and must remain prime in order for this noise - // function to work correctly. - int n = (X_NOISE_GEN * x - + Y_NOISE_GEN * y - + Z_NOISE_GEN * z - + SEED_NOISE_GEN * seed) - & 0x7fffffff; - - n = (n >> 13) ^ n; - - return (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; - } - - public static double ValueCoherentNoise3D (double x, double y, double z, int seed, - NoiseQuality noiseQuality) - { - // Create a unit-length cube aligned along an integer boundary. This cube - // surrounds the input point. - int x0 = (x > 0.0? (int)x: (int)x - 1); - int x1 = x0 + 1; - int y0 = (y > 0.0? (int)y: (int)y - 1); - int y1 = y0 + 1; - int z0 = (z > 0.0? (int)z: (int)z - 1); - int z1 = z0 + 1; - - // Map the difference between the coordinates of the input value and the - // coordinates of the cube's outer-lower-left vertex onto an S-curve. - double xs = 0, ys = 0, zs = 0; - switch (noiseQuality) - { - case QUALITY_FAST: - xs = (x - (double)x0); - ys = (y - (double)y0); - zs = (z - (double)z0); - break; - case QUALITY_STD: - xs = Interp.SCurve3 (x - (double)x0); - ys = Interp.SCurve3 (y - (double)y0); - zs = Interp.SCurve3 (z - (double)z0); - break; - case QUALITY_BEST: - xs = Interp.SCurve5 (x - (double)x0); - ys = Interp.SCurve5 (y - (double)y0); - zs = Interp.SCurve5 (z - (double)z0); - break; - } - - // Now calculate the noise values at each vertex of the cube. To generate - // the coherent-noise value at the input point, interpolate these eight - // noise values using the S-curve value as the interpolant (trilinear - // interpolation.) - double n0, n1, ix0, ix1, iy0, iy1; - n0 = ValueNoise3D (x0, y0, z0, seed); - n1 = ValueNoise3D (x1, y0, z0, seed); - ix0 = Interp.linearInterp (n0, n1, xs); - n0 = ValueNoise3D (x0, y1, z0, seed); - n1 = ValueNoise3D (x1, y1, z0, seed); - ix1 = Interp.linearInterp (n0, n1, xs); - iy0 = Interp.linearInterp (ix0, ix1, ys); - n0 = ValueNoise3D (x0, y0, z1, seed); - n1 = ValueNoise3D (x1, y0, z1, seed); - ix0 = Interp.linearInterp (n0, n1, xs); - n0 = ValueNoise3D (x0, y1, z1, seed); - n1 = ValueNoise3D (x1, y1, z1, seed); - ix1 = Interp.linearInterp (n0, n1, xs); - iy1 = Interp.linearInterp (ix0, ix1, ys); - - return Interp.linearInterp (iy0, iy1, zs); - } - - public static double ValueNoise3D (int x, int y, int z, int seed) - { - return 1.0 - ((double)IntValueNoise3D (x, y, z, seed) / 1073741824.0); - } - - /// Modifies a floating-point value so that it can be stored in a - /// int32 variable. - /// - /// @param n A floating-point number. - /// - /// @returns The modified floating-point number. - /// - /// This function does not modify @a n. - /// - /// In libnoise, the noise-generating algorithms are all integer-based; - /// they use variables of type int32. Before calling a noise - /// function, pass the @a x, @a y, and @a z coordinates to this function to - /// ensure that these coordinates can be cast to a int32 value. - /// - /// Although you could do a straight cast from double to int32, the - /// resulting value may differ between platforms. By using this function, - /// you ensure that the resulting value is identical between platforms. - public static double MakeInt32Range (double n) - { - if (n >= 1073741824.0) - return (2.0 * (n % 1073741824.0)) - 1073741824.0; - else if (n <= -1073741824.0) - return (2.0 * (n % 1073741824.0)) + 1073741824.0; - else - return n; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava; + +public class NoiseGen +{ + + /// Enumerates the noise quality. + public enum NoiseQuality + { + + /// Generates coherent noise quickly. When a coherent-noise function with + /// this quality setting is used to generate a bump-map image, there are + /// noticeable "creasing" artifacts in the resulting image. This is + /// because the derivative of that function is discontinuous at integer + /// boundaries. + QUALITY_FAST, + + /// Generates standard-quality coherent noise. When a coherent-noise + /// function with this quality setting is used to generate a bump-map + /// image, there are some minor "creasing" artifacts in the resulting + /// image. This is because the second derivative of that function is + /// discontinuous at integer boundaries. + QUALITY_STD, + + /// Generates the best-quality coherent noise. When a coherent-noise + /// function with this quality setting is used to generate a bump-map + /// image, there are no "creasing" artifacts in the resulting image. This + /// is because the first and second derivatives of that function are + /// continuous at integer boundaries. + QUALITY_BEST + } + + static final int X_NOISE_GEN = 1619; + static final int Y_NOISE_GEN = 31337; + static final int Z_NOISE_GEN = 6971; + static final int SEED_NOISE_GEN = 1013; + static final int SHIFT_NOISE_GEN = 8; + + public static double GradientCoherentNoise3D (double x, double y, double z, int seed, + NoiseQuality noiseQuality) + { + + // Create a unit-length cube aligned along an integer boundary. This cube + // surrounds the input point. + int x0 = (x > 0.0? (int)x: (int)x - 1); + int x1 = x0 + 1; + int y0 = (y > 0.0? (int)y: (int)y - 1); + int y1 = y0 + 1; + int z0 = (z > 0.0? (int)z: (int)z - 1); + int z1 = z0 + 1; + + // Map the difference between the coordinates of the input value and the + // coordinates of the cube's outer-lower-left vertex onto an S-curve. + double xs = 0, ys = 0, zs = 0; + switch (noiseQuality) + { + case QUALITY_FAST: + xs = (x - (double)x0); + ys = (y - (double)y0); + zs = (z - (double)z0); + break; + case QUALITY_STD: + xs = Interp.SCurve3 (x - (double)x0); + ys = Interp.SCurve3 (y - (double)y0); + zs = Interp.SCurve3 (z - (double)z0); + break; + case QUALITY_BEST: + xs = Interp.SCurve5 (x - (double)x0); + ys = Interp.SCurve5 (y - (double)y0); + zs = Interp.SCurve5 (z - (double)z0); + break; + } + + // Now calculate the noise values at each vertex of the cube. To generate + // the coherent-noise value at the input point, interpolate these eight + // noise values using the S-curve value as the interpolant (trilinear + // interpolation.) + double n0, n1, ix0, ix1, iy0, iy1; + n0 = GradientNoise3D (x, y, z, x0, y0, z0, seed); + n1 = GradientNoise3D (x, y, z, x1, y0, z0, seed); + ix0 = Interp.linearInterp (n0, n1, xs); + n0 = GradientNoise3D (x, y, z, x0, y1, z0, seed); + n1 = GradientNoise3D (x, y, z, x1, y1, z0, seed); + ix1 = Interp.linearInterp (n0, n1, xs); + iy0 = Interp.linearInterp (ix0, ix1, ys); + n0 = GradientNoise3D (x, y, z, x0, y0, z1, seed); + n1 = GradientNoise3D (x, y, z, x1, y0, z1, seed); + ix0 = Interp.linearInterp (n0, n1, xs); + n0 = GradientNoise3D (x, y, z, x0, y1, z1, seed); + n1 = GradientNoise3D (x, y, z, x1, y1, z1, seed); + ix1 = Interp.linearInterp (n0, n1, xs); + iy1 = Interp.linearInterp (ix0, ix1, ys); + + return Interp.linearInterp (iy0, iy1, zs); + } + + public static double GradientNoise3D (double fx, double fy, double fz, int ix, + int iy, int iz, int seed) + { + + VectorTable vectorTable = new VectorTable(); + // Randomly generate a gradient vector given the integer coordinates of the + // input value. This implementation generates a random number and uses it + // as an index into a normalized-vector lookup table. + int vectorIndex = (X_NOISE_GEN * ix + + Y_NOISE_GEN * iy + + Z_NOISE_GEN * iz + + SEED_NOISE_GEN * seed) + & 0xffffffff; + + vectorIndex ^= (vectorIndex >> SHIFT_NOISE_GEN); + vectorIndex &= 0xff; + + double xvGradient = vectorTable.getRandomVectors(vectorIndex, 0); + double yvGradient = vectorTable.getRandomVectors(vectorIndex, 1); + double zvGradient = vectorTable.getRandomVectors(vectorIndex, 2); + // array size too large when using this original, changed to above for all 3 + // double zvGradient = vectorTable.getRandomVectors(vectorIndex << 2, 2); + + // Set up us another vector equal to the distance between the two vectors + // passed to this function. + double xvPoint = (fx - (double)ix); + double yvPoint = (fy - (double)iy); + double zvPoint = (fz - (double)iz); + + // Now compute the dot product of the gradient vector with the distance + // vector. The resulting value is gradient noise. Apply a scaling value + // so that this noise value ranges from -1.0 to 1.0. + return ((xvGradient * xvPoint) + + (yvGradient * yvPoint) + + (zvGradient * zvPoint)) * 2.12; + } + + public static int IntValueNoise3D (int x, int y, int z, int seed) + { + // All constants are primes and must remain prime in order for this noise + // function to work correctly. + int n = (X_NOISE_GEN * x + + Y_NOISE_GEN * y + + Z_NOISE_GEN * z + + SEED_NOISE_GEN * seed) + & 0x7fffffff; + + n = (n >> 13) ^ n; + + return (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; + } + + public static double ValueCoherentNoise3D (double x, double y, double z, int seed, + NoiseQuality noiseQuality) + { + // Create a unit-length cube aligned along an integer boundary. This cube + // surrounds the input point. + int x0 = (x > 0.0? (int)x: (int)x - 1); + int x1 = x0 + 1; + int y0 = (y > 0.0? (int)y: (int)y - 1); + int y1 = y0 + 1; + int z0 = (z > 0.0? (int)z: (int)z - 1); + int z1 = z0 + 1; + + // Map the difference between the coordinates of the input value and the + // coordinates of the cube's outer-lower-left vertex onto an S-curve. + double xs = 0, ys = 0, zs = 0; + switch (noiseQuality) + { + case QUALITY_FAST: + xs = (x - (double)x0); + ys = (y - (double)y0); + zs = (z - (double)z0); + break; + case QUALITY_STD: + xs = Interp.SCurve3 (x - (double)x0); + ys = Interp.SCurve3 (y - (double)y0); + zs = Interp.SCurve3 (z - (double)z0); + break; + case QUALITY_BEST: + xs = Interp.SCurve5 (x - (double)x0); + ys = Interp.SCurve5 (y - (double)y0); + zs = Interp.SCurve5 (z - (double)z0); + break; + } + + // Now calculate the noise values at each vertex of the cube. To generate + // the coherent-noise value at the input point, interpolate these eight + // noise values using the S-curve value as the interpolant (trilinear + // interpolation.) + double n0, n1, ix0, ix1, iy0, iy1; + n0 = ValueNoise3D (x0, y0, z0, seed); + n1 = ValueNoise3D (x1, y0, z0, seed); + ix0 = Interp.linearInterp (n0, n1, xs); + n0 = ValueNoise3D (x0, y1, z0, seed); + n1 = ValueNoise3D (x1, y1, z0, seed); + ix1 = Interp.linearInterp (n0, n1, xs); + iy0 = Interp.linearInterp (ix0, ix1, ys); + n0 = ValueNoise3D (x0, y0, z1, seed); + n1 = ValueNoise3D (x1, y0, z1, seed); + ix0 = Interp.linearInterp (n0, n1, xs); + n0 = ValueNoise3D (x0, y1, z1, seed); + n1 = ValueNoise3D (x1, y1, z1, seed); + ix1 = Interp.linearInterp (n0, n1, xs); + iy1 = Interp.linearInterp (ix0, ix1, ys); + + return Interp.linearInterp (iy0, iy1, zs); + } + + public static double ValueNoise3D (int x, int y, int z, int seed) + { + return 1.0 - ((double)IntValueNoise3D (x, y, z, seed) / 1073741824.0); + } + + /// Modifies a floating-point value so that it can be stored in a + /// int32 variable. + /// + /// @param n A floating-point number. + /// + /// @returns The modified floating-point number. + /// + /// This function does not modify @a n. + /// + /// In libnoise, the noise-generating algorithms are all integer-based; + /// they use variables of type int32. Before calling a noise + /// function, pass the @a x, @a y, and @a z coordinates to this function to + /// ensure that these coordinates can be cast to a int32 value. + /// + /// Although you could do a straight cast from double to int32, the + /// resulting value may differ between platforms. By using this function, + /// you ensure that the resulting value is identical between platforms. + public static double MakeInt32Range (double n) + { + if (n >= 1073741824.0) + return (2.0 * (n % 1073741824.0)) - 1073741824.0; + else if (n <= -1073741824.0) + return (2.0 * (n % 1073741824.0)) + 1073741824.0; + else + return n; + } + +} diff --git a/src/libnoiseforjava/exception/ExceptionInvalidParam.java b/src/libnoiseforjava/exception/ExceptionInvalidParam.java index 8c62ce4..f79bf91 100644 --- a/src/libnoiseforjava/exception/ExceptionInvalidParam.java +++ b/src/libnoiseforjava/exception/ExceptionInvalidParam.java @@ -1,42 +1,42 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.exception; - -public class ExceptionInvalidParam extends Exception -{ - - /** - * - */ - private static final long serialVersionUID = 5603051793321603318L; - - public ExceptionInvalidParam(String message) - { - super(message); - } - -} - +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.exception; + +public class ExceptionInvalidParam extends Exception +{ + + /** + * + */ + private static final long serialVersionUID = 5603051793321603318L; + + public ExceptionInvalidParam(String message) + { + super(message); + } + +} + diff --git a/src/libnoiseforjava/exception/ExceptionNoModule.java b/src/libnoiseforjava/exception/ExceptionNoModule.java index 026d2c5..c6dccd6 100644 --- a/src/libnoiseforjava/exception/ExceptionNoModule.java +++ b/src/libnoiseforjava/exception/ExceptionNoModule.java @@ -1,41 +1,41 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.exception; - -public class ExceptionNoModule extends Exception -{ - - /** - * - */ - private static final long serialVersionUID = -4820882012470782847L; - - public ExceptionNoModule(String message) - { - super(message); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.exception; + +public class ExceptionNoModule extends Exception +{ + + /** + * + */ + private static final long serialVersionUID = -4820882012470782847L; + + public ExceptionNoModule(String message) + { + super(message); + } + +} diff --git a/src/libnoiseforjava/model/Cylinder.java b/src/libnoiseforjava/model/Cylinder.java index 1bda73c..04647b7 100644 --- a/src/libnoiseforjava/model/Cylinder.java +++ b/src/libnoiseforjava/model/Cylinder.java @@ -1,115 +1,115 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.model; - -import libnoiseforjava.module.ModuleBase; - -public class Cylinder -{ - /// Model that defines the surface of a cylinder. - /// - /// @image html modelcylinder.png - /// - /// This model returns an output value from a noise module given the - /// coordinates of an input value located on the surface of a cylinder. - /// - /// To generate an output value, pass the (angle, height) coordinates of - /// an input value to the GetValue() method. - /// - /// This model is useful for creating: - /// - seamless textures that can be mapped onto a cylinder - /// - /// This cylinder has a radius of 1.0 unit and has infinite height. It is - /// oriented along the @a y axis. Its center is located at the origin. - - - - /// A pointer to the noise module used to generate the output values. - ModuleBase module; - - public Cylinder () - { - module = new ModuleBase(1); - } - - Cylinder (ModuleBase module) - { - this.module = module; - } - - /// Returns the output value from the noise module given the - /// (angle, height) coordinates of the specified input value located - /// on the surface of the cylinder. - /// - /// @param angle The angle around the cylinder's center, in degrees. - /// @param height The height along the @a y axis. - /// - /// @returns The output value from the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - /// - /// This output value is generated by the noise module passed to the - /// SetModule() method. - /// - /// This cylinder has a radius of 1.0 unit and has infinite height. - /// It is oriented along the @a y axis. Its center is located at the - /// origin. - public double getValue (double angle, double height) - { - assert (module != null); - - double x, y, z; - x = Math.cos(Math.toRadians(angle)); - y = height; - z = Math.sin(Math.toRadians(angle)); - return module.getValue (x, y, z); - } - - /// Returns the noise module that is used to generate the output - /// values. - /// - /// @returns A reference to the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - public ModuleBase getModule () - { - assert (module != null); - return module; - } - - /// Sets the noise module that is used to generate the output values. - /// - /// @param module The noise module that is used to generate the output - /// values. - /// - /// This noise module must exist for the lifetime of this object, - /// until you pass a new noise module to this method. - public void setModule (ModuleBase module) - { - this.module = module; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.model; + +import libnoiseforjava.module.ModuleBase; + +public class Cylinder +{ + /// Model that defines the surface of a cylinder. + /// + /// @image html modelcylinder.png + /// + /// This model returns an output value from a noise module given the + /// coordinates of an input value located on the surface of a cylinder. + /// + /// To generate an output value, pass the (angle, height) coordinates of + /// an input value to the GetValue() method. + /// + /// This model is useful for creating: + /// - seamless textures that can be mapped onto a cylinder + /// + /// This cylinder has a radius of 1.0 unit and has infinite height. It is + /// oriented along the @a y axis. Its center is located at the origin. + + + + /// A pointer to the noise module used to generate the output values. + ModuleBase module; + + public Cylinder () + { + module = new ModuleBase(1); + } + + Cylinder (ModuleBase module) + { + this.module = module; + } + + /// Returns the output value from the noise module given the + /// (angle, height) coordinates of the specified input value located + /// on the surface of the cylinder. + /// + /// @param angle The angle around the cylinder's center, in degrees. + /// @param height The height along the @a y axis. + /// + /// @returns The output value from the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + /// + /// This output value is generated by the noise module passed to the + /// SetModule() method. + /// + /// This cylinder has a radius of 1.0 unit and has infinite height. + /// It is oriented along the @a y axis. Its center is located at the + /// origin. + public double getValue (double angle, double height) + { + assert (module != null); + + double x, y, z; + x = Math.cos(Math.toRadians(angle)); + y = height; + z = Math.sin(Math.toRadians(angle)); + return module.getValue (x, y, z); + } + + /// Returns the noise module that is used to generate the output + /// values. + /// + /// @returns A reference to the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + public ModuleBase getModule () + { + assert (module != null); + return module; + } + + /// Sets the noise module that is used to generate the output values. + /// + /// @param module The noise module that is used to generate the output + /// values. + /// + /// This noise module must exist for the lifetime of this object, + /// until you pass a new noise module to this method. + public void setModule (ModuleBase module) + { + this.module = module; + } + +} diff --git a/src/libnoiseforjava/model/Line.java b/src/libnoiseforjava/model/Line.java index 1543b0c..7509f90 100644 --- a/src/libnoiseforjava/model/Line.java +++ b/src/libnoiseforjava/model/Line.java @@ -1,264 +1,264 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.model; - -import libnoiseforjava.module.ModuleBase; - -public class Line -{ - - /// Model that defines the displacement of a line segment. - /// - /// This model returns an output value from a noise module given the - /// one-dimensional coordinate of an input value located on a line - /// segment, which can be used as displacements. - /// - /// This class is useful for creating: - /// - roads and rivers - /// - disaffected college students - /// - /// To generate an output value, pass an input value between 0.0 and 1.0 - /// to the getValue() method. 0.0 represents the start position of the - /// line segment and 1.0 represents the end position of the line segment. - - - /// A flag that specifies whether the value is to be attenuated - /// (moved toward 0.0) as the ends of the line segment are approached. - boolean attenuate; - - /// A pointer to the noise module used to generate the output values. - ModuleBase module; - - /// @a x coordinate of the start of the line segment. - double x0; - - /// @a x coordinate of the end of the line segment. - double x1; - - /// @a y coordinate of the start of the line segment. - double y0; - - /// @a y coordinate of the end of the line segment. - double y1; - - /// @a z coordinate of the start of the line segment. - double z0; - - /// @a z coordinate of the end of the line segment. - double z1; - - Line () - { - attenuate = true; - module = new ModuleBase(1); - x0 = 0.0; - x1 = 1.0; - y0 = 0.0; - y1 = 1.0; - z0 = 0.0; - z1 = 1.0; - } - - Line (ModuleBase module) - { - attenuate = true; - this.module = module; - x0 = 0.0; - x1 = 1.0; - y0 = 0.0; - y1 = 1.0; - z0 = 0.0; - z1 = 1.0; - } - - /// Returns the output value from the noise module given the - /// one-dimensional coordinate of the specified input value located - /// on the line segment. - /// - /// @param p The distance along the line segment (ranges from 0.0 - /// to 1.0) - /// - /// @returns The output value from the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - /// @pre The start and end points of the line segment were specified. - /// - /// The output value is generated by the noise module passed to the - /// setModule() method. This value may be attenuated (moved toward - /// 0.0) as @a p approaches either end of the line segment; this is - /// the default behavior. - /// - /// If the value is not to be attenuated, @a p can safely range - /// outside the 0.0 to 1.0 range; the output value will be - /// extrapolated along the line that this segment is part of. - public double getValue (double p) - { - assert (module != null); - - double x = (x1 - x0) * p + x0; - double y = (y1 - y0) * p + y0; - double z = (z1 - z0) * p + z0; - double value = module.getValue (x, y, z); - - if (attenuate) - return p * (1.0 - p) * 4 * value; - else - return value; - } - - /// Returns a flag indicating whether the output value is to be - /// attenuated (moved toward 0.0) as the ends of the line segment are - /// approached by the input value. - /// - /// @returns - /// - @a true if the value is to be attenuated - /// - @a false if not. - public boolean getAttenuate () - { - return attenuate; - } - - /// Returns the noise module that is used to generate the output - /// values. - /// - /// @returns A reference to the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - public ModuleBase getModule () - { - assert (module != null); - return module; - } - - /// Sets a flag indicating that the output value is to be attenuated - /// (moved toward 0.0) as the ends of the line segment are approached. - /// - /// @param att A flag that specifies whether the output value is to be - /// attenuated. - public void setAttenuate (boolean att) - { - attenuate = att; - } - - /// Sets the position ( @a x, @a y, @a z ) of the end of the line - /// segment to choose values along. - /// - /// @param x x coordinate of the end position. - /// @param y y coordinate of the end position. - /// @param z z coordinate of the end position. - public void setEndPoint (double x, double y, double z) - { - x1 = x; - y1 = y; - z1 = z; - } - - /// Sets the noise module that is used to generate the output values. - /// - /// @param module The noise module that is used to generate the output - /// values. - /// - /// This noise module must exist for the lifetime of this object, - /// until you pass a new noise module to this method. - public void setModule (ModuleBase module) - { - this.module = module; - } - - /// Sets the position ( @a x, @a y, @a z ) of the start of the line - /// segment to choose values along. - /// - /// @param x x coordinate of the start position. - /// @param y y coordinate of the start position. - /// @param z z coordinate of the start position. - public void setStartPoint (double x, double y, double z) - { - x0 = x; - y0 = y; - z0 = z; - } - - public double getX0() - { - return x0; - } - - public double getX1() - { - return x1; - } - - public double getY0() - { - return y0; - } - - public double getY1() - { - return y1; - } - - public double getZ0() - { - return z0; - } - - public double getZ1() - { - return z1; - } - - public void setX0(double x0) - { - this.x0 = x0; - } - - public void setX1(double x1) - { - this.x1 = x1; - } - - public void setY0(double y0) - { - this.y0 = y0; - } - - public void setY1(double y1) - { - this.y1 = y1; - } - - public void setZ0(double z0) - { - this.z0 = z0; - } - - public void setZ1(double z1) - { - this.z1 = z1; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.model; + +import libnoiseforjava.module.ModuleBase; + +public class Line +{ + + /// Model that defines the displacement of a line segment. + /// + /// This model returns an output value from a noise module given the + /// one-dimensional coordinate of an input value located on a line + /// segment, which can be used as displacements. + /// + /// This class is useful for creating: + /// - roads and rivers + /// - disaffected college students + /// + /// To generate an output value, pass an input value between 0.0 and 1.0 + /// to the getValue() method. 0.0 represents the start position of the + /// line segment and 1.0 represents the end position of the line segment. + + + /// A flag that specifies whether the value is to be attenuated + /// (moved toward 0.0) as the ends of the line segment are approached. + boolean attenuate; + + /// A pointer to the noise module used to generate the output values. + ModuleBase module; + + /// @a x coordinate of the start of the line segment. + double x0; + + /// @a x coordinate of the end of the line segment. + double x1; + + /// @a y coordinate of the start of the line segment. + double y0; + + /// @a y coordinate of the end of the line segment. + double y1; + + /// @a z coordinate of the start of the line segment. + double z0; + + /// @a z coordinate of the end of the line segment. + double z1; + + Line () + { + attenuate = true; + module = new ModuleBase(1); + x0 = 0.0; + x1 = 1.0; + y0 = 0.0; + y1 = 1.0; + z0 = 0.0; + z1 = 1.0; + } + + Line (ModuleBase module) + { + attenuate = true; + this.module = module; + x0 = 0.0; + x1 = 1.0; + y0 = 0.0; + y1 = 1.0; + z0 = 0.0; + z1 = 1.0; + } + + /// Returns the output value from the noise module given the + /// one-dimensional coordinate of the specified input value located + /// on the line segment. + /// + /// @param p The distance along the line segment (ranges from 0.0 + /// to 1.0) + /// + /// @returns The output value from the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + /// @pre The start and end points of the line segment were specified. + /// + /// The output value is generated by the noise module passed to the + /// setModule() method. This value may be attenuated (moved toward + /// 0.0) as @a p approaches either end of the line segment; this is + /// the default behavior. + /// + /// If the value is not to be attenuated, @a p can safely range + /// outside the 0.0 to 1.0 range; the output value will be + /// extrapolated along the line that this segment is part of. + public double getValue (double p) + { + assert (module != null); + + double x = (x1 - x0) * p + x0; + double y = (y1 - y0) * p + y0; + double z = (z1 - z0) * p + z0; + double value = module.getValue (x, y, z); + + if (attenuate) + return p * (1.0 - p) * 4 * value; + else + return value; + } + + /// Returns a flag indicating whether the output value is to be + /// attenuated (moved toward 0.0) as the ends of the line segment are + /// approached by the input value. + /// + /// @returns + /// - @a true if the value is to be attenuated + /// - @a false if not. + public boolean getAttenuate () + { + return attenuate; + } + + /// Returns the noise module that is used to generate the output + /// values. + /// + /// @returns A reference to the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + public ModuleBase getModule () + { + assert (module != null); + return module; + } + + /// Sets a flag indicating that the output value is to be attenuated + /// (moved toward 0.0) as the ends of the line segment are approached. + /// + /// @param att A flag that specifies whether the output value is to be + /// attenuated. + public void setAttenuate (boolean att) + { + attenuate = att; + } + + /// Sets the position ( @a x, @a y, @a z ) of the end of the line + /// segment to choose values along. + /// + /// @param x x coordinate of the end position. + /// @param y y coordinate of the end position. + /// @param z z coordinate of the end position. + public void setEndPoint (double x, double y, double z) + { + x1 = x; + y1 = y; + z1 = z; + } + + /// Sets the noise module that is used to generate the output values. + /// + /// @param module The noise module that is used to generate the output + /// values. + /// + /// This noise module must exist for the lifetime of this object, + /// until you pass a new noise module to this method. + public void setModule (ModuleBase module) + { + this.module = module; + } + + /// Sets the position ( @a x, @a y, @a z ) of the start of the line + /// segment to choose values along. + /// + /// @param x x coordinate of the start position. + /// @param y y coordinate of the start position. + /// @param z z coordinate of the start position. + public void setStartPoint (double x, double y, double z) + { + x0 = x; + y0 = y; + z0 = z; + } + + public double getX0() + { + return x0; + } + + public double getX1() + { + return x1; + } + + public double getY0() + { + return y0; + } + + public double getY1() + { + return y1; + } + + public double getZ0() + { + return z0; + } + + public double getZ1() + { + return z1; + } + + public void setX0(double x0) + { + this.x0 = x0; + } + + public void setX1(double x1) + { + this.x1 = x1; + } + + public void setY0(double y0) + { + this.y0 = y0; + } + + public void setY1(double y1) + { + this.y1 = y1; + } + + public void setZ0(double z0) + { + this.z0 = z0; + } + + public void setZ1(double z1) + { + this.z1 = z1; + } + +} diff --git a/src/libnoiseforjava/model/Plane.java b/src/libnoiseforjava/model/Plane.java index 8cabdda..4d498f4 100644 --- a/src/libnoiseforjava/model/Plane.java +++ b/src/libnoiseforjava/model/Plane.java @@ -1,105 +1,105 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.model; - -import libnoiseforjava.module.ModuleBase; - -public class Plane -{ - /// Model that defines the surface of a plane. - /// - /// This model returns an output value from a noise module given the - /// coordinates of an input value located on the surface of an ( @a x, - /// @a z ) plane. - /// - /// To generate an output value, pass the ( @a x, @a z ) coordinates of - /// an input value to the GetValue() method. - /// - /// This model is useful for creating: - /// - two-dimensional textures - /// - terrain height maps for local areas - /// - /// This plane extends infinitely in both directions. - - - /// A pointer to the noise module used to generate the output values. - ModuleBase module; - - public Plane () - { - module = new ModuleBase(1); - } - - public Plane (ModuleBase module) - { - this.module = module; - } - - /// Returns the output value from the noise module given the - /// ( @a x, @a z ) coordinates of the specified input value located - /// on the surface of the plane. - /// - /// @param x The @a x coordinate of the input value. - /// @param z The @a z coordinate of the input value. - /// - /// @returns The output value from the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - /// - /// This output value is generated by the noise module passed to the - /// setModule() method. - public double getValue (double x, double z) - { - assert (module != null); - - return module.getValue (x, 0, z); - } - - /// Returns the noise module that is used to generate the output - /// values. - /// - /// @returns A reference to the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - public ModuleBase getModule () - { - assert (module != null); - return module; - } - - /// Sets the noise module that is used to generate the output values. - /// - /// @param module The noise module that is used to generate the output - /// values. - /// - /// This noise module must exist for the lifetime of this object, - /// until you pass a new noise module to this method. - public void setModule (ModuleBase module) - { - this.module = module; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.model; + +import libnoiseforjava.module.ModuleBase; + +public class Plane +{ + /// Model that defines the surface of a plane. + /// + /// This model returns an output value from a noise module given the + /// coordinates of an input value located on the surface of an ( @a x, + /// @a z ) plane. + /// + /// To generate an output value, pass the ( @a x, @a z ) coordinates of + /// an input value to the GetValue() method. + /// + /// This model is useful for creating: + /// - two-dimensional textures + /// - terrain height maps for local areas + /// + /// This plane extends infinitely in both directions. + + + /// A pointer to the noise module used to generate the output values. + ModuleBase module; + + public Plane () + { + module = new ModuleBase(1); + } + + public Plane (ModuleBase module) + { + this.module = module; + } + + /// Returns the output value from the noise module given the + /// ( @a x, @a z ) coordinates of the specified input value located + /// on the surface of the plane. + /// + /// @param x The @a x coordinate of the input value. + /// @param z The @a z coordinate of the input value. + /// + /// @returns The output value from the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + /// + /// This output value is generated by the noise module passed to the + /// setModule() method. + public double getValue (double x, double z) + { + assert (module != null); + + return module.getValue (x, 0, z); + } + + /// Returns the noise module that is used to generate the output + /// values. + /// + /// @returns A reference to the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + public ModuleBase getModule () + { + assert (module != null); + return module; + } + + /// Sets the noise module that is used to generate the output values. + /// + /// @param module The noise module that is used to generate the output + /// values. + /// + /// This noise module must exist for the lifetime of this object, + /// until you pass a new noise module to this method. + public void setModule (ModuleBase module) + { + this.module = module; + } + +} diff --git a/src/libnoiseforjava/model/Sphere.java b/src/libnoiseforjava/model/Sphere.java index b6733d7..43d2bb9 100644 --- a/src/libnoiseforjava/model/Sphere.java +++ b/src/libnoiseforjava/model/Sphere.java @@ -1,118 +1,118 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.model; - -import libnoiseforjava.module.ModuleBase; - -public class Sphere -{ - /// Model that defines the surface of a sphere. - /// - /// @image html modelsphere.png - /// - /// This model returns an output value from a noise module given the - /// coordinates of an input value located on the surface of a sphere. - /// - /// To generate an output value, pass the (latitude, longitude) - /// coordinates of an input value to the getValue() method. - /// - /// This model is useful for creating: - /// - seamless textures that can be mapped onto a sphere - /// - terrain height maps for entire planets - /// - /// This sphere has a radius of 1.0 unit and its center is located at - /// the origin. - - /// A pointer to the noise module used to generate the output values. - ModuleBase module; - - - public Sphere() - { - module = new ModuleBase(1); - } - - Sphere(ModuleBase module) - { - this.module = module; - } - - /// Returns the output value from the noise module given the - /// (latitude, longitude) coordinates of the specified input value - /// located on the surface of the sphere. - /// - /// @param lat The latitude of the input value, in degrees. - /// @param lon The longitude of the input value, in degrees. - /// - /// @returns The output value from the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - /// - /// This output value is generated by the noise module passed to the - /// setModule() method. - /// - /// Use a negative latitude if the input value is located on the - /// southern hemisphere. - /// - /// Use a negative longitude if the input value is located on the - /// western hemisphere. - public double getValue (double lat, double lon) - { - assert (module != null); - - double x, y, z; - double r = Math.cos(Math.toRadians(lat)); - x = r * Math.cos (Math.toRadians(lon)); - y = Math.sin (Math.toRadians(lat)); - z = r * Math.sin (Math.toRadians(lon)); - return module.getValue (x, y, z); - } - - /// Returns the noise module that is used to generate the output - /// values. - /// - /// @returns A reference to the noise module. - /// - /// @pre A noise module was passed to the setModule() method. - public ModuleBase getModule () - { - assert (module != null); - return module; - } - - /// Sets the noise module that is used to generate the output values. - /// - /// @param module The noise module that is used to generate the output - /// values. - /// - /// This noise module must exist for the lifetime of this object, - /// until you pass a new noise module to this method. - public void setModule (ModuleBase module) - { - this.module = module; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.model; + +import libnoiseforjava.module.ModuleBase; + +public class Sphere +{ + /// Model that defines the surface of a sphere. + /// + /// @image html modelsphere.png + /// + /// This model returns an output value from a noise module given the + /// coordinates of an input value located on the surface of a sphere. + /// + /// To generate an output value, pass the (latitude, longitude) + /// coordinates of an input value to the getValue() method. + /// + /// This model is useful for creating: + /// - seamless textures that can be mapped onto a sphere + /// - terrain height maps for entire planets + /// + /// This sphere has a radius of 1.0 unit and its center is located at + /// the origin. + + /// A pointer to the noise module used to generate the output values. + ModuleBase module; + + + public Sphere() + { + module = new ModuleBase(1); + } + + Sphere(ModuleBase module) + { + this.module = module; + } + + /// Returns the output value from the noise module given the + /// (latitude, longitude) coordinates of the specified input value + /// located on the surface of the sphere. + /// + /// @param lat The latitude of the input value, in degrees. + /// @param lon The longitude of the input value, in degrees. + /// + /// @returns The output value from the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + /// + /// This output value is generated by the noise module passed to the + /// setModule() method. + /// + /// Use a negative latitude if the input value is located on the + /// southern hemisphere. + /// + /// Use a negative longitude if the input value is located on the + /// western hemisphere. + public double getValue (double lat, double lon) + { + assert (module != null); + + double x, y, z; + double r = Math.cos(Math.toRadians(lat)); + x = r * Math.cos (Math.toRadians(lon)); + y = Math.sin (Math.toRadians(lat)); + z = r * Math.sin (Math.toRadians(lon)); + return module.getValue (x, y, z); + } + + /// Returns the noise module that is used to generate the output + /// values. + /// + /// @returns A reference to the noise module. + /// + /// @pre A noise module was passed to the setModule() method. + public ModuleBase getModule () + { + assert (module != null); + return module; + } + + /// Sets the noise module that is used to generate the output values. + /// + /// @param module The noise module that is used to generate the output + /// values. + /// + /// This noise module must exist for the lifetime of this object, + /// until you pass a new noise module to this method. + public void setModule (ModuleBase module) + { + this.module = module; + } + +} diff --git a/src/libnoiseforjava/module/Abs.java b/src/libnoiseforjava/module/Abs.java index b1f06df..89e5c22 100644 --- a/src/libnoiseforjava/module/Abs.java +++ b/src/libnoiseforjava/module/Abs.java @@ -1,52 +1,52 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Abs extends ModuleBase -{ - /// Noise module that outputs the absolute value of the output value from - /// a source module. - /// - /// @image html moduleabs.png - /// - /// This noise module requires one source module. - - Abs (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - } - - public double getValue (double x, double y, double z) - { - assert (this.sourceModules[0] != null); - - return Math.abs(this.sourceModules[0].getValue (x, y, z)); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Abs extends ModuleBase +{ + /// Noise module that outputs the absolute value of the output value from + /// a source module. + /// + /// @image html moduleabs.png + /// + /// This noise module requires one source module. + + Abs (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + } + + public double getValue (double x, double y, double z) + { + assert (this.sourceModules[0] != null); + + return Math.abs(this.sourceModules[0].getValue (x, y, z)); + } + +} diff --git a/src/libnoiseforjava/module/Add.java b/src/libnoiseforjava/module/Add.java index 3cceecb..f06c895 100644 --- a/src/libnoiseforjava/module/Add.java +++ b/src/libnoiseforjava/module/Add.java @@ -1,53 +1,53 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Add extends ModuleBase -{ - /// Noise module that outputs the additive value of the output value from - /// two source modules. - /// - /// This noise module requires two source modules. - - public Add (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam - { - super(2); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - - return sourceModules[0].getValue (x, y, z) - + sourceModules[1].getValue (x, y, z); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Add extends ModuleBase +{ + /// Noise module that outputs the additive value of the output value from + /// two source modules. + /// + /// This noise module requires two source modules. + + public Add (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam + { + super(2); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + + return sourceModules[0].getValue (x, y, z) + + sourceModules[1].getValue (x, y, z); + } + +} diff --git a/src/libnoiseforjava/module/Billow.java b/src/libnoiseforjava/module/Billow.java index a63d1ae..047c8df 100644 --- a/src/libnoiseforjava/module/Billow.java +++ b/src/libnoiseforjava/module/Billow.java @@ -1,184 +1,184 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.NoiseGen; -import libnoiseforjava.NoiseGen.NoiseQuality; - -public class Billow extends ModuleBase -{ - /// Noise module that outputs three-dimensional "billowy" noise. - /// - /// @image html modulebillow.png - /// - /// This noise module generates "billowy" noise suitable for clouds and - /// rocks. - /// - /// This noise module is nearly identical to noise::module::Perlin except - /// this noise module modifies each octave with an absolute-value - /// function. See the documentation of noise::module::Perlin for more - /// information. - - - /// Default frequency for the Billow noise module. - static final double DEFAULT_BILLOW_FREQUENCY = 1.0; - - /// Default lacunarity for the Billow noise module. - static final double DEFAULT_BILLOW_LACUNARITY = 2.0; - - /// Default number of octaves for the the noise::module::Billow noise - /// module. - static final int DEFAULT_BILLOW_OCTAVE_COUNT = 6; - - /// Default persistence value for the the noise::module::Billow noise - /// module. - static final double DEFAULT_BILLOW_PERSISTENCE = 0.5; - - /// Default noise quality for the the noise::module::Billow noise module. - static final NoiseQuality DEFAULT_BILLOW_QUALITY = NoiseQuality.QUALITY_STD; - - /// Default noise seed for the the noise::module::Billow noise module. - static final int DEFAULT_BILLOW_SEED = 0; - - /// Maximum number of octaves for the the noise::module::Billow noise - /// module. - static final int BILLOW_MAX_OCTAVE = 30; - - double frequency, lacunarity, persistence; - int octaveCount, seed; - NoiseQuality noiseQuality; - - public Billow () - { - super(0); - frequency = DEFAULT_BILLOW_FREQUENCY; - lacunarity = DEFAULT_BILLOW_LACUNARITY; - noiseQuality = DEFAULT_BILLOW_QUALITY; - octaveCount = DEFAULT_BILLOW_OCTAVE_COUNT; - persistence = DEFAULT_BILLOW_PERSISTENCE; - seed = DEFAULT_BILLOW_SEED; - } - - public double getValue (double x, double y, double z) - { - double value = 0.0; - double signal = 0.0; - double curPersistence = 1.0; - double nx, ny, nz; - int calcSeed; - - x *= frequency; - y *= frequency; - z *= frequency; - - for (int curOctave = 0; curOctave < octaveCount; curOctave++) - { - // Make sure that these floating-point values have the same range as a 32- - // bit integer so that we can pass them to the coherent-noise functions. - nx = NoiseGen.MakeInt32Range (x); - ny = NoiseGen.MakeInt32Range (y); - nz = NoiseGen.MakeInt32Range (z); - - // Get the coherent-noise value from the input value and add it to the - // final result. - calcSeed = (seed + curOctave) & 0xffffffff; - signal = NoiseGen.GradientCoherentNoise3D (nx, ny, nz, calcSeed, noiseQuality); - signal = 2.0 * Math.abs(signal) - 1.0; - value += signal * curPersistence; - - // Prepare the next octave. - x *= lacunarity; - y *= lacunarity; - z *= lacunarity; - curPersistence *= persistence; - } - - value += 0.5; - - return value; - } - - public double getFrequency() - { - return frequency; - } - - public double getLacunarity() - { - return lacunarity; - } - - public double getPersistence() - { - return persistence; - } - - public int getOctaveCount() - { - return octaveCount; - } - - public int getSeed() - { - return seed; - } - - public NoiseQuality getNoiseQuality() - { - return noiseQuality; - } - - public void setFrequency(double frequency) - { - this.frequency = frequency; - } - - public void setLacunarity(double lacunarity) - { - this.lacunarity = lacunarity; - } - - public void setPersistence(double persistence) - { - this.persistence = persistence; - } - - public void setOctaveCount(int octaveCount) - { - this.octaveCount = octaveCount; - } - - public void setSeed(int seed) - { - this.seed = seed; - } - - public void setNoiseQuality(NoiseQuality noiseQuality) - { - this.noiseQuality = noiseQuality; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.NoiseGen; +import libnoiseforjava.NoiseGen.NoiseQuality; + +public class Billow extends ModuleBase +{ + /// Noise module that outputs three-dimensional "billowy" noise. + /// + /// @image html modulebillow.png + /// + /// This noise module generates "billowy" noise suitable for clouds and + /// rocks. + /// + /// This noise module is nearly identical to noise::module::Perlin except + /// this noise module modifies each octave with an absolute-value + /// function. See the documentation of noise::module::Perlin for more + /// information. + + + /// Default frequency for the Billow noise module. + static final double DEFAULT_BILLOW_FREQUENCY = 1.0; + + /// Default lacunarity for the Billow noise module. + static final double DEFAULT_BILLOW_LACUNARITY = 2.0; + + /// Default number of octaves for the the noise::module::Billow noise + /// module. + static final int DEFAULT_BILLOW_OCTAVE_COUNT = 6; + + /// Default persistence value for the the noise::module::Billow noise + /// module. + static final double DEFAULT_BILLOW_PERSISTENCE = 0.5; + + /// Default noise quality for the the noise::module::Billow noise module. + static final NoiseQuality DEFAULT_BILLOW_QUALITY = NoiseQuality.QUALITY_STD; + + /// Default noise seed for the the noise::module::Billow noise module. + static final int DEFAULT_BILLOW_SEED = 0; + + /// Maximum number of octaves for the the noise::module::Billow noise + /// module. + static final int BILLOW_MAX_OCTAVE = 30; + + double frequency, lacunarity, persistence; + int octaveCount, seed; + NoiseQuality noiseQuality; + + public Billow () + { + super(0); + frequency = DEFAULT_BILLOW_FREQUENCY; + lacunarity = DEFAULT_BILLOW_LACUNARITY; + noiseQuality = DEFAULT_BILLOW_QUALITY; + octaveCount = DEFAULT_BILLOW_OCTAVE_COUNT; + persistence = DEFAULT_BILLOW_PERSISTENCE; + seed = DEFAULT_BILLOW_SEED; + } + + public double getValue (double x, double y, double z) + { + double value = 0.0; + double signal = 0.0; + double curPersistence = 1.0; + double nx, ny, nz; + int calcSeed; + + x *= frequency; + y *= frequency; + z *= frequency; + + for (int curOctave = 0; curOctave < octaveCount; curOctave++) + { + // Make sure that these floating-point values have the same range as a 32- + // bit integer so that we can pass them to the coherent-noise functions. + nx = NoiseGen.MakeInt32Range (x); + ny = NoiseGen.MakeInt32Range (y); + nz = NoiseGen.MakeInt32Range (z); + + // Get the coherent-noise value from the input value and add it to the + // final result. + calcSeed = (seed + curOctave) & 0xffffffff; + signal = NoiseGen.GradientCoherentNoise3D (nx, ny, nz, calcSeed, noiseQuality); + signal = 2.0 * Math.abs(signal) - 1.0; + value += signal * curPersistence; + + // Prepare the next octave. + x *= lacunarity; + y *= lacunarity; + z *= lacunarity; + curPersistence *= persistence; + } + + value += 0.5; + + return value; + } + + public double getFrequency() + { + return frequency; + } + + public double getLacunarity() + { + return lacunarity; + } + + public double getPersistence() + { + return persistence; + } + + public int getOctaveCount() + { + return octaveCount; + } + + public int getSeed() + { + return seed; + } + + public NoiseQuality getNoiseQuality() + { + return noiseQuality; + } + + public void setFrequency(double frequency) + { + this.frequency = frequency; + } + + public void setLacunarity(double lacunarity) + { + this.lacunarity = lacunarity; + } + + public void setPersistence(double persistence) + { + this.persistence = persistence; + } + + public void setOctaveCount(int octaveCount) + { + this.octaveCount = octaveCount; + } + + public void setSeed(int seed) + { + this.seed = seed; + } + + public void setNoiseQuality(NoiseQuality noiseQuality) + { + this.noiseQuality = noiseQuality; + } + +} diff --git a/src/libnoiseforjava/module/Blend.java b/src/libnoiseforjava/module/Blend.java index 4df0dab..5e442b7 100644 --- a/src/libnoiseforjava/module/Blend.java +++ b/src/libnoiseforjava/module/Blend.java @@ -1,79 +1,79 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.Interp; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Blend extends ModuleBase -{ - /// Noise module that outputs a weighted blend of the output values from - /// two source modules given the output value supplied by a control module. - /// - /// Unlike most other noise modules, the index value assigned to a source - /// module determines its role in the blending operation: - /// - Source module 0 outputs one of the - /// values to blend. - /// - Source module 1 outputs one of the - /// values to blend. - /// - Source module 2 is known as the control - /// module. The control module determines the weight of the - /// blending operation. Negative values weigh the blend towards the - /// output value from the source module with an index value of 0. - /// Positive values weigh the blend towards the output value from the - /// source module with an index value of 1. - /// - /// An application can pass the control module to the setControlModule() - /// method instead of the setSourceModule() method. This may make the - /// application code easier to read. - /// - /// This noise module uses linear interpolation to perform the blending - /// operation. - /// - /// This noise module requires three source modules. - - public Blend (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, - ModuleBase sourceModuleThree) throws ExceptionInvalidParam - { - super(3); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - setSourceModule(2, sourceModuleThree); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - assert (sourceModules[2] != null); - - double v0 = sourceModules[0].getValue (x, y, z); - double v1 = sourceModules[1].getValue (x, y, z); - double alpha = (sourceModules[2].getValue (x, y, z) + 1.0) / 2.0; - - return Interp.linearInterp (v0, v1, alpha); - } -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.Interp; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Blend extends ModuleBase +{ + /// Noise module that outputs a weighted blend of the output values from + /// two source modules given the output value supplied by a control module. + /// + /// Unlike most other noise modules, the index value assigned to a source + /// module determines its role in the blending operation: + /// - Source module 0 outputs one of the + /// values to blend. + /// - Source module 1 outputs one of the + /// values to blend. + /// - Source module 2 is known as the control + /// module. The control module determines the weight of the + /// blending operation. Negative values weigh the blend towards the + /// output value from the source module with an index value of 0. + /// Positive values weigh the blend towards the output value from the + /// source module with an index value of 1. + /// + /// An application can pass the control module to the setControlModule() + /// method instead of the setSourceModule() method. This may make the + /// application code easier to read. + /// + /// This noise module uses linear interpolation to perform the blending + /// operation. + /// + /// This noise module requires three source modules. + + public Blend (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, + ModuleBase sourceModuleThree) throws ExceptionInvalidParam + { + super(3); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + setSourceModule(2, sourceModuleThree); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + assert (sourceModules[2] != null); + + double v0 = sourceModules[0].getValue (x, y, z); + double v1 = sourceModules[1].getValue (x, y, z); + double alpha = (sourceModules[2].getValue (x, y, z) + 1.0) / 2.0; + + return Interp.linearInterp (v0, v1, alpha); + } +} diff --git a/src/libnoiseforjava/module/Cached.java b/src/libnoiseforjava/module/Cached.java index ddfdd6c..3afab4c 100644 --- a/src/libnoiseforjava/module/Cached.java +++ b/src/libnoiseforjava/module/Cached.java @@ -1,97 +1,97 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Cached extends ModuleBase -{ - /// Noise module that caches the last output value generated by a source - /// module. - /// - /// If an application passes an input value to the getValue() method that - /// differs from the previously passed-in input value, this noise module - /// instructs the source module to calculate the output value. This - /// value, as well as the ( @a x, @a y, @a z ) coordinates of the input - /// value, are stored (cached) in this noise module. - /// - /// If the application passes an input value to the getValue() method - /// that is equal to the previously passed-in input value, this noise - /// module returns the cached output value without having the source - /// module recalculate the output value. - /// - /// If an application passes a new source module to the setSourceModule() - /// method, the cache is invalidated. - /// - /// Caching a noise module is useful if it is used as a source module for - /// multiple noise modules. If a source module is not cached, the source - /// module will redundantly calculate the same output value once for each - /// noise module in which it is included. - /// - /// This noise module requires one source module. - - /// The cached output value at the cached input value. - double cachedValue; - - /// Determines if a cached output value is stored in this noise module. - boolean isCached; - - /// @a x coordinate of the cached input value. - double xCache; - - /// @a y coordinate of the cached input value. - double yCache; - - /// @a z coordinate of the cached input value. - double zCache; - - public Cached(ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - isCached = false; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - if (!(isCached && x == xCache && y == yCache && z == zCache)) - { - cachedValue = sourceModules[0].getValue (x, y, z); - xCache = x; - yCache = y; - zCache = z; - } - - isCached = true; - - return cachedValue; - } - - - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Cached extends ModuleBase +{ + /// Noise module that caches the last output value generated by a source + /// module. + /// + /// If an application passes an input value to the getValue() method that + /// differs from the previously passed-in input value, this noise module + /// instructs the source module to calculate the output value. This + /// value, as well as the ( @a x, @a y, @a z ) coordinates of the input + /// value, are stored (cached) in this noise module. + /// + /// If the application passes an input value to the getValue() method + /// that is equal to the previously passed-in input value, this noise + /// module returns the cached output value without having the source + /// module recalculate the output value. + /// + /// If an application passes a new source module to the setSourceModule() + /// method, the cache is invalidated. + /// + /// Caching a noise module is useful if it is used as a source module for + /// multiple noise modules. If a source module is not cached, the source + /// module will redundantly calculate the same output value once for each + /// noise module in which it is included. + /// + /// This noise module requires one source module. + + /// The cached output value at the cached input value. + double cachedValue; + + /// Determines if a cached output value is stored in this noise module. + boolean isCached; + + /// @a x coordinate of the cached input value. + double xCache; + + /// @a y coordinate of the cached input value. + double yCache; + + /// @a z coordinate of the cached input value. + double zCache; + + public Cached(ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + isCached = false; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + if (!(isCached && x == xCache && y == yCache && z == zCache)) + { + cachedValue = sourceModules[0].getValue (x, y, z); + xCache = x; + yCache = y; + zCache = z; + } + + isCached = true; + + return cachedValue; + } + + + +} diff --git a/src/libnoiseforjava/module/Checkerboard.java b/src/libnoiseforjava/module/Checkerboard.java index 07ce003..d9f4e1f 100644 --- a/src/libnoiseforjava/module/Checkerboard.java +++ b/src/libnoiseforjava/module/Checkerboard.java @@ -1,62 +1,62 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.NoiseGen; - -public class Checkerboard extends ModuleBase -{ - /// Noise module that outputs a checkerboard pattern. - /// - /// This noise module outputs unit-sized blocks of alternating values. - /// The values of these blocks alternate between -1.0 and +1.0. - /// - /// This noise module is not really useful by itself, but it is often used - /// for debugging purposes. - /// - /// This noise module does not require any source modules. - - public Checkerboard() - { - super(0); - } - - public double getValue (double x, double y, double z) - { - int ix = (int)(Math.floor(NoiseGen.MakeInt32Range (x))); - int iy = (int)(Math.floor(NoiseGen.MakeInt32Range (y))); - int iz = (int)(Math.floor(NoiseGen.MakeInt32Range (z))); - - // original was - //(ix & 1 ^ iy & 1 ^ iz & 1) - // not certain if this duplicates it or not - if ((ix%2 == 1) ^ (iy%2 == 1) ^ (iz%2 == 1)) - return -1.0; - else - return 1.0; - } -} - +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.NoiseGen; + +public class Checkerboard extends ModuleBase +{ + /// Noise module that outputs a checkerboard pattern. + /// + /// This noise module outputs unit-sized blocks of alternating values. + /// The values of these blocks alternate between -1.0 and +1.0. + /// + /// This noise module is not really useful by itself, but it is often used + /// for debugging purposes. + /// + /// This noise module does not require any source modules. + + public Checkerboard() + { + super(0); + } + + public double getValue (double x, double y, double z) + { + int ix = (int)(Math.floor(NoiseGen.MakeInt32Range (x))); + int iy = (int)(Math.floor(NoiseGen.MakeInt32Range (y))); + int iz = (int)(Math.floor(NoiseGen.MakeInt32Range (z))); + + // original was + //(ix & 1 ^ iy & 1 ^ iz & 1) + // not certain if this duplicates it or not + if ((ix%2 == 1) ^ (iy%2 == 1) ^ (iz%2 == 1)) + return -1.0; + else + return 1.0; + } +} + diff --git a/src/libnoiseforjava/module/Clamp.java b/src/libnoiseforjava/module/Clamp.java index 7e98a09..3ad6d39 100644 --- a/src/libnoiseforjava/module/Clamp.java +++ b/src/libnoiseforjava/module/Clamp.java @@ -1,90 +1,90 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Clamp extends ModuleBase -{ - - /// Noise module that clamps the output value from a source module to a - /// range of values. - /// - /// @image html moduleclamp.png - /// - /// The range of values in which to clamp the output value is called the - /// clamping range. - /// - /// If the output value from the source module is less than the lower - /// bound of the clamping range, this noise module clamps that value to - /// the lower bound. If the output value from the source module is - /// greater than the upper bound of the clamping range, this noise module - /// clamps that value to the upper bound. - /// - /// To specify the upper and lower bounds of the clamping range, call the - /// setBounds() method. - /// - /// This noise module requires one source module. - - /// Default lower bound of the clamping range for the Clamp noise module. - static final double DEFAULT_CLAMP_LOWER_BOUND = -1.0; - - /// Default upper bound of the clamping range for the Clamp noise module. - static final double DEFAULT_CLAMP_UPPER_BOUND = 1.0; - - double lowerBound, upperBound; - - public Clamp (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - - lowerBound = DEFAULT_CLAMP_LOWER_BOUND; - upperBound = DEFAULT_CLAMP_UPPER_BOUND; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - double value = sourceModules[0].getValue (x, y, z); - if (value < lowerBound) - return lowerBound; - else if (value > upperBound) - return upperBound; - else - return value; - } - - public void setBounds (double lowerBound, double upperBound) - { - assert (lowerBound < upperBound); - - this.lowerBound = lowerBound; - this.upperBound = upperBound; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Clamp extends ModuleBase +{ + + /// Noise module that clamps the output value from a source module to a + /// range of values. + /// + /// @image html moduleclamp.png + /// + /// The range of values in which to clamp the output value is called the + /// clamping range. + /// + /// If the output value from the source module is less than the lower + /// bound of the clamping range, this noise module clamps that value to + /// the lower bound. If the output value from the source module is + /// greater than the upper bound of the clamping range, this noise module + /// clamps that value to the upper bound. + /// + /// To specify the upper and lower bounds of the clamping range, call the + /// setBounds() method. + /// + /// This noise module requires one source module. + + /// Default lower bound of the clamping range for the Clamp noise module. + static final double DEFAULT_CLAMP_LOWER_BOUND = -1.0; + + /// Default upper bound of the clamping range for the Clamp noise module. + static final double DEFAULT_CLAMP_UPPER_BOUND = 1.0; + + double lowerBound, upperBound; + + public Clamp (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + + lowerBound = DEFAULT_CLAMP_LOWER_BOUND; + upperBound = DEFAULT_CLAMP_UPPER_BOUND; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + double value = sourceModules[0].getValue (x, y, z); + if (value < lowerBound) + return lowerBound; + else if (value > upperBound) + return upperBound; + else + return value; + } + + public void setBounds (double lowerBound, double upperBound) + { + assert (lowerBound < upperBound); + + this.lowerBound = lowerBound; + this.upperBound = upperBound; + } + +} diff --git a/src/libnoiseforjava/module/Const.java b/src/libnoiseforjava/module/Const.java index f6ebe2b..f57ea0c 100644 --- a/src/libnoiseforjava/module/Const.java +++ b/src/libnoiseforjava/module/Const.java @@ -1,65 +1,65 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -public class Const extends ModuleBase -{ - /// Noise module that outputs a constant value. - /// - /// @image html moduleconst.png - /// - /// To specify the constant value, call the setConstValue() method. - /// - /// This noise module is not useful by itself, but it is often used as a - /// source module for other noise modules. - /// - /// This noise module does not require any source modules. - - /// Default constant value for the Const noise module. - static final double DEFAULT_CONST_VALUE = 0.0; - - double constValue; - - public Const () - { - super(0); - this.constValue = DEFAULT_CONST_VALUE; - } - - public double getValue (double x, double y, double z) - { - return constValue; - } - - /// Sets the constant output value for this noise module. - /// - /// @param constValue The constant output value for this noise module. - public void setConstValue (double constValue) - { - this.constValue = constValue; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +public class Const extends ModuleBase +{ + /// Noise module that outputs a constant value. + /// + /// @image html moduleconst.png + /// + /// To specify the constant value, call the setConstValue() method. + /// + /// This noise module is not useful by itself, but it is often used as a + /// source module for other noise modules. + /// + /// This noise module does not require any source modules. + + /// Default constant value for the Const noise module. + static final double DEFAULT_CONST_VALUE = 0.0; + + double constValue; + + public Const () + { + super(0); + this.constValue = DEFAULT_CONST_VALUE; + } + + public double getValue (double x, double y, double z) + { + return constValue; + } + + /// Sets the constant output value for this noise module. + /// + /// @param constValue The constant output value for this noise module. + public void setConstValue (double constValue) + { + this.constValue = constValue; + } + +} diff --git a/src/libnoiseforjava/module/Curve.java b/src/libnoiseforjava/module/Curve.java index 4bbd75c..2d78818 100644 --- a/src/libnoiseforjava/module/Curve.java +++ b/src/libnoiseforjava/module/Curve.java @@ -1,203 +1,203 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.Interp; -import libnoiseforjava.Misc; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Curve extends ModuleBase -{ - /// Noise module that maps the output value from a source module onto an - /// arbitrary function curve. - /// - /// This noise module maps the output value from the source module onto an - /// application-defined curve. This curve is defined by a number of - /// control points; each control point has an input value - /// that maps to an output value. - /// - /// To add the control points to this curve, call the addControlPoint() - /// method. Note that the class ControlPoint follows the class Curve in - /// this file. - /// - /// Since this curve is a cubic spline, an application must add a minimum - /// of four control points to the curve. If this is not done, the - /// getValue() method fails. Each control point can have any input and - /// output value, although no two control points can have the same input - /// value. There is no limit to the number of control points that can be - /// added to the curve. - /// - /// This noise module requires one source module - - int controlPointCount; - ControlPoint[] controlPoints; - - public Curve (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - controlPointCount = 0; - controlPoints= new ControlPoint[1]; - controlPoints[0] = new ControlPoint(0.0, 0.0); - } - - public void addControlPoint (double inputValue, double outputValue) - throws ExceptionInvalidParam - { - // Find the insertion point for the new control point and insert the new - // point at that position. The control point array will remain sorted by - // input value. - int insertionPos = findInsertionPos(inputValue); - insertAtPos (insertionPos, inputValue, outputValue); - } - - public void clearAllControlPoints () - { - controlPoints = null; - controlPointCount = 0; - } - - public int findInsertionPos (double inputValue) throws ExceptionInvalidParam - { - int insertionPos; - for (insertionPos = 0; insertionPos < controlPointCount; insertionPos++) - { - if (inputValue < controlPoints[insertionPos].inputValue) - // We found the array index in which to insert the new control point. - // Exit now. - break; - else if (inputValue == controlPoints[insertionPos].inputValue) - // Each control point is required to contain a unique input value, so - // throw an exception. - throw new ExceptionInvalidParam("Invalid Parameter in Curve"); - } - return insertionPos; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (controlPointCount >= 4); - - // Get the output value from the source module. - double sourceModuleValue = sourceModules[0].getValue (x, y, z); - - // Find the first element in the control point array that has an input value - // larger than the output value from the source module. - int indexPos; - for (indexPos = 0; indexPos < controlPointCount; indexPos++) - { - if (sourceModuleValue < controlPoints[indexPos].inputValue) - break; - - } - - // Find the four nearest control points so that we can perform cubic - // interpolation. - int index0 = Misc.ClampValue (indexPos - 2, 0, controlPointCount - 1); - int index1 = Misc.ClampValue (indexPos - 1, 0, controlPointCount - 1); - int index2 = Misc.ClampValue (indexPos , 0, controlPointCount - 1); - int index3 = Misc.ClampValue (indexPos + 1, 0, controlPointCount - 1); - - // If some control points are missing (which occurs if the value from the - // source module is greater than the largest input value or less than the - // smallest input value of the control point array), get the corresponding - // output value of the nearest control point and exit now. - if (index1 == index2) { - return controlPoints[index1].outputValue; - } - - // Compute the alpha value used for cubic interpolation. - double input0 = controlPoints[index1].inputValue; - double input1 = controlPoints[index2].inputValue; - double alpha = (sourceModuleValue - input0) / (input1 - input0); - - // Now perform the cubic interpolation given the alpha value. - return Interp.cubicInterp( - controlPoints[index0].outputValue, - controlPoints[index1].outputValue, - controlPoints[index2].outputValue, - controlPoints[index3].outputValue, - alpha); - } - - public void insertAtPos (int insertionPos, double inputValue, - double outputValue) - { - // Make room for the new control point at the specified position within the - // control point array. The position is determined by the input value of - // the control point; the control points must be sorted by input value - // within that array. - ControlPoint[] newControlPoints = new ControlPoint[controlPointCount + 1]; - - for (int t = 0; t < (controlPointCount + 1); t++) - newControlPoints[t] = new ControlPoint(); - - for (int i = 0; i < controlPointCount; i++) { - if (i < insertionPos) { - newControlPoints[i] = controlPoints[i]; - } else { - newControlPoints[i + 1] = controlPoints[i]; - } - } - - controlPoints = newControlPoints; - ++controlPointCount; - - // Now that we've made room for the new control point within the array, add - // the new control point. - controlPoints[insertionPos].inputValue = inputValue; - controlPoints[insertionPos].outputValue = outputValue; - } -} - - -/// This class defines a control point. -/// -/// Control points are used for defining splines. -class ControlPoint -{ - /// The input value. - double inputValue; - - /// The output value that is mapped from the input value. - double outputValue; - - ControlPoint() - { - inputValue = 0.0; - outputValue = 0.0; - } - - ControlPoint(double inputValue, double outputValue) - { - this.inputValue = inputValue; - this.outputValue = outputValue; - } - - - +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.Interp; +import libnoiseforjava.Misc; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Curve extends ModuleBase +{ + /// Noise module that maps the output value from a source module onto an + /// arbitrary function curve. + /// + /// This noise module maps the output value from the source module onto an + /// application-defined curve. This curve is defined by a number of + /// control points; each control point has an input value + /// that maps to an output value. + /// + /// To add the control points to this curve, call the addControlPoint() + /// method. Note that the class ControlPoint follows the class Curve in + /// this file. + /// + /// Since this curve is a cubic spline, an application must add a minimum + /// of four control points to the curve. If this is not done, the + /// getValue() method fails. Each control point can have any input and + /// output value, although no two control points can have the same input + /// value. There is no limit to the number of control points that can be + /// added to the curve. + /// + /// This noise module requires one source module + + int controlPointCount; + ControlPoint[] controlPoints; + + public Curve (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + controlPointCount = 0; + controlPoints= new ControlPoint[1]; + controlPoints[0] = new ControlPoint(0.0, 0.0); + } + + public void addControlPoint (double inputValue, double outputValue) + throws ExceptionInvalidParam + { + // Find the insertion point for the new control point and insert the new + // point at that position. The control point array will remain sorted by + // input value. + int insertionPos = findInsertionPos(inputValue); + insertAtPos (insertionPos, inputValue, outputValue); + } + + public void clearAllControlPoints () + { + controlPoints = null; + controlPointCount = 0; + } + + public int findInsertionPos (double inputValue) throws ExceptionInvalidParam + { + int insertionPos; + for (insertionPos = 0; insertionPos < controlPointCount; insertionPos++) + { + if (inputValue < controlPoints[insertionPos].inputValue) + // We found the array index in which to insert the new control point. + // Exit now. + break; + else if (inputValue == controlPoints[insertionPos].inputValue) + // Each control point is required to contain a unique input value, so + // throw an exception. + throw new ExceptionInvalidParam("Invalid Parameter in Curve"); + } + return insertionPos; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (controlPointCount >= 4); + + // Get the output value from the source module. + double sourceModuleValue = sourceModules[0].getValue (x, y, z); + + // Find the first element in the control point array that has an input value + // larger than the output value from the source module. + int indexPos; + for (indexPos = 0; indexPos < controlPointCount; indexPos++) + { + if (sourceModuleValue < controlPoints[indexPos].inputValue) + break; + + } + + // Find the four nearest control points so that we can perform cubic + // interpolation. + int index0 = Misc.ClampValue (indexPos - 2, 0, controlPointCount - 1); + int index1 = Misc.ClampValue (indexPos - 1, 0, controlPointCount - 1); + int index2 = Misc.ClampValue (indexPos , 0, controlPointCount - 1); + int index3 = Misc.ClampValue (indexPos + 1, 0, controlPointCount - 1); + + // If some control points are missing (which occurs if the value from the + // source module is greater than the largest input value or less than the + // smallest input value of the control point array), get the corresponding + // output value of the nearest control point and exit now. + if (index1 == index2) { + return controlPoints[index1].outputValue; + } + + // Compute the alpha value used for cubic interpolation. + double input0 = controlPoints[index1].inputValue; + double input1 = controlPoints[index2].inputValue; + double alpha = (sourceModuleValue - input0) / (input1 - input0); + + // Now perform the cubic interpolation given the alpha value. + return Interp.cubicInterp( + controlPoints[index0].outputValue, + controlPoints[index1].outputValue, + controlPoints[index2].outputValue, + controlPoints[index3].outputValue, + alpha); + } + + public void insertAtPos (int insertionPos, double inputValue, + double outputValue) + { + // Make room for the new control point at the specified position within the + // control point array. The position is determined by the input value of + // the control point; the control points must be sorted by input value + // within that array. + ControlPoint[] newControlPoints = new ControlPoint[controlPointCount + 1]; + + for (int t = 0; t < (controlPointCount + 1); t++) + newControlPoints[t] = new ControlPoint(); + + for (int i = 0; i < controlPointCount; i++) { + if (i < insertionPos) { + newControlPoints[i] = controlPoints[i]; + } else { + newControlPoints[i + 1] = controlPoints[i]; + } + } + + controlPoints = newControlPoints; + ++controlPointCount; + + // Now that we've made room for the new control point within the array, add + // the new control point. + controlPoints[insertionPos].inputValue = inputValue; + controlPoints[insertionPos].outputValue = outputValue; + } +} + + +/// This class defines a control point. +/// +/// Control points are used for defining splines. +class ControlPoint +{ + /// The input value. + double inputValue; + + /// The output value that is mapped from the input value. + double outputValue; + + ControlPoint() + { + inputValue = 0.0; + outputValue = 0.0; + } + + ControlPoint(double inputValue, double outputValue) + { + this.inputValue = inputValue; + this.outputValue = outputValue; + } + + + } \ No newline at end of file diff --git a/src/libnoiseforjava/module/Cylinders.java b/src/libnoiseforjava/module/Cylinders.java index 8fa010e..c67d019 100644 --- a/src/libnoiseforjava/module/Cylinders.java +++ b/src/libnoiseforjava/module/Cylinders.java @@ -1,103 +1,103 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -public class Cylinders extends ModuleBase -{ - /// Noise module that outputs concentric cylinders. - /// - /// This noise module outputs concentric cylinders centered on the origin. - /// These cylinders are oriented along the @a y axis similar to the - /// concentric rings of a tree. Each cylinder extends infinitely along - /// the @a y axis. - /// - /// The first cylinder has a radius of 1.0. Each subsequent cylinder has - /// a radius that is 1.0 unit larger than the previous cylinder. - /// - /// The output value from this noise module is determined by the distance - /// between the input value and the the nearest cylinder surface. The - /// input values that are located on a cylinder surface are given the - /// output value 1.0 and the input values that are equidistant from two - /// cylinder surfaces are given the output value -1.0. - /// - /// An application can change the frequency of the concentric cylinders. - /// Increasing the frequency reduces the distances between cylinders. To - /// specify the frequency, call the setFrequency() method. - /// - /// This noise module, modified with some low-frequency, low-power - /// turbulence, is useful for generating wood-like textures. - /// - /// This noise module does not require any source modules. - - /// Default frequency value for the Cylinders noise module. - static final double DEFAULT_CYLINDERS_FREQUENCY = 1.0; - - /// Frequency of the concentric cylinders. - double frequency; - - public Cylinders () - { - super(0); - frequency = DEFAULT_CYLINDERS_FREQUENCY; - } - - public double getValue (double x, double y, double z) - { - x *= frequency; - z *= frequency; - - double distFromCenter = Math.sqrt(x * x + z * z); - double distFromSmallerSphere = distFromCenter - Math.floor(distFromCenter); - double distFromLargerSphere = 1.0 - distFromSmallerSphere; - double nearestDist = Math.min(distFromSmallerSphere, distFromLargerSphere); - - // Puts it in the -1.0 to +1.0 range. - return 1.0 - (nearestDist * 4.0); - } - - /// Returns the frequency of the concentric cylinders. - /// - /// @returns The frequency of the concentric cylinders. - /// - /// Increasing the frequency increases the density of the concentric - /// cylinders, reducing the distances between them. - public double getFrequency() - { - return frequency; - } - - /// Sets the frequency of the concentric cylinders. - /// - /// @param frequency The frequency of the concentric cylinders. - /// - /// Increasing the frequency increases the density of the concentric - /// cylinders, reducing the distances between them. - public void setFrequency (double frequency) - { - this.frequency = frequency; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +public class Cylinders extends ModuleBase +{ + /// Noise module that outputs concentric cylinders. + /// + /// This noise module outputs concentric cylinders centered on the origin. + /// These cylinders are oriented along the @a y axis similar to the + /// concentric rings of a tree. Each cylinder extends infinitely along + /// the @a y axis. + /// + /// The first cylinder has a radius of 1.0. Each subsequent cylinder has + /// a radius that is 1.0 unit larger than the previous cylinder. + /// + /// The output value from this noise module is determined by the distance + /// between the input value and the the nearest cylinder surface. The + /// input values that are located on a cylinder surface are given the + /// output value 1.0 and the input values that are equidistant from two + /// cylinder surfaces are given the output value -1.0. + /// + /// An application can change the frequency of the concentric cylinders. + /// Increasing the frequency reduces the distances between cylinders. To + /// specify the frequency, call the setFrequency() method. + /// + /// This noise module, modified with some low-frequency, low-power + /// turbulence, is useful for generating wood-like textures. + /// + /// This noise module does not require any source modules. + + /// Default frequency value for the Cylinders noise module. + static final double DEFAULT_CYLINDERS_FREQUENCY = 1.0; + + /// Frequency of the concentric cylinders. + double frequency; + + public Cylinders () + { + super(0); + frequency = DEFAULT_CYLINDERS_FREQUENCY; + } + + public double getValue (double x, double y, double z) + { + x *= frequency; + z *= frequency; + + double distFromCenter = Math.sqrt(x * x + z * z); + double distFromSmallerSphere = distFromCenter - Math.floor(distFromCenter); + double distFromLargerSphere = 1.0 - distFromSmallerSphere; + double nearestDist = Math.min(distFromSmallerSphere, distFromLargerSphere); + + // Puts it in the -1.0 to +1.0 range. + return 1.0 - (nearestDist * 4.0); + } + + /// Returns the frequency of the concentric cylinders. + /// + /// @returns The frequency of the concentric cylinders. + /// + /// Increasing the frequency increases the density of the concentric + /// cylinders, reducing the distances between them. + public double getFrequency() + { + return frequency; + } + + /// Sets the frequency of the concentric cylinders. + /// + /// @param frequency The frequency of the concentric cylinders. + /// + /// Increasing the frequency increases the density of the concentric + /// cylinders, reducing the distances between them. + public void setFrequency (double frequency) + { + this.frequency = frequency; + } + +} diff --git a/src/libnoiseforjava/module/Displace.java b/src/libnoiseforjava/module/Displace.java index 732f20f..7725ce6 100644 --- a/src/libnoiseforjava/module/Displace.java +++ b/src/libnoiseforjava/module/Displace.java @@ -1,238 +1,238 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.exception.ExceptionNoModule; - -public class Displace extends ModuleBase -{ - /// Noise module that uses three source modules to displace each - /// coordinate of the input value before returning the output value from - /// a source module. - /// - /// Unlike most other noise modules, the index value assigned to a source - /// module determines its role in the displacement operation: - /// - Source module 0 (left in the diagram) outputs a value. - /// - Source module 1 (lower left in the diagram) specifies the offset to - /// apply to the @a x coordinate of the input value. - /// - Source module 2 (lower center in the diagram) specifies the - /// offset to apply to the @a y coordinate of the input value. - /// - Source module 3 (lower right in the diagram) specifies the offset - /// to apply to the @a z coordinate of the input value. - /// - /// The getValue() method modifies the ( @a x, @a y, @a z ) coordinates of - /// the input value using the output values from the three displacement - /// modules before retrieving the output value from the source module. - /// - /// The Turbulence noise module is a special case of the - /// Displace module; internally, there are three Perlin-noise modules - /// that perform the displacement operation. - /// - /// This noise module requires four source modules. - - public Displace (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, - ModuleBase sourceModuleThree, ModuleBase sourceModuleFour) throws ExceptionInvalidParam - { - super(4); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - setSourceModule(2, sourceModuleThree); - setSourceModule(3, sourceModuleFour); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - assert (sourceModules[2] != null); - assert (sourceModules[3] != null); - - // Get the output values from the three displacement modules. Add each - // value to the corresponding coordinate in the input value. - double xDisplace = x + (sourceModules[1].getValue (x, y, z)); - double yDisplace = y + (sourceModules[2].getValue (x, y, z)); - double zDisplace = z + (sourceModules[3].getValue (x, y, z)); - - // Retrieve the output value using the offset input value instead of - // the original input value. - return sourceModules[0].getValue (xDisplace, yDisplace, zDisplace); - } - - public ModuleBase getXDisplaceModule() throws ExceptionNoModule - { - if (sourceModules == null || sourceModules[1] == null) - throw new ExceptionNoModule ("Could not retrieve a source module " + - "from a noise module."); - - return sourceModules[1]; - } - - /// Returns the @a y displacement module. - /// - /// @returns A reference to the @a y displacement module. - /// - /// @pre This displacement module has been added to this noise module - /// via a call to setSourceModule() or setYDisplaceModule(). - /// - /// @throw ExceptionNoModule See the preconditions for more - /// information. - /// - /// The getValue() method displaces the input value by adding the output - /// value from this displacement module to the @a y coordinate of the - /// input value before returning the output value from the source - /// module. - public ModuleBase getYDisplaceModule () throws ExceptionNoModule - { - if (sourceModules == null || sourceModules[2] == null) - throw new ExceptionNoModule ("Could not retrieve a source module " + - "from Displace noise module."); - - return sourceModules[2]; - } - - /// Returns the @a z displacement module. - /// - /// @returns A reference to the @a z displacement module. - /// - /// @pre This displacement module has been added to this noise module - /// via a call to setSourceModule() or setZDisplaceModule(). - /// - /// @throw ExceptionNoModule See the preconditions for more - /// information. - /// - /// The getValue() method displaces the input value by adding the output - /// value from this displacement module to the @a z coordinate of the - /// input value before returning the output value from the source - /// module. - public ModuleBase getZDisplaceModule () throws ExceptionNoModule - { - if (sourceModules == null || sourceModules[3] == null) - throw new ExceptionNoModule ("Could not retrieve a source module " + - "from Displace noise module."); - - return sourceModules[3]; - } - - - /// Sets the @a x, @a y, and @a z displacement modules. - /// - /// @param xDisplaceModule Displacement module that displaces the @a x - /// coordinate of the input value. - /// @param yDisplaceModule Displacement module that displaces the @a y - /// coordinate of the input value. - /// @param zDisplaceModule Displacement module that displaces the @a z - /// coordinate of the input value. - /// - /// The getValue() method displaces the input value by adding the output - /// value from each of the displacement modules to the corresponding - /// coordinates of the input value before returning the output value - /// from the source module. - /// - /// This method assigns an index value of 1 to the @a x displacement - /// module, an index value of 2 to the @a y displacement module, and an - /// index value of 3 to the @a z displacement module. - /// - /// These displacement modules must exist throughout the lifetime of - /// this noise module unless another displacement module replaces it. - public void setDisplaceModules (ModuleBase xDisplaceModule, - ModuleBase yDisplaceModule, ModuleBase zDisplaceModule) - { - setXDisplaceModule (xDisplaceModule); - setYDisplaceModule (yDisplaceModule); - setZDisplaceModule (zDisplaceModule); - } - - /// Sets the @a x displacement module. - /// - /// @param xDisplaceModule Displacement module that displaces the @a x - /// coordinate. - /// - /// The getValue() method displaces the input value by adding the output - /// value from this displacement module to the @a x coordinate of the - /// input value before returning the output value from the source - /// module. - /// - /// This method assigns an index value of 1 to the @a x displacement - /// module. Passing this displacement module to this method produces - /// the same results as passing this displacement module to the - /// setSourceModule() method while assigning it an index value of 1. - /// - /// This displacement module must exist throughout the lifetime of this - /// noise module unless another displacement module replaces it. - public void setXDisplaceModule (ModuleBase xDisplaceModule) - { - assert (sourceModules != null); - sourceModules[1] = xDisplaceModule; - } - - /// Sets the @a y displacement module. - /// - /// @param yDisplaceModule Displacement module that displaces the @a y - /// coordinate. - /// - /// The getValue() method displaces the input value by adding the output - /// value from this displacement module to the @a y coordinate of the - /// input value before returning the output value from the source - /// module. - /// - /// This method assigns an index value of 2 to the @a y displacement - /// module. Passing this displacement module to this method produces - /// the same results as passing this displacement module to the - /// setSourceModule() method while assigning it an index value of 2. - /// - /// This displacement module must exist throughout the lifetime of this - /// noise module unless another displacement module replaces it. - public void setYDisplaceModule (ModuleBase yDisplaceModule) - { - assert (sourceModules != null); - sourceModules[2] = yDisplaceModule; - } - - /// Sets the @a z displacement module. - /// - /// @param zDisplaceModule Displacement module that displaces the @a z - /// coordinate. - /// - /// The getValue() method displaces the input value by adding the output - /// value from this displacement module to the @a z coordinate of the - /// input value before returning the output value from the source - /// module. - /// - /// This method assigns an index value of 3 to the @a z displacement - /// module. Passing this displacement module to this method produces - /// the same results as passing this displacement module to the - /// setSourceModule() method while assigning it an index value of 3. - /// - /// This displacement module must exist throughout the lifetime of this - /// noise module unless another displacement module replaces it. - public void setZDisplaceModule (ModuleBase zDisplaceModule) - { - assert (sourceModules != null); - sourceModules[3] = zDisplaceModule; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.exception.ExceptionNoModule; + +public class Displace extends ModuleBase +{ + /// Noise module that uses three source modules to displace each + /// coordinate of the input value before returning the output value from + /// a source module. + /// + /// Unlike most other noise modules, the index value assigned to a source + /// module determines its role in the displacement operation: + /// - Source module 0 (left in the diagram) outputs a value. + /// - Source module 1 (lower left in the diagram) specifies the offset to + /// apply to the @a x coordinate of the input value. + /// - Source module 2 (lower center in the diagram) specifies the + /// offset to apply to the @a y coordinate of the input value. + /// - Source module 3 (lower right in the diagram) specifies the offset + /// to apply to the @a z coordinate of the input value. + /// + /// The getValue() method modifies the ( @a x, @a y, @a z ) coordinates of + /// the input value using the output values from the three displacement + /// modules before retrieving the output value from the source module. + /// + /// The Turbulence noise module is a special case of the + /// Displace module; internally, there are three Perlin-noise modules + /// that perform the displacement operation. + /// + /// This noise module requires four source modules. + + public Displace (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, + ModuleBase sourceModuleThree, ModuleBase sourceModuleFour) throws ExceptionInvalidParam + { + super(4); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + setSourceModule(2, sourceModuleThree); + setSourceModule(3, sourceModuleFour); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + assert (sourceModules[2] != null); + assert (sourceModules[3] != null); + + // Get the output values from the three displacement modules. Add each + // value to the corresponding coordinate in the input value. + double xDisplace = x + (sourceModules[1].getValue (x, y, z)); + double yDisplace = y + (sourceModules[2].getValue (x, y, z)); + double zDisplace = z + (sourceModules[3].getValue (x, y, z)); + + // Retrieve the output value using the offset input value instead of + // the original input value. + return sourceModules[0].getValue (xDisplace, yDisplace, zDisplace); + } + + public ModuleBase getXDisplaceModule() throws ExceptionNoModule + { + if (sourceModules == null || sourceModules[1] == null) + throw new ExceptionNoModule ("Could not retrieve a source module " + + "from a noise module."); + + return sourceModules[1]; + } + + /// Returns the @a y displacement module. + /// + /// @returns A reference to the @a y displacement module. + /// + /// @pre This displacement module has been added to this noise module + /// via a call to setSourceModule() or setYDisplaceModule(). + /// + /// @throw ExceptionNoModule See the preconditions for more + /// information. + /// + /// The getValue() method displaces the input value by adding the output + /// value from this displacement module to the @a y coordinate of the + /// input value before returning the output value from the source + /// module. + public ModuleBase getYDisplaceModule () throws ExceptionNoModule + { + if (sourceModules == null || sourceModules[2] == null) + throw new ExceptionNoModule ("Could not retrieve a source module " + + "from Displace noise module."); + + return sourceModules[2]; + } + + /// Returns the @a z displacement module. + /// + /// @returns A reference to the @a z displacement module. + /// + /// @pre This displacement module has been added to this noise module + /// via a call to setSourceModule() or setZDisplaceModule(). + /// + /// @throw ExceptionNoModule See the preconditions for more + /// information. + /// + /// The getValue() method displaces the input value by adding the output + /// value from this displacement module to the @a z coordinate of the + /// input value before returning the output value from the source + /// module. + public ModuleBase getZDisplaceModule () throws ExceptionNoModule + { + if (sourceModules == null || sourceModules[3] == null) + throw new ExceptionNoModule ("Could not retrieve a source module " + + "from Displace noise module."); + + return sourceModules[3]; + } + + + /// Sets the @a x, @a y, and @a z displacement modules. + /// + /// @param xDisplaceModule Displacement module that displaces the @a x + /// coordinate of the input value. + /// @param yDisplaceModule Displacement module that displaces the @a y + /// coordinate of the input value. + /// @param zDisplaceModule Displacement module that displaces the @a z + /// coordinate of the input value. + /// + /// The getValue() method displaces the input value by adding the output + /// value from each of the displacement modules to the corresponding + /// coordinates of the input value before returning the output value + /// from the source module. + /// + /// This method assigns an index value of 1 to the @a x displacement + /// module, an index value of 2 to the @a y displacement module, and an + /// index value of 3 to the @a z displacement module. + /// + /// These displacement modules must exist throughout the lifetime of + /// this noise module unless another displacement module replaces it. + public void setDisplaceModules (ModuleBase xDisplaceModule, + ModuleBase yDisplaceModule, ModuleBase zDisplaceModule) + { + setXDisplaceModule (xDisplaceModule); + setYDisplaceModule (yDisplaceModule); + setZDisplaceModule (zDisplaceModule); + } + + /// Sets the @a x displacement module. + /// + /// @param xDisplaceModule Displacement module that displaces the @a x + /// coordinate. + /// + /// The getValue() method displaces the input value by adding the output + /// value from this displacement module to the @a x coordinate of the + /// input value before returning the output value from the source + /// module. + /// + /// This method assigns an index value of 1 to the @a x displacement + /// module. Passing this displacement module to this method produces + /// the same results as passing this displacement module to the + /// setSourceModule() method while assigning it an index value of 1. + /// + /// This displacement module must exist throughout the lifetime of this + /// noise module unless another displacement module replaces it. + public void setXDisplaceModule (ModuleBase xDisplaceModule) + { + assert (sourceModules != null); + sourceModules[1] = xDisplaceModule; + } + + /// Sets the @a y displacement module. + /// + /// @param yDisplaceModule Displacement module that displaces the @a y + /// coordinate. + /// + /// The getValue() method displaces the input value by adding the output + /// value from this displacement module to the @a y coordinate of the + /// input value before returning the output value from the source + /// module. + /// + /// This method assigns an index value of 2 to the @a y displacement + /// module. Passing this displacement module to this method produces + /// the same results as passing this displacement module to the + /// setSourceModule() method while assigning it an index value of 2. + /// + /// This displacement module must exist throughout the lifetime of this + /// noise module unless another displacement module replaces it. + public void setYDisplaceModule (ModuleBase yDisplaceModule) + { + assert (sourceModules != null); + sourceModules[2] = yDisplaceModule; + } + + /// Sets the @a z displacement module. + /// + /// @param zDisplaceModule Displacement module that displaces the @a z + /// coordinate. + /// + /// The getValue() method displaces the input value by adding the output + /// value from this displacement module to the @a z coordinate of the + /// input value before returning the output value from the source + /// module. + /// + /// This method assigns an index value of 3 to the @a z displacement + /// module. Passing this displacement module to this method produces + /// the same results as passing this displacement module to the + /// setSourceModule() method while assigning it an index value of 3. + /// + /// This displacement module must exist throughout the lifetime of this + /// noise module unless another displacement module replaces it. + public void setZDisplaceModule (ModuleBase zDisplaceModule) + { + assert (sourceModules != null); + sourceModules[3] = zDisplaceModule; + } + +} diff --git a/src/libnoiseforjava/module/Exponent.java b/src/libnoiseforjava/module/Exponent.java index 95cd234..64f9cdc 100644 --- a/src/libnoiseforjava/module/Exponent.java +++ b/src/libnoiseforjava/module/Exponent.java @@ -1,79 +1,79 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Exponent extends ModuleBase -{ - /// Noise module that maps the output value from a source module onto an - /// exponential curve. - /// - /// Because most noise modules will output values that range from -1.0 to - /// +1.0, this noise module first normalizes this output value (the range - /// becomes 0.0 to 1.0), maps that value onto an exponential curve, then - /// rescales that value back to the original range. - /// - /// This noise module requires one source module. - - /// Default exponent for the Exponent noise module. - static final double DEFAULT_EXPONENT = 1.0; - - - /// Exponent to apply to the output value from the source module. - double exponent; - - public Exponent (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - exponent = DEFAULT_EXPONENT; - - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - double value = sourceModules[0].getValue (x, y, z); - return (Math.pow (Math.abs ((value + 1.0) / 2.0), exponent) * 2.0 - 1.0); - } - - /// Returns the exponent value to apply to the output value from the - /// source module. - /// - /// @returns The exponent value. - public double getExponent () - { - return exponent; - } - - public void setExponent(double exponent) - { - this.exponent = exponent; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Exponent extends ModuleBase +{ + /// Noise module that maps the output value from a source module onto an + /// exponential curve. + /// + /// Because most noise modules will output values that range from -1.0 to + /// +1.0, this noise module first normalizes this output value (the range + /// becomes 0.0 to 1.0), maps that value onto an exponential curve, then + /// rescales that value back to the original range. + /// + /// This noise module requires one source module. + + /// Default exponent for the Exponent noise module. + static final double DEFAULT_EXPONENT = 1.0; + + + /// Exponent to apply to the output value from the source module. + double exponent; + + public Exponent (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + exponent = DEFAULT_EXPONENT; + + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + double value = sourceModules[0].getValue (x, y, z); + return (Math.pow (Math.abs ((value + 1.0) / 2.0), exponent) * 2.0 - 1.0); + } + + /// Returns the exponent value to apply to the output value from the + /// source module. + /// + /// @returns The exponent value. + public double getExponent () + { + return exponent; + } + + public void setExponent(double exponent) + { + this.exponent = exponent; + } + +} diff --git a/src/libnoiseforjava/module/Invert.java b/src/libnoiseforjava/module/Invert.java index bacd2fc..106b79f 100644 --- a/src/libnoiseforjava/module/Invert.java +++ b/src/libnoiseforjava/module/Invert.java @@ -1,48 +1,48 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Invert extends ModuleBase -{ - /// Noise module that inverts the output value from a source module. - /// - /// This noise module requires one source module. - public Invert (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - return -(sourceModules[0].getValue (x, y, z)); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Invert extends ModuleBase +{ + /// Noise module that inverts the output value from a source module. + /// + /// This noise module requires one source module. + public Invert (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + return -(sourceModules[0].getValue (x, y, z)); + } + +} diff --git a/src/libnoiseforjava/module/Max.java b/src/libnoiseforjava/module/Max.java index e03bfa2..34757f6 100644 --- a/src/libnoiseforjava/module/Max.java +++ b/src/libnoiseforjava/module/Max.java @@ -1,54 +1,54 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Max extends ModuleBase -{ - /// Noise module that outputs the larger of the two output values from two - /// source modules. - /// - /// This noise module requires two source modules. - - public Max (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam - { - super(2); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - - double v0 = sourceModules[0].getValue (x, y, z); - double v1 = sourceModules[1].getValue (x, y, z); - return Math.max(v0, v1); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Max extends ModuleBase +{ + /// Noise module that outputs the larger of the two output values from two + /// source modules. + /// + /// This noise module requires two source modules. + + public Max (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam + { + super(2); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + + double v0 = sourceModules[0].getValue (x, y, z); + double v1 = sourceModules[1].getValue (x, y, z); + return Math.max(v0, v1); + } + +} diff --git a/src/libnoiseforjava/module/Min.java b/src/libnoiseforjava/module/Min.java index 5594c67..cd6bea0 100644 --- a/src/libnoiseforjava/module/Min.java +++ b/src/libnoiseforjava/module/Min.java @@ -1,56 +1,56 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Min extends ModuleBase -{ - /// Noise module that outputs the smaller of the two output values from - /// two source modules. - /// - /// @image html modulemin.png - /// - /// This noise module requires two source modules. - - public Min (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam - { - super(2); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - - double v0 = sourceModules[0].getValue (x, y, z); - double v1 = sourceModules[1].getValue (x, y, z); - return Math.min(v0, v1); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Min extends ModuleBase +{ + /// Noise module that outputs the smaller of the two output values from + /// two source modules. + /// + /// @image html modulemin.png + /// + /// This noise module requires two source modules. + + public Min (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam + { + super(2); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + + double v0 = sourceModules[0].getValue (x, y, z); + double v1 = sourceModules[1].getValue (x, y, z); + return Math.min(v0, v1); + } + +} diff --git a/src/libnoiseforjava/module/ModuleBase.java b/src/libnoiseforjava/module/ModuleBase.java index ece119e..895e67b 100644 --- a/src/libnoiseforjava/module/ModuleBase.java +++ b/src/libnoiseforjava/module/ModuleBase.java @@ -1,171 +1,171 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.exception.ExceptionNoModule; - -public class ModuleBase -{ - - // base class for noise modules. - public ModuleBase[] sourceModules; - public int modulesRequired; - - public ModuleBase() - { - modulesRequired = 0; - } - - public ModuleBase (int modulesRequired) - { - // Create an array of pointers to all source modules required by this - // noise module. Set these pointers to null. - if(modulesRequired>0) - { - sourceModules = new ModuleBase[modulesRequired]; - for (int i = 0; i < modulesRequired; i++) - { - sourceModules[i] = new ModuleBase(); - } - } - else - sourceModules = null; - - this.modulesRequired = modulesRequired; - } - - /// Returns a reference to a source module connected to this noise - /// module. - /// - /// @param index The index value assigned to the source module. - /// - /// @returns A reference to the source module. - /// - /// @pre The index value ranges from 0 to one less than the number of - /// source modules required by this noise module. - /// @pre A source module with the specified index value has been added - /// to this noise module via a call to setSourceModule(). - /// - /// @throw ExceptionNoModule See the preconditions for more - /// information. - /// - /// Each noise module requires the attachment of a certain number of - /// source modules before an application can call the getValue() - /// method. - public ModuleBase getSourceModule (int index) throws ExceptionNoModule - { - if (sourceModules != null) - { - if (index >= getSourceModuleCount () || index < 0 - || sourceModules[index] == null) - { - throw new ExceptionNoModule ("Could not retrieve a source module " + - "from a noise module."); - } - - return (sourceModules[index]); - } - throw new ExceptionNoModule ("Could not retrieve a source module " + - "from a noise module."); - } - - /// Returns the number of source modules required by this noise - /// module. - /// - /// @returns The number of source modules required by this noise - /// module. - public int getSourceModuleCount() - { - return modulesRequired; - } - - /// Generates an output value given the coordinates of the specified - /// input value. - /// - /// @param x The @a x coordinate of the input value. - /// @param y The @a y coordinate of the input value. - /// @param z The @a z coordinate of the input value. - /// - /// @returns The output value. - /// - /// @pre All source modules required by this noise module have been - /// passed to the setSourceModule() method. - /// - /// Before an application can call this method, it must first connect - /// all required source modules via the setSourceModule() method. If - /// these source modules are not connected to this noise module, this - /// method raises a debug assertion. - /// - /// To determine the number of source modules required by this noise - /// module, call the getSourceModuleCount() method. - public double getValue (double x, double y, double z) - { - return x; - } - - /// Connects a source module to this noise module. - /// - /// @param index An index value to assign to this source module. - /// @param sourceModule The source module to attach. - /// - /// @pre The index value ranges from 0 to one less than the number of - /// source modules required by this noise module. - /// - /// @throw ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// A noise module mathematically combines the output values from the - /// source modules to generate the value returned by getValue(). - /// - /// The index value to assign a source module is a unique identifier - /// for that source module. If an index value has already been - /// assigned to a source module, this noise module replaces the old - /// source module with the new source module. - /// - /// Before an application can call the getValue() method, it must - /// first connect all required source modules. To determine the - /// number of source modules required by this noise module, call the - /// getSourceModuleCount() method. - /// - /// This source module must exist throughout the lifetime of this - /// noise module unless another source module replaces that source - /// module. - /// - /// A noise module does not modify a source module; it only modifies - /// its output values. - public void setSourceModule (int index, ModuleBase sourceModule) - throws ExceptionInvalidParam - { - if (sourceModules != null) - { - if (index >= getSourceModuleCount () || index < 0) - throw new ExceptionInvalidParam ("Invalid Parameter in ModuleBase"); - } - this.sourceModules[index] = sourceModule; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.exception.ExceptionNoModule; + +public class ModuleBase +{ + + // base class for noise modules. + public ModuleBase[] sourceModules; + public int modulesRequired; + + public ModuleBase() + { + modulesRequired = 0; + } + + public ModuleBase (int modulesRequired) + { + // Create an array of pointers to all source modules required by this + // noise module. Set these pointers to null. + if(modulesRequired>0) + { + sourceModules = new ModuleBase[modulesRequired]; + for (int i = 0; i < modulesRequired; i++) + { + sourceModules[i] = new ModuleBase(); + } + } + else + sourceModules = null; + + this.modulesRequired = modulesRequired; + } + + /// Returns a reference to a source module connected to this noise + /// module. + /// + /// @param index The index value assigned to the source module. + /// + /// @returns A reference to the source module. + /// + /// @pre The index value ranges from 0 to one less than the number of + /// source modules required by this noise module. + /// @pre A source module with the specified index value has been added + /// to this noise module via a call to setSourceModule(). + /// + /// @throw ExceptionNoModule See the preconditions for more + /// information. + /// + /// Each noise module requires the attachment of a certain number of + /// source modules before an application can call the getValue() + /// method. + public ModuleBase getSourceModule (int index) throws ExceptionNoModule + { + if (sourceModules != null) + { + if (index >= getSourceModuleCount () || index < 0 + || sourceModules[index] == null) + { + throw new ExceptionNoModule ("Could not retrieve a source module " + + "from a noise module."); + } + + return (sourceModules[index]); + } + throw new ExceptionNoModule ("Could not retrieve a source module " + + "from a noise module."); + } + + /// Returns the number of source modules required by this noise + /// module. + /// + /// @returns The number of source modules required by this noise + /// module. + public int getSourceModuleCount() + { + return modulesRequired; + } + + /// Generates an output value given the coordinates of the specified + /// input value. + /// + /// @param x The @a x coordinate of the input value. + /// @param y The @a y coordinate of the input value. + /// @param z The @a z coordinate of the input value. + /// + /// @returns The output value. + /// + /// @pre All source modules required by this noise module have been + /// passed to the setSourceModule() method. + /// + /// Before an application can call this method, it must first connect + /// all required source modules via the setSourceModule() method. If + /// these source modules are not connected to this noise module, this + /// method raises a debug assertion. + /// + /// To determine the number of source modules required by this noise + /// module, call the getSourceModuleCount() method. + public double getValue (double x, double y, double z) + { + return x; + } + + /// Connects a source module to this noise module. + /// + /// @param index An index value to assign to this source module. + /// @param sourceModule The source module to attach. + /// + /// @pre The index value ranges from 0 to one less than the number of + /// source modules required by this noise module. + /// + /// @throw ExceptionInvalidParam An invalid parameter was + /// specified; see the preconditions for more information. + /// + /// A noise module mathematically combines the output values from the + /// source modules to generate the value returned by getValue(). + /// + /// The index value to assign a source module is a unique identifier + /// for that source module. If an index value has already been + /// assigned to a source module, this noise module replaces the old + /// source module with the new source module. + /// + /// Before an application can call the getValue() method, it must + /// first connect all required source modules. To determine the + /// number of source modules required by this noise module, call the + /// getSourceModuleCount() method. + /// + /// This source module must exist throughout the lifetime of this + /// noise module unless another source module replaces that source + /// module. + /// + /// A noise module does not modify a source module; it only modifies + /// its output values. + public void setSourceModule (int index, ModuleBase sourceModule) + throws ExceptionInvalidParam + { + if (sourceModules != null) + { + if (index >= getSourceModuleCount () || index < 0) + throw new ExceptionInvalidParam ("Invalid Parameter in ModuleBase"); + } + this.sourceModules[index] = sourceModule; + } + +} diff --git a/src/libnoiseforjava/module/Multiply.java b/src/libnoiseforjava/module/Multiply.java index 1e29899..944994e 100644 --- a/src/libnoiseforjava/module/Multiply.java +++ b/src/libnoiseforjava/module/Multiply.java @@ -1,55 +1,55 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Multiply extends ModuleBase -{ - /// Noise module that outputs the product of the two output values from - /// two source modules. - /// - /// @image html modulemultiply.png - /// - /// This noise module requires two source modules. - - public Multiply (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam - { - super(2); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - - return sourceModules[0].getValue (x, y, z) - * sourceModules[1].getValue (x, y, z); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Multiply extends ModuleBase +{ + /// Noise module that outputs the product of the two output values from + /// two source modules. + /// + /// @image html modulemultiply.png + /// + /// This noise module requires two source modules. + + public Multiply (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam + { + super(2); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + + return sourceModules[0].getValue (x, y, z) + * sourceModules[1].getValue (x, y, z); + } + +} diff --git a/src/libnoiseforjava/module/Perlin.java b/src/libnoiseforjava/module/Perlin.java index 6174d1b..70fb22e 100644 --- a/src/libnoiseforjava/module/Perlin.java +++ b/src/libnoiseforjava/module/Perlin.java @@ -1,357 +1,357 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.NoiseGen; -import libnoiseforjava.NoiseGen.NoiseQuality; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Perlin extends ModuleBase -{ - /// Noise module that outputs 3-dimensional Perlin noise. - /// - /// Perlin noise is the sum of several coherent-noise functions of - /// ever-increasing frequencies and ever-decreasing amplitudes. - /// - /// An important property of Perlin noise is that a small change in the - /// input value will produce a small change in the output value, while a - /// large change in the input value will produce a random change in the - /// output value. - /// - /// This noise module outputs Perlin-noise values that usually range from - /// -1.0 to +1.0, but there are no guarantees that all output values will - /// exist within that range. - /// - /// For a better description of Perlin noise, see the links in the - /// References and Acknowledgments section. - /// - /// This noise module does not require any source modules. - /// - /// Octaves - /// - /// The number of octaves control the amount of detail of the - /// Perlin noise. Adding more octaves increases the detail of the Perlin - /// noise, but with the drawback of increasing the calculation time. - /// - /// An octave is one of the coherent-noise functions in a series of - /// coherent-noise functions that are added together to form Perlin - /// noise. - /// - /// An application may specify the frequency of the first octave by - /// calling the setFrequency() method. - /// - /// An application may specify the number of octaves that generate Perlin - /// noise by calling the setOctaveCount() method. - /// - /// These coherent-noise functions are called octaves because each octave - /// has, by default, double the frequency of the previous octave. Musical - /// tones have this property as well; a musical C tone that is one octave - /// higher than the previous C tone has double its frequency. - /// - /// Frequency - /// - /// An application may specify the frequency of the first octave by - /// calling the setFrequency() method. - /// - /// Persistence - /// - /// The persistence value controls the roughness of the Perlin - /// noise. Larger values produce rougher noise. - /// - /// The persistence value determines how quickly the amplitudes diminish - /// for successive octaves. The amplitude of the first octave is 1.0. - /// The amplitude of each subsequent octave is equal to the product of the - /// previous octave's amplitude and the persistence value. So a - /// persistence value of 0.5 sets the amplitude of the first octave to - /// 1.0; the second, 0.5; the third, 0.25; etc. - /// - /// An application may specify the persistence value by calling the - /// setPersistence() method. - /// - /// Lacunarity - /// - /// The lacunarity specifies the frequency multipler between successive - /// octaves. - /// - /// The effect of modifying the lacunarity is subtle; you may need to play - /// with the lacunarity value to determine the effects. For best results, - /// set the lacunarity to a number between 1.5 and 3.5. - /// - /// References & acknowledgments - /// - /// The Noise Machine - - /// From the master, Ken Perlin himself. This page contains a - /// presentation that describes Perlin noise and some of its variants. - /// He won an Oscar for creating the Perlin noise algorithm! - /// - /// - /// Perlin Noise - Hugo Elias's webpage contains a very good - /// description of Perlin noise and describes its many applications. This - /// page gave me the inspiration to create libnoise in the first place. - /// Now that I know how to generate Perlin noise, I will never again use - /// cheesy subdivision algorithms to create terrain (unless I absolutely - /// need the speed.) - /// - /// The - /// Perlin noise math FAQ - A good page that describes Perlin noise in - /// plain English with only a minor amount of math. During development of - /// libnoise, I noticed that my coherent-noise function generated terrain - /// with some "regularity" to the terrain features. This page describes a - /// better coherent-noise function called gradient noise. This - /// version of the Perlin module uses gradient coherent noise to - /// generate Perlin noise. - - - /// Default frequency for the noise::module::Perlin noise module. - static final double DEFAULT_PERLIN_FREQUENCY = 1.0; - - /// Default lacunarity for the noise::module::Perlin noise module. - static final double DEFAULT_PERLIN_LACUNARITY = 2.0; - - /// Default number of octaves for the noise::module::Perlin noise module. - static final int DEFAULT_PERLIN_OCTAVE_COUNT = 6; - - /// Default persistence value for the noise::module::Perlin noise module. - static final double DEFAULT_PERLIN_PERSISTENCE = 0.5; - - /// Default noise quality for the noise::module::Perlin noise module. - static final NoiseQuality DEFAULT_PERLIN_QUALITY = NoiseQuality.QUALITY_STD; - - /// Default noise seed for the noise::module::Perlin noise module. - static final int DEFAULT_PERLIN_SEED = 0; - - /// Maximum number of octaves for the noise::module::Perlin noise module. - static final int PERLIN_MAX_OCTAVE = 30; - - - /// Frequency of the first octave. - double frequency; - - /// Frequency multiplier between successive octaves. - double lacunarity; - - /// Quality of the Perlin noise. - NoiseQuality noiseQuality; - - /// Total number of octaves that generate the Perlin noise. - int octaveCount; - - /// Persistence of the Perlin noise. - double persistence; - - /// Seed value used by the Perlin-noise function. - int seed; - - - public Perlin () - { - super(0); - frequency = DEFAULT_PERLIN_FREQUENCY; - lacunarity = DEFAULT_PERLIN_LACUNARITY; - noiseQuality = DEFAULT_PERLIN_QUALITY; - octaveCount = DEFAULT_PERLIN_OCTAVE_COUNT; - persistence = DEFAULT_PERLIN_PERSISTENCE; - seed = DEFAULT_PERLIN_SEED; - } - - public double getValue (double x, double y, double z) - { - double value = 0.0; - double signal = 0.0; - double curPersistence = 1.0; - double nx, ny, nz; - int curSeed; - - x *= frequency; - y *= frequency; - z *= frequency; - - for (int curOctave = 0; curOctave < octaveCount; curOctave++) - { - - // Make sure that these floating-point values have the same range as a 32- - // bit integer so that we can pass them to the coherent-noise functions. - nx = NoiseGen.MakeInt32Range (x); - ny = NoiseGen.MakeInt32Range (y); - nz = NoiseGen.MakeInt32Range (z); - - // Get the coherent-noise value from the input value and add it to the - // final result. - curSeed = (seed + curOctave) & 0xffffffff; - signal = NoiseGen.GradientCoherentNoise3D (nx, ny, nz, curSeed, noiseQuality); - value += signal * curPersistence; - - // Prepare the next octave. - x *= lacunarity; - y *= lacunarity; - z *= lacunarity; - curPersistence *= persistence; - } - - return value; - } - - /// Returns the frequency of the first octave. - /// - /// @returns The frequency of the first octave. - public double getFrequency () - { - return frequency; - } - - /// Returns the lacunarity of the Perlin noise. - /// - /// @returns The lacunarity of the Perlin noise. - /// - /// The lacunarity is the frequency multiplier between successive - /// octaves. - public double getLacunarity () - { - return lacunarity; - } - - /// Returns the quality of the Perlin noise. - /// - /// @returns The quality of the Perlin noise. - /// - /// See NoiseQuality for definitions of the various - /// coherent-noise qualities. - public NoiseQuality getNoiseQuality () - { - return noiseQuality; - } - - /// Returns the number of octaves that generate the Perlin noise. - /// - /// @returns The number of octaves that generate the Perlin noise. - /// - /// The number of octaves controls the amount of detail in the Perlin - /// noise. - public int getOctaveCount () - { - return octaveCount; - } - - /// Returns the persistence value of the Perlin noise. - /// - /// @returns The persistence value of the Perlin noise. - /// - /// The persistence value controls the roughness of the Perlin noise. - public double getPersistence () - { - return persistence; - } - - /// Returns the seed value used by the Perlin-noise function. - /// - /// @returns The seed value. - public int getSeed () - { - return seed; - } - - /// Sets the frequency of the first octave. - /// - /// @param frequency The frequency of the first octave. - public void setFrequency (double frequency) - { - this.frequency = frequency; - } - - /// Sets the lacunarity of the Perlin noise. - /// - /// @param lacunarity The lacunarity of the Perlin noise. - /// - /// The lacunarity is the frequency multiplier between successive - /// octaves. - /// - /// For best results, set the lacunarity to a number between 1.5 and - /// 3.5. - public void setLacunarity (double lacunarity) - { - this.lacunarity = lacunarity; - } - - /// Sets the quality of the Perlin noise. - /// - /// @param noiseQuality The quality of the Perlin noise. - /// - /// See NoiseQuality for definitions of the various - /// coherent-noise qualities. - public void setNoiseQuality (NoiseQuality noiseQuality) - { - this.noiseQuality = noiseQuality; - } - - /// Sets the number of octaves that generate the Perlin noise. - /// - /// @param octaveCount The number of octaves that generate the Perlin - /// noise. - /// - /// @pre The number of octaves ranges from 1 to PERLIN_MAX_OCTAVE. - /// - /// @throw noise::ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// The number of octaves controls the amount of detail in the Perlin - /// noise. - /// - /// The larger the number of octaves, the more time required to - /// calculate the Perlin-noise value. - public void setOctaveCount (int octaveCount) throws ExceptionInvalidParam - { - if (octaveCount < 1 || octaveCount > PERLIN_MAX_OCTAVE) - { - throw new ExceptionInvalidParam ("Invalid parameter In Perlin Noise Module"); - } - - this.octaveCount = octaveCount; - } - - /// Sets the persistence value of the Perlin noise. - /// - /// @param persistence The persistence value of the Perlin noise. - /// - /// The persistence value controls the roughness of the Perlin noise. - /// - /// For best results, set the persistence to a number between 0.0 and - /// 1.0. - public void setPersistence (double persistence) - { - this.persistence = persistence; - } - - /// Sets the seed value used by the Perlin-noise function. - /// - /// @param seed The seed value. - public void setSeed (int seed) - { - this.seed = seed; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.NoiseGen; +import libnoiseforjava.NoiseGen.NoiseQuality; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Perlin extends ModuleBase +{ + /// Noise module that outputs 3-dimensional Perlin noise. + /// + /// Perlin noise is the sum of several coherent-noise functions of + /// ever-increasing frequencies and ever-decreasing amplitudes. + /// + /// An important property of Perlin noise is that a small change in the + /// input value will produce a small change in the output value, while a + /// large change in the input value will produce a random change in the + /// output value. + /// + /// This noise module outputs Perlin-noise values that usually range from + /// -1.0 to +1.0, but there are no guarantees that all output values will + /// exist within that range. + /// + /// For a better description of Perlin noise, see the links in the + /// References and Acknowledgments section. + /// + /// This noise module does not require any source modules. + /// + /// Octaves + /// + /// The number of octaves control the amount of detail of the + /// Perlin noise. Adding more octaves increases the detail of the Perlin + /// noise, but with the drawback of increasing the calculation time. + /// + /// An octave is one of the coherent-noise functions in a series of + /// coherent-noise functions that are added together to form Perlin + /// noise. + /// + /// An application may specify the frequency of the first octave by + /// calling the setFrequency() method. + /// + /// An application may specify the number of octaves that generate Perlin + /// noise by calling the setOctaveCount() method. + /// + /// These coherent-noise functions are called octaves because each octave + /// has, by default, double the frequency of the previous octave. Musical + /// tones have this property as well; a musical C tone that is one octave + /// higher than the previous C tone has double its frequency. + /// + /// Frequency + /// + /// An application may specify the frequency of the first octave by + /// calling the setFrequency() method. + /// + /// Persistence + /// + /// The persistence value controls the roughness of the Perlin + /// noise. Larger values produce rougher noise. + /// + /// The persistence value determines how quickly the amplitudes diminish + /// for successive octaves. The amplitude of the first octave is 1.0. + /// The amplitude of each subsequent octave is equal to the product of the + /// previous octave's amplitude and the persistence value. So a + /// persistence value of 0.5 sets the amplitude of the first octave to + /// 1.0; the second, 0.5; the third, 0.25; etc. + /// + /// An application may specify the persistence value by calling the + /// setPersistence() method. + /// + /// Lacunarity + /// + /// The lacunarity specifies the frequency multipler between successive + /// octaves. + /// + /// The effect of modifying the lacunarity is subtle; you may need to play + /// with the lacunarity value to determine the effects. For best results, + /// set the lacunarity to a number between 1.5 and 3.5. + /// + /// References & acknowledgments + /// + /// The Noise Machine - + /// From the master, Ken Perlin himself. This page contains a + /// presentation that describes Perlin noise and some of its variants. + /// He won an Oscar for creating the Perlin noise algorithm! + /// + /// + /// Perlin Noise - Hugo Elias's webpage contains a very good + /// description of Perlin noise and describes its many applications. This + /// page gave me the inspiration to create libnoise in the first place. + /// Now that I know how to generate Perlin noise, I will never again use + /// cheesy subdivision algorithms to create terrain (unless I absolutely + /// need the speed.) + /// + /// The + /// Perlin noise math FAQ - A good page that describes Perlin noise in + /// plain English with only a minor amount of math. During development of + /// libnoise, I noticed that my coherent-noise function generated terrain + /// with some "regularity" to the terrain features. This page describes a + /// better coherent-noise function called gradient noise. This + /// version of the Perlin module uses gradient coherent noise to + /// generate Perlin noise. + + + /// Default frequency for the noise::module::Perlin noise module. + static final double DEFAULT_PERLIN_FREQUENCY = 1.0; + + /// Default lacunarity for the noise::module::Perlin noise module. + static final double DEFAULT_PERLIN_LACUNARITY = 2.0; + + /// Default number of octaves for the noise::module::Perlin noise module. + static final int DEFAULT_PERLIN_OCTAVE_COUNT = 6; + + /// Default persistence value for the noise::module::Perlin noise module. + static final double DEFAULT_PERLIN_PERSISTENCE = 0.5; + + /// Default noise quality for the noise::module::Perlin noise module. + static final NoiseQuality DEFAULT_PERLIN_QUALITY = NoiseQuality.QUALITY_STD; + + /// Default noise seed for the noise::module::Perlin noise module. + static final int DEFAULT_PERLIN_SEED = 0; + + /// Maximum number of octaves for the noise::module::Perlin noise module. + static final int PERLIN_MAX_OCTAVE = 30; + + + /// Frequency of the first octave. + double frequency; + + /// Frequency multiplier between successive octaves. + double lacunarity; + + /// Quality of the Perlin noise. + NoiseQuality noiseQuality; + + /// Total number of octaves that generate the Perlin noise. + int octaveCount; + + /// Persistence of the Perlin noise. + double persistence; + + /// Seed value used by the Perlin-noise function. + int seed; + + + public Perlin () + { + super(0); + frequency = DEFAULT_PERLIN_FREQUENCY; + lacunarity = DEFAULT_PERLIN_LACUNARITY; + noiseQuality = DEFAULT_PERLIN_QUALITY; + octaveCount = DEFAULT_PERLIN_OCTAVE_COUNT; + persistence = DEFAULT_PERLIN_PERSISTENCE; + seed = DEFAULT_PERLIN_SEED; + } + + public double getValue (double x, double y, double z) + { + double value = 0.0; + double signal = 0.0; + double curPersistence = 1.0; + double nx, ny, nz; + int curSeed; + + x *= frequency; + y *= frequency; + z *= frequency; + + for (int curOctave = 0; curOctave < octaveCount; curOctave++) + { + + // Make sure that these floating-point values have the same range as a 32- + // bit integer so that we can pass them to the coherent-noise functions. + nx = NoiseGen.MakeInt32Range (x); + ny = NoiseGen.MakeInt32Range (y); + nz = NoiseGen.MakeInt32Range (z); + + // Get the coherent-noise value from the input value and add it to the + // final result. + curSeed = (seed + curOctave) & 0xffffffff; + signal = NoiseGen.GradientCoherentNoise3D (nx, ny, nz, curSeed, noiseQuality); + value += signal * curPersistence; + + // Prepare the next octave. + x *= lacunarity; + y *= lacunarity; + z *= lacunarity; + curPersistence *= persistence; + } + + return value; + } + + /// Returns the frequency of the first octave. + /// + /// @returns The frequency of the first octave. + public double getFrequency () + { + return frequency; + } + + /// Returns the lacunarity of the Perlin noise. + /// + /// @returns The lacunarity of the Perlin noise. + /// + /// The lacunarity is the frequency multiplier between successive + /// octaves. + public double getLacunarity () + { + return lacunarity; + } + + /// Returns the quality of the Perlin noise. + /// + /// @returns The quality of the Perlin noise. + /// + /// See NoiseQuality for definitions of the various + /// coherent-noise qualities. + public NoiseQuality getNoiseQuality () + { + return noiseQuality; + } + + /// Returns the number of octaves that generate the Perlin noise. + /// + /// @returns The number of octaves that generate the Perlin noise. + /// + /// The number of octaves controls the amount of detail in the Perlin + /// noise. + public int getOctaveCount () + { + return octaveCount; + } + + /// Returns the persistence value of the Perlin noise. + /// + /// @returns The persistence value of the Perlin noise. + /// + /// The persistence value controls the roughness of the Perlin noise. + public double getPersistence () + { + return persistence; + } + + /// Returns the seed value used by the Perlin-noise function. + /// + /// @returns The seed value. + public int getSeed () + { + return seed; + } + + /// Sets the frequency of the first octave. + /// + /// @param frequency The frequency of the first octave. + public void setFrequency (double frequency) + { + this.frequency = frequency; + } + + /// Sets the lacunarity of the Perlin noise. + /// + /// @param lacunarity The lacunarity of the Perlin noise. + /// + /// The lacunarity is the frequency multiplier between successive + /// octaves. + /// + /// For best results, set the lacunarity to a number between 1.5 and + /// 3.5. + public void setLacunarity (double lacunarity) + { + this.lacunarity = lacunarity; + } + + /// Sets the quality of the Perlin noise. + /// + /// @param noiseQuality The quality of the Perlin noise. + /// + /// See NoiseQuality for definitions of the various + /// coherent-noise qualities. + public void setNoiseQuality (NoiseQuality noiseQuality) + { + this.noiseQuality = noiseQuality; + } + + /// Sets the number of octaves that generate the Perlin noise. + /// + /// @param octaveCount The number of octaves that generate the Perlin + /// noise. + /// + /// @pre The number of octaves ranges from 1 to PERLIN_MAX_OCTAVE. + /// + /// @throw noise::ExceptionInvalidParam An invalid parameter was + /// specified; see the preconditions for more information. + /// + /// The number of octaves controls the amount of detail in the Perlin + /// noise. + /// + /// The larger the number of octaves, the more time required to + /// calculate the Perlin-noise value. + public void setOctaveCount (int octaveCount) throws ExceptionInvalidParam + { + if (octaveCount < 1 || octaveCount > PERLIN_MAX_OCTAVE) + { + throw new ExceptionInvalidParam ("Invalid parameter In Perlin Noise Module"); + } + + this.octaveCount = octaveCount; + } + + /// Sets the persistence value of the Perlin noise. + /// + /// @param persistence The persistence value of the Perlin noise. + /// + /// The persistence value controls the roughness of the Perlin noise. + /// + /// For best results, set the persistence to a number between 0.0 and + /// 1.0. + public void setPersistence (double persistence) + { + this.persistence = persistence; + } + + /// Sets the seed value used by the Perlin-noise function. + /// + /// @param seed The seed value. + public void setSeed (int seed) + { + this.seed = seed; + } + +} diff --git a/src/libnoiseforjava/module/Power.java b/src/libnoiseforjava/module/Power.java index 0760f8b..53484a9 100644 --- a/src/libnoiseforjava/module/Power.java +++ b/src/libnoiseforjava/module/Power.java @@ -1,56 +1,56 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Power extends ModuleBase -{ - /// Noise module that raises the output value from a first source module - /// to the power of the output value from a second source module. - /// - /// The first source module must have an index value of 0. - /// - /// The second source module must have an index value of 1. - /// - /// This noise module requires two source modules. - - public Power (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam - { - super(2); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - - return Math.pow (sourceModules[0].getValue (x, y, z), - sourceModules[1].getValue (x, y, z)); - } -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Power extends ModuleBase +{ + /// Noise module that raises the output value from a first source module + /// to the power of the output value from a second source module. + /// + /// The first source module must have an index value of 0. + /// + /// The second source module must have an index value of 1. + /// + /// This noise module requires two source modules. + + public Power (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo) throws ExceptionInvalidParam + { + super(2); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + + return Math.pow (sourceModules[0].getValue (x, y, z), + sourceModules[1].getValue (x, y, z)); + } +} diff --git a/src/libnoiseforjava/module/RidgedMulti.java b/src/libnoiseforjava/module/RidgedMulti.java index 36916d9..c1a2d4c 100644 --- a/src/libnoiseforjava/module/RidgedMulti.java +++ b/src/libnoiseforjava/module/RidgedMulti.java @@ -1,357 +1,357 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.NoiseGen; -import libnoiseforjava.NoiseGen.NoiseQuality; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class RidgedMulti extends ModuleBase -{ - /// Noise module that outputs 3-dimensional ridged-multifractal noise. - /// - /// This noise module, heavily based on the Perlin-noise module, generates - /// ridged-multifractal noise. Ridged-multifractal noise is generated in - /// much of the same way as Perlin noise, except the output of each octave - /// is modified by an absolute-value function. Modifying the octave - /// values in this way produces ridge-like formations. - /// - /// Ridged-multifractal noise does not use a persistence value. This is - /// because the persistence values of the octaves are based on the values - /// generated from from previous octaves, creating a feedback loop (or - /// that's what it looks like after reading the code.) - /// - /// This noise module outputs ridged-multifractal-noise values that - /// usually range from -1.0 to +1.0, but there are no guarantees that all - /// output values will exist within that range. - /// - /// @note For ridged-multifractal noise generated with only one octave, - /// the output value ranges from -1.0 to 0.0. - /// - /// Ridged-multifractal noise is often used to generate craggy mountainous - /// terrain or marble-like textures. - /// - /// This noise module does not require any source modules. - /// - /// Octaves - /// - /// The number of octaves control the amount of detail of the - /// ridged-multifractal noise. Adding more octaves increases the detail - /// of the ridged-multifractal noise, but with the drawback of increasing - /// the calculation time. - /// - /// An application may specify the number of octaves that generate - /// ridged-multifractal noise by calling the setOctaveCount() method. - /// - /// Frequency - /// - /// An application may specify the frequency of the first octave by - /// calling the setFrequency() method. - /// - /// Lacunarity - /// - /// The lacunarity specifies the frequency multipler between successive - /// octaves. - /// - /// The effect of modifying the lacunarity is subtle; you may need to play - /// with the lacunarity value to determine the effects. For best results, - /// set the lacunarity to a number between 1.5 and 3.5. - /// - /// References & Acknowledgments - /// - /// F. - /// Kenton "Doc Mojo" Musgrave's texturing page - This page contains - /// links to source code that generates ridged-multfractal noise, among - /// other types of noise. The source file - /// fractal.c contains the code I used in my ridged-multifractal class - /// (see the @a RidgedMultifractal() function.) This code was written by F. - /// Kenton Musgrave, the person who created - /// MojoWorld. He is also one of - /// the authors in Texturing and Modeling: A Procedural Approach - /// (Morgan Kaufmann, 2002. ISBN 1-55860-848-6.) - - /// Default frequency for the noise::module::RidgedMulti noise module. - static final double DEFAULT_RIDGED_FREQUENCY = 1.0; - - /// Default lacunarity for the noise::module::RidgedMulti noise module. - static final double DEFAULT_RIDGED_LACUNARITY = 2.0; - - /// Default number of octaves for the noise::module::RidgedMulti noise - /// module. - static final int DEFAULT_RIDGED_OCTAVE_COUNT = 6; - - /// Default noise quality for the noise::module::RidgedMulti noise - /// module. - static final NoiseQuality DEFAULT_RIDGED_QUALITY = NoiseQuality.QUALITY_STD; - - /// Default noise seed for the noise::module::RidgedMulti noise module. - static final int DEFAULT_RIDGED_SEED = 0; - - /// Maximum number of octaves for the noise::module::RidgedMulti noise - /// module. - static final int RIDGED_MAX_OCTAVE = 30; - - /// Frequency of the first octave. - double frequency; - - /// Frequency multiplier between successive octaves. - double lacunarity; - - /// Quality of the ridged-multifractal noise. - NoiseQuality noiseQuality; - - /// Total number of octaves that generate the ridged-multifractal - /// noise. - int octaveCount; - - /// Contains the spectral weights for each octave. - double [] spectralWeights = new double[RIDGED_MAX_OCTAVE]; - - /// Seed value used by the ridged-multfractal-noise function. - int seed; - - - public RidgedMulti () - { - super(0); - frequency = DEFAULT_RIDGED_FREQUENCY; - lacunarity = DEFAULT_RIDGED_LACUNARITY; - noiseQuality = DEFAULT_RIDGED_QUALITY; - octaveCount = DEFAULT_RIDGED_OCTAVE_COUNT; - seed = DEFAULT_RIDGED_SEED; - - calcSpectralWeights(); - } - - // Calculates the spectral weights for each octave. - public void calcSpectralWeights () - { - // This exponent parameter should be user-defined; it may be exposed in a - // future version of libnoise. - double h = 1.0; - - double frequency = 1.0; - for (int i = 0; i < RIDGED_MAX_OCTAVE; i++) { - // Compute weight for each frequency. - this.spectralWeights[i] = Math.pow (frequency, -h); - frequency *= lacunarity; - } - } - - // Multifractal code originally written by F. Kenton "Doc Mojo" Musgrave, - // 1998. Modified by jas for use with libnoise. - public double getValue (double x, double y, double z) - { - x *= frequency; - y *= frequency; - z *= frequency; - - double signal = 0.0; - double value = 0.0; - double weight = 1.0; - - // These parameters should be user-defined; they may be exposed in a - // future version of libnoiseforjava. - double offset = 1.0; - double gain = 2.0; - - for (int curOctave = 0; curOctave < octaveCount; curOctave++) - { - // Make sure that these floating-point values have the same range as a 32- - // bit integer so that we can pass them to the coherent-noise functions. - double nx, ny, nz; - nx = NoiseGen.MakeInt32Range (x); - ny = NoiseGen.MakeInt32Range (y); - nz = NoiseGen.MakeInt32Range (z); - - // Get the coherent-noise value. - int curSeed = (seed + curOctave) & 0x7fffffff; - signal = NoiseGen.GradientCoherentNoise3D (nx, ny, nz, curSeed, noiseQuality); - - // Make the ridges. - signal = Math.abs (signal); - signal = offset - signal; - - // Square the signal to increase the sharpness of the ridges. - signal *= signal; - - // The weighting from the previous octave is applied to the signal. - // Larger values have higher weights, producing sharp points along the - // ridges. - signal *= weight; - - // Weight successive contributions by the previous signal. - weight = signal * gain; - if (weight > 1.0) - weight = 1.0; - if (weight < 0.0) - weight = 0.0; - - - // Add the signal to the output value. - value += (signal * spectralWeights[curOctave]); - - // Go to the next octave. - x *= lacunarity; - y *= lacunarity; - z *= lacunarity; - } - - return (value * 1.25) - 1.0; - } - - public double getFrequency () - { - return frequency; - } - - /// Returns the lacunarity of the ridged-multifractal noise. - /// - /// @returns The lacunarity of the ridged-multifractal noise. - /// - /// The lacunarity is the frequency multiplier between successive - /// octaves. - public double getLacunarity () - { - return lacunarity; - } - - /// Returns the quality of the ridged-multifractal noise. - /// - /// @returns The quality of the ridged-multifractal noise. - /// - /// See noise::NoiseQuality for definitions of the various - /// coherent-noise qualities. - public NoiseQuality getNoiseQuality () - { - return noiseQuality; - } - - /// Returns the number of octaves that generate the - /// ridged-multifractal noise. - /// - /// @returns The number of octaves that generate the - /// ridged-multifractal noise. - /// - /// The number of octaves controls the amount of detail in the - /// ridged-multifractal noise. - public int getOctaveCount () - { - return octaveCount; - } - - /// Returns the seed value used by the ridged-multifractal-noise - /// function. - /// - /// @returns The seed value. - public int getSeed () - { - return seed; - } - - - /// Sets the frequency of the first octave. - /// - /// @param frequency The frequency of the first octave. - public void setFrequency (double frequency) - { - this.frequency = frequency; - } - - /// Sets the lacunarity of the ridged-multifractal noise. - /// - /// @param lacunarity The lacunarity of the ridged-multifractal noise. - /// - /// The lacunarity is the frequency multiplier between successive - /// octaves. - /// - /// For best results, set the lacunarity to a number between 1.5 and - /// 3.5. - public void setLacunarity (double lacunarity) - { - this.lacunarity = lacunarity; - calcSpectralWeights (); - } - - /// Sets the quality of the ridged-multifractal noise. - /// - /// @param noiseQuality The quality of the ridged-multifractal noise. - /// - /// See NoiseQuality for definitions of the various - /// coherent-noise qualities. - public void setNoiseQuality (NoiseQuality noiseQuality) - { - this.noiseQuality = noiseQuality; - } - - /// Sets the number of octaves that generate the ridged-multifractal - /// noise. - /// - /// @param octaveCount The number of octaves that generate the - /// ridged-multifractal noise. - /// - /// @pre The number of octaves ranges from 1 to RIDGED_MAX_OCTAVE. - /// - /// @throw ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// The number of octaves controls the amount of detail in the - /// ridged-multifractal noise. - /// - /// The larger the number of octaves, the more time required to - /// calculate the ridged-multifractal-noise value. - public void setOctaveCount (int octaveCount) throws ExceptionInvalidParam - { - if (octaveCount > RIDGED_MAX_OCTAVE) - { - throw new ExceptionInvalidParam ("An invalid parameter was passed" + - " to a libnoise function or method."); - } - - this.octaveCount = octaveCount; - } - - /// Sets the seed value used by the ridged-multifractal-noise - /// function. - /// - /// @param seed The seed value. - public void setSeed (int seed) - { - this.seed = seed; - } - - public double[] getSpectralWeights() - { - return spectralWeights; - } - - public void setSpectralWeights(double[] spectralWeights) - { - this.spectralWeights = spectralWeights; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.NoiseGen; +import libnoiseforjava.NoiseGen.NoiseQuality; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class RidgedMulti extends ModuleBase +{ + /// Noise module that outputs 3-dimensional ridged-multifractal noise. + /// + /// This noise module, heavily based on the Perlin-noise module, generates + /// ridged-multifractal noise. Ridged-multifractal noise is generated in + /// much of the same way as Perlin noise, except the output of each octave + /// is modified by an absolute-value function. Modifying the octave + /// values in this way produces ridge-like formations. + /// + /// Ridged-multifractal noise does not use a persistence value. This is + /// because the persistence values of the octaves are based on the values + /// generated from from previous octaves, creating a feedback loop (or + /// that's what it looks like after reading the code.) + /// + /// This noise module outputs ridged-multifractal-noise values that + /// usually range from -1.0 to +1.0, but there are no guarantees that all + /// output values will exist within that range. + /// + /// @note For ridged-multifractal noise generated with only one octave, + /// the output value ranges from -1.0 to 0.0. + /// + /// Ridged-multifractal noise is often used to generate craggy mountainous + /// terrain or marble-like textures. + /// + /// This noise module does not require any source modules. + /// + /// Octaves + /// + /// The number of octaves control the amount of detail of the + /// ridged-multifractal noise. Adding more octaves increases the detail + /// of the ridged-multifractal noise, but with the drawback of increasing + /// the calculation time. + /// + /// An application may specify the number of octaves that generate + /// ridged-multifractal noise by calling the setOctaveCount() method. + /// + /// Frequency + /// + /// An application may specify the frequency of the first octave by + /// calling the setFrequency() method. + /// + /// Lacunarity + /// + /// The lacunarity specifies the frequency multipler between successive + /// octaves. + /// + /// The effect of modifying the lacunarity is subtle; you may need to play + /// with the lacunarity value to determine the effects. For best results, + /// set the lacunarity to a number between 1.5 and 3.5. + /// + /// References & Acknowledgments + /// + /// F. + /// Kenton "Doc Mojo" Musgrave's texturing page - This page contains + /// links to source code that generates ridged-multfractal noise, among + /// other types of noise. The source file + /// fractal.c contains the code I used in my ridged-multifractal class + /// (see the @a RidgedMultifractal() function.) This code was written by F. + /// Kenton Musgrave, the person who created + /// MojoWorld. He is also one of + /// the authors in Texturing and Modeling: A Procedural Approach + /// (Morgan Kaufmann, 2002. ISBN 1-55860-848-6.) + + /// Default frequency for the noise::module::RidgedMulti noise module. + static final double DEFAULT_RIDGED_FREQUENCY = 1.0; + + /// Default lacunarity for the noise::module::RidgedMulti noise module. + static final double DEFAULT_RIDGED_LACUNARITY = 2.0; + + /// Default number of octaves for the noise::module::RidgedMulti noise + /// module. + static final int DEFAULT_RIDGED_OCTAVE_COUNT = 6; + + /// Default noise quality for the noise::module::RidgedMulti noise + /// module. + static final NoiseQuality DEFAULT_RIDGED_QUALITY = NoiseQuality.QUALITY_STD; + + /// Default noise seed for the noise::module::RidgedMulti noise module. + static final int DEFAULT_RIDGED_SEED = 0; + + /// Maximum number of octaves for the noise::module::RidgedMulti noise + /// module. + static final int RIDGED_MAX_OCTAVE = 30; + + /// Frequency of the first octave. + double frequency; + + /// Frequency multiplier between successive octaves. + double lacunarity; + + /// Quality of the ridged-multifractal noise. + NoiseQuality noiseQuality; + + /// Total number of octaves that generate the ridged-multifractal + /// noise. + int octaveCount; + + /// Contains the spectral weights for each octave. + double [] spectralWeights = new double[RIDGED_MAX_OCTAVE]; + + /// Seed value used by the ridged-multfractal-noise function. + int seed; + + + public RidgedMulti () + { + super(0); + frequency = DEFAULT_RIDGED_FREQUENCY; + lacunarity = DEFAULT_RIDGED_LACUNARITY; + noiseQuality = DEFAULT_RIDGED_QUALITY; + octaveCount = DEFAULT_RIDGED_OCTAVE_COUNT; + seed = DEFAULT_RIDGED_SEED; + + calcSpectralWeights(); + } + + // Calculates the spectral weights for each octave. + public void calcSpectralWeights () + { + // This exponent parameter should be user-defined; it may be exposed in a + // future version of libnoise. + double h = 1.0; + + double frequency = 1.0; + for (int i = 0; i < RIDGED_MAX_OCTAVE; i++) { + // Compute weight for each frequency. + this.spectralWeights[i] = Math.pow (frequency, -h); + frequency *= lacunarity; + } + } + + // Multifractal code originally written by F. Kenton "Doc Mojo" Musgrave, + // 1998. Modified by jas for use with libnoise. + public double getValue (double x, double y, double z) + { + x *= frequency; + y *= frequency; + z *= frequency; + + double signal = 0.0; + double value = 0.0; + double weight = 1.0; + + // These parameters should be user-defined; they may be exposed in a + // future version of libnoiseforjava. + double offset = 1.0; + double gain = 2.0; + + for (int curOctave = 0; curOctave < octaveCount; curOctave++) + { + // Make sure that these floating-point values have the same range as a 32- + // bit integer so that we can pass them to the coherent-noise functions. + double nx, ny, nz; + nx = NoiseGen.MakeInt32Range (x); + ny = NoiseGen.MakeInt32Range (y); + nz = NoiseGen.MakeInt32Range (z); + + // Get the coherent-noise value. + int curSeed = (seed + curOctave) & 0x7fffffff; + signal = NoiseGen.GradientCoherentNoise3D (nx, ny, nz, curSeed, noiseQuality); + + // Make the ridges. + signal = Math.abs (signal); + signal = offset - signal; + + // Square the signal to increase the sharpness of the ridges. + signal *= signal; + + // The weighting from the previous octave is applied to the signal. + // Larger values have higher weights, producing sharp points along the + // ridges. + signal *= weight; + + // Weight successive contributions by the previous signal. + weight = signal * gain; + if (weight > 1.0) + weight = 1.0; + if (weight < 0.0) + weight = 0.0; + + + // Add the signal to the output value. + value += (signal * spectralWeights[curOctave]); + + // Go to the next octave. + x *= lacunarity; + y *= lacunarity; + z *= lacunarity; + } + + return (value * 1.25) - 1.0; + } + + public double getFrequency () + { + return frequency; + } + + /// Returns the lacunarity of the ridged-multifractal noise. + /// + /// @returns The lacunarity of the ridged-multifractal noise. + /// + /// The lacunarity is the frequency multiplier between successive + /// octaves. + public double getLacunarity () + { + return lacunarity; + } + + /// Returns the quality of the ridged-multifractal noise. + /// + /// @returns The quality of the ridged-multifractal noise. + /// + /// See noise::NoiseQuality for definitions of the various + /// coherent-noise qualities. + public NoiseQuality getNoiseQuality () + { + return noiseQuality; + } + + /// Returns the number of octaves that generate the + /// ridged-multifractal noise. + /// + /// @returns The number of octaves that generate the + /// ridged-multifractal noise. + /// + /// The number of octaves controls the amount of detail in the + /// ridged-multifractal noise. + public int getOctaveCount () + { + return octaveCount; + } + + /// Returns the seed value used by the ridged-multifractal-noise + /// function. + /// + /// @returns The seed value. + public int getSeed () + { + return seed; + } + + + /// Sets the frequency of the first octave. + /// + /// @param frequency The frequency of the first octave. + public void setFrequency (double frequency) + { + this.frequency = frequency; + } + + /// Sets the lacunarity of the ridged-multifractal noise. + /// + /// @param lacunarity The lacunarity of the ridged-multifractal noise. + /// + /// The lacunarity is the frequency multiplier between successive + /// octaves. + /// + /// For best results, set the lacunarity to a number between 1.5 and + /// 3.5. + public void setLacunarity (double lacunarity) + { + this.lacunarity = lacunarity; + calcSpectralWeights (); + } + + /// Sets the quality of the ridged-multifractal noise. + /// + /// @param noiseQuality The quality of the ridged-multifractal noise. + /// + /// See NoiseQuality for definitions of the various + /// coherent-noise qualities. + public void setNoiseQuality (NoiseQuality noiseQuality) + { + this.noiseQuality = noiseQuality; + } + + /// Sets the number of octaves that generate the ridged-multifractal + /// noise. + /// + /// @param octaveCount The number of octaves that generate the + /// ridged-multifractal noise. + /// + /// @pre The number of octaves ranges from 1 to RIDGED_MAX_OCTAVE. + /// + /// @throw ExceptionInvalidParam An invalid parameter was + /// specified; see the preconditions for more information. + /// + /// The number of octaves controls the amount of detail in the + /// ridged-multifractal noise. + /// + /// The larger the number of octaves, the more time required to + /// calculate the ridged-multifractal-noise value. + public void setOctaveCount (int octaveCount) throws ExceptionInvalidParam + { + if (octaveCount > RIDGED_MAX_OCTAVE) + { + throw new ExceptionInvalidParam ("An invalid parameter was passed" + + " to a libnoise function or method."); + } + + this.octaveCount = octaveCount; + } + + /// Sets the seed value used by the ridged-multifractal-noise + /// function. + /// + /// @param seed The seed value. + public void setSeed (int seed) + { + this.seed = seed; + } + + public double[] getSpectralWeights() + { + return spectralWeights; + } + + public void setSpectralWeights(double[] spectralWeights) + { + this.spectralWeights = spectralWeights; + } + +} diff --git a/src/libnoiseforjava/module/RotatePoint.java b/src/libnoiseforjava/module/RotatePoint.java index ead6c06..a1a57b5 100644 --- a/src/libnoiseforjava/module/RotatePoint.java +++ b/src/libnoiseforjava/module/RotatePoint.java @@ -1,216 +1,216 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class RotatePoint extends ModuleBase -{ - /// Noise module that rotates the input value around the origin before - /// returning the output value from a source module. - /// - /// The getValue() method rotates the coordinates of the input value - /// around the origin before returning the output value from the source - /// module. To set the rotation angles, call the setAngles() method. To - /// set the rotation angle around the individual @a x, @a y, or @a z axes, - /// call the setXAngle(), setYAngle() or setZAngle() methods, - /// respectively. - /// - /// The coordinate system of the input value is assumed to be - /// "left-handed" (@a x increases to the right, @a y increases upward, - /// and @a z increases inward.) - /// - /// This noise module requires one source module. - - /// Default @a x rotation angle for the RotatePoint noise - /// module. - static final double DEFAULT_ROTATE_X = 0.0; - - /// Default @a y rotation angle for the RotatePoint noise - /// module. - static final double DEFAULT_ROTATE_Y = 0.0; - - /// Default @a z rotation angle for the RotatePoint noise - /// module. - static final double DEFAULT_ROTATE_Z = 0.0; - - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double x1Matrix; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double x2Matrix; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double x3Matrix; - - /// @a x rotation angle applied to the input value, in degrees. - double xAngle; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double y1Matrix; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double y2Matrix; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double y3Matrix; - - /// @a y rotation angle applied to the input value, in degrees. - double yAngle; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double z1Matrix; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double z2Matrix; - - /// An entry within the 3x3 rotation matrix used for rotating the - /// input value. - double z3Matrix; - - /// @a z rotation angle applied to the input value, in degrees. - double zAngle; - - - public RotatePoint (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - setAngles (DEFAULT_ROTATE_X, DEFAULT_ROTATE_Y, DEFAULT_ROTATE_Z); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - double nx = (x1Matrix * x) + (y1Matrix * y) + (z1Matrix * z); - double ny = (x2Matrix * x) + (y2Matrix * y) + (z2Matrix * z); - double nz = (x3Matrix * x) + (y3Matrix * y) + (z3Matrix * z); - return sourceModules[0].getValue (nx, ny, nz); - } - - public void setAngles (double xAngle, double yAngle, - double zAngle) - { - double xCos, yCos, zCos, xSin, ySin, zSin; - xCos = Math.cos (Math.toRadians(xAngle)); - yCos = Math.cos (Math.toRadians(yAngle)); - zCos = Math.cos (Math.toRadians(zAngle)); - xSin = Math.sin (Math.toRadians(xAngle)); - ySin = Math.sin (Math.toRadians(yAngle)); - zSin = Math.sin (Math.toRadians(zAngle)); - - x1Matrix = ySin * xSin * zSin + yCos * zCos; - y1Matrix = xCos * zSin; - z1Matrix = ySin * zCos - yCos * xSin * zSin; - x2Matrix = ySin * xSin * zCos - yCos * zSin; - y2Matrix = xCos * zCos; - z2Matrix = -yCos * xSin * zCos - ySin * zSin; - x3Matrix = -ySin * xCos; - y3Matrix = xSin; - z3Matrix = yCos * xCos; - - this.xAngle = xAngle; - this.yAngle = yAngle; - this.zAngle = zAngle; - } - - /// Returns the rotation angle around the @a x axis to apply to the - /// input value. - /// - /// @returns The rotation angle around the @a x axis, in degrees. - public double getXAngle () - { - return xAngle; - } - - /// Returns the rotation angle around the @a y axis to apply to the - /// input value. - /// - /// @returns The rotation angle around the @a y axis, in degrees. - public double getYAngle () - { - return yAngle; - } - - /// Returns the rotation angle around the @a z axis to apply to the - /// input value. - /// - /// @returns The rotation angle around the @a z axis, in degrees. - public double getZAngle () - { - return zAngle; - } - - /// Sets the rotation angle around the @a x axis to apply to the input - /// value. - /// - /// @param xAngle The rotation angle around the @a x axis, in degrees. - /// - /// The getValue() method rotates the coordinates of the input value - /// around the origin before returning the output value from the - /// source module. - public void setXAngle (double xAngle) - { - setAngles (xAngle, this.yAngle, this.zAngle); - } - - /// Sets the rotation angle around the @a y axis to apply to the input - /// value. - /// - /// @param yAngle The rotation angle around the @a y axis, in degrees. - /// - /// The getValue() method rotates the coordinates of the input value - /// around the origin before returning the output value from the - /// source module. - public void SetYAngle (double yAngle) - { - setAngles (this.xAngle, yAngle, this.zAngle); - } - - /// Sets the rotation angle around the @a z axis to apply to the input - /// value. - /// - /// @param zAngle The rotation angle around the @a z axis, in degrees. - /// - /// The getValue() method rotates the coordinates of the input value - /// around the origin before returning the output value from the - /// source module. - public void SetZAngle (double zAngle) - { - setAngles (this.xAngle, this.yAngle, zAngle); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class RotatePoint extends ModuleBase +{ + /// Noise module that rotates the input value around the origin before + /// returning the output value from a source module. + /// + /// The getValue() method rotates the coordinates of the input value + /// around the origin before returning the output value from the source + /// module. To set the rotation angles, call the setAngles() method. To + /// set the rotation angle around the individual @a x, @a y, or @a z axes, + /// call the setXAngle(), setYAngle() or setZAngle() methods, + /// respectively. + /// + /// The coordinate system of the input value is assumed to be + /// "left-handed" (@a x increases to the right, @a y increases upward, + /// and @a z increases inward.) + /// + /// This noise module requires one source module. + + /// Default @a x rotation angle for the RotatePoint noise + /// module. + static final double DEFAULT_ROTATE_X = 0.0; + + /// Default @a y rotation angle for the RotatePoint noise + /// module. + static final double DEFAULT_ROTATE_Y = 0.0; + + /// Default @a z rotation angle for the RotatePoint noise + /// module. + static final double DEFAULT_ROTATE_Z = 0.0; + + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double x1Matrix; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double x2Matrix; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double x3Matrix; + + /// @a x rotation angle applied to the input value, in degrees. + double xAngle; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double y1Matrix; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double y2Matrix; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double y3Matrix; + + /// @a y rotation angle applied to the input value, in degrees. + double yAngle; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double z1Matrix; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double z2Matrix; + + /// An entry within the 3x3 rotation matrix used for rotating the + /// input value. + double z3Matrix; + + /// @a z rotation angle applied to the input value, in degrees. + double zAngle; + + + public RotatePoint (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + setAngles (DEFAULT_ROTATE_X, DEFAULT_ROTATE_Y, DEFAULT_ROTATE_Z); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + double nx = (x1Matrix * x) + (y1Matrix * y) + (z1Matrix * z); + double ny = (x2Matrix * x) + (y2Matrix * y) + (z2Matrix * z); + double nz = (x3Matrix * x) + (y3Matrix * y) + (z3Matrix * z); + return sourceModules[0].getValue (nx, ny, nz); + } + + public void setAngles (double xAngle, double yAngle, + double zAngle) + { + double xCos, yCos, zCos, xSin, ySin, zSin; + xCos = Math.cos (Math.toRadians(xAngle)); + yCos = Math.cos (Math.toRadians(yAngle)); + zCos = Math.cos (Math.toRadians(zAngle)); + xSin = Math.sin (Math.toRadians(xAngle)); + ySin = Math.sin (Math.toRadians(yAngle)); + zSin = Math.sin (Math.toRadians(zAngle)); + + x1Matrix = ySin * xSin * zSin + yCos * zCos; + y1Matrix = xCos * zSin; + z1Matrix = ySin * zCos - yCos * xSin * zSin; + x2Matrix = ySin * xSin * zCos - yCos * zSin; + y2Matrix = xCos * zCos; + z2Matrix = -yCos * xSin * zCos - ySin * zSin; + x3Matrix = -ySin * xCos; + y3Matrix = xSin; + z3Matrix = yCos * xCos; + + this.xAngle = xAngle; + this.yAngle = yAngle; + this.zAngle = zAngle; + } + + /// Returns the rotation angle around the @a x axis to apply to the + /// input value. + /// + /// @returns The rotation angle around the @a x axis, in degrees. + public double getXAngle () + { + return xAngle; + } + + /// Returns the rotation angle around the @a y axis to apply to the + /// input value. + /// + /// @returns The rotation angle around the @a y axis, in degrees. + public double getYAngle () + { + return yAngle; + } + + /// Returns the rotation angle around the @a z axis to apply to the + /// input value. + /// + /// @returns The rotation angle around the @a z axis, in degrees. + public double getZAngle () + { + return zAngle; + } + + /// Sets the rotation angle around the @a x axis to apply to the input + /// value. + /// + /// @param xAngle The rotation angle around the @a x axis, in degrees. + /// + /// The getValue() method rotates the coordinates of the input value + /// around the origin before returning the output value from the + /// source module. + public void setXAngle (double xAngle) + { + setAngles (xAngle, this.yAngle, this.zAngle); + } + + /// Sets the rotation angle around the @a y axis to apply to the input + /// value. + /// + /// @param yAngle The rotation angle around the @a y axis, in degrees. + /// + /// The getValue() method rotates the coordinates of the input value + /// around the origin before returning the output value from the + /// source module. + public void SetYAngle (double yAngle) + { + setAngles (this.xAngle, yAngle, this.zAngle); + } + + /// Sets the rotation angle around the @a z axis to apply to the input + /// value. + /// + /// @param zAngle The rotation angle around the @a z axis, in degrees. + /// + /// The getValue() method rotates the coordinates of the input value + /// around the origin before returning the output value from the + /// source module. + public void SetZAngle (double zAngle) + { + setAngles (this.xAngle, this.yAngle, zAngle); + } + +} diff --git a/src/libnoiseforjava/module/ScaleBias.java b/src/libnoiseforjava/module/ScaleBias.java index cde1750..66daea4 100644 --- a/src/libnoiseforjava/module/ScaleBias.java +++ b/src/libnoiseforjava/module/ScaleBias.java @@ -1,124 +1,124 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class ScaleBias extends ModuleBase -{ - - /// Noise module that applies a scaling factor and a bias to the output - /// value from a source module. - /// - /// The getValue() method retrieves the output value from the source - /// module, multiplies it with a scaling factor, adds a bias to it, then - /// outputs the value. - /// - /// This noise module requires one source module. - - /// Default bias for the ScaleBias noise module. - static final double DEFAULT_BIAS = 0.0; - - /// Default scale for the ScaleBias noise module. - static final double DEFAULT_SCALE = 1.0; - - /// Bias to apply to the scaled output value from the source module. - double bias; - - /// Scaling factor to apply to the output value from the source - /// module. - double scale; - - - public ScaleBias (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - bias = DEFAULT_BIAS; - scale = DEFAULT_SCALE; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - return sourceModules[0].getValue (x, y, z) * scale + bias; - } - - /// Returns the bias to apply to the scaled output value from the - /// source module. - /// - /// @returns The bias to apply. - /// - /// The getValue() method retrieves the output value from the source - /// module, multiplies it with the scaling factor, adds the bias to - /// it, then outputs the value. - public double getBias () - { - return bias; - } - - /// Returns the scaling factor to apply to the output value from the - /// source module. - /// - /// @returns The scaling factor to apply. - /// - /// The getValue() method retrieves the output value from the source - /// module, multiplies it with the scaling factor, adds the bias to - /// it, then outputs the value. - public double getScale () - { - return scale; - } - - - /// Sets the bias to apply to the scaled output value from the source - /// module. - /// - /// @param bias The bias to apply. - /// - /// The getValue() method retrieves the output value from the source - /// module, multiplies it with the scaling factor, adds the bias to - /// it, then outputs the value. - public void setBias (double bias) - { - this.bias = bias; - } - - /// Sets the scaling factor to apply to the output value from the - /// source module. - /// - /// @param scale The scaling factor to apply. - /// - /// The getValue() method retrieves the output value from the source - /// module, multiplies it with the scaling factor, adds the bias to - /// it, then outputs the value. - public void setScale (double scale) - { - this.scale = scale; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class ScaleBias extends ModuleBase +{ + + /// Noise module that applies a scaling factor and a bias to the output + /// value from a source module. + /// + /// The getValue() method retrieves the output value from the source + /// module, multiplies it with a scaling factor, adds a bias to it, then + /// outputs the value. + /// + /// This noise module requires one source module. + + /// Default bias for the ScaleBias noise module. + static final double DEFAULT_BIAS = 0.0; + + /// Default scale for the ScaleBias noise module. + static final double DEFAULT_SCALE = 1.0; + + /// Bias to apply to the scaled output value from the source module. + double bias; + + /// Scaling factor to apply to the output value from the source + /// module. + double scale; + + + public ScaleBias (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + bias = DEFAULT_BIAS; + scale = DEFAULT_SCALE; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + return sourceModules[0].getValue (x, y, z) * scale + bias; + } + + /// Returns the bias to apply to the scaled output value from the + /// source module. + /// + /// @returns The bias to apply. + /// + /// The getValue() method retrieves the output value from the source + /// module, multiplies it with the scaling factor, adds the bias to + /// it, then outputs the value. + public double getBias () + { + return bias; + } + + /// Returns the scaling factor to apply to the output value from the + /// source module. + /// + /// @returns The scaling factor to apply. + /// + /// The getValue() method retrieves the output value from the source + /// module, multiplies it with the scaling factor, adds the bias to + /// it, then outputs the value. + public double getScale () + { + return scale; + } + + + /// Sets the bias to apply to the scaled output value from the source + /// module. + /// + /// @param bias The bias to apply. + /// + /// The getValue() method retrieves the output value from the source + /// module, multiplies it with the scaling factor, adds the bias to + /// it, then outputs the value. + public void setBias (double bias) + { + this.bias = bias; + } + + /// Sets the scaling factor to apply to the output value from the + /// source module. + /// + /// @param scale The scaling factor to apply. + /// + /// The getValue() method retrieves the output value from the source + /// module, multiplies it with the scaling factor, adds the bias to + /// it, then outputs the value. + public void setScale (double scale) + { + this.scale = scale; + } + +} diff --git a/src/libnoiseforjava/module/ScalePoint.java b/src/libnoiseforjava/module/ScalePoint.java index 37fa00f..f0c10ce 100644 --- a/src/libnoiseforjava/module/ScalePoint.java +++ b/src/libnoiseforjava/module/ScalePoint.java @@ -1,182 +1,182 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class ScalePoint extends ModuleBase -{ - - /// Noise module that scales the coordinates of the input value before - /// returning the output value from a source module. - /// - /// The getValue() method multiplies the ( @a x, @a y, @a z ) coordinates - /// of the input value with a scaling factor before returning the output - /// value from the source module. To set the scaling factor, call the - /// setScale() method. To set the scaling factor to apply to the - /// individual @a x, @a y, or @a z coordinates, call the setXScale(), - /// setYScale() or setZScale() methods, respectively. - /// - /// This noise module requires one source module. - - // Default scaling factor applied to the @a x coordinate for the - /// ScalePoint noise module. - static final double DEFAULT_SCALE_POINT_X = 1.0; - - /// Default scaling factor applied to the @a y coordinate for the - /// ScalePoint noise module. - static final double DEFAULT_SCALE_POINT_Y = 1.0; - - /// Default scaling factor applied to the @a z coordinate for the - /// ScalePoint noise module. - static final double DEFAULT_SCALE_POINT_Z = 1.0; - - /// Scaling factor applied to the @a x coordinate of the input value. - double xScale; - - /// Scaling factor applied to the @a y coordinate of the input value. - double yScale; - - /// Scaling factor applied to the @a z coordinate of the input value. - double zScale; - - - public ScalePoint (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - - xScale = DEFAULT_SCALE_POINT_X; - yScale = DEFAULT_SCALE_POINT_Y; - zScale = DEFAULT_SCALE_POINT_Z; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - return sourceModules[0].getValue (x * xScale, y * yScale, - z * zScale); - } - - /// Returns the scaling factor applied to the @a x coordinate of the - /// input value. - /// - /// @returns The scaling factor applied to the @a x coordinate. - public double getXScale () - { - return xScale; - } - - /// Returns the scaling factor applied to the @a y coordinate of the - /// input value. - /// - /// @returns The scaling factor applied to the @a y coordinate. - public double getYScale () - { - return yScale; - } - - /// Returns the scaling factor applied to the @a z coordinate of the - /// input value. - /// - /// @returns The scaling factor applied to the @a z coordinate. - public double getZScale () - { - return zScale; - } - - /// Sets the scaling factor to apply to the input value. - /// - /// @param scale The scaling factor to apply. - /// - /// The getValue() method multiplies the ( @a x, @a y, @a z ) - /// coordinates of the input value with a scaling factor before - /// returning the output value from the source module. - public void setScale (double scale) - { - this.xScale = scale; - this.yScale = scale; - this.zScale = scale; - } - - /// Sets the scaling factor to apply to the ( @a x, @a y, @a z ) - /// coordinates of the input value. - /// - /// @param xScale The scaling factor to apply to the @a x coordinate. - /// @param yScale The scaling factor to apply to the @a y coordinate. - /// @param zScale The scaling factor to apply to the @a z coordinate. - /// - /// The getValue() method multiplies the ( @a x, @a y, @a z ) - /// coordinates of the input value with a scaling factor before - /// returning the output value from the source module. - public void setScale (double xScale, double yScale, double zScale) - { - this.xScale = xScale; - this.yScale = yScale; - this.zScale = zScale; - } - - /// Sets the scaling factor to apply to the @a x coordinate of the - /// input value. - /// - /// @param xScale The scaling factor to apply to the @a x coordinate. - /// - /// The getValue() method multiplies the ( @a x, @a y, @a z ) - /// coordinates of the input value with a scaling factor before - /// returning the output value from the source module. - public void setXScale (double xScale) - { - this.xScale = xScale; - } - - /// Sets the scaling factor to apply to the @a y coordinate of the - /// input value. - /// - /// @param yScale The scaling factor to apply to the @a y coordinate. - /// - /// The getValue() method multiplies the ( @a x, @a y, @a z ) - /// coordinates of the input value with a scaling factor before - /// returning the output value from the source module. - public void setYScale (double yScale) - { - this.yScale = yScale; - } - - /// Sets the scaling factor to apply to the @a z coordinate of the - /// input value. - /// - /// @param zScale The scaling factor to apply to the @a z coordinate. - /// - /// The getValue() method multiplies the ( @a x, @a y, @a z ) - /// coordinates of the input value with a scaling factor before - /// returning the output value from the source module. - public void setZScale (double zScale) - { - this.zScale = zScale; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class ScalePoint extends ModuleBase +{ + + /// Noise module that scales the coordinates of the input value before + /// returning the output value from a source module. + /// + /// The getValue() method multiplies the ( @a x, @a y, @a z ) coordinates + /// of the input value with a scaling factor before returning the output + /// value from the source module. To set the scaling factor, call the + /// setScale() method. To set the scaling factor to apply to the + /// individual @a x, @a y, or @a z coordinates, call the setXScale(), + /// setYScale() or setZScale() methods, respectively. + /// + /// This noise module requires one source module. + + // Default scaling factor applied to the @a x coordinate for the + /// ScalePoint noise module. + static final double DEFAULT_SCALE_POINT_X = 1.0; + + /// Default scaling factor applied to the @a y coordinate for the + /// ScalePoint noise module. + static final double DEFAULT_SCALE_POINT_Y = 1.0; + + /// Default scaling factor applied to the @a z coordinate for the + /// ScalePoint noise module. + static final double DEFAULT_SCALE_POINT_Z = 1.0; + + /// Scaling factor applied to the @a x coordinate of the input value. + double xScale; + + /// Scaling factor applied to the @a y coordinate of the input value. + double yScale; + + /// Scaling factor applied to the @a z coordinate of the input value. + double zScale; + + + public ScalePoint (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + + xScale = DEFAULT_SCALE_POINT_X; + yScale = DEFAULT_SCALE_POINT_Y; + zScale = DEFAULT_SCALE_POINT_Z; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + return sourceModules[0].getValue (x * xScale, y * yScale, + z * zScale); + } + + /// Returns the scaling factor applied to the @a x coordinate of the + /// input value. + /// + /// @returns The scaling factor applied to the @a x coordinate. + public double getXScale () + { + return xScale; + } + + /// Returns the scaling factor applied to the @a y coordinate of the + /// input value. + /// + /// @returns The scaling factor applied to the @a y coordinate. + public double getYScale () + { + return yScale; + } + + /// Returns the scaling factor applied to the @a z coordinate of the + /// input value. + /// + /// @returns The scaling factor applied to the @a z coordinate. + public double getZScale () + { + return zScale; + } + + /// Sets the scaling factor to apply to the input value. + /// + /// @param scale The scaling factor to apply. + /// + /// The getValue() method multiplies the ( @a x, @a y, @a z ) + /// coordinates of the input value with a scaling factor before + /// returning the output value from the source module. + public void setScale (double scale) + { + this.xScale = scale; + this.yScale = scale; + this.zScale = scale; + } + + /// Sets the scaling factor to apply to the ( @a x, @a y, @a z ) + /// coordinates of the input value. + /// + /// @param xScale The scaling factor to apply to the @a x coordinate. + /// @param yScale The scaling factor to apply to the @a y coordinate. + /// @param zScale The scaling factor to apply to the @a z coordinate. + /// + /// The getValue() method multiplies the ( @a x, @a y, @a z ) + /// coordinates of the input value with a scaling factor before + /// returning the output value from the source module. + public void setScale (double xScale, double yScale, double zScale) + { + this.xScale = xScale; + this.yScale = yScale; + this.zScale = zScale; + } + + /// Sets the scaling factor to apply to the @a x coordinate of the + /// input value. + /// + /// @param xScale The scaling factor to apply to the @a x coordinate. + /// + /// The getValue() method multiplies the ( @a x, @a y, @a z ) + /// coordinates of the input value with a scaling factor before + /// returning the output value from the source module. + public void setXScale (double xScale) + { + this.xScale = xScale; + } + + /// Sets the scaling factor to apply to the @a y coordinate of the + /// input value. + /// + /// @param yScale The scaling factor to apply to the @a y coordinate. + /// + /// The getValue() method multiplies the ( @a x, @a y, @a z ) + /// coordinates of the input value with a scaling factor before + /// returning the output value from the source module. + public void setYScale (double yScale) + { + this.yScale = yScale; + } + + /// Sets the scaling factor to apply to the @a z coordinate of the + /// input value. + /// + /// @param zScale The scaling factor to apply to the @a z coordinate. + /// + /// The getValue() method multiplies the ( @a x, @a y, @a z ) + /// coordinates of the input value with a scaling factor before + /// returning the output value from the source module. + public void setZScale (double zScale) + { + this.zScale = zScale; + } + +} diff --git a/src/libnoiseforjava/module/Select.java b/src/libnoiseforjava/module/Select.java index 26a54cf..d394aa3 100644 --- a/src/libnoiseforjava/module/Select.java +++ b/src/libnoiseforjava/module/Select.java @@ -1,300 +1,300 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.Interp; -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.exception.ExceptionNoModule; - -public class Select extends ModuleBase -{ - /// Noise module that outputs the value selected from one of two source - /// modules chosen by the output value from a control module. - /// - /// Unlike most other noise modules, the index value assigned to a source - /// module determines its role in the selection operation: - /// - Source module 0 (upper left in the diagram) outputs a value. - /// - Source module 1 (lower left in the diagram) outputs a value. - /// - Source module 2 (bottom of the diagram) is known as the control - /// module. The control module determines the value to select. If - /// the output value from the control module is within a range of values - /// known as the selection range, this noise module outputs the - /// value from the source module with an index value of 1. Otherwise, - /// this noise module outputs the value from the source module with an - /// index value of 0. - /// - /// To specify the bounds of the selection range, call the setBounds() - /// method. - /// - /// An application can pass the control module to the setControlModule() - /// method instead of the setSourceModule() method. This may make the - /// application code easier to read. - /// - /// By default, there is an abrupt transition between the output values - /// from the two source modules at the selection-range boundary. To - /// smooth the transition, pass a non-zero value to the setEdgeFalloff() - /// method. Higher values result in a smoother transition. - /// - /// This noise module requires three source modules. - - /// Default edge-falloff value for the Select noise module. - static final double DEFAULT_SELECT_EDGE_FALLOFF = 0.0; - - /// Default lower bound of the selection range for the - /// Select noise module. - static final double DEFAULT_SELECT_LOWER_BOUND = -1.0; - - /// Default upper bound of the selection range for the - /// Select noise module. - static final double DEFAULT_SELECT_UPPER_BOUND = 1.0; - - /// Edge-falloff value. - double edgeFalloff; - - /// Lower bound of the selection range. - double lowerBound; - - /// Upper bound of the selection range. - double upperBound; - - - public Select (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, - ModuleBase sourceModuleThree) throws ExceptionInvalidParam - { - super(3); - setSourceModule(0, sourceModuleOne); - setSourceModule(1, sourceModuleTwo); - setSourceModule(2, sourceModuleThree); - - edgeFalloff = DEFAULT_SELECT_EDGE_FALLOFF; - lowerBound = DEFAULT_SELECT_LOWER_BOUND; - upperBound = DEFAULT_SELECT_UPPER_BOUND; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (sourceModules[1] != null); - assert (sourceModules[2] != null); - - double controlValue = sourceModules[2].getValue (x, y, z); - double alpha; - - if (edgeFalloff > 0.0) - { - if (controlValue < (lowerBound - edgeFalloff)) - // The output value from the control module is below the selector - // threshold; return the output value from the first source module. - return sourceModules[0].getValue (x, y, z); - else if (controlValue < (lowerBound + edgeFalloff)) - { - // The output value from the control module is near the lower end of the - // selector threshold and within the smooth curve. Interpolate between - // the output values from the first and second source modules. - double lowerCurve = (lowerBound - edgeFalloff); - double upperCurve = (lowerBound + edgeFalloff); - alpha = Interp.SCurve3 ( - (controlValue - lowerCurve) / (upperCurve - lowerCurve)); - return Interp.linearInterp (sourceModules[0].getValue (x, y, z), - sourceModules[2].getValue (x, y, z), - alpha); - } - else if (controlValue < (upperBound - edgeFalloff)) - // The output value from the control module is within the selector - // threshold; return the output value from the second source module. - return sourceModules[1].getValue (x, y, z); - else if (controlValue < (upperBound + edgeFalloff)) - { - // The output value from the control module is near the upper end of the - // selector threshold and within the smooth curve. Interpolate between - // the output values from the first and second source modules. - double lowerCurve = (upperBound - edgeFalloff); - double upperCurve = (upperBound + edgeFalloff); - alpha = Interp.SCurve3 ( - (controlValue - lowerCurve) / (upperCurve - lowerCurve)); - return Interp.linearInterp (sourceModules[1].getValue (x, y, z), - sourceModules[0].getValue (x, y, z), - alpha); - } - else - // Output value from the control module is above the selector threshold; - // return the output value from the first source module. - return sourceModules[0].getValue (x, y, z); - } - else - { - if (controlValue < lowerBound || controlValue > upperBound) - return sourceModules[0].getValue (x, y, z); - else - return sourceModules[1].getValue (x, y, z); - } - } - - /// Sets the lower and upper bounds of the selection range. - /// - /// @param lowerBound The lower bound. - /// @param upperBound The upper bound. - /// - /// @pre The lower bound must be less than or equal to the upper - /// bound. - public void setBounds (double lowerBound, double upperBound) - { - assert (lowerBound < upperBound); - - this.lowerBound = lowerBound; - this.upperBound = upperBound; - - // Make sure that the edge falloff curves do not overlap. - setEdgeFalloff (edgeFalloff); - } - - /// Sets the falloff value at the edge transition. - /// - /// @param edgeFalloff The falloff value at the edge transition. - /// - /// The falloff value is the width of the edge transition at either - /// edge of the selection range. - /// - /// By default, there is an abrupt transition between the values from - /// the two source modules at the boundaries of the selection range. - /// - /// For example, if the selection range is 0.5 to 0.8, and the edge - /// falloff value is 0.1, then the getValue() method outputs: - /// - the output value from the source module with an index value of 0 - /// if the output value from the control module is less than 0.4 - /// ( = 0.5 - 0.1). - /// - a linear blend between the two output values from the two source - /// modules if the output value from the control module is between - /// 0.4 ( = 0.5 - 0.1) and 0.6 ( = 0.5 + 0.1). - /// - the output value from the source module with an index value of 1 - /// if the output value from the control module is between 0.6 - /// ( = 0.5 + 0.1) and 0.7 ( = 0.8 - 0.1). - /// - a linear blend between the output values from the two source - /// modules if the output value from the control module is between - /// 0.7 ( = 0.8 - 0.1 ) and 0.9 ( = 0.8 + 0.1). - /// - the output value from the source module with an index value of 0 - /// if the output value from the control module is greater than 0.9 - /// ( = 0.8 + 0.1). - public void setEdgeFalloff (double edgeFalloff) - { - // Make sure that the edge falloff curves do not overlap. - double boundSize = upperBound - lowerBound; - edgeFalloff = (edgeFalloff > boundSize / 2)? boundSize / 2: edgeFalloff; - } - - /// Returns the control module. - /// - /// @returns A reference to the control module. - /// - /// @pre A control module has been added to this noise module via a - /// call to setSourceModule() or setControlModule(). - /// - /// @throw ExceptionNoModule See the preconditions for more - /// information. - /// - /// The control module determines the output value to select. If the - /// output value from the control module is within a range of values - /// known as the selection range, the getValue() method outputs - /// the value from the source module with an index value of 1. - /// Otherwise, this method outputs the value from the source module - /// with an index value of 0. - - // not sure this does what it says it does. Recheck original source - public ModuleBase getControlModule () throws ExceptionNoModule - { - if (sourceModules == null || sourceModules[2] == null) { - throw new ExceptionNoModule ("Could not retrieve a source module from a noise module."); - } - return (sourceModules[2]); - } - - /// Returns the falloff value at the edge transition. - /// - /// @returns The falloff value at the edge transition. - /// - /// The falloff value is the width of the edge transition at either - /// edge of the selection range. - /// - /// By default, there is an abrupt transition between the output - /// values from the two source modules at the selection-range - /// boundary. - public double getEdgeFalloff () - { - return edgeFalloff; - } - - /// Returns the lower bound of the selection range. - /// - /// @returns The lower bound of the selection range. - /// - /// If the output value from the control module is within the - /// selection range, the getValue() method outputs the value from the - /// source module with an index value of 1. Otherwise, this method - /// outputs the value from the source module with an index value of 0. - public double getLowerBound () - { - return lowerBound; - } - - /// Returns the upper bound of the selection range. - /// - /// @returns The upper bound of the selection range. - /// - /// If the output value from the control module is within the - /// selection range, the getValue() method outputs the value from the - /// source module with an index value of 1. Otherwise, this method - /// outputs the value from the source module with an index value of 0. - public double getUpperBound () - { - return upperBound; - } - - /// Sets the control module. - /// - /// @param controlModule The control module. - /// - /// The control module determines the output value to select. If the - /// output value from the control module is within a range of values - /// known as the selection range, the getValue() method outputs - /// the value from the source module with an index value of 1. - /// Otherwise, this method outputs the value from the source module - /// with an index value of 0. - /// - /// This method assigns the control module an index value of 2. - /// Passing the control module to this method produces the same - /// results as passing the control module to the setSourceModule() - /// method while assigning that noise module an index value of 2. - /// - /// This control module must exist throughout the lifetime of this - /// noise module unless another control module replaces that control - /// module. - public void setControlModule (ModuleBase controlModule) - { - assert (sourceModules != null); - sourceModules[2] = controlModule; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.Interp; +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.exception.ExceptionNoModule; + +public class Select extends ModuleBase +{ + /// Noise module that outputs the value selected from one of two source + /// modules chosen by the output value from a control module. + /// + /// Unlike most other noise modules, the index value assigned to a source + /// module determines its role in the selection operation: + /// - Source module 0 (upper left in the diagram) outputs a value. + /// - Source module 1 (lower left in the diagram) outputs a value. + /// - Source module 2 (bottom of the diagram) is known as the control + /// module. The control module determines the value to select. If + /// the output value from the control module is within a range of values + /// known as the selection range, this noise module outputs the + /// value from the source module with an index value of 1. Otherwise, + /// this noise module outputs the value from the source module with an + /// index value of 0. + /// + /// To specify the bounds of the selection range, call the setBounds() + /// method. + /// + /// An application can pass the control module to the setControlModule() + /// method instead of the setSourceModule() method. This may make the + /// application code easier to read. + /// + /// By default, there is an abrupt transition between the output values + /// from the two source modules at the selection-range boundary. To + /// smooth the transition, pass a non-zero value to the setEdgeFalloff() + /// method. Higher values result in a smoother transition. + /// + /// This noise module requires three source modules. + + /// Default edge-falloff value for the Select noise module. + static final double DEFAULT_SELECT_EDGE_FALLOFF = 0.0; + + /// Default lower bound of the selection range for the + /// Select noise module. + static final double DEFAULT_SELECT_LOWER_BOUND = -1.0; + + /// Default upper bound of the selection range for the + /// Select noise module. + static final double DEFAULT_SELECT_UPPER_BOUND = 1.0; + + /// Edge-falloff value. + double edgeFalloff; + + /// Lower bound of the selection range. + double lowerBound; + + /// Upper bound of the selection range. + double upperBound; + + + public Select (ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, + ModuleBase sourceModuleThree) throws ExceptionInvalidParam + { + super(3); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + setSourceModule(2, sourceModuleThree); + + edgeFalloff = DEFAULT_SELECT_EDGE_FALLOFF; + lowerBound = DEFAULT_SELECT_LOWER_BOUND; + upperBound = DEFAULT_SELECT_UPPER_BOUND; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + assert (sourceModules[2] != null); + + double controlValue = sourceModules[2].getValue (x, y, z); + double alpha; + + if (edgeFalloff > 0.0) + { + if (controlValue < (lowerBound - edgeFalloff)) + // The output value from the control module is below the selector + // threshold; return the output value from the first source module. + return sourceModules[0].getValue (x, y, z); + else if (controlValue < (lowerBound + edgeFalloff)) + { + // The output value from the control module is near the lower end of the + // selector threshold and within the smooth curve. Interpolate between + // the output values from the first and second source modules. + double lowerCurve = (lowerBound - edgeFalloff); + double upperCurve = (lowerBound + edgeFalloff); + alpha = Interp.SCurve3 ( + (controlValue - lowerCurve) / (upperCurve - lowerCurve)); + return Interp.linearInterp (sourceModules[0].getValue (x, y, z), + sourceModules[2].getValue (x, y, z), + alpha); + } + else if (controlValue < (upperBound - edgeFalloff)) + // The output value from the control module is within the selector + // threshold; return the output value from the second source module. + return sourceModules[1].getValue (x, y, z); + else if (controlValue < (upperBound + edgeFalloff)) + { + // The output value from the control module is near the upper end of the + // selector threshold and within the smooth curve. Interpolate between + // the output values from the first and second source modules. + double lowerCurve = (upperBound - edgeFalloff); + double upperCurve = (upperBound + edgeFalloff); + alpha = Interp.SCurve3 ( + (controlValue - lowerCurve) / (upperCurve - lowerCurve)); + return Interp.linearInterp (sourceModules[1].getValue (x, y, z), + sourceModules[0].getValue (x, y, z), + alpha); + } + else + // Output value from the control module is above the selector threshold; + // return the output value from the first source module. + return sourceModules[0].getValue (x, y, z); + } + else + { + if (controlValue < lowerBound || controlValue > upperBound) + return sourceModules[0].getValue (x, y, z); + else + return sourceModules[1].getValue (x, y, z); + } + } + + /// Sets the lower and upper bounds of the selection range. + /// + /// @param lowerBound The lower bound. + /// @param upperBound The upper bound. + /// + /// @pre The lower bound must be less than or equal to the upper + /// bound. + public void setBounds (double lowerBound, double upperBound) + { + assert (lowerBound < upperBound); + + this.lowerBound = lowerBound; + this.upperBound = upperBound; + + // Make sure that the edge falloff curves do not overlap. + setEdgeFalloff (edgeFalloff); + } + + /// Sets the falloff value at the edge transition. + /// + /// @param edgeFalloff The falloff value at the edge transition. + /// + /// The falloff value is the width of the edge transition at either + /// edge of the selection range. + /// + /// By default, there is an abrupt transition between the values from + /// the two source modules at the boundaries of the selection range. + /// + /// For example, if the selection range is 0.5 to 0.8, and the edge + /// falloff value is 0.1, then the getValue() method outputs: + /// - the output value from the source module with an index value of 0 + /// if the output value from the control module is less than 0.4 + /// ( = 0.5 - 0.1). + /// - a linear blend between the two output values from the two source + /// modules if the output value from the control module is between + /// 0.4 ( = 0.5 - 0.1) and 0.6 ( = 0.5 + 0.1). + /// - the output value from the source module with an index value of 1 + /// if the output value from the control module is between 0.6 + /// ( = 0.5 + 0.1) and 0.7 ( = 0.8 - 0.1). + /// - a linear blend between the output values from the two source + /// modules if the output value from the control module is between + /// 0.7 ( = 0.8 - 0.1 ) and 0.9 ( = 0.8 + 0.1). + /// - the output value from the source module with an index value of 0 + /// if the output value from the control module is greater than 0.9 + /// ( = 0.8 + 0.1). + public void setEdgeFalloff (double edgeFalloff) + { + // Make sure that the edge falloff curves do not overlap. + double boundSize = upperBound - lowerBound; + edgeFalloff = (edgeFalloff > boundSize / 2)? boundSize / 2: edgeFalloff; + } + + /// Returns the control module. + /// + /// @returns A reference to the control module. + /// + /// @pre A control module has been added to this noise module via a + /// call to setSourceModule() or setControlModule(). + /// + /// @throw ExceptionNoModule See the preconditions for more + /// information. + /// + /// The control module determines the output value to select. If the + /// output value from the control module is within a range of values + /// known as the selection range, the getValue() method outputs + /// the value from the source module with an index value of 1. + /// Otherwise, this method outputs the value from the source module + /// with an index value of 0. + + // not sure this does what it says it does. Recheck original source + public ModuleBase getControlModule () throws ExceptionNoModule + { + if (sourceModules == null || sourceModules[2] == null) { + throw new ExceptionNoModule ("Could not retrieve a source module from a noise module."); + } + return (sourceModules[2]); + } + + /// Returns the falloff value at the edge transition. + /// + /// @returns The falloff value at the edge transition. + /// + /// The falloff value is the width of the edge transition at either + /// edge of the selection range. + /// + /// By default, there is an abrupt transition between the output + /// values from the two source modules at the selection-range + /// boundary. + public double getEdgeFalloff () + { + return edgeFalloff; + } + + /// Returns the lower bound of the selection range. + /// + /// @returns The lower bound of the selection range. + /// + /// If the output value from the control module is within the + /// selection range, the getValue() method outputs the value from the + /// source module with an index value of 1. Otherwise, this method + /// outputs the value from the source module with an index value of 0. + public double getLowerBound () + { + return lowerBound; + } + + /// Returns the upper bound of the selection range. + /// + /// @returns The upper bound of the selection range. + /// + /// If the output value from the control module is within the + /// selection range, the getValue() method outputs the value from the + /// source module with an index value of 1. Otherwise, this method + /// outputs the value from the source module with an index value of 0. + public double getUpperBound () + { + return upperBound; + } + + /// Sets the control module. + /// + /// @param controlModule The control module. + /// + /// The control module determines the output value to select. If the + /// output value from the control module is within a range of values + /// known as the selection range, the getValue() method outputs + /// the value from the source module with an index value of 1. + /// Otherwise, this method outputs the value from the source module + /// with an index value of 0. + /// + /// This method assigns the control module an index value of 2. + /// Passing the control module to this method produces the same + /// results as passing the control module to the setSourceModule() + /// method while assigning that noise module an index value of 2. + /// + /// This control module must exist throughout the lifetime of this + /// noise module unless another control module replaces that control + /// module. + public void setControlModule (ModuleBase controlModule) + { + assert (sourceModules != null); + sourceModules[2] = controlModule; + } + +} diff --git a/src/libnoiseforjava/module/Spheres.java b/src/libnoiseforjava/module/Spheres.java index 0ff1fd5..03b580f 100644 --- a/src/libnoiseforjava/module/Spheres.java +++ b/src/libnoiseforjava/module/Spheres.java @@ -1,102 +1,102 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -public class Spheres extends ModuleBase -{ - - /// Noise module that outputs concentric spheres. - /// - /// This noise module outputs concentric spheres centered on the origin - /// like the concentric rings of an onion. - /// - /// The first sphere has a radius of 1.0. Each subsequent sphere has a - /// radius that is 1.0 unit larger than the previous sphere. - /// - /// The output value from this noise module is determined by the distance - /// between the input value and the the nearest spherical surface. The - /// input values that are located on a spherical surface are given the - /// output value 1.0 and the input values that are equidistant from two - /// spherical surfaces are given the output value -1.0. - /// - /// An application can change the frequency of the concentric spheres. - /// Increasing the frequency reduces the distances between spheres. To - /// specify the frequency, call the setFrequency() method. - /// - /// This noise module, modified with some low-frequency, low-power - /// turbulence, is useful for generating agate-like textures. - /// - /// This noise module does not require any source modules. - - /// Default frequency value for the Spheres noise module. - static final double DEFAULT_SPHERES_FREQUENCY = 1.0; - - /// Frequency of the concentric spheres. - double frequency; - - - public Spheres () - { - super(0); - frequency = DEFAULT_SPHERES_FREQUENCY; - } - - public double getValue (double x, double y, double z) - { - x *= frequency; - y *= frequency; - z *= frequency; - - double distFromCenter = Math.sqrt (x * x + y * y + z * z); - double distFromSmallerSphere = distFromCenter - Math.floor (distFromCenter); - double distFromLargerSphere = 1.0 - distFromSmallerSphere; - double nearestDist = Math.min(distFromSmallerSphere, distFromLargerSphere); - return 1.0 - (nearestDist * 4.0); // Puts it in the -1.0 to +1.0 range. - } - - /// Returns the frequency of the concentric spheres. - /// - /// @returns The frequency of the concentric spheres. - /// - /// Increasing the frequency increases the density of the concentric - /// spheres, reducing the distances between them. - public double getFrequency () - { - return frequency; - } - - /// Sets the frequency of the concentric spheres. - /// - /// @param frequency The frequency of the concentric spheres. - /// - /// Increasing the frequency increases the density of the concentric - /// spheres, reducing the distances between them. - public void setFrequency (double frequency) - { - this.frequency = frequency; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +public class Spheres extends ModuleBase +{ + + /// Noise module that outputs concentric spheres. + /// + /// This noise module outputs concentric spheres centered on the origin + /// like the concentric rings of an onion. + /// + /// The first sphere has a radius of 1.0. Each subsequent sphere has a + /// radius that is 1.0 unit larger than the previous sphere. + /// + /// The output value from this noise module is determined by the distance + /// between the input value and the the nearest spherical surface. The + /// input values that are located on a spherical surface are given the + /// output value 1.0 and the input values that are equidistant from two + /// spherical surfaces are given the output value -1.0. + /// + /// An application can change the frequency of the concentric spheres. + /// Increasing the frequency reduces the distances between spheres. To + /// specify the frequency, call the setFrequency() method. + /// + /// This noise module, modified with some low-frequency, low-power + /// turbulence, is useful for generating agate-like textures. + /// + /// This noise module does not require any source modules. + + /// Default frequency value for the Spheres noise module. + static final double DEFAULT_SPHERES_FREQUENCY = 1.0; + + /// Frequency of the concentric spheres. + double frequency; + + + public Spheres () + { + super(0); + frequency = DEFAULT_SPHERES_FREQUENCY; + } + + public double getValue (double x, double y, double z) + { + x *= frequency; + y *= frequency; + z *= frequency; + + double distFromCenter = Math.sqrt (x * x + y * y + z * z); + double distFromSmallerSphere = distFromCenter - Math.floor (distFromCenter); + double distFromLargerSphere = 1.0 - distFromSmallerSphere; + double nearestDist = Math.min(distFromSmallerSphere, distFromLargerSphere); + return 1.0 - (nearestDist * 4.0); // Puts it in the -1.0 to +1.0 range. + } + + /// Returns the frequency of the concentric spheres. + /// + /// @returns The frequency of the concentric spheres. + /// + /// Increasing the frequency increases the density of the concentric + /// spheres, reducing the distances between them. + public double getFrequency () + { + return frequency; + } + + /// Sets the frequency of the concentric spheres. + /// + /// @param frequency The frequency of the concentric spheres. + /// + /// Increasing the frequency increases the density of the concentric + /// spheres, reducing the distances between them. + public void setFrequency (double frequency) + { + this.frequency = frequency; + } + +} diff --git a/src/libnoiseforjava/module/Terrace.java b/src/libnoiseforjava/module/Terrace.java index 0f01019..2c22733 100644 --- a/src/libnoiseforjava/module/Terrace.java +++ b/src/libnoiseforjava/module/Terrace.java @@ -1,326 +1,326 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.Interp; -import libnoiseforjava.Misc; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Terrace extends ModuleBase -{ - - /// Noise module that maps the output value from a source module onto a - /// terrace-forming curve. - /// - /// This noise module maps the output value from the source module onto a - /// terrace-forming curve. The start of this curve has a slope of zero; - /// its slope then smoothly increases. This curve also contains - /// control points which resets the slope to zero at that point, - /// producing a "terracing" effect. Refer to the following illustration: - /// - /// @image html terrace.png - /// - /// To add a control point to this noise module, call the - /// addControlPoint() method. - /// - /// An application must add a minimum of two control points to the curve. - /// If this is not done, the getValue() method fails. The control points - /// can have any value, although no two control points can have the same - /// value. There is no limit to the number of control points that can be - /// added to the curve. - /// - /// This noise module clamps the output value from the source module if - /// that value is less than the value of the lowest control point or - /// greater than the value of the highest control point. - /// - /// This noise module is often used to generate terrain features such as - /// your stereotypical desert canyon. - /// - /// This noise module requires one source module. - - - /// Number of control points stored in this noise module. - int controlPointCount; - - /// Determines if the terrace-forming curve between all control points - /// is inverted. - boolean invertTerraces; - - /// Array that stores the control points. - double [] controlPoints; - - - public Terrace (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - controlPointCount = 0; - invertTerraces = false; - controlPoints = new double [0]; - - } - - /// Adds a control point to the terrace-forming curve. - /// - /// @param value The value of the control point to add. - /// - /// @pre No two control points have the same value. - /// - /// @throw ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// Two or more control points define the terrace-forming curve. The - /// start of this curve has a slope of zero; its slope then smoothly - /// increases. At the control points, its slope resets to zero. - /// - /// It does not matter which order these points are added. - public void addControlPoint (double value) throws ExceptionInvalidParam - { - // Find the insertion point for the new control point and insert the new - // point at that position. The control point array will remain sorted by - // value. - int insertionPos = findInsertionPos (value); - insertAtPos (insertionPos, value); - } - - - /// Deletes all the control points on the terrace-forming curve. - /// - /// @post All control points on the terrace-forming curve are deleted. - public void clearAllControlPoints () - { - controlPoints = null; - controlPointCount = 0; - } - - /// Determines the array index in which to insert the control point - /// into the internal control point array. - /// - /// @param value The value of the control point. - /// - /// @returns The array index in which to insert the control point. - /// - /// @pre No two control points have the same value. - /// - /// @throw ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// By inserting the control point at the returned array index, this - /// class ensures that the control point array is sorted by value. - /// The code that maps a value onto the curve requires a sorted - /// control point array. - public int findInsertionPos (double value) throws ExceptionInvalidParam - { - int insertionPos; - for (insertionPos = 0; insertionPos < controlPointCount; insertionPos++) - { - if (value < controlPoints[insertionPos]) - // We found the array index in which to insert the new control point. - // Exit now. - break; - else if (value == controlPoints[insertionPos]) - // Each control point is required to contain a unique value, so throw - // an exception. - throw new ExceptionInvalidParam ("Invalid Parameter in Terrace Noise Moduled"); - } - return insertionPos; - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - assert (controlPointCount >= 2); - - // Get the output value from the source module. - double sourceModuleValue = sourceModules[0].getValue (x, y, z); - - // Find the first element in the control point array that has a value - // larger than the output value from the source module. - int indexPos; - for (indexPos = 0; indexPos < controlPointCount; indexPos++) - { - if (sourceModuleValue < controlPoints[indexPos]) - break; - - } - - // Find the two nearest control points so that we can map their values - // onto a quadratic curve. - int index0 = Misc.ClampValue (indexPos - 1, 0, controlPointCount - 1); - int index1 = Misc.ClampValue (indexPos, 0, controlPointCount - 1); - - // If some control points are missing (which occurs if the output value from - // the source module is greater than the largest value or less than the - // smallest value of the control point array), get the value of the nearest - // control point and exit now. - if (index0 == index1) - return controlPoints[index1]; - - // Compute the alpha value used for linear interpolation. - double value0 = controlPoints[index0]; - double value1 = controlPoints[index1]; - double alpha = (sourceModuleValue - value0) / (value1 - value0); - if (invertTerraces) - { - alpha = 1.0 - alpha; - double tempValue = value0; - value0 = value1; - value1 = tempValue; - } - - // Squaring the alpha produces the terrace effect. - alpha *= alpha; - - // Now perform the linear interpolation given the alpha value. - return Interp.linearInterp (value0, value1, alpha); - } - - /// Inserts the control point at the specified position in the - /// internal control point array. - /// - /// @param insertionPos The zero-based array position in which to - /// insert the control point. - /// @param value The value of the control point. - /// - /// To make room for this new control point, this method reallocates - /// the control point array and shifts all control points occurring - /// after the insertion position up by one. - /// - /// Because the curve mapping algorithm in this noise module requires - /// that all control points in the array be sorted by value, the new - /// control point should be inserted at the position in which the - /// order is still preserved. - public void insertAtPos (int insertionPos, double value) - { - // Make room for the new control point at the specified position within - // the control point array. The position is determined by the value of - // the control point; the control points must be sorted by value within - // that array. - double[] newControlPoints = new double[controlPointCount + 1]; - - for (int i = 0; i < controlPointCount; i++) - { - if (i < insertionPos) - newControlPoints[i] = controlPoints[i]; - else - newControlPoints[i + 1] = controlPoints[i]; - } - - controlPoints = newControlPoints; - ++controlPointCount; - - // Now that we've made room for the new control point within the array, - // add the new control point. - controlPoints[insertionPos] = value; - } - - /// Creates a number of equally-spaced control points that range from - /// -1 to +1. - /// - /// @param controlPointCount The number of control points to generate. - /// - /// @pre The number of control points must be greater than or equal to - /// 2. - /// - /// @post The previous control points on the terrace-forming curve are - /// deleted. - /// - /// @throw ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// Two or more control points define the terrace-forming curve. The - /// start of this curve has a slope of zero; its slope then smoothly - /// increases. At the control points, its slope resets to zero. - void makeControlPoints (int controlPointCount) throws ExceptionInvalidParam - { - if (controlPointCount < 2) - throw new ExceptionInvalidParam ("Invalid Parameter in Terrace Noise Module"); - - clearAllControlPoints (); - - double terraceStep = 2.0 / ((double)controlPointCount - 1.0); - double curValue = -1.0; - for (int i = 0; i < (int)controlPointCount; i++) - { - addControlPoint (curValue); - curValue += terraceStep; - } - } - - /// Returns a pointer to the array of control points on the - /// terrace-forming curve. - /// - /// @returns A pointer to the array of control points in this noise - /// module. - /// - /// Two or more control points define the terrace-forming curve. The - /// start of this curve has a slope of zero; its slope then smoothly - /// increases. At the control points, its slope resets to zero. - /// - /// Before calling this method, call getControlPointCount() to - /// determine the number of control points in this array. - /// - /// It is recommended that an application does not store this pointer - /// for later use since the pointer to the array may change if the - /// application calls another method of this object. - public double[] getControlPointArray () - { - return controlPoints; - } - - /// Returns the number of control points on the terrace-forming curve. - /// - /// @returns The number of control points on the terrace-forming - /// curve. - public int getControlPointCount () - { - return controlPointCount; - } - - /// Enables or disables the inversion of the terrace-forming curve - /// between the control points. - /// - /// @param invert Specifies whether to invert the curve between the - /// control points. - public void invertTerraces (boolean invert) - { - if (invert) - invertTerraces = invert; - } - - /// Determines if the terrace-forming curve between the control - /// points is inverted. - /// - /// @returns - /// - @a true if the curve between the control points is inverted. - /// - @a false if the curve between the control points is not - /// inverted. - public boolean isTerracesInverted () - { - return invertTerraces; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.Interp; +import libnoiseforjava.Misc; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Terrace extends ModuleBase +{ + + /// Noise module that maps the output value from a source module onto a + /// terrace-forming curve. + /// + /// This noise module maps the output value from the source module onto a + /// terrace-forming curve. The start of this curve has a slope of zero; + /// its slope then smoothly increases. This curve also contains + /// control points which resets the slope to zero at that point, + /// producing a "terracing" effect. Refer to the following illustration: + /// + /// @image html terrace.png + /// + /// To add a control point to this noise module, call the + /// addControlPoint() method. + /// + /// An application must add a minimum of two control points to the curve. + /// If this is not done, the getValue() method fails. The control points + /// can have any value, although no two control points can have the same + /// value. There is no limit to the number of control points that can be + /// added to the curve. + /// + /// This noise module clamps the output value from the source module if + /// that value is less than the value of the lowest control point or + /// greater than the value of the highest control point. + /// + /// This noise module is often used to generate terrain features such as + /// your stereotypical desert canyon. + /// + /// This noise module requires one source module. + + + /// Number of control points stored in this noise module. + int controlPointCount; + + /// Determines if the terrace-forming curve between all control points + /// is inverted. + boolean invertTerraces; + + /// Array that stores the control points. + double [] controlPoints; + + + public Terrace (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + controlPointCount = 0; + invertTerraces = false; + controlPoints = new double [0]; + + } + + /// Adds a control point to the terrace-forming curve. + /// + /// @param value The value of the control point to add. + /// + /// @pre No two control points have the same value. + /// + /// @throw ExceptionInvalidParam An invalid parameter was + /// specified; see the preconditions for more information. + /// + /// Two or more control points define the terrace-forming curve. The + /// start of this curve has a slope of zero; its slope then smoothly + /// increases. At the control points, its slope resets to zero. + /// + /// It does not matter which order these points are added. + public void addControlPoint (double value) throws ExceptionInvalidParam + { + // Find the insertion point for the new control point and insert the new + // point at that position. The control point array will remain sorted by + // value. + int insertionPos = findInsertionPos (value); + insertAtPos (insertionPos, value); + } + + + /// Deletes all the control points on the terrace-forming curve. + /// + /// @post All control points on the terrace-forming curve are deleted. + public void clearAllControlPoints () + { + controlPoints = null; + controlPointCount = 0; + } + + /// Determines the array index in which to insert the control point + /// into the internal control point array. + /// + /// @param value The value of the control point. + /// + /// @returns The array index in which to insert the control point. + /// + /// @pre No two control points have the same value. + /// + /// @throw ExceptionInvalidParam An invalid parameter was + /// specified; see the preconditions for more information. + /// + /// By inserting the control point at the returned array index, this + /// class ensures that the control point array is sorted by value. + /// The code that maps a value onto the curve requires a sorted + /// control point array. + public int findInsertionPos (double value) throws ExceptionInvalidParam + { + int insertionPos; + for (insertionPos = 0; insertionPos < controlPointCount; insertionPos++) + { + if (value < controlPoints[insertionPos]) + // We found the array index in which to insert the new control point. + // Exit now. + break; + else if (value == controlPoints[insertionPos]) + // Each control point is required to contain a unique value, so throw + // an exception. + throw new ExceptionInvalidParam ("Invalid Parameter in Terrace Noise Moduled"); + } + return insertionPos; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (controlPointCount >= 2); + + // Get the output value from the source module. + double sourceModuleValue = sourceModules[0].getValue (x, y, z); + + // Find the first element in the control point array that has a value + // larger than the output value from the source module. + int indexPos; + for (indexPos = 0; indexPos < controlPointCount; indexPos++) + { + if (sourceModuleValue < controlPoints[indexPos]) + break; + + } + + // Find the two nearest control points so that we can map their values + // onto a quadratic curve. + int index0 = Misc.ClampValue (indexPos - 1, 0, controlPointCount - 1); + int index1 = Misc.ClampValue (indexPos, 0, controlPointCount - 1); + + // If some control points are missing (which occurs if the output value from + // the source module is greater than the largest value or less than the + // smallest value of the control point array), get the value of the nearest + // control point and exit now. + if (index0 == index1) + return controlPoints[index1]; + + // Compute the alpha value used for linear interpolation. + double value0 = controlPoints[index0]; + double value1 = controlPoints[index1]; + double alpha = (sourceModuleValue - value0) / (value1 - value0); + if (invertTerraces) + { + alpha = 1.0 - alpha; + double tempValue = value0; + value0 = value1; + value1 = tempValue; + } + + // Squaring the alpha produces the terrace effect. + alpha *= alpha; + + // Now perform the linear interpolation given the alpha value. + return Interp.linearInterp (value0, value1, alpha); + } + + /// Inserts the control point at the specified position in the + /// internal control point array. + /// + /// @param insertionPos The zero-based array position in which to + /// insert the control point. + /// @param value The value of the control point. + /// + /// To make room for this new control point, this method reallocates + /// the control point array and shifts all control points occurring + /// after the insertion position up by one. + /// + /// Because the curve mapping algorithm in this noise module requires + /// that all control points in the array be sorted by value, the new + /// control point should be inserted at the position in which the + /// order is still preserved. + public void insertAtPos (int insertionPos, double value) + { + // Make room for the new control point at the specified position within + // the control point array. The position is determined by the value of + // the control point; the control points must be sorted by value within + // that array. + double[] newControlPoints = new double[controlPointCount + 1]; + + for (int i = 0; i < controlPointCount; i++) + { + if (i < insertionPos) + newControlPoints[i] = controlPoints[i]; + else + newControlPoints[i + 1] = controlPoints[i]; + } + + controlPoints = newControlPoints; + ++controlPointCount; + + // Now that we've made room for the new control point within the array, + // add the new control point. + controlPoints[insertionPos] = value; + } + + /// Creates a number of equally-spaced control points that range from + /// -1 to +1. + /// + /// @param controlPointCount The number of control points to generate. + /// + /// @pre The number of control points must be greater than or equal to + /// 2. + /// + /// @post The previous control points on the terrace-forming curve are + /// deleted. + /// + /// @throw ExceptionInvalidParam An invalid parameter was + /// specified; see the preconditions for more information. + /// + /// Two or more control points define the terrace-forming curve. The + /// start of this curve has a slope of zero; its slope then smoothly + /// increases. At the control points, its slope resets to zero. + void makeControlPoints (int controlPointCount) throws ExceptionInvalidParam + { + if (controlPointCount < 2) + throw new ExceptionInvalidParam ("Invalid Parameter in Terrace Noise Module"); + + clearAllControlPoints (); + + double terraceStep = 2.0 / ((double)controlPointCount - 1.0); + double curValue = -1.0; + for (int i = 0; i < (int)controlPointCount; i++) + { + addControlPoint (curValue); + curValue += terraceStep; + } + } + + /// Returns a pointer to the array of control points on the + /// terrace-forming curve. + /// + /// @returns A pointer to the array of control points in this noise + /// module. + /// + /// Two or more control points define the terrace-forming curve. The + /// start of this curve has a slope of zero; its slope then smoothly + /// increases. At the control points, its slope resets to zero. + /// + /// Before calling this method, call getControlPointCount() to + /// determine the number of control points in this array. + /// + /// It is recommended that an application does not store this pointer + /// for later use since the pointer to the array may change if the + /// application calls another method of this object. + public double[] getControlPointArray () + { + return controlPoints; + } + + /// Returns the number of control points on the terrace-forming curve. + /// + /// @returns The number of control points on the terrace-forming + /// curve. + public int getControlPointCount () + { + return controlPointCount; + } + + /// Enables or disables the inversion of the terrace-forming curve + /// between the control points. + /// + /// @param invert Specifies whether to invert the curve between the + /// control points. + public void invertTerraces (boolean invert) + { + if (invert) + invertTerraces = invert; + } + + /// Determines if the terrace-forming curve between the control + /// points is inverted. + /// + /// @returns + /// - @a true if the curve between the control points is inverted. + /// - @a false if the curve between the control points is not + /// inverted. + public boolean isTerracesInverted () + { + return invertTerraces; + } + +} diff --git a/src/libnoiseforjava/module/TranslatePoint.java b/src/libnoiseforjava/module/TranslatePoint.java index 1f9b845..23ed712 100644 --- a/src/libnoiseforjava/module/TranslatePoint.java +++ b/src/libnoiseforjava/module/TranslatePoint.java @@ -1,192 +1,192 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class TranslatePoint extends ModuleBase -{ - /// Noise module that moves the coordinates of the input value before - /// returning the output value from a source module. - /// - /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates of - /// the input value by a translation amount before returning the output - /// value from the source module. To set the translation amount, call - /// the setTranslation() method. To set the translation amount to - /// apply to the individual @a x, @a y, or @a z coordinates, call the - /// setXTranslation(), setYTranslation() or setZTranslation() methods, - /// respectively. - /// - /// This noise module requires one source module. - - - /// Default translation factor applied to the @a x coordinate for the - /// TranslatePoint noise module. - static final double DEFAULT_TRANSLATE_POINT_X = 0.0; - - /// Default translation factor applied to the @a y coordinate for the - /// TranslatePoint noise module. - static final double DEFAULT_TRANSLATE_POINT_Y = 0.0; - - /// Default translation factor applied to the @a z coordinate for the - /// TranslatePoint noise module. - static final double DEFAULT_TRANSLATE_POINT_Z = 0.0; - - /// Translation amount applied to the @a x coordinate of the input - /// value. - double xTranslation; - - /// Translation amount applied to the @a y coordinate of the input - /// value. - double yTranslation; - - /// Translation amount applied to the @a z coordinate of the input - /// value. - double zTranslation; - - public TranslatePoint (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - xTranslation = DEFAULT_TRANSLATE_POINT_X; - yTranslation = DEFAULT_TRANSLATE_POINT_Y; - zTranslation = DEFAULT_TRANSLATE_POINT_Z; - - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - return sourceModules[0].getValue (x + xTranslation, y + yTranslation, - z + zTranslation); - } - - /// Returns the translation amount to apply to the @a x coordinate of - /// the input value. - /// - /// @returns The translation amount to apply to the @a x coordinate. - public double getXTranslation () - { - return xTranslation; - } - - /// Returns the translation amount to apply to the @a y coordinate of - /// the input value. - /// - /// @returns The translation amount to apply to the @a y coordinate. - public double getYTranslation () - { - return yTranslation; - } - - /// Returns the translation amount to apply to the @a z coordinate of - /// the input value. - /// - /// @returns The translation amount to apply to the @a z coordinate. - public double getZTranslation () - { - return zTranslation; - } - - /// Sets the translation amount to apply to the input value. - /// - /// @param translation The translation amount to apply. - /// - /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates - /// of the input value by a translation amount before returning the - /// output value from the source module - public void setTranslation (double translation) - { - this.xTranslation = translation; - this.yTranslation = translation; - this.zTranslation = translation; - } - - /// Sets the translation amounts to apply to the ( @a x, @a y, @a z ) - /// coordinates of the input value. - /// - /// @param xTranslation The translation amount to apply to the @a x - /// coordinate. - /// @param yTranslation The translation amount to apply to the @a y - /// coordinate. - /// @param zTranslation The translation amount to apply to the @a z - /// coordinate. - /// - /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates - /// of the input value by a translation amount before returning the - /// output value from the source module - public void setTranslation (double xTranslation, double yTranslation, - double zTranslation) - { - this.xTranslation = xTranslation; - this.yTranslation = yTranslation; - this.zTranslation = zTranslation; - } - - /// Sets the translation amount to apply to the @a x coordinate of the - /// input value. - /// - /// @param xTranslation The translation amount to apply to the @a x - /// coordinate. - /// - /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates - /// of the input value by a translation amount before returning the - /// output value from the source module - public void setXTranslation (double xTranslation) - { - this.xTranslation = xTranslation; - } - - /// Sets the translation amount to apply to the @a y coordinate of the - /// input value. - /// - /// @param yTranslation The translation amount to apply to the @a y - /// coordinate. - /// - /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates - /// of the input value by a translation amount before returning the - /// output value from the source module - public void setYTranslation (double yTranslation) - { - this.yTranslation = yTranslation; - } - - /// Sets the translation amount to apply to the @a z coordinate of the - /// input value. - /// - /// @param zTranslation The translation amount to apply to the @a z - /// coordinate. - /// - /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates - /// of the input value by a translation amount before returning the - /// output value from the source module - public void setZTranslation (double zTranslation) - { - this.zTranslation = zTranslation; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class TranslatePoint extends ModuleBase +{ + /// Noise module that moves the coordinates of the input value before + /// returning the output value from a source module. + /// + /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates of + /// the input value by a translation amount before returning the output + /// value from the source module. To set the translation amount, call + /// the setTranslation() method. To set the translation amount to + /// apply to the individual @a x, @a y, or @a z coordinates, call the + /// setXTranslation(), setYTranslation() or setZTranslation() methods, + /// respectively. + /// + /// This noise module requires one source module. + + + /// Default translation factor applied to the @a x coordinate for the + /// TranslatePoint noise module. + static final double DEFAULT_TRANSLATE_POINT_X = 0.0; + + /// Default translation factor applied to the @a y coordinate for the + /// TranslatePoint noise module. + static final double DEFAULT_TRANSLATE_POINT_Y = 0.0; + + /// Default translation factor applied to the @a z coordinate for the + /// TranslatePoint noise module. + static final double DEFAULT_TRANSLATE_POINT_Z = 0.0; + + /// Translation amount applied to the @a x coordinate of the input + /// value. + double xTranslation; + + /// Translation amount applied to the @a y coordinate of the input + /// value. + double yTranslation; + + /// Translation amount applied to the @a z coordinate of the input + /// value. + double zTranslation; + + public TranslatePoint (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + xTranslation = DEFAULT_TRANSLATE_POINT_X; + yTranslation = DEFAULT_TRANSLATE_POINT_Y; + zTranslation = DEFAULT_TRANSLATE_POINT_Z; + + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + return sourceModules[0].getValue (x + xTranslation, y + yTranslation, + z + zTranslation); + } + + /// Returns the translation amount to apply to the @a x coordinate of + /// the input value. + /// + /// @returns The translation amount to apply to the @a x coordinate. + public double getXTranslation () + { + return xTranslation; + } + + /// Returns the translation amount to apply to the @a y coordinate of + /// the input value. + /// + /// @returns The translation amount to apply to the @a y coordinate. + public double getYTranslation () + { + return yTranslation; + } + + /// Returns the translation amount to apply to the @a z coordinate of + /// the input value. + /// + /// @returns The translation amount to apply to the @a z coordinate. + public double getZTranslation () + { + return zTranslation; + } + + /// Sets the translation amount to apply to the input value. + /// + /// @param translation The translation amount to apply. + /// + /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates + /// of the input value by a translation amount before returning the + /// output value from the source module + public void setTranslation (double translation) + { + this.xTranslation = translation; + this.yTranslation = translation; + this.zTranslation = translation; + } + + /// Sets the translation amounts to apply to the ( @a x, @a y, @a z ) + /// coordinates of the input value. + /// + /// @param xTranslation The translation amount to apply to the @a x + /// coordinate. + /// @param yTranslation The translation amount to apply to the @a y + /// coordinate. + /// @param zTranslation The translation amount to apply to the @a z + /// coordinate. + /// + /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates + /// of the input value by a translation amount before returning the + /// output value from the source module + public void setTranslation (double xTranslation, double yTranslation, + double zTranslation) + { + this.xTranslation = xTranslation; + this.yTranslation = yTranslation; + this.zTranslation = zTranslation; + } + + /// Sets the translation amount to apply to the @a x coordinate of the + /// input value. + /// + /// @param xTranslation The translation amount to apply to the @a x + /// coordinate. + /// + /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates + /// of the input value by a translation amount before returning the + /// output value from the source module + public void setXTranslation (double xTranslation) + { + this.xTranslation = xTranslation; + } + + /// Sets the translation amount to apply to the @a y coordinate of the + /// input value. + /// + /// @param yTranslation The translation amount to apply to the @a y + /// coordinate. + /// + /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates + /// of the input value by a translation amount before returning the + /// output value from the source module + public void setYTranslation (double yTranslation) + { + this.yTranslation = yTranslation; + } + + /// Sets the translation amount to apply to the @a z coordinate of the + /// input value. + /// + /// @param zTranslation The translation amount to apply to the @a z + /// coordinate. + /// + /// The getValue() method moves the ( @a x, @a y, @a z ) coordinates + /// of the input value by a translation amount before returning the + /// output value from the source module + public void setZTranslation (double zTranslation) + { + this.zTranslation = zTranslation; + } + +} diff --git a/src/libnoiseforjava/module/Turbulence.java b/src/libnoiseforjava/module/Turbulence.java index acf4cd7..021cff0 100644 --- a/src/libnoiseforjava/module/Turbulence.java +++ b/src/libnoiseforjava/module/Turbulence.java @@ -1,282 +1,282 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class Turbulence extends ModuleBase -{ - /// Noise module that randomly displaces the input value before - /// returning the output value from a source module. - /// - /// @a Turbulence is the pseudo-random displacement of the input value. - /// The getValue() method randomly displaces the ( @a x, @a y, @a z ) - /// coordinates of the input value before retrieving the output value from - /// the source module. To control the turbulence, an application can - /// modify its frequency, its power, and its roughness. - /// - /// The frequency of the turbulence determines how rapidly the - /// displacement amount changes. To specify the frequency, call the - /// setFrequency() method. - /// - /// The power of the turbulence determines the scaling factor that is - /// applied to the displacement amount. To specify the power, call the - /// setPower() method. - /// - /// The roughness of the turbulence determines the roughness of the - /// changes to the displacement amount. Low values smoothly change the - /// displacement amount. High values roughly change the displacement - /// amount, which produces more "kinky" changes. To specify the - /// roughness, call the setRoughness() method. - /// - /// Use of this noise module may require some trial and error. Assuming - /// that you are using a generator module as the source module, you - /// should first: - /// - Set the frequency to the same frequency as the source module. - /// - Set the power to the reciprocal of the frequency. - /// - /// From these initial frequency and power values, modify these values - /// until this noise module produce the desired changes in your terrain or - /// texture. For example: - /// - Low frequency (1/8 initial frequency) and low power (1/8 initial - /// power) produces very minor, almost unnoticeable changes. - /// - Low frequency (1/8 initial frequency) and high power (8 times - /// initial power) produces "ropey" lava-like terrain or marble-like - /// textures. - /// - High frequency (8 times initial frequency) and low power (1/8 - /// initial power) produces a noisy version of the initial terrain or - /// texture. - /// - High frequency (8 times initial frequency) and high power (8 times - /// initial power) produces nearly pure noise, which isn't entirely - /// useful. - /// - /// Displacing the input values result in more realistic terrain and - /// textures. If you are generating elevations for terrain height maps, - /// you can use this noise module to produce more realistic mountain - /// ranges or terrain features that look like flowing lava rock. If you - /// are generating values for textures, you can use this noise module to - /// produce realistic marble-like or "oily" textures. - /// - /// Internally, there are three noise::module::Perlin noise modules - /// that displace the input value; one for the @a x, one for the @a y, - /// and one for the @a z coordinate. - /// - /// This noise module requires one source module. - - /// Default frequency for the Turbulence noise module. - static final double DEFAULT_TURBULENCE_FREQUENCY = Perlin.DEFAULT_PERLIN_FREQUENCY; - - /// Default power for the Turbulence noise module. - static final double DEFAULT_TURBULENCE_POWER = 1.0; - - /// Default roughness for the Turbulence noise module. - static final int DEFAULT_TURBULENCE_ROUGHNESS = 3; - - /// Default noise seed for the Turbulence noise module. - static final int DEFAULT_TURBULENCE_SEED = Perlin.DEFAULT_PERLIN_SEED; - - - /// The power (scale) of the displacement. - double power; - - /// Noise module that displaces the @a x coordinate. - Perlin xDistortModule; - - /// Noise module that displaces the @a y coordinate. - Perlin yDistortModule; - - /// Noise module that displaces the @a z coordinate. - Perlin zDistortModule; - - public Turbulence (ModuleBase sourceModule) throws ExceptionInvalidParam - { - super(1); - setSourceModule(0, sourceModule); - - power = DEFAULT_TURBULENCE_POWER; - - xDistortModule = new Perlin(); - yDistortModule = new Perlin(); - zDistortModule = new Perlin(); - - setSeed(DEFAULT_TURBULENCE_SEED); - setFrequency(DEFAULT_TURBULENCE_FREQUENCY); - setRoughness (DEFAULT_TURBULENCE_ROUGHNESS); - } - - /// Returns the frequency of the turbulence. - /// - /// @returns The frequency of the turbulence. - /// - /// The frequency of the turbulence determines how rapidly the - /// displacement amount changes. - public double getFrequency () - { - // Since each noise::module::Perlin noise module has the same frequency, it - // does not matter which module we use to retrieve the frequency. - return xDistortModule.getFrequency (); - } - - /// Returns the seed value of the internal Perlin-noise modules that - /// are used to displace the input values. - /// - /// @returns The seed value. - /// - /// Internally, there are three Perlin noise modules - /// that displace the input value; one for the @a x, one for the @a y, - /// and one for the @a z coordinate. - public int getSeed () - { - return xDistortModule.getSeed (); - } - - public double getValue (double x, double y, double z) - { - assert (sourceModules[0] != null); - - // Get the values from the three Perlin noise modules and - // add each value to each coordinate of the input value. There are also - // some offsets added to the coordinates of the input values. This prevents - // the distortion modules from returning zero if the (x, y, z) coordinates, - // when multiplied by the frequency, are near an integer boundary. This is - // due to a property of gradient coherent noise, which returns zero at - // integer boundaries. - double x0, y0, z0; - double x1, y1, z1; - double x2, y2, z2; - - x0 = x + (12414.0 / 65536.0); - y0 = y + (65124.0 / 65536.0); - z0 = z + (31337.0 / 65536.0); - x1 = x + (26519.0 / 65536.0); - y1 = y + (18128.0 / 65536.0); - z1 = z + (60493.0 / 65536.0); - x2 = x + (53820.0 / 65536.0); - y2 = y + (11213.0 / 65536.0); - z2 = z + (44845.0 / 65536.0); - - double xDistort = x + (xDistortModule.getValue (x0, y0, z0) - * power); - double yDistort = y + (yDistortModule.getValue (x1, y1, z1) - * power); - double zDistort = z + (zDistortModule.getValue (x2, y2, z2) - * power); - - // Retrieve the output value at the offsetted input value instead of the - // original input value. - return sourceModules[0].getValue (xDistort, yDistort, zDistort); - } - - /// Sets the seed value of the internal noise modules that are used to - /// displace the input values. - /// - /// @param seed The seed value. - /// - /// Internally, there are three Perlin noise modules - /// that displace the input value; one for the @a x, one for the @a y, - /// and one for the @a z coordinate. This noise module assigns the - /// following seed values to the Perlin noise modules: - /// - It assigns the seed value (@a seed + 0) to the @a x noise module. - /// - It assigns the seed value (@a seed + 1) to the @a y noise module. - /// - It assigns the seed value (@a seed + 2) to the @a z noise module. - /// This is done to prevent any sort of weird artifacting. - public void setSeed (int seed) - { - xDistortModule.setSeed (seed); - yDistortModule.setSeed (seed + 1); - zDistortModule.setSeed (seed + 2); - } - - /// Returns the power of the turbulence. - /// - /// @returns The power of the turbulence. - /// - /// The power of the turbulence determines the scaling factor that is - /// applied to the displacement amount. - public double getPower () - { - return power; - } - - /// Returns the roughness of the turbulence. - /// - /// @returns The roughness of the turbulence. - /// - /// The roughness of the turbulence determines the roughness of the - /// changes to the displacement amount. Low values smoothly change - /// the displacement amount. High values roughly change the - /// displacement amount, which produces more "kinky" changes. - public int getRoughnessCount () - { - return xDistortModule.getOctaveCount (); - } - - /// Sets the frequency of the turbulence. - /// - /// @param frequency The frequency of the turbulence. - /// - /// The frequency of the turbulence determines how rapidly the - /// displacement amount changes. - public void setFrequency (double frequency) - { - xDistortModule.setFrequency (frequency); - yDistortModule.setFrequency (frequency); - zDistortModule.setFrequency (frequency); - } - - /// Sets the power of the turbulence. - /// - /// @param power The power of the turbulence. - /// - /// The power of the turbulence determines the scaling factor that is - /// applied to the displacement amount. - public void setPower (double power) - { - this.power = power; - } - - /// Sets the roughness of the turbulence. - /// - /// @param roughness The roughness of the turbulence. - /// - /// The roughness of the turbulence determines the roughness of the - /// changes to the displacement amount. Low values smoothly change - /// the displacement amount. High values roughly change the - /// displacement amount, which produces more "kinky" changes. - /// - /// Internally, there are three Perlin noise modules - /// that displace the input value; one for the @a x, one for the @a y, - /// and one for the @a z coordinate. The roughness value is equal to - /// the number of octaves used by the noise::module::Perlin noise - /// modules. - public void setRoughness (int roughness) throws ExceptionInvalidParam - { - xDistortModule.setOctaveCount (roughness); - yDistortModule.setOctaveCount (roughness); - zDistortModule.setOctaveCount (roughness); - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class Turbulence extends ModuleBase +{ + /// Noise module that randomly displaces the input value before + /// returning the output value from a source module. + /// + /// @a Turbulence is the pseudo-random displacement of the input value. + /// The getValue() method randomly displaces the ( @a x, @a y, @a z ) + /// coordinates of the input value before retrieving the output value from + /// the source module. To control the turbulence, an application can + /// modify its frequency, its power, and its roughness. + /// + /// The frequency of the turbulence determines how rapidly the + /// displacement amount changes. To specify the frequency, call the + /// setFrequency() method. + /// + /// The power of the turbulence determines the scaling factor that is + /// applied to the displacement amount. To specify the power, call the + /// setPower() method. + /// + /// The roughness of the turbulence determines the roughness of the + /// changes to the displacement amount. Low values smoothly change the + /// displacement amount. High values roughly change the displacement + /// amount, which produces more "kinky" changes. To specify the + /// roughness, call the setRoughness() method. + /// + /// Use of this noise module may require some trial and error. Assuming + /// that you are using a generator module as the source module, you + /// should first: + /// - Set the frequency to the same frequency as the source module. + /// - Set the power to the reciprocal of the frequency. + /// + /// From these initial frequency and power values, modify these values + /// until this noise module produce the desired changes in your terrain or + /// texture. For example: + /// - Low frequency (1/8 initial frequency) and low power (1/8 initial + /// power) produces very minor, almost unnoticeable changes. + /// - Low frequency (1/8 initial frequency) and high power (8 times + /// initial power) produces "ropey" lava-like terrain or marble-like + /// textures. + /// - High frequency (8 times initial frequency) and low power (1/8 + /// initial power) produces a noisy version of the initial terrain or + /// texture. + /// - High frequency (8 times initial frequency) and high power (8 times + /// initial power) produces nearly pure noise, which isn't entirely + /// useful. + /// + /// Displacing the input values result in more realistic terrain and + /// textures. If you are generating elevations for terrain height maps, + /// you can use this noise module to produce more realistic mountain + /// ranges or terrain features that look like flowing lava rock. If you + /// are generating values for textures, you can use this noise module to + /// produce realistic marble-like or "oily" textures. + /// + /// Internally, there are three noise::module::Perlin noise modules + /// that displace the input value; one for the @a x, one for the @a y, + /// and one for the @a z coordinate. + /// + /// This noise module requires one source module. + + /// Default frequency for the Turbulence noise module. + static final double DEFAULT_TURBULENCE_FREQUENCY = Perlin.DEFAULT_PERLIN_FREQUENCY; + + /// Default power for the Turbulence noise module. + static final double DEFAULT_TURBULENCE_POWER = 1.0; + + /// Default roughness for the Turbulence noise module. + static final int DEFAULT_TURBULENCE_ROUGHNESS = 3; + + /// Default noise seed for the Turbulence noise module. + static final int DEFAULT_TURBULENCE_SEED = Perlin.DEFAULT_PERLIN_SEED; + + + /// The power (scale) of the displacement. + double power; + + /// Noise module that displaces the @a x coordinate. + Perlin xDistortModule; + + /// Noise module that displaces the @a y coordinate. + Perlin yDistortModule; + + /// Noise module that displaces the @a z coordinate. + Perlin zDistortModule; + + public Turbulence (ModuleBase sourceModule) throws ExceptionInvalidParam + { + super(1); + setSourceModule(0, sourceModule); + + power = DEFAULT_TURBULENCE_POWER; + + xDistortModule = new Perlin(); + yDistortModule = new Perlin(); + zDistortModule = new Perlin(); + + setSeed(DEFAULT_TURBULENCE_SEED); + setFrequency(DEFAULT_TURBULENCE_FREQUENCY); + setRoughness (DEFAULT_TURBULENCE_ROUGHNESS); + } + + /// Returns the frequency of the turbulence. + /// + /// @returns The frequency of the turbulence. + /// + /// The frequency of the turbulence determines how rapidly the + /// displacement amount changes. + public double getFrequency () + { + // Since each noise::module::Perlin noise module has the same frequency, it + // does not matter which module we use to retrieve the frequency. + return xDistortModule.getFrequency (); + } + + /// Returns the seed value of the internal Perlin-noise modules that + /// are used to displace the input values. + /// + /// @returns The seed value. + /// + /// Internally, there are three Perlin noise modules + /// that displace the input value; one for the @a x, one for the @a y, + /// and one for the @a z coordinate. + public int getSeed () + { + return xDistortModule.getSeed (); + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + + // Get the values from the three Perlin noise modules and + // add each value to each coordinate of the input value. There are also + // some offsets added to the coordinates of the input values. This prevents + // the distortion modules from returning zero if the (x, y, z) coordinates, + // when multiplied by the frequency, are near an integer boundary. This is + // due to a property of gradient coherent noise, which returns zero at + // integer boundaries. + double x0, y0, z0; + double x1, y1, z1; + double x2, y2, z2; + + x0 = x + (12414.0 / 65536.0); + y0 = y + (65124.0 / 65536.0); + z0 = z + (31337.0 / 65536.0); + x1 = x + (26519.0 / 65536.0); + y1 = y + (18128.0 / 65536.0); + z1 = z + (60493.0 / 65536.0); + x2 = x + (53820.0 / 65536.0); + y2 = y + (11213.0 / 65536.0); + z2 = z + (44845.0 / 65536.0); + + double xDistort = x + (xDistortModule.getValue (x0, y0, z0) + * power); + double yDistort = y + (yDistortModule.getValue (x1, y1, z1) + * power); + double zDistort = z + (zDistortModule.getValue (x2, y2, z2) + * power); + + // Retrieve the output value at the offsetted input value instead of the + // original input value. + return sourceModules[0].getValue (xDistort, yDistort, zDistort); + } + + /// Sets the seed value of the internal noise modules that are used to + /// displace the input values. + /// + /// @param seed The seed value. + /// + /// Internally, there are three Perlin noise modules + /// that displace the input value; one for the @a x, one for the @a y, + /// and one for the @a z coordinate. This noise module assigns the + /// following seed values to the Perlin noise modules: + /// - It assigns the seed value (@a seed + 0) to the @a x noise module. + /// - It assigns the seed value (@a seed + 1) to the @a y noise module. + /// - It assigns the seed value (@a seed + 2) to the @a z noise module. + /// This is done to prevent any sort of weird artifacting. + public void setSeed (int seed) + { + xDistortModule.setSeed (seed); + yDistortModule.setSeed (seed + 1); + zDistortModule.setSeed (seed + 2); + } + + /// Returns the power of the turbulence. + /// + /// @returns The power of the turbulence. + /// + /// The power of the turbulence determines the scaling factor that is + /// applied to the displacement amount. + public double getPower () + { + return power; + } + + /// Returns the roughness of the turbulence. + /// + /// @returns The roughness of the turbulence. + /// + /// The roughness of the turbulence determines the roughness of the + /// changes to the displacement amount. Low values smoothly change + /// the displacement amount. High values roughly change the + /// displacement amount, which produces more "kinky" changes. + public int getRoughnessCount () + { + return xDistortModule.getOctaveCount (); + } + + /// Sets the frequency of the turbulence. + /// + /// @param frequency The frequency of the turbulence. + /// + /// The frequency of the turbulence determines how rapidly the + /// displacement amount changes. + public void setFrequency (double frequency) + { + xDistortModule.setFrequency (frequency); + yDistortModule.setFrequency (frequency); + zDistortModule.setFrequency (frequency); + } + + /// Sets the power of the turbulence. + /// + /// @param power The power of the turbulence. + /// + /// The power of the turbulence determines the scaling factor that is + /// applied to the displacement amount. + public void setPower (double power) + { + this.power = power; + } + + /// Sets the roughness of the turbulence. + /// + /// @param roughness The roughness of the turbulence. + /// + /// The roughness of the turbulence determines the roughness of the + /// changes to the displacement amount. Low values smoothly change + /// the displacement amount. High values roughly change the + /// displacement amount, which produces more "kinky" changes. + /// + /// Internally, there are three Perlin noise modules + /// that displace the input value; one for the @a x, one for the @a y, + /// and one for the @a z coordinate. The roughness value is equal to + /// the number of octaves used by the noise::module::Perlin noise + /// modules. + public void setRoughness (int roughness) throws ExceptionInvalidParam + { + xDistortModule.setOctaveCount (roughness); + yDistortModule.setOctaveCount (roughness); + zDistortModule.setOctaveCount (roughness); + } + +} diff --git a/src/libnoiseforjava/module/Voronoi.java b/src/libnoiseforjava/module/Voronoi.java index 4be23de..79776c1 100644 --- a/src/libnoiseforjava/module/Voronoi.java +++ b/src/libnoiseforjava/module/Voronoi.java @@ -1,283 +1,283 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.module; - -import libnoiseforjava.NoiseGen; - -public class Voronoi extends ModuleBase -{ - - /// Noise module that outputs Voronoi cells. - /// - /// In mathematics, a Voronoi cell is a region containing all the - /// points that are closer to a specific seed point than to any - /// other seed point. These cells mesh with one another, producing - /// polygon-like formations. - /// - /// By default, this noise module randomly places a seed point within - /// each unit cube. By modifying the frequency of the seed points, - /// an application can change the distance between seed points. The - /// higher the frequency, the closer together this noise module places - /// the seed points, which reduces the size of the cells. To specify the - /// frequency of the cells, call the setFrequency() method. - /// - /// This noise module assigns each Voronoi cell with a random constant - /// value from a coherent-noise function. The displacement value - /// controls the range of random values to assign to each cell. The - /// range of random values is +/- the displacement value. Call the - /// setDisplacement() method to specify the displacement value. - /// - /// To modify the random positions of the seed points, call the SetSeed() - /// method. - /// - /// This noise module can optionally add the distance from the nearest - /// seed to the output value. To enable this feature, call the - /// enableDistance() method. This causes the points in the Voronoi cells - /// to increase in value the further away that point is from the nearest - /// seed point. - /// - /// Voronoi cells are often used to generate cracked-mud terrain - /// formations or crystal-like textures - /// - /// This noise module requires no source modules. - - - /// Default displacement to apply to each cell for the - /// Voronoi noise module. - final static double DEFAULT_VORONOI_DISPLACEMENT = 1.0; - - /// Default frequency of the seed points for the Voronoi - /// noise module. - final static double DEFAULT_VORONOI_FREQUENCY = 1.0; - - /// Default seed of the noise function for the Voronoi - /// noise module. - final static int DEFAULT_VORONOI_SEED = 0; - - private static final double SQRT_3 = 1.7320508075688772935; - - - /// Scale of the random displacement to apply to each Voronoi cell. - double displacement; - - /// Determines if the distance from the nearest seed point is applied to - /// the output value. - boolean enableDistance; - - /// Frequency of the seed points. - double frequency; - - /// Seed value used by the coherent-noise function to determine the - /// positions of the seed points. - int seed; - - - public Voronoi () - { - super(0); - displacement = DEFAULT_VORONOI_DISPLACEMENT; - enableDistance = false; - frequency = DEFAULT_VORONOI_FREQUENCY; - seed = DEFAULT_VORONOI_SEED; - } - - public double getValue (double x, double y, double z) - { - // This method could be more efficient by caching the seed values. Fix - // later. - - x *= frequency; - y *= frequency; - z *= frequency; - - int xInt = (x > 0.0? (int)x: (int)x - 1); - int yInt = (y > 0.0? (int)y: (int)y - 1); - int zInt = (z > 0.0? (int)z: (int)z - 1); - - double minDist = 2147483647.0; - double xCandidate = 0; - double yCandidate = 0; - double zCandidate = 0; - - // Inside each unit cube, there is a seed point at a random position. Go - // through each of the nearby cubes until we find a cube with a seed point - // that is closest to the specified position. - for (int zCur = zInt - 2; zCur <= zInt + 2; zCur++) - { - for (int yCur = yInt - 2; yCur <= yInt + 2; yCur++) - { - for (int xCur = xInt - 2; xCur <= xInt + 2; xCur++) - { - // Calculate the position and distance to the seed point inside of - // this unit cube. - double xPos = xCur + NoiseGen.ValueNoise3D (xCur, yCur, zCur, seed); - double yPos = yCur + NoiseGen.ValueNoise3D (xCur, yCur, zCur, seed + 1); - double zPos = zCur + NoiseGen.ValueNoise3D (xCur, yCur, zCur, seed + 2); - double xDist = xPos - x; - double yDist = yPos - y; - double zDist = zPos - z; - double dist = xDist * xDist + yDist * yDist + zDist * zDist; - - if (dist < minDist) - { - // This seed point is closer to any others found so far, so record - // this seed point. - minDist = dist; - xCandidate = xPos; - yCandidate = yPos; - zCandidate = zPos; - } - } - } - } - - double value; - if (enableDistance) - { - // Determine the distance to the nearest seed point. - double xDist = xCandidate - x; - double yDist = yCandidate - y; - double zDist = zCandidate - z; - value = (Math.sqrt(xDist * xDist + yDist * yDist + zDist * zDist) - ) * SQRT_3 - 1.0; - } else { - value = 0.0; - } - - // Return the calculated distance with the displacement value applied. - return value + (displacement * (double)NoiseGen.ValueNoise3D ( - (int)(Math.floor (xCandidate)), - (int)(Math.floor (yCandidate)), - (int)(Math.floor (zCandidate)), seed));// added seed here, not in original - // but there isn't a working method - // without seed - } - - /// Enables or disables applying the distance from the nearest seed - /// point to the output value. - /// - /// @param enable Specifies whether to apply the distance to the - /// output value or not. - /// - /// Applying the distance from the nearest seed point to the output - /// value causes the points in the Voronoi cells to increase in value - /// the further away that point is from the nearest seed point. - /// Setting this value to @a true (and setting the displacement to a - /// near-zero value) causes this noise module to generate cracked mud - /// formations. - public void enableDistance (boolean enable) - { - enableDistance = enable; - } - - /// Returns the displacement value of the Voronoi cells. - /// - /// @returns The displacement value of the Voronoi cells. - /// - /// This noise module assigns each Voronoi cell with a random constant - /// value from a coherent-noise function. The displacement - /// value controls the range of random values to assign to each - /// cell. The range of random values is +/- the displacement value. - public double getDisplacement () - { - return displacement; - } - - /// Returns the frequency of the seed points. - /// - /// @returns The frequency of the seed points. - /// - /// The frequency determines the size of the Voronoi cells and the - /// distance between these cells. - public double GetFrequency () - { - return frequency; - } - - /// Returns the seed value used by the Voronoi cells - /// - /// @returns The seed value. - /// - /// The positions of the seed values are calculated by a - /// coherent-noise function. By modifying the seed value, the output - /// of that function changes. - public int getSeed () - { - return seed; - } - - /// Determines if the distance from the nearest seed point is applied - /// to the output value. - /// - /// @returns - /// - @a true if the distance is applied to the output value. - /// - @a false if not. - /// - /// Applying the distance from the nearest seed point to the output - /// value causes the points in the Voronoi cells to increase in value - /// the further away that point is from the nearest seed point. - public boolean IsDistanceEnabled () - { - return enableDistance; - } - - /// Sets the displacement value of the Voronoi cells. - /// - /// @param displacement The displacement value of the Voronoi cells. - /// - /// This noise module assigns each Voronoi cell with a random constant - /// value from a coherent-noise function. The displacement - /// value controls the range of random values to assign to each - /// cell. The range of random values is +/- the displacement value. - public void setDisplacement (double displacement) - { - this.displacement = displacement; - } - - /// Sets the frequency of the seed points. - /// - /// @param frequency The frequency of the seed points. - /// - /// The frequency determines the size of the Voronoi cells and the - /// distance between these cells. - public void setFrequency (double frequency) - { - this.frequency = frequency; - } - - /// Sets the seed value used by the Voronoi cells - /// - /// @param seed The seed value. - /// - /// The positions of the seed values are calculated by a - /// coherent-noise function. By modifying the seed value, the output - /// of that function changes. - public void setSeed (int seed) - { - this.seed = seed; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.module; + +import libnoiseforjava.NoiseGen; + +public class Voronoi extends ModuleBase +{ + + /// Noise module that outputs Voronoi cells. + /// + /// In mathematics, a Voronoi cell is a region containing all the + /// points that are closer to a specific seed point than to any + /// other seed point. These cells mesh with one another, producing + /// polygon-like formations. + /// + /// By default, this noise module randomly places a seed point within + /// each unit cube. By modifying the frequency of the seed points, + /// an application can change the distance between seed points. The + /// higher the frequency, the closer together this noise module places + /// the seed points, which reduces the size of the cells. To specify the + /// frequency of the cells, call the setFrequency() method. + /// + /// This noise module assigns each Voronoi cell with a random constant + /// value from a coherent-noise function. The displacement value + /// controls the range of random values to assign to each cell. The + /// range of random values is +/- the displacement value. Call the + /// setDisplacement() method to specify the displacement value. + /// + /// To modify the random positions of the seed points, call the SetSeed() + /// method. + /// + /// This noise module can optionally add the distance from the nearest + /// seed to the output value. To enable this feature, call the + /// enableDistance() method. This causes the points in the Voronoi cells + /// to increase in value the further away that point is from the nearest + /// seed point. + /// + /// Voronoi cells are often used to generate cracked-mud terrain + /// formations or crystal-like textures + /// + /// This noise module requires no source modules. + + + /// Default displacement to apply to each cell for the + /// Voronoi noise module. + final static double DEFAULT_VORONOI_DISPLACEMENT = 1.0; + + /// Default frequency of the seed points for the Voronoi + /// noise module. + final static double DEFAULT_VORONOI_FREQUENCY = 1.0; + + /// Default seed of the noise function for the Voronoi + /// noise module. + final static int DEFAULT_VORONOI_SEED = 0; + + private static final double SQRT_3 = 1.7320508075688772935; + + + /// Scale of the random displacement to apply to each Voronoi cell. + double displacement; + + /// Determines if the distance from the nearest seed point is applied to + /// the output value. + boolean enableDistance; + + /// Frequency of the seed points. + double frequency; + + /// Seed value used by the coherent-noise function to determine the + /// positions of the seed points. + int seed; + + + public Voronoi () + { + super(0); + displacement = DEFAULT_VORONOI_DISPLACEMENT; + enableDistance = false; + frequency = DEFAULT_VORONOI_FREQUENCY; + seed = DEFAULT_VORONOI_SEED; + } + + public double getValue (double x, double y, double z) + { + // This method could be more efficient by caching the seed values. Fix + // later. + + x *= frequency; + y *= frequency; + z *= frequency; + + int xInt = (x > 0.0? (int)x: (int)x - 1); + int yInt = (y > 0.0? (int)y: (int)y - 1); + int zInt = (z > 0.0? (int)z: (int)z - 1); + + double minDist = 2147483647.0; + double xCandidate = 0; + double yCandidate = 0; + double zCandidate = 0; + + // Inside each unit cube, there is a seed point at a random position. Go + // through each of the nearby cubes until we find a cube with a seed point + // that is closest to the specified position. + for (int zCur = zInt - 2; zCur <= zInt + 2; zCur++) + { + for (int yCur = yInt - 2; yCur <= yInt + 2; yCur++) + { + for (int xCur = xInt - 2; xCur <= xInt + 2; xCur++) + { + // Calculate the position and distance to the seed point inside of + // this unit cube. + double xPos = xCur + NoiseGen.ValueNoise3D (xCur, yCur, zCur, seed); + double yPos = yCur + NoiseGen.ValueNoise3D (xCur, yCur, zCur, seed + 1); + double zPos = zCur + NoiseGen.ValueNoise3D (xCur, yCur, zCur, seed + 2); + double xDist = xPos - x; + double yDist = yPos - y; + double zDist = zPos - z; + double dist = xDist * xDist + yDist * yDist + zDist * zDist; + + if (dist < minDist) + { + // This seed point is closer to any others found so far, so record + // this seed point. + minDist = dist; + xCandidate = xPos; + yCandidate = yPos; + zCandidate = zPos; + } + } + } + } + + double value; + if (enableDistance) + { + // Determine the distance to the nearest seed point. + double xDist = xCandidate - x; + double yDist = yCandidate - y; + double zDist = zCandidate - z; + value = (Math.sqrt(xDist * xDist + yDist * yDist + zDist * zDist) + ) * SQRT_3 - 1.0; + } else { + value = 0.0; + } + + // Return the calculated distance with the displacement value applied. + return value + (displacement * (double)NoiseGen.ValueNoise3D ( + (int)(Math.floor (xCandidate)), + (int)(Math.floor (yCandidate)), + (int)(Math.floor (zCandidate)), seed));// added seed here, not in original + // but there isn't a working method + // without seed + } + + /// Enables or disables applying the distance from the nearest seed + /// point to the output value. + /// + /// @param enable Specifies whether to apply the distance to the + /// output value or not. + /// + /// Applying the distance from the nearest seed point to the output + /// value causes the points in the Voronoi cells to increase in value + /// the further away that point is from the nearest seed point. + /// Setting this value to @a true (and setting the displacement to a + /// near-zero value) causes this noise module to generate cracked mud + /// formations. + public void enableDistance (boolean enable) + { + enableDistance = enable; + } + + /// Returns the displacement value of the Voronoi cells. + /// + /// @returns The displacement value of the Voronoi cells. + /// + /// This noise module assigns each Voronoi cell with a random constant + /// value from a coherent-noise function. The displacement + /// value controls the range of random values to assign to each + /// cell. The range of random values is +/- the displacement value. + public double getDisplacement () + { + return displacement; + } + + /// Returns the frequency of the seed points. + /// + /// @returns The frequency of the seed points. + /// + /// The frequency determines the size of the Voronoi cells and the + /// distance between these cells. + public double GetFrequency () + { + return frequency; + } + + /// Returns the seed value used by the Voronoi cells + /// + /// @returns The seed value. + /// + /// The positions of the seed values are calculated by a + /// coherent-noise function. By modifying the seed value, the output + /// of that function changes. + public int getSeed () + { + return seed; + } + + /// Determines if the distance from the nearest seed point is applied + /// to the output value. + /// + /// @returns + /// - @a true if the distance is applied to the output value. + /// - @a false if not. + /// + /// Applying the distance from the nearest seed point to the output + /// value causes the points in the Voronoi cells to increase in value + /// the further away that point is from the nearest seed point. + public boolean IsDistanceEnabled () + { + return enableDistance; + } + + /// Sets the displacement value of the Voronoi cells. + /// + /// @param displacement The displacement value of the Voronoi cells. + /// + /// This noise module assigns each Voronoi cell with a random constant + /// value from a coherent-noise function. The displacement + /// value controls the range of random values to assign to each + /// cell. The range of random values is +/- the displacement value. + public void setDisplacement (double displacement) + { + this.displacement = displacement; + } + + /// Sets the frequency of the seed points. + /// + /// @param frequency The frequency of the seed points. + /// + /// The frequency determines the size of the Voronoi cells and the + /// distance between these cells. + public void setFrequency (double frequency) + { + this.frequency = frequency; + } + + /// Sets the seed value used by the Voronoi cells + /// + /// @param seed The seed value. + /// + /// The positions of the seed values are calculated by a + /// coherent-noise function. By modifying the seed value, the output + /// of that function changes. + public void setSeed (int seed) + { + this.seed = seed; + } + +} diff --git a/src/libnoiseforjava/util/ColorCafe.java b/src/libnoiseforjava/util/ColorCafe.java index 08cb4ab..0b98471 100644 --- a/src/libnoiseforjava/util/ColorCafe.java +++ b/src/libnoiseforjava/util/ColorCafe.java @@ -1,105 +1,105 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -public class ColorCafe -{ - /// Defines a color. - /// - /// A color object contains four 8-bit channels: red, green, blue, and an - /// alpha (transparency) channel. Channel values range from 0 to 255. - /// - /// The alpha channel defines the transparency of the color. If the alpha - /// channel has a value of 0, the color is completely transparent. If the - /// alpha channel has a value of 255, the color is completely opaque. - - /// Value of the alpha (transparency) channel. - int alpha; - - /// Value of the blue channel. - int blue; - - /// Value of the green channel. - int green; - - /// Value of the red channel. - int red; - - /// Constructor. - /// - /// @param r Value of the red channel. - /// @param g Value of the green channel. - /// @param b Value of the blue channel. - /// @param a Value of the alpha (transparency) channel. - public ColorCafe (int red, int green, int blue, int alpha) - { - this.red = red; - this.blue = blue; - this.green = green; - this.alpha = alpha; - } - - public int getAlpha() - { - return alpha; - } - - public int getBlue() - { - return blue; - } - - public int getGreen() - { - return green; - } - - public int getRed() - { - return red; - } - - public void setAlpha(int alpha) - { - this.alpha = alpha; - } - - public void setBlue(int blue) - { - this.blue = blue; - } - - public void setGreen(int green) - { - this.green = green; - } - - public void setRed(int red) - { - this.red = red; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +public class ColorCafe +{ + /// Defines a color. + /// + /// A color object contains four 8-bit channels: red, green, blue, and an + /// alpha (transparency) channel. Channel values range from 0 to 255. + /// + /// The alpha channel defines the transparency of the color. If the alpha + /// channel has a value of 0, the color is completely transparent. If the + /// alpha channel has a value of 255, the color is completely opaque. + + /// Value of the alpha (transparency) channel. + int alpha; + + /// Value of the blue channel. + int blue; + + /// Value of the green channel. + int green; + + /// Value of the red channel. + int red; + + /// Constructor. + /// + /// @param r Value of the red channel. + /// @param g Value of the green channel. + /// @param b Value of the blue channel. + /// @param a Value of the alpha (transparency) channel. + public ColorCafe (int red, int green, int blue, int alpha) + { + this.red = red; + this.blue = blue; + this.green = green; + this.alpha = alpha; + } + + public int getAlpha() + { + return alpha; + } + + public int getBlue() + { + return blue; + } + + public int getGreen() + { + return green; + } + + public int getRed() + { + return red; + } + + public void setAlpha(int alpha) + { + this.alpha = alpha; + } + + public void setBlue(int blue) + { + this.blue = blue; + } + + public void setGreen(int green) + { + this.green = green; + } + + public void setRed(int red) + { + this.red = red; + } + +} diff --git a/src/libnoiseforjava/util/GradientColor.java b/src/libnoiseforjava/util/GradientColor.java index d9f5736..ee241f4 100644 --- a/src/libnoiseforjava/util/GradientColor.java +++ b/src/libnoiseforjava/util/GradientColor.java @@ -1,251 +1,251 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.Misc; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class GradientColor -{ - /// Defines a color gradient. - /// - /// A color gradient is a list of gradually-changing colors. A color - /// gradient is defined by a list of gradient points. Each - /// gradient point has a position and a color. In a color gradient, the - /// colors between two adjacent gradient points are linearly interpolated. - /// - /// To add a gradient point to the color gradient, pass its position and - /// color to the addGradientPoint() method. - /// - /// To retrieve a color from a specific position in the color gradient, - /// pass that position to the getColor() method. - /// - /// This class is a useful tool for coloring height maps based on - /// elevation. - /// - /// Gradient example - /// - /// Suppose a gradient object contains the following gradient points: - /// - -1.0 maps to black. - /// - 0.0 maps to white. - /// - 1.0 maps to red. - /// - /// If an application passes -0.5 to the getColor() method, this method - /// will return a gray color that is halfway between black and white. - /// - /// If an application passes 0.25 to the getColor() method, this method - /// will return a very light pink color that is one quarter of the way - /// between white and red. - - - GradientPoint [] gradientPoints; - int gradientPointCount; - ColorCafe workingColor; - - public GradientColor() - { - gradientPoints = new GradientPoint[1]; - gradientPoints[0] = new GradientPoint(0.0, new ColorCafe(0, 0, 0, 0)); - } - - /// Adds a gradient point to this gradient object. - /// - /// @param gradientPos The position of this gradient point. - /// @param gradientColor The color of this gradient point. - /// - /// @pre No two gradient points have the same position. - /// - /// @throw noise::ExceptionInvalidParam See the precondition. - /// - /// It does not matter which order these gradient points are added. - public void addGradientPoint (double gradientPos, ColorCafe gradientColor) throws ExceptionInvalidParam - { - // Find the insertion point for the new gradient point and insert the new - // gradient point at that insertion point. The gradient point array will - // remain sorted by gradient position. - int insertionPos = findInsertionPos (gradientPos); - insertAtPos (insertionPos, gradientPos, gradientColor); - } - - /// Deletes all the gradient points from this gradient object. - /// - /// @post All gradient points from this gradient object are deleted. - public void clear () - { - gradientPoints = null; - gradientPointCount = 0; - } - - /// Determines the array index in which to insert the gradient point - /// into the internal gradient-point array. - /// - /// @param gradientPos The position of this gradient point. - /// - /// @returns The array index in which to insert the gradient point. - /// - /// @pre No two gradient points have the same input value. - /// - /// @throw noise::ExceptionInvalidParam See the precondition. - /// - /// By inserting the gradient point at the returned array index, this - /// object ensures that the gradient-point array is sorted by input - /// value. The code that maps a value to a color requires a sorted - /// gradient-point array. - public int findInsertionPos (double gradientPos) throws ExceptionInvalidParam - { - int insertionPos; - for (insertionPos = 0; insertionPos < gradientPointCount; - insertionPos++) { - if (gradientPos < gradientPoints[insertionPos].position) { - // We found the array index in which to insert the new gradient point. - // Exit now. - break; - } else if (gradientPos == gradientPoints[insertionPos].position) { - // Each gradient point is required to contain a unique gradient - // position, so throw an exception. - throw new ExceptionInvalidParam ("Invalid Parameter in Gradient Color"); - } - } - return insertionPos; - } - - /// Returns the color at the specified position in the color gradient. - /// - /// @param gradientPos The specified position. - /// - /// @returns The color at that position. - public ColorCafe getColor (double gradientPos) - { - assert (gradientPointCount >= 2); - - // Find the first element in the gradient point array that has a gradient - // position larger than the gradient position passed to this method. - int indexPos; - for (indexPos = 0; indexPos < gradientPointCount; indexPos++) - { - if (gradientPos < gradientPoints[indexPos].position) - break; - } - - // Find the two nearest gradient points so that we can perform linear - // interpolation on the color. - int index0 = Misc.ClampValue (indexPos - 1, 0, gradientPointCount - 1); - int index1 = Misc.ClampValue (indexPos, 0, gradientPointCount - 1); - - // If some gradient points are missing (which occurs if the gradient - // position passed to this method is greater than the largest gradient - // position or less than the smallest gradient position in the array), get - // the corresponding gradient color of the nearest gradient point and exit - // now. - if (index0 == index1) - { - workingColor = gradientPoints[index1].color; - return workingColor; - } - - // Compute the alpha value used for linear interpolation. - double input0 = gradientPoints[index0].position; - double input1 = gradientPoints[index1].position; - double alpha = (gradientPos - input0) / (input1 - input0); - - // Now perform the linear interpolation given the alpha value. - ColorCafe color0 = gradientPoints[index0].color; - ColorCafe color1 = gradientPoints[index1].color; - workingColor = MiscUtilities.linearInterpColor (color0, color1, (float)alpha); - return workingColor; - } - - /// Inserts the gradient point at the specified position in the - /// internal gradient-point array. - /// - /// @param insertionPos The zero-based array position in which to - /// insert the gradient point. - /// @param gradientPos The position of this gradient point. - /// @param gradientColor The color of this gradient point. - /// - /// To make room for this new gradient point, this method reallocates - /// the gradient-point array and shifts all gradient points occurring - /// after the insertion position up by one. - /// - /// Because this object requires that all gradient points in the array - /// must be sorted by the position, the new gradient point should be - /// inserted at the position in which the order is still preserved. - public void insertAtPos (int insertionPos, double gradientPos, - ColorCafe gradientColor) - { - // Make room for the new gradient point at the specified insertion position - // within the gradient point array. The insertion position is determined by - // the gradient point's position; the gradient points must be sorted by - // gradient position within that array. - GradientPoint [] newGradientPoints; - newGradientPoints = new GradientPoint[gradientPointCount + 1]; - - for (int t = 0; t < (gradientPointCount + 1); t++) - newGradientPoints[t] = new GradientPoint(); - - - - for (int i = 0; i < gradientPointCount; i++) - { - if (i < insertionPos) - newGradientPoints[i] = gradientPoints[i]; - else - newGradientPoints[i + 1] = gradientPoints[i]; - } - - gradientPoints = newGradientPoints; - ++gradientPointCount; - - // Now that we've made room for the new gradient point within the array, add - // the new gradient point. - gradientPoints[insertionPos].position = gradientPos; - gradientPoints[insertionPos].color = gradientColor; - } - - /// Returns a pointer to the array of gradient points in this object. - /// - /// @returns A pointer to the array of gradient points. - /// - /// Before calling this method, call getGradientPointCount() to - /// determine the number of gradient points in this array. - /// - /// It is recommended that an application does not store this pointer - /// for later use since the pointer to the array may change if the - /// application calls another method of this object. - public GradientPoint[] getGradientPointArray () - { - return gradientPoints; - } - - /// Returns the number of gradient points stored in this object. - /// - /// @returns The number of gradient points stored in this object. - public int getGradientPointCount () - { - return gradientPointCount; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.Misc; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class GradientColor +{ + /// Defines a color gradient. + /// + /// A color gradient is a list of gradually-changing colors. A color + /// gradient is defined by a list of gradient points. Each + /// gradient point has a position and a color. In a color gradient, the + /// colors between two adjacent gradient points are linearly interpolated. + /// + /// To add a gradient point to the color gradient, pass its position and + /// color to the addGradientPoint() method. + /// + /// To retrieve a color from a specific position in the color gradient, + /// pass that position to the getColor() method. + /// + /// This class is a useful tool for coloring height maps based on + /// elevation. + /// + /// Gradient example + /// + /// Suppose a gradient object contains the following gradient points: + /// - -1.0 maps to black. + /// - 0.0 maps to white. + /// - 1.0 maps to red. + /// + /// If an application passes -0.5 to the getColor() method, this method + /// will return a gray color that is halfway between black and white. + /// + /// If an application passes 0.25 to the getColor() method, this method + /// will return a very light pink color that is one quarter of the way + /// between white and red. + + + GradientPoint [] gradientPoints; + int gradientPointCount; + ColorCafe workingColor; + + public GradientColor() + { + gradientPoints = new GradientPoint[1]; + gradientPoints[0] = new GradientPoint(0.0, new ColorCafe(0, 0, 0, 0)); + } + + /// Adds a gradient point to this gradient object. + /// + /// @param gradientPos The position of this gradient point. + /// @param gradientColor The color of this gradient point. + /// + /// @pre No two gradient points have the same position. + /// + /// @throw noise::ExceptionInvalidParam See the precondition. + /// + /// It does not matter which order these gradient points are added. + public void addGradientPoint (double gradientPos, ColorCafe gradientColor) throws ExceptionInvalidParam + { + // Find the insertion point for the new gradient point and insert the new + // gradient point at that insertion point. The gradient point array will + // remain sorted by gradient position. + int insertionPos = findInsertionPos (gradientPos); + insertAtPos (insertionPos, gradientPos, gradientColor); + } + + /// Deletes all the gradient points from this gradient object. + /// + /// @post All gradient points from this gradient object are deleted. + public void clear () + { + gradientPoints = null; + gradientPointCount = 0; + } + + /// Determines the array index in which to insert the gradient point + /// into the internal gradient-point array. + /// + /// @param gradientPos The position of this gradient point. + /// + /// @returns The array index in which to insert the gradient point. + /// + /// @pre No two gradient points have the same input value. + /// + /// @throw noise::ExceptionInvalidParam See the precondition. + /// + /// By inserting the gradient point at the returned array index, this + /// object ensures that the gradient-point array is sorted by input + /// value. The code that maps a value to a color requires a sorted + /// gradient-point array. + public int findInsertionPos (double gradientPos) throws ExceptionInvalidParam + { + int insertionPos; + for (insertionPos = 0; insertionPos < gradientPointCount; + insertionPos++) { + if (gradientPos < gradientPoints[insertionPos].position) { + // We found the array index in which to insert the new gradient point. + // Exit now. + break; + } else if (gradientPos == gradientPoints[insertionPos].position) { + // Each gradient point is required to contain a unique gradient + // position, so throw an exception. + throw new ExceptionInvalidParam ("Invalid Parameter in Gradient Color"); + } + } + return insertionPos; + } + + /// Returns the color at the specified position in the color gradient. + /// + /// @param gradientPos The specified position. + /// + /// @returns The color at that position. + public ColorCafe getColor (double gradientPos) + { + assert (gradientPointCount >= 2); + + // Find the first element in the gradient point array that has a gradient + // position larger than the gradient position passed to this method. + int indexPos; + for (indexPos = 0; indexPos < gradientPointCount; indexPos++) + { + if (gradientPos < gradientPoints[indexPos].position) + break; + } + + // Find the two nearest gradient points so that we can perform linear + // interpolation on the color. + int index0 = Misc.ClampValue (indexPos - 1, 0, gradientPointCount - 1); + int index1 = Misc.ClampValue (indexPos, 0, gradientPointCount - 1); + + // If some gradient points are missing (which occurs if the gradient + // position passed to this method is greater than the largest gradient + // position or less than the smallest gradient position in the array), get + // the corresponding gradient color of the nearest gradient point and exit + // now. + if (index0 == index1) + { + workingColor = gradientPoints[index1].color; + return workingColor; + } + + // Compute the alpha value used for linear interpolation. + double input0 = gradientPoints[index0].position; + double input1 = gradientPoints[index1].position; + double alpha = (gradientPos - input0) / (input1 - input0); + + // Now perform the linear interpolation given the alpha value. + ColorCafe color0 = gradientPoints[index0].color; + ColorCafe color1 = gradientPoints[index1].color; + workingColor = MiscUtilities.linearInterpColor (color0, color1, (float)alpha); + return workingColor; + } + + /// Inserts the gradient point at the specified position in the + /// internal gradient-point array. + /// + /// @param insertionPos The zero-based array position in which to + /// insert the gradient point. + /// @param gradientPos The position of this gradient point. + /// @param gradientColor The color of this gradient point. + /// + /// To make room for this new gradient point, this method reallocates + /// the gradient-point array and shifts all gradient points occurring + /// after the insertion position up by one. + /// + /// Because this object requires that all gradient points in the array + /// must be sorted by the position, the new gradient point should be + /// inserted at the position in which the order is still preserved. + public void insertAtPos (int insertionPos, double gradientPos, + ColorCafe gradientColor) + { + // Make room for the new gradient point at the specified insertion position + // within the gradient point array. The insertion position is determined by + // the gradient point's position; the gradient points must be sorted by + // gradient position within that array. + GradientPoint [] newGradientPoints; + newGradientPoints = new GradientPoint[gradientPointCount + 1]; + + for (int t = 0; t < (gradientPointCount + 1); t++) + newGradientPoints[t] = new GradientPoint(); + + + + for (int i = 0; i < gradientPointCount; i++) + { + if (i < insertionPos) + newGradientPoints[i] = gradientPoints[i]; + else + newGradientPoints[i + 1] = gradientPoints[i]; + } + + gradientPoints = newGradientPoints; + ++gradientPointCount; + + // Now that we've made room for the new gradient point within the array, add + // the new gradient point. + gradientPoints[insertionPos].position = gradientPos; + gradientPoints[insertionPos].color = gradientColor; + } + + /// Returns a pointer to the array of gradient points in this object. + /// + /// @returns A pointer to the array of gradient points. + /// + /// Before calling this method, call getGradientPointCount() to + /// determine the number of gradient points in this array. + /// + /// It is recommended that an application does not store this pointer + /// for later use since the pointer to the array may change if the + /// application calls another method of this object. + public GradientPoint[] getGradientPointArray () + { + return gradientPoints; + } + + /// Returns the number of gradient points stored in this object. + /// + /// @returns The number of gradient points stored in this object. + public int getGradientPointCount () + { + return gradientPointCount; + } + +} diff --git a/src/libnoiseforjava/util/GradientPoint.java b/src/libnoiseforjava/util/GradientPoint.java index 8711d67..b48dab6 100644 --- a/src/libnoiseforjava/util/GradientPoint.java +++ b/src/libnoiseforjava/util/GradientPoint.java @@ -1,74 +1,74 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -public class GradientPoint -{ - /// Defines a point used to build a color gradient. - /// - /// A color gradient is a list of gradually-changing colors. A color - /// gradient is defined by a list of gradient points. Each - /// gradient point has a position and a color. In a color gradient, the - /// colors between two adjacent gradient points are linearly interpolated. - /// - /// The ColorGradient class defines a color gradient by a list of these - /// objects. - - double position; - ColorCafe color; - - public GradientPoint() - { - position = 0.0; - color = new ColorCafe(0,0,0,0); - } - - public GradientPoint(double position, ColorCafe color) - { - this.position = position; - this.color = color; - } - - public double getPosition() - { - return position; - } - - public ColorCafe getColor() - { - return color; - } - - public void setPosition(double position) - { - this.position = position; - } - - public void setColor(ColorCafe color) - { - this.color = color; - } -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +public class GradientPoint +{ + /// Defines a point used to build a color gradient. + /// + /// A color gradient is a list of gradually-changing colors. A color + /// gradient is defined by a list of gradient points. Each + /// gradient point has a position and a color. In a color gradient, the + /// colors between two adjacent gradient points are linearly interpolated. + /// + /// The ColorGradient class defines a color gradient by a list of these + /// objects. + + double position; + ColorCafe color; + + public GradientPoint() + { + position = 0.0; + color = new ColorCafe(0,0,0,0); + } + + public GradientPoint(double position, ColorCafe color) + { + this.position = position; + this.color = color; + } + + public double getPosition() + { + return position; + } + + public ColorCafe getColor() + { + return color; + } + + public void setPosition(double position) + { + this.position = position; + } + + public void setColor(ColorCafe color) + { + this.color = color; + } +} diff --git a/src/libnoiseforjava/util/ImageCafe.java b/src/libnoiseforjava/util/ImageCafe.java index 192e93d..746e700 100644 --- a/src/libnoiseforjava/util/ImageCafe.java +++ b/src/libnoiseforjava/util/ImageCafe.java @@ -1,173 +1,173 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class ImageCafe -{ - - /// Implements an image, a 2-dimensional array of color values. - /// - /// An image can be used to store a color texture. - /// - /// These color values are of type ColorCafe. - /// - /// The size (width and height) of the image can be specified during - /// object construction. - /// - /// The getValue() and setValue() methods can be used to access individual - /// color values stored in the image. - /// - /// - /// Border Values - /// - /// All of the color values outside of the image are assumed to have a - /// common color value known as the border value. - /// - /// To set the border value, call the setBorderValue() method. - /// - /// The getValue() method returns the border value if the specified - /// position lies outside of the image. - /// - - /// The Color value used for all positions outside of the image. - ColorCafe borderValue; - - /// The current height of the image. - int height; - - /// The current width of the image. - int width; - - /// Array of ColorCafes holding the color values - ColorCafe [][] imageCafeColors; - - public ImageCafe (int width, int height) throws ExceptionInvalidParam - { - setSize (width, height); - borderValue = new ColorCafe (0, 0, 0, 0); - imageCafeColors = new ColorCafe[width][height]; - } - - /// Returns a color value from the specified position in the image. - /// - /// @param x The x coordinate of the position. - /// @param y The y coordinate of the position. - /// - /// @returns The color value at that position. - /// - /// This method returns the border value if the coordinates exist - /// outside of the image. - public ColorCafe getValue (int x, int y) - { - if (x >= 0 && x < width && y >= 0 && y < height) - return imageCafeColors[x][y]; - else - // The coordinates specified are outside the image. Return the border - // value. - return borderValue; - } - - /// Sets the new size for the image. - /// - /// @param width The new width for the image. - /// @param height The new height for the image. - /// - /// @pre The width and height values are positive. - /// @pre The width and height values do not exceed the maximum - /// possible width and height for the image. - /// - /// @throw ExceptionInvalidParam See the preconditions. - public void setSize (int width, int height) throws ExceptionInvalidParam - { - if (width < 0 || height < 0) - // Invalid width or height. - throw new ExceptionInvalidParam ("Invalid Parameter in ImageCafe"); - else - { - this.width = width; - this.height = height; - } - } - - /// Sets a color value at a specified position in the image. - /// - /// @param x The x coordinate of the position. - /// @param y The y coordinate of the position. - /// @param value The color value to set at the given position. - /// - /// This method does nothing if the image is empty or the position is - /// outside the bounds of the image. - public void setValue (int x, int y, ColorCafe value) - { - if (x >= 0 && x < width && y >= 0 && y < height) - this.imageCafeColors[x][y] = value; - } - - - /// Returns the color value used for all positions outside of the - /// image. - /// - /// @returns The color value used for all positions outside of the - /// image. - /// - /// All positions outside of the image are assumed to have a common - /// color value known as the border value. - public ColorCafe getBorderValue () - { - return borderValue; - } - - /// Returns the height of the image. - /// - /// @returns The height of the image. - public int getHeight () - { - return height; - } - - /// Returns the width of the image. - /// - /// @returns The width of the image. - public int getWidth () - { - return width; - } - - /// Sets the color value to use for all positions outside of the - /// image. - /// - /// @param borderValue The color value to use for all positions - /// outside of the image. - /// - /// All positions outside of the image are assumed to have a common - /// color value known as the border value. - public void setBorderValue (ColorCafe borderValue) - { - this.borderValue = borderValue; - } -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class ImageCafe +{ + + /// Implements an image, a 2-dimensional array of color values. + /// + /// An image can be used to store a color texture. + /// + /// These color values are of type ColorCafe. + /// + /// The size (width and height) of the image can be specified during + /// object construction. + /// + /// The getValue() and setValue() methods can be used to access individual + /// color values stored in the image. + /// + /// + /// Border Values + /// + /// All of the color values outside of the image are assumed to have a + /// common color value known as the border value. + /// + /// To set the border value, call the setBorderValue() method. + /// + /// The getValue() method returns the border value if the specified + /// position lies outside of the image. + /// + + /// The Color value used for all positions outside of the image. + ColorCafe borderValue; + + /// The current height of the image. + int height; + + /// The current width of the image. + int width; + + /// Array of ColorCafes holding the color values + ColorCafe [][] imageCafeColors; + + public ImageCafe (int width, int height) throws ExceptionInvalidParam + { + setSize (width, height); + borderValue = new ColorCafe (0, 0, 0, 0); + imageCafeColors = new ColorCafe[width][height]; + } + + /// Returns a color value from the specified position in the image. + /// + /// @param x The x coordinate of the position. + /// @param y The y coordinate of the position. + /// + /// @returns The color value at that position. + /// + /// This method returns the border value if the coordinates exist + /// outside of the image. + public ColorCafe getValue (int x, int y) + { + if (x >= 0 && x < width && y >= 0 && y < height) + return imageCafeColors[x][y]; + else + // The coordinates specified are outside the image. Return the border + // value. + return borderValue; + } + + /// Sets the new size for the image. + /// + /// @param width The new width for the image. + /// @param height The new height for the image. + /// + /// @pre The width and height values are positive. + /// @pre The width and height values do not exceed the maximum + /// possible width and height for the image. + /// + /// @throw ExceptionInvalidParam See the preconditions. + public void setSize (int width, int height) throws ExceptionInvalidParam + { + if (width < 0 || height < 0) + // Invalid width or height. + throw new ExceptionInvalidParam ("Invalid Parameter in ImageCafe"); + else + { + this.width = width; + this.height = height; + } + } + + /// Sets a color value at a specified position in the image. + /// + /// @param x The x coordinate of the position. + /// @param y The y coordinate of the position. + /// @param value The color value to set at the given position. + /// + /// This method does nothing if the image is empty or the position is + /// outside the bounds of the image. + public void setValue (int x, int y, ColorCafe value) + { + if (x >= 0 && x < width && y >= 0 && y < height) + this.imageCafeColors[x][y] = value; + } + + + /// Returns the color value used for all positions outside of the + /// image. + /// + /// @returns The color value used for all positions outside of the + /// image. + /// + /// All positions outside of the image are assumed to have a common + /// color value known as the border value. + public ColorCafe getBorderValue () + { + return borderValue; + } + + /// Returns the height of the image. + /// + /// @returns The height of the image. + public int getHeight () + { + return height; + } + + /// Returns the width of the image. + /// + /// @returns The width of the image. + public int getWidth () + { + return width; + } + + /// Sets the color value to use for all positions outside of the + /// image. + /// + /// @param borderValue The color value to use for all positions + /// outside of the image. + /// + /// All positions outside of the image are assumed to have a common + /// color value known as the border value. + public void setBorderValue (ColorCafe borderValue) + { + this.borderValue = borderValue; + } +} diff --git a/src/libnoiseforjava/util/MiscUtilities.java b/src/libnoiseforjava/util/MiscUtilities.java index 6b0c1c3..a18aa6b 100644 --- a/src/libnoiseforjava/util/MiscUtilities.java +++ b/src/libnoiseforjava/util/MiscUtilities.java @@ -1,52 +1,52 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -public class MiscUtilities -{ - - // Performs linear interpolation between two 8-bit channel values. - public static short blendChannel (int red, int red2, float alpha) - { - double c0 = (float)red / 255.0; - double c1 = (float)red2 / 255.0; - return (short)(((c1 * alpha) + (c0 * (1.0f - alpha))) * 255.0f); - } - - // Performs linear interpolation between two colors - public static ColorCafe linearInterpColor (ColorCafe color0, ColorCafe color1, - float alpha) - { - ColorCafe color = new ColorCafe( - blendChannel (color0.red, color1.red, alpha), - blendChannel (color0.green, color1.green, alpha), - blendChannel (color0.blue, color1.blue, alpha), - blendChannel (color0.alpha, color1.alpha, alpha) - ); - return color; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +public class MiscUtilities +{ + + // Performs linear interpolation between two 8-bit channel values. + public static short blendChannel (int red, int red2, float alpha) + { + double c0 = (float)red / 255.0; + double c1 = (float)red2 / 255.0; + return (short)(((c1 * alpha) + (c0 * (1.0f - alpha))) * 255.0f); + } + + // Performs linear interpolation between two colors + public static ColorCafe linearInterpColor (ColorCafe color0, ColorCafe color1, + float alpha) + { + ColorCafe color = new ColorCafe( + blendChannel (color0.red, color1.red, alpha), + blendChannel (color0.green, color1.green, alpha), + blendChannel (color0.blue, color1.blue, alpha), + blendChannel (color0.alpha, color1.alpha, alpha) + ); + return color; + } + +} diff --git a/src/libnoiseforjava/util/NoiseMap.java b/src/libnoiseforjava/util/NoiseMap.java index 551270b..07f71bd 100644 --- a/src/libnoiseforjava/util/NoiseMap.java +++ b/src/libnoiseforjava/util/NoiseMap.java @@ -1,150 +1,150 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class NoiseMap -{ - /// Implements a noise map, a 2-dimensional array of floating-point - /// values. - /// - /// A noise map is designed to store coherent-noise values generated by a - /// noise module, although it can store values from any source. A noise - /// map is often used as a terrain height map or a grayscale texture. - /// - /// The size (width and height) of the noise map can be specified during - /// object construction. - /// - /// The getValue() method can be used to access individual - /// values stored in the noise map. - /// - - - /// The height of the noise map. - int height; - - /// The width of the noise map. - int width; - - /// The array of doubles holding the noise map values - double [] [] noiseMap; - - double borderValue; - - public NoiseMap (int width, int height) throws ExceptionInvalidParam - { - setSize (width, height); - noiseMap = new double [width][height]; - borderValue = 0.0; - } - - /// Returns a value from the specified position in the noise map. - /// - /// @param x The x coordinate of the position. - /// @param y The y coordinate of the position. - /// - /// @returns The value at that position. - /// - /// This method returns the border value if the coordinates exist - /// outside of the noise map. - public double getValue (int x, int y) - { - if (x >= 0 && x < width && y >= 0 && y < height) - return noiseMap[x] [y]; - // The coordinates specified are outside the noise map. Return the border - // value. - else - return borderValue; - } - - public void setSize (int width, int height) throws ExceptionInvalidParam - { - if (width < 1 || height < 1) - // Invalid width or height. - throw new ExceptionInvalidParam ("Invalid parameter in NoiseMap"); - else - { - this.width = width; - this.height = height; - } - } - - /// Sets a value at a specified position in the noise map. - /// - /// @param x The x coordinate of the position. - /// @param y The y coordinate of the position. - /// @param value The value to set at the given position. - /// - /// This method does nothing if the noise map object is empty or the - /// position is outside the bounds of the noise map. - public void setValue (int x, int y, double value) - { - if (x >= 0 && x < width && y >= 0 && y < height) - this.noiseMap[x][y]= value; - } - - /// Returns the value used for all positions outside of the noise map. - /// - /// @returns The value used for all positions outside of the noise - /// map. - /// - /// All positions outside of the noise map are assumed to have a - /// common value known as the border value. - public double getBorderValue () - { - return borderValue; - } - - /// Returns the height of the noise map. - /// - /// @returns The height of the noise map. - public int getHeight () - { - return height; - } - - /// Returns the width of the noise map. - /// - /// @returns The width of the noise map. - public int getWidth () - { - return width; - } - - /// Sets the value to use for all positions outside of the noise map. - /// - /// @param borderValue The value to use for all positions outside of - /// the noise map. - /// - /// All positions outside of the noise map are assumed to have a - /// common value known as the border value. - public void setBorderValue (double borderValue) - { - this.borderValue = borderValue; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class NoiseMap +{ + /// Implements a noise map, a 2-dimensional array of floating-point + /// values. + /// + /// A noise map is designed to store coherent-noise values generated by a + /// noise module, although it can store values from any source. A noise + /// map is often used as a terrain height map or a grayscale texture. + /// + /// The size (width and height) of the noise map can be specified during + /// object construction. + /// + /// The getValue() method can be used to access individual + /// values stored in the noise map. + /// + + + /// The height of the noise map. + int height; + + /// The width of the noise map. + int width; + + /// The array of doubles holding the noise map values + double [] [] noiseMap; + + double borderValue; + + public NoiseMap (int width, int height) throws ExceptionInvalidParam + { + setSize (width, height); + noiseMap = new double [width][height]; + borderValue = 0.0; + } + + /// Returns a value from the specified position in the noise map. + /// + /// @param x The x coordinate of the position. + /// @param y The y coordinate of the position. + /// + /// @returns The value at that position. + /// + /// This method returns the border value if the coordinates exist + /// outside of the noise map. + public double getValue (int x, int y) + { + if (x >= 0 && x < width && y >= 0 && y < height) + return noiseMap[x] [y]; + // The coordinates specified are outside the noise map. Return the border + // value. + else + return borderValue; + } + + public void setSize (int width, int height) throws ExceptionInvalidParam + { + if (width < 1 || height < 1) + // Invalid width or height. + throw new ExceptionInvalidParam ("Invalid parameter in NoiseMap"); + else + { + this.width = width; + this.height = height; + } + } + + /// Sets a value at a specified position in the noise map. + /// + /// @param x The x coordinate of the position. + /// @param y The y coordinate of the position. + /// @param value The value to set at the given position. + /// + /// This method does nothing if the noise map object is empty or the + /// position is outside the bounds of the noise map. + public void setValue (int x, int y, double value) + { + if (x >= 0 && x < width && y >= 0 && y < height) + this.noiseMap[x][y]= value; + } + + /// Returns the value used for all positions outside of the noise map. + /// + /// @returns The value used for all positions outside of the noise + /// map. + /// + /// All positions outside of the noise map are assumed to have a + /// common value known as the border value. + public double getBorderValue () + { + return borderValue; + } + + /// Returns the height of the noise map. + /// + /// @returns The height of the noise map. + public int getHeight () + { + return height; + } + + /// Returns the width of the noise map. + /// + /// @returns The width of the noise map. + public int getWidth () + { + return width; + } + + /// Sets the value to use for all positions outside of the noise map. + /// + /// @param borderValue The value to use for all positions outside of + /// the noise map. + /// + /// All positions outside of the noise map are assumed to have a + /// common value known as the border value. + public void setBorderValue (double borderValue) + { + this.borderValue = borderValue; + } + +} diff --git a/src/libnoiseforjava/util/NoiseMapBuilder.java b/src/libnoiseforjava/util/NoiseMapBuilder.java index d7873de..a091226 100644 --- a/src/libnoiseforjava/util/NoiseMapBuilder.java +++ b/src/libnoiseforjava/util/NoiseMapBuilder.java @@ -1,195 +1,195 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.module.ModuleBase; - -public class NoiseMapBuilder -{ - - /// Base class for a noise-map builder - /// - /// A builder class builds a noise map by filling it with coherent-noise - /// values generated from the surface of a three-dimensional mathematical - /// object. Each builder class defines a specific three-dimensional - /// surface, such as a cylinder, sphere, or plane. - /// - /// A builder class describes these input values using a coordinate system - /// applicable for the mathematical object (e.g., a latitude/longitude - /// coordinate system for the spherical noise-map builder.) It then - /// "flattens" these coordinates onto a plane so that it can write the - /// coherent-noise values into a two-dimensional noise map. - /// - /// Building the Noise Map - /// - /// To build the noise map, perform the following steps: - /// - Pass the bounding coordinates to the setBounds() method. - /// - Pass the noise map size, in points, to the setDestSize() method. - /// - Pass a NoiseMap object to the setDestNoiseMap() method. - /// - Pass a noise module (derived from ModuleBase) to the - /// setSourceModule() method. - /// - Call the build() method. - /// - /// Note that setBounds() is not defined in the base class; it is - /// only defined in the derived classes. This is because each model uses - /// a different coordinate system. - - - /// Count of rows completed - int callback; - - /// Height of the destination noise map, in points. - int destHeight; - - /// Width of the destination noise map, in points. - int destWidth; - - /// Destination noise map that will contain the coherent-noise values. - NoiseMap destNoiseMap; - - /// Source noise module that will generate the coherent-noise values. - ModuleBase sourceModule; - - - public NoiseMapBuilder () throws ExceptionInvalidParam - { - callback = 0; - destHeight = 0; - destWidth = 0; - destNoiseMap = new NoiseMap(1,1); - sourceModule = new ModuleBase(0); - } - - public NoiseMapBuilder (int height, int width) throws ExceptionInvalidParam - { - callback = 0; - destHeight = 0; - destWidth = 0; - destNoiseMap = new NoiseMap(height,width); - sourceModule = new ModuleBase(0); - } - - /// Builds the noise map. - /// - /// @pre setBounds() was previously called. - /// @pre setDestNoiseMap() was previously called. - /// @pre setSourceModule() was previously called. - /// @pre The width and height values specified by setDestSize() are - /// positive. - /// @pre The width and height values specified by setDestSize() do not - /// exceed the maximum possible width and height for the noise map. - /// - /// @post The original contents of the destination noise map is - /// destroyed. - /// - /// @throw ExceptionInvalidParam See the preconditions. - /// @throw ExceptionOutOfMemory Out of memory. - /// - /// If this method is successful, the destination noise map contains - /// the coherent-noise values from the noise module specified by - /// setSourceModule(). - public void build () throws ExceptionInvalidParam - { - //override in child classes - } - - /// Returns the height of the destination noise map. - /// - /// @returns The height of the destination noise map, in points. - /// - /// This object does not change the height in the destination noise - /// map object until the build() method is called. - public double getDestHeight () - { - return destHeight; - } - - /// Returns the width of the destination noise map. - /// - /// @returns The width of the destination noise map, in points. - /// - /// This object does not change the height in the destination noise - /// map object until the build() method is called. - public double getDestWidth () - { - return destWidth; - } - - /// Sets the destination noise map. - /// - /// @param destNoiseMap The destination noise map. - /// - /// The destination noise map will contain the coherent-noise values - /// from this noise map after a successful call to the build() method. - /// - /// The destination noise map must exist throughout the lifetime of - /// this object unless another noise map replaces that noise map. - public void setDestNoiseMap (NoiseMap destNoiseMap) - { - this.destNoiseMap = destNoiseMap; - } - - /// Sets the source module. - /// - /// @param sourceModule The source module. - /// - /// This object fills in a noise map with the coherent-noise values - /// from this source module. - /// - /// The source module must exist throughout the lifetime of this - /// object unless another noise module replaces that noise module. - public void setSourceModule (ModuleBase sourceModule) - { - this.sourceModule = sourceModule; - } - - /// Sets the size of the destination noise map. - /// - /// @param destWidth The width of the destination noise map, in - /// points. - /// @param destHeight The height of the destination noise map, in - /// points. - /// - /// This method does not change the size of the destination noise map - /// until the build() method is called. - public void setDestSize (int destWidth, int destHeight) - { - this.destWidth = destWidth ; - this.destHeight = destHeight; - } - - public NoiseMap getDestNoiseMap() - { - return destNoiseMap; - } - - void setCallback (int callback) - { - this.callback = callback; - } - +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.module.ModuleBase; + +public class NoiseMapBuilder +{ + + /// Base class for a noise-map builder + /// + /// A builder class builds a noise map by filling it with coherent-noise + /// values generated from the surface of a three-dimensional mathematical + /// object. Each builder class defines a specific three-dimensional + /// surface, such as a cylinder, sphere, or plane. + /// + /// A builder class describes these input values using a coordinate system + /// applicable for the mathematical object (e.g., a latitude/longitude + /// coordinate system for the spherical noise-map builder.) It then + /// "flattens" these coordinates onto a plane so that it can write the + /// coherent-noise values into a two-dimensional noise map. + /// + /// Building the Noise Map + /// + /// To build the noise map, perform the following steps: + /// - Pass the bounding coordinates to the setBounds() method. + /// - Pass the noise map size, in points, to the setDestSize() method. + /// - Pass a NoiseMap object to the setDestNoiseMap() method. + /// - Pass a noise module (derived from ModuleBase) to the + /// setSourceModule() method. + /// - Call the build() method. + /// + /// Note that setBounds() is not defined in the base class; it is + /// only defined in the derived classes. This is because each model uses + /// a different coordinate system. + + + /// Count of rows completed + int callback; + + /// Height of the destination noise map, in points. + int destHeight; + + /// Width of the destination noise map, in points. + int destWidth; + + /// Destination noise map that will contain the coherent-noise values. + NoiseMap destNoiseMap; + + /// Source noise module that will generate the coherent-noise values. + ModuleBase sourceModule; + + + public NoiseMapBuilder () throws ExceptionInvalidParam + { + callback = 0; + destHeight = 0; + destWidth = 0; + destNoiseMap = new NoiseMap(1,1); + sourceModule = new ModuleBase(0); + } + + public NoiseMapBuilder (int height, int width) throws ExceptionInvalidParam + { + callback = 0; + destHeight = 0; + destWidth = 0; + destNoiseMap = new NoiseMap(height,width); + sourceModule = new ModuleBase(0); + } + + /// Builds the noise map. + /// + /// @pre setBounds() was previously called. + /// @pre setDestNoiseMap() was previously called. + /// @pre setSourceModule() was previously called. + /// @pre The width and height values specified by setDestSize() are + /// positive. + /// @pre The width and height values specified by setDestSize() do not + /// exceed the maximum possible width and height for the noise map. + /// + /// @post The original contents of the destination noise map is + /// destroyed. + /// + /// @throw ExceptionInvalidParam See the preconditions. + /// @throw ExceptionOutOfMemory Out of memory. + /// + /// If this method is successful, the destination noise map contains + /// the coherent-noise values from the noise module specified by + /// setSourceModule(). + public void build () throws ExceptionInvalidParam + { + //override in child classes + } + + /// Returns the height of the destination noise map. + /// + /// @returns The height of the destination noise map, in points. + /// + /// This object does not change the height in the destination noise + /// map object until the build() method is called. + public double getDestHeight () + { + return destHeight; + } + + /// Returns the width of the destination noise map. + /// + /// @returns The width of the destination noise map, in points. + /// + /// This object does not change the height in the destination noise + /// map object until the build() method is called. + public double getDestWidth () + { + return destWidth; + } + + /// Sets the destination noise map. + /// + /// @param destNoiseMap The destination noise map. + /// + /// The destination noise map will contain the coherent-noise values + /// from this noise map after a successful call to the build() method. + /// + /// The destination noise map must exist throughout the lifetime of + /// this object unless another noise map replaces that noise map. + public void setDestNoiseMap (NoiseMap destNoiseMap) + { + this.destNoiseMap = destNoiseMap; + } + + /// Sets the source module. + /// + /// @param sourceModule The source module. + /// + /// This object fills in a noise map with the coherent-noise values + /// from this source module. + /// + /// The source module must exist throughout the lifetime of this + /// object unless another noise module replaces that noise module. + public void setSourceModule (ModuleBase sourceModule) + { + this.sourceModule = sourceModule; + } + + /// Sets the size of the destination noise map. + /// + /// @param destWidth The width of the destination noise map, in + /// points. + /// @param destHeight The height of the destination noise map, in + /// points. + /// + /// This method does not change the size of the destination noise map + /// until the build() method is called. + public void setDestSize (int destWidth, int destHeight) + { + this.destWidth = destWidth ; + this.destHeight = destHeight; + } + + public NoiseMap getDestNoiseMap() + { + return destNoiseMap; + } + + void setCallback (int callback) + { + this.callback = callback; + } + } \ No newline at end of file diff --git a/src/libnoiseforjava/util/NoiseMapBuilderCylinder.java b/src/libnoiseforjava/util/NoiseMapBuilderCylinder.java index 6bd3513..4d33219 100644 --- a/src/libnoiseforjava/util/NoiseMapBuilderCylinder.java +++ b/src/libnoiseforjava/util/NoiseMapBuilderCylinder.java @@ -1,213 +1,213 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.model.Cylinder; - -public class NoiseMapBuilderCylinder extends NoiseMapBuilder -{ - - /// Builds a cylindrical noise map. - /// - /// This class builds a noise map by filling it with coherent-noise values - /// generated from the surface of a cylinder. - /// - /// This class describes these input values using an (angle, height) - /// coordinate system. After generating the coherent-noise value from the - /// input value, it then "flattens" these coordinates onto a plane so that - /// it can write the values into a two-dimensional noise map. - /// - /// The cylinder model has a radius of 1.0 unit and has infinite height. - /// The cylinder is oriented along the @a y axis. Its center is at the - /// origin. - /// - /// The x coordinate in the noise map represents the angle around the - /// cylinder's y axis. The y coordinate in the noise map represents the - /// height above the x-z plane. - /// - /// The application must provide the lower and upper angle bounds of the - /// noise map, in degrees, and the lower and upper height bounds of the - /// noise map, in units. - - /// Lower angle boundary of the cylindrical noise map, in degrees. - double lowerAngleBound; - - /// Lower height boundary of the cylindrical noise map, in units. - double lowerHeightBound; - - /// Upper angle boundary of the cylindrical noise map, in degrees. - double upperAngleBound; - - /// Upper height boundary of the cylindrical noise map, in units. - double upperHeightBound; - - - - public NoiseMapBuilderCylinder () throws ExceptionInvalidParam - { - super(); - lowerAngleBound = 0.0; - lowerHeightBound = 0.0; - upperAngleBound = 0.0; - upperHeightBound = 0.0; - } - - public void build () throws ExceptionInvalidParam - { - if (upperAngleBound <= lowerAngleBound - || upperHeightBound <= lowerHeightBound - || destWidth <= 0 - || destHeight <= 0 - || sourceModule == null - || destNoiseMap == null) - throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilderCylinder"); - - - // Resize the destination noise map so that it can store the new output - // values from the source model. - destNoiseMap.setSize (destWidth, destHeight); - - // Create the cylinder model. - Cylinder cylinderModel = new Cylinder(); - cylinderModel.setModule (sourceModule); - - double angleExtent = upperAngleBound - lowerAngleBound ; - double heightExtent = upperHeightBound - lowerHeightBound; - double xDelta = angleExtent / (double)destWidth ; - double yDelta = heightExtent / (double)destHeight; - double curAngle = lowerAngleBound ; - double curHeight = lowerHeightBound; - - // Fill every point in the noise map with the output values from the model. - for (int y = 0; y < destHeight; y++) - { - curAngle = lowerAngleBound; - for (int x = 0; x < destWidth; x++) - { - float curValue = (float)cylinderModel.getValue (curAngle, curHeight); - destNoiseMap.setValue(x, y, curValue); - curAngle += xDelta; - } - curHeight += yDelta; - setCallback (y); - } - } - - /// Returns the lower angle boundary of the cylindrical noise map. - /// - /// @returns The lower angle boundary of the noise map, in degrees. - public double getLowerAngleBound () - { - return lowerAngleBound; - } - - /// Returns the lower height boundary of the cylindrical noise map. - /// - /// @returns The lower height boundary of the noise map, in units. - /// - /// One unit is equal to the radius of the cylinder. - public double getLowerHeightBound () - { - return lowerHeightBound; - } - - /// Returns the upper angle boundary of the cylindrical noise map. - /// - /// @returns The upper angle boundary of the noise map, in degrees. - public double GetUpperAngleBound () - { - return upperAngleBound; - } - - /// Returns the upper height boundary of the cylindrical noise map. - /// - /// @returns The upper height boundary of the noise map, in units. - /// - /// One unit is equal to the radius of the cylinder. - public double getUpperHeightBound () - { - return upperHeightBound; - } - - /// Sets the coordinate boundaries of the noise map. - /// - /// @param lowerAngleBound The lower angle boundary of the noise map, - /// in degrees. - /// @param upperAngleBound The upper angle boundary of the noise map, - /// in degrees. - /// @param lowerHeightBound The lower height boundary of the noise - /// map, in units. - /// @param upperHeightBound The upper height boundary of the noise - /// map, in units. - /// - /// @pre The lower angle boundary is less than the upper angle - /// boundary. - /// @pre The lower height boundary is less than the upper height - /// boundary. - /// - /// @throw noise::ExceptionInvalidParam See the preconditions. - /// - /// One unit is equal to the radius of the cylinder. - public void setBounds (double lowerAngleBound, double upperAngleBound, - double lowerHeightBound, double upperHeightBound) throws ExceptionInvalidParam - { - if (lowerAngleBound >= upperAngleBound - || lowerHeightBound >= upperHeightBound) - throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilder Cylinder"); - - this.lowerAngleBound = lowerAngleBound ; - this.upperAngleBound = upperAngleBound ; - this.lowerHeightBound = lowerHeightBound; - this.upperHeightBound = upperHeightBound; - } - - public double getUpperAngleBound() - { - return upperAngleBound; - } - - public void setLowerAngleBound(double lowerAngleBound) - { - this.lowerAngleBound = lowerAngleBound; - } - - public void setLowerHeightBound(double lowerHeightBound) - { - this.lowerHeightBound = lowerHeightBound; - } - - public void setUpperAngleBound(double upperAngleBound) - { - this.upperAngleBound = upperAngleBound; - } - - public void setUpperHeightBound(double upperHeightBound) - { - this.upperHeightBound = upperHeightBound; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.model.Cylinder; + +public class NoiseMapBuilderCylinder extends NoiseMapBuilder +{ + + /// Builds a cylindrical noise map. + /// + /// This class builds a noise map by filling it with coherent-noise values + /// generated from the surface of a cylinder. + /// + /// This class describes these input values using an (angle, height) + /// coordinate system. After generating the coherent-noise value from the + /// input value, it then "flattens" these coordinates onto a plane so that + /// it can write the values into a two-dimensional noise map. + /// + /// The cylinder model has a radius of 1.0 unit and has infinite height. + /// The cylinder is oriented along the @a y axis. Its center is at the + /// origin. + /// + /// The x coordinate in the noise map represents the angle around the + /// cylinder's y axis. The y coordinate in the noise map represents the + /// height above the x-z plane. + /// + /// The application must provide the lower and upper angle bounds of the + /// noise map, in degrees, and the lower and upper height bounds of the + /// noise map, in units. + + /// Lower angle boundary of the cylindrical noise map, in degrees. + double lowerAngleBound; + + /// Lower height boundary of the cylindrical noise map, in units. + double lowerHeightBound; + + /// Upper angle boundary of the cylindrical noise map, in degrees. + double upperAngleBound; + + /// Upper height boundary of the cylindrical noise map, in units. + double upperHeightBound; + + + + public NoiseMapBuilderCylinder () throws ExceptionInvalidParam + { + super(); + lowerAngleBound = 0.0; + lowerHeightBound = 0.0; + upperAngleBound = 0.0; + upperHeightBound = 0.0; + } + + public void build () throws ExceptionInvalidParam + { + if (upperAngleBound <= lowerAngleBound + || upperHeightBound <= lowerHeightBound + || destWidth <= 0 + || destHeight <= 0 + || sourceModule == null + || destNoiseMap == null) + throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilderCylinder"); + + + // Resize the destination noise map so that it can store the new output + // values from the source model. + destNoiseMap.setSize (destWidth, destHeight); + + // Create the cylinder model. + Cylinder cylinderModel = new Cylinder(); + cylinderModel.setModule (sourceModule); + + double angleExtent = upperAngleBound - lowerAngleBound ; + double heightExtent = upperHeightBound - lowerHeightBound; + double xDelta = angleExtent / (double)destWidth ; + double yDelta = heightExtent / (double)destHeight; + double curAngle = lowerAngleBound ; + double curHeight = lowerHeightBound; + + // Fill every point in the noise map with the output values from the model. + for (int y = 0; y < destHeight; y++) + { + curAngle = lowerAngleBound; + for (int x = 0; x < destWidth; x++) + { + float curValue = (float)cylinderModel.getValue (curAngle, curHeight); + destNoiseMap.setValue(x, y, curValue); + curAngle += xDelta; + } + curHeight += yDelta; + setCallback (y); + } + } + + /// Returns the lower angle boundary of the cylindrical noise map. + /// + /// @returns The lower angle boundary of the noise map, in degrees. + public double getLowerAngleBound () + { + return lowerAngleBound; + } + + /// Returns the lower height boundary of the cylindrical noise map. + /// + /// @returns The lower height boundary of the noise map, in units. + /// + /// One unit is equal to the radius of the cylinder. + public double getLowerHeightBound () + { + return lowerHeightBound; + } + + /// Returns the upper angle boundary of the cylindrical noise map. + /// + /// @returns The upper angle boundary of the noise map, in degrees. + public double GetUpperAngleBound () + { + return upperAngleBound; + } + + /// Returns the upper height boundary of the cylindrical noise map. + /// + /// @returns The upper height boundary of the noise map, in units. + /// + /// One unit is equal to the radius of the cylinder. + public double getUpperHeightBound () + { + return upperHeightBound; + } + + /// Sets the coordinate boundaries of the noise map. + /// + /// @param lowerAngleBound The lower angle boundary of the noise map, + /// in degrees. + /// @param upperAngleBound The upper angle boundary of the noise map, + /// in degrees. + /// @param lowerHeightBound The lower height boundary of the noise + /// map, in units. + /// @param upperHeightBound The upper height boundary of the noise + /// map, in units. + /// + /// @pre The lower angle boundary is less than the upper angle + /// boundary. + /// @pre The lower height boundary is less than the upper height + /// boundary. + /// + /// @throw noise::ExceptionInvalidParam See the preconditions. + /// + /// One unit is equal to the radius of the cylinder. + public void setBounds (double lowerAngleBound, double upperAngleBound, + double lowerHeightBound, double upperHeightBound) throws ExceptionInvalidParam + { + if (lowerAngleBound >= upperAngleBound + || lowerHeightBound >= upperHeightBound) + throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilder Cylinder"); + + this.lowerAngleBound = lowerAngleBound ; + this.upperAngleBound = upperAngleBound ; + this.lowerHeightBound = lowerHeightBound; + this.upperHeightBound = upperHeightBound; + } + + public double getUpperAngleBound() + { + return upperAngleBound; + } + + public void setLowerAngleBound(double lowerAngleBound) + { + this.lowerAngleBound = lowerAngleBound; + } + + public void setLowerHeightBound(double lowerHeightBound) + { + this.lowerHeightBound = lowerHeightBound; + } + + public void setUpperAngleBound(double upperAngleBound) + { + this.upperAngleBound = upperAngleBound; + } + + public void setUpperHeightBound(double upperHeightBound) + { + this.upperHeightBound = upperHeightBound; + } + +} diff --git a/src/libnoiseforjava/util/NoiseMapBuilderPlane.java b/src/libnoiseforjava/util/NoiseMapBuilderPlane.java index fbd8f97..20e5fcb 100644 --- a/src/libnoiseforjava/util/NoiseMapBuilderPlane.java +++ b/src/libnoiseforjava/util/NoiseMapBuilderPlane.java @@ -1,245 +1,245 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.Interp; -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.model.Plane; - -public class NoiseMapBuilderPlane extends NoiseMapBuilder -{ - /// Builds a planar noise map. - /// - /// This class builds a noise map by filling it with coherent-noise values - /// generated from the surface of a plane. - /// - /// This class describes these input values using (x, z) coordinates. - /// Their y coordinates are always 0.0. - /// - /// The application must provide the lower and upper x coordinate bounds - /// of the noise map, in units, and the lower and upper z coordinate - /// bounds of the noise map, in units. - /// - /// To make a tileable noise map with no seams at the edges, call the - /// enableSeamless() method. - - /// A flag specifying whether seamless tiling is enabled. - boolean isSeamlessEnabled; - - /// Lower x boundary of the planar noise map, in units. - double lowerXBound; - - /// Lower z boundary of the planar noise map, in units. - double lowerZBound; - - /// Upper x boundary of the planar noise map, in units. - double upperXBound; - - /// Upper z boundary of the planar noise map, in units. - double upperZBound; - - - public NoiseMapBuilderPlane () throws ExceptionInvalidParam - { - super(); - isSeamlessEnabled = false; - lowerXBound = 0.0; - lowerZBound = 0.0; - upperXBound = 0.0; - upperZBound = 0.0; - } - - public NoiseMapBuilderPlane (int height, int width) throws ExceptionInvalidParam - { - super(height, width); - isSeamlessEnabled = false; - lowerXBound = 0.0; - lowerZBound = 0.0; - upperXBound = 0.0; - upperZBound = 0.0; - } - - public void build () throws ExceptionInvalidParam - { - if ( upperXBound <= lowerXBound - || upperZBound <= lowerZBound - || destWidth <= 0 - || destHeight <= 0 - || sourceModule == null - || destNoiseMap == null) - throw new ExceptionInvalidParam ("Invalid parameter in NoiseMapBuilderPlane"); - - // Resize the destination noise map so that it can store the new output - // values from the source model. - destNoiseMap.setSize (destWidth, destHeight); - - // Create the plane model. - Plane planeModel = new Plane(); - planeModel.setModule (sourceModule); - - double xExtent = upperXBound - lowerXBound; - double zExtent = upperZBound - lowerZBound; - double xDelta = xExtent / (double)destWidth ; - double zDelta = zExtent / (double)destHeight; - double xCur = lowerXBound; - double zCur = lowerZBound; - - // Fill every point in the noise map with the output values from the model. - for (int z = 0; z < destHeight; z++) - { - xCur = lowerXBound; - for (int x = 0; x < destWidth; x++) - { - double finalValue; - - if (!isSeamlessEnabled) - finalValue = planeModel.getValue (xCur, zCur); - else - { - double swValue, seValue, nwValue, neValue; - swValue = planeModel.getValue (xCur, zCur); - seValue = planeModel.getValue (xCur + xExtent, zCur); - nwValue = planeModel.getValue (xCur, zCur + zExtent); - neValue = planeModel.getValue (xCur + xExtent, zCur + zExtent); - double xBlend = 1.0 - ((xCur - lowerXBound) / xExtent); - double zBlend = 1.0 - ((zCur - lowerZBound) / zExtent); - double z0 = Interp.linearInterp (swValue, seValue, xBlend); - double z1 = Interp.linearInterp (nwValue, neValue, xBlend); - finalValue = Interp.linearInterp (z0, z1, zBlend); - } - - destNoiseMap.setValue(x, z, finalValue); - xCur += xDelta; - } - zCur += zDelta; - setCallback (z); - } - } - - /// Enables or disables seamless tiling. - /// - /// @param enable A flag that enables or disables seamless tiling. - /// - /// Enabling seamless tiling builds a noise map with no seams at the - /// edges. This allows the noise map to be tileable. - public void enableSeamless (boolean enable) - { - isSeamlessEnabled = enable; - } - - /// Returns the lower x boundary of the planar noise map. - /// - /// @returns The lower x boundary of the planar noise map, in units. - public double getLowerXBound () - { - return lowerXBound; - } - - /// Returns the lower z boundary of the planar noise map. - /// - /// @returns The lower z boundary of the noise map, in units. - public double getLowerZBound () - { - return lowerZBound; - } - - /// Returns the upper x boundary of the planar noise map. - /// - /// @returns The upper x boundary of the noise map, in units. - public double getUpperXBound () - { - return upperXBound; - } - - /// Returns the upper z boundary of the planar noise map. - /// - /// @returns The upper z boundary of the noise map, in units. - public double getUpperZBound () - { - return upperZBound; - } - - /// Determines if seamless tiling is enabled. - /// - /// @returns - /// - @a true if seamless tiling is enabled. - /// - @a false if seamless tiling is disabled. - /// - /// Enabling seamless tiling builds a noise map with no seams at the - /// edges. This allows the noise map to be tileable. - public boolean isSeamlessEnabled () - { - return isSeamlessEnabled; - } - - /// Sets the boundaries of the planar noise map. - /// - /// @param lowerXBound The lower x boundary of the noise map, in - /// units. - /// @param upperXBound The upper x boundary of the noise map, in - /// units. - /// @param lowerZBound The lower z boundary of the noise map, in - /// units. - /// @param upperZBound The upper z boundary of the noise map, in - /// units. - /// - /// @pre The lower x boundary is less than the upper x boundary. - /// @pre The lower z boundary is less than the upper z boundary. - /// - /// @throw ExceptionInvalidParam See the preconditions. - public void setBounds (double lowerXBound, double upperXBound, - double lowerZBound, double upperZBound) throws ExceptionInvalidParam - { - if (lowerXBound >= upperXBound || lowerZBound >= upperZBound) - throw new ExceptionInvalidParam ("Invalid parameter in NoiseMapBuilderPlane"); - - this.lowerXBound = lowerXBound; - this.upperXBound = upperXBound; - this.lowerZBound = lowerZBound; - this.upperZBound = upperZBound; - } - - public void setLowerXBound(double lowerXBound) - { - this.lowerXBound = lowerXBound; - } - - public void setLowerZBound(double lowerZBound) - { - this.lowerZBound = lowerZBound; - } - - public void setUpperXBound(double upperXBound) - { - this.upperXBound = upperXBound; - } - - public void setUpperZBound(double upperZBound) - { - this.upperZBound = upperZBound; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.Interp; +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.model.Plane; + +public class NoiseMapBuilderPlane extends NoiseMapBuilder +{ + /// Builds a planar noise map. + /// + /// This class builds a noise map by filling it with coherent-noise values + /// generated from the surface of a plane. + /// + /// This class describes these input values using (x, z) coordinates. + /// Their y coordinates are always 0.0. + /// + /// The application must provide the lower and upper x coordinate bounds + /// of the noise map, in units, and the lower and upper z coordinate + /// bounds of the noise map, in units. + /// + /// To make a tileable noise map with no seams at the edges, call the + /// enableSeamless() method. + + /// A flag specifying whether seamless tiling is enabled. + boolean isSeamlessEnabled; + + /// Lower x boundary of the planar noise map, in units. + double lowerXBound; + + /// Lower z boundary of the planar noise map, in units. + double lowerZBound; + + /// Upper x boundary of the planar noise map, in units. + double upperXBound; + + /// Upper z boundary of the planar noise map, in units. + double upperZBound; + + + public NoiseMapBuilderPlane () throws ExceptionInvalidParam + { + super(); + isSeamlessEnabled = false; + lowerXBound = 0.0; + lowerZBound = 0.0; + upperXBound = 0.0; + upperZBound = 0.0; + } + + public NoiseMapBuilderPlane (int height, int width) throws ExceptionInvalidParam + { + super(height, width); + isSeamlessEnabled = false; + lowerXBound = 0.0; + lowerZBound = 0.0; + upperXBound = 0.0; + upperZBound = 0.0; + } + + public void build () throws ExceptionInvalidParam + { + if ( upperXBound <= lowerXBound + || upperZBound <= lowerZBound + || destWidth <= 0 + || destHeight <= 0 + || sourceModule == null + || destNoiseMap == null) + throw new ExceptionInvalidParam ("Invalid parameter in NoiseMapBuilderPlane"); + + // Resize the destination noise map so that it can store the new output + // values from the source model. + destNoiseMap.setSize (destWidth, destHeight); + + // Create the plane model. + Plane planeModel = new Plane(); + planeModel.setModule (sourceModule); + + double xExtent = upperXBound - lowerXBound; + double zExtent = upperZBound - lowerZBound; + double xDelta = xExtent / (double)destWidth ; + double zDelta = zExtent / (double)destHeight; + double xCur = lowerXBound; + double zCur = lowerZBound; + + // Fill every point in the noise map with the output values from the model. + for (int z = 0; z < destHeight; z++) + { + xCur = lowerXBound; + for (int x = 0; x < destWidth; x++) + { + double finalValue; + + if (!isSeamlessEnabled) + finalValue = planeModel.getValue (xCur, zCur); + else + { + double swValue, seValue, nwValue, neValue; + swValue = planeModel.getValue (xCur, zCur); + seValue = planeModel.getValue (xCur + xExtent, zCur); + nwValue = planeModel.getValue (xCur, zCur + zExtent); + neValue = planeModel.getValue (xCur + xExtent, zCur + zExtent); + double xBlend = 1.0 - ((xCur - lowerXBound) / xExtent); + double zBlend = 1.0 - ((zCur - lowerZBound) / zExtent); + double z0 = Interp.linearInterp (swValue, seValue, xBlend); + double z1 = Interp.linearInterp (nwValue, neValue, xBlend); + finalValue = Interp.linearInterp (z0, z1, zBlend); + } + + destNoiseMap.setValue(x, z, finalValue); + xCur += xDelta; + } + zCur += zDelta; + setCallback (z); + } + } + + /// Enables or disables seamless tiling. + /// + /// @param enable A flag that enables or disables seamless tiling. + /// + /// Enabling seamless tiling builds a noise map with no seams at the + /// edges. This allows the noise map to be tileable. + public void enableSeamless (boolean enable) + { + isSeamlessEnabled = enable; + } + + /// Returns the lower x boundary of the planar noise map. + /// + /// @returns The lower x boundary of the planar noise map, in units. + public double getLowerXBound () + { + return lowerXBound; + } + + /// Returns the lower z boundary of the planar noise map. + /// + /// @returns The lower z boundary of the noise map, in units. + public double getLowerZBound () + { + return lowerZBound; + } + + /// Returns the upper x boundary of the planar noise map. + /// + /// @returns The upper x boundary of the noise map, in units. + public double getUpperXBound () + { + return upperXBound; + } + + /// Returns the upper z boundary of the planar noise map. + /// + /// @returns The upper z boundary of the noise map, in units. + public double getUpperZBound () + { + return upperZBound; + } + + /// Determines if seamless tiling is enabled. + /// + /// @returns + /// - @a true if seamless tiling is enabled. + /// - @a false if seamless tiling is disabled. + /// + /// Enabling seamless tiling builds a noise map with no seams at the + /// edges. This allows the noise map to be tileable. + public boolean isSeamlessEnabled () + { + return isSeamlessEnabled; + } + + /// Sets the boundaries of the planar noise map. + /// + /// @param lowerXBound The lower x boundary of the noise map, in + /// units. + /// @param upperXBound The upper x boundary of the noise map, in + /// units. + /// @param lowerZBound The lower z boundary of the noise map, in + /// units. + /// @param upperZBound The upper z boundary of the noise map, in + /// units. + /// + /// @pre The lower x boundary is less than the upper x boundary. + /// @pre The lower z boundary is less than the upper z boundary. + /// + /// @throw ExceptionInvalidParam See the preconditions. + public void setBounds (double lowerXBound, double upperXBound, + double lowerZBound, double upperZBound) throws ExceptionInvalidParam + { + if (lowerXBound >= upperXBound || lowerZBound >= upperZBound) + throw new ExceptionInvalidParam ("Invalid parameter in NoiseMapBuilderPlane"); + + this.lowerXBound = lowerXBound; + this.upperXBound = upperXBound; + this.lowerZBound = lowerZBound; + this.upperZBound = upperZBound; + } + + public void setLowerXBound(double lowerXBound) + { + this.lowerXBound = lowerXBound; + } + + public void setLowerZBound(double lowerZBound) + { + this.lowerZBound = lowerZBound; + } + + public void setUpperXBound(double upperXBound) + { + this.upperXBound = upperXBound; + } + + public void setUpperZBound(double upperZBound) + { + this.upperZBound = upperZBound; + } + +} diff --git a/src/libnoiseforjava/util/NoiseMapBuilderSphere.java b/src/libnoiseforjava/util/NoiseMapBuilderSphere.java index 6f96061..0aa560a 100644 --- a/src/libnoiseforjava/util/NoiseMapBuilderSphere.java +++ b/src/libnoiseforjava/util/NoiseMapBuilderSphere.java @@ -1,195 +1,195 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.exception.ExceptionInvalidParam; -import libnoiseforjava.model.Sphere; - -public class NoiseMapBuilderSphere extends NoiseMapBuilder -{ - /// Builds a spherical noise map. - /// - /// This class builds a noise map by filling it with coherent-noise values - /// generated from the surface of a sphere. - /// - /// This class describes these input values using a (latitude, longitude) - /// coordinate system. After generating the coherent-noise value from the - /// input value, it then "flattens" these coordinates onto a plane so that - /// it can write the values into a two-dimensional noise map. - /// - /// The sphere model has a radius of 1.0 unit. Its center is at the - /// origin. - /// - /// The x coordinate in the noise map represents the longitude. The y - /// coordinate in the noise map represents the latitude. - /// - /// The application must provide the southern, northern, western, and - /// eastern bounds of the noise map, in degrees. - - /// Eastern boundary of the spherical noise map, in degrees. - double eastLonBound; - - /// Northern boundary of the spherical noise map, in degrees. - double northLatBound; - - /// Southern boundary of the spherical noise map, in degrees. - double southLatBound; - - /// Western boundary of the spherical noise map, in degrees. - double westLonBound; - - public NoiseMapBuilderSphere () throws ExceptionInvalidParam - { - super(); - eastLonBound = 0.0; - northLatBound = 0.0; - southLatBound = 0.0; - westLonBound = 0.0; - } - - public void build () throws ExceptionInvalidParam - { - if ( eastLonBound <= westLonBound - || northLatBound <= southLatBound - || destWidth <= 0 - || destHeight <= 0 - || sourceModule == null - || destNoiseMap == null) - throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilderSphere"); - - - // Resize the destination noise map so that it can store the new output - // values from the source model. - destNoiseMap.setSize (destWidth, destHeight); - - // Create the plane model. - Sphere sphereModel = new Sphere(); - sphereModel.setModule (sourceModule); - - double lonExtent = eastLonBound - westLonBound ; - double latExtent = northLatBound - southLatBound; - double xDelta = lonExtent / (double)destWidth ; - double yDelta = latExtent / (double)destHeight; - double curLon = westLonBound ; - double curLat = southLatBound; - - // Fill every point in the noise map with the output values from the model. - for (int y = 0; y < destHeight; y++) - { - curLon = westLonBound; - for (int x = 0; x < destWidth; x++) - { - float curValue = (float)sphereModel.getValue (curLat, curLon); - destNoiseMap.setValue(x, y, curValue); - curLon += xDelta; - } - curLat += yDelta; - setCallback(y); - - } - } - - /// Returns the eastern boundary of the spherical noise map. - /// - /// @returns The eastern boundary of the noise map, in degrees. - public double getEastLonBound () - { - return eastLonBound; - } - - /// Returns the northern boundary of the spherical noise map - /// - /// @returns The northern boundary of the noise map, in degrees. - public double getNorthLatBound () - { - return northLatBound; - } - - /// Returns the southern boundary of the spherical noise map - /// - /// @returns The southern boundary of the noise map, in degrees. - public double getSouthLatBound () - { - return southLatBound; - } - - /// Returns the western boundary of the spherical noise map - /// - /// @returns The western boundary of the noise map, in degrees. - public double getWestLonBound () - { - return westLonBound; - } - - /// Sets the coordinate boundaries of the noise map. - /// - /// @param southLatBound The southern boundary of the noise map, in - /// degrees. - /// @param northLatBound The northern boundary of the noise map, in - /// degrees. - /// @param westLonBound The western boundary of the noise map, in - /// degrees. - /// @param eastLonBound The eastern boundary of the noise map, in - /// degrees. - /// - /// @pre The southern boundary is less than the northern boundary. - /// @pre The western boundary is less than the eastern boundary. - /// - /// @throw noise::ExceptionInvalidParam See the preconditions. - public void setBounds (double southLatBound, double northLatBound, - double westLonBound, double eastLonBound) throws ExceptionInvalidParam - { - if (southLatBound >= northLatBound - || westLonBound >= eastLonBound) - throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilderSphere"); - - this.southLatBound = southLatBound; - this.northLatBound = northLatBound; - this.westLonBound = westLonBound ; - this.eastLonBound = eastLonBound ; - } - - public void setEastLonBound(double eastLonBound) - { - this.eastLonBound = eastLonBound; - } - - public void setNorthLatBound(double northLatBound) - { - this.northLatBound = northLatBound; - } - - public void setSouthLatBound(double southLatBound) - { - this.southLatBound = southLatBound; - } - - public void setWestLonBound(double westLonBound) - { - this.westLonBound = westLonBound; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.exception.ExceptionInvalidParam; +import libnoiseforjava.model.Sphere; + +public class NoiseMapBuilderSphere extends NoiseMapBuilder +{ + /// Builds a spherical noise map. + /// + /// This class builds a noise map by filling it with coherent-noise values + /// generated from the surface of a sphere. + /// + /// This class describes these input values using a (latitude, longitude) + /// coordinate system. After generating the coherent-noise value from the + /// input value, it then "flattens" these coordinates onto a plane so that + /// it can write the values into a two-dimensional noise map. + /// + /// The sphere model has a radius of 1.0 unit. Its center is at the + /// origin. + /// + /// The x coordinate in the noise map represents the longitude. The y + /// coordinate in the noise map represents the latitude. + /// + /// The application must provide the southern, northern, western, and + /// eastern bounds of the noise map, in degrees. + + /// Eastern boundary of the spherical noise map, in degrees. + double eastLonBound; + + /// Northern boundary of the spherical noise map, in degrees. + double northLatBound; + + /// Southern boundary of the spherical noise map, in degrees. + double southLatBound; + + /// Western boundary of the spherical noise map, in degrees. + double westLonBound; + + public NoiseMapBuilderSphere () throws ExceptionInvalidParam + { + super(); + eastLonBound = 0.0; + northLatBound = 0.0; + southLatBound = 0.0; + westLonBound = 0.0; + } + + public void build () throws ExceptionInvalidParam + { + if ( eastLonBound <= westLonBound + || northLatBound <= southLatBound + || destWidth <= 0 + || destHeight <= 0 + || sourceModule == null + || destNoiseMap == null) + throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilderSphere"); + + + // Resize the destination noise map so that it can store the new output + // values from the source model. + destNoiseMap.setSize (destWidth, destHeight); + + // Create the plane model. + Sphere sphereModel = new Sphere(); + sphereModel.setModule (sourceModule); + + double lonExtent = eastLonBound - westLonBound ; + double latExtent = northLatBound - southLatBound; + double xDelta = lonExtent / (double)destWidth ; + double yDelta = latExtent / (double)destHeight; + double curLon = westLonBound ; + double curLat = southLatBound; + + // Fill every point in the noise map with the output values from the model. + for (int y = 0; y < destHeight; y++) + { + curLon = westLonBound; + for (int x = 0; x < destWidth; x++) + { + float curValue = (float)sphereModel.getValue (curLat, curLon); + destNoiseMap.setValue(x, y, curValue); + curLon += xDelta; + } + curLat += yDelta; + setCallback(y); + + } + } + + /// Returns the eastern boundary of the spherical noise map. + /// + /// @returns The eastern boundary of the noise map, in degrees. + public double getEastLonBound () + { + return eastLonBound; + } + + /// Returns the northern boundary of the spherical noise map + /// + /// @returns The northern boundary of the noise map, in degrees. + public double getNorthLatBound () + { + return northLatBound; + } + + /// Returns the southern boundary of the spherical noise map + /// + /// @returns The southern boundary of the noise map, in degrees. + public double getSouthLatBound () + { + return southLatBound; + } + + /// Returns the western boundary of the spherical noise map + /// + /// @returns The western boundary of the noise map, in degrees. + public double getWestLonBound () + { + return westLonBound; + } + + /// Sets the coordinate boundaries of the noise map. + /// + /// @param southLatBound The southern boundary of the noise map, in + /// degrees. + /// @param northLatBound The northern boundary of the noise map, in + /// degrees. + /// @param westLonBound The western boundary of the noise map, in + /// degrees. + /// @param eastLonBound The eastern boundary of the noise map, in + /// degrees. + /// + /// @pre The southern boundary is less than the northern boundary. + /// @pre The western boundary is less than the eastern boundary. + /// + /// @throw noise::ExceptionInvalidParam See the preconditions. + public void setBounds (double southLatBound, double northLatBound, + double westLonBound, double eastLonBound) throws ExceptionInvalidParam + { + if (southLatBound >= northLatBound + || westLonBound >= eastLonBound) + throw new ExceptionInvalidParam ("Invalid Parameter in NoiseMapBuilderSphere"); + + this.southLatBound = southLatBound; + this.northLatBound = northLatBound; + this.westLonBound = westLonBound ; + this.eastLonBound = eastLonBound ; + } + + public void setEastLonBound(double eastLonBound) + { + this.eastLonBound = eastLonBound; + } + + public void setNorthLatBound(double northLatBound) + { + this.northLatBound = northLatBound; + } + + public void setSouthLatBound(double southLatBound) + { + this.southLatBound = southLatBound; + } + + public void setWestLonBound(double westLonBound) + { + this.westLonBound = westLonBound; + } + +} diff --git a/src/libnoiseforjava/util/RendererImage.java b/src/libnoiseforjava/util/RendererImage.java index 5940527..bc9aa7e 100644 --- a/src/libnoiseforjava/util/RendererImage.java +++ b/src/libnoiseforjava/util/RendererImage.java @@ -1,813 +1,813 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.Interp; -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class RendererImage -{ - - /// Renders an image from a noise map. - /// - /// This class renders an image given the contents of a noise-map object. - /// - /// An application can configure the output of the image in three ways: - /// - Specify the color gradient. - /// - Specify the light source parameters. - /// - Specify the background image. - /// - /// Specify the color gradient - /// - /// This class uses a color gradient to calculate the color for each pixel - /// in the destination image according to the value from the corresponding - /// position in the noise map. - /// - /// A color gradient is a list of gradually-changing colors. A color - /// gradient is defined by a list of gradient points. Each - /// gradient point has a position and a color. In a color gradient, the - /// colors between two adjacent gradient points are linearly interpolated. - /// - /// For example, suppose this class contains the following color gradient: - /// - /// - -1.0 maps to dark blue. - /// - -0.2 maps to light blue. - /// - -0.1 maps to tan. - /// - 0.0 maps to green. - /// - 1.0 maps to white. - /// - /// The value 0.5 maps to a greenish-white color because 0.5 is halfway - /// between 0.0 (mapped to green) and 1.0 (mapped to white). - /// - /// The value -0.6 maps to a medium blue color because -0.6 is halfway - /// between -1.0 (mapped to dark blue) and -0.2 (mapped to light blue). - /// - /// The color gradient requires a minimum of two gradient points. - /// - /// This class contains two pre-made gradients: a grayscale gradient and a - /// color gradient suitable for terrain. To use these pre-made gradients, - /// call the buildGrayscaleGradient() or buildTerrainGradient() methods, - /// respectively. - /// - /// @note The color value passed to addGradientPoint() has an alpha - /// channel. This alpha channel specifies how a pixel in the background - /// image (if specified) is blended with the calculated color. If the - /// alpha value is high, this class weighs the blend towards the - /// calculated color, and if the alpha value is low, this class weighs the - /// blend towards the color from the corresponding pixel in the background - /// image. - /// - /// Specify the light source parameters - /// - /// This class contains a parallel light source that lights the image. It - /// interprets the noise map as a bump map. - /// - /// To enable or disable lighting, pass a Boolean value to the - /// enableLight() method. - /// - /// To set the position of the light source in the "sky", call the - /// setLightAzimuth() and setLightElev() methods. - /// - /// To set the color of the light source, call the setLightColor() method. - /// - /// To set the intensity of the light source, call the setLightIntensity() - /// method. A good intensity value is 2.0, although that value tends to - /// "wash out" very light colors from the image. - /// - /// To set the contrast amount between areas in light and areas in shadow, - /// call the setLightContrast() method. Determining the correct contrast - /// amount requires some trial and error, but if your application - /// interprets the noise map as a height map that has its elevation values - /// measured in meters and has a horizontal resolution of @a h meters, a - /// good contrast amount to use is ( 1.0 / @a h ). - /// - /// Specify the background image - /// - /// To specify a background image, pass an Image object to the - /// setBackgroundImage() method. - /// - /// This class determines the color of a pixel in the destination image by - /// blending the calculated color with the color of the corresponding - /// pixel from the background image. - /// - /// The blend amount is determined by the alpha of the calculated color. - /// If the alpha value is high, this class weighs the blend towards the - /// calculated color, and if the alpha value is low, this class weighs the - /// blend towards the color from the corresponding pixel in the background - /// image. - /// - /// Rendering the image - /// - /// To render the image, perform the following steps: - /// - Pass a NoiseMap object to the setSourceNoiseMap() method. - /// - Pass an ImageCafe object to the setDestImage() method. - /// - Pass an ImageCafe object to the setBackgroundImage() method (optional) - /// - Call the render() method. - - - static final double SQRT_2 = 1.4142135623730950488; - - - /// The cosine of the azimuth of the light source. - double cosAzimuth; - - /// The cosine of the elevation of the light source. - double cosElev; - - /// The color gradient used to specify the image colors. - GradientColor gradient; - - /// A flag specifying whether lighting is enabled. - boolean isLightEnabled; - - /// A flag specifying whether wrapping is enabled. - boolean isWrapEnabled; - - /// The azimuth of the light source, in degrees. - double lightAzimuth; - - /// The brightness of the light source. - double lightBrightness; - - /// The color of the light source. - ColorCafe lightColor; - - /// The contrast between areas in light and areas in shadow. - double lightContrast; - - /// The elevation of the light source, in degrees. - double lightElev; - - /// The intensity of the light source. - double lightIntensity; - - /// A pointer to the background image. - ImageCafe backgroundImage; - - /// A pointer to the destination image. - ImageCafe destImageCafe; - - /// A pointer to the source noise map. - NoiseMap sourceNoiseMap; - - /// Used by the calcLightIntensity() method to recalculate the light - /// values only if the light parameters change. - /// - /// When the light parameters change, this value is set to True. When - /// the calcLightIntensity() method is called, this value is set to - /// false. - boolean recalcLightValues; - - /// The sine of the azimuth of the light source. - double sinAzimuth; - - /// The sine of the elevation of the light source. - double sinElev; - - - public RendererImage () throws ExceptionInvalidParam - { - isLightEnabled = false; - isWrapEnabled = false; - lightAzimuth = 45.0; - lightBrightness = 1.0; - lightColor = new ColorCafe(255, 255, 255, 255); - lightContrast = 1.0; - lightElev = 45.0; - lightIntensity = 1.0; - backgroundImage = null; - destImageCafe = null; - sourceNoiseMap = null; - recalcLightValues = true; - - buildGrayscaleGradient (); - } - - /// Adds a gradient point to this gradient object. - /// - /// @param gradientPos The position of this gradient point. - /// @param gradientColor The color of this gradient point. - /// - /// @pre No two gradient points have the same position. - /// - /// @throw noise::ExceptionInvalidParam See the preconditions. - /// - /// This object uses a color gradient to calculate the color for each - /// pixel in the destination image according to the value from the - /// corresponding position in the noise map. - /// - /// The gradient requires a minimum of two gradient points. - /// - /// The specified color value passed to this method has an alpha - /// channel. This alpha channel specifies how a pixel in the - /// background image (if specified) is blended with the calculated - /// color. If the alpha value is high, this object weighs the blend - /// towards the calculated color, and if the alpha value is low, this - /// object weighs the blend towards the color from the corresponding - /// pixel in the background image. - public void addGradientPoint (double gradientPos, ColorCafe gradientColor) throws ExceptionInvalidParam - { - gradient.addGradientPoint (gradientPos, gradientColor); - } - - /// Builds a grayscale gradient. - /// - /// @post The original gradient is cleared and a grayscale gradient is - /// created. - /// - /// This color gradient contains the following gradient points: - /// - -1.0 maps to black - /// - 1.0 maps to white - public void buildGrayscaleGradient () throws ExceptionInvalidParam - { - clearGradient (); - gradient.addGradientPoint (-1.0, new ColorCafe ( 0, 0, 0, 255)); - gradient.addGradientPoint ( 1.0, new ColorCafe (255, 255, 255, 255)); - } - - /// Builds a color gradient suitable for terrain. - /// - /// @post The original gradient is cleared and a terrain gradient is - /// created. - /// - /// This gradient color at position 0.0 is the "sea level". Above - /// that value, the gradient contains greens, browns, and whites. - /// Below that value, the gradient contains various shades of blue. - public void buildTerrainGradient () throws ExceptionInvalidParam - { - clearGradient (); - gradient.addGradientPoint (-1.00, new ColorCafe ( 0, 0, 128, 255)); - gradient.addGradientPoint (-0.20, new ColorCafe ( 32, 64, 128, 255)); - gradient.addGradientPoint (-0.04, new ColorCafe ( 64, 96, 192, 255)); - gradient.addGradientPoint (-0.02, new ColorCafe (192, 192, 128, 255)); - gradient.addGradientPoint ( 0.00, new ColorCafe ( 0, 192, 0, 255)); - gradient.addGradientPoint ( 0.25, new ColorCafe (192, 192, 0, 255)); - gradient.addGradientPoint ( 0.50, new ColorCafe (160, 96, 64, 255)); - gradient.addGradientPoint ( 0.75, new ColorCafe (128, 255, 255, 255)); - gradient.addGradientPoint ( 1.00, new ColorCafe (255, 255, 255, 255)); - } - - /// Calculates the destination color. - /// - /// @param sourceColor The source color generated from the color - /// gradient. - /// @param backgroundColor The color from the background image at the - /// corresponding position. - /// @param lightValue The intensity of the light at that position. - /// - /// @returns The destination color. - public ColorCafe calcDestColor (ColorCafe sourceColor, ColorCafe backgroundColor, - double lightValue) - { - double sourceRed = (double)sourceColor.red / 255.0; - double sourceGreen = (double)sourceColor.green / 255.0; - double sourceBlue = (double)sourceColor.blue / 255.0; - double sourceAlpha = (double)sourceColor.alpha / 255.0; - double backgroundRed = (double)backgroundColor.red / 255.0; - double backgroundGreen = (double)backgroundColor.green / 255.0; - double backgroundBlue = (double)backgroundColor.blue / 255.0; - - // First, blend the source color to the background color using the alpha - // of the source color. - double red = Interp.linearInterp (backgroundRed, sourceRed , sourceAlpha); - double green = Interp.linearInterp (backgroundGreen, sourceGreen, sourceAlpha); - double blue = Interp.linearInterp (backgroundBlue, sourceBlue , sourceAlpha); - - if (isLightEnabled) - { - // Now calculate the light color. - double lightRed = lightValue * (double)lightColor.red / 255.0; - double lightGreen = lightValue * (double)lightColor.green / 255.0; - double lightBlue = lightValue * (double)lightColor.blue / 255.0; - - // Apply the light color to the new color. - red *= lightRed ; - green *= lightGreen; - blue *= lightBlue ; - } - - // Clamp the color channels to the (0..1) range. - red = (red < 0.0)? 0.0: red ; - red = (red > 1.0)? 1.0: red ; - green = (green < 0.0)? 0.0: green; - green = (green > 1.0)? 1.0: green; - blue = (blue < 0.0)? 0.0: blue ; - blue = (blue > 1.0)? 1.0: blue ; - - // Rescale the color channels to the noise::uint8 (0..255) range and return - // the new color. - ColorCafe newColor = new ColorCafe((int)(red * 255.0) & 0xff,(int)(green * 255.0) & 0xff, - (int)(blue * 255.0) & 0xff, Math.max (sourceColor.alpha, backgroundColor.alpha)); - return newColor; - } - - /// Calculates the intensity of the light given some elevation values. - /// - /// @param center Elevation of the center point. - /// @param left Elevation of the point directly left of the center - /// point. - /// @param right Elevation of the point directly right of the center - /// point. - /// @param down Elevation of the point directly below the center - /// point. - /// @param up Elevation of the point directly above the center point. - /// - /// These values come directly from the noise map. - public double calcLightIntensity (double center, double left, - double right, double down, double up) - { - // Recalculate the sine and cosine of the various light values if - // necessary so it does not have to be calculated each time this method is - // called. - if (recalcLightValues) { - cosAzimuth = Math.cos (Math.toRadians(lightAzimuth)); - sinAzimuth = Math.sin (Math.toRadians(lightAzimuth)); - cosElev = Math.cos (Math.toRadians(lightElev)); - sinElev = Math.sin (Math.toRadians(lightElev)); - recalcLightValues = false; - } - - // Now do the lighting calculations. - double I_MAX = 1.0; - double io = I_MAX * SQRT_2 * sinElev / 2.0; - double ix = (I_MAX - io) * lightContrast * SQRT_2 * cosElev - * cosAzimuth; - double iy = (I_MAX - io) * lightContrast * SQRT_2 * cosElev - * sinAzimuth; - double intensity = (ix * (left - right) + iy * (down - up) + io); - - if (intensity < 0.0) - intensity = 0.0; - - return intensity; - } - - /// Clears the color gradient. - /// - /// Before calling the render() method, the application must specify a - /// new color gradient with at least two gradient points. - public void clearGradient () - { - gradient = new GradientColor(); - gradient.clear(); - } - - /// Renders the destination image using the contents of the source - /// noise map and an optional background image. - /// - /// @pre setSourceNoiseMap() has been previously called. - /// @pre setDestImage() has been previously called. - /// @pre There are at least two gradient points in the color gradient. - /// @pre No two gradient points have the same position. - /// @pre If a background image was specified, it has the exact same - /// size as the source height map. - /// - /// @post The original contents of the destination image is destroyed. - /// - /// @throw ExceptionInvalidParam See the preconditions. - /// - /// The background image and the destination image can safely refer to - /// the same image, although in this case, the destination image is - /// irretrievably blended into the background image. - public void render () throws ExceptionInvalidParam - { - if ( sourceNoiseMap == null - || destImageCafe == null - || sourceNoiseMap.getWidth () <= 0 - || sourceNoiseMap.getHeight () <= 0 - || gradient.getGradientPointCount () < 2) - throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); - - - int width = sourceNoiseMap.getWidth (); - int height = sourceNoiseMap.getHeight (); - - // If a background image was provided, make sure it is the same size the - // source noise map. - if (backgroundImage != null) - if ( backgroundImage.getWidth () != width || backgroundImage.getHeight () != height) - throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); - - - // Create the destination image. It is safe to reuse it if this is also the - // background image. - if (destImageCafe != backgroundImage) - destImageCafe.setSize (width, height); - - for (int y = 0; y < height; y++) - { - ColorCafe background = new ColorCafe(255, 255, 255, 255); - - for (int x = 0; x < width; x++) - { - // Get the color based on the value at the current point in the noise - // map. - ColorCafe destColor = gradient.getColor (sourceNoiseMap.getValue(x,y)); - - // If lighting is enabled, calculate the light intensity based on the - // rate of change at the current point in the noise map. - double lightIntensity; - if (isLightEnabled) - { - // Calculate the positions of the current point's four-neighbors. - int xLeftOffset, xRightOffset; - int yUpOffset , yDownOffset ; - if (isWrapEnabled) - { - if (x == 0) - { - xLeftOffset = (int)width - 1; - xRightOffset = 1;} - else if (x == (int)width - 1) - { - xLeftOffset = -1; - xRightOffset = -((int)width - 1); - } - else - { - xLeftOffset = -1; - xRightOffset = 1; - } - - if (y == 0) - { - yDownOffset = (int)height - 1; - yUpOffset = 1; - } - else if (y == (int)height - 1) - { - yDownOffset = -1; - yUpOffset = -((int)height - 1); - } - else - { - yDownOffset = -1; - yUpOffset = 1; - } - } - else - { - if (x == 0) - { - xLeftOffset = 0; - xRightOffset = 1; - } - else if (x == (int)width - 1) - { - xLeftOffset = -1; - xRightOffset = 0; - } - else - { - xLeftOffset = -1; - xRightOffset = 1; - } - - if (y == 0) - { - yDownOffset = 0; - yUpOffset = 1; - } - else if (y == (int)height - 1) - { - yDownOffset = -1; - yUpOffset = 0; - } - else - { - yDownOffset = -1; - yUpOffset = 1; - } - } - - // Get the noise value of the current point in the source noise map - // and the noise values of its four-neighbors. - double nc = (double)(sourceNoiseMap.getValue(x,y)); - double nl = (double)(sourceNoiseMap.getValue(x + xLeftOffset, y)); - double nr = (double)(sourceNoiseMap.getValue(x + xRightOffset, y)); - double nd = (double)(sourceNoiseMap.getValue(x,y + yDownOffset)); - double nu = (double)(sourceNoiseMap.getValue(x,y + yUpOffset)); - - // Now we can calculate the lighting intensity. - lightIntensity = calcLightIntensity (nc, nl, nr, nd, nu); - lightIntensity *= lightBrightness; - - } - else - { - // These values will apply no lighting to the destination image. - lightIntensity = 1.0; - } - - // Get the current background color from the background image. - ColorCafe backgroundColor = new ColorCafe (255, 255, 255, 255); - if (backgroundImage != null) - backgroundColor = backgroundImage.getValue(x, y); - - // Blend the destination color, background color, and the light - // intensity together, then update the destination image with that - // color. - destImageCafe.setValue(x, y, calcDestColor (destColor, backgroundColor, lightIntensity)); - } - } - } - - /// Enables or disables the light source. - /// - /// @param enable A flag that enables or disables the light source. - /// - /// If the light source is enabled, this object will interpret the - /// noise map as a bump map. - public void enableLight (boolean enable) - { - isLightEnabled = enable; - } - - /// Enables or disables noise-map wrapping. - /// - /// @param enable A flag that enables or disables noise-map wrapping. - /// - /// This object requires five points (the initial point and its four - /// neighbors) to calculate light shading. If wrapping is enabled, - /// and the initial point is on the edge of the noise map, the - /// appropriate neighbors that lie outside of the noise map will - /// "wrap" to the opposite side(s) of the noise map. Otherwise, the - /// appropriate neighbors are cropped to the edge of the noise map. - /// - /// Enabling wrapping is useful when creating spherical renderings and - /// tileable textures. - public void enableWrap (boolean enable) - { - isWrapEnabled = enable; - } - - /// Returns the azimuth of the light source, in degrees. - /// - /// @returns The azimuth of the light source. - /// - /// The azimuth is the location of the light source around the - /// horizon: - /// - 0.0 degrees is east. - /// - 90.0 degrees is north. - /// - 180.0 degrees is west. - /// - 270.0 degrees is south. - public double getLightAzimuth () - { - return lightAzimuth; - } - - /// Returns the brightness of the light source. - /// - /// @returns The brightness of the light source. - public double getLightBrightness () - { - return lightBrightness; - } - - /// Returns the color of the light source. - /// - /// @returns The color of the light source. - public ColorCafe getLightColor () - { - return lightColor; - } - - /// Returns the contrast of the light source. - /// - /// @returns The contrast of the light source. - /// - /// The contrast specifies how sharp the boundary is between the - /// light-facing areas and the shadowed areas. - /// - /// The contrast determines the difference between areas in light and - /// areas in shadow. Determining the correct contrast amount requires - /// some trial and error, but if your application interprets the noise - /// map as a height map that has a spatial resolution of @a h meters - /// and an elevation resolution of 1 meter, a good contrast amount to - /// use is ( 1.0 / @a h ). - public double getLightContrast () - { - return lightContrast; - } - - /// Returns the elevation of the light source, in degrees. - /// - /// @returns The elevation of the light source. - /// - /// The elevation is the angle above the horizon: - /// - 0 degrees is on the horizon. - /// - 90 degrees is straight up. - public double getLightElev () - { - return lightElev; - } - - /// Returns the intensity of the light source. - /// - /// @returns The intensity of the light source. - public double getLightIntensity () - { - return lightIntensity; - } - - /// Determines if the light source is enabled. - /// - /// @returns - /// - @a true if the light source is enabled. - /// - @a false if the light source is disabled. - public boolean isLightEnabled () - { - return isLightEnabled; - } - - /// Determines if noise-map wrapping is enabled. - /// - /// @returns - /// - @a true if noise-map wrapping is enabled. - /// - @a false if noise-map wrapping is disabled. - /// - /// This object requires five points (the initial point and its four - /// neighbors) to calculate light shading. If wrapping is enabled, - /// and the initial point is on the edge of the noise map, the - /// appropriate neighbors that lie outside of the noise map will - /// "wrap" to the opposite side(s) of the noise map. Otherwise, the - /// appropriate neighbors are cropped to the edge of the noise map. - /// - /// Enabling wrapping is useful when creating spherical renderings and - /// tileable textures - public boolean isWrapEnabled () - { - return isWrapEnabled; - } - - /// Sets the background image. - /// - /// @param backgroundImage The background image. - /// - /// If a background image has been specified, the Render() method - /// blends the pixels from the background image onto the corresponding - /// pixels in the destination image. The blending weights are - /// determined by the alpha channel in the pixels in the destination - /// image. - /// - /// The destination image must exist throughout the lifetime of this - /// object unless another image replaces that image. - public void setBackgroundImage (ImageCafe backgroundImage) - { - this.backgroundImage = backgroundImage; - } - - /// Sets the destination image. - /// - /// @param destImage The destination image. - /// - /// The destination image will contain the rendered image after a - /// successful call to the Render() method. - /// - /// The destination image must exist throughout the lifetime of this - /// object unless another image replaces that image. - public void setDestImage (ImageCafe destImage) - { - this.destImageCafe = destImage; - } - - /// Sets the azimuth of the light source, in degrees. - /// - /// @param lightAzimuth The azimuth of the light source. - /// - /// The azimuth is the location of the light source around the - /// horizon: - /// - 0.0 degrees is east. - /// - 90.0 degrees is north. - /// - 180.0 degrees is west. - /// - 270.0 degrees is south. - /// - /// Make sure the light source is enabled via a call to the - /// EnableLight() method before calling the Render() method. - public void setLightAzimuth (double lightAzimuth) - { - this.lightAzimuth = lightAzimuth; - this.recalcLightValues = true; - } - - /// Sets the brightness of the light source. - /// - /// @param lightBrightness The brightness of the light source. - /// - /// Make sure the light source is enabled via a call to the - /// EnableLight() method before calling the Render() method. - public void setLightBrightness (double lightBrightness) - { - this.lightBrightness = lightBrightness; - this.recalcLightValues = true; - } - - /// Sets the color of the light source. - /// - /// @param lightColor The light color. - /// - /// Make sure the light source is enabled via a call to the - /// EnableLight() method before calling the Render() method. - public void setLightColor (ColorCafe lightColor) - { - this.lightColor = lightColor; - } - - /// Sets the contrast of the light source. - /// - /// @param lightContrast The contrast of the light source. - /// - /// @pre The specified light contrast is positive. - /// - /// @throw noise::ExceptionInvalidParam See the preconditions. - /// - /// The contrast specifies how sharp the boundary is between the - /// light-facing areas and the shadowed areas. - /// - /// The contrast determines the difference between areas in light and - /// areas in shadow. Determining the correct contrast amount requires - /// some trial and error, but if your application interprets the noise - /// map as a height map that has a spatial resolution of @a h meters - /// and an elevation resolution of 1 meter, a good contrast amount to - /// use is ( 1.0 / @a h ). - /// - /// Make sure the light source is enabled via a call to the - /// EnableLight() method before calling the Render() method. - public void setLightContrast (double lightContrast) throws ExceptionInvalidParam - { - if (lightContrast <= 0.0) - throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); - - - this.lightContrast = lightContrast; - this.recalcLightValues = true; - } - - /// Sets the elevation of the light source, in degrees. - /// - /// @param lightElev The elevation of the light source. - /// - /// The elevation is the angle above the horizon: - /// - 0 degrees is on the horizon. - /// - 90 degrees is straight up. - /// - /// Make sure the light source is enabled via a call to the - /// EnableLight() method before calling the Render() method. - public void setLightElev (double lightElev) - { - this.lightElev = lightElev; - this.recalcLightValues = true; - } - - /// Sets the intensity of the light source. - /// - /// @returns The intensity of the light source. - /// - /// A good value for intensity is 2.0. - /// - /// Make sure the light source is enabled via a call to the - /// enableLight() method before calling the render() method. - public void setLightIntensity (double lightIntensity) throws ExceptionInvalidParam - { - if (lightIntensity < 0.0) - throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); - - this.lightIntensity = lightIntensity; - this.recalcLightValues = true; - } - - /// Sets the source noise map. - /// - /// @param sourceNoiseMap The source noise map. - /// - /// The destination image must exist throughout the lifetime of this - /// object unless another image replaces that image. - public void setSourceNoiseMap (NoiseMap sourceNoiseMap) - { - this.sourceNoiseMap = sourceNoiseMap; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.Interp; +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class RendererImage +{ + + /// Renders an image from a noise map. + /// + /// This class renders an image given the contents of a noise-map object. + /// + /// An application can configure the output of the image in three ways: + /// - Specify the color gradient. + /// - Specify the light source parameters. + /// - Specify the background image. + /// + /// Specify the color gradient + /// + /// This class uses a color gradient to calculate the color for each pixel + /// in the destination image according to the value from the corresponding + /// position in the noise map. + /// + /// A color gradient is a list of gradually-changing colors. A color + /// gradient is defined by a list of gradient points. Each + /// gradient point has a position and a color. In a color gradient, the + /// colors between two adjacent gradient points are linearly interpolated. + /// + /// For example, suppose this class contains the following color gradient: + /// + /// - -1.0 maps to dark blue. + /// - -0.2 maps to light blue. + /// - -0.1 maps to tan. + /// - 0.0 maps to green. + /// - 1.0 maps to white. + /// + /// The value 0.5 maps to a greenish-white color because 0.5 is halfway + /// between 0.0 (mapped to green) and 1.0 (mapped to white). + /// + /// The value -0.6 maps to a medium blue color because -0.6 is halfway + /// between -1.0 (mapped to dark blue) and -0.2 (mapped to light blue). + /// + /// The color gradient requires a minimum of two gradient points. + /// + /// This class contains two pre-made gradients: a grayscale gradient and a + /// color gradient suitable for terrain. To use these pre-made gradients, + /// call the buildGrayscaleGradient() or buildTerrainGradient() methods, + /// respectively. + /// + /// @note The color value passed to addGradientPoint() has an alpha + /// channel. This alpha channel specifies how a pixel in the background + /// image (if specified) is blended with the calculated color. If the + /// alpha value is high, this class weighs the blend towards the + /// calculated color, and if the alpha value is low, this class weighs the + /// blend towards the color from the corresponding pixel in the background + /// image. + /// + /// Specify the light source parameters + /// + /// This class contains a parallel light source that lights the image. It + /// interprets the noise map as a bump map. + /// + /// To enable or disable lighting, pass a Boolean value to the + /// enableLight() method. + /// + /// To set the position of the light source in the "sky", call the + /// setLightAzimuth() and setLightElev() methods. + /// + /// To set the color of the light source, call the setLightColor() method. + /// + /// To set the intensity of the light source, call the setLightIntensity() + /// method. A good intensity value is 2.0, although that value tends to + /// "wash out" very light colors from the image. + /// + /// To set the contrast amount between areas in light and areas in shadow, + /// call the setLightContrast() method. Determining the correct contrast + /// amount requires some trial and error, but if your application + /// interprets the noise map as a height map that has its elevation values + /// measured in meters and has a horizontal resolution of @a h meters, a + /// good contrast amount to use is ( 1.0 / @a h ). + /// + /// Specify the background image + /// + /// To specify a background image, pass an Image object to the + /// setBackgroundImage() method. + /// + /// This class determines the color of a pixel in the destination image by + /// blending the calculated color with the color of the corresponding + /// pixel from the background image. + /// + /// The blend amount is determined by the alpha of the calculated color. + /// If the alpha value is high, this class weighs the blend towards the + /// calculated color, and if the alpha value is low, this class weighs the + /// blend towards the color from the corresponding pixel in the background + /// image. + /// + /// Rendering the image + /// + /// To render the image, perform the following steps: + /// - Pass a NoiseMap object to the setSourceNoiseMap() method. + /// - Pass an ImageCafe object to the setDestImage() method. + /// - Pass an ImageCafe object to the setBackgroundImage() method (optional) + /// - Call the render() method. + + + static final double SQRT_2 = 1.4142135623730950488; + + + /// The cosine of the azimuth of the light source. + double cosAzimuth; + + /// The cosine of the elevation of the light source. + double cosElev; + + /// The color gradient used to specify the image colors. + GradientColor gradient; + + /// A flag specifying whether lighting is enabled. + boolean isLightEnabled; + + /// A flag specifying whether wrapping is enabled. + boolean isWrapEnabled; + + /// The azimuth of the light source, in degrees. + double lightAzimuth; + + /// The brightness of the light source. + double lightBrightness; + + /// The color of the light source. + ColorCafe lightColor; + + /// The contrast between areas in light and areas in shadow. + double lightContrast; + + /// The elevation of the light source, in degrees. + double lightElev; + + /// The intensity of the light source. + double lightIntensity; + + /// A pointer to the background image. + ImageCafe backgroundImage; + + /// A pointer to the destination image. + ImageCafe destImageCafe; + + /// A pointer to the source noise map. + NoiseMap sourceNoiseMap; + + /// Used by the calcLightIntensity() method to recalculate the light + /// values only if the light parameters change. + /// + /// When the light parameters change, this value is set to True. When + /// the calcLightIntensity() method is called, this value is set to + /// false. + boolean recalcLightValues; + + /// The sine of the azimuth of the light source. + double sinAzimuth; + + /// The sine of the elevation of the light source. + double sinElev; + + + public RendererImage () throws ExceptionInvalidParam + { + isLightEnabled = false; + isWrapEnabled = false; + lightAzimuth = 45.0; + lightBrightness = 1.0; + lightColor = new ColorCafe(255, 255, 255, 255); + lightContrast = 1.0; + lightElev = 45.0; + lightIntensity = 1.0; + backgroundImage = null; + destImageCafe = null; + sourceNoiseMap = null; + recalcLightValues = true; + + buildGrayscaleGradient (); + } + + /// Adds a gradient point to this gradient object. + /// + /// @param gradientPos The position of this gradient point. + /// @param gradientColor The color of this gradient point. + /// + /// @pre No two gradient points have the same position. + /// + /// @throw noise::ExceptionInvalidParam See the preconditions. + /// + /// This object uses a color gradient to calculate the color for each + /// pixel in the destination image according to the value from the + /// corresponding position in the noise map. + /// + /// The gradient requires a minimum of two gradient points. + /// + /// The specified color value passed to this method has an alpha + /// channel. This alpha channel specifies how a pixel in the + /// background image (if specified) is blended with the calculated + /// color. If the alpha value is high, this object weighs the blend + /// towards the calculated color, and if the alpha value is low, this + /// object weighs the blend towards the color from the corresponding + /// pixel in the background image. + public void addGradientPoint (double gradientPos, ColorCafe gradientColor) throws ExceptionInvalidParam + { + gradient.addGradientPoint (gradientPos, gradientColor); + } + + /// Builds a grayscale gradient. + /// + /// @post The original gradient is cleared and a grayscale gradient is + /// created. + /// + /// This color gradient contains the following gradient points: + /// - -1.0 maps to black + /// - 1.0 maps to white + public void buildGrayscaleGradient () throws ExceptionInvalidParam + { + clearGradient (); + gradient.addGradientPoint (-1.0, new ColorCafe ( 0, 0, 0, 255)); + gradient.addGradientPoint ( 1.0, new ColorCafe (255, 255, 255, 255)); + } + + /// Builds a color gradient suitable for terrain. + /// + /// @post The original gradient is cleared and a terrain gradient is + /// created. + /// + /// This gradient color at position 0.0 is the "sea level". Above + /// that value, the gradient contains greens, browns, and whites. + /// Below that value, the gradient contains various shades of blue. + public void buildTerrainGradient () throws ExceptionInvalidParam + { + clearGradient (); + gradient.addGradientPoint (-1.00, new ColorCafe ( 0, 0, 128, 255)); + gradient.addGradientPoint (-0.20, new ColorCafe ( 32, 64, 128, 255)); + gradient.addGradientPoint (-0.04, new ColorCafe ( 64, 96, 192, 255)); + gradient.addGradientPoint (-0.02, new ColorCafe (192, 192, 128, 255)); + gradient.addGradientPoint ( 0.00, new ColorCafe ( 0, 192, 0, 255)); + gradient.addGradientPoint ( 0.25, new ColorCafe (192, 192, 0, 255)); + gradient.addGradientPoint ( 0.50, new ColorCafe (160, 96, 64, 255)); + gradient.addGradientPoint ( 0.75, new ColorCafe (128, 255, 255, 255)); + gradient.addGradientPoint ( 1.00, new ColorCafe (255, 255, 255, 255)); + } + + /// Calculates the destination color. + /// + /// @param sourceColor The source color generated from the color + /// gradient. + /// @param backgroundColor The color from the background image at the + /// corresponding position. + /// @param lightValue The intensity of the light at that position. + /// + /// @returns The destination color. + public ColorCafe calcDestColor (ColorCafe sourceColor, ColorCafe backgroundColor, + double lightValue) + { + double sourceRed = (double)sourceColor.red / 255.0; + double sourceGreen = (double)sourceColor.green / 255.0; + double sourceBlue = (double)sourceColor.blue / 255.0; + double sourceAlpha = (double)sourceColor.alpha / 255.0; + double backgroundRed = (double)backgroundColor.red / 255.0; + double backgroundGreen = (double)backgroundColor.green / 255.0; + double backgroundBlue = (double)backgroundColor.blue / 255.0; + + // First, blend the source color to the background color using the alpha + // of the source color. + double red = Interp.linearInterp (backgroundRed, sourceRed , sourceAlpha); + double green = Interp.linearInterp (backgroundGreen, sourceGreen, sourceAlpha); + double blue = Interp.linearInterp (backgroundBlue, sourceBlue , sourceAlpha); + + if (isLightEnabled) + { + // Now calculate the light color. + double lightRed = lightValue * (double)lightColor.red / 255.0; + double lightGreen = lightValue * (double)lightColor.green / 255.0; + double lightBlue = lightValue * (double)lightColor.blue / 255.0; + + // Apply the light color to the new color. + red *= lightRed ; + green *= lightGreen; + blue *= lightBlue ; + } + + // Clamp the color channels to the (0..1) range. + red = (red < 0.0)? 0.0: red ; + red = (red > 1.0)? 1.0: red ; + green = (green < 0.0)? 0.0: green; + green = (green > 1.0)? 1.0: green; + blue = (blue < 0.0)? 0.0: blue ; + blue = (blue > 1.0)? 1.0: blue ; + + // Rescale the color channels to the noise::uint8 (0..255) range and return + // the new color. + ColorCafe newColor = new ColorCafe((int)(red * 255.0) & 0xff,(int)(green * 255.0) & 0xff, + (int)(blue * 255.0) & 0xff, Math.max (sourceColor.alpha, backgroundColor.alpha)); + return newColor; + } + + /// Calculates the intensity of the light given some elevation values. + /// + /// @param center Elevation of the center point. + /// @param left Elevation of the point directly left of the center + /// point. + /// @param right Elevation of the point directly right of the center + /// point. + /// @param down Elevation of the point directly below the center + /// point. + /// @param up Elevation of the point directly above the center point. + /// + /// These values come directly from the noise map. + public double calcLightIntensity (double center, double left, + double right, double down, double up) + { + // Recalculate the sine and cosine of the various light values if + // necessary so it does not have to be calculated each time this method is + // called. + if (recalcLightValues) { + cosAzimuth = Math.cos (Math.toRadians(lightAzimuth)); + sinAzimuth = Math.sin (Math.toRadians(lightAzimuth)); + cosElev = Math.cos (Math.toRadians(lightElev)); + sinElev = Math.sin (Math.toRadians(lightElev)); + recalcLightValues = false; + } + + // Now do the lighting calculations. + double I_MAX = 1.0; + double io = I_MAX * SQRT_2 * sinElev / 2.0; + double ix = (I_MAX - io) * lightContrast * SQRT_2 * cosElev + * cosAzimuth; + double iy = (I_MAX - io) * lightContrast * SQRT_2 * cosElev + * sinAzimuth; + double intensity = (ix * (left - right) + iy * (down - up) + io); + + if (intensity < 0.0) + intensity = 0.0; + + return intensity; + } + + /// Clears the color gradient. + /// + /// Before calling the render() method, the application must specify a + /// new color gradient with at least two gradient points. + public void clearGradient () + { + gradient = new GradientColor(); + gradient.clear(); + } + + /// Renders the destination image using the contents of the source + /// noise map and an optional background image. + /// + /// @pre setSourceNoiseMap() has been previously called. + /// @pre setDestImage() has been previously called. + /// @pre There are at least two gradient points in the color gradient. + /// @pre No two gradient points have the same position. + /// @pre If a background image was specified, it has the exact same + /// size as the source height map. + /// + /// @post The original contents of the destination image is destroyed. + /// + /// @throw ExceptionInvalidParam See the preconditions. + /// + /// The background image and the destination image can safely refer to + /// the same image, although in this case, the destination image is + /// irretrievably blended into the background image. + public void render () throws ExceptionInvalidParam + { + if ( sourceNoiseMap == null + || destImageCafe == null + || sourceNoiseMap.getWidth () <= 0 + || sourceNoiseMap.getHeight () <= 0 + || gradient.getGradientPointCount () < 2) + throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); + + + int width = sourceNoiseMap.getWidth (); + int height = sourceNoiseMap.getHeight (); + + // If a background image was provided, make sure it is the same size the + // source noise map. + if (backgroundImage != null) + if ( backgroundImage.getWidth () != width || backgroundImage.getHeight () != height) + throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); + + + // Create the destination image. It is safe to reuse it if this is also the + // background image. + if (destImageCafe != backgroundImage) + destImageCafe.setSize (width, height); + + for (int y = 0; y < height; y++) + { + ColorCafe background = new ColorCafe(255, 255, 255, 255); + + for (int x = 0; x < width; x++) + { + // Get the color based on the value at the current point in the noise + // map. + ColorCafe destColor = gradient.getColor (sourceNoiseMap.getValue(x,y)); + + // If lighting is enabled, calculate the light intensity based on the + // rate of change at the current point in the noise map. + double lightIntensity; + if (isLightEnabled) + { + // Calculate the positions of the current point's four-neighbors. + int xLeftOffset, xRightOffset; + int yUpOffset , yDownOffset ; + if (isWrapEnabled) + { + if (x == 0) + { + xLeftOffset = (int)width - 1; + xRightOffset = 1;} + else if (x == (int)width - 1) + { + xLeftOffset = -1; + xRightOffset = -((int)width - 1); + } + else + { + xLeftOffset = -1; + xRightOffset = 1; + } + + if (y == 0) + { + yDownOffset = (int)height - 1; + yUpOffset = 1; + } + else if (y == (int)height - 1) + { + yDownOffset = -1; + yUpOffset = -((int)height - 1); + } + else + { + yDownOffset = -1; + yUpOffset = 1; + } + } + else + { + if (x == 0) + { + xLeftOffset = 0; + xRightOffset = 1; + } + else if (x == (int)width - 1) + { + xLeftOffset = -1; + xRightOffset = 0; + } + else + { + xLeftOffset = -1; + xRightOffset = 1; + } + + if (y == 0) + { + yDownOffset = 0; + yUpOffset = 1; + } + else if (y == (int)height - 1) + { + yDownOffset = -1; + yUpOffset = 0; + } + else + { + yDownOffset = -1; + yUpOffset = 1; + } + } + + // Get the noise value of the current point in the source noise map + // and the noise values of its four-neighbors. + double nc = (double)(sourceNoiseMap.getValue(x,y)); + double nl = (double)(sourceNoiseMap.getValue(x + xLeftOffset, y)); + double nr = (double)(sourceNoiseMap.getValue(x + xRightOffset, y)); + double nd = (double)(sourceNoiseMap.getValue(x,y + yDownOffset)); + double nu = (double)(sourceNoiseMap.getValue(x,y + yUpOffset)); + + // Now we can calculate the lighting intensity. + lightIntensity = calcLightIntensity (nc, nl, nr, nd, nu); + lightIntensity *= lightBrightness; + + } + else + { + // These values will apply no lighting to the destination image. + lightIntensity = 1.0; + } + + // Get the current background color from the background image. + ColorCafe backgroundColor = new ColorCafe (255, 255, 255, 255); + if (backgroundImage != null) + backgroundColor = backgroundImage.getValue(x, y); + + // Blend the destination color, background color, and the light + // intensity together, then update the destination image with that + // color. + destImageCafe.setValue(x, y, calcDestColor (destColor, backgroundColor, lightIntensity)); + } + } + } + + /// Enables or disables the light source. + /// + /// @param enable A flag that enables or disables the light source. + /// + /// If the light source is enabled, this object will interpret the + /// noise map as a bump map. + public void enableLight (boolean enable) + { + isLightEnabled = enable; + } + + /// Enables or disables noise-map wrapping. + /// + /// @param enable A flag that enables or disables noise-map wrapping. + /// + /// This object requires five points (the initial point and its four + /// neighbors) to calculate light shading. If wrapping is enabled, + /// and the initial point is on the edge of the noise map, the + /// appropriate neighbors that lie outside of the noise map will + /// "wrap" to the opposite side(s) of the noise map. Otherwise, the + /// appropriate neighbors are cropped to the edge of the noise map. + /// + /// Enabling wrapping is useful when creating spherical renderings and + /// tileable textures. + public void enableWrap (boolean enable) + { + isWrapEnabled = enable; + } + + /// Returns the azimuth of the light source, in degrees. + /// + /// @returns The azimuth of the light source. + /// + /// The azimuth is the location of the light source around the + /// horizon: + /// - 0.0 degrees is east. + /// - 90.0 degrees is north. + /// - 180.0 degrees is west. + /// - 270.0 degrees is south. + public double getLightAzimuth () + { + return lightAzimuth; + } + + /// Returns the brightness of the light source. + /// + /// @returns The brightness of the light source. + public double getLightBrightness () + { + return lightBrightness; + } + + /// Returns the color of the light source. + /// + /// @returns The color of the light source. + public ColorCafe getLightColor () + { + return lightColor; + } + + /// Returns the contrast of the light source. + /// + /// @returns The contrast of the light source. + /// + /// The contrast specifies how sharp the boundary is between the + /// light-facing areas and the shadowed areas. + /// + /// The contrast determines the difference between areas in light and + /// areas in shadow. Determining the correct contrast amount requires + /// some trial and error, but if your application interprets the noise + /// map as a height map that has a spatial resolution of @a h meters + /// and an elevation resolution of 1 meter, a good contrast amount to + /// use is ( 1.0 / @a h ). + public double getLightContrast () + { + return lightContrast; + } + + /// Returns the elevation of the light source, in degrees. + /// + /// @returns The elevation of the light source. + /// + /// The elevation is the angle above the horizon: + /// - 0 degrees is on the horizon. + /// - 90 degrees is straight up. + public double getLightElev () + { + return lightElev; + } + + /// Returns the intensity of the light source. + /// + /// @returns The intensity of the light source. + public double getLightIntensity () + { + return lightIntensity; + } + + /// Determines if the light source is enabled. + /// + /// @returns + /// - @a true if the light source is enabled. + /// - @a false if the light source is disabled. + public boolean isLightEnabled () + { + return isLightEnabled; + } + + /// Determines if noise-map wrapping is enabled. + /// + /// @returns + /// - @a true if noise-map wrapping is enabled. + /// - @a false if noise-map wrapping is disabled. + /// + /// This object requires five points (the initial point and its four + /// neighbors) to calculate light shading. If wrapping is enabled, + /// and the initial point is on the edge of the noise map, the + /// appropriate neighbors that lie outside of the noise map will + /// "wrap" to the opposite side(s) of the noise map. Otherwise, the + /// appropriate neighbors are cropped to the edge of the noise map. + /// + /// Enabling wrapping is useful when creating spherical renderings and + /// tileable textures + public boolean isWrapEnabled () + { + return isWrapEnabled; + } + + /// Sets the background image. + /// + /// @param backgroundImage The background image. + /// + /// If a background image has been specified, the Render() method + /// blends the pixels from the background image onto the corresponding + /// pixels in the destination image. The blending weights are + /// determined by the alpha channel in the pixels in the destination + /// image. + /// + /// The destination image must exist throughout the lifetime of this + /// object unless another image replaces that image. + public void setBackgroundImage (ImageCafe backgroundImage) + { + this.backgroundImage = backgroundImage; + } + + /// Sets the destination image. + /// + /// @param destImage The destination image. + /// + /// The destination image will contain the rendered image after a + /// successful call to the Render() method. + /// + /// The destination image must exist throughout the lifetime of this + /// object unless another image replaces that image. + public void setDestImage (ImageCafe destImage) + { + this.destImageCafe = destImage; + } + + /// Sets the azimuth of the light source, in degrees. + /// + /// @param lightAzimuth The azimuth of the light source. + /// + /// The azimuth is the location of the light source around the + /// horizon: + /// - 0.0 degrees is east. + /// - 90.0 degrees is north. + /// - 180.0 degrees is west. + /// - 270.0 degrees is south. + /// + /// Make sure the light source is enabled via a call to the + /// EnableLight() method before calling the Render() method. + public void setLightAzimuth (double lightAzimuth) + { + this.lightAzimuth = lightAzimuth; + this.recalcLightValues = true; + } + + /// Sets the brightness of the light source. + /// + /// @param lightBrightness The brightness of the light source. + /// + /// Make sure the light source is enabled via a call to the + /// EnableLight() method before calling the Render() method. + public void setLightBrightness (double lightBrightness) + { + this.lightBrightness = lightBrightness; + this.recalcLightValues = true; + } + + /// Sets the color of the light source. + /// + /// @param lightColor The light color. + /// + /// Make sure the light source is enabled via a call to the + /// EnableLight() method before calling the Render() method. + public void setLightColor (ColorCafe lightColor) + { + this.lightColor = lightColor; + } + + /// Sets the contrast of the light source. + /// + /// @param lightContrast The contrast of the light source. + /// + /// @pre The specified light contrast is positive. + /// + /// @throw noise::ExceptionInvalidParam See the preconditions. + /// + /// The contrast specifies how sharp the boundary is between the + /// light-facing areas and the shadowed areas. + /// + /// The contrast determines the difference between areas in light and + /// areas in shadow. Determining the correct contrast amount requires + /// some trial and error, but if your application interprets the noise + /// map as a height map that has a spatial resolution of @a h meters + /// and an elevation resolution of 1 meter, a good contrast amount to + /// use is ( 1.0 / @a h ). + /// + /// Make sure the light source is enabled via a call to the + /// EnableLight() method before calling the Render() method. + public void setLightContrast (double lightContrast) throws ExceptionInvalidParam + { + if (lightContrast <= 0.0) + throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); + + + this.lightContrast = lightContrast; + this.recalcLightValues = true; + } + + /// Sets the elevation of the light source, in degrees. + /// + /// @param lightElev The elevation of the light source. + /// + /// The elevation is the angle above the horizon: + /// - 0 degrees is on the horizon. + /// - 90 degrees is straight up. + /// + /// Make sure the light source is enabled via a call to the + /// EnableLight() method before calling the Render() method. + public void setLightElev (double lightElev) + { + this.lightElev = lightElev; + this.recalcLightValues = true; + } + + /// Sets the intensity of the light source. + /// + /// @returns The intensity of the light source. + /// + /// A good value for intensity is 2.0. + /// + /// Make sure the light source is enabled via a call to the + /// enableLight() method before calling the render() method. + public void setLightIntensity (double lightIntensity) throws ExceptionInvalidParam + { + if (lightIntensity < 0.0) + throw new ExceptionInvalidParam ("Invalid Parameter in RendererImage"); + + this.lightIntensity = lightIntensity; + this.recalcLightValues = true; + } + + /// Sets the source noise map. + /// + /// @param sourceNoiseMap The source noise map. + /// + /// The destination image must exist throughout the lifetime of this + /// object unless another image replaces that image. + public void setSourceNoiseMap (NoiseMap sourceNoiseMap) + { + this.sourceNoiseMap = sourceNoiseMap; + } + +} diff --git a/src/libnoiseforjava/util/RendererNormalMap.java b/src/libnoiseforjava/util/RendererNormalMap.java index f9320a5..c3b648b 100644 --- a/src/libnoiseforjava/util/RendererNormalMap.java +++ b/src/libnoiseforjava/util/RendererNormalMap.java @@ -1,316 +1,316 @@ -/* - * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) - * Copyright © 2010 Thomas J. Hodge (java port of libnoise) - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava.util; - -import libnoiseforjava.exception.ExceptionInvalidParam; - -public class RendererNormalMap -{ - /// Renders a normal map from a noise map. - /// - /// This class renders an image containing the normal vectors from a noise - /// map object. This image can then be used as a bump map for a 3D - /// application or game. - /// - /// This class encodes the (x, y, z) components of the normal vector into - /// the (red, green, blue) channels of the image. Like any 24-bit - /// true-color image, the channel values range from 0 to 255. 0 - /// represents a normal coordinate of -1.0 and 255 represents a normal - /// coordinate of +1.0. - /// - /// You should also specify the bump height before rendering the - /// normal map. The bump height specifies the ratio of spatial resolution - /// to elevation resolution. For example, if your noise map has a spatial - /// resolution of 30 meters and an elevation resolution of one meter, set - /// the bump height to 1.0 / 30.0. - /// - /// Rendering the normal map - /// - /// To render the image containing the normal map, perform the following - /// steps: - /// - Pass a NoiseMap object to the setSourceNoiseMap() method. - /// - Pass an ImageCafe object to the setDestImage() method. - /// - Call the render() method. - - /// The bump height for the normal map. - double bumpHeight; - - /// A flag specifying whether wrapping is enabled. - boolean isWrapEnabled; - - /// A pointer to the destination image. - ImageCafe destImageCafe; - - /// A pointer to the source noise map. - NoiseMap sourceNoiseMap; - - - public RendererNormalMap () throws ExceptionInvalidParam - { - bumpHeight = 1.0; - isWrapEnabled = false; - destImageCafe = new ImageCafe(0,0); - sourceNoiseMap = new NoiseMap(0,0); - } - - public RendererNormalMap (int height, int width) throws ExceptionInvalidParam - { - bumpHeight = 1.0; - isWrapEnabled = false; - destImageCafe = new ImageCafe(height, width); - sourceNoiseMap = new NoiseMap(height, width); - } - - /// Calculates the normal vector at a given point on the noise map. - /// - /// @param nc The height of the given point in the noise map. - /// @param nr The height of the left neighbor. - /// @param nu The height of the up neighbor. - /// @param bumpHeight The bump height. - /// - /// @returns The normal vector represented as a color. - /// - /// This method encodes the (x, y, z) components of the normal vector - /// into the (red, green, blue) channels of the returned color. In - /// order to represent the vector as a color, each coordinate of the - /// normal is mapped from the -1.0 to 1.0 range to the 0 to 255 range. - /// - /// The bump height specifies the ratio of spatial resolution to - /// elevation resolution. For example, if your noise map has a - /// spatial resolution of 30 meters and an elevation resolution of one - /// meter, set the bump height to 1.0 / 30.0. - /// - /// The spatial resolution and elevation resolution are determined by - /// the application. - public ColorCafe calcNormalColor (double nc, double nr, double nu, - double bumpHeight) - { - // Calculate the surface normal. - nc *= bumpHeight; - nr *= bumpHeight; - nu *= bumpHeight; - double ncr = (nc - nr); - double ncu = (nc - nu); - double d = Math.sqrt ((ncu * ncu) + (ncr * ncr) + 1); - double vxc = (nc - nr) / d; - double vyc = (nc - nu) / d; - double vzc = 1.0 / d; - - // Map the normal range from the (-1.0 .. +1.0) range to the (0 .. 255) - // range. - int xc, yc, zc; - xc = (int) (Math.floor((vxc + 1.0) * 127.5)) & 0xff; - yc = (int) (Math.floor((vyc + 1.0) * 127.5)) & 0xff; - zc = (int) (Math.floor((vzc + 1.0) * 127.5)) & 0xff; - - // left as example of what was here in case above conversion doesn't work. - //zc = (noise::uint8)((noise::uint)((floor)((vzc + 1.0) * 127.5)) & 0xff); - - return new ColorCafe (xc, yc, zc, 255); - } - - /// Renders the noise map to the destination image. - /// - /// @pre setSourceNoiseMap() has been previously called. - /// @pre setDestImage() has been previously called. - /// - /// @post The original contents of the destination image is destroyed. - /// - /// @throw ExceptionInvalidParam See the preconditions. - public void render () throws ExceptionInvalidParam - { - if ( sourceNoiseMap == null - || destImageCafe == null - || sourceNoiseMap.getWidth () <= 0 - || sourceNoiseMap.getHeight () <= 0) - throw new ExceptionInvalidParam ("Invalid Parameter in RendererNormalMap"); - - - int width = sourceNoiseMap.getWidth (); - int height = sourceNoiseMap.getHeight (); - - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) - { - // Calculate the positions of the current point's right and up - // neighbors. - int xRightOffset, yUpOffset; - if (isWrapEnabled) - { - if (x == (int)width - 1) - xRightOffset = -((int)width - 1); - else - xRightOffset = 1; - - if (y == (int)height - 1) - yUpOffset = -((int)height - 1); - else - yUpOffset = 1; - } - else - { - if (x == (int)width - 1) - xRightOffset = 0; - else - xRightOffset = 1; - - if (y == (int)height - 1) - yUpOffset = 0; - else - yUpOffset = 1; - - } - - // Get the noise value of the current point in the source noise map - // and the noise values of its right and up neighbors. - double nc = (double)(sourceNoiseMap.getValue(x, y)); - double nr = (double)(sourceNoiseMap.getValue((x + xRightOffset),y)); - double nu = (double)(sourceNoiseMap.getValue(x, (y + yUpOffset))); - - // Calculate the normal product. - destImageCafe.setValue(x,y, (calcNormalColor (nc, nr, nu, bumpHeight))); - - // Go to the next point. - //++pSource; - //++pDest; - } - } - } - - /// Enables or disables noise-map wrapping. - /// - /// @param enable A flag that enables or disables noise-map wrapping. - /// - /// This object requires three points (the initial point and the right - /// and up neighbors) to calculate the normal vector at that point. - /// If wrapping is/ enabled, and the initial point is on the edge of - /// the noise map, the appropriate neighbors that lie outside of the - /// noise map will "wrap" to the opposite side(s) of the noise map. - /// Otherwise, the appropriate neighbors are cropped to the edge of - /// the noise map. - /// - /// Enabling wrapping is useful when creating spherical and tileable - /// normal maps. - public void enableWrap (boolean enable) - { - isWrapEnabled = enable; - } - - /// Returns the bump height. - /// - /// @returns The bump height. - /// - /// The bump height specifies the ratio of spatial resolution to - /// elevation resolution. For example, if your noise map has a - /// spatial resolution of 30 meters and an elevation resolution of one - /// meter, set the bump height to 1.0 / 30.0. - /// - /// The spatial resolution and elevation resolution are determined by - /// the application. - public double getBumpHeight () - { - return bumpHeight; - } - - /// Determines if noise-map wrapping is enabled. - /// - /// @returns - /// - @a true if noise-map wrapping is enabled. - /// - @a false if noise-map wrapping is disabled. - /// - /// This object requires three points (the initial point and the right - /// and up neighbors) to calculate the normal vector at that point. - /// If wrapping is/ enabled, and the initial point is on the edge of - /// the noise map, the appropriate neighbors that lie outside of the - /// noise map will "wrap" to the opposite side(s) of the noise map. - /// Otherwise, the appropriate neighbors are cropped to the edge of - /// the noise map. - /// - /// Enabling wrapping is useful when creating spherical and tileable - /// normal maps. - public boolean isWrapEnabled () - { - return isWrapEnabled; - } - - /// Sets the bump height. - /// - /// @param bumpHeight The bump height. - /// - /// The bump height specifies the ratio of spatial resolution to - /// elevation resolution. For example, if your noise map has a - /// spatial resolution of 30 meters and an elevation resolution of one - /// meter, set the bump height to 1.0 / 30.0. - /// - /// The spatial resolution and elevation resolution are determined by - /// the application. - public void setBumpHeight (double bumpHeight) - { - this.bumpHeight = bumpHeight; - } - - /// Sets the destination image. - /// - /// @param destImage The destination image. - /// - /// The destination image will contain the normal map after a - /// successful call to the render() method. - /// - /// The destination image must exist throughout the lifetime of this - /// object unless another image replaces that image. - public void setDestImage (ImageCafe destImage) - { - this.destImageCafe = destImage; - } - - /// Sets the source noise map. - /// - /// @param sourceNoiseMap The source noise map. - /// - /// The destination image must exist throughout the lifetime of this - /// object unless another image replaces that image. - public void setSourceNoiseMap (NoiseMap sourceNoiseMap) - { - this.sourceNoiseMap = sourceNoiseMap; - } - - public ImageCafe getDestImageCafe() - { - return destImageCafe; - } - - public NoiseMap getSourceNoiseMap() - { - return sourceNoiseMap; - } - - public void setDestImageCafe(ImageCafe destImageCafe) - { - this.destImageCafe = destImageCafe; - } - -} +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava.util; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +public class RendererNormalMap +{ + /// Renders a normal map from a noise map. + /// + /// This class renders an image containing the normal vectors from a noise + /// map object. This image can then be used as a bump map for a 3D + /// application or game. + /// + /// This class encodes the (x, y, z) components of the normal vector into + /// the (red, green, blue) channels of the image. Like any 24-bit + /// true-color image, the channel values range from 0 to 255. 0 + /// represents a normal coordinate of -1.0 and 255 represents a normal + /// coordinate of +1.0. + /// + /// You should also specify the bump height before rendering the + /// normal map. The bump height specifies the ratio of spatial resolution + /// to elevation resolution. For example, if your noise map has a spatial + /// resolution of 30 meters and an elevation resolution of one meter, set + /// the bump height to 1.0 / 30.0. + /// + /// Rendering the normal map + /// + /// To render the image containing the normal map, perform the following + /// steps: + /// - Pass a NoiseMap object to the setSourceNoiseMap() method. + /// - Pass an ImageCafe object to the setDestImage() method. + /// - Call the render() method. + + /// The bump height for the normal map. + double bumpHeight; + + /// A flag specifying whether wrapping is enabled. + boolean isWrapEnabled; + + /// A pointer to the destination image. + ImageCafe destImageCafe; + + /// A pointer to the source noise map. + NoiseMap sourceNoiseMap; + + + public RendererNormalMap () throws ExceptionInvalidParam + { + bumpHeight = 1.0; + isWrapEnabled = false; + destImageCafe = new ImageCafe(0,0); + sourceNoiseMap = new NoiseMap(0,0); + } + + public RendererNormalMap (int height, int width) throws ExceptionInvalidParam + { + bumpHeight = 1.0; + isWrapEnabled = false; + destImageCafe = new ImageCafe(height, width); + sourceNoiseMap = new NoiseMap(height, width); + } + + /// Calculates the normal vector at a given point on the noise map. + /// + /// @param nc The height of the given point in the noise map. + /// @param nr The height of the left neighbor. + /// @param nu The height of the up neighbor. + /// @param bumpHeight The bump height. + /// + /// @returns The normal vector represented as a color. + /// + /// This method encodes the (x, y, z) components of the normal vector + /// into the (red, green, blue) channels of the returned color. In + /// order to represent the vector as a color, each coordinate of the + /// normal is mapped from the -1.0 to 1.0 range to the 0 to 255 range. + /// + /// The bump height specifies the ratio of spatial resolution to + /// elevation resolution. For example, if your noise map has a + /// spatial resolution of 30 meters and an elevation resolution of one + /// meter, set the bump height to 1.0 / 30.0. + /// + /// The spatial resolution and elevation resolution are determined by + /// the application. + public ColorCafe calcNormalColor (double nc, double nr, double nu, + double bumpHeight) + { + // Calculate the surface normal. + nc *= bumpHeight; + nr *= bumpHeight; + nu *= bumpHeight; + double ncr = (nc - nr); + double ncu = (nc - nu); + double d = Math.sqrt ((ncu * ncu) + (ncr * ncr) + 1); + double vxc = (nc - nr) / d; + double vyc = (nc - nu) / d; + double vzc = 1.0 / d; + + // Map the normal range from the (-1.0 .. +1.0) range to the (0 .. 255) + // range. + int xc, yc, zc; + xc = (int) (Math.floor((vxc + 1.0) * 127.5)) & 0xff; + yc = (int) (Math.floor((vyc + 1.0) * 127.5)) & 0xff; + zc = (int) (Math.floor((vzc + 1.0) * 127.5)) & 0xff; + + // left as example of what was here in case above conversion doesn't work. + //zc = (noise::uint8)((noise::uint)((floor)((vzc + 1.0) * 127.5)) & 0xff); + + return new ColorCafe (xc, yc, zc, 255); + } + + /// Renders the noise map to the destination image. + /// + /// @pre setSourceNoiseMap() has been previously called. + /// @pre setDestImage() has been previously called. + /// + /// @post The original contents of the destination image is destroyed. + /// + /// @throw ExceptionInvalidParam See the preconditions. + public void render () throws ExceptionInvalidParam + { + if ( sourceNoiseMap == null + || destImageCafe == null + || sourceNoiseMap.getWidth () <= 0 + || sourceNoiseMap.getHeight () <= 0) + throw new ExceptionInvalidParam ("Invalid Parameter in RendererNormalMap"); + + + int width = sourceNoiseMap.getWidth (); + int height = sourceNoiseMap.getHeight (); + + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + // Calculate the positions of the current point's right and up + // neighbors. + int xRightOffset, yUpOffset; + if (isWrapEnabled) + { + if (x == (int)width - 1) + xRightOffset = -((int)width - 1); + else + xRightOffset = 1; + + if (y == (int)height - 1) + yUpOffset = -((int)height - 1); + else + yUpOffset = 1; + } + else + { + if (x == (int)width - 1) + xRightOffset = 0; + else + xRightOffset = 1; + + if (y == (int)height - 1) + yUpOffset = 0; + else + yUpOffset = 1; + + } + + // Get the noise value of the current point in the source noise map + // and the noise values of its right and up neighbors. + double nc = (double)(sourceNoiseMap.getValue(x, y)); + double nr = (double)(sourceNoiseMap.getValue((x + xRightOffset),y)); + double nu = (double)(sourceNoiseMap.getValue(x, (y + yUpOffset))); + + // Calculate the normal product. + destImageCafe.setValue(x,y, (calcNormalColor (nc, nr, nu, bumpHeight))); + + // Go to the next point. + //++pSource; + //++pDest; + } + } + } + + /// Enables or disables noise-map wrapping. + /// + /// @param enable A flag that enables or disables noise-map wrapping. + /// + /// This object requires three points (the initial point and the right + /// and up neighbors) to calculate the normal vector at that point. + /// If wrapping is/ enabled, and the initial point is on the edge of + /// the noise map, the appropriate neighbors that lie outside of the + /// noise map will "wrap" to the opposite side(s) of the noise map. + /// Otherwise, the appropriate neighbors are cropped to the edge of + /// the noise map. + /// + /// Enabling wrapping is useful when creating spherical and tileable + /// normal maps. + public void enableWrap (boolean enable) + { + isWrapEnabled = enable; + } + + /// Returns the bump height. + /// + /// @returns The bump height. + /// + /// The bump height specifies the ratio of spatial resolution to + /// elevation resolution. For example, if your noise map has a + /// spatial resolution of 30 meters and an elevation resolution of one + /// meter, set the bump height to 1.0 / 30.0. + /// + /// The spatial resolution and elevation resolution are determined by + /// the application. + public double getBumpHeight () + { + return bumpHeight; + } + + /// Determines if noise-map wrapping is enabled. + /// + /// @returns + /// - @a true if noise-map wrapping is enabled. + /// - @a false if noise-map wrapping is disabled. + /// + /// This object requires three points (the initial point and the right + /// and up neighbors) to calculate the normal vector at that point. + /// If wrapping is/ enabled, and the initial point is on the edge of + /// the noise map, the appropriate neighbors that lie outside of the + /// noise map will "wrap" to the opposite side(s) of the noise map. + /// Otherwise, the appropriate neighbors are cropped to the edge of + /// the noise map. + /// + /// Enabling wrapping is useful when creating spherical and tileable + /// normal maps. + public boolean isWrapEnabled () + { + return isWrapEnabled; + } + + /// Sets the bump height. + /// + /// @param bumpHeight The bump height. + /// + /// The bump height specifies the ratio of spatial resolution to + /// elevation resolution. For example, if your noise map has a + /// spatial resolution of 30 meters and an elevation resolution of one + /// meter, set the bump height to 1.0 / 30.0. + /// + /// The spatial resolution and elevation resolution are determined by + /// the application. + public void setBumpHeight (double bumpHeight) + { + this.bumpHeight = bumpHeight; + } + + /// Sets the destination image. + /// + /// @param destImage The destination image. + /// + /// The destination image will contain the normal map after a + /// successful call to the render() method. + /// + /// The destination image must exist throughout the lifetime of this + /// object unless another image replaces that image. + public void setDestImage (ImageCafe destImage) + { + this.destImageCafe = destImage; + } + + /// Sets the source noise map. + /// + /// @param sourceNoiseMap The source noise map. + /// + /// The destination image must exist throughout the lifetime of this + /// object unless another image replaces that image. + public void setSourceNoiseMap (NoiseMap sourceNoiseMap) + { + this.sourceNoiseMap = sourceNoiseMap; + } + + public ImageCafe getDestImageCafe() + { + return destImageCafe; + } + + public NoiseMap getSourceNoiseMap() + { + return sourceNoiseMap; + } + + public void setDestImageCafe(ImageCafe destImageCafe) + { + this.destImageCafe = destImageCafe; + } + +} From 9431b4fb3240a7be39e186f9634be615aa7a2be7 Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Sun, 19 Feb 2012 23:29:56 -0800 Subject: [PATCH 02/12] Added some filters that could in theory be built out of others, but are easier for me in one place. Also, const; I want to be able to pass new Const(x) to other modules --- src/libnoiseforjava/module/Const.java | 5 + src/libnoiseforjava/module/Gradient.java | 96 ++++++++++++++++++++ src/libnoiseforjava/module/Identity.java | 44 +++++++++ src/libnoiseforjava/module/Intersection.java | 37 ++++++++ src/libnoiseforjava/util/NoiseMap.java | 10 +- 5 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/libnoiseforjava/module/Gradient.java create mode 100644 src/libnoiseforjava/module/Identity.java create mode 100644 src/libnoiseforjava/module/Intersection.java diff --git a/src/libnoiseforjava/module/Const.java b/src/libnoiseforjava/module/Const.java index f57ea0c..4454f7d 100644 --- a/src/libnoiseforjava/module/Const.java +++ b/src/libnoiseforjava/module/Const.java @@ -48,6 +48,11 @@ public Const () super(0); this.constValue = DEFAULT_CONST_VALUE; } + + public Const(double c) { + super(0); + this.constValue = c; + } public double getValue (double x, double y, double z) { diff --git a/src/libnoiseforjava/module/Gradient.java b/src/libnoiseforjava/module/Gradient.java new file mode 100644 index 0000000..1b8ce94 --- /dev/null +++ b/src/libnoiseforjava/module/Gradient.java @@ -0,0 +1,96 @@ +package libnoiseforjava.module; + +/** + * Created by IntelliJ IDEA. + * User: mike + * Date: 2/5/12 + * Time: 4:40 PM + * Package: libnoiseforjava.module + */ +public class Gradient extends ModuleBase { + + AXIS axis = AXIS.Y; + + double inputMin = 0; + double inputMax = 255; + + double scaledMin = -1; + double scaledMax = 1; + + public enum AXIS { + X, + Y, + Z + }; + + public Gradient() { + super(); + } + + public Gradient(AXIS axis) { + super(); + this.axis = axis; + } + + public double getValue(double x, double y, double z) { + if (axis == AXIS.Y) { + double outy = scale(y, this.inputMin, this.inputMax, this.scaledMin, this.scaledMax); + System.out.println("y/outy " + y + "/" + outy); + return outy; + } else if (axis == AXIS.X) { + double outx = scale(x, this.inputMin, this.inputMax, this.scaledMin, this.scaledMax); + System.out.println("x/outx " + x + "/" + outx); + return outx; + } else { + double outz = scale(z, this.inputMin, this.inputMax, this.scaledMin, this.scaledMax); + //System.out.println("z/outz " + z + "/" + outz); + return outz; + } + } + + private double scale( double inVal, double inMin, double inMax, double min, double max ) { + double m = (max-min)/(inMax-inMin); + double c = min-inMin*m; + return m*inVal+c; + } + + public AXIS getAxis() { + return axis; + } + + public void setAxis(AXIS axis) { + this.axis = axis; + } + + public double getInputMin() { + return inputMin; + } + + public void setInputMin(double inputMin) { + this.inputMin = inputMin; + } + + public double getInputMax() { + return inputMax; + } + + public void setInputMax(double inputMax) { + this.inputMax = inputMax; + } + + public double getScaledMin() { + return scaledMin; + } + + public void setScaledMin(double scaledMin) { + this.scaledMin = scaledMin; + } + + public double getScaledMax() { + return scaledMax; + } + + public void setScaledMax(double scaledMax) { + this.scaledMax = scaledMax; + } +} diff --git a/src/libnoiseforjava/module/Identity.java b/src/libnoiseforjava/module/Identity.java new file mode 100644 index 0000000..9248029 --- /dev/null +++ b/src/libnoiseforjava/module/Identity.java @@ -0,0 +1,44 @@ +package libnoiseforjava.module; + +/** + * Created by IntelliJ IDEA. + * User: mike + * Date: 2/5/12 + * Time: 4:19 PM + * Package: libnoiseforjava.module + */ +public class Identity extends ModuleBase { + + AXIS axis = AXIS.Y; + + public enum AXIS { + X, + Y, + Z + }; + + public Identity() { + super(); + } + + public Identity( AXIS axis ) { + super(); + this.axis = axis; + } + + public void setAxis( AXIS axis ) { + this.axis = axis; + } + + public double getValue(double x, double y, double z) { + if ( axis == AXIS.Y ) { + return y; + } + else if ( axis == AXIS.X ) { + return x; + } + else { + return z; + } + } +} diff --git a/src/libnoiseforjava/module/Intersection.java b/src/libnoiseforjava/module/Intersection.java new file mode 100644 index 0000000..deef01c --- /dev/null +++ b/src/libnoiseforjava/module/Intersection.java @@ -0,0 +1,37 @@ +package libnoiseforjava.module; + +import libnoiseforjava.exception.ExceptionInvalidParam; + +/** + * Created by IntelliJ IDEA. + * User: mike + * Date: 2/18/12 + * Time: 11:57 PM + * Package: libnoiseforjava.module + */ +public class Intersection extends ModuleBase { + double cutoff; + public Intersection(ModuleBase sourceModuleOne, ModuleBase sourceModuleTwo, double cutoff) throws ExceptionInvalidParam + { + super(2); + setSourceModule(0, sourceModuleOne); + setSourceModule(1, sourceModuleTwo); + this.cutoff = cutoff; + } + + public double getValue (double x, double y, double z) + { + assert (sourceModules[0] != null); + assert (sourceModules[1] != null); + + double s1 = sourceModules[0].getValue(x,y,z); + double s2 = sourceModules[1].getValue(x,y,z); + + if ( s1 + cutoff > s2 && s1 - cutoff < s2 ) { + return s1; + } + else { + return -1; + } + } +} diff --git a/src/libnoiseforjava/util/NoiseMap.java b/src/libnoiseforjava/util/NoiseMap.java index 07f71bd..b0636f2 100644 --- a/src/libnoiseforjava/util/NoiseMap.java +++ b/src/libnoiseforjava/util/NoiseMap.java @@ -51,7 +51,7 @@ public class NoiseMap int width; /// The array of doubles holding the noise map values - double [] [] noiseMap; + double[][] noiseMap; double borderValue; @@ -147,4 +147,12 @@ public void setBorderValue (double borderValue) this.borderValue = borderValue; } + public double[][] getNoiseMap() { + return noiseMap; + } + + public void setNoiseMap(double[][] noiseMap) { + this.noiseMap = noiseMap; + } + } From fa3beb29d7e4570345d1be9a46e0c03ebb07e3a9 Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Sun, 19 Feb 2012 23:41:02 -0800 Subject: [PATCH 03/12] Updated copyright on my modules to match the project --- src/libnoiseforjava/module/Gradient.java | 30 ++++++++++++++++++-- src/libnoiseforjava/module/Identity.java | 30 ++++++++++++++++++-- src/libnoiseforjava/module/Intersection.java | 30 ++++++++++++++++++-- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/libnoiseforjava/module/Gradient.java b/src/libnoiseforjava/module/Gradient.java index 1b8ce94..3efccfe 100644 --- a/src/libnoiseforjava/module/Gradient.java +++ b/src/libnoiseforjava/module/Gradient.java @@ -1,10 +1,36 @@ +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * Copyright 2012 Michael Nugent (This module) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + package libnoiseforjava.module; /** - * Created by IntelliJ IDEA. - * User: mike + * User: mike nugent * Date: 2/5/12 * Time: 4:40 PM + * URL: https://github.com/michaelnugent/libnoiseforjava * Package: libnoiseforjava.module */ public class Gradient extends ModuleBase { diff --git a/src/libnoiseforjava/module/Identity.java b/src/libnoiseforjava/module/Identity.java index 9248029..6fe0f3a 100644 --- a/src/libnoiseforjava/module/Identity.java +++ b/src/libnoiseforjava/module/Identity.java @@ -1,10 +1,36 @@ +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * Copyright 2012 Michael Nugent (This module) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + package libnoiseforjava.module; /** - * Created by IntelliJ IDEA. - * User: mike + * User: mike nugent * Date: 2/5/12 * Time: 4:19 PM + * URL: https://github.com/michaelnugent/libnoiseforjava * Package: libnoiseforjava.module */ public class Identity extends ModuleBase { diff --git a/src/libnoiseforjava/module/Intersection.java b/src/libnoiseforjava/module/Intersection.java index deef01c..4a0f20e 100644 --- a/src/libnoiseforjava/module/Intersection.java +++ b/src/libnoiseforjava/module/Intersection.java @@ -1,12 +1,38 @@ +/* + * Copyright (C) 2003, 2004 Jason Bevins (original libnoise code) + * Copyright 2010 Thomas J. Hodge (java port of libnoise) + * Copyright 2012 Michael Nugent (This module) + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + package libnoiseforjava.module; import libnoiseforjava.exception.ExceptionInvalidParam; /** - * Created by IntelliJ IDEA. - * User: mike + * User: mike nugent * Date: 2/18/12 * Time: 11:57 PM + * URL: https://github.com/michaelnugent/libnoiseforjava * Package: libnoiseforjava.module */ public class Intersection extends ModuleBase { From c9fe1b0f1894aa9ca1a6533b1e01c4b61798e80b Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Sun, 19 Feb 2012 23:42:18 -0800 Subject: [PATCH 04/12] removed debugging prints --- src/libnoiseforjava/module/Gradient.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libnoiseforjava/module/Gradient.java b/src/libnoiseforjava/module/Gradient.java index 3efccfe..91b4132 100644 --- a/src/libnoiseforjava/module/Gradient.java +++ b/src/libnoiseforjava/module/Gradient.java @@ -61,15 +61,12 @@ public Gradient(AXIS axis) { public double getValue(double x, double y, double z) { if (axis == AXIS.Y) { double outy = scale(y, this.inputMin, this.inputMax, this.scaledMin, this.scaledMax); - System.out.println("y/outy " + y + "/" + outy); return outy; } else if (axis == AXIS.X) { double outx = scale(x, this.inputMin, this.inputMax, this.scaledMin, this.scaledMax); - System.out.println("x/outx " + x + "/" + outx); return outx; } else { double outz = scale(z, this.inputMin, this.inputMax, this.scaledMin, this.scaledMax); - //System.out.println("z/outz " + z + "/" + outz); return outz; } } From 4063059dff562f86dd1e90978ecad99cdd4487b4 Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Fri, 24 Feb 2012 00:01:45 -0800 Subject: [PATCH 05/12] VectorTable should be static. Building a new (data huge) object every time we want to gradient isn't a good idea in Java. According to my runs through VisualVM, this small change speeds up the drawing time x1000. I'd be interested if others that have more complex requirements see a similar difference. --- src/libnoiseforjava/NoiseGen.java | 11 +- src/libnoiseforjava/VectorTable.java | 618 +++++++++++++-------------- 2 files changed, 316 insertions(+), 313 deletions(-) diff --git a/src/libnoiseforjava/NoiseGen.java b/src/libnoiseforjava/NoiseGen.java index 712d864..e1a321c 100644 --- a/src/libnoiseforjava/NoiseGen.java +++ b/src/libnoiseforjava/NoiseGen.java @@ -122,7 +122,7 @@ public static double GradientNoise3D (double fx, double fy, double fz, int ix, int iy, int iz, int seed) { - VectorTable vectorTable = new VectorTable(); + //VectorTable vectorTable = new VectorTable(); // Randomly generate a gradient vector given the integer coordinates of the // input value. This implementation generates a random number and uses it // as an index into a normalized-vector lookup table. @@ -135,9 +135,12 @@ public static double GradientNoise3D (double fx, double fy, double fz, int ix, vectorIndex ^= (vectorIndex >> SHIFT_NOISE_GEN); vectorIndex &= 0xff; - double xvGradient = vectorTable.getRandomVectors(vectorIndex, 0); - double yvGradient = vectorTable.getRandomVectors(vectorIndex, 1); - double zvGradient = vectorTable.getRandomVectors(vectorIndex, 2); + //double xvGradient = vectorTable.getRandomVectors(vectorIndex, 0); + //double yvGradient = vectorTable.getRandomVectors(vectorIndex, 1); + //double zvGradient = vectorTable.getRandomVectors(vectorIndex, 2); + double xvGradient = VectorTable.getRandomVectors(vectorIndex, 0); + double yvGradient = VectorTable.getRandomVectors(vectorIndex, 1); + double zvGradient = VectorTable.getRandomVectors(vectorIndex, 2); // array size too large when using this original, changed to above for all 3 // double zvGradient = vectorTable.getRandomVectors(vectorIndex << 2, 2); diff --git a/src/libnoiseforjava/VectorTable.java b/src/libnoiseforjava/VectorTable.java index 7253443..c8dbbcd 100644 --- a/src/libnoiseforjava/VectorTable.java +++ b/src/libnoiseforjava/VectorTable.java @@ -1,309 +1,309 @@ -/* - * This file is in the public domain - * - * This file is part of libnoiseforjava. - * - * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at - * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be - * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). - * Porting to Java was done by Thomas Hodge, who may be contacted at - * libnoisezagforjava@gzagmail.com (remove every 'zag'). - * - * libnoiseforjava 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. - * - * libnoiseforjava 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 - * libnoiseforjava. If not, see . - * - */ - -package libnoiseforjava; - -public class VectorTable -{ - // A table of 256 random normalized vectors. Each row is an (x, y, z, 0) - // coordinate. The 0 is used as padding so we can use bit shifts to index - // any row in the table. These vectors have an even statistical - // distribution, which improves the quality of the coherent noise - // generated by these vectors. For more information, see "GPU Gems", - // Chapter 5 - Implementing Improved Perlin Noise by Ken Perlin, - // specifically page 76. - - double[][] randomVectors = { - {-0.763874, -0.596439, -0.246489, 0.0}, - {0.396055, 0.904518, -0.158073, 0.0}, - {-0.499004, -0.8665, -0.0131631, 0.0}, - {0.468724, -0.824756, 0.316346, 0.0}, - {0.829598, 0.43195, 0.353816, 0.0}, - {-0.454473, 0.629497, -0.630228, 0.0}, - {-0.162349, -0.869962, -0.465628, 0.0}, - {0.932805, 0.253451, 0.256198, 0.0}, - {-0.345419, 0.927299, -0.144227, 0.0}, - {-0.715026, -0.293698, -0.634413, 0.0}, - {-0.245997, 0.717467, -0.651711, 0.0}, - {-0.967409, -0.250435, -0.037451, 0.0}, - {0.901729, 0.397108, -0.170852, 0.0}, - {0.892657, -0.0720622, -0.444938, 0.0}, - {0.0260084, -0.0361701, 0.999007, 0.0}, - {0.949107, -0.19486, 0.247439, 0.0}, - {0.471803, -0.807064, -0.355036, 0.0}, - {0.879737, 0.141845, 0.453809, 0.0}, - {0.570747, 0.696415, 0.435033, 0.0}, - {-0.141751, -0.988233, -0.0574584, 0.0}, - {-0.58219, -0.0303005, 0.812488, 0.0}, - {-0.60922, 0.239482, -0.755975, 0.0}, - {0.299394, -0.197066, -0.933557, 0.0}, - {-0.851615, -0.220702, -0.47544, 0.0}, - {0.848886, 0.341829, -0.403169, 0.0}, - {-0.156129, -0.687241, 0.709453, 0.0}, - {-0.665651, 0.626724, 0.405124, 0.0}, - {0.595914, -0.674582, 0.43569, 0.0}, - {0.171025, -0.509292, 0.843428, 0.0}, - {0.78605, 0.536414, -0.307222, 0.0}, - {0.18905, -0.791613, 0.581042, 0.0}, - {-0.294916, 0.844994, 0.446105, 0.0}, - {0.342031, -0.58736, -0.7335, 0.0}, - {0.57155, 0.7869, 0.232635, 0.0}, - {0.885026, -0.408223, 0.223791, 0.0}, - {-0.789518, 0.571645, 0.223347, 0.0}, - {0.774571, 0.31566, 0.548087, 0.0}, - {-0.79695, -0.0433603, -0.602487, 0.0}, - {-0.142425, -0.473249, -0.869339, 0.0}, - {-0.0698838, 0.170442, 0.982886, 0.0}, - {0.687815, -0.484748, 0.540306, 0.0}, - {0.543703, -0.534446, -0.647112, 0.0}, - {0.97186, 0.184391, -0.146588, 0.0}, - {0.707084, 0.485713, -0.513921, 0.0}, - {0.942302, 0.331945, 0.043348, 0.0}, - {0.499084, 0.599922, 0.625307, 0.0}, - {-0.289203, 0.211107, 0.9337, 0.0}, - {0.412433, -0.71667, -0.56239, 0.0}, - {0.87721, -0.082816, 0.47291, 0.0}, - {-0.420685, -0.214278, 0.881538, 0.0}, - {0.752558, -0.0391579, 0.657361, 0.0}, - {0.0765725, -0.996789, 0.0234082, 0.0}, - {-0.544312, -0.309435, -0.779727, 0.0}, - {-0.455358, -0.415572, 0.787368, 0.0}, - {-0.874586, 0.483746, 0.0330131, 0.0}, - {0.245172, -0.0838623, 0.965846, 0.0}, - {0.382293, -0.432813, 0.81641, 0.0}, - {-0.287735, -0.905514, 0.311853, 0.0}, - {-0.667704, 0.704955, -0.239186, 0.0}, - {0.717885, -0.464002, -0.518983, 0.0}, - {0.976342, -0.214895, 0.0240053, 0.0}, - {-0.0733096, -0.921136, 0.382276, 0.0}, - {-0.986284, 0.151224, -0.0661379, 0.0}, - {-0.899319, -0.429671, 0.0812908, 0.0}, - {0.652102, -0.724625, 0.222893, 0.0}, - {0.203761, 0.458023, -0.865272, 0.0}, - {-0.030396, 0.698724, -0.714745, 0.0}, - {-0.460232, 0.839138, 0.289887, 0.0}, - {-0.0898602, 0.837894, 0.538386, 0.0}, - {-0.731595, 0.0793784, 0.677102, 0.0}, - {-0.447236, -0.788397, 0.422386, 0.0}, - {0.186481, 0.645855, -0.740335, 0.0}, - {-0.259006, 0.935463, 0.240467, 0.0}, - {0.445839, 0.819655, -0.359712, 0.0}, - {0.349962, 0.755022, -0.554499, 0.0}, - {-0.997078, -0.0359577, 0.0673977, 0.0}, - {-0.431163, -0.147516, -0.890133, 0.0}, - {0.299648, -0.63914, 0.708316, 0.0}, - {0.397043, 0.566526, -0.722084, 0.0}, - {-0.502489, 0.438308, -0.745246, 0.0}, - {0.0687235, 0.354097, 0.93268, 0.0}, - {-0.0476651, -0.462597, 0.885286, 0.0}, - {-0.221934, 0.900739, -0.373383, 0.0}, - {-0.956107, -0.225676, 0.186893, 0.0}, - {-0.187627, 0.391487, -0.900852, 0.0}, - {-0.224209, -0.315405, 0.92209, 0.0}, - {-0.730807, -0.537068, 0.421283, 0.0}, - {-0.0353135, -0.816748, 0.575913, 0.0}, - {-0.941391, 0.176991, -0.287153, 0.0}, - {-0.154174, 0.390458, 0.90762, 0.0}, - {-0.283847, 0.533842, 0.796519, 0.0}, - {-0.482737, -0.850448, 0.209052, 0.0}, - {-0.649175, 0.477748, 0.591886, 0.0}, - {0.885373, -0.405387, -0.227543, 0.0}, - {-0.147261, 0.181623, -0.972279, 0.0}, - {0.0959236, -0.115847, -0.988624, 0.0}, - {-0.89724, -0.191348, 0.397928, 0.0}, - {0.903553, -0.428461, -0.00350461, 0.0}, - {0.849072, -0.295807, -0.437693, 0.0}, - {0.65551, 0.741754, -0.141804, 0.0}, - {0.61598, -0.178669, 0.767232, 0.0}, - {0.0112967, 0.932256, -0.361623, 0.0}, - {-0.793031, 0.258012, 0.551845, 0.0}, - {0.421933, 0.454311, 0.784585, 0.0}, - {-0.319993, 0.0401618, -0.946568, 0.0}, - {-0.81571, 0.551307, -0.175151, 0.0}, - {-0.377644, 0.00322313, 0.925945, 0.0}, - {0.129759, -0.666581, -0.734052, 0.0}, - {0.601901, -0.654237, -0.457919, 0.0}, - {-0.927463, -0.0343576, -0.372334, 0.0}, - {-0.438663, -0.868301, -0.231578, 0.0}, - {-0.648845, -0.749138, -0.133387, 0.0}, - {0.507393, -0.588294, 0.629653, 0.0}, - {0.726958, 0.623665, 0.287358, 0.0}, - {0.411159, 0.367614, -0.834151, 0.0}, - {0.806333, 0.585117, -0.0864016, 0.0}, - {0.263935, -0.880876, 0.392932, 0.0}, - {0.421546, -0.201336, 0.884174, 0.0}, - {-0.683198, -0.569557, -0.456996, 0.0}, - {-0.117116, -0.0406654, -0.992285, 0.0}, - {-0.643679, -0.109196, -0.757465, 0.0}, - {-0.561559, -0.62989, 0.536554, 0.0}, - {0.0628422, 0.104677, -0.992519, 0.0}, - {0.480759, -0.2867, -0.828658, 0.0}, - {-0.228559, -0.228965, -0.946222, 0.0}, - {-0.10194, -0.65706, -0.746914, 0.0}, - {0.0689193, -0.678236, 0.731605, 0.0}, - {0.401019, -0.754026, 0.52022, 0.0}, - {-0.742141, 0.547083, -0.387203, 0.0}, - {-0.00210603, -0.796417, -0.604745, 0.0}, - {0.296725, -0.409909, -0.862513, 0.0}, - {-0.260932, -0.798201, 0.542945, 0.0}, - {-0.641628, 0.742379, 0.192838, 0.0}, - {-0.186009, -0.101514, 0.97729, 0.0}, - {0.106711, -0.962067, 0.251079, 0.0}, - {-0.743499, 0.30988, -0.592607, 0.0}, - {-0.795853, -0.605066, -0.0226607, 0.0}, - {-0.828661, -0.419471, -0.370628, 0.0}, - {0.0847218, -0.489815, -0.8677, 0.0}, - {-0.381405, 0.788019, -0.483276, 0.0}, - {0.282042, -0.953394, 0.107205, 0.0}, - {0.530774, 0.847413, 0.0130696, 0.0}, - {0.0515397, 0.922524, 0.382484, 0.0}, - {-0.631467, -0.709046, 0.313852, 0.0}, - {0.688248, 0.517273, 0.508668, 0.0}, - {0.646689, -0.333782, -0.685845, 0.0}, - {-0.932528, -0.247532, -0.262906, 0.0}, - {0.630609, 0.68757, -0.359973, 0.0}, - {0.577805, -0.394189, 0.714673, 0.0}, - {-0.887833, -0.437301, -0.14325, 0.0}, - {0.690982, 0.174003, 0.701617, 0.0}, - {-0.866701, 0.0118182, 0.498689, 0.0}, - {-0.482876, 0.727143, 0.487949, 0.0}, - {-0.577567, 0.682593, -0.447752, 0.0}, - {0.373768, 0.0982991, 0.922299, 0.0}, - {0.170744, 0.964243, -0.202687, 0.0}, - {0.993654, -0.035791, -0.106632, 0.0}, - {0.587065, 0.4143, -0.695493, 0.0}, - {-0.396509, 0.26509, -0.878924, 0.0}, - {-0.0866853, 0.83553, -0.542563, 0.0}, - {0.923193, 0.133398, -0.360443, 0.0}, - {0.00379108, -0.258618, 0.965972, 0.0}, - {0.239144, 0.245154, -0.939526, 0.0}, - {0.758731, -0.555871, 0.33961, 0.0}, - {0.295355, 0.309513, 0.903862, 0.0}, - {0.0531222, -0.91003, -0.411124, 0.0}, - {0.270452, 0.0229439, -0.96246, 0.0}, - {0.563634, 0.0324352, 0.825387, 0.0}, - {0.156326, 0.147392, 0.976646, 0.0}, - {-0.0410141, 0.981824, 0.185309, 0.0}, - {-0.385562, -0.576343, -0.720535, 0.0}, - {0.388281, 0.904441, 0.176702, 0.0}, - {0.945561, -0.192859, -0.262146, 0.0}, - {0.844504, 0.520193, 0.127325, 0.0}, - {0.0330893, 0.999121, -0.0257505, 0.0}, - {-0.592616, -0.482475, -0.644999, 0.0}, - {0.539471, 0.631024, -0.557476, 0.0}, - {0.655851, -0.027319, -0.754396, 0.0}, - {0.274465, 0.887659, 0.369772, 0.0}, - {-0.123419, 0.975177, -0.183842, 0.0}, - {-0.223429, 0.708045, 0.66989, 0.0}, - {-0.908654, 0.196302, 0.368528, 0.0}, - {-0.95759, -0.00863708, 0.288005, 0.0}, - {0.960535, 0.030592, 0.276472, 0.0}, - {-0.413146, 0.907537, 0.0754161, 0.0}, - {-0.847992, 0.350849, -0.397259, 0.0}, - {0.614736, 0.395841, 0.68221, 0.0}, - {-0.503504, -0.666128, -0.550234, 0.0}, - {-0.268833, -0.738524, -0.618314, 0.0}, - {0.792737, -0.60001, -0.107502, 0.0}, - {-0.637582, 0.508144, -0.579032, 0.0}, - {0.750105, 0.282165, -0.598101, 0.0}, - {-0.351199, -0.392294, -0.850155, 0.0}, - {0.250126, -0.960993, -0.118025, 0.0}, - {-0.732341, 0.680909, -0.0063274, 0.0}, - {-0.760674, -0.141009, 0.633634, 0.0}, - {0.222823, -0.304012, 0.926243, 0.0}, - {0.209178, 0.505671, 0.836984, 0.0}, - {0.757914, -0.56629, -0.323857, 0.0}, - {-0.782926, -0.339196, 0.52151, 0.0}, - {-0.462952, 0.585565, 0.665424, 0.0}, - {0.61879, 0.194119, -0.761194, 0.0}, - {0.741388, -0.276743, 0.611357, 0.0}, - {0.707571, 0.702621, 0.0752872, 0.0}, - {0.156562, 0.819977, 0.550569, 0.0}, - {-0.793606, 0.440216, 0.42, 0.0}, - {0.234547, 0.885309, -0.401517, 0.0}, - {0.132598, 0.80115, -0.58359, 0.0}, - {-0.377899, -0.639179, 0.669808, 0.0}, - {-0.865993, -0.396465, 0.304748, 0.0}, - {-0.624815, -0.44283, 0.643046, 0.0}, - {-0.485705, 0.825614, -0.287146, 0.0}, - {-0.971788, 0.175535, 0.157529, 0.0}, - {-0.456027, 0.392629, 0.798675, 0.0}, - {-0.0104443, 0.521623, -0.853112, 0.0}, - {-0.660575, -0.74519, 0.091282, 0.0}, - {-0.0157698, -0.307475, -0.951425, 0.0}, - {-0.603467, -0.250192, 0.757121, 0.0}, - {0.506876, 0.25006, 0.824952, 0.0}, - {0.255404, 0.966794, 0.00884498, 0.0}, - {0.466764, -0.874228, -0.133625, 0.0}, - {0.475077, -0.0682351, -0.877295, 0.0}, - {-0.224967, -0.938972, -0.260233, 0.0}, - {-0.377929, -0.814757, -0.439705, 0.0}, - {-0.305847, 0.542333, -0.782517, 0.0}, - {0.26658, -0.902905, -0.337191, 0.0}, - {0.0275773, 0.322158, -0.946284, 0.0}, - {0.0185422, 0.716349, 0.697496, 0.0}, - {-0.20483, 0.978416, 0.0273371, 0.0}, - {-0.898276, 0.373969, 0.230752, 0.0}, - {-0.00909378, 0.546594, 0.837349, 0.0}, - {0.6602, -0.751089, 0.000959236, 0.0}, - {0.855301, -0.303056, 0.420259, 0.0}, - {0.797138, 0.0623013, -0.600574, 0.0}, - {0.48947, -0.866813, 0.0951509, 0.0}, - {0.251142, 0.674531, 0.694216, 0.0}, - {-0.578422, -0.737373, -0.348867, 0.0}, - {-0.254689, -0.514807, 0.818601, 0.0}, - {0.374972, 0.761612, 0.528529, 0.0}, - {0.640303, -0.734271, -0.225517, 0.0}, - {-0.638076, 0.285527, 0.715075, 0.0}, - {0.772956, -0.15984, -0.613995, 0.0}, - {0.798217, -0.590628, 0.118356, 0.0}, - {-0.986276, -0.0578337, -0.154644, 0.0}, - {-0.312988, -0.94549, 0.0899272, 0.0}, - {-0.497338, 0.178325, 0.849032, 0.0}, - {-0.101136, -0.981014, 0.165477, 0.0}, - {-0.521688, 0.0553434, -0.851339, 0.0}, - {-0.786182, -0.583814, 0.202678, 0.0}, - {-0.565191, 0.821858, -0.0714658, 0.0}, - {0.437895, 0.152598, -0.885981, 0.0}, - {-0.92394, 0.353436, -0.14635, 0.0}, - {0.212189, -0.815162, -0.538969, 0.0}, - {-0.859262, 0.143405, -0.491024, 0.0}, - {0.991353, 0.112814, 0.0670273, 0.0}, - {0.0337884, -0.979891, -0.196654, 0.0} - }; - - public VectorTable() - { - - } - - public double getRandomVectors(int a, int b) - { - return randomVectors[a][b]; - } - - - -} - +/* + * This file is in the public domain + * + * This file is part of libnoiseforjava. + * + * libnoiseforjava is a Java port of the C++ library libnoise, which may be found at + * http://libnoise.sourceforge.net/. libnoise was developed by Jason Bevins, who may be + * contacted at jlbezigvins@gmzigail.com (for great email, take off every 'zig'). + * Porting to Java was done by Thomas Hodge, who may be contacted at + * libnoisezagforjava@gzagmail.com (remove every 'zag'). + * + * libnoiseforjava 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. + * + * libnoiseforjava 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 + * libnoiseforjava. If not, see . + * + */ + +package libnoiseforjava; + +public class VectorTable +{ + // A table of 256 random normalized vectors. Each row is an (x, y, z, 0) + // coordinate. The 0 is used as padding so we can use bit shifts to index + // any row in the table. These vectors have an even statistical + // distribution, which improves the quality of the coherent noise + // generated by these vectors. For more information, see "GPU Gems", + // Chapter 5 - Implementing Improved Perlin Noise by Ken Perlin, + // specifically page 76. + + static double[][] randomVectors = { + {-0.763874, -0.596439, -0.246489, 0.0}, + {0.396055, 0.904518, -0.158073, 0.0}, + {-0.499004, -0.8665, -0.0131631, 0.0}, + {0.468724, -0.824756, 0.316346, 0.0}, + {0.829598, 0.43195, 0.353816, 0.0}, + {-0.454473, 0.629497, -0.630228, 0.0}, + {-0.162349, -0.869962, -0.465628, 0.0}, + {0.932805, 0.253451, 0.256198, 0.0}, + {-0.345419, 0.927299, -0.144227, 0.0}, + {-0.715026, -0.293698, -0.634413, 0.0}, + {-0.245997, 0.717467, -0.651711, 0.0}, + {-0.967409, -0.250435, -0.037451, 0.0}, + {0.901729, 0.397108, -0.170852, 0.0}, + {0.892657, -0.0720622, -0.444938, 0.0}, + {0.0260084, -0.0361701, 0.999007, 0.0}, + {0.949107, -0.19486, 0.247439, 0.0}, + {0.471803, -0.807064, -0.355036, 0.0}, + {0.879737, 0.141845, 0.453809, 0.0}, + {0.570747, 0.696415, 0.435033, 0.0}, + {-0.141751, -0.988233, -0.0574584, 0.0}, + {-0.58219, -0.0303005, 0.812488, 0.0}, + {-0.60922, 0.239482, -0.755975, 0.0}, + {0.299394, -0.197066, -0.933557, 0.0}, + {-0.851615, -0.220702, -0.47544, 0.0}, + {0.848886, 0.341829, -0.403169, 0.0}, + {-0.156129, -0.687241, 0.709453, 0.0}, + {-0.665651, 0.626724, 0.405124, 0.0}, + {0.595914, -0.674582, 0.43569, 0.0}, + {0.171025, -0.509292, 0.843428, 0.0}, + {0.78605, 0.536414, -0.307222, 0.0}, + {0.18905, -0.791613, 0.581042, 0.0}, + {-0.294916, 0.844994, 0.446105, 0.0}, + {0.342031, -0.58736, -0.7335, 0.0}, + {0.57155, 0.7869, 0.232635, 0.0}, + {0.885026, -0.408223, 0.223791, 0.0}, + {-0.789518, 0.571645, 0.223347, 0.0}, + {0.774571, 0.31566, 0.548087, 0.0}, + {-0.79695, -0.0433603, -0.602487, 0.0}, + {-0.142425, -0.473249, -0.869339, 0.0}, + {-0.0698838, 0.170442, 0.982886, 0.0}, + {0.687815, -0.484748, 0.540306, 0.0}, + {0.543703, -0.534446, -0.647112, 0.0}, + {0.97186, 0.184391, -0.146588, 0.0}, + {0.707084, 0.485713, -0.513921, 0.0}, + {0.942302, 0.331945, 0.043348, 0.0}, + {0.499084, 0.599922, 0.625307, 0.0}, + {-0.289203, 0.211107, 0.9337, 0.0}, + {0.412433, -0.71667, -0.56239, 0.0}, + {0.87721, -0.082816, 0.47291, 0.0}, + {-0.420685, -0.214278, 0.881538, 0.0}, + {0.752558, -0.0391579, 0.657361, 0.0}, + {0.0765725, -0.996789, 0.0234082, 0.0}, + {-0.544312, -0.309435, -0.779727, 0.0}, + {-0.455358, -0.415572, 0.787368, 0.0}, + {-0.874586, 0.483746, 0.0330131, 0.0}, + {0.245172, -0.0838623, 0.965846, 0.0}, + {0.382293, -0.432813, 0.81641, 0.0}, + {-0.287735, -0.905514, 0.311853, 0.0}, + {-0.667704, 0.704955, -0.239186, 0.0}, + {0.717885, -0.464002, -0.518983, 0.0}, + {0.976342, -0.214895, 0.0240053, 0.0}, + {-0.0733096, -0.921136, 0.382276, 0.0}, + {-0.986284, 0.151224, -0.0661379, 0.0}, + {-0.899319, -0.429671, 0.0812908, 0.0}, + {0.652102, -0.724625, 0.222893, 0.0}, + {0.203761, 0.458023, -0.865272, 0.0}, + {-0.030396, 0.698724, -0.714745, 0.0}, + {-0.460232, 0.839138, 0.289887, 0.0}, + {-0.0898602, 0.837894, 0.538386, 0.0}, + {-0.731595, 0.0793784, 0.677102, 0.0}, + {-0.447236, -0.788397, 0.422386, 0.0}, + {0.186481, 0.645855, -0.740335, 0.0}, + {-0.259006, 0.935463, 0.240467, 0.0}, + {0.445839, 0.819655, -0.359712, 0.0}, + {0.349962, 0.755022, -0.554499, 0.0}, + {-0.997078, -0.0359577, 0.0673977, 0.0}, + {-0.431163, -0.147516, -0.890133, 0.0}, + {0.299648, -0.63914, 0.708316, 0.0}, + {0.397043, 0.566526, -0.722084, 0.0}, + {-0.502489, 0.438308, -0.745246, 0.0}, + {0.0687235, 0.354097, 0.93268, 0.0}, + {-0.0476651, -0.462597, 0.885286, 0.0}, + {-0.221934, 0.900739, -0.373383, 0.0}, + {-0.956107, -0.225676, 0.186893, 0.0}, + {-0.187627, 0.391487, -0.900852, 0.0}, + {-0.224209, -0.315405, 0.92209, 0.0}, + {-0.730807, -0.537068, 0.421283, 0.0}, + {-0.0353135, -0.816748, 0.575913, 0.0}, + {-0.941391, 0.176991, -0.287153, 0.0}, + {-0.154174, 0.390458, 0.90762, 0.0}, + {-0.283847, 0.533842, 0.796519, 0.0}, + {-0.482737, -0.850448, 0.209052, 0.0}, + {-0.649175, 0.477748, 0.591886, 0.0}, + {0.885373, -0.405387, -0.227543, 0.0}, + {-0.147261, 0.181623, -0.972279, 0.0}, + {0.0959236, -0.115847, -0.988624, 0.0}, + {-0.89724, -0.191348, 0.397928, 0.0}, + {0.903553, -0.428461, -0.00350461, 0.0}, + {0.849072, -0.295807, -0.437693, 0.0}, + {0.65551, 0.741754, -0.141804, 0.0}, + {0.61598, -0.178669, 0.767232, 0.0}, + {0.0112967, 0.932256, -0.361623, 0.0}, + {-0.793031, 0.258012, 0.551845, 0.0}, + {0.421933, 0.454311, 0.784585, 0.0}, + {-0.319993, 0.0401618, -0.946568, 0.0}, + {-0.81571, 0.551307, -0.175151, 0.0}, + {-0.377644, 0.00322313, 0.925945, 0.0}, + {0.129759, -0.666581, -0.734052, 0.0}, + {0.601901, -0.654237, -0.457919, 0.0}, + {-0.927463, -0.0343576, -0.372334, 0.0}, + {-0.438663, -0.868301, -0.231578, 0.0}, + {-0.648845, -0.749138, -0.133387, 0.0}, + {0.507393, -0.588294, 0.629653, 0.0}, + {0.726958, 0.623665, 0.287358, 0.0}, + {0.411159, 0.367614, -0.834151, 0.0}, + {0.806333, 0.585117, -0.0864016, 0.0}, + {0.263935, -0.880876, 0.392932, 0.0}, + {0.421546, -0.201336, 0.884174, 0.0}, + {-0.683198, -0.569557, -0.456996, 0.0}, + {-0.117116, -0.0406654, -0.992285, 0.0}, + {-0.643679, -0.109196, -0.757465, 0.0}, + {-0.561559, -0.62989, 0.536554, 0.0}, + {0.0628422, 0.104677, -0.992519, 0.0}, + {0.480759, -0.2867, -0.828658, 0.0}, + {-0.228559, -0.228965, -0.946222, 0.0}, + {-0.10194, -0.65706, -0.746914, 0.0}, + {0.0689193, -0.678236, 0.731605, 0.0}, + {0.401019, -0.754026, 0.52022, 0.0}, + {-0.742141, 0.547083, -0.387203, 0.0}, + {-0.00210603, -0.796417, -0.604745, 0.0}, + {0.296725, -0.409909, -0.862513, 0.0}, + {-0.260932, -0.798201, 0.542945, 0.0}, + {-0.641628, 0.742379, 0.192838, 0.0}, + {-0.186009, -0.101514, 0.97729, 0.0}, + {0.106711, -0.962067, 0.251079, 0.0}, + {-0.743499, 0.30988, -0.592607, 0.0}, + {-0.795853, -0.605066, -0.0226607, 0.0}, + {-0.828661, -0.419471, -0.370628, 0.0}, + {0.0847218, -0.489815, -0.8677, 0.0}, + {-0.381405, 0.788019, -0.483276, 0.0}, + {0.282042, -0.953394, 0.107205, 0.0}, + {0.530774, 0.847413, 0.0130696, 0.0}, + {0.0515397, 0.922524, 0.382484, 0.0}, + {-0.631467, -0.709046, 0.313852, 0.0}, + {0.688248, 0.517273, 0.508668, 0.0}, + {0.646689, -0.333782, -0.685845, 0.0}, + {-0.932528, -0.247532, -0.262906, 0.0}, + {0.630609, 0.68757, -0.359973, 0.0}, + {0.577805, -0.394189, 0.714673, 0.0}, + {-0.887833, -0.437301, -0.14325, 0.0}, + {0.690982, 0.174003, 0.701617, 0.0}, + {-0.866701, 0.0118182, 0.498689, 0.0}, + {-0.482876, 0.727143, 0.487949, 0.0}, + {-0.577567, 0.682593, -0.447752, 0.0}, + {0.373768, 0.0982991, 0.922299, 0.0}, + {0.170744, 0.964243, -0.202687, 0.0}, + {0.993654, -0.035791, -0.106632, 0.0}, + {0.587065, 0.4143, -0.695493, 0.0}, + {-0.396509, 0.26509, -0.878924, 0.0}, + {-0.0866853, 0.83553, -0.542563, 0.0}, + {0.923193, 0.133398, -0.360443, 0.0}, + {0.00379108, -0.258618, 0.965972, 0.0}, + {0.239144, 0.245154, -0.939526, 0.0}, + {0.758731, -0.555871, 0.33961, 0.0}, + {0.295355, 0.309513, 0.903862, 0.0}, + {0.0531222, -0.91003, -0.411124, 0.0}, + {0.270452, 0.0229439, -0.96246, 0.0}, + {0.563634, 0.0324352, 0.825387, 0.0}, + {0.156326, 0.147392, 0.976646, 0.0}, + {-0.0410141, 0.981824, 0.185309, 0.0}, + {-0.385562, -0.576343, -0.720535, 0.0}, + {0.388281, 0.904441, 0.176702, 0.0}, + {0.945561, -0.192859, -0.262146, 0.0}, + {0.844504, 0.520193, 0.127325, 0.0}, + {0.0330893, 0.999121, -0.0257505, 0.0}, + {-0.592616, -0.482475, -0.644999, 0.0}, + {0.539471, 0.631024, -0.557476, 0.0}, + {0.655851, -0.027319, -0.754396, 0.0}, + {0.274465, 0.887659, 0.369772, 0.0}, + {-0.123419, 0.975177, -0.183842, 0.0}, + {-0.223429, 0.708045, 0.66989, 0.0}, + {-0.908654, 0.196302, 0.368528, 0.0}, + {-0.95759, -0.00863708, 0.288005, 0.0}, + {0.960535, 0.030592, 0.276472, 0.0}, + {-0.413146, 0.907537, 0.0754161, 0.0}, + {-0.847992, 0.350849, -0.397259, 0.0}, + {0.614736, 0.395841, 0.68221, 0.0}, + {-0.503504, -0.666128, -0.550234, 0.0}, + {-0.268833, -0.738524, -0.618314, 0.0}, + {0.792737, -0.60001, -0.107502, 0.0}, + {-0.637582, 0.508144, -0.579032, 0.0}, + {0.750105, 0.282165, -0.598101, 0.0}, + {-0.351199, -0.392294, -0.850155, 0.0}, + {0.250126, -0.960993, -0.118025, 0.0}, + {-0.732341, 0.680909, -0.0063274, 0.0}, + {-0.760674, -0.141009, 0.633634, 0.0}, + {0.222823, -0.304012, 0.926243, 0.0}, + {0.209178, 0.505671, 0.836984, 0.0}, + {0.757914, -0.56629, -0.323857, 0.0}, + {-0.782926, -0.339196, 0.52151, 0.0}, + {-0.462952, 0.585565, 0.665424, 0.0}, + {0.61879, 0.194119, -0.761194, 0.0}, + {0.741388, -0.276743, 0.611357, 0.0}, + {0.707571, 0.702621, 0.0752872, 0.0}, + {0.156562, 0.819977, 0.550569, 0.0}, + {-0.793606, 0.440216, 0.42, 0.0}, + {0.234547, 0.885309, -0.401517, 0.0}, + {0.132598, 0.80115, -0.58359, 0.0}, + {-0.377899, -0.639179, 0.669808, 0.0}, + {-0.865993, -0.396465, 0.304748, 0.0}, + {-0.624815, -0.44283, 0.643046, 0.0}, + {-0.485705, 0.825614, -0.287146, 0.0}, + {-0.971788, 0.175535, 0.157529, 0.0}, + {-0.456027, 0.392629, 0.798675, 0.0}, + {-0.0104443, 0.521623, -0.853112, 0.0}, + {-0.660575, -0.74519, 0.091282, 0.0}, + {-0.0157698, -0.307475, -0.951425, 0.0}, + {-0.603467, -0.250192, 0.757121, 0.0}, + {0.506876, 0.25006, 0.824952, 0.0}, + {0.255404, 0.966794, 0.00884498, 0.0}, + {0.466764, -0.874228, -0.133625, 0.0}, + {0.475077, -0.0682351, -0.877295, 0.0}, + {-0.224967, -0.938972, -0.260233, 0.0}, + {-0.377929, -0.814757, -0.439705, 0.0}, + {-0.305847, 0.542333, -0.782517, 0.0}, + {0.26658, -0.902905, -0.337191, 0.0}, + {0.0275773, 0.322158, -0.946284, 0.0}, + {0.0185422, 0.716349, 0.697496, 0.0}, + {-0.20483, 0.978416, 0.0273371, 0.0}, + {-0.898276, 0.373969, 0.230752, 0.0}, + {-0.00909378, 0.546594, 0.837349, 0.0}, + {0.6602, -0.751089, 0.000959236, 0.0}, + {0.855301, -0.303056, 0.420259, 0.0}, + {0.797138, 0.0623013, -0.600574, 0.0}, + {0.48947, -0.866813, 0.0951509, 0.0}, + {0.251142, 0.674531, 0.694216, 0.0}, + {-0.578422, -0.737373, -0.348867, 0.0}, + {-0.254689, -0.514807, 0.818601, 0.0}, + {0.374972, 0.761612, 0.528529, 0.0}, + {0.640303, -0.734271, -0.225517, 0.0}, + {-0.638076, 0.285527, 0.715075, 0.0}, + {0.772956, -0.15984, -0.613995, 0.0}, + {0.798217, -0.590628, 0.118356, 0.0}, + {-0.986276, -0.0578337, -0.154644, 0.0}, + {-0.312988, -0.94549, 0.0899272, 0.0}, + {-0.497338, 0.178325, 0.849032, 0.0}, + {-0.101136, -0.981014, 0.165477, 0.0}, + {-0.521688, 0.0553434, -0.851339, 0.0}, + {-0.786182, -0.583814, 0.202678, 0.0}, + {-0.565191, 0.821858, -0.0714658, 0.0}, + {0.437895, 0.152598, -0.885981, 0.0}, + {-0.92394, 0.353436, -0.14635, 0.0}, + {0.212189, -0.815162, -0.538969, 0.0}, + {-0.859262, 0.143405, -0.491024, 0.0}, + {0.991353, 0.112814, 0.0670273, 0.0}, + {0.0337884, -0.979891, -0.196654, 0.0} + }; + + public VectorTable() + { + + } + + public static double getRandomVectors(int a, int b) + { + return randomVectors[a][b]; + } + + + +} + From eca535b8bbd84a050034089f0d9b1d4fc96352b8 Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Fri, 24 Feb 2012 00:13:16 -0800 Subject: [PATCH 06/12] VectorTable more like a static class now. --- src/libnoiseforjava/VectorTable.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libnoiseforjava/VectorTable.java b/src/libnoiseforjava/VectorTable.java index c8dbbcd..7b904a2 100644 --- a/src/libnoiseforjava/VectorTable.java +++ b/src/libnoiseforjava/VectorTable.java @@ -34,7 +34,7 @@ public class VectorTable // Chapter 5 - Implementing Improved Perlin Noise by Ken Perlin, // specifically page 76. - static double[][] randomVectors = { + private static double[][] randomVectors = { {-0.763874, -0.596439, -0.246489, 0.0}, {0.396055, 0.904518, -0.158073, 0.0}, {-0.499004, -0.8665, -0.0131631, 0.0}, @@ -293,7 +293,7 @@ public class VectorTable {0.0337884, -0.979891, -0.196654, 0.0} }; - public VectorTable() + private VectorTable() { } From 77a60e31dac46dbb2657b9c69604fdb29adde70d Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Fri, 9 Mar 2012 18:20:46 -0800 Subject: [PATCH 07/12] Simplex, first version for libnoise --- src/libnoiseforjava/module/Simplex.java | 372 ++++++++++++++++++++++++ 1 file changed, 372 insertions(+) create mode 100644 src/libnoiseforjava/module/Simplex.java diff --git a/src/libnoiseforjava/module/Simplex.java b/src/libnoiseforjava/module/Simplex.java new file mode 100644 index 0000000..8a418ee --- /dev/null +++ b/src/libnoiseforjava/module/Simplex.java @@ -0,0 +1,372 @@ +package libnoiseforjava.module; + +/** + * Michael Nugent + * Date: 3/9/12 + * Time: 6:12 PM + * URL: https://github.com/michaelnugent/libnoiseforjava + * Package: libnoiseforjava.module + */ + + +/* +* A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java. +* +* Based on example code by Stefan Gustavson (stegu@itn.liu.se). +* Optimisations by Peter Eastman (peastman@drizzle.stanford.edu). +* Better rank ordering method by Stefan Gustavson in 2012. +* +* This could be speeded up even further, but it's useful as it is. +* +* Version 2012-03-09 +* +* This code was placed in the public domain by its original author, +* Stefan Gustavson. You may use it as you see fit, but +* attribution is appreciated. +* +* Modified by Michael Nugent (michael@michaelnugent.org) for the +* libnoise framework 20120309 +* All libnoise expects 3d, but I've left the 2d and 4d functions in for +* reference. +* +*/ + +public class Simplex extends ModuleBase { // Simplex noise in 2D, 3D and 4D + private static Grad grad3[] = {new Grad(1,1,0),new Grad(-1,1,0),new Grad(1,-1,0),new Grad(-1,-1,0), + new Grad(1,0,1),new Grad(-1,0,1),new Grad(1,0,-1),new Grad(-1,0,-1), + new Grad(0,1,1),new Grad(0,-1,1),new Grad(0,1,-1),new Grad(0,-1,-1)}; + + private static Grad grad4[]= {new Grad(0,1,1,1),new Grad(0,1,1,-1),new Grad(0,1,-1,1),new Grad(0,1,-1,-1), + new Grad(0,-1,1,1),new Grad(0,-1,1,-1),new Grad(0,-1,-1,1),new Grad(0,-1,-1,-1), + new Grad(1,0,1,1),new Grad(1,0,1,-1),new Grad(1,0,-1,1),new Grad(1,0,-1,-1), + new Grad(-1,0,1,1),new Grad(-1,0,1,-1),new Grad(-1,0,-1,1),new Grad(-1,0,-1,-1), + new Grad(1,1,0,1),new Grad(1,1,0,-1),new Grad(1,-1,0,1),new Grad(1,-1,0,-1), + new Grad(-1,1,0,1),new Grad(-1,1,0,-1),new Grad(-1,-1,0,1),new Grad(-1,-1,0,-1), + new Grad(1,1,1,0),new Grad(1,1,-1,0),new Grad(1,-1,1,0),new Grad(1,-1,-1,0), + new Grad(-1,1,1,0),new Grad(-1,1,-1,0),new Grad(-1,-1,1,0),new Grad(-1,-1,-1,0)}; + + private static short p[] = {151,160,137,91,90,15, + 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, + 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, + 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, + 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, + 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, + 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, + 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, + 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, + 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, + 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, + 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, + 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180}; + // To remove the need for index wrapping, double the permutation table length + private static short perm[] = new short[512]; + private static short permMod12[] = new short[512]; + static { + for(int i=0; i<512; i++) + { + perm[i]=p[i & 255]; + permMod12[i] = (short)(perm[i] % 12); + } + } + + // Skewing and unskewing factors for 2, 3, and 4 dimensions + private static final double F2 = 0.5*(Math.sqrt(3.0)-1.0); + private static final double G2 = (3.0-Math.sqrt(3.0))/6.0; + private static final double F3 = 1.0/3.0; + private static final double G3 = 1.0/6.0; + private static final double F4 = (Math.sqrt(5.0)-1.0)/4.0; + private static final double G4 = (5.0-Math.sqrt(5.0))/20.0; + + // This method is a *lot* faster than using (int)Math.floor(x) + private static int fastfloor(double x) { + int xi = (int)x; + return xy0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1) + else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1) + // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and + // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where + // c = (3-sqrt(3))/6 + double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords + double y1 = y0 - j1 + G2; + double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords + double y2 = y0 - 1.0 + 2.0 * G2; + // Work out the hashed gradient indices of the three simplex corners + int ii = i & 255; + int jj = j & 255; + int gi0 = permMod12[ii+perm[jj]]; + int gi1 = permMod12[ii+i1+perm[jj+j1]]; + int gi2 = permMod12[ii+1+perm[jj+1]]; + // Calculate the contribution from the three corners + double t0 = 0.5 - x0*x0-y0*y0; + if(t0<0) n0 = 0.0; + else { + t0 *= t0; + n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient + } + double t1 = 0.5 - x1*x1-y1*y1; + if(t1<0) n1 = 0.0; + else { + t1 *= t1; + n1 = t1 * t1 * dot(grad3[gi1], x1, y1); + } + double t2 = 0.5 - x2*x2-y2*y2; + if(t2<0) n2 = 0.0; + else { + t2 *= t2; + n2 = t2 * t2 * dot(grad3[gi2], x2, y2); + } + // Add contributions from each corner to get the final noise value. + // The result is scaled to return values in the interval [-1,1]. + return 70.0 * (n0 + n1 + n2); + } + + + // 3D simplex noise + public double getValue(double xin, double yin, double zin) { + double n0, n1, n2, n3; // Noise contributions from the four corners + // Skew the input space to determine which simplex cell we're in + double s = (xin+yin+zin)*F3; // Very nice and simple skew factor for 3D + int i = fastfloor(xin+s); + int j = fastfloor(yin+s); + int k = fastfloor(zin+s); + double t = (i+j+k)*G3; + double X0 = i-t; // Unskew the cell origin back to (x,y,z) space + double Y0 = j-t; + double Z0 = k-t; + double x0 = xin-X0; // The x,y,z distances from the cell origin + double y0 = yin-Y0; + double z0 = zin-Z0; + // For the 3D case, the simplex shape is a slightly irregular tetrahedron. + // Determine which simplex we are in. + int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords + int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords + if(x0>=y0) { + if(y0>=z0) + { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order + else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order + else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order + } + else { // x0 y0) rankx++; else ranky++; + if(x0 > z0) rankx++; else rankz++; + if(x0 > w0) rankx++; else rankw++; + if(y0 > z0) ranky++; else rankz++; + if(y0 > w0) ranky++; else rankw++; + if(z0 > w0) rankz++; else rankw++; + int i1, j1, k1, l1; // The integer offsets for the second simplex corner + int i2, j2, k2, l2; // The integer offsets for the third simplex corner + int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner + // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order. + // Many values of c will never occur, since e.g. x>y>z>w makes x= 3 ? 1 : 0; + j1 = ranky >= 3 ? 1 : 0; + k1 = rankz >= 3 ? 1 : 0; + l1 = rankw >= 3 ? 1 : 0; + // Rank 2 denotes the second largest coordinate. + i2 = rankx >= 2 ? 1 : 0; + j2 = ranky >= 2 ? 1 : 0; + k2 = rankz >= 2 ? 1 : 0; + l2 = rankw >= 2 ? 1 : 0; + // Rank 1 denotes the second smallest coordinate. + i3 = rankx >= 1 ? 1 : 0; + j3 = ranky >= 1 ? 1 : 0; + k3 = rankz >= 1 ? 1 : 0; + l3 = rankw >= 1 ? 1 : 0; + // The fifth corner has all coordinate offsets = 1, so no need to compute that. + double x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords + double y1 = y0 - j1 + G4; + double z1 = z0 - k1 + G4; + double w1 = w0 - l1 + G4; + double x2 = x0 - i2 + 2.0*G4; // Offsets for third corner in (x,y,z,w) coords + double y2 = y0 - j2 + 2.0*G4; + double z2 = z0 - k2 + 2.0*G4; + double w2 = w0 - l2 + 2.0*G4; + double x3 = x0 - i3 + 3.0*G4; // Offsets for fourth corner in (x,y,z,w) coords + double y3 = y0 - j3 + 3.0*G4; + double z3 = z0 - k3 + 3.0*G4; + double w3 = w0 - l3 + 3.0*G4; + double x4 = x0 - 1.0 + 4.0*G4; // Offsets for last corner in (x,y,z,w) coords + double y4 = y0 - 1.0 + 4.0*G4; + double z4 = z0 - 1.0 + 4.0*G4; + double w4 = w0 - 1.0 + 4.0*G4; + // Work out the hashed gradient indices of the five simplex corners + int ii = i & 255; + int jj = j & 255; + int kk = k & 255; + int ll = l & 255; + int gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32; + int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32; + int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32; + int gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32; + int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32; + // Calculate the contribution from the five corners + double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0; + if(t0<0) n0 = 0.0; + else { + t0 *= t0; + n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0); + } + double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1; + if(t1<0) n1 = 0.0; + else { + t1 *= t1; + n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1); + } + double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2; + if(t2<0) n2 = 0.0; + else { + t2 *= t2; + n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2); + } + double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3; + if(t3<0) n3 = 0.0; + else { + t3 *= t3; + n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3); + } + double t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4; + if(t4<0) n4 = 0.0; + else { + t4 *= t4; + n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4); + } + // Sum up and scale the result to cover the range [-1,1] + return 27.0 * (n0 + n1 + n2 + n3 + n4); + } + + // Inner class to speed upp gradient computations + // (array access is a lot slower than member access) + private static class Grad + { + double x, y, z, w; + + Grad(double x, double y, double z) + { + this.x = x; + this.y = y; + this.z = z; + } + + Grad(double x, double y, double z, double w) + { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + } +} From 2ad7354697d0df5eb32e6bd78e2f0dae8d1f8ef8 Mon Sep 17 00:00:00 2001 From: Michael Nugent Date: Mon, 26 Mar 2012 20:24:12 -0700 Subject: [PATCH 08/12] Added seed value to simple 3d. Will extend to 2/4d if I need it of if anyone requests. --- src/libnoiseforjava/NoiseGen.java | 8 ++--- src/libnoiseforjava/module/Simplex.java | 43 +++++++++++++++++++------ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/libnoiseforjava/NoiseGen.java b/src/libnoiseforjava/NoiseGen.java index e1a321c..fd98be3 100644 --- a/src/libnoiseforjava/NoiseGen.java +++ b/src/libnoiseforjava/NoiseGen.java @@ -134,10 +134,7 @@ public static double GradientNoise3D (double fx, double fy, double fz, int ix, vectorIndex ^= (vectorIndex >> SHIFT_NOISE_GEN); vectorIndex &= 0xff; - - //double xvGradient = vectorTable.getRandomVectors(vectorIndex, 0); - //double yvGradient = vectorTable.getRandomVectors(vectorIndex, 1); - //double zvGradient = vectorTable.getRandomVectors(vectorIndex, 2); + double xvGradient = VectorTable.getRandomVectors(vectorIndex, 0); double yvGradient = VectorTable.getRandomVectors(vectorIndex, 1); double zvGradient = VectorTable.getRandomVectors(vectorIndex, 2); @@ -252,8 +249,7 @@ public static double ValueNoise3D (int x, int y, int z, int seed) /// Although you could do a straight cast from double to int32, the /// resulting value may differ between platforms. By using this function, /// you ensure that the resulting value is identical between platforms. - public static double MakeInt32Range (double n) - { + public static double MakeInt32Range (double n) { if (n >= 1073741824.0) return (2.0 * (n % 1073741824.0)) - 1073741824.0; else if (n <= -1073741824.0) diff --git a/src/libnoiseforjava/module/Simplex.java b/src/libnoiseforjava/module/Simplex.java index 8a418ee..82d58c6 100644 --- a/src/libnoiseforjava/module/Simplex.java +++ b/src/libnoiseforjava/module/Simplex.java @@ -59,16 +59,31 @@ public class Simplex extends ModuleBase { // Simplex noise in 2D, 3D and 4D 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180}; // To remove the need for index wrapping, double the permutation table length - private static short perm[] = new short[512]; - private static short permMod12[] = new short[512]; - static { - for(int i=0; i<512; i++) - { + private short perm[] = new short[512]; + private short permMod12[] = new short[512]; + + private double seed = 0; + + public Simplex() { + super(0); + for(int i=0; i<512; i++) { perm[i]=p[i & 255]; permMod12[i] = (short)(perm[i] % 12); } } + public double getSeed() { + return seed; + } + + public void setSeed(double seed) { + this.seed = seed; + } + + public void setSeed(int seed) { + this.seed = (double)seed; + } + // Skewing and unskewing factors for 2, 3, and 4 dimensions private static final double F2 = 0.5*(Math.sqrt(3.0)-1.0); private static final double G2 = (3.0-Math.sqrt(3.0))/6.0; @@ -84,17 +99,20 @@ private static int fastfloor(double x) { } private static double dot(Grad g, double x, double y) { - return g.x*x + g.y*y; } + return g.x*x + g.y*y; + } private static double dot(Grad g, double x, double y, double z) { - return g.x*x + g.y*y + g.z*z; } + return g.x*x + g.y*y + g.z*z; + } private static double dot(Grad g, double x, double y, double z, double w) { - return g.x*x + g.y*y + g.z*z + g.w*w; } + return g.x*x + g.y*y + g.z*z + g.w*w; + } // 2D simplex noise - public static double getValue2d(double xin, double yin) { + public double getValue2d(double xin, double yin) { double n0, n1, n2; // Noise contributions from the three corners // Skew the input space to determine which simplex cell we're in double s = (xin+yin)*F2; // Hairy factor for 2D @@ -120,6 +138,7 @@ public static double getValue2d(double xin, double yin) { // Work out the hashed gradient indices of the three simplex corners int ii = i & 255; int jj = j & 255; + int gi0 = permMod12[ii+perm[jj]]; int gi1 = permMod12[ii+i1+perm[jj+j1]]; int gi2 = permMod12[ii+1+perm[jj+1]]; @@ -152,6 +171,9 @@ public static double getValue2d(double xin, double yin) { public double getValue(double xin, double yin, double zin) { double n0, n1, n2, n3; // Noise contributions from the four corners // Skew the input space to determine which simplex cell we're in + xin+=(seed + (seed * 7)) % Double.MAX_VALUE; + xin+=(seed + (seed * 13)) % Double.MAX_VALUE; + xin+=(seed + (seed * 17)) % Double.MAX_VALUE; double s = (xin+yin+zin)*F3; // Very nice and simple skew factor for 3D int i = fastfloor(xin+s); int j = fastfloor(yin+s); @@ -195,6 +217,7 @@ public double getValue(double xin, double yin, double zin) { int ii = i & 255; int jj = j & 255; int kk = k & 255; + int gi0 = permMod12[ii+perm[jj+perm[kk]]]; int gi1 = permMod12[ii+i1+perm[jj+j1+perm[kk+k1]]]; int gi2 = permMod12[ii+i2+perm[jj+j2+perm[kk+k2]]]; @@ -231,7 +254,7 @@ public double getValue(double xin, double yin, double zin) { // 4D simplex noise, better simplex rank ordering method 2012-03-09 - public static double getValue4d(double x, double y, double z, double w) { + public double getValue4d(double x, double y, double z, double w) { double n0, n1, n2, n3, n4; // Noise contributions from the five corners // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in From 2a15c06cd6144da8fe2d9ad03d17c85b49574047 Mon Sep 17 00:00:00 2001 From: Rsgm Date: Fri, 20 Dec 2013 16:07:59 -0600 Subject: [PATCH 09/12] BugFix dealing with edge fall off in Select module The local variable edgeFallOff was being set instead of the field(Line 205). The getValue method was also using the control module instead of the second module(Line 122). I submitted this as a pull request to the original repository as well, but yours seems like the most likely one to be updated. Sorry for that last pull request, I closed it. I didn't like it saying 600 changes, there are only 2. --- src/libnoiseforjava/module/Select.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libnoiseforjava/module/Select.java b/src/libnoiseforjava/module/Select.java index d394aa3..786a043 100644 --- a/src/libnoiseforjava/module/Select.java +++ b/src/libnoiseforjava/module/Select.java @@ -119,7 +119,7 @@ else if (controlValue < (lowerBound + edgeFalloff)) alpha = Interp.SCurve3 ( (controlValue - lowerCurve) / (upperCurve - lowerCurve)); return Interp.linearInterp (sourceModules[0].getValue (x, y, z), - sourceModules[2].getValue (x, y, z), + sourceModules[1].getValue (x, y, z), alpha); } else if (controlValue < (upperBound - edgeFalloff)) @@ -202,7 +202,7 @@ public void setEdgeFalloff (double edgeFalloff) { // Make sure that the edge falloff curves do not overlap. double boundSize = upperBound - lowerBound; - edgeFalloff = (edgeFalloff > boundSize / 2)? boundSize / 2: edgeFalloff; + this.edgeFalloff = (edgeFalloff > boundSize / 2)? boundSize / 2: edgeFalloff; } /// Returns the control module. From a573a96042108d9ba0de5e80f803d458c15dc7bc Mon Sep 17 00:00:00 2001 From: Roah Date: Fri, 20 Mar 2015 04:19:34 +0100 Subject: [PATCH 10/12] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6fdc471 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# libnoiseforjava +Java port of the C++ library libnoise. (http://libnoise.sourceforge.net/index.html) From eaad196c5ba1edd2fa82537f1c994326a76cacc4 Mon Sep 17 00:00:00 2001 From: roah Date: Fri, 20 Mar 2015 06:06:33 +0100 Subject: [PATCH 11/12] Update on the javadoc to be read correctly. --- libnoiseforjava/build.xml | 74 + libnoiseforjava/nbproject/build-impl.xml | 1411 +++++++++++++++++ libnoiseforjava/nbproject/genfiles.properties | 8 + libnoiseforjava/nbproject/project.properties | 72 + libnoiseforjava/nbproject/project.xml | 15 + src/libnoiseforjava/NoiseGen.java | 90 +- src/libnoiseforjava/module/ModuleBase.java | 158 +- src/libnoiseforjava/module/Perlin.java | 425 ++--- 8 files changed, 1952 insertions(+), 301 deletions(-) create mode 100644 libnoiseforjava/build.xml create mode 100644 libnoiseforjava/nbproject/build-impl.xml create mode 100644 libnoiseforjava/nbproject/genfiles.properties create mode 100644 libnoiseforjava/nbproject/project.properties create mode 100644 libnoiseforjava/nbproject/project.xml diff --git a/libnoiseforjava/build.xml b/libnoiseforjava/build.xml new file mode 100644 index 0000000..5b0bfa4 --- /dev/null +++ b/libnoiseforjava/build.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project libnoiseforjava. + + + diff --git a/libnoiseforjava/nbproject/build-impl.xml b/libnoiseforjava/nbproject/build-impl.xml new file mode 100644 index 0000000..c9c56ce --- /dev/null +++ b/libnoiseforjava/nbproject/build-impl.xml @@ -0,0 +1,1411 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/libnoiseforjava/nbproject/genfiles.properties b/libnoiseforjava/nbproject/genfiles.properties new file mode 100644 index 0000000..b1673bd --- /dev/null +++ b/libnoiseforjava/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=f8ac9732 +build.xml.script.CRC32=72cd8828 +build.xml.stylesheet.CRC32=28e38971@1.56.1.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=f8ac9732 +nbproject/build-impl.xml.script.CRC32=427098b2 +nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 diff --git a/libnoiseforjava/nbproject/project.properties b/libnoiseforjava/nbproject/project.properties new file mode 100644 index 0000000..f4e0eea --- /dev/null +++ b/libnoiseforjava/nbproject/project.properties @@ -0,0 +1,72 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=libnoiseforjava +application.vendor=roah +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/libnoiseforjava.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.libnoiseforjava-src=../src +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=true +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.src.dir=${file.reference.libnoiseforjava-src} +test.src.dir=test diff --git a/libnoiseforjava/nbproject/project.xml b/libnoiseforjava/nbproject/project.xml new file mode 100644 index 0000000..8add5b7 --- /dev/null +++ b/libnoiseforjava/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + libnoiseforjava + + + + + + + + + diff --git a/src/libnoiseforjava/NoiseGen.java b/src/libnoiseforjava/NoiseGen.java index fd98be3..6b0cb55 100644 --- a/src/libnoiseforjava/NoiseGen.java +++ b/src/libnoiseforjava/NoiseGen.java @@ -28,29 +28,40 @@ public class NoiseGen { - /// Enumerates the noise quality. + /** + * Enumerates the noise quality. + * @see #QUALITY_FAST + * @see #QUALITY_STD + * @see #QUALITY_BEST + */ public enum NoiseQuality { - - /// Generates coherent noise quickly. When a coherent-noise function with - /// this quality setting is used to generate a bump-map image, there are - /// noticeable "creasing" artifacts in the resulting image. This is - /// because the derivative of that function is discontinuous at integer - /// boundaries. + + /** + * Generates coherent noise quickly, when a coherent-noise function with + * this quality setting is used to generate a bump-map image, there are + * noticeable "creasing" artifacts in the resulting image, this is + * because the derivative of that function is discontinuous at integer + * boundaries. + */ QUALITY_FAST, - - /// Generates standard-quality coherent noise. When a coherent-noise - /// function with this quality setting is used to generate a bump-map - /// image, there are some minor "creasing" artifacts in the resulting - /// image. This is because the second derivative of that function is - /// discontinuous at integer boundaries. + + /** + * Generates standard-quality coherent noise, when a coherent-noise + * function with this quality setting is used to generate a bump-map + * image, there are some minor "creasing" artifacts in the resulting + * image, this is because the second derivative of that function is + * discontinuous at integer boundaries. + */ QUALITY_STD, - - /// Generates the best-quality coherent noise. When a coherent-noise - /// function with this quality setting is used to generate a bump-map - /// image, there are no "creasing" artifacts in the resulting image. This - /// is because the first and second derivatives of that function are - /// continuous at integer boundaries. + + /** + * Generates the best-quality coherent noise when a coherent-noise + * function with this quality setting is used to generate a bump-map + * image, there are no "creasing" artifacts in the resulting image this + * is because the first and second derivatives of that function are + * continuous at integer boundaries. + */ QUALITY_BEST } @@ -232,23 +243,30 @@ public static double ValueNoise3D (int x, int y, int z, int seed) return 1.0 - ((double)IntValueNoise3D (x, y, z, seed) / 1073741824.0); } - /// Modifies a floating-point value so that it can be stored in a - /// int32 variable. - /// - /// @param n A floating-point number. - /// - /// @returns The modified floating-point number. - /// - /// This function does not modify @a n. - /// - /// In libnoise, the noise-generating algorithms are all integer-based; - /// they use variables of type int32. Before calling a noise - /// function, pass the @a x, @a y, and @a z coordinates to this function to - /// ensure that these coordinates can be cast to a int32 value. - /// - /// Although you could do a straight cast from double to int32, the - /// resulting value may differ between platforms. By using this function, - /// you ensure that the resulting value is identical between platforms. + /** + * Modifies a floating-point value so that it can be stored in a + * int32 variable. + *

+ * In libnoise, the noise-generating algorithms are all integer-based, + * they use variables of type int32. Before calling a noise + * function, pass the @a x, @a y, and @a z coordinates to + * this function to ensure that these coordinates can be cast + * to a int32 value. + * Modifies a floating-point value so that it can be stored in a + * int32 variable. + *

+ *

This function does not modify @a n.

+ *

+ * Although you could do a straight cast from double to int32, the + * resulting value may differ between platforms. By using this function, + * you ensure that the resulting value is identical between platforms. + * Modifies a floating-point value so that it can be stored in a + * int32 variable. + *

+ * + * @param n A floating-point number. + * @return The modified floating-point number. + */ public static double MakeInt32Range (double n) { if (n >= 1073741824.0) return (2.0 * (n % 1073741824.0)) - 1073741824.0; diff --git a/src/libnoiseforjava/module/ModuleBase.java b/src/libnoiseforjava/module/ModuleBase.java index 895e67b..7c050ef 100644 --- a/src/libnoiseforjava/module/ModuleBase.java +++ b/src/libnoiseforjava/module/ModuleBase.java @@ -28,10 +28,12 @@ import libnoiseforjava.exception.ExceptionInvalidParam; import libnoiseforjava.exception.ExceptionNoModule; +/** + * Base class for noise modules. + */ public class ModuleBase { - // base class for noise modules. public ModuleBase[] sourceModules; public int modulesRequired; @@ -57,25 +59,25 @@ public ModuleBase (int modulesRequired) this.modulesRequired = modulesRequired; } - - /// Returns a reference to a source module connected to this noise - /// module. - /// - /// @param index The index value assigned to the source module. - /// - /// @returns A reference to the source module. - /// - /// @pre The index value ranges from 0 to one less than the number of - /// source modules required by this noise module. - /// @pre A source module with the specified index value has been added - /// to this noise module via a call to setSourceModule(). - /// - /// @throw ExceptionNoModule See the preconditions for more - /// information. - /// - /// Each noise module requires the attachment of a certain number of - /// source modules before an application can call the getValue() - /// method. + + /** + * Each noise module requires the attachment of a certain number of + * source modules before an application can call the getValue() + * method. + * + * @pre The index value ranges from 0 to one less than the number of + * source modules required by this noise module. + * @pre A source module with the specified index value has been added + * to this noise module via a call to setSourceModule(). + * @param index The index value assigned to the source module. + *
PreCondition: The index value ranges from 0 to one + * less than the number of source modules required by this noise module. + *
PreCondition : source module with the specified index value + * has been added to this noise module via a call to setSourceModule(). + * @return A reference to the source module. + * @throws ExceptionNoModule See the preconditions for more + * information. + */ public ModuleBase getSourceModule (int index) throws ExceptionNoModule { if (sourceModules != null) @@ -92,71 +94,75 @@ public ModuleBase getSourceModule (int index) throws ExceptionNoModule throw new ExceptionNoModule ("Could not retrieve a source module " + "from a noise module."); } - - /// Returns the number of source modules required by this noise - /// module. - /// - /// @returns The number of source modules required by this noise - /// module. + + /** + * Returns the number of source modules required by this noise module. + * + * @return The number of source modules required by this noise module. + */ public int getSourceModuleCount() { return modulesRequired; } - - /// Generates an output value given the coordinates of the specified - /// input value. - /// - /// @param x The @a x coordinate of the input value. - /// @param y The @a y coordinate of the input value. - /// @param z The @a z coordinate of the input value. - /// - /// @returns The output value. - /// - /// @pre All source modules required by this noise module have been - /// passed to the setSourceModule() method. - /// - /// Before an application can call this method, it must first connect - /// all required source modules via the setSourceModule() method. If - /// these source modules are not connected to this noise module, this - /// method raises a debug assertion. - /// - /// To determine the number of source modules required by this noise - /// module, call the getSourceModuleCount() method. + + /** + * Generates an output value given the coordinates of the specified + * input value. + * + *

Before an application can call this method, it must first connect + * all required source modules via the setSourceModule() method. If + * these source modules are not connected to this noise module, this + * method raises a debug assertion.

+ * + * To determine the number of source modules required by this noise + * module, call the getSourceModuleCount() method.

+ *

+ * @pre All source modules required by this noise module have been + * passed to the setSourceModule() method. + * @param x The @a x coordinate of the input value. + * @param y The @a y coordinate of the input value. + * @param z The @a z coordinate of the input value. + * @return The output value. + *
PreCondition : All source modules required by this noise + * module have been passed to the setSourceModule() method. + */ public double getValue (double x, double y, double z) { return x; } - - /// Connects a source module to this noise module. - /// - /// @param index An index value to assign to this source module. - /// @param sourceModule The source module to attach. - /// - /// @pre The index value ranges from 0 to one less than the number of - /// source modules required by this noise module. - /// - /// @throw ExceptionInvalidParam An invalid parameter was + + /** + * Connects a source module to this noise module. + *

+ * A noise module mathematically combines the output values from the + * source modules to generate the value returned by getValue().

+ * + * The index value to assign a source module is a unique identifier + * for that source module. If an index value has already been + * assigned to a source module, this noise module replaces the old + * source module with the new source module.

+ * + * Before an application can call the getValue() method, it must + * first connect all required source modules. To determine the + * number of source modules required by this noise module, call the + * getSourceModuleCount() method.

+ * + * This source module must exist throughout the lifetime of this + * noise module unless another source module replaces that source + * module.

+ * + * A noise module does not modify a source module; it only modifies + * its output values. + *

+ * @pre The index value ranges from 0 to one less than the number of + * source modules required by this noise module. + * @param index An index value to assign to this source module. + *
PreCondition :The index value ranges from 0 to one less + * than the number of source modules required by this noise module. + * @param sourceModule The source module to attach. + * @throws ExceptionInvalidParam An invalid parameter was /// specified; see the preconditions for more information. - /// - /// A noise module mathematically combines the output values from the - /// source modules to generate the value returned by getValue(). - /// - /// The index value to assign a source module is a unique identifier - /// for that source module. If an index value has already been - /// assigned to a source module, this noise module replaces the old - /// source module with the new source module. - /// - /// Before an application can call the getValue() method, it must - /// first connect all required source modules. To determine the - /// number of source modules required by this noise module, call the - /// getSourceModuleCount() method. - /// - /// This source module must exist throughout the lifetime of this - /// noise module unless another source module replaces that source - /// module. - /// - /// A noise module does not modify a source module; it only modifies - /// its output values. + */ public void setSourceModule (int index, ModuleBase sourceModule) throws ExceptionInvalidParam { diff --git a/src/libnoiseforjava/module/Perlin.java b/src/libnoiseforjava/module/Perlin.java index 70fb22e..d455097 100644 --- a/src/libnoiseforjava/module/Perlin.java +++ b/src/libnoiseforjava/module/Perlin.java @@ -29,142 +29,176 @@ import libnoiseforjava.NoiseGen.NoiseQuality; import libnoiseforjava.exception.ExceptionInvalidParam; +/** + * Noise module that outputs 3-dimensional Perlin noise. + * + *

+ * Perlin noise is the sum of several coherent-noise functions of + * ever-increasing frequencies and ever-decreasing amplitudes.
+ * + * An important property of Perlin noise is that a small change in the + * input value will produce a small change in the output value, while a + * large change in the input value will produce a random change in the + * output value.
+ * + * This noise module outputs Perlin-noise values that usually range from + * -1.0 to +1.0, but there are no guarantees that all output values will + * exist within that range.
+ * + * For a better description of Perlin noise, see the links in the + * References and Acknowledgments section.
+ * + * This noise module does not require any source modules. + *

+ * + *

+ * Octaves
+ * The number of octaves control the amount of detail of the + * Perlin noise. Adding more octaves increases the detail of the Perlin + * noise, but with the drawback of increasing the calculation time.
+ * + * An octave is one of the coherent-noise functions in a series of + * coherent-noise functions that are added together to form Perlin + * noise.
+ * + * An application may specify the frequency of the first octave by + * calling the setFrequency() method.
+ * + * An application may specify the number of octaves that generate Perlin + * noise by calling the setOctaveCount() method.
+ * + * These coherent-noise functions are called octaves because each octave + * has, by default, double the frequency of the previous octave. Musical + * tones have this property as well; a musical C tone that is one octave + * higher than the previous C tone has double its frequency. + *

+ * + *

+ * Frequency
+ * An application may specify the frequency of the first octave by + * calling the setFrequency() method. + *

+ *

+ * Persistence
+ * + * The persistence value controls the roughness of the Perlin + * noise. Larger values produce rougher noise.
+ * + * The persistence value determines how quickly the amplitudes diminish + * for successive octaves. The amplitude of the first octave is 1.0. + * The amplitude of each subsequent octave is equal to the product of the + * previous octave's amplitude and the persistence value. So a + * persistence value of 0.5 sets the amplitude of the first octave to + * 1.0; the second, 0.5; the third, 0.25; etc.
+ * + * An application may specify the persistence value by calling the + * setPersistence() method. + *

+ *

+ * Lacunarity
+ * + * The lacunarity specifies the frequency multipler between successive + * octaves. + * + * The effect of modifying the lacunarity is subtle; you may need to play + * with the lacunarity value to determine the effects. For best results, + * set the lacunarity to a number between 1.5 and 3.5. + *

+ *

+ * References & acknowledgments
+ * + * The Noise Machine - + * From the master, Ken Perlin himself. This page contains a + * presentation that describes Perlin noise and some of its variants. + * He won an Oscar for creating the Perlin noise algorithm!
+ * + * + * Perlin Noise - Hugo Elias's webpage contains a very good + * description of Perlin noise and describes its many applications. This + * page gave me the inspiration to create libnoise in the first place. + * Now that I know how to generate Perlin noise, I will never again use + * cheesy subdivision algorithms to create terrain (unless I absolutely + * need the speed.)
+ * + * The + * Perlin noise math FAQ - A good page that describes Perlin noise in + * plain English with only a minor amount of math. During development of + * libnoise, I noticed that my coherent-noise function generated terrain + * with some "regularity" to the terrain features. This page describes a + * better coherent-noise function called gradient noise. This + * version of the Perlin module uses gradient coherent noise to + * generate Perlin noise.
+ *

+ */ public class Perlin extends ModuleBase -{ - /// Noise module that outputs 3-dimensional Perlin noise. - /// - /// Perlin noise is the sum of several coherent-noise functions of - /// ever-increasing frequencies and ever-decreasing amplitudes. - /// - /// An important property of Perlin noise is that a small change in the - /// input value will produce a small change in the output value, while a - /// large change in the input value will produce a random change in the - /// output value. - /// - /// This noise module outputs Perlin-noise values that usually range from - /// -1.0 to +1.0, but there are no guarantees that all output values will - /// exist within that range. - /// - /// For a better description of Perlin noise, see the links in the - /// References and Acknowledgments section. - /// - /// This noise module does not require any source modules. - /// - /// Octaves - /// - /// The number of octaves control the amount of detail of the - /// Perlin noise. Adding more octaves increases the detail of the Perlin - /// noise, but with the drawback of increasing the calculation time. - /// - /// An octave is one of the coherent-noise functions in a series of - /// coherent-noise functions that are added together to form Perlin - /// noise. - /// - /// An application may specify the frequency of the first octave by - /// calling the setFrequency() method. - /// - /// An application may specify the number of octaves that generate Perlin - /// noise by calling the setOctaveCount() method. - /// - /// These coherent-noise functions are called octaves because each octave - /// has, by default, double the frequency of the previous octave. Musical - /// tones have this property as well; a musical C tone that is one octave - /// higher than the previous C tone has double its frequency. - /// - /// Frequency - /// - /// An application may specify the frequency of the first octave by - /// calling the setFrequency() method. - /// - /// Persistence - /// - /// The persistence value controls the roughness of the Perlin - /// noise. Larger values produce rougher noise. - /// - /// The persistence value determines how quickly the amplitudes diminish - /// for successive octaves. The amplitude of the first octave is 1.0. - /// The amplitude of each subsequent octave is equal to the product of the - /// previous octave's amplitude and the persistence value. So a - /// persistence value of 0.5 sets the amplitude of the first octave to - /// 1.0; the second, 0.5; the third, 0.25; etc. - /// - /// An application may specify the persistence value by calling the - /// setPersistence() method. - /// - /// Lacunarity - /// - /// The lacunarity specifies the frequency multipler between successive - /// octaves. - /// - /// The effect of modifying the lacunarity is subtle; you may need to play - /// with the lacunarity value to determine the effects. For best results, - /// set the lacunarity to a number between 1.5 and 3.5. - /// - /// References & acknowledgments - /// - /// The Noise Machine - - /// From the master, Ken Perlin himself. This page contains a - /// presentation that describes Perlin noise and some of its variants. - /// He won an Oscar for creating the Perlin noise algorithm! - /// - /// - /// Perlin Noise - Hugo Elias's webpage contains a very good - /// description of Perlin noise and describes its many applications. This - /// page gave me the inspiration to create libnoise in the first place. - /// Now that I know how to generate Perlin noise, I will never again use - /// cheesy subdivision algorithms to create terrain (unless I absolutely - /// need the speed.) - /// - /// The - /// Perlin noise math FAQ - A good page that describes Perlin noise in - /// plain English with only a minor amount of math. During development of - /// libnoise, I noticed that my coherent-noise function generated terrain - /// with some "regularity" to the terrain features. This page describes a - /// better coherent-noise function called gradient noise. This - /// version of the Perlin module uses gradient coherent noise to - /// generate Perlin noise. - - - /// Default frequency for the noise::module::Perlin noise module. + { + + /** + * Default frequency for the {@see libnoiseforjava.module.Perlin} noise module. + */ static final double DEFAULT_PERLIN_FREQUENCY = 1.0; - /// Default lacunarity for the noise::module::Perlin noise module. + /** + * Default lacunarity for the noise::module::Perlin noise module. + */ static final double DEFAULT_PERLIN_LACUNARITY = 2.0; - /// Default number of octaves for the noise::module::Perlin noise module. + /** + * Default number of octaves for the noise::module::Perlin noise module. + */ static final int DEFAULT_PERLIN_OCTAVE_COUNT = 6; - /// Default persistence value for the noise::module::Perlin noise module. + /** + * Default persistence value for the noise::module::Perlin noise module. + */ static final double DEFAULT_PERLIN_PERSISTENCE = 0.5; - /// Default noise quality for the noise::module::Perlin noise module. + /** + * Default noise quality for the noise::module::Perlin noise module. + */ static final NoiseQuality DEFAULT_PERLIN_QUALITY = NoiseQuality.QUALITY_STD; - /// Default noise seed for the noise::module::Perlin noise module. + /** + * Default noise seed for the noise::module::Perlin noise module. + */ static final int DEFAULT_PERLIN_SEED = 0; - /// Maximum number of octaves for the noise::module::Perlin noise module. + /** + * Maximum number of octaves for the noise::module::Perlin noise module. + */ static final int PERLIN_MAX_OCTAVE = 30; - /// Frequency of the first octave. + /** + * Frequency of the first octave. + */ double frequency; - /// Frequency multiplier between successive octaves. + /** + * Frequency multiplier between successive octaves. + */ double lacunarity; - /// Quality of the Perlin noise. + /** + * Quality of the Perlin noise. + */ NoiseQuality noiseQuality; - /// Total number of octaves that generate the Perlin noise. + /** + * Total number of octaves that generate the Perlin noise. + */ int octaveCount; - /// Persistence of the Perlin noise. + /** + * Persistence of the Perlin noise. + */ double persistence; - /// Seed value used by the Perlin-noise function. + /** + * Seed value used by the Perlin-noise function. + */ int seed; @@ -179,6 +213,7 @@ public Perlin () seed = DEFAULT_PERLIN_SEED; } + @Override public double getValue (double x, double y, double z) { double value = 0.0; @@ -216,139 +251,151 @@ public double getValue (double x, double y, double z) return value; } - /// Returns the frequency of the first octave. - /// - /// @returns The frequency of the first octave. + /** + * Returns the frequency of the first octave. + * + * @return The frequency of the first octave. + */ public double getFrequency () { return frequency; } - /// Returns the lacunarity of the Perlin noise. - /// - /// @returns The lacunarity of the Perlin noise. - /// - /// The lacunarity is the frequency multiplier between successive - /// octaves. + /** + * The lacunarity is the frequency multiplier between successive + * octaves. + * + * @return The lacunarity of the Perlin noise. + */ public double getLacunarity () { return lacunarity; } - - /// Returns the quality of the Perlin noise. - /// - /// @returns The quality of the Perlin noise. - /// - /// See NoiseQuality for definitions of the various - /// coherent-noise qualities. + + /** + * Returns the quality of the Perlin noise. + * + * @see libnoiseforjava.NoiseGen.NoiseQuality NoiseQuality, + * for definitions of the various + * coherent-noise qualities. + * + * @return The quality of the Perlin noise. + */ public NoiseQuality getNoiseQuality () { return noiseQuality; } - - /// Returns the number of octaves that generate the Perlin noise. - /// - /// @returns The number of octaves that generate the Perlin noise. - /// - /// The number of octaves controls the amount of detail in the Perlin - /// noise. + + /** + * The number of octaves controls the amount of detail in the Perlin + * noise. + * + * @return The number of octaves that generate the Perlin noise. + */ public int getOctaveCount () { return octaveCount; } - - /// Returns the persistence value of the Perlin noise. - /// - /// @returns The persistence value of the Perlin noise. - /// - /// The persistence value controls the roughness of the Perlin noise. + + /** + * The persistence value controls the roughness of the Perlin noise. + * + * @return The persistence value of the Perlin noise. + */ public double getPersistence () { return persistence; } - /// Returns the seed value used by the Perlin-noise function. - /// - /// @returns The seed value. + /** + * Returns the seed value used by the Perlin-noise function. + * + * @return The seed value. + */ public int getSeed () { return seed; } - /// Sets the frequency of the first octave. - /// - /// @param frequency The frequency of the first octave. + /** + * Sets the frequency of the first octave. + * + * @param frequency The frequency of the first octave. + */ public void setFrequency (double frequency) { this.frequency = frequency; } - /// Sets the lacunarity of the Perlin noise. - /// - /// @param lacunarity The lacunarity of the Perlin noise. - /// - /// The lacunarity is the frequency multiplier between successive - /// octaves. - /// - /// For best results, set the lacunarity to a number between 1.5 and - /// 3.5. + /** + * The lacunarity is the frequency multiplier between successive + * octaves. + * + * For best results, set the lacunarity to a number between 1.5 and + * 3.5. + * + * @param lacunarity The lacunarity of the Perlin noise. + */ public void setLacunarity (double lacunarity) { this.lacunarity = lacunarity; } - - /// Sets the quality of the Perlin noise. - /// - /// @param noiseQuality The quality of the Perlin noise. - /// - /// See NoiseQuality for definitions of the various - /// coherent-noise qualities. + + /** + * Sets the quality of the Perlin noise. + * + * @see libnoiseforjava.NoiseGen.NoiseQuality NoiseQuality, + * for definitions of the various + * coherent-noise qualities. + * @param noiseQuality The quality of the Perlin noise. + */ public void setNoiseQuality (NoiseQuality noiseQuality) { this.noiseQuality = noiseQuality; } - - /// Sets the number of octaves that generate the Perlin noise. - /// - /// @param octaveCount The number of octaves that generate the Perlin - /// noise. - /// - /// @pre The number of octaves ranges from 1 to PERLIN_MAX_OCTAVE. - /// - /// @throw noise::ExceptionInvalidParam An invalid parameter was - /// specified; see the preconditions for more information. - /// - /// The number of octaves controls the amount of detail in the Perlin - /// noise. - /// - /// The larger the number of octaves, the more time required to - /// calculate the Perlin-noise value. + + /** + * The number of octaves controls the amount of detail in the Perlin + * noise. + * + * The larger the number of octaves, the more time required to + * calculate the Perlin-noise value. + * + * @pre The number of octaves ranges from 1 to PERLIN_MAX_OCTAVE. + * @param octaveCount The number of octaves that generate the Perlin + * noise.
+ * PreCondition : The number of octaves ranges from 1 to PERLIN_MAX_OCTAVE.) + * @throws ExceptionInvalidParam An invalid parameter was + * specified; see the preconditions for more information. + */ public void setOctaveCount (int octaveCount) throws ExceptionInvalidParam { if (octaveCount < 1 || octaveCount > PERLIN_MAX_OCTAVE) - { + { setOctaveCount(octaveCount); throw new ExceptionInvalidParam ("Invalid parameter In Perlin Noise Module"); } this.octaveCount = octaveCount; } - /// Sets the persistence value of the Perlin noise. - /// - /// @param persistence The persistence value of the Perlin noise. - /// - /// The persistence value controls the roughness of the Perlin noise. - /// - /// For best results, set the persistence to a number between 0.0 and - /// 1.0. + /** + * The persistence value controls the roughness of the Perlin noise. + * + * For best results, set the persistence to a number between 0.0 and + * 1.0. + * + * @param persistence The persistence value of the Perlin noise. + */ public void setPersistence (double persistence) { this.persistence = persistence; } - /// Sets the seed value used by the Perlin-noise function. - /// - /// @param seed The seed value. + /** + * Sets the seed value used by the Perlin-noise function. + * + * @param seed The seed value. + */ public void setSeed (int seed) { this.seed = seed; From 6bfdc85be052592d561000bfba3051ecb81064b2 Mon Sep 17 00:00:00 2001 From: roah Date: Fri, 20 Mar 2015 08:45:18 +0100 Subject: [PATCH 12/12] Update on the javadoc to be read correctly. --- src/libnoiseforjava/Interp.java | 100 ++++++++------- src/libnoiseforjava/Misc.java | 25 ++-- src/libnoiseforjava/VectorTable.java | 19 +-- src/libnoiseforjava/util/MiscUtilities.java | 10 +- src/libnoiseforjava/util/NoiseMap.java | 129 +++++++++++--------- 5 files changed, 150 insertions(+), 133 deletions(-) diff --git a/src/libnoiseforjava/Interp.java b/src/libnoiseforjava/Interp.java index 53ed56d..98eeed0 100644 --- a/src/libnoiseforjava/Interp.java +++ b/src/libnoiseforjava/Interp.java @@ -27,21 +27,21 @@ public class Interp { - - /// Performs cubic interpolation between two values bound between two other - /// values. - /// - /// @param n0 The value before the first value. - /// @param n1 The first value. - /// @param n2 The second value. - /// @param n3 The value after the second value. - /// @param a The alpha value. - /// - /// @returns The interpolated value. - /// - /// The alpha value should range from 0.0 to 1.0. If the alpha value is - /// 0.0, this function returns @a n1. If the alpha value is 1.0, this - /// function returns @a n2. + + /** + * Performs cubic interpolation between two values bound between two other + * values. + * The alpha value should range from 0.0 to 1.0. If the alpha value is + * 0.0, this function returns {@param n1}. If the alpha value is 1.0, this + * function returns {@param n2}. + * + * @param n0 The value before the first value. + * @param n1 The first value. + * @param n2 The second value. + * @param n3 The value after the second value. + * @param a The alpha value. + * @return The interpolated value. + */ public static double cubicInterp (double n0, double n1, double n2, double n3, double a) { @@ -51,51 +51,47 @@ public static double cubicInterp (double n0, double n1, double n2, double n3, double s = n1; return p * a * a * a + q * a * a + r * a + s; } - - /// Performs linear interpolation between two values. - /// - /// @param n0 The first value. - /// @param n1 The second value. - /// @param a The alpha value. - /// - /// @returns The interpolated value. - /// - /// The alpha value should range from 0.0 to 1.0. If the alpha value is - /// 0.0, this function returns @a n0. If the alpha value is 1.0, this - /// function returns @a n1. + + /** + * Performs linear interpolation between two values. + * The alpha value should range from 0.0 to 1.0. If the alpha value is + * 0.0, this function returns {@param n0}. If the alpha value is 1.0, this + * function returns {@param n1}. + * @param n0 The first value. + * @param n1 The second value. + * @param a The alpha value. + * @return The interpolated value. + */ public static double linearInterp (double n0, double n1, double a) { return ((1.0 - a) * n0) + (a * n1); } - - /// Maps a value onto a cubic S-curve. - /// - /// @param a The value to map onto a cubic S-curve. - /// - /// @returns The mapped value. - /// - /// @a a should range from 0.0 to 1.0. - /// - /// The derivative of a cubic S-curve is zero at @a a = 0.0 and @a a = - /// 1.0 + + /** + * Maps a value onto a cubic S-curve. + * {@param a} should range from 0.0 to 1.0. + * + * The derivative of a cubic S-curve is zero at {@param a} = 0.0 + * and {@param a} = 1.0 + * + * @param a The value to map onto a cubic S-curve. + * @return The mapped value. + */ public static double SCurve3 (double a) { return (a * a * (3.0 - 2.0 * a)); } - - /// Maps a value onto a quintic S-curve. - /// - /// @param a The value to map onto a quintic S-curve. - /// - /// @returns The mapped value. - /// - /// @a a should range from 0.0 to 1.0. - /// - /// The first derivative of a quintic S-curve is zero at @a a = 0.0 and - /// @a a = 1.0 - /// - /// The second derivative of a quintic S-curve is zero at @a a = 0.0 and - /// @a a = 1.0 + + /** + * Maps a value onto a quintic S-curve. + * The first derivative of a quintic S-curve is zero at {@param a} = 0.0 + * and {@param a} = 1.0 + * The second derivative of a quintic S-curve is zero at {@param a} = 0.0 + * and {@param a} = 1.0 + * + * @param a The value to map onto a quintic S-curve. + * @return The mapped value. + */ static double SCurve5 (double a) { double a3 = a * a * a; diff --git a/src/libnoiseforjava/Misc.java b/src/libnoiseforjava/Misc.java index 11d2a7f..58202b3 100644 --- a/src/libnoiseforjava/Misc.java +++ b/src/libnoiseforjava/Misc.java @@ -27,18 +27,19 @@ public class Misc { - /// Clamps a value onto a clamping range. - /// - /// @param value The value to clamp. - /// @param lowerBound The lower bound of the clamping range. - /// @param upperBound The upper bound of the clamping range. - /// - /// @returns - /// - @a value if @a value lies between @a lowerBound and @a upperBound. - /// - @a lowerBound if @a value is less than @a lowerBound. - /// - @a upperBound if @a value is greater than @a upperBound. - /// - /// This function does not modify any parameters. + + /** + * Clamps a value onto a clamping range. + * This function does not modify any parameters. + * + * @param value The value to clamp. + * @param lowerBound The lower bound of the clamping range. + * @param upperBound The upper bound of the clamping range. + * @return + * - {@param value} if lies between {@param lowerBound} and {@param upperBound}.
+ * - {@param lowerBound} if {@param value} is less than {@param lowerBound}.
+ * - {@param upperBound} if {@param value} is greater than {@param upperBound}.
+ */ public static int ClampValue (int value, int lowerBound, int upperBound) { if (value < lowerBound) diff --git a/src/libnoiseforjava/VectorTable.java b/src/libnoiseforjava/VectorTable.java index 7b904a2..8a54f22 100644 --- a/src/libnoiseforjava/VectorTable.java +++ b/src/libnoiseforjava/VectorTable.java @@ -26,14 +26,17 @@ public class VectorTable { - // A table of 256 random normalized vectors. Each row is an (x, y, z, 0) - // coordinate. The 0 is used as padding so we can use bit shifts to index - // any row in the table. These vectors have an even statistical - // distribution, which improves the quality of the coherent noise - // generated by these vectors. For more information, see "GPU Gems", - // Chapter 5 - Implementing Improved Perlin Noise by Ken Perlin, - // specifically page 76. - + + /** + * A table of 256 random normalized vectors. + * Each row is an (x, y, z, 0) coordinate. + * The 0 is used as padding so we can use bit shifts to index + * any row in the table. These vectors have an even statistical + * distribution, which improves the quality of the coherent noise + * generated by these vectors. For more information, see "GPU Gems", + * Chapter 5 - Implementing Improved Perlin Noise by Ken Perlin, + * specifically page 76. + */ private static double[][] randomVectors = { {-0.763874, -0.596439, -0.246489, 0.0}, {0.396055, 0.904518, -0.158073, 0.0}, diff --git a/src/libnoiseforjava/util/MiscUtilities.java b/src/libnoiseforjava/util/MiscUtilities.java index a18aa6b..0ed86ca 100644 --- a/src/libnoiseforjava/util/MiscUtilities.java +++ b/src/libnoiseforjava/util/MiscUtilities.java @@ -28,15 +28,19 @@ public class MiscUtilities { - // Performs linear interpolation between two 8-bit channel values. + /** + * Performs linear interpolation between two 8-bit channel values. + */ public static short blendChannel (int red, int red2, float alpha) { double c0 = (float)red / 255.0; double c1 = (float)red2 / 255.0; return (short)(((c1 * alpha) + (c0 * (1.0f - alpha))) * 255.0f); } - - // Performs linear interpolation between two colors + + /** + * Performs linear interpolation between two colors + */ public static ColorCafe linearInterpColor (ColorCafe color0, ColorCafe color1, float alpha) { diff --git a/src/libnoiseforjava/util/NoiseMap.java b/src/libnoiseforjava/util/NoiseMap.java index b0636f2..ca27ddf 100644 --- a/src/libnoiseforjava/util/NoiseMap.java +++ b/src/libnoiseforjava/util/NoiseMap.java @@ -27,30 +27,37 @@ import libnoiseforjava.exception.ExceptionInvalidParam; +/** + * Implements a noise map, a 2-dimensional array of floating-point + * values. + *

+ * A noise map is designed to store coherent-noise values generated by a + * noise module, although it can store values from any source. A noise + * map is often used as a terrain height map or a grayscale texture.
+ * + * The size (width and height) of the noise map can be specified during + * object construction.
+ * + * The getValue() method can be used to access individual + * values stored in the noise map. + *

+ */ public class NoiseMap { - /// Implements a noise map, a 2-dimensional array of floating-point - /// values. - /// - /// A noise map is designed to store coherent-noise values generated by a - /// noise module, although it can store values from any source. A noise - /// map is often used as a terrain height map or a grayscale texture. - /// - /// The size (width and height) of the noise map can be specified during - /// object construction. - /// - /// The getValue() method can be used to access individual - /// values stored in the noise map. - /// - - /// The height of the noise map. + /** + * The height of the noise map. + */ int height; - /// The width of the noise map. + /** + * The width of the noise map. + */ int width; - /// The array of doubles holding the noise map values + /** + * The array of doubles holding the noise map values + */ double[][] noiseMap; double borderValue; @@ -61,16 +68,16 @@ public NoiseMap (int width, int height) throws ExceptionInvalidParam noiseMap = new double [width][height]; borderValue = 0.0; } - - /// Returns a value from the specified position in the noise map. - /// - /// @param x The x coordinate of the position. - /// @param y The y coordinate of the position. - /// - /// @returns The value at that position. - /// - /// This method returns the border value if the coordinates exist - /// outside of the noise map. + + /** + * Returns a value from the specified position in the noise map. + * This method returns the border value if the coordinates exist + * outside of the noise map. + * + * @param x The x coordinate of the position. + * @param y The y coordinate of the position. + * @return The value at that position. + */ public double getValue (int x, int y) { if (x >= 0 && x < width && y >= 0 && y < height) @@ -93,55 +100,61 @@ public void setSize (int width, int height) throws ExceptionInvalidParam } } - /// Sets a value at a specified position in the noise map. - /// - /// @param x The x coordinate of the position. - /// @param y The y coordinate of the position. - /// @param value The value to set at the given position. - /// - /// This method does nothing if the noise map object is empty or the - /// position is outside the bounds of the noise map. + /** + * Sets a value at a specified position in the noise map. + * This method does nothing if the noise map object is empty or the + * position is outside the bounds of the noise map. + * + * @param x The x coordinate of the position. + * @param y The y coordinate of the position. + * @param value The value to set at the given position. + */ public void setValue (int x, int y, double value) { if (x >= 0 && x < width && y >= 0 && y < height) this.noiseMap[x][y]= value; } - - /// Returns the value used for all positions outside of the noise map. - /// - /// @returns The value used for all positions outside of the noise - /// map. - /// - /// All positions outside of the noise map are assumed to have a - /// common value known as the border value. + + /** + * Returns the value used for all positions outside of the noise map. + * All positions outside of the noise map are assumed to have a + * common value known as the border value. + * + * @return The value used for all positions outside of the noise map. + */ public double getBorderValue () { return borderValue; } - /// Returns the height of the noise map. - /// - /// @returns The height of the noise map. + /** + * Returns the height of the noise map. + * + * @return The height of the noise map. + */ public int getHeight () { return height; } - - /// Returns the width of the noise map. - /// - /// @returns The width of the noise map. + + /** + * Returns the width of the noise map. + * + * @return The width of the noise map. + */ public int getWidth () { return width; } - - /// Sets the value to use for all positions outside of the noise map. - /// - /// @param borderValue The value to use for all positions outside of - /// the noise map. - /// - /// All positions outside of the noise map are assumed to have a - /// common value known as the border value. + + /** + * Sets the value to use for all positions outside of the noise map. + * All positions outside of the noise map are assumed to have a + * common value known as the border value. + * + * @param borderValue The value to use for all positions outside of + * the noise map. + */ public void setBorderValue (double borderValue) { this.borderValue = borderValue;