cccorelib.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2021, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef CORELIB_SRC_ICP_CCCORELIB_H_
29 #define CORELIB_SRC_ICP_CCCORELIB_H_
30 
31 #include <CCCoreLib/RegistrationTools.h>
32 
33 namespace rtabmap {
34 
36  const pcl::PointCloud<pcl::PointXYZI>::Ptr & fromCloud,
37  pcl::PointCloud<pcl::PointXYZI>::Ptr & toCloud,
38  int maxIterations = 150,
39  double minRMSDecrease = 0.00001,
40  bool force3DoF = false,
41  bool force4DoF = false,
42  int samplingLimit = 50000,
43  double finalOverlapRatio = 0.85,
44  bool filterOutFarthestPoints = false,
45  double maxFinalRMS = 0.2,
46  float * finalRMS = 0,
47  std::string * errorMsg = 0)
48 {
49  UDEBUG("maxIterations=%d", maxIterations);
50  UDEBUG("minRMSDecrease=%f", minRMSDecrease);
51  UDEBUG("samplingLimit=%d", samplingLimit);
52  UDEBUG("finalOverlapRatio=%f", finalOverlapRatio);
53  UDEBUG("filterOutFarthestPoints=%s", filterOutFarthestPoints?"true":"false");
54  UDEBUG("force 3DoF=%s 4DoF=%s", force3DoF?"true":"false", force4DoF?"true":"false");
55  UDEBUG("maxFinalRMS=%f", maxFinalRMS);
56 
57  rtabmap::Transform icpTransformation;
58 
59  CCCoreLib::ICPRegistrationTools::RESULT_TYPE result;
60  CCCoreLib::PointProjectionTools::Transformation transform;
61  CCCoreLib::ICPRegistrationTools::Parameters params;
62  {
63  if(minRMSDecrease > 0.0)
64  {
65  params.convType = CCCoreLib::ICPRegistrationTools::MAX_ERROR_CONVERGENCE;
66  params.minRMSDecrease = minRMSDecrease;
67  }
68  else
69  {
70  params.convType = CCCoreLib::ICPRegistrationTools::MAX_ITER_CONVERGENCE;
71  params.nbMaxIterations = maxIterations;
72  }
73  params.adjustScale = false;
74  params.filterOutFarthestPoints = filterOutFarthestPoints;
75  params.samplingLimit = samplingLimit;
76  params.finalOverlapRatio = finalOverlapRatio;
77  params.modelWeights = nullptr;
78  params.dataWeights = nullptr;
79  params.transformationFilters = force3DoF?33:force4DoF?1:0;
80  params.maxThreadCount = 0;
81  }
82 
83  double finalError = 0.0;
84  unsigned finalPointCount = 0;
85 
86  CCCoreLib::PointCloud toPointCloud = CCCoreLib::PointCloud();
87  CCCoreLib::PointCloud fromPointCloud = CCCoreLib::PointCloud();
88 
89  fromPointCloud.reserve(fromCloud->points.size());
90  for(uint nIndex=0; nIndex < fromCloud->points.size(); nIndex++)
91  {
92  CCVector3 P;
93  P.x = fromCloud->points[nIndex].x;
94  P.y = fromCloud->points[nIndex].y;
95  P.z = force3DoF?0:fromCloud->points[nIndex].z;
96  fromPointCloud.addPoint(P);
97  }
98  toPointCloud.reserve(toCloud->points.size());
99  for(uint nIndex=0; nIndex < toCloud->points.size(); nIndex++)
100  {
101  CCVector3 P;
102  P.x = toCloud->points[nIndex].x;
103  P.y = toCloud->points[nIndex].y;
104  P.z = force3DoF?0:toCloud->points[nIndex].z;
105  toPointCloud.addPoint(P);
106  }
107 
108  UDEBUG("CCCoreLib: start ICP");
109  result = CCCoreLib::ICPRegistrationTools::Register(
110  &fromPointCloud,
111  nullptr,
112  &toPointCloud,
113  params,
114  transform,
115  finalError,
116  finalPointCount);
117  UDEBUG("CCCoreLib: ICP done!");
118 
119  UDEBUG("CC ICP result: %d", result);
120  UDEBUG("CC Final error: %f . Finall Pointcount: %d", finalError, finalPointCount);
121  UDEBUG("CC ICP success Trans: %f %f %f", transform.T.x,transform.T.y,transform.T.z);
122 
123  if(finalRMS)
124  {
125  *finalRMS = (float)finalError;
126  }
127 
128  if(result != 1)
129  {
130  std::string msg = uFormat("CCCoreLib has failed: Rejecting transform as result %d !=1", result);
131  UDEBUG(msg.c_str());
132  if(errorMsg)
133  {
134  *errorMsg = msg;
135  }
136 
137  icpTransformation.setNull();
138  return icpTransformation;
139  }
140  else if(!transform.R.isValid())
141  {
142  std::string msg = uFormat("CCCoreLib has failed: Rotation matrix is invalid");
143  UDEBUG(msg.c_str());
144  if(errorMsg)
145  {
146  *errorMsg = msg;
147  }
148  icpTransformation.setNull();
149  return icpTransformation;
150  }
151  //CC transform to EIgen4f
152  Eigen::Matrix4f matrix;
153  matrix.setIdentity();
154  for(int i=0;i<3;i++)
155  {
156  for(int j=0;j<3;j++)
157  {
158  matrix(i,j)=transform.R.getValue(i,j);
159  }
160  }
161  for(int i=0;i<3;i++)
162  {
163  matrix(i,3)=transform.T[i];
164  }
165 
166  icpTransformation = rtabmap::Transform::fromEigen4f(matrix);
167  icpTransformation = icpTransformation.inverse();
168  UDEBUG("CC ICP result: %s", icpTransformation.prettyPrint().c_str());
169 
170  if(finalError > maxFinalRMS)
171  {
172  std::string msg = uFormat("CCCoreLib has failed: Rejecting transform as RMS %f > %f (%s) ", finalError, maxFinalRMS, rtabmap::Parameters::kIcpCCMaxFinalRMS().c_str());
173  UDEBUG(msg.c_str());
174  if(errorMsg)
175  {
176  *errorMsg = msg;
177  }
178  icpTransformation.setNull();
179  }
180  return icpTransformation;
181 }
182 
183 }
184 
185 #endif /* CORELIB_SRC_ICP_CCCORELIB_H_ */
rtabmap::Transform::fromEigen4f
static Transform fromEigen4f(const Eigen::Matrix4f &matrix)
Definition: Transform.cpp:416
rtabmap::Transform::setNull
void setNull()
Definition: Transform.cpp:152
rtabmap::Transform::prettyPrint
std::string prettyPrint() const
Definition: Transform.cpp:326
j
std::ptrdiff_t j
uint
unsigned int uint
Definition: posegraph3.h:51
params
SmartProjectionParams params(gtsam::HESSIAN, gtsam::ZERO_ON_DEGENERACY)
rtabmap::icpCC
rtabmap::Transform icpCC(const pcl::PointCloud< pcl::PointXYZI >::Ptr &fromCloud, pcl::PointCloud< pcl::PointXYZI >::Ptr &toCloud, int maxIterations=150, double minRMSDecrease=0.00001, bool force3DoF=false, bool force4DoF=false, int samplingLimit=50000, double finalOverlapRatio=0.85, bool filterOutFarthestPoints=false, double maxFinalRMS=0.2, float *finalRMS=0, std::string *errorMsg=0)
Definition: cccorelib.h:35
rtabmap::Transform::inverse
Transform inverse() const
Definition: Transform.cpp:178
uFormat
std::string UTILITE_EXPORT uFormat(const char *fmt,...)
Definition: UConversion.cpp:365
rtabmap::Transform
Definition: Transform.h:41
PointMatcherSupport::Parametrizable
c_str
const char * c_str(Args &&...args)
UDEBUG
#define UDEBUG(...)
float
float
transform
EIGEN_DONT_INLINE void transform(const Quaternion< Scalar > &t, Data &data)
matrix
Map< const Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(const T *data, int rows, int cols, int stride)
rtabmap
Definition: CameraARCore.cpp:35
i
int i
result
RESULT & result
msg
msg


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:07