private void hfzWriteTile(ref EndianBinaryWriter writer, HfzHeader fh, HfzTile tile) { List<float> pTileData = tile.tileData; Int32 i=0, TempInt, FirstVal; float f, HFmin, HFmax, VertScale, VertOffset; sbyte c; short s; UInt32 nx = fh.nx; UInt32 ny = fh.ny; UInt32 TileSize = fh.TileSize; float Precis = fh.Precis; // get min/max alt in block (used for vert scale) HFmin = 0; HFmax = 0; Epsilon epsilon = new Epsilon(1E-3); for (i = 0; i < pTileData.Count; i++) { // find max diff in line f = pTileData.ElementAt(i); if (i == 0) { HFmin = HFmax = f; } else { if (RealExtensions.LT(f, HFmin, epsilon)) { HFmin = f; } if (RealExtensions.GT(f, HFmax, epsilon)) { HFmax = f; } } } // number of int levels required for this block float BlockLevels = ((HFmax - HFmin) / Precis) + 1; // calc scale VertScale = (HFmax - HFmin) / BlockLevels; VertOffset = HFmin; if (RealExtensions.LE(VertScale, 0, epsilon)) { VertScale = 1.0f; // this is for niceness } writer.Write(VertScale); writer.Write(VertOffset); // determine number of blocks Int32 nBlocksX = ((int)fh.nx / fh.TileSize) - 1; // -1 due to index starting at 0 Int32 exBlocksX = -1; Int32 nBlocksY = ((int)fh.ny / fh.TileSize) - 1; // -1 due to index starting at 0 Int32 exBlocksY = -1; if (fh.nx % fh.TileSize > 0) { exBlocksX = nBlocksX + 1; } if (fh.ny % fh.TileSize > 0) { exBlocksY = nBlocksY + 1; } UInt32 rows = TileSize; if(tile.x != exBlocksX && tile.y == exBlocksY || tile.x == exBlocksX && tile.y == exBlocksY) { rows = (uint)(TileSize - (((exBlocksY+1) * TileSize) - ny)); } else if (tile.x == exBlocksX && tile.y != exBlocksY) { rows = calculateRows(tile, ny, TileSize, rows); } int rowCount = 0; int rowZeroElement = 0; try { for (rowCount = 0; rowCount < rows; rowCount++) { List<float> row = null; int range = calculateRange(tile, nx, TileSize); rowZeroElement = (int)(rowCount * range); row = pTileData.GetRange(rowZeroElement, range); f = row.ElementAt(0); FirstVal = Convert.ToInt32(Math.Truncate((f - VertOffset) / VertScale)); Int32 LastVal = FirstVal; // find max diff in line Int32 Diff; Int32 MaxDev = 0; List<Int32> pDiffBuf = new List<Int32>(); for (int rowElementCounter = 1; rowElementCounter < row.Count; rowElementCounter++) { // find max diff in line f = row.ElementAt(rowElementCounter); TempInt = Convert.ToInt32(Math.Truncate((f - VertOffset) / VertScale)); Diff = TempInt - LastVal; pDiffBuf.Add(Diff); LastVal = TempInt; MaxDev = MaxDev > Math.Abs(Diff) ? MaxDev : Math.Abs(Diff); } // should we use 8, 16 or 32 bit pixels? byte LineDepth = 4; if (MaxDev <= 127) { LineDepth = 1; } else if (MaxDev <= 32767) { LineDepth = 2; } writer.Write(LineDepth); writer.Write(FirstVal); for (int rowElementCounter = 0; rowElementCounter < pDiffBuf.Count; rowElementCounter++) { Diff = pDiffBuf.ElementAt(rowElementCounter); switch (LineDepth) { case 1: c = Convert.ToSByte(Diff); writer.Write(c); break; case 2: s = Convert.ToInt16(Diff); writer.Write(s); break; case 4: writer.Write(Diff); break; } } } } catch (Exception ex) { System.Console.WriteLine("Tile x :" + tile.x); System.Console.WriteLine("Tile y :" + tile.y); System.Console.WriteLine("Rows :" + rows); System.Console.WriteLine("RowCount :" + rowCount); System.Console.WriteLine("RowZeroElement :" + rowZeroElement); System.Console.WriteLine("exBlocksX :" + exBlocksX); System.Console.WriteLine("exBlocksY :" + exBlocksY); System.Console.WriteLine("pTileData count :" + pTileData.Count); throw ex; } }
private int calculateRange(HfzTile tile, UInt32 nx, UInt32 TileSize) { int range = (int)(nx - (TileSize * tile.x)); if (range > TileSize) { range = (int)TileSize; } debugLine("range: " + range); return range; }
private uint calculateRows(HfzTile tile, UInt32 ny, UInt32 TileSize, UInt32 rows) { rows = ny - (TileSize * tile.y); if (rows > TileSize) { rows = TileSize; } debugLine("rows: " + rows); return rows; }
public void addTile(short tileNo, HfzTile tile) { mapData.Add(tileNo, tile); }
public void HfzTileDataNotNull() { HfzTile tile = new HfzTile(0, 0, 0, 0, null); }
public void HfzTileDataNotEmpty() { HfzTile tile = new HfzTile(0, 0, 0, 0, new List<float>()); }
public void HfzTileCreation() { List<float> data = new List<float>(); data.Add(1.0f); HfzTile tile = new HfzTile(0, 0, 0, 0, data); Assert.NotNull(tile); }