File: RectangleContains.cpp

package info (click to toggle)
geos 3.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 10,060 kB
  • ctags: 8,674
  • sloc: cpp: 64,513; xml: 23,384; sh: 8,965; ruby: 1,295; makefile: 1,124; python: 824; ansic: 289
file content (148 lines) | stat: -rw-r--r-- 3,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**********************************************************************
 * $Id: RectangleContains.cpp 1820 2006-09-06 16:54:23Z mloskot $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2006 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************
 *
 * Last port: operation/predicate/RectangleContains.java rev 1.1 (JTS-1.7)
 *
 **********************************************************************/

#include <geos/operation/predicate/RectangleContains.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/Point.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/LineString.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequence.h>

using namespace geos::geom;

namespace geos {
namespace operation { // geos.operation
namespace predicate { // geos.operation.predicate

bool
RectangleContains::contains(const Geometry& geom)
{
	if ( ! rectEnv.contains(geom.getEnvelopeInternal()) )
		return false;

	// check that geom is not contained entirely in the rectangle boundary
	if (isContainedInBoundary(geom))
		return false;

	return true;
}

/*private*/
bool
RectangleContains::isContainedInBoundary(const Geometry& geom)
{
	// polygons can never be wholely contained in the boundary
	if (dynamic_cast<const geom::Polygon *>(&geom)) return false;
	if (const Point *p=dynamic_cast<const Point *>(&geom))
		return isPointContainedInBoundary(*p);
	if (const LineString *l=dynamic_cast<const LineString *>(&geom))
		return isLineStringContainedInBoundary(*l);

	for (unsigned i=0, n=geom.getNumGeometries(); i<n; ++i)
	{
		const Geometry &comp = *(geom.getGeometryN(i));
		if ( !isContainedInBoundary(comp) ) 
			return false;
	}

	return true;
}

/*private*/
bool
RectangleContains::isPointContainedInBoundary(const Point& point)
{
	return isPointContainedInBoundary(*(point.getCoordinate()));
}

/*private*/
bool
RectangleContains::isPointContainedInBoundary(const Coordinate& pt)
{
	// we already know that the point is contained in the
	// rectangle envelope
	if (! (pt.x == rectEnv.getMinX() || pt.x == rectEnv.getMaxX()) )
		return false;
	if (! (pt.y == rectEnv.getMinY() || pt.y == rectEnv.getMaxY()) )
		return false;

	return true;
}

/*private*/
bool
RectangleContains::isLineStringContainedInBoundary(const LineString& line)
{
	const CoordinateSequence &seq = *(line.getCoordinatesRO());
	for (unsigned int i=0, n=seq.getSize()-1; i<n; ++i) {
		const Coordinate& p0=seq.getAt(i);
		const Coordinate& p1=seq.getAt(i+1);
		if (! isLineSegmentContainedInBoundary(p0, p1))
			return false;
	}
	return true;
}

/*private*/
bool
RectangleContains::isLineSegmentContainedInBoundary(const Coordinate& p0,
		const Coordinate& p1)
{
	if (p0.equals2D(p1))
		return isPointContainedInBoundary(p0);

	// we already know that the segment is contained in
	// the rectangle envelope
	if (p0.x == p1.x) {
		if (p0.x == rectEnv.getMinX() ||
			p0.x == rectEnv.getMaxX() )
		{
			return true;
		}
	}
	else if (p0.y == p1.y) {
		if (p0.y == rectEnv.getMinY() ||
			p0.y == rectEnv.getMaxY() )
		{
			return true;
		}
	}

	/**
	 * Either
	 *   both x and y values are different
	 * or
	 *   one of x and y are the same, but the other ordinate
	 *   is not the same as a boundary ordinate
	 *
	 * In either case, the segment is not wholely in the boundary
	 */
	return false;
}



} // namespace geos.operation.predicate
} // namespace geos.operation
} // namespace geos