vtkSTLWriter存储了cell相关的点,而vtkXMLPolyDataWriter保存了原始polydata中所有的点,不管它有没有被cell使用到。所以保存数据之后要看一下效果,最好用 vtkXMLPolyDataWriter,保存不丢失数据;
常用,这里记录一下
#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSTLReader.h>
#include <vtkSTLWriter.h>
#include <vtkSphereSource.h>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
if (argc != 2)
{
std::cout << "Required parameters: filename.stl" << std::endl;
return EXIT_FAILURE;
}
std::string filename = argv[1];
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
vtkNew<vtkSTLWriter> stlWriter;
stlWriter->SetFileName(filename.c_str());
stlWriter->SetInputConnection(sphereSource->GetOutputPort());
stlWriter->Write();
// Read and display for verification
vtkNew<vtkSTLReader> reader;
reader->SetFileName(filename.c_str());
reader->Update();
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("WriteSTL");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->SetBackground(colors->GetColor3d("cobalt_green").GetData());
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
// VTK 保存
--cpp
vtkSmartPointer<vtkPolyDataWriter> vtkWriter = vtkSmartPointer<vtkPolyDataWriter>::New();
vtkWriter->SetInput(skinExtractor->GetOutput());
vtkWriter->SetFileName("test.vtk");
vtkWriter->Write();
-------
// Write the unstructured grid
```cpp
vtkNew<vtkXMLUnstructuredGridWriter> writer;
writer->SetFileName("test.vtu");
writer->SetInputData(unstructuredGrid);
writer->Write();
vtkSmartPointer<vtkUnstructuredGridReader> reader =
vtkSmartPointer<vtkUnstructuredGridReader>::New();
reader->SetFileName("test.vtk");
// delete[] fileName;
reader->Update();
--------------
数据旋转后保存 STL
void SaveSTL(char* name, vtkPolyData* data)
{
vtkNew<vtkTransform> transform;
double * bounds = data->GetBounds();
// Compute the center of the image
double center[3];
center[0] = (bounds[1] + bounds[0]) / 2.0;
center[1] = (bounds[3] + bounds[2]) / 2.0;
center[2] = (bounds[5] + bounds[4]) / 2.0;
// Rotate about the center
transform->Translate(center[0], center[1], center[2]);
transform->RotateWXYZ(180, 1, 0, 0);
transform->Translate(-center[0], -center[1], -center[2]);
vtkSmartPointer<vtkTransform> pTransform = vtkSmartPointer<vtkTransform>::New();
// pTransform->SetInput(actor->GetUserTransform());
pTransform->SetMatrix(transform->GetMatrix());
pTransform->Update();
vtkSmartPointer<vtkTransformPolyDataFilter> pTransformPolyDataFilterSTL = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
pTransformPolyDataFilterSTL->SetInputData(data);
pTransformPolyDataFilterSTL->SetTransform(pTransform);
pTransformPolyDataFilterSTL->Update();
vtkNew<vtkSTLWriter> stlWriter;
stlWriter->SetFileName(name);
stlWriter->SetInputData(pTransformPolyDataFilterSTL->GetOutput());
stlWriter->Update();
stlWriter->Write();
}
使用 vtkSTLWriter 保存数据,发现和实际显示的数据有差异,查资料发现,
vtkSTLWriter存储了cell相关的点,而vtkXMLPolyDataWriter保存了原始polydata中所有的点,不管它有没有被cell使用到。
char filename[128] = "./a.vtp";
vtkNew<vtkXMLPolyDataWriter> stlWriter;
stlWriter->SetFileName(filename);
stlWriter->SetInputData(pigPoly);
stlWriter->Update();
stlWriter->Write();
或者保存成 obj
vtkSmartPointer<vtkOBJExporter> porter = vtkSmartPointer<vtkOBJExporter>::New();
porter->SetFilePrefix("./qiu.obj");
porter->SetInput(renderWindow);
porter->Write();
读;
std::string filename = "aa.obj";
vtkSmartPointer<vtkOBJReader> reader1 =
vtkSmartPointer<vtkOBJReader>::New();
reader1->SetFileName(filename.c_str());
reader1->Update();