| 1 | //########################################################################## |
| 2 | //# # |
| 3 | //# CLOUDCOMPARE PLUGIN: qPCL # |
| 4 | //# # |
| 5 | //# This program is free software; you can redistribute it and/or modify # |
| 6 | //# it under the terms of the GNU General Public License as published by # |
| 7 | //# the Free Software Foundation; version 2 or later of the License. # |
| 8 | //# # |
| 9 | //# This program is distributed in the hope that it will be useful, # |
| 10 | //# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 11 | //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 12 | //# GNU General Public License for more details. # |
| 13 | //# # |
| 14 | //# COPYRIGHT: Luca Penasa # |
| 15 | //# # |
| 16 | //########################################################################## |
| 17 | // |
| 18 | #include "sm2cc.h" |
| 19 | |
| 20 | //Local |
| 21 | #include "PCLConv.h" |
| 22 | #include "my_point_types.h" |
| 23 | |
| 24 | //PCL |
| 25 | #include <pcl/common/io.h> |
| 26 | |
| 27 | //qCC_db |
| 28 | #include <ccPointCloud.h> |
| 29 | #include <ccScalarField.h> |
| 30 | |
| 31 | //system |
| 32 | #include <list> |
| 33 | |
| 34 | //PCL V1.6 or older |
| 35 | #ifdef PCL_VER_1_6_OR_OLDER |
| 36 | |
| 37 | #include <sensor_msgs/PointField.h> |
| 38 | typedef sensor_msgs::PointField PCLScalarField; |
| 39 | |
| 40 | #else //Version 1.7 or newer |
| 41 | |
| 42 | #include <pcl/PCLPointField.h> |
| 43 | typedef pcl::PCLPointField PCLScalarField; |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | //system |
| 48 | #include <assert.h> |
| 49 | |
| 50 | // Custom PCL point types |
| 51 | template<typename T> struct PointXYZTpl |
| 52 | { |
| 53 | union EIGEN_ALIGN16 |
| 54 | { |
| 55 | T data[3]; |
| 56 | struct |
| 57 | { |
| 58 | T x; |
| 59 | T y; |
| 60 | T z; |
| 61 | }; |
| 62 | }; |
| 63 | }; |
| 64 | |
| 65 | POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZTpl<std::int32_t>, |
| 66 | (std::int32_t, x, x) |
| 67 | (std::int32_t, y, y) |
| 68 | (std::int32_t, z, z) ) |
| 69 | |
| 70 | POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZTpl<std::int16_t>, |
| 71 | (std::int16_t, x, x) |
| 72 | (std::int16_t, y, y) |
| 73 | (std::int16_t, z, z) ) |
| 74 | |
| 75 | POINT_CLOUD_REGISTER_POINT_STRUCT(PointXYZTpl<double>, |
| 76 | (double, x, x) |
| 77 | (double, y, y) |
| 78 | (double, z, z) ) |
| 79 | |
| 80 | static size_t GetNumberOfPoints(const PCLCloud& pclCloud) |
| 81 | { |
| 82 | return static_cast<size_t>(pclCloud.width) * pclCloud.height; |
| 83 | } |
| 84 | |
| 85 | static bool ExistField(const PCLCloud& pclCloud, std::string fieldName) |
| 86 | { |
| 87 | for (size_t index = 0; index < pclCloud.fields.size(); ++index) |
| 88 | if (pclCloud.fields[index].name == fieldName) |
| 89 | return true; |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | template<class T> void PCLCloudToCCCloud( const PCLCloud& pclCloud, |
| 95 | ccPointCloud& ccCloud, |
| 96 | ccGLMatrixd* _transform = nullptr, |
| 97 | FileIOFilter::LoadParameters* _loadParameters = nullptr ) |
| 98 | { |
| 99 | size_t pointCount = GetNumberOfPoints(pclCloud); |
| 100 | |
| 101 | pcl::PointCloud<T> pcl_cloud; |
| 102 | FROM_PCL_CLOUD(pclCloud, pcl_cloud); |
| 103 | |
| 104 | CCVector3d Pshift(0, 0, 0); |
| 105 | |
| 106 | for (size_t i = 0; i < pointCount; ++i) |
| 107 | { |
| 108 | CCVector3d P( pcl_cloud.at(i).x, |
| 109 | pcl_cloud.at(i).y, |
| 110 | pcl_cloud.at(i).z ); |
| 111 | |
| 112 | if (_transform) |
| 113 | { |
| 114 | P = (*_transform) * P; |
| 115 | } |
| 116 | |
| 117 | if (_loadParameters) |
| 118 | { |
| 119 | if (i == 0) // first point |
| 120 | { |
| 121 | bool preserveCoordinateShift = true; |
| 122 | if (FileIOFilter::HandleGlobalShift(P, Pshift, preserveCoordinateShift, *_loadParameters)) |
| 123 | { |
| 124 | if (preserveCoordinateShift) |
| 125 | { |
| 126 | ccCloud.setGlobalShift(Pshift); |
| 127 | } |
| 128 | ccLog::Warning("[PCL-to-CC] PCL cloud has been recentered! Translation: (%.2f ; %.2f ; %.2f)", Pshift.x, Pshift.y, Pshift.z); |
| 129 | |
| 130 | if (_transform) |
| 131 | { |
| 132 | // we update the input transform so as to shift the points already 'at the source' |
| 133 | _transform->setTranslation(_transform->getTranslationAsVec3D() + Pshift); |
| 134 | |
| 135 | // and the first point has to be shifted only this time |
| 136 | P += Pshift; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (nullptr == _transform) |
| 142 | { |
| 143 | // we have to apply the shift locally |
| 144 | P += Pshift; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | ccCloud.addPoint(P.toPC()); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | bool pcl2cc::CopyXYZ( const PCLCloud& pclCloud, |
| 153 | ccPointCloud& ccCloud, |
| 154 | uint8_t coordinateType, |
| 155 | ccGLMatrixd* _transform/*=nullptr*/, |
| 156 | FileIOFilter::LoadParameters* _loadParameters/*=nullptr*/) |
| 157 | { |
| 158 | size_t pointCount = GetNumberOfPoints(pclCloud); |
| 159 | if (pointCount == 0) |
| 160 | { |
| 161 | assert(false); |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | if (!ccCloud.reserve(static_cast<unsigned>(pointCount))) |
| 166 | { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | //add xyz to the input cloud taking xyz infos from the sm cloud |
| 171 | switch (coordinateType) |
| 172 | { |
| 173 | case pcl::PCLPointField::INT16: |
| 174 | PCLCloudToCCCloud<PointXYZTpl<std::int16_t>>(pclCloud, ccCloud, _transform, _loadParameters); |
| 175 | break; |
| 176 | case pcl::PCLPointField::INT32: |
| 177 | PCLCloudToCCCloud<PointXYZTpl<std::int32_t>>(pclCloud, ccCloud, _transform, _loadParameters); |
| 178 | break; |
| 179 | case pcl::PCLPointField::FLOAT32: |
| 180 | PCLCloudToCCCloud<pcl::PointXYZ>(pclCloud, ccCloud, _transform, _loadParameters); |
| 181 | break; |
| 182 | case pcl::PCLPointField::FLOAT64: |
| 183 | PCLCloudToCCCloud< PointXYZTpl<double>>(pclCloud, ccCloud, _transform, _loadParameters); |
| 184 | break; |
| 185 | default: |
| 186 | ccLog::Warning("[PCL] Unsupported coordinate type " + QString::number(coordinateType)); |
| 187 | return false; |
| 188 | }; |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | bool pcl2cc::CopyNormals(const PCLCloud& pclCloud, ccPointCloud& ccCloud) |
| 194 | { |
| 195 | pcl::PointCloud<OnlyNormals>::Ptr pcl_cloud_normals (new pcl::PointCloud<OnlyNormals>); |
| 196 | FROM_PCL_CLOUD(pclCloud, *pcl_cloud_normals); |
| 197 | |
| 198 | if (!ccCloud.reserveTheNormsTable()) |
| 199 | return false; |
| 200 | |
| 201 | size_t pointCount = GetNumberOfPoints(pclCloud); |
| 202 | |
| 203 | //loop |
| 204 | for (size_t i = 0; i < pointCount; ++i) |
| 205 | { |
| 206 | CCVector3 N( static_cast<PointCoordinateType>(pcl_cloud_normals->at(i).normal_x), |
| 207 | static_cast<PointCoordinateType>(pcl_cloud_normals->at(i).normal_y), |
| 208 | static_cast<PointCoordinateType>(pcl_cloud_normals->at(i).normal_z) ); |
| 209 | |
| 210 | ccCloud.addNorm(N); |
| 211 | } |
| 212 | |
| 213 | ccCloud.showNormals(true); |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | bool pcl2cc::CopyRGB(const PCLCloud& pclCloud, ccPointCloud& ccCloud) |
| 219 | { |
| 220 | pcl::PointCloud<OnlyRGB>::Ptr pcl_cloud_rgb (new pcl::PointCloud<OnlyRGB>); |
| 221 | FROM_PCL_CLOUD(pclCloud, *pcl_cloud_rgb); |
| 222 | size_t pointCount = GetNumberOfPoints(pclCloud); |
| 223 | if (pointCount == 0) |
| 224 | return true; |
| 225 | if (!ccCloud.reserveTheRGBTable()) |
| 226 | return false; |
| 227 | |
| 228 | |
| 229 | //loop |
| 230 | for (size_t i = 0; i < pointCount; ++i) |
| 231 | { |
| 232 | ccColor::Rgb C( static_cast<ColorCompType>(pcl_cloud_rgb->points[i].r), |
| 233 | static_cast<ColorCompType>(pcl_cloud_rgb->points[i].g), |
| 234 | static_cast<ColorCompType>(pcl_cloud_rgb->points[i].b) ); |
| 235 | ccCloud.addColor(C); |
| 236 | } |
| 237 | |
| 238 | ccCloud.showColors(true); |
| 239 | |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | bool pcl2cc::CopyScalarField( const PCLCloud& pclCloud, |
| 244 | const std::string& sfName, |
| 245 | ccPointCloud& ccCloud, |
| 246 | bool overwriteIfExist/*=true*/) |
| 247 | { |
| 248 | //if the input field already exists... |
| 249 | int id = ccCloud.getScalarFieldIndexByName(sfName.c_str()); |
| 250 | if (id >= 0) |
| 251 | { |
| 252 | if (overwriteIfExist) |
| 253 | //we simply delete it |
| 254 | ccCloud.deleteScalarField(id); |
| 255 | else |
| 256 | //we keep it as is |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | unsigned pointCount = ccCloud.size(); |
| 261 | |
| 262 | //create new scalar field |
| 263 | ccScalarField* newSF = new ccScalarField(sfName.c_str()); |
| 264 | if (!newSF->reserveSafe(pointCount)) |
| 265 | { |
| 266 | newSF->release(); |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | //get PCL field |
| 271 | int fieldIndex = pcl::getFieldIndex(pclCloud, sfName); |
| 272 | if (fieldIndex < 0) |
| 273 | { |
| 274 | newSF->release(); |
| 275 | return false; |
| 276 | } |
| 277 | const PCLScalarField& pclField = pclCloud.fields[fieldIndex]; |
| 278 | //temporary change the name of the given field to something else -> S5c4laR should be a pretty uncommon name, |
| 279 | const_cast<PCLScalarField&>(pclField).name = "S5c4laR"; |
| 280 | |
| 281 | switch (pclField.datatype) |
| 282 | { |
| 283 | case PCLScalarField::FLOAT32: |
| 284 | { |
| 285 | pcl::PointCloud<FloatScalar> pclScalar; |
| 286 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 287 | |
| 288 | for (unsigned i = 0; i < pointCount; ++i) |
| 289 | { |
| 290 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 291 | newSF->addElement(scalar); |
| 292 | } |
| 293 | } |
| 294 | break; |
| 295 | |
| 296 | case PCLScalarField::FLOAT64: |
| 297 | { |
| 298 | pcl::PointCloud<DoubleScalar> pclScalar; |
| 299 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 300 | |
| 301 | for (unsigned i = 0; i < pointCount; ++i) |
| 302 | { |
| 303 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 304 | newSF->addElement(scalar); |
| 305 | } |
| 306 | } |
| 307 | break; |
| 308 | |
| 309 | case PCLScalarField::INT8: |
| 310 | { |
| 311 | pcl::PointCloud<Int8Scalar> pclScalar; |
| 312 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 313 | |
| 314 | for (unsigned i = 0; i < pointCount; ++i) |
| 315 | { |
| 316 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 317 | newSF->addElement(scalar); |
| 318 | } |
| 319 | } |
| 320 | break; |
| 321 | |
| 322 | case PCLScalarField::UINT8: |
| 323 | { |
| 324 | pcl::PointCloud<UInt8Scalar> pclScalar; |
| 325 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 326 | |
| 327 | for (unsigned i = 0; i < pointCount; ++i) |
| 328 | { |
| 329 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 330 | newSF->addElement(scalar); |
| 331 | } |
| 332 | } |
| 333 | break; |
| 334 | |
| 335 | case PCLScalarField::INT16: |
| 336 | { |
| 337 | pcl::PointCloud<ShortScalar> pclScalar; |
| 338 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 339 | |
| 340 | for (unsigned i = 0; i < pointCount; ++i) |
| 341 | { |
| 342 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 343 | newSF->addElement(scalar); |
| 344 | } |
| 345 | } |
| 346 | break; |
| 347 | |
| 348 | case PCLScalarField::UINT16: |
| 349 | { |
| 350 | pcl::PointCloud<UShortScalar> pclScalar; |
| 351 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 352 | |
| 353 | for (unsigned i = 0; i < pointCount; ++i) |
| 354 | { |
| 355 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 356 | newSF->addElement(scalar); |
| 357 | } |
| 358 | } |
| 359 | break; |
| 360 | |
| 361 | case PCLScalarField::UINT32: |
| 362 | { |
| 363 | pcl::PointCloud<UIntScalar> pclScalar; |
| 364 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 365 | |
| 366 | for (unsigned i = 0; i < pointCount; ++i) |
| 367 | { |
| 368 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 369 | newSF->addElement(scalar); |
| 370 | } |
| 371 | } |
| 372 | break; |
| 373 | |
| 374 | case PCLScalarField::INT32: |
| 375 | { |
| 376 | pcl::PointCloud<IntScalar> pclScalar; |
| 377 | FROM_PCL_CLOUD(pclCloud, pclScalar); |
| 378 | |
| 379 | for (unsigned i = 0; i < pointCount; ++i) |
| 380 | { |
| 381 | ScalarType scalar = static_cast<ScalarType>(pclScalar.points[i].S5c4laR); |
| 382 | newSF->addElement(scalar); |
| 383 | } |
| 384 | } |
| 385 | break; |
| 386 | |
| 387 | default: |
| 388 | ccLog::Warning(QString("[PCL] Field with an unmanaged type (= %1)").arg(pclField.datatype)); |
| 389 | newSF->release(); |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | newSF->computeMinAndMax(); |
| 394 | int sfIdex = ccCloud.addScalarField(newSF); |
| 395 | ccCloud.setCurrentDisplayedScalarField(sfIdex); |
| 396 | ccCloud.showSF(true); |
| 397 | |
| 398 | //restore old name for the scalar field |
| 399 | const_cast<PCLScalarField&>(pclField).name = sfName; |
| 400 | |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | ccPointCloud* pcl2cc::Convert( const PCLCloud& pclCloud, |
| 405 | ccGLMatrixd* _transform/*=nullptr*/, |
| 406 | FileIOFilter::LoadParameters* _loadParameters/*=nullptr*/) |
| 407 | { |
| 408 | //retrieve the valid fields |
| 409 | std::list<std::string> fields; |
| 410 | bool hasIntegerCoordinates = false; |
| 411 | uint8_t coordinateType = 0; |
| 412 | for (const auto& field : pclCloud.fields) |
| 413 | { |
| 414 | if (field.name != "_") //PCL padding fields |
| 415 | { |
| 416 | fields.push_back(field.name); |
| 417 | } |
| 418 | |
| 419 | if (coordinateType == 0 |
| 420 | && (field.name == "x" || field.name == "y" || field.name == "z")) |
| 421 | { |
| 422 | coordinateType = field.datatype; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | //begin with checks and conversions |
| 427 | //be sure we have x, y, and z fields |
| 428 | if (!ExistField(pclCloud, "x") || !ExistField(pclCloud, "y") || !ExistField(pclCloud, "z")) |
| 429 | { |
| 430 | return nullptr; |
| 431 | } |
| 432 | |
| 433 | //create cloud |
| 434 | ccPointCloud* ccCloud = new ccPointCloud(); |
| 435 | size_t expectedPointCount = GetNumberOfPoints(pclCloud); |
| 436 | if (expectedPointCount != 0) |
| 437 | { |
| 438 | //push points inside |
| 439 | if (!CopyXYZ(pclCloud, *ccCloud, coordinateType, _transform, _loadParameters)) |
| 440 | { |
| 441 | delete ccCloud; |
| 442 | return nullptr; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | //remove x,y,z fields from the vector of field names |
| 447 | fields.remove("x"); |
| 448 | fields.remove("y"); |
| 449 | fields.remove("z"); |
| 450 | |
| 451 | //do we have normals? |
| 452 | if (ExistField(pclCloud, "normal_x") || ExistField(pclCloud, "normal_y") || ExistField(pclCloud, "normal_z")) |
| 453 | { |
| 454 | CopyNormals(pclCloud, *ccCloud); |
| 455 | |
| 456 | //remove the corresponding fields |
| 457 | fields.remove("normal_x"); |
| 458 | fields.remove("normal_y"); |
| 459 | fields.remove("normal_z"); |
| 460 | } |
| 461 | |
| 462 | //The same for colors |
| 463 | if (ExistField(pclCloud, "rgb")) |
| 464 | { |
| 465 | CopyRGB(pclCloud, *ccCloud); |
| 466 | |
| 467 | //remove the corresponding field |
| 468 | fields.remove("rgb"); |
| 469 | } |
| 470 | //The same for colors |
| 471 | else if (ExistField(pclCloud, "rgba")) |
| 472 | { |
| 473 | CopyRGB(pclCloud, *ccCloud); |
| 474 | |
| 475 | //remove the corresponding field |
| 476 | fields.remove("rgba"); |
| 477 | } |
| 478 | |
| 479 | //All the remaining fields will be stored as scalar fields |
| 480 | for (const std::string& name : fields) |
| 481 | { |
| 482 | CopyScalarField(pclCloud, name, *ccCloud); |
| 483 | } |
| 484 | |
| 485 | return ccCloud; |
| 486 | } |