Skip to content

Commit

Permalink
Transitioned to FastLinkedList for core BSP operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkkellogg committed Jul 23, 2015
1 parent a0d0925 commit c6f8f1f
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 48 deletions.
1 change: 1 addition & 0 deletions Unity/Assembly-CSharp-vs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Assets\CSG-BSP\Scripts\CSG\Vector4D.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\CSG\Vertex.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\CSGUtil.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\Structure\FastLinkedList.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\Tests\BasicTest.cs" />
<Reference Include="UnityEngine.UI">
<HintPath>D:/Programs/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
Expand Down
1 change: 1 addition & 0 deletions Unity/Assembly-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Assets\CSG-BSP\Scripts\CSG\Vector4D.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\CSG\Vertex.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\CSGUtil.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\Structure\FastLinkedList.cs" />
<Compile Include="Assets\CSG-BSP\Scripts\Tests\BasicTest.cs" />
<Reference Include="UnityEngine.UI">
<HintPath>D:/Programs/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
Expand Down
Binary file modified Unity/Assets/CSG-BSP/Scenes/BaseScene.unity
Binary file not shown.
93 changes: 64 additions & 29 deletions Unity/Assets/CSG-BSP/Scripts/CSG/BSPTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,101 @@ public void AddTriangles(List<Triangle> triangles)
root = Node.Create(triangles[0].OrientationPlane);
}

AddTriangles(root, triangles);
FastLinkedList<Triangle> linkedTriangles = new FastLinkedList<Triangle>();
var enumerator = triangles.GetEnumerator();
while(enumerator.MoveNext())
{
linkedTriangles.AddLast(enumerator.Current);
}

AddTriangles(root, linkedTriangles);
}

private void AddTriangles(Node node, List<Triangle> triangles)
private void AddTriangles(Node node, FastLinkedList<Triangle> allTriangles)
{
if (triangles == null || triangles.Count <= 0)return;
if (allTriangles == null)return;
if(node == null)return;

List<Triangle> nodeTriangles = node.GetTriangleList();

List<Triangle> lessThan = new List<Triangle> ();
List<Triangle> greaterThan = new List<Triangle> ();

for (int i = 0; i < triangles.Count; i++)
FastLinkedList<Triangle> lessThan = new FastLinkedList<Triangle> ();
FastLinkedList<Triangle> greaterThan = new FastLinkedList<Triangle> ();
FastLinkedList<Triangle> addToNode = new FastLinkedList<Triangle> ();

FastLinkedList<Triangle>.Node current = allTriangles.First;
while(current != null)
{
Partitioner.Orientation orient = Partitioner.SliceTriangle(current.Value, node.SplitPlane, lessThan, greaterThan, addToNode, addToNode);
current = current.Next;
}

current = addToNode.First;
while(current != null)
{
Partitioner.Orientation orient = Partitioner.SliceTriangle(triangles[i], node.SplitPlane, lessThan, greaterThan, nodeTriangles, nodeTriangles);
nodeTriangles.Add(current.Value);
current = current.Next;
}

if(lessThan.Count > 0)
if(lessThan.First != null)
{
if(node.LessThan == null)
node.LessThan = Node.Create(lessThan[0].OrientationPlane);
node.LessThan = Node.Create(lessThan.First.Value.OrientationPlane);
AddTriangles(node.LessThan, lessThan);
}
if(greaterThan.Count > 0)

if(greaterThan.First != null)
{
if( node.GreaterThan == null)
node.GreaterThan = Node.Create(greaterThan[0].OrientationPlane);
AddTriangles(node.GreaterThan, greaterThan);
node.GreaterThan = Node.Create(greaterThan.First.Value.OrientationPlane);
AddTriangles(node.GreaterThan, greaterThan);
}
}

public void ClipOutTriangles(List<Triangle> triangles)
{
ClipOutTriangles (root, triangles);
}
FastLinkedList<Triangle> linkedTriangles = new FastLinkedList<Triangle>();
var enumerator = triangles.GetEnumerator();
while(enumerator.MoveNext())
{
linkedTriangles.AddLast(enumerator.Current);
}

ClipOutTriangles (root, linkedTriangles);

private void ClipOutTriangles(Node node, List<Triangle> triangles)
triangles.Clear();
FastLinkedList<Triangle>.Node current = linkedTriangles.First;
while(current != null)
{
triangles.Add(current.Value);
current = current.Next;
}
}

private void ClipOutTriangles(Node node, FastLinkedList<Triangle> triangles)
{
if (triangles == null || triangles.Count <= 0)return;
if (triangles == null || triangles.First == null)return;
if(node == null)return;

List<Triangle> lessThan = new List<Triangle>();
List<Triangle> greaterThan = new List<Triangle>();

for (int i = 0; i < triangles.Count; i++)

FastLinkedList<Triangle> lessThan = new FastLinkedList<Triangle>();
FastLinkedList<Triangle> greaterThan = new FastLinkedList<Triangle>();

FastLinkedList<Triangle>.Node current = triangles.First;
while(current != null)
{
Partitioner.Orientation orient = Partitioner.SliceTriangle(triangles[i], node.SplitPlane, lessThan, greaterThan, lessThan, greaterThan);
Partitioner.Orientation orient = Partitioner.SliceTriangle(current.Value, node.SplitPlane, lessThan, greaterThan, lessThan, greaterThan);
current = current.Next;
}

triangles.Clear();

if(node.LessThan != null)
ClipOutTriangles (node.LessThan, lessThan);
else
lessThan.Clear();
ClipOutTriangles (node.GreaterThan, greaterThan);
ClipOutTriangles (node.GreaterThan, greaterThan);

triangles.Clear ();
triangles.AddRange (lessThan);
triangles.AddRange (greaterThan);
triangles.AppendList(lessThan);
triangles.AppendList(greaterThan);
}

public void ClipByTree(BSPTree tree)
Expand Down
20 changes: 10 additions & 10 deletions Unity/Assets/CSG-BSP/Scripts/CSG/Partitioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public enum Orientation
private const float SplitEpsilon = 0.00001f;

public static Orientation SliceTriangle(Triangle triangle, Plane plane,
List<Triangle> lessThan, List<Triangle> greaterThan,
List<Triangle> lessThanPlanar, List<Triangle> greaterThanPlanar)
FastLinkedList<Triangle> lessThan, FastLinkedList<Triangle> greaterThan,
FastLinkedList<Triangle> lessThanPlanar, FastLinkedList<Triangle> greaterThanPlanar)
{
Orientation[] vertOrientations = new Orientation[3];
Orientation triOrientation = Orientation.CoPlanar;
Expand Down Expand Up @@ -55,21 +55,21 @@ public static Orientation SliceTriangle(Triangle triangle, Plane plane,
if(planeTriOrientation > 0)
{
//Debug.Log(">> coplanar - GREATER");
greaterThanPlanar.Add(triangle);
greaterThanPlanar.AddLast(triangle);
}
else
{
//Debug.Log(">> coplanar - LESS");
lessThanPlanar.Add(triangle);
lessThanPlanar.AddLast(triangle);
}
break;
case Orientation.LessThan:
//Debug.Log(">> LESS");
lessThan.Add(triangle);
lessThan.AddLast(triangle);
break;
case Orientation.GreaterThan:
//Debug.Log(">> GREATER");
greaterThan.Add(triangle);
greaterThan.AddLast(triangle);
break;
case Orientation.Spanning:
//Debug.Log(">> SPANNING");
Expand Down Expand Up @@ -108,14 +108,14 @@ public static Orientation SliceTriangle(Triangle triangle, Plane plane,

if (ltSpanning.Count >= 3)
{
lessThan.Add(new Triangle(ltSpanning[0], ltSpanning[1], ltSpanning[2]));
if(ltSpanning.Count >= 4)lessThan.Add(new Triangle(ltSpanning[0], ltSpanning[2], ltSpanning[3]));
lessThan.AddLast(new Triangle(ltSpanning[0], ltSpanning[1], ltSpanning[2]));
if(ltSpanning.Count >= 4)lessThan.AddLast(new Triangle(ltSpanning[0], ltSpanning[2], ltSpanning[3]));
}

if (gtSpanning.Count >= 3)
{
greaterThan.Add(new Triangle(gtSpanning[0], gtSpanning[1], gtSpanning[2]));
if(gtSpanning.Count >= 4)greaterThan.Add(new Triangle(gtSpanning[0], gtSpanning[2], gtSpanning[3]));
greaterThan.AddLast(new Triangle(gtSpanning[0], gtSpanning[1], gtSpanning[2]));
if(gtSpanning.Count >= 4)greaterThan.AddLast(new Triangle(gtSpanning[0], gtSpanning[2], gtSpanning[3]));
}
break;
}
Expand Down
9 changes: 9 additions & 0 deletions Unity/Assets/CSG-BSP/Scripts/Structure.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using UnityEngine;
using System.Collections;

namespace CSG
{
public class FastLinkedList<T>
{
private Node _First;
private Node _Last;

public Node First
{
get { return _First;}
}

public Node Last
{
get { return _Last;}
}

public FastLinkedList()
{
_First = null;
_Last = null;
}

public Node AddLast(T value)
{
Node newNode = new Node(value);

if(_First == null)
{
_First = _Last = newNode;
}
else
{
_Last.Next = newNode;
newNode.Previous = _Last;
_Last = newNode;
}

return newNode;
}

public void AppendList(FastLinkedList<T> list)
{
if(list == null)return;
if(list._First == null)return;

if(_First == null)
{
_First = list._First;
_Last = list._Last;
}
else
{
Node temp = _Last;
_Last.Next = list._First;
_Last.Next.Previous = _Last;
_Last = list._Last;
if(_Last.Previous == null)_Last.Previous = temp;
}
}

public void Clear()
{
_First = null;
_Last = null;
}

public class Node
{
public T Value;
public Node Next;
public Node Previous;

public Node(T value)
{
Value = value;
}
}
}
}
12 changes: 12 additions & 0 deletions Unity/Assets/CSG-BSP/Scripts/Structure/FastLinkedList.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Unity/Unity-csharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
Policies = $0
$0.TextStylePolicy = $1
Expand Down
2 changes: 1 addition & 1 deletion Unity/Unity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
Policies = $0
$0.TextStylePolicy = $1
Expand Down
15 changes: 8 additions & 7 deletions Unity/Unity.userprefs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets\CSG-BSP\Scripts\CSGUtil.cs">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" PreferredExecutionTarget="MonoDevelop.Default" />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets\CSG-BSP\Scripts\Tests\BasicTest.cs">
<Files>
<File FileName="Assets\CSG-BSP\Scripts\CSG\Partitioner.cs" Line="116" Column="52" />
<File FileName="Assets\CSG-BSP\Scripts\CSGUtil.cs" Line="79" Column="3" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\BSPTree.cs" Line="52" Column="4" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Partitioner.cs" Line="102" Column="36" />
<File FileName="Assets\CSG-BSP\Scripts\CSGUtil.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\BSPTree.cs" Line="47" Column="26" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Triangle.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Plane.cs" Line="32" Column="6" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Plane.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Vertex.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\BSPOperations.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Vector3D.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\CSG\Vector4D.cs" Line="1" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\Tests\BasicTest.cs" Line="20" Column="42" />
<File FileName="Assets\CSG-BSP\Scripts\Tests\BasicTest.cs" Line="38" Column="1" />
<File FileName="Assets\CSG-BSP\Scripts\Structure\FastLinkedList.cs" Line="19" Column="27" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
Expand Down

0 comments on commit c6f8f1f

Please sign in to comment.