Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "ros/node_handle.h"
00034 #include "ros/time.h"
00035 #include "ros/common.h"
00036
00037 #include "std_msgs/PointCloud.h"
00038
00039
00040 #include <stdlib.h>
00041 #include "string_utils/string_utils.h"
00042
00043 #include <fstream>
00044
00045 using namespace std;
00046
00047 class PCDGenerator: public ros::node
00048 {
00049 public:
00050
00051
00052 std_msgs::PointCloud msg_cloud_;
00053
00054 PCDGenerator () : ros::node ("PCD_generator")
00055 {
00056
00057 advertise<std_msgs::PointCloud>("cloud_stoc", 1);
00058 }
00059
00060 ~PCDGenerator ()
00061 {
00062 }
00063
00065
00067
00068
00069
00070
00071
00072 std_msgs::PointCloud
00073 LoadPointCloud (const char* fileName)
00074 {
00075 std_msgs::PointCloud cloud;
00076
00077 int nr_points = 0;
00078 std::ifstream fs;
00079 std::string line;
00080
00081 int idx = 0;
00082
00083 fs.open (fileName);
00084 if (!fs.is_open ())
00085 {
00086 fprintf (stderr, "Couldn't open %s for reading!\n", fileName);
00087 return cloud;
00088 }
00089
00090 while (!fs.eof ())
00091 {
00092 getline (fs, line);
00093 if (line == "")
00094 continue;
00095
00096 vector<string> st;
00097 string_utils::split (line, st, " ");
00098
00099
00100
00101 std::string lineType = st.at (0);
00102
00104 if (lineType.substr (0, 1) == "#")
00105 continue;
00106
00107 if (lineType.substr (0, 7) == "COLUMNS")
00108 {
00109
00110 int remainingTokens = st.size () - (1 + 3);
00111
00112
00113
00114 cloud.set_chan_size (remainingTokens);
00115
00116
00117 for (int i = 0; i < remainingTokens; i++)
00118 {
00119
00120 std::string colType = st.at (i + 4);
00121 cloud.chan[i].name = colType;
00122 }
00123 continue;
00124 }
00125
00126 if (lineType.substr (0, 6) == "POINTS")
00127 {
00128
00129 nr_points = atoi (st.at (1).c_str ());
00130 cloud.set_pts_size (nr_points);
00131
00132 for (unsigned int d = 0; d < cloud.get_chan_size (); d++)
00133 cloud.chan[d].set_vals_size (nr_points);
00134
00135 continue;
00136 }
00137
00138
00139 if (lineType.substr (0, 4) == "DATA")
00140 continue;
00141
00142
00143
00144 if (idx >= nr_points)
00145 {
00146 fprintf (stderr, "Error: input file %s has more points than advertised (%d)!\n", fileName, nr_points);
00147 break;
00148 }
00149
00150
00151
00152
00153
00154
00155 cloud.pts[idx].x = atof (st.at (0).c_str ());
00156 cloud.pts[idx].y = atof (st.at (1).c_str ());
00157 cloud.pts[idx].z = atof (st.at (2).c_str ());
00158 for (unsigned int i = 0; i < cloud.get_chan_size (); i++)
00159
00160 cloud.chan[i].vals[idx] = atof (st.at (i+3).c_str ());
00161
00162 idx++;
00163 }
00164
00165 fs.close ();
00166
00167 if (idx != nr_points)
00168 fprintf (stderr, "Warning! Number of points read (%d) is different than expected (%d)\n", idx, nr_points);
00169
00170 return cloud;
00171 }
00172
00173
00175
00176 int
00177 start ()
00178 {
00179 msg_cloud_ = LoadPointCloud ("stoc.pcd");
00180 return (0);
00181 }
00182
00184
00185 bool spin ()
00186 {
00187 while (ok ())
00188 {
00189 usleep (100000);
00190
00191 publish ("cloud_stoc", msg_cloud_);
00192 }
00193
00194 return true;
00195 }
00196
00197
00198 };
00199
00200
00201 int
00202 main (int argc, char** argv)
00203 {
00204 ros::init (argc, argv);
00205
00206 PCDGenerator c;
00207
00208 c.start ();
00209 c.spin ();
00210
00211 ros::fini ();
00212
00213 return (0);
00214 }
00215