From 46381da9abf56eeeb524dc0adcaf1019b5398e4e Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Tue, 12 Jun 2007 23:48:03 +0000 Subject: * Importing libTerrain-BSD into libraries folder --- libraries/libTerrain/libTerrain.sln | 26 ++ libraries/libTerrain/libTerrain/Bitmap/Bitmap.cs | 77 ++++++ libraries/libTerrain/libTerrain/Channel/Channel.cs | 66 +++++ libraries/libTerrain/libTerrain/Channel/Common.cs | 228 ++++++++++++++++ .../libTerrain/Channel/Editing/Flatten.cs | 102 +++++++ .../libTerrain/libTerrain/Channel/Editing/Raise.cs | 117 ++++++++ libraries/libTerrain/libTerrain/Channel/File.cs | 75 ++++++ .../libTerrain/Channel/Generators/Cellular.cs | 1 + .../libTerrain/Channel/Generators/Fracture.cs | 113 ++++++++ .../libTerrain/Channel/Generators/Gradient.cs | 67 +++++ .../libTerrain/Channel/Generators/HillPlanter.cs | 278 +++++++++++++++++++ .../libTerrain/Channel/Generators/Midpoint.cs | 1 + .../libTerrain/Channel/Generators/Mountain.cs | 1 + .../libTerrain/Channel/Generators/Noise.cs | 57 ++++ .../libTerrain/Channel/Generators/Spiral.cs | 152 +++++++++++ .../libTerrain/Channel/Generators/Voronoi.cs | 212 +++++++++++++++ .../libTerrain/Channel/Generators/Worms.cs | 75 ++++++ libraries/libTerrain/libTerrain/Channel/Grid.cs | 264 ++++++++++++++++++ .../Channel/Manipulators/AerobicErosion.cs | 170 ++++++++++++ .../Channel/Manipulators/HydraulicErosion.cs | 1 + .../Channel/Manipulators/ThermalWeathering.cs | 113 ++++++++ .../libTerrain/libTerrain/Channel/Neighbours.cs | 144 ++++++++++ .../libTerrain/libTerrain/Channel/Operators.cs | 221 +++++++++++++++ .../libTerrain/Properties/AssemblyInfo.cs | 35 +++ libraries/libTerrain/libTerrain/TODO.txt | 42 +++ .../libTerrain/libTerrain/Test/SimpleTerrain.cs | 47 ++++ libraries/libTerrain/libTerrain/Tools/Point2D.cs | 49 ++++ libraries/libTerrain/libTerrain/Tools/Tools.cs | 60 +++++ .../libTerrain/libTerrain/libTerrain-BSD.csproj | 75 ++++++ libraries/libTerrain/libTerrainTests/Program.cs | 299 +++++++++++++++++++++ .../libTerrainTests/Properties/AssemblyInfo.cs | 33 +++ .../libTerrain/libTerrainTests/libTerrain.csproj | 75 ++++++ .../libTerrainTests/libTerrainTests.csproj | 56 ++++ 33 files changed, 3332 insertions(+) create mode 100644 libraries/libTerrain/libTerrain.sln create mode 100644 libraries/libTerrain/libTerrain/Bitmap/Bitmap.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Channel.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Common.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Editing/Flatten.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Editing/Raise.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/File.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Cellular.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Fracture.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Gradient.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/HillPlanter.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Midpoint.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Mountain.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Noise.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Spiral.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Voronoi.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Generators/Worms.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Grid.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Manipulators/AerobicErosion.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Manipulators/HydraulicErosion.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Manipulators/ThermalWeathering.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Neighbours.cs create mode 100644 libraries/libTerrain/libTerrain/Channel/Operators.cs create mode 100644 libraries/libTerrain/libTerrain/Properties/AssemblyInfo.cs create mode 100644 libraries/libTerrain/libTerrain/TODO.txt create mode 100644 libraries/libTerrain/libTerrain/Test/SimpleTerrain.cs create mode 100644 libraries/libTerrain/libTerrain/Tools/Point2D.cs create mode 100644 libraries/libTerrain/libTerrain/Tools/Tools.cs create mode 100644 libraries/libTerrain/libTerrain/libTerrain-BSD.csproj create mode 100644 libraries/libTerrain/libTerrainTests/Program.cs create mode 100644 libraries/libTerrain/libTerrainTests/Properties/AssemblyInfo.cs create mode 100644 libraries/libTerrain/libTerrainTests/libTerrain.csproj create mode 100644 libraries/libTerrain/libTerrainTests/libTerrainTests.csproj diff --git a/libraries/libTerrain/libTerrain.sln b/libraries/libTerrain/libTerrain.sln new file mode 100644 index 0000000..3ccfe23 --- /dev/null +++ b/libraries/libTerrain/libTerrain.sln @@ -0,0 +1,26 @@ + +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libTerrainTests", "libTerrainTests\libTerrainTests.csproj", "{7214C9E2-1390-41BD-BFFC-83FA5931C5CF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "libTerrain-BSD", "libTerrain\libTerrain-BSD.csproj", "{EEC06368-30E8-42E8-A23C-2C9D139AD495}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7214C9E2-1390-41BD-BFFC-83FA5931C5CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7214C9E2-1390-41BD-BFFC-83FA5931C5CF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7214C9E2-1390-41BD-BFFC-83FA5931C5CF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7214C9E2-1390-41BD-BFFC-83FA5931C5CF}.Release|Any CPU.Build.0 = Release|Any CPU + {EEC06368-30E8-42E8-A23C-2C9D139AD495}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EEC06368-30E8-42E8-A23C-2C9D139AD495}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EEC06368-30E8-42E8-A23C-2C9D139AD495}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EEC06368-30E8-42E8-A23C-2C9D139AD495}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/libraries/libTerrain/libTerrain/Bitmap/Bitmap.cs b/libraries/libTerrain/libTerrain/Bitmap/Bitmap.cs new file mode 100644 index 0000000..923da47 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Bitmap/Bitmap.cs @@ -0,0 +1,77 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace libTerrain +{ + class Raster + { + int w; + int h; + Bitmap bmp; + + public Raster(int width, int height) + { + w = width; + h = height; + bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); + } + + public Channel toChannel() + { + Channel chan = new Channel(bmp.Width, bmp.Height); + + int x, y; + for (x = 0; x < bmp.Width; x++) + { + for (y = 0; y < bmp.Height; y++) + { + Color val = bmp.GetPixel(x, y); + chan.map[x, y] = (((double)val.R + (double)val.G + (double)val.B) / 3.0) / 255.0; + } + } + + return chan; + } + + public void drawText(string txt, string font, double size) + { + Graphics gd = Graphics.FromImage(bmp); + //gd.DrawString(txt, + + + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Channel.cs b/libraries/libTerrain/libTerrain/Channel/Channel.cs new file mode 100644 index 0000000..9849ef2 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Channel.cs @@ -0,0 +1,66 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + + +/* Channel + * A channel is a single heightmap array + * */ + +namespace libTerrain +{ + partial class Channel + { + public double[,] map; + public int w; + public int h; + + public int seed = 1338; // One better than 1337 + + public Channel() + { + w = 256; + h = 256; + map = new double[w, h]; + } + + public Channel(int width, int height) + { + w = width; + h = height; + map = new double[w, h]; + } + + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Common.cs b/libraries/libTerrain/libTerrain/Channel/Common.cs new file mode 100644 index 0000000..438359c --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Common.cs @@ -0,0 +1,228 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + public partial class Channel + { + public int getWidth() + { + return w; + } + public int getHeight() + { + return h; + } + + public Channel copy() + { + Channel x = new Channel(w, h); + x.map = (double[,])this.map.Clone(); + return x; + } + + public void set(int x, int y, double val) + { + if (x >= w) + throw new Exception("Bounds error while setting pixel (width)"); + if (y >= h) + throw new Exception("Bounds error while setting pixel (height)"); + if (x < 0) + throw new Exception("Bounds error while setting pixel (width)"); + if (y < 0) + throw new Exception("Bounds error while setting pixel (height)"); + + map[x, y] = val; + } + + public void setClip(int x, int y, double val) + { + if (x >= w) + throw new Exception("Bounds error while setting pixel (width)"); + if (y >= h) + throw new Exception("Bounds error while setting pixel (height)"); + if (x < 0) + throw new Exception("Bounds error while setting pixel (width)"); + if (y < 0) + throw new Exception("Bounds error while setting pixel (height)"); + + if (val > 1.0) + val = 1.0; + if (val < 0.0) + val = 0.0; + + map[x, y] = val; + } + + private double getBilinearInterpolate(double x, double y) + { + if (x > w - 2.0) + x = w - 2.0; + if (y > h - 2.0) + y = h - 2.0; + if (x < 0.0) + x = 0.0; + if (y < 0.0) + y = 0.0; + + int STEP_SIZE = 1; + double h00 = get((int)x, (int)y); + double h10 = get((int)x + STEP_SIZE, (int)y); + double h01 = get((int)x, (int)y + STEP_SIZE); + double h11 = get((int)x + STEP_SIZE, (int)y + STEP_SIZE); + double h1 = h00; + double h2 = h10; + double h3 = h01; + double h4 = h11; + double a00 = h1; + double a10 = h2 - h1; + double a01 = h3 - h1; + double a11 = h1 - h2 - h3 + h4; + double partialx = x - (int)x; + double partialz = y - (int)y; + double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); + return hi; + } + + public double get(int x, int y) + { + if (x >= w) + x = w - 1; + if (y >= h) + y = h - 1; + if (x < 0) + x = 0; + if (y < 0) + y = 0; + return map[x, y]; + } + + public void setWrap(int x, int y, double val) + { + map[x % w, y % h] = val; + } + + public void setWrapClip(int x, int y, double val) + { + if (val > 1.0) + val = 1.0; + if (val < 0.0) + val = 0.0; + + map[x % w, y % h] = val; + } + + public void fill(double val) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = val; + } + } + } + + public void fill(double min, double max, double val) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (map[x, y] >= min && map[x, y] <= max) + map[x, y] = val; + } + } + } + + public double findMax() + { + int x, y; + double max = double.MinValue; + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (map[x, y] > max) + max = map[x, y]; + } + } + + return max; + } + + public double findMin() + { + int x, y; + double min = double.MaxValue; + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (map[x, y] < min) + min = map[x, y]; + } + } + + return min; + } + + public double sum() + { + int x, y; + double sum = 0.0; + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + sum += map[x, y]; + } + } + + return sum; + } + + public double avg() + { + return sum() / (w * h); + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Editing/Flatten.cs b/libraries/libTerrain/libTerrain/Channel/Editing/Flatten.cs new file mode 100644 index 0000000..5e5254d --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Editing/Flatten.cs @@ -0,0 +1,102 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Flattens the area underneath rx,ry by moving it to the average of the area. Uses a spherical mask provided by the raise() function. + /// + /// The X coordinate of the terrain mask + /// The Y coordinate of the terrain mask + /// The size of the terrain mask + /// The scale of the terrain mask + public void flatten(double rx, double ry, double size, double amount) + { + // Generate the mask + Channel temp = new Channel(w, h); + temp.fill(0); + temp.raise(rx, ry, size, amount); + temp.normalise(); + double total_mod = temp.sum(); + + // Establish the average height under the area + Channel newmap = new Channel(w, h); + newmap.map = (double[,])map.Clone(); + + newmap *= temp; + + double total_terrain = newmap.sum(); + double avg_height = total_terrain / total_mod; + + // Create a flat terrain using the average height + Channel flat = new Channel(w, h); + flat.fill(avg_height); + + // Blend the current terrain with the average height terrain + // using the "raised" empty terrain as a mask + blend(flat, temp); + + } + + public void flatten(Channel mask, double amount) + { + // Generate the mask + Channel temp = mask * amount; + temp.clip(0, 1); // Cut off out-of-bounds values + + double total_mod = temp.sum(); + + // Establish the average height under the area + Channel map = new Channel(w, h); + map.map = (double[,])this.map.Clone(); + + map *= temp; + + double total_terrain = map.sum(); + double avg_height = total_terrain / total_mod; + + // Create a flat terrain using the average height + Channel flat = new Channel(w, h); + flat.fill(avg_height); + + // Blend the current terrain with the average height terrain + // using the "raised" empty terrain as a mask + blend(flat, temp); + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Editing/Raise.cs b/libraries/libTerrain/libTerrain/Channel/Editing/Raise.cs new file mode 100644 index 0000000..ce1ce98 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Editing/Raise.cs @@ -0,0 +1,117 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Raises land around the selection + /// + /// The center the X coordinate of where you wish to raise the land + /// The center the Y coordinate of where you wish to raise the land + /// The radius of the dimple + /// How much impact to add to the terrain (0..2 usually) + public void raise(double rx, double ry, double size, double amount) + { + raiseSphere(rx, ry, size, amount); + } + + /// + /// Raises land in a sphere around the selection + /// + /// The center the X coordinate of where you wish to raise the land + /// The center the Y coordinate of where you wish to raise the land + /// The radius of the sphere dimple + /// How much impact to add to the terrain (0..2 usually) + public void raiseSphere(double rx, double ry, double size, double amount) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double z = size; + z *= z; + z -= ((x - rx) * (x - rx)) + ((y - ry) * (y - ry)); + + if (z < 0) + z = 0; + + map[x, y] += z * amount; + } + } + } + + /// + /// Lowers land in a sphere around the selection + /// + /// The center the X coordinate of where you wish to lower the land + /// The center the Y coordinate of where you wish to lower the land + /// The radius of the sphere dimple + /// How much impact to remove from the terrain (0..2 usually) + public void lower(double rx, double ry, double size, double amount) + { + lowerSphere(rx, ry, size, amount); + } + + /// + /// Lowers land in a sphere around the selection + /// + /// The center the X coordinate of where you wish to lower the land + /// The center the Y coordinate of where you wish to lower the land + /// The radius of the sphere dimple + /// How much impact to remove from the terrain (0..2 usually) + public void lowerSphere(double rx, double ry, double size, double amount) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double z = size; + z *= z; + z -= ((x - rx) * (x - rx)) + ((y - ry) * (y - ry)); + + if (z < 0) + z = 0; + + map[x, y] -= z * amount; + } + } + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/File.cs b/libraries/libTerrain/libTerrain/Channel/File.cs new file mode 100644 index 0000000..4163a5f --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/File.cs @@ -0,0 +1,75 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace libTerrain +{ + partial class Channel + { + public Channel loadImage(string filename) + { + Bitmap bit = new Bitmap(filename); + Channel chan = new Channel(bit.Width, bit.Height); + + int x, y; + for (x = 0; x < bit.Width; x++) + { + for (y = 0; y < bit.Height; y++) + { + Color val = bit.GetPixel(x, y); + chan.map[x, y] = (((double)val.R + (double)val.G + (double)val.B) / 3.0) / 255.0; + } + } + + return chan; + } + + public void saveImage(string filename) + { + Bitmap bit = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb); + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + int val = Math.Min(255, (int)(map[x,y] * 255)); + Color col = Color.FromArgb(val,val,val); + bit.SetPixel(x, y, col); + } + } + bit.Save(filename); + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Cellular.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Cellular.cs new file mode 100644 index 0000000..0cec05d --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Cellular.cs @@ -0,0 +1 @@ +/* Needs BSD rewrite */ \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Fracture.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Fracture.cs new file mode 100644 index 0000000..2a24ecf --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Fracture.cs @@ -0,0 +1,113 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Produces a set of coordinates defined by an edge point. Eg - 0 = 0,0. 256 = 0,256. 512 = 256,256 + /// Assumes a 256^2 heightmap. This needs fixing for input values of w,h + /// + /// + /// + /// + /// + private int[] radialEdge256(int val) + { + // Four cases: + // 1. 000..255 return 0,val + // 2. 256..511 return val - 256,255 + // 3. 512..767 return 255, val - 511 + // 4. 768..1023 return val - 768,0 + + int[] ret = new int[2]; + + if (val < 256) + { + ret[0] = 0; + ret[1] = val; + return ret; + } + if (val < 512) + { + ret[0] = (val % 256); + ret[1] = 255; + return ret; + } + if (val < 768) + { + ret[0] = 255; + ret[1] = 255 - (val % 256); + return ret; + } + if (val < 1024) + { + ret[0] = 255 - (val % 256); + ret[1] = 255; + return ret; + } + + throw new Exception("Out of bounds parameter (val)"); + } + public void fracture(int number, double scalemin, double scalemax) + { + Random rand = new Random(seed); + + for (int i = 0; i < number; i++) + { + int[] a, b; + + a = radialEdge256(rand.Next(1023)); // TODO: Broken + b = radialEdge256(rand.Next(1023)); // TODO: Broken + double z = rand.NextDouble(); + + for (int x = 0; x < w; x++) + { + for (int y = 0; y < h; y++) + { + double miny = Tools.linearInterpolate(a[1], b[1], (double)x / (double)w); + + if (y > miny) + { + map[x, y] += Tools.linearInterpolate(scalemin, scalemax, z); + } + } + } + } + normalise(); + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Gradient.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Gradient.cs new file mode 100644 index 0000000..8f45bf7 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Gradient.cs @@ -0,0 +1,67 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + + public void gradientCube() + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = x*y; + } + } + normalise(); + } + + public void gradientStripe() + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = x; + } + } + normalise(); + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/HillPlanter.cs b/libraries/libTerrain/libTerrain/Channel/Generators/HillPlanter.cs new file mode 100644 index 0000000..9316911 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/HillPlanter.cs @@ -0,0 +1,278 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Generates a series of spheres which are then either max()'d or added together. Inspired by suggestion from jh. + /// + /// 3-Clause BSD Licensed + /// The number of hills to generate + /// The minimum size of each hill + /// The maximum size of each hill + /// Whether to bias hills towards the center of the map + /// Whether to add hills together or to pick the largest value + /// Generates hill-shaped noise instead of consistent hills + public void hillsSpheres(int number, double scale_min, double scale_range, bool island, bool additive, bool noisy) + { + Random random = new Random(seed); + + int x, y; + int i; + + for (i = 0; i < number; i++) + { + double rx = Math.Min(255.0, random.NextDouble() * w); + double ry = Math.Min(255.0, random.NextDouble() * h); + double rand = random.NextDouble(); + + if (island) + { + // Move everything towards the center + rx -= w / 2; + rx /= 2; + rx += w / 2; + + ry -= h / 2; + ry /= 2; + ry += h / 2; + } + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (noisy) + rand = random.NextDouble(); + + double z = (scale_min + (scale_range * rand)); + z *= z; + z -= ((x - rx) * (x - rx)) + ((y - ry) * (y - ry)); + + if (z < 0) + z = 0; + + if (additive) + { + map[x, y] += z; + } + else + { + map[x, y] = Math.Max(map[x, y], z); + } + } + } + } + + normalise(); + } + + /// + /// Generates a series of cones which are then either max()'d or added together. Inspired by suggestion from jh. + /// + /// 3-Clause BSD Licensed + /// The number of hills to generate + /// The minimum size of each hill + /// The maximum size of each hill + /// Whether to bias hills towards the center of the map + /// Whether to add hills together or to pick the largest value + /// Generates hill-shaped noise instead of consistent hills + public void hillsCones(int number, double scale_min, double scale_range, bool island, bool additive, bool noisy) + { + Random random = new Random(seed); + + int x, y; + int i; + + for (i = 0; i < number; i++) + { + double rx = Math.Min(255.0, random.NextDouble() * w); + double ry = Math.Min(255.0, random.NextDouble() * h); + double rand = random.NextDouble(); + + if (island) + { + // Move everything towards the center + rx -= w / 2; + rx /= 2; + rx += w / 2; + + ry -= h / 2; + ry /= 2; + ry += h / 2; + } + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (noisy) + rand = random.NextDouble(); + + double z = (scale_min + (scale_range * rand)); + z -= Math.Sqrt(((x - rx) * (x - rx)) + ((y - ry) * (y - ry))); + + if (z < 0) + z = 0; + + if (additive) + { + map[x, y] += z; + } + else + { + map[x, y] = Math.Max(map[x, y], z); + } + } + } + } + + normalise(); + } + + public void hillsBlocks(int number, double scale_min, double scale_range, bool island, bool additive, bool noisy) + { + Random random = new Random(seed); + + int x, y; + int i; + + for (i = 0; i < number; i++) + { + double rx = Math.Min(255.0, random.NextDouble() * w); + double ry = Math.Min(255.0, random.NextDouble() * h); + double rand = random.NextDouble(); + + if (island) + { + // Move everything towards the center + rx -= w / 2; + rx /= 2; + rx += w / 2; + + ry -= h / 2; + ry /= 2; + ry += h / 2; + } + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (noisy) + rand = random.NextDouble(); + + double z = (scale_min + (scale_range * rand)); + z -= Math.Abs(x-rx) + Math.Abs(y-ry); + //z -= Math.Sqrt(((x - rx) * (x - rx)) + ((y - ry) * (y - ry))); + + if (z < 0) + z = 0; + + if (additive) + { + map[x, y] += z; + } + else + { + map[x, y] = Math.Max(map[x, y], z); + } + } + } + } + + normalise(); + } + + public void hillsSquared(int number, double scale_min, double scale_range, bool island, bool additive, bool noisy) + { + Random random = new Random(seed); + + int x, y; + int i; + + for (i = 0; i < number; i++) + { + double rx = Math.Min(255.0, random.NextDouble() * w); + double ry = Math.Min(255.0, random.NextDouble() * h); + double rand = random.NextDouble(); + + if (island) + { + // Move everything towards the center + rx -= w / 2; + rx /= 2; + rx += w / 2; + + ry -= h / 2; + ry /= 2; + ry += h / 2; + } + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + if (noisy) + rand = random.NextDouble(); + + double z = (scale_min + (scale_range * rand)); + z *= z * z * z; + double dx = Math.Abs(x - rx); + double dy = Math.Abs(y - ry); + z -= (dx * dx * dx * dx) + (dy * dy * dy * dy); + + if (z < 0) + z = 0; + + if (additive) + { + map[x, y] += z; + } + else + { + map[x, y] = Math.Max(map[x, y], z); + } + } + } + } + + normalise(); + } + + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Midpoint.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Midpoint.cs new file mode 100644 index 0000000..0cec05d --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Midpoint.cs @@ -0,0 +1 @@ +/* Needs BSD rewrite */ \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Mountain.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Mountain.cs new file mode 100644 index 0000000..0cec05d --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Mountain.cs @@ -0,0 +1 @@ +/* Needs BSD rewrite */ \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Noise.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Noise.cs new file mode 100644 index 0000000..7789d3e --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Noise.cs @@ -0,0 +1,57 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Fills a channel with 0..1 noise + /// + /// 3-Clause BSD Licensed + public void noise() + { + Random rand = new Random(seed); + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = rand.NextDouble(); + } + } + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Spiral.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Spiral.cs new file mode 100644 index 0000000..ede4600 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Spiral.cs @@ -0,0 +1,152 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + private double[] coordinatesToPolar(int x, int y) + { + + double theta = Math.Atan2(x - (w / 2), y - (h / 2)); + double rx = (double)x - ((double)w / 2); + double ry = (double)y - ((double)h / 2); + double r = Math.Sqrt((rx * rx) + (ry * ry)); + + double[] coords = new double[2]; + coords[0] = r; + coords[1] = theta; + return coords; + } + + public int[] polarToCoordinates(double r, double theta) { + double nx; + double ny; + + nx = (double)r * Math.Cos(theta); + ny = (double)r * Math.Sin(theta); + + nx += w / 2; + ny += h / 2; + + if (nx >= w) + nx = w - 1; + + if (ny >= h) + ny = h - 1; + + if (nx < 0) + nx = 0; + + if (ny < 0) + ny = 0; + + int[] coords = new int[2]; + coords[0] = (int)nx; + coords[1] = (int)ny; + return coords; + } + + public void Polar() + { + Channel n = this.copy(); + + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double[] coords = coordinatesToPolar(x,y); + + coords[0] += w / 2.0; + coords[1] += h / 2.0; + + map[x, y] = n.map[(int)coords[0] % n.w, (int)coords[1] % n.h]; + } + } + } + + public void SpiralPlanter(int steps, double incAngle, double incRadius, double offsetRadius, double offsetAngle) + { + int i; + double r = offsetRadius; + double theta = offsetAngle; + for (i = 0; i < steps; i++) + { + r += incRadius; + theta += incAngle; + + int[] coords = polarToCoordinates(r,theta); + raise(coords[0], coords[1], 20, 1); + } + } + + public void SpiralCells(int steps, double incAngle, double incRadius, double offsetRadius, double offsetAngle, double[] c) + { + List points = new List(); + + int i; + double r = offsetRadius; + double theta = offsetAngle; + for (i = 0; i < steps; i++) + { + r += incRadius; + theta += incAngle; + + int[] coords = polarToCoordinates(r, theta); + points.Add(new Point2D(coords[0],coords[1])); + } + + voronoiDiagram(points, c); + } + + public void Spiral(double wid, double hig, double offset) + { + int x, y, z; + z = 0; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + z++; + double dx = Math.Abs((w / 2) - x); + double dy = Math.Abs((h / 2) - y); + map[x, y] += Math.Sin(dx / wid) + Math.Cos(dy / hig); + } + } + normalise(); + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Voronoi.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Voronoi.cs new file mode 100644 index 0000000..df21ecc --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Voronoi.cs @@ -0,0 +1,212 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Generates a Voronoi diagram (sort of a stained glass effect) which will fill the entire channel + /// + /// 3-Clause BSD Licensed + /// The number of generator points in each block + /// A multiple of the channel width and height which will have voronoi points generated in it. + /// This is to ensure a more even distribution of the points than pure random allocation. + /// The Voronoi diagram type. Usually an array with values consisting of [-1,1]. Experiment with the chain, you can have as many values as you like. + public void voronoiDiagram(int pointsPerBlock, int blockSize, double[] c) + { + List points = new List(); + Random generator = new Random(seed); + + // Generate the emitter points + int x, y, i; + for (x = -blockSize; x < w + blockSize; x += blockSize) + { + for (y = -blockSize; y < h + blockSize; y += blockSize) + { + for (i = 0; i < pointsPerBlock; i++) + { + double pX = x + (generator.NextDouble() * (double)blockSize); + double pY = y + (generator.NextDouble() * (double)blockSize); + + points.Add(new Point2D(pX, pY)); + } + } + } + + double[] distances = new double[points.Count]; + + // Calculate the distance each pixel is from an emitter + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + for (i = 0; i < points.Count; i++) + { + double dx, dy; + dx = Math.Abs((double)x - points[i].x); + dy = Math.Abs((double)y - points[i].y); + + distances[i] = (dx * dx + dy * dy); + } + + Array.Sort(distances); + + double f = 0.0; + + // Multiply the distances with their 'c' counterpart + // ordering the distances descending + for (i = 0; i < c.Length; i++) + { + if (i >= points.Count) + break; + + f += c[i] * distances[i]; + } + + map[x, y] = f; + } + } + + // Normalise the result + normalise(); + } + + public void voronoiDiagram(List points, double[] c) + { + + Random generator = new Random(seed); + int x, y, i; + double[] distances = new double[points.Count]; + + // Calculate the distance each pixel is from an emitter + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + for (i = 0; i < points.Count; i++) + { + double dx, dy; + dx = Math.Abs((double)x - points[i].x); + dy = Math.Abs((double)y - points[i].y); + + distances[i] = (dx * dx + dy * dy); + } + + Array.Sort(distances); + + double f = 0.0; + + // Multiply the distances with their 'c' counterpart + // ordering the distances descending + for (i = 0; i < c.Length; i++) + { + if (i >= points.Count) + break; + + f += c[i] * distances[i]; + } + + map[x, y] = f; + } + } + + // Normalise the result + normalise(); + } + + public void voroflatDiagram(int pointsPerBlock, int blockSize) + { + List points = new List(); + Random generator = new Random(seed); + + // Generate the emitter points + int x, y, i; + for (x = -blockSize; x < w + blockSize; x += blockSize) + { + for (y = -blockSize; y < h + blockSize; y += blockSize) + { + for (i = 0; i < pointsPerBlock; i++) + { + double pX = x + (generator.NextDouble() * (double)blockSize); + double pY = y + (generator.NextDouble() * (double)blockSize); + + points.Add(new Point2D(pX, pY)); + } + } + } + + double[] distances = new double[points.Count]; + + // Calculate the distance each pixel is from an emitter + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + for (i = 0; i < points.Count; i++) + { + double dx, dy; + dx = Math.Abs((double)x - points[i].x); + dy = Math.Abs((double)y - points[i].y); + + distances[i] = (dx * dx + dy * dy); + } + + //Array.Sort(distances); + + double f = 0.0; + + double min = double.MaxValue; + for (int j = 0; j < distances.Length;j++ ) + { + if (distances[j] < min) + { + min = distances[j]; + f = j; + } + } + + // Multiply the distances with their 'c' counterpart + // ordering the distances descending + + map[x, y] = f; + } + } + + // Normalise the result + normalise(); + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Generators/Worms.cs b/libraries/libTerrain/libTerrain/Channel/Generators/Worms.cs new file mode 100644 index 0000000..a54c946 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Generators/Worms.cs @@ -0,0 +1,75 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// Generates 'number' of worms which navigate randomly around the landscape creating terrain as they go. + /// + /// The number of worms which will traverse the map + /// The number of steps each worm will traverse + /// The maximum distance each worm will move each step + /// The size of the area around the worm modified + /// Do worms start in the middle, or randomly? + public void worms(int number, int rounds, double movement, double size, bool centerspawn) + { + Random random = new Random(seed); + int i, j; + + for (i = 0; i < number; i++) + { + double rx, ry; + if (centerspawn) + { + rx = w / 2.0; + ry = h / 2.0; + } + else + { + rx = random.NextDouble() * (w - 1); + ry = random.NextDouble() * (h - 1); + } + for (j = 0; j < rounds; j++) + { + rx += (random.NextDouble() * movement) - (movement / 2.0); + ry += (random.NextDouble() * movement) - (movement / 2.0); + raise(rx, ry, size, 1.0); + } + } + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Grid.cs b/libraries/libTerrain/libTerrain/Channel/Grid.cs new file mode 100644 index 0000000..41d20f6 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Grid.cs @@ -0,0 +1,264 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + public Channel normalise() + { + double max = findMax(); + double min = findMin(); + + int x, y; + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = (map[x, y] - min) * (1.0 / (max - min)); + } + } + + return this; + } + + public Channel normalise(double minv, double maxv) + { + double max = findMax(); + double min = findMin(); + + int x, y; + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double val = (map[x, y] - min) * (1.0 / max - min); + val *= maxv - minv; + val += minv; + + map[x, y] = val; + } + } + + return this; + } + + public Channel clip() + { + int x, y; + + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + setClip(x, y, map[x, y]); + } + } + + return this; + } + + public Channel clip(double min, double max) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double val = map[x, y]; + if (val > max) val = max; + if (val < min) val = min; + map[x, y] = val; + } + } + return this; + } + + public Channel crop(int x1, int y1, int x2, int y2) + { + int width = x1 - x2 + 1; + int height = y1 - y2 + 1; + Channel chan = new Channel(width, height); + + int x, y; + int nx, ny; + + nx = 0; + for (x = x1; x < x2; x++) + { + ny = 0; + for (y = y1; y < y2; y++) + { + chan.map[nx, ny] = map[x, y]; + + ny++; + } + nx++; + } + + return this; + } + + public Channel addClip(Channel other) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = other.map[x, y]; + if (map[x, y] > 1) + map[x, y] = 1; + if (map[x, y] < 0) + map[x, y] = 0; + } + } + return this; + } + + public void smooth(double amount) + { + double area = amount; + double step = amount / 4.0; + + double[,] manipulate = new double[w, h]; + int x, y; + double n, l; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double average = 0.0; + int avgsteps = 0; + + for (n = 0.0 - area; n < area; n += step) + { + for (l = 0.0 - area; l < area; l += step) + { + avgsteps++; + average += getBilinearInterpolate(x + n, y + l); + } + } + + manipulate[x, y] = average / avgsteps; + } + } + map = manipulate; + } + + public void pertubation(double amount) + { + // Simple pertubation filter + double[,] manipulated = new double[w, h]; + Random generator = new Random(seed); // Seeds FTW! + //double amount = 8.0; + + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + double offset_x = (double)x + (generator.NextDouble() * amount) - (amount / 2.0); + double offset_y = (double)y + (generator.NextDouble() * amount) - (amount / 2.0); + double p = getBilinearInterpolate(offset_x, offset_y); + manipulated[x, y] = p; + } + } + map = manipulated; + } + + public void pertubationMask(Channel mask) + { + // Simple pertubation filter + double[,] manipulated = new double[w, h]; + Random generator = new Random(seed); // Seeds FTW! + //double amount = 8.0; + + double amount; + + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + amount = mask.map[x, y]; + double offset_x = (double)x + (generator.NextDouble() * amount) - (amount / 2.0); + double offset_y = (double)y + (generator.NextDouble() * amount) - (amount / 2.0); + + if (offset_x > w) + offset_x = w - 1; + if (offset_y > h) + offset_y = h - 1; + if (offset_y < 0) + offset_y = 0; + if (offset_x < 0) + offset_x = 0; + + double p = getBilinearInterpolate(offset_x, offset_y); + manipulated[x, y] = p; + } + } + map = manipulated; + } + + public Channel blend(Channel other, double amount) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = Tools.linearInterpolate(map[x,y],other.map[x,y],amount); + } + } + return this; + } + + public Channel blend(Channel other, Channel amount) + { + int x, y; + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + map[x, y] = Tools.linearInterpolate(map[x, y], other.map[x, y], amount.map[x,y]); + } + } + return this; + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Manipulators/AerobicErosion.cs b/libraries/libTerrain/libTerrain/Channel/Manipulators/AerobicErosion.cs new file mode 100644 index 0000000..ebeabe9 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Manipulators/AerobicErosion.cs @@ -0,0 +1,170 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + // Ideas for Aerobic erosion + // + // Unlike thermal (gravity) and hydraulic (water suspension) + // aerobic erosion should displace mass by moving sediment + // in "hops". The length of the hop being dictated by the + // presence of sharp cliffs and wind speed. + + // The ability to pickup sediment is defined by the total + // surface area, such that: + // 0 0 0 + // 0 1 0 + // 0 0 0 + // Would be the best possible value for sediment to be + // picked up (total difference = 8) and flatter land + // will erode less quickly. + + // Suspended particles assist the erosion process by hitting + // the surface and chiselling additional particles off faster + // than alone. + + // Particles are deposited when one of two conditions is met + // First: + // When particles hit a wall - such that the + // wind direction points at a difference >= the + // deposition mininum talus. + // Second: + // When wind speed is lowered to below the minimum + // required for transit. An idea for this is to + // use the navier-stokes algorithms for simulating + // pressure across the terrain. + + /// + /// An experimental erosion algorithm developed by Adam. Moves sediment by factoring the surface area of each height point. + /// + /// 0..1 The speed of the wind + /// The minimum angle at which rock is eroded 0..1 (recommended: <= 0.30) + /// The minimum angle at which rock is dropped 0..1 (recommended: >= 0.00) + /// The percentage of rock which can be picked up to pickup 0..1 + /// The number of erosion rounds (recommended: 25+) + /// Drop sediment at the lowest point? + public void AerobicErosion(double windspeed, double pickup_talus_minimum, double drop_talus_minimum, double carry, int rounds, bool lowest) + { + Channel wind = new Channel(w, h) ; + Channel sediment = new Channel(w, h); + int x, y, i, j; + + wind = this.copy(); + wind.normalise(); // Cheap wind calculations + wind *= windspeed; + wind.pertubation(30); // Can do better later + + for (i = 0; i < rounds; i++) + { + // Convert some rocks to sand + for (x = 1; x < w - 1; x++) + { + for (y = 1; y < h - 1; y++) + { + double me = get(x, y); + double surfacearea = 0.3; // Everything will erode even if it's flat. Just slower. + + for (j = 0; j < 9; j++) + { + int[] coords = neighbours(NEIGHBOURS.NEIGHBOUR_MOORE, j); + double target = get(x + coords[0], y + coords[1]); + + surfacearea += Math.Abs(target - me); + } + + double amount = surfacearea * wind.map[x, y] * carry; + + if (amount < 0) + amount = 0; + + if (surfacearea > pickup_talus_minimum) + { + this.map[x, y] -= amount; + sediment.map[x, y] += amount; + } + } + } + sediment.pertubation(10); // Sediment is blown around a bit + sediment.seed++; + wind.pertubation(15); // So is the wind + wind.seed++; + + // Convert some sand to rock + for (x = 1; x < w - 1; x++) + { + for (y = 1; y < h - 1; y++) + { + double me = get(x, y); + double surfacearea = 0.01; // Flat land does not get deposition + double min = double.MaxValue; + int[] minside = new int[2]; + + for (j = 0; j < 9; j++) + { + int[] coords = neighbours(NEIGHBOURS.NEIGHBOUR_MOORE, j); + double target = get(x + coords[0], y + coords[1]); + + surfacearea += Math.Abs(target - me); + + if (target < min && lowest) + { + minside = (int[])coords.Clone(); + min = target; + } + } + + double amount = surfacearea * (1.0 - wind.map[x, y]) * carry; + + if (amount < 0) + amount = 0; + + if (surfacearea > drop_talus_minimum) + { + this.map[x + minside[0], y + minside[1]] += amount; + sediment.map[x, y] -= amount; + } + } + } + + } + + Channel myself = this; + myself += sediment; + myself.normalise(); + } + } +} \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Manipulators/HydraulicErosion.cs b/libraries/libTerrain/libTerrain/Channel/Manipulators/HydraulicErosion.cs new file mode 100644 index 0000000..0cec05d --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Manipulators/HydraulicErosion.cs @@ -0,0 +1 @@ +/* Needs BSD rewrite */ \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Channel/Manipulators/ThermalWeathering.cs b/libraries/libTerrain/libTerrain/Channel/Manipulators/ThermalWeathering.cs new file mode 100644 index 0000000..0cae56d --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Manipulators/ThermalWeathering.cs @@ -0,0 +1,113 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /// + /// A thermal weathering implementation based on Musgrave's original 1989 algorithm. This is Adam's custom implementation which may differ slightly from the original. + /// + /// The rock angle (represented as a dy/dx ratio) at which point it will be succeptible to breakage + /// The number of erosion rounds + /// The amount of rock to carry each round + public Channel thermalWeathering(double talus, int rounds, double c) + { + double[,] lastFrame; + double[,] thisFrame; + + lastFrame = (double[,])map.Clone(); + thisFrame = (double[,])map.Clone(); + + NEIGHBOURS type = NEIGHBOURS.NEIGHBOUR_MOORE; // Using moore neighbourhood (twice as computationally expensive) + int NEIGHBOUR_ME = 4; // I am always 4 in both systems. + + int NEIGHBOUR_MAX = type == NEIGHBOURS.NEIGHBOUR_MOORE ? 9 : 5; + + int frames = rounds; // Number of thermal erosion iterations to run + int i, j; + int x, y; + + for (i = 0; i < frames; i++) + { + for (x = 0; x < w; x++) + { + for (y = 0; y < h; y++) + { + for (j = 0; j < NEIGHBOUR_MAX; j++) + { + if (j != NEIGHBOUR_ME) + { + int[] coords = neighbours(type, j); + + coords[0] += x; + coords[1] += y; + + if (coords[0] > w - 1) + coords[0] = w - 1; + if (coords[1] > h - 1) + coords[1] = h - 1; + if (coords[0] < 0) + coords[0] = 0; + if (coords[1] < 0) + coords[1] = 0; + + double heightF = thisFrame[x, y]; + double target = thisFrame[coords[0], coords[1]]; + + if (target > heightF + talus) + { + double calc = c * ((target - heightF) - talus); + heightF += calc; + target -= calc; + } + + thisFrame[x, y] = heightF; + thisFrame[coords[0], coords[1]] = target; + + } + } + } + } + lastFrame = (double[,])thisFrame.Clone(); + } + + map = thisFrame; + + normalise(); // Just to guaruntee a smooth 0..1 value + return this; + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Neighbours.cs b/libraries/libTerrain/libTerrain/Channel/Neighbours.cs new file mode 100644 index 0000000..f2b4aed --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Neighbours.cs @@ -0,0 +1,144 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + enum NEIGHBOURS + { + NEIGHBOUR_MOORE, + NEIGHBOUR_VONNEUMANN + }; + + private int[] neighbours(NEIGHBOURS type, int index) + { + int[] coord = new int[2]; + + index++; + + switch (type) + { + case NEIGHBOURS.NEIGHBOUR_MOORE: + switch (index) + { + case 1: + coord[0] = -1; + coord[1] = -1; + break; + + case 2: + coord[0] = -0; + coord[1] = -1; + break; + + case 3: + coord[0] = +1; + coord[1] = -1; + break; + + case 4: + coord[0] = -1; + coord[1] = -0; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + case 6: + coord[0] = +1; + coord[1] = -0; + break; + + case 7: + coord[0] = -1; + coord[1] = +1; + break; + + case 8: + coord[0] = -0; + coord[1] = +1; + break; + + case 9: + coord[0] = +1; + coord[1] = +1; + break; + + default: + break; + } + break; + + case NEIGHBOURS.NEIGHBOUR_VONNEUMANN: + switch (index) + { + case 1: + coord[0] = 0; + coord[1] = -1; + break; + + case 2: + coord[0] = -1; + coord[1] = 0; + break; + + case 3: + coord[0] = +1; + coord[1] = 0; + break; + + case 4: + coord[0] = 0; + coord[1] = +1; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + default: + break; + } + break; + } + + return coord; + } + } +} diff --git a/libraries/libTerrain/libTerrain/Channel/Operators.cs b/libraries/libTerrain/libTerrain/Channel/Operators.cs new file mode 100644 index 0000000..81db690 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Channel/Operators.cs @@ -0,0 +1,221 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + partial class Channel + { + /* Operator combination of channel datatypes */ + + public static Channel operator +(Channel A, Channel B) + { + if (A.h != B.h) + throw new Exception("Cannot add heightmaps, of different height."); + if (A.w != B.w) + throw new Exception("Cannot add heightmaps, of different width."); + + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] += B.map[x, y]; + } + } + + return A; + } + + public static Channel operator *(Channel A, Channel B) + { + if (A.h != B.h) + throw new Exception("Cannot multiply heightmaps, of different height."); + if (A.w != B.w) + throw new Exception("Cannot multiply heightmaps, of different width."); + + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] *= B.map[x, y]; + } + } + + return A; + } + + public static Channel operator -(Channel A, Channel B) + { + if (A.h != B.h) + throw new Exception("Cannot subtract heightmaps, of different height."); + if (A.w != B.w) + throw new Exception("Cannot subtract heightmaps, of different width."); + + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] -= B.map[x, y]; + } + } + + return A; + } + + public static Channel operator /(Channel A, Channel B) + { + if (A.h != B.h) + throw new Exception("Cannot divide heightmaps, of different height."); + if (A.w != B.w) + throw new Exception("Cannot divide heightmaps, of different width."); + + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] /= B.map[x, y]; + } + } + + return A; + } + + public static Channel operator ^(Channel A, Channel B) + { + if (A.h != B.h) + throw new Exception("Cannot divide heightmaps, of different height."); + if (A.w != B.w) + throw new Exception("Cannot divide heightmaps, of different width."); + + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] = Math.Pow(A.map[x,y],B.map[x, y]); + } + } + + return A; + } + + + /* Operator combination of channel and double datatypes */ + + public static Channel operator +(Channel A, double B) + { + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] += B; + } + } + + return A; + } + + public static Channel operator -(Channel A, double B) + { + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] -= B; + } + } + + return A; + } + + public static Channel operator *(Channel A, double B) + { + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] *= B; + } + } + + return A; + } + + public static Channel operator /(Channel A, double B) + { + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] /= B; + } + } + + return A; + } + + public static Channel operator ^(Channel A, double B) + { + int x, y; + + for (x = 0; x < A.w; x++) + { + for (y = 0; y < A.h; y++) + { + A.map[x, y] = Math.Pow(A.map[x,y],B); + } + } + + return A; + } + + } +} diff --git a/libraries/libTerrain/libTerrain/Properties/AssemblyInfo.cs b/libraries/libTerrain/libTerrain/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d47f359 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("libTerrain")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("libTerrain")] +[assembly: AssemblyCopyright("Copyright © 2007")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("69f89dd2-fbd1-400e-bb0d-ac73b80853de")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/libraries/libTerrain/libTerrain/TODO.txt b/libraries/libTerrain/libTerrain/TODO.txt new file mode 100644 index 0000000..e1998ac --- /dev/null +++ b/libraries/libTerrain/libTerrain/TODO.txt @@ -0,0 +1,42 @@ +TODO + Anyone is free to take on one of these projects + since implementations offer differ in results + the more the merrier. + + New Generators + + * Musgraves multifractal suite + * Hybrid Multifractal + * Ridged Multifractal + * Heterogenous procedural terrain + + Example implementation: + http://cobweb.ecn.purdue.edu/~ebertd/texture/1stEdition/musgrave/musgrave.c + + * Ken Perlin's noise functions + * Perlin noise + * Original + * Improved versions + + * Port the libnoise.sourceforge.net generators + + * Cellular / Organic Generators + **ADDED CELLULAR.CS** // Adam Frisby (20/03/2007) + + * Finish porting the procedurality code in + /branches/proceduality/ + currently Java, but porting is easy enough + mostly a case of find/search/replace. + + Improvements + + * HillPlanter.cs + * Add rectangular & square hills + + * Voronoi.cs + * Add different distance metrics + Currently using Eulicidean distance + Want: + * Manhattan Distance + * Camberra Distance + * Chebychev Distance \ No newline at end of file diff --git a/libraries/libTerrain/libTerrain/Test/SimpleTerrain.cs b/libraries/libTerrain/libTerrain/Test/SimpleTerrain.cs new file mode 100644 index 0000000..9f0967b --- /dev/null +++ b/libraries/libTerrain/libTerrain/Test/SimpleTerrain.cs @@ -0,0 +1,47 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain.Test +{ + class SimpleTerrain + { + public SimpleTerrain() + { + Channel terrain = new Channel(256, 256); + terrain.fill(0.5); + terrain.thermalWeathering(0.1, 50, 0.1); + } + } +} diff --git a/libraries/libTerrain/libTerrain/Tools/Point2D.cs b/libraries/libTerrain/libTerrain/Tools/Point2D.cs new file mode 100644 index 0000000..69c1148 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Tools/Point2D.cs @@ -0,0 +1,49 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + public class Point2D + { + public double x; + public double y; + + public Point2D(double X, double Y) + { + x = X; + y = Y; + } + } +} diff --git a/libraries/libTerrain/libTerrain/Tools/Tools.cs b/libraries/libTerrain/libTerrain/Tools/Tools.cs new file mode 100644 index 0000000..3f7ab68 --- /dev/null +++ b/libraries/libTerrain/libTerrain/Tools/Tools.cs @@ -0,0 +1,60 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace libTerrain +{ + class Tools + { + public static double linearInterpolate(double a, double b, double amount) + { + return a + ((b - a) * amount); + } + public static double exponentialInterpolate(double a, double b, double amount) + { + a = Math.Pow(a, amount); + b = Math.Pow(b - a, 1.0 - amount); + return a+b; + } + public static int powerOf2Log2(int n) { + for (int i = 0; i < 31; i++) { + if ((n & 1) == 1) { + return i; + } + n >>= 1; + } + return 0; + } + } +} diff --git a/libraries/libTerrain/libTerrain/libTerrain-BSD.csproj b/libraries/libTerrain/libTerrain/libTerrain-BSD.csproj new file mode 100644 index 0000000..df6d651 --- /dev/null +++ b/libraries/libTerrain/libTerrain/libTerrain-BSD.csproj @@ -0,0 +1,75 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {EEC06368-30E8-42E8-A23C-2C9D139AD495} + Library + Properties + libTerrain + libTerrain-BSD + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/libTerrain/libTerrainTests/Program.cs b/libraries/libTerrain/libTerrainTests/Program.cs new file mode 100644 index 0000000..cf3da3d --- /dev/null +++ b/libraries/libTerrain/libTerrainTests/Program.cs @@ -0,0 +1,299 @@ +/* +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of libTerrain nor the names of + its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +using System; +using System.Collections.Generic; +using System.Text; +using libTerrain; + +namespace libTerrainTests +{ + class Program + { + static void Main(string[] args) + { + System.Console.WriteLine("Starting tests..."); + + Channel test; + + try + { + System.Console.WriteLine("Blank Heightmap"); + test = new Channel(); + test.fill(0); + test.saveImage("test_blank.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + + try + { + System.Console.WriteLine("Grayscale Cube"); + test = new Channel(); + test.fill(0); + test.gradientCube(); + test.saveImage("test_cube.png"); + + test.Polar(); + test.saveImage("test_polarcube.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Spiral Planters"); + test = new Channel(); + test.fill(0); + + test.SpiralPlanter(200, Math.PI / 15, 0.75, 0, 0); + test.normalise(); + //test.Spiral(192, 192, 50); + test.saveImage("test_spiral.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Spiral Cells"); + test = new Channel(); + test.fill(0); + + double[] c = new double[2]; + c[0] = -1; + c[1] = 1; + + test.SpiralCells(200, Math.PI / 15, 0.75, 0, 0, c); + test.normalise(); + //test.Spiral(192, 192, 50); + test.saveImage("test_spiralcells.png"); + + test.fill(0); + test.SpiralCells(30, Math.PI / 30, 0, 75, 0, c); + test.normalise(); + //test.Spiral(192, 192, 50); + test.saveImage("test_circlecells.png"); + + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Fracturing"); + test = new Channel(); + test.fill(0); + test.fracture(300, 0, 1); + test.saveImage("test_fracture.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Voronoi (Flat)"); + test = new Channel(); + test.fill(0); + test.voroflatDiagram(64, 384); + test.saveImage("test_voroflat.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + + try + { + System.Console.WriteLine("Voronoi (Flat / Fixnormal)"); + test = new Channel(); + test.fill(0); + test.voroflatDiagram(64, 384); + test ^= 4; + test.normalise(); + test.saveImage("test_voroflatfixnormal.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("File Import (Mask Flatten)"); + test = new Channel(); + Channel test2; + test2 = test.loadImage("test_chained_aerobic.png"); + + test.fill(0); + test.voroflatDiagram(64, 384); + test ^= 4; + test.normalise(); + + test.smooth(4); + test.normalise(); + + test.saveImage("test_flatmask.png"); + test2.flatten(test, 1.0); + + test2.saveImage("test_fileflatmask.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Worms"); + test = new Channel(); + test.worms(100, 10, 20.0, 16, false); + test.normalise(); + test.saveImage("test_worms.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Noise"); + test = new Channel(); + test.noise(); + test.saveImage("test_noise.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Hills (Spherical)"); + test = new Channel(); + test.hillsSpheres(200, 20, 30, true, true, false); + test.saveImage("test_hillspheres.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Hills (Blocks)"); + test = new Channel(); + test.hillsBlocks(200, 20, 30, true, true, false); +// test.hillsSpheres(200, 20, 30, true, true, false); + test.saveImage("test_hillblocks.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Hills (Cones)"); + test = new Channel(); + test.fill(0); + test.hillsCones(200, 20, 30, true, true, false); + test.normalise(); + test.saveImage("test_hillcones.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Voronoi Diagram"); + test = new Channel(); + double[] c = new double[2]; + c[0] = -1; + c[1] = 1; + test.voronoiDiagram(4, 128, c); + test.saveImage("test_voronoi.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Raising Terrain"); + test = new Channel(); + test.fill(0); + test.raise(128, 128, 64, 1.0); + test.normalise(); + test.saveImage("test_raise.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + try + { + System.Console.WriteLine("Flattening Terrain (unmasked)"); + test = new Channel(); + test.noise(); + test.flatten(128, 128, 64, 1.0); + test.normalise(); + test.saveImage("test_flatten.png"); + } + catch (Exception e) + { + System.Console.WriteLine("Unhandled exception: " + e.ToString()); + } + + + + System.Console.WriteLine("Done"); + } + } +} diff --git a/libraries/libTerrain/libTerrainTests/Properties/AssemblyInfo.cs b/libraries/libTerrain/libTerrainTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..09e0845 --- /dev/null +++ b/libraries/libTerrain/libTerrainTests/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("libTerrainTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("libTerrainTests")] +[assembly: AssemblyCopyright("Copyright © 2007")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("528417c9-dd71-487e-8e0b-e8e42e52fa10")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/libraries/libTerrain/libTerrainTests/libTerrain.csproj b/libraries/libTerrain/libTerrainTests/libTerrain.csproj new file mode 100644 index 0000000..047ad02 --- /dev/null +++ b/libraries/libTerrain/libTerrainTests/libTerrain.csproj @@ -0,0 +1,75 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {EEC06368-30E8-42E8-A23C-2C9D139AD495} + Library + Properties + libTerrain + libTerrain + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libraries/libTerrain/libTerrainTests/libTerrainTests.csproj b/libraries/libTerrain/libTerrainTests/libTerrainTests.csproj new file mode 100644 index 0000000..4227155 --- /dev/null +++ b/libraries/libTerrain/libTerrainTests/libTerrainTests.csproj @@ -0,0 +1,56 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {7214C9E2-1390-41BD-BFFC-83FA5931C5CF} + Exe + Properties + libTerrainTests + libTerrainTests + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + {EEC06368-30E8-42E8-A23C-2C9D139AD495} + libTerrain-BSD + + + + + + + + \ No newline at end of file -- cgit v1.1