11#include " curvatureEstimation.h"
22
3+ #include < fstream>
4+ #include " nlohmann/json.hpp"
5+
36#include < iostream>
47
8+ using json = nlohmann::json;
9+
10+ struct TimeResult
11+ {
12+ float mean{0 }; // / Mean time in msec
13+ float var{0 }; // / Variance in msec
14+ };
15+
16+
17+ template <int nbRuns, typename PProcess, typename RProcessList>
18+ std::vector<TimeResult> mesureTime (PProcess prepare, RProcessList runList, const std::vector<std::string>& names)
19+ {
20+ std::vector<TimeResult> res;
21+ res.resize (names.size ());
22+
23+ // collect measurements
24+ std::vector<std::array<int , nbRuns>> times;
25+ times.resize (names.size ());
26+
27+ for (int i = 0 ; i != nbRuns; ++i)
28+ {
29+ prepare ();
30+ int j = 0 ;
31+ for (auto run : runList)
32+ {
33+ auto start = std::chrono::steady_clock::now ();
34+ run ();
35+ auto end = std::chrono::steady_clock::now ();
36+ std::chrono::duration<double , std::milli> elapsed = end - start;
37+ times[j][i] = elapsed.count ();
38+ res[j].mean += times[j][i];
39+ ++j;
40+ }
41+ }
42+
43+ int j = 0 ;
44+ for (const auto & name : names)
45+ {
46+ res[j].mean /= float (nbRuns);
47+
48+ // compute mean v
49+ for (int i = 0 ; i != nbRuns; ++i)
50+ {
51+ res[j].var += std::pow ((times[j][i]-res[j].mean ),2 );
52+ }
53+ res[j].var /= float (nbRuns);
54+ ++j;
55+ }
56+
57+ return res;
58+ }
59+
60+ std::vector<int >logScale (int start, double base, int nbElements = 10 )
61+ {
62+
63+ std::vector<int > scale;
64+ scale.reserve (nbElements);
65+ double current = start;
66+
67+ // Generate the logarithmically spaced values
68+ std::generate_n (std::back_inserter (scale), nbElements, [&start, base, ¤t]() {
69+ int c (current);
70+ current *= base;
71+ return c;
72+ });
73+ return scale;
74+ }
75+
576int main (int argc, char **argv)
677{
7- int nbPoints = 10000 ;
8- int nbQueries = 100 ;
978 double dataScale = 10 ;
10- double scale = dataScale / 5 ;
79+ double scale = dataScale / 10 ;
80+
81+ Eigen::MatrixXd points;
82+ Eigen::MatrixXd queries;
1183
12- Eigen::MatrixXd points (nbPoints, 6 );
13- Eigen::MatrixXd queries (nbQueries, 3 );
84+ int start = 500 ;
85+ double base = 1.9 ;
86+ int nbSteps = 10 ;
87+ auto values = logScale (start, base, nbSteps);
88+
89+ std::vector<std::string> names {
90+ " buildKdTree" ,
91+ " asoCurvatureEstimation" ,
92+ " planeFit" };
93+ std::vector<std::function<void (void )>> runs {
94+ [&points](){buildKdTree (points);},
95+ [&queries, scale](){int k; asoCurvatureEstimation (queries, scale, k);},
96+ [&queries, scale](){int k; planeFit (queries, scale, k);}
97+ };
98+
99+ json j;
100+
101+ // prepare json structure
102+ {
103+ std::vector<double >placeholder;
104+ placeholder.resize (nbSteps);
105+ for (const auto & name : names)
106+ {
107+ j[name][" mean" ] = placeholder;
108+ j[name][" var" ] = placeholder;
109+ }
110+ j[" steps" ] = values;
111+ }
14112
15- generatePointClouds (points, queries, dataScale) ;
16- if ( ! buildKdTree (points) )
113+ int stepId = 0 ;
114+ for ( auto v : values )
17115 {
18- return EXIT_FAILURE ;
116+ std::cout << " Run test with nb points = " << v << std::endl;
117+ int n = v; // number of points
118+ int q = v/10 ; // number of queries
119+
120+ auto prepare = [&points, &queries, dataScale, n, q]()
121+ {
122+ points = Eigen::MatrixXd (n, 6 );
123+ queries = Eigen::MatrixXd (q, 3 );
124+ generatePointClouds (points, queries, dataScale);
125+ buildKdTree (points);
126+ };
127+
128+ auto res = mesureTime<10 >(prepare, runs, names);
129+
130+ int index = 0 ;
131+ for (const auto & name : names)
132+ {
133+ j[name][" mean" ][stepId] = res[index].mean ;
134+ j[name][" var" ][stepId] = res[index].var ;
135+ ++index;
136+ }
137+
138+ ++stepId;
19139 }
20140
21- int meanK;
22- int ret = asoCurvatureEstimation (queries, scale, meanK);
23- std::cout << " [ASO] Number of fits: " << ret << " (over " << nbQueries << " tries) with " << meanK << " neighbors in average" << std::endl;
24- ret = planeFit (queries, scale, meanK);
25- std::cout << " [PLANE] Number of fits: " << ret << " (over " << nbQueries << " tries) with " << meanK << " neighbors in average" << std::endl;
141+ // write prettified JSON
142+ std::ofstream o (" run_output.json" );
143+ o << std::setw (4 ) << j<< std::endl;
144+ o.close ();
145+
146+
147+
148+ // int ret = asoCurvatureEstimation(queries, scale, meanK);
149+ // std::cout << "[ASO] Number of fits: " << ret << " (over " << nbQueries << " tries) with " << meanK << " neighbors in average" << std::endl;
150+ // ret = planeFit(queries, scale, meanK);
151+ // std::cout << "[PLANE] Number of fits: " << ret << " (over " << nbQueries << " tries) with " << meanK << " neighbors in average" << std::endl;
26152
27153 return EXIT_SUCCESS ;
28154}
0 commit comments