vtk 显示效果设置

在vtk中,显示样式的显示,是通过vtkProperty来控制的,这里介绍三种基本的属性设置方式,
点方式,网格方式和面方式,
这里以 头像三角网格的显示为例介绍其显示。其设置方法为

actor->GetProperty()->SetRepresentationToPoints()

actor->GetProperty()->SetRepresentationToWireframe()

actor->GetProperty()->SetRepresentationToSurface()


#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkSphereSource.h"
#include "vtkPoints.h"
#include "vtkPolyVertex.h"
#include "vtkUnstructuredGrid.h"
#include "vtkDataSetMapper.h"
#include "vtkCamera.h"
#include "vtkDecimatePro.h"
#include "vtkPolyDataReader.h"
#include "vtkSmoothPolyDataFilter.h"
#include "vtkProperty.h"
#include "vtkPolyDataMapper.h"
#include "vtkPolyDataNormals.h"
 
int main(int argc, char* argv[])
{
    vtkRenderer *ren=vtkRenderer::New();
 
    vtkPolyDataReader *reader = vtkPolyDataReader::New() ;
    reader->SetFileName("venus.vtk");       /*读取网格数据文件*/
    vtkDecimatePro *deci = vtkDecimatePro::New();    //进行精简
    deci->SetInputConnection(reader->GetOutputPort());
    deci->SetTargetReduction(0.9);
    deci->PreserveTopologyOn();
   
//设置优化
    vtkSmoothPolyDataFilter *smoother= vtkSmoothPolyDataFilter::New();
    smoother->SetInputConnection(deci->GetOutputPort());
    smoother->SetNumberOfIterations(50);
    vtkPolyDataNormals *normals= vtkPolyDataNormals::New();
    normals->SetInputConnection(smoother->GetOutputPort());
    normals->FlipNormalsOn();
   
    vtkPolyDataMapper *map1 = vtkPolyDataMapper::New();
    map1->SetInput(normals->GetOutput());
 
    vtkActor *actor1 = vtkActor::New();
    actor1->SetMapper(map1);
    actor1->GetProperty()->SetColor(1.0000, 0, 0);  //设定曲面背面颜色
    map1->ScalarVisibilityOff();

    actor1->GetProperty()->SetRepresentationToWireframe();/*设定网格显示方式*/
 
    vtkProperty *back_prop=vtkProperty::New();     //新建属性,设定曲面前面颜色
    back_prop->SetDiffuseColor(1,1,0);
    actor1->SetBackfaceProperty(back_prop);
 
    ren->AddActor(actor1);
 
    ren->SetBackground(0.6, 0.8, 0.8);          //设定背景色
 
 
    vtkRenderWindow* win=vtkRenderWindow::New();
    win->AddRenderer(ren);

    win->SetSize(400,400);

 
    //建立交互窗口,设定交互方式
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(win);
    vtkInteractorStyleTrackballCamera *style =
        vtkInteractorStyleTrackballCamera::New();
    iren->SetInteractorStyle(style);   
 
    iren->Start();
    ren->Delete();
    win->Delete();
    iren->Delete();
 
    return 0;
}

网格数据,显示效果如下:
vtk 显示效果设置_第1张图片

如果要以点集或者面显示方式显示,只需要用下面其中一段代码替换第47行即可

actor1->GetProperty()->SetRepresentationToPoints();/设定点显示方式/

actor1->GetProperty()->SetRepresentationToSurface();/设定面显示方式/

面方式显示效果如下:
vtk 显示效果设置_第2张图片
vtk的属性设置,首先要明确设置谁的属性,找到该对象所在的vtkActor对象actor,然后通过actor设置属性,

设置透明度为0.4

actor->GetProperty()->SetOpacity(0.4);

设置对象颜色

actor->GetProperty()->SetColor(double, double, double);

对象边界线宽设置

actor->GetProperty()->SetLineWidth(float);

你可能感兴趣的:(VTK)