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
;
31 namespace operation
{ // geos.operation
32 namespace predicate
{ // geos.operation.predicate
35 RectangleContains::contains(const Geometry
& geom
)
37 if ( ! rectEnv
.contains(geom
.getEnvelopeInternal()) )
40 // check that geom is not contained entirely in the rectangle boundary
41 if (isContainedInBoundary(geom
))
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
) )
70 RectangleContains::isPointContainedInBoundary(const Point
& point
)
72 return isPointContainedInBoundary(*(point
.getCoordinate()));
77 RectangleContains::isPointContainedInBoundary(const Coordinate
& pt
)
80 * contains = false iff the point is properly contained
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();
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
))
107 RectangleContains::isLineSegmentContainedInBoundary(const Coordinate
& p0
,
108 const Coordinate
& p1
)
111 return isPointContainedInBoundary(p0
);
113 // we already know that the segment is contained in
114 // the rectangle envelope
116 if (p0
.x
== rectEnv
.getMinX() ||
117 p0
.x
== rectEnv
.getMaxX() )
122 else if (p0
.y
== p1
.y
) {
123 if (p0
.y
== rectEnv
.getMinY() ||
124 p0
.y
== rectEnv
.getMaxY() )
132 * both x and y values are different
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
144 } // namespace geos.operation.predicate
145 } // namespace geos.operation