渲染SDK 0.1.2
载入中...
搜索中...
未找到
通过Struct创建几何实体

概述

本教程提供了Render SDK基于Struct数据创建实体的使用教程。

前置条件

如果未初始化SDK,需要通过以下代码初始化

//创建sdk组件
auto mRenderComponent = AMCAXRender::CreateRenderComponent(this);
//创建实体Render
auto mRender = mRenderComponent->CreateBasicRender();
AMCAX_RENDER_API std::shared_ptr< IRenderComponent > CreateRenderComponent(QWidget *parent)
Create Render Component

通过Struct数据创建实体示例

本示例通过加载struct创建实体。

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(createEntityFromStruct)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
add_definitions(-DUSE_AMCAX_KERNEL)
list(APPEND CMAKE_PREFIX_PATH ${AMCAXRender_PATH})
find_package(AMCAXRender )
find_package(Qt6 COMPONENTS Widgets Core Gui REQUIRED)
list(APPEND CMAKE_PREFIX_PATH ${Json_PATH})
find_package(nlohmann_json 3.11.3 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 ${AMCAX_components})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets)
target_link_libraries(${PROJECT_NAME} PRIVATE AMCAXRender::AMCAXRender)

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.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.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);
mRender->entityManage->AddEntity(entity);
mRender->cameraManage->ResetCamera();
mRender->entityManage->DoRepaint();
}
Mainwindow::~Mainwindow()
{
}
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
Vertex vertex
Vertex data collection
定义 CAXMeshInfo.h:44
std::vector< Edge > edges
Edge data collection
定义 CAXMeshInfo.h:45
std::string topoType
Type, e.g., "point"
定义 CAXMeshInfo.h:16

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>

模型预览

加载struct数据后,可以看到创建的实体。

更多

更多设置请参考 EntityFactory.h EntityManage.h