public static TriangleIntersection3d CalcTriangleIntersection3d(Triangle3d t1, Triangle3d t2) { if (ut.AllVerticesPlanar(t1, t2)) { TriangleIntersection3d ti = new TriangleIntersection3d(); xyz n = t1.n; xyz i = t1.iv; xyz j = t1.jv; xy a1 = ut.ConvertPointTo2d(t1.a, t1.a, i, j); xy b1 = ut.ConvertPointTo2d(t1.b, t1.a, i, j); xy c1 = ut.ConvertPointTo2d(t1.c, t1.a, i, j); xy a2 = ut.ConvertPointTo2d(t2.a, t1.a, i, j); xy b2 = ut.ConvertPointTo2d(t2.b, t1.a, i, j); xy c2 = ut.ConvertPointTo2d(t2.c, t1.a, i, j); TriangleIntersection2d ti2d = TriangleIntersection2d.CalcTriangleIntersection2d(a1, b1, c1, a2, b2, c2); if (ti2d.Count > 0) { foreach (xy p in ti2d) { xyz q = ut.Convert2dPointTo3d(p, t1.a, i, j); ti.Add(q); } } return(ti); } else { return(CalcTriangleIntersection3d_NotCoplanar(t1, t2)); } }
public static TriangleIntersection2d CalcTriangleIntersection2d(xy a1, xy b1, xy c1, xy a2, xy b2, xy c2) { #if PP_DEBUG Console.Out.WriteLine("tri 1: a={0} b={1} c={2}", a1, b1, c1); Console.Out.WriteLine("tri 2: a={0} b={1} c={2}", a2, b2, c2); #endif TriangleIntersection2d pts = new TriangleIntersection2d(); List <seg2d> segs = new List <seg2d>(); CalcSegmentTriangleIntersection2d(pts, segs, a1, b1, a2, b2, c2); CalcSegmentTriangleIntersection2d(pts, segs, b1, c1, a2, b2, c2); CalcSegmentTriangleIntersection2d(pts, segs, c1, a1, a2, b2, c2); CalcSegmentTriangleIntersection2d(pts, segs, a2, b2, a1, b1, c1); CalcSegmentTriangleIntersection2d(pts, segs, b2, c2, a1, b1, c1); CalcSegmentTriangleIntersection2d(pts, segs, c2, a2, a1, b1, c1); TriangleIntersection2d ti = new TriangleIntersection2d(); Debug.Assert( (segs.Count >= 0) && (segs.Count <= 6) ); switch (segs.Count) { case 0: { Debug.Assert( (pts.Count == 0) || (pts.Count == 1) ); if (pts.Count == 0) { return(ti); } else // if (pts.Count == 1) { ti.Add(pts[0]); return(ti); } } case 1: case 2: case 3: case 4: case 5: case 6: { int i = 0; seg2d cur = segs[i]; ti.Add(cur.a); ti.Add(cur.b); segs.Remove(cur); xy lastpt = cur.b; while (segs.Count > 0) { xy nextpt; find_seg(segs, lastpt, out nextpt, out cur); ti.Add(nextpt); segs.Remove(cur); lastpt = nextpt; } break; } } return(ti); }
public static void CalcSegmentTriangleIntersection2d(TriangleIntersection2d pts, List <seg2d> segs, xy a, xy b, xy ta, xy tb, xy tc) { TriangleIntersection2d ti2 = new TriangleIntersection2d(); SegIntersection si; xy q1; xy q2; if (ut.PointInsideTriangle(a, ta, tb, tc)) { ti2.Add(a); } si = ut.GetSegIntersection(a, b, ta, tb, out q1, out q2); if (si == SegIntersection.Point) { ti2.Add(q1); } else if (si == SegIntersection.Overlap) { ti2.Add(q1); ti2.Add(q2); } si = ut.GetSegIntersection(a, b, tb, tc, out q1, out q2); if (si == SegIntersection.Point) { ti2.Add(q1); } else if (si == SegIntersection.Overlap) { ti2.Add(q1); ti2.Add(q2); } si = ut.GetSegIntersection(a, b, tc, ta, out q1, out q2); if (si == SegIntersection.Point) { ti2.Add(q1); } else if (si == SegIntersection.Overlap) { ti2.Add(q1); ti2.Add(q2); } if (ut.PointInsideTriangle(b, ta, tb, tc)) { ti2.Add(b); } Debug.Assert( (ti2.Count == 0) || (ti2.Count == 1) || (ti2.Count == 2) ); if (ti2.Count == 0) { return; } else if (ti2.Count == 1) { pts.Add(ti2[0]); } else // if (ti2.Count == 2) { add_seg(segs, ti2[0], ti2[1]); } }