geolib2
DepthCamera.h
Go to the documentation of this file.
1 #ifndef GEOLIB_DEPTHCAMERA_H_
2 #define GEOLIB_DEPTHCAMERA_H_
3 
4 #include <opencv2/core/core.hpp>
5 
6 #include "geolib/Ray.h"
7 #include "geolib/math_types.h"
8 
9 #include <image_geometry/pinhole_camera_model.h>
10 #include <sensor_msgs/CameraInfo.h>
11 
12 #include <vector>
13 
14 namespace geo {
15 
20 
26 
28  int min_x, min_y;
29  int max_x, max_y;
30 };
31 
34 
35 class Mesh;
36 class DepthCamera;
37 
42 
43  friend class DepthCamera;
44 
45 public:
46 
48 
54  void setMesh(const geo::Mesh& mesh, const geo::Pose3D& pose = geo::Pose3D::identity()) {
55  mesh_ = &mesh;
56  pose_ = pose;
57  }
58 
60 
61 protected:
62 
66  const geo::Mesh* mesh_;
67 
72 
77 
78 };
79 
80 
81 class RenderResult {
82 
83  friend class DepthCamera;
84 
85 public:
86 
87  RenderResult(int width, int height) : stop_(false), width_(width), height_(height) {
88  }
89 
90  virtual ~RenderResult() {}
91 
92  virtual void renderPixel(int x, int y, float depth, int i_triangle) = 0;
93 
94  void stop() { stop_ = true; }
95 
96  int getWidth() const { return width_; }
97 
98  int getHeight() const { return height_; }
99 
100 private:
101 
102  bool stop_;
103 
105 
106 };
107 
108 
110 
111  friend class DepthCamera;
112 
113 public:
114 
115  DefaultRenderResult(cv::Mat& image, void* pointer, PointerMap& pointer_map, TriangleMap& triangle_map)
116  : geo::RenderResult(image.cols, image.rows), image_(image), pointer_(pointer), pointer_map_(pointer_map), triangle_map_(triangle_map)
117  {
118 
119  // reserve pointer map
120  if (pointer_) {
121  if (pointer_map_.empty() || (int)pointer_map_.size() != image.cols || (int)pointer_map_[0].size() != image.rows) {
122  pointer_map_.resize(image.cols, std::vector<void*>(image.rows, (void*)NULL));
123  }
124  }
125 
126  // reserve triangle map
127  if (triangle_map_.empty() || (int)triangle_map_.size() != image.cols || (int)triangle_map_[0].size() != image.rows) {
129  }
130  }
131 
132  virtual ~DefaultRenderResult() {}
133 
134  const cv::Mat& getDepthImage() const { return image_; }
135  const PointerMap& getPointerMap() const { return pointer_map_; }
136  const TriangleMap& getTriangleMap() const { return triangle_map_; }
137 
138  virtual void renderPixel(int x, int y, float depth, int i_triangle);
139 
140 protected:
141 
142  cv::Mat& image_;
143  void* pointer_;
146 
147 };
148 
159 class DepthCamera {
160 
161 public:
162 
163  DepthCamera();
164 
165  DepthCamera(uint width, uint height, double fx, double fy, double cx, double cy, double tx, double ty);
166 
167  DepthCamera(const image_geometry::PinholeCameraModel& cam_model);
168 
169  DepthCamera(const sensor_msgs::CameraInfo& cam_info);
170 
171  virtual ~DepthCamera();
172 
177  void initFromCamModel(const image_geometry::PinholeCameraModel& cam_model);
178 
179  void render(const RenderOptions& opt, RenderResult& res) const;
180 
191  RasterizeResult rasterize(const Shape& shape, const Pose3D& pose, cv::Mat& image,
192  PointerMap& pointer_map = EMPTY_POINTER_MAP,
193  void* pointer = 0, TriangleMap& triangle_map = EMPTY_TRIANGLE_MAP) const;
194 
195  RasterizeResult rasterize(const Shape& shape, const Pose3D& cam_pose, const Pose3D& obj_pose, cv::Mat& image,
196  PointerMap& pointer_map = EMPTY_POINTER_MAP,
197  void* pointer = 0, TriangleMap& triangle_map = EMPTY_TRIANGLE_MAP) const;
198 
199  template<typename Tin=double, typename Tout=double>
200  inline cv::Point_<Tout> project3Dto2D(const geo::Vec3T<Tin>& p) const {
201  return cv::Point_<Tout>((fx() * p.x + Tx()) / -p.z + cx(), (fy() * -p.y + Ty()) / -p.z + cy());
202  }
203 
204  inline double project2Dto3DX(int x) const { return (x - (cx() + Tx())) / fx(); }
205 
206  inline double project2Dto3DY(int y) const { return -(y - (cy() + Ty())) / fy(); }
207 
214  template<typename T=double>
215  inline geo::Vec3T<T> project2Dto3D(int x, int y) const {
216  return geo::Vec3T<T>(project2Dto3DX(x), project2Dto3DY(y), -1.0);
217  }
218 
219  inline double fx() const { return cam_model_.fx(); }
220  inline double getFocalLengthX() const { return fx(); }
221 
222  inline double fy() const { return cam_model_.fy(); }
223  inline double getFocalLengthY() const { return fy(); }
224 
225  inline double cx() const { return cam_model_.cx(); }
226  inline double getOpticalCenterX() const { return cx(); }
227 
228  inline double cy() const { return cam_model_.cy(); }
229  inline double getOpticalCenterY() const { return cam_model_.cy(); }
230 
231  inline double Tx() const { return cam_model_.Tx(); }
232  inline double getOpticalTranslationX() const { return Tx(); }
233 
234  inline double Ty() const { return cam_model_.Ty(); }
235  inline double getOpticalTranslationY() const { return Ty(); }
236 
237  inline int height() const { return cam_model_.fullResolution().height; }
238 
239  inline int width() const { return cam_model_.fullResolution().width; }
240 
245  inline bool initialized() const { return cam_model_.initialized(); }
246 
247 protected:
248 
249  static constexpr const double near_clip_z_ = -0.1;
250 
251  image_geometry::PinholeCameraModel cam_model_;
252 
253  template<typename Tin=double, typename Tout=double>
254  void drawTriangle(const geo::Vec3T<Tin>& p1, const geo::Vec3T<Tin>& p2, const geo::Vec3T<Tin>& p3,
255  const RenderOptions& opt, RenderResult& res, uint i_triangle) const;
256 
257  template<typename T=double>
258  void drawTriangle2D(const geo::Vec3T<T>& p1, const geo::Vec3T<T>& p2, const geo::Vec3T<T>& p3,
259  const RenderOptions& opt, RenderResult& res, uint i_triangle) const;
260 
261  void drawTrianglePart(int y_start, int y_end,
262  float x_start, float x_start_delta, float x_end, float x_end_delta,
263  float d_start, float d_start_delta, float d_end, float d_end_delta,
264  const RenderOptions& opt, RenderResult& res, uint i_triangle) const;
265 
266  template<typename T=double>
267  void sort(const geo::Vec3T<T>*& p_min, const geo::Vec3T<T>*& p_mid, const geo::Vec3T<T>*& p_max, uchar dim) const;
268 
269 };
270 
271 }
272 
273 #endif
geo::DepthCamera::near_clip_z_
static constexpr const double near_clip_z_
Definition: DepthCamera.h:249
math_types.h
geo::EMPTY_POINTER_MAP
static PointerMap EMPTY_POINTER_MAP
Definition: DepthCamera.h:32
geo::RenderResult::getWidth
int getWidth() const
Definition: DepthCamera.h:96
geo::RasterizeResult::min_x
int min_x
Definition: DepthCamera.h:28
geo::DepthCamera::getOpticalCenterY
double getOpticalCenterY() const
Definition: DepthCamera.h:229
std::vector::resize
T resize(T... args)
geo::RenderResult::stop
void stop()
Definition: DepthCamera.h:94
geo::RenderOptions::setMesh
void setMesh(const geo::Mesh &mesh, const geo::Pose3D &pose=geo::Pose3D::identity())
setMesh: set mesh to be rendered
Definition: DepthCamera.h:54
geo
Definition: Box.h:6
geo::RenderResult::~RenderResult
virtual ~RenderResult()
Definition: DepthCamera.h:90
geo::DefaultRenderResult::renderPixel
virtual void renderPixel(int x, int y, float depth, int i_triangle)
Definition: DepthCamera.cpp:11
geo::DepthCamera::drawTriangle
void drawTriangle(const geo::Vec3T< Tin > &p1, const geo::Vec3T< Tin > &p2, const geo::Vec3T< Tin > &p3, const RenderOptions &opt, RenderResult &res, uint i_triangle) const
Definition: DepthCamera.cpp:254
geo::DefaultRenderResult::pointer_
void * pointer_
Definition: DepthCamera.h:143
geo::RenderOptions::pose_
Pose3D pose_
pose_ pose of the mesh with respect to the virtual camera
Definition: DepthCamera.h:71
geo::Transform3T::identity
static Transform3T identity()
Definition: math_types.h:778
geo::DepthCamera::~DepthCamera
virtual ~DepthCamera()
Definition: DepthCamera.cpp:93
vector
std::vector::size
T size(T... args)
geo::RenderOptions::setBackFaceCulling
void setBackFaceCulling(bool b)
Definition: DepthCamera.h:59
geo::DepthCamera::getOpticalTranslationX
double getOpticalTranslationX() const
Definition: DepthCamera.h:232
geo::Vec3T
Definition: math_types.h:13
geo::DepthCamera::rasterize
RasterizeResult rasterize(const Shape &shape, const Pose3D &pose, cv::Mat &image, PointerMap &pointer_map=EMPTY_POINTER_MAP, void *pointer=0, TriangleMap &triangle_map=EMPTY_TRIANGLE_MAP) const
rasterize: render a 3D shape onto a 2D image
Definition: DepthCamera.cpp:114
geo::DepthCamera::render
void render(const RenderOptions &opt, RenderResult &res) const
Definition: DepthCamera.cpp:129
geo::Transform3T
Definition: math_types.h:19
geo::DepthCamera::initFromCamModel
void initFromCamModel(const image_geometry::PinholeCameraModel &cam_model)
Set camera parameters from pinhole camera model.
Definition: DepthCamera.cpp:96
geo::RenderOptions::back_face_culling_
bool back_face_culling_
back_face_culling_ flag to optimise rendering mesh triangles facing away from the camera.
Definition: DepthCamera.h:76
geo::DepthCamera::project2Dto3DY
double project2Dto3DY(int y) const
Definition: DepthCamera.h:206
geo::EMPTY_TRIANGLE_MAP
static TriangleMap EMPTY_TRIANGLE_MAP
Definition: DepthCamera.h:33
geo::DepthCamera::drawTriangle2D
void drawTriangle2D(const geo::Vec3T< T > &p1, const geo::Vec3T< T > &p2, const geo::Vec3T< T > &p3, const RenderOptions &opt, RenderResult &res, uint i_triangle) const
Definition: DepthCamera.cpp:269
geo::TriangleMap
std::vector< std::vector< int > > TriangleMap
TriangleMap maps pixels in a depth image to an index in the list of triangles in the mesh....
Definition: DepthCamera.h:25
geo::RasterizeResult::max_y
int max_y
Definition: DepthCamera.h:29
geo::RenderOptions::mesh_
const geo::Mesh * mesh_
mesh_ mesh to be rendered
Definition: DepthCamera.h:66
geo::DepthCamera::DepthCamera
DepthCamera()
Definition: DepthCamera.cpp:26
geo::Vec3T::y
T y
Definition: math_types.h:217
geo::DepthCamera::Ty
double Ty() const
Definition: DepthCamera.h:234
geo::RenderResult::getHeight
int getHeight() const
Definition: DepthCamera.h:98
geo::RenderResult
Definition: DepthCamera.h:81
geo::DefaultRenderResult::DefaultRenderResult
DefaultRenderResult(cv::Mat &image, void *pointer, PointerMap &pointer_map, TriangleMap &triangle_map)
Definition: DepthCamera.h:115
geo::RenderOptions
Definition: DepthCamera.h:41
geo::DefaultRenderResult::pointer_map_
PointerMap & pointer_map_
Definition: DepthCamera.h:144
geo::DefaultRenderResult::getTriangleMap
const TriangleMap & getTriangleMap() const
Definition: DepthCamera.h:136
geo::DepthCamera::sort
void sort(const geo::Vec3T< T > *&p_min, const geo::Vec3T< T > *&p_mid, const geo::Vec3T< T > *&p_max, uchar dim) const
Definition: DepthCamera.cpp:368
geo::RasterizeResult::max_x
int max_x
Definition: DepthCamera.h:29
geo::DefaultRenderResult::~DefaultRenderResult
virtual ~DefaultRenderResult()
Definition: DepthCamera.h:132
geo::DefaultRenderResult
Definition: DepthCamera.h:109
geo::RasterizeResult
Definition: DepthCamera.h:27
geo::DepthCamera::fy
double fy() const
Definition: DepthCamera.h:222
geo::DepthCamera::cam_model_
image_geometry::PinholeCameraModel cam_model_
Definition: DepthCamera.h:251
geo::DepthCamera::getFocalLengthX
double getFocalLengthX() const
Definition: DepthCamera.h:220
geo::DepthCamera::Tx
double Tx() const
Definition: DepthCamera.h:231
geo::DepthCamera::project2Dto3D
geo::Vec3T< T > project2Dto3D(int x, int y) const
Definition: DepthCamera.h:215
geo::DefaultRenderResult::triangle_map_
TriangleMap & triangle_map_
Definition: DepthCamera.h:145
geo::DepthCamera::getOpticalTranslationY
double getOpticalTranslationY() const
Definition: DepthCamera.h:235
b
void b()
geo::DepthCamera::drawTrianglePart
void drawTrianglePart(int y_start, int y_end, float x_start, float x_start_delta, float x_end, float x_end_delta, float d_start, float d_start_delta, float d_end, float d_end_delta, const RenderOptions &opt, RenderResult &res, uint i_triangle) const
Definition: DepthCamera.cpp:322
geo::RenderOptions::RenderOptions
RenderOptions()
Definition: DepthCamera.h:47
geo::DepthCamera::initialized
bool initialized() const
Indicates whether the camera parameters are set. Using the camera when not initialized is useless.
Definition: DepthCamera.h:245
geo::RenderResult::width_
int width_
Definition: DepthCamera.h:104
geo::DepthCamera::getFocalLengthY
double getFocalLengthY() const
Definition: DepthCamera.h:223
geo::DepthCamera::project3Dto2D
cv::Point_< Tout > project3Dto2D(const geo::Vec3T< Tin > &p) const
Definition: DepthCamera.h:200
geo::Vec3T::z
T z
Definition: math_types.h:217
std::vector::empty
T empty(T... args)
geo::RasterizeResult::min_y
int min_y
Definition: DepthCamera.h:28
geo::DepthCamera
Definition: DepthCamera.h:159
geo::DepthCamera::cx
double cx() const
Definition: DepthCamera.h:225
geo::DefaultRenderResult::getPointerMap
const PointerMap & getPointerMap() const
Definition: DepthCamera.h:135
geo::PointerMap
std::vector< std::vector< void * > > PointerMap
PointerMap maps pixels in a depth image to an identifier.
Definition: DepthCamera.h:19
geo::RenderResult::height_
int height_
Definition: DepthCamera.h:104
geo::DepthCamera::project2Dto3DX
double project2Dto3DX(int x) const
Definition: DepthCamera.h:204
geo::DepthCamera::fx
double fx() const
Definition: DepthCamera.h:219
geo::RenderResult::RenderResult
RenderResult(int width, int height)
Definition: DepthCamera.h:87
geo::DepthCamera::width
int width() const
Definition: DepthCamera.h:239
geo::DepthCamera::height
int height() const
Definition: DepthCamera.h:237
geo::Mesh
Definition: Mesh.h:25
geo::DefaultRenderResult::getDepthImage
const cv::Mat & getDepthImage() const
Definition: DepthCamera.h:134
geo::RenderResult::stop_
bool stop_
Definition: DepthCamera.h:102
geo::Vec3T::x
T x
Definition: math_types.h:217
geo::DepthCamera::getOpticalCenterX
double getOpticalCenterX() const
Definition: DepthCamera.h:226
Ray.h
geo::Shape
A geometric description of a shape.
Definition: Shape.h:19
geo::DepthCamera::cy
double cy() const
Definition: DepthCamera.h:228
geo::DefaultRenderResult::image_
cv::Mat & image_
Definition: DepthCamera.h:142
geo::RenderResult::renderPixel
virtual void renderPixel(int x, int y, float depth, int i_triangle)=0