private static (bool, int) TestIntersect(int index, KAABBTree o1, KAABBTree o2) { if (!o2._boxes[index].Intersects(o1._boxes[index])) { return(false, 1); } if (o1._isALeef && o2._isALeef) { return(new IntrTriangle2Triangle2(o1._mesh, o2._mesh).Test(), 2); } else { if (!o1._isALeef) { index = index + 1; if (index == NumberOfAlignment) { index = 0; } var(b, r) = TestIntersect(index, o1._left, o2); if (b) { return(b, r + 1); } var(b1, r1) = TestIntersect(index, o1._right, o2); return(b || b1, r + r1 + 1); } else { return(TestIntersect(index, o2, o1)); } } }
public KAABBTree(IList <Triangle2d> mesh) { _boxes = new AxisAlignedBox2d[NumberOfAlignment]; for (var i = 0; i < NumberOfAlignment; ++i) { var i1 = i; _boxes[i] = mesh.Select(tra => tra.Rotate(Alignment[i1])).GetBBox(); } var alignmentToSplitBy = Random.Next(NumberOfAlignment); mesh = (_boxes[alignmentToSplitBy].Height > _boxes[alignmentToSplitBy].Width ? mesh.OrderBy(tra => (Alignment[alignmentToSplitBy] * tra.V0).y) : mesh.OrderBy(tra => (Alignment[alignmentToSplitBy] * tra.V0).x)).ToList(); if (mesh.Count != 1) { _isALeef = false; var midIndex = mesh.Count / 2; _left = new KAABBTree(mesh.Take(midIndex).ToList()); _right = new KAABBTree(mesh.Skip(midIndex).Take(mesh.Count - midIndex).ToList()); } else { _isALeef = true; _mesh = mesh.First(); } }
private static (double, double, double, double) GetRandomMesure(double diff) { var rotMat = Matrix2DExtension.GetRandomRotationMatrix(); var mesh1 = GetRandomMesh(NumberOfTriangles, rotMat, Vector2d.Zero); var mesh2 = GetRandomMesh(NumberOfTriangles, rotMat, rotMat * Vector2d.AxisX * (1 + diff)); var watch = StartNew(); AABBTree t1 = new AABBTree(mesh1), t2 = new AABBTree(mesh2); watch.Stop(); var AABBBuildTime = watch.ElapsedMilliseconds; var AABBCollisionAction = AABBTree.TestIntersect(t1, t2).Item2; watch = StartNew(); KAABBTree.KAABBTree kt1 = new KAABBTree.KAABBTree(mesh1), kt2 = new KAABBTree.KAABBTree(mesh2); watch.Stop(); var KAABBBuildTime = watch.ElapsedMilliseconds; var KAABBCollisionAction = KAABBTree.KAABBTree.TestIntersect(kt1, kt2).Item2; return(AABBBuildTime, AABBCollisionAction, KAABBBuildTime, KAABBCollisionAction); }
public static (bool, int) TestIntersect(KAABBTree o1, KAABBTree o2) { return(TestIntersect(0, o1, o2)); }