Skip to content

Commit 1f2a291

Browse files
committed
wip: first attempts to save some time...
1 parent 664d2f1 commit 1f2a291

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/data/VoxelGrid.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ void VoxelGrid::initialize(float resolution, const Eigen::Vector4f& min, const E
2323
occlusions_.resize(sizex_ * sizey_ * sizez_);
2424
occludedBy_.resize(sizex_ * sizey_ * sizez_);
2525

26-
std::cout << "[Voxelgrid::initialize] " << resolution_ << "; num. voxels = [" << sizex_ << ", " << sizey_ << ", " << sizez_
27-
<< "], maxExtent = " << max << ", minExtent" << min << std::endl;
26+
std::cout << "[Voxelgrid::initialize] " << resolution_ << "; num. voxels = [" << sizex_ << ", " << sizey_ << ", "
27+
<< sizez_ << "], maxExtent = " << max.transpose() << ", minExtent" << min.transpose() << std::endl;
2828
}
2929

3030
void VoxelGrid::clear() {
@@ -54,7 +54,7 @@ void VoxelGrid::insert(const Eigen::Vector4f& p, uint32_t label) {
5454
int32_t gidx = index(i, j, k);
5555
if (gidx < 0 || gidx >= int32_t(voxels_.size())) return;
5656

57-
occupied_.push_back(gidx);
57+
if (voxels_[gidx].count == 0) occupied_.push_back(gidx);
5858

5959
// float n = voxels_[gidx].count;
6060
voxels_[gidx].labels[label] += 1; //(1. / (n + 1)) * (n * voxels_[gidx].point + p);
@@ -98,7 +98,7 @@ void VoxelGrid::updateOcclusions() {
9898
for (uint32_t j = 0; j < sizey_; ++j) {
9999
for (uint32_t k = 0; k < sizez_; ++k) {
100100
int32_t idx = index(i, j, k);
101-
occludedBy_[idx] = occludedBy(i, j, k);
101+
if (occludedBy_[idx] == -2) occludedBy_[idx] = occludedBy(i, j, k);
102102
occlusions_[idx] = occludedBy_[idx];
103103
if (occlusions_[idx] != idx) occluded_.push_back(idx);
104104
}
@@ -112,21 +112,21 @@ void VoxelGrid::updateOcclusions() {
112112
void VoxelGrid::updateInvalid(const Eigen::Vector3f& position) {
113113
if (!occlusionsValid_) updateOcclusions();
114114

115-
// std::fill(occludedBy_.begin(), occludedBy_.end(), -2);
115+
std::fill(occludedBy_.begin(), occludedBy_.end(), -2);
116116
for (uint32_t x = 0; x < sizex_; ++x) {
117117
for (uint32_t y = 0; y < sizey_; ++y) {
118118
for (uint32_t z = 0; z < sizez_; ++z) {
119119
int32_t idx = index(x, y, z);
120120
// idea: if voxel is not occluded, the value should be -1.
121-
occludedBy_[idx] = occludedBy(x, y, z, position);
121+
if (occludedBy_[idx] == -2) occludedBy_[idx] = occludedBy(x, y, z, position);
122122
invalid_[idx] = std::min<int32_t>(invalid_[idx], occludedBy_[idx]);
123123
}
124124
}
125125
}
126126
}
127127

128128
int32_t VoxelGrid::occludedBy(int32_t i, int32_t j, int32_t k, const Eigen::Vector3f& endpoint,
129-
std::vector<Eigen::Vector3i>* visited) const {
129+
std::vector<Eigen::Vector3i>* visited) {
130130
float NextCrossingT[3], DeltaT[3]; /** t for next intersection with voxel boundary of axis, t increment for axis
131131
**/
132132
int32_t Step[3], Out[3], Pos[3]; /** voxel increment for axis, index of of outside voxels, current position **/
@@ -181,11 +181,13 @@ int32_t VoxelGrid::occludedBy(int32_t i, int32_t j, int32_t k, const Eigen::Vect
181181
int32_t idx = index(Pos[0], Pos[1], Pos[2]);
182182
bool occupied = voxels_[idx].count > 0;
183183
if (visited != nullptr) visited->push_back(Eigen::Vector3i(Pos[0], Pos[1], Pos[2]));
184-
if (occludedBy_[idx] > -2) {
185-
return occludedBy_[idx];
184+
185+
if (occupied) {
186+
// for (auto i : traversed) occludedBy_[i] = idx;
187+
return idx;
186188
}
187189

188-
if (occupied) return idx;
190+
traversed.push_back(idx);
189191

190192
int32_t bits = ((NextCrossingT[0] < NextCrossingT[1]) << 2) + ((NextCrossingT[0] < NextCrossingT[2]) << 1) +
191193
((NextCrossingT[1] < NextCrossingT[2]));
@@ -201,5 +203,7 @@ int32_t VoxelGrid::occludedBy(int32_t i, int32_t j, int32_t k, const Eigen::Vect
201203
++iteration;
202204
}
203205

206+
// for (auto i : traversed) occludedBy_[i] = -1;
207+
204208
return -1;
205209
}

src/data/VoxelGrid.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class VoxelGrid {
8686
* \return index of voxel that occludes given voxel, -1 if voxel is not occluded.
8787
**/
8888
int32_t occludedBy(int32_t i, int32_t j, int32_t k, const Eigen::Vector3f& endpoint = Eigen::Vector3f::Zero(),
89-
std::vector<Eigen::Vector3i>* visited = nullptr) const;
89+
std::vector<Eigen::Vector3i>* visited = nullptr);
9090

9191
/** \brief get position of voxel center. **/
9292
inline Eigen::Vector3f voxel2position(int32_t i, int32_t j, int32_t k) const {

src/gen_data.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
#include <QtCore/QDir>
99
#include "data/voxelize_utils.h"
10+
#include "rv/Stopwatch.h"
1011
#include "rv/string_utils.h"
1112
#include "widget/KittiReader.h"
1213

14+
using namespace rv;
15+
1316
float poseDistance(const Eigen::Matrix4f& A, const Eigen::Matrix4f& B) {
1417
return (A.col(3).head(3) - B.col(3).head(3)).norm();
1518
}
@@ -88,29 +91,39 @@ int32_t main(int32_t argc, char** argv) {
8891
if (percentageLabeled > 90.0f) {
8992
Eigen::Matrix4f anchor_pose = priorPoints.back()->pose;
9093

94+
Stopwatch::tic();
9195
fillVoxelGrid(anchor_pose, priorPoints, priorLabels, priorGrid, config);
9296

9397
fillVoxelGrid(anchor_pose, priorPoints, priorLabels, pastGrid, config);
9498
fillVoxelGrid(anchor_pose, pastPoints, pastLabels, pastGrid, config);
99+
std::cout << "fill voxelgrid took " << Stopwatch::toc() << std::endl;
95100

101+
Stopwatch::tic();
96102
// updating occlusions.
97103
// std::cout << "updating occlusions." << std::endl;
98104
priorGrid.updateOcclusions();
99105
pastGrid.updateOcclusions();
106+
std::cout << "update occlusions took " << Stopwatch::toc() << std::endl;
100107

108+
Stopwatch::tic();
101109
priorGrid.insertOcclusionLabels();
102110
pastGrid.insertOcclusionLabels();
111+
std::cout << "occlusion labels took " << Stopwatch::toc() << std::endl;
103112

113+
Stopwatch::tic();
104114
for (uint32_t i = 0; i < pastPoints.size(); ++i) {
105115
Eigen::Vector3f endpoint = (anchor_pose.inverse() * pastPoints[i]->pose).col(3).head(3);
106116
pastGrid.updateInvalid(endpoint);
107117
}
118+
std::cout << "update invalid took " << Stopwatch::toc() << std::endl;
108119

120+
Stopwatch::tic();
109121
// store grid in mat file.
110122
std::stringstream outname;
111123
outname << seq << "_" << std::setfill('0') << std::setw(6) << current << ".mat";
112124
saveVoxelGrid(priorGrid, output_dirname + "/input/" + outname.str());
113125
saveVoxelGrid(pastGrid, output_dirname + "/label/" + outname.str());
126+
std::cout << "saving took " << Stopwatch::toc() << std::endl;
114127
} else {
115128
std::cout << "skipped." << std::endl;
116129
}

0 commit comments

Comments
 (0)