Skip to content

Commit

Permalink
Merge pull request #1064 from DmytroMuravskyi/dmuravskyi/adaptive-gri…
Browse files Browse the repository at this point in the history
…d-clone

Add AdaptiveGrid.Clone (#1064)
  • Loading branch information
wynged committed Dec 1, 2023
2 parents 27cda66 + c5a8577 commit 0f97b40
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- `Elements.Door`
- `ComponentBase.UseRepresentationInstances` - an option flag to make generating fitting models faster/smaller.
- `ContentConfiguration.AllowRotatation`
- `AdaptiveGrid.Clone`

### Fixed

Expand Down
38 changes: 37 additions & 1 deletion Elements/src/Spatial/AdaptiveGrid/AdaptiveGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,42 @@ public void InsertSnapshot(
}
}

/// <summary>
/// Perform deep cloning of the AdaptiveGrid.
/// New grid will have the same ids but can be edited independently.
/// </summary>
/// <returns>Cloned AdaptiveGrid</returns>
public AdaptiveGrid Clone()
{
AdaptiveGrid clone = new AdaptiveGrid(new Transform(Transform));
clone._edgeId = _edgeId;
clone._vertexId = _vertexId;

clone._edges = _edges.ToDictionary(
e => e.Key,
e => new Edge(e.Value.Id, e.Value.StartId, e.Value.EndId));

clone._vertices = _vertices.ToDictionary(
e => e.Key,
e => {
var v = new Vertex(e.Value.Id, e.Value.Point);
foreach (var edge in e.Value.Edges)
{
v.Edges.Add(clone._edges[edge.Id]);
}
return v;
});

clone._edgesLookup = _edgesLookup.ToDictionary(e => e.Key, e => e.Value);

clone._xyzLookup = _xyzLookup.ToDictionary(
xyz => xyz.Key, xyz => xyz.Value.ToDictionary(
yz => yz.Key, yz => yz.Value.ToDictionary(
z => z.Key, z => z.Value)));

return clone;
}

#endregion

#region Private logic
Expand Down Expand Up @@ -799,7 +835,7 @@ private Edge AddInsertEdge(ulong vertexId1, ulong vertexId2)
throw new ArgumentException("Can't create edge. End vertex id is not present in the grid.", $"{vertexId2}");
}

var edge = new Edge(this, this._edgeId, vertexId1, vertexId2);
var edge = new Edge(this._edgeId, vertexId1, vertexId2);
edgeId = edge.Id;

this._edgesLookup[hash] = edgeId;
Expand Down
2 changes: 1 addition & 1 deletion Elements/src/Spatial/AdaptiveGrid/Edge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Edge
/// </summary>
public ulong Id { get; internal set; }

internal Edge(AdaptiveGrid adaptiveGrid, ulong id, ulong vertexId1, ulong vertexId2)
internal Edge(ulong id, ulong vertexId1, ulong vertexId2)
{
Id = id;
this.SetVerticesFromIds(vertexId1, vertexId2);
Expand Down
31 changes: 31 additions & 0 deletions Elements/test/AdaptiveGridTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,37 @@ public void EdgeInfoFlagsTest()
Assert.True(horizontalEdgeInfo.HasAnyFlag(EdgeFlags.UserDefinedHint2D | EdgeFlags.UserDefinedHint3D));
}

[Fact]
public void AdaptiveGridClone()
{
var grid = SampleGrid();
var clone = grid.Clone();

grid.GetVertex(4).Point.IsAlmostEqualTo(new Vector3(5, 5));
clone.GetVertex(4).Point.IsAlmostEqualTo(new Vector3(5, 5));

grid.AddVertex(new Vector3(5, -5),
new ConnectVertexStrategy(grid.GetVertex(1), grid.GetVertex(3)), cut: false);
clone.RemoveVertex(clone.GetVertex(5));
clone.AddEdge(grid.GetVertex(2), grid.GetVertex(4));

Assert.True(grid.TryGetVertexIndex((5, 2), out var index));
Assert.False(clone.TryGetVertexIndex((5, 2), out index));

Assert.True(grid.TryGetVertexIndex((5, -5), out index));
Assert.False(clone.TryGetVertexIndex((5, -5), out index));

var v = grid.GetVertex(4);
Assert.Equal(2, grid.GetVertex(4).Edges.Count);
Assert.Null(v.GetEdge(2));

Assert.True(clone.TryGetVertexIndex((5, 5), out index));
Assert.Equal(4u, index);
v = clone.GetVertex(index);
Assert.Equal(3, v.Edges.Count);
Assert.NotNull(v.GetEdge(2));
}

// (4)
// / \
// / \
Expand Down

0 comments on commit 0f97b40

Please sign in to comment.