Skip to content

Commit

Permalink
Micro-opts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Halmai Ákos authored and Halmai Ákos committed Dec 28, 2023
1 parent 81b916d commit c8e20d3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
42 changes: 22 additions & 20 deletions Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ internal static void InitTimestampBase(uint hardwareTime) =>
#endregion

#region WGS84
private const double RadToDeg = 360d / double.Tau;

private const double PolarRadius = 6356752.3142d; // Lowrance uses float, causing truncated precision.
public readonly double Longitude => X / PolarRadius * RadToDeg;
public readonly double Latitude => RadToDeg * double.Atan(double.Sinh(Y / PolarRadius));
public readonly double Longitude => double.RadiansToDegrees( X / PolarRadius);
public readonly double Latitude => double.RadiansToDegrees( double.Atan(double.Sinh(Y / PolarRadius)));
#endregion WGS84

#region String generation
Expand Down Expand Up @@ -184,62 +184,64 @@ public readonly override string ToString()
Milliseconds.ToString()]);
}

[SkipLocalsInit]
public readonly ReadOnlySpan<byte> Format(Span<byte> buffer)
{
buffer.Fill(44);//','u8;
ReadOnlySpan<char> Unit3 = "0.###";
CultureInfo invariantCulture = Frame.invariantCulture;
CampaignID.TryFormat(buffer, out int pos, provider: invariantCulture);
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
Timestamp.TryFormat(buffer[pos..], out int charWritten, DateTimeFormat, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8;
pos++; // Next step: leave the 44 there.
ReadOnlySpan<byte> surveyType = SurveyTypeTranslator.ToSpan(SurveyType);
surveyType.CopyTo(buffer[pos..]);
pos += surveyType.Length;
buffer[pos++] = 44;//','u8;
pos++; // Next step: leave the 44 there.
WaterDepth.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8;
pos++; // Next step: leave the 44 there.
Longitude.TryFormat(buffer[pos..], out charWritten, "0.000000", invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8;
pos++; // Next step: leave the 44 there.
Latitude.TryFormat(buffer[pos..], out charWritten, "0.0000000", invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
GNSSAltitude.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
GNSSHeading.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
GNSSSpeed.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
MagneticHeading.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
MinRange.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
MaxRange.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
WaterTemperature.TryFormat(buffer[pos..], out charWritten, "0.#", invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
WaterSpeed.TryFormat(buffer[pos..], out charWritten, Unit3, invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
HardwareTime.TryFormat(buffer[pos..], out charWritten, provider: invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
ReadOnlySpan<byte> frequency = FrequencyTranslator.ToSpan(Frequency);
frequency.CopyTo(buffer[pos..]);
pos += frequency.Length;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
Milliseconds.TryFormat(buffer[pos..], out charWritten, provider: invariantCulture);
pos += charWritten;
buffer[pos++] = 44;//','u8
pos++; // Next step: leave the 44 there.
return buffer[..pos];
}

Expand Down
2 changes: 2 additions & 0 deletions Frequency.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace SL3Reader
{
Expand All @@ -11,6 +12,7 @@ public enum Frequency : ushort
}
public static class FrequencyTranslator
{
[SkipLocalsInit]
public static ReadOnlySpan<byte> ToSpan(Frequency frequency) => frequency switch
{
Frequency.Fq_200kHz => "Fq_200kHz"u8,
Expand Down
2 changes: 2 additions & 0 deletions GeoPoint.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

namespace SL3Reader
Expand All @@ -18,6 +19,7 @@ public override readonly string ToString()
return X.ToString("0.###", invariantCulture) + ',' +
Y.ToString("0.###", invariantCulture);
}
[SkipLocalsInit]
public readonly ReadOnlySpan<byte> Format(Span<byte> buffer, bool addNewLine = true)
{
CultureInfo invariantCulture = CultureInfo.InvariantCulture;
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static void PrintUsage()
[SkipLocalsInit]
static void PrintSummary([DisallowNull] SL3Reader sl3reader)
{
ReadOnlyDictionary<SurveyType, ReadOnlyCollection<nuint>> indexByType = sl3reader.IndexByType;
ReadOnlyDictionary<SurveyType, ReadOnlyCollection<nuint>> indexByType = sl3reader.FrameByType;
ReadOnlyCollection<nuint> frames = sl3reader.Frames;
int len = frames.Count;
System.Globalization.CultureInfo invariantCulture = System.Globalization.CultureInfo.InvariantCulture;
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": "\"F:\\Sonar2.sl3\" F:\\PS -ps"
"commandLineArgs": "\"F:\\Sonar2.sl3\" F:\\t3.csv -route"
}
}
}
16 changes: 8 additions & 8 deletions SL3Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SL3Reader : IDisposable
#region Public properties
public ReadOnlyCollection<nuint> Frames { get; }
public ReadOnlyCollection<GeoPoint> AugmentedCoordinates { get; }
public ReadOnlyDictionary<SurveyType, ReadOnlyCollection<nuint>> IndexByType { get; }
public ReadOnlyDictionary<SurveyType, ReadOnlyCollection<nuint>> FrameByType { get; }
#endregion End Public properties

#region Private variables
Expand Down Expand Up @@ -136,7 +136,7 @@ public unsafe SL3Reader([DisallowNull] string path)
Frames = frames.ToReadOnlyCollection();

// Populate major index
IndexByType = new SortedDictionary<SurveyType, ReadOnlyCollection<nuint>>()
FrameByType = new SortedDictionary<SurveyType, ReadOnlyCollection<nuint>>()
{
{ SurveyType.Primary, Primary.ToReadOnlyCollection() },
{ SurveyType.Secondary, Secondary.ToReadOnlyCollection() },
Expand Down Expand Up @@ -252,11 +252,11 @@ public unsafe void ExportRoutePoints(string path)

public unsafe void ExportImagery(string path, SurveyType surveyType = SurveyType.SideScan)
{
ArgumentNullException.ThrowIfNull(nameof(path));

ReadOnlyCollection<nuint> imageFrames = IndexByType[surveyType];
ReadOnlyCollection<nuint> imageFrames = FrameByType[surveyType];
if (imageFrames.Count < 1) return; // Return when no imagery exists.

ArgumentNullException.ThrowIfNull(nameof(path));

CultureInfo invariantCulture = CultureInfo.InvariantCulture;

List<int> breakpoints = GetBreakPoints(imageFrames, out int maxHeight);
Expand Down Expand Up @@ -290,6 +290,7 @@ public unsafe void ExportImagery(string path, SurveyType surveyType = SurveyType
// TODO: Remove intermediate solution:
GeoPoint firstStrip = AugmentedCoordinates[Frames.IndexOf(imageFrames[first])];
GeoPoint lastStrip = AugmentedCoordinates[Frames.IndexOf(imageFrames[final - 1])];

Frame* lastFrame = (Frame*)imageFrames[final - 1];

double XSize = -(lastStrip.Distance - firstStrip.Distance) / (final - first - 1);
Expand Down Expand Up @@ -361,7 +362,7 @@ static unsafe List<int> GetBreakPoints(ReadOnlyCollection<nuint> framesToCheck,

public unsafe void ExamineUnknown8Datasets()
{
ReadOnlyCollection<nuint> unknown8Frames = IndexByType[SurveyType.Unknown8];
ReadOnlyCollection<nuint> unknown8Frames = FrameByType[SurveyType.Unknown8];
int unknown8FrameCount = unknown8Frames.Count;
if (unknown8FrameCount < 1) return; // Return when no U8 exists

Expand All @@ -386,7 +387,7 @@ public unsafe void Export3D(string path, bool includeUnreliable = false, bool ma
const string doubleFormat = "0.####";
CultureInfo invariantCulture = CultureInfo.InvariantCulture;

ReadOnlyCollection<nuint> frames3D = IndexByType[SurveyType.ThreeDimensional];
ReadOnlyCollection<nuint> frames3D = FrameByType[SurveyType.ThreeDimensional];
int frames3DLength = frames3D.Count;
if (frames3DLength < 1) return;

Expand All @@ -412,7 +413,6 @@ public unsafe void Export3D(string path, bool includeUnreliable = false, bool ma
Frame* frame = (Frame*)frames3D[i];
ThreeDimensionalFrameHeader* header = (ThreeDimensionalFrameHeader*)((byte*)frame + frame->HeaderSize);

// TODO: Remove intermediate solution:
GeoPoint augmentedCoordinate = augmentedCoordinates[coordinate3DHelper[i]];
byte* measurements = (byte*)header + ThreeDimensionalFrameHeader.Size;

Expand Down
2 changes: 2 additions & 0 deletions SurveyType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace SL3Reader
{
Expand All @@ -13,6 +14,7 @@ public enum SurveyType : ushort
}
public static class SurveyTypeTranslator
{
[SkipLocalsInit]
public static ReadOnlySpan<byte> ToSpan(SurveyType surveyType) => surveyType switch
{
SurveyType.Primary => "Primary"u8,
Expand Down

0 comments on commit c8e20d3

Please sign in to comment.