Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made a fix that worked for me to stop Stackoverflow exception. Replac… #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
88 changes: 51 additions & 37 deletions CSG/Classes/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,45 +86,59 @@ public void Invert()
// (no heuristic is used to pick a good split).
public void Build(List<Polygon> list)
{
if (list.Count < 1)
return;
Stack<(Node, List<Polygon>)> buildStack = new Stack<(Node, List<Polygon>)>();
buildStack.Push((this, list));

bool newNode = plane == null || !plane.Valid();

if (newNode)
{
plane = new Plane();
plane.normal = list[0].plane.normal;
plane.w = list[0].plane.w;
}

if (polygons == null)
polygons = new List<Polygon>();

var listFront = new List<Polygon>();
var listBack = new List<Polygon>();

for (int i = 0; i < list.Count; i++)
plane.SplitPolygon(list[i], polygons, polygons, listFront, listBack);


if (listFront.Count > 0)
{
// SplitPolygon can fail to correctly identify coplanar planes when the epsilon value is too low. When
// this happens, the front or back list will be filled and built into a new node recursively. This
// check catches that case and sorts the front/back lists into the coplanar polygons collection.
if (newNode && list.SequenceEqual(listFront))
polygons.AddRange(listFront);
else
(front ?? (front = new Node())).Build(listFront);
}

if (listBack.Count > 0)
while (buildStack.Count > 0)
{
if (newNode && list.SequenceEqual(listBack))
polygons.AddRange(listBack);
else
(back ?? (back = new Node())).Build(listBack);
var current = buildStack.Pop();
Node currentNode = current.Item1;
List<Polygon> currentList = current.Item2;

if (currentList.Count < 1)
continue;

bool newNode = currentNode.plane == null || !currentNode.plane.Valid();

if (newNode)
{
currentNode.plane = new Plane();
currentNode.plane.normal = currentList[0].plane.normal;
currentNode.plane.w = currentList[0].plane.w;
}

if (currentNode.polygons == null)
currentNode.polygons = new List<Polygon>();

var listFront = new List<Polygon>();
var listBack = new List<Polygon>();

for (int i = 0; i < currentList.Count; i++)
currentNode.plane.SplitPolygon(currentList[i], currentNode.polygons, currentNode.polygons, listFront, listBack);

if (listFront.Count > 0)
{
if (newNode && currentList.SequenceEqual(listFront))
currentNode.polygons.AddRange(listFront);
else
{
if (currentNode.front == null)
currentNode.front = new Node();
buildStack.Push((currentNode.front, listFront));
}
}

if (listBack.Count > 0)
{
if (newNode && currentList.SequenceEqual(listBack))
currentNode.polygons.AddRange(listBack);
else
{
if (currentNode.back == null)
currentNode.back = new Node();
buildStack.Push((currentNode.back, listBack));
}
}
}
}

Expand Down