38 #ifndef PCL_SURFACE_IMPL_TEXTURE_MAPPING_HPP_
39 #define PCL_SURFACE_IMPL_TEXTURE_MAPPING_HPP_
42 #include <pcl/surface/texture_mapping.h>
45 template<
typename Po
intInT> std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> >
47 const Eigen::Vector3f &p1,
48 const Eigen::Vector3f &p2,
49 const Eigen::Vector3f &p3)
51 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > tex_coordinates;
53 Eigen::Vector3f p1p2 (p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]);
54 Eigen::Vector3f p1p3 (p3[0] - p1[0], p3[1] - p1[1], p3[2] - p1[2]);
55 Eigen::Vector3f p2p3 (p3[0] - p2[0], p3[1] - p2[1], p3[2] - p2[2]);
58 p1p2 /= std::sqrt (p1p2.dot (p1p2));
59 p1p3 /= std::sqrt (p1p3.dot (p1p3));
60 p2p3 /= std::sqrt (p2p3.dot (p2p3));
63 Eigen::Vector3f f_normal = p1p2.cross (p1p3);
64 f_normal /= std::sqrt (f_normal.dot (f_normal));
67 Eigen::Vector3f f_vector_field = vector_field_ - vector_field_.dot (f_normal) * f_normal;
70 f_vector_field /= std::sqrt (f_vector_field.dot (f_vector_field));
73 Eigen::Vector2f tp1, tp2, tp3;
75 double alpha = std::acos (f_vector_field.dot (p1p2));
78 double e1 = (p2 - p3).norm () / f_;
79 double e2 = (p1 - p3).norm () / f_;
80 double e3 = (p1 - p2).norm () / f_;
86 tp2[0] =
static_cast<float> (e3);
90 double cos_p1 = (e2 * e2 + e3 * e3 - e1 * e1) / (2 * e2 * e3);
91 double sin_p1 = sqrt (1 - (cos_p1 * cos_p1));
93 tp3[0] =
static_cast<float> (cos_p1 * e2);
94 tp3[1] =
static_cast<float> (sin_p1 * e2);
97 Eigen::Vector2f r_tp2, r_tp3;
98 r_tp2[0] =
static_cast<float> (tp2[0] * std::cos (alpha) - tp2[1] * std::sin (alpha));
99 r_tp2[1] =
static_cast<float> (tp2[0] * std::sin (alpha) + tp2[1] * std::cos (alpha));
101 r_tp3[0] =
static_cast<float> (tp3[0] * std::cos (alpha) - tp3[1] * std::sin (alpha));
102 r_tp3[1] =
static_cast<float> (tp3[0] * std::sin (alpha) + tp3[1] * std::cos (alpha));
112 float min_x = tp1[0];
113 float min_y = tp1[1];
136 tex_coordinates.push_back (tp1);
137 tex_coordinates.push_back (tp2);
138 tex_coordinates.push_back (tp3);
139 return (tex_coordinates);
143 template<
typename Po
intInT>
void
148 int point_size =
static_cast<int> (tex_mesh.
cloud.
data.size ()) / nr_points;
153 Eigen::Vector3f facet[3];
156 std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > >texture_map;
158 for (std::size_t m = 0; m < tex_mesh.
tex_polygons.size (); ++m)
161 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > texture_map_tmp;
164 for (std::size_t i = 0; i < tex_mesh.
tex_polygons[m].size (); ++i)
169 for (std::size_t j = 0; j < tex_mesh.
tex_polygons[m][i].vertices.size (); ++j)
172 memcpy (&x, &tex_mesh.
cloud.
data[idx * point_size + tex_mesh.
cloud.
fields[0].offset],
sizeof(
float));
173 memcpy (&y, &tex_mesh.
cloud.
data[idx * point_size + tex_mesh.
cloud.
fields[1].offset],
sizeof(
float));
174 memcpy (&z, &tex_mesh.
cloud.
data[idx * point_size + tex_mesh.
cloud.
fields[2].offset],
sizeof(
float));
181 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > tex_coordinates = mapTexture2Face (facet[0], facet[1], facet[2]);
182 for (
const auto &tex_coordinate : tex_coordinates)
183 texture_map_tmp.push_back (tex_coordinate);
187 std::stringstream tex_name;
188 tex_name <<
"material_" << m;
189 tex_name >> tex_material_.tex_name;
190 tex_material_.tex_file = tex_files_[m];
199 template<
typename Po
intInT>
void
204 int point_size =
static_cast<int> (tex_mesh.
cloud.
data.size ()) / nr_points;
206 float x_lowest = 100000;
208 float y_lowest = 100000;
210 float z_lowest = 100000;
214 for (
int i = 0; i < nr_points; ++i)
216 memcpy (&x_, &tex_mesh.
cloud.
data[i * point_size + tex_mesh.
cloud.
fields[0].offset],
sizeof(
float));
217 memcpy (&y_, &tex_mesh.
cloud.
data[i * point_size + tex_mesh.
cloud.
fields[1].offset],
sizeof(
float));
218 memcpy (&z_, &tex_mesh.
cloud.
data[i * point_size + tex_mesh.
cloud.
fields[2].offset],
sizeof(
float));
237 float x_range = (x_lowest - x_highest) * -1;
238 float x_offset = 0 - x_lowest;
243 float z_range = (z_lowest - z_highest) * -1;
244 float z_offset = 0 - z_lowest;
247 std::vector<std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > >texture_map;
249 for (std::size_t m = 0; m < tex_mesh.
tex_polygons.size (); ++m)
252 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > texture_map_tmp;
255 for (std::size_t i = 0; i < tex_mesh.
tex_polygons[m].size (); ++i)
257 Eigen::Vector2f tmp_VT;
258 for (std::size_t j = 0; j < tex_mesh.
tex_polygons[m][i].vertices.size (); ++j)
260 std::size_t idx = tex_mesh.
tex_polygons[m][i].vertices[j];
261 memcpy (&x_, &tex_mesh.
cloud.
data[idx * point_size + tex_mesh.
cloud.
fields[0].offset],
sizeof(
float));
262 memcpy (&y_, &tex_mesh.
cloud.
data[idx * point_size + tex_mesh.
cloud.
fields[1].offset],
sizeof(
float));
263 memcpy (&z_, &tex_mesh.
cloud.
data[idx * point_size + tex_mesh.
cloud.
fields[2].offset],
sizeof(
float));
266 tmp_VT[0] = (x_ + x_offset) / x_range;
267 tmp_VT[1] = (z_ + z_offset) / z_range;
268 texture_map_tmp.push_back (tmp_VT);
273 std::stringstream tex_name;
274 tex_name <<
"material_" << m;
275 tex_name >> tex_material_.tex_name;
276 tex_material_.tex_file = tex_files_[m];
285 template<
typename Po
intInT>
void
291 PCL_ERROR (
"The mesh should be divided into nbCamera+1 sub-meshes.\n");
292 PCL_ERROR (
"You provided %d cameras and a mesh containing %d sub-meshes.\n", cams.size (), tex_mesh.
tex_polygons.size ());
296 PCL_INFO (
"You provided %d cameras and a mesh containing %d sub-meshes.\n", cams.size (), tex_mesh.
tex_polygons.size ());
304 for (std::size_t m = 0; m < cams.size (); ++m)
307 Camera current_cam = cams[m];
310 Eigen::Affine3f cam_trans = current_cam.
pose;
316 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > texture_map_tmp;
319 for (
const auto &tex_polygon : tex_mesh.
tex_polygons[m])
321 Eigen::Vector2f tmp_VT;
323 for (
const unsigned int &vertex : tex_polygon.vertices)
326 PointInT pt = camera_transformed_cloud->
points[vertex];
329 getPointUVCoordinates (pt, current_cam, tmp_VT);
330 texture_map_tmp.push_back (tmp_VT);
335 std::stringstream tex_name;
336 tex_name <<
"material_" << m;
337 tex_name >> tex_material_.tex_name;
346 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > texture_map_tmp;
347 for (
const auto &tex_polygon : tex_mesh.
tex_polygons[cams.size ()])
348 for (std::size_t j = 0; j < tex_polygon.vertices.size (); ++j)
350 Eigen::Vector2f tmp_VT;
353 texture_map_tmp.push_back (tmp_VT);
359 tex_material_.tex_name =
"material_" + std::to_string(cams.size());
360 tex_material_.tex_file =
"occluded.jpg";
365 template<
typename Po
intInT>
bool
368 Eigen::Vector3f direction;
369 direction (0) = pt.x;
370 direction (1) = pt.y;
371 direction (2) = pt.z;
373 std::vector<int> indices;
376 cloud = octree->getInputCloud();
378 double distance_threshold = octree->getResolution();
381 octree->getIntersectedVoxelIndices(direction, -direction, indices);
383 int nbocc =
static_cast<int> (indices.size ());
384 for (
const int &index : indices)
387 if (pt.z * cloud->points[index].z < 0)
393 if (std::fabs (cloud->points[index].z - pt.z) <= distance_threshold)
404 template<
typename Po
intInT>
void
407 const double octree_voxel_size, std::vector<int> &visible_indices,
408 std::vector<int> &occluded_indices)
411 double maxDeltaZ = octree_voxel_size;
414 Octree octree (octree_voxel_size);
422 visible_indices.clear ();
425 Eigen::Vector3f direction;
426 std::vector<int> indices;
427 for (std::size_t i = 0; i < input_cloud->points.size (); ++i)
429 direction (0) = input_cloud->points[i].x;
430 direction (1) = input_cloud->points[i].y;
431 direction (2) = input_cloud->points[i].z;
436 int nbocc =
static_cast<int> (indices.size ());
437 for (
const int &index : indices)
440 if (input_cloud->points[i].z * input_cloud->points[index].z < 0)
446 if (std::fabs (input_cloud->points[index].z - input_cloud->points[i].z) <= maxDeltaZ)
456 filtered_cloud->points.push_back (input_cloud->points[i]);
457 visible_indices.push_back (
static_cast<int> (i));
461 occluded_indices.push_back (
static_cast<int> (i));
468 template<
typename Po
intInT>
void
472 cleaned_mesh = tex_mesh;
480 std::vector<int> visible, occluded;
481 removeOccludedPoints (cloud, filtered_cloud, octree_voxel_size, visible, occluded);
485 for (std::size_t polygons = 0; polygons < cleaned_mesh.
tex_polygons.size (); ++polygons)
490 for (std::size_t faces = 0; faces < tex_mesh.
tex_polygons[polygons].size (); ++faces)
493 bool faceIsVisible =
true;
494 std::vector<int>::iterator it;
497 for (
const unsigned int &vertex : tex_mesh.
tex_polygons[polygons][faces].vertices)
499 it = find (occluded.begin (), occluded.end (), vertex);
501 if (it == occluded.end ())
510 faceIsVisible =
false;
524 template<
typename Po
intInT>
void
526 const double octree_voxel_size)
533 std::vector<int> visible, occluded;
534 removeOccludedPoints (cloud, filtered_cloud, octree_voxel_size, visible, occluded);
539 template<
typename Po
intInT>
int
546 PCL_ERROR (
"The mesh must contain only 1 sub-mesh!\n");
550 if (cameras.empty ())
552 PCL_ERROR (
"Must provide at least one camera info!\n");
557 sorted_mesh = tex_mesh;
569 for (
const auto &camera : cameras)
572 Eigen::Affine3f cam_trans = camera.pose;
578 std::vector<int> visible, occluded;
579 removeOccludedPoints (transformed_cloud, filtered_cloud, octree_voxel_size, visible, occluded);
580 visible_pts = *filtered_cloud;
584 std::vector<pcl::Vertices> visibleFaces_currentCam;
586 for (std::size_t faces = 0; faces < tex_mesh.
tex_polygons[0].size (); ++faces)
589 bool faceIsVisible =
true;
590 std::vector<int>::iterator it;
593 for (std::size_t current_pt_indice = 0; faceIsVisible && current_pt_indice < tex_mesh.
tex_polygons[0][faces].vertices.size (); ++current_pt_indice)
596 it = find (occluded.begin (), occluded.end (), tex_mesh.
tex_polygons[0][faces].vertices[current_pt_indice]);
598 if (it == occluded.end ())
602 PointInT pt = transformed_cloud->
points[tex_mesh.
tex_polygons[0][faces].vertices[current_pt_indice]];
603 Eigen::Vector2f dummy_UV;
604 if (!getPointUVCoordinates (pt, camera, dummy_UV))
607 faceIsVisible =
false;
612 faceIsVisible =
false;
619 visibleFaces_currentCam.push_back (tex_mesh.
tex_polygons[0][faces]);
626 sorted_mesh.
tex_polygons.push_back (visibleFaces_currentCam);
636 template<
typename Po
intInT>
void
639 const double octree_voxel_size,
const bool show_nb_occlusions,
640 const int max_occlusions)
643 double maxDeltaZ = octree_voxel_size * 2.0;
646 Octree octree (octree_voxel_size);
655 Eigen::Vector3f direction;
657 std::vector<int> indices;
661 std::vector<double> zDist;
662 std::vector<double> ptDist;
664 for (std::size_t i = 0; i < input_cloud->points.size (); ++i)
666 direction (0) = input_cloud->points[i].x;
667 pt.x = input_cloud->points[i].x;
668 direction (1) = input_cloud->points[i].y;
669 pt.y = input_cloud->points[i].y;
670 direction (2) = input_cloud->points[i].z;
671 pt.z = input_cloud->points[i].z;
677 nbocc =
static_cast<int> (indices.size ());
680 for (
const int &index : indices)
683 if (pt.z * input_cloud->points[index].z < 0)
687 else if (std::fabs (input_cloud->points[index].z - pt.z) <= maxDeltaZ)
694 zDist.push_back (std::fabs (input_cloud->points[index].z - pt.z));
699 if (show_nb_occlusions)
700 (nbocc <= max_occlusions) ? (pt.
intensity =
static_cast<float> (nbocc)) : (pt.
intensity =
static_cast<float> (max_occlusions));
704 colored_cloud->
points.push_back (pt);
707 if (zDist.size () >= 2)
709 std::sort (zDist.begin (), zDist.end ());
710 std::sort (ptDist.begin (), ptDist.end ());
715 template<
typename Po
intInT>
void
717 double octree_voxel_size,
bool show_nb_occlusions,
int max_occlusions)
723 showOcclusions (cloud, colored_cloud, octree_voxel_size, show_nb_occlusions, max_occlusions);
727 template<
typename Po
intInT>
void
738 std::vector<pcl::Vertices> faces;
740 for (
int current_cam = 0; current_cam < static_cast<int> (cameras.size ()); ++current_cam)
742 PCL_INFO (
"Processing camera %d of %d.\n", current_cam+1, cameras.size ());
750 std::vector<bool> visibility;
751 visibility.resize (mesh.
tex_polygons[current_cam].size ());
752 std::vector<UvIndex> indexes_uv_to_points;
757 nan_point.
x = std::numeric_limits<float>::quiet_NaN ();
758 nan_point.
y = std::numeric_limits<float>::quiet_NaN ();
764 for (
int idx_face = 0; idx_face < static_cast<int> (mesh.
tex_polygons[current_cam].size ()); ++idx_face)
771 if (isFaceProjected (cameras[current_cam],
782 projections->
points.push_back (uv_coord1);
783 projections->
points.push_back (uv_coord2);
784 projections->
points.push_back (uv_coord3);
792 indexes_uv_to_points.push_back (u1);
793 indexes_uv_to_points.push_back (u2);
794 indexes_uv_to_points.push_back (u3);
797 visibility[idx_face] =
true;
801 projections->
points.push_back (nan_point);
802 projections->
points.push_back (nan_point);
803 projections->
points.push_back (nan_point);
804 indexes_uv_to_points.push_back (u_null);
805 indexes_uv_to_points.push_back (u_null);
806 indexes_uv_to_points.push_back (u_null);
808 visibility[idx_face] =
false;
818 if (visibility.size () - cpt_invisible !=0)
824 std::vector<int> idxNeighbors;
825 std::vector<float> neighborsSquaredDistance;
829 for (
int idx_pcam = 0 ; idx_pcam <= current_cam ; ++idx_pcam)
832 for (
int idx_face = 0; idx_face < static_cast<int> (mesh.
tex_polygons[idx_pcam].size ()); ++idx_face)
835 if (idx_pcam == current_cam && !visibility[idx_face])
848 if (isFaceProjected (cameras[current_cam],
861 getTriangleCircumcscribedCircleCentroid(uv_coord1, uv_coord2, uv_coord3, center, radius);
864 if (kdtree.
radiusSearch (center, radius, idxNeighbors, neighborsSquaredDistance) > 0 )
867 for (
const int &idxNeighbor : idxNeighbors)
869 if (std::max (camera_cloud->
points[mesh.
tex_polygons[idx_pcam][idx_face].vertices[0]].z,
872 < camera_cloud->
points[indexes_uv_to_points[idxNeighbor].idx_cloud].z)
875 if (checkPointInsideTriangle(uv_coord1, uv_coord2, uv_coord3, projections->
points[idxNeighbor]))
878 visibility[indexes_uv_to_points[idxNeighbor].idx_face] =
false;
895 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > dummy_container;
900 std::vector<pcl::Vertices> occluded_faces;
901 occluded_faces.resize (visibility.size ());
902 std::vector<pcl::Vertices> visible_faces;
903 visible_faces.resize (visibility.size ());
905 int cpt_occluded_faces = 0;
906 int cpt_visible_faces = 0;
908 for (std::size_t idx_face = 0 ; idx_face < visibility.size () ; ++idx_face)
910 if (visibility[idx_face])
916 mesh.
tex_coordinates[current_cam][cpt_visible_faces * 3 + 1](0) = projections->
points[idx_face*3 + 1].x;
917 mesh.
tex_coordinates[current_cam][cpt_visible_faces * 3 + 1](1) = projections->
points[idx_face*3 + 1].y;
919 mesh.
tex_coordinates[current_cam][cpt_visible_faces * 3 + 2](0) = projections->
points[idx_face*3 + 2].x;
920 mesh.
tex_coordinates[current_cam][cpt_visible_faces * 3 + 2](1) = projections->
points[idx_face*3 + 2].y;
922 visible_faces[cpt_visible_faces] = mesh.
tex_polygons[current_cam][idx_face];
929 occluded_faces[cpt_occluded_faces] = mesh.
tex_polygons[current_cam][idx_face];
930 cpt_occluded_faces++;
935 occluded_faces.resize (cpt_occluded_faces);
938 visible_faces.resize (cpt_visible_faces);
949 std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > dummy_container;
954 for(std::size_t idx_face = 0 ; idx_face < mesh.
tex_polygons[cameras.size()].size() ; ++idx_face)
956 Eigen::Vector2f UV1, UV2, UV3;
957 UV1(0) = -1.0; UV1(1) = -1.0;
958 UV2(0) = -1.0; UV2(1) = -1.0;
959 UV3(0) = -1.0; UV3(1) = -1.0;
968 template<
typename Po
intInT>
inline void
973 ptB.
x = p2.
x - p1.
x; ptB.
y = p2.
y - p1.
y;
974 ptC.
x = p3.
x - p1.
x; ptC.
y = p3.
y - p1.
y;
976 double D = 2.0*(ptB.
x*ptC.
y - ptB.
y*ptC.
x);
981 circomcenter.
x = p1.
x;
982 circomcenter.
y = p1.
y;
987 double bx2 = ptB.
x * ptB.
x;
988 double by2 = ptB.
y * ptB.
y;
989 double cx2 = ptC.
x * ptC.
x;
990 double cy2 = ptC.
y * ptC.
y;
993 circomcenter.
x =
static_cast<float> (p1.
x + (ptC.
y*(bx2 + by2) - ptB.
y*(cx2 + cy2)) / D);
994 circomcenter.
y =
static_cast<float> (p1.
y + (ptB.
x*(cx2 + cy2) - ptC.
x*(bx2 + by2)) / D);
997 radius = std::sqrt( (circomcenter.
x - p1.
x)*(circomcenter.
x - p1.
x) + (circomcenter.
y - p1.
y)*(circomcenter.
y - p1.
y));
1001 template<
typename Po
intInT>
inline void
1005 circumcenter.
x =
static_cast<float> (p1.
x + p2.
x + p3.
x ) / 3;
1006 circumcenter.
y =
static_cast<float> (p1.
y + p2.
y + p3.
y ) / 3;
1007 double r1 = (circumcenter.
x - p1.
x) * (circumcenter.
x - p1.
x) + (circumcenter.
y - p1.
y) * (circumcenter.
y - p1.
y) ;
1008 double r2 = (circumcenter.
x - p2.
x) * (circumcenter.
x - p2.
x) + (circumcenter.
y - p2.
y) * (circumcenter.
y - p2.
y) ;
1009 double r3 = (circumcenter.
x - p3.
x) * (circumcenter.
x - p3.
x) + (circumcenter.
y - p3.
y) * (circumcenter.
y - p3.
y) ;
1012 radius = std::sqrt( std::max( r1, std::max( r2, r3) )) ;
1017 template<
typename Po
intInT>
inline bool
1023 double sizeX = cam.
width;
1024 double sizeY = cam.
height;
1035 double focal_x, focal_y;
1046 UV_coordinates.
x =
static_cast<float> ((focal_x * (pt.x / pt.z) + cx) / sizeX);
1047 UV_coordinates.
y = 1.0f -
static_cast<float> ((focal_y * (pt.y / pt.z) + cy) / sizeY);
1050 if (UV_coordinates.
x >= 0.0 && UV_coordinates.
x <= 1.0 && UV_coordinates.
y >= 0.0 && UV_coordinates.
y <= 1.0)
1055 UV_coordinates.
x = -1.0f;
1056 UV_coordinates.
y = -1.0f;
1061 template<
typename Po
intInT>
inline bool
1065 Eigen::Vector2d v0, v1, v2;
1066 v0(0) = p3.
x - p1.
x; v0(1) = p3.
y - p1.
y;
1067 v1(0) = p2.
x - p1.
x; v1(1) = p2.
y - p1.
y;
1068 v2(0) = pt.
x - p1.
x; v2(1) = pt.
y - p1.
y;
1071 double dot00 = v0.dot(v0);
1072 double dot01 = v0.dot(v1);
1073 double dot02 = v0.dot(v2);
1074 double dot11 = v1.dot(v1);
1075 double dot12 = v1.dot(v2);
1078 double invDenom = 1.0 / (dot00*dot11 - dot01*dot01);
1079 double u = (dot11*dot02 - dot01*dot12) * invDenom;
1080 double v = (dot00*dot12 - dot01*dot02) * invDenom;
1083 return ((u >= 0) && (v >= 0) && (u + v < 1));
1087 template<
typename Po
intInT>
inline bool
1090 return (getPointUVCoordinates(p1, camera, proj1)
1092 getPointUVCoordinates(p2, camera, proj2)
1094 getPointUVCoordinates(p3, camera, proj3)
1098 #define PCL_INSTANTIATE_TextureMapping(T) \
1099 template class PCL_EXPORTS pcl::TextureMapping<T>;