-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbm.cpp
206 lines (165 loc) · 5.01 KB
/
bm.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
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/voronoi_covariance_3.h>
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits_3.h>
#include <CGAL/Fuzzy_sphere.h>
#include <boost/timer.hpp>
#include <boost/progress.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <iostream>
#include <fstream>
#include <vector>
template <class T>
bool load(const std::string &s,
std::vector<T> &data)
{
std::ifstream is (s.c_str());
data.clear();
std::copy (std::istream_iterator<T> (is),
std::istream_iterator<T> (),
std::back_inserter(data));
return true;
}
template <class T>
bool load_if_newer (const std::string &t,
const std::string &s,
std::vector<T> &data)
{
if (!boost::filesystem::exists(s))
return false;
if (boost::filesystem::last_write_time(s) <
boost::filesystem::last_write_time(t))
return false;
return load (s, data);
}
template <class T>
bool save(const std::string &s,
const std::vector<T> &data)
{
std::ofstream os (s.c_str());
std::copy (data.begin(), data.end(),
std::ostream_iterator<T> (os, "\n"));
return true;
}
template <class K>
void convolve (const std::vector< typename CGAL::Point_3<K> > &points,
const std::vector<double> &cov,
std::vector<double> &ncov,
double r)
{
typedef typename CGAL::Point_3<K> Point;
typedef typename CGAL::Search_traits_3<K> Traits;
typedef typename CGAL::Kd_tree<Traits> Tree;
typedef typename CGAL::Fuzzy_sphere<Traits> Fuzzy_sphere;
Tree tree (points.begin(), points.end());
boost::progress_display p(points.size(), std::cerr);
std::map<Point, size_t> indices;
for (size_t i = 0; i < points.size(); ++i)
indices[points[i]] = i;
ncov.clear();
for (size_t i = 0; i < points.size(); ++i)
{
std::vector<Point> nn;
tree.search(std::back_inserter(nn),
Fuzzy_sphere (points[i], r));
double m;
for (size_t k = 0; k < nn.size(); ++k)
m += cov[indices [nn[k]]];
ncov.push_back(m);
++p;
}
}
template <class K>
void offset (const std::vector< typename CGAL::Point_3<K> > &points,
std::vector<double> &cov,
double R, size_t N)
{
typedef CGAL::Delaunay_triangulation_3<K> DT;
typename CGAL::Sphere_discretization<K> sphere(R, N);
DT dt(points.begin(), points.end());
boost::progress_display p(points.size(), std::cerr);
cov.clear();
boost::timer t;
for (size_t i = 0; i < points.size(); ++i)
{
typename DT::Vertex_handle vh = dt.nearest_vertex(points[i]);
double c = CGAL::voronoi_volume_3(dt, vh, sphere);
cov.push_back(c);
++p;
}
std::cerr << "time: " << t.elapsed() << "s\n";
}
namespace po = boost::program_options;
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::FT FT;
typedef K::Point_3 Point;
int main(int argc, char **argv)
{
double R, r;
size_t N = 20;
po::options_description desc("Options");
desc.add_options()
("help", "produce help message")
("N", po::value<size_t>(&N)->default_value(20),
"number of planes used to discretize sphere:\n"
" 20 for dodecahedron\n"
" 12 for icosahedron")
("R", po::value<double>(&R)->default_value(0.1),
"offset radius")
("r", po::value<double>(&r)->default_value(0),
"convolution radius")
("input", po::value< std::vector<std::string> >(),
"input cloud/xyz files");
po::positional_options_description p;
p.add("input", -1);
po::variables_map args;
po::store(po::command_line_parser(argc, argv).options(desc)
.style (po::command_line_style::default_style |
po::command_line_style::allow_long_disguise)
.positional(p).run(), args);
po::notify(args);
if (args.count("help"))
{
std::cout << desc;
return 1;
}
if (!args.count ("input"))
{
std::cout << argv[0] << ": no input file\n";
return 1;
}
std::vector< std::string > files =
args["input"].as< std::vector<std::string> >();
for (size_t i = 0; i < files.size(); ++i)
{
std::vector<double> cov, ccov;
std::vector<Point> points;
std::string source = files[i],
bn = boost::filesystem::basename(source),
fncov = (boost::format("%s-R=%g.p") % bn % R).str(),
fnccov = (boost::format("%s-R=%g-r=%g.p") % bn % R % r).str();
load(files[i], points);
//std::cerr << "built Delaunay triangulation in " << t.elapsed() << "s\n";
//std::cerr<< "number of vertices: " << dt.number_of_vertices() << "\n";
std::cerr << "offsetting " << source << " into " << fncov << "\n";
if (!load_if_newer (source, fncov, cov) ||
cov.size() != points.size())
{
offset (points, cov, R, N);
save (fncov, cov);
}
if (r == 0)
continue;
std::cerr << "convolving " << source << " into " << fnccov << "\n";
if (!load_if_newer (fncov, fnccov, ccov) ||
ccov.size() != points.size())
{
convolve (points, cov, ccov, r);
save (fnccov, ccov);
}
}
return 0;
}