Cloudcompare 标准插件初探(MyPlugin)

Cloudcompare 插件初探--MyPlugin

  • 一、先看效果
  • 二、将自己的插件项目加入Cloudcompare
    • 2.1 修改CMakeLists
    • 2.2 复制例程,修改名字
    • 2.3 重新生成项目
  • 三、参考例程修改代码
    • 3.1 MyPlugin.h
    • 3.2 MyPlugin.cpp
    • 3.3 配置文件 Json
    • 3.4 资源文件 qrc
  • 四、编译生成
  • 五、参考链接

一、先看效果

Cloudcompare 标准插件初探(MyPlugin)_第1张图片

二、将自己的插件项目加入Cloudcompare

先不修改代码,加入Cloudcompare后使用vs编辑

2.1 修改CMakeLists

\plugins\example\CMakeLists.txt 添加自己的插件源码目录

add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/ExampleGLPlugin )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/ExampleIOPlugin )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/ExamplePlugin )
add_subdirectory( ${CMAKE_CURRENT_SOURCE_DIR}/MyPlugin )

\plugins\example\MyPlugin\CMakeLists.txt 修改项目名称

cmake_minimum_required( VERSION 3.0 )
option( PLUGIN_EXAMPLE_STANDARD "Check to install example plugin" OFF )
if ( PLUGIN_EXAMPLE_STANDARD )
    project( MyPlugin )
    include( ../../CMakePluginTpl.cmake )
endif()

2.2 复制例程,修改名字

直接复制例程ExamplePlugin,修改文件名字,如下图所示
Cloudcompare 标准插件初探(MyPlugin)_第2张图片

2.3 重新生成项目

使用CMake重新生成项目,注意勾选 PLUGIN_EXAMPLE_STANDARD
打开以后就能看到MyPlugin
在这里插入图片描述在这里插入图片描述

三、参考例程修改代码

直接看例程的注释就知道怎么修改,我这里为了减少篇幅就删掉注释了,直接放核心源码

3.1 MyPlugin.h

#ifndef MyPlugin_PLUGIN_HEADER
#define MyPlugin_PLUGIN_HEADER

#include "ccStdPluginInterface.h"


class MyPlugin : public QObject, public ccStdPluginInterface
{
	Q_OBJECT
	Q_INTERFACES(ccStdPluginInterface)
	Q_PLUGIN_METADATA(IID "cccorp.cloudcompare.plugin.MyPlugin" FILE "info.json")    // 改成自己的
	
public:
	explicit MyPlugin( QObject *parent = nullptr );
	~MyPlugin() override = default;
	void onNewSelection( const ccHObject::Container &selectedEntities ) override;
	QList<QAction *> getActions() override;
	
private:
	void doAction();
	QAction* m_action;
};

#endif

3.2 MyPlugin.cpp

#include 
#include "MyPlugin.h"
#include 

MyPlugin::MyPlugin( QObject *parent )
	: QObject( parent )
	, ccStdPluginInterface( ":/CC/plugin/MyPlugin/info.json" )
	, m_action( nullptr )
{
}

void MyPlugin::onNewSelection( const ccHObject::Container &selectedEntities )
{
	if ( m_action == nullptr )
	{
		return;
	}
	m_action->setEnabled( !selectedEntities.empty() );
}

QList<QAction *> MyPlugin::getActions()
{
	if ( !m_action )
	{
		m_action = new QAction( getName(), this );
		m_action->setToolTip( getDescription() );
		m_action->setIcon( getIcon() );
		connect( m_action, &QAction::triggered, this, &MyPlugin::doAction);
	}

	return { m_action };
}

void MyPlugin::doAction()
{	
	if ( m_app == nullptr )
	{
		Q_ASSERT( false );
		return;
	}
	// 1.获取所选点云
	ccPointCloud* cloud;
	const ccHObject::Container& selectedEntities = m_app->getSelectedEntities();
	if (selectedEntities.size() != 1)
	{
		m_app->dispToConsole("[MyPlugin] selectedEntities.size() != 1", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
		return;
	}
	ccHObject *entity = selectedEntities[0];
	if (entity->isA(CC_TYPES::POINT_CLOUD))
	{
		cloud = static_cast<ccPointCloud*>(entity);
	}
	else {
		m_app->dispToConsole("[MyPlugin] not a CC_TYPES::POINT_CLOUD", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
		return;
	}

	// 2.遍历点云坐标
	CCLib::ReferenceCloud sampledCloud(cloud);
	QElapsedTimer eTimer;                       // 统计时间
	eTimer.start();
	CCVector3 bbmin, bbmax;
	cloud->getBoundingBox(bbmin, bbmax);        // 获取边界框
	float th = (bbmin[2] + bbmax[2]) / 2;       // 设定阈值
	for (unsigned int i = 0; i < cloud->size(); ++i) {
		if (cloud->getPoint(i)->z > th) {
			sampledCloud.addPointIndex(i);      // 取大于阈值的坐标
		}
	}
	ccLog::Print("[MyPlugin] Timing: %3.3f s.", eTimer.elapsed() / 1000.0);

	// 3.生成新的点云
	int warnings = 0;
	ccPointCloud* newPointCloud = cloud->partialClone(&sampledCloud, &warnings);
	if (warnings){
		ccLog::Warning("[MyPlugin] Not enough memory: colors, normals or scalar fields may be missing!");
	}
	if (newPointCloud)   
	{
		newPointCloud->setName(cloud->getName() + QString(".MyPlugin"));
		newPointCloud->setGlobalShift(cloud->getGlobalShift());
		newPointCloud->setGlobalScale(cloud->getGlobalScale());
		newPointCloud->setDisplay(cloud->getDisplay());
		newPointCloud->prepareDisplayForRefresh();
		if (cloud->getParent())
			cloud->getParent()->addChild(newPointCloud);
		cloud->setEnabled(false);
		m_app->addToDB(newPointCloud);
		newPointCloud->prepareDisplayForRefresh();
	}

	// 4.结尾 Hello world!
	m_app->dispToConsole( "[MyPlugin] Hello world!", ccMainAppInterface::STD_CONSOLE_MESSAGE );
}

3.3 配置文件 Json

{
  "type": "Standard",
  "name": "MyPlugin",
  "icon": ":/CC/plugin/MyPlugin/images/rabbit.ico",
  "description": "This is my plugin.",
  "authors": [
    {
      "name": "dasbai",
      "email": "https://blog.csdn.net/qq_38204686"
    }
  ],
  "maintainers": [
    {
      "name": "dasbai",
      "email": "https://blog.csdn.net/qq_38204686"
    }
  ]
}

3.4 资源文件 qrc

<RCC>
    <qresource prefix="/CC/plugin/MyPlugin">
        <file>info.json</file>
        <file>images/rabbit.ico</file>
    </qresource>
</RCC>

Cloudcompare 标准插件初探(MyPlugin)_第3张图片

四、编译生成

鼠标移到MyPlugin然后右键,点击生成
Cloudcompare 标准插件初探(MyPlugin)_第4张图片
如果设定好安装路径可以右键INSTALL,点生成
或者手动将dll复制到exe的\plugins文件夹中

五、参考链接

  • https://blog.huihut.com/2017/04/27/CloudCompareSAFPlugin_3_Algorithm/ (CloudCompare插件编写三) – 推荐阅读
  • https://blog.csdn.net/dayuhaitang1/article/details/116277780 (CloudCompare之插件式开发)
  • https://www.bilibili.com/video/BV1mC4y1a7FG (CloudCompare插件开发教程) – 推荐观看
  • https://blog.csdn.net/Fcc_bd_stars/article/details/121289154 (CloudCompare+vs+cmake插件功能编译)

你可能感兴趣的:(#,Qt,Cloudcompare,插件,标准插件,Qt)