Skip to content

Commit 8e2d171

Browse files
Fix joining
1 parent 3142c01 commit 8e2d171

4 files changed

Lines changed: 17 additions & 25 deletions

File tree

‎src/PolygonClipper/Contour.cs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,19 @@ public void Clear()
254254
/// <summary>
255255
/// Creates a deep copy of this contour.
256256
/// </summary>
257-
/// <param name="contourIndexOffset">
258-
/// Offset applied to parent and hole contour indices so copied topology remains valid
259-
/// when inserted into another polygon.
260-
/// </param>
261257
/// <returns>A detached contour copy.</returns>
262-
internal Contour DeepClone(int contourIndexOffset = 0)
258+
public Contour DeepClone()
263259
{
264260
Contour clone = new(this.vertices.Count)
265261
{
266-
ParentIndex = this.ParentIndex is int parentIndex ? parentIndex + contourIndexOffset : null,
262+
ParentIndex = this.ParentIndex,
267263
Depth = this.Depth,
268264
hasCachedOrientation = this.hasCachedOrientation,
269265
cachedCounterClockwise = this.cachedCounterClockwise
270266
};
271267

272268
clone.vertices.AddRange(this.vertices);
273-
for (int i = 0; i < this.holeIndices.Count; i++)
274-
{
275-
clone.holeIndices.Add(this.holeIndices[i] + contourIndexOffset);
276-
}
269+
clone.holeIndices.AddRange(this.holeIndices);
277270

278271
return clone;
279272
}

‎src/PolygonClipper/Polygon.cs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,17 @@ public Contour this[int index]
7676
/// <param name="polygon">The polygon to join.</param>
7777
public void Join(Polygon polygon)
7878
{
79-
ArgumentNullException.ThrowIfNull(polygon);
80-
81-
int sourceCount = polygon.contours.Count;
82-
int contourIndexOffset = this.contours.Count;
83-
for (int i = 0; i < sourceCount; ++i)
79+
int size = this.Count;
80+
for (int i = 0; i < polygon.contours.Count; ++i)
8481
{
85-
this.contours.Add(polygon.contours[i].DeepClone(contourIndexOffset));
82+
Contour contour = polygon.contours[i];
83+
this.Add(contour);
84+
this.GetLastContour().ClearHoles();
85+
86+
for (int j = 0; j < contour.HoleCount; ++j)
87+
{
88+
this.GetLastContour().AddHoleIndex(contour.GetHoleIndex(j) + size);
89+
}
8690
}
8791
}
8892

@@ -141,7 +145,7 @@ public void Translate(double x, double y)
141145
/// Creates a deep copy of this polygon and all of its contours.
142146
/// </summary>
143147
/// <returns>A detached polygon copy.</returns>
144-
internal Polygon DeepClone()
148+
public Polygon DeepClone()
145149
{
146150
Polygon clone = new(this.contours.Count);
147151
for (int i = 0; i < this.contours.Count; i++)

‎src/PolygonClipper/PolygonClipper.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ private static bool TryTrivialOperationForNonOverlappingBoundingBoxes(
512512
if (operation is BooleanOperation.Union or BooleanOperation.Xor)
513513
{
514514
result = new(subject.Count + clipping.Count);
515-
result.Join(subject);
516-
result.Join(clipping);
515+
result.Join(subject.DeepClone());
516+
result.Join(clipping.DeepClone());
517517
return true;
518518
}
519519
}

‎src/PolygonClipper/PolygonStroker.cs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,7 @@ public Polygon Stroke(Polygon polygon)
229229
Polygon stroked = this.ProcessPathToPolygon(contour, isClosed);
230230
if (stroked.Count > 0)
231231
{
232-
// Stroked contours are produced as fresh objects; append by reference
233-
// here to avoid an unnecessary clone step in the hot path.
234-
for (int j = 0; j < stroked.Count; j++)
235-
{
236-
allContours.Add(stroked[j]);
237-
}
232+
allContours.Join(stroked);
238233
}
239234
}
240235

0 commit comments

Comments
 (0)