Example #1
0
        private static IGeometry ClipPolygon(IPolygon clipper, IGeometry clippee)
        {
            if (clippee is IPolygon)
            {
                if (clipper is Polygon)
                {
                    ((Polygon)clipper).VerifyHoles();
                }
                if (clippee is Polygon)
                {
                    ((Polygon)clippee).VerifyHoles();
                }

                //if (SpatialAlgorithms.Algorithm.Contains(clippee, clipper))
                //    return clipper.Clone() as IGeometry;

                GeomPolygon clipperPolygon = new GeomPolygon(clipper);
                GeomPolygon clippeePolygon = new GeomPolygon((IPolygon)clippee);

                GeomPolygon res = clippeePolygon.Clip(ClipOperation.Intersection, clipperPolygon);
                return(res.ToPolygon());
            }
            if (clippee is IPoint)
            {
                if (Algorithm.Jordan(clipper, ((IPoint)clippee).X, ((IPoint)clippee).Y))
                {
                    return(clippee);
                }
                else
                {
                    return(null);
                }
            }
            return(null);
        }
Example #2
0
        private static IGeometry ClipEnvelope(Envelope envelope, IGeometry clippee)
        {
            if (envelope == null || clippee == null)
            {
                return(null);
            }
            if (double.IsInfinity(envelope.Width) ||
                double.IsInfinity(envelope.Height))
            {
                return(clippee);
            }

            IEnvelope geomEnv = clippee.Envelope;

            if (!envelope.Intersects(geomEnv))
            {
                return(null);
            }

            if (geomEnv.minx >= envelope.minx && geomEnv.maxx <= envelope.maxx &&
                geomEnv.miny >= envelope.miny && geomEnv.maxy <= envelope.maxy)
            {
                // Full included...
                return(clippee);
            }

            if (clippee is IMultiPoint)
            {
                // Point ist schon durch den oberen Test enthalten...
                IMultiPoint multipoint    = (IMultiPoint)clippee;
                MultiPoint  newMultiPoint = new MultiPoint();

                for (int i = 0; i < multipoint.PointCount; i++)
                {
                    IPoint point = ClipPoint2Envelope(envelope, multipoint[i]);
                    if (point != null)
                    {
                        newMultiPoint.AddPoint(point);
                    }
                }
                return(newMultiPoint);
            }
            if (clippee is IPolyline)
            {
                return(ClipPolyline2Envelope(envelope, (IPolyline)clippee));
            }
            if (clippee is IPolygon)
            {
                GeomPolygon clipperGeom = new GeomPolygon(envelope);
                GeomPolygon clippeeGeom = new GeomPolygon((IPolygon)clippee);

                GeomPolygon result = clippeeGeom.Clip(ClipOperation.Intersection, clipperGeom);
                int         x      = result.NofContours;
                return(result.ToPolygon());
            }
            return(null);
        }
Example #3
0
        public static System.Drawing.Drawing2D.GraphicsPath PerformClip(System.Drawing.Rectangle rect, System.Drawing.Drawing2D.GraphicsPath clippeePath)
        {
            System.Drawing.Drawing2D.GraphicsPath clipperPath = new System.Drawing.Drawing2D.GraphicsPath();
            clipperPath.AddPolygon(new System.Drawing.PointF[] {
                new System.Drawing.PointF(rect.Left, rect.Bottom),
                new System.Drawing.PointF(rect.Right, rect.Bottom),
                new System.Drawing.PointF(rect.Right, rect.Top),
                new System.Drawing.PointF(rect.Left, rect.Top)
            }
                                   );

            GeomPolygon clipper = new GeomPolygon(clipperPath);
            GeomPolygon clippee = new GeomPolygon(clippeePath);

            GeomPolygon result = clipper.Clip(ClipOperation.Intersection, clippee);

            return(result.ToGraphicsPath());
        }
Example #4
0
        public static GeomTristrip GeomPolygonToTristrip(GeomPolygon polygon)
        {
            Clip_tristrip Clip_strip = new Clip_tristrip();
            Clip_polygon  Clip_pol   = ClipWrapper.GeomPolygonTo_Clip_polygon(polygon);

            try
            {
                Polygon2Tristrip(ref Clip_pol, ref Clip_strip);
                GeomTristrip tristrip = ClipWrapper.Clip_strip_ToTristrip(Clip_strip);

                return(tristrip);
            }
            finally
            {
                ClipWrapper.Free_Clip_polygon(Clip_pol);
                ClipWrapper.FreeTristrip(ref Clip_strip);
            }
        }
Example #5
0
        private static GeomPolygon Clip_polygon_ToGeomPolygon(Clip_polygon Clip_polygon)
        {
            GeomPolygon polygon = new GeomPolygon();

            polygon.NofContours = Clip_polygon.num_contours;
            if (polygon.NofContours == 0)
            {
                return(new GeomPolygon());
            }

            polygon.ContourIsHole = new bool[polygon.NofContours];
            polygon.Contour       = new GeomVertexList[polygon.NofContours];
            short[] holeShort = new short[polygon.NofContours];
            IntPtr  ptr       = Clip_polygon.hole;

            Marshal.Copy(Clip_polygon.hole, holeShort, 0, polygon.NofContours);

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

            ptr = Clip_polygon.contour;
            for (int i = 0; i < polygon.NofContours; i++)
            {
                Clip_vertex_list Clip_vtx_list = (Clip_vertex_list)Marshal.PtrToStructure(ptr, typeof(Clip_vertex_list));
                polygon.Contour[i]             = new GeomVertexList();
                polygon.Contour[i].NofVertices = Clip_vtx_list.num_vertices;
                polygon.Contour[i].Vertex      = new GeomVertex[polygon.Contour[i].NofVertices];
                IntPtr ptr2 = Clip_vtx_list.vertex;
                for (int j = 0; j < polygon.Contour[i].NofVertices; j++)
                {
                    Clip_vertex Clip_vtx = (Clip_vertex)Marshal.PtrToStructure(ptr2, typeof(Clip_vertex));
                    polygon.Contour[i].Vertex[j].X = Clip_vtx.x;
                    polygon.Contour[i].Vertex[j].Y = Clip_vtx.y;

                    ptr2 = IntPtrPlus(ptr2, Marshal.SizeOf(Clip_vtx)); //(IntPtr)(((int)ptr2) + Marshal.SizeOf(Clip_vtx));
                }
                ptr = IntPtrPlus(ptr, Marshal.SizeOf(Clip_vtx_list));  //(IntPtr)(((int)ptr) + Marshal.SizeOf(Clip_vtx_list));
            }

            return(polygon);
        }
Example #6
0
        public static GeomPolygon Clip(ClipOperation operation, GeomPolygon subject_polygon, GeomPolygon clip_polygon)
        {
            Clip_polygon Clip_polygon         = new Clip_polygon();
            Clip_polygon Clip_subject_polygon = ClipWrapper.GeomPolygonTo_Clip_polygon(subject_polygon);
            Clip_polygon Clip_clip_polygon    = ClipWrapper.GeomPolygonTo_Clip_polygon(clip_polygon);

            try
            {
                ClipPolygon(ref Clip_subject_polygon, ref Clip_clip_polygon, operation, ref Clip_polygon);
                GeomPolygon polygon = ClipWrapper.Clip_polygon_ToGeomPolygon(Clip_polygon);

                return(polygon);
            }
            finally
            {
                ClipWrapper.Free_Clip_polygon(Clip_subject_polygon);
                ClipWrapper.Free_Clip_polygon(Clip_clip_polygon);
                ClipWrapper.FreePolygon(ref Clip_polygon);
            }
        }
Example #7
0
        public static GeomTristrip ClipToTristrip(ClipOperation operation, GeomPolygon subject_polygon, GeomPolygon clip_polygon)
        {
            Clip_tristrip Clip_strip           = new Clip_tristrip();
            Clip_polygon  Clip_subject_polygon = ClipWrapper.GeomPolygonTo_Clip_polygon(subject_polygon);
            Clip_polygon  Clip_clip_polygon    = ClipWrapper.GeomPolygonTo_Clip_polygon(clip_polygon);

            try
            {
                ClipTristrip(ref Clip_subject_polygon, ref Clip_clip_polygon, operation, ref Clip_strip);
                GeomTristrip tristrip = ClipWrapper.Clip_strip_ToTristrip(Clip_strip);

                return(tristrip);
            }
            finally
            {
                ClipWrapper.Free_Clip_polygon(Clip_subject_polygon);
                ClipWrapper.Free_Clip_polygon(Clip_clip_polygon);
                ClipWrapper.FreeTristrip(ref Clip_strip);
            }
        }
Example #8
0
        /*
         *      public static void SavePolygon( string filename, bool writeHoleFlags, GeomPolygon polygon )
         *      {
         *              Clip_polygon Clip_polygon = ClipWrapper.PolygonTo_Clip_polygon( polygon );
         *
         *              IntPtr fp = fopen( filename, "wb" );
         *              Clip_write_polygon( fp, writeHoleFlags?((int)1):((int)0), ref Clip_polygon );
         *              fclose( fp );
         *
         *              ClipWrapper.Free_Clip_polygon( Clip_polygon );
         *      }
         *
         *      public static GeomPolygon ReadPolygon( string filename, bool readHoleFlags )
         *      {
         *              Clip_polygon Clip_polygon = new Clip_polygon();
         *
         *              IntPtr fp = fopen( filename, "rb" );
         *              Clip_read_polygon( fp, readHoleFlags?((int)1):((int)0), ref Clip_polygon );
         *              GeomPolygon polygon = Clip_polygon_ToPolygon( Clip_polygon );
         *              FreePolygon( ref Clip_polygon );
         *              fclose( fp );
         *
         *              return polygon;
         *      }
         * */

        private static Clip_polygon GeomPolygonTo_Clip_polygon(GeomPolygon polygon)
        {
            Clip_polygon Clip_pol = new Clip_polygon();

            Clip_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);
            }
            Clip_pol.hole = Marshal.AllocCoTaskMem(polygon.NofContours * Marshal.SizeOf(hole[0]));
            Marshal.Copy(hole, 0, Clip_pol.hole, polygon.NofContours);

            Clip_pol.contour = Marshal.AllocCoTaskMem(polygon.NofContours * Marshal.SizeOf(new Clip_vertex_list()));
            IntPtr ptr = Clip_pol.contour;

            for (int i = 0; i < polygon.NofContours; i++)
            {
                Clip_vertex_list Clip_vtx_list = new Clip_vertex_list();
                Clip_vtx_list.num_vertices = polygon.Contour[i].NofVertices;
                Clip_vtx_list.vertex       = Marshal.AllocCoTaskMem(polygon.Contour[i].NofVertices * Marshal.SizeOf(new Clip_vertex()));
                IntPtr ptr2 = Clip_vtx_list.vertex;
                for (int j = 0; j < polygon.Contour[i].NofVertices; j++)
                {
                    Clip_vertex Clip_vtx = new Clip_vertex();
                    Clip_vtx.x = polygon.Contour[i].Vertex[j].X;
                    Clip_vtx.y = polygon.Contour[i].Vertex[j].Y;
                    Marshal.StructureToPtr(Clip_vtx, ptr2, false);
                    ptr2 = IntPtrPlus(ptr2, Marshal.SizeOf(Clip_vtx)); // (IntPtr)(((int)ptr2) + Marshal.SizeOf(Clip_vtx));
                }
                Marshal.StructureToPtr(Clip_vtx_list, ptr, false);
                ptr = IntPtrPlus(ptr, Marshal.SizeOf(Clip_vtx_list)); //(IntPtr)(((int)ptr) + Marshal.SizeOf(Clip_vtx_list));
            }

            return(Clip_pol);
        }
Example #9
0
 public GeomPolygon Clip(ClipOperation operation, GeomPolygon polygon)
 {
     return(ClipWrapper.Clip(operation, this, polygon));
 }