1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
//##########################################################################
//# #
//# CLOUDCOMPARE PLUGIN: q3DMASC #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: Dimitri Lague / CNRS / UEB #
//# #
//##########################################################################
#include "ContextBasedFeature.h"
//Local
#include "q3DMASCTools.h"
//qCC_db
#include <ccScalarField.h>
//Qt
#include <QMutex>
#if defined(_OPENMP)
#include <omp.h>
#endif
using namespace masc;
bool ContextBasedFeature::checkValidity(QString corePointRole, QString &error) const
{
if (!Feature::checkValidity(corePointRole, error))
{
return false;
}
if (type == Invalid)
{
assert(false);
error = "invalid feature type";
return false;
}
if (!cloud1)
{
error = "one cloud is required to compute context-based features";
return false;
}
CCCoreLib::ScalarField* classifSF = Tools::GetClassificationSF(cloud1);
if (!classifSF)
{
error = QString("Context cloud (%1) has no classification field").arg(cloud1Label);
return false;
}
if (classifSF->size() < cloud1->size())
{
error = QString("Context cloud (%1) has an invalid classification field").arg(cloud1Label);
return false;
}
if (!scaled() && kNN < 1)
{
error = QString("invalid kNN value for a scale-less context-based feature (%1)").arg(kNN);
return false;
}
return true;
}
bool ContextBasedFeature::prepare( const CorePoints& corePoints,
QString& errorMessage,
CCCoreLib::GenericProgressCallback* progressCb/*=nullptr*/,
SFCollector* generatedScalarFields/*=nullptr*/)
{
if (!corePoints.cloud)
{
//invalid input
assert(false);
errorMessage = "internal error (no input core points)";
return false;
}
if (!cloud1)
{
//invalid input
assert(false);
errorMessage = "internal error (no contextual cloud)";
return false;
}
if (!checkValidity(corePoints.role, errorMessage))
{
assert(false);
return false;
}
CCCoreLib::ScalarField* classifSF = Tools::GetClassificationSF(cloud1);
if (!classifSF || classifSF->size() < cloud1->size())
{
assert(false);
//already checked by 'checkValidity'
return false;
}
cloud1->setCurrentOutScalarField(cloud1->getScalarFieldIndexByName(classifSF->getName()));
//build the final SF name
QString typeStr = ToString(type);
QString resultSFName = typeStr + "_" + cloud1Label + "_" + QString::number(ctxClassLabel);
if (scaled())
{
resultSFName += "@" + QString::number(scale);
}
else
{
resultSFName += "@kNN=" + QString::number(kNN);
}
//and the scalar field
assert(!sf);
sf1WasAlreadyExisting = CheckSFExistence(corePoints.cloud, qPrintable(resultSFName));
sf = PrepareSF(corePoints.cloud, qPrintable(resultSFName), generatedScalarFields, SFCollector::CAN_REMOVE);
if (!sf)
{
errorMessage = QString("[ContextBasedFeature::prepare] Failed to prepare scalar %1 @ scale %2").arg(resultSFName).arg(scale);
return false;
}
source.name = sf->getName();
// NOT NECESSARY IF THE VALUE IS ALREADY COMPUTED
if (!scaled() && !sf1WasAlreadyExisting) //with 'kNN' neighbors, we can compute the values right away
{
unsigned pointCount = corePoints.size();
QString logMessage = "Computing " + typeStr
+ " on cloud " + corePoints.cloud->getName() + " (" + QString::number(pointCount) + " points)"
+ " with context cloud " + cloud1Label
+ " (class " + QString::number(ctxClassLabel) + ")";
//first: look for the number of points in the relevent class
const ScalarType fClass = static_cast<ScalarType>(ctxClassLabel);
unsigned classCount = 0;
for (unsigned i = 0; i < classifSF->size(); ++i)
{
if (classifSF->getValue(i) == fClass)
++classCount;
}
if (classCount >= static_cast<unsigned>(kNN))
{
ccPointCloud classCloud;
if (!classCloud.reserve(classCount))
{
errorMessage = "Not enough memory";
return false;
}
for (unsigned i = 0; i < classifSF->size(); ++i)
{
if (classifSF->getValue(i) == fClass)
{
classCloud.addPoint(*cloud1->getPoint(i));
}
}
//compute the octree
ccLog::Print(QString("Computing octree of class %1 (%2 points)").arg(ctxClassLabel).arg(classCount));
ccOctree::Shared classOctree = classCloud.computeOctree(progressCb);
if (!classOctree)
{
errorMessage = "[ContextBasedFeature::prepare] Failed to compute octree (not enough memory?)";
return false;
}
//now extract the neighborhoods
unsigned char octreeLevel = classOctree->findBestLevelForAGivenPopulationPerCell(static_cast<unsigned>(std::max(3, kNN)));
ccLog::Print(QString("[Initial octree level] level = %1").arg(octreeLevel));
if (progressCb)
{
progressCb->setMethodTitle(qPrintable("Compute " + typeStr));
progressCb->setInfo(qPrintable(logMessage));
}
ccLog::Print(logMessage);
CCCoreLib::NormalizedProgress nProgress(progressCb, pointCount);
QMutex mutex;
double meanNeighborhoodSize = 0;
int tenth = pointCount / 10;
bool cancelled = false;
#ifndef _DEBUG
#if defined(_OPENMP)
#pragma omp parallel for num_threads(std::max(1, omp_get_max_threads() - 2))
#endif
#endif
for (int i = 0; i < static_cast<int>(pointCount); ++i)
{
if (!cancelled)
{
const CCVector3* P = corePoints.cloud->getPoint(i);
CCCoreLib::ReferenceCloud Yk(&classCloud);
double maxSquareDist = 0;
ScalarType s = CCCoreLib::NAN_VALUE;
int neighborhoodSize = 0;
if (classOctree->findPointNeighbourhood(P, &Yk, static_cast<unsigned>(kNN), octreeLevel, maxSquareDist, 0, &neighborhoodSize) >= static_cast<unsigned>(kNN))
{
CCVector3d sumQ(0, 0, 0);
for (int k = 0; k < kNN; ++k)
{
sumQ += CCVector3d::fromArray(Yk.getPoint(k)->u);
}
switch (type)
{
case DZ:
s = static_cast<ScalarType>(P->z - sumQ.z / kNN);
break;
case DH:
s = static_cast<ScalarType>(sqrt(pow(P->x - sumQ.x / kNN, 2.0) + pow(P->y - sumQ.y / kNN, 2.0)));
break;
}
}
if (i && (i % tenth) == 0)
{
double density = meanNeighborhoodSize / tenth;
if (density < 1.1)
{
if (octreeLevel + 1 < CCCoreLib::DgmOctree::MAX_OCTREE_LEVEL)
++octreeLevel;
}
else while (density > 2.9)
{
if (octreeLevel <= 5)
break;
--octreeLevel;
density /= 2.0;
}
ccLog::Print(QString("[Adaptative octree level] Mean neighborhood size: %1 --> new level = %2").arg(meanNeighborhoodSize / tenth).arg(octreeLevel));
meanNeighborhoodSize = 0;
}
else
{
meanNeighborhoodSize += neighborhoodSize;
}
sf->setValue(i, s);
if (progressCb)
{
mutex.lock();
cancelled = !nProgress.oneStep();
mutex.unlock();
if (cancelled)
{
//process cancelled by the user
errorMessage = "[ContextBasedFeature] Process cancelled";
}
}
}
}
if (progressCb)
{
progressCb->stop();
}
if (cancelled)
{
sf->computeMinAndMax();
return false;
}
} // classCount >= kNN
else // classCount < kNN
{
//specific case: not enough points of this class in the whole cloud!
//sf->fill(NAN_VALUE); //already the case
ccLog::Warning(QString("Cloud %1 has less than %2 points of class %3").arg(cloud1Label).arg(classCount).arg(ctxClassLabel));
}
sf->computeMinAndMax();
}
return true;
}
bool ContextBasedFeature::computeValue(CCCoreLib::DgmOctree::NeighboursSet& pointsInNeighbourhood, const CCVector3& queryPoint, ScalarType& outputValue) const
{
const ScalarType fClass = static_cast<ScalarType>(ctxClassLabel);
CCVector3d sumQ(0, 0, 0);
unsigned validCount = 0;
for (CCCoreLib::DgmOctree::PointDescriptor& Pd : pointsInNeighbourhood)
{
//we only consider points with the right class!!!
if (cloud1->getPointScalarValue(Pd.pointIndex) != fClass)
continue;
sumQ += CCVector3d::fromArray(Pd.point->u);
++validCount;
}
if (validCount == 0)
{
outputValue = CCCoreLib::NAN_VALUE;
return true;
}
switch (type)
{
case DZ:
outputValue = static_cast<ScalarType>(queryPoint.z - sumQ.z / validCount);
break;
case DH:
outputValue = static_cast<ScalarType>(sqrt(pow(queryPoint.x - sumQ.x / validCount, 2.0) + pow(queryPoint.y - sumQ.y / validCount, 2.0)));
break;
default:
assert(false);
outputValue = CCCoreLib::NAN_VALUE;
return false;
}
return true;
}
bool ContextBasedFeature::finish(const CorePoints& corePoints, QString& error)
{
if (!corePoints.cloud)
{
//invalid input
assert(false);
error = "internal error (no input core points)";
return false;
}
bool success = true;
if (sf)
{
sf->computeMinAndMax();
//update display
//if (corePoints.cloud->getDisplay())
{
int sfIndex = corePoints.cloud->getScalarFieldIndexByName(sf->getName());
corePoints.cloud->setCurrentDisplayedScalarField(sfIndex);
//corePoints.cloud->getDisplay()->redraw();
//QCoreApplication::processEvents();
}
}
return success;
}
QString ContextBasedFeature::toString() const
{
//use the default keyword + number of neighbors + the scale + the context class
QString str = ToString(type);
if (!scaled())
{
if (kNN != 1)
str += QString::number(kNN);
str += "_SC0";
}
else
{
str += "_SC" + QString::number(scale);
}
str += "_" + cloud1Label + "_" + QString::number(ctxClassLabel);
return str;
}
|