vtkSurfaceNets3D does not preserve triangle/quad orientation
vtkSurfaceNets3D does not correctly preserve polygon orientation. If I want to extract surfaces from a labelfield and obtain each labels surface with correct triangle orientation (normal pointing outwards) I would first need to run e.g. vtkPolyDataNormals
. This seems a shame, as I loose some of the performance (and the result may be incorrect).
What I expected was that the BoundaryLabels
array would store two labels
- the first component would correspond to the triangle/quad stored in
Polys
- the second would be the label corresponding to a triangle with opposite orientation
If I extract label surfaces like this and render using backface culling, I see half of the triangles have the wrong orientation. The culprit is here:
void WriteScalarTuple(T s0, T s1, T backgroundLabel, vtkIdType quadId)
{
T* scalars = this->NewScalars + 2 * quadId;
if (s0 == backgroundLabel || (s1 != backgroundLabel && s0 > s1))
{
// Background label is placed last; s0<s1 if both inside
std::swap(s0, s1);
}
*scalars++ = s0; // write 2-tuple
*scalars++ = s1;
} // WriteScalarTuple
If I comment out the std::swap(s0, s1)
the orientation information is preserved.
If it is important to not have the background label in the first component, then I guess this could be corrected by flipping the quad in GenerateQuadsImpl::operator(...)
, or as a postprocessing pass. I think the "BoundaryLabels" array could also just be documented.