Skip to content

Commit

Permalink
Frame version 13 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
halmaia committed Aug 7, 2023
1 parent 60d1267 commit 7129a6a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
27 changes: 23 additions & 4 deletions Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

namespace SL3Reader
{
[StructLayout(LayoutKind.Explicit, Size = ExtendedSize)]
[StructLayout(LayoutKind.Explicit, Size = ExtendedSizeV10)]
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
public readonly struct Frame
{
public const int ExtendedSize = 168;
public const int ExtendedSizeV10 = 168;
public const int ExtendedSizeV13 = 180;
public const int BasicSize = 128;
public const int MinimumInitSize = 44;

Expand All @@ -32,7 +33,7 @@ public readonly struct Frame

#region Basic Properties
[field: FieldOffset(0)] public readonly uint PositionOfFirstByte { get; } // (0)
[field: FieldOffset(4)] public readonly uint UnknownAt4 { get; } // (4) In my files it is always 10.
[field: FieldOffset(4)] public readonly uint Version { get; } // (4)
[field: FieldOffset(8)] public readonly ushort LengthOfFrame { get; } // (8)
[field: FieldOffset(10)] public readonly ushort PreviousFrameLength { get; } // (10)
[field: FieldOffset(12)] public readonly SurveyType SurveyType { get; } // (12)
Expand Down Expand Up @@ -75,7 +76,21 @@ public readonly struct Frame
[field: FieldOffset(124)] public readonly uint Milliseconds { get; } // (124)

// Derived:
public readonly long DataOffset => PositionOfFirstByte + (FrameType is FrameType.Extended ? ExtendedSize : BasicSize);
/// <summary>
/// Provides the data offset relatively to the begining of the frame.
/// </summary>
public readonly int RelativeDataOffset
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => FrameType is FrameType.Extended
? Version switch
{
10 => ExtendedSizeV10,
13 => ExtendedSizeV13,
_ => throw new NotImplementedException("Unsupported frame type. Contact developer at GitHub!")
}
: BasicSize;
}
#endregion Basic properties

#region Type support
Expand Down Expand Up @@ -116,6 +131,10 @@ public readonly FrameType FrameType

#endregion Extended properties

#region Extended v13 properties
// In version 13 the frame is bigger.
#endregion Extended v13 properties

#region DateTime support
private static DateTime timestampBase;
[SkipLocalsInit, MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
2 changes: 1 addition & 1 deletion Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"SL3Reader": {
"commandName": "Project",
"commandLineArgs": "\"C:\\Users\\halmaia\\Desktop\\CardSave\\M2\\K.sl3\" F: -route"
"commandLineArgs": "\"C:\\Users\\halmaia\\Downloads\\Sonar_2023-08-06_12.43.36.sl3\" F:\\3D.csv -3dg"
}
}
}
17 changes: 8 additions & 9 deletions SL3Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ static ReadOnlyCollection<GeoPoint> AugmentTrajectory(ReadOnlyCollectionBuilder<
else
{
double dy = y0 - y1;
if (double.Abs(dy) > 50) // Detect seriuos errors
if (double.Abs(dy) > 50) // Detect seriuos errors.
y0 = frame->Y;

double dx = x0 - x1;
if (double.Abs(dx) > 50)
if (double.Abs(dx) > 50) // Detect seriuos errors.
x0 = frame->X;
}

Expand Down Expand Up @@ -275,7 +275,7 @@ public unsafe void ExportImagery(string path, SurveyType surveyType = SurveyType
out Span<byte> fileBuffer,
out Span<byte> pixelData);

byte* offset = ((Frame*)imageFrames[0])->FrameType is FrameType.Basic ? (byte*)Frame.BasicSize : (byte*)Frame.ExtendedSize;
byte* offset = ((Frame*)imageFrames[0])->FrameType is FrameType.Basic ? (byte*)Frame.BasicSize : (byte*)Frame.ExtendedSizeV10;

fixed (byte* pixelPtr = pixelData)
for (int j = first, k = 0; j < final; j++)
Expand Down Expand Up @@ -368,7 +368,7 @@ public unsafe void ExamineUnknown8Datasets()
for (int i = 0; i < unknown8FrameCount - 1; i++)
{
nuint ptr = unknown8Frames[i];
Buffer.MemoryCopy((byte*)(ptr + (nuint)((Frame*)ptr)->DataOffset), p, 512, 512);
Buffer.MemoryCopy((byte*)(ptr + (nuint)((Frame*)ptr)->RelativeDataOffset), p, 512, 512);
for (int j = 0; j < 256; j += 2)
{
Debug.Print($"{buffer[j]}, {buffer[1 + j]}");
Expand All @@ -389,18 +389,17 @@ public unsafe void Export3D(string path, bool includeUnreliable = false, bool ma
int frames3DLength = frames3D.Count;
if (frames3DLength < 1) return;
ReadOnlyCollection<GeoPoint> augmentedCoordinates = AugmentedCoordinates;
var coordinate3DHelper = Coordinate3DHelper;
ReadOnlyCollection<int> coordinate3DHelper = Coordinate3DHelper;

using StreamWriter streamWriter = File.CreateText(path);
using StreamWriter streamWriter = File.CreateText(path!);
streamWriter.BaseStream.Write("CampaignID,DateTime,X[Lowrance_m],Y[Lowrance_m],Z[m_WGS84Ellipsoid],Depth[m],Angle[°],Distance[m],Reliability\r\n"u8);

string[] stringArray = GC.AllocateUninitializedArray<string>(9);


for (int i = 0; i < frames3DLength; i++)
{
Frame* frame = (Frame*)frames3D[i];
ThreeDimensionalFrameHeader* header = (ThreeDimensionalFrameHeader*)(((byte*)frame) + Frame.ExtendedSize);
ThreeDimensionalFrameHeader* header = (ThreeDimensionalFrameHeader*)((byte*)frame + frame->RelativeDataOffset);

// TODO: Remove intermediate solution:
GeoPoint augmentedCoordinate = augmentedCoordinates[coordinate3DHelper[i]];
Expand Down Expand Up @@ -521,7 +520,7 @@ static bool IsValidMeasurement(InterferometricMeasurement* measurement, out doub
{
delta = measurement->Delta;
depth = measurement->Depth;
return delta is not < 0.001 and not > 5000.0 && depth is not < 1 and not > 250.0;
return delta is not < 0.001 and not > 5000.0 && depth is not < 1 and not > 600.0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion SLFileHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public readonly override string ToString() =>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void ThrowIfInvalidFormatDetected()
{
if (FileFormat != LogFileFormat.SL3)
if (FileFormat is not LogFileFormat.SL3)
throw new InvalidDataException("Unsupported file type. Expected type SL3.");
}
}
Expand Down

0 comments on commit 7129a6a

Please sign in to comment.