1.参数
vector<double> d = {
2,1,1,3,
1,1,1,5,
3,2,3,5,
7,5
};
2.代码
#pragma once
#include "InitInclude.h"
#include "vtkPlaneSource.h"
#include <vtkNew.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkPoints.h>
#include <vtkPolygon.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTriangleFilter.h>
#include <vtkProperty.h>
#include <vtkTriangleFilter.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkLinearExtrusionFilter.h>
#include <vtkBMPReader.h>
#include <vtkImageReader2Factory.h>
#include <vector>
// 创建一个二维标准的机房图纸,通过拉伸的方式完成三维的建模。初步完成
void creat2Dmodel_0(vector<double> d)
{
const int num_points = 16;
vtkSmartPointer<vtkUnstructuredGrid> polygonGrid = vtkSmartPointer<vtkUnstructuredGrid>::New();
vtkSmartPointer<vtkPoints> polygonPoints = vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
vtkSmartPointer<vtkDataSetMapper> polygonMapper = vtkSmartPointer<vtkDataSetMapper>::New();
if (0 == d[3])
{
d[4] = 0;
d[5] = 0;
d[6] = d[8];
double top_left[num_points][3] = {
{ 1, 1, 0 },
{ 1 + d[0], 1, 0 },
{ 1 + d[0], 1 + d[1], 0 },
{ 1 + d[0] - d[2], 1 + d[1], 0 },
{ 1 + d[0] - d[2], 1 + d[1] + d[4], 0 },
{ 1 + d[0] - d[2] + d[3], 1 + d[1] + d[4], 0 },
{ 1 + d[0] - d[2] + d[3], 1 + d[1] + d[4] + d[5], 0 },
{ 1 + d[0] - d[2], 1 + d[1] + d[4] + d[5], 0 },
{ 1 + d[0] - d[2], 1 + d[1] + d[4] + d[5] + d[6], 0 },
{ 1 + d[0] - d[2] + d[7], 1 + d[1] + d[4] + d[5] + d[6], 0 },
{ 1 + d[0] - d[2] + d[7], 1 + d[1] + d[4] + d[5] + d[6] - d[8], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9], 1 + d[1] + d[4] + d[5] + d[6] - d[8], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9] + d[10], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9] + d[10], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2] + d[11], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9] + d[10] - d[12], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2] + d[11], 0 },
};
// 几何数据 + 拓扑数据
polygonPoints->SetNumberOfPoints(num_points);
polygon->GetPointIds()->SetNumberOfIds(num_points);
for (int i = 0; i < num_points; ++i){
polygonPoints->SetPoint(i, top_left[i]);
polygon->GetPointIds()->SetId(i, i);
}
}
else
{
double top_left[num_points][3] = {
{ 1, 1, 0 },
{ 1 + d[0], 1, 0 },
{ 1 + d[0], 1 + d[1], 0 },
{ 1 + d[0] - d[2], 1 + d[1], 0 },
{ 1 + d[0] - d[2], 1 + d[1] + d[4], 0 },
{ 1 + d[0] - d[2] + d[3], 1 + d[1] + d[4], 0 },
{ 1 + d[0] - d[2] + d[3], 1 + d[1] + d[4] + d[5], 0 },
{ 1 + d[0] - d[2], 1 + d[1] + d[4] + d[5], 0 },
{ 1 + d[0] - d[2], 1 + d[1] + d[4] + d[5] + d[6], 0 },
{ 1 + d[0] - d[2] + d[7], 1 + d[1] + d[4] + d[5] + d[6], 0 },
{ 1 + d[0] - d[2] + d[7], 1 + d[1] + d[4] + d[5] + d[6] - d[8], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9], 1 + d[1] + d[4] + d[5] + d[6] - d[8], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9] + d[10], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9] + d[10], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2] + d[11], 0 },
{ 1 + d[0] - d[2] + d[7] - d[9] + d[10] - d[12], 1 + d[1] + d[4] + d[5] + d[6] - d[8] - d[2] + d[11], 0 },
};
// 几何数据 + 拓扑数据
polygonPoints->SetNumberOfPoints(num_points);
polygon->GetPointIds()->SetNumberOfIds(num_points);
for (int i = 0; i < num_points; ++i){
polygonPoints->SetPoint(i, top_left[i]);
polygon->GetPointIds()->SetId(i, i);
}
}
polygonGrid->InsertNextCell(polygon->GetCellType(), polygon->GetPointIds());
polygonGrid->SetPoints(polygonPoints);
vtkNew<vtkDataSetSurfaceFilter> surface_filter;
surface_filter->SetInputData(polygonGrid);
surface_filter->Update();
vtkNew<vtkTriangleFilter> filter;
filter->SetInputData(surface_filter->GetOutput());
filter->Update();
polygonMapper->SetInputConnection(filter->GetOutputPort());
vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
polygonMapper->ScalarVisibilityOff();
actor->SetMapper(polygonMapper);
////读取纹理图像
//vtkBMPReader *bmpReader = vtkBMPReader::New();
//bmpReader->SetFileName("D:\\DKL\\work\\project\\001\\code\\QtProjTest\\up256.bmp");
//vtkTexture *atext = vtkTexture::New();
//atext->SetInputConnection(bmpReader->GetOutputPort());
//atext->InterpolateOn();
//vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(polygonMapper);
//actor->GetProperty()->SetOpacity(0.5);
//actor->SetTexture(atext);
render->AddActor(actor);
actor->GetProperty()->SetOpacity(0.5);
actor->GetProperty()->SetColor(1, 0.2, 0.61);
render->AddActor(actor);
render->SetBackground(1, 1, 1); // Background color dark blue
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
//vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(render);
renWin->SetSize(1024, 768);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
renWin->Render();
iren->Initialize();
iren->Start(); //blog.csdn.net/liushao1031177/article/details/118443631
}
void myShow2D(vector<double> d)
{
creat2Dmodel_0(d);
}
3.效果
4.备注
坐标系建立中,图像的右下角是(1,1,0)