public void test_convert_to_2d() { Triangle3d t = new Triangle3d(new xyz(0, 0, 0), new xyz(5, 0, 0), new xyz(5, 5, 0)); Triangle2d t2 = t.ConvertTo2d(); Assert.IsTrue(fp.eq_inches(new xy(0, 0), t2.a)); Assert.IsTrue(fp.eq_inches(new xy(5, 0), t2.b)); Assert.IsTrue(fp.eq_inches(new xy(5, 5), t2.c)); }
public static bool TriangleLineSegmentIntersect3d(Triangle3d t, xyz d, xyz e, out xyz pt1, out xyz pt2, int sign_d, int sign_e) { if ( (sign_d > 0) && (sign_e > 0) ) { pt1 = null; pt2 = null; return(false); } if ( (sign_d < 0) && (sign_e < 0) ) { pt1 = null; pt2 = null; return(false); } if ( (sign_d == 0) && (sign_e == 0) ) { // the segment is in the same plane as the triangle // convert everything to 2d Triangle2d t2d = t.ConvertTo2d(); xy d2 = ut.ConvertPointTo2d(d, t.a, t.iv, t.jv); xy e2 = ut.ConvertPointTo2d(e, t.a, t.iv, t.jv); bool bd = t2d.PointInside(d2); bool be = t2d.PointInside(e2); if (bd && be) { pt1 = d; pt2 = e; return(true); } else { xy w1; xy w2; bool bresult = t2d.SegmentIntersection(d2, e2, out w1, out w2); if (bresult) { pt1 = ut.Convert2dPointTo3d(w1, t.a, t.iv, t.jv); if (w2 != null) { pt2 = ut.Convert2dPointTo3d(w2, t.a, t.iv, t.jv); } else { pt2 = null; } } else { pt1 = null; pt2 = null; } return(bresult); } } else { // the segment intersects the plane in only one point, so pt2 will be null pt2 = null; xyz p; if (sign_d == 0) { p = d; } else if (sign_e == 0) { p = e; } else { // find the point where the pp_segment intersects the plane double u = xyz.dotsub(t.n, t.a, d) / xyz.dotsub(t.n, e, d); p = (e - d).multiply_in_place(u).add_in_place(d); } Triangle2d t2d = t.ConvertTo2d(); xy ipt = ut.ConvertPointTo2d(p, t.a, t.iv, t.jv); bool b = t2d.PointInside(ipt); if (b) { pt1 = p.copy(); } else { pt1 = null; } return(b); } }