aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/TerrainData.cs
diff options
context:
space:
mode:
authorRobert Adams2015-03-27 19:32:50 -0700
committerRobert Adams2015-03-27 19:32:50 -0700
commitbedafb8fae9898ef0c5fc6470236ee7244e616a9 (patch)
tree618728d92dad56dd587c243892d966b9ff9ea89b /OpenSim/Framework/TerrainData.cs
parentvarregion: restore checkAgentAccessToRegion routine in EntityTransferModule. (diff)
downloadopensim-SC_OLD-bedafb8fae9898ef0c5fc6470236ee7244e616a9.zip
opensim-SC_OLD-bedafb8fae9898ef0c5fc6470236ee7244e616a9.tar.gz
opensim-SC_OLD-bedafb8fae9898ef0c5fc6470236ee7244e616a9.tar.bz2
opensim-SC_OLD-bedafb8fae9898ef0c5fc6470236ee7244e616a9.tar.xz
varregion: refactor use of 'double heightmap[,]' into references to new class TerrainData
and push the implementation from Scene into the database readers and writers.
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/TerrainData.cs464
1 files changed, 464 insertions, 0 deletions
diff --git a/OpenSim/Framework/TerrainData.cs b/OpenSim/Framework/TerrainData.cs
new file mode 100644
index 0000000..6b1be4e
--- /dev/null
+++ b/OpenSim/Framework/TerrainData.cs
@@ -0,0 +1,464 @@
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 abstract bool IsTaintedAt(int xx, int yy);
54 public abstract bool IsTaintedAt(int xx, int yy, bool clearOnTest);
55 public abstract void TaintAllTerrain();
56 public abstract void ClearTaint();
57
58 public abstract void ClearLand();
59 public abstract void ClearLand(float height);
60
61 // Return a representation of this terrain for storing as a blob in the database.
62 // Returns 'true' to say blob was stored in the 'out' locations.
63 public abstract bool GetDatabaseBlob(out int DBFormatRevisionCode, out Array blob);
64
65 // Given a revision code and a blob from the database, create and return the right type of TerrainData.
66 // The sizes passed are the expected size of the region. The database info will be used to
67 // initialize the heightmap of that sized region with as much data is in the blob.
68 // Return created TerrainData or 'null' if unsuccessful.
69 public static TerrainData CreateFromDatabaseBlobFactory(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
70 {
71 // For the moment, there is only one implementation class
72 return new HeightmapTerrainData(pSizeX, pSizeY, pSizeZ, pFormatCode, pBlob);
73 }
74
75 // return a special compressed representation of the heightmap in ints
76 public abstract int[] GetCompressedMap();
77 public abstract float CompressionFactor { get; }
78
79 public abstract float[] GetFloatsSerialized();
80 public abstract double[,] GetDoubles();
81 public abstract TerrainData Clone();
82 }
83
84 // The terrain is stored in the database as a blob with a 'revision' field.
85 // Some implementations of terrain storage would fill the revision field with
86 // the time the terrain was stored. When real revisions were added and this
87 // feature removed, that left some old entries with the time in the revision
88 // field.
89 // Thus, if revision is greater than 'RevisionHigh' then terrain db entry is
90 // left over and it is presumed to be 'Legacy256'.
91 // Numbers are arbitrary and are chosen to to reduce possible mis-interpretation.
92 // If a revision does not match any of these, it is assumed to be Legacy256.
93 public enum DBTerrainRevision
94 {
95 // Terrain is 'double[256,256]'
96 Legacy256 = 11,
97 // Terrain is 'int32, int32, float[,]' where the ints are X and Y dimensions
98 // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
99 Variable2D = 22,
100 // Terrain is 'int32, int32, int32, int16[]' where the ints are X and Y dimensions
101 // and third int is the 'compression factor'. The heights are compressed as
102 // "int compressedHeight = (int)(height * compressionFactor);"
103 // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
104 Compressed2D = 27,
105 // A revision that is not listed above or any revision greater than this value is 'Legacy256'.
106 RevisionHigh = 1234
107 }
108
109 // Version of terrain that is a heightmap.
110 // This should really be 'LLOptimizedHeightmapTerrainData' as it includes knowledge
111 // of 'patches' which are 16x16 terrain areas which can be sent separately to the viewer.
112 // The heighmap is kept as an array of integers. The integer values are converted to
113 // and from floats by TerrainCompressionFactor.
114 public class HeightmapTerrainData : TerrainData
115 {
116 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
117 private static string LogHeader = "[HEIGHTMAP TERRAIN DATA]";
118
119 // TerrainData.this[x, y]
120 public override float this[int x, int y]
121 {
122 get { return FromCompressedHeight(m_heightmap[x, y]); }
123 set {
124 int newVal = ToCompressedHeight(value);
125 if (m_heightmap[x, y] != newVal)
126 {
127 m_heightmap[x, y] = newVal;
128 m_taint[x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize] = true;
129 }
130 }
131 }
132
133 // TerrainData.this[x, y, z]
134 public override float this[int x, int y, int z]
135 {
136 get { return this[x, y]; }
137 set { this[x, y] = value; }
138 }
139
140 // TerrainData.ClearTaint
141 public override void ClearTaint()
142 {
143 SetAllTaint(false);
144 }
145
146 // TerrainData.TaintAllTerrain
147 public override void TaintAllTerrain()
148 {
149 SetAllTaint(true);
150 }
151
152 private void SetAllTaint(bool setting)
153 {
154 for (int ii = 0; ii < m_taint.GetLength(0); ii++)
155 for (int jj = 0; jj < m_taint.GetLength(1); jj++)
156 m_taint[ii, jj] = setting;
157 }
158
159 // TerrainData.ClearLand
160 public override void ClearLand()
161 {
162 ClearLand(DefaultTerrainHeight);
163 }
164 // TerrainData.ClearLand(float)
165 public override void ClearLand(float pHeight)
166 {
167 int flatHeight = ToCompressedHeight(pHeight);
168 for (int xx = 0; xx < SizeX; xx++)
169 for (int yy = 0; yy < SizeY; yy++)
170 m_heightmap[xx, yy] = flatHeight;
171 }
172
173 // Return 'true' of the patch that contains these region coordinates has been modified.
174 // Note that checking the taint clears it.
175 // There is existing code that relies on this feature.
176 public override bool IsTaintedAt(int xx, int yy, bool clearOnTest)
177 {
178 int tx = xx / Constants.TerrainPatchSize;
179 int ty = yy / Constants.TerrainPatchSize;
180 bool ret = m_taint[tx, ty];
181 if (ret && clearOnTest)
182 m_taint[tx, ty] = false;
183 return ret;
184 }
185
186 // Old form that clears the taint flag when we check it.
187 public override bool IsTaintedAt(int xx, int yy)
188 {
189 return IsTaintedAt(xx, yy, true /* clearOnTest */);
190 }
191
192 // TerrainData.GetDatabaseBlob
193 // The user wants something to store in the database.
194 public override bool GetDatabaseBlob(out int DBRevisionCode, out Array blob)
195 {
196 bool ret = false;
197 if (SizeX == Constants.RegionSize && SizeY == Constants.RegionSize)
198 {
199 DBRevisionCode = (int)DBTerrainRevision.Legacy256;
200 blob = ToLegacyTerrainSerialization();
201 ret = true;
202 }
203 else
204 {
205 DBRevisionCode = (int)DBTerrainRevision.Compressed2D;
206 blob = ToCompressedTerrainSerialization();
207 ret = true;
208 }
209 return ret;
210 }
211
212 // TerrainData.CompressionFactor
213 private float m_compressionFactor = 100.0f;
214 public override float CompressionFactor { get { return m_compressionFactor; } }
215
216 // TerrainData.GetCompressedMap
217 public override int[] GetCompressedMap()
218 {
219 int[] newMap = new int[SizeX * SizeY];
220
221 int ind = 0;
222 for (int xx = 0; xx < SizeX; xx++)
223 for (int yy = 0; yy < SizeY; yy++)
224 newMap[ind++] = m_heightmap[xx, yy];
225
226 return newMap;
227
228 }
229 // TerrainData.Clone
230 public override TerrainData Clone()
231 {
232 HeightmapTerrainData ret = new HeightmapTerrainData(SizeX, SizeY, SizeZ);
233 ret.m_heightmap = (int[,])this.m_heightmap.Clone();
234 return ret;
235 }
236
237 // TerrainData.GetFloatsSerialized
238 // This one dimensional version is ordered so height = map[y*sizeX+x];
239 // DEPRECATED: don't use this function as it does not retain the dimensions of the terrain
240 // and the caller will probably do the wrong thing if the terrain is not the legacy 256x256.
241 public override float[] GetFloatsSerialized()
242 {
243 int points = SizeX * SizeY;
244 float[] heights = new float[points];
245
246 int idx = 0;
247 for (int jj = 0; jj < SizeY; jj++)
248 for (int ii = 0; ii < SizeX; ii++)
249 {
250 heights[idx++] = FromCompressedHeight(m_heightmap[ii, jj]);
251 }
252
253 return heights;
254 }
255
256 // TerrainData.GetDoubles
257 public override double[,] GetDoubles()
258 {
259 double[,] ret = new double[SizeX, SizeY];
260 for (int xx = 0; xx < SizeX; xx++)
261 for (int yy = 0; yy < SizeY; yy++)
262 ret[xx, yy] = FromCompressedHeight(m_heightmap[xx, yy]);
263
264 return ret;
265 }
266
267
268 // =============================================================
269
270 private int[,] m_heightmap;
271 // Remember subregions of the heightmap that has changed.
272 private bool[,] m_taint;
273
274 // To save space (especially for large regions), keep the height as a short integer
275 // that is coded as the float height times the compression factor (usually '100'
276 // to make for two decimal points).
277 public int ToCompressedHeight(double pHeight)
278 {
279 return (int)(pHeight * CompressionFactor);
280 }
281
282 public float FromCompressedHeight(int pHeight)
283 {
284 return ((float)pHeight) / CompressionFactor;
285 }
286
287 // To keep with the legacy theme, create an instance of this class based on the
288 // way terrain used to be passed around.
289 public HeightmapTerrainData(double[,] pTerrain)
290 {
291 SizeX = pTerrain.GetLength(0);
292 SizeY = pTerrain.GetLength(1);
293 SizeZ = (int)Constants.RegionHeight;
294 m_compressionFactor = 100.0f;
295
296 m_heightmap = new int[SizeX, SizeY];
297 for (int ii = 0; ii < SizeX; ii++)
298 {
299 for (int jj = 0; jj < SizeY; jj++)
300 {
301 m_heightmap[ii, jj] = ToCompressedHeight(pTerrain[ii, jj]);
302
303 }
304 }
305 // m_log.DebugFormat("{0} new by doubles. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
306
307 m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
308 ClearTaint();
309 }
310
311 // Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
312 public HeightmapTerrainData(int pX, int pY, int pZ)
313 {
314 SizeX = pX;
315 SizeY = pY;
316 SizeZ = pZ;
317 m_compressionFactor = 100.0f;
318 m_heightmap = new int[SizeX, SizeY];
319 m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
320 // m_log.DebugFormat("{0} new by dimensions. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
321 ClearTaint();
322 ClearLand(0f);
323 }
324
325 public HeightmapTerrainData(int[] cmap, float pCompressionFactor, int pX, int pY, int pZ) : this(pX, pY, pZ)
326 {
327 m_compressionFactor = pCompressionFactor;
328 int ind = 0;
329 for (int xx = 0; xx < SizeX; xx++)
330 for (int yy = 0; yy < SizeY; yy++)
331 m_heightmap[xx, yy] = cmap[ind++];
332 // m_log.DebugFormat("{0} new by compressed map. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
333 }
334
335 // Create a heighmap from a database blob
336 public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob) : this(pSizeX, pSizeY, pSizeZ)
337 {
338 switch ((DBTerrainRevision)pFormatCode)
339 {
340 case DBTerrainRevision.Compressed2D:
341 FromCompressedTerrainSerialization(pBlob);
342 m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
343 break;
344 default:
345 FromLegacyTerrainSerialization(pBlob);
346 m_log.DebugFormat("{0} HeightmapTerrainData create from legacy serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
347 break;
348 }
349 }
350
351 // Just create an array of doubles. Presumes the caller implicitly knows the size.
352 public Array ToLegacyTerrainSerialization()
353 {
354 Array ret = null;
355
356 using (MemoryStream str = new MemoryStream((int)Constants.RegionSize * (int)Constants.RegionSize * sizeof(double)))
357 {
358 using (BinaryWriter bw = new BinaryWriter(str))
359 {
360 for (int xx = 0; xx < Constants.RegionSize; xx++)
361 {
362 for (int yy = 0; yy < Constants.RegionSize; yy++)
363 {
364 double height = this[xx, yy];
365 if (height == 0.0)
366 height = double.Epsilon;
367 bw.Write(height);
368 }
369 }
370 }
371 ret = str.ToArray();
372 }
373 return ret;
374 }
375
376 // Just create an array of doubles. Presumes the caller implicitly knows the size.
377 public void FromLegacyTerrainSerialization(byte[] pBlob)
378 {
379 // In case database info doesn't match real terrain size, initialize the whole terrain.
380 ClearLand();
381
382 using (MemoryStream mstr = new MemoryStream(pBlob))
383 {
384 using (BinaryReader br = new BinaryReader(mstr))
385 {
386 for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
387 {
388 for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
389 {
390 float val = (float)br.ReadDouble();
391 if (xx < SizeX && yy < SizeY)
392 m_heightmap[xx, yy] = ToCompressedHeight(val);
393 }
394 }
395 }
396 ClearTaint();
397 }
398 }
399
400 // See the reader below.
401 public Array ToCompressedTerrainSerialization()
402 {
403 Array ret = null;
404 using (MemoryStream str = new MemoryStream((3 * sizeof(Int32)) + (SizeX * SizeY * sizeof(Int16))))
405 {
406 using (BinaryWriter bw = new BinaryWriter(str))
407 {
408 bw.Write((Int32)DBTerrainRevision.Compressed2D);
409 bw.Write((Int32)SizeX);
410 bw.Write((Int32)SizeY);
411 bw.Write((Int32)CompressionFactor);
412 for (int yy = 0; yy < SizeY; yy++)
413 for (int xx = 0; xx < SizeX; xx++)
414 {
415 bw.Write((Int16)m_heightmap[xx, yy]);
416 }
417 }
418 ret = str.ToArray();
419 }
420 return ret;
421 }
422
423 // Initialize heightmap from blob consisting of:
424 // int32, int32, int32, int32, int16[]
425 // where the first int32 is format code, next two int32s are the X and y of heightmap data and
426 // the forth int is the compression factor for the following int16s
427 // This is just sets heightmap info. The actual size of the region was set on this instance's
428 // creation and any heights not initialized by theis blob are set to the default height.
429 public void FromCompressedTerrainSerialization(byte[] pBlob)
430 {
431 Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
432
433 using (MemoryStream mstr = new MemoryStream(pBlob))
434 {
435 using (BinaryReader br = new BinaryReader(mstr))
436 {
437 hmFormatCode = br.ReadInt32();
438 hmSizeX = br.ReadInt32();
439 hmSizeY = br.ReadInt32();
440 hmCompressionFactor = br.ReadInt32();
441
442 m_compressionFactor = hmCompressionFactor;
443
444 // In case database info doesn't match real terrain size, initialize the whole terrain.
445 ClearLand();
446
447 for (int yy = 0; yy < hmSizeY; yy++)
448 {
449 for (int xx = 0; xx < hmSizeX; xx++)
450 {
451 Int16 val = br.ReadInt16();
452 if (xx < SizeX && yy < SizeY)
453 m_heightmap[xx, yy] = val;
454 }
455 }
456 }
457 ClearTaint();
458
459 m_log.InfoFormat("{0} Read compressed 2d heightmap. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
460 LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
461 }
462 }
463 }
464}