33#include " src/OctreeGenerator.h"
44#include " src/dbScan.h"
55#include " src/HTRBasicDataStructures.h"
6+ #include < boost/filesystem.hpp>
7+ #include < boost/algorithm/algorithm.hpp>
8+ #include < boost/thread/thread.hpp>
69
710
811// Colors to display the generated clusters
@@ -29,6 +32,17 @@ float colors[] = { 255,0,0, //red 1
2932
3033 }; // 20x3=60 color elements
3134
35+ bool is_number (const std::string& s){
36+
37+ std::string::const_iterator it = s.begin ();
38+ while (it != s.end () && std::isdigit (*it)) ++it;
39+ return !s.empty () && it == s.end ();
40+ /*
41+ return !s.empty() && std::find_if(s.begin(),
42+ s.end(), [](char c) { return !std::isdigit(c); }) == s.end();
43+ */
44+ }
45+
3246void readCloudFromFile (int argc, char ** argv, std::vector<htr::Point3D>& points,
3347 pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud){
3448
@@ -214,13 +228,49 @@ void init(int argc, char** argv,bool show){
214228 readCloudFromFile (argc, argv, groupA,cloud);
215229
216230 // ----------------------------------------------------------------
217- int octreeResolution = std::atoi (argv[2 ]);
218- float eps = std::atof (argv[3 ]); // 40.0f
219- int minPtsAux_ = std::atof (argv[4 ]); // >=3 /*INPUT PARAMETERS*/
220- int minPts = std::atof (argv[5 ]); // 10
221- std::string output_dir = argv[6 ];
231+ std::string octreeResolution_str = argv[2 ];
232+ std::string eps_str = argv[3 ];// 40.0f
233+ std::string minPtsAux_str = argv[4 ];// >=3 /*INPUT PARAMETERS*/
234+ std::string minPts_str = argv[5 ];// >=3
235+ std::string output_dir = argv[6 ];// 10
236+ // ----------------------------------------------------------------
237+
238+ // ----------------------------------------------------------------
239+
240+ if (not is_number (octreeResolution_str)){
241+ PCL_ERROR (" \n Error: enter a valid octree resolution\n " );
242+ std::exit (-1 );
243+ }else if (not is_number (eps_str)){
244+ PCL_ERROR (" \n Error: enter a valid epsilon\n " );
245+ std::exit (-1 ); /* VALIDATION*/
246+ }else if (not is_number (minPtsAux_str)){
247+ PCL_ERROR (" \n Error: enter a valid min points aux\n " );
248+ std::exit (-1 );
249+ }else if (not is_number (minPts_str)){
250+ PCL_ERROR (" \n Error: enter a valid min points\n " );
251+ std::exit (-1 );
252+ }
253+
254+ boost::filesystem::path dirPath (output_dir);
255+
256+ if (not boost::filesystem::exists (dirPath) or not boost::filesystem::is_directory (dirPath)){
257+ pcl::console::print_error (" \n Error. does not exist or it's not valid: " );
258+ std::cout << output_dir << std::endl;
259+ std::exit (-1 );
260+ }
261+
222262 // ----------------------------------------------------------------
223263
264+ int octreeResolution = std::atoi (octreeResolution_str.c_str ());
265+ float eps = std::atof (eps_str.c_str ());
266+ int minPtsAux_ = std::atof (minPtsAux_str.c_str ());
267+ int minPts = std::atof (minPts_str.c_str ());
268+
269+ if (minPts < 3 ){
270+ pcl::console::print_error (" \n minPts must be >= 3! \n " );
271+ std::exit (-1 );
272+ }
273+
224274 /*
225275 DBSCAN algorithm requires 5 parameters - octreeResoluion, describes the length of the smallest voxels at lowest
226276 octree level. epsilon , which specifies how close points should be to each other to be considered a part of a
@@ -242,9 +292,16 @@ void init(int argc, char** argv,bool show){
242292 ofstream fout;
243293 int cont = 0 ;
244294
245- for ( auto & cluster : dbscan.getClusters ()){
295+ if ( dbscan.getClusters (). size ()<= 0 ){
246296
247- // std::cout << "cluster " << cont << " size " << cluster.clusterPoints.size() << std::endl;
297+ pcl::console::print_error (" \n Could not generated clusters, bad parameters\n " );
298+ std::exit (-1 );
299+
300+ }
301+
302+ // Save cloud_cluster_#.txt:
303+
304+ for (auto & cluster : dbscan.getClusters ()){
248305
249306 std::string str1 = output_dir;
250307 str1 += " /cloud_cluster_" ;
@@ -253,57 +310,27 @@ void init(int argc, char** argv,bool show){
253310
254311 fout.open (str1.c_str ());
255312
256- for (auto & point:cluster.clusterPoints ){
257-
258- // fout << point.x << " " << point.y << " "<< point.z << " " << point.r << " " << point.g << " " << point.b << std::endl;
259-
260- uint32_t rgb_ = *reinterpret_cast <int *>(&point.rgb );
261- uint8_t r_, g_, b_;
262-
263- r_ = (rgb_ >> 16 ) & 0x0000ff ;
264- g_ = (rgb_ >> 8 ) & 0x0000ff ;
265- b_ = (rgb_) & 0x0000ff ;
266-
267- unsigned int r, g, b;
268- r = *((uint8_t *) &r_);
269- g = *((uint8_t *) &g_);
270- b = *((uint8_t *) &b_);
271-
272- fout << point.x << " " << point.y << " " << point.z << " " << r << " " << g << " " << b << std::endl;
273- /*
274-
275- for(size_t i=0;i<cloud->points.size();i++){
276-
277- pcl::PointXYZRGB pt = cloud->points.at(i);
278-
279- if(pt.x == point.x and pt.y == point.y and pt.z == point.z){
280-
281- uint32_t rgb_ = *reinterpret_cast<int*>(&pt.rgb);
282- uint8_t r_, g_, b_;
283-
284- r_ = (rgb_ >> 16) & 0x0000ff;
285- g_ = (rgb_ >> 8) & 0x0000ff;
286- b_ = (rgb_) & 0x0000ff;
287-
288- unsigned int r, g, b;
289- r = *((uint8_t *) &r_);
290- g = *((uint8_t *) &g_);
291- b = *((uint8_t *) &b_);
292-
293- fout << point.x << " " << point.y << " "<< point.z << " " << r << " " << g << " " << b << std::endl;
294-
295- }else{
296- continue;
297- }
298-
299- }
300- */
301-
302- }
313+ for (auto & point:cluster.clusterPoints ){
314+
315+ uint32_t rgb_ = *reinterpret_cast <int *>(&point.rgb );
316+ uint8_t r_, g_, b_;
317+
318+ r_ = (rgb_ >> 16 ) & 0x0000ff ;
319+ g_ = (rgb_ >> 8 ) & 0x0000ff ;
320+ b_ = (rgb_) & 0x0000ff ;
321+
322+ unsigned int r, g, b;
323+ r = *((uint8_t *) &r_);
324+ g = *((uint8_t *) &g_);
325+ b = *((uint8_t *) &b_);
326+
327+ fout << point.x << " " << point.y << " " << point.z << " " << r << " " << g << " " << b << std::endl;
328+
329+ }
303330
304331 fout.close ();
305332 cont +=1 ;
306- }
333+ }
307334
308335 ofstream fout2;
309336 std::string str2 = output_dir;
@@ -365,28 +392,22 @@ void init(int argc, char** argv,bool show){
365392 r = (uint8_t ) uniform_0_255 (gen);
366393 g = (uint8_t ) uniform_0_255 (gen);
367394 b = (uint8_t ) uniform_0_255 (gen);
395+
396+ }
368397
369-
370- }
398+ // Adding different color to each cluster
371399
372-
373400 for (auto & pointCluster:cluster.clusterPoints ){
374401
375402 pcl::PointXYZRGB point;
376403 point.x = pointCluster.x ;
377404 point.y = pointCluster.y ;
378405 point.z = pointCluster.z ;
379-
380- // uint32_t rgb = (static_cast<uint32_t>(r) << 16 |
381- // static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
382- // point.rgb = *reinterpret_cast<float*>(&rgb);
383406
384407 uint32_t rgb = (static_cast <uint32_t >(r) << 16 |
385408 static_cast <uint32_t >(g) << 8 | static_cast <uint32_t >(b));
386409 point.rgb = *reinterpret_cast <float *>(&rgb);
387- // point.r = 255;
388- // point.g = 255;
389- // point.b = (uint8_t)colors[j+2];
410+
390411 cluster_rgb->points .push_back (point);
391412 }
392413
@@ -445,12 +466,7 @@ int main(int argc, char** argv){
445466 std::cerr << " Support: ply - pcd - txt - xyz" << std::endl;
446467 return -1 ;
447468 }
448-
449- if (std::atoi (argv[5 ]) < 3 ){
450- std::cerr << " minPts must be >= 3!" << std::endl;
451- return -1 ;
452- }
453-
469+
454470 std::cout << " \n *************************************" << std::endl;
455471 std::cout << " *** DBSCAN Cluster Segmentation *** " << std::endl;
456472 std::cout << " *************************************" << std::endl;
0 commit comments