-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOccupancyGrid.cpp
177 lines (158 loc) · 5.43 KB
/
OccupancyGrid.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
#include "common.h" // NOLINT
#include "core/OccupancyGrid.h"
#include <mLibCore.h>
#include <io/io.h>
namespace sg {
namespace core {
const unsigned kThresh = 1;
const ml::BinaryGrid3& OccupancyGrid::occupied() const {
if (m_occupied.getNumElements()) { return m_occupied; }
const auto dim = m_grid.getDimensions();
m_occupied.allocate(dim.x, dim.y, dim.z);
m_occupied.clearVoxels();
for (size_t k = 0; k < dim.z; k++) {
for (size_t j = 0; j < dim.y; j++) {
for (size_t i = 0; i < dim.x; i++) {
const OccupancyVoxel& voxel = m_grid(i, j, k);
if (voxel.occupiedCtr >= kThresh) { // occupied
m_occupied.setVoxel(i, j, k);
}
}
}
}
return m_occupied;
}
const ml::BinaryGrid3& OccupancyGrid::free() const {
if (m_free.getNumElements()) { return m_free; }
const auto dim = m_grid.getDimensions();
m_free.allocate(dim.x, dim.y, dim.z);
m_free.clearVoxels();
for (size_t k = 0; k < dim.z; k++) {
for (size_t j = 0; j < dim.y; j++) {
for (size_t i = 0; i < dim.x; i++) {
const OccupancyVoxel& voxel = m_grid(i, j, k);
if (voxel.freeCtr >= kThresh) { // free
m_free.setVoxel(i, j, k);
}
}
}
}
return m_free;
}
const ml::BinaryGrid3& OccupancyGrid::unknown() const {
if (m_unknown.getNumElements()) { return m_unknown; }
const auto dim = m_grid.getDimensions();
m_unknown.allocate(dim.x, dim.y, dim.z);
m_unknown.clearVoxels();
for (size_t k = 0; k < dim.z; k++) {
for (size_t j = 0; j < dim.y; j++) {
for (size_t i = 0; i < dim.x; i++) {
const OccupancyVoxel& voxel = m_grid(i, j, k);
if ((voxel.freeCtr == 0 && voxel.occupiedCtr == 0)) { // unknown is not free or occupied
if (voxel.unkCtr >= kThresh) {
m_unknown.setVoxel(i, j, k);
}
}
}
}
}
return m_unknown;
}
const ml::BinaryGrid3& OccupancyGrid::unknownOrOccupied() const {
if (m_unkOrOcc.getNumElements()) { return m_unkOrOcc; }
const auto dim = m_grid.getDimensions();
m_unkOrOcc.allocate(dim.x, dim.y, dim.z);
m_unkOrOcc.clearVoxels();
for (size_t k = 0; k < dim.z; k++) {
for (size_t j = 0; j < dim.y; j++) {
for (size_t i = 0; i < dim.x; i++) {
const OccupancyVoxel& voxel = m_grid(i, j, k);
if (voxel.occupiedCtr >= kThresh ||
(voxel.unkCtr >= kThresh && voxel.freeCtr == 0 && voxel.occupiedCtr == 0)) { // occupied or unknown
m_unkOrOcc.setVoxel(i, j, k);
}
}
}
}
return m_unkOrOcc;
}
//! Read an occupancy grid file. File starts with ASCII header of following format (excluding comments after #):
//! voxelgrid\t1
//! dimensions\tdimX dimY dimZ # vec3ul
//! worldToGrid\tfloat00 float01 float02 floa03 float10 ... float33 # mat4f
//! depthMin\tfloat
//! depthMax\tfloat
//! voxelSize\tfloat
//! labels\tId1,Id2,Id3,...
//! data:
//! Binary section follows with pairs of uint32 (freeCtr,occCtr) in iteration order x, y, z (outermost to innermost)
bool OccupancyGrid::readFromGridFile(const string& file) {
if (!io::fileExists(file)) { return false; }
const auto readerFun = [&] (istream& is) {
// Parse header
string symbol, line;
size_t dimX, dimY, dimZ;
getline(is, line);
if (line.compare("voxelgrid\t1") != 0) {
SG_LOG_ERROR << "Unknown VoxelGrid format. "
<< "First line should be 'voxelgrid\t1'. "
<< "Line is: " << line << endl;
}
while (line.compare("data:") != 0) {
getline(is, line);
std::stringstream ss(line);
ss >> symbol;
if (symbol == "dimensions") {
ss >> dimX >> dimY >> dimZ;
} else if (symbol == "worldToGrid") {
for (int i = 0; i < 16; ++i) {
ss >> m_worldToGrid.matrix[i];
}
} else if (symbol == "depthMin") {
ss >> m_depthMin;
} else if (symbol == "depthMax") {
ss >> m_depthMax;
} else if (symbol == "voxelSize") {
ss >> m_voxelSize;
} else if (symbol == "labels") {
string labelsString;
ss >> labelsString;
m_labels = ml::util::split(labelsString, ',');
}
}
m_gridToWorld = m_worldToGrid.getInverse();
// Allocate and read grid binary section
m_grid.allocate(dimX, dimY, dimZ);
is.read(reinterpret_cast<char*>(m_grid.getData()), sizeof(OccupancyVoxel) * m_grid.getNumElements());
};
io::readFile(file, readerFun, std::ios::binary);
return true;
}
void OccupancyGrid::saveToFile(const string& filename) const {
ofstream outFile(filename, std::ios::binary);
outFile << "voxelgrid\t" << kFormatVersion << endl;
outFile << "dimensions\t" << m_grid.getDimX() << " " << m_grid.getDimY() << " " << m_grid.getDimZ() << endl;
outFile << "worldToGrid\t";
for (int i = 0; i < 15; ++i) {
outFile << m_worldToGrid.getData()[i] / m_voxelSize << " ";
}
outFile << m_worldToGrid.getData()[15] << endl;
outFile << "depthMin\t" << m_depthMin << endl;
outFile << "depthMax\t" << m_depthMax << endl;
outFile << "voxelSize\t" << m_voxelSize << endl;
outFile << "labels\t";
const int numLabels = static_cast<int>(m_labels.size());
for (int i = 0; i < numLabels; ++i) {
outFile << m_labels[i];
if (i == numLabels - 1) {
outFile << endl;
} else {
outFile << ",";
}
}
outFile << "data:" << endl;
outFile.write(reinterpret_cast<const char*>(m_grid.getData()), sizeof(OccupancyVoxel) * m_grid.getNumElements());
outFile.close();
}
} // namespace core
} // namespace sg