File: DirectedEdgeStar.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 (189 lines) | stat: -rw-r--r-- 3,966 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**********************************************************************
 * $Id: DirectedEdgeStar.cpp 1820 2006-09-06 16:54:23Z mloskot $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2001-2002 Vivid Solutions Inc.
 * Copyright (C) 2005 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************/

#include <geos/planargraph/DirectedEdgeStar.h>
#include <geos/planargraph/DirectedEdge.h>

#include <vector>
#include <algorithm>

using namespace std;
using namespace geos::geom;

namespace geos {
namespace planargraph {


/*
 * Adds a new member to this DirectedEdgeStar.
 */
void
DirectedEdgeStar::add(DirectedEdge *de)
{
	outEdges.push_back(de);
	sorted=false;
}

/*
 * Drops a member of this DirectedEdgeStar.
 */
void
DirectedEdgeStar::remove(DirectedEdge *de)
{
	for(unsigned int i=0; i<outEdges.size(); ++i)
	{
		if(outEdges[i]==de)
		{
			outEdges.erase(outEdges.begin()+i);
			--i;
		}
	}
}

vector<DirectedEdge*>::iterator
DirectedEdgeStar::begin() {
	sortEdges();
	return outEdges.begin();
}

vector<DirectedEdge*>::iterator
DirectedEdgeStar::end() {
	sortEdges();
	return outEdges.end();
}

vector<DirectedEdge*>::const_iterator
DirectedEdgeStar::begin() const {
	sortEdges();
	return outEdges.begin();
}

vector<DirectedEdge*>::const_iterator
DirectedEdgeStar::end() const {
	sortEdges();
	return outEdges.end();
}

/*
 * Returns the coordinate for the node at wich this star is based
 */
Coordinate&
DirectedEdgeStar::getCoordinate() const
{
	if (outEdges.empty())
		return Coordinate::getNull();
	DirectedEdge *e=outEdges[0];
	return e->getCoordinate();
}

/*
 * Returns the DirectedEdges, in ascending order by angle with
 * the positive x-axis.
 */
vector<DirectedEdge*>&
DirectedEdgeStar::getEdges()
{
	sortEdges();
	return outEdges;
}

bool
pdeLessThan(DirectedEdge *first, DirectedEdge * second)
{
	if (first->compareTo(second)<0)
		return true;
	else
		return false;
}

/*private*/
void
DirectedEdgeStar::sortEdges() const
{
	if (!sorted) {
		sort(outEdges.begin(), outEdges.end(), pdeLessThan);
		sorted=true;
	}
}

/*
 * Returns the zero-based index of the given Edge, after sorting in
 * ascending order by angle with the positive x-axis.
 */
int
DirectedEdgeStar::getIndex(const Edge *edge)
{
	sortEdges();
	for (unsigned int i = 0; i<outEdges.size(); ++i)
	{
		DirectedEdge *de =outEdges[i];
		if (de->getEdge() == edge)
		return i;
	}
	return -1;
}

/*
 * Returns the zero-based index of the given DirectedEdge, after sorting
 * in ascending order by angle with the positive x-axis.
 */  
int
DirectedEdgeStar::getIndex(const DirectedEdge *dirEdge)
{
	sortEdges();
	for (unsigned int i = 0; i <outEdges.size(); ++i)
	{
		DirectedEdge *de =outEdges[i];
		if (de == dirEdge)
		return i;
	}
	return -1;
}

/*
 * Returns the remainder when i is divided by the number of edges in this
 * DirectedEdgeStar. 
 */
int
DirectedEdgeStar::getIndex(int i) const
{
	int modi = i % (int)outEdges.size();
	//I don't think modi can be 0 (assuming i is positive) [Jon Aquino 10/28/2003] 
	if (modi < 0) modi += (int)outEdges.size();
	return modi;
}

/*
 * Returns the DirectedEdge on the left-hand side of the given
 * DirectedEdge (which must be a member of this DirectedEdgeStar). 
 */
DirectedEdge*
DirectedEdgeStar::getNextEdge(DirectedEdge *dirEdge)
{
	int i = getIndex(dirEdge);
	return outEdges[getIndex(i + 1)];
}

} // namespace planargraph
} // namespace geos

/**********************************************************************
 * $Log$
 * Revision 1.1  2006/03/21 21:42:54  strk
 * planargraph.h header split, planargraph:: classes renamed to match JTS symbols
 *
 **********************************************************************/