|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Collections.Generic; |
| 4 | +using Aardvark.Base; |
| 5 | +using Aardvark.Data.Points.Import; |
| 6 | +using PointCloudConverter.Structs; |
| 7 | +using static Aardvark.Data.Points.Import.E57; |
| 8 | +using Aardvark.Data.Points; |
| 9 | +using System.Text.Json; |
| 10 | +using Aardvark.Data.E57; |
| 11 | + |
| 12 | +namespace PointCloudConverter.Readers |
| 13 | +{ |
| 14 | + public class E57 : IReader, IDisposable |
| 15 | + { |
| 16 | + private IEnumerator<E57Chunk> chunkEnumerator; |
| 17 | + private E57Chunk currentChunk; |
| 18 | + private int currentPointIndex = 0; |
| 19 | + |
| 20 | + private ASTM_E57.E57FileHeader header; |
| 21 | + private E57MetaData metaData; |
| 22 | + |
| 23 | + private Float3 lastXYZ; |
| 24 | + |
| 25 | + public struct E57MetaData |
| 26 | + { |
| 27 | + public string Name { get; set; } |
| 28 | + public double X { get; set; } |
| 29 | + public double Y { get; set; } |
| 30 | + public double Z { get; set; } |
| 31 | + public double RX { get; set; } |
| 32 | + public double RY { get; set; } |
| 33 | + public double RZ { get; set; } |
| 34 | + public double RW { get; set; } |
| 35 | + } |
| 36 | + |
| 37 | + public bool InitReader(ImportSettings importSettings, int fileIndex) |
| 38 | + { |
| 39 | + try |
| 40 | + { |
| 41 | + var filePath = importSettings.inputFiles[fileIndex]; |
| 42 | + |
| 43 | + // Read header metadata |
| 44 | + using var stream = File.OpenRead(filePath); |
| 45 | + header = ASTM_E57.E57FileHeader.Parse(stream, new FileInfo(filePath).Length, false); |
| 46 | + stream.Close(); |
| 47 | + |
| 48 | + var pose = header.E57Root.Data3D[0].Pose; |
| 49 | + |
| 50 | + metaData = new E57MetaData |
| 51 | + { |
| 52 | + Name = header.E57Root.Data3D[0].Name, |
| 53 | + X = pose.Translation.X, |
| 54 | + Y = importSettings.swapYZ ? pose.Translation.Z : pose.Translation.Y, |
| 55 | + Z = importSettings.swapYZ ? pose.Translation.Y : pose.Translation.Z, |
| 56 | + RX = pose.Rotation.X, |
| 57 | + RY = importSettings.swapYZ ? pose.Rotation.Z : pose.Rotation.Y, |
| 58 | + RZ = importSettings.swapYZ ? pose.Rotation.Y : pose.Rotation.Z, |
| 59 | + RW = pose.Rotation.W |
| 60 | + }; |
| 61 | + |
| 62 | + var chunks = ChunksFull(filePath, ParseConfig.Default); |
| 63 | + chunkEnumerator = chunks.GetEnumerator(); |
| 64 | + |
| 65 | + if (!chunkEnumerator.MoveNext()) |
| 66 | + return false; |
| 67 | + |
| 68 | + currentChunk = chunkEnumerator.Current; |
| 69 | + currentPointIndex = 0; |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + catch (Exception ex) |
| 74 | + { |
| 75 | + Console.WriteLine("E57 InitReader error: " + ex.Message); |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public LasHeader GetMetaData(ImportSettings importSettings, int fileIndex) |
| 81 | + { |
| 82 | + return new LasHeader |
| 83 | + { |
| 84 | + FileName = importSettings.inputFiles[fileIndex], |
| 85 | + NumberOfPointRecords = (uint)(header?.E57Root?.Data3D?[0]?.Points?.RecordCount ?? 0) |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + public Bounds GetBounds() |
| 90 | + { |
| 91 | + var bounds = header.E57Root.Data3D[0].CartesianBounds.Bounds; |
| 92 | + |
| 93 | + return new Bounds |
| 94 | + { |
| 95 | + minX = (float)bounds.X.Min, |
| 96 | + maxX = (float)bounds.X.Max, |
| 97 | + minY = (float)bounds.Y.Min, |
| 98 | + maxY = (float)bounds.Y.Max, |
| 99 | + minZ = (float)bounds.Z.Min, |
| 100 | + maxZ = (float)bounds.Z.Max |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + public int GetPointCount() |
| 105 | + { |
| 106 | + return (int)(header?.E57Root?.Data3D?[0]?.Points?.RecordCount ?? 0); |
| 107 | + } |
| 108 | + |
| 109 | + public Float3 GetXYZ() |
| 110 | + { |
| 111 | + if (currentChunk == null || currentPointIndex >= currentChunk.Count) |
| 112 | + { |
| 113 | + if (!chunkEnumerator.MoveNext()) |
| 114 | + return new Float3 { hasError = true }; |
| 115 | + |
| 116 | + currentChunk = chunkEnumerator.Current; |
| 117 | + currentPointIndex = 0; |
| 118 | + |
| 119 | + // clear cachedColors when chunk changes |
| 120 | + cachedColors = null; |
| 121 | + } |
| 122 | + |
| 123 | + var p = currentChunk.Positions[currentPointIndex]; |
| 124 | + lastXYZ.x = p.X; |
| 125 | + lastXYZ.y = p.Y; |
| 126 | + lastXYZ.z = p.Z; |
| 127 | + lastXYZ.hasError = false; |
| 128 | + |
| 129 | + currentPointIndex++; |
| 130 | + return lastXYZ; |
| 131 | + } |
| 132 | + |
| 133 | + private C3b[] cachedColors = null; |
| 134 | + |
| 135 | + public Color GetRGB() |
| 136 | + { |
| 137 | + if (cachedColors == null && currentChunk?.Colors != null) |
| 138 | + { |
| 139 | + cachedColors = currentChunk.Colors; |
| 140 | + } |
| 141 | + |
| 142 | + int i = currentPointIndex - 1; |
| 143 | + if (cachedColors != null && i >= 0 && i < cachedColors.Length) |
| 144 | + { |
| 145 | + var c = cachedColors[i]; |
| 146 | + return new Color |
| 147 | + { |
| 148 | + r = c.R / 255f, |
| 149 | + g = c.G / 255f, |
| 150 | + b = c.B / 255f |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + return default; |
| 155 | + } |
| 156 | + |
| 157 | + public byte GetIntensity() |
| 158 | + { |
| 159 | + var i = currentPointIndex - 1; |
| 160 | + if (currentChunk?.Intensities != null && i >= 0 && i < currentChunk.Intensities.Length) |
| 161 | + { |
| 162 | + return (byte)currentChunk.Intensities[i]; |
| 163 | + } |
| 164 | + return 0; |
| 165 | + } |
| 166 | + |
| 167 | + public byte GetClassification() => 0; |
| 168 | + |
| 169 | + public double GetTime() |
| 170 | + { |
| 171 | + // Not implemented for now |
| 172 | + return 0; |
| 173 | + } |
| 174 | + |
| 175 | + public void Close() { } |
| 176 | + |
| 177 | + public void Dispose() |
| 178 | + { |
| 179 | + Close(); |
| 180 | + GC.SuppressFinalize(this); |
| 181 | + } |
| 182 | + |
| 183 | + ~E57() |
| 184 | + { |
| 185 | + Dispose(); |
| 186 | + } |
| 187 | + |
| 188 | + public string GetMetaDataJSON() |
| 189 | + { |
| 190 | + return JsonSerializer.Serialize(metaData); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments