I am new to VTK and interested to make a CAD software. Recently I have been working with decimation. I have tried to write the reduced STL using vtkSTLWriter. But the exported STL is not correctly visible due to disturbed vertices.
Weird thing is if I import the same STL in blender, the export seems fine. And If I export an STL of the same vtkSTLWritten imported STL from blender, then the blender exported STL is visible fine in Unity3D.
Some filters may not preserve consistency of cell orientations, so I would consider it to be normal that you need to reorient (unless you analysed all input data sets and processing filters and based on that you would not expect it to be necessary).
@lassoan So what I am going to assume in this case is vtkSTLReader is somehow reading STL with flipped faces? So that before exporting, I need to perform setFlipNormals(true) everytime. Right?
@lassoan I wrote a code to Flip normals in Unity3D after importing STL mesh.
I have verified by test case of original input STL and vtk written STL files that vtk is causing this issue to output STL files. I discarded the use of vtkPolyDataNormals at vtkSTLWriter time.
Test Files:
original.stl (input)
original_test.stl (export of original.stl by vtk)
original_test2.stl (export of original_test.stl by vtk)
original_test3.stl (export of original_test2.stl by vtk)
After importing all 4 STL files in Unity3D, I see original input STL all fine and all 3 vtk exported STL files faces incorrectly oriented. So I place the code to Flip normals in Unity3D for all, and this time original input STL faces were incorrectly oriented due to code and all 3 vtk exported STL files were fine as fixed by code.
My point is if I missing something at write time? Maybe stlWriter->Update()?
The code looks fine and straightforward. Saving and loading and STL file with VTK, I didn’t see any reorientation of the geometry faces taking place.
Could you share a small STL file that shows the odd behavior with this program?
I have looked at both your original STL files and the files converted by VTK. The only difference I see between the original files and VTK-produced files is that the original is a binary STL file and those saved out from the VTK STL writer are binary. Content-wise, they are exactly the same in terms of polygon winding, points, and normals, aside from some minor precision differences in the floating point numbers. Hence, there is no problem with VTK.
Perhaps the difference is in how Unity is importing binary vs. STL files. You can check whether this is the case by calling
vtkSTLWriter::SetFileTypeToBinary()
and saving out the VTK files that way. Hopefully Unity will treat normals in those files the same way as your original binary STL.