diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b5ba2e..aa326bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 from the range of the field). - Spherical coordinates of E57 scans are converted to Cartesian points as the standard defines; scaled integers work for the spherical components too. +- Points that the E57 validity flags mark as invalid are skipped, and flagged colour or intensity channels are left empty. + Colour with a wider Integer range from zero (16 or 32 bits) and intensity as a wider Integer, a Float or a scaled + integer are decoded; other shapes stay unread. ## [0.1.0] - 2026-11-15 diff --git a/README.md b/README.md index b854cae..dcd5299 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ The library targets .NET 8 and .NET 10. It is managed code with no dependencies | Format | Extensions | Support level | | --- | --- | --- | -| ASTM E57 | `.e57` | Points decoded for a defined subset: uncompressed bit-packed streams with single or double precision Cartesian coordinates or scaled-integer coordinates (raw value times scale plus offset), or spherical range, azimuth and elevation converted to Cartesian points, with 8-bit colour and 8-bit intensity. Pose translation and rotation are applied. Other codecs are not supported: a scan that uses them lists normally and fails when its points are read. Per-point validity flags and images are not read, so points flagged as invalid are returned as stored, and colour or intensity fields of any other type or range are ignored. Page checksums are verified. | +| ASTM E57 | `.e57` | Points decoded for a defined subset: uncompressed bit-packed streams with single or double precision Cartesian coordinates or scaled-integer coordinates (raw value times scale plus offset), or spherical range, azimuth and elevation converted to Cartesian points, with 8-bit colour and 8-bit intensity, or colour with a wider Integer range from zero and intensity as a wider Integer, a Float or a scaled integer. Pose translation and rotation are applied. Other codecs are not supported: a scan that uses them lists normally and fails when its points are read. Per-point validity flags skip invalid points and clear invalid channels when their streams hold one flag byte per record; other shapes are not interpreted. Images are not read, so a scan with them still lists and reads its points. Page checksums are verified. | | ReCap scan | `.rcs` | Experimental. The header is read; point decoding covers the node layouts that are described in the code, which is the one of major version 3. A file of another version is listed with its header, but reading its points fails with a message that names the version. A further layout is described only when the files of that version may be used for the tests, so that its offsets can be verified. The channels of the points follow from the size of the payload of the nodes: colour and intensity when the records are long enough, and normals when the node holds the plane of them. | | ReCap project | `.rcp` | Lists the scans of the project manifest and opens the `.rcs` files it finds next to the project. The transform of a scan node places the scan in the project and is applied to its points unless `ApplyTransform` is false; when the manifest states no transform, the transform of the scan file is used. | | Leica scan container | `.lgsx`, `.lgs` | Experimental. Points are decoded from the compressed point blocks: position, and colour, intensity and normals when the block has them. When the container assigns its points to its setups, there is one scan per setup, named after the setup, with the pose and the bounds of the setup when its record holds them; the pose is applied unless `ApplyTransform` is false. When the container does not assign its points, all points of the container form one scan and a warning says so. The point count is taken from the container when it states one; otherwise it is found by decoding the data, which is slow on large files. | diff --git a/docs/public-api.md b/docs/public-api.md index f2e9d50..b885904 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -59,7 +59,7 @@ drop points that fail a plausibility rule (see the README), so a scan can yield | Reader | `PointCount` | |---|---| -| E57 | Declared by the file. It is not checked against the point data until the points are read. | +| E57 | Declared by the file. It is not checked against the point data until the points are read. Points that the validity flags mark as invalid are skipped, so a read can yield fewer points. | | ReCap scan | Declared in the header. It is not checked against the node table. The node layout of major version 3 is decoded; a file of another version is listed but its points cannot be read (`RcsScanDocument.DecodedNodeLayoutVersions` names the decoded versions). | | ReCap project | Declared by the project manifest; 0 when the manifest states none. | | Leica | The number of points the container states, when it states one; otherwise it is computed. The computed value needs the first access to decode all of the point data and caches the result, which can be slow on large files; the access throws `InvalidDataException` for corrupt data. A container that assigns its points to its setups has one scan per setup, each with the id and the name of its setup; otherwise the container is one scan with the id `unified-cloud`. The `Transform` and the `Bounds` of a setup are read from its record and are empty when the record does not hold them. | diff --git a/src/PclIO/Formats/E57/E57Document.cs b/src/PclIO/Formats/E57/E57Document.cs index 452191d..d90eebe 100644 --- a/src/PclIO/Formats/E57/E57Document.cs +++ b/src/PclIO/Formats/E57/E57Document.cs @@ -11,11 +11,12 @@ namespace PclIO.Formats.E57; /// Supported: the file header, the checksummed page layout, the XML section, scan names, ids, Cartesian bounds and /// poses (translation and rotation quaternion), and points stored as uncompressed bit-packed streams where /// Cartesian X/Y/Z are single or double precision floats or scaled integers (raw value times scale plus offset), -/// or spherical range, azimuth and elevation converted to Cartesian points, and colour or intensity are Integer -/// fields in the range 0..255. +/// or spherical range, azimuth and elevation converted to Cartesian points. Colour and intensity are Integer fields +/// with a range from zero (8, 16 or 32 bits), Float fields, or scaled-integer intensity; per-point validity flags +/// skip invalid points and clear invalid channels. Images are not read. /// Not supported (reported with when points are read): integer coordinates, /// other codecs, and packets whose streams hold different record counts. -/// Spherical coordinates, per-point invalid-state flags, images and 2D data are not interpreted. +/// Per-point validity flags of another shape, images and 2D data are not interpreted. /// public sealed class E57Document : PclDocument { @@ -318,9 +319,33 @@ internal sealed class E57PointLayout /// Stream index of red, green and blue, or all -1 when colour is not decoded. public int[] Color { get; } = [-1, -1, -1]; + /// Bytes per record of the colour streams: 1, 2 or 4 for Integer ranges from zero. + public int[] ColorSize { get; } = [1, 1, 1]; + + /// Maximum of the colour ranges; an 8-bit channel holds the raw value times 255 divided by it. + public ulong[] ColorMax { get; } = [255, 255, 255]; + /// Stream index of intensity, or -1 when intensity is not decoded. public int Intensity { get; private set; } = -1; + /// Bytes per record of the intensity stream. + public int IntensitySize { get; private set; } = 1; + + /// True when intensity is an integer (raw value times scale plus offset); false for Float. + public bool IntensityScaled { get; private set; } = true; + + /// True when the integer intensity range is not negative. + public bool IntensityUnsigned { get; private set; } = true; + + /// Scale of an integer intensity (1 divided by the maximum for a plain Integer); 1 for Float. + public double IntensityScale { get; private set; } = 1.0 / 255.0; + + /// Offset of a scaled integer intensity; 0 otherwise. + public double IntensityOffset { get; private set; } + + /// Stream index of the validity flags (Cartesian point, spherical point, colour, intensity), or -1. + public int[] Invalid { get; } = [-1, -1, -1, -1]; + public bool HasColor => Color[0] >= 0; private E57PointLayout(int streamCount) => StreamCount = streamCount; @@ -369,6 +394,15 @@ internal sealed class E57PointLayout _ => -1, }; + int invalidKind = field.Name.LocalName switch + { + "cartesianInvalidState" => 0, + "sphericalInvalidState" => 1, + "isColorInvalid" => 2, + "isIntensityInvalid" => 3, + _ => -1, + }; + if (axis >= 0) { if (layout.Coordinate[axis] >= 0) @@ -429,17 +463,35 @@ internal sealed class E57PointLayout layout.SphericalScale[spherical] = scale; layout.SphericalOffset[spherical] = offset; } - else if (IsByteRange(field)) + else if (invalidKind >= 0) + { + // Validity flags hold one flag byte per record; any other shape stays unread like an unknown field. + if (layout.Invalid[invalidKind] < 0 && TryValidityField(field)) layout.Invalid[invalidKind] = i; + } + else if (field.Name.LocalName is "colorRed" or "colorGreen" or "colorBlue") { int channel = field.Name.LocalName switch { "colorRed" => 0, "colorGreen" => 1, - "colorBlue" => 2, - _ => -1, + _ => 2, }; - if (channel >= 0 && layout.Color[channel] < 0) layout.Color[channel] = i; - else if (field.Name.LocalName == "intensity" && layout.Intensity < 0) layout.Intensity = i; + if (layout.Color[channel] < 0 && TryColorField(field, out int size, out ulong max)) + { + layout.Color[channel] = i; + layout.ColorSize[channel] = size; + layout.ColorMax[channel] = max; + } + } + else if (field.Name.LocalName == "intensity" && layout.Intensity < 0 && + TryIntensityField(field, out int size, out bool isFloat, out bool unsigned, out double scale, out double offset)) + { + layout.Intensity = i; + layout.IntensitySize = size; + layout.IntensityScaled = !isFloat; + layout.IntensityUnsigned = unsigned; + layout.IntensityScale = scale; + layout.IntensityOffset = offset; } } @@ -462,8 +514,9 @@ internal sealed class E57PointLayout layout.IsSpherical = hasSpherical; - // Colour is all-or-nothing. - if (layout.Color.Any(c => c < 0)) Array.Fill(layout.Color, -1); + // Colour is all-or-nothing with one shared encoding. + if (layout.Color.Any(c => c < 0) || layout.ColorSize.Distinct().Count() > 1 || layout.ColorMax.Distinct().Count() > 1) + Array.Fill(layout.Color, -1); return layout; } @@ -504,6 +557,31 @@ private static int ScaledIntegerSize(XElement field, out double scale, out doubl (!double.TryParse(offsetText, NumberStyles.Float, CultureInfo.InvariantCulture, out offset) || !double.IsFinite(offset))) throw FormatError.Corrupted("E57 scaled integer offset is not a finite number."); + return IntegerByteSize(min, max, out unsigned); + } + + // The binary size of an Integer field follows from its range like a ScaledInteger's: the smallest of 1, 2, 4 or + // 8 bytes that holds it. 0 when the field is no Integer or its range is invalid; auxiliary fields with such a + // range stay unread instead of failing the whole file. + private static int IntegerSize(XElement field, out long min, out long max, out bool unsigned) + { + min = 0; + max = 0; + unsigned = true; + if ((string?)field.Attribute("type") != "Integer") return 0; + if (!long.TryParse((string?)field.Attribute("minimum"), NumberStyles.Integer, CultureInfo.InvariantCulture, out min) || + !long.TryParse((string?)field.Attribute("maximum"), NumberStyles.Integer, CultureInfo.InvariantCulture, out max) || + min > max) + return 0; + + return IntegerByteSize(min, max, out unsigned); + } + + // The smallest of 1, 2, 4 or 8 bytes that holds every value from minimum to maximum, unsigned when the minimum + // is not negative and signed otherwise. + private static int IntegerByteSize(long min, long max, out bool unsigned) + { + unsigned = true; if (min >= 0) { if ((ulong)max <= 0xFF) return 1; @@ -519,11 +597,68 @@ private static int ScaledIntegerSize(XElement field, out double scale, out doubl return 8; } - // An Integer in 0..255 is bit-packed into exactly one byte per record. - private static bool IsByteRange(XElement field) => - (string?)field.Attribute("type") == "Integer" && - long.TryParse((string?)field.Attribute("minimum"), NumberStyles.Integer, CultureInfo.InvariantCulture, out long min) && min == 0 && - long.TryParse((string?)field.Attribute("maximum"), NumberStyles.Integer, CultureInfo.InvariantCulture, out long max) && max == 255; + // A colour channel is an Integer range from zero with a byte-aligned maximum, decoded to 8 bits. + private static bool TryColorField(XElement field, out int size, out ulong max) + { + size = 0; + max = 0; + if (IntegerSize(field, out long min, out long rangeMax, out _) != 0 && min == 0 && + (rangeMax == 0xFF || rangeMax == 0xFFFF || rangeMax == 0xFFFFFFFFL)) + { + size = IntegerByteSize(min, rangeMax, out _); + max = (ulong)rangeMax; + return true; + } + + return false; + } + + // Intensity is a Float used as-is, a ScaledInteger decoded with its scale and offset, or an Integer range from + // zero normalized by its maximum. A malformed scaled intensity leaves the channel unread instead of failing the file. + private static bool TryIntensityField(XElement field, out int size, out bool isFloat, out bool unsigned, out double scale, out double offset) + { + size = 1; + isFloat = false; + unsigned = true; + scale = 1.0 / 255.0; + offset = 0; + + int floatSize = FloatSize(field); + if (floatSize != 0) + { + size = floatSize; + isFloat = true; + scale = 1; + return true; + } + + try + { + int scaledSize = ScaledIntegerSize(field, out scale, out offset, out unsigned); + if (scaledSize != 0) + { + size = scaledSize; + return true; + } + } + catch (InvalidDataException) + { + return false; + } + + if (IntegerSize(field, out long min, out long max, out _) == 0 || min != 0 || + (max != 0xFF && max != 0xFFFF && max != 0xFFFFFFFFL)) + return false; + + size = IntegerByteSize(min, max, out _); + scale = 1.0 / max; + return true; + } + + // A validity flag is an Integer field that holds one flag byte per record. Any other shape stays unread like an + // unknown field, so files the reader cannot interpret keep today's behaviour instead of failing. + private static bool TryValidityField(XElement field) => + IntegerSize(field, out _, out _, out _) == 1; } /// An individual scan inside an ASTM E57 file. @@ -549,10 +684,10 @@ public sealed class E57Scan : PclScan /// Physical file offset of the compressed-vector section that holds the points, or 0 when there is none. public ulong FileOffset { get; } - /// True when the prototype has red, green and blue fields in the range 0..255. + /// True when the prototype has red, green and blue fields with a supported encoding. public override bool HasColors => _layout?.HasColor == true; - /// True when the prototype has an intensity field in the range 0..255. + /// True when the prototype has an intensity field with a supported encoding. public override bool HasIntensities => _layout is { Intensity: >= 0 }; internal E57Scan(string id, string name, long pointCount, ulong fileOffset, PclBounds bounds, PclTransform transform, ulong pageSize, string filePath, E57PointLayout? layout, string? unsupportedReason) @@ -570,7 +705,9 @@ internal E57Scan(string id, string name, long pointCount, ulong fileOffset, PclB } /// - /// Streams the points of this scan. Intensity is the stored byte divided by 255. + /// Streams the points of this scan. Points flagged invalid by the validity fields of the standard are skipped; + /// a flagged colour or intensity leaves the point without that channel. Intensity is the stored value divided by + /// the maximum of its range, or the decoded value for Float and scaled fields. /// is applied to the record index across the whole scan. /// /// Read options, or null for . @@ -647,21 +784,33 @@ public override IEnumerable ReadPoints(PclReadOptions? options = null) { if ((consumed + i) % step != 0) continue; + // A validity stream holds one flag byte per record of its packet; any other shape is not interpreted. + int pointInvalid = layout.IsSpherical ? layout.Invalid[1] : layout.Invalid[0]; + bool skipInvalid = HonorsFlags(lengths, pointInvalid, records); + if (skipInvalid && pkt[starts[pointInvalid] + i] != 0) continue; + var coords = layout.IsSpherical ? ReadSpherical(layout, pkt, starts, i) : new PclVector3(ReadAxis(layout, pkt, starts, 0, i), ReadAxis(layout, pkt, starts, 1, i), ReadAxis(layout, pkt, starts, 2, i)); + bool colorInvalid = HonorsFlags(lengths, layout.Invalid[2], records) && pkt[starts[layout.Invalid[2]] + i] != 0; PclColor? color = null; - if (layout.HasColor) + if (layout.HasColor && !colorInvalid) { color = new PclColor( - pkt[starts[layout.Color[0]] + i], - pkt[starts[layout.Color[1]] + i], - pkt[starts[layout.Color[2]] + i]); + ReadColorChannel(pkt, starts[layout.Color[0]], i, layout.ColorSize[0], layout.ColorMax[0]), + ReadColorChannel(pkt, starts[layout.Color[1]], i, layout.ColorSize[1], layout.ColorMax[1]), + ReadColorChannel(pkt, starts[layout.Color[2]], i, layout.ColorSize[2], layout.ColorMax[2])); } + bool intensityInvalid = HonorsFlags(lengths, layout.Invalid[3], records) && pkt[starts[layout.Invalid[3]] + i] != 0; float? intensity = null; - if (layout.Intensity >= 0) intensity = pkt[starts[layout.Intensity] + i] / 255.0f; + if (layout.Intensity >= 0 && !intensityInvalid) + { + intensity = layout.IntensityScaled + ? (float)(ReadRawInteger(pkt, starts[layout.Intensity], layout.IntensitySize, i, layout.IntensityUnsigned) * layout.IntensityScale + layout.IntensityOffset) + : (float)ReadCoordinate(pkt, starts[layout.Intensity], layout.IntensitySize, i); + } var pt = new PclPoint(coords, Color: color, Intensity: intensity, Index: (ulong)yielded); if (applyTransform) pt = pt with { Position = Transform.TransformPoint(pt.Position) }; @@ -702,10 +851,21 @@ private static int RecordsInPacket(E57PointLayout layout, int[] lengths) if (layout.HasColor) { - foreach (int stream in layout.Color) Agree(ref records, lengths[stream]); + for (int c = 0; c < 3; c++) + { + int size = layout.ColorSize[c]; + int length = lengths[layout.Color[c]]; + if (length % size != 0) throw FormatError.Corrupted("E57 colour stream holds a partial record."); + Agree(ref records, length / size); + } } - if (layout.Intensity >= 0) Agree(ref records, lengths[layout.Intensity]); + if (layout.Intensity >= 0) + { + int length = lengths[layout.Intensity]; + if (length % layout.IntensitySize != 0) throw FormatError.Corrupted("E57 intensity stream holds a partial record."); + Agree(ref records, length / layout.IntensitySize); + } return records; } @@ -749,10 +909,24 @@ private static double ReadCoordinate(byte[] packet, int start, int size, int ind ? Bytes.ReadSingleLE(packet.AsSpan(start + index * 4, 4)) : Bytes.ReadDoubleLE(packet.AsSpan(start + index * 8, 8)); - // A raw value of the sized width, signed or unsigned as the range requires; the value is raw times scale plus offset. - private static double ReadScaledInteger(byte[] packet, int start, int size, int index, bool unsigned, double scale, double offset) + // A validity stream holds one flag byte per record of its packet; any other shape is not interpreted. + private static bool HonorsFlags(int[] lengths, int stream, int records) => + stream >= 0 && lengths[stream] == records; + + // An 8-bit channel holds the raw value times 255 divided by the maximum of its range. + private static byte ReadColorChannel(byte[] packet, int start, int index, int size, ulong max) { - double raw = (size, unsigned) switch + double raw = ReadRawInteger(packet, start, size, index, unsigned: true); + return (byte)Math.Round(raw * 255.0 / max); + } + + // A raw value of the sized width, signed or unsigned as the range requires; the value is raw times scale plus offset. + private static double ReadScaledInteger(byte[] packet, int start, int size, int index, bool unsigned, double scale, double offset) => + ReadRawInteger(packet, start, size, index, unsigned) * scale + offset; + + // A raw value of the sized width, signed or unsigned as the range requires. + private static double ReadRawInteger(byte[] packet, int start, int size, int index, bool unsigned) => + (size, unsigned) switch { (1, true) => packet[start + index], (1, false) => (sbyte)packet[start + index], @@ -763,6 +937,4 @@ private static double ReadScaledInteger(byte[] packet, int start, int size, int (8, true) => Bytes.ReadUInt64LE(packet.AsSpan(start + index * 8, 8)), _ => Bytes.ReadInt64LE(packet.AsSpan(start + index * 8, 8)), }; - return raw * scale + offset; - } } diff --git a/tests/PclIO.Tests/FormatTests.cs b/tests/PclIO.Tests/FormatTests.cs index ba228cd..884f6a5 100644 --- a/tests/PclIO.Tests/FormatTests.cs +++ b/tests/PclIO.Tests/FormatTests.cs @@ -703,6 +703,271 @@ public void E57_With_A_Partial_Spherical_Record_Is_Rejected() Assert.Throws(() => doc.Scans[0].ReadPoints().ToList()); } + [Fact] + public void E57_With_Validity_Flags_Skips_Invalid_Points_And_Channels() + { + const string prototype = """ + + + + + + + + + + + + + """; + byte[] floats(float a, float b, float c) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b), .. BitConverter.GetBytes(c)]; + var spec = new E57Spec + { + PrototypeXml = prototype, + Points = [], + RecordCount = 3, + RawPackets = E57Spec.DataPacket([ + floats(1, 2, 3), floats(4, 5, 6), floats(7, 8, 9), + [0, 1, 0], + [10, 40, 70], [20, 50, 80], [30, 60, 90], + [100, 110, 120], + [0, 0, 1], + [1, 0, 0], + ]), + }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("flags.e57", spec.Build())); + + var points = doc.Scans[0].ReadPoints(NoTransform).ToList(); + + Assert.Equal(2, points.Count); + Assert.Equal([0UL, 1UL], points.Select(p => p.Index)); + AssertNear(new PclVector3(1, 4, 7), points[0].Position); + AssertNear(new PclVector3(3, 6, 9), points[1].Position); + Assert.Equal(new PclColor(10, 20, 30), points[0].Color); + Assert.Null(points[0].Intensity); + Assert.Null(points[1].Color); + Assert.Equal(120 / 255.0f, points[1].Intensity); + } + + [Fact] + public void E57_With_A_Spherical_Validity_Flag_Skips_Invalid_Points() + { + const string prototype = """ + + + + + + + """; + byte[] rs = [.. BitConverter.GetBytes(2.0f), .. BitConverter.GetBytes(2.0f)]; + byte[] azs = [.. BitConverter.GetBytes(0.0f), .. BitConverter.GetBytes(0.0f)]; + byte[] els = [.. BitConverter.GetBytes(0.0f), .. BitConverter.GetBytes(0.0f)]; + var spec = new E57Spec { PrototypeXml = prototype, Points = [], RecordCount = 2, RawPackets = E57Spec.DataPacket([rs, azs, els, [1, 0]]) }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("spherical-flags.e57", spec.Build())); + + var point = Assert.Single(doc.Scans[0].ReadPoints(NoTransform)); + AssertNear(new PclVector3(2, 0, 0), point.Position); + } + + [Fact] + public void E57_With_Sixteen_Bit_Colour_Is_Decoded() + { + const string prototype = """ + + + + + + + + + """; + byte[] floats(float a, float b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + byte[] words(ushort a, ushort b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var spec = new E57Spec + { + PrototypeXml = prototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), words(0, 257), words(0, 257), words(0, 65535)]), + }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("color16.e57", spec.Build())); + + var scan = Assert.Single(doc.Scans); + Assert.True(scan.HasColors); + + var points = scan.ReadPoints(NoTransform).ToList(); + Assert.Equal(new PclColor(0, 0, 0), points[0].Color); + Assert.Equal(new PclColor(1, 1, 255), points[1].Color); + } + + [Fact] + public void E57_With_Float_And_Scaled_Integer_Intensity_Is_Decoded() + { + const string floatPrototype = """ + + + + + + + """; + byte[] floats(float a, float b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var floatSpec = new E57Spec + { + PrototypeXml = floatPrototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), floats(0.5f, 1.0f)]), + }; + + const string scaledPrototype = """ + + + + + + + """; + byte[] words(ushort a, ushort b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var scaledSpec = new E57Spec + { + PrototypeXml = scaledPrototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), words(0, 500)]), + }; + + using var dir = new TempDirectory(); + using var floatDoc = E57Document.Open(dir.Write("float-intensity.e57", floatSpec.Build())); + using var scaledDoc = E57Document.Open(dir.Write("scaled-intensity.e57", scaledSpec.Build())); + + Assert.True(floatDoc.Scans[0].HasIntensities); + Assert.True(scaledDoc.Scans[0].HasIntensities); + + var floatPoints = floatDoc.Scans[0].ReadPoints(NoTransform).ToList(); + Assert.Equal(0.5f, floatPoints[0].Intensity); + Assert.Equal(1.0f, floatPoints[1].Intensity); + + var scaledPoints = scaledDoc.Scans[0].ReadPoints(NoTransform).ToList(); + Assert.Equal(0.0f, scaledPoints[0].Intensity); + Assert.Equal(0.5f, scaledPoints[1].Intensity); + } + + [Fact] + public void E57_With_Mixed_Colour_Encodings_Reports_No_Colour() + { + const string prototype = """ + + + + + + + + + """; + byte[] floats(float a, float b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var spec = new E57Spec + { + PrototypeXml = prototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), [0, 0], [0, 0], [0, 0]]), + }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("mixed.e57", spec.Build())); + + // The colour encodings differ, so the scan is listed without colour and reads its positions. + var scan = Assert.Single(doc.Scans); + Assert.False(scan.HasColors); + Assert.Equal(2, scan.ReadPoints(NoTransform).Count()); + } + + [Fact] + public void E57_With_A_Validity_Stream_Of_Another_Shape_Returns_All_Points() + { + // A validity stream that does not hold one flag byte per record is not interpreted. + const string prototype = """ + + + + + + + """; + byte[] floats(float a, float b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var spec = new E57Spec + { + PrototypeXml = prototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), [1]]), + }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("flags.e57", spec.Build())); + + Assert.Equal(2, doc.Scans[0].ReadPoints(NoTransform).Count()); + } + + [Fact] + public void E57_With_A_Multi_Byte_Validity_Range_Returns_All_Points() + { + // Only one-byte validity flags are honoured. + const string prototype = """ + + + + + + + """; + byte[] floats(float a, float b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + byte[] words(ushort a, ushort b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var spec = new E57Spec + { + PrototypeXml = prototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), words(1, 1)]), + }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("flags.e57", spec.Build())); + + Assert.Equal(2, doc.Scans[0].ReadPoints(NoTransform).Count()); + } + + [Fact] + public void E57_With_A_Partial_Colour_Record_Is_Rejected() + { + const string prototype = """ + + + + + + + + + """; + byte[] floats(float a, float b) => [.. BitConverter.GetBytes(a), .. BitConverter.GetBytes(b)]; + var spec = new E57Spec + { + PrototypeXml = prototype, + Points = [], + RecordCount = 2, + RawPackets = E57Spec.DataPacket([floats(1, 2), floats(1, 2), floats(1, 2), floats(1, 2)[..3], floats(1, 2), floats(1, 2)]), + }; + using var dir = new TempDirectory(); + using var doc = E57Document.Open(dir.Write("partial.e57", spec.Build())); + + Assert.Throws(() => doc.Scans[0].ReadPoints().ToList()); + } + // ---- Leica ----------------------------------------------------------------------------------------------- [Fact]