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
|
//##########################################################################
//# #
//# 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 "FeaturesInterface.h"
//qCC_db
#include <ccScalarField.h>
//system
#include <assert.h>
using namespace masc;
bool Feature::CheckSFExistence(ccPointCloud* cloud, const char* resultSFName)
{
if (!cloud || !resultSFName)
{
assert(false);
return false;
}
int sfIdx = cloud->getScalarFieldIndexByName(resultSFName);
return (sfIdx >= 0);
}
CCCoreLib::ScalarField* Feature::PrepareSF(ccPointCloud* cloud, const char* resultSFName, SFCollector* generatedScalarFields/*=nullptr*/, SFCollector::Behavior behavior/*=SFCollector::CAN_REMOVE*/)
{
if (!cloud || !resultSFName)
{
//invalid input parameters
assert(false);
return nullptr;
}
CCCoreLib::ScalarField* resultSF = nullptr;
int sfIdx = cloud->getScalarFieldIndexByName(resultSFName);
if (sfIdx >= 0)
{
// ccLog::Warning("Existing SF: " + QString(resultSFName) + ", do not store in generatedScalarFields");
resultSF = cloud->getScalarField(sfIdx);
}
else
{
// ccLog::Warning("SF does not exist, create it: " + QString(resultSFName) + ", SFCollector::Behavior " + QString::number(behavior));
ccScalarField* newSF = new ccScalarField(resultSFName);
if (!newSF->resizeSafe(cloud->size()))
{
ccLog::Warning("Not enough memory");
newSF->release();
return nullptr;
}
cloud->addScalarField(newSF);
if (generatedScalarFields)
{
//track the generated scalar-field
generatedScalarFields->push(cloud, newSF, behavior);
}
resultSF = newSF;
resultSF->fill(CCCoreLib::NAN_VALUE);
}
assert(resultSF);
return resultSF;
}
ScalarType Feature::PerformMathOp(double s1, double s2, Operation op)
{
ScalarType s = CCCoreLib::NAN_VALUE;
switch (op)
{
case Feature::MINUS:
s = static_cast<ScalarType>(s1 - s2);
break;
case Feature::PLUS:
s = static_cast<ScalarType>(s1 + s2);
break;
case Feature::DIVIDE:
if (std::abs(s2) > std::numeric_limits<ScalarType>::epsilon())
s = static_cast<ScalarType>(s1 / s2);
break;
case Feature::MULTIPLY:
s = static_cast<ScalarType>(s1 * s2);
break;
default:
assert(false);
break;
}
return s;
}
bool Feature::PerformMathOp(CCCoreLib::ScalarField* sf1, const CCCoreLib::ScalarField* sf2, Feature::Operation op)
{
if (!sf1 || !sf2 || sf1->size() != sf2->size() || op == Feature::NO_OPERATION)
{
//invalid input parameters
assert(false);
return false;
}
for (unsigned i = 0; i < sf1->size(); ++i)
{
ScalarType s1 = sf1->getValue(i);
ScalarType s2 = sf2->getValue(i);
ScalarType s = PerformMathOp(s1, s2, op);
sf1->setValue(i, s);
}
sf1->computeMinAndMax();
return true;
}
bool Feature::PerformMathOp(const IScalarFieldWrapper& sf1, const IScalarFieldWrapper& sf2, Operation op, CCCoreLib::ScalarField* outSF)
{
if (!outSF || sf1.size() != sf2.size() || sf1.size() != outSF->size() || op == Feature::NO_OPERATION)
{
//invalid input parameters
assert(false);
return false;
}
for (unsigned i = 0; i < sf1.size(); ++i)
{
double s1 = sf1.pointValue(i);
double s2 = sf2.pointValue(i);
ScalarType s = PerformMathOp(s1, s2, op);
outSF->setValue(i, s);
}
outSF->computeMinAndMax();
return true;
}
bool Feature::SaveSources(const Source::Set& sources, QString filename)
{
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text))
{
ccLog::Warning("Failed to open file for writing: " + filename);
return false;
}
QTextStream stream(&file);
stream << "#Features_SF" << endl;
for (const Source& s : sources)
{
stream << s.type << ":" << s.name << endl;
}
return true;
}
bool Feature::LoadSources(Source::Set& sources, QString filename)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
ccLog::Warning("Failed to open file for reading: " + filename);
return false;
}
QTextStream stream(&file);
QString header = stream.readLine();
if (!header.startsWith("#Features_SF"))
{
ccLog::Warning("Unexpected header");
return false;
}
while (true)
{
QString line = stream.readLine();
if (line.isNull())
break;
if (line.isEmpty())
continue; //unexpected but we can survive
QStringList tokens = line.split(':');
if (tokens.size() != 2)
{
ccLog::Warning("Malformed file");
return false;
}
Source src;
bool ok = false;
int sourceType = tokens[0].toInt(&ok);
if (!ok || sourceType < Feature::Source::ScalarField || sourceType > Feature::Source::Blue)
{
ccLog::Warning("Unhandled source type");
return false;
}
src.type = static_cast<Feature::Source::Type>(sourceType);
src.name = tokens[1];
sources.push_back(src);
}
return true;
}
bool Feature::ExtractSources(const Set& features, Source::Set& sources)
{
sources.clear();
try
{
sources.reserve(features.size());
}
catch (const std::bad_alloc&)
{
ccLog::Warning("Not enough memory");
return false;
}
for (Feature::Shared f : features)
{
sources.push_back(f->source);
}
return true;
}
|