diff --git a/Runtime/3rdParty/TIFF/TiffUtils.cs b/Runtime/3rdParty/TIFF/TiffUtils.cs index 74f8110d8df0e34f1748f97ce72f458f5891f11a..acfb1958791b3cc9559cb8b8355f7aaa33b3e5bc 100644 --- a/Runtime/3rdParty/TIFF/TiffUtils.cs +++ b/Runtime/3rdParty/TIFF/TiffUtils.cs @@ -10,7 +10,9 @@ namespace Wander { static float InvalidSample = -999; - public static float[] DecodePixelsAsFloat( string path, out int width, out int height ) + public static float[] DecodePixelsAsFloat( string path, out int width, out int height, bool flipVertically, + out double originX, out double originY, + out double pixelSizeX, out double pixelSizeY ) { width = 0; height = 0; @@ -18,20 +20,34 @@ namespace Wander byte [] tiffBytes = File.ReadAllBytes( path ); TiffStreamForBytes ts = new TiffStreamForBytes(tiffBytes); - using ( Tiff inImage = Tiff.ClientOpen( "bytes", "r", null, ts ) ) + using ( Tiff tiff = Tiff.ClientOpen( "bytes", "r", null, ts ) ) { - if ( inImage == null ) + if ( tiff == null ) { throw new InvalidDataException( "Tiff data not valid" ); } - width = inImage.GetField( TiffTag.IMAGEWIDTH )[0].ToInt(); - height = inImage.GetField( TiffTag.IMAGELENGTH )[0].ToInt(); - int tile_width = inImage.GetField(TiffTag.TILEWIDTH)[0].ToInt(); - int tile_height = inImage.GetField( TiffTag.TILELENGTH )[0].ToInt(); - int tileSize = inImage.TileSize(); + width = tiff.GetField( TiffTag.IMAGEWIDTH )[0].ToInt(); + height = tiff.GetField( TiffTag.IMAGELENGTH )[0].ToInt(); + int tile_width = tiff.GetField(TiffTag.TILEWIDTH)[0].ToInt(); + int tile_height = tiff.GetField( TiffTag.TILELENGTH )[0].ToInt(); + int tileSize = tiff.TileSize(); byte [] tileData = new byte[tileSize]; + // Obtain coords + { + FieldValue[] modelPixelScaleTag = tiff.GetField((TiffTag)33550); + FieldValue[] modelTiepointTag = tiff.GetField((TiffTag)33922); + + byte[] modelPixelScale = modelPixelScaleTag[1].GetBytes(); + pixelSizeX = BitConverter.ToDouble(modelPixelScale, 0); + pixelSizeY = BitConverter.ToDouble( modelPixelScale, 8 ) * -1; + + byte[] modelTransformation = modelTiepointTag[1].GetBytes(); + originX = BitConverter.ToDouble(modelTransformation, 24); + originY = BitConverter.ToDouble(modelTransformation, 32); + } + // output output = new float[width*height]; for ( int row = 0; row < height; row += tile_height ) @@ -39,7 +55,7 @@ namespace Wander for ( int col = 0; col < width; col += tile_width ) { // Read the tile into an RGBA array - int res = inImage.ReadTile( tileData, 0, col, row, 0, 0 ); + int res = tiff.ReadTile( tileData, 0, col, row, 0, 0 ); if ( res == -1) throw new InvalidDataException( "Invalid tiff tile" ); @@ -48,14 +64,16 @@ namespace Wander int y2 = row + y; if ( y2 >= height ) continue; - for ( int x = 0; x < tile_width; x++ ) + for (int x = 0;x < tile_width;x++) { - int x2 = col + x; - if ( x2 >= width ) + int x2 = col + x; + if (x2 >= width) continue; - float h = BitConverter.ToSingle( tileData, (x * tile_height + y) * 4 ); - output[y2*width + x2] = h; + //float h = BitConverter.ToSingle( tileData, (x * tile_height + y) * 4 ); + float h = BitConverter.ToSingle( tileData, (y * tile_width + x) * 4 ); + //int y3 = flipVertically? height-1-y2 : y2; + output[y2*width + x2] = h; } } } @@ -101,7 +119,7 @@ namespace Wander } } h = data[addr]; - if (h < -10 || h > 250) + if (h < -10 || h > 250) // Ensure final is valid. h = 0; } } diff --git a/Runtime/BasisVoorziening.cs b/Runtime/BasisVoorziening.cs index f51c599a859c96be421988e26debd58624d87822..600a1b70760c3441e6861afdce0ae529ce5823f1 100644 --- a/Runtime/BasisVoorziening.cs +++ b/Runtime/BasisVoorziening.cs @@ -16,6 +16,10 @@ namespace Wander public float[] data; public int width; public int height; + public double originX; + public double originY; + public double pixelSizeX; + public double pixelSizeY; } public interface IGetHeight @@ -222,6 +226,8 @@ namespace Wander m.SetUVs( 0, cb.uv ); m.SetTriangles( cb.indices, 0 ); m.RecalculateNormals(); + m.Optimize(); + m.UploadMeshData( true ); #if UNITY_EDITOR if (saveAssetsToDisk) @@ -371,19 +377,24 @@ namespace Wander Vector3 offset = new Vector3( tsl.X, tsl.Y, tsl.Z ); var unityVertices = cj.Vertices.Select( v => { - double x = ((v.x * (double)sc.X + (double)offset.x) - (double)offsetX); - // double y = ((v.z * (double)sc.Z + (double)offset.z) - (double)0); - double z = ((v.y * (double)sc.Y + (double)offset.y) - (double)offsetY); + double x = ((v.x * (double)sc.X + (double)offset.x)); + double z = ((v.y * (double)sc.Y + (double)offset.y)); double y = 0; + if ( heightData.data != null ) { - int xPixel = (int)((x - cj.GeoExtentMin.X) * tileWidth); - int yPixel = (int)((z - cj.GeoExtentMin.Z) * tileHeight); + int xPixel = Mathf.FloorToInt( (float)((x-heightData.originX) / heightData.pixelSizeX ) ); + int yPixel = Mathf.FloorToInt( (float)((z-heightData.originY) / heightData.pixelSizeY ) ); if ( xPixel >= 0 && xPixel < heightData.width && yPixel >= 0 && yPixel < heightData.height ) { y = heightData.data[yPixel*heightData.width+xPixel]; + // if ( y < -10 ) y = 0; // Filter already ensures final output is valid. + // if ( y > 250 ) y = 0; } } + + x -= (double)offsetX; + z -= (double)offsetY; //y=0; var vNew = new Vector3((float)x, (float)y, (float)z); //vNew -= new Vector3(offsetX, 0, offsetY); diff --git a/Runtime/GeoTiffHeight.cs b/Runtime/GeoTiffHeight.cs index c73a529b3368d4cccd6d4957e275665ca5983e1a..1233b5c9e0f2ab6dc0b94efbbe303b6fe1336fea 100644 --- a/Runtime/GeoTiffHeight.cs +++ b/Runtime/GeoTiffHeight.cs @@ -9,6 +9,8 @@ namespace Wander { void RegisterInterfaces() { + // if (!enabled) + // return; var bsv = GetComponent<BasisVoorziening>(); if ( bsv != null ) { @@ -20,7 +22,6 @@ namespace Wander { tileName = Path.GetFileNameWithoutExtension( tileName ).ToLower(); string [] tileWords = tileName.Split( '_', '.', '#', ',' ); - tileName = tileName.ToLower(); List<string> files = new List<string>(); files.AddRange( Directory.GetFiles( rootFolder, "*.tiff", SearchOption.AllDirectories ) ); @@ -34,17 +35,26 @@ namespace Wander { if (tileWords[k].Length == 5 && tileWords[k]==fileWords[j]) { - float [] data = TiffUtils.DecodePixelsAsFloat( files[i], out int width, out int height ); + float [] data = TiffUtils.DecodePixelsAsFloat + ( + files[i], out int width, out int height, true, + out double originX, out double originY, + out double pixelSizeX, out double pixelSizeY + ); TiffUtils.FilterFloatData( data, width, height ); return new HeightData { - width = width, - height = height, - data = data + width = width, + height = height, + data = data, + originX = originX, + originY = originY, + pixelSizeX = pixelSizeX, + pixelSizeY = pixelSizeY }; } } - } + } } return default; }