Drake Designer
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
ddBotImageQueue.h
Go to the documentation of this file.
1 #ifndef __ddBotImageQueue_h
2 #define __ddBotImageQueue_h
3 
4 #include <QObject>
5 #include <ddMacros.h>
6 
7 #include "ddLCMThread.h"
8 #include "ddLCMSubscriber.h"
9 #include "ddAppConfigure.h"
10 
11 
12 #include <string>
13 #include <sstream>
14 
15 #include <Eigen/Geometry>
16 #include <lcm/lcm-cpp.hpp>
17 #include <lcmtypes/bot_core/image_t.hpp>
18 #include <lcmtypes/bot_core/images_t.hpp>
19 
20 #include <bot_core/camtrans.h>
21 #include <bot_param/param_client.h>
22 #include <bot_param/param_util.h>
23 #include <bot_frames/bot_frames.h>
24 
25 #include <image_utils/jpeg.h>
26 #include <pcl/filters/passthrough.h>
27 
28 #include <vtkSmartPointer.h>
29 #include <vtkPNGWriter.h>
30 #include <vtkImageData.h>
31 #include <vtkPolyData.h>
32 #include <vtkPointData.h>
33 #include <vtkUnsignedCharArray.h>
34 #include <vtkFloatArray.h>
35 #include <vtkTransform.h>
36 #include <vtkMatrix4x4.h>
37 
38 class DD_APP_EXPORT ddBotImageQueue : public QObject
39  {
40  Q_OBJECT
41 
42 public:
43 
44  class CameraData
45  {
46  public:
47 
50  std::string mName;
51  std::string mCoordFrame;
52  BotCamTrans* mCamTrans;
53  bot_core::image_t mImageMessage;
54  Eigen::Isometry3d mLocalToCamera;
55  Eigen::Isometry3d mBodyToCamera;
56  std::vector<uint8_t> mImageBuffer;
57  QMutex mMutex;
58 
60  {
61  mHasCalibration = false;
62  mZlibCompression = false;
63  mImageMessage.width = 0;
64  mImageMessage.height = 0;
65  mImageMessage.utime = 0;
66  }
67 
69  {
70  if (mCamTrans) bot_camtrans_destroy(mCamTrans);
71  }
72  };
73 
74  ddBotImageQueue(QObject* parent=NULL);
75 
76  virtual ~ddBotImageQueue();
77 
78  // Adds an lcm subscriber for the given channel if one does not already exit.
79  // Assumes the camera name is the same as the given channel name. Camera name
80  // is used to lookup camera parameters from botparam. Not all image channels
81  // have named cameras in botparam.
82  bool addCameraStream(const QString& channel);
83 
84  // Adds an lcm subscriber for the given channel if one does not already exist.
85  // Looks for camera parameters in botparam using the given cameraName. Not
86  // all image channels have named cameras in botparam. The imageType, if >= 0,
87  // is used to extract image_t message from images_t.
88  bool addCameraStream(const QString& channel, const QString& cameraName, int imageType);
89 
90  void init(ddLCMThread* lcmThread, const QString& botConfigFile);
91 
92  qint64 getImage(const QString& cameraName, vtkImageData* image);
93  qint64 getCurrentImageTime(const QString& cameraName);
94 
95  // Returns four xyz vectors as a 12 element list. The vectors are rays
96  // representing the top left, top right, bottom right, and bottom left
97  // edges of the camera frustum.
98  QList<double> getCameraFrustumBounds(const QString& cameraName);
99 
100  QList<double> unprojectPixel(const QString& cameraName, int px, int py);
101 
102  void colorizePoints(const QString& cameraName, vtkPolyData* polyData);
103 
104  void computeTextureCoords(const QString& cameraName, vtkPolyData* polyData);
105 
106  void publishRGBDImagesMessage(const QString& channel, vtkImageData* colorImage, vtkImageData* depthImage, qint64 utime);
107  void publishRGBImageMessage(const QString& channel, vtkImageData* image, qint64 utime);
108 
109  // Computes a point cloud with rgb and copies it into the given polyData.
110  // The channel argument names an lcm channel where an images message are received
111  // that contains disparity and color images.
112  // decimation: power of 2 to reduce the data by (1,2,4,8...)
113  // removeSize: remove disconnected components smaller than this size (in pixels), set=0 to skip
114  // rangeThreshold: remove points which are further than rangeThreshold (in meters), set -1 to skip
115  void getPointCloudFromImages(const QString& channel, vtkPolyData* polyData, int decimation, int removeSize, float rangeThreshold);
116 
117  // Project the points of the given polydata into image space. The points must
118  // already be in the camera coordinate system. The points will be written to
119  // in place.
120  int projectPoints(const QString& cameraName, vtkPolyData* polyData);
121 
122  void getBodyToCameraTransform(const QString& cameraName, vtkTransform* transform);
123 
124  // Initializes the given transform to the camera projection matrix.
125  // This will transform points from the camera coordinate system to image space.
126  void getCameraProjectionTransform(const QString& cameraName, vtkTransform* transform);
127 
128  int getTransform(const QString& fromFrame, const QString& toFrame, qint64 utime, vtkTransform* transform);
129  int getTransform(const QString& fromFrame, const QString& toFrame, vtkTransform* transform);
130 
131  QStringList getBotFrameNames() const;
132  QStringList getCameraNames() const;
133 
134 protected slots:
135 
136  void onImagesMessage(const QByteArray& data, const QString& channel);
137  void onImageMessage(const QByteArray& data, const QString& channel);
138 
139 protected:
140 
141  CameraData* getCameraData(const QString& cameraName);
142  bool initCameraData(const QString& cameraName, CameraData* cameraData);
143 
144  vtkSmartPointer<vtkImageData> toVtkImage(CameraData* cameraData);
145 
146  void colorizePoints(vtkPolyData* polyData, CameraData* cameraData);
147 
148  void computeTextureCoords(vtkPolyData* polyData, CameraData* cameraData);
149 
150  QList<double> getCameraFrustumBounds(CameraData* cameraData);
151 
152  int getTransform(std::string from_frame, std::string to_frame,
153  Eigen::Isometry3d& mat, qint64 utime);
154 
155  BotParam* mBotParam;
156  BotFrames* mBotFrames;
157 
159  QMap<QString, QMap<int, QString> > mChannelMap;
160  QMap<QString, bot_core::images_t> mImagesMessageMap;
161  QMap<QString, ddLCMSubscriber*> mSubscribers;
162  QMap<QString, CameraData*> mCameraData;
163 };
164 
165 #endif
QMap< QString, ddLCMSubscriber * > mSubscribers
QMap< QString, QMap< int, QString > > mChannelMap
ddLCMThread * mLCM
QMap< QString, CameraData * > mCameraData
Eigen::Isometry3d mBodyToCamera
QMap< QString, bot_core::images_t > mImagesMessageMap
BotFrames * mBotFrames
std::vector< uint8_t > mImageBuffer
Eigen::Isometry3d mLocalToCamera
bot_core::image_t mImageMessage
BotParam * mBotParam
#define DD_APP_EXPORT