Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
texture_mapping.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: texture_mapping.h 6064 2012-06-29 17:57:23Z raph $
37  *
38  */
39 
40 #ifndef PCL_SURFACE_TEXTURE_MAPPING_H_
41 #define PCL_SURFACE_TEXTURE_MAPPING_H_
42 
44 #include <pcl/common/transforms.h>
45 #include <pcl/TextureMesh.h>
46 
47 
48 namespace pcl
49 {
50  namespace texture_mapping
51  {
52 
54  struct Camera
55  {
56  Camera () : pose (), focal_length (), height (), width (), texture_file () {}
57  Eigen::Affine3f pose;
58  double focal_length;
59  double height;
60  double width;
61  std::string texture_file;
62 
63  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
64  };
65 
68  struct UvIndex
69  {
70  UvIndex () : idx_cloud (), idx_face () {}
71  int idx_cloud; // Index of the PointXYZ in the camera's cloud
72  int idx_face; // Face corresponding to that projection
73  };
74 
75  typedef std::vector<Camera, Eigen::aligned_allocator<Camera> > CameraVector;
76 
77  }
78 
83  template<typename PointInT>
85  {
86  public:
87 
88  typedef boost::shared_ptr< PointInT > Ptr;
89  typedef boost::shared_ptr< const PointInT > ConstPtr;
90 
92  typedef typename PointCloud::Ptr PointCloudPtr;
94 
96  typedef typename Octree::Ptr OctreePtr;
97  typedef typename Octree::ConstPtr OctreeConstPtr;
98 
101 
104  f_ (), vector_field_ (), tex_files_ (), tex_material_ ()
105  {
106  }
107 
110  {
111  }
112 
116  inline void
117  setF (float f)
118  {
119  f_ = f;
120  }
121 
127  inline void
128  setVectorField (float x, float y, float z)
129  {
130  vector_field_ = Eigen::Vector3f (x, y, z);
131  // normalize vector field
132  vector_field_ = vector_field_ / std::sqrt (vector_field_.dot (vector_field_));
133  }
134 
138  inline void
139  setTextureFiles (std::vector<std::string> tex_files)
140  {
141  tex_files_ = tex_files;
142  }
143 
147  inline void
149  {
150  tex_material_ = tex_material;
151  }
152 
156  void
157  mapTexture2Mesh (pcl::TextureMesh &tex_mesh);
158 
162  void
164 
171  void
174 
181  inline bool
182  getPointUVCoordinates (const pcl::PointXYZ &pt, const Camera &cam, Eigen::Vector2f &UV_coordinates)
183  {
184  // if the point is in front of the camera
185  if (pt.z > 0)
186  {
187  // compute image center and dimension
188  double sizeX = cam.width;
189  double sizeY = cam.height;
190  double cx = (sizeX) / 2.0;
191  double cy = (sizeY) / 2.0;
192 
193  double focal_x = cam.focal_length;
194  double focal_y = cam.focal_length;
195 
196  // project point on image frame
197  UV_coordinates[0] = static_cast<float> ((focal_x * (pt.x / pt.z) + cx) / sizeX); //horizontal
198  UV_coordinates[1] = 1.0f - static_cast<float> (((focal_y * (pt.y / pt.z) + cy) / sizeY)); //vertical
199 
200  // point is visible!
201  if (UV_coordinates[0] >= 0.0 && UV_coordinates[0] <= 1.0 && UV_coordinates[1] >= 0.0 && UV_coordinates[1]
202  <= 1.0)
203  return (true);
204  }
205 
206  // point is NOT visible by the camera
207  UV_coordinates[0] = -1.0;
208  UV_coordinates[1] = -1.0;
209  return (false);
210  }
211 
217  inline bool
218  isPointOccluded (const pcl::PointXYZ &pt, const OctreePtr octree);
219 
227  void
228  removeOccludedPoints (const PointCloudPtr &input_cloud,
229  PointCloudPtr &filtered_cloud, const double octree_voxel_size,
230  std::vector<int> &visible_indices, std::vector<int> &occluded_indices);
231 
237  void
238  removeOccludedPoints (const pcl::TextureMesh &tex_mesh, pcl::TextureMesh &cleaned_mesh, const double octree_voxel_size);
239 
240 
246  void
247  removeOccludedPoints (const pcl::TextureMesh &tex_mesh, PointCloudPtr &filtered_cloud, const double octree_voxel_size);
248 
249 
258  int
260  pcl::TextureMesh &sorted_mesh,
261  const pcl::texture_mapping::CameraVector &cameras,
262  const double octree_voxel_size, PointCloud &visible_pts);
263 
274  void
275  showOcclusions (const PointCloudPtr &input_cloud,
277  const double octree_voxel_size,
278  const bool show_nb_occlusions = true,
279  const int max_occlusions = 4);
280 
291  void
292  showOcclusions (pcl::TextureMesh &tex_mesh,
294  double octree_voxel_size,
295  bool show_nb_occlusions = true,
296  int max_occlusions = 4);
297 
304  void
306  const pcl::texture_mapping::CameraVector &cameras);
307 
308  protected:
310  float f_;
311 
313  Eigen::Vector3f vector_field_;
314 
316  std::vector<std::string> tex_files_;
317 
319  TexMaterial tex_material_;
320 
326  std::vector<Eigen::Vector2f>
327  mapTexture2Face (const Eigen::Vector3f &p1, const Eigen::Vector3f &p2, const Eigen::Vector3f &p3);
328 
337  inline void
338  getTriangleCircumcenterAndSize (const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, pcl::PointXY &circomcenter, double &radius);
339 
346  inline bool
347  getPointUVCoordinates (const pcl::PointXYZ &pt, const Camera &cam, pcl::PointXY &UV_coordinates);
348 
358  inline bool
359  isFaceProjected (const Camera &camera,
360  const pcl::PointXYZ &p1, const pcl::PointXYZ &p2, const pcl::PointXYZ &p3,
361  pcl::PointXY &proj1, pcl::PointXY &proj2, pcl::PointXY &proj3);
362 
370  inline bool
371  checkPointInsideTriangle (const pcl::PointXY &p1, const pcl::PointXY &p2, const pcl::PointXY &p3, const pcl::PointXY &pt);
372 
374  std::string
375  getClassName () const
376  {
377  return ("TextureMapping");
378  }
379 
380  public:
381  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
382  };
383 }
384 
385 #endif /* TEXTURE_MAPPING_H_ */
386