CloudCompare简单二次开发 下(功能实现)

上一篇博客主要讲了CC二次开发过程中功能界面UI的设计与添加方法,下面具体来讲一下功能类ColorChangeTool的实现~

CloudCompare简单二次开发(上):https://blog.csdn.net/NEXUS666/article/details/111330786

一、moc_ColorChangeTool.cpp文件生成

(1)新建一个空类ColorChangeTool,为了使得功能界面能够停靠在GLWindow上,需要令ColorChangeTool继承自ccOverlayDialog类。而ccOverlayDialog类又继承自QDialog,QDialog继承自QWidget,QWidget继承自Qt祖宗类QObject,因此ColorChangeTool是一个支持Q_OBJECT宏的QObject对象!为了使得ColorChangeTool支持“信号-槽”机制,需要加入“Q_OBJECT宏”!

(2)右键,进入ColorChangeTool.h的属性页,将“配置属性>>>常规>>>项类型”从“C\C++标头”更改为自定义生成工具,点击应用。

(3)自定义生成工具配置
命令行:(注意命令行里是有空格的!)
moc.exe ColorChangeTool.h文件的全路径 -o moc_ColorChangeTool.cpp文件生成路径的全路径

说明:
Performing Custom Build Tools

输出:
moc_ColorChangeTool.cpp

附加依赖项:
moc.exe
ColorChangeTool.h

配置图如下:
CloudCompare简单二次开发 下(功能实现)_第1张图片

(4)利用ColorChangeTool.h文件编译生成moc_ColorChangeTool.cpp文件,并务必将moc_ColorChangeTool.cpp文件添加现有项至工程中!如果这里moc文件编译失败,请检查文件名、内容格式、路径等有无错误!
CloudCompare简单二次开发 下(功能实现)_第2张图片

二、废话少说,下面直接挒代码

主窗体菜单栏二次开发功能按钮槽函数doActionColorChange():

void MainWindow::doActionColorChange()
{
	//判断DB-Tree内是否选中对象
	if (!haveOneSelection())
	{
		ccConsole::Error(QStringLiteral("请在 DB Tree 内选择点云实体对象!"));
		return;
	}

	//获取当前DB-Tree中选择的实体对象
	ccHObject* ent = m_selectedEntities[0];
	
	//判断是否为点云对象(这里也可能是矢量栅格对象)
	if (!ent->isKindOf(CC_TYPES::POINT_CLOUD))
	{
		ccConsole::Error(QStringLiteral("选择的对象不是点云类型!"));
		return;
	}
	//将实体对象ent转换成ccGenericPointCloud对象
	ccGenericPointCloud* gCloud = ccHObjectCaster::ToGenericPointCloud(ent);

	//ccGenericPointCloud是抽象类,不能被实例化,所以向下转换成子类对象ccPointCloud,我们可以通过ccPointCloud来操作点云
	ccPointCloud* pCloud = static_cast<ccPointCloud*>(gCloud);

	//Qt MDI框架
	QMdiSubWindow* qWin = m_mdiArea->activeSubWindow();
	if (!qWin)
		return;

	if (!m_colorDlg)
	{
		//自定义对象m_colorDlg传入点云pCloud
		m_colorDlg = new ColorChangeTool(qWin, pCloud);
		
		//连接信号槽:使得后台可以实时获取用户在DB-Tree内所选中的点云
		connect(m_ccRoot, &ccDBRoot::selectionChanged, m_colorDlg, &ColorChangeTool::GetSelectedCloud);
		
		//当DB-Tree为空时,清空点云
		connect(m_ccRoot, &ccDBRoot::dbIsEmpty, m_colorDlg, &ColorChangeTool::onClear);
		
		//使得m_colorDlg能够停靠在点云显示窗口的右上角
		registerOverlayDialog(m_colorDlg, Qt::TopRightCorner);
	}

	//显示m_colorDlg
	m_colorDlg->start();
	updateOverlayDialogsPlacement();
}

ColorChangeTool.h:

#include "ccOverlayDialog.h"

class QMdiSubWindow;
class ccGenericPointCloud;
class ccPointCloud;
class MainWindow;

namespace Ui
{
	class myColorChangeDlg;
}


class ColorChangeTool :public ccOverlayDialog
{
	Q_OBJECT

public:
	explicit ColorChangeTool(QWidget* parent, ccPointCloud* pc);

	virtual ~ColorChangeTool() override;

	bool start() override;

public slots:
	void onClone();   //点云克隆
	
	void redSliderValueChanged(int r);
	void greenSliderValueChanged(int g);  
	void blueSliderValueChanged(int b);

	void redSpinboxValueChanged(int r);
	void greenSpinboxValueChanged(int g);
	void blueSpinboxValueChanged(int b);

	void GetSelectedCloud();
	void onClear();
private:
	void initUi();    //初始化界面控件属性
	void updateColor(const int& r, const int& g, const int& b);  //更新点云颜色

	Ui::myColorChangeDlg* m_ui = nullptr;    //功能界面
	ccPointCloud* m_cloud = nullptr;         //DB-Tree中被选中的点云
	MainWindow* m_app = nullptr;             //主窗体单例
};

ColorChangeTool.cpp:

#include "ColorChangeTool.h"
#include "ccGLWindow.h"
#include 
#include 
#include "ccPointCloud.h"
#include "mainwindow.h"
#include "ccDBRoot.h"
#include "ccConsole.h"
#include "ui_myColorChangeDlg.h"

ColorChangeTool::ColorChangeTool(QWidget* parent, ccPointCloud* pc)
	:ccOverlayDialog( parent )
	,m_ui( new Ui::myColorChangeDlg )
	,m_cloud( pc )
{
	m_ui->setupUi(this);

	//获取主窗体单例
	m_app = MainWindow::TheInstance();
	
	initUi();

	//信号槽连接
	connect(m_ui->cloneBtn, &QAbstractButton::clicked, this, &ColorChangeTool::onClone);
	connect(m_ui->redSlider, &QAbstractSlider::valueChanged, this, &ColorChangeTool::redSliderValueChanged);
	connect(m_ui->greenSlider, &QAbstractSlider::valueChanged, this, &ColorChangeTool::greenSliderValueChanged);
	connect(m_ui->blueSlider, &QAbstractSlider::valueChanged, this, &ColorChangeTool::blueSliderValueChanged);
	connect(m_ui->redSpinBox, SIGNAL(valueChanged(int)), this, SLOT(redSpinboxValueChanged(int)));
	connect(m_ui->greenSpinBox, SIGNAL(valueChanged(int)), this, SLOT(greenSpinboxValueChanged(int)));
	connect(m_ui->blueSpinBox, SIGNAL(valueChanged(int)), this, SLOT(blueSpinboxValueChanged(int)));
}


ColorChangeTool::~ColorChangeTool()
{
	if (m_ui){
		delete m_ui;
		m_ui = nullptr;
	}

	if (m_cloud){
		delete m_cloud;
		m_cloud = nullptr;
	}
}


void ColorChangeTool::initUi()
{
	m_ui->redSlider->setMinimum(0);
	m_ui->redSlider->setMaximum(255);
	m_ui->redSlider->setValue(125);

	m_ui->greenSlider->setMinimum(0);
	m_ui->greenSlider->setMaximum(255);
	m_ui->greenSlider->setValue(125);

	m_ui->blueSlider->setMinimum(0);
	m_ui->blueSlider->setMaximum(255);
	m_ui->blueSlider->setValue(125);

	m_ui->redSpinBox->setValue(m_ui->redSlider->value());
	m_ui->greenSpinBox->setValue(m_ui->greenSlider->value());
	m_ui->blueSpinBox->setValue(m_ui->blueSlider->value());
}


bool ColorChangeTool::start()
{
	ccOverlayDialog::start();
	m_processing = false;
	return true;
}


void ColorChangeTool::updateColor(const int& r, const int& g, const int& b)
{
	//设置点云颜色RGB
	m_cloud->setColor((unsigned char)r, (unsigned char)g, (unsigned char)b);
	
	//点云对象添加到DB-Tree中
	m_app->addToDB(m_cloud);
	m_app->RefreshAllGLWindow();
}


void ColorChangeTool::redSliderValueChanged(int r)
{
	m_ui->redSpinBox->blockSignals(true);
	m_ui->redSpinBox->setValue(r);
	m_ui->redSpinBox->blockSignals(false);
	updateColor(m_ui->redSpinBox->value(), m_ui->greenSpinBox->value(), m_ui->blueSpinBox->value());
}


void ColorChangeTool::greenSliderValueChanged(int g)
{
	m_ui->greenSpinBox->blockSignals(true);
	m_ui->greenSpinBox->setValue(g);
	m_ui->greenSpinBox->blockSignals(false);
	updateColor(m_ui->redSpinBox->value(), m_ui->greenSpinBox->value(), m_ui->blueSpinBox->value());
}


void ColorChangeTool::blueSliderValueChanged(int b)
{
	m_ui->blueSpinBox->blockSignals(true);
	m_ui->blueSpinBox->setValue(b);
	m_ui->blueSpinBox->blockSignals(false);
	updateColor(m_ui->redSpinBox->value(), m_ui->greenSpinBox->value(), m_ui->blueSpinBox->value());
}


void ColorChangeTool::redSpinboxValueChanged(int r)
{
	m_ui->redSlider->blockSignals(true);
	m_ui->redSlider->setValue(r);
	m_ui->redSlider->blockSignals(false);
	updateColor(m_ui->redSpinBox->value(), m_ui->greenSpinBox->value(), m_ui->blueSpinBox->value());
}

void ColorChangeTool::greenSpinboxValueChanged(int g)
{
	m_ui->greenSlider->blockSignals(true);
	m_ui->greenSlider->setValue(g);
	m_ui->greenSlider->blockSignals(false);
	updateColor(m_ui->redSpinBox->value(), m_ui->greenSpinBox->value(), m_ui->blueSpinBox->value());
}

void ColorChangeTool::blueSpinboxValueChanged(int b)
{
	m_ui->blueSlider->blockSignals(true);
	m_ui->blueSlider->setValue(b);
	m_ui->blueSlider->blockSignals(false);
	updateColor(m_ui->redSpinBox->value(), m_ui->greenSpinBox->value(), m_ui->blueSpinBox->value());
}


void ColorChangeTool::GetSelectedCloud()
{
	if (m_cloud)
	{
		//设置取消选中上一块点云,同时包围盒消失
		m_cloud->setSelected(false);
	}
	
	//DB-Tree中所有被选中的实体存入container
	ccHObject::Container container;
	m_app->db()->getSelectedEntities(container);
	
	if (container.size())
	{
		//拿到第一个被选中的实体
		ccHObject* ent = container[0];
		if (!ent->isKindOf(CC_TYPES::POINT_CLOUD))
		{
			ccConsole::Error(QStringLiteral("选择的对象不是点云类型!"));
			return;
		}
		ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent);
		m_cloud = static_cast<ccPointCloud*>(cloud);
		
		//设置选中当前点云,同时显示包围盒
		m_cloud->setSelected(true);
	}
	//刷新
	m_app->RefreshAllGLWindow();
}


void ColorChangeTool::onClear()
{
	m_cloud->clear();
	m_cloud = nullptr;
}


void ColorChangeTool::onClone()
{
	if (!m_cloud)
	{
		return;
	}

	//法一(点到点的克隆)
	ccPointCloud* pc = new ccPointCloud(m_cloud->getName() + QString("-Clone"));
	//为克隆对象分配内存(坐标与颜色)
	pc->reserve(m_cloud->size());
	pc->reserveTheRGBTable();
	size_t pointSize = m_cloud->size();
	for (size_t i = 0; i < pointSize; ++i)
	{
		pc->addPoint(*m_cloud->getPoint(i));
		pc->addColor(m_cloud->getPointColor(i));
	}
	pc->showColors(true);

	//法二(调用ccPointCloud的接口克隆)
	//ccPointCloud* pc = new ccPointCloud(m_cloud->getName() + QString("-Clone"));
	//m_cloud->clone(pc);

	//将克隆点云pc添加到与m_cloud相同的目录下,并选中。
	m_cloud->getParent()->addChild(pc);
	m_app->db()->selectEntity(pc);
	m_app->addToDB(pc);
	m_app->UpdateUI();
}

最终效果如下图所示:
CloudCompare简单二次开发 下(功能实现)_第3张图片

你可能感兴趣的:(C++/Qt,c++,qt)