| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: qManualSeg # |
| 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 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 |
//# Cyberbuild,2020 # |
| 15 |
//# v1.2 # |
| 16 |
//# # |
| 17 |
//########################################################################## |
| 18 |
|
| 19 |
#include "qManualSeg.h" |
| 20 |
|
| 21 |
//PCL |
| 22 |
#include <pcl/point_cloud.h> |
| 23 |
#include <pcl/kdtree/kdtree_flann.h> |
| 24 |
|
| 25 |
//Qt |
| 26 |
#include <QtGui> |
| 27 |
#include <QSettings> |
| 28 |
#include <QFileInfo> |
| 29 |
#include <QFile> |
| 30 |
#include <QMessageBox> |
| 31 |
#include <QTextStream> |
| 32 |
#include <QMainWindow> |
| 33 |
|
| 34 |
//qCC_db |
| 35 |
#include <ccFileUtils.h> |
| 36 |
#include <ccHObjectCaster.h> |
| 37 |
#include <ccPointCloud.h> |
| 38 |
#include <ccProgressDialog.h> |
| 39 |
#include <ccMesh.h> |
| 40 |
#include <ccPolyline.h> |
| 41 |
#include <ccCone.h> |
| 42 |
#include <ccScalarField.h> |
| 43 |
#include <ccGLWindowInterface.h> |
| 44 |
#include <ccGenericPointCloud.h> |
| 45 |
|
| 46 |
//CCCoreLib |
| 47 |
#include <CloudSamplingTools.h> |
| 48 |
#include <ManualSegmentationTools.h> |
| 49 |
#include <SquareMatrix.h> |
| 50 |
#include <CCConst.h> |
| 51 |
|
| 52 |
//OpenCV |
| 53 |
#include <opencv2/opencv.hpp> |
| 54 |
#include <opencv2/imgproc/imgproc.hpp> |
| 55 |
#include <opencv2/highgui/highgui.hpp> |
| 56 |
|
| 57 |
//System |
| 58 |
#include <iostream> |
| 59 |
#include <fstream> |
| 60 |
#include <random> |
| 61 |
#include <algorithm> |
| 62 |
#include <math.h> |
| 63 |
|
| 64 |
using namespace std; |
| 65 |
using namespace cv; |
| 66 |
|
| 67 |
ccManualSeg::ccManualSeg( QObject *parent ) |
| 68 |
: QObject( parent ) |
| 69 |
, ccStdPluginInterface( ":/CC/plugin/qManualSeg/info.json" ) |
| 70 |
, m_action(nullptr) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
QList<QAction*> ccManualSeg::getActions() |
| 75 |
{ |
| 76 |
if ( !m_action ) |
| 77 |
{ |
| 78 |
m_action = new QAction( getName(), this ); |
| 79 |
m_action->setToolTip( getDescription() ); |
| 80 |
m_action->setIcon(QIcon(QString::fromUtf8(":/CC/plugin/qManualSeg/cyberbuildIcon.png"))); |
| 81 |
|
| 82 |
// Connect appropriate signal |
| 83 |
connect( m_action, &QAction::triggered, this, &ccManualSeg::doAction ); |
| 84 |
} |
| 85 |
|
| 86 |
return { m_action }; |
| 87 |
} |
| 88 |
|
| 89 |
vector<unsigned> ccManualSeg::pointIdx(ccPointCloud* cloud, ccPolyline* poly) const |
| 90 |
{ |
| 91 |
if (!cloud) |
| 92 |
{ |
| 93 |
assert(false); |
| 94 |
return {}; |
| 95 |
} |
| 96 |
|
| 97 |
if (!poly) |
| 98 |
{ |
| 99 |
ccLog::Error("No polyline defined!"); |
| 100 |
return {}; |
| 101 |
} |
| 102 |
|
| 103 |
if (!poly->isClosed()) |
| 104 |
{ |
| 105 |
ccLog::Error("Define and/or close the segmentation polygon first! (right click to close)"); |
| 106 |
return {}; |
| 107 |
} |
| 108 |
|
| 109 |
ccGLWindowInterface* win = m_app->getActiveGLWindow(); |
| 110 |
if (!win) |
| 111 |
{ |
| 112 |
assert(false); |
| 113 |
return {}; |
| 114 |
} |
| 115 |
|
| 116 |
//viewing parameters |
| 117 |
ccGLCameraParameters camera; |
| 118 |
win->getGLCameraParameters(camera); |
| 119 |
const double half_w = camera.viewport[2] / 2.0; |
| 120 |
const double half_h = camera.viewport[3] / 2.0; |
| 121 |
|
| 122 |
//duplicate polyline 'a minima' (only points and indexes + closed state) |
| 123 |
ccPointCloud* polyVertices = new ccPointCloud("vertices"); |
| 124 |
ccPolyline segmentationPoly(polyVertices); |
| 125 |
{ |
| 126 |
segmentationPoly.set2DMode(true); |
| 127 |
segmentationPoly.addChild(polyVertices); |
| 128 |
|
| 129 |
CCCoreLib::GenericIndexedCloudPersist* vertices = poly->getAssociatedCloud(); |
| 130 |
assert(vertices); |
| 131 |
|
| 132 |
if ( !polyVertices->reserve(vertices->size() + (poly->isClosed() ? 0 : 1)) |
| 133 |
|| !segmentationPoly.reserve(poly->size() + (poly->isClosed() ? 0 : 1))) |
| 134 |
{ |
| 135 |
ccLog::Error("Not enough memory"); |
| 136 |
return {}; |
| 137 |
} |
| 138 |
|
| 139 |
bool mode3D = !poly->is2DMode(); |
| 140 |
|
| 141 |
for (unsigned i = 0; i < vertices->size(); ++i) |
| 142 |
{ |
| 143 |
CCVector3 P = *vertices->getPoint(i); |
| 144 |
if (mode3D) |
| 145 |
{ |
| 146 |
CCVector3d Q2D; |
| 147 |
camera.project(P, Q2D); |
| 148 |
|
| 149 |
P.x = static_cast<PointCoordinateType>(Q2D.x - half_w); |
| 150 |
P.y = static_cast<PointCoordinateType>(Q2D.y - half_h); |
| 151 |
P.z = 0; |
| 152 |
} |
| 153 |
polyVertices->addPoint(P); |
| 154 |
} |
| 155 |
|
| 156 |
for (unsigned j = 0; j < poly->size(); ++j) |
| 157 |
{ |
| 158 |
segmentationPoly.addPointIndex(poly->getPointGlobalIndex(j)); |
| 159 |
} |
| 160 |
|
| 161 |
segmentationPoly.setClosed(poly->isClosed()); |
| 162 |
} |
| 163 |
|
| 164 |
if (!cloud->isVisibilityTableInstantiated() && !cloud->resetVisibilityArray()) |
| 165 |
{ |
| 166 |
ccLog::Error("Not enough memory"); |
| 167 |
return {}; |
| 168 |
} |
| 169 |
|
| 170 |
vector<unsigned> ptsIdx; |
| 171 |
|
| 172 |
try |
| 173 |
{ |
| 174 |
ccGenericPointCloud::VisibilityTableType& gVisibilityArray = cloud->getTheVisibilityArray(); |
| 175 |
for (unsigned j = 0; j < cloud->size(); ++j) |
| 176 |
{ |
| 177 |
if (gVisibilityArray[j] == CCCoreLib::POINT_VISIBLE) |
| 178 |
{ |
| 179 |
const CCVector3* P3D = cloud->getPoint(j); |
| 180 |
|
| 181 |
CCVector3d Q2D; |
| 182 |
camera.project(*P3D, Q2D); |
| 183 |
|
| 184 |
CCVector2 P2D( static_cast<PointCoordinateType>(Q2D.x - half_w), |
| 185 |
static_cast<PointCoordinateType>(Q2D.y - half_h)); |
| 186 |
|
| 187 |
bool pointInside = CCCoreLib::ManualSegmentationTools::isPointInsidePoly(P2D, &segmentationPoly); |
| 188 |
|
| 189 |
gVisibilityArray[j] = (pointInside ? CCCoreLib::POINT_HIDDEN : CCCoreLib::POINT_VISIBLE); //visibility values of 1 are points inside |
| 190 |
if (pointInside) |
| 191 |
{ |
| 192 |
ptsIdx.push_back(j); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
catch (const std::bad_alloc&) |
| 198 |
{ |
| 199 |
ccLog::Error("Not enough memory"); |
| 200 |
return {}; |
| 201 |
} |
| 202 |
|
| 203 |
ptsIdx.shrink_to_fit(); |
| 204 |
|
| 205 |
return ptsIdx; |
| 206 |
} |
| 207 |
|
| 208 |
vector<int> ccManualSeg::StIdFromPtId(const std::vector<unsigned>& pts, const std::vector<pair<unsigned, int>>& pairs) |
| 209 |
{ |
| 210 |
vector<int> v; |
| 211 |
|
| 212 |
try |
| 213 |
{ |
| 214 |
for (unsigned ptIndex : pts) |
| 215 |
{ |
| 216 |
if (find(v.begin(), v.end(), ptIndex) == v.end()) |
| 217 |
{ |
| 218 |
v.push_back(pairs[ptIndex].second); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
sort(v.begin(), v.end()); |
| 223 |
|
| 224 |
v.erase(unique(v.begin(), v.end()), v.end()); |
| 225 |
} |
| 226 |
catch (const std::bad_alloc&) |
| 227 |
{ |
| 228 |
// not enough memory |
| 229 |
return {}; |
| 230 |
} |
| 231 |
|
| 232 |
return v; |
| 233 |
} |
| 234 |
|
| 235 |
vector<unsigned> ccManualSeg::PtIdFromStId(const std::vector<int>& stIdx, const std::vector<pair<unsigned, int>>& pairs) |
| 236 |
{ |
| 237 |
vector<unsigned> v; |
| 238 |
|
| 239 |
try |
| 240 |
{ |
| 241 |
for (int ptIndex : stIdx) |
| 242 |
{ |
| 243 |
for (size_t i = 0; i < pairs.size(); i++) |
| 244 |
if (pairs[i].second == ptIndex) |
| 245 |
v.push_back(pairs[i].first); |
| 246 |
} |
| 247 |
} |
| 248 |
catch (const std::bad_alloc&) |
| 249 |
{ |
| 250 |
// not enough memory |
| 251 |
return {}; |
| 252 |
} |
| 253 |
|
| 254 |
return v; |
| 255 |
} |
| 256 |
|
| 257 |
static void Cloud2Binary(ccPointCloud* cloud, Mat& corrMatS, vector<Point>& idxPxS, vector<int>& idxPx1DS, Mat& imageBWS) |
| 258 |
{ |
| 259 |
if (!cloud) |
| 260 |
{ |
| 261 |
assert(false); |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
//Correspondence 2D->1D. Each px has an index instead of two coords |
| 266 |
int cntC = 0; |
| 267 |
for (int y = 0; y < corrMatS.rows; y++) |
| 268 |
{ |
| 269 |
for (int x = 0; x < corrMatS.cols; x++) |
| 270 |
{ |
| 271 |
corrMatS.at<int>(y, x) = cntC; |
| 272 |
++cntC; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
//Binary |
| 277 |
unsigned s = cloud->size(); |
| 278 |
for (unsigned i = 0; i < s; i++) |
| 279 |
{ |
| 280 |
//depth |
| 281 |
const CCVector3* pointC0 = cloud->getPoint(i); |
| 282 |
int z = floor(pointC0->z); |
| 283 |
int x = floor(pointC0->x); |
| 284 |
imageBWS.at<uchar>(z, x) = 255; |
| 285 |
|
| 286 |
//idxPx per 3D point |
| 287 |
idxPxS.push_back(Point(z, x)); |
| 288 |
idxPx1DS.push_back(corrMatS.at<int>(z, x)); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
//Extract Skeleton from Binary (CV_8U) |
| 293 |
static Mat Skeleton(const Mat& I, bool flagInv) |
| 294 |
{ |
| 295 |
Mat toSkel; |
| 296 |
if (flagInv) |
| 297 |
{ |
| 298 |
//If black-white inversion is required |
| 299 |
toSkel = 255 * cv::Mat::ones(I.rows, I.cols, CV_8U) - I; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
toSkel = I; |
| 304 |
} |
| 305 |
|
| 306 |
cv::Mat skel(toSkel.size(), CV_8U, cv::Scalar(0)); |
| 307 |
cv::Mat element = cv::getStructuringElement(cv::MORPH_CROSS, cv::Size(3, 3)); |
| 308 |
|
| 309 |
int iterations = 0; |
| 310 |
bool done = false; |
| 311 |
do |
| 312 |
{ |
| 313 |
cv::Mat eroded; |
| 314 |
cv::erode(toSkel, eroded, element); |
| 315 |
cv::Mat temp; |
| 316 |
cv::dilate(eroded, temp, element); |
| 317 |
cv::subtract(toSkel, temp, temp); |
| 318 |
cv::bitwise_or(skel, temp, skel); |
| 319 |
eroded.copyTo(toSkel); |
| 320 |
|
| 321 |
done = (cv::countNonZero(toSkel) == 0); |
| 322 |
iterations++; |
| 323 |
} while (!done && (iterations < 100)); |
| 324 |
|
| 325 |
return skel; |
| 326 |
} |
| 327 |
|
| 328 |
vector<int> SetIntersectIdxPixs1D(const vector<int>& listNew, const vector<int>& listOld) |
| 329 |
{ |
| 330 |
vector<int> commonList; |
| 331 |
try |
| 332 |
{ |
| 333 |
//Common |
| 334 |
int cnt = 0; |
| 335 |
for (auto &e : listNew) |
| 336 |
{ |
| 337 |
for (auto &ee : listOld) |
| 338 |
{ |
| 339 |
if (e == ee) |
| 340 |
{ |
| 341 |
commonList.push_back(cnt); |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
++cnt; |
| 346 |
} |
| 347 |
} |
| 348 |
catch (const std::bad_alloc&) |
| 349 |
{ |
| 350 |
return {}; |
| 351 |
} |
| 352 |
|
| 353 |
return commonList; |
| 354 |
} |
| 355 |
|
| 356 |
ccPolyline* ContourPoly2(ccPointCloud* cloud0, const vector<int>& V, const QString& name) |
| 357 |
{ |
| 358 |
if (!cloud0) |
| 359 |
{ |
| 360 |
assert(false); |
| 361 |
return nullptr; |
| 362 |
} |
| 363 |
|
| 364 |
vector<unsigned> Vec; |
| 365 |
try |
| 366 |
{ |
| 367 |
std::vector<CCCoreLib::PointProjectionTools::IndexedCCVector2> points; |
| 368 |
for (size_t i = 0; i < V.size(); ++i) |
| 369 |
{ |
| 370 |
PointCoordinateType x = cloud0->getPoint(V[i])->x; |
| 371 |
PointCoordinateType z = cloud0->getPoint(V[i])->z; |
| 372 |
CCCoreLib::PointProjectionTools::IndexedCCVector2 P(x, z, i); |
| 373 |
|
| 374 |
points.push_back(P); |
| 375 |
} |
| 376 |
|
| 377 |
std::list<CCCoreLib::PointProjectionTools::IndexedCCVector2*> hullPoint2; |
| 378 |
CCCoreLib::PointProjectionTools::extractConcaveHull2D(points, hullPoint2, 0.0001); |
| 379 |
|
| 380 |
for (size_t i = 0; i < hullPoint2.size(); ++i) |
| 381 |
{ |
| 382 |
auto it = hullPoint2.begin(); |
| 383 |
std::advance(it, i); |
| 384 |
CCCoreLib::PointProjectionTools::IndexedCCVector2* P2 = *it; |
| 385 |
Vec.push_back(P2->index); |
| 386 |
} |
| 387 |
} |
| 388 |
catch (const std::bad_alloc&) |
| 389 |
{ |
| 390 |
return {}; |
| 391 |
} |
| 392 |
|
| 393 |
ccPointCloud* contour = new ccPointCloud(name + " - Contour Cloud"); |
| 394 |
if (!contour->reserve(Vec.size()) || !contour->reserveTheRGBTable()) |
| 395 |
{ |
| 396 |
// not enough memory |
| 397 |
ccLog::Warning("Not enough memory"); |
| 398 |
delete contour; |
| 399 |
return nullptr; |
| 400 |
} |
| 401 |
|
| 402 |
for (unsigned pointIndex : Vec) |
| 403 |
{ |
| 404 |
const CCVector3* P0 = cloud0->getPoint(pointIndex); |
| 405 |
contour->addPoint(*P0); |
| 406 |
|
| 407 |
const ccColor::Rgba& col = cloud0->getPointColor(pointIndex); |
| 408 |
contour->addColor(col); |
| 409 |
} |
| 410 |
|
| 411 |
unsigned cnt = static_cast<unsigned>(Vec.size()); |
| 412 |
|
| 413 |
ccPolyline* pContour = new ccPolyline(contour); |
| 414 |
pContour->addChild(contour); |
| 415 |
|
| 416 |
if (pContour->reserve(cnt)) |
| 417 |
{ |
| 418 |
pContour->addPointIndex(0, cnt); |
| 419 |
pContour->setClosed(true); |
| 420 |
pContour->setVisible(true); |
| 421 |
pContour->setName(name + " - Contour"); |
| 422 |
pContour->setColor(ccColor::cyan); |
| 423 |
pContour->showColors(true); |
| 424 |
pContour->showVertices(true); |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
ccLog::Warning("Not enough memory"); |
| 429 |
delete pContour; |
| 430 |
pContour = nullptr; |
| 431 |
} |
| 432 |
|
| 433 |
return pContour; |
| 434 |
} |
| 435 |
|
| 436 |
static string DateStamp() |
| 437 |
{ |
| 438 |
time_t rawtime; |
| 439 |
time(&rawtime); |
| 440 |
|
| 441 |
struct tm* timeinfo = localtime(&rawtime); |
| 442 |
char buffer[80]; |
| 443 |
strftime(buffer, sizeof(buffer), "%H:%M:%S", timeinfo); |
| 444 |
std::string str(buffer); |
| 445 |
|
| 446 |
return "[" + str + "]"; |
| 447 |
} |
| 448 |
|
| 449 |
// Returns mortar maps (Depth and width) |
| 450 |
static ccPointCloud* GetMortarMaps(ccPointCloud* f_cloudStones, ccPointCloud* f_cloudMortar) |
| 451 |
{ |
| 452 |
if (!f_cloudStones || !f_cloudMortar) |
| 453 |
{ |
| 454 |
assert(false); |
| 455 |
return nullptr; |
| 456 |
} |
| 457 |
|
| 458 |
//Binary Mortar |
| 459 |
|
| 460 |
//Bringing clouds to the origin |
| 461 |
CCVector3 minBox0, maxBox0; |
| 462 |
f_cloudMortar->getBoundingBox(minBox0, maxBox0); |
| 463 |
f_cloudMortar->translate(-minBox0); |
| 464 |
f_cloudStones->translate(-minBox0); |
| 465 |
|
| 466 |
f_cloudMortar->scale(100, 1000, 100); //To cm mm cm |
| 467 |
CCVector3 minBox, maxBox; |
| 468 |
f_cloudMortar->getBoundingBox(minBox, maxBox); |
| 469 |
|
| 470 |
int rowsM = static_cast<int>(ceil(maxBox.z)); |
| 471 |
int colsM = static_cast<int>(ceil(maxBox.x)); |
| 472 |
|
| 473 |
cv::Mat corrMatM = Mat::zeros(rowsM, colsM, CV_32F); |
| 474 |
cv::Mat imageBWS = Mat::zeros(rowsM, colsM, CV_8U); |
| 475 |
vector<Point> idxPxM; |
| 476 |
vector<int> idxPxM1D; |
| 477 |
Cloud2Binary(f_cloudMortar, corrMatM, idxPxM, idxPxM1D, imageBWS); |
| 478 |
|
| 479 |
//Skeleton |
| 480 |
Mat skel = Skeleton(imageBWS, false); |
| 481 |
|
| 482 |
f_cloudMortar->scale(0.01, 0.001, 0.01); //Scale back |
| 483 |
|
| 484 |
//Skeleton 3D |
| 485 |
vector<int> pixelsSkel; |
| 486 |
for (int i = 0; i < skel.rows; i++) |
| 487 |
{ |
| 488 |
for (int j = 0; j < skel.cols; j++) |
| 489 |
{ |
| 490 |
unsigned char a = skel.at<char>(i, j); |
| 491 |
if (a == 255) |
| 492 |
{ |
| 493 |
int idx = corrMatM.at<int>(i, j); |
| 494 |
pixelsSkel.push_back(idx); |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
vector<int> idxCloudMortarSkel = SetIntersectIdxPixs1D(idxPxM1D, pixelsSkel); |
| 500 |
ccPointCloud* skelM = new ccPointCloud("Skeleton Mortar"); |
| 501 |
{ |
| 502 |
if (!skelM->reserve(idxCloudMortarSkel.size()) || !skelM->reserveTheRGBTable()) |
| 503 |
{ |
| 504 |
ccLog::Warning("Not enough memory"); |
| 505 |
delete skelM; |
| 506 |
return nullptr; |
| 507 |
} |
| 508 |
for (auto e : idxCloudMortarSkel) |
| 509 |
{ |
| 510 |
skelM->addPoint(*f_cloudMortar->getPoint(e)); |
| 511 |
const ccColor::Rgba& col = f_cloudMortar->getPointColor(e); |
| 512 |
skelM->addColor(col); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
//Subsample result |
| 517 |
CCCoreLib::CloudSamplingTools::SFModulationParams modParams(false); |
| 518 |
CCCoreLib::ReferenceCloud* refCloud = CCCoreLib::CloudSamplingTools::resampleCloudSpatially(skelM, 0.01, modParams, 0, 0); //1 point per cm^2 |
| 519 |
if (!refCloud) |
| 520 |
{ |
| 521 |
ccLog::Warning("Not enough memory"); |
| 522 |
delete skelM; |
| 523 |
return nullptr; |
| 524 |
} |
| 525 |
|
| 526 |
//Save output |
| 527 |
ccPointCloud* f_skelMortar = skelM->partialClone(refCloud); |
| 528 |
|
| 529 |
delete refCloud; |
| 530 |
refCloud = nullptr; |
| 531 |
delete skelM; |
| 532 |
skelM = nullptr; |
| 533 |
|
| 534 |
if (!f_skelMortar) |
| 535 |
{ |
| 536 |
ccLog::Warning("Not enough memory"); |
| 537 |
return nullptr; |
| 538 |
} |
| 539 |
f_skelMortar->setName("Mortar Maps"); |
| 540 |
|
| 541 |
//Maps using PCL |
| 542 |
//Mortar depth map |
| 543 |
pcl::PointCloud<pcl::PointXYZ>::Ptr pcl_cloudStones(new pcl::PointCloud<pcl::PointXYZ>); |
| 544 |
{ |
| 545 |
pcl_cloudStones->width = f_cloudStones->size(); |
| 546 |
pcl_cloudStones->height = 1; |
| 547 |
pcl_cloudStones->points.resize(pcl_cloudStones->width * pcl_cloudStones->height); |
| 548 |
|
| 549 |
for (size_t i = 0; i < f_cloudStones->size(); ++i) |
| 550 |
{ |
| 551 |
pcl_cloudStones->points[i].x = f_cloudStones->getPoint(i)->x; |
| 552 |
pcl_cloudStones->points[i].y = f_cloudStones->getPoint(i)->y; |
| 553 |
pcl_cloudStones->points[i].z = f_cloudStones->getPoint(i)->z; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree; |
| 558 |
kdtree.setInputCloud(pcl_cloudStones); |
| 559 |
|
| 560 |
vector<double> distStD, dist3St, distStD2, distStWidth, distStW; |
| 561 |
for (unsigned i = 0; i < f_skelMortar->size(); i++) |
| 562 |
{ |
| 563 |
pcl::PointXYZ searchPoint( f_skelMortar->getPoint(i)->x, |
| 564 |
f_skelMortar->getPoint(i)->y, |
| 565 |
f_skelMortar->getPoint(i)->z); |
| 566 |
|
| 567 |
std::vector<int> pointIdxRadiusSearch; |
| 568 |
std::vector<float> pointRadiusSquaredDistance; |
| 569 |
|
| 570 |
float radius = 0.1f; //10cm |
| 571 |
|
| 572 |
if (kdtree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0) |
| 573 |
{ |
| 574 |
pcl::PointXYZ nnPoint = pcl_cloudStones->points[pointIdxRadiusSearch[0]]; |
| 575 |
|
| 576 |
double disty = abs(searchPoint.y - nnPoint.y); //Depth |
| 577 |
double distx = abs(searchPoint.x - nnPoint.x); |
| 578 |
double distz = abs(searchPoint.z - nnPoint.z); |
| 579 |
double dist3 = sqrt(distx*distx + distz*distz);//2D distance (XZ) will be used as a reference for the creation of mortar width map |
| 580 |
|
| 581 |
distStD.push_back(disty * 1000); //depth (y axis) in mm |
| 582 |
dist3St.push_back(dist3); |
| 583 |
|
| 584 |
double dist1 = 1000; |
| 585 |
double dist2 = 1000; |
| 586 |
double dist1y = 1000; |
| 587 |
double dist2y = 1000; |
| 588 |
int idx1 = -1; |
| 589 |
int idx2 = -1; |
| 590 |
for (size_t j = 0; j < pointIdxRadiusSearch.size(); ++j) |
| 591 |
{ |
| 592 |
double distx = abs(searchPoint.x - pcl_cloudStones->points[pointIdxRadiusSearch[j]].x); //2D distance |
| 593 |
double distz = abs(searchPoint.z - pcl_cloudStones->points[pointIdxRadiusSearch[j]].z); |
| 594 |
double disty = abs(searchPoint.y - pcl_cloudStones->points[pointIdxRadiusSearch[j]].y); |
| 595 |
double dist3 = sqrt(distx*distx + distz*distz); |
| 596 |
|
| 597 |
int idxSt = f_cloudStones->getPointScalarValue(pointIdxRadiusSearch[j]); |
| 598 |
|
| 599 |
if (j == 0) |
| 600 |
{ |
| 601 |
//Initialise NN stone 1 |
| 602 |
dist1 = dist3; |
| 603 |
dist1y = disty; |
| 604 |
idx1 = idxSt; |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
if (idxSt == idx1) |
| 609 |
{ |
| 610 |
//If point belongs to stone 1 |
| 611 |
if (dist3 < dist1) |
| 612 |
dist1 = dist3; |
| 613 |
dist1y = disty; |
| 614 |
} |
| 615 |
if (idxSt != idx1 && idx2 == -1) |
| 616 |
{ |
| 617 |
//Initialise stone 2 |
| 618 |
idx2 = idxSt; |
| 619 |
dist2 = dist3; |
| 620 |
dist2y = disty; |
| 621 |
} |
| 622 |
if (idxSt == idx2) |
| 623 |
{ |
| 624 |
//If point belongs to stone 2 |
| 625 |
if (dist3 < dist2) |
| 626 |
dist2 = dist3; |
| 627 |
dist2y = disty; |
| 628 |
} |
| 629 |
} |
| 630 |
} //Points are ordered by radius. Idx 0 is the nearest neighbour |
| 631 |
|
| 632 |
//For each point, there is dist1 or dist1 and dist2 |
| 633 |
if (idx1 > -1 && idx2 == -1 && dist1 < 1000) |
| 634 |
{ |
| 635 |
//Only one stone (boundaries of wall) |
| 636 |
distStW.push_back(dist1 * 1000); |
| 637 |
distStD2.push_back(dist1y * 1000); |
| 638 |
} |
| 639 |
else if (idx1 > -1 && idx2 > -1) |
| 640 |
{ |
| 641 |
distStW.push_back((dist1 + dist2) * 1000); |
| 642 |
distStD2.push_back((dist1y + dist2y) / 2 * 1000); |
| 643 |
} |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
distStD.push_back(100); //Add high values for non-calculated values (no neighbours available). These values can be modified according to the needs |
| 648 |
distStD2.push_back(100); |
| 649 |
dist3St.push_back(200); |
| 650 |
distStW.push_back(200); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
//Add depth as a scalarField to f_skelMortar |
| 655 |
{ |
| 656 |
static constexpr const char MortarRelativeDepthSFName[] = "Mortar relative depth (mm)"; |
| 657 |
int sfIdxD = f_skelMortar->getScalarFieldIndexByName(MortarRelativeDepthSFName); |
| 658 |
if (sfIdxD < 0) |
| 659 |
{ |
| 660 |
sfIdxD = f_skelMortar->addScalarField(MortarRelativeDepthSFName); |
| 661 |
} |
| 662 |
|
| 663 |
CCCoreLib::ScalarField* depthSF = f_skelMortar->getScalarField(sfIdxD); |
| 664 |
if (depthSF) |
| 665 |
{ |
| 666 |
for (unsigned int i = 0; i < distStD2.size(); i++) |
| 667 |
{ |
| 668 |
ScalarType val = static_cast<ScalarType>(distStD2.at(i)); |
| 669 |
depthSF->setValue(i, val); |
| 670 |
} |
| 671 |
depthSF->computeMinAndMax(); |
| 672 |
|
| 673 |
f_skelMortar->setCurrentDisplayedScalarField(sfIdxD); |
| 674 |
f_skelMortar->showSF(true); |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
ccLog::Warning(QString("Failed to create SF:") + MortarRelativeDepthSFName); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
//Add width as a scalarField to f_skelMortar |
| 683 |
{ |
| 684 |
static constexpr const char MortarRelativeWidthSFName[] = "Mortar relative width (mm)"; |
| 685 |
int sfIdxW = f_skelMortar->getScalarFieldIndexByName(MortarRelativeWidthSFName); |
| 686 |
if (sfIdxW < 0) |
| 687 |
{ |
| 688 |
sfIdxW = f_skelMortar->addScalarField(MortarRelativeWidthSFName); |
| 689 |
} |
| 690 |
|
| 691 |
CCCoreLib::ScalarField* widthSF = f_skelMortar->getScalarField(sfIdxW); |
| 692 |
if (widthSF) |
| 693 |
{ |
| 694 |
for (unsigned int i = 0; i < distStW.size(); i++) |
| 695 |
{ |
| 696 |
ScalarType val = static_cast<ScalarType>(distStW.at(i)); |
| 697 |
widthSF->setValue(i, val); |
| 698 |
} |
| 699 |
widthSF->computeMinAndMax(); |
| 700 |
|
| 701 |
f_skelMortar->setCurrentDisplayedScalarField(sfIdxW); |
| 702 |
f_skelMortar->showSF(true); |
| 703 |
} |
| 704 |
else |
| 705 |
{ |
| 706 |
ccLog::Warning(QString("Failed to create SF:") + MortarRelativeWidthSFName); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
f_cloudMortar->translate(minBox0); |
| 711 |
f_cloudStones->translate(minBox0); |
| 712 |
f_skelMortar->translate(minBox0); |
| 713 |
f_skelMortar->setPointSize(10); |
| 714 |
f_skelMortar->setName("Mortar Maps"); |
| 715 |
|
| 716 |
return f_skelMortar; |
| 717 |
} |
| 718 |
|
| 719 |
void ccManualSeg::doAction() |
| 720 |
{ |
| 721 |
if (m_app == nullptr) |
| 722 |
{ |
| 723 |
// m_app should have already been initialized by CC when plugin is loaded |
| 724 |
assert(false); |
| 725 |
return; |
| 726 |
} |
| 727 |
|
| 728 |
ccHObject* root = m_app->dbRootObject(); |
| 729 |
if (!root) |
| 730 |
{ |
| 731 |
ccLog::Error("No DB root object"); |
| 732 |
return; |
| 733 |
} |
| 734 |
|
| 735 |
ccHObject::Container pcs; |
| 736 |
root->filterChildren(pcs, true, CC_TYPES::POINT_CLOUD); |
| 737 |
|
| 738 |
if (pcs.size() < 2) |
| 739 |
{ |
| 740 |
m_app->dispToConsole("At least one cloud and a polyline are required", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 741 |
return; |
| 742 |
} |
| 743 |
|
| 744 |
// Create log file |
| 745 |
time_t rawtime; |
| 746 |
time(&rawtime); |
| 747 |
|
| 748 |
struct tm* timeinfo = localtime(&rawtime); |
| 749 |
char buffer[80]; |
| 750 |
strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S", timeinfo); |
| 751 |
std::string str(buffer); |
| 752 |
|
| 753 |
string filename = "MSP_log_" + str + ".txt"; |
| 754 |
ofstream auto_seg_log; |
| 755 |
auto_seg_log.open(filename); |
| 756 |
|
| 757 |
ccGLWindowInterface* win = m_app->getActiveGLWindow(); |
| 758 |
if (win) |
| 759 |
{ |
| 760 |
win->setView(CC_FRONT_VIEW); |
| 761 |
} |
| 762 |
|
| 763 |
// Removing old contours and mortar maps from DB |
| 764 |
{ |
| 765 |
ccHObject::Container contourfolder; |
| 766 |
root->filterChildren(contourfolder, true, CC_TYPES::HIERARCHY_OBJECT); |
| 767 |
for (ccHObject* folder : contourfolder) |
| 768 |
{ |
| 769 |
if (folder && folder->getName().contains("Stone contours")) |
| 770 |
{ |
| 771 |
m_app->removeFromDB(folder, false); |
| 772 |
break; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
ccHObject::Container oldMortarMaps; |
| 777 |
root->filterChildren(oldMortarMaps, true, CC_TYPES::POINT_CLOUD); |
| 778 |
for (ccHObject* folder : contourfolder) |
| 779 |
{ |
| 780 |
if (folder && folder->getName().contains("Mortar Maps")) |
| 781 |
{ |
| 782 |
m_app->removeFromDB(folder, false); |
| 783 |
break; |
| 784 |
} |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
ccHObject::Container polys; |
| 789 |
root->filterChildren(polys, true, CC_TYPES::POLY_LINE); |
| 790 |
|
| 791 |
// Check loaded point clouds (there should be 2: base and segmented) |
| 792 |
ccPointCloud* cloudStone = nullptr; |
| 793 |
ccPointCloud* cloudMortar = nullptr; |
| 794 |
{ |
| 795 |
int idxStone = -1; |
| 796 |
int idxMortar = -1; |
| 797 |
for (size_t i = 0; i < pcs.size(); ++i) |
| 798 |
{ |
| 799 |
ccPointCloud* cl = static_cast<ccPointCloud*>(pcs.at(i)); |
| 800 |
QString cloudName = cl->getName(); |
| 801 |
|
| 802 |
if (!cloudName.contains("ref")) |
| 803 |
{ |
| 804 |
if (idxStone < 0 && cloudName.contains("Stone")) |
| 805 |
{ |
| 806 |
if (cl->getScalarFieldIndexByName("Stone Index") >= 0) |
| 807 |
{ |
| 808 |
idxStone = i; |
| 809 |
m_app->dispToConsole("Stone cloud found!", ccMainAppInterface::STD_CONSOLE_MESSAGE); |
| 810 |
auto_seg_log << DateStamp() << " Stone cloud found" << endl; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
if (idxMortar < 0 && cloudName.contains("Mortar")) |
| 815 |
{ |
| 816 |
idxMortar = i; |
| 817 |
m_app->dispToConsole("Mortar cloud found!", ccMainAppInterface::STD_CONSOLE_MESSAGE); |
| 818 |
auto_seg_log << DateStamp() << " Mortar cloud found" << endl; |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
if (idxMortar != -1 && idxStone != -1) |
| 824 |
{ |
| 825 |
// Both clouds were found |
| 826 |
cloudStone = static_cast<ccPointCloud*>(pcs.at(idxStone)); |
| 827 |
cloudMortar = static_cast<ccPointCloud*>(pcs.at(idxMortar)); |
| 828 |
} |
| 829 |
else |
| 830 |
{ |
| 831 |
if (idxMortar == -1 && idxStone == -1) |
| 832 |
{ |
| 833 |
int cnt = 0; |
| 834 |
for (size_t i = 0; i < pcs.size(); ++i) |
| 835 |
{ |
| 836 |
ccPointCloud* cl = static_cast<ccPointCloud*>(pcs.at(i)); |
| 837 |
if (cl->size() > 100) |
| 838 |
{ |
| 839 |
idxMortar = static_cast<int>(i); |
| 840 |
++cnt; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
if (cnt > 1) |
| 845 |
{ |
| 846 |
m_app->dispToConsole("For manual segmentation leave only one cloud in DB", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 847 |
return; |
| 848 |
} |
| 849 |
else if (cnt == 0) |
| 850 |
{ |
| 851 |
m_app->dispToConsole("Clouds in DB are too small. Ignoring them", ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 852 |
} |
| 853 |
|
| 854 |
cloudMortar = static_cast<ccPointCloud*>(pcs.at(idxMortar)); |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
m_app->dispToConsole("Only one cloud has been foud\nFor manual segmentation please change its name\nso that it doesn't contain the sequence 'stone' or 'mortar'", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 859 |
return; |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
// Make the distinction between rectangles and non rectangular polylines |
| 865 |
std::vector<size_t> recIdx; |
| 866 |
std::vector<size_t> polyIdx; |
| 867 |
for (size_t i = 0; i < polys.size(); i++) |
| 868 |
{ |
| 869 |
ccPolyline* poly = static_cast<ccPolyline*>(polys.at(i)); |
| 870 |
if (poly->size() != 4) |
| 871 |
{ |
| 872 |
polyIdx.push_back(i); |
| 873 |
} |
| 874 |
else |
| 875 |
{ |
| 876 |
PointCoordinateType dx1 = poly->getPoint(1)->x - poly->getPoint(0)->x; |
| 877 |
PointCoordinateType dx2 = poly->getPoint(3)->x - poly->getPoint(2)->x; |
| 878 |
|
| 879 |
if (dx1 == dx2) |
| 880 |
{ |
| 881 |
recIdx.push_back(i); |
| 882 |
} |
| 883 |
else |
| 884 |
{ |
| 885 |
polyIdx.push_back(i); |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
// Creating vectors |
| 891 |
vector< vector< pair<unsigned, int> > > stonePairs(2); |
| 892 |
vector< vector<unsigned> > mortarPoints(2); |
| 893 |
vector<unsigned> allStIdx; |
| 894 |
|
| 895 |
if (cloudMortar && cloudMortar->size() != 0) |
| 896 |
{ |
| 897 |
mortarPoints[0].reserve(cloudMortar->size()); |
| 898 |
for (unsigned i = 0; i < cloudMortar->size(); i++) |
| 899 |
{ |
| 900 |
mortarPoints[0].push_back(i); |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
int maxStoneId = 0; |
| 905 |
if (cloudStone) |
| 906 |
{ |
| 907 |
int sfindex = cloudStone->getScalarFieldIndexByName("Stone Index"); |
| 908 |
assert(sfindex != 0); |
| 909 |
CCCoreLib::ScalarField* stIdSF = cloudStone->getScalarField(sfindex); |
| 910 |
|
| 911 |
for (unsigned i = 0; i < cloudStone->size(); i++) |
| 912 |
{ |
| 913 |
int index = static_cast<int>(stIdSF->getValue(i)); |
| 914 |
stonePairs[0].push_back(make_pair(i, index)); |
| 915 |
allStIdx.push_back(index); |
| 916 |
} |
| 917 |
|
| 918 |
sort(allStIdx.begin(), allStIdx.end()); |
| 919 |
maxStoneId = static_cast<int>(allStIdx.back()); |
| 920 |
|
| 921 |
auto_seg_log << DateStamp() << " Evaluating " << polyIdx.size() + recIdx.size() << " polylines..." << endl; |
| 922 |
|
| 923 |
// Rectangular polyline |
| 924 |
if (recIdx.size() != 0) |
| 925 |
{ |
| 926 |
auto_seg_log << DateStamp() << " Deleting incorrectly identified stones: " << recIdx.size() << "..." << endl; |
| 927 |
for (size_t polyIndex = 0; polyIndex < recIdx.size(); ++polyIndex) |
| 928 |
{ |
| 929 |
auto_seg_log << DateStamp() << " Polyline " << polyIndex + 1 << "/" << recIdx.size() << endl; |
| 930 |
|
| 931 |
ccPolyline* poly = static_cast<ccPolyline*>(polys[polyIndex]); |
| 932 |
|
| 933 |
vector<unsigned> v = pointIdx(cloudStone, poly); |
| 934 |
vector<int> stIds = StIdFromPtId(v, stonePairs[0]); |
| 935 |
|
| 936 |
for (int stoneIndex : stIds) |
| 937 |
{ |
| 938 |
for (size_t i = 0; i < stonePairs[0].size(); ++i) |
| 939 |
{ |
| 940 |
if (stonePairs[0][i].second == stoneIndex) |
| 941 |
{ |
| 942 |
stonePairs[0][i].second = -1; |
| 943 |
mortarPoints[1].push_back(stonePairs[0][i].first); |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
m_app->removeFromDB(poly, false); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
// Standard polylines |
| 953 |
if (polyIdx.size() != 0) |
| 954 |
{ |
| 955 |
auto_seg_log << DateStamp() << " Correcting boundaries of stones (" << polyIdx.size() << ")..." << endl; |
| 956 |
for (size_t polyIndex = 0; polyIndex < polyIdx.size(); ++polyIndex) |
| 957 |
{ |
| 958 |
auto_seg_log << DateStamp() << " Polyline " << polyIndex + 1 << "/" << polyIdx.size() << endl; |
| 959 |
|
| 960 |
ccPolyline* poly = static_cast<ccPolyline*>(polys[polyIndex]); |
| 961 |
vector<unsigned> vs = pointIdx(cloudStone, poly); |
| 962 |
vector<unsigned> vm; |
| 963 |
if (cloudMortar) |
| 964 |
{ |
| 965 |
vm = pointIdx(cloudMortar, poly); |
| 966 |
} |
| 967 |
|
| 968 |
int k = 0; |
| 969 |
string kLabel; |
| 970 |
|
| 971 |
if (vm.empty() && !vs.empty()) |
| 972 |
{ |
| 973 |
// Values can be test for higher than zero just in case noise |
| 974 |
k = 1; |
| 975 |
kLabel = " (Removing stone)"; |
| 976 |
} |
| 977 |
else if (vs.empty() && !vm.empty()) |
| 978 |
{ |
| 979 |
k = 2; |
| 980 |
kLabel = " (Adding new stone)"; |
| 981 |
} |
| 982 |
else if (!vs.empty() && !vm.empty()) |
| 983 |
{ |
| 984 |
k = 3; |
| 985 |
kLabel = " (Correcting previously segmented stone)"; |
| 986 |
} |
| 987 |
auto_seg_log << DateStamp() << " Case " << k << kLabel << endl; |
| 988 |
|
| 989 |
switch (k) |
| 990 |
{ |
| 991 |
case 1: |
| 992 |
{ |
| 993 |
vector<int> stId = StIdFromPtId(vs, stonePairs[0]); |
| 994 |
|
| 995 |
if (stId.size() == 1 && stId[0] == -1) |
| 996 |
{ |
| 997 |
++maxStoneId; |
| 998 |
for (unsigned e : vs) |
| 999 |
{ |
| 1000 |
stonePairs[0][e].second = maxStoneId; |
| 1001 |
} |
| 1002 |
|
| 1003 |
vector<unsigned> tempMortar = mortarPoints[1]; |
| 1004 |
vector<unsigned> resMortar; |
| 1005 |
sort(tempMortar.begin(), tempMortar.end()); |
| 1006 |
std::set_difference(tempMortar.begin(), tempMortar.end(), vs.begin(), vs.end(), std::inserter(resMortar, resMortar.end())); |
| 1007 |
mortarPoints[1] = resMortar; |
| 1008 |
} |
| 1009 |
else |
| 1010 |
{ |
| 1011 |
vector<unsigned> vall = PtIdFromStId(stId, stonePairs[0]); |
| 1012 |
|
| 1013 |
vector<unsigned> vr; |
| 1014 |
std::set_difference(vall.begin(), vall.end(), vs.begin(), vs.end(), std::inserter(vr, vr.end())); |
| 1015 |
|
| 1016 |
for (unsigned index : vr) |
| 1017 |
{ |
| 1018 |
mortarPoints[1].push_back(static_cast<int>(index)); |
| 1019 |
stonePairs[0][index].second = -1; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
break; |
| 1024 |
} |
| 1025 |
|
| 1026 |
case 2: |
| 1027 |
{ |
| 1028 |
++maxStoneId; |
| 1029 |
for (unsigned index : vm) |
| 1030 |
{ |
| 1031 |
stonePairs[1].push_back(make_pair(index, maxStoneId)); |
| 1032 |
(mortarPoints[0])[index] = -1; |
| 1033 |
} |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
|
| 1037 |
case 3: |
| 1038 |
{ |
| 1039 |
vector<int> stId = StIdFromPtId(vs, stonePairs[0]); //Extract stone indices from selection |
| 1040 |
|
| 1041 |
int minStoneId = -1; |
| 1042 |
if (!stId.empty()) |
| 1043 |
{ |
| 1044 |
// Select minStoneId from indices |
| 1045 |
minStoneId = *std::min_element(stId.begin(), stId.end()); |
| 1046 |
} |
| 1047 |
|
| 1048 |
if (minStoneId > 0) // If there is stone labelled as -1 inside (not belonging to any stone) |
| 1049 |
{ |
| 1050 |
++maxStoneId; |
| 1051 |
minStoneId = maxStoneId; // Create a new label (?). This should be done only if there is no other stone in the selection |
| 1052 |
} |
| 1053 |
|
| 1054 |
for (unsigned e : vm) // For each point from mortar cloud in the selection |
| 1055 |
{ |
| 1056 |
stonePairs[1].push_back(make_pair(e, minStoneId)); // Add to stone vector. Note that stonePairs[1] contains points from the stone cloud to be added as mortar |
| 1057 |
mortarPoints[0][e] = -1; // Remove from mortar (idx to -1) |
| 1058 |
} |
| 1059 |
|
| 1060 |
vector<unsigned> vall = PtIdFromStId(stId, stonePairs[0]); // All the points with idx similar to the ones in the selection |
| 1061 |
for (unsigned e : vall) |
| 1062 |
{ |
| 1063 |
stonePairs[0][e].second = minStoneId; // Relabel all those points with minId |
| 1064 |
} |
| 1065 |
|
| 1066 |
sort(vs.begin(), vs.end()); |
| 1067 |
sort(vall.begin(), vall.end()); |
| 1068 |
|
| 1069 |
vector<unsigned> vr; |
| 1070 |
std::set_difference(vall.begin(), vall.end(), vs.begin(), vs.end(), std::inserter(vr, vr.end())); //Difference between vall and vs > Extract to vr |
| 1071 |
|
| 1072 |
for (auto e : vr) //Label these as mortar. Some points can be duplicates (e.g. stone divided in several chunks and outliers assigned to mortar several times), so this is checked below |
| 1073 |
{ |
| 1074 |
mortarPoints[1].push_back(e); |
| 1075 |
stonePairs[0][e].second = -1; //remove from stone (label -1) |
| 1076 |
} |
| 1077 |
|
| 1078 |
//Remove duplicates |
| 1079 |
sort(mortarPoints[1].begin(), mortarPoints[1].end()); |
| 1080 |
mortarPoints[1].erase(unique(mortarPoints[1].begin(), mortarPoints[1].end()), mortarPoints[1].end()); |
| 1081 |
|
| 1082 |
if (!stId.empty() && stId[0] == -1) |
| 1083 |
{ |
| 1084 |
vector<unsigned> tempMortar = mortarPoints[1]; |
| 1085 |
sort(tempMortar.begin(), tempMortar.end()); |
| 1086 |
|
| 1087 |
vector<unsigned> resMortar; |
| 1088 |
std::set_difference(tempMortar.begin(), tempMortar.end(), vs.begin(), vs.end(), std::inserter(resMortar, resMortar.end())); |
| 1089 |
|
| 1090 |
mortarPoints[1] = resMortar; |
| 1091 |
} |
| 1092 |
break; |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
m_app->removeFromDB(poly, false); |
| 1097 |
} |
| 1098 |
|
| 1099 |
if (cloudStone) |
| 1100 |
cloudStone->unallocateVisibilityArray(); |
| 1101 |
if (cloudMortar) |
| 1102 |
cloudMortar->unallocateVisibilityArray(); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (win) |
| 1107 |
{ |
| 1108 |
win->redraw(true, true); |
| 1109 |
} |
| 1110 |
|
| 1111 |
// Create Stone cloud |
| 1112 |
ccPointCloud* new_CloudStone = new ccPointCloud("Stone - cloud"); |
| 1113 |
CCCoreLib::ScalarField* new_stIdSF = nullptr; |
| 1114 |
if (cloudStone) |
| 1115 |
{ |
| 1116 |
// Adding points to the final Stone cloud |
| 1117 |
vector<pair<unsigned, int>> rStPairs; |
| 1118 |
|
| 1119 |
// Removing points that are going to the mortar cloud (with index -1) |
| 1120 |
rStPairs.reserve(stonePairs.size()); |
| 1121 |
for (auto e : stonePairs[0]) |
| 1122 |
{ |
| 1123 |
if (e.second != -1) |
| 1124 |
rStPairs.push_back(e); |
| 1125 |
} |
| 1126 |
rStPairs.shrink_to_fit(); |
| 1127 |
|
| 1128 |
if (!new_CloudStone->reserve(rStPairs.size()) || !new_CloudStone->reserveTheRGBTable()) |
| 1129 |
{ |
| 1130 |
delete new_CloudStone; |
| 1131 |
ccLog::Error("Not enough memory"); |
| 1132 |
return; |
| 1133 |
} |
| 1134 |
|
| 1135 |
for (size_t i = 0; i < rStPairs.size(); ++i) |
| 1136 |
{ |
| 1137 |
unsigned stoneCloudIndex = rStPairs[i].first; |
| 1138 |
new_CloudStone->addPoint(*cloudStone->getPoint(stoneCloudIndex)); |
| 1139 |
new_CloudStone->addColor(cloudStone->getPointColor(stoneCloudIndex)); |
| 1140 |
} |
| 1141 |
|
| 1142 |
// Adding points coming from mortar |
| 1143 |
if (stonePairs[1].size() != 0 && cloudMortar) |
| 1144 |
{ |
| 1145 |
if (!new_CloudStone->reserve(new_CloudStone->size() + static_cast<unsigned>(stonePairs[1].size()))) |
| 1146 |
{ |
| 1147 |
delete new_CloudStone; |
| 1148 |
ccLog::Error("Not enough memory"); |
| 1149 |
return; |
| 1150 |
} |
| 1151 |
|
| 1152 |
for (size_t i = 0; i < stonePairs[1].size(); ++i) |
| 1153 |
{ |
| 1154 |
unsigned mortarPointIndex = stonePairs[1][i].first; |
| 1155 |
new_CloudStone->addPoint(*cloudMortar->getPoint(mortarPointIndex)); |
| 1156 |
new_CloudStone->addColor(cloudMortar->getPointColor(mortarPointIndex)); |
| 1157 |
|
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
// Adding scalar field to the stone cloud |
| 1162 |
{ |
| 1163 |
int nsfIdx = new_CloudStone->getScalarFieldIndexByName("Stone Index"); |
| 1164 |
if (nsfIdx < 0) |
| 1165 |
{ |
| 1166 |
nsfIdx = new_CloudStone->addScalarField("Stone Index"); |
| 1167 |
} |
| 1168 |
|
| 1169 |
new_stIdSF = new_CloudStone->getScalarField(nsfIdx); |
| 1170 |
if (new_stIdSF) |
| 1171 |
{ |
| 1172 |
for (size_t i = 0; i < rStPairs.size(); ++i) |
| 1173 |
{ |
| 1174 |
ScalarType value = static_cast<ScalarType>(rStPairs[i].second); |
| 1175 |
new_stIdSF->setValue(static_cast<unsigned>(i), value); |
| 1176 |
} |
| 1177 |
|
| 1178 |
// Adding points coming from mortar |
| 1179 |
for (size_t i = 0; i < stonePairs[1].size(); ++i) |
| 1180 |
{ |
| 1181 |
ScalarType value = static_cast<ScalarType>(stonePairs[1][i].second); |
| 1182 |
new_stIdSF->setValue(static_cast<unsigned>(rStPairs.size() + i), value); |
| 1183 |
} |
| 1184 |
|
| 1185 |
new_stIdSF->computeMinAndMax(); |
| 1186 |
new_CloudStone->setCurrentDisplayedScalarField(nsfIdx); |
| 1187 |
new_CloudStone->showSF(true); |
| 1188 |
} |
| 1189 |
else |
| 1190 |
{ |
| 1191 |
ccLog::Warning("Failed to create stone index SF"); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
new_CloudStone->setPointSize(cloudStone->getPointSize()); |
| 1196 |
m_app->addToDB(new_CloudStone, true, true, false, true); |
| 1197 |
} |
| 1198 |
|
| 1199 |
// Create Motrar cloud |
| 1200 |
ccPointCloud* new_CloudMortar = new ccPointCloud("Mortar - cloud"); |
| 1201 |
if (cloudMortar) |
| 1202 |
{ |
| 1203 |
if (!new_CloudMortar->reserve(mortarPoints.size()) || !new_CloudMortar->reserveTheRGBTable()) |
| 1204 |
{ |
| 1205 |
delete new_CloudMortar; |
| 1206 |
ccLog::Error("Not enough memory"); |
| 1207 |
return; |
| 1208 |
} |
| 1209 |
|
| 1210 |
new_CloudMortar->showColors(true); |
| 1211 |
|
| 1212 |
for (auto index : mortarPoints[0]) |
| 1213 |
{ |
| 1214 |
if (index != -1) |
| 1215 |
{ |
| 1216 |
new_CloudMortar->addPoint(*cloudMortar->getPoint(index)); |
| 1217 |
new_CloudMortar->addColor(cloudMortar->getPointColor(index)); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
// Points coming from stone cloud |
| 1222 |
if (mortarPoints[1].size() != 0) |
| 1223 |
{ |
| 1224 |
if (!new_CloudMortar->reserve(new_CloudMortar->size() + static_cast<unsigned>(mortarPoints[1].size()))) |
| 1225 |
{ |
| 1226 |
delete new_CloudMortar; |
| 1227 |
ccLog::Error("Not enough memory"); |
| 1228 |
return; |
| 1229 |
} |
| 1230 |
|
| 1231 |
for (auto index : mortarPoints[1]) |
| 1232 |
{ |
| 1233 |
if (index != -1) |
| 1234 |
{ |
| 1235 |
new_CloudMortar->addPoint(*cloudMortar->getPoint(index)); |
| 1236 |
new_CloudMortar->addColor(cloudMortar->getPointColor(index)); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
new_CloudMortar->setPointSize(cloudMortar->getPointSize()); |
| 1242 |
m_app->addToDB(new_CloudMortar, true, true, false, true); |
| 1243 |
} |
| 1244 |
|
| 1245 |
// Mortar maps |
| 1246 |
{ |
| 1247 |
auto_seg_log << DateStamp() << " Starting mortar maps generation..." << endl; |
| 1248 |
|
| 1249 |
// Moving the clouds to center in case they were not |
| 1250 |
CCVector3 minBox0, maxBox0; |
| 1251 |
if (cloudMortar) |
| 1252 |
{ |
| 1253 |
cloudMortar->getBoundingBox(minBox0, maxBox0); |
| 1254 |
|
| 1255 |
new_CloudStone->translate(-minBox0); |
| 1256 |
new_CloudMortar->translate(-minBox0); |
| 1257 |
} |
| 1258 |
|
| 1259 |
ccPointCloud* mortarMaps = GetMortarMaps(new_CloudStone, new_CloudMortar); |
| 1260 |
|
| 1261 |
// Move all the clouds back |
| 1262 |
if (cloudMortar) |
| 1263 |
{ |
| 1264 |
new_CloudStone->translate(minBox0); |
| 1265 |
new_CloudMortar->translate(minBox0); |
| 1266 |
if (mortarMaps) |
| 1267 |
{ |
| 1268 |
mortarMaps->translate(minBox0); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
|
| 1272 |
if (mortarMaps) |
| 1273 |
{ |
| 1274 |
m_app->addToDB(mortarMaps, true, true, false, true); |
| 1275 |
auto_seg_log << DateStamp() << " Mortar maps OK" << endl; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
// Creating new vectors |
| 1280 |
if (new_stIdSF && new_stIdSF->size() > 0) |
| 1281 |
{ |
| 1282 |
vector<pair<unsigned, int>> nstonePairs; |
| 1283 |
nstonePairs.reserve(new_stIdSF->size()); |
| 1284 |
|
| 1285 |
vector<unsigned> nallStIdx; |
| 1286 |
nallStIdx.reserve(new_stIdSF->size()); |
| 1287 |
for (unsigned i = 0; i < new_stIdSF->size(); i++) |
| 1288 |
{ |
| 1289 |
nstonePairs.push_back(make_pair(i, new_stIdSF->getValue(i))); |
| 1290 |
nallStIdx.push_back(static_cast<unsigned>(new_stIdSF->getValue(i))); |
| 1291 |
} |
| 1292 |
sort(nallStIdx.begin(), nallStIdx.end()); |
| 1293 |
nallStIdx.erase(unique(nallStIdx.begin(), nallStIdx.end()), nallStIdx.end()); |
| 1294 |
|
| 1295 |
sort(nstonePairs.begin(), nstonePairs.end(), [](auto &left, auto &right) {return left.second < right.second; }); |
| 1296 |
|
| 1297 |
// Correcting indexes |
| 1298 |
int old_idx = nstonePairs[0].second; |
| 1299 |
int new_idx = 0; |
| 1300 |
|
| 1301 |
vector<vector<int>> vecStones; |
| 1302 |
vecStones.reserve(nstonePairs.size()); |
| 1303 |
for (size_t i = 0; i < nstonePairs.size(); i++) |
| 1304 |
{ |
| 1305 |
if (nstonePairs[i].second == old_idx) |
| 1306 |
{ |
| 1307 |
nstonePairs[i].second = new_idx; |
| 1308 |
vecStones[new_idx].push_back(nstonePairs[i].first); |
| 1309 |
} |
| 1310 |
else |
| 1311 |
{ |
| 1312 |
++new_idx; |
| 1313 |
old_idx = nstonePairs[i].second; |
| 1314 |
nstonePairs[i].second = new_idx; |
| 1315 |
vecStones[new_idx].push_back(nstonePairs[i].first); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
sort(nstonePairs.begin(), nstonePairs.end(), [](auto &left, auto &right) {return left.first < right.first; }); |
| 1320 |
|
| 1321 |
// Shuffling scalar field for better visuals |
| 1322 |
vector<int> stIdShuffle; |
| 1323 |
stIdShuffle.reserve(nstonePairs.size()); |
| 1324 |
for (const auto& p : nstonePairs) |
| 1325 |
{ |
| 1326 |
stIdShuffle.push_back(p.second); |
| 1327 |
} |
| 1328 |
sort(stIdShuffle.begin(), stIdShuffle.end()); |
| 1329 |
|
| 1330 |
stIdShuffle.erase(unique(stIdShuffle.begin(), stIdShuffle.end()), stIdShuffle.end()); |
| 1331 |
|
| 1332 |
auto rng = std::default_random_engine{}; |
| 1333 |
std::shuffle(stIdShuffle.begin(), stIdShuffle.end(), rng); |
| 1334 |
|
| 1335 |
for (unsigned i = 0; i < new_stIdSF->size(); i++) |
| 1336 |
{ |
| 1337 |
ScalarType value = static_cast<ScalarType>(stIdShuffle[nstonePairs[i].second]); |
| 1338 |
new_stIdSF->setValue(i, value); |
| 1339 |
} |
| 1340 |
|
| 1341 |
// contours in an alternative way using stone indexes and original cloud |
| 1342 |
auto_seg_log << DateStamp() << " Starting contours regeneration" << endl; |
| 1343 |
|
| 1344 |
//generating contour polylines |
| 1345 |
ccHObject* nContoursContainer = new ccHObject("Stone contours"); |
| 1346 |
|
| 1347 |
size_t polylineIndex = 0; |
| 1348 |
for (const auto& ve : vecStones) |
| 1349 |
{ |
| 1350 |
if (ve.size() != 0) |
| 1351 |
{ |
| 1352 |
auto_seg_log << DateStamp() << " Polyline " << polylineIndex + 1 << "/" << vecStones.size() << endl; |
| 1353 |
ccPolyline* contour = ContourPoly2(new_CloudStone, ve, QString("stone %1").arg(polylineIndex)); |
| 1354 |
if (contour) |
| 1355 |
{ |
| 1356 |
nContoursContainer->addChild(contour); |
| 1357 |
++polylineIndex; |
| 1358 |
} |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
auto_seg_log << DateStamp() << " Contours regeneration done" << endl; |
| 1363 |
|
| 1364 |
if (nContoursContainer->getChildrenNumber() != 0) |
| 1365 |
{ |
| 1366 |
m_app->addToDB(nContoursContainer, true, true, false, true); |
| 1367 |
} |
| 1368 |
else |
| 1369 |
{ |
| 1370 |
delete nContoursContainer; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
if (win) |
| 1375 |
{ |
| 1376 |
win->setView(CC_FRONT_VIEW); |
| 1377 |
} |
| 1378 |
} |