Skip to content

Commit

Permalink
Refactoring I.: Intermediate results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Halmai Ákos authored and Halmai Ákos committed Jun 17, 2023
1 parent 4dd4b7c commit bcbe442
Showing 1 changed file with 42 additions and 55 deletions.
97 changes: 42 additions & 55 deletions SL3Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,26 @@
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO.MemoryMappedFiles;
using System.Collections.ObjectModel;

namespace SL3Reader
{

[DebuggerDisplay("{Name}")]
public class SL3Reader : IDisposable
{
#region Frame support
public List<nuint> Frames { get; }
#endregion Frame support
// TODO: Integrate into the constructor:
#region Augmented Coordinates
private List<GeoPoint> augmentedCoordinates;

public List<GeoPoint> AugmentedCoordinates => augmentedCoordinates ??= CreateNewCoordinateList();
private List<GeoPoint> CreateNewCoordinateList()
{
const long averageFrameSize = 2118L; // Empirically set value to avoid frequent resize of the underlying array.
return new((int)(Length / averageFrameSize));
}
#endregion Augmented Coordinates
#region Public properties
public ReadOnlyCollection<nuint> Frames { get; }
public ReadOnlyCollection<GeoPoint> AugmentedCoordinates { get; }
public ReadOnlyDictionary<SurveyType, ReadOnlyCollection<nuint>> IndexByType { get; }
#endregion End Public properties

#region Memory Mapped File Tools
#region Private variables
private readonly MemoryMappedFile memoryMappedFile;
private readonly MemoryMappedViewAccessor viewAccessor;
private readonly SafeMemoryMappedViewHandle viewHandle;
#endregion Memory Mapped File Tools

#region Indices
public SortedDictionary<SurveyType, List<nuint>> IndexByType { get; }
#endregion End Private variables

#endregion Indices
[SkipLocalsInit]
public unsafe SL3Reader(string path)
{
Expand All @@ -62,21 +50,21 @@ public unsafe SL3Reader(string path)
// Init Frames
const long averageFrameSize = 2118L; // Empirically set value to avoid frequent resize of the underlying array.
int estimatedCount = (int)(len / averageFrameSize);
Frames = new(estimatedCount);
ReadOnlyCollectionBuilder<nuint> frames = new(estimatedCount);

// Init index lists
List<nuint> Primary = new(estimatedCount / 8),
Secondary = new(estimatedCount / 8),
DownScan = new(estimatedCount / 10),
LeftSidescan = new(),
RightSidescan = new(),
SideScan = new(estimatedCount / 10),
Unknown6 = new(),
Unknown7 = new(estimatedCount / 4),
Unknown8 = new(estimatedCount / 4),
ThreeDimensional = new(estimatedCount / 10),
DebugDigital = new(),
DebugNoise = new();
ReadOnlyCollectionBuilder<nuint> Primary = new(estimatedCount / 8),
Secondary = new(estimatedCount / 8),
DownScan = new(estimatedCount / 10),
LeftSidescan = new(),
RightSidescan = new(),
SideScan = new(estimatedCount / 10),
Unknown6 = new(),
Unknown7 = new(estimatedCount / 4),
Unknown8 = new(estimatedCount / 4),
ThreeDimensional = new(estimatedCount / 10),
DebugDigital = new(),
DebugNoise = new();

// Init time
Frame* currentFrame = (Frame*)ptr; // Transfer to static private constructor!
Expand All @@ -85,22 +73,24 @@ public unsafe SL3Reader(string path)
// Load frames


Frames = frames.ToReadOnlyCollection();

// Populate major index
IndexByType = new()
IndexByType = new SortedDictionary<SurveyType, ReadOnlyCollection<nuint>>()
{
{ SurveyType.Primary, Primary },
{ SurveyType.Secondary, Secondary },
{ SurveyType.DownScan, DownScan },
{ SurveyType.LeftSidescan, LeftSidescan },
{ SurveyType. RightSidescan, RightSidescan },
{ SurveyType.SideScan, SideScan },
{ SurveyType.Unknown6, Unknown6 },
{ SurveyType.Unknown7, Unknown7 },
{ SurveyType.Unknown8, Unknown8 },
{ SurveyType.ThreeDimensional, ThreeDimensional },
{ SurveyType.DebugDigital, DebugDigital },
{ SurveyType.DebugNoise, DebugNoise }
};
{ SurveyType.Primary, Primary.ToReadOnlyCollection() },
{ SurveyType.Secondary, Secondary.ToReadOnlyCollection() },
{ SurveyType.DownScan, DownScan.ToReadOnlyCollection() },
{ SurveyType.LeftSidescan, LeftSidescan.ToReadOnlyCollection() },
{ SurveyType. RightSidescan, RightSidescan.ToReadOnlyCollection() },
{ SurveyType.SideScan, SideScan.ToReadOnlyCollection() },
{ SurveyType.Unknown6, Unknown6.ToReadOnlyCollection() },
{ SurveyType.Unknown7, Unknown7.ToReadOnlyCollection() },
{ SurveyType.Unknown8, Unknown8.ToReadOnlyCollection() },
{ SurveyType.ThreeDimensional, ThreeDimensional.ToReadOnlyCollection() },
{ SurveyType.DebugDigital, DebugDigital.ToReadOnlyCollection() },
{ SurveyType.DebugNoise, DebugNoise.ToReadOnlyCollection() }
}.AsReadOnly();

AugmentTrajectory();
}
Expand Down Expand Up @@ -164,21 +154,18 @@ private List<int> GetBreakPoints(List<int> framesToCheck, out int contiguousLeng
return breakpoints;
}

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

if (Directory.Exists(path!))
Directory.Delete(path!, true);
path = Directory.CreateDirectory(path!).FullName;

IReadOnlyList<IFrame> frames = Frames; // Initialize the frames.
int frameCount = frames.Count;
if (frameCount < 1) return;

List<int> imageFrames = IndexByType[surveyType];
if (imageFrames.Count < 1) return; // Return when no sidescan exists
int numberOfColumns = (int)frames[imageFrames[0]].LengthOfEchoData;
ReadOnlyCollection<nuint> imageFrames = IndexByType[surveyType];
if (imageFrames.Count < 1) return; // Return when no imagery exists.

uint numberOfColumns = ((Frame*)imageFrames[0])->LengthOfEchoData;
string prefix = GetPrefix(surveyType);

List<int> breakpoints = GetBreakPoints(imageFrames, out int maxHeight);
Expand Down

0 comments on commit bcbe442

Please sign in to comment.