00001 /* 00002 * Copyright (c) 2011, Nico Blodow <blodow@cs.tum.edu> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Intelligent Autonomous Systems Group/ 00014 * Technische Universitaet Muenchen nor the names of its contributors 00015 * may be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 #ifndef REALTIME_URDF_FILTER_URDF_FILTER_H_ 00032 #define REALTIME_URDF_FILTER_URDF_FILTER_H_ 00033 00034 #include <ros/node_handle.h> 00035 #include <sensor_msgs/Image.h> 00036 #include <sensor_msgs/CameraInfo.h> 00037 #include <tf/transform_listener.h> 00038 00039 #include <opencv2/opencv.hpp> 00040 #include <image_transport/image_transport.h> 00041 00042 #include "realtime_urdf_filter/FrameBufferObject.h" 00043 #include "realtime_urdf_filter/shader_wrapper.h" 00044 #include "realtime_urdf_filter/urdf_renderer.h" 00045 00046 #include <GL/freeglut.h> 00047 00048 namespace realtime_urdf_filter 00049 { 00050 00051 class RealtimeURDFFilter 00052 { 00053 public: 00054 // constructor. sets up ros and reads in parameters 00055 RealtimeURDFFilter (ros::NodeHandle &nh, int argc, char **argv); 00056 00057 ~RealtimeURDFFilter (); 00058 00059 // loads URDF models 00060 void loadModels (); 00061 00062 // helper function to get current time 00063 double getTime (); 00064 00065 // callback function that gets ROS images and does everything 00066 void filter_callback 00067 (const sensor_msgs::ImageConstPtr& ros_depth_image, 00068 const sensor_msgs::CameraInfo::ConstPtr& camera_info); 00069 00070 // does virtual rendering and filtering based on depth buffer and opengl proj. matrix 00071 void filter ( 00072 unsigned char* buffer, double* glTf, int width, int height); 00073 00074 // copy cv::Mat1f to char buffer 00075 unsigned char* bufferFromDepthImage (cv::Mat1f depth_image); 00076 00077 // copy char buffer to OpenGL texture 00078 void textureBufferFromDepthBuffer (unsigned char* buffer, int size_in_bytes); 00079 00080 // set up OpenGL stuff 00081 void initGL (); 00082 00083 // set up FBO 00084 void initFrameBufferObject (); 00085 00086 // compute Projection matrix from CameraInfo message 00087 void getProjectionMatrix (const sensor_msgs::CameraInfo::ConstPtr& current_caminfo, double* glTf); 00088 00089 void render (const double* camera_projection_matrix); 00090 00091 GLfloat* getMaskedDepth() 00092 {return masked_depth_;} 00093 00094 public: 00095 // ROS objects 00096 ros::NodeHandle nh_; 00097 tf::TransformListener tf_; 00098 image_transport::ImageTransport image_transport_; 00099 image_transport::CameraSubscriber depth_sub_; 00100 image_transport::CameraPublisher depth_pub_; 00101 image_transport::CameraPublisher depth_pub_raw_; 00102 image_transport::CameraPublisher mask_pub_; 00103 00104 // rendering objects 00105 FramebufferObject *fbo_; 00106 bool fbo_initialized_; 00107 GLuint depth_image_pbo_; 00108 GLuint depth_texture_; 00109 00110 // vector of renderables 00111 std::vector<URDFRenderer*> renderers_; 00112 00113 // parameters from launch file 00114 tf::Vector3 camera_offset_t_; 00115 tf::Quaternion camera_offset_q_; 00116 std::string cam_frame_; 00117 std::string fixed_frame_; 00118 bool show_gui_; 00119 00120 // do we have subscribers for the mask image? 00121 bool need_mask_; 00122 00123 // image size 00124 GLint width_; 00125 GLint height_; 00126 00127 // Camera parameters 00128 double camera_tx_; 00129 double camera_ty_; 00130 00131 // OpenGL virtual camera setup 00132 double far_plane_; 00133 double near_plane_; 00134 double depth_distance_threshold_; 00135 double filter_replace_value_; 00136 00137 // neccesary for glutInit().. 00138 int argc_; 00139 char **argv_; 00140 00141 // output from rendering 00142 GLfloat* masked_depth_; 00143 GLubyte* mask_; 00144 }; 00145 00146 } // end namespace 00147 #endif