CylinderPatch.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Fraunhofer FKIE
00003  *
00004  * Authors: Bastian Gaspers
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * * Neither the name of the Fraunhofer FKIE nor the names of its
00015  *   contributors may be used to endorse or promote products derived from
00016  *   this software without specific prior written permission.
00017  *
00018  * This file is part of the StructureColoring ROS package.
00019  *
00020  * The StructureColoring ROS package is free software:
00021  * you can redistribute it and/or modify it under the terms of the
00022  * GNU Lesser General Public License as published by the Free
00023  * Software Foundation, either version 3 of the License, or
00024  * (at your option) any later version.
00025  *
00026  * The StructureColoring ROS package is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU Lesser General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU Lesser General Public License
00032  * along with The StructureColoring ROS package.
00033  * If not, see <http://www.gnu.org/licenses/>.
00034  */
00035 
00036 #ifndef _CylinderPatch_h_
00037 #define _CylinderPatch_h_
00038 
00039 #include "pcl/point_types.h"
00040 #include "pcl/point_cloud.h"
00041 #include <vector>
00042 #include <limits.h>
00043 #include <Eigen/Dense>
00044 #include <Eigen/StdVector>
00045 #include <opencv2/core/core.hpp>
00046 #include <boost/shared_ptr.hpp>
00047 #include <list>
00048 #include <structureColoring/structures/Cylinder3D.h>
00049 
00050 class CylinderPatch : public Cylinder3D{
00051 public:
00052         typedef boost::shared_ptr<CylinderPatch> CylinderPatchPtr;
00053         typedef boost::shared_ptr<const CylinderPatch> CylinderPatchConstPtr;
00054         typedef std::list<CylinderPatchPtr> CylinderPatches;
00055 
00056         typedef std::vector<int> PointIndices;
00057         typedef pcl::PointXYZ PointT;
00058         typedef pcl::PointXYZRGB ColoredPointT;
00059         typedef pcl::PointXYZI IntensityPointT;
00060         typedef pcl::PointCloud<PointT> PointCloud;
00061         typedef pcl::PointCloud<ColoredPointT> ColoredPointCloud;
00062         typedef pcl::PointCloud<IntensityPointT> IntensityPointCloud;
00063         typedef std::pair<unsigned int, unsigned int> Pair2ui;
00064         typedef Eigen::Vector3f Vec3;
00065         typedef Eigen::Matrix3f Mat3;
00066         typedef std::vector<Vec3, Eigen::aligned_allocator<Vec3> > Points;
00067 
00068 
00069         CylinderPatch() :
00070                 Cylinder3D(), mCylinderId(0), mTextureMap(NULL), mHeightMap(NULL),
00071                                 mNormalMap(NULL) {
00072         }
00073 
00074         template<typename PointCloudType>
00075         CylinderPatch(const PointIndices& inliers, const PointCloudType& pointCloud, const Vec3& pointOnAxis,
00076                         const Vec3& axisDirection, const float radius, const float distThreshold) :
00077                 Cylinder3D(pointOnAxis, axisDirection, radius), mDistThreshold(distThreshold), mPointIndices(inliers),
00078                 mCylinderId(0), mTextureMap(NULL), mHeightMap(NULL), mNormalMap(NULL) {
00079                 computeTransformation();
00080                 initialize(inliers, pointCloud);
00081         }
00082 
00083         template<typename PointCloudType>
00084         CylinderPatch(const PointIndices& inliers, const PointCloudType& pointCloud, const Vec3& pointOnAxis,
00085                         const Vec3& axisDirection, const float radius, const Vec3& rotationAxis, const float rotationAngle, const float distThreshold) :
00086                 Cylinder3D(pointOnAxis, axisDirection, radius, rotationAxis, rotationAngle),
00087                 mDistThreshold(distThreshold), mPointIndices(inliers), mCylinderId(0), mTextureMap(NULL), mHeightMap(NULL), mNormalMap(NULL) {
00088                 setTransformation(rotationAngle, rotationAxis);
00089                 initialize(inliers, pointCloud);
00090         }
00091 
00092         virtual ~CylinderPatch(){
00093                 if (mTextureMap) delete mTextureMap;
00094                 if (mHeightMap) delete mHeightMap;
00095                 if (mNormalMap) delete mNormalMap;
00096         }
00097 
00098 //      template<typename PointType>
00099 //      static Vec3 getEigenVecFromPCL(const PointType& point) {
00100 //              return point.getVector3fMap();
00101 //      }
00102 
00103 //      void setAxis(const Vec3& axis ){mAxisDirection = axis;}
00104 //      void setRadius(float rad){mRadius = rad;}
00105 //      void setMidPoint (const Vec3& midPoint){mMidPoint = midPoint;}
00106 //      void setPointOnAxis(const Vec3& poa){mPointOnAxis = poa;}
00107         void setId(const size_t& id){mCylinderId = id;}
00108 
00109 //getter
00110         const Points& getBBVertices() const {return mBBVertices;}
00111 //      const Vec3& getPointOnAxis() const {return mPointOnAxis;}
00112 //      const Vec3& getAxisDirection() const {return mAxisDirection;}
00113 //      const float& getRadius() const {return mRadius;}
00114         float getAxisMin() const {return mAxisMin;}
00115         float getAxisMax() const {return mAxisMax;}
00116         void getAxisLimits(float& min,float& max) const {min = mAxisMin; max = mAxisMax;}
00117         const float& getHeight()const{return mHeight;}
00118         const Vec3& getTopPoint()const{return mTopPoint;}
00119         const Vec3& getMidPoint()const{return mMidPoint;}
00120 //      const Vec3& getRotationAxis()const{return mRotAxis;}
00121 //      const float& getRotationAngle()const{return mRotAngle;}
00122 //      const Mat3& getTransformRot()const{return mTransformRot;}
00123 //      const Mat3& getReTransformRot()const{return mReTransformRot;}
00124         const PointIndices& getInliers()const{return mPointIndices;}
00125         const size_t& getId()const{return mCylinderId;}
00126 
00127         const cv::Mat* getTextureMap() const {
00128                 return mTextureMap;
00129         }
00130         const cv::Mat* getHeightMap() const {
00131                 return mHeightMap;
00132         }
00133         const cv::Mat* getNormalMap() const {
00134                 return mNormalMap;
00135         }
00136 
00137 // inlier checks
00138         bool checkDistance(const Vec3& point) const{
00139                 return Cylinder3D::checkDistance(point, mDistThreshold);
00140         }
00141 
00142 //      bool checkNormal(const Vec3& normal, const Vec3& point, const float& mAngleEps) const{
00143 //              Vec3 transformedPoint;
00144 //              transformToCylinder(transformedPoint, point);
00145 //              Eigen::Vector2f dirFromCenter(transformedPoint.x(), transformedPoint.y());
00146 //              dirFromCenter.normalize();
00147 //
00148 //              Vec3 normalCylinder = mTransformRot * normal;
00149 //              Eigen::Vector2f normal2 = normalCylinder.block<2,1>(0,0);
00150 //
00151 //              return fabsf(dirFromCenter.dot(normal2)) >= cosf(mAngleEps);
00152 //      }
00153 
00154         bool checkHeight(const Vec3& point, const float& heightDev){
00155                 Vec3 transformedPoint;
00156                 transformToCylinder(transformedPoint, point);
00157                 if( transformedPoint.z() < mAxisMax + heightDev * mHeight && transformedPoint.z() > mAxisMin - heightDev * mHeight) return true;
00158                 return false;
00159         }
00160 
00161         template<typename PointCloudType>
00162         void addPoints(const PointIndices& pointIndices, const PointCloudType& pointCloud){
00163                 mPointIndices.reserve(mPointIndices.size() + pointIndices.size());
00164                 for(PointIndices::const_iterator pi_it = pointIndices.begin();pi_it != pointIndices.end(); ++pi_it){
00165                         mPointIndices.push_back(*pi_it);
00166                 }
00167         this->addToHeight(pointIndices, pointCloud);
00168 
00169         }
00170 
00171 //      /**
00172 //       * \brief Compute distance from point to cylinder surface.
00173 //       * \param point Compute this point's distance.
00174 //       */
00175 //      float distanceToCylinderSurface(const Vec3& point) const{
00176 //              return distanceToAxis(point) - mRadius;
00177 //      }
00178 //
00179 //      float distanceToAxis(const Vec3& point) const;
00180 
00181 //      void projectPoint(Vec3& outPoint, const Vec3& point) const;
00182 //      void transformToCylinder(Vec3& outPoint, const Vec3& point) const { outPoint = mTransformRot * (point + mTransformTrans);}
00183 //      void transformToXYZCoords(Vec3& outPoint, const Vec3& point) const { outPoint = (mReTransformRot * point) - mTransformTrans;}
00184 
00185 //      void computeTexture(float texturePixelSize);
00186 private:
00187 //      /**
00188 //       * \brief Calculate transformations from and to cylinder coordinate system.
00189 //       */
00190 //      void computeTransformation();
00191 
00195         void setTransformation(const float rotationAngle, const Vec3& rotationAxis);
00196 
00197         template<typename PointCloudType>
00198         void initialize(const PointIndices& pointIndices, const PointCloudType& pointCloud);
00199 
00200         template<typename PointCloudType>
00201         void addToHeight(const PointIndices& pointIndices, const PointCloudType& pointCloud);
00202 
00203         void computeOrientedBoundingBox();
00204 
00205 //      Vec3 mPointOnAxis;
00206 //      Vec3 mAxisDirection;
00207 //      float mRadius;
00208 
00209 //      Vec3 mRotAxis;
00210 //      float mRotAngle;
00211 //      Mat3 mTransformRot, mReTransformRot;
00212 //      Vec3 mTransformTrans;
00213         float mAxisMin, mAxisMax;
00214         float mHeight;
00215         Vec3 mTopPoint;
00216         Vec3 mMidPoint;
00217 
00218         float mDistThreshold;
00219 
00220         PointIndices mPointIndices;
00221         Points mBBVertices;
00222         size_t mCylinderId;
00223 
00224         cv::Mat* mTextureMap;
00225         cv::Mat* mHeightMap;
00226         cv::Mat* mNormalMap;
00227 };
00228 
00229 //inline float CylinderPatch::distanceToAxis(const Vec3& point) const {
00230 //      Vec3 transformedPoint;
00231 //      transformToCylinder(transformedPoint, point);
00232 //      Eigen::Vector2f pointOnHeightZ(transformedPoint.x(), transformedPoint.y());
00233 //      return pointOnHeightZ.norm();
00234 //}
00235 
00236 //inline void CylinderPatch::projectPoint(Vec3& outPoint, const Vec3& point) const{
00237 //      Vec3 transformedPoint;
00238 //      transformToCylinder(transformedPoint, point);
00239 //      Vec3 pOnAxisSameZ(0.f, 0.f, transformedPoint.z());
00240 //      Vec3 connection = transformedPoint - pOnAxisSameZ;
00241 //      float connectionLength = connection.norm();
00242 //      outPoint = pOnAxisSameZ + connection * (mRadius / connectionLength);
00243 //}
00244 
00245 template<typename PointCloudType>
00246 void CylinderPatch::initialize(const PointIndices& pointIndices, const PointCloudType& pointCloud){
00247         //calculate mAxisMin/mAxisMax (on z axis) for bounding box
00248         mAxisMin = std::numeric_limits<float>::max();
00249         mAxisMax = -std::numeric_limits<float>::max();
00250         for(PointIndices::const_iterator pit = pointIndices.begin(); pit != pointIndices.end(); ++pit){
00251                 Vec3 p;
00252                 transformToCylinder(p, pointCloud.points[*pit].getVector3fMap());
00253 
00254                 if (p.z() < mAxisMin) mAxisMin = p.z();
00255                 if (p.z() > mAxisMax) mAxisMax = p.z();
00256         }
00257         mHeight = fabs(mAxisMax - mAxisMin);
00258         Vec3 tp(0.f, 0.f, mAxisMax);
00259         Vec3 mp(0.f, 0.f, (mAxisMax + mAxisMin) * 0.5f);
00260         transformToXYZCoords(mMidPoint, mp);
00261         transformToXYZCoords(mTopPoint, tp);
00262 
00263         computeOrientedBoundingBox();
00264 }
00265 
00266 template<typename PointCloudType>
00267 void CylinderPatch::addToHeight(const PointIndices& pointIndices, const PointCloudType& pointCloud){
00268         for(PointIndices::const_iterator pit = pointIndices.begin(); pit != pointIndices.end(); ++pit){
00269                 Vec3 p;
00270                 transformToCylinder(p, pointCloud.points[*pit].getVector3fMap());
00271 
00272                 if (p.z() < mAxisMin){
00273                         mAxisMin = p.z();
00274                 }
00275                 if (p.z() > mAxisMax){
00276                         mAxisMax = p.z();
00277                 }
00278         }
00279         mHeight = fabs(mAxisMax - mAxisMin);
00280         Vec3 tp(0.f, 0.f, mAxisMax);
00281         Vec3 mp(0.f, 0.f, (mAxisMax + mAxisMin) * 0.5f);
00282         transformToXYZCoords(mMidPoint, mp);
00283         transformToXYZCoords(mTopPoint, tp);
00284 
00285         computeOrientedBoundingBox();
00286 }
00287 
00288 #endif /*_CylinderPatch_h_*/


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09