aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/TerrainData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/TerrainData.cs')
-rw-r--r--OpenSim/Framework/TerrainData.cs243
1 files changed, 243 insertions, 0 deletions
diff --git a/OpenSim/Framework/TerrainData.cs b/OpenSim/Framework/TerrainData.cs
new file mode 100644
index 0000000..bee6814
--- /dev/null
+++ b/OpenSim/Framework/TerrainData.cs
@@ -0,0 +1,243 @@
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;
31
32using OpenMetaverse;
33
34namespace OpenSim.Framework
35{
36 public abstract class TerrainData
37 {
38 // Terrain always is a square
39 public int SizeX { get; protected set; }
40 public int SizeY { get; protected set; }
41 public int SizeZ { get; protected set; }
42
43 public abstract float this[int x, int y] { get; set; }
44 // Someday terrain will have caves
45 public abstract float this[int x, int y, int z] { get; set; }
46
47 public bool IsTainted { get; protected set; }
48 public abstract bool IsTaintedAt(int xx, int yy);
49 public abstract void ClearTaint();
50
51 // Return a representation of this terrain for storing as a blob in the database.
52 // Returns 'true' to say blob was stored in the 'out' locations.
53 public abstract bool GetDatabaseBlob(out int DBFormatRevisionCode, out Array blob);
54
55 // return a special compressed representation of the heightmap in shorts
56 public abstract short[] GetCompressedMap();
57 public abstract void SetCompressedMap(short[] cmap);
58
59 public abstract TerrainData Clone();
60 }
61
62 // The terrain is stored as a blob in the database with a 'revision' field.
63 // Some implementations of terrain storage would fill the revision field with
64 // the time the terrain was stored. When real revisions were added and this
65 // feature removed, that left some old entries with the time in the revision
66 // field.
67 // Thus, if revision is greater than 'RevisionHigh' then terrain db entry is
68 // left over and it is presumed to be 'Legacy256'.
69 // Numbers are arbitrary and are chosen to to reduce possible mis-interpretation.
70 // If a revision does not match any of these, it is assumed to be Legacy256.
71 public enum DBTerrainRevision
72 {
73 // Terrain is 'double[256,256]'
74 Legacy256 = 11,
75 // Terrain is 'int32, int32, float[,]' where the shorts are X and Y dimensions
76 // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
77 Variable2D = 22,
78 // A revision that is not listed above or any revision greater than this value is 'Legacy256'.
79 RevisionHigh = 1234
80 }
81
82 // Version of terrain that is a heightmap.
83 // This should really be 'LLOptimizedHeightmapTerrainData' as it includes knowledge
84 // of 'patches' which are 16x16 terrain areas which can be sent separately to the viewer.
85 // The heighmap is kept as an array of short integers. The integer values are converted to
86 // and from floats by TerrainCompressionFactor.
87 public class HeightmapTerrainData : TerrainData
88 {
89 // TerrainData.this[x, y]
90 public override float this[int x, int y]
91 {
92 get { return FromCompressedHeight(m_heightmap[x, y]); }
93 set {
94 short newVal = ToCompressedHeight(value);
95 if (m_heightmap[x, y] != newVal)
96 {
97 m_heightmap[x, y] = newVal;
98 m_taint[x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize] = true;
99
100 }
101 }
102 }
103
104 // TerrainData.this[x, y, z]
105 public override float this[int x, int y, int z]
106 {
107 get { return this[x, y]; }
108 set { this[x, y] = value; }
109 }
110
111 // TerrainData.ClearTaint
112 public override void ClearTaint()
113 {
114 IsTainted = false;
115 for (int ii = 0; ii < m_taint.GetLength(0); ii++)
116 for (int jj = 0; jj < m_taint.GetLength(1); jj++)
117 m_taint[ii, jj] = false;
118 }
119
120 public override bool IsTaintedAt(int xx, int yy)
121 {
122 return m_taint[xx / Constants.TerrainPatchSize, yy / Constants.TerrainPatchSize];
123 }
124
125 // TerrainData.GetDatabaseBlob
126 // The user wants something to store in the database.
127 public override bool GetDatabaseBlob(out int DBRevisionCode, out Array blob)
128 {
129 DBRevisionCode = (int)DBTerrainRevision.Legacy256;
130 blob = LegacyTerrainSerialization();
131 return false;
132 }
133
134 public override short[] GetCompressedMap()
135 {
136 short[] newMap = new short[SizeX * SizeY];
137
138 int ind = 0;
139 for (int xx = 0; xx < SizeX; xx++)
140 for (int yy = 0; yy < SizeY; yy++)
141 newMap[ind++] = m_heightmap[xx, yy];
142
143 return newMap;
144
145 }
146 public override void SetCompressedMap(short[] cmap)
147 {
148 int ind = 0;
149 for (int xx = 0; xx < SizeX; xx++)
150 for (int yy = 0; yy < SizeY; yy++)
151 m_heightmap[xx, yy] = cmap[ind++];
152 }
153
154 // TerrainData.Clone
155 public override TerrainData Clone()
156 {
157 HeightmapTerrainData ret = new HeightmapTerrainData(SizeX, SizeY, SizeZ);
158 ret.m_heightmap = (short[,])this.m_heightmap.Clone();
159 return ret;
160 }
161
162 // =============================================================
163
164 private short[,] m_heightmap;
165 // Remember subregions of the heightmap that has changed.
166 private bool[,] m_taint;
167
168 // To save space (especially for large regions), keep the height as a short integer
169 // that is coded as the float height times the compression factor (usually '100'
170 // to make for two decimal points).
171 public static short ToCompressedHeight(double pHeight)
172 {
173 return (short)(pHeight * Constants.TerrainCompression);
174 }
175
176 public static float FromCompressedHeight(short pHeight)
177 {
178 return ((float)pHeight) / Constants.TerrainCompression;
179 }
180
181 // To keep with the legacy theme, this can be created with the way terrain
182 // used to passed around as.
183 public HeightmapTerrainData(double[,] pTerrain)
184 {
185 SizeX = pTerrain.GetLength(0);
186 SizeY = pTerrain.GetLength(1);
187 SizeZ = (int)Constants.RegionHeight;
188
189 m_heightmap = new short[SizeX, SizeY];
190 for (int ii = 0; ii < SizeX; ii++)
191 {
192 for (int jj = 0; jj < SizeY; jj++)
193 {
194 m_heightmap[ii, jj] = ToCompressedHeight(pTerrain[ii, jj]);
195
196 }
197 }
198
199 m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
200 ClearTaint();
201 }
202
203 // Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
204 public HeightmapTerrainData(int pX, int pY, int pZ)
205 {
206 SizeX = pX;
207 SizeY = pY;
208 SizeZ = pZ;
209 m_heightmap = new short[SizeX, SizeY];
210 m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
211 ClearTaint();
212 }
213
214 public HeightmapTerrainData(short[] cmap, int pX, int pY, int pZ) : this(pX, pY, pZ)
215 {
216 SetCompressedMap(cmap);
217 }
218
219
220 // Just create an array of doubles. Presumes the caller implicitly knows the size.
221 public Array LegacyTerrainSerialization()
222 {
223 Array ret = null;
224 using (MemoryStream str = new MemoryStream(SizeX * SizeY * sizeof(double)))
225 {
226 using (BinaryWriter bw = new BinaryWriter(str))
227 {
228 // TODO: COMPATIBILITY - Add byte-order conversions
229 for (int ii = 0; ii < SizeX; ii++)
230 for (int jj = 0; jj < SizeY; jj++)
231 {
232 double height = this[ii, jj];
233 if (height == 0.0)
234 height = double.Epsilon;
235 bw.Write(height);
236 }
237 }
238 ret = str.ToArray();
239 }
240 return ret;
241 }
242 }
243}