Пример #1
0
        public static Geometry Intersection(Geometry source, Geometry dest)
        {
            List <List <PointF> > source_polygons = ConvertToPointF(source);
            List <List <PointF> > dest_polygons   = ConvertToPointF(dest);
            GpcPolygon            polygon         = Intersection(source_polygons, dest_polygons);

            if (polygon.Contour.Length == 0)
            {
                return(null);
            }
            if (polygon.Contour.Length > 2)
            {
                MultiPolygon ret = new MultiPolygon();
                polygon.Contour.ToList().ForEach(
                    a =>
                {
                    Polygon subret = new Polygon();
                    IList <EasyMap.Geometries.Point> vertices = subret.ExteriorRing.Vertices;
                    a.Vertex.ToList().ForEach(p => { vertices.Add(new EasyMap.Geometries.Point(p.X, p.Y)); });
                    ret.Polygons.Add(subret);
                });
                return(ret);
            }
            else
            {
                Polygon ret = new Polygon();
                IList <EasyMap.Geometries.Point> vertices = ret.ExteriorRing.Vertices;
                polygon.Contour[0].Vertex.ToList().ForEach(p => { vertices.Add(new EasyMap.Geometries.Point(p.X, p.Y)); });
                return(ret);
            }
        }
Пример #2
0
        private static List <GpcPolygon> ConvertGpcPolygon(Geometry geom)
        {
            List <GpcPolygon> list = new List <GpcPolygon>();

            if (geom is Polygon)
            {
                Polygon       polygon = geom as Polygon;
                List <PointF> ps      = (from p in polygon.ExteriorRing.Vertices select new PointF((float)p.X, (float)p.Y)).ToList();
                GraphicsPath  gp1     = new GraphicsPath();
                gp1.AddPolygon(ps.ToArray());
                GpcPolygon pp = new GpcPolygon(gp1);
                list.Add(pp);
            }
            else if (geom is MultiPolygon)
            {
                MultiPolygon polygons = geom as MultiPolygon;
                foreach (Polygon polygon in polygons.Polygons)
                {
                    List <PointF> ps  = (from p in polygon.ExteriorRing.Vertices select new PointF((float)p.X, (float)p.Y)).ToList();
                    GraphicsPath  gp1 = new GraphicsPath();
                    gp1.AddPolygon(ps.ToArray());
                    GpcPolygon pp = new GpcPolygon(gp1);
                    list.Add(pp);
                }
            }
            return(list);
        }
Пример #3
0
        public static void SavePolygon(string filename, bool writeHoleFlags, GpcPolygon polygon)
        {
            gpc_polygon gpc_polygon = GpcWrapper.PolygonTo_gpc_polygon(polygon);

            IntPtr fp = fopen(filename, "wb");

            gpc_write_polygon(fp, writeHoleFlags?((int)1):((int)0), ref gpc_polygon);
            fclose(fp);

            GpcWrapper.Free_gpc_polygon(gpc_polygon);
        }
Пример #4
0
        public static GpcPolygon Intersection(EasyMap.Geometries.Point[] source, EasyMap.Geometries.Point[] dest)
        {
            GraphicsPath gp1 = new GraphicsPath();
            //source.ForEach(a => { gp1.AddPolygon(a.ToArray()); });
            GpcPolygon   polygonA = new GpcPolygon(source);
            GraphicsPath gp2      = new GraphicsPath();
            //dest.ForEach(a => { gp2.AddPolygon(a.ToArray()); });
            GpcPolygon polygonB = new GpcPolygon(dest);
            GpcPolygon polygon  = polygonA.Clip(GpcOperation.Intersection, polygonB);

            return(polygon);
        }
Пример #5
0
        public static Tristrip PolygonToTristrip(GpcPolygon polygon)
        {
            gpc_tristrip gpc_strip = new gpc_tristrip();
            gpc_polygon  gpc_pol   = GpcWrapper.PolygonTo_gpc_polygon(polygon);

            gpc_polygon_to_tristrip(ref gpc_pol, ref gpc_strip);
            Tristrip tristrip = GpcWrapper.gpc_strip_ToTristrip(gpc_strip);

            GpcWrapper.Free_gpc_polygon(gpc_pol);
            GpcWrapper.gpc_free_tristrip(ref gpc_strip);

            return(tristrip);
        }
Пример #6
0
        //public static bool PolygnIsIn(List<PointF> source, List<PointF> dest)
        //{
        //    GpcPolygon polygon = Intersection(source, dest);
        //    return PolygnIsIn(dest, polygon);
        //}

        public static GpcPolygon Intersection(List <List <PointF> > source, List <List <PointF> > dest)
        {
            GraphicsPath gp1 = new GraphicsPath();

            source.ForEach(a => { gp1.AddPolygon(a.ToArray()); });
            GpcPolygon   polygonA = new GpcPolygon(gp1);
            GraphicsPath gp2      = new GraphicsPath();

            dest.ForEach(a => { gp2.AddPolygon(a.ToArray()); });
            GpcPolygon polygonB = new GpcPolygon(gp2);
            GpcPolygon polygon  = polygonA.Clip(GpcOperation.Intersection, polygonB);

            return(polygon);
        }
Пример #7
0
        public static GpcPolygon ReadPolygon(string filename, bool readHoleFlags)
        {
            gpc_polygon gpc_polygon = new gpc_polygon();

            IntPtr fp = fopen(filename, "rb");

            gpc_read_polygon(fp, readHoleFlags?((int)1):((int)0), ref gpc_polygon);
            GpcPolygon polygon = gpc_polygon_ToPolygon(gpc_polygon);

            gpc_free_polygon(ref gpc_polygon);
            fclose(fp);

            return(polygon);
        }
Пример #8
0
        public static GpcPolygon Clip(GpcOperation operation, GpcPolygon subject_polygon, GpcPolygon clip_polygon)
        {
            gpc_polygon gpc_polygon         = new gpc_polygon();
            gpc_polygon gpc_subject_polygon = GpcWrapper.PolygonTo_gpc_polygon(subject_polygon);
            gpc_polygon gpc_clip_polygon    = GpcWrapper.PolygonTo_gpc_polygon(clip_polygon);

            gpc_polygon_clip(operation, ref gpc_subject_polygon, ref gpc_clip_polygon, ref gpc_polygon);
            GpcPolygon polygon = GpcWrapper.gpc_polygon_ToPolygon(gpc_polygon);

            GpcWrapper.Free_gpc_polygon(gpc_subject_polygon);
            GpcWrapper.Free_gpc_polygon(gpc_clip_polygon);
            GpcWrapper.gpc_free_polygon(ref gpc_polygon);

            return(polygon);
        }
Пример #9
0
        public static Tristrip ClipToTristrip(GpcOperation operation, GpcPolygon subject_polygon, GpcPolygon clip_polygon)
        {
            gpc_tristrip gpc_strip           = new gpc_tristrip();
            gpc_polygon  gpc_subject_polygon = GpcWrapper.PolygonTo_gpc_polygon(subject_polygon);
            gpc_polygon  gpc_clip_polygon    = GpcWrapper.PolygonTo_gpc_polygon(clip_polygon);

            gpc_tristrip_clip(operation, ref gpc_subject_polygon, ref gpc_clip_polygon, ref gpc_strip);
            Tristrip tristrip = GpcWrapper.gpc_strip_ToTristrip(gpc_strip);

            GpcWrapper.Free_gpc_polygon(gpc_subject_polygon);
            GpcWrapper.Free_gpc_polygon(gpc_clip_polygon);
            GpcWrapper.gpc_free_tristrip(ref gpc_strip);

            return(tristrip);
        }
Пример #10
0
        private static gpc_polygon PolygonTo_gpc_polygon(GpcPolygon polygon)
        {
            gpc_polygon gpc_pol = new gpc_polygon();

            gpc_pol.num_contours = polygon.NofContours;

            int[] hole = new int[polygon.NofContours];
            for (int i = 0; i < polygon.NofContours; i++)
            {
                hole[i] = (polygon.ContourIsHole[i] ? 1 : 0);
            }
            if (polygon.NofContours == 0)
            {
                //gpc_pol.hole = 0;
            }
            else
            {
                gpc_pol.hole = Marshal.AllocCoTaskMem(polygon.NofContours * Marshal.SizeOf(hole[0]));
            }
            if (polygon.NofContours > 0)
            {
                Marshal.Copy(hole, 0, gpc_pol.hole, polygon.NofContours);
                gpc_pol.contour = Marshal.AllocCoTaskMem(polygon.NofContours * Marshal.SizeOf(new gpc_vertex_list()));
            }
            IntPtr ptr = gpc_pol.contour;

            for (int i = 0; i < polygon.NofContours; i++)
            {
                gpc_vertex_list gpc_vtx_list = new gpc_vertex_list();
                gpc_vtx_list.num_vertices = polygon.Contour[i].NofVertices;
                gpc_vtx_list.vertex       = Marshal.AllocCoTaskMem(polygon.Contour[i].NofVertices * Marshal.SizeOf(new gpc_vertex()));
                IntPtr ptr2 = gpc_vtx_list.vertex;
                for (int j = 0; j < polygon.Contour[i].NofVertices; j++)
                {
                    gpc_vertex gpc_vtx = new gpc_vertex();
                    gpc_vtx.x = polygon.Contour[i].Vertex[j].X;
                    gpc_vtx.y = polygon.Contour[i].Vertex[j].Y;
                    Marshal.StructureToPtr(gpc_vtx, ptr2, false);
                    ptr2 = (IntPtr)(((int)ptr2) + Marshal.SizeOf(gpc_vtx));
                }
                Marshal.StructureToPtr(gpc_vtx_list, ptr, false);
                ptr = (IntPtr)(((int)ptr) + Marshal.SizeOf(gpc_vtx_list));
            }

            return(gpc_pol);
        }
Пример #11
0
        private static GpcPolygon gpc_polygon_ToPolygon(gpc_polygon gpc_polygon)
        {
            GpcPolygon polygon = new GpcPolygon();

            polygon.NofContours   = gpc_polygon.num_contours;
            polygon.ContourIsHole = new bool[polygon.NofContours];
            polygon.Contour       = new VertexList[polygon.NofContours];
            int[]  holeInt = new int[polygon.NofContours];
            IntPtr ptr     = gpc_polygon.hole;

            if (polygon.NofContours > 0)
            {
                Marshal.Copy(gpc_polygon.hole, holeInt, 0, polygon.NofContours);
            }

            for (int i = 0; i < polygon.NofContours; i++)
            {
                polygon.ContourIsHole[i] = (holeInt[i] != 0);
            }

            ptr = gpc_polygon.contour;
            for (int i = 0; i < polygon.NofContours; i++)
            {
                gpc_vertex_list gpc_vtx_list = (gpc_vertex_list)Marshal.PtrToStructure(ptr, typeof(gpc_vertex_list));
                polygon.Contour[i]             = new VertexList();
                polygon.Contour[i].NofVertices = gpc_vtx_list.num_vertices;
                polygon.Contour[i].Vertex      = new Vertex[polygon.Contour[i].NofVertices];
                IntPtr ptr2 = gpc_vtx_list.vertex;
                for (int j = 0; j < polygon.Contour[i].NofVertices; j++)
                {
                    gpc_vertex gpc_vtx = (gpc_vertex)Marshal.PtrToStructure(ptr2, typeof(gpc_vertex));
                    polygon.Contour[i].Vertex[j].X = gpc_vtx.x;
                    polygon.Contour[i].Vertex[j].Y = gpc_vtx.y;

                    ptr2 = (IntPtr)(((int)ptr2) + Marshal.SizeOf(gpc_vtx));
                }
                ptr = (IntPtr)(((int)ptr) + Marshal.SizeOf(gpc_vtx_list));
            }

            return(polygon);
        }
Пример #12
0
        private static bool PolygnIsIn(List <PointF> dest, GpcPolygon polygon)
        {
            GraphicsPath gp2 = new GraphicsPath();

            gp2.AddPolygon(dest.ToArray());
            List <PointF> dps = new List <PointF>();
            List <PointF> sps = new List <PointF>();

            polygon.Contour.ToList().ForEach(a => { dps.AddRange(a.ToPoints()); });
            polygon.Contour.ToList().ForEach(a => { sps.AddRange(a.ToPoints()); });
            sps.ForEach(a =>
            {
                int index = dps.FindIndex(d => { return(a.X == d.X && a.Y == d.Y); });
                if (index >= 0)
                {
                    dps.RemoveAt(index);
                }
            });
            bool find = true;

            while (find && dps.Count > 0)
            {
                find = false;
                for (int j = 1; j < dps.Count; j++)
                {
                    if (dps[0].X == dps[j].X && dps[0].Y == dps[j].Y)
                    {
                        find = true;
                        dps.RemoveAt(j);
                        dps.RemoveAt(0);
                        break;
                    }
                }
            }
            return(dps.Count == 0);
        }
Пример #13
0
 public GpcPolygon Clip(GpcOperation operation, GpcPolygon polygon)
 {
     return(GpcWrapper.Clip(operation, this, polygon));
 }
Пример #14
0
 public Tristrip ClipToTristrip(GpcOperation operation, GpcPolygon polygon)
 {
     return(GpcWrapper.ClipToTristrip(operation, this, polygon));
 }