1 #include "vtkAutoInit.h" 2 VTK_MODULE_INIT(vtkRenderingOpenGL2); 3 VTK_MODULE_INIT(vtkInteractionStyle); 4 5 #include <vtkSmartPointer.h> 6 #include <vtkSelectionNode.h> 7 #include <vtkInformation.h> 8 #include <vtkUnstructuredGrid.h> 9 #include <vtkPolyData.h> 10 #include <vtkPolyDataNormals.h> 11 #include <vtkPointData.h> 12 #include <vtkXMLPolyDataReader.h> 13 #include <vtkRenderWindow.h> 14 #include <vtkRenderWindowInteractor.h> 15 #include <vtkRenderer.h> 16 #include <vtkSelection.h> 17 #include <vtkSelectionNode.h> 18 #include <vtkSphereSource.h> 19 #include <vtkPolyDataMapper.h> 20 #include <vtkActor.h> 21 #include <vtkCamera.h> 22 #include <vtkProperty.h> 23 #include <vtkIdTypeArray.h> 24 #include <vtkExtractSelection.h> 25 #include <vtkDataSetSurfaceFilter.h> 26 #include <vtkFeatureEdges.h> 27 #include <vtkFillHolesFilter.h> 28 29 void GenerateData(vtkSmartPointer<vtkPolyData> input) 30 { 31 vtkSmartPointer<vtkSphereSource> sphereSource = 32 vtkSmartPointer<vtkSphereSource>::New(); 33 sphereSource->Update(); 34 35 vtkSmartPointer<vtkIdTypeArray> ids = 36 vtkSmartPointer<vtkIdTypeArray>::New(); 37 ids->SetNumberOfComponents(1); 38 ids->InsertNextValue(2); 39 ids->InsertNextValue(10); 40 41 vtkSmartPointer<vtkSelectionNode> selectionNode = 42 vtkSmartPointer<vtkSelectionNode>::New(); 43 selectionNode->SetFieldType(vtkSelectionNode::CELL); 44 selectionNode->SetContentType(vtkSelectionNode::INDICES); 45 selectionNode->SetSelectionList(ids); 46 selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(), 1); 47 48 vtkSmartPointer<vtkSelection> selection = 49 vtkSmartPointer<vtkSelection>::New(); 50 selection->AddNode(selectionNode); 51 52 vtkSmartPointer<vtkExtractSelection> extractSelection = 53 vtkSmartPointer<vtkExtractSelection>::New(); 54 extractSelection->SetInputData(0, sphereSource->GetOutput()); 55 extractSelection->SetInputData(1, selection); 56 extractSelection->Update(); 57 58 vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = 59 vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); 60 surfaceFilter->SetInputConnection(extractSelection->GetOutputPort()); 61 surfaceFilter->Update(); 62 63 input->ShallowCopy(surfaceFilter->GetOutput()); 64 } 65 66 int main(int argc, char *argv[]) 67 { 68 vtkSmartPointer<vtkPolyData> input = 69 vtkSmartPointer<vtkPolyData>::New(); 70 GenerateData(input); 71 72 vtkSmartPointer<vtkFeatureEdges> featureEdges = 73 vtkSmartPointer<vtkFeatureEdges>::New(); 74 featureEdges->SetInputData(input); 75 featureEdges->BoundaryEdgesOn(); 76 featureEdges->FeatureEdgesOff(); 77 featureEdges->ManifoldEdgesOff(); 78 featureEdges->NonManifoldEdgesOff(); 79 featureEdges->Update(); 80 81 int numberOfOpenEdges = featureEdges->GetOutput()->GetNumberOfCells(); 82 if(numberOfOpenEdges) 83 { 84 std::cout<<"该网格模型不是封闭的..."<<std::endl; 85 } 86 else 87 { 88 std::cout<<"该网格模型是封闭的..."<<std::endl; 89 return EXIT_SUCCESS; 90 } 91 92 vtkSmartPointer<vtkFillHolesFilter> fillHolesFilter = 93 vtkSmartPointer<vtkFillHolesFilter>::New(); 94 fillHolesFilter->SetInputData(input); 95 fillHolesFilter->Update(); 96 97 vtkSmartPointer<vtkPolyDataNormals> normals = 98 vtkSmartPointer<vtkPolyDataNormals>::New(); 99 normals->SetInputConnection(fillHolesFilter->GetOutputPort()); 100 normals->ConsistencyOn(); 101 normals->SplittingOff(); 102 normals->Update(); 103 104 // 105 double leftViewport[4] = {0.0, 0.0, 0.5, 1.0}; 106 double rightViewport[4] = {0.5, 0.0, 1.0, 1.0}; 107 108 vtkSmartPointer<vtkPolyDataMapper> originalMapper = 109 vtkSmartPointer<vtkPolyDataMapper>::New(); 110 originalMapper->SetInputData(input); 111 112 vtkSmartPointer<vtkProperty> backfaceProp = 113 vtkSmartPointer<vtkProperty>::New(); 114 backfaceProp->SetDiffuseColor(0.89,0.81,0.34); 115 116 vtkSmartPointer<vtkActor> originalActor = 117 vtkSmartPointer<vtkActor>::New(); 118 originalActor->SetMapper(originalMapper); 119 originalActor->SetBackfaceProperty(backfaceProp); 120 originalActor->GetProperty()->SetDiffuseColor(1.0, 0.3882, 0.2784); 121 122 vtkSmartPointer<vtkPolyDataMapper> edgeMapper = 123 vtkSmartPointer<vtkPolyDataMapper>::New(); 124 edgeMapper->SetInputData(featureEdges->GetOutput()); 125 vtkSmartPointer<vtkActor> edgeActor = 126 vtkSmartPointer<vtkActor>::New(); 127 edgeActor->SetMapper(edgeMapper); 128 edgeActor->GetProperty()->SetEdgeColor(0.,0.,1.0); 129 edgeActor->GetProperty()->SetEdgeVisibility(1); 130 edgeActor->GetProperty()->SetLineWidth(5); 131 132 vtkSmartPointer<vtkPolyDataMapper> filledMapper = 133 vtkSmartPointer<vtkPolyDataMapper>::New(); 134 filledMapper->SetInputData(normals->GetOutput()); 135 136 vtkSmartPointer<vtkActor> filledActor = 137 vtkSmartPointer<vtkActor>::New(); 138 filledActor->SetMapper(filledMapper); 139 filledActor->GetProperty()->SetDiffuseColor(1.0, 0.3882, 0.2784); 140 141 vtkSmartPointer<vtkRenderer> leftRenderer = 142 vtkSmartPointer<vtkRenderer>::New(); 143 leftRenderer->SetViewport(leftViewport); 144 leftRenderer->AddActor(originalActor); 145 leftRenderer->AddActor(edgeActor); 146 leftRenderer->SetBackground(1.0, 1.0, 1.0); 147 148 vtkSmartPointer<vtkRenderer> rightRenderer = 149 vtkSmartPointer<vtkRenderer>::New(); 150 rightRenderer->SetViewport(rightViewport); 151 rightRenderer->AddActor(filledActor); 152 rightRenderer->SetBackground(1.0, 1.0, 1.0); 153 154 vtkSmartPointer<vtkRenderWindow> renderWindow = 155 vtkSmartPointer<vtkRenderWindow>::New(); 156 renderWindow->AddRenderer(leftRenderer); 157 renderWindow->AddRenderer(rightRenderer); 158 renderWindow->SetSize(640, 320); 159 renderWindow->Render(); 160 renderWindow->SetWindowName("PolyDataClosed"); 161 162 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 163 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 164 renderWindowInteractor->SetRenderWindow(renderWindow); 165 166 leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0); 167 leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0); 168 leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1); 169 leftRenderer->GetActiveCamera()->Azimuth(30); 170 leftRenderer->GetActiveCamera()->Elevation(30); 171 leftRenderer->ResetCamera(); 172 rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera()); 173 174 renderWindow->Render(); 175 renderWindowInteractor->Start(); 176 177 return EXIT_SUCCESS; 178 }
左图为原始模型,右图为漏洞填补后的结果
标签:1.0,54,VTK,封闭性,vtkSmartPointer,featureEdges,New,include,leftRenderer From: https://www.cnblogs.com/ybqjymy/p/17632964.html