| 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 |
#ifndef Q_CANUPO_TOOLS_HEADER |
| 19 |
#define Q_CANUPO_TOOLS_HEADER |
| 20 |
|
| 21 |
//Local |
| 22 |
#include "classifier.h" |
| 23 |
|
| 24 |
//system |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
class QComboBox; |
| 28 |
class QString; |
| 29 |
|
| 30 |
class ccGenericPointCloud; |
| 31 |
class ccHObject; |
| 32 |
class ccMainAppInterface; |
| 33 |
class ccScalarField; |
| 34 |
|
| 35 |
namespace CCCoreLib { |
| 36 |
class GenericProgressCallback; |
| 37 |
class DgmOctree; |
| 38 |
} |
| 39 |
|
| 40 |
//! Helper: pre-computed cos and sin values between 0 and Pi |
| 41 |
template<unsigned Count> struct CosSinTable |
| 42 |
{ |
| 43 |
float cosines[Count]; |
| 44 |
float sines [Count]; |
| 45 |
|
| 46 |
CosSinTable() |
| 47 |
{ |
| 48 |
for (int i=0; i<Count; ++i) |
| 49 |
{ |
| 50 |
cosines[i] = static_cast<float>( cos(M_PI * i / Count) ); |
| 51 |
sines [i] = static_cast<float>( sin(M_PI * i / Count) ); |
| 52 |
} |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
//! CANUPO generic tools |
| 57 |
class qCanupoTools |
| 58 |
{ |
| 59 |
public: |
| 60 |
|
| 61 |
//! Returns whether two numbers are almost equal or not |
| 62 |
template<typename T> static bool Fpeq(T a, T b, T epsilon = static_cast<T>(1.0e-6)) |
| 63 |
{ |
| 64 |
if (b == 0) |
| 65 |
return std::abs(a) < epsilon; |
| 66 |
|
| 67 |
return std::abs(a/b - 1) <= epsilon; |
| 68 |
} |
| 69 |
|
| 70 |
//! Compares two vectors |
| 71 |
/** \return true if both are 'equivalent' (i.e. same number of values, almost same values, same order) |
| 72 |
**/ |
| 73 |
static bool CompareVectors(const std::vector<float>& first, const std::vector<float>& second); |
| 74 |
|
| 75 |
//! Tests whether a vector contains the values of another one (at the end!) |
| 76 |
/** \return number of matching elements |
| 77 |
**/ |
| 78 |
static size_t TestVectorsOverlap(const std::vector<float>& first, const std::vector<float>& second); |
| 79 |
|
| 80 |
//! Computes the 'descriptors' for various scales on core points only |
| 81 |
static bool ComputeCorePointsDescriptors( CCCoreLib::GenericIndexedCloud* corePoints, |
| 82 |
CorePointDescSet& corePointsDescriptors, |
| 83 |
ccGenericPointCloud* sourceCloud, |
| 84 |
const std::vector<float>& sortedScales, |
| 85 |
bool& invalidDescriptors, |
| 86 |
QString& error, //if any |
| 87 |
unsigned descriptorID = DESC_DIMENSIONALITY, |
| 88 |
int maxThreadCount = 0, |
| 89 |
CCCoreLib::GenericProgressCallback* progressCb = nullptr, |
| 90 |
CCCoreLib::DgmOctree* inputOctree = nullptr, |
| 91 |
std::vector<ccScalarField*>* roughnessSFs = nullptr /*for tests*/); |
| 92 |
|
| 93 |
//! Returns a long description of a given entity (name + [ID]) |
| 94 |
static QString GetEntityName(ccHObject* obj); |
| 95 |
|
| 96 |
//! Returns the cloud associated to the currently selected item of a combox box |
| 97 |
/** Relies on the item associated data (itemData) that should be equal to the cloud's ID. |
| 98 |
**/ |
| 99 |
static ccPointCloud* GetCloudFromCombo(QComboBox* comboBox, ccHObject* dbRoot); |
| 100 |
|
| 101 |
//! Trains a classifier |
| 102 |
static bool TrainClassifier(Classifier& classifier, |
| 103 |
const CorePointDescSet& descriptors1, |
| 104 |
const CorePointDescSet& descriptors2, |
| 105 |
const std::vector<float>& scales, |
| 106 |
ccPointCloud* mscCloud, |
| 107 |
const CorePointDescSet* evaluationDescriptors = nullptr, |
| 108 |
ccMainAppInterface* app = nullptr); |
| 109 |
|
| 110 |
//! Classifer evaluation parameters |
| 111 |
/** See original CANUPO paper ('5. Results' section). |
| 112 |
**/ |
| 113 |
struct EvalParameters |
| 114 |
{ |
| 115 |
//! Default constructor |
| 116 |
EvalParameters() |
| 117 |
: false1(0) |
| 118 |
, false2(0) |
| 119 |
, true1(0) |
| 120 |
, true2(0) |
| 121 |
, mu1(0) |
| 122 |
, mu2(0) |
| 123 |
, var1(0) |
| 124 |
, var2(0) |
| 125 |
{} |
| 126 |
//! Falsely positive |
| 127 |
unsigned false1, false2; |
| 128 |
//! Truly positive |
| 129 |
unsigned true1, true2; |
| 130 |
//! Balanced accuracy |
| 131 |
double ba() const |
| 132 |
{ |
| 133 |
unsigned n1 = true1+false1; |
| 134 |
unsigned n2 = true2+false2; |
| 135 |
double a1 = (n1 ? static_cast<double>(true1)/static_cast<double>(n1) : 0); |
| 136 |
double a2 = (n2 ? static_cast<double>(true2)/static_cast<double>(n2) : 0); |
| 137 |
return (a1+a2)/2; |
| 138 |
} |
| 139 |
|
| 140 |
//! Mean distance to boundary line |
| 141 |
double mu1, mu2; |
| 142 |
//! Variance to boundary line |
| 143 |
double var1, var2; |
| 144 |
//! Fisher Discriminant Ratio |
| 145 |
double fdr() const |
| 146 |
{ |
| 147 |
double v = var1 + var2; |
| 148 |
return v != 0 ? (mu1-mu2)*(mu1-mu2) / v : 0; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
//! Evaluates a classifier |
| 153 |
static bool EvaluateClassifier( const Classifier& classifier, |
| 154 |
const CorePointDescSet& descriptors1, |
| 155 |
const CorePointDescSet& descriptors2, |
| 156 |
const std::vector<float>& scales, |
| 157 |
EvalParameters& params); |
| 158 |
}; |
| 159 |
|
| 160 |
#endif //Q_CANUPO_TOOLS_HEADER |