-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
128 lines (102 loc) · 4.22 KB
/
code.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
#include "header.hpp"
#include "raster.hpp"
#include <regex>
#include <unordered_map>
#include <ogrsf_frmts.h>
#include <CGAL/IO/WKT.h>
std::list<Polygon> get_LOD0_from_shapefile(char *path) {
GDALAllRegister();
GDALDataset *dataset;
std::list<Polygon> polygons;
dataset = (GDALDataset*) GDALOpenEx(path, GDAL_OF_VECTOR, NULL, NULL, NULL );
if( dataset == NULL ) {
std::cerr << "Unable to open " << path << "." << std::endl;
return polygons;
}
for( OGRLayer *layer: dataset->GetLayers() ) {
for( const auto& feature: *layer ) {
if ((*feature)["num_class"].GetInteger() == 4) {
// Get only the buildings
OGRGeometry *geometry = feature->GetGeometryRef();
if (wkbFlatten(geometry->getGeometryType()) == wkbPolygon) {
OGRPolygon *shp_polygon = geometry->toPolygon();
Polygon cgal_polygon;
std::istringstream wkt(shp_polygon->exportToWkt());
CGAL::IO::read_polygon_WKT (wkt, cgal_polygon);
polygons.push_back(cgal_polygon);
} else if (wkbFlatten(geometry->getGeometryType()) == wkbMultiPolygon) {
OGRMultiPolygon *shp_multi_polygon = geometry->toMultiPolygon();
for (OGRPolygon *shp_polygon: *shp_multi_polygon) {
Polygon cgal_polygon;
std::istringstream wkt(shp_polygon->exportToWkt());
CGAL::IO::read_polygon_WKT (wkt, cgal_polygon);
polygons.push_back(cgal_polygon);
}
}
}
}
}
return polygons;
}
void add_label(const Raster &raster, Surface_mesh &mesh) {
Surface_mesh::Property_map<Surface_mesh::Face_index, unsigned char> label;
bool created;
boost::tie(label, created) = mesh.add_property_map<Surface_mesh::Face_index, unsigned char>("f:label", LABEL_UNKNOWN);
assert(created);
for (auto face : mesh.faces()) {
int face_label[LABELS.size()] = {0};
int sum_face_label = 0;
CGAL::Vertex_around_face_iterator<Surface_mesh> vbegin, vend;
boost::tie(vbegin, vend) = vertices_around_face(mesh.halfedge(face), mesh);
for (auto pixel : raster.triangle_to_pixel(mesh.point(*(vbegin++)), mesh.point(*(vbegin++)), mesh.point(*(vbegin++)))) {
sum_face_label++;
face_label[raster.land_cover[pixel.second][pixel.first]]++;
}
auto argmax = std::max_element(face_label, face_label+LABELS.size());
label[face] = argmax - face_label;
}
}
void change_vertical_faces(Surface_mesh &mesh) {
Surface_mesh::Property_map<Surface_mesh::Face_index, unsigned char> label;
bool has_label;
boost::tie(label, has_label) = mesh.property_map<Surface_mesh::Face_index, unsigned char>("f:label");
assert(has_label);
for (auto face : mesh.faces()) {
if (label[face] == LABEL_RAIL) {
CGAL::Vertex_around_face_iterator<Surface_mesh> vbegin, vend;
boost::tie(vbegin, vend) = vertices_around_face(mesh.halfedge(face), mesh);
auto p0 = mesh.point(*(vbegin++));
auto p1 = mesh.point(*(vbegin++));
auto p2 = mesh.point(*(vbegin++));
auto n = CGAL::orthogonal_vector(p0, p1, p2);
if (CGAL::scalar_product(n, K::Vector_3(0,0,1)) / CGAL::sqrt(n.squared_length()) < 0.98) {
label[face] = LABEL_UNKNOWN;
}
} else if (label[face] == LABEL_ROAD) {
CGAL::Vertex_around_face_iterator<Surface_mesh> vbegin, vend;
boost::tie(vbegin, vend) = vertices_around_face(mesh.halfedge(face), mesh);
auto p0 = mesh.point(*(vbegin++));
auto p1 = mesh.point(*(vbegin++));
auto p2 = mesh.point(*(vbegin++));
auto n = CGAL::orthogonal_vector(p0, p1, p2);
if (CGAL::scalar_product(n, K::Vector_3(0,0,1)) / CGAL::sqrt(n.squared_length()) < 0.95) {
label[face] = LABEL_UNKNOWN;
}
}
}
}
void compute_normal_angle_coef(Surface_mesh &mesh) {
Surface_mesh::Property_map<Surface_mesh::Face_index, K::FT> normal_angle_coef;
bool created_normal_angle_coef;
boost::tie(normal_angle_coef, created_normal_angle_coef) = mesh.add_property_map<Surface_mesh::Face_index, K::FT>("f:n_a_coef", 1);
assert(created_normal_angle_coef);
for (auto face : mesh.faces()) {
CGAL::Vertex_around_face_iterator<Surface_mesh> vbegin, vend;
boost::tie(vbegin, vend) = vertices_around_face(mesh.halfedge(face), mesh);
auto p0 = mesh.point(*(vbegin++));
auto p1 = mesh.point(*(vbegin++));
auto p2 = mesh.point(*(vbegin++));
auto n = CGAL::orthogonal_vector (p0, p1, p2);
normal_angle_coef[face] = sin(-CGAL::approximate_angle(n, K::Vector_3(0,0,1)) * M_PI / 180) + 1;
}
}