-
Notifications
You must be signed in to change notification settings - Fork 347
/
SimpleMCSweepLineIntersector.cpp
175 lines (159 loc) · 4.24 KB
/
SimpleMCSweepLineIntersector.cpp
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
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* 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 Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <algorithm>
#include <vector>
#include <geos/geomgraph/index/SimpleMCSweepLineIntersector.h>
#include <geos/geomgraph/index/MonotoneChainEdge.h>
#include <geos/geomgraph/index/MonotoneChain.h>
#include <geos/geomgraph/index/SweepLineEvent.h>
#include <geos/geomgraph/Edge.h>
#include <geos/util/Interrupt.h>
using namespace std;
namespace geos {
namespace geomgraph { // geos.geomgraph
namespace index { // geos.geomgraph.index
SimpleMCSweepLineIntersector::SimpleMCSweepLineIntersector()
//events(new vector<SweepLineEvent*>())
{
}
SimpleMCSweepLineIntersector::~SimpleMCSweepLineIntersector()
{
for(size_t i=0; i<events.size(); ++i)
{
SweepLineEvent *sle=events[i];
if (sle->isDelete()) delete sle;
}
//delete events;
}
void
SimpleMCSweepLineIntersector::computeIntersections(vector<Edge*> *edges,
SegmentIntersector *si, bool testAllSegments)
{
if (testAllSegments)
add(edges,nullptr);
else
add(edges);
computeIntersections(si);
}
void
SimpleMCSweepLineIntersector::computeIntersections(vector<Edge*> *edges0,
vector<Edge*> *edges1, SegmentIntersector *si)
{
add(edges0,edges0);
add(edges1,edges1);
computeIntersections(si);
}
void
SimpleMCSweepLineIntersector::add(vector<Edge*> *edges)
{
for (size_t i=0; i<edges->size(); ++i)
{
Edge *edge=(*edges)[i];
// edge is its own group
add(edge, edge);
}
}
void
SimpleMCSweepLineIntersector::add(vector<Edge*> *edges,void* edgeSet)
{
for (size_t i=0; i<edges->size(); ++i)
{
Edge *edge=(*edges)[i];
add(edge,edgeSet);
}
}
void
SimpleMCSweepLineIntersector::add(Edge *edge, void* edgeSet)
{
MonotoneChainEdge *mce=edge->getMonotoneChainEdge();
auto& startIndex = mce->getStartIndexes();
size_t n = startIndex.size() - 1;
events.reserve(events.size()+(n*2));
for(size_t i = 0; i < n; ++i)
{
GEOS_CHECK_FOR_INTERRUPTS();
MonotoneChain *mc=new MonotoneChain(mce, i);
SweepLineEvent *insertEvent=new SweepLineEvent(edgeSet, mce->getMinX(i), nullptr, mc);
events.push_back(insertEvent);
events.push_back(new SweepLineEvent(edgeSet, mce->getMaxX(i), insertEvent, mc));
}
}
/**
* Because Delete Events have a link to their corresponding Insert event,
* it is possible to compute exactly the range of events which must be
* compared to a given Insert event object.
*/
void
SimpleMCSweepLineIntersector::prepareEvents()
{
sort(events.begin(), events.end(), SweepLineEventLessThen());
for(size_t i=0; i<events.size(); ++i)
{
GEOS_CHECK_FOR_INTERRUPTS();
SweepLineEvent *ev=events[i];
if (ev->isDelete())
{
ev->getInsertEvent()->setDeleteEventIndex(i);
}
}
}
void
SimpleMCSweepLineIntersector::computeIntersections(SegmentIntersector *si)
{
nOverlaps=0;
prepareEvents();
for(size_t i=0; i<events.size(); ++i)
{
GEOS_CHECK_FOR_INTERRUPTS();
SweepLineEvent *ev=events[i];
if (ev->isInsert())
{
processOverlaps(i, ev->getDeleteEventIndex(), ev, si);
}
if (si->getIsDone())
{
break;
}
}
}
void
SimpleMCSweepLineIntersector::processOverlaps(size_t start, size_t end,
SweepLineEvent *ev0, SegmentIntersector *si)
{
MonotoneChain *mc0=(MonotoneChain*) ev0->getObject();
/*
* Since we might need to test for self-intersections,
* include current insert event object in list of event objects to test.
* Last index can be skipped, because it must be a Delete event.
*/
for(auto i = start; i < end; ++i)
{
SweepLineEvent *ev1=events[i];
if (ev1->isInsert())
{
MonotoneChain *mc1=(MonotoneChain*) ev1->getObject();
// don't compare edges in same group
// null group indicates that edges should be compared
if (ev0->edgeSet==nullptr || (ev0->edgeSet!=ev1->edgeSet))
{
mc0->computeIntersections(mc1,si);
nOverlaps++;
}
}
}
}
} // namespace geos.geomgraph.index
} // namespace geos.geomgraph
} // namespace geos