ceshi/plugins/CA_PreProcessing/PreProcessing.cpp

1634 lines
56 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "BuildGridSettingWidget.h"
#include "CuboidSettingWidget.h"
#include "ImportGridSettingWidget.h"
#include "PreProcessing.h"
#include "RectangleSettingWidget.h"
#include <QDebug>
#include <QString>
#include <QUuid>
#include <public/services/PhysicalFieldService.h>
#include <public/services/VisualWidgetService.h>
#include <utils/ServiceRegistry.h>
#include<iostream>
#include <fstream>
#include<string>
#include<sstream>
#include<vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "processor/readNas.h"
#include "processor/gen3DGrid.h"
#include "processor/gen2DGrid.h"
#include<vtkPoints.h>
#include<vtkPolyData.h>
#include<vtkCellData.h>
#include<vtkCellArray.h>
#include<vtkQuad.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkHexahedron.h>
#include <vtkAutoInit.h>
#include<vtkUnstructuredGrid.h>
#include<vtkDataSetMapper.h>
#include <QToolButton>
#include <vtkCellPicker.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkSphereSource.h>
#include <set>
#include <vtkCell.h>
#include <vtkIdList.h>
#include <vtkDoubleArray.h>
#include <vtkMapper.h>
#include <vtkInformation.h>
#include <vtkInformationObjectBaseVectorKey.h>
#include <QFile>
#include <vtkQuad.h>
#include <vtkMath.h>
#include <vtkLine.h>
#include <vtkCellArray.h>
#include <vtkLODActor.h>
#include <vtkAppendPolyData.h>
#include <vtkGlyph3D.h>
#include <vtkPointData.h>
#include <vtkCleanPolyData.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkVectorText.h>
#include <vtkLightCollection.h>
#include <vtkCollectionIterator.h>
#include <vtkLight.h>
#include <vtkFollower.h>
#include <vtkCamera.h>
#include <vtkMatrix4x4.h>
PreProcessing::PreProcessing()
{
m_projectManagerService = ServiceRegistry<ProjectManagerService>::instance().getService();
m_visualWidgetService = ServiceRegistry<VisualWidgetService>::instance().getService();
m_uiFrameService = ServiceRegistry<UiFrameService>::instance().getService();
ServiceRegistry<PreProcessingService>::instance().registerService(this);
m_cubiodActor = nullptr;
m_rectangleActor = nullptr;
setupTreeWidget();
m_modelType = -1;
m_status = 0;
}
void PreProcessing::setupTreeWidget()
{
//------------------------------前处理
QToolButton *rectangleButton = new QToolButton;
rectangleButton->setText(QStringLiteral("矩形"));
rectangleButton->setIcon(QIcon(":/resources/menu/rectangle.png"));
m_uiFrameService->addMenuButton(QStringLiteral("前处理"), "preProcessing", rectangleButton);
QToolButton *cubiodButton = new QToolButton;
cubiodButton->setText(QStringLiteral("长方体"));
cubiodButton->setIcon(QIcon(":/resources/menu/cuboid.png"));
m_uiFrameService->addMenuButton(QStringLiteral("前处理"), "preProcessing", cubiodButton);
QToolButton *gridButton = new QToolButton;
gridButton->setText(QStringLiteral("构建网格"));
gridButton->setIcon(QIcon(":/resources/menu/grid.png"));
m_uiFrameService->addMenuButton(QStringLiteral("前处理"), "preProcessing", gridButton);
QToolButton *importModelButton = new QToolButton;
importModelButton->setText(QStringLiteral("导入网格"));
importModelButton->setIcon(QIcon(":/resources/menu/importModel.png"));
m_uiFrameService->addMenuButton(QStringLiteral("前处理"), "preProcessing", importModelButton);
m_buildGridSettingWidget = new BuildGridSettingWidget(this);
m_cuboidSettingWidget = new CuboidSettingWidget(this);
m_importGridSettingWidget = new ImportGridSettingWidget(this);
m_rectangleSettingWidget = new RectangleSettingWidget(this);
QString uuid = QUuid::createUuid().toString();
QTreeWidgetItem *rootItem;
rootItem = new QTreeWidgetItem();
rootItem->setText(0, QStringLiteral("前处理"));
rootItem->setData(0, Qt::UserRole, uuid);
m_projectManagerService->addTreeItem(rootItem);
m_projectManagerService->setItemWidget(uuid,m_importGridSettingWidget);
connect(importModelButton, &QToolButton::clicked, [=]() {
m_projectManagerService->setCurrentItem(rootItem);
});
QTreeWidgetItem *childItem = nullptr;
childItem = new QTreeWidgetItem(rootItem);
childItem->setText(0, QStringLiteral("矩形"));
uuid = QUuid::createUuid().toString();
childItem->setData(0, Qt::UserRole, uuid);
m_projectManagerService->setItemWidget(uuid,m_rectangleSettingWidget);
connect(rectangleButton, &QToolButton::clicked, [=]() {
m_projectManagerService->setCurrentItem(childItem);
});
childItem = new QTreeWidgetItem(rootItem);
childItem->setText(0, QStringLiteral("长方体"));
uuid = QUuid::createUuid().toString();
childItem->setData(0, Qt::UserRole, uuid);
m_projectManagerService->setItemWidget(uuid,m_cuboidSettingWidget);
connect(cubiodButton, &QToolButton::clicked, [=]() {
m_projectManagerService->setCurrentItem(childItem);
});
childItem = new QTreeWidgetItem(rootItem);
childItem->setText(0, QStringLiteral("构建网格"));
uuid = QUuid::createUuid().toString();
childItem->setData(0, Qt::UserRole, uuid);
m_projectManagerService->setItemWidget(uuid,m_buildGridSettingWidget);
connect(gridButton, &QToolButton::clicked, [=]() {
m_projectManagerService->setCurrentItem(childItem);
});
// childItem = new QTreeWidgetItem(rootItem);
// childItem->setText(0, QStringLiteral("导入网格"));
// uuid = QUuid::createUuid().toString();
// childItem->setData(0, Qt::UserRole, uuid);
}
bool PreProcessing::buildquad(ifstream &elements, vtkSmartPointer<vtkCellArray> cells)
{
return true;
}
void PreProcessing::importGrid( QString fileName)
{
int faceNum = 0;
readNas *read = new readNas;
read->readNasFile(fileName.toStdString());
m_gridStructure = read->getStructure();
m_pointStructure = vtkSmartPointer<vtkUnstructuredGrid>::New();
m_pointStructure->DeepCopy(m_gridStructure);
m_faceToPoint = read->getFaceToPoint();
m_faceToCell = read->getFaceToCell();
m_cellIdToGridCellId = read->getCellIdToGridIdMap();
//------------------------------------整体
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(m_gridStructure);
//设置颜色
vtkDataArray *cellcolor = vtkUnsignedCharArray::New();
cellcolor->SetNumberOfComponents(3);
m_gridStructure->GetCellData()->SetScalars(cellcolor);
cellcolor->SetNumberOfTuples(m_gridStructure->GetNumberOfCells());
cellcolor->FillComponent(0, 255);
cellcolor->FillComponent(1, 255);
cellcolor->FillComponent(2, 255);
m_cellActor = vtkSmartPointer<vtkActor>::New();
m_cellActor->SetMapper(mapper);
m_cellActor->GetProperty()->EdgeVisibilityOn();
m_visualWidgetService->removeAllActor();
m_visualWidgetService->addActor(m_cellActor);
m_visualWidgetService->getRender()->ResetCamera();
m_visualWidgetService->getrenderWindow()->Render();
//---------------------------------------------线条
// vtkSmartPointer<vtkDataSetMapper> mapper2 = vtkSmartPointer<vtkDataSetMapper>::New();
// vtkSmartPointer<vtkUnstructuredGrid> test = read->getTest();
// mapper2->SetInputData(test);
// //设置颜色
// vtkDataArray *cellcolor2 = vtkUnsignedCharArray::New();
// cellcolor2->SetNumberOfComponents(3);
// test->GetCellData()->SetScalars(cellcolor2);
// cellcolor2->SetNumberOfTuples(test->GetNumberOfCells());
// cellcolor2->FillComponent(0, 255);
// cellcolor2->FillComponent(1, 0);
// cellcolor2->FillComponent(2, 0);
// vtkSmartPointer<vtkActor> tt = vtkSmartPointer<vtkActor>::New();
// tt->SetMapper(mapper2);
// m_visualWidgetService->addActor(tt);
// m_visualWidgetService->getRender()->ResetCamera();
// m_visualWidgetService->getrenderWindow()->Render();
//---------------------------------------------线条
//点
m_sphereSource = vtkSmartPointer<vtkSphereSource>::New();
m_sphereSource->SetRadius(0.00007); // Adjust the radius to change the size of the points
vtkSmartPointer<vtkGlyph3D> glyphFilter = vtkSmartPointer<vtkGlyph3D>::New();
glyphFilter->SetInputData(m_pointStructure);
glyphFilter->SetSourceConnection(m_sphereSource->GetOutputPort());
glyphFilter->SetColorModeToColorByScalar(); // Use scalar data for colors
glyphFilter->Update();
vtkSmartPointer<vtkPolyDataMapper> glyphMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
glyphMapper->SetInputConnection(glyphFilter->GetOutputPort());
glyphMapper->ScalarVisibilityOn(); // Ensure colors are visible
glyphMapper->SetScalarModeToUsePointData();
m_pointActor = vtkSmartPointer<vtkActor>::New();
m_pointActor->SetMapper(glyphMapper);
m_pointActor->SetPickable(false);
//设置点颜色
vtkSmartPointer<vtkUnsignedCharArray> pointColor = vtkSmartPointer<vtkUnsignedCharArray>::New();
pointColor->SetNumberOfComponents(3);
pointColor->SetName("Colors");
pointColor->SetNumberOfTuples(m_pointStructure->GetNumberOfPoints());
pointColor->FillComponent(0, 255);
pointColor->FillComponent(1, 255);
pointColor->FillComponent(2, 255);
m_pointStructure->GetPointData()->SetScalars(pointColor);
{
vtkSmartPointer<vtkRenderWindow> renderWindow = m_visualWidgetService->getrenderWindow();
vtkSmartPointer<vtkRenderer> renderer = m_visualWidgetService->getRender();
double distance = renderer->GetActiveCamera()->GetDistance();
qDebug()<<"distance "<<distance;
m_sphereSource->SetRadius(0.00007*distance/10);
}
m_status = 1;
}
vtkSmartPointer<vtkSphereSource> PreProcessing::getSphereSource()
{
return m_sphereSource;
}
QMap<int,int> PreProcessing::getCellIdToGridIdMap()
{
return m_cellIdToGridCellId;
}
QVector<QSet<int>> PreProcessing::getFaceToPoint()
{
return m_faceToPoint;
}
vtkSmartPointer<vtkActor> PreProcessing::getPointActor()
{
return m_pointActor;
}
QVector<QVector<int>> PreProcessing::getFaceToCell()
{
return m_faceToCell;
}
vtkSmartPointer<vtkUnstructuredGrid> PreProcessing::getGridStructure()
{
return m_gridStructure;
}
bool PreProcessing::isHaveStructured()
{
if(m_status == 1)
return true;
else
return false;
}
bool PreProcessing::is3DType()
{
if(m_modelType == 1)
return false;
else
return true;
}
vtkSmartPointer<vtkUnstructuredGrid> PreProcessing::getPointStructure()
{
return m_pointStructure;
}
// 添加点
void PreProcessing::displayVertices(vtkSmartPointer<vtkRenderer> renderer, vtkSmartPointer<vtkUnstructuredGrid> grid)
{
VisualWidgetService *tmp = ServiceRegistry<VisualWidgetService>::instance().getService();
// 获取所有的点
vtkSmartPointer<vtkPoints> points = grid->GetPoints();
if (!points) {
std::cerr << "Grid has no points." << std::endl;
return;
}
// 创建一个球体作为点的几何表示
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(0.1); // 球体的半径可以根据需要调整
// 创建球体的映射器
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
qDebug()<<"numsssssssssss "<<points->GetNumberOfPoints();
// 创建每个点的 actor 并添加到渲染器中
for (vtkIdType i = 0; i < points->GetNumberOfPoints(); ++i) {
qDebug()<<i;
double point[3];
points->GetPoint(i, point);
// 创建 actor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->SetPosition(point);
actor->SetScale(0.5);
m_map[actor] = QString::number(i+1) +"(" +QString::number(point[0]) +","+QString::number(point[1])+","+QString::number(point[2])+")";
actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
actor->GetProperty()->SetOpacity(0.5);
// actor->GetProperty()->SetRepresentationToSurface();
tmp->addActor(actor);
}
}
// 添加边
void PreProcessing::displayEdges(vtkSmartPointer<vtkRenderer> renderer, vtkSmartPointer<vtkUnstructuredGrid> grid) {
// // 检查渲染器中是否已包含边的 vtkActor
// vtkSmartPointer<vtkActorCollection> actors = renderer->GetActors();
// actors->InitTraversal();
// while (vtkActor* actor = actors->GetNextActor()) {
// if (actor->GetMapper()->IsA("vtkPolyDataMapper")) {
// vtkPolyDataMapper* mapper = dynamic_cast<vtkPolyDataMapper*>(actor->GetMapper());
// if (mapper && mapper->GetInput() && mapper->GetInput()->IsA("vtkLines")) {
// // 如果已经存在边的 vtkActor则直接返回
// return;
// }
// }
// }
// // 创建边的 vtkActor
// vtkSmartPointer<vtkCellArray> cells = grid->GetCells();
// vtkSmartPointer<vtkIdList> cellPoints = vtkSmartPointer<vtkIdList>::New();
// for (cells->InitTraversal(); cells->GetNextCell(cellPoints); ) {
// vtkSmartPointer<vtkIdList> cellPoints = vtkSmartPointer<vtkIdList>::New();
// cells->GetNextCell(cellPoints);
// if (cellPoints->GetNumberOfIds() > 1) { // 仅绘制包含至少两个点的单元的边
// vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New();
// polyLine->GetPointIds()->SetNumberOfIds(cellPoints->GetNumberOfIds());
// for (vtkIdType i = 0; i < cellPoints->GetNumberOfIds(); ++i) {
// polyLine->GetPointIds()->SetId(i, cellPoints->GetId(i));
// }
// vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
// lines->InsertNextCell(polyLine);
// vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
// polyData->SetPoints(grid->GetPoints());
// polyData->SetLines(lines);
// vtkSmartPointer<vtkPolyDataMapper> edgeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
// edgeMapper->SetInputData(polyData);
// vtkSmartPointer<vtkActor> edgeActor = vtkSmartPointer<vtkActor>::New();
// edgeActor->SetMapper(edgeMapper);
// edgeActor->GetProperty()->SetColor(0.0, 1.0, 0.0); // 设置边的颜色为绿色
// renderer->AddActor(edgeActor);
// }
// }
}
// 添加面
void PreProcessing::displayFaces(vtkSmartPointer<vtkRenderer> renderer, vtkSmartPointer<vtkUnstructuredGrid> grid) {
VisualWidgetService *tmp = ServiceRegistry<VisualWidgetService>::instance().getService();
// Get the number of cells
vtkIdType numCells = grid->GetNumberOfCells();
QMap<QString,int>mp;
// Iterate over each cell
for (vtkIdType cellId = 0; cellId < numCells; ++cellId) {
vtkSmartPointer<vtkCell> cell = grid->GetCell(cellId);
// Get the number of faces (polygons) in the cell
int numFaces = cell->GetNumberOfFaces();
// Iterate over each face of the cell
for (int faceId = 0; faceId < numFaces; ++faceId) {
vtkSmartPointer<vtkIdList> faceIds = vtkSmartPointer<vtkIdList>::New();
// Get the face (vtkIdList) of the cell
vtkCell *facecell = cell->GetFace( faceId);
std::cout << "Cell " << cellId << ", Face " << faceId << " points: ";
vtkPoints *points = facecell->GetPoints();
std::cout <<points->GetNumberOfPoints();
vtkSmartPointer<vtkPoints> newpoints = vtkSmartPointer<vtkPoints>::New();
QVector<int>vector;
QString str = QString::number(faceId + 1);
for (int i = 0; i < points->GetNumberOfPoints(); ++i) {
double point[3]; // 存储点的坐标
// 获取第i个点的坐标
points->GetPoint(i, point);
newpoints->InsertNextPoint(point);
std::cout << "Point " << i << ": (" << point[0] << ", " << point[1] << ", " << point[2] << ")" << std::endl;
str += QString(" Point %1: (%2, %3, %4)").arg(i)
.arg(point[0])
.arg(point[1])
.arg(point[2]);
}
if(mp[str] == 1)
{
continue;
}
mp[str] = 0;
// 创建 vtkCellArray 对象
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
vtkIdType vertices[5] = {0, 1, 2, 3, 0}; // 闭合
cells->InsertNextCell(5, vertices);
// 创建 vtkPolyData
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(newpoints);
polyData->SetPolys(cells);
// 创建 vtkPolyDataMapper
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
// 创建 vtkActor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
actor->GetProperty()->SetOpacity(0.5);
tmp->addActor(actor);
m_map[actor] = str;
std::cout << std::endl;
}
}
}
bool PreProcessing::buildcells(ifstream& elements, vtkSmartPointer<vtkCellArray> cells,int state, vtkSmartPointer<vtkUnstructuredGrid> Grid)
{
if (!elements.is_open())return false;
elements.seekg(0, ios::beg);
string strLine;
if (!getline(elements, strLine))return false;
int NElem = stoi(strLine);
//跳过空格行
getline(elements, strLine);
if (state==0)
{
vector<int>quad(4, 0);
while (NElem--)
{
vtkSmartPointer<vtkQuad> quadpoly = vtkSmartPointer<vtkQuad>::New();
getline(elements, strLine);
istringstream iss(strLine);
string tem;
//跳过前两条
iss >> tem;
iss >> tem;
//文件起始序号为1VTK为0在原序号上-1
for (int i = 0;i < 4;i++)
{
iss >> tem;
quad[i] = stoi(tem) - 1;
quadpoly->GetPointIds()->SetId(i, quad[i]);
}
//cout << quad[0] << " " << quad[1] << " " << quad[2] << " " << quad[3] << endl;
cells->InsertNextCell(quadpoly);
Grid->InsertNextCell(quadpoly->GetCellType(), quadpoly->GetPointIds());
//cout << "quadcelltypenumber is " << quadpoly->GetCellType() << endl;
}
return true;
}
//读取六面体
else if (state == 1)
{
vector<int>hexah(8, 0);
while (NElem--)
{
vtkSmartPointer<vtkHexahedron> hexpoly = vtkSmartPointer<vtkHexahedron>::New();
getline(elements, strLine);
istringstream iss(strLine);
string tem;
//跳过前两条
iss >> tem;
iss >> tem;
//文件起始序号为1VTK为0在原序号上-1
for (int i = 0;i < 8;i++)
{
iss >> tem;
hexah[i] = stoi(tem) - 1;
hexpoly->GetPointIds()->SetId(i, hexah[i]);
//cout << hexah[0] << " " << hexah[1] << " " << hexah[2] << " " << hexah[3] << " " << hexah[4] << " " << hexah[5] << " " << hexah[6] << " " << hexah[7] << endl;
}
cells->InsertNextCell(hexpoly);
Grid->InsertNextCell(hexpoly->GetCellType(), hexpoly->GetPointIds());
}
return true;
}
return false;
}
void PreProcessing::test()
{
}
void PreProcessing::generateRectangle(float x,float y,float ox,float oy,int type)
{
m_modelType = 1;
m_x = x; m_y = y; m_z = 0;
m_ox = ox; m_oy = oy; m_oz = 0;
char out[100];
char outcor[100];
float length = x;
float width = y;
float height = 10;
float max_jiange_x = 20;
float max_jiange_y = 100;
float max_jiange_z = 20;
float origin_x =ox;
float origin_y =oy;
float origin_z =0;
int max_num_x;
int max_num_y;
int max_num_z;
max_num_x = floor(length/max_jiange_x) + 1;
max_num_y = floor(width/max_jiange_y) + 1;
max_num_z = floor(height/max_jiange_z) + 1;
//std::cout<<length/max_jiange_x<<std::endl;
//std::cout<<width/max_jiange_y<<std::endl;
//std::cout<<height/max_jiange_z<<std::endl;
//std::cout<<max_num_x<<std::endl;
//std::cout<<max_num_y<<std::endl;
//std::cout<<max_num_z<<std::endl;
if(ceil(length/max_jiange_x) == floor(length/max_jiange_x))
max_num_x = floor(length/max_jiange_x);
if(ceil(width/max_jiange_y) == floor(width/max_jiange_y))
max_num_y = floor(width/max_jiange_y);
if(ceil(height/max_jiange_z) == floor(height/max_jiange_z))
max_num_z = floor(height/max_jiange_z);
vector<float> vec_points;
vector<int> vec_elements;
float point_min_x = 0.0;
float point_min_y = 0.0;
float point_min_z = 0.0;
float point_max_x = 0.0;
float point_max_y = 0.0;
float point_max_z = 0.0;
float use_interval_x = 0.0;
float use_interval_y = 0.0;
float use_interval_z = 0.0;
use_interval_x = length / max_num_x;
use_interval_y = width / max_num_y;
use_interval_z = height / max_num_z;
int use_number_x = max_num_x + 1;
int use_number_y = max_num_y + 1;
int use_number_z = max_num_z + 1;
//std::cout<<use_number_x<<std::endl;
//std::cout<<use_number_y<<std::endl;
//std::cout<<use_number_z<<std::endl;
if(0==type)// jiao
{
point_min_x = origin_x + 0;
point_min_y = origin_y + 0;
point_min_z = origin_z + 0;
point_max_x = origin_x + length;
point_max_y = origin_y + width;
point_max_z = origin_z + height;
}
else if(1==type)
{
point_min_x = origin_x - length/2.0;
point_min_y = origin_y - width/2.0;
point_min_z = origin_z - height/2.0;
point_max_x = origin_x + length/2.0;
point_max_y = origin_y + width/2.0;
point_max_z = origin_z + height/2.0;
}
float temp_x = 0.0;
float temp_y = 0.0;
float temp_z = 0.0;
auto start_time = std::chrono::high_resolution_clock::now();
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
float point[3];
point[0] = point_min_x;
point[1] = point_min_y;
point[2] = 0;
points->InsertNextPoint(point);
point[0] = point_max_x;
point[1] = point_min_y;
point[2] = 0;
points->InsertNextPoint(point);
point[0] = point_max_x;
point[1] = point_max_y;
point[2] = 0;
points->InsertNextPoint(point);
point[0] = point_min_x;
point[1] = point_max_y;
point[2] = 0;
points->InsertNextPoint(point);
//创建vtk拓扑数据结构
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkQuad> quadpoly = vtkSmartPointer<vtkQuad>::New();
vtkSmartPointer<vtkPolyData> poly = vtkSmartPointer<vtkPolyData>::New();
quadpoly->GetPointIds()->SetId(0, 0);
quadpoly->GetPointIds()->SetId(1, 1);
quadpoly->GetPointIds()->SetId(2, 2);
quadpoly->GetPointIds()->SetId(3, 3);
cells->InsertNextCell(quadpoly);
poly->SetPoints(points);
poly->SetPolys(cells);
//构建渲染管线
vtkSmartPointer<vtkDataSetMapper>mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(poly);
m_rectangleActor = vtkSmartPointer<vtkActor>::New();
m_rectangleActor->GetProperty()->EdgeVisibilityOff();
m_rectangleActor->SetMapper(mapper);
m_visualWidgetService->removeAllActor();
m_cubiodActor = nullptr;
m_rectangleActor->GetProperty()->SetColor(0.85,0.85,0.85);
m_visualWidgetService->addActor(m_rectangleActor);
}
void PreProcessing::generateCubiod(float x,float y, float z, float ox,float oy,float oz,int type1)
{
m_modelType = 0;
char out[100];
char outcor[100];
float length = x;
float width = y;
float height = z;
m_x = x;
m_y = y;
m_z = z;
float max_jiange_x = 20;
float max_jiange_y = 100;
float max_jiange_z = 20;
float origin_x = ox;
float origin_y = oy;
float origin_z = oz;
m_ox = ox;
m_oy = oy;
m_oz = oz;
qDebug()<<origin_x<<" "<<origin_y<<" "<<origin_z;
int type = type1;
int max_num_x;
int max_num_y;
int max_num_z;
max_num_x = floor(length/max_jiange_x) + 1;
max_num_y = floor(width/max_jiange_y) + 1;
max_num_z = floor(height/max_jiange_z) + 1;
//std::cout<<length/max_jiange_x<<std::endl;
//std::cout<<width/max_jiange_y<<std::endl;
//std::cout<<height/max_jiange_z<<std::endl;
//std::cout<<max_num_x<<std::endl;
//std::cout<<max_num_y<<std::endl;
//std::cout<<max_num_z<<std::endl;
if(ceil(length/max_jiange_x) == floor(length/max_jiange_x))
max_num_x = floor(length/max_jiange_x);
if(ceil(width/max_jiange_y) == floor(width/max_jiange_y))
max_num_y = floor(width/max_jiange_y);
if(ceil(height/max_jiange_z) == floor(height/max_jiange_z))
max_num_z = floor(height/max_jiange_z);
vector<float> vec_points;
vector<int> vec_elements;
float point_min_x = 0.0;
float point_min_y = 0.0;
float point_min_z = 0.0;
float point_max_x = 0.0;
float point_max_y = 0.0;
float point_max_z = 0.0;
float use_interval_x = 0.0;
float use_interval_y = 0.0;
float use_interval_z = 0.0;
use_interval_x = length / max_num_x;
use_interval_y = width / max_num_y;
use_interval_z = height / max_num_z;
int use_number_x = max_num_x + 1;
int use_number_y = max_num_y + 1;
int use_number_z = max_num_z + 1;
//std::cout<<use_number_x<<std::endl;
//std::cout<<use_number_y<<std::endl;
//std::cout<<use_number_z<<std::endl;
if(0==type)//dian
{
point_min_x = origin_x + 0;
point_min_y = origin_y + 0;
point_min_z = origin_z + 0;
point_max_x = origin_x + length;
point_max_y = origin_y + width;
point_max_z = origin_z + height;
}
else if(1==type)//zhong xing dian
{
point_min_x = origin_x - length/2.0;
point_min_y = origin_y - width/2.0;
point_min_z = origin_z - height/2.0;
point_max_x = origin_x + length/2.0;
point_max_y = origin_y + width/2.0;
point_max_z = origin_z + height/2.0;
}
float temp_x = 0.0;
float temp_y = 0.0;
float temp_z = 0.0;
auto start_time = std::chrono::high_resolution_clock::now();
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
float point[3];
point[0] = point_min_x;
point[1] = point_min_y;
point[2] = point_min_z;
points->InsertNextPoint(point);
point[0] = point_max_x;
point[1] = point_min_y;
point[2] = point_min_z;
points->InsertNextPoint(point);
point[0] = point_max_x;
point[1] = point_max_y;
point[2] = point_min_z;
points->InsertNextPoint(point);
point[0] = point_min_x;
point[1] = point_max_y;
point[2] = point_min_z;
points->InsertNextPoint(point);
point[0] = point_min_x;
point[1] = point_min_y;
point[2] = point_max_z;
points->InsertNextPoint(point);
point[0] = point_max_x;
point[1] = point_min_y;
point[2] = point_max_z;
points->InsertNextPoint(point);
point[0] = point_max_x;
point[1] = point_max_y;
point[2] = point_max_z;
points->InsertNextPoint(point);
point[0] = point_min_x;
point[1] = point_max_y;
point[2] = point_max_z;
points->InsertNextPoint(point);
//创建vtk拓扑数据结构
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkQuad> quadpoly = vtkSmartPointer<vtkQuad>::New();
vtkSmartPointer<vtkPolyData> poly = vtkSmartPointer<vtkPolyData>::New();
quadpoly->GetPointIds()->SetId(0, 0);
quadpoly->GetPointIds()->SetId(1, 1);
quadpoly->GetPointIds()->SetId(2, 2);
quadpoly->GetPointIds()->SetId(3, 3);
cells->InsertNextCell(quadpoly);
quadpoly->GetPointIds()->SetId(0, 4);
quadpoly->GetPointIds()->SetId(1, 5);
quadpoly->GetPointIds()->SetId(2, 6);
quadpoly->GetPointIds()->SetId(3, 7);
cells->InsertNextCell(quadpoly);
quadpoly->GetPointIds()->SetId(0, 0);
quadpoly->GetPointIds()->SetId(1, 1);
quadpoly->GetPointIds()->SetId(2, 5);
quadpoly->GetPointIds()->SetId(3, 4);
cells->InsertNextCell(quadpoly);
quadpoly->GetPointIds()->SetId(0, 3);
quadpoly->GetPointIds()->SetId(1, 2);
quadpoly->GetPointIds()->SetId(2, 6);
quadpoly->GetPointIds()->SetId(3, 7);
cells->InsertNextCell(quadpoly);
quadpoly->GetPointIds()->SetId(0, 3);
quadpoly->GetPointIds()->SetId(1, 0);
quadpoly->GetPointIds()->SetId(2, 4);
quadpoly->GetPointIds()->SetId(3, 7);
cells->InsertNextCell(quadpoly);
quadpoly->GetPointIds()->SetId(0, 2);
quadpoly->GetPointIds()->SetId(1, 1);
quadpoly->GetPointIds()->SetId(2, 5);
quadpoly->GetPointIds()->SetId(3, 6);
cells->InsertNextCell(quadpoly);
poly->SetPoints(points);
poly->SetPolys(cells);
//构建渲染管线
vtkSmartPointer<vtkDataSetMapper>mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(poly);
m_cubiodActor = vtkSmartPointer<vtkActor>::New();
m_cubiodActor->GetProperty()->EdgeVisibilityOff();
m_cubiodActor->SetMapper(mapper);
m_cubiodActor->GetProperty()->SetColor(0.85,0.85,0.85);
m_visualWidgetService->removeAllActor();
m_rectangleActor = nullptr;
m_visualWidgetService->addActor(m_cubiodActor);
}
void PreProcessing::generateGrid(float maxgrid)
{
if(m_modelType == -1)
{
return;
}
if(!m_modelType)
{
gen3DGrid *read = new gen3DGrid;
float length = m_x;
float width = m_y;
float height = m_z;
length = 10;
width = 10;
height = 10;
read->setLength(length);
read->setWidth(width);
read->setHeight(height);
read->setMaxInterval(maxgrid);
int faceNum = 0;
read->genProcess();
m_gridStructure = read->getStructure();
m_pointStructure = vtkSmartPointer<vtkUnstructuredGrid>::New();
m_pointStructure->DeepCopy(m_gridStructure);
m_faceToPoint = read->getFaceToPoint();
m_faceToCell = read->getFaceToCell();
m_cellIdToGridCellId = read->getCellIdToGridIdMap();
//------------------------------------整体
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(m_gridStructure);
//设置颜色
vtkDataArray *cellcolor = vtkUnsignedCharArray::New();
cellcolor->SetNumberOfComponents(3);
m_gridStructure->GetCellData()->SetScalars(cellcolor);
cellcolor->SetNumberOfTuples(m_gridStructure->GetNumberOfCells());
cellcolor->FillComponent(0, 255);
cellcolor->FillComponent(1, 255);
cellcolor->FillComponent(2, 255);
m_cellActor = vtkSmartPointer<vtkActor>::New();
m_cellActor->SetMapper(mapper);
m_cellActor->GetProperty()->EdgeVisibilityOn();
m_visualWidgetService->removeAllActor();
m_visualWidgetService->addActor(m_cellActor);
m_visualWidgetService->getRender()->ResetCamera();
m_visualWidgetService->getrenderWindow()->Render();
//---------------------------------------------线条
// vtkSmartPointer<vtkDataSetMapper> mapper2 = vtkSmartPointer<vtkDataSetMapper>::New();
// vtkSmartPointer<vtkUnstructuredGrid> test = read->getTest();
// mapper2->SetInputData(test);
// //设置颜色
// vtkDataArray *cellcolor2 = vtkUnsignedCharArray::New();
// cellcolor2->SetNumberOfComponents(3);
// test->GetCellData()->SetScalars(cellcolor2);
// cellcolor2->SetNumberOfTuples(test->GetNumberOfCells());
// cellcolor2->FillComponent(0, 255);
// cellcolor2->FillComponent(1, 0);
// cellcolor2->FillComponent(2, 0);
// vtkSmartPointer<vtkActor> tt = vtkSmartPointer<vtkActor>::New();
// tt->SetMapper(mapper2);
// m_visualWidgetService->addActor(tt);
// m_visualWidgetService->getRender()->ResetCamera();
// m_visualWidgetService->getrenderWindow()->Render();
//---------------------------------------------线条
//点
m_sphereSource = vtkSmartPointer<vtkSphereSource>::New();
m_sphereSource->SetRadius(0.00007); // Adjust the radius to change the size of the points
vtkSmartPointer<vtkGlyph3D> glyphFilter = vtkSmartPointer<vtkGlyph3D>::New();
glyphFilter->SetInputData(m_pointStructure);
glyphFilter->SetSourceConnection(m_sphereSource->GetOutputPort());
glyphFilter->SetColorModeToColorByScalar(); // Use scalar data for colors
glyphFilter->Update();
vtkSmartPointer<vtkPolyDataMapper> glyphMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
glyphMapper->SetInputConnection(glyphFilter->GetOutputPort());
glyphMapper->ScalarVisibilityOn(); // Ensure colors are visible
glyphMapper->SetScalarModeToUsePointData();
m_pointActor = vtkSmartPointer<vtkActor>::New();
m_pointActor->SetMapper(glyphMapper);
m_pointActor->SetPickable(false);
//设置点颜色
vtkSmartPointer<vtkUnsignedCharArray> pointColor = vtkSmartPointer<vtkUnsignedCharArray>::New();
pointColor->SetNumberOfComponents(3);
pointColor->SetName("Colors");
pointColor->SetNumberOfTuples(m_pointStructure->GetNumberOfPoints());
pointColor->FillComponent(0, 255);
pointColor->FillComponent(1, 255);
pointColor->FillComponent(2, 255);
m_pointStructure->GetPointData()->SetScalars(pointColor);
{
vtkSmartPointer<vtkRenderWindow> renderWindow = m_visualWidgetService->getrenderWindow();
vtkSmartPointer<vtkRenderer> renderer = m_visualWidgetService->getRender();
double distance = renderer->GetActiveCamera()->GetDistance();
qDebug()<<"distance "<<distance;
m_sphereSource->SetRadius(0.00007*distance/10);
}
m_status = 1;
}
else
{
gen2DGrid *read = new gen2DGrid;
float length = m_x;
float width = m_y;
float height = m_z;
length = 10;
width = 10;
height = 10;
read->setLength(length);
read->setWidth(width);
//read->setHeight(height);
read->setMaxInterval(maxgrid);
int faceNum = 0;
read->genProcess();
m_gridStructure = read->getStructure();
m_gridStructure_origin = read->getStructureOrigin();
m_pointStructure = vtkSmartPointer<vtkUnstructuredGrid>::New();
m_pointStructure->DeepCopy(m_gridStructure);
m_faceToPoint = read->getFaceToPoint();
m_faceToCell = read->getFaceToCell();
m_cellIdToGridCellId = read->getCellIdToGridIdMap();
//------------------------------------整体
vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(m_gridStructure);
mapper->SetRelativeCoincidentTopologyPolygonOffsetParameters(-500.0,-500.0);
mapper->SetRelativeCoincidentTopologyLineOffsetParameters(-500.0,-500.0);
mapper->SetRelativeCoincidentTopologyPointOffsetParameter(-500.0);
//设置颜色
vtkDataArray *cellcolor = vtkUnsignedCharArray::New();
cellcolor->SetNumberOfComponents(3);
//m_gridStructure->GetCellData()->SetScalars(cellcolor);
m_gridStructure->GetCellData()->SetScalars(cellcolor);
cellcolor->SetNumberOfTuples(m_gridStructure->GetNumberOfCells());
cellcolor->FillComponent(0, 0);
cellcolor->FillComponent(1, 0);
cellcolor->FillComponent(2, 0);
m_cellActor = vtkSmartPointer<vtkActor>::New();
m_cellActor->SetMapper(mapper);
m_cellActor->GetProperty()->EdgeVisibilityOn();
m_cellActor->GetProperty()->SetLineWidth(5);
//m_cellActor->SetPickable(false);
vtkSmartPointer<vtkDataSetMapper> mapper2 = vtkSmartPointer<vtkDataSetMapper>::New();
mapper2->SetInputData(m_gridStructure_origin);
mapper2->SetRelativeCoincidentTopologyPolygonOffsetParameters(500.0,500.0);
mapper2->SetRelativeCoincidentTopologyLineOffsetParameters(500.0,500.0);
mapper2->SetRelativeCoincidentTopologyPointOffsetParameter(500.0);
//设置颜色
vtkDataArray *cellcolor2 = vtkUnsignedCharArray::New();
cellcolor2->SetNumberOfComponents(3);
//m_gridStructure->GetCellData()->SetScalars(cellcolor);
m_gridStructure->GetCellData()->SetScalars(cellcolor);
cellcolor2->SetNumberOfTuples(m_gridStructure_origin->GetNumberOfCells());
cellcolor2->FillComponent(0, 0);
cellcolor2->FillComponent(1, 0);
cellcolor2->FillComponent(2, 0);
m_cellActor2 = vtkSmartPointer<vtkActor>::New();
m_cellActor2->SetMapper(mapper2);
m_cellActor2->GetProperty()->EdgeVisibilityOn();
m_cellActor2->SetPickable(false);
m_visualWidgetService->removeAllActor();
m_visualWidgetService->addActor(m_cellActor);
m_visualWidgetService->addActor(m_cellActor2);
m_visualWidgetService->getRender()->ResetCamera();
m_visualWidgetService->getrenderWindow()->Render();
//点
m_sphereSource = vtkSmartPointer<vtkSphereSource>::New();
m_sphereSource->SetRadius(0.00007); // Adjust the radius to change the size of the points
vtkSmartPointer<vtkGlyph3D> glyphFilter = vtkSmartPointer<vtkGlyph3D>::New();
glyphFilter->SetInputData(m_pointStructure);
glyphFilter->SetSourceConnection(m_sphereSource->GetOutputPort());
glyphFilter->SetColorModeToColorByScalar(); // Use scalar data for colors
glyphFilter->Update();
vtkSmartPointer<vtkPolyDataMapper> glyphMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
glyphMapper->SetInputConnection(glyphFilter->GetOutputPort());
glyphMapper->ScalarVisibilityOn(); // Ensure colors are visible
glyphMapper->SetScalarModeToUsePointData();
glyphMapper->SetRelativeCoincidentTopologyPolygonOffsetParameters(-3000.0,-3000.0);
glyphMapper->SetRelativeCoincidentTopologyLineOffsetParameters(-3000.0,-3000.0);
glyphMapper->SetRelativeCoincidentTopologyPointOffsetParameter(-3000.0);
m_pointActor = vtkSmartPointer<vtkActor>::New();
m_pointActor->SetMapper(glyphMapper);
m_pointActor->SetPickable(false);
//设置点颜色
vtkSmartPointer<vtkUnsignedCharArray> pointColor = vtkSmartPointer<vtkUnsignedCharArray>::New();
pointColor->SetNumberOfComponents(3);
pointColor->SetName("Colors");
pointColor->SetNumberOfTuples(m_pointStructure->GetNumberOfPoints());
pointColor->FillComponent(0, 255);
pointColor->FillComponent(1, 255);
pointColor->FillComponent(2, 255);
m_pointStructure->GetPointData()->SetScalars(pointColor);
{
vtkSmartPointer<vtkRenderWindow> renderWindow = m_visualWidgetService->getrenderWindow();
vtkSmartPointer<vtkRenderer> renderer = m_visualWidgetService->getRender();
double distance = renderer->GetActiveCamera()->GetDistance();
qDebug()<<"distance "<<distance;
m_sphereSource->SetRadius(0.00007*distance/10);
}
m_status = 1;
}
}
/*
void PreProcessing::generateGrid(float maxgrid)
{
std::cout<<"666666666666666666666666666"<<std::endl;
if(m_modelType == -1)
{
return;
}
if(m_modelType)
{
char out[100];
char outcor[100];
float length = m_x;
float width = m_y;
float height = m_z;
float max_jiange_x = maxgrid;
float max_jiange_y = maxgrid;
float max_jiange_z = maxgrid;
float origin_x = 0;
float origin_y = 0;
float origin_z = 0;
int type = 0;
int max_num_x;
int max_num_y;
int max_num_z;
max_num_x = floor(length/max_jiange_x) + 1;
max_num_y = floor(width/max_jiange_y) + 1;
max_num_z = floor(height/max_jiange_z) + 1;
//std::cout<<length/max_jiange_x<<std::endl;
//std::cout<<width/max_jiange_y<<std::endl;
//std::cout<<height/max_jiange_z<<std::endl;
//std::cout<<max_num_x<<std::endl;
//std::cout<<max_num_y<<std::endl;
//std::cout<<max_num_z<<std::endl;
if(ceil(length/max_jiange_x) == floor(length/max_jiange_x))
max_num_x = floor(length/max_jiange_x);
if(ceil(width/max_jiange_y) == floor(width/max_jiange_y))
max_num_y = floor(width/max_jiange_y);
if(ceil(height/max_jiange_z) == floor(height/max_jiange_z))
max_num_z = floor(height/max_jiange_z);
vector<float> vec_points;
vector<int> vec_elements;
float point_min_x = 0.0;
float point_min_y = 0.0;
float point_min_z = 0.0;
float point_max_x = 0.0;
float point_max_y = 0.0;
float point_max_z = 0.0;
float use_interval_x = 0.0;
float use_interval_y = 0.0;
float use_interval_z = 0.0;
use_interval_x = length / max_num_x;
use_interval_y = width / max_num_y;
use_interval_z = height / max_num_z;
int use_number_x = max_num_x + 1;
int use_number_y = max_num_y + 1;
int use_number_z = max_num_z + 1;
//std::cout<<use_number_x<<std::endl;
//std::cout<<use_number_y<<std::endl;
//std::cout<<use_number_z<<std::endl;
if(0==type)
{
point_min_x = origin_x + 0;
point_min_y = origin_y + 0;
point_min_z = origin_y + 0;
point_max_x = origin_x + length;
point_max_y = origin_y + width;
point_max_z = origin_z + height;
}
else if(1==type)
{
point_min_x = origin_x - length/2.0;
point_min_y = origin_y - width/2.0;
point_min_z = origin_y - height/2.0;
point_max_x = origin_x + length/2.0;
point_max_y = origin_y + width/2.0;
point_max_z = origin_z + height/2.0;
}
float temp_x = 0.0;
float temp_y = 0.0;
float temp_z = 0.0;
auto start_time = std::chrono::high_resolution_clock::now();
for (int j=0;j<use_number_y;j++)
{
temp_y = point_min_y + use_interval_y*j;
if (j==use_number_y-1)
{
temp_y = point_max_y;
}
for (int i=0;i<use_number_x;i++)
{
temp_x = point_min_x + use_interval_x*i;
if (i==use_number_x-1)
{
temp_x = point_max_x;
}
vec_points.push_back(temp_x);
vec_points.push_back(temp_y);
vec_points.push_back(0);
}
}
for (int j=0;j<use_number_y-1;j++)
{
for (int i=0;i<use_number_x-1;i++)
{
vec_elements.push_back(i+j*use_number_x);
vec_elements.push_back((i+1)+j*use_number_x);
vec_elements.push_back((i+1)+(j+1)*use_number_x);
vec_elements.push_back(i+(j+1)*use_number_x);
}
}
//char hexIDstr_cor[100];
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
float point[3];
int count1 = 0;
for (auto iter = vec_points.begin(); iter != vec_points.end();)
{
//std::cout<<*iter++;
//std::cout<<*iter++;
//std::cout<<*iter++<<endl;
point[0]=*iter++;
point[1]=*iter++;
point[2]=*iter++;
points->InsertNextPoint(point);
count1=count1+1;
}
//创建vtk拓扑数据结构
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
//创建vtkpolydata
vtkSmartPointer<vtkUnstructuredGrid> poly = vtkSmartPointer<vtkUnstructuredGrid>::New();
poly->SetPoints(points);
int quad[4];
char hexIDstr[100];
auto end_time1 = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end_time1 - start_time).count();
// cout << "程序运行时间1" << duration1 << " 微秒" << endl;
ostringstream ostr1;
int count = 0;
for (auto iter = vec_elements.begin(); iter != vec_elements.end();)
{
vtkSmartPointer<vtkQuad> quadpoly = vtkSmartPointer<vtkQuad>::New();
//std::cout<<*iter++;
//std::cout<<*iter++;
//std::cout<<*iter++<<endl;
quad[0]=*iter++;
quad[1]=*iter++;
quad[2]=*iter++;
quad[3]=*iter++;
quadpoly->GetPointIds()->SetId(0, quad[0]);
quadpoly->GetPointIds()->SetId(1, quad[1]);
quadpoly->GetPointIds()->SetId(2, quad[2]);
quadpoly->GetPointIds()->SetId(3, quad[3]);
poly->InsertNextCell(quadpoly->GetCellType(),quadpoly->GetPointIds());
//snprintf(hexIDstr, 100, "%d 1 %d %d %d %d %d %d %d %d" , count + 1, quad[0] + 1, quad[1] + 1, quad[2] + 1, quad[3] + 1,quad[4] + 1, quad[5] + 1, quad[6] + 1, quad[7] + 1);
ostr1<<count + 1<<" "<<1<<" ";
ostr1<<quad[0] + 1<<" ";
ostr1<<quad[1] + 1<<" ";
ostr1<<quad[2] + 1<<" ";
ostr1<<quad[3] + 1<<" ";
ostr1<<quad[4] + 1<<" ";
ostr1<<quad[5] + 1<<" ";
ostr1<<quad[6] + 1<<" ";
ostr1<<quad[7] + 1<<" ";
ostr1.str("");
//os << hexIDstr << endl;
count =count +1;
}
auto end_time2 = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end_time2 - start_time).count();
// cout << "程序运行时间2" << duration2 << " 微秒" << endl;
//构建渲染管线
vtkSmartPointer<vtkDataSetMapper>mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(poly);
vtkSmartPointer<vtkActor>actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->EdgeVisibilityOn();
actor->SetMapper(mapper);
m_visualWidgetService->removeAllActor();
m_rectangleActor = nullptr;
actor->GetProperty()->SetColor(0.85,0.85,0.85);
m_visualWidgetService->addActor(actor);
}
else
{
char out[100];
char outcor[100];
float length = m_x;
float width = m_y;
float height = m_z;
float max_jiange_x = maxgrid;
float max_jiange_y = maxgrid;
float max_jiange_z = maxgrid;
float origin_x = 0;
float origin_y = 0;
float origin_z = 0;
int type = 0;
int max_num_x;
int max_num_y;
int max_num_z;
max_num_x = floor(length/max_jiange_x) + 1;
max_num_y = floor(width/max_jiange_y) + 1;
max_num_z = floor(height/max_jiange_z) + 1;
//std::cout<<length/max_jiange_x<<std::endl;
//std::cout<<width/max_jiange_y<<std::endl;
//std::cout<<height/max_jiange_z<<std::endl;
//std::cout<<max_num_x<<std::endl;
//std::cout<<max_num_y<<std::endl;
//std::cout<<max_num_z<<std::endl;
if(ceil(length/max_jiange_x) == floor(length/max_jiange_x))
max_num_x = floor(length/max_jiange_x);
if(ceil(width/max_jiange_y) == floor(width/max_jiange_y))
max_num_y = floor(width/max_jiange_y);
if(ceil(height/max_jiange_z) == floor(height/max_jiange_z))
max_num_z = floor(height/max_jiange_z);
vector<float> vec_points;
vector<int> vec_elements;
float point_min_x = 0.0;
float point_min_y = 0.0;
float point_min_z = 0.0;
float point_max_x = 0.0;
float point_max_y = 0.0;
float point_max_z = 0.0;
float use_interval_x = 0.0;
float use_interval_y = 0.0;
float use_interval_z = 0.0;
use_interval_x = length / max_num_x;
use_interval_y = width / max_num_y;
use_interval_z = height / max_num_z;
int use_number_x = max_num_x + 1;
int use_number_y = max_num_y + 1;
int use_number_z = max_num_z + 1;
//std::cout<<use_number_x<<std::endl;
//std::cout<<use_number_y<<std::endl;
//std::cout<<use_number_z<<std::endl;
if(0==type)
{
point_min_x = origin_x + 0;
point_min_y = origin_y + 0;
point_min_z = origin_y + 0;
point_max_x = origin_x + length;
point_max_y = origin_y + width;
point_max_z = origin_z + height;
}
else if(1==type)
{
point_min_x = origin_x - length/2.0;
point_min_y = origin_y - width/2.0;
point_min_z = origin_y - height/2.0;
point_max_x = origin_x + length/2.0;
point_max_y = origin_y + width/2.0;
point_max_z = origin_z + height/2.0;
}
float temp_x = 0.0;
float temp_y = 0.0;
float temp_z = 0.0;
auto start_time = std::chrono::high_resolution_clock::now();
for (int k=0;k<use_number_z;k++)
{
temp_z = point_min_z + use_interval_z*k;
if (k==use_number_z-1)
{
temp_z = point_max_z;
}
for (int j=0;j<use_number_y;j++)
{
temp_y = point_min_y + use_interval_y*j;
if (j==use_number_y-1)
{
temp_y = point_max_y;
}
for (int i=0;i<use_number_x;i++)
{
temp_x = point_min_x + use_interval_x*i;
if (i==use_number_x-1)
{
temp_x = point_max_x;
}
vec_points.push_back(temp_x);
vec_points.push_back(temp_y);
vec_points.push_back(temp_z);
}
}
}
for (int k=0;k<use_number_z-1;k++)
{
for (int j=0;j<use_number_y-1;j++)
{
for (int i=0;i<use_number_x-1;i++)
{
vec_elements.push_back(i+j*use_number_x+k*use_number_x*use_number_y);
vec_elements.push_back((i+1)+j*use_number_x+k*use_number_x*use_number_y);
vec_elements.push_back((i+1)+(j+1)*use_number_x+k*use_number_x*use_number_y);
vec_elements.push_back(i+(j+1)*use_number_x+k*use_number_x*use_number_y);
vec_elements.push_back(i+j*use_number_x+(k+1)*use_number_x*use_number_y);
vec_elements.push_back((i+1)+j*use_number_x+(k+1)*use_number_x*use_number_y);
vec_elements.push_back((i+1)+(j+1)*use_number_x+(k+1)*use_number_x*use_number_y);
vec_elements.push_back(i+(j+1)*use_number_x+(k+1)*use_number_x*use_number_y);
}
}
}
//char hexIDstr_cor[100];
ostringstream ostr2;
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
float point[3];
int count1 = 0;
for (auto iter = vec_points.begin(); iter != vec_points.end();)
{
//std::cout<<*iter++;
//std::cout<<*iter++;
//std::cout<<*iter++<<endl;
point[0]=*iter++;
point[1]=*iter++;
point[2]=*iter++;
points->InsertNextPoint(point);
//snprintf(hexIDstr_cor, 100, " %d %.8e %.8e %.8e", count1 + 1, point[0], point[1], point[2]);
ostr2.str("");
//ostr2.clear();
count1=count1+1;
}
//创建vtk拓扑数据结构
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
//创建vtkpolydata
vtkSmartPointer<vtkUnstructuredGrid> poly = vtkSmartPointer<vtkUnstructuredGrid>::New();
poly->SetPoints(points);
int quad[8];
char hexIDstr[100];
auto end_time1 = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end_time1 - start_time).count();
// cout << "程序运行时间1" << duration1 << " 微秒" << endl;
ostringstream ostr1;
int count = 0;
for (auto iter = vec_elements.begin(); iter != vec_elements.end();)
{
vtkSmartPointer<vtkHexahedron> hexpoly = vtkSmartPointer<vtkHexahedron>::New();
//std::cout<<*iter++;
//std::cout<<*iter++;
//std::cout<<*iter++<<endl;
quad[0]=*iter++;
quad[1]=*iter++;
quad[2]=*iter++;
quad[3]=*iter++;
quad[4]=*iter++;
quad[5]=*iter++;
quad[6]=*iter++;
quad[7]=*iter++;
hexpoly->GetPointIds()->SetId(0, quad[0]);
hexpoly->GetPointIds()->SetId(1, quad[1]);
hexpoly->GetPointIds()->SetId(2, quad[2]);
hexpoly->GetPointIds()->SetId(3, quad[3]);
hexpoly->GetPointIds()->SetId(4, quad[4]);
hexpoly->GetPointIds()->SetId(5, quad[5]);
hexpoly->GetPointIds()->SetId(6, quad[6]);
hexpoly->GetPointIds()->SetId(7, quad[7]);
poly->InsertNextCell(hexpoly->GetCellType(), hexpoly->GetPointIds());
//snprintf(hexIDstr, 100, "%d 1 %d %d %d %d %d %d %d %d" , count + 1, quad[0] + 1, quad[1] + 1, quad[2] + 1, quad[3] + 1,quad[4] + 1, quad[5] + 1, quad[6] + 1, quad[7] + 1);
ostr1<<count + 1<<" "<<1<<" ";
ostr1<<quad[0] + 1<<" ";
ostr1<<quad[1] + 1<<" ";
ostr1<<quad[2] + 1<<" ";
ostr1<<quad[3] + 1<<" ";
ostr1<<quad[4] + 1<<" ";
ostr1<<quad[5] + 1<<" ";
ostr1<<quad[6] + 1<<" ";
ostr1<<quad[7] + 1<<" ";
ostr1.str("");
//os << hexIDstr << endl;
count =count +1;
}
auto end_time2 = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end_time2 - start_time).count();
// cout << "程序运行时间2" << duration2 << " 微秒" << endl;
//构建渲染管线
vtkSmartPointer<vtkDataSetMapper>mapper = vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(poly);
vtkSmartPointer<vtkActor>actor = vtkSmartPointer<vtkActor>::New();
actor->GetProperty()->EdgeVisibilityOn();
actor->SetMapper(mapper);
m_visualWidgetService->removeAllActor();
m_cubiodActor = nullptr;
actor->GetProperty()->SetColor(0.85,0.85,0.85);
m_visualWidgetService->addActor(actor);
}
}*/