Files
ptrans/HSVDialog.cpp
T

184 lines
4.8 KiB
C++
Raw Normal View History

2020-02-29 18:00:36 +01:00
//##########################################################################
//# #
//# 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>
2020-03-20 16:52:31 +01:00
/*
Constructor
*/
2020-02-29 18:00:36 +01:00
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
2020-03-20 16:52:31 +01:00
red->setValue(0);
green->setValue(0);
blue->setValue(0);
2020-02-29 18:00:36 +01:00
//Link between Ui and actions
2020-03-20 16:52:31 +01:00
connect(pointPickingButton_first, &QCheckBox::toggled, this, &HSVDialog::pickPoint);
2020-04-16 10:20:09 +02:00
connect(red, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, &HSVDialog::updateValues);
connect(green, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, &HSVDialog::updateValues);
connect(blue, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, &HSVDialog::updateValues);
2020-02-29 18:00:36 +01:00
//auto disable picking mode on quit
connect(this, &QDialog::finished, [&]()
{
if (pointPickingButton_first->isChecked()) pointPickingButton_first->setChecked(false);
}
);
}
2020-03-20 16:52:31 +01:00
/*
Method for the picking point functionnality
*/
void HSVDialog::pickPoint(bool state)
2020-02-29 18:00:36 +01:00
{
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);
}
2020-03-20 16:52:31 +01:00
/*
Method applied after a point is picked by picking point functionnality
*/
2020-02-29 18:00:36 +01:00
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()) {
2020-03-20 16:52:31 +01:00
ccLog::Print("Point picked");
2020-04-16 10:20:09 +02:00
//blocking signals to avoid updating 2 times hsv values for nothing
red->blockSignals(true);
green->blockSignals(true);
2020-03-20 16:52:31 +01:00
red->setValue(rgb.r);
green->setValue(rgb.g);
blue->setValue(rgb.b);
2020-02-29 18:00:36 +01:00
2020-04-16 10:20:09 +02:00
red->blockSignals(false);
green->blockSignals(false);
2020-02-29 18:00:36 +01:00
pointPickingButton_first->setChecked(false);
}
}
}
2020-04-16 10:20:09 +02:00
/*
Method applied after entering a value in RGB text fields
*/
void HSVDialog::updateValues()
{
ccColor::Rgb rgb(red->value(), green->value(), blue->value());
hsv hsv_values = rgb2hsv(rgb);
hue_first->setValue(hsv_values.h);
sat_first->setValue(hsv_values.s);
val_first->setValue(hsv_values.v);
}
2020-03-20 16:52:31 +01:00
/*
Method to convert from rgb values to hsv values
return : hsv struct
*/
2020-02-29 18:00:36 +01:00
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;
}