VTK资料分享:张晓东的《VTK图形图像开发进阶》全套资料(包含书籍电子版,配套每章节所有源码、实现效果图和工程配置文件):
https://download.csdn.net/download/qq_32809093/12550945
水灵VTK学习全套资料(包含全部视频、PPT、源码和每个项目对应的工程配置文件CmakeLists.txt):
https://download.csdn.net/download/qq_32809093/12550933
水灵VT视频在线观看:https://www.ixigua.com/6834676787503432203
本文参考的主要是张晓东的《VTK图形图像开发进阶》和水灵的视频Study VTK Together。使用的平台是VS2019+VTK8.2
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
vtkSmartPointer cubeSource =
vtkSmartPointer::New();
cubeSource->Update();
vtkSmartPointer triFilter =
vtkSmartPointer::New();
triFilter->SetInputData(cubeSource->GetOutput());
triFilter->Update();
vtkSmartPointer massProp =
vtkSmartPointer::New();
massProp->SetInputData(triFilter->GetOutput());
float vol = massProp->GetVolume();
float area= massProp->GetSurfaceArea();
float maxArea = massProp->GetMaxCellArea();
float minArea = massProp->GetMinCellArea();
std::cout<<"Volume :"< mapper =
vtkSmartPointer::New();
//mapper->SetInput(cubeSource->GetOutput());
mapper->SetInputData(triFilter->GetOutput());
vtkSmartPointer actor =
vtkSmartPointer::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(0,1,0);
actor->GetProperty()->SetEdgeColor(1,0,0);
actor->GetProperty()->SetEdgeVisibility(1);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(actor);
renderer->SetBackground(1.0,1.0,1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize( 640, 480 );
renderWindow->Render();
renderWindow->SetWindowName("PolyDataMassProperty");
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int , char *[])
{
vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->Update();
vtkSmartPointer dijkstra =
vtkSmartPointer::New();
dijkstra->SetInputData(sphereSource->GetOutput());
dijkstra->SetStartVertex(0);
dijkstra->SetEndVertex(10);
dijkstra->Update();
vtkSmartPointer pathMapper =
vtkSmartPointer::New();
pathMapper->SetInputData(dijkstra->GetOutput());
vtkSmartPointer pathActor =
vtkSmartPointer::New();
pathActor->SetMapper(pathMapper);
pathActor->GetProperty()->SetColor(1,0,0);
pathActor->GetProperty()->SetLineWidth(4);
vtkSmartPointer mapper =
vtkSmartPointer::New();
mapper->SetInputData(sphereSource->GetOutput());
vtkSmartPointer actor =
vtkSmartPointer::New();
actor->SetMapper(mapper);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(actor);
renderer->AddActor(pathActor);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize( 640, 480 );
renderWindow->Render();
renderWindow->SetWindowName("PolyDataGeodesic");
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include //计算法向量
#include
#include
#include
#include
#include
//
#include
#include
#include
#include
#include
int main()
{
vtkSmartPointer plyReader =
vtkSmartPointer::New();
plyReader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
plyReader->Update();
vtkSmartPointer normFilter =
vtkSmartPointer::New();
normFilter->SetInputData(plyReader->GetOutput());
normFilter->SetComputePointNormals(1);//开启点法向量计算
normFilter->SetComputeCellNormals(0); //关闭单元法向量计算
normFilter->SetAutoOrientNormals(1);
normFilter->SetSplitting(0);
normFilter->Update();
vtkSmartPointer mask =
vtkSmartPointer::New();
mask->SetInputData(normFilter->GetOutput());
mask->SetMaximumNumberOfPoints(300);
mask->RandomModeOn();
mask->Update();
vtkSmartPointer arrow =
vtkSmartPointer::New();
arrow->Update(); //一定要更新 否则数据没有添加进来,程序会报错
vtkSmartPointer glyph =
vtkSmartPointer::New();
glyph->SetInputData(mask->GetOutput());
glyph->SetSourceData(arrow->GetOutput());//每一点用箭头代替
glyph->SetVectorModeToUseNormal();//设置向量显示模式和法向量一致
glyph->SetScaleFactor(0.01); //设置伸缩比例
glyph->Update();
vtkSmartPointer mapper =
vtkSmartPointer::New();
mapper->SetInputData(plyReader->GetOutput());
vtkSmartPointer normMapper =
vtkSmartPointer::New();
normMapper->SetInputData(normFilter->GetOutput());
vtkSmartPointer glyphMapper =
vtkSmartPointer::New();
glyphMapper->SetInputData(glyph->GetOutput());
vtkSmartPointer actor =
vtkSmartPointer::New();
actor->SetMapper(mapper);
vtkSmartPointer normActor =
vtkSmartPointer::New();
normActor->SetMapper(normMapper);
vtkSmartPointer glyphActor =
vtkSmartPointer::New();
glyphActor->SetMapper(glyphMapper);
glyphActor->GetProperty()->SetColor(1, 0, 0);
double origView[4] = { 0, 0, 0.33, 1 };
double normView[4] = { 0.33, 0, 0.66, 1 };
double glyphView[4] = { 0.66, 0, 1, 1 };
vtkSmartPointer origRender =
vtkSmartPointer::New();
origRender->SetViewport(origView);
origRender->AddActor(actor);
origRender->SetBackground(1, 0, 0);
vtkSmartPointer normRender =
vtkSmartPointer::New();
normRender->SetViewport(normView);
normRender->AddActor(normActor);
normRender->SetBackground(0, 1, 0);
vtkSmartPointer glyphRender =
vtkSmartPointer::New();
glyphRender->SetViewport(glyphView);
glyphRender->AddActor(glyphActor);
glyphRender->AddActor(normActor);
glyphRender->SetBackground(0, 0, 1);
vtkSmartPointer rw =
vtkSmartPointer::New();
rw->AddRenderer(origRender);
rw->AddRenderer(normRender);
rw->AddRenderer(glyphRender);
rw->SetWindowName("Calculating Point Norm & Cell Norm");
rw->SetSize(960, 320);
rw->Render();
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
rwi->Initialize();
rwi->Start();
return 0;
}
#include "vtkAutoInit.h"
//VTK_MODULE_INIT(vtkRenderingOpenGL2);
//VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
reader->Update();
vtkSmartPointer curvaturesFilter =
vtkSmartPointer::New();
curvaturesFilter->SetInputConnection(reader->GetOutputPort());
//curvaturesFilter->SetCurvatureTypeToMinimum(); //最小曲率
curvaturesFilter->SetCurvatureTypeToMaximum(); //最大曲率
//curvaturesFilter->SetCurvatureTypeToGaussian();//高斯曲率
//curvaturesFilter->SetCurvatureTypeToMean(); //平均曲率
curvaturesFilter->Update();
double scalarRange[2];
curvaturesFilter->GetOutput()->GetScalarRange(scalarRange);
//建立查找表 做颜色映射
vtkSmartPointer lut =
vtkSmartPointer::New();
lut->SetHueRange(0.0, 0.6);
lut->SetAlphaRange(1.0, 1.0);
lut->SetValueRange(1.0, 1.0);
lut->SetSaturationRange(1.0, 1.0);
lut->SetNumberOfTableValues(256);
lut->SetRange(scalarRange);
lut->Build();
///
vtkSmartPointer mapper =
vtkSmartPointer::New();
mapper->SetInputData(curvaturesFilter->GetOutput());
mapper->SetLookupTable(lut);
mapper->SetScalarRange(scalarRange);
vtkSmartPointer actor =
vtkSmartPointer::New();
actor->SetMapper(mapper);
vtkSmartPointer scalarBar =
vtkSmartPointer::New();
scalarBar->SetLookupTable(mapper->GetLookupTable());
scalarBar->SetTitle(curvaturesFilter->GetOutput()->GetPointData()->GetScalars()->GetName());
scalarBar->SetNumberOfLabels(5); //设置5个标签
vtkSmartPointer render =
vtkSmartPointer::New();
render->AddActor(actor);
render->AddActor2D(scalarBar);
render->SetBackground(0, 0, 0);
vtkSmartPointer rw =
vtkSmartPointer::New();
rw->AddRenderer(render);
rw->SetSize(640, 480);
rw->SetWindowName("Calculating PolyData Curvature");
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
rwi->Initialize();
rwi->Start();
return 0;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//测试文件:../data/fran_cut.vtk
int main(int argc, char *argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
reader->Update();
vtkSmartPointer smoothFilter =
vtkSmartPointer::New();
smoothFilter->SetInputConnection(reader->GetOutputPort());
smoothFilter->SetNumberOfIterations(200);
smoothFilter->Update();
vtkSmartPointer inputMapper =
vtkSmartPointer::New();
inputMapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer inputActor =
vtkSmartPointer::New();
inputActor->SetMapper(inputMapper);
vtkSmartPointer smoothedMapper =
vtkSmartPointer::New();
smoothedMapper->SetInputConnection(smoothFilter->GetOutputPort());
vtkSmartPointer smoothedActor =
vtkSmartPointer::New();
smoothedActor->SetMapper(smoothedMapper);
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer leftRenderer =
vtkSmartPointer::New();
leftRenderer->SetViewport(leftViewport);
leftRenderer->AddActor(inputActor);
leftRenderer->SetBackground(0.8, 0.8, 0.8);
leftRenderer->ResetCamera();
vtkSmartPointer rightRenderer =
vtkSmartPointer::New();
rightRenderer->SetViewport(rightViewport);
rightRenderer->AddActor(smoothedActor);
rightRenderer->SetBackground(0.8, 0.8, 0.8);
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
rightRenderer->ResetCamera();
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataLapLasianSmooth");
vtkSmartPointer interactor =
vtkSmartPointer::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void GenerateData(vtkSmartPointer input)
{
vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->Update();
vtkSmartPointer ids =
vtkSmartPointer::New();
ids->SetNumberOfComponents(1);
ids->InsertNextValue(2);
ids->InsertNextValue(10);
vtkSmartPointer selectionNode =
vtkSmartPointer::New();
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(), 1);
vtkSmartPointer selection =
vtkSmartPointer::New();
selection->AddNode(selectionNode);
vtkSmartPointer extractSelection =
vtkSmartPointer::New();
extractSelection->SetInputData(0, sphereSource->GetOutput());
extractSelection->SetInputData(1, selection);
extractSelection->Update();
vtkSmartPointer surfaceFilter =
vtkSmartPointer::New();
surfaceFilter->SetInputConnection(extractSelection->GetOutputPort());
surfaceFilter->Update();
input->ShallowCopy(surfaceFilter->GetOutput());
}
int main(int argc, char *argv[])
{
vtkSmartPointer input =
vtkSmartPointer::New();
GenerateData(input);
vtkSmartPointer featureEdges =
vtkSmartPointer::New();
featureEdges->SetInputData(input);
featureEdges->BoundaryEdgesOn();
featureEdges->FeatureEdgesOff();
featureEdges->ManifoldEdgesOff();
featureEdges->NonManifoldEdgesOff();
featureEdges->Update();
int numberOfOpenEdges = featureEdges->GetOutput()->GetNumberOfCells();
if(numberOfOpenEdges)
{
std::cout<<"该网格模型不是封闭的..."< fillHolesFilter =
vtkSmartPointer::New();
fillHolesFilter->SetInputData(input);
fillHolesFilter->Update();
vtkSmartPointer normals =
vtkSmartPointer::New();
normals->SetInputConnection(fillHolesFilter->GetOutputPort());
normals->ConsistencyOn();
normals->SplittingOff();
normals->Update();
//
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer originalMapper =
vtkSmartPointer::New();
originalMapper->SetInputData(input);
vtkSmartPointer backfaceProp =
vtkSmartPointer::New();
backfaceProp->SetDiffuseColor(0.89,0.81,0.34);
vtkSmartPointer originalActor =
vtkSmartPointer::New();
originalActor->SetMapper(originalMapper);
originalActor->SetBackfaceProperty(backfaceProp);
originalActor->GetProperty()->SetDiffuseColor(1.0, 0.3882, 0.2784);
vtkSmartPointer edgeMapper =
vtkSmartPointer::New();
edgeMapper->SetInputData(featureEdges->GetOutput());
vtkSmartPointer edgeActor =
vtkSmartPointer::New();
edgeActor->SetMapper(edgeMapper);
edgeActor->GetProperty()->SetEdgeColor(0.,0.,1.0);
edgeActor->GetProperty()->SetEdgeVisibility(1);
edgeActor->GetProperty()->SetLineWidth(5);
vtkSmartPointer filledMapper =
vtkSmartPointer::New();
filledMapper->SetInputData(normals->GetOutput());
vtkSmartPointer filledActor =
vtkSmartPointer::New();
filledActor->SetMapper(filledMapper);
filledActor->GetProperty()->SetDiffuseColor(1.0, 0.3882, 0.2784);
vtkSmartPointer leftRenderer =
vtkSmartPointer::New();
leftRenderer->SetViewport(leftViewport);
leftRenderer->AddActor(originalActor);
leftRenderer->AddActor(edgeActor);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer rightRenderer =
vtkSmartPointer::New();
rightRenderer->SetViewport(rightViewport);
rightRenderer->AddActor(filledActor);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataClosed");
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
leftRenderer->GetActiveCamera()->Azimuth(30);
leftRenderer->GetActiveCamera()->Elevation(30);
leftRenderer->ResetCamera();
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
左图为原始模型,右图为漏洞填补后的结果
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sphereSource =
vtkSmartPointer::New();
sphereSource->SetRadius(10);
sphereSource->SetThetaResolution(10);
sphereSource->SetPhiResolution(10);
sphereSource->Update();
vtkSmartPointer coneSource =
vtkSmartPointer::New();
coneSource->SetRadius(5);
coneSource->SetHeight(10);
coneSource->SetCenter(25,0,0);
coneSource->Update();
vtkSmartPointer appendFilter =
vtkSmartPointer::New();
appendFilter->AddInputData(sphereSource->GetOutput());
appendFilter->AddInputData(coneSource->GetOutput());
appendFilter->Update();
vtkSmartPointer connectivityFilter =
vtkSmartPointer::New();
connectivityFilter->SetInputData(appendFilter->GetOutput());
connectivityFilter->SetExtractionModeToCellSeededRegions();
connectivityFilter->AddSeed(100);
connectivityFilter->Update();
vtkSmartPointer originalMapper =
vtkSmartPointer::New();
originalMapper->SetInputConnection(appendFilter->GetOutputPort());
originalMapper->Update();
vtkSmartPointer originalActor =
vtkSmartPointer::New();
originalActor->SetMapper(originalMapper);
vtkSmartPointer extractedMapper =
vtkSmartPointer::New();
extractedMapper->SetInputConnection(connectivityFilter->GetOutputPort());
extractedMapper->Update();
vtkSmartPointer extractedActor =
vtkSmartPointer::New();
extractedActor->SetMapper(extractedMapper);
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer leftRenderer =
vtkSmartPointer::New();
leftRenderer->SetViewport(leftViewport);
leftRenderer->AddActor(originalActor);
leftRenderer->SetBackground(0.8, 0.8, 0.8);
vtkSmartPointer rightRenderer =
vtkSmartPointer::New();
rightRenderer->SetViewport(rightViewport);
rightRenderer->AddActor(extractedActor);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataConnectedCompExtract");
leftRenderer->ResetCamera();
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
vtkSmartPointer interactor =
vtkSmartPointer::New();
interactor->SetRenderWindow(renderWindow);
interactor->Initialize();
interactor->Start();
return EXIT_SUCCESS;
}
SetExtractionModeToLargestRegion():用于提取具有最多点数的连通区域;
SetExtractionModeToAllRegions():该模式主要用于连通区域标记,配合函数ColorRegionsOn()使用,在连通区域像是的同时,生成一个名为RegionId的点属性数据。
SetExtractionModeToSpecifiedRegions():该模式用于提取一个或多个连通区域,在该模式下,需要通过AddSpecifiedRegion()来添加西药提取的区域号,区域号从零开始。
SetExtractionModeToClosestPointRegion():该模式需要使用SetClosestPoint()函数设置一个空间点坐标,执行结果为离该点最近的连通区域。
SetExtractionModeToPointSeededRegions():该模式下需要使用AddSeed()函数添加种子点,提取种子点所在的区域。
SetExtractionModeToCellSeededRegions():该模式下需要使用AddSeed()函数调价种子单元,提取种子单元所在的区域。
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//测试文件:../data/fran_cut.vtk
int main(int argc, char * argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
reader->Update();
vtkSmartPointer original = reader->GetOutput();
std::cout << "抽取前:" << std::endl << "------------" << std::endl;
std::cout << "模型点数为: " << original->GetNumberOfPoints() << std::endl;
std::cout << "模型面数为: " << original->GetNumberOfPolys() << std::endl;
vtkSmartPointer decimate =
vtkSmartPointer::New();
decimate->SetInputData(original);
decimate->SetTargetReduction(.80); //80%的三角面片被移除
decimate->Update();
vtkSmartPointer decimated = decimate->GetOutput();
std::cout << "抽取后" << std::endl << "------------" << std::endl;
std::cout << "模型点数为:" << decimated->GetNumberOfPoints()<< std::endl;
std::cout << "模型面数为:" << decimated->GetNumberOfPolys()<< std::endl;
vtkSmartPointer origianlMapper =
vtkSmartPointer::New();
origianlMapper->SetInputData(original);
vtkSmartPointer origianlActor =
vtkSmartPointer::New();
origianlActor->SetMapper(origianlMapper);
vtkSmartPointer decimatedMapper =
vtkSmartPointer::New();
decimatedMapper->SetInputData(decimated);
vtkSmartPointer decimatedActor =
vtkSmartPointer::New();
decimatedActor->SetMapper(decimatedMapper);
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer leftRenderer =
vtkSmartPointer::New();
leftRenderer->SetViewport(leftViewport);
leftRenderer->AddActor(origianlActor);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer rightRenderer =
vtkSmartPointer::New();
rightRenderer->SetViewport(rightViewport);
rightRenderer->AddActor(decimatedActor);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
leftRenderer->GetActiveCamera()->Azimuth(30);
leftRenderer->GetActiveCamera()->Elevation(30);
leftRenderer->ResetCamera();
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataDecimation");
vtkSmartPointer interactor =
vtkSmartPointer::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
//读数据
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
reader->Update();
vtkSmartPointer orig = reader->GetOutput();
std::cout << "original" << "-----------------------" << std::endl;
std::cout << "模型点数为: " << orig->GetNumberOfPoints() << std::endl;
std::cout << "模型面数为: " << orig->GetNumberOfPolys() << std::endl;
//线性网格细分滤波器
vtkSmartPointer linear =
vtkSmartPointer::New();
linear->SetInputData(orig);
linear->SetNumberOfSubdivisions(4);
linear->Update();
vtkSmartPointer linearInfo = linear->GetOutput();
std::cout << "linear" << "-----------------------" << std::endl;
std::cout << "模型点数为: " << linearInfo->GetNumberOfPoints() << std::endl;
std::cout << "模型面数为: " << linearInfo->GetNumberOfPolys() << std::endl;
//Loop网格细分滤波器
vtkSmartPointer loop =
vtkSmartPointer::New();
loop->SetInputData(orig);
loop->SetNumberOfSubdivisions(4);
loop->Update();
vtkSmartPointer loopInfo = loop->GetOutput();
std::cout << "loop" << "-----------------------" << std::endl;
std::cout << "模型点数为: " << loopInfo->GetNumberOfPoints() << std::endl;
std::cout << "模型面数为: " << loopInfo->GetNumberOfPolys() << std::endl;
//butterfly网格细分滤波器
vtkSmartPointer butterfly =
vtkSmartPointer::New();
butterfly->SetInputData(orig);
butterfly->SetNumberOfSubdivisions(4);
butterfly->Update();
vtkSmartPointer butterflyInfo = butterfly->GetOutput();
std::cout << "butterfly" << "-----------------------" << std::endl;
std::cout << "模型点数为: " << butterflyInfo->GetNumberOfPoints() << std::endl;
std::cout << "模型面数为: " << butterflyInfo->GetNumberOfPolys() << std::endl;
vtkSmartPointer origMapper =
vtkSmartPointer::New();
origMapper->SetInputData(orig);
vtkSmartPointer origActor =
vtkSmartPointer::New();
origActor->SetMapper(origMapper);
vtkSmartPointer linearMapper =
vtkSmartPointer::New();
linearMapper->SetInputData(linear->GetOutput());
vtkSmartPointer linearActor =
vtkSmartPointer::New();
linearActor->SetMapper(linearMapper);
vtkSmartPointer loopMapper =
vtkSmartPointer::New();
loopMapper->SetInputData(loop->GetOutput());
vtkSmartPointer loopActor =
vtkSmartPointer::New();
loopActor->SetMapper(loopMapper);
vtkSmartPointer butterflyMapper =
vtkSmartPointer::New();
butterflyMapper->SetInputData(butterfly->GetOutput());
vtkSmartPointer butterflyActor =
vtkSmartPointer::New();
butterflyActor->SetMapper(butterflyMapper);
//
double ltView[4] = { 0, 0, 0.5, 0.5 };
double rtView[4] = { 0.5, 0, 1, 0.5 };
double lbView[4] = { 0, 0.5, 0.5, 1 };
double rbView[4] = { 0.5, 0.5, 1, 1 };
vtkSmartPointer origRender =
vtkSmartPointer::New();
origRender->SetViewport(ltView);
origRender->AddActor(origActor);
origRender->SetBackground(1, 0, 0);
vtkSmartPointer linearRender =
vtkSmartPointer::New();
linearRender->SetViewport(rtView);
linearRender->AddActor(linearActor);
linearRender->SetBackground(0, 1, 0);
vtkSmartPointer loopRender =
vtkSmartPointer::New();
loopRender->SetViewport(lbView);
loopRender->AddActor(loopActor);
loopRender->SetBackground(0, 0, 1);
vtkSmartPointer butterflyRender =
vtkSmartPointer::New();
butterflyRender->SetViewport(rbView);
butterflyRender->AddActor(butterflyActor);
butterflyRender->SetBackground(0, 0, 0);
//
vtkSmartPointer rw =
vtkSmartPointer::New();
rw->AddRenderer(origRender);
rw->AddRenderer(linearRender);
rw->AddRenderer(loopRender);
rw->AddRenderer(butterflyRender);
rw->SetSize(640, 640);
rw->SetWindowName("PolyData Subdivision");
origRender->GetActiveCamera()->SetPosition(0, -1, 0);
origRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
origRender->GetActiveCamera()->SetViewUp(0, 0, 1);
origRender->GetActiveCamera()->Azimuth(30);
origRender->GetActiveCamera()->Elevation(30);
origRender->ResetCamera();//刷新照相机
linearRender->SetActiveCamera(origRender->GetActiveCamera());
loopRender->SetActiveCamera(origRender->GetActiveCamera());
butterflyRender->SetActiveCamera(origRender->GetActiveCamera());
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
rwi->Start();
rw->Render();
return 0;
}
红色视窗为原始三角网格模型,绿色视窗采用了线性细分算法;蓝色视窗采用Loop细分算法;黑色视窗采用了Butterfly细分算法。可见Loop细分、Butterfly细分能够得到较为光滑的效果;效果优于线性细分算法。
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
unsigned int gridSize = 10;
vtkSmartPointer points =
vtkSmartPointer::New();
for(unsigned int x = 0; x < gridSize; x++)
{
for(unsigned int y = 0; y < gridSize; y++)
{
points->InsertNextPoint(x, y, vtkMath::Random(0.0, 3.0));
}
}
vtkSmartPointer polydata =
vtkSmartPointer::New();
polydata->SetPoints(points);
vtkSmartPointer delaunay =
vtkSmartPointer::New();
delaunay->SetInputData(polydata);
delaunay->Update();
vtkSmartPointer glyphFilter =
vtkSmartPointer::New();
glyphFilter->SetInputData(polydata);
glyphFilter->Update();
vtkSmartPointer pointsMapper =
vtkSmartPointer::New();
pointsMapper->SetInputData(glyphFilter->GetOutput());
vtkSmartPointer pointsActor =
vtkSmartPointer::New();
pointsActor->SetMapper(pointsMapper);
pointsActor->GetProperty()->SetPointSize(3);
pointsActor->GetProperty()->SetColor(1,0,0);
vtkSmartPointer triangulatedMapper =
vtkSmartPointer::New();
triangulatedMapper->SetInputData(delaunay->GetOutput());
vtkSmartPointer triangulatedActor =
vtkSmartPointer::New();
triangulatedActor->SetMapper(triangulatedMapper);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(pointsActor);
renderer->AddActor(triangulatedActor);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataDelaunay2D");
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer points =
vtkSmartPointer::New();
unsigned int gridSize = 10;
for(unsigned int x = 0; x < gridSize; x++)
{
for(unsigned int y = 0; y < gridSize; y++)
{
points->InsertNextPoint(x, y, vtkMath::Random(0.0, 3.0));
}
}
vtkSmartPointer polydata =
vtkSmartPointer::New();
polydata->SetPoints(points);
vtkSmartPointer poly =
vtkSmartPointer::New();
/*poly->GetPointIds()->InsertNextId(22);
poly->GetPointIds()->InsertNextId(23);
poly->GetPointIds()->InsertNextId(24);
poly->GetPointIds()->InsertNextId(25);
poly->GetPointIds()->InsertNextId(35);
poly->GetPointIds()->InsertNextId(45);
poly->GetPointIds()->InsertNextId(44);
poly->GetPointIds()->InsertNextId(43);
poly->GetPointIds()->InsertNextId(42);
poly->GetPointIds()->InsertNextId(32);*/
poly->GetPointIds()->InsertNextId(32);
poly->GetPointIds()->InsertNextId(42);
poly->GetPointIds()->InsertNextId(43);
poly->GetPointIds()->InsertNextId(44);
poly->GetPointIds()->InsertNextId(45);
poly->GetPointIds()->InsertNextId(35);
poly->GetPointIds()->InsertNextId(25);
poly->GetPointIds()->InsertNextId(24);
poly->GetPointIds()->InsertNextId(23);
poly->GetPointIds()->InsertNextId(22);
vtkSmartPointer cell =
vtkSmartPointer::New();
cell->InsertNextCell(poly);
vtkSmartPointer boundary =
vtkSmartPointer::New();
boundary->SetPoints(points);
boundary->SetPolys(cell);
vtkSmartPointer delaunay =
vtkSmartPointer::New();
delaunay->SetInputData(polydata);
delaunay->SetSourceData(boundary);
delaunay->Update();
vtkSmartPointer glyphFilter =
vtkSmartPointer::New();
glyphFilter->SetInputData(polydata);
glyphFilter->Update();
vtkSmartPointer pointsMapper =
vtkSmartPointer::New();
pointsMapper->SetInputData(glyphFilter->GetOutput());
vtkSmartPointer pointsActor =
vtkSmartPointer::New();
pointsActor->SetMapper(pointsMapper);
pointsActor->GetProperty()->SetPointSize(3);
pointsActor->GetProperty()->SetColor(1,0,0);
vtkSmartPointer triangulatedMapper =
vtkSmartPointer::New();
triangulatedMapper->SetInputData(delaunay->GetOutput());
vtkSmartPointer triangulatedActor =
vtkSmartPointer::New();
triangulatedActor->SetMapper(triangulatedMapper);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(pointsActor);
renderer->AddActor(triangulatedActor);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataConstrainedDelaunay2D");
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//测试:../data/HeadMRVolume.mhd 200
int main(int argc, char *argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\HeadMRVolume.mhd");
reader->Update();
double isoValue = atof("200");//设置等值面值为200
vtkSmartPointer surface =
vtkSmartPointer::New();
surface->SetInputData(reader->GetOutput());
surface->ComputeNormalsOn();
surface->SetValue(0, isoValue);
//surface->GenerateValues(5, 150,200);
/*vtkSmartPointer surface =
vtkSmartPointer::New();
surface->SetInput(reader->GetOutput());
surface->ComputeNormalsOn();
surface->SetValue(0, isoValue);*/
vtkSmartPointer surfMapper =
vtkSmartPointer::New();
surfMapper->SetInputConnection(surface->GetOutputPort());
surfMapper->ScalarVisibilityOff();
vtkSmartPointer surfActor =
vtkSmartPointer::New();
surfActor->SetMapper(surfMapper);
surfActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(surfActor);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataMarchingCubes");
renderWindow->Render();
vtkSmartPointer interactor =
vtkSmartPointer::New();
interactor->SetRenderWindow(renderWindow);
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
reader->Update();
vtkSmartPointer points =
vtkSmartPointer::New();
points->SetPoints(reader->GetOutput()->GetPoints()); //获得网格模型中的几何数据:点集
vtkSmartPointer surf =
vtkSmartPointer::New();
surf->SetInputData(points);
surf->SetNeighborhoodSize(20);
surf->SetSampleSpacing(0.005);
surf->Update();
vtkSmartPointer contour =
vtkSmartPointer::New();
contour->SetInputConnection(surf->GetOutputPort());
contour->SetValue(0, 0.0);
contour->Update();
//
vtkSmartPointer vertexGlyphFilter =
vtkSmartPointer::New();
vertexGlyphFilter->AddInputData(points);
vertexGlyphFilter->Update();
vtkSmartPointer pointMapper =
vtkSmartPointer::New();
pointMapper->SetInputData(vertexGlyphFilter->GetOutput());
pointMapper->ScalarVisibilityOff();
vtkSmartPointer pointActor =
vtkSmartPointer::New();
pointActor->SetMapper(pointMapper);
pointActor->GetProperty()->SetColor(1, 0, 0);
pointActor->GetProperty()->SetPointSize(4);
vtkSmartPointer contourMapper =
vtkSmartPointer::New();
contourMapper->SetInputData(contour->GetOutput());
vtkSmartPointer contourActor =
vtkSmartPointer::New();
contourActor->SetMapper(contourMapper);
///
double pointView[4] = { 0, 0, 0.5, 1 };
double contourView[4] = { 0.5, 0, 1, 1 };
vtkSmartPointer pointRender =
vtkSmartPointer::New();
pointRender->AddActor(pointActor);
pointRender->SetViewport(pointView);
pointRender->SetBackground(1, 1, 1);
vtkSmartPointer contourRender =
vtkSmartPointer::New();
contourRender->AddActor(contourActor);
contourRender->SetViewport(contourView);
contourRender->SetBackground(0, 1, 0);
pointRender->GetActiveCamera()->SetPosition(0, -1, 0);
pointRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
pointRender->GetActiveCamera()->SetViewUp(0, 0, 1);
pointRender->GetActiveCamera()->Azimuth(30);
pointRender->GetActiveCamera()->Elevation(30);
pointRender->ResetCamera();
contourRender->SetActiveCamera(pointRender->GetActiveCamera());
vtkSmartPointer rw =
vtkSmartPointer::New();
rw->AddRenderer(pointRender);
rw->AddRenderer(contourRender);
rw->SetSize(640, 320);
rw->SetWindowName("3D Surface Reconstruction ");
rw->Render();
vtkSmartPointer rwi =
vtkSmartPointer::New();
rwi->SetRenderWindow(rw);
rwi->Initialize();
rwi->Start();
return 0;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int, char *[])
{
vtkSmartPointer sourcePoints =
vtkSmartPointer::New();
double sourcePoint1[3] = {0.5, 0.0, 0.0};
sourcePoints->InsertNextPoint(sourcePoint1);
double sourcePoint2[3] = {0.0, 0.5, 0.0};
sourcePoints->InsertNextPoint(sourcePoint2);
double sourcePoint3[3] = {0.0, 0.0, 0.5};
sourcePoints->InsertNextPoint(sourcePoint3);
vtkSmartPointer targetPoints =
vtkSmartPointer::New();
double targetPoint1[3] = {0.0, 0.0, 0.55};
targetPoints->InsertNextPoint(targetPoint1);
double targetPoint2[3] = {0.0, 0.55, 0.0};
targetPoints->InsertNextPoint(targetPoint2);
double targetPoint3[3] = {-0.55, 0.0, 0.0};
targetPoints->InsertNextPoint(targetPoint3);
vtkSmartPointer landmarkTransform =
vtkSmartPointer::New();
landmarkTransform->SetSourceLandmarks(sourcePoints);
landmarkTransform->SetTargetLandmarks(targetPoints);
landmarkTransform->SetModeToRigidBody();
landmarkTransform->Update();
vtkSmartPointer source =
vtkSmartPointer::New();
source->SetPoints(sourcePoints);
vtkSmartPointer target =
vtkSmartPointer::New();
target->SetPoints(targetPoints);
vtkSmartPointer sourceGlyphFilter =
vtkSmartPointer::New();
sourceGlyphFilter->SetInputData(source);
sourceGlyphFilter->Update();
vtkSmartPointer targetGlyphFilter =
vtkSmartPointer::New();
targetGlyphFilter->SetInputData(target);
targetGlyphFilter->Update();
vtkSmartPointer transformFilter =
vtkSmartPointer::New();
transformFilter->SetInputData(sourceGlyphFilter->GetOutput());
transformFilter->SetTransform(landmarkTransform);
transformFilter->Update();
vtkSmartPointer sourceMapper =
vtkSmartPointer::New();
sourceMapper->SetInputConnection(sourceGlyphFilter->GetOutputPort());
vtkSmartPointer sourceActor =
vtkSmartPointer::New();
sourceActor->SetMapper(sourceMapper);
sourceActor->GetProperty()->SetColor(1,1,0);
sourceActor->GetProperty()->SetPointSize(5);
vtkSmartPointer targetMapper =
vtkSmartPointer::New();
targetMapper->SetInputConnection(targetGlyphFilter->GetOutputPort());
vtkSmartPointer targetActor =
vtkSmartPointer::New();
targetActor->SetMapper(targetMapper);
targetActor->GetProperty()->SetColor(1,0,0);
targetActor->GetProperty()->SetPointSize(5);
vtkSmartPointer solutionMapper =
vtkSmartPointer::New();
solutionMapper->SetInputConnection(transformFilter->GetOutputPort());
vtkSmartPointer solutionActor =
vtkSmartPointer::New();
solutionActor->SetMapper(solutionMapper);
solutionActor->GetProperty()->SetColor(0,0,1);
solutionActor->GetProperty()->SetPointSize(5);
vtkSmartPointer renderer =
vtkSmartPointer::New();
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(sourceActor);
renderer->AddActor(targetActor);
renderer->AddActor(solutionActor);
vtkSmartPointer axes =
vtkSmartPointer::New();
axes->SetScale(30);
renderer->AddActor(axes);
renderer->SetBackground(.3, .6, .3);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataLandmarkReg");
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//测试文件:../data/fran_cut.vtk
int main(int argc, char * argv[])
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\fran_cut.vtk");
reader->Update();
vtkSmartPointer original = reader->GetOutput();
vtkSmartPointer translation =
vtkSmartPointer::New();
translation->Translate(0.2, 0.0, 0.0);
translation->RotateX(30);
vtkSmartPointer transformFilter1 =
vtkSmartPointer::New();
transformFilter1->SetInputData(reader->GetOutput());
transformFilter1->SetTransform(translation);
transformFilter1->Update();
vtkSmartPointer source =
vtkSmartPointer::New();
source->SetPoints(original->GetPoints());
vtkSmartPointer target =
vtkSmartPointer::New();
target->SetPoints(transformFilter1->GetOutput()->GetPoints());
vtkSmartPointer sourceGlyphFilter =
vtkSmartPointer::New();
sourceGlyphFilter->SetInputData(source);
sourceGlyphFilter->Update();
vtkSmartPointer targetGlyphFilter =
vtkSmartPointer::New();
targetGlyphFilter->SetInputData(target);
targetGlyphFilter->Update();
vtkSmartPointer icpTransform =
vtkSmartPointer::New();
icpTransform->SetSource(sourceGlyphFilter->GetOutput());
icpTransform->SetTarget(targetGlyphFilter->GetOutput());
icpTransform->GetLandmarkTransform()->SetModeToRigidBody();
icpTransform->SetMaximumNumberOfIterations(20);
icpTransform->StartByMatchingCentroidsOn();
icpTransform->Modified();
icpTransform->Update();
vtkSmartPointer transformFilter2 =
vtkSmartPointer::New();
transformFilter2->SetInputData(sourceGlyphFilter->GetOutput());
transformFilter2->SetTransform(icpTransform);
transformFilter2->Update();
vtkSmartPointer sourceMapper =
vtkSmartPointer::New();
sourceMapper->SetInputConnection(sourceGlyphFilter->GetOutputPort());
vtkSmartPointer sourceActor =
vtkSmartPointer::New();
sourceActor->SetMapper(sourceMapper);
sourceActor->GetProperty()->SetColor(0,1,0);
sourceActor->GetProperty()->SetPointSize(3);
vtkSmartPointer targetMapper =
vtkSmartPointer::New();
targetMapper->SetInputConnection(targetGlyphFilter->GetOutputPort());
vtkSmartPointer targetActor =
vtkSmartPointer::New();
targetActor->SetMapper(targetMapper);
targetActor->GetProperty()->SetColor(1,0,0);
targetActor->GetProperty()->SetPointSize(3);
vtkSmartPointer solutionMapper =
vtkSmartPointer::New();
solutionMapper->SetInputConnection(transformFilter2->GetOutputPort());
vtkSmartPointer solutionActor =
vtkSmartPointer::New();
solutionActor->SetMapper(solutionMapper);
solutionActor->GetProperty()->SetColor(0,0,1);
solutionActor->GetProperty()->SetPointSize(3);
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer(renderer);
renderer->AddActor(sourceActor);
renderer->AddActor(targetActor);
renderer->AddActor(solutionActor);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataICP");
renderWindow->Render();
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
#include
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//测试:../data/masonry.bmp ../data/cow.vtp
int main (int argc, char *argv[])
{
double translate[3];
translate[0] = 10.0;
translate[1] = 0.0;
translate[2] = 0.0;
std::cout << translate[0] << ", "
<< translate[1] << ", "
<< translate[2] << "\n";
vtkSmartPointer texReader =
vtkSmartPointer::New();
texReader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\masonry.bmp");//读取纹理图像
vtkSmartPointer texture =
vtkSmartPointer::New();
texture->SetInputConnection(texReader->GetOutputPort());
vtkSmartPointer modelReader =
vtkSmartPointer::New();
modelReader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第六章_图像处理\\data\\cow.vtp");//读取模型
vtkSmartPointer texturemap =
vtkSmartPointer::New();
texturemap->SetInputConnection(modelReader->GetOutputPort());
vtkSmartPointer mapper =
vtkSmartPointer::New();
mapper->SetInputConnection(texturemap->GetOutputPort());
vtkSmartPointer actor =
vtkSmartPointer::New();
actor->SetMapper( mapper );
actor->SetTexture( texture );
vtkSmartPointer renderer =
vtkSmartPointer::New();
renderer->AddActor(actor);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->AddRenderer( renderer );
vtkSmartPointer renWinInteractor =
vtkSmartPointer::New();
renWinInteractor->SetRenderWindow( renderWindow );
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("TextureMap");
renderWindow->Render();
renderWindow->Render();
renWinInteractor->Start();
return EXIT_SUCCESS;
}