-
Notifications
You must be signed in to change notification settings - Fork 0
/
cityjson_reader.h
510 lines (417 loc) · 11.9 KB
/
cityjson_reader.h
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include "typedefs.h"
using namespace std;
class CityJsonReader
{
private:
LCC lcc;
nlohmann::json cityModel;
unsigned int start_i = 0, object_limit = 0;
int precision = 3;
string id_filter = "";
int lod_filter = -1;
bool index_1_per_object = false;
double scale[3] = {1, 1, 1};
double translate[3] = {0, 0, 0};
ostringstream log_str;
unordered_map<string, vector<Dart_handle> > index_0_cell;
unordered_map<string, Dart_handle> index_1_cell;
unordered_map<string, Dart_handle> index_2_cell;
public:
Point json_to_point(nlohmann::json p)
{
return Point((double)p[0] * scale[0] + translate[0], (double)p[1] * scale[1] + translate[1], (double)p[2] * scale[2] + translate[2]);
}
double round_by(double f, int d)
{
double ex = pow(10, d);
return round(f * ex) / ex;
}
string get_point_name(Point p)
{
ostringstream st;
st << fixed << setprecision(precision);
st << p.x() << "-" << p.y() << "-" << p.z();
//st << round_by(p.x(), precision) << "-" << round_by(p.y(), precision) << "-" << round_by(p.z(), precision);
return st.str();
}
string index_to_string(unordered_map<string, Dart_handle>::iterator it)
{
ostringstream str;
str << it->first << ": ";
if (it->second == nullptr)
str << "IS NULL" << endl;
else
str << lcc.point(it->second) << endl;
return str.str();
}
void show_null_index_records()
{
#ifdef DEBUG
for( unordered_map<string, Dart_handle>::iterator it = index_1_cell.begin(); it != index_1_cell.end(); it++)
{
if (it->second == NULL)
{
log_str << "NOW " << it->first << " IS NULL!!!!!" << endl;
}
}
#endif // DEBUG
}
template <class T>
string get_edge_name(T v1, T v2)
{
return get_point_name(v1) + "-" + get_point_name(v2);
}
void get_polygon_name(vector<Dart_handle> darts, string &name, Dart_handle &lowest_dart, bool step_forward)
{
string lowest_name = get_point_name(lcc.point(darts.front()));
lowest_dart = darts.front();
for( vector<Dart_handle>::iterator it = darts.begin(); it != darts.end(); ++it)
{
string new_point = get_point_name(lcc.point(*darts.begin()));
if (new_point < lowest_name)
{
lowest_name = new_point;
lowest_dart = *it;
}
}
int beta_i = step_forward ? 1 : 0;
name = lowest_name;
Dart_handle next = lcc.beta(lowest_dart, beta_i);
while (next != lowest_dart && next != lcc.null_dart_handle)
{
name += "-" + get_point_name(lcc.point(next));
next = lcc.beta(next, beta_i);
}
}
Dart_handle add_vertex(Point v, int i_free = -1)
{
Dart_handle result;
bool found = false;
string v_name = get_point_name(v);
if (index_0_cell.find(v_name) != index_0_cell.end())
{
for (vector<Dart_handle>::iterator it = index_0_cell[v_name].begin(); it != index_0_cell[v_name].end(); ++it)
{
if (i_free < 0 || lcc.beta(*it, i_free) == lcc.null_dart_handle)
{
found = true;
result = *it;
index_0_cell[v_name].erase(it);
if (index_0_cell[v_name].empty())
{
index_0_cell.erase(v_name);
}
break;
}
}
}
if( !found )
{
result = lcc.create_dart( v );
index_0_cell[v_name].push_back(result);
// log_str << "Created " << lcc.point(result) << endl;
}
return result;
}
Dart_handle add_edge(Point v1, Point v2)
{
Dart_handle result;
result = add_vertex(v1, 1);
Dart_handle temp_dart = add_vertex(v2, 0);
lcc.sew<1>(result, temp_dart);
string edge_v1_v2_name = get_edge_name(v1, v2);
string edge_v2_v1_name = get_edge_name(v2, v1);
index_1_cell[edge_v1_v2_name] = result;
if (index_1_cell.find(edge_v2_v1_name) != index_1_cell.end())
{
lcc.sew<2>(result, index_1_cell[edge_v2_v1_name]);
index_1_cell.erase(edge_v1_v2_name);
index_1_cell.erase(edge_v2_v1_name);
}
show_null_index_records();
return result;
}
vector<Point> parse_vertices(nlohmann::json vertices)
{
vector<Point> verts;
for (int i : vertices)
{
verts.push_back(json_to_point(cityModel["vertices"][i]));
}
return verts;
}
vector<Dart_handle> parse_polygon(nlohmann::json poly, int level = 1)
{
vector<Dart_handle> result;
log_str << "+" << string(level * 2 - 2, '-') << " Polygon" << endl;
// TODO: Add support for holes
const vector<Point> verts = parse_vertices(poly[0]);
auto id = poly[0].begin();
if (verts.size() > 2)
{
for(auto it = verts.begin(); (it + 1) != verts.end(); it++, id++)
{
if (get_point_name(*it) != get_point_name(*(it + 1)))
{
Dart_handle new_dart = add_edge(*it, *(it + 1));
lcc.info<0>(new_dart).set_vertex(*id);
result.push_back(new_dart);
}
}
if (get_point_name(verts.back()) != get_point_name(*verts.begin()))
{
Dart_handle new_dart = add_edge(verts.back(), *verts.begin());
lcc.info<0>(new_dart).set_vertex(*id);
result.push_back(new_dart);
}
if (result.size() > 2)
{
// Try to 3-sew with other polygons
string new_name;
Dart_handle new_dart;
get_polygon_name(result, new_name, new_dart, true);
string inverse_name;
get_polygon_name(result, inverse_name, new_dart, false);
if (index_2_cell.find(inverse_name) != index_2_cell.end())
{
Dart_handle other_dart = lcc.beta<0>(index_2_cell[inverse_name]);
log_str << "3-Sewing " << new_name << " with " << inverse_name << endl;
lcc.sew<3>(new_dart, other_dart);
index_2_cell.erase(inverse_name);
}
else
{
index_2_cell[new_name] = new_dart;
}
}
}
else
{
log_str << "Ignoring this polygon because only 2 individual lines where found." << endl;
}
log_str << "Now we have " << lcc.number_of_darts() << " darts!" << endl << endl;
return result;
}
vector<Dart_handle> parse_shell(nlohmann::json solid, bool has_semantics, nlohmann::json::iterator semantic_id, int level)
{
vector<Dart_handle> result;
for (auto& polygon : solid)
{
auto temp_darts = parse_polygon( polygon, level + 1 );
result.insert(result.end(), temp_darts.begin(), temp_darts.end());
for (auto temp_dart: temp_darts)
{
init_face(temp_dart);
if (has_semantics)
{
lcc.info<2>(temp_dart).set_semantic_surface_id(*semantic_id);
}
}
if (has_semantics)
{
semantic_id++;
}
}
return result;
}
vector<Dart_handle> parse_geometry(nlohmann::json geom, int level = 1)
{
vector<Dart_handle> result;
log_str << string(level * 2 - 1, '-') << " Geometry (" << geom["type"] << ")" << endl;
if (lod_filter > 0 && geom.find("lod") != geom.end() && geom["lod"] != lod_filter)
{
log_str << "Skipping LoD " << geom["lod"] << " because of LoD" << lod_filter << " filter!" << endl;
return result;
}
bool has_semantics = geom.find("semantics") != geom.end();
nlohmann::json::iterator semantic_id;
if (has_semantics)
{
semantic_id = geom["semantics"]["values"].begin();
}
if (geom["type"] == "Solid")
{
log_str << "|" << string(level * 2 - 1, '-') << " Solid count: " << geom["boundaries"].size() << endl;
for (auto& shell : geom["boundaries"])
{
nlohmann::json::iterator semantic_list;
if (has_semantics)
{
semantic_list = (*semantic_id).begin();
}
auto temp_darts = parse_shell(shell, has_semantics, semantic_list, level);
result.insert(result.end(), temp_darts.begin(), temp_darts.end());
}
}
else if (geom["type"] == "MultiSurface" || geom["type"] == "CompositeSurface")
{
log_str << "|" << string(level * 2 - 1, '-') << " Polygon count: " << geom["boundaries"].size() << endl;
auto temp_darts = parse_shell(geom["boundaries"], has_semantics, semantic_id, level);
result.insert(result.end(), temp_darts.begin(), temp_darts.end());
}
return result;
}
void parse_object(pair<const string, nlohmann::json> &obj)
{
log_str << "Object " << obj.first << endl;
log_str << "---------------------" << endl;
auto obj_content = obj.second;
log_str << "Type: " << obj_content["type"] << endl;
log_str << "Geometry count: " << obj_content["geometry"].size() << endl;
int g_id = 0;
for (auto& geom : obj_content["geometry"])
{
vector<Dart_handle> darts = parse_geometry( geom );
for (vector<Dart_handle>::iterator it = darts.begin(); it != darts.end(); ++it)
{
lcc.info<2>(*it).set_guid(obj.first);
lcc.info<2>(*it).set_geometry_id(g_id);
init_volume(*it);
lcc.info<3>(*it).set_guid(obj.first);
}
g_id++;
}
}
void init_all_volumes()
{
for (LCC::One_dart_per_cell_range<3>::iterator
it(lcc.one_dart_per_cell<3>().begin());
it.cont(); ++it)
init_volume(it);
}
void init_all_faces()
{
for (LCC::One_dart_per_cell_range<2>::iterator
it(lcc.one_dart_per_cell<2>().begin());
it.cont(); ++it)
init_face(it);
}
void init_volume(Dart_handle dh)
{
if ( lcc.attribute<3>(dh)==LCC::null_handle )
{ lcc.set_attribute<3>(dh, lcc.create_attribute<3>()); }
}
void init_face(Dart_handle dh)
{
if (lcc.attribute<2>(dh) == LCC::null_handle)
{
lcc.set_attribute<2>(dh, lcc.create_attribute<2>());
}
}
LCC readCityModel(nlohmann::json city)
{
cityModel = city;
if (cityModel.find("transform") != cityModel.end())
{
for (int d = 0; d < 3; d++)
{
scale[d] = cityModel["transform"]["scale"][d];
translate[d] = cityModel["transform"]["translate"][d];
}
}
map<string, nlohmann::json> objs = city["CityObjects"];
int obj_count = objs.size();
if (object_limit == 0)
{
object_limit = obj_count;
}
if (start_i + object_limit > objs.size())
{
object_limit = obj_count - start_i;
}
unsigned int i = 0;
for (auto& obj : objs)
{
if (!id_filter.empty())
{
size_t pos = obj.first.find(id_filter);
if (pos == string::npos)
{
continue;
}
}
parse_object(obj);
log_str << i << ") ";
#ifdef DEBUG
lcc.display_characteristics(log_str);
#endif
log_str << endl << endl;
if (index_1_per_object) {
index_1_cell.clear();
}
cout << "\rDone with " << i - start_i + 1 << "/" << object_limit;
cout.flush();
i++;
if (i + 1 > object_limit)
{
break;
}
}
cout << endl;
init_all_faces();
init_all_volumes();
return lcc;
}
void setStartingIndex(unsigned int start_index)
{
start_i = start_index;
}
unsigned int getStartingIndex()
{
return start_i;
}
void setObjectLimit(unsigned int new_limit)
{
object_limit = new_limit;
}
unsigned int getObjectLimit()
{
return object_limit;
}
void setIdFilter(string filter)
{
id_filter = filter;
}
void setLodFilter(int lod)
{
lod_filter = lod;
}
string getLog()
{
return log_str.str();
}
string getIndex()
{
ostringstream str;
str << "This is the final status of the 1-cell index" << endl << "--------" << endl;
for(unordered_map<string, Dart_handle>::iterator it=index_1_cell.begin(); it!=index_1_cell.end(); ++it)
{
str << index_to_string(it);
}
return str.str();
}
LCC getLinearCellComplex()
{
return lcc;
}
void setPrecision(int new_precision)
{
precision = new_precision;
}
int getPrecision()
{
return precision;
}
void setIndexPerObject(bool new_value)
{
index_1_per_object = new_value;
}
bool getIndexPerObject()
{
return index_1_per_object;
}
};