解決思路:多看CloudCompare的源碼,源碼裏面有真經!
一、cmake構建cloudcompare工程
▪ 打開cmake GUI設置源代碼和輸出文件路徑
▪ 設置Qt安裝路徑
▪ 選擇所需插件
▪ 再次點集Configure無誤後,再點擊Generate即可,但是由於部分插件需要一些依賴庫需自行下載,如
在配置時同樣需設置好相應的文件路徑。需要注意的是,liblas沒有自己找到相應路徑(估計是cmake文件中沒有寫),可能會報錯。因此,需要你自己添加相應路徑。
▪ 打開工程即可以進行編譯
二、添加自己的插件
我們需要在右邊工具欄加入自己的插件
▪ 在源代碼路徑F:\SZDX_airborneLiDAR\Software_design\CloudCompare-2.9.0\CloudCompare-2.9.0\plugins\下新建一個文件夾TestPlugin
▪ 在TestPlugin文件夾下新建五個空文件
▪ TestPlugin.h裏內容如下:
#ifndef TestPlugin_PLUGIN_HEADER
#define TestPlugin_PLUGIN_HEADER
#include "../ccStdPluginInterface.h"
class TestPlugin : public QObject, public ccStdPluginInterface
{
Q_OBJECT
Q_INTERFACES(ccStdPluginInterface)
Q_PLUGIN_METADATA(IID "cccorp.cloudcompare.plugin.TestPlugin" FILE "info.json")
public:
//! Default constructor
explicit TestPlugin(QObject* parent = nullptr);
virtual ~TestPlugin() = default;
//inherited from ccPluginInterface
virtual QString getName() const{ return "Test Plugin"; }
virtual QString getDescription() const{ return "Test Plugin for test"; }
virtual QIcon getIcon() const;
//inherited from ccStdPluginInterface
virtual void onNewSelection(const ccHObject::Container& selectedEntities) override;
virtual void getActions(QActionGroup& group) override;
protected slots:
//! Slot called when associated ation is triggered
void doAction();
protected:
//! Associated action
QAction* m_action;
};
#endif //TestPlugin_PLUGIN_HEADER
TestPlugin.cpp裏內容如下:
#include "TestPlugin.h"
//qCC_db
#include <ccPointCloud.h>
//Qt
#include <QMainWindow>
#include <QProgressDialog>
//system
#include <assert.h>
TestPlugin::TestPlugin(QObject* parent)
: QObject(parent)
/*, ccStdPluginInterface( ":/CC/plugin/TestPlugin/info.json" )*/
, m_action( nullptr )
{
}
void TestPlugin::onNewSelection(const ccHObject::Container& selectedEntities)
{
//此處設置是否需要有點雲時激活插件
if (m_action)
{
for (ccHObject* entity : selectedEntities)
{
//if we have found at least one cloud
if (entity && entity->isA(CC_TYPES::POINT_CLOUD))
{
m_action->setEnabled(true);
return;
}
}
//no cloud?
m_action->setEnabled(true);// 爲True則不需要點雲也可以激活插件,爲false則需要點雲纔可以激活插件
}
}
void TestPlugin::getActions(QActionGroup& group)
{
//default action
if (!m_action)
{
m_action = new QAction(getName(),this);
m_action->setToolTip(getDescription());
m_action->setIcon(getIcon());
//connect signal
connect(m_action, &QAction::triggered, this, &TestPlugin::doAction);
}
group.addAction(m_action);
}
//persistent settings during a single session
// CoamingMeasurmentDialog::Parameters s_params;
void TestPlugin::doAction()
{
assert(m_app);
if (!m_app)
return;
if (!m_app->haveOneSelection())
{
m_app->dispToConsole("Select only one cloud!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
return;
}
m_app->dispToConsole("Hello!", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
//ccLog::LogMessage("hello", 1);//這裏設置TestPlugin點擊一下,日誌欄輸出hello字樣
//currently selected entities parameters may have changed!
m_app->updateUI();
//currently selected entities appearance may have changed!
m_app->refreshAll();
}
QIcon TestPlugin::getIcon() const
{
return QIcon(":/CC/plugin/TestPlugin/TestPlugin.png");
}
TestPlugin.qrc裏內容如下,這是Qt資源文件添加了兩個資源(TestPlugin.png)和(info.json),因此需把這兩個資源補齊
<RCC>
<qresource prefix="/CC/plugin/TestPlugin" >
<file>TestPlugin.png</file>
<file>info.json</file>
</qresource>
</RCC>
同時,將TestPlugin.png文件拷貝到TestPlugin目錄下,這個圖片就是插件的圖標
info.json內容如下,這是插件的信息描述,在對應模板處填寫相應信息即可
{
"type" : "Standard",
"core" : true,
"name" : "TestPlugin",
"icon" : ":/CC/plugin/TestPlugin/images/TestPlugin.png",
"description": "",
"authors" : [
{
"name" : "",
"email" : ""
}
],
"maintainers" : [
{
"name" : "",
"email" : ""
}
],
"references" : [
{
"text": "\" \"<br/>
Talk by Alexandre Boulch & Renaud Marlet at the Symposium of Geometry Processing, 2016",
"url" : ""
},
{
"text": "TestPlugin library",
"url": ""
},
{
"text": "Documentation",
"url": ""
}
]
}
CMakeLists.txt內容如下:
cmake_minimum_required(VERSION 3.0)
option( INSTALL_TestPlugin_PLUGIN "Check to install TestPlugin plugin" ON )
if (INSTALL_TestPlugin_PLUGIN )
project( TestPlugin )
include( ../CMakePluginTpl.cmake )
target_link_libraries( ${PROJECT_NAME} )
target_link_libraries( ${PROJECT_NAME} ${OPENGL_LIBRARIES} )
endif()
上一級目錄plugins中的CMakeLists.txt文件就不用修改啦,CC已經在後臺幫我們在解決方案添加該插件的源碼寫好了(不得不說“真高級”。怎麼做到的,還需要學習)。
▪ 最後重複步驟一,重新構建工程文件,打開解決方案,即出現TestPlugin工程
▪ 安裝成功後,插件已在工具欄中,點集圖標出現正確的輸出,到處就完事啦!
總結:
這些都是我看CloudCompare源碼,外加網上資料摸索出來的。重點閱讀了CSF的源碼,從中看懂了不少CC添加源碼的機制。
所以啊,源碼纔是重中之重!不由得想到侯捷《STL源碼剖析》中的一句話“源碼面前,了無祕密”!真的很實用。