Ejemplo n.º 1
0
        static void Check(Plane P1, TriangleF T, ref int PtCount, xyzf A, xyzf B, ref xyzf Pt1, ref xyzf Pt2)
        {
            double Lam = -1;
            xyz    Pt  = new xyz(0, 0, 0);

            if (P1.Cross(new LineType(A.Toxyz(), (B.Toxyz() - A.Toxyz())), out Lam, out Pt))
            {
                xyz N = A.Toxyz() + (B.Toxyz() - A.Toxyz()) * Lam;

                if ((Lam >= -0.000000001) && (Lam < 1.000000001))
                {
                    if (T.Inside(Pt.toXYZF()))
                    {
                        if (PtCount == 0)
                        {
                            Pt1     = Pt.toXYZF();
                            PtCount = 1;
                        }
                        else
                        if (PtCount == 1)
                        {
                            if (Pt1.dist(Pt.toXYZF()) > 0.0001)
                            {
                                Pt2 = Pt.toXYZF();
                                PtCount++;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        bool intersectPlaneWithLine(SnapItem Plane, SnapItem Line, xyzArray LineArray, ref double Lam, ref xyz Result)
        {
            TriangleF T = Plane.GetTriangle();

            if (T == null)
            {
                return(false);
            }
            xyz N1 = ((T.B - T.A) & (T.C - T.A)).Toxyz();

            if (LineArray == null)
            {
            }
            xyz N2 = LineArray.cross();

            if ((N1 & N2).length() < 0.00001)   // Parallel
            {
                xyzArray A = Plane.getxyzArray();
                if (A != null)
                {
                    xyzArray         B = LineArray;
                    List <CrossTag1> L = CrossTag1.GetCrossList(A, B);

                    for (int i = 0; i < L.Count; i++)
                    {
                        if (A.Value(L[i].Lam1).dist(Plane.Point) <= SnapItem.Snapdist)
                        {
                            Lam = L[i].Lam1;

                            Result = A.Value(L[i].Lam1);
                            return(true);
                        }
                    }
                }
            }

            int Object = StoredSnapItems.IndexOf(Plane);
            int siz    = SnapDistance * 2 + 1;

            //  int siz = SnapDistance;
            Drawing3d.PrimIdObj[,] P = SubArray(Device.MousePos.X - siz / 2, (int)Device.ViewPort.Height - Device.MousePos.Y - siz / 2, siz, siz);
            List <TriangleF> LT = GetTriangles(P, Object);

            Lam = -1;



            bool R = Cross(LT, LineArray, ref Lam, ref Result);

            return(R);
        }
Ejemplo n.º 3
0
        bool Inside(LineType L, xyzf A, xyzf B, xyzf C)
        {
            TriangleF T        = new TriangleF(A, B, C);
            Plane     Triangle = new Plane(A, B, C);
            xyzf      N        = Triangle.NormalUnit.toXYZF();
            double    Depth    = -1;
            xyz       pt       = new xyz(0, 0, 0);

            Triangle.Cross(L, out Depth, out pt);
            bool tttt   = T.Inside(pt.toXYZF());
            xyzf _P     = pt.toXYZF();
            bool result = ((((A - _P) & (B - _P)) * N >= -0.00001) &&
                           (((B - _P) & (C - _P)) * N >= -0.00001) &&
                           (((C - _P) & (A - _P)) * N >= -0.00001));

            if (tttt != result)
            {
            }
            return(tttt);
        }
Ejemplo n.º 4
0
        public static bool Cross(TriangleF T1, TriangleF T2, ref xyzf Pt1, ref xyzf Pt2)
        {
            int   PtCount = 0;
            Plane P1      = new Plane(T1.A, T1.B, T1.C);

            Check(P1, T1, ref PtCount, T2.A, T2.B, ref Pt1, ref Pt2);
            if (PtCount == 2)
            {
                return(true);
            }
            Check(P1, T1, ref PtCount, T2.B, T2.C, ref Pt1, ref Pt2);
            if (PtCount == 2)
            {
                return(true);
            }
            Check(P1, T1, ref PtCount, T2.C, T2.A, ref Pt1, ref Pt2);
            if (PtCount == 2)
            {
                return(true);
            }

            Plane P2 = new Plane(T2.A, T2.B, T2.C);

            Check(P2, T2, ref PtCount, T1.A, T1.B, ref Pt1, ref Pt2);
            if (PtCount == 2)
            {
                return(true);
            }
            Check(P2, T2, ref PtCount, T1.B, T1.C, ref Pt1, ref Pt2);
            if (PtCount == 2)
            {
                return(true);
            }
            Check(P2, T2, ref PtCount, T1.C, T1.A, ref Pt1, ref Pt2);
            if (PtCount == 2)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        public static bool Cross(TriangleF t1, TriangleF t2, out double Lam, out double Mue, out xyz P, out xyz Q)
        {
            xyzArray A = new xyzArray();
            xyzArray B = new xyzArray();

            A.data = new xyz[] { t1.A.Toxyz(), t1.B.Toxyz(), t1.C.Toxyz() };
            B.data = new xyz[] { t2.A.Toxyz(), t2.B.Toxyz(), t2.C.Toxyz() };
            Lam    = -1;
            Mue    = -1;
            P      = new Drawing3d.xyz(0, 0, 0);
            Q      = new Drawing3d.xyz(0, 0, 0);

            double d = (A.Distance(B, 1e10, out Lam, out Mue));

            if (d < 2)
            {
                P = A.Value(Lam);
                Q = B.Value(Mue);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 6
0
        public bool Cross(LineType L, ref xyzf Pt, ref int NearestTriangle)
        {
            xyzf   P   = new xyzf(0, 0, 0);
            bool   Res = false;
            double di  = 1e10;

            NearestTriangle = -1;
            double Lam    = -1;
            double Depth  = 1e10;
            xyzf   Result = new xyzf(0, 0, 0);

            for (int i = 0; i < Indices.Length; i = i + 3)
            {
                xyzf A = Points[Indices[i]];
                xyzf B = Points[Indices[i + 1]];
                xyzf C = Points[Indices[i + 2]];
                //  if (((B - A) & (C - A)) * L.Direction.toXYZF() < 0) continue;
                TriangleF T = new TriangleF(A, B, C);
                if ((T.Cross(L, ref P, ref Lam)) && (Lam < Depth))
                {
                    Res   = true;
                    Depth = Lam;
                    Pt    = P;
                    // return true;
                }
                else
                {
                    xyzf   t = (T.A + T.B + T.C) * (1f / 3f);
                    double d = t.DistQ(P);
                    if (d < di)
                    {
                        NearestTriangle = i;
                        di = d;
                    }
                    //  if ((T.A + T.B + T.C) * (1f / 3f))}
                }
            }
            return(Res);
        }
Ejemplo n.º 7
0
        public List <Line3D> Cross(List <TriangleF> LL1, List <TriangleF> LL2, LineType ViewLine)
        {
            List <Line3D> Lines = new List <Line3D>();

            if ((LL1 != null) && (LL2 != null))
            {
                for (int i = 0; i < LL1.Count; i++)
                {
                    for (int j = 0; j < LL2.Count; j++)
                    {
                        xyzf Pt1 = new xyzf(0, 0, 0);
                        xyzf Pt2 = new xyzf(0, 0, 0);


                        if (TriangleF.Cross(LL1[i], LL2[j], ref Pt1, ref Pt2))
                        {
                            Lines.Add(new Line3D(Pt1.Toxyz(), Pt2.Toxyz()));
                        }
                    }
                }
            }
            return(Lines);
        }
Ejemplo n.º 8
0
        List <TriangleF> GetTriangles(Drawing3d.PrimIdObj[,] P, int Object)
        {
            List <TriangleF> Result  = new List <TriangleF>();
            List <PrimIdObj> PrimIds = new List <PrimIdObj>();

            SnapItem SI = CopySI(Device.Selector.SnapList[Object]);
            Matrix   M  = SI.ModelMatrix;

            for (int i = 0; i < P.GetLength(0); i++)
            {
                for (int j = 0; j < P.GetLength(1); j++)
                {
                    if (P[i, j].Object >= 0)
                    {
                        int ll = P[i, j].Object;
                    }
                    if (P[i, j].Object == 2)
                    {
                    }
                    if ((PrimIds.IndexOf(P[i, j]) < 0))
                    {
                        if ((P[i, j].Object == Object) && (SI.TriangleInfo != null))
                        {
                            TriangleF T = new TriangleF(SI.ModelMatrix * SI.TriangleInfo.Points[SI.TriangleInfo.Indices[P[i, j].PrimId * 3]],
                                                        SI.ModelMatrix * SI.TriangleInfo.Points[SI.TriangleInfo.Indices[P[i, j].PrimId * 3 + 1]],
                                                        SI.ModelMatrix * SI.TriangleInfo.Points[SI.TriangleInfo.Indices[P[i, j].PrimId * 3 + 2]]);

                            {
                                PrimIds.Add(P[i, j]);
                                Result.Add(T);
                            }
                        }
                    }
                }
            }
            return(Result);
        }
Ejemplo n.º 9
0
        public bool IntersectPlaneWithPlane(SnapItem Plane1, SnapItem Plane2, LineType ViewLine)
        {
            int Object1 = StoredSnapItems.IndexOf(Plane1);
            int Object2 = StoredSnapItems.IndexOf(Plane2);
            int siz     = 2 * SnapDistance + 1;

            PrimIdObj[,] P   = SubArray(Device.MousePos.X - siz / 2, (int)Device.ViewPort.Height - Device.MousePos.Y - siz / 2, siz, siz);
            ListOfTriangles1 = GetTriangles(P, Object1);
            ListOfTriangles1.Clear();
            Matrix M = Plane1.ModelMatrix;

            for (int i = 0; i < Plane1.TriangleInfo.Indices.Length; i++)
            {
                TriangleInfo TI = Plane1.TriangleInfo;
                TriangleF    T  = new TriangleF(M * TI.Points[TI.Indices[i]], M * TI.Points[TI.Indices[i + 1]], M * TI.Points[TI.Indices[i + 2]]);
                i = i + 2;
                //   if (T.Inside(ViewLine.P.toXYZF()))
                ListOfTriangles1.Add(T);
            }
            for (int i = 0; i < ListOfTriangles1.Count; i++)
            {
                if (ListOfTriangles1[i].Inside(ViewLine.P.toXYZF()))
                {
                    TriangleF F = ListOfTriangles1[i];
                    ListOfTriangles1.RemoveAt(i);
                    ListOfTriangles1.Insert(0, F);
                }
            }
            ListOfTriangles2 = GetTriangles(P, Object2);
            ListOfTriangles2.Clear();
            M = Plane2.ModelMatrix;
            for (int i = 0; i < Plane2.TriangleInfo.Indices.Length; i++)
            {
                TriangleInfo TI = Plane2.TriangleInfo;
                TriangleF    T  = new TriangleF(M * TI.Points[TI.Indices[i]], M * TI.Points[TI.Indices[i + 1]], M * TI.Points[TI.Indices[i + 2]]);
                i = i + 2;
                //  if (T.Inside(ViewLine.P.toXYZF()))
                ListOfTriangles2.Add(T);
            }
            for (int i = 0; i < ListOfTriangles2.Count; i++)
            {
                if (ListOfTriangles2[i].Inside(ViewLine.P.toXYZF()))
                {
                    TriangleF F = ListOfTriangles2[i];
                    ListOfTriangles2.RemoveAt(i);
                    ListOfTriangles2.Insert(0, F);
                }
            }
            List <Line3D> Lines = Cross(ListOfTriangles1, ListOfTriangles2, ViewLine);
            xyz           P1    = new xyz(0, 0, 0);
            xyz           P2    = new xyz(0, 0, 0);

            if (Lines.Count < 2)
            {
                double           Lam = -1;
                double           Mue = -1;
                List <TriangleF> L1  = new List <TriangleF>();
                List <TriangleF> L2  = new List <TriangleF>();
                for (int i = 0; i < ListOfTriangles1.Count; i++)
                {
                    if (ListOfTriangles1[i].Inside(ViewLine.P.toXYZF()))
                    {
                        L1.Add(ListOfTriangles1[i]);
                    }
                }
                for (int i = 0; i < ListOfTriangles2.Count; i++)
                {
                    if (ListOfTriangles2[i].Inside(ViewLine.P.toXYZF()))
                    {
                        L2.Add(ListOfTriangles2[i]);
                    }
                }
                L1 = ListOfTriangles1;
                L2 = ListOfTriangles2;
                for (int i = 0; i < L1.Count; i++)
                {
                    for (int j = 0; j < L2.Count; j++)
                    {
                        if (TriangleF.Cross(L1[i], L2[j], out Lam, out Mue, out P1, out P2))
                        {
                            int _lam = (int)Lam;
                            switch (_lam)
                            {
                            case 0:
                                Lines.Add(new Line3D(L1[i].A.Toxyz(), L1[i].B.Toxyz()));
                                break;

                            case 1:
                                Lines.Add(new Line3D(L1[i].B.Toxyz(), L1[i].C.Toxyz()));
                                break;

                            case 2:
                                Lines.Add(new Line3D(L1[i].C.Toxyz(), L1[i].A.Toxyz()));
                                break;

                            default:
                                break;
                            }
                            int _mue = (int)Mue;
                            switch (_mue)
                            {
                            case 0:
                                Lines.Add(new Line3D(L2[j].A.Toxyz(), L2[j].B.Toxyz()));
                                break;

                            case 1:
                                Lines.Add(new Line3D(L2[j].B.Toxyz(), L2[j].C.Toxyz()));
                                break;

                            case 2:
                                Lines.Add(new Line3D(L2[j].C.Toxyz(), L2[j].A.Toxyz()));
                                break;

                            default:
                                break;
                            }
                        }
                        break;
                    }
                }
                //if (TriangleF.Cross(ListOfTriangles1[0], ListOfTriangles2[0], out Lam, out Mue, out P1, out P2))
                //{
                //    int _lam = (int)Lam;
                //    switch (_lam)
                //    {case  0: Lines.Add(new Line3D(ListOfTriangles1[0].A.Toxyz(),ListOfTriangles1[0].B.Toxyz()));
                //           break;
                //        case 1: Lines.Add(new Line3D(ListOfTriangles1[0].B.Toxyz(), ListOfTriangles1[0].C.Toxyz()));
                //            break;
                //        case 2: Lines.Add(new Line3D(ListOfTriangles1[0].C.Toxyz(), ListOfTriangles1[0].A.Toxyz()));
                //            break;
                //        default:
                //            break;
                //    }
                //    int _mue = (int)Mue;
                //    switch (_mue)
                //    {
                //        case 0:
                //            Lines.Add(new Line3D(ListOfTriangles2[0].A.Toxyz(), ListOfTriangles2[0].B.Toxyz()));
                //            break;
                //        case 1: Lines.Add(new Line3D(ListOfTriangles2[0].B.Toxyz(), ListOfTriangles2[0].C.Toxyz()));
                //            break;
                //        case 2: Lines.Add(new Line3D(ListOfTriangles2[0].C.Toxyz(), ListOfTriangles2[0].A.Toxyz()));
                //            break;
                //        default:
                //            break;
                //    }
                //}
            }

            double di  = 1e10;
            xyz    Pos = new xyz(0, 0, 0);
            int    Id  = -1;

            for (int i = 0; i < Lines.Count; i++)
            {
                LineType L   = new LineType(Lines[i].A, Lines[i].B - Lines[i].A);
                double   Lam = -1;

                double d       = ViewLine.Distance(L, SnapItem.Snapdist, false, false, out Lam);
                xyz    Nearest = L.Value(Lam);
                if ((Lam >= -0.00001) && (Lam <= 1.00001))
                {
                    if (d < di)
                    {
                        Pos = Nearest;
                        di  = d;
                        Id  = i;
                    }
                }
            }
            if ((di < 1e10) && (di <= SnapItem.Snapdist))
            {
                Plane1.Crossed = true;
                Plane1.Point   = Pos;
                Plane2.Crossed = true;
                Plane2.Point   = Pos;
                return(true);
            }

            xyzArray A = Plane1.getxyzArray();
            xyzArray B = Plane2.getxyzArray();

            if ((A != null) && (B != null))
            {
                return(IntersectLineWithLine(Plane1, Plane2, ViewLine));
            }



            return(false);
        }