2.演示各种不同的鼠标交互模式

来源:互联网 发布:c语言教程视频百度云 编辑:程序博客网 时间:2024/06/06 03:14

  1. /* 
  2. 演示各种不同的鼠标交互模式 
  3. */  
  4. #include "vtkRenderer.h"  
  5. #include "vtkRenderWindow.h"  
  6. #include "vtkRenderWindowInteractor.h"  
  7. #include "vtkVolume16Reader.h"  
  8. #include "vtkPolyDataMapper.h"  
  9. #include "vtkActor.h"  
  10. #include "vtkOutlineFilter.h"  
  11. #include "vtkCamera.h"  
  12. #include "vtkProperty.h"  
  13. #include "vtkPolyDataNormals.h"  
  14. #include "vtkContourFilter.h"  
  15. #include "vtkMarchingCubes.h"  
  16. #include "vtkDICOMImageReader.h"  
  17. #include "vtkImageCast.h"  
  18. #include "vtkDecimatePro.h"  
  19. #include "vtkStripper.h"  
  20. #include "vtkImageShrink3D.h"  
  21. #include "vtkSmoothPolyDataFilter.h"   
  22. #include "vtkTriangleFilter.h"  
  23. #include "vtkFeatureEdges.h"  
  24. #include "vtkPolyDataWriter.h"  
  25. #include "vtkImageData.h"  
  26. #include "vtkInteractorStyleTrackballCamera.h"  
  27. #include "vtkInteractorStyleUnicam.h"  
  28. #include "vtkInteractorStyleUser.h"  
  29. #include "vtkInteractorStyleTrackballActor.h"  
  30. #include "vtkInteractorStyleTerrain.h"  
  31. #include "vtkInteractorStyleSwitch.h"  
  32. #include "vtkInteractorStyleFlight.h"  
  33. int main ()  
  34. {  
  35. vtkRenderer *aRenderer = vtkRenderer::New();  
  36. vtkRenderWindow *renWin = vtkRenderWindow::New();  
  37. renWin->AddRenderer(aRenderer);  
  38. ///////////////////////////////////////////////////////////////////////////  
  39. vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();  
  40.     iren->SetRenderWindow(renWin);  
  41. // 总共10种交互方式 下面是八种  
  42. //   vtkInteractorStyleTrackballCamera *style = //常用的方式 移动摄像机  
  43. //     vtkInteractorStyleTrackballCamera::New();  
  44. //   iren->SetInteractorStyle(style);  
  45. // vtkInteractorStyleTrackballActor *style = //移动对象  
  46. //    vtkInteractorStyleTrackballActor::New();  
  47. // iren->SetInteractorStyle(style);  
  48. // For a 3-button mouse, the left button is for rotation, the right button for zooming,   
  49. //the middle button for panning, and ctrl + left button for spinning.   
  50. //(With fewer mouse buttons, ctrl + shift + left button is for zooming,   
  51.     //and shift + left button is for panning.)  
  52.   
  53. //   vtkInteractorStyleUnicam *style = //只有放大和平移的功能  
  54. //     vtkInteractorStyleUnicam::New();  
  55. //   iren->SetInteractorStyle(style);  
  56.   
  57. //   vtkInteractorStyleUser *style =   //没有鼠标响应 主要用于用户自定义的操作  
  58. //     vtkInteractorStyleUser::New();  
  59. //   iren->SetInteractorStyle(style);  
  60. //   vtkInteractorStyleTerrain *style =   
  61. //     vtkInteractorStyleTerrain::New();  
  62. //   iren->SetInteractorStyle(style);  
  63. /* 
  64. vtkInteractorStyleSwitch *style =  
  65.     vtkInteractorStyleSwitch::New(); 
  66. iren->SetInteractorStyle(style); 
  67. */  
  68. //   vtkInteractorStyleFlight *style =   
  69. //     vtkInteractorStyleFlight::New();  
  70. //   iren->SetInteractorStyle(style);  
  71. // vtkInteractorStyleRubberBandZoom *style=vtkInteractorStyleRubberBandZoom::New();  
  72. // iren->SetInteractorStyle(style);  
  73. // vtkInteractorStyleSwitch *style=vtkInteractorStyleSwitch::New();  
  74. //     iren->SetInteractorStyle(style);  
  75. //     vtkInteractorStyleJoystickCamera *style=vtkInteractorStyleJoystickCamera::New();  
  76. // iren->SetInteractorStyle(style);  
  77. // vtkInteractorStyleJoystickActor *style=vtkInteractorStyleJoystickActor::New();  
  78. // iren->SetInteractorStyle(style);  
  79.   
  80. ///////////////////////////////////////////////////////////////////////////  
  81. vtkDICOMImageReader   *reader = vtkDICOMImageReader::New();  
  82. reader->SetDataByteOrderToLittleEndian();  
  83. reader->SetDirectoryName("D://1//3");  
  84. vtkImageData *imageData = vtkImageData::New();  
  85. imageData = reader->GetOutput();  
  86. imageData->SetOrigin(.0,.0,.0);  
  87. //imageData->SetDimensions(64,64,93);  
  88. vtkImageShrink3D *shrink=vtkImageShrink3D::New();//二次采样  
  89.    shrink->SetInput((vtkDataObject *)reader->GetOutput());  
  90.    shrink->SetShrinkFactors(4,4,1);//采样因子  
  91. vtkMarchingCubes *skinExtractor = vtkMarchingCubes::New();  
  92.     skinExtractor->SetInputConnection(shrink->GetOutputPort());  
  93.     skinExtractor->SetValue(0,200);//计算体素的等值面 可以提取多个等值面 0~200表示第一个等值面的值为200  
  94. vtkDecimatePro *deci=vtkDecimatePro::New();//减少mesh的三角面片  
  95.    deci->SetInputConnection(skinExtractor->GetOutputPort());  
  96.    deci->SetTargetReduction(0.3);//将原先的三角面片减少到原来的百分之七十  
  97. // Specify the desired reduction in the total number of polygons (e.g., if TargetReduction is set to 0.9, this filter will try to reduce the data set to 10% of its original size). Because of various constraints, this level of reduction may not be realized. If you want to guarantee a particular reduction, you must turn off PreserveTopology, turn on SplitEdges and BoundaryVertexDeletion, and set the MaximumError to VTK_DOUBLE_MAX (these ivars are initialized this way when the object is instantiated).  
  98.   
  99. //adjust point positions using Laplacian smoothing   
  100. vtkSmoothPolyDataFilter *smooth=vtkSmoothPolyDataFilter::New();   
  101.     smooth->SetInputConnection(deci->GetOutputPort());  
  102. smooth->SetNumberOfIterations(100) ;//设置Laplace平滑的迭代次数  
  103. vtkPolyDataNormals *skinNormals = vtkPolyDataNormals::New();//compute normals for polygonal mesh   
  104.     skinNormals->SetInputConnection(smooth->GetOutputPort());  
  105.     skinNormals->SetFeatureAngle(60.0);//Specify the angle that defines a sharp edge. If the difference in angle across neighboring polygons is greater than this value, the shared edge is considered "sharp".  
  106. ////////////////////////////////////////////////////////////////////////////////////////  
  107. // vtkTriangleFilter *triangle_filter=vtkTriangleFilter::New();//create triangle polygons from input polygons and triangle strips   
  108. // triangle_filter->SetInput( skinNormals->GetOutput());  
  109. vtkFeatureEdges *edges_extractor=vtkFeatureEdges::New();//extract boundary, non-manifold, and/or sharp edges from polygonal data   
  110. edges_extractor->SetInput(skinNormals->GetOutput());  
  111. edges_extractor->ColoringOff();//Turn on/off the coloring of edges by type.   
  112. edges_extractor->BoundaryEdgesOn();//Turn on/off the extraction of boundary edges  
  113. edges_extractor->ManifoldEdgesOn();//Turn on/off the extraction of manifold edges.   
  114. edges_extractor->NonManifoldEdgesOn();//Turn on/off the extraction of non-manifold edges.   
  115. vtkStripper *stripper=vtkStripper::New();   
  116.     stripper->SetInput(skinNormals->GetOutput());  
  117. /* 
  118. vtkPolyDataWriter *wSP=vtkPolyDataWriter::New(); 
  119.     wSP->SetInput(stripper->GetOutput()); 
  120.     wSP->SetFileName("E://CT/aaaa.VTK"); 
  121.     wSP->Write(); 
  122.    wSP->Delete(); 
  123.    *///保存为VTK格式  
  124. vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New();  
  125.     skinMapper->SetInput(stripper->GetOutput());  
  126. skinMapper->ScalarVisibilityOff();//Turn on/off flag to control whether the symbol's scalar data is used to color the symbol. If off, the color of the vtkLegendBoxActor is used.  
  127.   
  128. vtkActor *skin = vtkActor::New();  
  129.     skin->SetMapper(skinMapper);  
  130. skin->GetProperty()->SetDiffuseColor(1, 0.49, 0.25);  
  131.     skin->GetProperty()->SetSpecular(.3);  
  132.     skin->GetProperty()->SetSpecularPower(20);  
  133. vtkCamera *aCamera = vtkCamera::New();  
  134.     aCamera->SetViewUp (0, 0, -1);  
  135.     aCamera->SetPosition (0, 1, 0);//Set/Get the position of the camera in world coordinates. The default position is (0,0,1).   
  136.     aCamera->SetFocalPoint (0, 0, 0);  
  137.     aCamera->ComputeViewPlaneNormal();  
  138. aRenderer->AddActor(skin);  
  139. aRenderer->SetActiveCamera(aCamera);  
  140. aRenderer->ResetCamera ();//Automatically set up the camera based on the visible actors. The camera will reposition itself to view the center point of the actors, and move along its initial view plane normal (i.e., vector defined from camera position to focal point) so that all of the actors can be seen.   
  141. aCamera->Dolly(1.5);// Move the position of the camera along the direction of projection. Moving towards the focal point (e.g., greater than 1) is a dolly-in, moving away from the focal point (e.g., less than 1) is a dolly-out.  
  142. aRenderer->SetBackground(1,1,1);  
  143. renWin->SetSize(640, 480);  
  144. aRenderer->ResetCameraClippingRange (); // Reset the camera clipping range based on the bounds of the visible actors. This ensures that no props are cut off  
  145.   
  146. iren->Initialize();  
  147. iren->Start();  
  148. shrink->Delete();  
  149. smooth->Delete();  
  150. deci->Delete();  
  151. stripper->Delete();  
  152. skinExtractor->Delete();  
  153. skinNormals->Delete();  
  154. skinMapper->Delete();  
  155. skin->Delete();  
  156. aCamera->Delete();  
  157. iren->Delete();  
  158. renWin->Delete();  
  159. aRenderer->Delete();  
  160. reader->Delete();  
  161. return 0;  
0 0
原创粉丝点击