| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: qCANUPO # |
| 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: UEB (UNIVERSITE EUROPEENNE DE BRETAGNE) / CNRS # |
| 15 |
//# # |
| 16 |
//########################################################################## |
| 17 |
|
| 18 |
#include "ccPointDescriptor.h" |
| 19 |
|
| 20 |
//qCC_db |
| 21 |
#include <ccPointCloud.h> |
| 22 |
#include <ccScalarField.h> |
| 23 |
|
| 24 |
//CCCoreLib |
| 25 |
#include <Neighbourhood.h> |
| 26 |
#include <Jacobi.h> |
| 27 |
|
| 28 |
//Qt |
| 29 |
#include <QMap> |
| 30 |
|
| 31 |
//system |
| 32 |
#include <fstream> |
| 33 |
|
| 34 |
/**** SCALE PARAMETERS COMPUTERS ****/ |
| 35 |
/* */ |
| 36 |
/* PUT THE CODE OF YOUR OWN BELOW */ |
| 37 |
/* THEN DECLARE IT IN THE "VAULT" */ |
| 38 |
/* (SEE ScaleParamsComputerVault) */ |
| 39 |
/* */ |
| 40 |
/************************************/ |
| 41 |
|
| 42 |
//useful constant |
| 43 |
static const double SQRT_3_DIV_2 = sqrt(3.0)/2; |
| 44 |
|
| 45 |
//! Per-scale "dimensionality" parameters computer (i.e. same as the original CANUPO suite) |
| 46 |
class DimensionalityScaleParamsComputer : public ScaleParamsComputer |
| 47 |
{ |
| 48 |
public: |
| 49 |
|
| 50 |
//! Default constructor |
| 51 |
DimensionalityScaleParamsComputer() : m_firstScale(true) {} |
| 52 |
|
| 53 |
//inherited from ScaleParamsComputer |
| 54 |
unsigned getID() const override { return DESC_DIMENSIONALITY; } |
| 55 |
|
| 56 |
//inherited from ScaleParamsComputer |
| 57 |
QString getName() const override { return "Dimensionality"; } |
| 58 |
|
| 59 |
//inherited from ScaleParamsComputer |
| 60 |
unsigned dimPerScale() const override { return 2; } |
| 61 |
|
| 62 |
//inherited from ScaleParamsComputer |
| 63 |
void reset() override |
| 64 |
{ |
| 65 |
//a = 1/3, b = 1/3 |
| 66 |
//c = 1 - a - b = 1/3 |
| 67 |
//x = b + c/2 = 1/2 |
| 68 |
//y = c * SQRT(3)/2 = SQRT(3)/2 * 1/3 |
| 69 |
|
| 70 |
m_defaultParams[0] = 0.5; |
| 71 |
m_defaultParams[1] = SQRT_3_DIV_2 / 3; |
| 72 |
|
| 73 |
m_firstScale = true; |
| 74 |
} |
| 75 |
|
| 76 |
//inherited from ScaleParamsComputer |
| 77 |
void computeScaleParams(CCCoreLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale) override |
| 78 |
{ |
| 79 |
//PCA analysis |
| 80 |
if (neighbors.size() >= 3) |
| 81 |
{ |
| 82 |
CCCoreLib::Neighbourhood Z(&neighbors); |
| 83 |
|
| 84 |
CCCoreLib::SquareMatrixd eigVectors; |
| 85 |
std::vector<double> eigValues; |
| 86 |
CCCoreLib::SquareMatrixd covarianceMatrix = Z.computeCovarianceMatrix(); |
| 87 |
if (CCCoreLib::Jacobi<double>::ComputeEigenValuesAndVectors(covarianceMatrix, eigVectors, eigValues, true)) |
| 88 |
{ |
| 89 |
CCCoreLib::Jacobi<double>::SortEigenValuesAndVectors(eigVectors, eigValues); //decreasing order of their associated eigenvalues |
| 90 |
|
| 91 |
double totalVariance = 0; |
| 92 |
CCVector3d sValues(0, 0, 0); |
| 93 |
{ |
| 94 |
// contrarily to Brodu's version, here we get directly the eigenvalues! |
| 95 |
for (unsigned j = 0; j < 3; ++j) |
| 96 |
{ |
| 97 |
sValues.u[j] = eigValues[j]; |
| 98 |
totalVariance += sValues.u[j]; |
| 99 |
} |
| 100 |
} |
| 101 |
if (totalVariance < CCCoreLib::ZERO_TOLERANCE_D) |
| 102 |
{ |
| 103 |
invalidScale = true; |
| 104 |
params[0] = m_defaultParams[0]; |
| 105 |
params[1] = m_defaultParams[1]; |
| 106 |
} |
| 107 |
sValues /= totalVariance; |
| 108 |
|
| 109 |
// Use barycentric coordinates : a for 1D, b for 2D and c for 3D |
| 110 |
// Formula on wikipedia page for barycentric coordinates |
| 111 |
// using directly the triangle in %variance space, they simplify a lot |
| 112 |
double a = std::min<double>(1.0, std::max<double>(0.0, sValues.x - sValues.y)); |
| 113 |
double b = std::min<double>(1.0, std::max<double>(0.0, 2 * sValues.x + 4 * sValues.y - 2.0)); |
| 114 |
double c = 1.0 - a - b; |
| 115 |
// see original Brodu's code for this transformation |
| 116 |
params[0] = static_cast<float>(b + c / 2); |
| 117 |
params[1] = static_cast<float>(c * SQRT_3_DIV_2); |
| 118 |
|
| 119 |
//save parameters for next scale |
| 120 |
m_defaultParams[0] = params[0]; |
| 121 |
m_defaultParams[1] = params[1]; |
| 122 |
m_firstScale = false; |
| 123 |
} |
| 124 |
else if (m_firstScale) //PCA failed at first scale?! |
| 125 |
{ |
| 126 |
invalidScale = true; |
| 127 |
params[0] = m_defaultParams[0]; |
| 128 |
params[1] = m_defaultParams[1]; |
| 129 |
} |
| 130 |
} |
| 131 |
else if (m_firstScale) //less than 3 points at the biggest scale?! |
| 132 |
{ |
| 133 |
invalidScale = true; |
| 134 |
params[0] = m_defaultParams[0]; |
| 135 |
params[1] = m_defaultParams[1]; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
protected: |
| 140 |
|
| 141 |
//! Default parameters (or last computed scale's ones!) |
| 142 |
float m_defaultParams[2]; |
| 143 |
//! First scale flag |
| 144 |
bool m_firstScale; |
| 145 |
}; |
| 146 |
|
| 147 |
#ifdef COMPILE_PRIVATE_CANUPO |
| 148 |
|
| 149 |
//! Per-scale "dimensionality" + mean scalar field value parameters computer |
| 150 |
class DimensionalityAndSFScaleParamsComputer : public ScaleParamsComputer |
| 151 |
{ |
| 152 |
public: |
| 153 |
|
| 154 |
//! Default constructor |
| 155 |
DimensionalityAndSFScaleParamsComputer() : m_firstScale(true) {} |
| 156 |
|
| 157 |
//inherited from ScaleParamsComputer |
| 158 |
unsigned getID() const override { return DESC_DIMENSIONALITY_SF; } |
| 159 |
|
| 160 |
//inherited from ScaleParamsComputer |
| 161 |
QString getName() const override { return "Dimensionality + SF"; } |
| 162 |
|
| 163 |
//inherited from ScaleParamsComputer |
| 164 |
unsigned dimPerScale() const override { return 3; } |
| 165 |
|
| 166 |
//inherited from ScaleParamsComputer |
| 167 |
bool needSF() const override { return true; } |
| 168 |
|
| 169 |
//inherited from ScaleParamsComputer |
| 170 |
void reset() override |
| 171 |
{ |
| 172 |
//a = 1/3, b = 1/3 |
| 173 |
//c = 1 - a - b = 1/3 |
| 174 |
//x = b + c/2 = 1/2 |
| 175 |
//y = c * SQRT(3)/2 = SQRT(3)/2 * 1/3 |
| 176 |
m_defaultParams[0] = 0.5; |
| 177 |
m_defaultParams[1] = SQRT_3_DIV_2 / 3; |
| 178 |
|
| 179 |
//mean SF value |
| 180 |
m_defaultParams[2] = 0.0; |
| 181 |
|
| 182 |
m_firstScale = true; |
| 183 |
} |
| 184 |
|
| 185 |
//inherited from ScaleParamsComputer |
| 186 |
void computeScaleParams(CCCoreLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale) override |
| 187 |
{ |
| 188 |
//PCA analysis |
| 189 |
if (neighbors.size() >= 3) |
| 190 |
{ |
| 191 |
//first compute the mean SF value |
| 192 |
unsigned validCount = 0; |
| 193 |
double meanVal = 0; |
| 194 |
for (unsigned i = 0; i < neighbors.size(); ++i) |
| 195 |
{ |
| 196 |
ScalarType val = neighbors.getPointScalarValue(i); |
| 197 |
if (CCCoreLib::ScalarField::ValidValue(val)) |
| 198 |
{ |
| 199 |
meanVal += val; |
| 200 |
++validCount; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if (validCount) |
| 205 |
{ |
| 206 |
m_defaultParams[2] = static_cast<float>(meanVal / validCount); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
//not enough (valid) SF values |
| 211 |
invalidScale = true; |
| 212 |
params[0] = m_defaultParams[0]; |
| 213 |
params[1] = m_defaultParams[1]; |
| 214 |
params[2] = m_defaultParams[2]; |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
CCCoreLib::Neighbourhood Z(&neighbors); |
| 219 |
|
| 220 |
CCCoreLib::SquareMatrixd eigVectors; |
| 221 |
std::vector<double> eigValues; |
| 222 |
CCCoreLib::SquareMatrixd covarianceMatrix = Z.computeCovarianceMatrix(); |
| 223 |
if (CCCoreLib::Jacobi<double>::ComputeEigenValuesAndVectors(covarianceMatrix, eigVectors, eigValues, true)) |
| 224 |
{ |
| 225 |
CCCoreLib::Jacobi<double>::SortEigenValuesAndVectors(eigVectors, eigValues); //decreasing order of their associated eigenvalues |
| 226 |
|
| 227 |
double totalVariance = 0; |
| 228 |
CCVector3d sValues(0, 0, 0); |
| 229 |
{ |
| 230 |
// contrarily to Brodu's version, here we get directly the eigenvalues! |
| 231 |
for (unsigned j = 0; j < 3; ++j) |
| 232 |
{ |
| 233 |
sValues.u[j] = eigValues[j]; |
| 234 |
totalVariance += sValues.u[j]; |
| 235 |
} |
| 236 |
} |
| 237 |
assert(totalVariance != 0); |
| 238 |
sValues /= totalVariance; |
| 239 |
|
| 240 |
// Use barycentric coordinates : a for 1D, b for 2D and c for 3D |
| 241 |
// Formula on wikipedia page for barycentric coordinates |
| 242 |
// using directly the triangle in %variance space, they simplify a lot |
| 243 |
double a = std::min<double>(1.0, std::max<double>(0.0, sValues.x - sValues.y)); |
| 244 |
double b = std::min<double>(1.0, std::max<double>(0.0, 2*sValues.x + 4*sValues.y - 2.0)); |
| 245 |
double c = 1.0 - a - b; |
| 246 |
// see original Brodu's code for this transformation |
| 247 |
params[0] = static_cast<float>(b + c/2); |
| 248 |
params[1] = static_cast<float>(c * SQRT_3_DIV_2); |
| 249 |
|
| 250 |
//save parameters for next scale |
| 251 |
m_defaultParams[0] = params[0]; |
| 252 |
m_defaultParams[1] = params[1]; |
| 253 |
m_defaultParams[2] = params[2]; |
| 254 |
m_firstScale = false; |
| 255 |
} |
| 256 |
else if (m_firstScale) //PCA failed at first scale?! |
| 257 |
{ |
| 258 |
invalidScale = true; |
| 259 |
params[0] = m_defaultParams[0]; |
| 260 |
params[1] = m_defaultParams[1]; |
| 261 |
params[2] = m_defaultParams[2]; |
| 262 |
} |
| 263 |
} |
| 264 |
else if (m_firstScale) //less than 3 points at the biggest scale?! |
| 265 |
{ |
| 266 |
invalidScale = true; |
| 267 |
params[0] = m_defaultParams[0]; |
| 268 |
params[1] = m_defaultParams[1]; |
| 269 |
params[2] = m_defaultParams[2]; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
protected: |
| 274 |
|
| 275 |
//! Default parameters (or last computed scale's ones!) |
| 276 |
float m_defaultParams[3]; |
| 277 |
//! First scale flag |
| 278 |
bool m_firstScale; |
| 279 |
}; |
| 280 |
|
| 281 |
//! Per scale curvature computer |
| 282 |
class CurvatureScaleParamsComputer : public ScaleParamsComputer |
| 283 |
{ |
| 284 |
public: |
| 285 |
|
| 286 |
//! Default constructor |
| 287 |
CurvatureScaleParamsComputer() : m_firstScale(true) {} |
| 288 |
|
| 289 |
//inherited from ScaleParamsComputer |
| 290 |
unsigned getID() const override { return DESC_CURVATURE; } |
| 291 |
|
| 292 |
//inherited from ScaleParamsComputer |
| 293 |
QString getName() const override { return "Gaussian curvature"; } |
| 294 |
|
| 295 |
//inherited from ScaleParamsComputer |
| 296 |
unsigned dimPerScale() const override { return 1; } |
| 297 |
|
| 298 |
//inherited from ScaleParamsComputer |
| 299 |
void reset() override |
| 300 |
{ |
| 301 |
m_defaultParams[0] = 0; |
| 302 |
m_firstScale = true; |
| 303 |
} |
| 304 |
|
| 305 |
//inherited from ScaleParamsComputer |
| 306 |
void computeScaleParams(CCCoreLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale) override |
| 307 |
{ |
| 308 |
//Curvature |
| 309 |
if (neighbors.size() >= 6) |
| 310 |
{ |
| 311 |
CCCoreLib::Neighbourhood Z(&neighbors); |
| 312 |
params[0] = Z.computeCurvature(*neighbors.getPoint(0), CCCoreLib::Neighbourhood::GAUSSIAN_CURV); |
| 313 |
|
| 314 |
//save parameters for next scale |
| 315 |
m_defaultParams[0] = params[0]; |
| 316 |
m_firstScale = false; |
| 317 |
} |
| 318 |
else if (m_firstScale) //less than 6 points at the biggest scale?! |
| 319 |
{ |
| 320 |
invalidScale = true; |
| 321 |
params[0] = m_defaultParams[0]; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
protected: |
| 326 |
|
| 327 |
//! Default parameters (or last computed scale's ones!) |
| 328 |
float m_defaultParams[1]; |
| 329 |
//! First scale flag |
| 330 |
bool m_firstScale; |
| 331 |
}; |
| 332 |
|
| 333 |
//! Custom parameters computer (TO BE REIMPLEMENTED) |
| 334 |
/*class CustomScaleParamsComputer : public ScaleParamsComputer |
| 335 |
{ |
| 336 |
public: |
| 337 |
|
| 338 |
//! Default constructor |
| 339 |
CustomScaleParamsComputer() : m_firstScale(true) {} |
| 340 |
|
| 341 |
//inherited from ScaleParamsComputer |
| 342 |
unsigned getID() const override { return DESC_CUSTOM; } |
| 343 |
|
| 344 |
//inherited from ScaleParamsComputer |
| 345 |
QString getName() const override { return "Custom"; } |
| 346 |
|
| 347 |
//inherited from ScaleParamsComputer |
| 348 |
unsigned dimPerScale() const override { return 1; } |
| 349 |
|
| 350 |
//inherited from ScaleParamsComputer |
| 351 |
void reset() override |
| 352 |
{ |
| 353 |
//FIXME |
| 354 |
m_defaultParams[0] = 0; |
| 355 |
m_firstScale = true; |
| 356 |
} |
| 357 |
|
| 358 |
//inherited from ScaleParamsComputer |
| 359 |
void computeScaleParams(CCCoreLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale) override |
| 360 |
{ |
| 361 |
//Curvature |
| 362 |
if (neighbors.size() >= ?) |
| 363 |
{ |
| 364 |
//do something and set the right values for 'params' |
| 365 |
|
| 366 |
//save parameters for next scale |
| 367 |
m_defaultParams[0] = params[0]; |
| 368 |
m_defaultParams[1] = params[1]; |
| 369 |
... |
| 370 |
m_firstScale = false; |
| 371 |
} |
| 372 |
else if (m_firstScale) //less than 6 points at the biggest scale?! |
| 373 |
{ |
| 374 |
invalidScale = true; |
| 375 |
params[0] = m_defaultParams[0]; |
| 376 |
params[1] = m_defaultParams[1]; |
| 377 |
... |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
protected: |
| 382 |
|
| 383 |
//! Default parameters (or last computed scale's ones!) |
| 384 |
float m_defaultParams[?]; |
| 385 |
//! First scale flag |
| 386 |
bool m_firstScale; |
| 387 |
}; |
| 388 |
//*/ |
| 389 |
|
| 390 |
#endif |
| 391 |
|
| 392 |
/**** SCALE PARAMETERS COMPUTERS VAULT ****/ |
| 393 |
/* */ |
| 394 |
/* DON'T FORGET TO ADD YOUR OWN HERE! */ |
| 395 |
/* */ |
| 396 |
/******************************************/ |
| 397 |
|
| 398 |
struct ScaleParamsComputerVault |
| 399 |
{ |
| 400 |
//! Default constructor |
| 401 |
ScaleParamsComputerVault() |
| 402 |
{ |
| 403 |
//DEFAULT DESCRIPTOR |
| 404 |
map.insert(DESC_DIMENSIONALITY, new DimensionalityScaleParamsComputer); |
| 405 |
|
| 406 |
#ifdef COMPILE_PRIVATE_CANUPO |
| 407 |
map.insert(DESC_DIMENSIONALITY_SF, new DimensionalityAndSFScaleParamsComputer); |
| 408 |
map.insert(DESC_CURVATURE, new CurvatureScaleParamsComputer); |
| 409 |
//ADD YOUR OWN DESCRIPTOR COMPUTERS HERE! |
| 410 |
//map.insert(DESC_CUSTOM, new CustomScaleParamsComputer); |
| 411 |
//map.insert(DESC_XXX, new XXXScaleParamsComputer); |
| 412 |
#endif |
| 413 |
} |
| 414 |
|
| 415 |
//! Destructor |
| 416 |
~ScaleParamsComputerVault() |
| 417 |
{ |
| 418 |
//auto delete computers |
| 419 |
for (QMap<unsigned, ScaleParamsComputer*>::Iterator it = map.begin(); it != map.end(); ++it) |
| 420 |
{ |
| 421 |
delete it.value(); |
| 422 |
it.value() = 0; |
| 423 |
} |
| 424 |
map.clear(); |
| 425 |
} |
| 426 |
|
| 427 |
QMap<unsigned, ScaleParamsComputer*> map; |
| 428 |
}; |
| 429 |
|
| 430 |
|
| 431 |
/********** OTHER STUFF ***********/ |
| 432 |
/* */ |
| 433 |
/* NO NEED TO LOOK AT THEM ;) */ |
| 434 |
/* */ |
| 435 |
/**********************************/ |
| 436 |
|
| 437 |
// Unique static instance |
| 438 |
static ScaleParamsComputerVault s_vault; |
| 439 |
|
| 440 |
unsigned ScaleParamsComputer::AvailableCount() |
| 441 |
{ |
| 442 |
return static_cast<unsigned>(s_vault.map.size()); |
| 443 |
} |
| 444 |
|
| 445 |
ScaleParamsComputer* ScaleParamsComputer::GetByID(unsigned descID) |
| 446 |
{ |
| 447 |
if (s_vault.map.contains(descID)) |
| 448 |
{ |
| 449 |
return s_vault.map[descID]; |
| 450 |
} |
| 451 |
else |
| 452 |
{ |
| 453 |
//couldn't find the corresponding descriptor! |
| 454 |
assert(false); |
| 455 |
return nullptr; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
ScaleParamsComputer* ScaleParamsComputer::GetByIndex(unsigned index) |
| 460 |
{ |
| 461 |
QMap<unsigned, ScaleParamsComputer*>::Iterator it = s_vault.map.begin(); |
| 462 |
for (unsigned i=0; i<index; ++i) |
| 463 |
{ |
| 464 |
++it; |
| 465 |
assert(it != s_vault.map.end()); |
| 466 |
} |
| 467 |
return it.value(); |
| 468 |
} |
| 469 |
|
| 470 |
QByteArray CorePointDescSet::toByteArray() const |
| 471 |
{ |
| 472 |
int scaleCount = static_cast<int>(m_scales.size()); |
| 473 |
int descCount = static_cast<int>(size()); |
| 474 |
|
| 475 |
if (scaleCount == 0 || descCount == 0) |
| 476 |
return QByteArray(); |
| 477 |
|
| 478 |
int totalSize = 4 * sizeof(int) /*header*/ + (scaleCount + descCount * scaleCount * m_dimPerScale) * sizeof(float) /*data*/; |
| 479 |
|
| 480 |
try |
| 481 |
{ |
| 482 |
QByteArray data(totalSize, Qt::Uninitialized); |
| 483 |
|
| 484 |
if (data.capacity() < totalSize) //not enough memory? |
| 485 |
return {}; |
| 486 |
|
| 487 |
char* buffer = data.data(); |
| 488 |
|
| 489 |
//header |
| 490 |
*reinterpret_cast<int*>(buffer) = scaleCount; |
| 491 |
buffer += sizeof(int); |
| 492 |
*reinterpret_cast<int*>(buffer) = descCount; |
| 493 |
buffer += sizeof(int); |
| 494 |
*reinterpret_cast<int*>(buffer) = static_cast<int>(m_descriptorID); |
| 495 |
buffer += sizeof(int); |
| 496 |
*reinterpret_cast<int*>(buffer) = static_cast<int>(m_dimPerScale); |
| 497 |
buffer += sizeof(int); |
| 498 |
|
| 499 |
//scales |
| 500 |
{ |
| 501 |
for (int i = 0; i < scaleCount; ++i) |
| 502 |
{ |
| 503 |
*reinterpret_cast<float*>(buffer) = m_scales[i]; |
| 504 |
buffer += sizeof(float); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
//descriptors |
| 509 |
{ |
| 510 |
for (int j = 0; j < descCount; ++j) |
| 511 |
{ |
| 512 |
const CorePointDesc& desc = at(j); |
| 513 |
assert(desc.params.size() == static_cast<size_t>(scaleCount) * m_dimPerScale); |
| 514 |
for (size_t i = 0; i < desc.params.size(); ++i) |
| 515 |
{ |
| 516 |
*reinterpret_cast<float*>(buffer) = desc.params[i]; |
| 517 |
buffer += sizeof(float); |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
//first scales |
| 523 |
return data; |
| 524 |
} |
| 525 |
catch (const std::bad_alloc&) |
| 526 |
{ |
| 527 |
ccLog::Warning("Not enough memory to convert the core point descriptors to a QByteArray"); |
| 528 |
return {}; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
bool CorePointDescSet::fromByteArray(const QByteArray& data) |
| 533 |
{ |
| 534 |
if (data.size() < 2*sizeof(int)) |
| 535 |
return false; |
| 536 |
|
| 537 |
const char* buffer = data.data(); |
| 538 |
|
| 539 |
//header |
| 540 |
int scaleCount = *reinterpret_cast<const int*>(buffer); |
| 541 |
buffer += sizeof(int); |
| 542 |
int descCount = *reinterpret_cast<const int*>(buffer); |
| 543 |
buffer += sizeof(int); |
| 544 |
int descriptorID = *reinterpret_cast<const int*>(buffer); |
| 545 |
buffer += sizeof(int); |
| 546 |
if (descriptorID <= 0) |
| 547 |
return false; |
| 548 |
m_descriptorID = static_cast<unsigned>(descriptorID); |
| 549 |
int dimPerScale = *reinterpret_cast<const int*>(buffer); |
| 550 |
buffer += sizeof(int); |
| 551 |
if (dimPerScale <= 0) |
| 552 |
return false; |
| 553 |
m_dimPerScale = static_cast<unsigned>(dimPerScale); |
| 554 |
|
| 555 |
if (scaleCount == 0 || descCount == 0) |
| 556 |
return false; |
| 557 |
|
| 558 |
//check that the array is big enough! |
| 559 |
int totalSize = 4 * sizeof(int) /*header*/ + (scaleCount + descCount * scaleCount * m_dimPerScale) * sizeof(float) /*data*/; |
| 560 |
if (data.size() < totalSize) |
| 561 |
return false; |
| 562 |
|
| 563 |
//check that we have enough memory |
| 564 |
std::vector<float> scales; |
| 565 |
try |
| 566 |
{ |
| 567 |
resize(descCount); |
| 568 |
scales.resize(scaleCount); |
| 569 |
} |
| 570 |
catch (const std::bad_alloc&) |
| 571 |
{ |
| 572 |
//not enough memory |
| 573 |
return false; |
| 574 |
} |
| 575 |
|
| 576 |
//scales |
| 577 |
{ |
| 578 |
for (int i = 0; i < scaleCount; ++i) |
| 579 |
{ |
| 580 |
scales[i] = *reinterpret_cast<const float*>(buffer); |
| 581 |
buffer += sizeof(float); |
| 582 |
} |
| 583 |
|
| 584 |
//check that we have even more memory ;) |
| 585 |
if (!setScales(scales)) |
| 586 |
return false; |
| 587 |
} |
| 588 |
|
| 589 |
//descriptors |
| 590 |
{ |
| 591 |
for (int j=0; j<descCount; ++j) |
| 592 |
{ |
| 593 |
CorePointDesc& desc = at(j); |
| 594 |
assert(desc.params.size() == static_cast<size_t>(scaleCount) * m_dimPerScale); |
| 595 |
for (size_t i = 0; i < static_cast<size_t>(scaleCount)*m_dimPerScale; ++i) |
| 596 |
{ |
| 597 |
desc.params[i] = *reinterpret_cast<const float*>(buffer); |
| 598 |
buffer += sizeof(float); |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
return true; |
| 604 |
} |
| 605 |
|
| 606 |
bool CorePointDescSet::setScales(const std::vector<float>& scales) |
| 607 |
{ |
| 608 |
assert(size() != 0); |
| 609 |
|
| 610 |
//same size as previous scales? Easy... |
| 611 |
if (scales.size() == m_scales.size()) |
| 612 |
{ |
| 613 |
m_scales = scales; |
| 614 |
return true; |
| 615 |
} |
| 616 |
|
| 617 |
//otherwise we must resize the scales vector AND the 'params' structures! |
| 618 |
try |
| 619 |
{ |
| 620 |
m_scales = scales; |
| 621 |
size_t scaleCount = m_scales.size(); |
| 622 |
|
| 623 |
size_t paramPerDesc = scaleCount*m_dimPerScale; |
| 624 |
for (size_t i = 0; i < size(); ++i) |
| 625 |
{ |
| 626 |
at(i).params.resize(paramPerDesc); |
| 627 |
} |
| 628 |
} |
| 629 |
catch (const std::bad_alloc&) |
| 630 |
{ |
| 631 |
//not enough memory! |
| 632 |
return false; |
| 633 |
} |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
bool CorePointDescSet::loadFromMSC(QString filename, QString& error, ccPointCloud* corePoints/*=nullptr*/) |
| 639 |
{ |
| 640 |
error.clear(); |
| 641 |
|
| 642 |
std::ifstream mscfile(qPrintable(filename), std::ifstream::binary); //DGM: warning, toStdString doesn't preserve "local" characters |
| 643 |
|
| 644 |
if (!mscfile.is_open()) |
| 645 |
{ |
| 646 |
error = "Failed to open input file"; |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
// read the file header |
| 651 |
int ncorepoints; |
| 652 |
mscfile.read((char*)&ncorepoints,sizeof(ncorepoints)); |
| 653 |
int nscales_msc; |
| 654 |
mscfile.read((char*)&nscales_msc, sizeof(int)); |
| 655 |
|
| 656 |
//default for CANUPO MSC files! |
| 657 |
m_descriptorID = DESC_DIMENSIONALITY; |
| 658 |
m_dimPerScale = 2; |
| 659 |
|
| 660 |
// allocate memory |
| 661 |
{ |
| 662 |
try |
| 663 |
{ |
| 664 |
resize(ncorepoints); |
| 665 |
} |
| 666 |
catch (const std::bad_alloc&) |
| 667 |
{ |
| 668 |
//not enough memory |
| 669 |
error = "Not enough memory to load core points"; |
| 670 |
return false; |
| 671 |
} |
| 672 |
|
| 673 |
if (corePoints) |
| 674 |
{ |
| 675 |
//not enough memory to load core points! |
| 676 |
if (!corePoints->reserve(ncorepoints)) |
| 677 |
corePoints = nullptr; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
//read scales |
| 682 |
{ |
| 683 |
std::vector<float> scales; |
| 684 |
try |
| 685 |
{ |
| 686 |
scales.resize(nscales_msc); |
| 687 |
} |
| 688 |
catch (const std::bad_alloc&) |
| 689 |
{ |
| 690 |
//not enough memory |
| 691 |
error = "Not enough memory to load scales"; |
| 692 |
return false; |
| 693 |
} |
| 694 |
|
| 695 |
for (int si = 0; si < nscales_msc; ++si) |
| 696 |
mscfile.read((char*)&scales[si], sizeof(float)); |
| 697 |
|
| 698 |
if (!setScales(scales)) //automatically resize the descriptors 'params' structure |
| 699 |
{ |
| 700 |
error = "Not enough memory to load scales"; |
| 701 |
return false; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
// now load the points and multiscale information from the msc file. |
| 706 |
// Put the points in the cloud, keep the multiscale information in a separate vector matched by point index |
| 707 |
int ptnparams; |
| 708 |
mscfile.read((char*)&ptnparams, sizeof(int)); |
| 709 |
|
| 710 |
std::vector<CCCoreLib::ScalarField*> paramsSf(3,nullptr); |
| 711 |
if (corePoints) |
| 712 |
{ |
| 713 |
//above 3, ptnparams contains additional scalars |
| 714 |
for (int i=3; i<ptnparams; ++i) |
| 715 |
{ |
| 716 |
int sfIdx = corePoints->addScalarField(qPrintable(QString("scalar #%1").arg(i-2))); |
| 717 |
paramsSf.push_back(sfIdx >= 0 ? corePoints->getScalarField(sfIdx) : nullptr); |
| 718 |
|
| 719 |
if (sfIdx < 0) |
| 720 |
{ |
| 721 |
//just a warning in fact |
| 722 |
error = "Not enough memory to import additional scalars! (they are not used for classification anyway)"; |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
//vector<float> avg_ndist_max_scale(ncorepoints); |
| 728 |
for (int pt=0; pt<ncorepoints; ++pt) |
| 729 |
{ |
| 730 |
float x = 0.0f; |
| 731 |
float y = 0.0f; |
| 732 |
float z = 0.0f; |
| 733 |
mscfile.read((char*)&x, sizeof(float)); |
| 734 |
mscfile.read((char*)&y, sizeof(float)); |
| 735 |
mscfile.read((char*)&z, sizeof(float)); |
| 736 |
if (corePoints) |
| 737 |
corePoints->addPoint(CCVector3(x,y,z)); |
| 738 |
if (ptnparams>=4) |
| 739 |
{ |
| 740 |
float dummy; |
| 741 |
mscfile.read((char*)&dummy, sizeof(float)); //already ignored in Brodu's code... |
| 742 |
|
| 743 |
for (int i=4; i<ptnparams; ++i) |
| 744 |
{ |
| 745 |
float param; |
| 746 |
mscfile.read((char*)¶m, sizeof(float)); |
| 747 |
|
| 748 |
if (static_cast<int>(paramsSf.size()) > i) |
| 749 |
paramsSf[i]->addElement(static_cast<ScalarType>(param)); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
for (int s=0; s<nscales_msc; ++s) |
| 754 |
{ |
| 755 |
float a = 0.0f; |
| 756 |
float b = 0.0f; |
| 757 |
mscfile.read((char*)(&a), sizeof(float)); |
| 758 |
mscfile.read((char*)(&b), sizeof(float)); |
| 759 |
|
| 760 |
// see original Brodu's code for this transformation |
| 761 |
float c = 1.0f - a - b; |
| 762 |
float x = b + c/2; |
| 763 |
float y = c * sqrt(3.0f)/2; |
| 764 |
at(pt).params[s*2 ] = x; |
| 765 |
at(pt).params[s*2+1] = y; |
| 766 |
} |
| 767 |
// we don't care for number of neighbors at max and min scales |
| 768 |
{ |
| 769 |
int dummyInt; |
| 770 |
for (int i=0; i<nscales_msc; ++i) |
| 771 |
mscfile.read((char*)&dummyInt, sizeof(int)); |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
mscfile.close(); |
| 776 |
|
| 777 |
//take care of scalar fields |
| 778 |
if (corePoints) |
| 779 |
{ |
| 780 |
bool first = true; |
| 781 |
for (size_t i=3; i<paramsSf.size(); ++i) |
| 782 |
{ |
| 783 |
if (paramsSf[i]) |
| 784 |
{ |
| 785 |
paramsSf[i]->computeMinAndMax(); |
| 786 |
if (first) |
| 787 |
{ |
| 788 |
corePoints->setCurrentDisplayedScalarField(corePoints->getScalarFieldIndexByName(paramsSf[i]->getName())); |
| 789 |
corePoints->showSF(true); |
| 790 |
first = true; |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
return true; |
| 797 |
} |