From 5113ba3f4cb29ef6c2b28ba0332d462ef1b93ee4 Mon Sep 17 00:00:00 2001 From: Daniel Girardeau-Montaut Date: Thu, 25 Oct 2018 10:09:38 +0200 Subject: [PATCH] Glitch fix --- Features.h | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ q3DMASC.cpp | 100 +++++----------------------------------------- 2 files changed, 120 insertions(+), 91 deletions(-) create mode 100644 Features.h diff --git a/Features.h b/Features.h new file mode 100644 index 0000000..f613c5a --- /dev/null +++ b/Features.h @@ -0,0 +1,111 @@ +#pragma once + +//########################################################################## +//# # +//# 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 # +//# # +//########################################################################## + +//qCC_db +#include +//CCLib +#include + +//Qt +#include + +class IScalarFieldWrapper +{ +public: + virtual double pointValue(unsigned index) const = 0; + virtual bool isValid() const = 0; +}; + +class ScalarFieldWrapper : public IScalarFieldWrapper +{ +public: + ScalarFieldWrapper(CCLib::ScalarField* sf) + : m_sf(sf) + {} + + virtual inline double pointValue(unsigned index) const override { return m_sf->at(index); } + virtual inline bool isValid() const { return m_sf != nullptr; } + +protected: + CCLib::ScalarField* m_sf; +}; + +class DimScalarFieldWrapper : public IScalarFieldWrapper +{ +public: + enum Dim { DimX = 0, DimY = 1, DimZ = 2 }; + + DimScalarFieldWrapper(ccPointCloud* cloud, Dim dim) + : m_cloud(cloud) + , m_dim(dim) + {} + + virtual inline double pointValue(unsigned index) const override { return m_cloud->getPoint(index)->u[m_dim]; } + virtual inline bool isValid() const { return m_cloud != nullptr; } + +protected: + ccPointCloud* m_cloud; + Dim m_dim; +}; + +class ColorScalarFieldWrapper : public IScalarFieldWrapper +{ +public: + enum Band { Red = 0, Green = 1, Blue = 2 }; + + ColorScalarFieldWrapper(ccPointCloud* cloud, Band band) + : m_cloud(cloud) + , m_band(band) + {} + + virtual inline double pointValue(unsigned index) const override { return m_cloud->getPointColor(index).rgb[m_band]; } + virtual inline bool isValid() const { return m_cloud != nullptr && m_cloud->hasColors(); } + +protected: + ccPointCloud* m_cloud; + Band m_band; +}; + +struct RTParams +{ + int maxDepth = 25; //To be left as a parameter of the training plugin (default 25) + int minSampleCount = 1; //To be left as a parameter of the training plugin (default 1) + int maxCategories = 0; //Normally not important as there’s no categorical variable + const bool calcVarImportance = true; //Must be true + int activeVarCount = 0; //USE 0 as the default parameter (works best) + int maxTreeCount = 100; //Left as a parameter of the training plugin (default: 100) + + float testDataRatio = 0.2f; //percentage of test data +}; + +struct Feature +{ + enum Source + { + ScalarField, DimX, DimY, DimZ, Red, Green, Blue + }; + + Feature(Source p_source, QString p_name) + : source(p_source) + , name(p_name) + {} + + Source source; + QString name; //especially for scalar fields +}; diff --git a/q3DMASC.cpp b/q3DMASC.cpp index 492b02c..db0b958 100644 --- a/q3DMASC.cpp +++ b/q3DMASC.cpp @@ -19,10 +19,14 @@ //local #include "q3DMASCDisclaimerDialog.h" +#include "Features.h" //qCC_db #include +//qCC_io +#include + //Qt #include #include @@ -30,6 +34,9 @@ #include #include +//OpenCV +#include + q3DMASCPlugin::q3DMASCPlugin(QObject* parent/*=0*/) : QObject(parent) , ccStdPluginInterface( ":/CC/plugin/q3DMASCPlugin/info.json" ) @@ -79,67 +86,6 @@ QList q3DMASCPlugin::getActions() return group; } -#include - -class IScalarFieldWrapper -{ -public: - virtual double pointValue(unsigned index) const = 0; - virtual bool isValid() const = 0; -}; - -class ScalarFieldWrapper : public IScalarFieldWrapper -{ -public: - ScalarFieldWrapper(CCLib::ScalarField* sf) - : m_sf(sf) - {} - - virtual inline double pointValue(unsigned index) const override { return m_sf->at(index); } - virtual inline bool isValid() const { return m_sf != nullptr; } - -protected: - CCLib::ScalarField* m_sf; -}; - -class DimScalarFieldWrapper : public IScalarFieldWrapper -{ -public: - enum Dim { DimX = 0, DimY = 1, DimZ = 2 }; - - DimScalarFieldWrapper(ccPointCloud* cloud, Dim dim) - : m_cloud(cloud) - , m_dim(dim) - {} - - virtual inline double pointValue(unsigned index) const override { return m_cloud->getPoint(index)->u[m_dim]; } - virtual inline bool isValid() const { return m_cloud != nullptr; } - -protected: - ccPointCloud* m_cloud; - Dim m_dim; -}; - -class ColorScalarFieldWrapper : public IScalarFieldWrapper -{ -public: - enum Band { Red = 0, Green = 1, Blue = 2 }; - - ColorScalarFieldWrapper(ccPointCloud* cloud, Band band) - : m_cloud(cloud) - , m_band(band) - {} - - virtual inline double pointValue(unsigned index) const override { return m_cloud->getPointColor(index).rgb[m_band]; } - virtual inline bool isValid() const { return m_cloud != nullptr && m_cloud->hasColors(); } - -protected: - ccPointCloud* m_cloud; - Band m_band; -}; - -#include - void q3DMASCPlugin::doClassifyAction() { if (!m_app) @@ -176,41 +122,13 @@ void q3DMASCPlugin::doClassifyAction() return; } - struct RTParams - { - int maxDepth = 25; //To be left as a parameter of the training plugin (default 25) - int minSampleCount = 1; //To be left as a parameter of the training plugin (default 1) - int maxCategories = 0; //Normally not important as there’s no categorical variable - const bool calcVarImportance = true; //Must be true - int activeVarCount = 0; //USE 0 as the default parameter (works best) - int maxTreeCount = 100; //Left as a parameter of the training plugin (default: 100) - - float testDataRatio = 0.2; //percentage of test data - }; RTParams params; - if (params.testDataRatio < 0 || params.testDataRatio > 0.99f) { m_app->dispToConsole("Invalid test data ratio", ccMainAppInterface::ERR_CONSOLE_MESSAGE); return; } - struct Feature - { - enum Source - { - ScalarField, DimX, DimY, DimZ, Red, Green, Blue - }; - - Feature(Source p_source, QString p_name) - : source(p_source) - , name(p_name) - {} - - Source source; - QString name; //especially for scalar fields - }; - std::vector features; features.push_back(Feature(Feature::DimZ, "Z")); features.push_back(Feature(Feature::ScalarField, "Intensity")); @@ -218,7 +136,7 @@ void q3DMASCPlugin::doClassifyAction() int totalSampleCount = static_cast(cloud->size()); int testSampleCount = static_cast(floor(totalSampleCount * params.testDataRatio)); - int sampleCount = totalSampleCount - sampleCount; + int sampleCount = totalSampleCount - testSampleCount; int attributesPerSample = static_cast(features.size()); m_app->dispToConsole(QString("[3DMASC] Training data: %1 samples with %2 feature(s) / %3 test samples").arg(sampleCount).arg(attributesPerSample).arg(testSampleCount), ccMainAppInterface::STD_CONSOLE_MESSAGE); @@ -236,7 +154,7 @@ void q3DMASCPlugin::doClassifyAction() return; } - unsigned randomCount = 0; + int randomCount = 0; while (randomCount < testSampleCount) { int randIndex = (std::rand() % totalSampleCount);