import_pts.cpp
Go to the documentation of this file.
00001 #include <unistd.h>
00002 #include <iostream>
00003 #include <argp.h>
00004 
00005 #include <boost/algorithm/string.hpp>
00006 
00007 #include <megatree/common.h>
00008 #include <megatree/megatree.h>
00009 #include <megatree/storage_factory.h>
00010 #include <megatree/tree_functions.h>
00011 
00012 #include <fstream>
00013 #include <iostream>
00014 
00015 using namespace megatree;
00016 
00017 static unsigned long total_points = 0;
00018 static Tictoc overall_timer;
00019 
00020 void importTxt(MegaTree &tree, const boost::filesystem::path &path, unsigned long max_intensity, unsigned int *skip = NULL)
00021 {
00022   printf("Loading pts file: %s\n", path.string().c_str());
00023   std::ifstream fin;
00024   fin.open(path.string().c_str(), std::ios::in);
00025 
00026   char line[1024];
00027   fin.getline(line, 1024);  // Skips the first line
00028 
00029   // Adds each point into the tree
00030   unsigned i = 0;
00031   std::vector<double> point(3, 0.0);
00032   std::vector<double> color(3, 0.0);
00033 
00034   // construct fast adding structure
00035   TreeFastCache tree_cache(tree);
00036 
00037   while (!fin.eof())
00038   {
00039     fin.getline(line, 1024);
00040 
00041     std::vector<std::string> bits;
00042     boost::split(bits, line, boost::is_any_of(" ,"));
00043     if (bits.size() != 7)
00044       continue;
00045 
00046     point[0] = atof(bits[0].c_str());
00047     point[1] = atof(bits[1].c_str());
00048     point[2] = atof(bits[2].c_str());
00049 
00050     color[0] = atoi(bits[4].c_str());
00051     color[1] = atoi(bits[5].c_str());
00052     color[2] = atoi(bits[6].c_str());
00053     
00054     if (i % 100000 == 0) {
00055       printf("Adding %u\n", i);
00056       printf("%8.1f   %s\n", overall_timer.toc(), tree.toString().c_str());
00057       tree.resetCount();
00058     }
00059     tree_cache.addPoint(point, color);
00060 
00061     ++total_points;
00062     /*
00063     if (total_points % 10000000L == 0)
00064     {
00065       printf("Flushing %lu points\n", total_points);
00066       tree.flushCache();
00067     }
00068     */
00069     
00070     ++i;
00071   }
00072   printf("%s\n", tree.toString().c_str());
00073   tree.resetCount();
00074 }
00075 
00076 
00077 struct arguments_t {
00078   int cache_size;
00079   char* tree;
00080   unsigned int skip;
00081   unsigned long max_intensity;
00082   std::vector<std::string> las_filenames;
00083 };
00084 
00085 
00086 static int parse_opt(int key, char *arg, struct argp_state *state)
00087 {
00088   struct arguments_t *arguments = (arguments_t*)state->input;
00089   
00090   switch (key)
00091   {
00092   case 'c':
00093     //arguments->cache_size = atoi(arg);
00094     arguments->cache_size = parseNumberSuffixed(arg);
00095     break;
00096   case 't':
00097     arguments->tree = arg;
00098     break;
00099   case 's':
00100     arguments->skip = (unsigned int)parseNumberSuffixed(arg);
00101     break;
00102   case 'i':
00103     arguments->max_intensity = atol(arg);
00104     break;
00105   case ARGP_KEY_ARG:
00106     arguments->las_filenames.push_back(arg);
00107     break;
00108   }
00109   return 0;
00110 }
00111 
00112 
00113 int main (int argc, char** argv)
00114 {
00115   // Default command line arguments
00116   struct arguments_t arguments;
00117   arguments.cache_size = 3000000;
00118   arguments.max_intensity = 255;  // 16384
00119   arguments.skip = 0;
00120   arguments.tree = 0;
00121   
00122   // Parses command line options
00123   struct argp_option options[] = {
00124     {"cache-size", 'c', "SIZE",   0,     "Cache size"},
00125     {"tree",       't', "TREE",   0,     "Path to tree"},
00126     {"skip",       's', "SKIP",   0,     "Number of points to skip"},
00127     {"max-intensity",  'i', "INTENSITY",  0,     "Maximum intensity value"},
00128     { 0 }
00129   };
00130   struct argp argp = { options, parse_opt };
00131   int parse_ok = argp_parse(&argp, argc, argv, 0, 0, &arguments);
00132   printf("Arguments parsed: %d\n", parse_ok);
00133   printf("Cache size: %d\n", arguments.cache_size);
00134   printf("Tree: %s\n", arguments.tree);
00135   if (arguments.skip > 0)
00136     printf("Skipping %u points\n", arguments.skip);
00137   assert(parse_ok == 0);
00138 
00139   if (!arguments.tree) {
00140     fprintf(stderr, "No tree path given.\n");
00141     return 1;
00142   }
00143 
00144   boost::shared_ptr<Storage> storage(openStorage(arguments.tree));
00145   MegaTree tree(storage, arguments.cache_size, false);
00146 
00147   overall_timer.tic();
00148   
00149   Tictoc timer;
00150   for (size_t i = 0; i < arguments.las_filenames.size(); ++i)
00151   {
00152     boost::filesystem::path las_path(arguments.las_filenames[i]);
00153     printf("Importing %s into tree\n", las_path.string().c_str());
00154 
00155     Tictoc one_file_timer;
00156     importTxt(tree, las_path, arguments.max_intensity, &arguments.skip);
00157     float t = one_file_timer.toc();
00158     printf("Flushing %s...\n", las_path.string().c_str());
00159     tree.flushCache();
00160     printf("Finished %s in %.3lf seconds (%.1lf min or %.1lf hours)\n",
00161            las_path.string().c_str(), t, t/60.0f, t/3600.0f);
00162   }
00163 
00164   printf("Flushing the cache\n");
00165   tree.flushCache();
00166   
00167   float t = timer.toc();
00168   unsigned long num_points = tree.getNumPoints();
00169   printf("Importing all files took %.3f seconds for %lu points ==> %.3f sec/million\n", t, num_points, t / num_points * 1e6);
00170 
00171   printf("Finished.\n");
00172   return 0;
00173 }


megatree_import
Author(s): Wim Meeussen
autogenerated on Thu Nov 28 2013 11:30:38