27 #include <DataStructures/KdTree/KdTree.h>
28 #include <Tracking/ICP.h>
39 m_pKdTree =
new CKdTree();
41 m_nNumScenePoints = 0;
42 m_pValues =
new float*[1];
49 for (
int i = 0; i < m_nNumScenePoints; i++)
51 delete[] m_pValues[i];
60 m_aScenePoints.clear();
62 for (
int i = 0; i < m_nNumScenePoints; i++)
64 delete[] m_pValues[i];
71 const int nSize = aScenePoints.size();
73 for (
int i = 0; i < nSize; i++)
75 m_aScenePoints.push_back(aScenePoints.at(i));
78 m_pKdTree =
new CKdTree(nSize);
80 m_pValues =
new float*[nSize];
81 m_nNumScenePoints = nSize;
83 for (
int i = 0; i < nSize; i++)
85 m_pValues[i] =
new float[3];
86 m_pValues[i][0] = aScenePoints.at(i)[0];
87 m_pValues[i][1] = aScenePoints.at(i)[1];
88 m_pValues[i][2] = aScenePoints.at(i)[2];
91 m_pKdTree->Build(m_pValues, 0, nSize - 1, m_nKdTreeBucketSize, 3, 0);
96 m_aObjectPoints.clear();
98 for (
size_t i = 0; i < aObjectPoints.size(); i++)
100 m_aObjectPoints.push_back(aObjectPoints.at(i));
105 Math3d::SetMat(mRotation, Math3d::unit_mat);
106 Math3d::SetVec(vTranslation, 0, 0, 0);
108 if (m_aObjectPoints.size() < 3)
114 if (m_aScenePoints.size() < 3)
120 float fLastDistance = FLT_MAX;
121 float fDistance = 0, fPointDistance;
122 Vec3d vPoint, vPointTransformed;
123 float* pPointData =
new float[3];
124 float* pNearestNeighbor;
125 Vec3d* pPointPositions =
new Vec3d[m_aObjectPoints.size()];
126 Vec3d* pCorrespondingPointPositions =
new Vec3d[m_aObjectPoints.size()];
127 int nNumPointCorrespondences;
130 for (
int n = 0; n < m_nMaxIterations; n++)
133 nNumPointCorrespondences = 0;
136 for (
size_t i = 0; i < m_aObjectPoints.size(); i++)
138 Math3d::SetVec(vPoint, m_aObjectPoints.at(i)[0], m_aObjectPoints.at(i)[1], m_aObjectPoints.at(i)[2]);
139 Math3d::MulMatVec(mRotation, vPoint, vTranslation, vPointTransformed);
141 pPointData[0] = vPointTransformed.x;
142 pPointData[1] = vPointTransformed.y;
143 pPointData[2] = vPointTransformed.z;
145 m_pKdTree->NearestNeighborBBF(pPointData, fPointDistance, pNearestNeighbor);
147 fPointDistance = sqrtf(fPointDistance);
150 if (fPointDistance > m_fCutoffDistance)
152 fDistance += m_fCutoffDistance;
156 fDistance += fPointDistance;
157 Math3d::SetVec(pPointPositions[nNumPointCorrespondences], vPoint);
158 Math3d::SetVec(pCorrespondingPointPositions[nNumPointCorrespondences], pNearestNeighbor[0], pNearestNeighbor[1], pNearestNeighbor[2]);
159 nNumPointCorrespondences++;
163 if (nNumPointCorrespondences < 3)
165 ARMARX_WARNING_S <<
"CColorICP::SearchObject: not enough correspondences found!";
170 CICP::CalculateOptimalTransformation(pPointPositions, pCorrespondingPointPositions, nNumPointCorrespondences, mRotation, vTranslation);
175 if (0.8f * fDistance < fBestDistanceUntilNow)
178 if (1.0f - fDistance / fLastDistance < 0.01f * m_fConvergenceDelta)
183 else if ((1.0f - fDistance / fLastDistance < m_fConvergenceDelta) || (2 * n > m_nMaxIterations))
188 else if ((0.5f * fDistance > fBestDistanceUntilNow) && (3 * n > m_nMaxIterations))
194 fLastDistance = fDistance;
200 delete[] pPointPositions;
201 delete[] pCorrespondingPointPositions;
203 return fDistance / m_aObjectPoints.size();
206 void ICP::SetParameters(
float fCutoffDistance,
float fConvergenceDelta,
int nMaxIterations,
int nKdTreeBucketSize)
208 m_fCutoffDistance = fCutoffDistance;
209 m_fConvergenceDelta = fConvergenceDelta;
210 m_nMaxIterations = nMaxIterations;
211 m_nKdTreeBucketSize = nKdTreeBucketSize;
216 float* pPointData =
new float[3];
217 float* pNearestNeighbor;
220 for (
size_t i = 0; i < m_aObjectPoints.size(); i++)
222 pPointData[0] = m_aObjectPoints.at(i)[0];
223 pPointData[1] = m_aObjectPoints.at(i)[1];
224 pPointData[2] = m_aObjectPoints.at(i)[2];
226 m_pKdTree->NearestNeighborBBF(pPointData, fDistance, pNearestNeighbor);
228 aPointMatchDistances.push_back(fDistance);
236 float* pPointData =
new float[3];
237 float* pNearestNeighbor;
238 Eigen::Vector3f pNearestNeighborPoint;
241 for (
size_t i = 0; i < m_aObjectPoints.size(); i++)
243 pPointData[0] = m_aObjectPoints.at(i)[0];
244 pPointData[1] = m_aObjectPoints.at(i)[1];
245 pPointData[2] = m_aObjectPoints.at(i)[2];
247 m_pKdTree->NearestNeighborBBF(pPointData, fDistance, pNearestNeighbor);
249 pNearestNeighborPoint[0] = pNearestNeighbor[0];
250 pNearestNeighborPoint[1] = pNearestNeighbor[1];
251 pNearestNeighborPoint[2] = pNearestNeighbor[2];
253 aNeighbors.push_back(pNearestNeighborPoint);
254 aPointMatchDistances.push_back(fDistance);