chisel_conversions.h
Go to the documentation of this file.
00001 /*
00002  * chisel_conversions.h
00003  *
00004  *  Created on: 2018-03-25
00005  *      Author: mathieu
00006  */
00007 
00008 #ifndef CHISEL_CONVERSIONS_H_
00009 #define CHISEL_CONVERSIONS_H_
00010 
00011 #include <rtabmap/core/CameraModel.h>
00012 #include <open_chisel/Chisel.h>
00013 #include <pcl/PolygonMesh.h>
00014 #include <pcl/common/transforms.h>
00015 
00016 namespace rtabmap
00017 {
00018 
00019 std::shared_ptr<chisel::ColorImage<unsigned char> > colorImageToChisel(const cv::Mat & image)
00020 {
00021         UASSERT(image.type() == CV_8UC3 || image.type() == CV_8UC4);
00022         std::shared_ptr<chisel::ColorImage<unsigned char> > imageChisel(new chisel::ColorImage<unsigned char>(image.cols, image.rows, image.channels()));
00023         memcpy(imageChisel->GetMutableData(), image.data, image.total()*sizeof(unsigned char)*image.channels());
00024         return imageChisel;
00025 }
00026 
00027 std::shared_ptr<chisel::DepthImage<float> > depthImageToChisel(const cv::Mat & image)
00028 {
00029         UASSERT(image.type() == CV_32FC1);
00030         std::shared_ptr<chisel::DepthImage<float> > imageChisel(new chisel::DepthImage<float>(image.cols, image.rows));
00031         memcpy(imageChisel->GetMutableData(), (float*)image.data, image.total()*sizeof(float));
00032         return imageChisel;
00033 }
00034 
00035 chisel::PinholeCamera cameraModelToChiselCamera(const CameraModel& camera)
00036 {
00037    chisel::PinholeCamera cameraToReturn;
00038    chisel::Intrinsics intrinsics;
00039    intrinsics.SetFx(camera.fx());
00040    intrinsics.SetFy(camera.fy());
00041    intrinsics.SetCx(camera.cx());
00042    intrinsics.SetCy(camera.cy());
00043    cameraToReturn.SetIntrinsics(intrinsics);
00044    cameraToReturn.SetWidth(camera.imageWidth());
00045    cameraToReturn.SetHeight(camera.imageHeight());
00046    return cameraToReturn;
00047 }
00048 
00049 template<typename PointRGBT>
00050 chisel::PointCloudPtr pointCloudRGBToChisel(const typename pcl::PointCloud<PointRGBT>& cloud, const Transform & transform = Transform::getIdentity())
00051 {
00052         chisel::PointCloudPtr chiselCloud(new chisel::PointCloud());
00053         chiselCloud->GetMutablePoints().resize(cloud.size());
00054         chiselCloud->GetMutableColors().resize(cloud.size());
00055         float byteToFloat = 1.0f / 255.0f;
00056         int oi=0;
00057         Eigen::Affine3f transformf = transform.toEigen3f();
00058         for(unsigned int i=0; i<cloud.size(); ++i)
00059         {
00060                 const PointRGBT & pt = cloud.at(i);
00061                 if(pcl::isFinite(pt))
00062                 {
00063                         PointRGBT ptt = pcl::transformPoint(pt, transformf);
00064 
00065                         chisel::Vec3& xyz =  chiselCloud->GetMutablePoints().at(oi);
00066                         xyz(0) = ptt.x;
00067                         xyz(1) = ptt.y;
00068                         xyz(2) = ptt.z;
00069 
00070                         chisel::Vec3& rgb = chiselCloud->GetMutableColors().at(oi);
00071                         rgb(0) = ptt.r * byteToFloat;
00072                         rgb(1) = ptt.g * byteToFloat;
00073                         rgb(2) = ptt.b * byteToFloat;
00074 
00075                         ++oi;
00076                 }
00077         }
00078         chiselCloud->GetMutablePoints().resize(oi);
00079         chiselCloud->GetMutableColors().resize(oi);
00080         return chiselCloud;
00081 }
00082 
00083 template<typename PointT>
00084 chisel::PointCloudPtr pointCloudToChisel(const typename pcl::PointCloud<PointT>& cloud, const Transform & transform = Transform::getIdentity())
00085 {
00086         chisel::PointCloudPtr chiselCloud(new chisel::PointCloud());
00087         chiselCloud->GetMutablePoints().resize(cloud.size());
00088         int oi=0;
00089         Eigen::Affine3f transformf = transform.toEigen3f();
00090         for(unsigned int i=0; i<cloud.size(); ++i)
00091         {
00092                 const PointT & pt = cloud.at(i);
00093                 if(pcl::isFinite(pt))
00094                 {
00095                         PointT ptt = pcl::transformPoint(pt, transformf);
00096 
00097                         chisel::Vec3& xyz =  chiselCloud->GetMutablePoints().at(oi);
00098                         xyz(0) = ptt.x;
00099                         xyz(1) = ptt.y;
00100                         xyz(2) = ptt.z;
00101 
00102                         ++oi;
00103                 }
00104         }
00105         chiselCloud->GetMutablePoints().resize(oi);
00106         return chiselCloud;
00107 }
00108 
00109 pcl::PolygonMesh::Ptr chiselToPolygonMesh(const chisel::MeshMap& meshMap, unsigned char r=100, unsigned char g=100, unsigned char b=100)
00110 {
00111         pcl::PolygonMesh::Ptr mesh (new pcl::PolygonMesh);
00112 
00113         if(meshMap.size())
00114         {
00115                 bool hasColor = meshMap.begin()->second->colors.size();
00116                 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
00117                 size_t v = 0;
00118                 for (const std::pair<chisel::ChunkID, chisel::MeshPtr>& it : meshMap)
00119                 {
00120                         UASSERT((!hasColor || (it.second->vertices.size() == it.second->colors.size())) &&
00121                                         it.second->vertices.size() == it.second->normals.size());
00122                         cloud->resize(cloud->size() + it.second->vertices.size());
00123 
00124                         mesh->polygons.resize(mesh->polygons.size()+it.second->vertices.size()/3);
00125 
00126                         for (unsigned int i=0;i<it.second->vertices.size(); ++i)
00127                         {
00128                                 pcl::PointXYZRGBNormal & pt = cloud->at(v);
00129                                 pt.x = it.second->vertices[i][0];
00130                                 pt.y = it.second->vertices[i][1];
00131                                 pt.z = it.second->vertices[i][2];
00132                                 if(hasColor)
00133                                 {
00134                                         pt.r = it.second->colors[i][0] * 255.0f;
00135                                         pt.g = it.second->colors[i][1] * 255.0f;
00136                                         pt.b = it.second->colors[i][2] * 255.0f;
00137                                 }
00138                                 else
00139                                 {
00140                                         pt.r = r;
00141                                         pt.g = g;
00142                                         pt.b = b;
00143                                 }
00144                                 pt.normal_x = it.second->normals[i][0];
00145                                 pt.normal_y = it.second->normals[i][1];
00146                                 pt.normal_z = it.second->normals[i][2];
00147                                 pcl::Vertices & polygon = mesh->polygons.at(v/3);
00148                                 polygon.vertices.push_back(v++);
00149                         }
00150                 }
00151                 pcl::toPCLPointCloud2(*cloud, mesh->cloud);
00152         }
00153         return mesh;
00154 }
00155 
00156 }
00157 
00158 
00159 #endif /* CHISEL_CONVERSIONS_H_ */


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:19