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
00034
00035
00036
00037
00038 #include <boost/thread/thread.hpp>
00039 #define MEASURE_FUNCTION_TIME
00040 #include <pcl/common/time.h>
00041 #include <pcl/io/openni_grabber.h>
00042 #include <pcl/visualization/point_cloud_handlers.h>
00043 #include <pcl/visualization/pcl_visualizer.h>
00044 #include <pcl/visualization/image_viewer.h>
00045 #include <pcl/console/print.h>
00046 #include <pcl/console/parse.h>
00047 #include <pcl/console/time.h>
00048
00049 #define SHOW_FPS 1
00050 #if SHOW_FPS
00051 #define FPS_CALC(_WHAT_) \
00052 do \
00053 { \
00054 static unsigned count = 0;\
00055 static double last = pcl::getTime ();\
00056 double now = pcl::getTime (); \
00057 ++count; \
00058 if (now - last >= 1.0) \
00059 { \
00060 std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" << std::endl; \
00061 count = 0; \
00062 last = now; \
00063 } \
00064 }while(false)
00065 #else
00066 #define FPS_CALC(_WHAT_) \
00067 do \
00068 { \
00069 }while(false)
00070 #endif
00071
00072 boost::mutex cld_mutex, img_mutex;
00073 pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr g_cloud;
00074 boost::shared_ptr<openni_wrapper::Image> g_image;
00075
00076 void
00077 printHelp (int argc, char **argv)
00078 {
00079 using pcl::console::print_error;
00080 using pcl::console::print_info;
00081 print_error ("Syntax is: %s <options>\n", argv[0]);
00082 print_info (" where options are:\n");
00083 print_info (" -dev device_id = device to be used\n");
00084 print_info (" maybe \"#n\", with n being the number of the device in device list.\n");
00085 print_info (" maybe \"bus@addr\", with bus and addr being the usb bus and address where device is connected.\n");
00086 print_info (" maybe \"serial\", with serial being the serial number of the device.\n");
00087 print_info ("\n");
00088 }
00089
00090
00091 boost::shared_ptr<pcl::visualization::PCLVisualizer> cld;
00092 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00093 boost::shared_ptr<pcl::visualization::ImageViewer> img;
00094 #endif
00095
00096 struct EventHelper
00097 {
00098 void
00099 cloud_cb (const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr & cloud)
00100 {
00101 FPS_CALC ("callback");
00102 cld_mutex.lock ();
00103 g_cloud = cloud;
00104 cld_mutex.unlock ();
00105 }
00106
00107 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00108 void
00109 image_callback (const boost::shared_ptr<openni_wrapper::Image>& image)
00110 {
00111 FPS_CALC ("image callback");
00112 img_mutex.lock ();
00113 g_image = image;
00114 img_mutex.unlock ();
00115 }
00116 #endif
00117 };
00118
00119 void
00120 keyboard_callback (const pcl::visualization::KeyboardEvent& event, void* cookie)
00121 {
00122 std::string* message = (std::string*)cookie;
00123 cout << (*message) << " :: ";
00124 if (event.getKeyCode())
00125 cout << "the key \'" << event.getKeyCode() << "\' (" << (int)event.getKeyCode() << ") was";
00126 else
00127 cout << "the special key \'" << event.getKeySym() << "\' was";
00128 if (event.keyDown())
00129 cout << " pressed" << endl;
00130 else
00131 cout << " released" << endl;
00132 }
00133
00134 void
00135 mouse_callback (const pcl::visualization::MouseEvent& mouse_event, void* cookie)
00136 {
00137 std::string* message = (std::string*) cookie;
00138 if (mouse_event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && mouse_event.getButton() == pcl::visualization::MouseEvent::LeftButton)
00139 {
00140 cout << (*message) << " :: " << mouse_event.getX () << " , " << mouse_event.getY () << endl;
00141 }
00142 }
00143
00144
00145 int
00146 main (int argc, char** argv)
00147 {
00148 if (argc > 1)
00149 {
00150 for (int i = 1; i < argc; i++)
00151 {
00152 if (std::string (argv[i]) == "-h")
00153 {
00154 printHelp (argc, argv);
00155 return (-1);
00156 }
00157 }
00158 }
00159
00160 EventHelper event_helper;
00161 std::string device_id = "";
00162 pcl::console::parse_argument (argc, argv, "-dev", device_id);
00163
00164 pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);
00165
00166 cld.reset (new pcl::visualization::PCLVisualizer (argc, argv, "OpenNI Viewer"));
00167
00168 std::string mouseMsg3D ("Mouse coordinates in PCL Visualizer");
00169 std::string keyMsg3D ("Key event for PCL Visualizer");
00170 cld->registerMouseCallback (&mouse_callback, (void*)(&mouseMsg3D));
00171 cld->registerKeyboardCallback(&keyboard_callback, (void*)(&keyMsg3D));
00172 boost::function<void(const pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr&) > f = boost::bind (&EventHelper::cloud_cb, &event_helper, _1);
00173 boost::signals2::connection c1 = interface->registerCallback (f);
00174
00175 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00176 img.reset (new pcl::visualization::ImageViewer ("OpenNI Viewer"));
00177
00178 std::string keyMsg2D ("Key event for image viewer");
00179 std::string mouseMsg2D ("Mouse coordinates in image viewer");
00180 img->registerMouseCallback (&mouse_callback, (void*)(&mouseMsg2D));
00181 img->registerKeyboardCallback(&keyboard_callback, (void*)(&keyMsg2D));
00182 boost::function<void (const boost::shared_ptr<openni_wrapper::Image>&) > image_cb = boost::bind (&EventHelper::image_callback, &event_helper, _1);
00183 boost::signals2::connection image_connection = interface->registerCallback (image_cb);
00184 unsigned char* rgb_data = 0;
00185 unsigned rgb_data_size = 0;
00186 #endif
00187
00188 interface->start ();
00189 bool cld_init = false;
00190
00191 while (!cld->wasStopped ())
00192 {
00193
00194 cld->spinOnce ();
00195 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00196 img->spinOnce ();
00197 #endif
00198 FPS_CALC ("drawing");
00199
00200
00201 if (g_cloud && cld_mutex.try_lock ())
00202 {
00203 if (!cld_init)
00204 {
00205 cld->getRenderWindow ()->SetSize (g_cloud->width, g_cloud->height);
00206 cld->getRenderWindow ()->SetPosition (g_cloud->width, 0);
00207 cld_init = !cld_init;
00208 }
00209
00210 pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> handler (g_cloud);
00211 if (!cld->updatePointCloud (g_cloud, handler, "OpenNICloud"))
00212 {
00213 cld->addPointCloud (g_cloud, handler, "OpenNICloud");
00214 cld->resetCameraViewpoint ("OpenNICloud");
00215 }
00216 cld_mutex.unlock ();
00217 }
00218
00219 #if !((VTK_MAJOR_VERSION == 5)&&(VTK_MINOR_VERSION <= 4))
00220
00221 if (g_image && img_mutex.try_lock ())
00222 {
00223 if (g_image->getEncoding() == openni_wrapper::Image::RGB)
00224 img->showRGBImage (g_image->getMetaData ().Data (),
00225 g_image->getWidth (), g_image->getHeight ());
00226 else
00227 {
00228 if (rgb_data_size < g_image->getWidth () * g_image->getHeight ())
00229 {
00230 rgb_data_size = g_image->getWidth () * g_image->getHeight ();
00231 rgb_data = new unsigned char [rgb_data_size * 3];
00232 }
00233 g_image->fillRGB (g_image->getWidth (), g_image->getHeight (), rgb_data);
00234 img->showRGBImage (rgb_data, g_image->getWidth (), g_image->getHeight ());
00235 }
00236 img_mutex.unlock ();
00237 }
00238 #endif
00239 boost::this_thread::sleep (boost::posix_time::microseconds (100));
00240 }
00241
00242 interface->stop ();
00243 }
00244