Exemplo n.º 1
0
        /**Check if projecting/extruding the passed polyline, an artifact will be generated**/
        public static bool checkArtifacts(Polyline polyHole)
        {
            //TODO: IMprove this, maybe check directly with y coord is not good enough
            Plane projection = ((InitialPolyline)polyHole).generateNormalPlane();

            //As its a hole polyline, first and second half are symmetric, there is need to just check one
            //If one half projected does not has all vertices on descendant or ascendant order, it will sure generate an aritfact
            if (Geometry.Utils.getPlaneProjection(projection, polyHole.getVertex(0).getPosition()).y <
                Geometry.Utils.getPlaneProjection(projection, polyHole.getVertex(1).getPosition()).y)                     //ascendant
            {
                for (int i = 1; i < polyHole.getSize() / 2; ++i)
                {
                    if (Geometry.Utils.getPlaneProjection(projection, polyHole.getVertex(i).getPosition()).y >
                        Geometry.Utils.getPlaneProjection(projection, polyHole.getVertex(i + 1).getPosition()).y)
                    {
                        return(true);
                    }
                }
            }
            else                 //descendent
            {
                for (int i = 1; i < polyHole.getSize() / 2; ++i)
                {
                    if (Geometry.Utils.getPlaneProjection(projection, polyHole.getVertex(i).getPosition()).y <
                        Geometry.Utils.getPlaneProjection(projection, polyHole.getVertex(i + 1).getPosition()).y)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        /** Closes a polyline by triangulating all it's vertices with it's baricenter, visualizing it as a polygon FROM INSIDE**/
        public void closePolyline(Polyline poly)
        {
            /*Vertex baricenter = new Vertex ();
             * baricenter.setPosition (poly.calculateBaricenter ());
             * baricenter.setUV (poly.calculateBaricenterUV());
             * int baricenterIndex = getNumVertices();
             * addVertex (baricenter);
             * addHoleIndex (baricenterIndex);//Special case when closing after a hole, helps to disimulate triangulation
             * for (int i = 0; i < poly.getSize(); ++i) {
             *      //Left-hand!!
             *      addTriangle(poly.getVertex (i).getIndex(), poly.getVertex (i + 1).getIndex(), baricenterIndex);
             * }*/

            Polyline closePoly = new Polyline(poly.getSize());

            Vector3 baricenterPos = poly.calculateBaricenter();
            float   baricenterUVY = poly.getVertex(0).getUV().y + (poly.computeRadius() / AbstractGenerator.UVfactor); //Same y for all by construction

            for (int i = 0; i < poly.getSize(); ++i)                                                                   //Generate the new vertices
            //Add vertex to polyline
            {
                closePoly.getVertex(i).setPosition(baricenterPos);
                //Add the index to vertex
                closePoly.getVertex(i).setIndex(getNumVertices() + i);
                //Add UV
                closePoly.getVertex(i).setUV(new Vector2(poly.getVertex(i).getUV().x, baricenterUVY));
                //addHoleIndex (getNumVertices () + i);
            }
            addPolyline(closePoly);
            triangulatePolylines(poly, closePoly);              //Could improve this to generate the half number of triangles, due to closePoly has vertices on same position
        }
Exemplo n.º 3
0
 /** Seen from Outside triangulation. Makes the triangulation between two polylines with same size. The order of
  * the vertices has to be the same. This is used then for neighbour polylines **/
 public void triangulatePolylinesOutside(Polyline pol1, Polyline pol2)
 {
     if (pol1.getSize() != pol2.getSize())                 //TODO : throw exception
     {
         Debug.Log("The two polylines do not have the same length!");
         return;
     }
     for (int i = 0; i < pol1.getSize(); ++i)                                                                 //don't triangulate between last and first vertex as have same position
     {
         triangulateQuad(pol1.getVertex(i + 1), pol1.getVertex(i), pol2.getVertex(i + 1), pol2.getVertex(i)); //Clockwise
         //triangulateQuad(pol1.getVertex(i), pol1.getVertex(i+1), pol2.getVertex(i), pol2.getVertex(i+1));//COunter- Clockwise
     }
 }
Exemplo n.º 4
0
 /** Seen from Inside triangulation. Makes the triangulation between two polylines with same size. The order of
  * the vertices has to be the same. This is used then for neighbour polylines **/
 public void triangulatePolylines(Polyline pol1, Polyline pol2)
 {
     if (pol1.getSize() != pol2.getSize())                 //TODO : throw exception
     {
         Debug.Log("The two polylines do not have the same length!");
         return;
     }
     for (int i = 0; i < pol1.getSize(); ++i)
     {
         triangulateQuad(pol1.getVertex(i), pol1.getVertex(i + 1), pol2.getVertex(i), pol2.getVertex(i + 1));            //Clockwise
         //triangulateQuad(pol1.getVertex(i+1), pol1.getVertex(i), pol2.getVertex(i+1),pol2.getVertex(i));//Counter-Clockwise
     }
 }
Exemplo n.º 5
0
 //******** Other functions ********//
 /** Adds a polyline to the mesh **/
 public void addPolyline(Polyline p)
 {
     for (int i = 0; i < p.getSize(); ++i)
     {
         addVertex(p.getVertex(i));
     }
 }
Exemplo n.º 6
0
 public Polyline(Polyline original)
 {
     mVertices = new Vertex[original.getSize()];
     for (int i = 0; i < getSize(); ++i)
     {
         mVertices [i] = new Vertex(original.getVertex(i));
     }
     minRadius = original.minRadius;
     maxRadius = original.maxRadius;
 }
Exemplo n.º 7
0
 /** Creates the mesh intializating it with a polyline's vertices **/
 public Mesh(Polyline iniPol)
 {
     mVertices    = new List <Vector3>();
     mTriangles   = new List <int>();
     mUVs         = new List <Vector2> ();
     mholeIndices = new HashSet <int> ();
     for (int i = 0; i < iniPol.getSize(); ++i)
     {
         addVertex(iniPol.getVertex(i));
     }
 }
Exemplo n.º 8
0
        /**Duplicates a polyline and triangulate between the original and the copy **/
        public Polyline duplicatePoly(Polyline originPoly)
        {
            Polyline newPoly = new Polyline(originPoly);

            for (int i = 0; i < originPoly.getSize(); ++i)
            {
                newPoly.getVertex(i).setIndex(getNumVertices() + i);
                newPoly.getVertex(i).setInHole(false);
            }
            addPolyline(newPoly);
            triangulatePolylines(originPoly, newPoly);
            return(newPoly);
        }
Exemplo n.º 9
0
        /** Closes a polyline by triangulating all it's vertices with it's baricenter, visualizing it as a polygon FROM OUTSIDE**/
        public void closePolylineOutside(Polyline poly)
        {
            Vertex baricenter = new Vertex();

            baricenter.setPosition(poly.calculateBaricenter());
            baricenter.setUV(poly.calculateBaricenterUV());
            int baricenterIndex = getNumVertices();

            addVertex(baricenter);
            //addHoleIndex (baricenterIndex); //Special case when closing after a hole, helps to disimulate triangulation
            for (int i = 0; i < poly.getSize(); ++i)
            {
                //Left-hand!!
                addTriangle(poly.getVertex(i + 1).getIndex(), poly.getVertex(i).getIndex(), baricenterIndex);
            }
        }
Exemplo n.º 10
0
        /** Triangulate between two polylines as if they where convex hulls, very similar to D&C merge
         * (Gift wrapping idea) from 3D Convex hull theory. Used for a tunnel start **/
        public void triangulateTunnelStart(Polyline originPoly, Polyline destinyPoly)           //TODO:some times does not triangulate correctly!
        //Start from a line between the first one of each polyline, by construction one is generated from the projection of the other=>NO as it has changed(smooth)
        {
            Vertex a      = originPoly.getVertex(0);
            Vertex b      = destinyPoly.getVertex(0);
            int    aIndex = 1;
            int    bIndex = 1;

            /*//For getting the starting line, using minimum distance points
             * //int j = 0;
             * float minDistance = Vector3.Distance (a.getPosition(), b.getPosition());
             * for (int j = 0; j < originPoly.getSize (); ++j) {
             *      for (int i = 0; i < destinyPoly.getSize (); ++i) {
             *              float auxDistance = Vector3.Distance (originPoly.getVertex(j).getPosition (), destinyPoly.getVertex (i).getPosition ());
             *              if (auxDistance < minDistance) {
             *                      a = originPoly.getVertex (j);
             *                      b = destinyPoly.getVertex (i);
             *                      minDistance = auxDistance;
             *                      bIndex = i + 1;
             *                      aIndex = j + 1;
             *              }
             *      }
             * }*/
            //Find lowerest tangent
            //The lower point of origin will be either the first or last vertex(by construction)

            /*if (a.getPosition ().y > originPoly.getVertex (-1).getPosition ().y) {
             *      a = originPoly.getVertex (-1);
             *      aIndex = 0;
             * }
             * int j = 0;
             * float minPos = float.MaxValue;
             * for (int i = 0; i < destinyPoly.getSize (); ++i) {
             *      float auxPos = destinyPoly.getVertex (i).getPosition ().y;
             *      if (auxPos < minPos) {
             *              b = destinyPoly.getVertex (i);
             *              minPos = auxPos;
             *              bIndex = i + 1;
             *      }
             * }*/
            const int maxFoll = 5;
            int       bFoll = 0, aFoll = 0;       //To controll not the same sida is always the winner, as it produces artifacts some cases
            int       bIter = 0, aIter = 0;
            bool      aWins = false;

            while (aIter < originPoly.getSize() && bIter < destinyPoly.getSize())
            {
                //This line ab will be triangulated with a point c either from one polyliline or the other, depending the one that has
                //smallest angle with ab line(A-winner or B-winner).c It's always an adjacent vertex, we can use they are clockwise sorted(lucky!)
                Vector3 ab = b.getPosition() - a.getPosition();
                //Check the B polylilne candidate
                Vertex bWinner = destinyPoly.getVertex(bIndex);
                float  bAngle  = Vector3.Angle(-ab, bWinner.getPosition() - b.getPosition());
                //Check the A polylilne candidate
                Vertex aWinner = originPoly.getVertex(aIndex);
                float  aAngle  = Vector3.Angle(ab, aWinner.getPosition() - a.getPosition());
                //aAngle = Vector3.Distance (aWinner.getPosition (), b.getPosition ());
                //bAngle = Vector3.Distance (bWinner.getPosition (), a.getPosition ());

                //Check not same polyline accumulates too much wins
                if (aFoll > maxFoll)
                {
                    aWins = false;
                    aFoll = 0;
                }
                else if (bFoll > maxFoll)
                {
                    aWins = true;
                    bFoll = 0;
                }
                else
                {
                    aWins = aAngle < bAngle;
                }

                //If it's A-winner(from first poly) a=c, if it's from b-Winner b = c
                if (aWins)                   //A wins!
                {
                    addTriangle(a.getIndex(), aWinner.getIndex(), b.getIndex());
                    a = aWinner;
                    ++aIndex;
                    ++aIter;
                    ++aFoll;
                }
                else                   //B wins!
                {
                    addTriangle(a.getIndex(), bWinner.getIndex(), b.getIndex());
                    b = bWinner;
                    ++bIndex;
                    ++bIter;
                    ++bFoll;
                }
            }            //Repeat until ab we arrive to some of the polylines start, then triangulate with a or b constant(depends on polyline)

            if (aIter >= originPoly.getSize())
            {
                while (bIter < destinyPoly.getSize())
                {
                    Vertex bWinner = destinyPoly.getVertex(bIndex);
                    addTriangle(a.getIndex(), bWinner.getIndex(), b.getIndex());
                    b = bWinner;
                    ++bIndex;
                    ++bIter;
                }
            }
            else if (bIter >= destinyPoly.getSize())
            {
                while (aIter < originPoly.getSize())
                {
                    Vertex aWinner = originPoly.getVertex(aIndex);
                    addTriangle(a.getIndex(), aWinner.getIndex(), b.getIndex());
                    a = aWinner;
                    ++aIndex;
                    ++aIter;
                }
            }

            //TODO: check strange artifacts cases,
            //may be procuduced by winner decision, maybe smallest angle is not the best choice(or checking worng angle)
        }