diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Generators/Voronoi.cs | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Generators/Voronoi.cs b/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Generators/Voronoi.cs new file mode 100644 index 0000000..df21ecc --- /dev/null +++ b/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Generators/Voronoi.cs | |||
@@ -0,0 +1,212 @@ | |||
1 | /* | ||
2 | Redistribution and use in source and binary forms, with or without | ||
3 | modification, are permitted provided that the following conditions are | ||
4 | met: | ||
5 | |||
6 | * Redistributions of source code must retain the above copyright | ||
7 | notice, this list of conditions and the following disclaimer. | ||
8 | |||
9 | * Redistributions in binary form must reproduce the above | ||
10 | copyright notice, this list of conditions and the following | ||
11 | disclaimer in the documentation and/or other materials provided | ||
12 | with the distribution. | ||
13 | |||
14 | * Neither the name of libTerrain nor the names of | ||
15 | its contributors may be used to endorse or promote products | ||
16 | derived from this software without specific prior written | ||
17 | permission. | ||
18 | |||
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | using System; | ||
33 | using System.Collections.Generic; | ||
34 | using System.Text; | ||
35 | |||
36 | namespace libTerrain | ||
37 | { | ||
38 | partial class Channel | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// Generates a Voronoi diagram (sort of a stained glass effect) which will fill the entire channel | ||
42 | /// </summary> | ||
43 | /// <remarks>3-Clause BSD Licensed</remarks> | ||
44 | /// <param name="pointsPerBlock">The number of generator points in each block</param> | ||
45 | /// <param name="blockSize">A multiple of the channel width and height which will have voronoi points generated in it. | ||
46 | /// <para>This is to ensure a more even distribution of the points than pure random allocation.</para></param> | ||
47 | /// <param name="c">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.</param> | ||
48 | public void voronoiDiagram(int pointsPerBlock, int blockSize, double[] c) | ||
49 | { | ||
50 | List<Point2D> points = new List<Point2D>(); | ||
51 | Random generator = new Random(seed); | ||
52 | |||
53 | // Generate the emitter points | ||
54 | int x, y, i; | ||
55 | for (x = -blockSize; x < w + blockSize; x += blockSize) | ||
56 | { | ||
57 | for (y = -blockSize; y < h + blockSize; y += blockSize) | ||
58 | { | ||
59 | for (i = 0; i < pointsPerBlock; i++) | ||
60 | { | ||
61 | double pX = x + (generator.NextDouble() * (double)blockSize); | ||
62 | double pY = y + (generator.NextDouble() * (double)blockSize); | ||
63 | |||
64 | points.Add(new Point2D(pX, pY)); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | double[] distances = new double[points.Count]; | ||
70 | |||
71 | // Calculate the distance each pixel is from an emitter | ||
72 | for (x = 0; x < w; x++) | ||
73 | { | ||
74 | for (y = 0; y < h; y++) | ||
75 | { | ||
76 | for (i = 0; i < points.Count; i++) | ||
77 | { | ||
78 | double dx, dy; | ||
79 | dx = Math.Abs((double)x - points[i].x); | ||
80 | dy = Math.Abs((double)y - points[i].y); | ||
81 | |||
82 | distances[i] = (dx * dx + dy * dy); | ||
83 | } | ||
84 | |||
85 | Array.Sort(distances); | ||
86 | |||
87 | double f = 0.0; | ||
88 | |||
89 | // Multiply the distances with their 'c' counterpart | ||
90 | // ordering the distances descending | ||
91 | for (i = 0; i < c.Length; i++) | ||
92 | { | ||
93 | if (i >= points.Count) | ||
94 | break; | ||
95 | |||
96 | f += c[i] * distances[i]; | ||
97 | } | ||
98 | |||
99 | map[x, y] = f; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | // Normalise the result | ||
104 | normalise(); | ||
105 | } | ||
106 | |||
107 | public void voronoiDiagram(List<Point2D> points, double[] c) | ||
108 | { | ||
109 | |||
110 | Random generator = new Random(seed); | ||
111 | int x, y, i; | ||
112 | double[] distances = new double[points.Count]; | ||
113 | |||
114 | // Calculate the distance each pixel is from an emitter | ||
115 | for (x = 0; x < w; x++) | ||
116 | { | ||
117 | for (y = 0; y < h; y++) | ||
118 | { | ||
119 | for (i = 0; i < points.Count; i++) | ||
120 | { | ||
121 | double dx, dy; | ||
122 | dx = Math.Abs((double)x - points[i].x); | ||
123 | dy = Math.Abs((double)y - points[i].y); | ||
124 | |||
125 | distances[i] = (dx * dx + dy * dy); | ||
126 | } | ||
127 | |||
128 | Array.Sort(distances); | ||
129 | |||
130 | double f = 0.0; | ||
131 | |||
132 | // Multiply the distances with their 'c' counterpart | ||
133 | // ordering the distances descending | ||
134 | for (i = 0; i < c.Length; i++) | ||
135 | { | ||
136 | if (i >= points.Count) | ||
137 | break; | ||
138 | |||
139 | f += c[i] * distances[i]; | ||
140 | } | ||
141 | |||
142 | map[x, y] = f; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | // Normalise the result | ||
147 | normalise(); | ||
148 | } | ||
149 | |||
150 | public void voroflatDiagram(int pointsPerBlock, int blockSize) | ||
151 | { | ||
152 | List<Point2D> points = new List<Point2D>(); | ||
153 | Random generator = new Random(seed); | ||
154 | |||
155 | // Generate the emitter points | ||
156 | int x, y, i; | ||
157 | for (x = -blockSize; x < w + blockSize; x += blockSize) | ||
158 | { | ||
159 | for (y = -blockSize; y < h + blockSize; y += blockSize) | ||
160 | { | ||
161 | for (i = 0; i < pointsPerBlock; i++) | ||
162 | { | ||
163 | double pX = x + (generator.NextDouble() * (double)blockSize); | ||
164 | double pY = y + (generator.NextDouble() * (double)blockSize); | ||
165 | |||
166 | points.Add(new Point2D(pX, pY)); | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | |||
171 | double[] distances = new double[points.Count]; | ||
172 | |||
173 | // Calculate the distance each pixel is from an emitter | ||
174 | for (x = 0; x < w; x++) | ||
175 | { | ||
176 | for (y = 0; y < h; y++) | ||
177 | { | ||
178 | for (i = 0; i < points.Count; i++) | ||
179 | { | ||
180 | double dx, dy; | ||
181 | dx = Math.Abs((double)x - points[i].x); | ||
182 | dy = Math.Abs((double)y - points[i].y); | ||
183 | |||
184 | distances[i] = (dx * dx + dy * dy); | ||
185 | } | ||
186 | |||
187 | //Array.Sort(distances); | ||
188 | |||
189 | double f = 0.0; | ||
190 | |||
191 | double min = double.MaxValue; | ||
192 | for (int j = 0; j < distances.Length;j++ ) | ||
193 | { | ||
194 | if (distances[j] < min) | ||
195 | { | ||
196 | min = distances[j]; | ||
197 | f = j; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | // Multiply the distances with their 'c' counterpart | ||
202 | // ordering the distances descending | ||
203 | |||
204 | map[x, y] = f; | ||
205 | } | ||
206 | } | ||
207 | |||
208 | // Normalise the result | ||
209 | normalise(); | ||
210 | } | ||
211 | } | ||
212 | } | ||