GEOS  3.6.1
Rectangle.h
1 /**********************************************************************
2  *
3  * GEOS - Geometry Engine Open Source
4  * http://geos.osgeo.org
5  *
6  * Copyright (C) 2014 Mika Heiskanen <mika.heiskanen@fmi.fi>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU Lesser General Public Licence as published
10  * by the Free Software Foundation.
11  * See the COPYING file for more information.
12  *
13  **********************************************************************/
14 
15 #ifndef GEOS_OP_INTERSECTION_RECTANGLE_H
16 #define GEOS_OP_INTERSECTION_RECTANGLE_H
17 
18 #include <geos/export.h>
19 
20 #ifdef _MSC_VER
21 #pragma warning(push)
22 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
23 #endif
24 
25 // Forward declarations
26 namespace geos {
27  namespace geom {
28  class GeometryFactory;
29  class Geometry;
30  class Polygon;
31  class LinearRing;
32  }
33 }
34 
35 namespace geos {
36 namespace operation { // geos::operation
37 namespace intersection { // geos::operation::intersection
38 
51 class GEOS_DLL Rectangle
52 {
53  public:
54 
65  Rectangle(double x1, double y1, double x2, double y2);
66 
70  double xmin() const { return xMin; }
71 
76  double ymin() const { return yMin; }
77 
78 
83  double xmax() const { return xMax; }
84 
85 
90  double ymax() const { return yMax; }
91 
97  geom::Polygon* toPolygon(const geom::GeometryFactory &f) const;
98 
99  geom::LinearRing* toLinearRing(const geom::GeometryFactory &f) const;
100 
105  enum Position
106  {
107  Inside = 1,
108  Outside = 2,
109 
110  Left = 4,
111  Top = 8,
112  Right = 16,
113  Bottom = 32,
114 
115  TopLeft = Top|Left, // 12
116  TopRight = Top|Right, // 24
117  BottomLeft = Bottom|Left, // 36
118  BottomRight = Bottom|Right // 48
119  };
120 
127  static bool onEdge(Position pos)
128  {
129  return (pos > Outside);
130  }
131 
139  static bool onSameEdge(Position pos1, Position pos2)
140  {
141  return onEdge(Position(pos1 & pos2));
142  }
143 
151  Position position(double x, double y) const
152  {
153  // We assume the point to be inside and test it first
154  if(x>xMin && x<xMax && y>yMin && y<yMax)
155  return Inside;
156  // Next we assume the point to be outside and test it next
157  if(x<xMin || x>xMax || y<yMin || y>yMax)
158  return Outside;
159  // Slower cases
160  unsigned int pos = 0;
161  if(x==xMin)
162  pos |= Left;
163  else if(x==xMax)
164  pos |= Right;
165  if(y==yMin)
166  pos |= Bottom;
167  else if(y==yMax)
168  pos |= Top;
169  return Position(pos);
170  }
171 
179  {
180  switch(pos)
181  {
182  case BottomLeft:
183  case Left: return Top;
184  case TopLeft:
185  case Top: return Right;
186  case TopRight:
187  case Right: return Bottom;
188  case BottomRight:
189  case Bottom: return Left;
190  /* silences compiler warnings, Inside & Outside are not handled explicitly */
191  default: return pos;
192  }
193  }
194 
195  private:
196 
197  Rectangle();
198  double xMin;
199  double yMin;
200  double xMax;
201  double yMax;
202 
203 }; // class RectangleIntersection
204 
205 } // namespace geos::operation::intersection
206 } // namespace geos::operation
207 } // namespace geos
208 
209 #endif // GEOS_OP_INTERSECTION_RECTANGLE_H
static Position nextEdge(Position pos)
Next edge in clock-wise direction.
Definition: Rectangle.h:178
static bool onSameEdge(Position pos1, Position pos2)
Test if the given positions are on the same Rectangle edge.
Definition: Rectangle.h:139
static bool onEdge(Position pos)
Test if the given position is on a edge.
Definition: Rectangle.h:127
Represents a linear polygon, which may include holes.
Definition: Polygon.h:66
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition: GeometryFactory.h:67
Basic namespace for all GEOS functionalities.
Definition: IndexedNestedRingTester.h:25
Position
Position with respect to a clipping rectangle.
Definition: Rectangle.h:105
Models an OGC SFS LinearRing.
Definition: LinearRing.h:57
double xmax() const
Definition: Rectangle.h:83
double xmin() const
Definition: Rectangle.h:70
Position position(double x, double y) const
Establish position of coordinate with respect to a Rectangle.
Definition: Rectangle.h:151
double ymax() const
Definition: Rectangle.h:90
Clipping rectangle.
Definition: Rectangle.h:51
double ymin() const
Definition: Rectangle.h:76