Skip to content

Commit

Permalink
Added Iterate() and CopyFrom() methods to FastLinkedList.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkkellogg committed Jul 27, 2015
1 parent d86a8cd commit 59c85f7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
46 changes: 15 additions & 31 deletions Unity/Assets/CSG-BSP/Scripts/CSG/BSPTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public void AddTriangles(List<Triangle> triangles)

// convert @triangles to a FastLinkedList structure.
FastLinkedList<Triangle> linkedTriangles = new FastLinkedList<Triangle>();
var enumerator = triangles.GetEnumerator();
while(enumerator.MoveNext())
{
linkedTriangles.AddLast(enumerator.Current);
}
linkedTriangles.CopyFrom(triangles);

// call the private, recursive version of AddTriangles
AddTriangles(root, linkedTriangles);
Expand All @@ -64,12 +60,9 @@ private void AddTriangles(Node node, FastLinkedList<Triangle> triangles)
// iterate through each triangle in @triangles and classify/partition each according
// @node's split plane. co-planar triangles go into @nodeTriangles, triangles on the front
// side go into @greaterThan, traingles on the back side go into @lessThan.
FastLinkedList<Triangle>.Node current = triangles.First;
while(current != null)
{
Partitioner.Orientation orient = Partitioner.SliceTriangle(current.Value, node.SplitPlane, lessThan, greaterThan, nodeTriangles, nodeTriangles);
current = current.Next;
}
triangles.Iterate((Triangle tri) => {
Partitioner.Orientation orient = Partitioner.SliceTriangle(tri, node.SplitPlane, lessThan, greaterThan, nodeTriangles, nodeTriangles);
});

// release clear memory occupied by @triangles
triangles.Clear();
Expand Down Expand Up @@ -135,13 +128,10 @@ private void ClipOutTriangles(Node node, FastLinkedList<Triangle> triangles, boo
// @node's split plane. triangles on the front side go into @greaterThan, triangles
// on the back side go into @lessThan. co-planar triangles whose normal matches that of
// @node's split plane go into @greaterThan; the rest go into @lessThan.
FastLinkedList<Triangle>.Node current = triangles.First;
while(current != null)
{
Partitioner.Orientation orient = Partitioner.SliceTriangle(current.Value, node.SplitPlane, lessThan, greaterThan, lessThan, greaterThan);
current = current.Next;
}

triangles.Iterate((Triangle tri) => {
Partitioner.Orientation orient = Partitioner.SliceTriangle(tri, node.SplitPlane, lessThan, greaterThan, lessThan, greaterThan);
});

// release memory used by @triangles
triangles.Clear();

Expand Down Expand Up @@ -221,13 +211,10 @@ private void GetAllTriangles(Node node, List<Triangle> triangles)
{
if(node == null)return;

FastLinkedList<Triangle>.Node current = node.GetTriangleList().First;
while(current != null)
{
triangles.Add(current.Value);
current = current.Next;
}

node.GetTriangleList().Iterate((Triangle tri) => {
triangles.Add(tri);
});

GetAllTriangles(node.LessThan, triangles);
GetAllTriangles(node.GreaterThan, triangles);
}
Expand Down Expand Up @@ -336,12 +323,9 @@ public Node Clone()

copy.SplitPlane = SplitPlane;

FastLinkedList<Triangle>.Node current = triangles.First;
while(current != null)
{
copy.triangles.AddLast(current.Value);
current = current.Next;
}
triangles.Iterate((Triangle tri) => {
copy.triangles.AddLast(tri);
});

copy.LessThan = LessThan;
copy.GreaterThan = GreaterThan;
Expand Down
23 changes: 23 additions & 0 deletions Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public void AppendIntoList(FastLinkedList<T> list)

public void CopyInto(IList<T> copy)
{
if(copy == null)return;

FastLinkedList<T>.Node current = First;
while(current != null)
{
Expand All @@ -79,6 +81,27 @@ public void CopyInto(IList<T> copy)
}
}

public void CopyFrom(IList<T> source)
{
if(source == null)return;

IEnumerator<T> enumerator = source.GetEnumerator();
while(enumerator.MoveNext())
{
AddLast (enumerator.Current);
}
}

public void Iterate(System.Action<T> action)
{
Node current = First;
while(current != null)
{
action.Invoke(current.Value);
current = current.Next;
}
}

public class Node
{
public T Value;
Expand Down

0 comments on commit 59c85f7

Please sign in to comment.