VTK读取.stl格式三维显示并对三维图像根据用户需求对部分图像进行隐藏

  • Post author:
  • Post category:其他




VTK读取.stl格式三维显示并对三维图像根据用户需求对部分图像进行隐藏



代码如下:

#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)

#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include "vtkPlane.h"
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmoothPolyDataFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkImplicitPlaneWidget2.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include "vtkImplicitPlaneRepresentation.h"
#include "PlaneMoveCallback.h"

int main ( int argc, char *argv[] )
{
    //std::string inputFilename = "Greedy_Delaunay_knee_piece.obj";
	//std::string inputFilename = "Greedy_Delaunay_knee_piece.ply";
	std::string inputFilename = "mask.stl";

    /* ================= Reader ================= */

    vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
	//vtkSmartPointer<vtkOBJReader>reader = vtkSmartPointer<vtkOBJReader>::New();
	//vtkSmartPointer<vtkPLYReader>reader = vtkSmartPointer<vtkPLYReader>::New();

    reader->SetFileName(inputFilename.c_str());
    reader->Update();

    /* ================= Filter ================= */

    // Triangulate the grid points
//    vtkSmartPointer<vtkSmoothPolyDataFilter> smoothFilter =
//            vtkSmartPointer<vtkSmoothPolyDataFilter>::New();
//    smoothFilter->SetInputConnection(reader->GetOutputPort());
//    smoothFilter->SetNumberOfIterations(2);
//    smoothFilter->SetRelaxationFactor(0.5);
//    smoothFilter->FeatureEdgeSmoothingOff();
//    smoothFilter->BoundarySmoothingOn();
//    smoothFilter->Update();
//
//    // Update normals on newly smoothed polydata
//    vtkSmartPointer<vtkPolyDataNormals> normalGenerator = vtkSmartPointer<vtkPolyDataNormals>::New();
//    normalGenerator->SetInputConnection(smoothFilter->GetOutputPort());
//    normalGenerator->ComputePointNormalsOn();
//    normalGenerator->ComputeCellNormalsOn();
//    normalGenerator->Update();

    /* Figure for cutting */

    vtkSmartPointer<vtkPlane> plane =
            vtkSmartPointer<vtkPlane>::New();
    plane->SetOrigin(1.0, 1.5, 2.0);
    plane->SetNormal(0.4, 0.2, 1.0);

    /* ================= Mapper ================= */

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());
    mapper->AddClippingPlane(plane);

    /* ================= Actor ================= */

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    /* ================= Renderer ================= */

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground(.0, .0, .0);

    /* ================= Window ================= */

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    /* ================= Interactor ================= */

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    /* The callback will do the work */
    vtkSmartPointer<PlaneMoveCallback> myCallback =
            vtkSmartPointer<PlaneMoveCallback>::New();
    myCallback->Plane = plane;
    myCallback->Actor = actor;

    vtkSmartPointer<vtkImplicitPlaneRepresentation> rep =
            vtkSmartPointer<vtkImplicitPlaneRepresentation>::New();
    rep->SetPlaceFactor(1.25); // This must be set prior to placing the widget
    rep->PlaceWidget(actor->GetBounds());
    rep->SetNormal(plane->GetNormal());
    rep->SetOrigin(plane->GetOrigin());

    vtkSmartPointer<vtkImplicitPlaneWidget2> planeWidget =
            vtkSmartPointer<vtkImplicitPlaneWidget2>::New();
    planeWidget->SetInteractor(renderWindowInteractor);
    planeWidget->SetRepresentation(rep);
    planeWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);
    planeWidget->On();

    /* ================= Run pipeline ================= */

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}



当然,自己也可以到GitHub下载,并通过CMake进行编译!其下载链接见参考博文链接。



参考博文:



1、

computer_graphics_course




版权声明:本文为weixin_42291376原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。