Complete Note#1 in the http://wiki.osgeo.org/wiki/GEOS_Provenance_Review to get out...
[geos.git] / src / operation / predicate / RectangleContains.cpp
blob6b27add7b01a7ae95c4376fbe2365c1074fbabd5
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2006 Refractions Research Inc.
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.
13 **********************************************************************
15 * Last port: operation/predicate/RectangleContains.java rev 1.5 (JTS-1.10)
17 **********************************************************************/
19 #include <geos/operation/predicate/RectangleContains.h>
20 #include <geos/geom/Geometry.h>
21 #include <geos/geom/Envelope.h>
22 #include <geos/geom/Point.h>
23 #include <geos/geom/Polygon.h>
24 #include <geos/geom/LineString.h>
25 #include <geos/geom/Coordinate.h>
26 #include <geos/geom/CoordinateSequence.h>
28 using namespace geos::geom;
30 namespace geos {
31 namespace operation { // geos.operation
32 namespace predicate { // geos.operation.predicate
34 bool
35 RectangleContains::contains(const Geometry& geom)
37 if ( ! rectEnv.contains(geom.getEnvelopeInternal()) )
38 return false;
40 // check that geom is not contained entirely in the rectangle boundary
41 if (isContainedInBoundary(geom))
42 return false;
44 return true;
47 /*private*/
48 bool
49 RectangleContains::isContainedInBoundary(const Geometry& geom)
51 // polygons can never be wholely contained in the boundary
52 if (dynamic_cast<const geom::Polygon *>(&geom)) return false;
53 if (const Point *p=dynamic_cast<const Point *>(&geom))
54 return isPointContainedInBoundary(*p);
55 if (const LineString *l=dynamic_cast<const LineString *>(&geom))
56 return isLineStringContainedInBoundary(*l);
58 for (unsigned i=0, n=geom.getNumGeometries(); i<n; ++i)
60 const Geometry &comp = *(geom.getGeometryN(i));
61 if ( !isContainedInBoundary(comp) )
62 return false;
65 return true;
68 /*private*/
69 bool
70 RectangleContains::isPointContainedInBoundary(const Point& point)
72 return isPointContainedInBoundary(*(point.getCoordinate()));
75 /*private*/
76 bool
77 RectangleContains::isPointContainedInBoundary(const Coordinate& pt)
79 /**
80 * contains = false iff the point is properly contained
81 * in the rectangle.
83 * This code assumes that the point lies in the rectangle envelope
85 return pt.x == rectEnv.getMinX()
86 || pt.x == rectEnv.getMaxX()
87 || pt.y == rectEnv.getMinY()
88 || pt.y == rectEnv.getMaxY();
91 /*private*/
92 bool
93 RectangleContains::isLineStringContainedInBoundary(const LineString& line)
95 const CoordinateSequence &seq = *(line.getCoordinatesRO());
96 for (unsigned int i=0, n=seq.getSize()-1; i<n; ++i) {
97 const Coordinate& p0=seq.getAt(i);
98 const Coordinate& p1=seq.getAt(i+1);
99 if (! isLineSegmentContainedInBoundary(p0, p1))
100 return false;
102 return true;
105 /*private*/
106 bool
107 RectangleContains::isLineSegmentContainedInBoundary(const Coordinate& p0,
108 const Coordinate& p1)
110 if (p0.equals2D(p1))
111 return isPointContainedInBoundary(p0);
113 // we already know that the segment is contained in
114 // the rectangle envelope
115 if (p0.x == p1.x) {
116 if (p0.x == rectEnv.getMinX() ||
117 p0.x == rectEnv.getMaxX() )
119 return true;
122 else if (p0.y == p1.y) {
123 if (p0.y == rectEnv.getMinY() ||
124 p0.y == rectEnv.getMaxY() )
126 return true;
131 * Either
132 * both x and y values are different
133 * or
134 * one of x and y are the same, but the other ordinate
135 * is not the same as a boundary ordinate
137 * In either case, the segment is not wholely in the boundary
139 return false;
144 } // namespace geos.operation.predicate
145 } // namespace geos.operation
146 } // namespace geos