You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWATI/Src/nmNum/nmSubWxs/nmWxGridVTKContainerWidget.cpp

233 lines
10 KiB
C++

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 "nmWxGridVTKContainerWidget.h"
// qglobal.h
#include <QVBoxLayout>
#include <QFrame>
#include <QDebug>
#include <QtGlobal>
#pragma comment(lib,"User32.lib")
#pragma comment(lib,"gdi32.lib")
#pragma comment(lib,"OpenGL32.lib")
//#pragma comment(lib,"DbgHelp.lib")
//#pragma comment(lib,"Psapi.lib")
//#pragma comment(lib,"WS2_32.lib")
#pragma comment(lib,"AdvAPI32.lib")
#pragma comment(lib,"shell32.lib")
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
//VTK_MODULE_INIT(vtkRenderingFreeType)
#include <QVTKWidget.h>
#include <vtkImageData.h>
#include <vtkActor.h>
#include <vtkCubeSource.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNew.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <QVTKInteractor.h>
#include <vtkRenderer.h>
#include <vtkAlgorithmOutput.h>
#include <vtkSmartPointer.h>
#include <vtkGenericDataObjectReader.h>
#include <vtkNamedColors.h>
#include <QLabel>
//#include "nmWxGridVTKWidget.h"
nmWxGridVTKContainerWidget::nmWxGridVTKContainerWidget(QWidget *parent, QString vtkPath) : QWidget(parent), m_sVtkFilePath(vtkPath)
{
this->initLayout();
// this->initVTKWidgetFromFile();
this->initVTKGridAndSurfaceWidgetFromFile();
this->showVTK();
}
void nmWxGridVTKContainerWidget::initVTKWidget()
{
// 初始化QVTKWidget
QVTKWidget* w = new QVTKWidget;
// This creates a polygonal cylinder model with eight circumferential facets
// (i.e, in practice an octagonal prism).
vtkNew<vtkCylinderSource> cylinder;
cylinder->SetResolution(8);
// The mapper is responsible for pushing the geometry into the graphics
// library. It may also do color mapping, if scalars or other attributes are
// defined.
vtkSmartPointer<vtkDataSetMapper> cylinderMapper = vtkSmartPointer<vtkDataSetMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
// The actor is a grouping mechanism: besides the geometry (mapper), it
// also has a property, transformation matrix, and/or texture map.
// Here we set its color and rotate it around the X and Y axes.
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
// The renderer generates the image
// which is then displayed on the render window.
// It can be thought of as a scene to which the actor is added
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cylinderActor);
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// The render window is the actual GUI window
// that appears on the computer screen
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800, 600);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
// The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
vtkSmartPointer<QVTKInteractor> renderWindowInteractor = vtkSmartPointer<QVTKInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
renderWindowInteractor->Initialize();
w->SetRenderWindow(renderWindow.Get());
renderWindow->Render();
w->showMaximized();
// w.show();
// vLayout.addWidget(&w);
m_frameLaoyt->addWidget(w);
}
void nmWxGridVTKContainerWidget::initVTKWidgetFromFile()
{
// 初始化QVTKWidget
QVTKWidget* w = new QVTKWidget;
// This creates a polygonal cylinder model with eight circumferential facets
// (i.e, in practice an octagonal prism).
QString vtkFilePath = m_sVtkFilePath.length() == 0 ? "D:/geomodelFineGrid.vtk" : m_sVtkFilePath;
vtkSmartPointer<vtkGenericDataObjectReader> m_reader = vtkSmartPointer<vtkGenericDataObjectReader>::New();
m_reader->SetFileName(vtkFilePath.toStdString().c_str());
m_reader->Update();
// The mapper is responsible for pushing the geometry into the graphics
// library. It may also do color mapping, if scalars or other attributes are
// defined.
vtkSmartPointer<vtkDataSetMapper> cylinderMapper = vtkSmartPointer<vtkDataSetMapper>::New();
cylinderMapper->SetInputConnection(m_reader->GetOutputPort());
// The actor is a grouping mechanism: besides the geometry (mapper), it
// also has a property, transformation matrix, and/or texture map.
// Here we set its color and rotate it around the X and Y axes.
vtkNew<vtkNamedColors> m_colors;
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
// cylinderActor->RotateX(30.0);
// cylinderActor->RotateY(-45.0);
cylinderActor->GetProperty()->SetColor(m_colors->GetColor3d("Black").GetData());
cylinderActor->GetProperty()->SetOpacity(0.6);
// 线actor
vtkSmartPointer<vtkActor> m_wireframeActor = vtkSmartPointer<vtkActor>::New();
m_wireframeActor->SetMapper(cylinderMapper);
m_wireframeActor->GetProperty()->SetRepresentationToWireframe();
m_wireframeActor->GetProperty()->SetColor(m_colors->GetColor3d("White").GetData());
m_wireframeActor->GetProperty()->SetSpecular(1.0);
m_wireframeActor->GetProperty()->SetSpecularPower(50.0);
m_wireframeActor->GetProperty()->SetAmbient(0.2);
m_wireframeActor->GetProperty()->SetDiffuse(0.8);
// The renderer generates the image
// which is then displayed on the render window.
// It can be thought of as a scene to which the actor is added
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cylinderActor);
renderer->AddActor(m_wireframeActor);
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// RGB颜色1.0代表白色
renderer->SetBackground(1.0, 1.0, 1.0);
// The render window is the actual GUI window
// that appears on the computer screen
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800, 600);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
// The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
vtkSmartPointer<QVTKInteractor> renderWindowInteractor = vtkSmartPointer<QVTKInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
renderWindowInteractor->Initialize();
w->SetRenderWindow(renderWindow.Get());
renderWindow->Render();
w->showMaximized();
// w.show();
// vLayout.addWidget(&w);
m_frameLaoyt->addWidget(w);
}
void nmWxGridVTKContainerWidget::initVTKGridAndSurfaceWidgetFromFile()
{
// 初始化QVTKWidget
QVTKWidget* w = new QVTKWidget;
QString vtkFilePath = m_sVtkFilePath.length() == 0 ? "D:/geomodelFineGrid.vtk" : m_sVtkFilePath;
vtkSmartPointer<vtkGenericDataObjectReader> m_reader = vtkSmartPointer<vtkGenericDataObjectReader>::New();
m_reader->SetFileName(vtkFilePath.toStdString().c_str());
m_reader->Update();
// 初始化mapper
vtkSmartPointer<vtkDataSetMapper> cylinderMapper = vtkSmartPointer<vtkDataSetMapper>::New();
cylinderMapper->SetInputConnection(m_reader->GetOutputPort());
// 初始化actor
// vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();
// vtkSmartPointer<vtkActor> cubeActor = vtkSmartPointer<vtkActor>::New();
// cubeActor->SetMapper(cylinderMapper);
// 创建一个actor
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(cylinderMapper);
// 设置actor的属性使其同时显示线和面
actor->GetProperty()->SetRepresentationToSurface();
actor->GetProperty()->EdgeVisibilityOn(); // 显示边缘
actor->GetProperty()->SetEdgeVisibility(true); // 允许交互
actor->GetProperty()->SetColor(0, 120.0/255.0, 215.0/255.0); // 设置面的颜色
//actor->GetProperty()->SetColor(33.0/255.0, 48.0/255.0, 131.0/255.0); // 设置面的颜色
// // 初始化render
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// RGB颜色1.0代表白色
renderer->SetBackground(0.1, 0.2, 0.3);
// The render window is the actual GUI window
// that appears on the computer screen
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800, 600);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
// The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
//vtkSmartPointer<QVTKInteractor> renderWindowInteractor = vtkSmartPointer<QVTKInteractor>::New();
//renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
//renderWindowInteractor->Initialize();
w->SetRenderWindow(renderWindow.Get());
renderWindow->Render();
w->showMaximized();
m_frameLaoyt->addWidget(w);
}
void nmWxGridVTKContainerWidget::showVTK()
{
//QString vtkFilePath = "D:/geomodelFineGrid.vtk";
//nmWxGridVTKWidget* vtkWidget = new nmWxGridVTKWidget(vtkFilePath);
//m_frameLaoyt->addWidget(vtkWidget);
}
void nmWxGridVTKContainerWidget::initLayout()
{
m_mainLayout = new QVBoxLayout;
this->setLayout(m_mainLayout);
QFrame* frame = new QFrame;
m_frameLaoyt = new QVBoxLayout;
frame->setLayout(m_frameLaoyt);
m_mainLayout->addWidget(frame);
qDebug() << "in initLayout";
}