Skip to content
This repository was archived by the owner on Oct 28, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions CPP/Clipper2Lib/src/clipper.engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,8 @@ namespace Clipper2Lib {
std::stable_sort(minima_list_.begin(), minima_list_.end(), LocMinSorter()); //#594
minima_list_sorted_ = true;
}
LocalMinimaList::const_reverse_iterator i;
for (i = minima_list_.rbegin(); i != minima_list_.rend(); ++i)
InsertScanline((*i)->vertex->pt.y);

// the local minima aren't added to scanline_list_ since they're already
// sorted, instead PopScanline() merges them with scanline_list_
current_locmin_iter_ = minima_list_.begin();
actives_ = nullptr;
sel_ = nullptr;
Expand Down Expand Up @@ -862,9 +860,18 @@ namespace Clipper2Lib {

bool ClipperBase::PopScanline(int64_t& y)
{
if (scanline_list_.empty()) return false;
// the next scanline is the larger of the next local minima's y (local
// minima are sorted by descending y) and the top of scanline_list_
bool has_locmin = current_locmin_iter_ != minima_list_.end();
if (scanline_list_.empty())
{
if (!has_locmin) return false;
y = (*current_locmin_iter_)->vertex->pt.y;
return true;
}
y = scanline_list_.top();
scanline_list_.pop();
if (has_locmin && (*current_locmin_iter_)->vertex->pt.y > y)
y = (*current_locmin_iter_)->vertex->pt.y;
while (!scanline_list_.empty() && y == scanline_list_.top())
scanline_list_.pop(); // Pop duplicates.
return true;
Expand Down
16 changes: 9 additions & 7 deletions CSharp/Clipper2Lib/Clipper.Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,8 @@ protected void Reset()
_isSortedMinimaList = true;
}

_scanlineList.EnsureCapacity(_minimaList.Count);
for (int i = _minimaList.Count - 1; i >= 0; i--)
_scanlineList.Add(_minimaList[i].vertex.pt.Y);

// the local minima aren't added to _scanlineList since they're already
// sorted, instead PopScanline() merges them with _scanlineList
_currentBotY = 0;
_currentLocMin = 0;
_actives = null;
Expand All @@ -812,15 +810,19 @@ private void InsertScanline(long y)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool PopScanline(out long y)
{
// the next scanline is the larger of the next local minima's y (local
// minima are sorted by descending y) and the last _scanlineList y
bool hasLocMin = _currentLocMin < _minimaList.Count;
int cnt = _scanlineList.Count - 1;
if (cnt < 0)
{
y = 0;
return false;
y = hasLocMin ? _minimaList[_currentLocMin].vertex.pt.Y : 0;
return hasLocMin;
}

y = _scanlineList[cnt];
_scanlineList.RemoveAt(cnt--);
if (hasLocMin && _minimaList[_currentLocMin].vertex.pt.Y > y)
y = _minimaList[_currentLocMin].vertex.pt.Y;
while (cnt >= 0 && y == _scanlineList[cnt])
_scanlineList.RemoveAt(cnt--);
return true;
Expand Down
32 changes: 19 additions & 13 deletions Delphi/Clipper2Lib/Clipper.Engine.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1403,13 +1403,10 @@ destructor TClipperBase.Destroy;
//------------------------------------------------------------------------------

function TClipperBase.ClearSolutionOnly: Boolean;
var
dummy: Int64;
begin
try
// in case of exceptions ...
DeleteEdges(FActives);
while assigned(FScanLine) do PopScanLine(dummy);
DisposeIntersectNodes;
DisposeScanLineList;
FOutRecList.Clear;
Expand All @@ -1433,17 +1430,15 @@ procedure TClipperBase.Clear;
//------------------------------------------------------------------------------

procedure TClipperBase.Reset;
var
i: Integer;
begin
if not FLocMinListSorted then
begin
FLocMinList.Sort(LocMinListSort);
FLocMinListSorted := true;
end;

for i := FLocMinList.Count -1 downto 0 do
InsertScanLine(PLocalMinima(FLocMinList.UnsafeGet(i)).vertex.pt.Y);
// the local minima aren't added to the scanline list since they're already
// sorted, instead PopScanLine merges them with the scanline list
FCurrentLocMinIdx := 0;
FActives := nil;
FSel := nil;
Expand Down Expand Up @@ -1495,10 +1490,10 @@ procedure TClipperBase.InsertScanLine(const Y: Int64);
begin
// The scanline list is a single-linked list of all the Y coordinates of
// subject and clip vertices in the clipping operation (sorted descending).
// However, only scanline Y's at Local Minima are inserted before clipping
// starts. While scanlines are removed sequentially during the sweep operation,
// new scanlines are only inserted whenever edge bounds are updated. This keeps
// the scanline list relatively short, optimising performance.
// However, the scanline Y's at Local Minima aren't inserted at all (they're
// merged in by PopScanLine), and while scanlines are removed sequentially
// during the sweep operation, new scanlines are only inserted whenever edge
// bounds are updated. This keeps the scanline list short, optimising performance.
if not Assigned(FScanLine) then
begin
new(newSl);
Expand Down Expand Up @@ -1528,9 +1523,20 @@ procedure TClipperBase.InsertScanLine(const Y: Int64);
function TClipperBase.PopScanLine(out Y: Int64): Boolean;
var
sl: PScanLine;
hasLocMin: Boolean;
begin
Result := assigned(FScanLine);
if not Result then Exit;
// the next scanline is the larger of the next local minima's Y (local
// minima are sorted by descending Y) and the first scanline list Y
hasLocMin := FCurrentLocMinIdx < FLocMinList.Count;
if hasLocMin then
Y := PLocalMinima(FLocMinList.UnsafeGet(FCurrentLocMinIdx)).vertex.pt.Y;
if not Assigned(FScanLine) then
begin
Result := hasLocMin;
Exit;
end;
Result := true;
if hasLocMin and (Y > FScanLine.y) then Exit;
Y := FScanLine.y;
sl := FScanLine;
FScanLine := FScanLine.next;
Expand Down