protected override async Task <ChunkHolder <float> > GenerateData(StandardChunkMetadata template, TraceListener log) { var ret = new ChunkHolder <float>( template.LatSteps, template.LonSteps, template.LatLo, template.LonLo, template.LatHi, template.LonHi, null, toDouble, fromDouble); int latMin = (int)(Math.Min(template.LatLo.DecimalDegree, template.LatHi.DecimalDegree) + 1.0e-5); int latMax = (int)(Math.Max(template.LatLo.DecimalDegree, template.LatHi.DecimalDegree) - 1.0e-5); int lonMin = (int)(Math.Min(template.LonLo.DecimalDegree, template.LonHi.DecimalDegree) + 1.0e-5); int lonMax = (int)(Math.Max(template.LonLo.DecimalDegree, template.LonHi.DecimalDegree) - 1.0e-5); var chunks = new List <ChunkHolder <float> >(); for (int latInt = latMin; latInt <= latMax; latInt++) { for (int lonInt = lonMin; lonInt <= lonMax; lonInt++) { chunks.Add(await UsgsRawChunks.GetRawHeightsInMeters(latInt, lonInt, log)); } } ret.RenderChunksInto(chunks, aggregate, log); return(ret); }
public ChunkStore SpawnChunk(Vector3 pos, ChunkHolder holder) { var prefab = GetPrefab(holder.Name); var instance = Instantiate(prefab, pos, Quaternion.identity) as GameObject; return(new ChunkStore(holder, instance)); }
public ChunkHolder GetHolder(ChunkHolder current, ChunkOffset dir) { _index++; if (_index > AllHolders.Count - 1) { _index = 0; } return(AllHolders[_index]); }
void Awake() { _manager = CreateManager(); _source = GetSource(); CurrentPos = Root.position; CurrentChunk = null; _storage = new ChunkStorage(); _storage.AddChunk(new ChunkOffset(0, 0), null); }
public async Task <ChunkHolder <T> > ProcessRawData(StandardChunkMetadata template, TraceListener log) { var computedChunk = await GetComputedChunk(template, log); string fileName = computedChunk.Item1; ChunkHolder <T> ret = computedChunk.Item2; if (computedChunk.Item2 != null) { log?.WriteLine("Cached " + description + " chunk file exists: " + fileName); return(computedChunk.Item2); } log?.WriteLine("Cached " + description + " chunk file does not exist: " + fileName); if (template.ZoomLevel > this.SourceDataZoom) { // Nothing to do for processing return(null); } else if (template.ZoomLevel == this.SourceDataZoom) { log?.WriteLine("Starting generation..."); ret = await GenerateData(template, log); await WriteChunk(ret, fileName, log); log?.WriteLine("Finished generation of " + description + " cached chunk file: " + fileName); return(ret); } log?.WriteLine("Need to aggregate up from higher zoom data"); var children = template.GetChildChunks(); List <ChunkHolder <T> > chunks = new List <ChunkHolder <T> >(); foreach (var child in children) { log?.WriteLine(child); chunks.Add(await ProcessRawData(child, log)); } ret = new ChunkHolder <T>( template.LatSteps, template.LonSteps, template.LatLo, template.LonLo, template.LatHi, template.LonHi, null, toDouble, fromDouble); ret.RenderChunksInto(chunks, aggregate, log); await WriteChunk(ret, fileName, log); log?.WriteLine("Finished generation of " + description + " cached chunk file: " + fileName); return(ret); }
public ChunkStore SpawnChunk(Vector3 pos, ChunkHolder holder) { var prefab = Resources.Load(holder.Name) as GameObject; if (prefab) { var instance = GameObject.Instantiate(prefab, pos, Quaternion.identity) as GameObject; return(new ChunkStore(holder, instance)); } return(null); }
public void GetChunks() { Chunk[] chunks = FindObjectsOfType <Chunk>(); chunkHolder = new ChunkHolder[chunks.Length]; int counter = 0; foreach (Chunk chunk in chunks) { chunkHolder[counter] = new ChunkHolder(chunk, chunk.coord, size); counter++; } }
private async Task WriteChunk(ChunkHolder <T> ret, string fileName, TraceListener log) { using (MemoryStream stream = new MemoryStream()) { stream.Write(BitConverter.GetBytes(ret.LatSteps), 0, 4); stream.Write(BitConverter.GetBytes(ret.LonSteps), 0, 4); for (int i = 0; i < ret.LatSteps; i++) { for (int j = 0; j < ret.LonSteps; j++) { WritePixel(stream, ret.Data[i][j]); } } stream.Position = 0; await BlobHelper.WriteStream(cachedFileContainer, fileName, stream, log); } }
public async Task <FriendlyMesh> GetData(StandardChunkMetadata template, TraceListener log) { var computedChunk = await GetComputedChunk(template, log); string fileName = computedChunk.Item1; FriendlyMesh ret = computedChunk.Item2; if (computedChunk.Item2 != null) { log?.WriteLine("Cached " + description + " chunk (" + template.ToString() + ") file exists: " + fileName); return(computedChunk.Item2); } log?.WriteLine("Cached " + description + " chunk (" + template.ToString() + ") file does not exist: " + fileName + ", so starting generation..."); ChunkHolder <float> pixels2 = null; try { pixels2 = await Heights.Current.GetData(template, log); } catch { } if (pixels2 == null) { // throw new InvalidOperationException("Source heights not found for chunk " + template.ToString()); return(null); } ret = new FriendlyMesh( template.LatSteps, template.LonSteps, template.LatLo, template.LonLo, template.LatHi, template.LonHi, pixels2.Data, log); await WriteChunk(ret, fileName, log); log?.WriteLine("Finished generation of " + description + " cached chunk (" + template.ToString() + ") file: " + fileName); return(ret); }
public async Task <ChunkHolder <T> > GetData(StandardChunkMetadata template, TraceListener log) { var computedChunk = await GetComputedChunk(template, log); string fileName = computedChunk.Item1; ChunkHolder <T> ret = computedChunk.Item2; if (computedChunk.Item2 != null) { log?.WriteLine("Cached " + description + " chunk file exists: " + fileName); return(computedChunk.Item2); } log?.WriteLine("Cached " + description + " chunk file does not exist: " + fileName); if (template.ZoomLevel <= this.SourceDataZoom) { throw new MountainViewException("Source data is missing for chunk " + template.ToString()); //log?.WriteLine("Starting generation..."); //ret = await GenerateData(template, log); //await WriteChunk(ret, fileName, log); //log?.WriteLine("Finished generation of " + description + " cached chunk file: " + fileName); //return ret; } log?.WriteLine("Need to interpolate from lower zoom data"); var parent = template.GetParentChunk(); var chunks = new ChunkHolder <T>[] { await GetData(parent, log) }; ret = new ChunkHolder <T>( template.LatSteps, template.LonSteps, template.LatLo, template.LonLo, template.LatHi, template.LonHi, null, toDouble, fromDouble); ret.RenderChunksInto(chunks, aggregate, log); await WriteChunk(ret, fileName, log); log?.WriteLine("Finished generation of " + description + " cached chunk file: " + fileName); return(ret); }
protected override async Task <ChunkHolder <MyColor> > GenerateData(StandardChunkMetadata template, TraceListener log) { var ret = new ChunkHolder <MyColor>( template.LatSteps, template.LonSteps, template.LatLo, template.LonLo, template.LatHi, template.LonHi, null, toDouble, fromDouble); var targetChunks = (await UsgsRawImageChunks.GetChunkMetadata(log)) .Select(p => new { p, Chunk = new ChunkMetadata(0, 0, Angle.FromDecimalDegrees(p.Points.Min(q => q.Item1)), Angle.FromDecimalDegrees(p.Points.Min(q => q.Item2)), Angle.FromDecimalDegrees(p.Points.Max(q => q.Item1)), Angle.FromDecimalDegrees(p.Points.Max(q => q.Item2))) }) .Where(p => !ret.Disjoint(p.Chunk)) .ToArray(); var chunks = new List <ChunkHolder <MyColor> >(); foreach (var tmp in targetChunks) { log?.WriteLine(tmp.Chunk); var col = await UsgsRawImageChunks.GetRawColors( Angle.Add(tmp.Chunk.LatLo, Angle.Divide(tmp.Chunk.LatDelta, 2)), Angle.Add(tmp.Chunk.LonLo, Angle.Divide(tmp.Chunk.LonDelta, 2)), log); if (col != null) { chunks.Add(col); } } ret.RenderChunksInto(chunks, aggregate, log); return(ret); }
public static void WriteImageFile <T>( ChunkHolder <T> colorBuff, string fileName, Func <T, MyColor> transform, OutputType outputType) { using (DirectBitmap bm = new DirectBitmap(colorBuff.LonSteps, colorBuff.LatSteps)) { for (int i = 0; i < colorBuff.LatSteps; i++) { var col = colorBuff.Data[i]; for (int j = 0; j < colorBuff.LonSteps; j++) { bm.SetPixel(j, i, transform(col[colorBuff.LonSteps - 1 - j])); } } File.Delete(fileName); using (FileStream stream = File.OpenWrite(fileName)) { bm.WriteFile(outputType, stream); } } }
public static MemoryStream GetBitmap <T>( ChunkHolder <T> colorBuff, Func <T, MyColor> transform, OutputType outputType) { using (DirectBitmap bm = new DirectBitmap(colorBuff.LonSteps, colorBuff.LatSteps)) { for (int i = 0; i < colorBuff.LatSteps; i++) { var col = colorBuff.Data[i]; for (int j = 0; j < colorBuff.LonSteps; j++) { bm.SetPixel(j, i, transform(col[colorBuff.LonSteps - 1 - j])); } } MemoryStream stream = new MemoryStream(); bm.WriteFile(outputType, stream); // Rewind the stream... stream.Seek(0, SeekOrigin.Begin); return(stream); } }
private ChunkHolder <T> ReadChunk(DeletableFileStream stream, StandardChunkMetadata template) { byte[] buffer = new byte[Math.Max(4, pixelDataSize)]; stream.Read(buffer, 0, 4); int width = BitConverter.ToInt32(buffer, 0); stream.Read(buffer, 0, 4); int height = BitConverter.ToInt32(buffer, 0); var ret = new ChunkHolder <T>(width, height, template.LatLo, template.LonLo, template.LatHi, template.LonHi, null, toDouble, fromDouble); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { ret.Data[i][j] = ReadPixel(stream, buffer); } } return(ret); }
public ChunkStore SpawnChunk(Vector3 pos, ChunkHolder holder) { throw new System.NotImplementedException(); }
public ChunkHolder GetHolder(ChunkHolder current, ChunkOffset dir) { return(AllHolders[Random.Range(0, AllHolders.Count)]); }
public ChunkStore(ChunkHolder holder) { _holder = holder; }
public static ChunkHolder <float> GetChunk(string folder) { byte[] buffer = new byte[8]; // Read the header file. double dfCellSizeX, dfCellSizeY; int nBlocksPerRow, nBlocksPerColumn, nBlockXSize, nBlockYSize; using (FileStream fs = File.OpenRead(Path.Combine(folder, "hdr.adf"))) { // Read the block size information. dfCellSizeX = ReadDouble(fs, buffer, 256); dfCellSizeY = ReadDouble(fs, buffer, 264); nBlocksPerRow = ReadInt32(fs, buffer, 288); nBlocksPerColumn = ReadInt32(fs, buffer, 292); nBlockXSize = ReadInt32(fs, buffer, 296); nBlockYSize = ReadInt32(fs, buffer, 304); } // Read the extents. double latLo, lonLo, latHi, lonHi; using (FileStream fs = File.OpenRead(Path.Combine(folder, "dblbnd.adf"))) { lonLo = ReadDouble(fs, buffer); latLo = ReadDouble(fs, buffer); lonHi = ReadDouble(fs, buffer); latHi = ReadDouble(fs, buffer); } // Read the block index file. int[] panBlockOffset, panBlockSize; using (FileStream fs = File.OpenRead(Path.Combine(folder, "w001001x.adf"))) { int nBlocks = (int)((ReadInt32(fs, buffer, 24) * 2 - 100) / 8); panBlockOffset = new int[nBlocks]; panBlockSize = new int[nBlocks]; fs.Seek(100, SeekOrigin.Begin); for (int i = 0; i < nBlocks; i++) { panBlockOffset[i] = ReadInt32(fs, buffer) * 2; panBlockSize[i] = ReadInt32(fs, buffer) * 2; } } // Compute the number of pixels and lines, and the number of tile files. int nPixels = (int)((lonHi - lonLo + 0.5 * dfCellSizeX) / dfCellSizeX); int nLines = (int)((latHi - latLo + 0.5 * dfCellSizeY) / dfCellSizeY); // Open the file w001001.adf file itself. ChunkHolder <float> output = null; using (FileStream fs = File.OpenRead(Path.Combine(folder, "w001001.adf"))) { output = new ChunkHolder <float>(nLines, nPixels, Angle.FromDecimalDegrees(latLo), Angle.FromDecimalDegrees(lonLo), Angle.FromDecimalDegrees(latHi), Angle.FromDecimalDegrees(lonHi), null, new Func <float, double>[] { p => p }, p => (float)p[0]); float[] panRaster = new float[nBlockXSize * nBlockYSize]; byte[] panRasterBuffer = new byte[4 * nBlockXSize * nBlockYSize]; for (int nBlock = 0; nBlock < panBlockSize.Length; nBlock++) { // Collect raw data. fs.Seek(panBlockOffset[nBlock] + 2, SeekOrigin.Begin); fs.Read(panRasterBuffer, 0, panRasterBuffer.Length); for (int j = 0; j < panRaster.Length; j++) { byte tmp = panRasterBuffer[j * 4 + 3]; panRasterBuffer[j * 4 + 3] = panRasterBuffer[j * 4 + 0]; panRasterBuffer[j * 4 + 0] = tmp; tmp = panRasterBuffer[j * 4 + 2]; panRasterBuffer[j * 4 + 2] = panRasterBuffer[j * 4 + 1]; panRasterBuffer[j * 4 + 1] = tmp; panRaster[j] = BitConverter.ToSingle(panRasterBuffer, j * 4); } int tileOffsetX = (nBlock % nBlocksPerRow) * nBlockXSize; int tileOffsetY = (nBlock / nBlocksPerRow) * nBlockYSize; for (int j = 0; j < nBlockYSize && j < output.LonSteps - tileOffsetY; j++) { for (int i = 0; i < nBlockXSize && i < output.LatSteps - tileOffsetX; i++) { output.Data[nLines - 1 - j - tileOffsetY][nPixels - 1 - i - tileOffsetX] = panRaster[i + j * nBlockXSize]; } } } } return(output); }
public ChunkStore(ChunkHolder holder, GameObject gameObject) { _holder = holder; _gameObject = gameObject; }