aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/TerrainData.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/TerrainData.cs416
1 files changed, 416 insertions, 0 deletions
diff --git a/OpenSim/Framework/TerrainData.cs b/OpenSim/Framework/TerrainData.cs
new file mode 100644
index 0000000..d5dad8f
--- /dev/null
+++ b/OpenSim/Framework/TerrainData.cs
@@ -0,0 +1,416 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
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
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
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
32
33using OpenMetaverse;
34
35using log4net;
36
37namespace OpenSim.Framework
38{
39 public abstract class TerrainData
40 {
41 // Terrain always is a square
42 public int SizeX { get; protected set; }
43 public int SizeY { get; protected set; }
44 public int SizeZ { get; protected set; }
45
46 // A height used when the user doesn't specify anything
47 public const float DefaultTerrainHeight = 21f;
48
49 public abstract float this[int x, int y] { get; set; }
50 // Someday terrain will have caves
51 public abstract float this[int x, int y, int z] { get; set; }
52
53 public bool IsTainted { get; protected set; }
54 public abstract bool IsTaintedAt(int xx, int yy);
55 public abstract void ClearTaint();
56
57 public abstract void ClearLand();
58 public abstract void ClearLand(float height);
59
60 // Return a representation of this terrain for storing as a blob in the database.
61 // Returns 'true' to say blob was stored in the 'out' locations.
62 public abstract bool GetDatabaseBlob(out int DBFormatRevisionCode, out Array blob);
63
64 // Given a revision code and a blob from the database, create and return the right type of TerrainData.
65 // The sizes passed are the expected size of the region. The database info will be used to
66 // initialize the heightmap of that sized region with as much data is in the blob.
67 // Return created TerrainData or 'null' if unsuccessful.
68 public static TerrainData CreateFromDatabaseBlobFactory(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
69 {
70 // For the moment, there is only one implementation class
71 return new HeightmapTerrainData(pSizeX, pSizeY, pSizeZ, pFormatCode, pBlob);
72 }
73
74 // return a special compressed representation of the heightmap in shorts
75 public abstract short[] GetCompressedMap();
76 public abstract float CompressionFactor { get; }
77
78 public abstract double[,] GetDoubles();
79 public abstract TerrainData Clone();
80 }
81
82 // The terrain is stored in the database as a blob with a 'revision' field.
83 // Some implementations of terrain storage would fill the revision field with
84 // the time the terrain was stored. When real revisions were added and this
85 // feature removed, that left some old entries with the time in the revision
86 // field.
87 // Thus, if revision is greater than 'RevisionHigh' then terrain db entry is
88 // left over and it is presumed to be 'Legacy256'.
89 // Numbers are arbitrary and are chosen to to reduce possible mis-interpretation.
90 // If a revision does not match any of these, it is assumed to be Legacy256.
91 public enum DBTerrainRevision
92 {
93 // Terrain is 'double[256,256]'
94 Legacy256 = 11,
95 // Terrain is 'int32, int32, float[,]' where the ints are X and Y dimensions
96 // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
97 Variable2D = 22,
98 // Terrain is 'int32, int32, int32, int16[]' where the ints are X and Y dimensions
99 // and third int is the 'compression factor'. The heights are compressed as
100 // "short compressedHeight = (short)(height * compressionFactor);"
101 // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
102 Compressed2D = 27,
103 // A revision that is not listed above or any revision greater than this value is 'Legacy256'.
104 RevisionHigh = 1234
105 }
106
107 // Version of terrain that is a heightmap.
108 // This should really be 'LLOptimizedHeightmapTerrainData' as it includes knowledge
109 // of 'patches' which are 16x16 terrain areas which can be sent separately to the viewer.
110 // The heighmap is kept as an array of short integers. The integer values are converted to
111 // and from floats by TerrainCompressionFactor. Shorts are used to limit storage used.
112 public class HeightmapTerrainData : TerrainData
113 {
114 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
115 private static string LogHeader = "[TERRAIN DATA]";
116
117 // TerrainData.this[x, y]
118 public override float this[int x, int y]
119 {
120 get { return FromCompressedHeight(m_heightmap[x, y]); }
121 set {
122 short newVal = ToCompressedHeight(value);
123 if (m_heightmap[x, y] != newVal)
124 {
125 m_heightmap[x, y] = newVal;
126 m_taint[x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize] = true;
127 // m_log.DebugFormat("{0} set[{1},{2}] to {3} ({4})", LogHeader, x, y, value, newVal);
128 }
129 }
130 }
131
132 // TerrainData.this[x, y, z]
133 public override float this[int x, int y, int z]
134 {
135 get { return this[x, y]; }
136 set { this[x, y] = value; }
137 }
138
139 // TerrainData.ClearTaint
140 public override void ClearTaint()
141 {
142 IsTainted = false;
143 for (int ii = 0; ii < m_taint.GetLength(0); ii++)
144 for (int jj = 0; jj < m_taint.GetLength(1); jj++)
145 m_taint[ii, jj] = false;
146 }
147
148 // TerrainData.ClearLand
149 public override void ClearLand()
150 {
151 ClearLand(DefaultTerrainHeight);
152 }
153 // TerrainData.ClearLand(float)
154 public override void ClearLand(float pHeight)
155 {
156 short flatHeight = ToCompressedHeight(pHeight);
157 for (int xx = 0; xx < SizeX; xx++)
158 for (int yy = 0; yy < SizeY; yy++)
159 m_heightmap[xx, yy] = flatHeight;
160 }
161
162 public override bool IsTaintedAt(int xx, int yy)
163 {
164 return m_taint[xx / Constants.TerrainPatchSize, yy / Constants.TerrainPatchSize];
165 }
166
167 // TerrainData.GetDatabaseBlob
168 // The user wants something to store in the database.
169 public override bool GetDatabaseBlob(out int DBRevisionCode, out Array blob)
170 {
171 bool ret = false;
172 if (SizeX == Constants.RegionSize && SizeY == Constants.RegionSize)
173 {
174 DBRevisionCode = (int)DBTerrainRevision.Legacy256;
175 blob = ToLegacyTerrainSerialization();
176 ret = true;
177 }
178 else
179 {
180 DBRevisionCode = (int)DBTerrainRevision.Compressed2D;
181 blob = ToCompressedTerrainSerialization();
182 ret = true;
183 }
184 return ret;
185 }
186
187 // TerrainData.CompressionFactor
188 private float m_compressionFactor = 100.0f;
189 public override float CompressionFactor { get { return m_compressionFactor; } }
190
191 // TerrainData.GetCompressedMap
192 public override short[] GetCompressedMap()
193 {
194 short[] newMap = new short[SizeX * SizeY];
195
196 int ind = 0;
197 for (int xx = 0; xx < SizeX; xx++)
198 for (int yy = 0; yy < SizeY; yy++)
199 newMap[ind++] = m_heightmap[xx, yy];
200
201 return newMap;
202
203 }
204 // TerrainData.Clone
205 public override TerrainData Clone()
206 {
207 HeightmapTerrainData ret = new HeightmapTerrainData(SizeX, SizeY, SizeZ);
208 ret.m_heightmap = (short[,])this.m_heightmap.Clone();
209 return ret;
210 }
211
212 // TerrainData.GetDoubles
213 public override double[,] GetDoubles()
214 {
215 double[,] ret = new double[SizeX, SizeY];
216 for (int xx = 0; xx < SizeX; xx++)
217 for (int yy = 0; yy < SizeY; yy++)
218 ret[xx, yy] = FromCompressedHeight(m_heightmap[xx, yy]);
219
220 return ret;
221 }
222
223
224 // =============================================================
225
226 private short[,] m_heightmap;
227 // Remember subregions of the heightmap that has changed.
228 private bool[,] m_taint;
229
230 // To save space (especially for large regions), keep the height as a short integer
231 // that is coded as the float height times the compression factor (usually '100'
232 // to make for two decimal points).
233 public short ToCompressedHeight(double pHeight)
234 {
235 return (short)(pHeight * CompressionFactor);
236 }
237
238 public float FromCompressedHeight(short pHeight)
239 {
240 return ((float)pHeight) / CompressionFactor;
241 }
242
243 // To keep with the legacy theme, create an instance of this class based on the
244 // way terrain used to be passed around.
245 public HeightmapTerrainData(double[,] pTerrain)
246 {
247 SizeX = pTerrain.GetLength(0);
248 SizeY = pTerrain.GetLength(1);
249 SizeZ = (int)Constants.RegionHeight;
250 m_compressionFactor = 100.0f;
251
252 m_heightmap = new short[SizeX, SizeY];
253 for (int ii = 0; ii < SizeX; ii++)
254 {
255 for (int jj = 0; jj < SizeY; jj++)
256 {
257 m_heightmap[ii, jj] = ToCompressedHeight(pTerrain[ii, jj]);
258
259 }
260 }
261
262 m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
263 ClearTaint();
264 }
265
266 // Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
267 public HeightmapTerrainData(int pX, int pY, int pZ)
268 {
269 SizeX = pX;
270 SizeY = pY;
271 SizeZ = pZ;
272 m_compressionFactor = 100.0f;
273 m_heightmap = new short[SizeX, SizeY];
274 m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
275 ClearTaint();
276 }
277
278 public HeightmapTerrainData(short[] cmap, float pCompressionFactor, int pX, int pY, int pZ) : this(pX, pY, pZ)
279 {
280 m_compressionFactor = pCompressionFactor;
281 int ind = 0;
282 for (int xx = 0; xx < SizeX; xx++)
283 for (int yy = 0; yy < SizeY; yy++)
284 m_heightmap[xx, yy] = cmap[ind++];
285 }
286
287 // Create a heighmap from a database blob
288 public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob) : this(pSizeX, pSizeY, pSizeZ)
289 {
290 switch ((DBTerrainRevision)pFormatCode)
291 {
292 case DBTerrainRevision.Compressed2D:
293 FromCompressedTerrainSerialization(pBlob);
294 m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
295 break;
296 default:
297 FromLegacyTerrainSerialization(pBlob);
298 m_log.DebugFormat("{0} HeightmapTerrainData create from legacy serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
299 break;
300 }
301 }
302
303 // Just create an array of doubles. Presumes the caller implicitly knows the size.
304 public Array ToLegacyTerrainSerialization()
305 {
306 Array ret = null;
307
308 using (MemoryStream str = new MemoryStream((int)Constants.RegionSize * (int)Constants.RegionSize * sizeof(double)))
309 {
310 using (BinaryWriter bw = new BinaryWriter(str))
311 {
312 for (int xx = 0; xx < Constants.RegionSize; xx++)
313 {
314 for (int yy = 0; yy < Constants.RegionSize; yy++)
315 {
316 double height = this[xx, yy];
317 if (height == 0.0)
318 height = double.Epsilon;
319 bw.Write(height);
320 }
321 }
322 }
323 ret = str.ToArray();
324 }
325 return ret;
326 }
327
328 // Just create an array of doubles. Presumes the caller implicitly knows the size.
329 public void FromLegacyTerrainSerialization(byte[] pBlob)
330 {
331 // In case database info doesn't match real terrain size, initialize the whole terrain.
332 ClearLand();
333
334 using (MemoryStream mstr = new MemoryStream(pBlob))
335 {
336 using (BinaryReader br = new BinaryReader(mstr))
337 {
338 for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
339 {
340 for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
341 {
342 float val = (float)br.ReadDouble();
343 if (xx < SizeX && yy < SizeY)
344 m_heightmap[xx, yy] = ToCompressedHeight(val);
345 }
346 }
347 }
348 ClearTaint();
349 }
350 }
351
352 // See the reader below.
353 public Array ToCompressedTerrainSerialization()
354 {
355 Array ret = null;
356 using (MemoryStream str = new MemoryStream((3 * sizeof(Int32)) + (SizeX * SizeY * sizeof(Int16))))
357 {
358 using (BinaryWriter bw = new BinaryWriter(str))
359 {
360 bw.Write((Int32)DBTerrainRevision.Compressed2D);
361 bw.Write((Int32)SizeX);
362 bw.Write((Int32)SizeY);
363 bw.Write((Int32)CompressionFactor);
364 for (int yy = 0; yy < SizeY; yy++)
365 for (int xx = 0; xx < SizeX; xx++)
366 {
367 bw.Write((Int16)m_heightmap[xx, yy]);
368 }
369 }
370 ret = str.ToArray();
371 }
372 return ret;
373 }
374
375 // Initialize heightmap from blob consisting of:
376 // int32, int32, int32, int32, int16[]
377 // where the first int32 is format code, next two int32s are the X and y of heightmap data and
378 // the forth int is the compression factor for the following int16s
379 // This is just sets heightmap info. The actual size of the region was set on this instance's
380 // creation and any heights not initialized by theis blob are set to the default height.
381 public void FromCompressedTerrainSerialization(byte[] pBlob)
382 {
383 Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
384
385 using (MemoryStream mstr = new MemoryStream(pBlob))
386 {
387 using (BinaryReader br = new BinaryReader(mstr))
388 {
389 hmFormatCode = br.ReadInt32();
390 hmSizeX = br.ReadInt32();
391 hmSizeY = br.ReadInt32();
392 hmCompressionFactor = br.ReadInt32();
393
394 m_compressionFactor = hmCompressionFactor;
395
396 // In case database info doesn't match real terrain size, initialize the whole terrain.
397 ClearLand();
398
399 for (int yy = 0; yy < hmSizeY; yy++)
400 {
401 for (int xx = 0; xx < hmSizeX; xx++)
402 {
403 Int16 val = br.ReadInt16();
404 if (xx < SizeX && yy < SizeY)
405 m_heightmap[xx, yy] = val;
406 }
407 }
408 }
409 ClearTaint();
410
411 m_log.InfoFormat("{0} Read compressed 2d heightmap. Heightmap size=<{1},{2}>. Region size={<{3},{4}>. CompFact={5}", LogHeader,
412 hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
413 }
414 }
415 }
416}