#include "nmWxGridVTKContainerWidget.h" // qglobal.h #include #include #include #include #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 VTK_MODULE_INIT(vtkRenderingOpenGL) VTK_MODULE_INIT(vtkInteractionStyle) //VTK_MODULE_INIT(vtkRenderingFreeType) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#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 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 cylinderMapper = vtkSmartPointer::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 cylinderActor = vtkSmartPointer::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 renderer = vtkSmartPointer::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 renderWindow = vtkSmartPointer::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 renderWindowInteractor = vtkSmartPointer::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 m_reader = vtkSmartPointer::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 cylinderMapper = vtkSmartPointer::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 m_colors; vtkSmartPointer cylinderActor = vtkSmartPointer::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 m_wireframeActor = vtkSmartPointer::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 renderer = vtkSmartPointer::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 renderWindow = vtkSmartPointer::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 renderWindowInteractor = vtkSmartPointer::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 m_reader = vtkSmartPointer::New(); m_reader->SetFileName(vtkFilePath.toStdString().c_str()); m_reader->Update(); // 初始化mapper vtkSmartPointer cylinderMapper = vtkSmartPointer::New(); cylinderMapper->SetInputConnection(m_reader->GetOutputPort()); // 初始化actor // vtkSmartPointer cubeSource = vtkSmartPointer::New(); // vtkSmartPointer cubeActor = vtkSmartPointer::New(); // cubeActor->SetMapper(cylinderMapper); // 创建一个actor vtkSmartPointer actor = vtkSmartPointer::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 renderer = vtkSmartPointer::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 renderWindow = vtkSmartPointer::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 renderWindowInteractor = vtkSmartPointer::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"; }