bool CheckSwapEdge(AM_Edge pedge) { // Controlla l'ammissibilità dello swap. // Uno spigolo può essere scambiato all'interno di un quadrangolo // se quest'ultimo è convesso. Point2d p1 = pedge.CcwEdge().DestCoord(); Point2d p2 = pedge.OrgCoord(); Point2d p3 = pedge.DestCoord(); Point2d p4 = pedge.Symm().CcwEdge().DestCoord(); Vector2d v1 = p4 - p2; Vector2d v2 = p1 - p2; v1.Unitize(); v2.Unitize(); if ((v1.X * v2.Y - v2.X * v1.Y) < AM_Util.FLT_EPSILON) { return(false); } Vector2d v3 = p1 - p3; Vector2d v4 = p4 - p3; v3.Unitize(); v4.Unitize(); if ((v3.X * v4.Y - v4.X * v3.Y) < AM_Util.FLT_EPSILON) { return(false); } return(true); }
bool OnEdge(Point2d p, AM_Edge edge) { double t1, t2, t3; double EPS = 1e-6; Point2d org = edge.OrgCoord(); t1 = (p - org).Length; t2 = (p - edge.DestCoord()).Length; if (t1 < EPS || t2 < EPS) { return(true); } Vector2d vector = org - edge.DestCoord(); t3 = vector.Length; if (t1 > t3 || t2 > t3) { return(false); } double a = vector.Y / t3; double b = -vector.X / t3; double c = -(a * org.X + b * org.Y); return(Math.Abs((a * p.X + b * p.Y + c)) < EPS); }
AM_Edge Locate(Point2d x) { AM_Edge e = m_StartingEdge; int actualEdge = 0; int numEdge = m_ArrayWEdges.Count; while (actualEdge++ <= numEdge) { if (x == e.OrgCoord() || x == e.DestCoord()) { return(e); } else if (AM_Edge.RightOf(x, e)) { e = e.Symm(); } else if (!AM_Edge.RightOf(x, e.Next)) { e = e.Next; } else if (!AM_Edge.RightOf(x, e.CcwEdge().Symm())) { e = e.CcwEdge().Symm(); } else { return(e); } } return(null); }
internal static void SwapEdge(AM_Edge pedge) { Debug.Assert(pedge.CcwFace().NumEdges == 3); Debug.Assert(pedge.CwFace().NumEdges == 3); AM_Edge pstart = pedge; do { Point2d p1 = pedge.CcwEdge().DestCoord(); Point2d p2 = pedge.OrgCoord(); Point2d p3 = pedge.DestCoord(); Point2d p4 = pedge.Symm().CcwEdge().DestCoord(); //int n1 = pedge.CcwEdge().Destination().Index; //int n2 = pedge.Origin().Index; //int n3 = pedge.Destination().Index; //int n4 = pedge.Symm().CcwEdge().Destination().Index; //int v1 = pedge.CcwEdge().Symm().m_nVertex; //int v2 = pedge.m_nVertex; //int v3 = pedge.Symm().m_nVertex; //int v4 = pedge.Symm().CcwEdge().Symm().m_nVertex; AM_Edge poldPrev = pedge.Prev; AM_Edge poldNext = pedge.Next; AM_Edge pnewNext = pedge.Prev.Symm(); AM_Edge pnewPrev = pnewNext.Prev; AM_Face poldFace = pedge.Face; pedge.Origin().Edge = poldNext; pedge.Vertex = pnewNext.Vertex; // Ripristina l'origine... pedge.Origin().Edge = pedge; // e aggiorna il suo puntatore // ripristina i collegamenti corretti poldPrev.Next = poldNext; poldNext.Prev = poldPrev; pnewPrev.Next = pedge; pnewNext.Prev = pedge; pedge.Next = pnewNext; pedge.Prev = pnewPrev; Debug.Assert(pedge.Origin() != pedge.Destination()); // parte dallo spigolo che definisce la faccia sinistra // e che è precedente in senso antiorario pnewNext = poldNext.Symm(); for (int i = 0; i < 3; i++) { pnewNext.Face = poldFace; poldFace[i] = pnewNext; pnewNext = pnewNext.CcwEdge(); } pedge = pedge.Symm(); } while (pstart != pedge); }
internal bool InnerTest(Point2d p) { Point2d x = p; // Per appartenere alla faccia devono essere tutte a sinistra dello spigolo for (int i = 0; i < 3; i++) { AM_Edge e = m_pEdges[i]; if (x.EpsilonEquals(e.OrgCoord(), AM_Util.FLT_EPSILON)) { return(true); } if (AM_Edge.RightOf(x, e)) { return(false); } } return(true); }
internal bool RecoverGenEdge(AM_Mesh2d mesh, int num, List <Point3d> AddArray, bool bStraight = false) { // se viene inserito un punto per aggiustare la conformità // il flag baddFlag diventa true bool baddFlag = false; if (!m_bFlagHole && num >= m_GenVertexArray.Count) { return(true); } int v1 = m_GenVertexArray[num]; int v2 = m_GenVertexArray[(num + 1) % m_GenVertexArray.Count]; if (v2 < v1) { v2 += GetNumVertex(); } AM_Edge pbase; AM_Edge pprev = pbase = Vertex(v1).Edge; for (int i = v1 + 1; i <= v2; i++) { AM_Vertex pV1 = Vertex((i - 1) % (GetNumVertex())); AM_Vertex pV2 = Vertex((i) % (GetNumVertex())); Point2d orgCoord = pV1.Coord; // si controlla che tutti i vertici siano in sequenza while (true) { Point2d baseCoord = pbase.DestCoord(); Point2d prvCoord = new Point2d(m_ArrayCoord[(i - 1) % (GetNumVertex())]); Point2d destCoord = new Point2d(m_ArrayCoord[i % (GetNumVertex())]); if (baseCoord == destCoord) { // il vertice è in sequenza: si continua con il successivo break; } else { pbase = pbase.Next; if (pbase == pprev) { // il ciclo dell'anello si è chiuso senza trovare il vertice // successivo; è necessario inserire un vertice in mezzeria del // lato mancante if (!bStraight) { // 1. Algoritmo di ripristino del bordo con l'aggiunta del punto medio baddFlag = true; // si segnala l'aggiunta di un vertice Point3d p1 = m_ArrayCoord[i - 1]; Point3d p2 = (m_ArrayCoord[i % (GetNumVertex())]); Point3d mid = 0.5 * (p1 + p2); Point3d insPt = new Point3d(mid.X, mid.Y, 0); // si inserisce un vertice nel mezzo del AM_Vertex pvertex; mesh.InsertPoint(new Point2d(insPt), insPt.Z, out pvertex); if (pvertex == null) { Debug.Assert(false); //throw 6; } InsertVertex(i, pvertex); v2++; AddArray.Add(insPt); // si ricomincia il controllo pbase = Vertex(i - 1).Edge; pprev = pbase; } else { // 2. Algoritmo di ripristino del bordo con swap di spigoli AM_Edge pdest = Vertex(i).Edge; Vector2d dir = destCoord - orgCoord; dir.Unitize(); var m = AM_Util.AffineMatrix(orgCoord, dir); while (pV1.FindEdge(pV2) == null) { bool bCoinc = false; AM_Edge pSearch = pbase; // Si controllano situazioni di appartenenza al lato da ripristinare do { double cosang = Vector2d.Multiply(pSearch.GetVersor(), dir); if (AM_Util.IsEqual(cosang, 1, AM_Util.FLT_EPSILON)) { // Lo spigolo appartiene già al lato da ripristinare InsertVertex(i, pSearch.Destination()); v2++; Point2d dc = pSearch.DestCoord(); AddArray.Add(new Point3d(dc.X, dc.Y, 0)); // si ricomincia il controllo pbase = Vertex(i - 1).Edge; pprev = pbase; bCoinc = true; break; } pSearch = pSearch.Next; } while (pSearch != pbase); if (bCoinc) { break; } // Trova il lato di partenza pSearch = pbase; while (!AM_Util.IsInside(pSearch.GetVector(), pSearch.Next.GetVector(), dir)) { pSearch = pSearch.Next; if (pSearch == pprev) { Debug.Assert(false); //mesh.ExportMesh("RecoverSt7.txt"); return(false); } } AM_Edge pStartEdge = pSearch.CcwEdge(); List <AM_Edge> swapArray = new List <AM_Edge>(); while (pStartEdge.Destination() != pV2) { Point2d o = pStartEdge.OrgCoord(); Point2d d = pStartEdge.DestCoord(); swapArray.Add(pStartEdge); pStartEdge = pStartEdge.Prev; Point2d pt = AM_Util.ToLocal(m, pStartEdge.DestCoord()); if (pt.Y < -AM_Util.FLT_EPSILON) { pStartEdge = pStartEdge.CcwEdge(); Debug.Assert(AM_Util.ToLocal(m, pStartEdge.DestCoord()).Y > 0); } } for (int j = 0; j < swapArray.Count; j++) { AM_Edge pSwapEdge = swapArray[j]; // Vengono ruotati gli spigoli all'interno if (AM_Util.CheckSwapEdge(pSwapEdge)) { Debug.Assert(pSearch.CcwFace() != null && pSearch.Next.CwFace() != null); Debug.Assert(pSwapEdge.CcwFace() != null && pSwapEdge.CwFace() != null); AM_Face.SwapEdge(pSwapEdge); } } } } } } } pbase = Vertex(i % (GetNumVertex())).Edge; pprev = pbase; } return(baddFlag); }
// --- Impostazioni --- internal Point2d InsertPoint(AM_Mesh2d pSpaceFunction) { AM_Edge pRif = null; for (int i = 0; i < 3; i++) { if (GetNearTriangle(i) == null) { pRif = m_pEdges[i]; break; } if (GetNearTriangle(i).m_FaceType == EFaceType.FT_ACCEPTED) { pRif = m_pEdges[i]; } } if (pRif == null) { return(Vertex(0).Coord); } Point2d thirdPoint = Point2d.Origin; Point2d midPoint = 0.5 * (pRif.OrgCoord() + pRif.DestCoord()); Point2d directionPoint = m_CircumCenter; for (int i = 0; i < 3; i++) { if (Vertex(i) != pRif.Origin() && Vertex(i) != pRif.Destination()) { thirdPoint = Vertex(i).Coord; break; } } if (m_CircumCenter == midPoint) //triangolo rettangolo { directionPoint = thirdPoint; } double radius = TeoricRadius(midPoint, pSpaceFunction); double p = (pRif.OrgCoord() - pRif.DestCoord()).Length / 2; double q = (midPoint - m_CircumCenter).Length; if (radius < p) { radius = p; } if (q != 0) { double tmp = (p * p + q * q) / (2 * q); if (radius > tmp) { radius = tmp; } } Vector2d versor; if (AM_Edge.LeftOf(directionPoint, pRif) && AM_Edge.RightOf(thirdPoint, pRif) || AM_Edge.LeftOf(thirdPoint, pRif) && AM_Edge.RightOf(directionPoint, pRif)) { versor = midPoint - directionPoint; } else { versor = directionPoint - midPoint; } versor.Unitize(); double d = radius + Math.Sqrt(radius * radius - p * p); Point2d point = midPoint + d * versor; return(point); }
internal static bool LeftOf(Point2d x, AM_Edge pedge) { return(AM_Util.CCW(x, pedge.OrgCoord(), pedge.DestCoord())); }
internal bool InsertPoint(Point2d x, double space, out AM_Vertex pvertex) { pvertex = null; AM_Face face = null; // Localizza uno spigolo vicino AM_Edge edge = Locate(x); if (edge == null) { return(false); } // Localizza il triangolo che contiene il punto x // e imposta 'm_pStartingEdge', primo spigolo del triangolo o del quadrilatero // che deve essere riconnesso al punto x if (AM_Edge.LeftOf(x, edge)) { face = (AM_Face)(edge.CcwFace()); m_StartingEdge = edge.CcwEdge(); } else { face = (AM_Face)(edge.CwFace()); m_StartingEdge = edge.Symm().CcwEdge(); } if (face == null) { return(false); } // Verifica dell'eventuale esistenza del punto if (x == edge.OrgCoord()) { pvertex = edge.Origin(); return(false); } if (x == edge.DestCoord()) { pvertex = edge.Destination(); return(false); } Point2d[] v1 = { face.Vertex(0).Coord, face.Vertex(1).Coord, face.Vertex(2).Coord, }; //isOnEdge = OnEdge(x, edge); AM_Edge pOnEdge = OnFaceEdge(x, face); if (pOnEdge != null) { m_StartingEdge = pOnEdge.CcwEdge(); // il punto si trova su un contorno! AM_Face pCwFace = pOnEdge.CwFace(); AM_Face pCcwFace = pOnEdge.CcwFace(); if (pCwFace == null || pCcwFace == null) { return(false); } } // Il punto è all'interno di un triangolo o su uno spigolo if (face.FaceType == AM_Face.EFaceType.FT_ACTIVE) { DeleteActiveFace(face); } DeleteFace(face); if (pOnEdge != null) { // Cancella lo spigolo su cui si appoggia e // conseguentemente anche l'altro spigolo AM_Face pCwFace = pOnEdge.CwFace(); AM_Face pCcwFace = pOnEdge.CcwFace(); if (pCwFace != null && pCwFace.FaceType == AM_Face.EFaceType.FT_ACTIVE) { DeleteActiveFace(pCwFace); } if (pCcwFace != null && pCcwFace.FaceType == AM_Face.EFaceType.FT_ACTIVE) { DeleteActiveFace(pCcwFace); } DeleteEdge(pOnEdge); } // Inserisce il nuovo vertice nell'array globale pvertex = new AM_Vertex(x, 0, space); if (pvertex == null) { Debug.Assert(false); //throw -1; } int m_nVertex = m_ArrayVertexes.Count; pvertex.Index = m_ArrayVertexes.Count; m_ArrayVertexes.Add(pvertex); // Inserisce i nuovi triangoli (facce) edge = m_StartingEdge.CcwEdge(); int numEdge = (pOnEdge != null? 4 : 3); for (int ne = 0; ne < numEdge; ne++) { AM_Face new_face = new AM_Face(); if (new_face == null) { Debug.Assert(false); //throw -1; } AM_Edge actEdge = edge; edge = edge.CcwEdge(); int [] nCoord = { m_nVertex, actEdge.Vertex.Index, actEdge.DestVertex().Index }; AddFace(new_face, nCoord); if (m_bFlagClassific) { new_face.SetTriangleParameter(m_pSpaceFunction); Classific(new_face); } } // Esamina gli spigoli per assicurare che la condizione di // Delaunay sia soddisfatta edge = m_StartingEdge; m_StartingEdge = m_StartingEdge.CcwEdge(); do { //TRACE_EDGE(edge); AM_Edge t = edge.Prev; if (edge.CwFace() != null && AM_Edge.RightOf(t.DestCoord(), edge) && AM_Util.InCircle(edge.OrgCoord(), t.DestCoord(), edge.DestCoord(), x)) { //TRACE0("Faccia swap: "); //TRACE_EDGE(edge); Swap(edge); edge = edge.Prev; } else if (edge.Next == m_StartingEdge) { // Non ci sono più spigoli break; } else { // Recupera un altro spigolo sospetto edge = edge.Next.CwEdge(); } } while (true); return(true); }