Example #1
0
        public void DrawEarsPolygon(CPolygonShape cutPolygon)
        {
            //draw ears:
            if (cutPolygon == null)
            {
                return;
            }

            for (int i = 0; i < cutPolygon.NumberOfPolygons; i++)
            {
                int     nPoints   = cutPolygon.Polygons(i).Length;
                Point[] tempArray = new Point[nPoints];
                for (int j = 0; j < nPoints; j++)
                {
                    tempArray[j].X = (int)cutPolygon.Polygons(i)[j].X;
                    tempArray[j].Y = (int)cutPolygon.Polygons(i)[j].Y;
                }

                Graphics gfx = pnlDraw.CreateGraphics();
                //fill triangles in different color
                int nBrush = i % 3;
                gfx.FillPolygon(m_aBrushes[nBrush], tempArray);
                Invalidate();
            }
        }
Example #2
0
    private void LoadWays()
    {
        foreach (XmlNode xmlnode in prs.SelectNodes("way"))
        {
            Way             way    = new Way();
            List <CPoint2D> points = new List <CPoint2D>();
            foreach (XmlNode nd in xmlnode.SelectNodes("nd"))
            {
                var id   = int.Parse(nd.Attributes["ref"].Value);
                var node = map.nodes[id];
                way.nodes.Add(node);
                points.Add(new CPoint2D(node.x, node.y));
            }
            way.points = points;
            var cpolyShape = new CPolygonShape(way.points.ToArray());
            cpolyShape.CutEar();

            foreach (XmlNode tg in xmlnode.SelectNodes("tag"))
            {
                way.tags.Add(new Tag {
                    k = tg.Attributes["k"].Value, v = tg.Attributes["v"].Value
                });
            }


            map.ways.Add(way);
        }
    }
Example #3
0
        /// <summary>
        /// Paint the triangles defined by the points
        /// </summary>
        /// <param name="points"></param>
        private LinkedList <Triangle> makeTriangles(LinkedList <Pair <double, double> > points)
        {
            if (points == null || points.Count == 0)
            {
                return(null);
            }

            CPoint2D[]            vertices  = new CPoint2D[points.Count];
            LinkedList <Triangle> triangles = new System.Collections.Generic.LinkedList <Triangle>();

            int index = 0;

            foreach (Pair <double, double> p in points)
            {
                vertices[index] = new CPoint2D(p.X, p.Y);
                index++;
            }

            CPolygonShape cutPolygon = new CPolygonShape(vertices);

            cutPolygon.CutEar();

            debugOut("Numer of polygons: " + cutPolygon.NumberOfPolygons);

            CPoint2D[] corners;

            for (int numPoly = 0; numPoly < cutPolygon.NumberOfPolygons; numPoly++)
            {
                #region find upper and lower
                corners = cutPolygon.Polygons(numPoly);

                Pair <double, double>[] corns = new Pair <double, double> [3];
                for (int cornIndex = 0; cornIndex < 3; cornIndex++)
                {
                    CPoint2D coern = corners[cornIndex];
                    corns[cornIndex] = new Pair <double, double>(coern.X, coern.Y);
                }

                Pair <Pair <double, double>, Pair <double, double> > pairResult = findUpperAndLower(new LinkedList <Pair <double, double> >(corns));

                Pair <double, double> upper = pairResult.X;
                Pair <double, double> lower = pairResult.Y;

                Triangle inside = new Triangle(corns, upper, lower);

                debugOut(inside.ToString());

                triangles.AddLast(inside);
                #endregion
            }
            return(triangles);
        }
Example #4
0
    private IEnumerator LoadMeshes()
    {
        Debug.Log("LoadMeshes");
        foreach (var way in map.ways)
        {
            Debug.Log("LoadWay");
            var cpolyShape = new CPolygonShape(way.points.ToArray());
            cpolyShape.CutEar();
            var       count     = cpolyShape.NumberOfPolygons * 3;
            Vector3[] vertices  = new Vector3[count];
            int[]     triangles = new int[count];
            int       ji        = 0;
            for (int i = 0; i < cpolyShape.NumberOfPolygons; i++)
            {
                var p = cpolyShape.Polygons(i);
                for (int j = 0; j < p.Length; j++)
                {
                    triangles[ji] = ji;;
                    vertices[ji]  = new Vector3(0, (float)p[j].Y, (float)p[j].X);
                    ji++;
                }
            }

            var g = new GameObject();
            var f = g.AddComponent <MeshFilter>();


            var info = g.AddComponent <Info>();
            info.tags = way.tags;

            var mesh = f.mesh = new Mesh();

            mesh.vertices  = vertices.ToArray();
            mesh.triangles = triangles.ToArray();
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            var r = g.AddComponent <MeshRenderer>();
            r.material = mat;

            g.AddComponent <BoxCollider>();
            yield return(null);
        }
        yield return(null);
    }
        public static List <List <Vector> > KonvexPolygons(List <Vector> input)
        {
            List <List <Vector> > result    = new List <List <Vector> >();
            List <Vector>         NoDoubles = new List <Vector>();

            foreach (Vector v in input)
            {
                if (!(NoDoubles.Count(p => p.Equals(v)) > 0))
                {
                    NoDoubles.Add(v);
                }
            }
            List <Vector> list = NoDoubles;


            int nVertices = list.Count;

            CPoint2D[] vertices = new CPoint2D[nVertices];
            for (int i = 0; i < nVertices; i++)
            {
                vertices[i] = new CPoint2D(list[i].X,
                                           list[i].Y);
            }
            CPolygonShape cutPolygon = new CPolygonShape(vertices);

            cutPolygon.CutEar();

            for (int i = 0; i < cutPolygon.NumberOfPolygons; i++)
            {
                int           nPoints = cutPolygon.Polygons(i).Length;
                List <Vector> polygon = new List <Vector>();
                for (int j = 0; j < nPoints; j++)
                {
                    polygon.Add(new Vector(cutPolygon.Polygons(i)[j].X, cutPolygon.Polygons(i)[j].Y, 0));
                }
                result.Add(polygon);
            }
            return(result);
        }
Example #6
0
        private void btnCut_Click(object sender, System.EventArgs e)
        {
            DrawOuterPolygon();

            int nVertices = m_aPolygon.Length;

            if (nVertices <= 0)
            {
                return;
            }

            CPoint2D[] vertices = new CPoint2D[nVertices];
            for (int i = 0; i < nVertices; i++)
            {
                vertices[i] = new CPoint2D(m_aPolygon[i].X,
                                           m_aPolygon[i].Y);
            }
            CPolygonShape cutPolygon = new CPolygonShape(vertices);

            cutPolygon.CutEar();

            DrawEarsPolygon(cutPolygon);
        }
Example #7
0
        public List <BodyElement> getTriangulatedShapes(CPolygonShape polygonShapes)
        {
            if (polygonShapes != null)
            {
                //Creer une liste de body elements
                List <BodyElement> elements = new List <BodyElement>();

                bool res = (bool)polygonShapes.CutEar();
                if (res == false)
                {
                    return(null);
                }

                //Recuperer le nombre d'elem deja present
                int nbElemExistants = this.coronaObject.PhysicsBody.BodyElements.Count;
                for (int i = 0; i < polygonShapes.NumberOfPolygons; i++)
                {
                    //Recuperer les polygons
                    CPoint2D[] tabCps = polygonShapes.Polygons(i);

                    //Recuperer le tableau de point associƩ
                    List <Point> pointsConverted = this.convertCPoint2DToPoint(tabCps);

                    //Creer un body elem
                    int indexElem = nbElemExistants;
                    nbElemExistants++;
                    string      name = "AUTO_SHAPE";
                    BodyElement elem = new BodyElement(indexElem, name, 0, 0, 0, pointsConverted);
                    elements.Add(elem);
                }

                return(elements);
            }

            return(null);
        }
        /// <summary>
        /// Creates activeListEditor vector and triangle list compatibles with mesh data.
        /// </summary>
        void InitializeMeshData()
        {
            List <CPoint2D> cPointList = new List <CPoint2D>();

            for (int i = 0; i < VectorListTarget.LocalVector3Coords.Count; i++)
            {
                var newCPoint2D = VectorListTarget.LocalVector3Coords[i].ToCPoint2D();
                cPointList.Add(newCPoint2D);
            }

            List <Vector3> verticesList  = new List <Vector3>();
            List <int>     trianglesList = new List <int>();

            var polygonShape = new CPolygonShape(cPointList.ToArray());

            polygonShape.CutEar();

            Vector3 newVector3;

            for (int i = 0; i < polygonShape.NumberOfPolygons; i++)
            {
                var polygonVertexes = polygonShape.Polygons(i);

                foreach (var polygonVertex in polygonVertexes)
                {
                    newVector3 = new Vector3((float)polygonVertex.X, (float)polygonVertex.Y, 0);

                    verticesList.Add(newVector3);

                    trianglesList.Add(verticesList.IndexOf(newVector3));
                }
            }

            Vertexes  = verticesList.ToArray();
            Triangles = trianglesList.ToArray();
        }
Example #9
0
        private void UpdateVertices()
        {
            m_verticalExaggeration = World.Settings.VerticalExaggeration;

            CPoint2D[] polygonVertices = new CPoint2D[m_polygonPoints.Length];
            for (int i = 0; i < polygonVertices.Length; i++)
            {
                polygonVertices[i] = new CPoint2D(m_polygonPoints[i].X, m_polygonPoints[i].Y);
            }

            CPolygonShape cutPolygon = new CPolygonShape(polygonVertices);

            cutPolygon.CutEar();

            ArrayList vertexList = new ArrayList();

            for (int i = 0; i < cutPolygon.NumberOfPolygons; i++)
            {
                int nPoints = cutPolygon.Polygons(i).Length;

                for (int j = 0; j < nPoints; j++)
                {
                    Point3d point = new Point3d(cutPolygon.Polygons(i)[j].X, cutPolygon.Polygons(i)[j].Y, 0);

                    foreach (Point3d sourcePoint in m_polygonPoints)
                    {
                        if (sourcePoint.X.Equals(point.X) &&
                            sourcePoint.Y.Equals(point.Y))
                        {
                            point.Z = sourcePoint.Z;
                            break;
                        }
                    }

                    vertexList.Add(point);
                }
            }

            if (m_extrudeHeight > 0)
            {
                CustomVertex.PositionNormalColored[] vertices = new CustomVertex.PositionNormalColored[vertexList.Count * 2];
                int polygonColor = Color.FromArgb(Opacity, m_polygonColor.R, m_polygonColor.G, m_polygonColor.B).ToArgb();
                int vertexOffset = vertices.Length / 2;

                // build bottom vertices
                for (int i = 0; i < vertices.Length / 2; i++)
                {
                    Point3d sphericalPoint = (Point3d)vertexList[i];
                    Vector3 xyzVector      = MathEngine.SphericalToCartesian(sphericalPoint.Y, sphericalPoint.X, World.EquatorialRadius + m_verticalExaggeration * (sphericalPoint.Z + m_distanceAboveSurface));

                    vertices[i].Color = polygonColor;
                    vertices[i].X     = xyzVector.X;
                    vertices[i].Y     = xyzVector.Y;
                    vertices[i].Z     = xyzVector.Z;
                }

                //build top vertices
                for (int i = vertexOffset; i < vertices.Length; i++)
                {
                    Point3d sphericalPoint = (Point3d)vertexList[i - vertexOffset];
                    Vector3 xyzVector      = MathEngine.SphericalToCartesian(sphericalPoint.Y, sphericalPoint.X, World.EquatorialRadius + m_verticalExaggeration * (sphericalPoint.Z + m_distanceAboveSurface + m_extrudeHeight));

                    vertices[i].Color = polygonColor;
                    vertices[i].X     = xyzVector.X;
                    vertices[i].Y     = xyzVector.Y;
                    vertices[i].Z     = xyzVector.Z;
                }

                m_vertices = vertices;
            }
            else
            {
                CustomVertex.PositionNormalColored[] vertices = new CustomVertex.PositionNormalColored[vertexList.Count];
                int polygonColor = Color.FromArgb(Opacity, m_polygonColor.R, m_polygonColor.G, m_polygonColor.B).ToArgb();

                for (int i = 0; i < vertices.Length; i++)
                {
                    Point3d sphericalPoint = (Point3d)vertexList[i];
                    Vector3 xyzVector      = MathEngine.SphericalToCartesian(sphericalPoint.Y, sphericalPoint.X, World.EquatorialRadius + m_verticalExaggeration * (sphericalPoint.Z + m_distanceAboveSurface));

                    vertices[i].Color = polygonColor;
                    vertices[i].X     = xyzVector.X;
                    vertices[i].Y     = xyzVector.Y;
                    vertices[i].Z     = xyzVector.Z;
                }

                m_vertices = vertices;
            }

            if (m_extrudeHeight > 0 || m_outline)
            {
                Point3d[] linePoints = new Point3d[m_polygonPoints.Length + 1];
                for (int i = 0; i < m_polygonPoints.Length; i++)
                {
                    linePoints[i] = m_polygonPoints[i];
                }

                linePoints[linePoints.Length - 1] = m_polygonPoints[0];

                m_lineFeature = new LineFeature(Name, World, linePoints, m_polygonColor);
                m_lineFeature.ExtrudeHeight          = m_extrudeHeight;
                m_lineFeature.DistanceAboveSurface   = m_distanceAboveSurface;
                m_lineFeature.ExtrudeUpwards         = m_extrudeUpwards;
                m_lineFeature.MinimumDisplayAltitude = m_minimumDisplayAltitude;
                m_lineFeature.MaximumDisplayAltitude = m_maximumDisplayAltitude;
                m_lineFeature.Opacity      = Opacity;
                m_lineFeature.Outline      = m_outline;
                m_lineFeature.OutlineColor = m_outlineColor;
            }
            else
            {
                if (m_lineFeature != null)
                {
                    m_lineFeature.Dispose();
                    m_lineFeature = null;
                }
            }
        }
Example #10
0
        public List <BodyElement> getTriangulatedShapes()
        {
            if (this.shapeBuilding != null)
            {
                //Creer une liste de body elements
                if (this.shapeBuilding.Count < 3)
                {
                    return(null);
                }

                List <BodyElement> elements = new List <BodyElement>();

                //Create a Cpolygon
                CPoint2D[] tabCpoint2D = getCPoint2D(this.shapeBuilding);

                CPolygonShape polygonShapes = new CPolygonShape(tabCpoint2D);
                //Generated
                bool res = (bool)polygonShapes.CutEar();
                if (res == false)
                {
                    return(null);
                }

                //Recuperer le nombre d'elem deja present
                int nbElemExistants = this.coronaObject.PhysicsBody.BodyElements.Count;
                for (int i = 0; i < polygonShapes.NumberOfPolygons; i++)
                {
                    //Recuperer les polygons
                    CPoint2D[] tabCps = polygonShapes.Polygons(i);

                    //Recuperer le tableau de point associƩ
                    List <Point> pointsConverted = this.convertCPoint2DToPoint(tabCps);

                    //Diviser les points en groupe de 8
                    List <List <Point> > listPolygones = new List <List <Point> >();
                    List <Point>         currentList   = new List <Point>();
                    for (int j = 0; j < pointsConverted.Count; j++)
                    {
                        if (currentList.Count == 8)
                        {
                            listPolygones.Add(currentList);
                            currentList = new List <Point>();
                            currentList.Add(pointsConverted[0]);
                            currentList.Add(pointsConverted[j - 1]);
                        }

                        currentList.Add(pointsConverted[j]);

                        if (j == pointsConverted.Count - 1)
                        {
                            listPolygones.Add(currentList);
                        }
                    }


                    //Creer un body elem par polygone
                    for (int j = 0; j < listPolygones.Count; j++)
                    {
                        int indexElem = nbElemExistants;
                        nbElemExistants++;
                        string      name = "AUTO_SHAPE";
                        BodyElement elem = new BodyElement(indexElem, name, 0, 0, 0, listPolygones[j]);
                        elements.Add(elem);
                    }
                }

                return(elements);
            }

            return(null);
        }