GEOS 3.14.0dev
operation/intersection/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#pragma once
16
17#include <geos/export.h>
18#include <memory>
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
26namespace geos {
27namespace geom {
28class GeometryFactory;
29class Geometry;
30class Polygon;
31class LinearRing;
32}
33}
34
35namespace geos {
36namespace operation { // geos::operation
37namespace intersection { // geos::operation::intersection
38
51class GEOS_DLL Rectangle {
52public:
53
64 Rectangle(double x1, double y1, double x2, double y2);
65
69 double
70 xmin() const
71 {
72 return xMin;
73 }
74
79 double
80 ymin() const
81 {
82 return yMin;
83 }
84
85
90 double
91 xmax() const
92 {
93 return xMax;
94 }
95
96
101 double
102 ymax() const
103 {
104 return yMax;
105 }
106
112 std::unique_ptr<geom::Polygon> toPolygon(const geom::GeometryFactory& f) const;
113
114 std::unique_ptr<geom::LinearRing> toLinearRing(const geom::GeometryFactory& f) const;
115
120 enum Position {
121 Inside = 1,
122 Outside = 2,
123
124 Left = 4,
125 Top = 8,
126 Right = 16,
127 Bottom = 32,
128
129 TopLeft = Top | Left, // 12
130 TopRight = Top | Right, // 24
131 BottomLeft = Bottom | Left, // 36
132 BottomRight = Bottom | Right // 48
133 };
134
141 static bool
143 {
144 return (pos > Outside);
145 }
146
154 static bool
156 {
157 return onEdge(Position(pos1 & pos2));
158 }
159
167 Position
168 position(double x, double y) const
169 {
170 // We assume the point to be inside and test it first
171 if(x > xMin && x < xMax && y > yMin && y < yMax) {
172 return Inside;
173 }
174 // Next we assume the point to be outside and test it next
175 if(x < xMin || x > xMax || y < yMin || y > yMax) {
176 return Outside;
177 }
178 // Slower cases
179 unsigned int pos = 0;
180 if(x == xMin) {
181 pos |= Left;
182 }
183 else if(x == xMax) {
184 pos |= Right;
185 }
186 if(y == yMin) {
187 pos |= Bottom;
188 }
189 else if(y == yMax) {
190 pos |= Top;
191 }
192 return Position(pos);
193 }
194
201 static Position
203 {
204 switch(pos) {
205 case BottomLeft:
206 case Left:
207 return Top;
208 case TopLeft:
209 case Top:
210 return Right;
211 case TopRight:
212 case Right:
213 return Bottom;
214 case BottomRight:
215 case Bottom:
216 return Left;
217 /* silences compiler warnings, Inside & Outside are not handled explicitly */
218 default:
219 return pos;
220 }
221 }
222
223private:
224
225 Rectangle();
226 double xMin;
227 double yMin;
228 double xMax;
229 double yMax;
230
231}; // class RectangleIntersection
232
233} // namespace geos::operation::intersection
234} // namespace geos::operation
235} // namespace geos
236
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
Clipping rectangle.
Definition operation/intersection/Rectangle.h:51
double xmax() const
Definition operation/intersection/Rectangle.h:91
Rectangle(double x1, double y1, double x2, double y2)
Construct a clipping rectangle.
static Position nextEdge(Position pos)
Next edge in clock-wise direction.
Definition operation/intersection/Rectangle.h:202
std::unique_ptr< geom::Polygon > toPolygon(const geom::GeometryFactory &f) const
static bool onEdge(Position pos)
Test if the given position is on a Rectangle edge.
Definition operation/intersection/Rectangle.h:142
Position position(double x, double y) const
Establish position of coordinate with respect to a Rectangle.
Definition operation/intersection/Rectangle.h:168
static bool onSameEdge(Position pos1, Position pos2)
Test if the given positions are on the same Rectangle edge.
Definition operation/intersection/Rectangle.h:155
double xmin() const
Definition operation/intersection/Rectangle.h:70
double ymin() const
Definition operation/intersection/Rectangle.h:80
double ymax() const
Definition operation/intersection/Rectangle.h:102
Position
Position with respect to a clipping rectangle.
Definition operation/intersection/Rectangle.h:120
Basic namespace for all GEOS functionalities.
Definition geos.h:39