Files
ptrans/HSVDialog.cpp
T
Tri-Thien TRUONG e093dfc08a ajout commentaires
2020-03-20 16:52:31 +01:00

166 lines
4.1 KiB
C++

//##########################################################################
//# #
//# CLOUDCOMPARE PLUGIN: ColorimetricSegmenter #
//# #
//# 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 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: Tri-Thien TRUONG, Ronan COLLIER, Mathieu LETRONE #
//# #
//##########################################################################
#include "HSVDialog.h"
//local
#include "mainwindow.h"
//Qt
#include <QVariant>
#include <QApplication>
#include <QClipboard>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <QSettings>
//common
#include <ccPickingHub.h>
#include <ccGenericPointCloud.h>
#include <ccPointCloud.h>
//qCC_gl
#include <ccGLWidget.h>
#include <ccGLWindow.h>
/*
Constructor
*/
HSVDialog::HSVDialog(ccPickingHub* pickingHub, QWidget* parent)
: QDialog(parent)
, Ui::HSVDialog()
, m_pickingWin(0)
, m_pickingHub(pickingHub)
{
assert(pickingHub);
setModal(false);
setupUi(this);
//restore semi-persistent parameters
red->setValue(0);
green->setValue(0);
blue->setValue(0);
//Link between Ui and actions
connect(pointPickingButton_first, &QCheckBox::toggled, this, &HSVDialog::pickPoint);
//auto disable picking mode on quit
connect(this, &QDialog::finished, [&]()
{
if (pointPickingButton_first->isChecked()) pointPickingButton_first->setChecked(false);
}
);
}
/*
Method for the picking point functionnality
*/
void HSVDialog::pickPoint(bool state)
{
if (!m_pickingHub)
{
return;
}
if (state)
{
if (!m_pickingHub->addListener(this, true))
{
ccLog::Error("Can't start the picking process (another tool is using it)");
state = false;
}
}
else
{
m_pickingHub->removeListener(this);
}
pointPickingButton_first->blockSignals(true);
pointPickingButton_first->setChecked(state);
pointPickingButton_first->blockSignals(false);
}
/*
Method applied after a point is picked by picking point functionnality
*/
void HSVDialog::onItemPicked(const PickedItem& pi)
{
assert(pi.entity);
m_pickingWin = m_pickingHub->activeWindow();
if (pi.entity->isKindOf(CC_TYPES::POINT_CLOUD))
{
//Get RGB values of the picked point
ccGenericPointCloud* cloud = static_cast<ccGenericPointCloud*>(pi.entity);
const ccColor::Rgb& rgb = cloud->getPointColor(pi.itemIndex);
if (pointPickingButton_first->isChecked()) {
ccLog::Print("Point picked");
red->setValue(rgb.r);
green->setValue(rgb.g);
blue->setValue(rgb.b);
hsv hsv_first = rgb2hsv(rgb);
hue_first->setValue(hsv_first.h);
sat_first->setValue(hsv_first.s);
val_first->setValue(hsv_first.v);
pointPickingButton_first->setChecked(false);
}
}
}
/*
Method to convert from rgb values to hsv values
return : hsv struct
*/
hsv HSVDialog::rgb2hsv(ccColor::Rgb rgb) {
hsv res;
float r = rgb.r / 255.0f;
float g = rgb.g / 255.0f;
float b = rgb.b / 255.0f;
float max = std::max(std::max(r, g), b);
float min = std::min(std::min(r, g), b);
float delta = max - min;
res.v = max;
if (delta != 0) {
float hue;
if (r == max) {
hue = (g - b) / delta;
}
else {
if (g == max) {
hue = 2 + (b - r) / delta;
}
else {
hue = 4 + (r - g) / delta;
}
}
hue *= 60;
if (hue < 0) hue += 360;
res.h = hue;
}
else {
res.h = 0;
}
res.s = max == 0 ? 0 : ((max - min) / max) * 100;
res.v = max * 100;
return res;
}