Exemple #1
0
        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);
            }
        }
Exemple #2
0
        public static TriangleIntersection3d CalcTriangleIntersection3d_NotCoplanar(Triangle3d t1, Triangle3d t2)
        {
            int sign_t2_a = t1.GetSide(t2.a);
            int sign_t2_b = t1.GetSide(t2.b);
            int sign_t2_c = t1.GetSide(t2.c);

            int sign_t1_a = t2.GetSide(t1.a);
            int sign_t1_b = t2.GetSide(t1.b);
            int sign_t1_c = t2.GetSide(t1.c);

            TriangleIntersection3d ti = new TriangleIntersection3d();

            if (!((sign_t2_a == sign_t2_b) && (sign_t2_b == sign_t2_c)))
            {
                xyz pt1;
                xyz pt2;
                if ((sign_t2_a != sign_t2_b) && TriangleLineSegmentIntersect3d(t1, t2.a, t2.b, out pt1, out pt2, sign_t2_a, sign_t2_b))
                {
                    ti.Add(pt1);
                    ti.Add(pt2);
                }
                if ((sign_t2_b != sign_t2_c) && TriangleLineSegmentIntersect3d(t1, t2.b, t2.c, out pt1, out pt2, sign_t2_b, sign_t2_c))
                {
                    ti.Add(pt1);
                    ti.Add(pt2);
                }
                if ((sign_t2_c != sign_t2_a) && TriangleLineSegmentIntersect3d(t1, t2.c, t2.a, out pt1, out pt2, sign_t2_c, sign_t2_a))
                {
                    ti.Add(pt1);
                    ti.Add(pt2);
                }
                if ((sign_t1_a != sign_t1_b) && TriangleLineSegmentIntersect3d(t2, t1.a, t1.b, out pt1, out pt2, sign_t1_a, sign_t1_b))
                {
                    ti.Add(pt1);
                    ti.Add(pt2);
                }
                if ((sign_t1_b != sign_t1_c) && TriangleLineSegmentIntersect3d(t2, t1.b, t1.c, out pt1, out pt2, sign_t1_b, sign_t1_c))
                {
                    ti.Add(pt1);
                    ti.Add(pt2);
                }
                if ((sign_t1_c != sign_t1_a) && TriangleLineSegmentIntersect3d(t2, t1.c, t1.a, out pt1, out pt2, sign_t1_c, sign_t1_a))
                {
                    ti.Add(pt1);
                    ti.Add(pt2);
                }
            }
            return(ti);
        }
Exemple #3
0
        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));
            }
        }