aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs264
1 files changed, 132 insertions, 132 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs b/OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs
index b593717..daef6bd 100644
--- a/OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs
+++ b/OpenSim/Region/Environment/Modules/World/Terrain/TerrainUtil.cs
@@ -1,133 +1,133 @@
1/* 1/*
2 * Copyright (c) Contributors, http://opensimulator.org/ 2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders. 3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met: 6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright 7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright 9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the 12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products 13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission. 14 * derived from this software without specific prior written permission.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY 16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY 19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28using System; 28using System;
29using OpenSim.Region.Environment.Interfaces; 29using OpenSim.Region.Environment.Interfaces;
30 30
31namespace OpenSim.Region.Environment.Modules.World.Terrain 31namespace OpenSim.Region.Environment.Modules.World.Terrain
32{ 32{
33 public static class TerrainUtil 33 public static class TerrainUtil
34 { 34 {
35 public static double MetersToSphericalStrength(double size) 35 public static double MetersToSphericalStrength(double size)
36 { 36 {
37 return Math.Pow(2, size); 37 return Math.Pow(2, size);
38 } 38 }
39 39
40 public static double SphericalFactor(double x, double y, double rx, double ry, double size) 40 public static double SphericalFactor(double x, double y, double rx, double ry, double size)
41 { 41 {
42 return size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry)); 42 return size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry));
43 } 43 }
44 44
45 public static double GetBilinearInterpolate(double x, double y, ITerrainChannel map) 45 public static double GetBilinearInterpolate(double x, double y, ITerrainChannel map)
46 { 46 {
47 int w = map.Width; 47 int w = map.Width;
48 int h = map.Height; 48 int h = map.Height;
49 49
50 if (x > w - 2.0) 50 if (x > w - 2.0)
51 x = w - 2.0; 51 x = w - 2.0;
52 if (y > h - 2.0) 52 if (y > h - 2.0)
53 y = h - 2.0; 53 y = h - 2.0;
54 if (x < 0.0) 54 if (x < 0.0)
55 x = 0.0; 55 x = 0.0;
56 if (y < 0.0) 56 if (y < 0.0)
57 y = 0.0; 57 y = 0.0;
58 58
59 int stepSize = 1; 59 int stepSize = 1;
60 double h00 = map[(int) x, (int) y]; 60 double h00 = map[(int) x, (int) y];
61 double h10 = map[(int) x + stepSize, (int) y]; 61 double h10 = map[(int) x + stepSize, (int) y];
62 double h01 = map[(int) x, (int) y + stepSize]; 62 double h01 = map[(int) x, (int) y + stepSize];
63 double h11 = map[(int) x + stepSize, (int) y + stepSize]; 63 double h11 = map[(int) x + stepSize, (int) y + stepSize];
64 double h1 = h00; 64 double h1 = h00;
65 double h2 = h10; 65 double h2 = h10;
66 double h3 = h01; 66 double h3 = h01;
67 double h4 = h11; 67 double h4 = h11;
68 double a00 = h1; 68 double a00 = h1;
69 double a10 = h2 - h1; 69 double a10 = h2 - h1;
70 double a01 = h3 - h1; 70 double a01 = h3 - h1;
71 double a11 = h1 - h2 - h3 + h4; 71 double a11 = h1 - h2 - h3 + h4;
72 double partialx = x - (int) x; 72 double partialx = x - (int) x;
73 double partialz = y - (int) y; 73 double partialz = y - (int) y;
74 double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); 74 double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz);
75 return hi; 75 return hi;
76 } 76 }
77 77
78 private static double Noise(double x, double y) 78 private static double Noise(double x, double y)
79 { 79 {
80 int n = (int) x + (int) (y * 749); 80 int n = (int) x + (int) (y * 749);
81 n = (n << 13) ^ n; 81 n = (n << 13) ^ n;
82 return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); 82 return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
83 } 83 }
84 84
85 private static double SmoothedNoise1(double x, double y) 85 private static double SmoothedNoise1(double x, double y)
86 { 86 {
87 double corners = (Noise(x - 1, y - 1) + Noise(x + 1, y - 1) + Noise(x - 1, y + 1) + Noise(x + 1, y + 1)) / 16; 87 double corners = (Noise(x - 1, y - 1) + Noise(x + 1, y - 1) + Noise(x - 1, y + 1) + Noise(x + 1, y + 1)) / 16;
88 double sides = (Noise(x - 1, y) + Noise(x + 1, y) + Noise(x, y - 1) + Noise(x, y + 1)) / 8; 88 double sides = (Noise(x - 1, y) + Noise(x + 1, y) + Noise(x, y - 1) + Noise(x, y + 1)) / 8;
89 double center = Noise(x, y) / 4; 89 double center = Noise(x, y) / 4;
90 return corners + sides + center; 90 return corners + sides + center;
91 } 91 }
92 92
93 private static double Interpolate(double x, double y, double z) 93 private static double Interpolate(double x, double y, double z)
94 { 94 {
95 return (x * (1.0 - z)) + (y * z); 95 return (x * (1.0 - z)) + (y * z);
96 } 96 }
97 97
98 private static double InterpolatedNoise(double x, double y) 98 private static double InterpolatedNoise(double x, double y)
99 { 99 {
100 int integer_X = (int) (x); 100 int integer_X = (int) (x);
101 double fractional_X = x - integer_X; 101 double fractional_X = x - integer_X;
102 102
103 int integer_Y = (int) y; 103 int integer_Y = (int) y;
104 double fractional_Y = y - integer_Y; 104 double fractional_Y = y - integer_Y;
105 105
106 double v1 = SmoothedNoise1(integer_X, integer_Y); 106 double v1 = SmoothedNoise1(integer_X, integer_Y);
107 double v2 = SmoothedNoise1(integer_X + 1, integer_Y); 107 double v2 = SmoothedNoise1(integer_X + 1, integer_Y);
108 double v3 = SmoothedNoise1(integer_X, integer_Y + 1); 108 double v3 = SmoothedNoise1(integer_X, integer_Y + 1);
109 double v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1); 109 double v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1);
110 110
111 double i1 = Interpolate(v1, v2, fractional_X); 111 double i1 = Interpolate(v1, v2, fractional_X);
112 double i2 = Interpolate(v3, v4, fractional_X); 112 double i2 = Interpolate(v3, v4, fractional_X);
113 113
114 return Interpolate(i1, i2, fractional_Y); 114 return Interpolate(i1, i2, fractional_Y);
115 } 115 }
116 116
117 public static double PerlinNoise2D(double x, double y, int octaves, double persistence) 117 public static double PerlinNoise2D(double x, double y, int octaves, double persistence)
118 { 118 {
119 double frequency = 0.0; 119 double frequency = 0.0;
120 double amplitude = 0.0; 120 double amplitude = 0.0;
121 double total = 0.0; 121 double total = 0.0;
122 122
123 for (int i = 0; i < octaves; i++) 123 for (int i = 0; i < octaves; i++)
124 { 124 {
125 frequency = Math.Pow(2, i); 125 frequency = Math.Pow(2, i);
126 amplitude = Math.Pow(persistence, i); 126 amplitude = Math.Pow(persistence, i);
127 127
128 total += InterpolatedNoise(x * frequency, y * frequency) * amplitude; 128 total += InterpolatedNoise(x * frequency, y * frequency) * amplitude;
129 } 129 }
130 return total; 130 return total;
131 } 131 }
132 } 132 }
133} \ No newline at end of file 133} \ No newline at end of file