19 #include <vtkCleanPolyData.h> 20 #include <vtkErrorCode.h> 21 #include <vtkPolyDataNormals.h> 22 #include <vtkSTLReader.h> 23 #include <vtkSTLWriter.h> 24 #include <vtkSmartPointer.h> 25 #include <vtkTriangleFilter.h> 29 std::string SurfaceStlIO::OPTION_MERGE_POINTS()
31 static std::string s =
"Merge points";
35 std::string SurfaceStlIO::OPTION_TAG_SOLIDS()
37 static std::string s =
"Tag solids";
41 std::string SurfaceStlIO::OPTION_CLEAN()
43 static std::string s =
"Clean poly data";
52 defaultOptions[OPTION_MERGE_POINTS()] =
us::Any(
true);
53 defaultOptions[OPTION_TAG_SOLIDS()] =
us::Any(
false);
54 defaultOptions[OPTION_CLEAN()] =
us::Any(
true);
69 vtkSmartPointer<vtkSTLReader> stlReader = vtkSmartPointer<vtkSTLReader>::New();
72 bool mergePoints =
true;
73 bool tagSolids =
false;
74 bool cleanData =
true;
78 mergePoints =
us::any_cast<
bool>(options[OPTION_MERGE_POINTS()]);
79 tagSolids =
us::any_cast<
bool>(options[OPTION_TAG_SOLIDS()]);
87 stlReader->SetMerging(mergePoints);
88 stlReader->SetScalarTags(tagSolids);
90 vtkSmartPointer<vtkPolyDataNormals> normalsGenerator = vtkSmartPointer<vtkPolyDataNormals>::New();
91 normalsGenerator->SetInputConnection(stlReader->GetOutputPort());
93 vtkSmartPointer<vtkPolyDataAlgorithm> algo = normalsGenerator;
96 vtkSmartPointer<vtkCleanPolyData> cleanPolyDataFilter = vtkSmartPointer<vtkCleanPolyData>::New();
97 cleanPolyDataFilter->SetInputConnection(normalsGenerator->GetOutputPort());
98 cleanPolyDataFilter->PieceInvariantOff();
99 cleanPolyDataFilter->ConvertLinesToPointsOff();
100 cleanPolyDataFilter->ConvertPolysToLinesOff();
101 cleanPolyDataFilter->ConvertStripsToPolysOff();
104 cleanPolyDataFilter->PointMergingOn();
106 algo = cleanPolyDataFilter;
110 if (algo->GetOutput() !=
nullptr)
112 vtkSmartPointer<vtkPolyData> surfaceWithNormals = algo->GetOutput();
113 output->SetVtkPolyData(surfaceWithNormals);
116 std::vector<BaseData::Pointer> result;
117 result.push_back(output.GetPointer());
129 for (
unsigned int t = 0; t < timesteps; ++t)
131 std::string fileName;
132 vtkSmartPointer<vtkPolyData> polyData = this->
GetPolyData(t, fileName);
133 vtkSmartPointer<vtkTriangleFilter> triangleFilter = vtkSmartPointer<vtkTriangleFilter>::New();
134 triangleFilter->SetInputData(polyData);
135 vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
136 writer->SetInputConnection(triangleFilter->GetOutputPort());
140 writer->SetFileName(localFile.
GetFileName().c_str());
142 if (writer->Write() == 0 || writer->GetErrorCode() != 0)
144 mitkThrow() <<
"Error during surface writing" 145 << (writer->GetErrorCode() ?
146 std::string(
": ") + vtkErrorCode::GetStringFromErrorCode(writer->GetErrorCode()) :
150 if (this->
GetOutputStream() && input->GetTimeGeometry()->CountTimeSteps() > 1)
152 MITK_WARN <<
"Writing multiple time-steps to output streams is not supported. " 153 <<
"Only the first time-step will be written";
Class for storing surfaces (vtkPolyData).
virtual TimeStepType CountTimeSteps() const =0
Returns the number of time steps.
std::string GetFileName()
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
void SetDefaultReaderOptions(const Options &defaultOptions)
vtkSmartPointer< vtkPolyData > GetPolyData(unsigned int t, std::string &fileName)
std::map< std::string, us::Any > Options
Options for reading or writing data.
void Write() override
Write the base data to the specified location or output stream.
const char * what() const override
std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
std::string GetLocalFileName() const
Get a local file name for reading.
const mitk::TimeGeometry * GetTimeGeometry() const
Return the TimeGeometry of the data as const pointer.
Convenience class to temporarily change the current locale.
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
const BaseData * GetInput() const override
Get the input data set via SetInput().
A local file representation for streams.
void ValidateOutputLocation() const
std::ostream * GetOutputStream() const override
Get the output stream.
Options GetReaderOptions() const