-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxialminimiser.cpp
356 lines (333 loc) · 15.5 KB
/
axialminimiser.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SPDX-FileCopyrightText: 2000-2010 University College London, Alasdair Turner
// SPDX-FileCopyrightText: 2011-2012 Tasos Varoudis
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "axialminimiser.hpp"
#include "tolerances.hpp"
static int compareValueTriplet(const void *p1, const void *p2) {
auto vp1 = static_cast<const ValueTriplet *>(p1);
auto vp2 = static_cast<const ValueTriplet *>(p2);
return (vp1->value1 > vp2->value1 ? 1
: vp1->value1 < vp2->value1
? -1
: (vp1->value2 > vp2->value2 ? 1
: vp1->value2 < vp2->value2 ? -1
: (vp1->index > vp2->index ? 1
: vp1->index < vp2->index ? -1
: 0)));
}
AxialMinimiser::AxialMinimiser(const ShapeGraph &alllinemap, size_t noOfAxsegcuts,
size_t noOfRadialsegs)
: m_alllinemap(static_cast<const ShapeGraph *>(&alllinemap)),
m_vps(new ValueTriplet[noOfAxsegcuts]), m_removed(new bool[noOfAxsegcuts]),
m_affected(new bool[noOfAxsegcuts]), m_vital(new bool[noOfAxsegcuts]),
m_radialsegcounts(new int[noOfRadialsegs]), m_axialconns() {}
AxialMinimiser::~AxialMinimiser() {
delete[] m_vital;
delete[] m_affected;
delete[] m_radialsegcounts;
delete[] m_vps;
delete[] m_removed;
}
// Alan and Bill's algo...
void AxialMinimiser::removeSubsets(std::map<int, std::set<int>> &axsegcuts,
std::map<RadialKey, RadialSegment> &radialsegs,
std::map<RadialKey, std::set<int>> &rlds,
std::vector<RadialLine> &radialLines,
std::vector<std::vector<int>> &keyvertexconns,
std::vector<int> &keyvertexcounts) {
bool removedflag = true;
m_axialconns = m_alllinemap->m_connectors;
for (size_t x = 0; x < radialsegs.size(); x++) {
m_radialsegcounts[x] = 0;
}
unsigned int y = 0;
for (const auto &axSegCut : axsegcuts) {
for (int cut : axSegCut.second) {
m_radialsegcounts[cut] += 1;
}
m_removed[y] = false;
m_vital[y] = false;
m_affected[y] = true;
m_vps[y].index = static_cast<int>(y);
double length = static_cast<double>(m_axialconns[y].connections.size());
m_vps[y].value1 = static_cast<int>(length);
length = depthmapX::getMapAtIndex(m_alllinemap->m_shapes, y)->second.getLine().length();
m_vps[y].value2 = static_cast<float>(length);
y++;
}
// sort according to number of connections then length
qsort(m_vps, m_axialconns.size(), sizeof(ValueTriplet), compareValueTriplet);
while (removedflag) {
removedflag = false;
for (size_t i = 0; i < m_axialconns.size(); i++) {
auto ii = static_cast<size_t>(m_vps[i].index);
if (m_removed[ii] || !m_affected[ii] || m_vital[ii]) {
continue;
}
// vital connections code (uses original unaltered connections)
{
bool vitalconn = false;
for (size_t j = 0; j < keyvertexconns[ii].size(); j++) {
// first check to see if removing this line will cause elimination of
// a vital connection
if (keyvertexcounts[static_cast<size_t>(keyvertexconns[ii][j])] <= 1) {
// connect vital... just go on to the next one:
vitalconn = true;
break;
}
}
if (vitalconn) {
m_vital[ii] = true;
continue;
}
}
//
Connector &axa = m_axialconns[ii];
m_affected[ii] = false;
bool subset = false;
for (size_t j = 0; j < axa.connections.size(); j++) {
auto indextob = axa.connections[j];
if (indextob == ii ||
m_removed[indextob]) { // <- removed[indextob] should never happen
// as it should have been removed below
continue;
}
Connector &axb = m_axialconns[indextob];
if (axa.connections.size() <= axb.connections.size()) {
// change to 10.08, coconnecting is 1 -> connection to other line is
// implicitly handled
int coconnecting = 1;
// first check it's a connection subset
// note that changes in 10.08 mean that lines no longer connect to
// themselves this means that the subset 1 connects {2,3} and 2
// connects {1,3} are equivalent
for (size_t axai = 0, axbi = 0;
axai < axa.connections.size() && axbi < axb.connections.size();
axai++, axbi++) {
// extra 10.08 -> step over connection to b
if (axa.connections[axai] == indextob) {
axai++;
}
// extra 10.08 add axb.connections[axbi] == ii -> step over
// connection to
// a
while (axbi < axb.connections.size() &&
(axb.connections[axbi] == ii ||
axa.connections[axai] > axb.connections[axbi])) {
axbi++;
}
if (axbi >= axb.connections.size()) {
break;
} else if (axa.connections[axai] == axb.connections[axbi]) {
coconnecting++;
} else if (axa.connections[axai] < axb.connections[axbi]) {
break;
}
}
if (coconnecting >= static_cast<int>(axa.connections.size())) {
subset = true;
break;
}
}
}
if (subset) {
size_t removeindex = ii;
// now check removing it won't break any topological loops
bool presumedvital = false;
auto &axSegCut = depthmapX::getMapAtIndex(axsegcuts, removeindex)->second;
for (int cut : axSegCut) {
if (m_radialsegcounts[cut] <= 1) {
presumedvital = true;
break;
}
}
if (presumedvital) {
presumedvital = checkVital(static_cast<int>(removeindex), axSegCut, radialsegs,
rlds, radialLines);
}
if (presumedvital) {
m_vital[removeindex] = true;
}
// if not, remove it...
if (!m_vital[removeindex]) {
m_removed[removeindex] = true;
auto &affectedconnections = m_axialconns[removeindex].connections;
for (auto affectedconnection : affectedconnections) {
if (!m_removed[affectedconnection]) {
auto &connections = m_axialconns[affectedconnection].connections;
depthmapX::findAndErase(connections, removeindex);
m_affected[affectedconnection] = true;
}
}
removedflag = true;
for (int cut : axSegCut) {
m_radialsegcounts[cut] -= 1;
}
// vital connections
for (size_t k = 0; k < keyvertexconns[removeindex].size(); k++) {
keyvertexcounts[static_cast<size_t>(keyvertexconns[removeindex][k])] -= 1;
}
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// My algo... v. simple... fewest longest
void AxialMinimiser::fewestLongest(std::map<int, std::set<int>> &axsegcuts,
std::map<RadialKey, RadialSegment> &radialsegs,
std::map<RadialKey, std::set<int>> &rlds,
std::vector<RadialLine> &radialLines,
std::vector<std::vector<int>> &keyvertexconns,
std::vector<int> &keyvertexcounts) {
// m_axialconns = m_alllinemap->m_connectors;
int livecount = 0;
for (size_t y = 0; y < m_axialconns.size(); y++) {
if (!m_removed[y] && !m_vital[y]) {
m_vps[livecount].index = static_cast<int>(y);
m_vps[livecount].value1 = static_cast<int>(m_axialconns[y].connections.size());
m_vps[livecount].value2 = static_cast<float>(
depthmapX::getMapAtIndex(m_alllinemap->m_shapes, y)->second.getLine().length());
livecount++;
}
}
qsort(m_vps, static_cast<size_t>(livecount), sizeof(ValueTriplet), compareValueTriplet);
for (int i = 0; i < livecount; i++) {
int j = m_vps[i].index;
// vital connections code (uses original unaltered connections)
bool vitalconn = false;
size_t k;
for (k = 0; k < keyvertexconns[static_cast<size_t>(j)].size(); k++) {
// first check to see if removing this line will cause elimination of a
// vital connection
if (keyvertexcounts[static_cast<size_t>(keyvertexconns[static_cast<size_t>(j)][k])] <=
1) {
// connect vital... just go on to the next one:
vitalconn = true;
break;
}
}
if (vitalconn) {
continue;
}
//
bool presumedvital = false;
auto &axSegCut = depthmapX::getMapAtIndex(axsegcuts, static_cast<size_t>(j))->second;
for (int cut : axSegCut) {
if (m_radialsegcounts[cut] <= 1) {
presumedvital = true;
break;
}
}
if (presumedvital) {
presumedvital = checkVital(j, axSegCut, radialsegs, rlds, radialLines);
}
if (!presumedvital) {
// don't let anything this is connected to go down to zero connections
auto &affectedconnections = m_axialconns[static_cast<size_t>(j)].connections;
for (auto affectedconnection : affectedconnections) {
if (!m_removed[affectedconnection]) {
auto &connections =
m_axialconns[static_cast<size_t>(affectedconnection)].connections;
if (connections.size() <= 2) { // <- note number of connections includes
// itself... so you and one other
presumedvital = true;
break;
}
}
}
}
if (!presumedvital) {
m_removed[j] = true;
auto &affectedconnections = m_axialconns[static_cast<size_t>(j)].connections;
for (auto affectedconnection : affectedconnections) {
if (!m_removed[affectedconnection]) {
auto &connections =
m_axialconns[static_cast<size_t>(affectedconnection)].connections;
depthmapX::findAndErase(connections, static_cast<size_t>(j));
m_affected[affectedconnection] = true;
}
}
for (auto cut : axSegCut) {
m_radialsegcounts[cut] -= 1;
}
// vital connections
for (size_t kvc = 0; kvc < keyvertexconns[static_cast<size_t>(j)].size(); kvc++) {
keyvertexcounts[static_cast<size_t>(keyvertexconns[static_cast<size_t>(j)][kvc])] -=
1;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
bool AxialMinimiser::checkVital(int checkindex, std::set<int> &axSegCut,
std::map<RadialKey, RadialSegment> &radialsegs,
std::map<RadialKey, std::set<int>> &rlds,
std::vector<RadialLine> &radialLines) const {
const std::map<int, SalaShape> &axiallines = m_alllinemap->m_shapes;
bool presumedvital = true;
int nonvitalcount = 0, vitalsegs = 0;
// again, this time more rigourously... check any connected pairs don't cover
// the link...
for (int cut : axSegCut) {
if (m_radialsegcounts[cut] <= 1) {
bool nonvitalseg = false;
vitalsegs++;
auto radialSegIter = depthmapX::getMapAtIndex(radialsegs, static_cast<size_t>(cut));
const RadialKey &key = radialSegIter->first;
RadialSegment &seg = radialSegIter->second;
auto keyIter = rlds.find(key);
if (keyIter == rlds.end()) {
throw depthmapX::RuntimeException(
"RadialKey A not found when checking for vital axial lines");
}
std::set<int> &divisorsa = keyIter->second;
auto radialBIter = rlds.find(seg.radialB);
if (radialBIter == rlds.end()) {
throw depthmapX::RuntimeException(
"RadialKey B not found when checking for vital axial lines");
}
std::set<int> &divisorsb = radialBIter->second;
auto iterKey = std::find(radialLines.begin(), radialLines.end(), key);
if (iterKey == radialLines.end()) {
throw depthmapX::RuntimeException("Radial key not found in radial lines");
}
const RadialLine &rlinea = *iterKey;
auto iterSegB = std::find(radialLines.begin(), radialLines.end(), seg.radialB);
if (iterSegB == radialLines.end()) {
throw depthmapX::RuntimeException("Radial key not found in radial lines");
}
const RadialLine &rlineb = *iterSegB;
for (int diva : divisorsa) {
if (diva == checkindex || m_removed[diva]) {
continue;
}
for (int divb : divisorsb) {
if (divb == checkindex || m_removed[divb]) {
continue;
}
auto &connections = m_axialconns[static_cast<size_t>(diva)].connections;
if (std::find(connections.begin(), connections.end(), divb) !=
connections.end()) {
// as a further challenge, they must link within in the zone of
// interest, not on the far side of it... arg!
Point2f p = axiallines.at(diva).getLine().intersection_point(
axiallines.at(divb).getLine(), TOLERANCE_A);
if (p.insegment(rlinea.keyvertex, rlinea.openspace, rlineb.openspace,
TOLERANCE_A)) {
nonvitalseg = true;
}
}
}
}
if (nonvitalseg) {
nonvitalcount++;
}
}
}
if (nonvitalcount == vitalsegs) {
presumedvital = false;
}
return presumedvital;
}