Esempio n. 1
0
        private void AddTransitionVertexes(AM_Vertex v, List <int> transitionVts)
        {
            if ((v.Flag & 0x02) == 0)
            {
                v.Flag |= 0x02;
                transitionVts.Add(v.Index);

                AM_Edge start_edge = v.Edge;
                AM_Edge edge       = start_edge.Next;

                while (edge != start_edge)
                {
                    if ((edge.Flag & 0x04) == 0)
                    {
                        edge.Flag |= 0x04;
                        var dest = edge.Destination();
                        if ((dest.Flag & 0x01) == 0)
                        {
                            AddTransitionVertexes(dest, transitionVts);
                        }
                    }
                    edge = edge.Next;
                }
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        internal AM_Edge FindEdge(AM_Vertex pVertex2)
        {
            AM_Edge pNext = m_Edge;

            do
            {
                if (pNext.Destination() == pVertex2)
                {
                    return(pNext);
                }
                pNext = pNext.Next;
            } while (pNext != m_Edge);
            return(null);
        }
Esempio n. 4
0
        void RelaxMesh()
        {
            // Esegue la procedura di "Mesh Relaxation"
            bool bPrev = m_bFlagClassific;

            m_bFlagClassific = false;
            for (int DiffDegree = 3; DiffDegree >= 2; DiffDegree--)
            {
                for (int i = 0; i < m_ArrayWEdges.Count; i++)
                {
                    AM_Edge edge = m_ArrayWEdges[i].Edge();
                    if (edge.CcwFace() != null && edge.CwFace() != null)
                    {
                        AM_Vertex [] Vertex = { edge.Origin(),
                                                edge.Destination(),
                                                edge.Next.Destination(),
                                                edge.Prev.Destination(), };
                        double []    degree = { 0, 0, 0, 0 };
                        for (int j = 0; j < degree.Length; j++)
                        {
                            degree[j] = Vertex[j].Degree();
                        }

                        double R = 0;
                        for (int j = 0; j < 4; j++)
                        {
                            R += (6 - degree[j]) * (6 - degree[j]);
                        }

                        // aggiorna il grado con l'ipotesi do swap
                        degree[0] -= 1;
                        degree[1] -= 1;
                        degree[2] += 1;
                        degree[3] += 1;

                        double R1 = 0;
                        for (int j = 0; j < 4; j++)
                        {
                            R1 += (6 - degree[j]) * (6 - degree[j]);
                        }

                        if (R - R1 >= DiffDegree)
                        {
                            Swap(edge);
                        }
                    }
                }
            }
            m_bFlagClassific = bPrev;
        }
Esempio n. 5
0
        AM_Edge FindEdge(AM_Vertex org, AM_Vertex dest)
        {
            AM_Edge pstart = org.Edge;

            if (pstart != null)
            {
                AM_Edge pnext = pstart;
                do
                {
                    if (pnext.Destination() == dest)
                    {
                        return(pnext); // lo spigolo esiste già
                    }
                    pnext = pnext.Next;
                } while (pnext != pstart);
            }
            return(null);
        }
Esempio n. 6
0
        internal bool CheckWEdge()
        {
            for (int i = 0; i < m_ArrayWEdges.Count; i++)
            {
                AM_Edge edge = m_ArrayWEdges[i].Edge();
                Debug.Assert(edge.Origin() != edge.Destination());
            }

            for (int i = 0; i < m_ArrayFaces.Count; i++)
            {
                AM_Face edge = m_ArrayFaces[i];
                double  area = AM_Util.TriArea(edge[0].OrgCoord(),
                                               edge[1].OrgCoord(),
                                               edge[2].OrgCoord());
                Debug.Assert(area > 0);
            }

            return(true);
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        // --- Copia ---
        AM_Boundary CopyBoundary(AM_Mesh2d source, AM_Mesh2d dest,
                                 bool bSameGenVertex = true)
        {
            AM_Boundary pCopy = new AM_Boundary();

            pCopy.m_LoopIndex = m_LoopIndex;

            if (bSameGenVertex)
            {
                // Il numero dei vertici generati è lo stesso;
                // viene normalmente usato in caso di 'merge' tra due mesh adiacenti
                pCopy.m_ArrayCoord.Capacity = m_ArrayCoord.Count;
                for (int i = 0; i < m_ArrayCoord.Count; i++)
                {
                    pCopy.m_ArrayCoord[i] = m_ArrayCoord[i];
                }

                pCopy.m_GenVertexArray.Capacity = m_GenVertexArray.Count;
                for (int i = 0; i < m_GenVertexArray.Count; i++)
                {
                    pCopy.m_GenVertexArray[i] = m_GenVertexArray[i];
                }

                // Trova il primo vertice
                int destVertex = dest.ArrayVertexes.Count;
                int nVertex    = dest.AddVertex(new Point2d(m_ArrayCoord[0]), m_ArrayCoord[0].Z);
                Debug.Assert(nVertex < destVertex); // non vengono aggiunti vertici

                AM_Vertex pVertex = dest.ArrayVertexes[nVertex];
                pVertex.Flag |= 0x01;
                pCopy.m_ArrayVertex.Add(pVertex);

                for (int i = 1; i < m_ArrayCoord.Count; i++)
                {
                    Point2d ptDest    = new Point2d(m_ArrayCoord[i]);
                    AM_Edge pEdge     = pVertex.Edge;
                    AM_Edge pNextEdge = pEdge.Next;

                    while ((ptDest - pNextEdge.DestCoord()).Length > AM_Util.FLT_EPSILON)
                    {
                        if (pNextEdge == pEdge)
                        {
                            Debug.Assert(false);
                            nVertex = dest.AddVertex(ptDest, 0);
                            Debug.Assert(nVertex < destVertex);
                            pNextEdge = dest.ArrayVertexes[nVertex].Edge.Symm();
                            break;
                        }
                        pNextEdge = pNextEdge.Next;
                    }

                    pVertex       = pNextEdge.Destination();
                    pVertex.Flag |= 0x01;
                    pCopy.m_ArrayVertex.Add(pVertex);
                }

                Debug.Assert(pCopy.m_ArrayVertex.Count == m_ArrayVertex.Count);
            }
            else
            {
                // Il numero dei vertici generati è diverso;
                // viene normalmente usato in caso di ricostruzione di contorni
                pCopy.m_GenVertexArray.Capacity = m_GenVertexArray.Count;

                for (int i = 1; i < m_GenVertexArray.Count; i++)
                {
                    Point2d p0 = new Point2d(m_ArrayCoord[m_GenVertexArray[i - 1]]);
                    Point2d p1 = new Point2d(m_ArrayCoord[m_GenVertexArray[i]]);

                    AM_Vertex pV0 = dest.RangeSearch.Search(p0.X, p0.Y);
                    AM_Vertex pV1 = dest.RangeSearch.Search(p1.X, p1.Y);
                    Debug.Assert(pV0 != null && pV1 != null);

                    // La direzione è data dal vettore p0-p1
                    Vector2d vDir = (p1 - p0);
                    vDir.Unitize();

                    pCopy.m_GenVertexArray[i - 1] = pCopy.m_ArrayCoord.Count;

                    AM_Vertex pV = pV0;
                    while (pV != pV1)
                    {
                        pCopy.m_ArrayCoord.Add(new Point3d(pV.Coord.X, pV.Coord.Y, 0));
                        pCopy.m_ArrayVertex.Add(pV);

                        // Trova il vertice successivo
                        double minCos = -double.MaxValue;

                        AM_Edge pEdge    = pV.Edge;
                        AM_Edge pDirEdge = null;

                        do
                        {
                            double dirCos = pEdge.GetVersor() * vDir;
                            if (dirCos > minCos)
                            {
                                minCos   = dirCos;
                                pDirEdge = pEdge;
                            }

                            pEdge = pEdge.Next;
                        } while (pEdge != pV.Edge);

                        Debug.Assert(AM_Util.IsEqual(minCos, 1));
                        pV = pDirEdge.Destination();
                    }
                }
            }

            return(pCopy);
        }
Esempio n. 9
0
        // --- 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);
        }
Esempio n. 10
0
        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);
        }