| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: qCork # |
| 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: Daniel Girardeau-Montaut # |
| 15 |
//# # |
| 16 |
//########################################################################## |
| 17 |
|
| 18 |
#include "qCork.h" |
| 19 |
|
| 20 |
//qCC_db |
| 21 |
#include <ccMesh.h> |
| 22 |
#include <ccPointCloud.h> |
| 23 |
|
| 24 |
//dialog |
| 25 |
#include "ccCorkDlg.h" |
| 26 |
|
| 27 |
//Qt |
| 28 |
#include <QMainWindow> |
| 29 |
#include <QtGui> |
| 30 |
#include <QProgressDialog> |
| 31 |
#include <QtConcurrentRun> |
| 32 |
|
| 33 |
//Cork |
| 34 |
#ifdef _MSC_VER |
| 35 |
#pragma warning( push ) |
| 36 |
#pragma warning( disable: 4005 ) // maco redefinition warnings |
| 37 |
#endif |
| 38 |
#include <mesh/corkMesh.h> |
| 39 |
#ifdef _MSC_VER |
| 40 |
#pragma warning( pop ) |
| 41 |
#endif |
| 42 |
|
| 43 |
//system |
| 44 |
#if defined(CC_WINDOWS) |
| 45 |
#include "windows.h" |
| 46 |
#else |
| 47 |
#include <time.h> |
| 48 |
#include <unistd.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
qCork::qCork(QObject* parent/*=nullptr*/) |
| 53 |
: QObject(parent) |
| 54 |
, ccStdPluginInterface(":/CC/plugin/qCork/info.json") |
| 55 |
, m_action(nullptr) |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
QList<QAction *> qCork::getActions() |
| 60 |
{ |
| 61 |
//default action |
| 62 |
if (!m_action) |
| 63 |
{ |
| 64 |
m_action = new QAction(getName(), this); |
| 65 |
m_action->setToolTip(getDescription()); |
| 66 |
m_action->setIcon(getIcon()); |
| 67 |
//connect signal |
| 68 |
connect(m_action, &QAction::triggered, this, &qCork::doAction); |
| 69 |
} |
| 70 |
|
| 71 |
return QList<QAction *>{ m_action }; |
| 72 |
} |
| 73 |
|
| 74 |
void qCork::onNewSelection(const ccHObject::Container& selectedEntities) |
| 75 |
{ |
| 76 |
if (m_action) |
| 77 |
{ |
| 78 |
//we need two and only two meshes! |
| 79 |
m_action->setEnabled(selectedEntities.size() == 2 |
| 80 |
&& selectedEntities[0]->isKindOf(CC_TYPES::MESH) |
| 81 |
&& selectedEntities[1]->isKindOf(CC_TYPES::MESH)); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
bool ToCorkMesh(const ccMesh* in, CorkMesh& out, ccMainAppInterface* app = nullptr) |
| 86 |
{ |
| 87 |
if (!in || !in->getAssociatedCloud()) |
| 88 |
{ |
| 89 |
if (app) |
| 90 |
app->dispToConsole("Invalid input mesh!", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 91 |
assert(false); |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
ccGenericPointCloud* vertices = in->getAssociatedCloud(); |
| 96 |
assert(vertices); |
| 97 |
|
| 98 |
unsigned triCount = in->size(); |
| 99 |
unsigned vertCount = vertices ? vertices->size() : 0; |
| 100 |
|
| 101 |
std::vector<CorkMesh::Tri>& outTris = out.getTris(); |
| 102 |
std::vector<CorkVertex>& outVerts = out.getVerts(); |
| 103 |
try |
| 104 |
{ |
| 105 |
outVerts.resize(vertCount); |
| 106 |
outTris.resize(triCount); |
| 107 |
} |
| 108 |
catch (const std::bad_alloc&) |
| 109 |
{ |
| 110 |
if (app) |
| 111 |
app->dispToConsole("Not enough memory!", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if (outVerts.empty() || outTris.empty()) |
| 116 |
{ |
| 117 |
if (app) |
| 118 |
app->dispToConsole(QString("[Cork] Input mesh '%1' is empty?!").arg(in->getName()), ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
//import triangle indexes |
| 123 |
{ |
| 124 |
for (unsigned i = 0; i < triCount; i++) |
| 125 |
{ |
| 126 |
const CCCoreLib::VerticesIndexes* tsi = in->getTriangleVertIndexes(i); |
| 127 |
CorkTriangle corkTri; |
| 128 |
corkTri.a = tsi->i1; |
| 129 |
corkTri.b = tsi->i2; |
| 130 |
corkTri.c = tsi->i3; |
| 131 |
outTris[i].data = corkTri; |
| 132 |
//DGM: it seems that Cork doubles this information?! |
| 133 |
outTris[i].a = tsi->i1; |
| 134 |
outTris[i].b = tsi->i2; |
| 135 |
outTris[i].c = tsi->i3; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
//import vertices |
| 140 |
{ |
| 141 |
for (unsigned i = 0; i < vertCount; i++) |
| 142 |
{ |
| 143 |
const CCVector3* P = vertices->getPoint(i); |
| 144 |
outVerts[i].pos.x = static_cast<double>(P->x); |
| 145 |
outVerts[i].pos.y = static_cast<double>(P->y); |
| 146 |
outVerts[i].pos.z = static_cast<double>(P->z); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
ccMesh* FromCorkMesh(const CorkMesh& in, ccMainAppInterface* app = nullptr) |
| 154 |
{ |
| 155 |
const std::vector<CorkMesh::Tri>& inTris = in.getTris(); |
| 156 |
const std::vector<CorkVertex>& inVerts = in.getVerts(); |
| 157 |
|
| 158 |
if (inTris.empty() || inVerts.empty()) |
| 159 |
{ |
| 160 |
if (app) |
| 161 |
app->dispToConsole("[Cork] Output mesh is empty?!", ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 162 |
return nullptr; |
| 163 |
} |
| 164 |
|
| 165 |
unsigned triCount = static_cast<unsigned>(inTris.size()); |
| 166 |
unsigned vertCount = static_cast<unsigned>(inVerts.size()); |
| 167 |
|
| 168 |
ccPointCloud* vertices = new ccPointCloud("vertices"); |
| 169 |
if (!vertices->reserve(vertCount)) |
| 170 |
{ |
| 171 |
if (app) |
| 172 |
app->dispToConsole("Not enough memory!", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 173 |
delete vertices; |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
|
| 177 |
ccMesh* mesh = new ccMesh(vertices); |
| 178 |
mesh->addChild(vertices); |
| 179 |
if (!mesh->reserve(triCount)) |
| 180 |
{ |
| 181 |
if (app) |
| 182 |
app->dispToConsole("Not enough memory!", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 183 |
delete mesh; |
| 184 |
return nullptr; |
| 185 |
} |
| 186 |
|
| 187 |
//import vertices |
| 188 |
{ |
| 189 |
for (unsigned i = 0; i < vertCount; i++) |
| 190 |
{ |
| 191 |
const CorkVertex& P = inVerts[i]; |
| 192 |
CCVector3 Pout(static_cast<PointCoordinateType>(P.pos.x), |
| 193 |
static_cast<PointCoordinateType>(P.pos.y), |
| 194 |
static_cast<PointCoordinateType>(P.pos.z)); |
| 195 |
vertices->addPoint(Pout); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
//import triangle indexes |
| 200 |
{ |
| 201 |
for (unsigned i = 0; i < triCount; i++) |
| 202 |
{ |
| 203 |
const CorkMesh::Tri& tri = inTris[i]; |
| 204 |
mesh->addTriangle(tri.a, tri.b, tri.c); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
mesh->setVisible(true); |
| 209 |
vertices->setEnabled(false); |
| 210 |
|
| 211 |
return mesh; |
| 212 |
} |
| 213 |
|
| 214 |
//! Boolean operation parameters (for concurrent run) |
| 215 |
struct BoolOpParameters |
| 216 |
{ |
| 217 |
BoolOpParameters() |
| 218 |
: operation(ccCorkDlg::UNION) |
| 219 |
, corkA(nullptr) |
| 220 |
, corkB(nullptr) |
| 221 |
, app(nullptr) |
| 222 |
, meshesAreOk(false) |
| 223 |
{} |
| 224 |
|
| 225 |
ccCorkDlg::CSG_OPERATION operation; |
| 226 |
CorkMesh* corkA; |
| 227 |
CorkMesh* corkB; |
| 228 |
QString nameA; |
| 229 |
QString nameB; |
| 230 |
ccMainAppInterface* app; |
| 231 |
bool meshesAreOk; |
| 232 |
}; |
| 233 |
static BoolOpParameters s_params; |
| 234 |
|
| 235 |
static bool DoPerformBooleanOp() |
| 236 |
{ |
| 237 |
//invalid parameters |
| 238 |
if (!s_params.corkA || !s_params.corkB) |
| 239 |
{ |
| 240 |
assert(false); |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
try |
| 245 |
{ |
| 246 |
QElapsedTimer timer; |
| 247 |
timer.start(); |
| 248 |
|
| 249 |
//check meshes |
| 250 |
s_params.meshesAreOk = true; |
| 251 |
if (false) |
| 252 |
{ |
| 253 |
if (s_params.corkA->isSelfIntersecting()) |
| 254 |
{ |
| 255 |
if (s_params.app) |
| 256 |
s_params.app->dispToConsole(QString("[Cork] Mesh '%1' is self-intersecting! Result may be jeopardized!").arg(s_params.nameA), ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 257 |
s_params.meshesAreOk = false; |
| 258 |
} |
| 259 |
else if (!s_params.corkA->isClosed()) |
| 260 |
{ |
| 261 |
if (s_params.app) |
| 262 |
s_params.app->dispToConsole(QString("[Cork] Mesh '%1' is not closed! Result may be jeopardized!").arg(s_params.nameA), ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 263 |
s_params.meshesAreOk = false; |
| 264 |
} |
| 265 |
if (s_params.corkB->isSelfIntersecting()) |
| 266 |
{ |
| 267 |
if (s_params.app) |
| 268 |
s_params.app->dispToConsole(QString("[Cork] Mesh '%1' is self-intersecting! Result may be jeopardized!").arg(s_params.nameB), ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 269 |
s_params.meshesAreOk = false; |
| 270 |
} |
| 271 |
else if (!s_params.corkB->isClosed()) |
| 272 |
{ |
| 273 |
if (s_params.app) |
| 274 |
s_params.app->dispToConsole(QString("[Cork] Mesh '%1' is not closed! Result may be jeopardized!").arg(s_params.nameB), ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 275 |
s_params.meshesAreOk = false; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
//perform the boolean operation |
| 280 |
switch (s_params.operation) |
| 281 |
{ |
| 282 |
case ccCorkDlg::UNION: |
| 283 |
s_params.corkA->boolUnion(*s_params.corkB); |
| 284 |
break; |
| 285 |
|
| 286 |
case ccCorkDlg::INTERSECT: |
| 287 |
s_params.corkA->boolIsct(*s_params.corkB); |
| 288 |
break; |
| 289 |
|
| 290 |
case ccCorkDlg::DIFF: |
| 291 |
s_params.corkA->boolDiff(*s_params.corkB); |
| 292 |
break; |
| 293 |
|
| 294 |
case ccCorkDlg::SYM_DIFF: |
| 295 |
s_params.corkA->boolXor(*s_params.corkB); |
| 296 |
break; |
| 297 |
|
| 298 |
default: |
| 299 |
assert(false); |
| 300 |
if (s_params.app) |
| 301 |
s_params.app->dispToConsole("[Cork] Unhandled operation?!", ccMainAppInterface::WRN_CONSOLE_MESSAGE); //DGM: can't issue an error message (i.e. with dialog) in another thread! |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
if (s_params.app) |
| 306 |
{ |
| 307 |
// display the duration time |
| 308 |
s_params.app->dispToConsole(QString("[Cork] CSG operation duration: %1 s").arg(timer.elapsed() / 1000.0, 0, 'f', 2)); |
| 309 |
} |
| 310 |
} |
| 311 |
catch (const std::exception& e) |
| 312 |
{ |
| 313 |
if (s_params.app) |
| 314 |
s_params.app->dispToConsole(QString("[Cork] Exception caught: %1").arg(e.what()), ccMainAppInterface::WRN_CONSOLE_MESSAGE); |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
void qCork::doAction() |
| 322 |
{ |
| 323 |
assert(m_app); |
| 324 |
if (!m_app) |
| 325 |
return; |
| 326 |
|
| 327 |
const ccHObject::Container& selectedEntities = m_app->getSelectedEntities(); |
| 328 |
size_t selNum = selectedEntities.size(); |
| 329 |
if (selNum != 2 |
| 330 |
|| !selectedEntities[0]->isKindOf(CC_TYPES::MESH) |
| 331 |
|| !selectedEntities[1]->isKindOf(CC_TYPES::MESH)) |
| 332 |
{ |
| 333 |
assert(false); |
| 334 |
m_app->dispToConsole("Select two and only two meshes!", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
ccMesh* meshA = static_cast<ccMesh*>(selectedEntities[0]); |
| 339 |
ccMesh* meshB = static_cast<ccMesh*>(selectedEntities[1]); |
| 340 |
|
| 341 |
//show dialog to let the user choose the operation to perform |
| 342 |
ccCorkDlg cDlg(m_app->getMainWindow()); |
| 343 |
cDlg.setNames(meshA->getName(), meshB->getName()); |
| 344 |
if (!cDlg.exec()) |
| 345 |
return; |
| 346 |
|
| 347 |
QElapsedTimer timer; |
| 348 |
timer.start(); |
| 349 |
|
| 350 |
if (cDlg.isSwapped()) |
| 351 |
std::swap(meshA, meshB); |
| 352 |
|
| 353 |
//try to convert both meshes to CorkMesh structures |
| 354 |
CorkMesh corkA; |
| 355 |
if (!ToCorkMesh(meshA, corkA, m_app)) |
| 356 |
return; |
| 357 |
CorkMesh corkB; |
| 358 |
if (!ToCorkMesh(meshB, corkB, m_app)) |
| 359 |
return; |
| 360 |
|
| 361 |
//launch process |
| 362 |
{ |
| 363 |
//run in a separate thread |
| 364 |
QProgressDialog pDlg("Operation in progress", QString(), 0, 0, m_app->getMainWindow()); |
| 365 |
pDlg.setWindowTitle("Cork"); |
| 366 |
pDlg.show(); |
| 367 |
QApplication::processEvents(); |
| 368 |
|
| 369 |
s_params.app = m_app; |
| 370 |
s_params.corkA = &corkA; |
| 371 |
s_params.corkB = &corkB; |
| 372 |
s_params.nameA = meshA->getName(); |
| 373 |
s_params.nameB = meshB->getName(); |
| 374 |
s_params.operation = cDlg.getSelectedOperation(); |
| 375 |
|
| 376 |
QFuture<bool> future = QtConcurrent::run(DoPerformBooleanOp); |
| 377 |
|
| 378 |
//wait until process is finished! |
| 379 |
while (!future.isFinished()) |
| 380 |
{ |
| 381 |
#if defined(CC_WINDOWS) |
| 382 |
::Sleep(500); |
| 383 |
#else |
| 384 |
usleep(500 * 1000); |
| 385 |
#endif |
| 386 |
|
| 387 |
pDlg.setValue(pDlg.value() + 1); |
| 388 |
QApplication::processEvents(); |
| 389 |
} |
| 390 |
|
| 391 |
//just to be sure |
| 392 |
s_params.app = nullptr; |
| 393 |
s_params.corkA = s_params.corkB = 0; |
| 394 |
|
| 395 |
pDlg.hide(); |
| 396 |
QApplication::processEvents(); |
| 397 |
|
| 398 |
if (!future.result()) |
| 399 |
{ |
| 400 |
if (m_app) |
| 401 |
m_app->dispToConsole(s_params.meshesAreOk ? "Computation failed!" : "Computation failed! (check console)", ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 402 |
//an error occurred |
| 403 |
return; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
//convert the updated mesh (A) to a new ccMesh structure |
| 408 |
ccMesh* result = FromCorkMesh(corkA); |
| 409 |
|
| 410 |
if (result) |
| 411 |
{ |
| 412 |
meshA->setEnabled(false); |
| 413 |
if (meshB->getDisplay() == meshA->getDisplay()) |
| 414 |
meshB->setEnabled(false); |
| 415 |
|
| 416 |
//set name |
| 417 |
QString opName; |
| 418 |
switch (cDlg.getSelectedOperation()) |
| 419 |
{ |
| 420 |
case ccCorkDlg::UNION: |
| 421 |
opName = "union"; |
| 422 |
break; |
| 423 |
case ccCorkDlg::INTERSECT: |
| 424 |
opName = "isect"; |
| 425 |
break; |
| 426 |
case ccCorkDlg::DIFF: |
| 427 |
opName = "diff"; |
| 428 |
break; |
| 429 |
case ccCorkDlg::SYM_DIFF: |
| 430 |
opName = "sym_diff"; |
| 431 |
break; |
| 432 |
default: |
| 433 |
assert(false); |
| 434 |
break; |
| 435 |
} |
| 436 |
result->setName(QString("(%1).%2.(%3)").arg(meshA->getName()).arg(opName).arg(meshB->getName())); |
| 437 |
|
| 438 |
//normals |
| 439 |
bool hasNormals = false; |
| 440 |
if (meshA->hasTriNormals()) |
| 441 |
hasNormals = result->computePerTriangleNormals(); |
| 442 |
else if (meshA->hasNormals()) |
| 443 |
hasNormals = result->computePerVertexNormals(); |
| 444 |
meshA->showNormals(hasNormals && meshA->normalsShown()); |
| 445 |
|
| 446 |
result->setDisplay(meshA->getDisplay()); |
| 447 |
m_app->addToDB(result); |
| 448 |
result->redrawDisplay(); |
| 449 |
} |
| 450 |
|
| 451 |
//currently selected entities appearance may have changed! |
| 452 |
m_app->refreshAll(); |
| 453 |
|
| 454 |
if (m_app) |
| 455 |
{ |
| 456 |
// display the duration time |
| 457 |
m_app->dispToConsole(QString("[Cork] Total duration: %1 s").arg(timer.elapsed() / 1000.0, 0, 'f', 2)); |
| 458 |
} |
| 459 |
} |