TerrainData.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using log4net;
  33. namespace OpenSim.Framework
  34. {
  35. public abstract class TerrainData
  36. {
  37. // Terrain always is a square
  38. public int SizeX { get; protected set; }
  39. public int SizeY { get; protected set; }
  40. public int SizeZ { get; protected set; }
  41. // A height used when the user doesn't specify anything
  42. public const float DefaultTerrainHeight = 21f;
  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. public bool IsTainted { get; protected set; }
  47. public abstract bool IsTaintedAt(int xx, int yy);
  48. public abstract void ClearTaint();
  49. public abstract void ClearLand();
  50. public abstract void ClearLand(float height);
  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. // Given a revision code and a blob from the database, create and return the right type of TerrainData.
  55. // The sizes passed are the expected size of the region. The database info will be used to
  56. // initialize the heightmap of that sized region with as much data is in the blob.
  57. // Return created TerrainData or 'null' if unsuccessful.
  58. public static TerrainData CreateFromDatabaseBlobFactory(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
  59. {
  60. // For the moment, there is only one implementation class
  61. return new HeightmapTerrainData(pSizeX, pSizeY, pSizeZ, pFormatCode, pBlob);
  62. }
  63. // return a special compressed representation of the heightmap in shorts
  64. public abstract short[] GetCompressedMap();
  65. public abstract float CompressionFactor { get; }
  66. public abstract double[,] GetDoubles();
  67. public abstract TerrainData Clone();
  68. }
  69. // The terrain is stored in the database as a blob with a 'revision' field.
  70. // Some implementations of terrain storage would fill the revision field with
  71. // the time the terrain was stored. When real revisions were added and this
  72. // feature removed, that left some old entries with the time in the revision
  73. // field.
  74. // Thus, if revision is greater than 'RevisionHigh' then terrain db entry is
  75. // left over and it is presumed to be 'Legacy256'.
  76. // Numbers are arbitrary and are chosen to to reduce possible mis-interpretation.
  77. // If a revision does not match any of these, it is assumed to be Legacy256.
  78. public enum DBTerrainRevision
  79. {
  80. // Terrain is 'double[256,256]'
  81. Legacy256 = 11,
  82. // Terrain is 'int32, int32, float[,]' where the ints are X and Y dimensions
  83. // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
  84. Variable2D = 22,
  85. // Terrain is 'int32, int32, int32, int16[]' where the ints are X and Y dimensions
  86. // and third int is the 'compression factor'. The heights are compressed as
  87. // "short compressedHeight = (short)(height * compressionFactor);"
  88. // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
  89. Compressed2D = 27,
  90. // A revision that is not listed above or any revision greater than this value is 'Legacy256'.
  91. RevisionHigh = 1234
  92. }
  93. // Version of terrain that is a heightmap.
  94. // This should really be 'LLOptimizedHeightmapTerrainData' as it includes knowledge
  95. // of 'patches' which are 16x16 terrain areas which can be sent separately to the viewer.
  96. // The heighmap is kept as an array of short integers. The integer values are converted to
  97. // and from floats by TerrainCompressionFactor. Shorts are used to limit storage used.
  98. public class HeightmapTerrainData : TerrainData
  99. {
  100. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  101. private static string LogHeader = "[HEIGHTMAP TERRAIN DATA]";
  102. // TerrainData.this[x, y]
  103. public override float this[int x, int y]
  104. {
  105. get { return FromCompressedHeight(m_heightmap[x, y]); }
  106. set {
  107. short newVal = ToCompressedHeight(value);
  108. if (m_heightmap[x, y] != newVal)
  109. {
  110. m_heightmap[x, y] = newVal;
  111. m_taint[x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize] = true;
  112. }
  113. }
  114. }
  115. // TerrainData.this[x, y, z]
  116. public override float this[int x, int y, int z]
  117. {
  118. get { return this[x, y]; }
  119. set { this[x, y] = value; }
  120. }
  121. // TerrainData.ClearTaint
  122. public override void ClearTaint()
  123. {
  124. IsTainted = false;
  125. for (int ii = 0; ii < m_taint.GetLength(0); ii++)
  126. for (int jj = 0; jj < m_taint.GetLength(1); jj++)
  127. m_taint[ii, jj] = false;
  128. }
  129. // TerrainData.ClearLand
  130. public override void ClearLand()
  131. {
  132. ClearLand(DefaultTerrainHeight);
  133. }
  134. // TerrainData.ClearLand(float)
  135. public override void ClearLand(float pHeight)
  136. {
  137. short flatHeight = ToCompressedHeight(pHeight);
  138. for (int xx = 0; xx < SizeX; xx++)
  139. for (int yy = 0; yy < SizeY; yy++)
  140. m_heightmap[xx, yy] = flatHeight;
  141. }
  142. public override bool IsTaintedAt(int xx, int yy)
  143. {
  144. return m_taint[xx / Constants.TerrainPatchSize, yy / Constants.TerrainPatchSize];
  145. }
  146. // TerrainData.GetDatabaseBlob
  147. // The user wants something to store in the database.
  148. public override bool GetDatabaseBlob(out int DBRevisionCode, out Array blob)
  149. {
  150. bool ret = false;
  151. if (SizeX == Constants.RegionSize && SizeY == Constants.RegionSize)
  152. {
  153. DBRevisionCode = (int)DBTerrainRevision.Legacy256;
  154. blob = ToLegacyTerrainSerialization();
  155. ret = true;
  156. }
  157. else
  158. {
  159. DBRevisionCode = (int)DBTerrainRevision.Compressed2D;
  160. blob = ToCompressedTerrainSerialization();
  161. ret = true;
  162. }
  163. return ret;
  164. }
  165. // TerrainData.CompressionFactor
  166. private float m_compressionFactor = 100.0f;
  167. public override float CompressionFactor { get { return m_compressionFactor; } }
  168. // TerrainData.GetCompressedMap
  169. public override short[] GetCompressedMap()
  170. {
  171. short[] newMap = new short[SizeX * SizeY];
  172. int ind = 0;
  173. for (int xx = 0; xx < SizeX; xx++)
  174. for (int yy = 0; yy < SizeY; yy++)
  175. newMap[ind++] = m_heightmap[xx, yy];
  176. return newMap;
  177. }
  178. // TerrainData.Clone
  179. public override TerrainData Clone()
  180. {
  181. HeightmapTerrainData ret = new HeightmapTerrainData(SizeX, SizeY, SizeZ);
  182. ret.m_heightmap = (short[,])this.m_heightmap.Clone();
  183. return ret;
  184. }
  185. // TerrainData.GetDoubles
  186. public override double[,] GetDoubles()
  187. {
  188. double[,] ret = new double[SizeX, SizeY];
  189. for (int xx = 0; xx < SizeX; xx++)
  190. for (int yy = 0; yy < SizeY; yy++)
  191. ret[xx, yy] = FromCompressedHeight(m_heightmap[xx, yy]);
  192. return ret;
  193. }
  194. // =============================================================
  195. private short[,] m_heightmap;
  196. // Remember subregions of the heightmap that has changed.
  197. private bool[,] m_taint;
  198. // To save space (especially for large regions), keep the height as a short integer
  199. // that is coded as the float height times the compression factor (usually '100'
  200. // to make for two decimal points).
  201. public short ToCompressedHeight(double pHeight)
  202. {
  203. return (short)(pHeight * CompressionFactor);
  204. }
  205. public float FromCompressedHeight(short pHeight)
  206. {
  207. return ((float)pHeight) / CompressionFactor;
  208. }
  209. // To keep with the legacy theme, create an instance of this class based on the
  210. // way terrain used to be passed around.
  211. public HeightmapTerrainData(double[,] pTerrain)
  212. {
  213. SizeX = pTerrain.GetLength(0);
  214. SizeY = pTerrain.GetLength(1);
  215. SizeZ = (int)Constants.RegionHeight;
  216. m_compressionFactor = 100.0f;
  217. m_heightmap = new short[SizeX, SizeY];
  218. for (int ii = 0; ii < SizeX; ii++)
  219. {
  220. for (int jj = 0; jj < SizeY; jj++)
  221. {
  222. m_heightmap[ii, jj] = ToCompressedHeight(pTerrain[ii, jj]);
  223. }
  224. }
  225. // m_log.DebugFormat("{0} new by doubles. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  226. m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
  227. ClearTaint();
  228. }
  229. // Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
  230. public HeightmapTerrainData(int pX, int pY, int pZ)
  231. {
  232. SizeX = pX;
  233. SizeY = pY;
  234. SizeZ = pZ;
  235. m_compressionFactor = 100.0f;
  236. m_heightmap = new short[SizeX, SizeY];
  237. m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
  238. // m_log.DebugFormat("{0} new by dimensions. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  239. ClearTaint();
  240. }
  241. public HeightmapTerrainData(short[] cmap, float pCompressionFactor, int pX, int pY, int pZ) : this(pX, pY, pZ)
  242. {
  243. m_compressionFactor = pCompressionFactor;
  244. int ind = 0;
  245. for (int xx = 0; xx < SizeX; xx++)
  246. for (int yy = 0; yy < SizeY; yy++)
  247. m_heightmap[xx, yy] = cmap[ind++];
  248. // m_log.DebugFormat("{0} new by compressed map. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  249. }
  250. // Create a heighmap from a database blob
  251. public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob) : this(pSizeX, pSizeY, pSizeZ)
  252. {
  253. switch ((DBTerrainRevision)pFormatCode)
  254. {
  255. case DBTerrainRevision.Compressed2D:
  256. FromCompressedTerrainSerialization(pBlob);
  257. m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  258. break;
  259. default:
  260. FromLegacyTerrainSerialization(pBlob);
  261. m_log.DebugFormat("{0} HeightmapTerrainData create from legacy serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  262. break;
  263. }
  264. }
  265. // Just create an array of doubles. Presumes the caller implicitly knows the size.
  266. public Array ToLegacyTerrainSerialization()
  267. {
  268. Array ret = null;
  269. using (MemoryStream str = new MemoryStream((int)Constants.RegionSize * (int)Constants.RegionSize * sizeof(double)))
  270. {
  271. using (BinaryWriter bw = new BinaryWriter(str))
  272. {
  273. for (int xx = 0; xx < Constants.RegionSize; xx++)
  274. {
  275. for (int yy = 0; yy < Constants.RegionSize; yy++)
  276. {
  277. double height = this[xx, yy];
  278. if (height == 0.0)
  279. height = double.Epsilon;
  280. bw.Write(height);
  281. }
  282. }
  283. }
  284. ret = str.ToArray();
  285. }
  286. return ret;
  287. }
  288. // Just create an array of doubles. Presumes the caller implicitly knows the size.
  289. public void FromLegacyTerrainSerialization(byte[] pBlob)
  290. {
  291. // In case database info doesn't match real terrain size, initialize the whole terrain.
  292. ClearLand();
  293. using (MemoryStream mstr = new MemoryStream(pBlob))
  294. {
  295. using (BinaryReader br = new BinaryReader(mstr))
  296. {
  297. for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
  298. {
  299. for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
  300. {
  301. float val = (float)br.ReadDouble();
  302. if (xx < SizeX && yy < SizeY)
  303. m_heightmap[xx, yy] = ToCompressedHeight(val);
  304. }
  305. }
  306. }
  307. ClearTaint();
  308. }
  309. }
  310. // See the reader below.
  311. public Array ToCompressedTerrainSerialization()
  312. {
  313. Array ret = null;
  314. using (MemoryStream str = new MemoryStream((3 * sizeof(Int32)) + (SizeX * SizeY * sizeof(Int16))))
  315. {
  316. using (BinaryWriter bw = new BinaryWriter(str))
  317. {
  318. bw.Write((Int32)DBTerrainRevision.Compressed2D);
  319. bw.Write((Int32)SizeX);
  320. bw.Write((Int32)SizeY);
  321. bw.Write((Int32)CompressionFactor);
  322. for (int yy = 0; yy < SizeY; yy++)
  323. for (int xx = 0; xx < SizeX; xx++)
  324. {
  325. bw.Write((Int16)m_heightmap[xx, yy]);
  326. }
  327. }
  328. ret = str.ToArray();
  329. }
  330. return ret;
  331. }
  332. // Initialize heightmap from blob consisting of:
  333. // int32, int32, int32, int32, int16[]
  334. // where the first int32 is format code, next two int32s are the X and y of heightmap data and
  335. // the forth int is the compression factor for the following int16s
  336. // This is just sets heightmap info. The actual size of the region was set on this instance's
  337. // creation and any heights not initialized by theis blob are set to the default height.
  338. public void FromCompressedTerrainSerialization(byte[] pBlob)
  339. {
  340. Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
  341. using (MemoryStream mstr = new MemoryStream(pBlob))
  342. {
  343. using (BinaryReader br = new BinaryReader(mstr))
  344. {
  345. hmFormatCode = br.ReadInt32();
  346. hmSizeX = br.ReadInt32();
  347. hmSizeY = br.ReadInt32();
  348. hmCompressionFactor = br.ReadInt32();
  349. m_compressionFactor = hmCompressionFactor;
  350. // In case database info doesn't match real terrain size, initialize the whole terrain.
  351. ClearLand();
  352. for (int yy = 0; yy < hmSizeY; yy++)
  353. {
  354. for (int xx = 0; xx < hmSizeX; xx++)
  355. {
  356. Int16 val = br.ReadInt16();
  357. if (xx < SizeX && yy < SizeY)
  358. m_heightmap[xx, yy] = val;
  359. }
  360. }
  361. }
  362. ClearTaint();
  363. m_log.InfoFormat("{0} Read compressed 2d heightmap. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
  364. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
  365. }
  366. }
  367. }
  368. }