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); }
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); }
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); }