public CSG Intersect(CSG csg) { Node a = new Node(this.Clone().polygons); Node b = new Node(csg.Clone().polygons); a.Invert(); b.ClipTo(a); b.Invert(); a.ClipTo(b); b.ClipTo(a); a.Build(b.AllPolygons.ToList()); a.Invert(); return CSG.FromPolygons(a.AllPolygons); }
public void Build(List<Polygon> polygons) { //Debug.Log("node:build started, polycount " + polygons.Count); if (polygons.Count == 0) { //Debug.Log("node:build no polys, returning"); return; } if (this.plane == null) this.plane = polygons[0].Plane.Clone(); List<Polygon> front = new List<Polygon>(); List<Polygon> back = new List<Polygon>(); for (int i = 0; i < polygons.Count; i++) { this.plane.SplitPolygon(polygons[i], ref this.polygons, ref this.polygons, ref front, ref back); } //Debug.Log("node:build front length: " + front.Count + " back length " + back.Count); if (front.Count > 0) { if (this.front == null) this.front = new Node(); this.front.Build(front); } if (back.Count > 0) { if (this.back == null) this.back = new Node(); this.back.Build(back); } }
/// <summary> /// Convert solid space to empty space and empty space to solid space. /// </summary> public void Invert() { for (int i = 0; i < polygons.Count; i++) { this.polygons[i].Flip(); } this.plane.Flip(); if (this.front != null) this.front.Invert(); if (this.back != null) this.back.Invert(); Node temp = this.front; this.front = this.back; this.back = temp; }
public Node Clone() { Node node = new Node(); node.plane = this.plane; node.front = this.front; node.back = this.back; node.polygons = this.polygons; return node; }
public void ClipTo(Node bsp) { this.polygons = bsp.ClipPolygons(this.polygons.ToArray()); if (this.front != null) this.front.ClipTo(bsp); if (this.back != null) this.back.ClipTo(bsp); }
// TODO: Check this function /// <summary> /// Recursively remove all polygons in `polygons` that are inside this BSP tree. /// </summary> public List<Polygon> ClipPolygons(Polygon[] polygons) { if (this.plane == null) { return polygons.Clone<Polygon>().ToList(); } // TODO: Removed slice here? List<Polygon> front = new List<Polygon>(); List<Polygon> back = new List<Polygon>(); for (int i = 0; i < polygons.Length; i++) { this.plane.SplitPolygon(polygons[i], ref front, ref back, ref front, ref back); } if (this.front != null) { front = this.front.ClipPolygons(front.ToArray()); } if (this.back != null) { back = this.back.ClipPolygons(back.ToArray()); } else back = new List<Polygon>(); return front.Concat(back).ToList(); }