Skip to content

Commit

Permalink
Merge pull request #35 from Digital-Production-Aachen/fixOVFMergingBug
Browse files Browse the repository at this point in the history
Bug fixes and stability improvements for ovf streaming merger
  • Loading branch information
SebastianDirks authored Apr 25, 2024
2 parents defe36a + e815160 commit 71f6d6e
Show file tree
Hide file tree
Showing 19 changed files with 384 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,21 @@ private MarkingParams TranslateBuildParams(IModelSectionParams iltParams)
private Job.Types.JobMetaData TranslateMetaData(ICLIFile section)
{
Job.Types.JobMetaData metaData = new Job.Types.JobMetaData();
if (section.Header.Date != 0)
if (section.Header.Date > 0)
{
//assuming that the date is after the year 2000, since there is no information about it
int year = (section.Header.Date % 100) + 2000;
int month = (section.Header.Date / 100) % 100;
int day = section.Header.Date % 100;
DateTime date = new DateTime(year, month, day);
metaData.JobCreationTime = new DateTimeOffset(date).ToUnixTimeSeconds();
try
{
DateTime date = new DateTime(year, month, day);
metaData.JobCreationTime = new DateTimeOffset(date).ToUnixTimeSeconds();
}
catch (System.ArgumentOutOfRangeException ex)

Check warning on line 657 in ReaderWriter/3rdPartyFormatAdapters/CLI_ILT/ILTFileReaderAdapter/ILTFileReaderAdapter.cs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest)

The variable 'ex' is declared but never used

Check warning on line 657 in ReaderWriter/3rdPartyFormatAdapters/CLI_ILT/ILTFileReaderAdapter/ILTFileReaderAdapter.cs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest)

The variable 'ex' is declared but never used

Check warning on line 657 in ReaderWriter/3rdPartyFormatAdapters/CLI_ILT/ILTFileReaderAdapter/ILTFileReaderAdapter.cs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest)

The variable 'ex' is declared but never used

Check warning on line 657 in ReaderWriter/3rdPartyFormatAdapters/CLI_ILT/ILTFileReaderAdapter/ILTFileReaderAdapter.cs

View workflow job for this annotation

GitHub Actions / publish_nuget

The variable 'ex' is declared but never used
{
// ignore invalid dates
};
}
metaData.Version = (ulong)section.Header.Version;
return metaData;
Expand Down
22 changes: 22 additions & 0 deletions ReaderWriter/FileReaderWriterFactory/FileWriterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ You should have received a copy of the GNU Lesser General Public

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenVectorFormat.AbstractReaderWriter;

namespace OpenVectorFormat.FileReaderWriterFactory
Expand Down Expand Up @@ -60,5 +62,25 @@ public static List<string> SupportedFileFormats
return formats;
}
}

/// <summary>
/// Utility for writing the contents of a file reader to disk.
/// </summary>
/// <param name="fileToWrite"></param>
/// <param name="targetFile"></param>
/// <returns></returns>
public static async Task StreamToFile(FileReader fileToWrite, string targetFile)
{
var fileInfo = new FileInfo(targetFile);
using (var writer = CreateNewWriter(fileInfo.Extension))
{
writer.StartWritePartial(fileToWrite.JobShell, targetFile, null);
for (int i = 0; i < fileToWrite.JobShell.NumWorkPlanes; i++)
{
var wp = fileToWrite.GetWorkPlaneShell(i);
await writer.AppendWorkPlaneAsync(wp);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.55.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.62.0" />

<!-- Explicitly include our proto file by adding this line: -->
<Protobuf Include="../../ReaderWriter/AbstractReaderWriter/grpc_reader_writer_interface.proto" GrpcServices="Both" Link="Protos/grpc_reader_writer_interface.proto" ProtoRoot="../../" />
Expand Down
4 changes: 2 additions & 2 deletions ReaderWriter/OVFDefinition/OVFDefinition.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
<PackageReference Include="Grpc.Tools" Version="2.56.0">
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
<PackageReference Include="Grpc.Tools" Version="2.62.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
38 changes: 38 additions & 0 deletions ReaderWriter/OVFDefinition/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
---- Copyright Start ----
This file is part of the OpenVectorFormatTools collection. This collection provides tools to facilitate the usage of the OpenVectorFormat.
Copyright (C) 2023 Digital-Production-Aachen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
---- Copyright End ----
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace OVFDefinition
{
public class Utils
{
public static bool ApproxEquals(float value1, float value2, float tolerance = 1e-6f)
{
return Math.Abs(value1 - value2) <= tolerance;
}
}
}
16 changes: 8 additions & 8 deletions ReaderWriter/OVFDefinition/VectorBlockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ You should have received a copy of the GNU Lesser General Public

using Google.Protobuf.Collections;
using OpenVectorFormat.Utils;
using OVFDefinition;
using OVFDefinition;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing;
using System.Linq;
using System.Numerics;
using static OpenVectorFormat.SIMDVectorOperations;
Expand Down Expand Up @@ -340,14 +340,14 @@ public static void StoreVectorBlockBoundsInMetaData(this VectorBlock vectorBlock
vectorBlock.MetaData.Bounds = vectorBlock.Bounds2D();
}

public static void SetDisplayColor(this VectorBlock.Types.VectorBlockMetaData metaData, Color color)
{
metaData.DisplayColor = ColorConversions.ColorToInt(color);
public static void SetDisplayColor(this VectorBlock.Types.VectorBlockMetaData metaData, Color color)
{
metaData.DisplayColor = ColorConversions.ColorToInt(color);
}

public static Color GetDisplayColor(this VectorBlock.Types.VectorBlockMetaData metaData)
{
return ColorConversions.IntToColor(metaData.DisplayColor);
public static Color GetDisplayColor(this VectorBlock.Types.VectorBlockMetaData metaData)
{
return ColorConversions.IntToColor(metaData.DisplayColor);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions ReaderWriter/OVFReaderWriter/OVFReaderWriter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
<PackageReference Include="Grpc.Tools" Version="2.56.0">
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
<PackageReference Include="Grpc.Tools" Version="2.62.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Binary file not shown.
Binary file added ReaderWriter/UnitTests/TestFiles/bunny.ovf
Binary file not shown.
19 changes: 19 additions & 0 deletions ReaderWriter/UnitTests/TestOVF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ You should have received a copy of the GNU Lesser General Public
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenVectorFormat.AbstractReaderWriter;
using OpenVectorFormat.OVFReaderWriter;
using System;
using System.IO;
using System.Threading.Tasks;

namespace OpenVectorFormat.ReaderWriter.UnitTests
Expand Down Expand Up @@ -106,6 +108,23 @@ public async Task TestAsyncWriteReadAsync()
testReader.Dispose();
}

// DEBUGGING
//[TestMethod]
//public void TestWPMetaDataNull()
//{
// var workPlane = new WorkPlane();
// workPlane.MetaData = new WorkPlane.Types.WorkPlaneMetaData();
// workPlane.MetaData.Bounds = null;
// var jobShell = new Job();
// jobShell.JobMetaData = new Job.Types.JobMetaData();
// jobShell.JobMetaData.Bounds = null;
// using (var writer = new OVFFileWriter())
// {
// writer.StartWritePartial(jobShell, Path.GetTempPath() + "TestWPMetaDataNull.ovf");
// writer.AppendWorkPlaneAsync(workPlane);
// }
//}

private Job SetupTestJob()
{
OpenVectorFormat.Job job = new OpenVectorFormat.Job();
Expand Down
159 changes: 159 additions & 0 deletions ReaderWriter/UnitTests/TestOVFStreaming.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
---- Copyright Start ----
This file is part of the OpenVectorFormatTools collection. This collection provides tools to facilitate the usage of the OpenVectorFormat.
Copyright (C) 2023 Digital-Production-Aachen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
---- Copyright End ----
*/

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenVectorFormat;
using OpenVectorFormat.OVFReaderWriter;
using OpenVectorFormat.Plausibility;
using OpenVectorFormat.Streaming;
using System;
using System.IO;
using System.Linq;
using System.Numerics;


namespace UnitTests
{
[TestClass]
public class TestOVFStreaming
{

/// <summary>
/// Test OVFStreamingMerger by merging one part and support and applying the Plausibilitychecker.
/// </summary>
[TestMethod]
public void TestMergePartConfig()
{
string sourceDir = new[] {"..", "..", "..", "TestFiles"}.Aggregate(Path.Combine);
string partFile = "bunny";
string supportFile = "bunny (solidsupport)";

using (OVFFileReader partReader = new OVFFileReader())
using (OVFFileReader supportReader = new OVFFileReader())
{
// load part and support ovf
partReader.OpenJobAsync(Path.Combine(sourceDir, partFile) + ".ovf", null).GetAwaiter().GetResult();
supportReader.OpenJobAsync(Path.Combine(sourceDir, supportFile) + ".ovf", null).GetAwaiter().GetResult();

// run plausibility checks on input
PlausibilityChecker.CheckJob(partReader.CacheJobToMemoryAsync().GetAwaiter().GetResult(), new CheckerConfig())
.GetAwaiter().GetResult();
PlausibilityChecker.CheckJob(supportReader.CacheJobToMemoryAsync().GetAwaiter().GetResult(), new CheckerConfig())
.GetAwaiter().GetResult();

// merge part with supports
var merger = new OVFStreamingMerger(partReader);
merger.AddFileReaderToMerge(new FileReaderToMerge() { fr = supportReader, markAsSupport = true });

// run plausibility checks on result
var job = merger.CacheJobToMemoryAsync().GetAwaiter().GetResult();
PlausibilityChecker.CheckJob(job, new CheckerConfig()).GetAwaiter().GetResult();
}
}

/// <summary>
/// Test OVFStreamingMerger by merging several instances of the same part together and
/// applying the Plausibilitychecker.
/// </summary>
[TestMethod]
public void TestMergeInstances()
{
string sourceDir = new[] { "..", "..", "..", "TestFiles" }.Aggregate(Path.Combine);
string partFile = "bunny";
string supportFile = "bunny (solidsupport)";
(float x, float y, float rot)[] positions = new (float, float, float)[]
{
(100, 0, 0),
(200, 100, (float)Math.PI/2),
(300, 0, 0),
};

using (OVFFileReader partReader = new OVFFileReader())
using (OVFFileReader supportReader = new OVFFileReader())
{
// load part and support ovf
partReader.OpenJobAsync(Path.Combine(sourceDir, partFile) + ".ovf", null).GetAwaiter().GetResult();
supportReader.OpenJobAsync(Path.Combine(sourceDir, supportFile) + ".ovf", null).GetAwaiter().GetResult();

// run plausibility checks on input
PlausibilityChecker.CheckJob(partReader.CacheJobToMemoryAsync().GetAwaiter().GetResult(), new CheckerConfig())
.GetAwaiter().GetResult();
PlausibilityChecker.CheckJob(supportReader.CacheJobToMemoryAsync().GetAwaiter().GetResult(), new CheckerConfig())
.GetAwaiter().GetResult();

// merge part with supports
var partSupportReader = new OVFStreamingMerger(partReader);
partSupportReader.AddFileReaderToMerge(new FileReaderToMerge() { fr = supportReader, markAsSupport = true });

// merge parts at different positions
OVFStreamingMerger jobMerger = null;
for (int i = 0; i < positions.Length; i++)
{
var pos = positions[i];
var toMerge = new FileReaderToMerge()
{
fr = partSupportReader,
translationX = pos.x,
translationY = pos.y,
rotationInRad = pos.rot,
markAsSupport = false
};
if (i == 0) jobMerger = new OVFStreamingMerger(toMerge);
else if (i > 0) jobMerger?.AddFileReaderToMerge(toMerge);
}

// run plausibility checks on result
var job = jobMerger.CacheJobToMemoryAsync().GetAwaiter().GetResult();
PlausibilityChecker.CheckJob(job, new CheckerConfig()).GetAwaiter().GetResult();

// check layer heights
jobMerger.JobShell.NumWorkPlanes.Should().Be(partSupportReader.JobShell.NumWorkPlanes);

// check bounding boxes
for (int wpnr = 0; wpnr < jobMerger.JobShell.NumWorkPlanes; wpnr++)
{
// aabb of full job should equal aggregated aabb of all instances
var jobAABB = jobMerger.GetWorkPlaneAsync(wpnr).GetAwaiter().GetResult().Bounds2D();
AxisAlignedBox2D aggregateAABB = null;
for (int i = 0; i < positions.Length; i++)
{
var pos = positions[i];
var wp = partSupportReader.GetWorkPlaneAsync(wpnr).GetAwaiter().GetResult();
var wpCopy = wp.Clone();
wpCopy.Rotate(pos.rot);
wpCopy.Translate(new Vector2(pos.x, pos.y));
var aabb = wpCopy.Bounds2D();
if (i == 0) aggregateAABB = aabb;
else aggregateAABB.Contain(aabb);
}
jobAABB.XMin.Should().BeApproximately(aggregateAABB.XMin, 1e-6f);
jobAABB.XMax.Should().BeApproximately(aggregateAABB.XMax, 1e-6f);
jobAABB.YMin.Should().BeApproximately(aggregateAABB.YMin, 1e-6f);
jobAABB.YMax.Should().BeApproximately(aggregateAABB.YMax, 1e-6f);
}
}
}
}
}
Loading

0 comments on commit 71f6d6e

Please sign in to comment.