渲染SDK 0.1.2
载入中...
搜索中...
未找到
创建实体插件

概述

本教程提供了实体插件创建的教程

描述

通过预先生成的Entity实体数据构建插件,目的是拓展实体的属性设置如颜色表设置,顶层显示等

示例代码

效果图

示例图片

main.cpp

#include <QApplication>
#include "Mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Mainwindow w;
w.show();
return a.exec();
}

Mainwindow.cpp

#include "Mainwindow.h"
Mainwindow::Mainwindow(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//初始化
mRenderComponent = AMCAXRender::CreateRenderComponent(this);
mRender = mRenderComponent->CreateBasicRender();
//窗口布局
ui.gridLayout->addWidget(mRender->widget);
cube.category = "SHAPE";
cube.id = "cube";
// 顶点坐标,每个面需要四个点,共24个点
cube.points = {
// 面1
{0.0, 0.0, 0.0}, {10.0, 0.0, 0.0}, {10.0, 10.0, 0.0}, {0.0, 10.0, 0.0},
// 面2
{0.0, 0.0, 10.0}, {10.0, 0.0, 10.0}, {10.0, 10.0, 10.0}, {0.0, 10.0, 10.0},
// 面3
{0.0, 0.0, 0.0}, {10.0, 0.0, 0.0}, {10.0, 0.0, 10.0}, {0.0, 0.0, 10.0},
// 面4
{10.0, 10.0, 0.0}, {0.0, 10.0, 0.0}, {0.0, 10.0, 10.0}, {10.0, 10.0, 10.0},
// 面5
{0.0, 0.0, 0.0}, {0.0, 10.0, 0.0}, {0.0, 10.0, 10.0}, {0.0, 0.0, 10.0},
// 面6
{10.0, 0.0, 0.0}, {10.0, 10.0, 0.0}, {10.0, 10.0, 10.0}, {10.0, 0.0, 10.0}
};
// 法向量,每个面需要四个法向量,共24个法向量
cube.normals = {
// 面1
{0.0, 0.0, -1.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0}, {0.0, 0.0, -1.0},
// 面2
{0.0, 0.0, 1.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, 1.0},
// 面3
{0.0, -1.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.0},
// 面4
{0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0},
// 面5
{-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.0},
// 面6
{1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.0}
};
// 顶点数据
//cube.vertex.topoType = "point";
//cube.vertex.vertices = {
// 0.0, 0.0, 0.0, 10.0, 0.0, 0.0, 10.0, 10.0, 0.0, 0.0, 10.0, 0.0,
// 0.0, 0.0, 10.0, 10.0, 0.0, 10.0, 10.0, 10.0, 10.0, 0.0, 10.0, 10.0
//};
// 边数据
//cube.edges = {
// {"index", {0, 1}}, {"index", {1, 2}}, {"index", {2, 3}}, {"index", {3, 0}},
// {"index", {4, 5}}, {"index", {5, 6}}, {"index", {6, 7}}, {"index", {7, 4}},
// {"index", {0, 4}}, {"index", {1, 5}}, {"index", {2, 6}}, {"index", {3, 7}}
//};
// 面数据
cube.faces = {
{{{0, 2, 1}, {0, 3, 2}}, {0, 1, 2, 3}, 4, {{0.0, 0.0, -1.0}}, {}},
{{{4, 5, 6}, {4, 6, 7}}, {4, 5, 6, 7}, 4, {{0.0, 0.0, 1.0}}, {}},
{{{8, 9, 10}, {8, 10, 11}}, {8, 9, 10, 11}, 4, {{0.0, -1.0, 0.0}}, {}},
{{{12, 13, 14}, {12, 14, 15}}, {12, 13, 14, 15}, 4, {{0.0, 1.0, 0.0}}, {}},
{{{16, 18, 17}, {16, 19, 18}}, {16, 17, 18, 19}, 4, {{-1.0, 0.0, 0.0}}, {}},
{{{20, 21, 22}, {20, 22, 23}}, {20, 21, 22, 23}, 4, {{1.0, 0.0, 0.0}}, {}}
};
//构建实体数据
auto entity = mRender->entityFactory->FromCAXMeshInfo(cube);
//颜色表
std::vector<double> scalars;
std::map<double, std::vector<double>> colorMap;
colorMap.emplace(0, std::vector<double>{ 0, 0, 0 });
colorMap.emplace(1, std::vector<double>{ 1, 0, 0 });
colorMap.emplace(2, std::vector<double>{ 1, 1, 0 });
colorMap.emplace(3, std::vector<double>{ 1, 1, 1 });
colorMap.emplace(4, std::vector<double>{ 0, 1, 1 });
colorMap.emplace(5, std::vector<double>{ 0, 0, 1 });
for (auto i = 0; i < 6; i++) {
scalars.push_back(i);
}
//矩阵设置
double matrix[4][4] = {
1,0,0,-25,
0,1,0,0,
0,0,1,0,
0,0,0,1,
};
//创建Entity插件
auto pId = mRender->pluginManage->AddPluginFromMeshEntity(entity);
auto att = mRender->pluginManage->GetProperty<AMCAXRender::EntityPlugProp>(pId);
att->SetColorMap(colorMap, scalars, AMCAXRender::ColorMapType::rampToStep);
att->SetOpacity(1);
att->SetLineWidth(2);
att->SetMatrix(*matrix);
//设置插件属性
mRender->pluginManage->SetProperty(pId, att);
//设置高亮颜色
double c[3] = { 1,0,1 };
mRender->pluginManage->SetHightLightColor(c);
//注册鼠标点击拾取插件
mRender->pluginManage->RegisterPickEvent(AMCAXRender::EventType::mouse_click,
[&](AMCAXRender::EntityId entityId, AMCAXRender::PickType type, int subIndex, double* worldPos) {
//设置高亮
mRender->pluginManage->ClearHightLight(entityId);
mRender->pluginManage->AddHightLight(entityId, type, subIndex);
}
);
//颜色图例设置
double pos[] = { 0.9,0.1,0.1,0.8 };
std::vector<std::vector<double>> colorList;
colorList.push_back(std::vector<double>{ 0, 0, 0 });
colorList.push_back(std::vector<double>{ 1, 0, 0 });
colorList.push_back(std::vector<double>{ 1, 1, 0 });
colorList.push_back(std::vector<double>{ 1, 1, 1 });
colorList.push_back(std::vector<double>{ 0, 1, 1 });
colorList.push_back(std::vector<double>{ 0, 0, 1 });
std::vector<std::string> labels;
labels.push_back("");
labels.push_back("0");
labels.push_back("1");
labels.push_back("2");
labels.push_back("3");
labels.push_back("4");
labels.push_back("5");
mRender->styleManage->SetColorBarPosition(pos);
mRender->styleManage->SetColorBarColorMap(colorList, labels);
mRender->styleManage->SetColorBarVisible(1);
//更新数据
mRender->cameraManage->ResetCamera();
mRender->entityManage->DoRepaint();
}
Mainwindow::~Mainwindow()
{
}
AMCAX_RENDER_API std::shared_ptr< IRenderComponent > CreateRenderComponent(QWidget *parent)
Create Render Component
PickType
Pick Type
定义 Constants.h:99
@ mouse_click
Left Click
定义 Constants.h:84
Entity Plugin
定义 PluginProperty.h:80
virtual void SetColorMap(std::map< double, std::vector< double > > colorMap, std::vector< double > scalars, ColorMapType type)=0
Set color map
Custom Mesh Data Structure
定义 CAXMeshInfo.h:39
std::vector< Face > faces
Face data collection
定义 CAXMeshInfo.h:46
std::vector< std::vector< double > > points
Point collection
定义 CAXMeshInfo.h:42
std::string id
Unique data ID
定义 CAXMeshInfo.h:41
std::vector< std::vector< double > > normals
Normal vector collection
定义 CAXMeshInfo.h:43
std::string category
Data type, e.g., "SHAPE", "POINT"
定义 CAXMeshInfo.h:40

Mainwindow.h

#pragma once
#include <QWidget>
#include "ui_Mainwindow.h"
#include <AMCAXRender.h>
class Mainwindow : public QWidget
{
Q_OBJECT
public:
Mainwindow(QWidget *parent = nullptr);
~Mainwindow();
private:
Ui::MainwindowClass ui;
std::shared_ptr<AMCAXRender::IRenderComponent> mRenderComponent;
std::shared_ptr<AMCAXRender::CBasicRender> mRender;
};
Component Creation

Mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainwindowClass</class>
<widget class="QWidget" name="MainwindowClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>Mainwindow</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(Coordinate)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
find_package(AMCAXRender )
find_package(Qt6 COMPONENTS Widgets Core Gui REQUIRED)
file(GLOB ALL_UI_FILES *.ui)
file(GLOB ALL_FILES *.cpp *.h)
add_executable(${PROJECT_NAME} ${ALL_UI_FILES} ${ALL_FILES})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets)
target_link_libraries(${PROJECT_NAME} PRIVATE AMCAXRender::AMCAXRender)

更多

更多属性请参考 AMCAXRender::EntityPlugProp