15 #include "PlusConfigure.h" 16 #include "vtkCamera.h" 17 #include "vtkFeatureEdges.h" 18 #include "vtkInteractorStyleTrackballCamera.h" 19 #include "vtkPNGWriter.h" 20 #include "vtkPolyDataMapper.h" 21 #include "vtkPolyDataNormals.h" 22 #include "vtkProperty.h" 23 #include "vtkRenderWindow.h" 24 #include "vtkRenderWindowInteractor.h" 25 #include "vtkRenderer.h" 26 #include "vtkSmartPointer.h" 27 #include "vtkSTLReader.h" 29 #include "vtkTriangleFilter.h" 30 #include "vtkWindowToImageFilter.h" 31 #include "vtksys/CommandLineArguments.hxx" 32 #include "vtksys/SystemTools.hxx" 34 int main(
int argc,
char **argv)
36 bool printHelp(
false);
37 std::string modelFileName;
38 std::string outputImageFileName;
39 std::vector<double> cameraRollPitchYawDeg;
40 double cameraZoomFactor = 1.0;
41 bool renderingOff(
false);
43 int verboseLevel=vtkPlusLogger::LOG_LEVEL_UNDEFINED;
45 vtksys::CommandLineArguments args;
46 args.Initialize(argc, argv);
48 args.AddArgument(
"--help", vtksys::CommandLineArguments::NO_ARGUMENT, &printHelp,
"Print this help.");
49 args.AddArgument(
"--model-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &modelFileName,
"Name of the input STL model file");
50 args.AddArgument(
"--output-image-file", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &outputImageFileName,
"Name of the output PNG image file that will be created. Optional. If not specified then an interactive 3D window is displayed.");
51 args.AddArgument(
"--camera-orientation", vtksys::CommandLineArguments::MULTI_ARGUMENT, &cameraRollPitchYawDeg,
"Roll, pitch, yaw angles of the camera in degrees. Optional. Default: 0 20 20.");
52 args.AddArgument(
"--camera-zoom", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &cameraZoomFactor,
"Zoom factor of the camera. Optional. Default: 1.0.");
53 args.AddArgument(
"--rendering-off", vtksys::CommandLineArguments::NO_ARGUMENT, &renderingOff,
"Run test without rendering.");
54 args.AddArgument(
"--verbose", vtksys::CommandLineArguments::EQUAL_ARGUMENT, &verboseLevel,
"Verbose level (1=error only, 2=warning, 3=info, 4=debug, 5=trace)");
58 std::cerr <<
"Problem parsing arguments" << std::endl;
59 std::cout <<
"Help: " << args.GetHelp() << std::endl;
65 std::cout << args.GetHelp() << std::endl;
71 if (modelFileName.empty())
73 std::cerr <<
"--model-file is required" << std::endl;
77 if (cameraRollPitchYawDeg.size()==0)
79 cameraRollPitchYawDeg.push_back(0);
80 cameraRollPitchYawDeg.push_back(20);
81 cameraRollPitchYawDeg.push_back(20);
83 else if (cameraRollPitchYawDeg.size()!=3)
85 LOG_ERROR(
"--camera-orientation requires 3 parameters");
89 vtkSmartPointer<vtkSTLReader> modelReader = vtkSmartPointer<vtkSTLReader>::New();
90 modelReader->SetFileName(modelFileName.c_str());
91 modelReader->Update();
92 vtkPolyData* modelPolyData=modelReader->GetOutput();
93 if (modelPolyData->GetNumberOfPoints()==0)
95 LOG_ERROR(
"Failed to load model from file: "<<modelFileName);
99 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
100 renderer->SetBackground(1,1,1);
101 renderer->SetUseDepthPeeling(1);
102 renderer->SetMaximumNumberOfPeels(100);
103 renderer->SetOcclusionRatio(0.1);
105 vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
106 if (!outputImageFileName.empty())
108 renWin->OffScreenRenderingOn();
110 renWin->SetSize(500,500);
111 renWin->SetAlphaBitPlanes(1);
112 renWin->SetMultiSamples(0);
113 renWin->AddRenderer(renderer);
116 vtkSmartPointer<vtkRenderWindowInteractor>
iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
117 iren->SetInteractorStyle(vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New());
118 iren->SetRenderWindow(renWin);
121 renderer->GetActiveCamera();
127 vtkSmartPointer<vtkPolyDataNormals> modelNormals = vtkSmartPointer<vtkPolyDataNormals>::New();
129 modelNormals->SetInputConnection(modelReader->GetOutputPort());
131 vtkSmartPointer<vtkPolyDataMapper> modelMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
132 modelMapper->SetInputConnection(modelNormals->GetOutputPort());
134 vtkSmartPointer<vtkActor> modelActor = vtkSmartPointer<vtkActor>::New();
135 modelActor->SetMapper(modelMapper);
136 modelActor->GetProperty()->SetColor(0.9, 0.9, 0.9);
137 modelActor->GetProperty()->SetOpacity(0.8);
138 renderer->AddActor(modelActor);
140 vtkSmartPointer<vtkTriangleFilter> triangleFilter = vtkSmartPointer<vtkTriangleFilter>::New();
141 triangleFilter->SetInputConnection(modelNormals->GetOutputPort());
143 vtkSmartPointer<vtkFeatureEdges> edgeExtractor = vtkSmartPointer<vtkFeatureEdges>::New();
144 edgeExtractor->SetInputConnection(triangleFilter->GetOutputPort());
145 edgeExtractor->ColoringOff();
146 edgeExtractor->BoundaryEdgesOn();
147 edgeExtractor->ManifoldEdgesOn();
148 edgeExtractor->NonManifoldEdgesOn();
150 vtkSmartPointer<vtkPolyDataMapper> modelEdgesMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
151 modelEdgesMapper->SetInputConnection(edgeExtractor->GetOutputPort());
152 modelEdgesMapper->SetResolveCoincidentTopologyToPolygonOffset();
153 vtkSmartPointer<vtkActor> modelEdgesActor = vtkSmartPointer<vtkActor>::New();
154 modelEdgesActor->SetMapper(modelEdgesMapper);
155 modelEdgesActor->GetProperty()->SetColor(0.0, 0.0, 0.0);
156 renderer->AddActor(modelEdgesActor);
159 vtkSmartPointer<vtkPlusToolAxesActor> originActor=vtkSmartPointer<vtkPlusToolAxesActor>::New();
160 originActor->SetName(
"origin");
164 renderer->GetActiveCamera()->ParallelProjectionOn();
165 renderer->GetActiveCamera()->Roll(cameraRollPitchYawDeg[0]);
166 renderer->GetActiveCamera()->Pitch(cameraRollPitchYawDeg[1]);
167 renderer->GetActiveCamera()->Yaw(cameraRollPitchYawDeg[2]);
168 renderer->ResetCamera();
169 renderer->GetActiveCamera()->Zoom(cameraZoomFactor);
171 if (outputImageFileName.empty())
177 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
178 windowToImageFilter->SetInput(renWin);
179 windowToImageFilter->Update();
181 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
182 writer->SetFileName(outputImageFileName.c_str());
183 writer->SetInputData(windowToImageFilter->GetOutput());
186 LOG_INFO(
"Rendered image is written to "<<outputImageFileName);
vtkRenderWindowInteractor * iren
int main(int argc, char **argv)
static vtkIGSIOLogger * Instance()