Skip to content

Commit

Permalink
PointSet.PartIndexRange (+tests and serialization)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanmaierhofer committed Sep 19, 2023
1 parent fb48402 commit 33fdf24
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 18 deletions.
87 changes: 76 additions & 11 deletions src/Aardvark.Algodat.Tests/PointSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public class PointSetTests
internal static Storage CreateStorage()
{
var x = new SimpleMemoryStore();
Action<string, object, Func<byte[]>> add = (name, value, create) => x.Add(name, create());
void add(string name, object value, Func<byte[]> create) => x.Add(name, create());
return new Storage(add, x.Get, x.GetSlice, x.Remove, x.Dispose, x.Flush, cache: default);
}

internal static Storage CreateDiskStorage(string dbDiskLocation)
{
var x = new SimpleDiskStore(dbDiskLocation);
Action<string, object, Func<byte[]>> add = (name, value, create) => x.Add(name, create());
void add(string name, object value, Func<byte[]> create) => x.Add(name, create());
return new Storage(add, x.Get, x.GetSlice, x.Remove, x.Dispose, x.Flush, cache: default);
}

Expand All @@ -56,7 +56,7 @@ public void CanCreateEmptyPointSet()
public void CanCreatePointSetFromSinglePoint()
{
var store = CreateStorage();
var ps = new List<V3d> { new V3d(0.1, 0.2, 0.3) };
var ps = new List<V3d> { new(0.1, 0.2, 0.3) };
var cs = new List<C4b> { C4b.White };
var pointset = PointSet.Create(
store, "id", ps, cs, null, null, null, 1000,
Expand All @@ -72,7 +72,7 @@ public void CanCreatePointSetFromSinglePoint()
[Test]
public void CanCreateInMemoryPointSet()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var ns = new List<V3f> { V3f.ZAxis };
var js = new List<int> { 123 };
Expand All @@ -83,7 +83,7 @@ public void CanCreateInMemoryPointSet()
[Test]
public void CanCreateInMemoryPointSetWithoutColors()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var ns = new List<V3f> { V3f.ZAxis };
var js = new List<int> { 123 };
var ks = new List<byte> { 42 };
Expand All @@ -93,7 +93,7 @@ public void CanCreateInMemoryPointSetWithoutColors()
[Test]
public void CanCreateInMemoryPointSetWithoutNormals()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var js = new List<int> { 123 };
var ks = new List<byte> { 42 };
Expand All @@ -103,7 +103,7 @@ public void CanCreateInMemoryPointSetWithoutNormals()
[Test]
public void CanCreateInMemoryPointSetWithoutIntensities()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var ns = new List<V3f> { V3f.ZAxis };
var ks = new List<byte> { 42 };
Expand All @@ -113,7 +113,7 @@ public void CanCreateInMemoryPointSetWithoutIntensities()
[Test]
public void CanCreateInMemoryPointSetWithoutClassifications()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var ns = new List<V3f> { V3f.ZAxis };
var js = new List<int> { 123 };
Expand Down Expand Up @@ -160,7 +160,7 @@ public void PointSetAttributes_EmptyPointSet()
[Test]
public void PointSetAttributes_All()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var ns = new List<V3f> { V3f.ZAxis };
var js = new List<int> { 123 };
Expand All @@ -179,7 +179,7 @@ public void PointSetAttributes_All()
[Test]
public void PointSetAttributes_NoLod()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var ns = new List<V3f> { V3f.ZAxis };
var js = new List<int> { 123 };
Expand All @@ -198,7 +198,7 @@ public void PointSetAttributes_NoLod()
[Test]
public void PointSetAttributes_PositionsAndColors()
{
var ps = new List<V3d> { new V3d(0.5, 0.5, 0.5) };
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var cs = new List<C4b> { C4b.White };
var storage = PointCloud.CreateInMemoryStore(cache: default);
var pointset = PointSet.Create(storage, "test", ps, cs, null, null, null, 1, generateLod: true, isTemporaryImportNode: true, default);
Expand All @@ -208,5 +208,70 @@ public void PointSetAttributes_PositionsAndColors()
Assert.IsTrue(pointset.HasNormals == true);
Assert.IsTrue(pointset.HasPositions == true);
}

[Test]
public void PointSet_PartIndexRange_No()
{
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var storage = PointCloud.CreateInMemoryStore(cache: default);
var pointset = PointSet.Create(storage, "test", ps, null, null, null, null, 1, generateLod: false, isTemporaryImportNode: true, default);
Assert.IsTrue(pointset.PartIndexRange == Range1i.Invalid);
Assert.IsTrue(pointset.HasPartIndexRange == false);
}

[Test]
public void PointSet_PartIndexRange()
{
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var storage = PointCloud.CreateInMemoryStore(cache: default);
var pointset = PointSet.Create(
storage, "test", ps, null, null, null, null, 1, generateLod: false, isTemporaryImportNode: true
)
.WithPartIndexRange(new(7, 11))
;
Assert.IsTrue(pointset.PartIndexRange == new Range1i(7, 11));
Assert.IsTrue(pointset.HasPartIndexRange == true);
}

[Test]
public void PointSet_PartIndexRange_Serialization()
{
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var storage = PointCloud.CreateInMemoryStore(cache: default);

var pointset = PointSet.Create(
storage, "test", ps, null, null, null, null, 1, generateLod: false, isTemporaryImportNode: true
)
.WithPartIndexRange(new(7, 11))
;

var json = pointset.ToJson();
var reloaded = PointSet.Parse(json, storage);

Assert.IsTrue(pointset.Id == reloaded.Id);
Assert.IsTrue(pointset.SplitLimit == reloaded.SplitLimit);
Assert.IsTrue(pointset.Root.Value.Id == reloaded.Root.Value.Id);
Assert.IsTrue(pointset.PartIndexRange == reloaded.PartIndexRange);
}

[Test]
public void PointSet_PartIndexRange_Serialization_NoRange()
{
var ps = new List<V3d> { new(0.5, 0.5, 0.5) };
var storage = PointCloud.CreateInMemoryStore(cache: default);

var pointset = PointSet.Create(
storage, "test", ps, null, null, null, null, 1, generateLod: false, isTemporaryImportNode: true
);

var json = pointset.ToJson();
var reloaded = PointSet.Parse(json, storage);

Assert.IsTrue(pointset.Id == reloaded.Id);
Assert.IsTrue(pointset.SplitLimit == reloaded.SplitLimit);
Assert.IsTrue(pointset.Root.Value.Id == reloaded.Root.Value.Id);
Assert.IsTrue(pointset.PartIndexRange == reloaded.PartIndexRange);
Assert.IsTrue(reloaded.PartIndexRange.IsInvalid);
}
}
}
37 changes: 30 additions & 7 deletions src/Aardvark.Geometry.PointSet/Octrees/PointSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class PointSet
/// </summary>
public static PointSet Create(Storage storage, string key,
IList<V3d> positions, IList<C4b> colors, IList<V3f> normals, IList<int> intensities, IList<byte> classifications,
int octreeSplitLimit, bool generateLod, bool isTemporaryImportNode, CancellationToken ct
int octreeSplitLimit, bool generateLod, bool isTemporaryImportNode, CancellationToken ct = default
)
{
if (key == null) throw new ArgumentNullException(nameof(key));
Expand Down Expand Up @@ -108,21 +108,30 @@ public PointSet(Storage storage, string key)
SplitLimit = 0;
}

private PointSet()
{
}

#endregion

#region Properties (state to serialize)

/// <summary>
/// </summary>
public string Id { get; }
public string Id { get; init; }

/// <summary>
/// </summary>
public int SplitLimit { get; }
public int SplitLimit { get; init; }

/// <summary>
/// </summary>
public PersistentRef<IPointCloudNode> Root { get; }
public PersistentRef<IPointCloudNode> Root { get; init; }

/// <summary>
/// Range (inclusive) of part indices, or invalid range if no part indices are stored.
/// </summary>
public Range1i PartIndexRange { get; init; } = Range1i.Invalid;

#endregion

Expand All @@ -135,7 +144,8 @@ public JsonNode ToJson() => JsonSerializer.SerializeToNode(new
Id,
RootCellId = Root?.Id,
OctreeId = Root?.Id,
SplitLimit
SplitLimit,
PartIndexRange
});

/// <summary>
Expand All @@ -156,8 +166,15 @@ public static PointSet Parse(JsonNode json, Storage storage)
// id
var id = (string)o["Id"];

// part index range (JsonArray)
var partIndexRangeArray = (JsonArray)o["PartIndexRange"];
var partIndexRange = partIndexRangeArray != null
? new Range1i((int)partIndexRangeArray[0], (int)partIndexRangeArray[1])
: Range1i.Invalid
;

//
return new PointSet(storage, id, octree?.Value ?? PointSetNode.Empty, splitLimit);
return new PointSet(storage, id, octree?.Value ?? PointSetNode.Empty, splitLimit).WithPartIndexRange(partIndexRange);
}

#endregion
Expand All @@ -167,7 +184,7 @@ public static PointSet Parse(JsonNode json, Storage storage)
/// <summary>
/// </summary>
[JsonIgnore]
public readonly Storage Storage;
public Storage Storage { get; init; }

/// <summary>
/// Returns true if pointset is empty.
Expand Down Expand Up @@ -220,6 +237,10 @@ public Box3d BoundingBox
/// <summary></summary>
public bool HasPositions => Root != null && Root.Value.HasPositions;


/// <summary></summary>
public bool HasPartIndexRange => PartIndexRange.IsValid;

#endregion

#region Immutable operations
Expand All @@ -244,6 +265,8 @@ public PointSet Merge(PointSet other, Action<long> pointsMergedCallback, ImportC
}
}

public PointSet WithPartIndexRange(Range1i x) => new() { Id = Id, PartIndexRange = x, Root = Root, SplitLimit = SplitLimit, Storage = Storage };

#endregion
}
}

0 comments on commit 33fdf24

Please sign in to comment.