// Populates the provided PartioningGrid with items (the triangle indices of this triangle) in the specified category // The category should be either 0 or 1, as it is used as an array index and will indicate which of the two objects this triangle goes with public void PopulateGrid(PartitioningGrid grid, int category) { int num_triangles = t_v.GetLength(0); for (int i = 0; i < num_triangles; i++) { grid.InsertItem(category, i, AABB.FitPointList(new List <Vec3>(new Vec3[] { verts[t_v[i, 0]], verts[t_v[i, 1]], verts[t_v[i, 2]] }))); } }
// Do the CSG stuff // This is a virtual function, just in case someone thinks they can do it better public virtual void Compute() { first_blob = ModelInput.FromBasicModelData(first_in, first_xform); second_blob = ModelInput.FromBasicModelData(second_in, second_xform); /* * CSGModel derp1 = ModelUtil.ModelFromBMD(first_in, first_xform); * CSGModel derp2 = ModelUtil.ModelFromBMD(second_in, second_xform); * int count = 0; * ModelIntersectTree.CullIntersections(derp1, derp2, * (i, j) => * { * count++; * }); */ AABB aa_box = first_blob.AABB; AABB bb_box = second_blob.AABB; if (AABB.CheckIntersection(aa_box, bb_box)) // check whether their bounding boxes even intersect... if they don't, most of the math can be skipped! { AABB intersection = AABB.Intersection(aa_box, bb_box); PartitioningGrid grid = new PartitioningGrid(intersection, 5); first_blob.PopulateGrid(grid, 0); second_blob.PopulateGrid(grid, 1); List <TrianglePair> pairs = new List <TrianglePair>(); grid.ForLeafLevelPairs( (first, second) => { pairs.Add(new TrianglePair { a = first, b = second }); }); Snip(first_blob, second_blob, pairs); } else { Snip(first_blob, second_blob, new List <TrianglePair>()); } }