public void Cut(Vector3 p0, Vector3 p1, Vector3 p2) { TTriangle tri = new TTriangle(p0, p1, p2); int high = triangle.High; for (int i = 0; i <= high; i++) { if (tri.Inters(pos[triangle[i].p0], pos[triangle[i].p1], pos[triangle[i].p2])) { Vector3 normal = (Vector3.Cross((p1 - p0), (p2 - p0))); normal.Normalize(); CutTriangleByPlane(new TPlane(normal, p0), i); } } }
public bool Inters(TTriangle tri) { //плоскости треугольников Vector3 N0 = Vector3.Cross(p0 - p1, p0 - p2), N1 = Vector3.Cross(tri.p0 - tri.p1, tri.p0 - tri.p2); float d0 = -Vector3.Dot(p0, N0); float d1 = -Vector3.Dot(tri.p0, N1); //расстояние от вершин первого треугольника до плоскости второго float d0T0 = Vector3.Dot(p0, N1) + d1; float d1T0 = Vector3.Dot(p1, N1) + d1; float d2T0 = Vector3.Dot(p2, N1) + d1; Vector3 line = Vector3.Cross(N0, N1); if (d0T0 == 0 && d1T0 == 0 && d2T0 == 0) { return(false); //треугольники лежат в одной плоскости } if (((d0T0 > 0) == (d1T0 > 0)) && ((d0T0 > 0) == (d2T0 > 0))) { return(false); } float d0T1 = Vector3.Dot(tri.p0, N0) + d0; float d1T1 = Vector3.Dot(tri.p1, N0) + d0; float d2T1 = Vector3.Dot(tri.p2, N0) + d0; if (((d0T1 > 0) == (d1T1 > 0)) && ((d0T1 > 0) == (d2T1 > 0))) { return(false); } //вычисляем проекции ребер треугольников на линию float t0T0, t1T0, t0T1, t1T1; if ((d0T0 > 0) == (d1T0 > 0)) { t0T0 = GetInterval(line, p1, p2, d1T0, d2T0); t1T0 = GetInterval(line, p0, p2, d0T0, d2T0); } else if ((d2T0 > 0) == (d1T0 > 0)) { t0T0 = GetInterval(line, p2, p0, d2T0, d0T0); t1T0 = GetInterval(line, p1, p0, d1T0, d0T0); } else// if ((d0T0 > 0) == (d2T0 > 0)) { t0T0 = GetInterval(line, p0, p1, d0T0, d1T0); t1T0 = GetInterval(line, p2, p1, d2T0, d1T0); } if ((d0T1 > 0) == (d1T1 > 0)) { t0T1 = GetInterval(line, tri.p1, tri.p2, d1T1, d2T1); t1T1 = GetInterval(line, tri.p0, tri.p2, d0T1, d2T1); } else if ((d2T1 > 0) == (d1T1 > 0)) { t0T1 = GetInterval(line, tri.p2, tri.p0, d2T1, d0T1); t1T1 = GetInterval(line, tri.p1, tri.p0, d1T1, d0T1); } else// if ((d0T1 > 0) == (d2T1 > 0)) { t0T1 = GetInterval(line, tri.p0, tri.p1, d0T1, d1T1); t1T1 = GetInterval(line, tri.p2, tri.p1, d2T1, d1T1); } //определяем пересечение проекций float temp; if (t0T0 > t1T0) { temp = t0T0; t0T0 = t1T0; t1T0 = temp; } if (t0T1 > t1T1) { temp = t0T1; t0T1 = t1T1; t1T1 = temp; } return(t0T1 <= t1T0 && t0T0 <= t1T1); }