Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef POINT_CLOUD_H
00017 #define POINT_CLOUD_H
00018
00019 #include <string>
00020 #include <vector>
00021
00022 #include "cloud.h"
00023 #include "options.h"
00024 #include "math_types.h"
00025 #include <boost/filesystem.hpp>
00026
00031 class point_cloud : public cloud3
00032 {
00033 public:
00053 point_cloud (const options &opts, const calibration &_calib)
00054 : cloud3 (opts, _calib),
00055 input_template (opts.data_template ())
00056 {
00057 init ();
00058 };
00059
00060 virtual bool read_background (const std::string &depth_background)
00061 {
00062 point_vector P;
00063 const int num_frames = 5;
00064 P.reserve (num_frames * 320 * 240);
00065 for (int bg_frame = 15; bg_frame < 15+num_frames; bg_frame++)
00066 {
00067 const char *format = "%s/points_%d.txt";
00068 char buffer [100];
00069 sprintf (buffer, format, depth_background.c_str (), bg_frame);
00070
00071 if (boost::filesystem::is_regular (buffer))
00072 {
00073 FILE *fid = fopen (buffer, "r");
00074 const bool found = (fid != NULL);
00075 if (!found)
00076 {
00077 std::cerr << "point_cloud::point_cloud: could not open depth background '"
00078 << buffer << "' for reading" << std::endl;
00079 }
00080 else
00081 {
00082
00083 const int buf_size = 100;
00084 char line [buf_size];
00085 size_t num_points = 0;
00086 while (fgets (line, buf_size, fid) != NULL)
00087 num_points ++;
00088
00089
00090 rewind (fid);
00091 for (size_t k = 0; k < num_points; k++)
00092 {
00093 double x, y, z;
00094 double r, g, b;
00095 const int num_read = fscanf (fid, "%lf %lf %lf %lf %lf %lf\n", &x, &y, &z, &r, &g, &b);
00096 if (num_read != 6)
00097 {
00098 std::cerr << "point_cloud::point_cloud: invalid data format" << std::endl;
00099 break;
00100 }
00101
00102 const vector3 point = calib.camera_to_world (vector3 (x, y, z));
00103 P.push_back (point);
00104 }
00105 }
00106
00107 fclose (fid);
00108 }
00109 }
00110
00111 if (P.size () > 0)
00112 bg_nearest_cloud.refresh (P);
00113
00114 return (P.size () > 0);
00115 }
00116
00131 bool refresh (bool goto_next = true)
00132 {
00133
00134 char filename [100];
00135 sprintf (filename, input_template.c_str (), frame);
00136 if (goto_next) frame ++;
00137
00138 FILE *fid = fopen (filename, "r");
00139 const bool found = (fid != NULL);
00140 if (!found)
00141 {
00142 std::cerr << "point_cloud::refresh: could not open '"
00143 << filename << "' for reading" << std::endl;
00144 }
00145 else
00146 {
00147
00148 const int buf_size = 100;
00149 char line [buf_size];
00150 size_t num_points = 0;
00151 while (fgets (line, buf_size, fid) != NULL)
00152 num_points ++;
00153
00154
00155 points.clear ();
00156 points.reserve (num_points / downsample);
00157 colours.clear ();
00158 colours.reserve (num_points / downsample);
00159
00160
00161 rewind (fid);
00162 const double y_thresh = -0.5;
00163 for (size_t k = 0; k < num_points; k++)
00164 {
00165 double x, y, z;
00166 double r, g, b;
00167 const int num_read = fscanf (fid, "%lf %lf %lf %lf %lf %lf\n", &x, &y, &z, &r, &g, &b);
00168 if (num_read != 6)
00169 {
00170 std::cerr << "point_cloud::refresh: invalid data format" << std::endl;
00171 break;
00172 }
00173
00174
00175 if (k % downsample == 0)
00176 {
00177 const vector3 point = calib.camera_to_world (vector3 (x, y, z));
00178
00179 if (!use_depth_threshold || (point [2] > depth_threshold && point [1] > y_thresh))
00180 {
00181 double d = -1;
00182 if (!use_background_subtraction || (d = bg_nearest_cloud.nearest_distance (point)) > 0.5)
00183 {
00184 points.push_back (point);
00185 colours.push_back (vector3 (r, g, b));
00186 }
00187 }
00188 }
00189 }
00190 fclose (fid);
00191
00192
00193
00194
00195
00196 nearest_cloud.refresh (points);
00197 }
00198
00199 return found;
00200 }
00201
00213 vector3 bg_normal (const size_t i) const
00214 {
00215 return vector3 (0, 0, 0);
00216 }
00217
00218 protected:
00219 const std::string input_template;
00220 };
00221
00222 #endif // POINT_CLOUD_H