// Build a BSP tree out of `polygons`. When called on an existing tree, the // new polygons are filtered down to the bottom of the tree and become new // nodes there. Each set of polygons is partitioned using the first polygon // (no heuristic is used to pick a good split). public void Build(List <CSG_Polygon> list) { if (list == null || list.Count < 1) { return; } if (plane == null || !plane.Valid()) { plane = new CSG_Plane { normal = list[0].plane.normal, w = list[0].plane.w }; } if (polygons == null) { polygons = new List <CSG_Polygon>(); } var listFront = new List <CSG_Polygon>(); var listBack = new List <CSG_Polygon>(); for (int i = 0; i < list.Count; i++) { plane.SplitPolygon(list[i], polygons, polygons, ref listFront, ref listBack); } if (listFront.Count > 0) { if (front == null) { front = new CSG_Node(); } front.Build(listFront); } if (listBack.Count > 0) { if (back == null) { back = new CSG_Node(); } back.Build(listBack); } }
// Return a new CSG solid representing space both this solid and in the // solid `csg`. Neither this solid nor the solid `csg` are modified. public static CSG_Node Intersect(CSG_Node a1, CSG_Node b1) { CSG_Node a = a1.Clone(); CSG_Node b = b1.Clone(); a.Invert(); b.ClipTo(a); b.Invert(); a.ClipTo(b); b.ClipTo(a); a.Build(b.AllPolygons()); a.Invert(); CSG_Node ret = new CSG_Node(a.AllPolygons()); return(ret); }