16 #ifndef __MISCELLANEOUS_H
17 #define __MISCELLANEOUS_H
19 #include <mrpt/config.h>
28 #include <pcl/point_types.h>
29 #include <pcl/point_cloud.h>
39 template <
class po
intPCL>
42 return Eigen::Vector3f(pt.x, pt.y, pt.z);
45 template <
class POINT>
46 inline Eigen::Vector3f
diffPoints(
const POINT& P1,
const POINT& P2)
49 diff[0] = P1.x - P2.x;
50 diff[1] = P1.y - P2.y;
51 diff[2] = P1.z - P2.z;
56 template <
class dataType>
58 Eigen::Matrix<dataType, 4, 4>& pose, Eigen::Matrix<dataType, 3, 1>& point)
60 Eigen::Matrix<dataType, 3, 1> transformedPoint =
61 pose.block(0, 0, 3, 3) * point + pose.block(0, 3, 3, 1);
62 return transformedPoint;
66 template <
class dataType>
68 Eigen::Matrix<dataType, 4, 4>& pose1, Eigen::Matrix<dataType, 4, 4>& pose2)
70 Eigen::Matrix<dataType, 4, 4> transformedPose;
71 transformedPose.block(0, 0, 3, 3) =
72 pose1.block(0, 0, 3, 3) * pose2.block(0, 0, 3, 3);
73 transformedPose.block(0, 3, 3, 1) =
74 pose1.block(0, 3, 3, 1) +
75 pose1.block(0, 0, 3, 3) * pose2.block(0, 3, 3, 1);
76 transformedPose.row(3) << 0, 0, 0, 1;
77 return transformedPose;
81 template <
class dataType>
82 Eigen::Matrix<dataType, 4, 4>
inverse(Eigen::Matrix<dataType, 4, 4>& pose)
84 Eigen::Matrix<dataType, 4, 4>
inverse;
85 inverse.block(0, 0, 3, 3) = pose.block(0, 0, 3, 3).transpose();
87 -(
inverse.block(0, 0, 3, 3) * pose.block(0, 3, 3, 1));
103 bool isInHull(
PointT& point3D, pcl::PointCloud<PointT>::Ptr hull3D);
105 template <
typename dataType>
108 float normalizeConst = 255.0 /
range;
109 std::vector<int> data2(
data.size());
110 for (
size_t i = 0; i <
data.size(); i++)
111 data2[i] = (
int)(
data[i] * normalizeConst);
114 for (
size_t i = 0; i < data2.size(); i++)
123 if (bin->second >
count)
129 return (dataType)
mode / normalizeConst;
153 template <
typename dataType>
155 std::vector<Eigen::Vector4f>&
data, dataType& stdDevHist,
156 dataType& concentration)
160 std::vector<Eigen::Vector4f> dataTemp =
data;
164 Eigen::Vector3f
sum = Eigen::Vector3f::Zero();
165 for (
size_t i = 0; i <
data.size(); i++)
169 Eigen::Vector3f meanShift =
sum /
size;
172 Eigen::Matrix3f
cov = Eigen::Matrix3f::Zero();
173 for (
size_t i = 0; i <
data.size(); i++)
175 Eigen::Vector3f diff =
data[i].head(3) - meanShift;
176 cov += diff * diff.transpose();
179 Eigen::JacobiSVD<Eigen::Matrix3f> svd(
cov);
180 stdDevHist = svd.singularValues().maxCoeff() / sqrt((
double)
size);
184 int iteration_counter = 0;
185 double convergence = 0.001;
186 while (2 * dataTemp.size() >
size && shift > convergence)
193 it != dataTemp.end();)
196 Eigen::Vector3f diff = (*it).head(3) - meanShift;
197 if (diff.norm() > stdDevHist)
199 sum -= (*it).head(3);
200 cov -= diff * diff.transpose();
208 Eigen::Vector3f meanUpdated =
sum / dataTemp.size();
209 shift = (meanUpdated - meanShift).
norm();
210 meanShift = meanUpdated;
211 svd = Eigen::JacobiSVD<Eigen::Matrix3f>(
cov);
214 svd.singularValues().maxCoeff() / sqrt((
double)dataTemp.size());
225 Eigen::Vector4f dominantColor;
226 dominantColor.head(3) = meanShift;
227 float averageIntensity = 0;
228 for (
unsigned i = 0; i < dataTemp.size(); i++)
229 averageIntensity += dataTemp[i][3];
230 averageIntensity /= dataTemp.size();
231 dominantColor(3) = averageIntensity;
234 int countFringe05 = 0;
236 it !=
data.end(); it++)
237 if ((it->head(3) - meanShift).norm() <
240 concentration =
static_cast<dataType
>(countFringe05) /
data.size();
242 return dominantColor;
247 template <
typename dataType>
249 std::vector<dataType>&
data,
double range,
250 dataType& stdDevHist_out)
256 std::vector<dataType> dataTemp =
data;
259 for (
size_t i = 0; i <
data.size(); i++)
263 dataType meanShift =
sum /
size;
274 int iteration_counter = 0;
275 double convergence =
range * 0.001;
276 while (2 * dataTemp.size() >
size && shift > convergence)
281 it != dataTemp.end();)
284 if (fabs(*it - meanShift) > stdDevHist)
294 double meanUpdated =
sum / dataTemp.size();
295 shift = fabs(meanUpdated - meanShift);
296 meanShift = meanUpdated;
317 return static_cast<dataType
>(meanShift);
342 std::ostream&
operator<<(std::ostream& os,
const std::vector<T>&
v)
344 std::copy(
v.begin(),
v.end(), std::ostream_iterator<T>(os,
" "));