如何从.vtk文件中提取点法线?

时间:2015-05-11 12:30:06

标签: point vtk

我使用vtkGenericDataObjectReader类来读取仅包含点位置和VTK_TETRA单元格的.vtk文件:

# vtk DataFile Version 2.0
Unstructured Grid
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 19002 double
-0.150669 0.33708199999999999 -0.053355
0.240651 -0.40023399999999998 -0.183224
-0.15488199999999999 -0.54804799999999998 -0.210316
... the other points ...
CELLS 75753 378765
4  3472 3996 7922 9626
4  5182 6191 12063 12612
4  5555 2996 18404 18616
... the other cells ...

成功读取文件后,我无法通过以下方式获得点法线:

VTK_CREATE(vtkGenericDataObjectReader, reader);
reader->SetFileName(file.toStdString().c_str());
reader->Update();

Q_ASSERT(reader->IsFileUnstructuredGrid());

VTK_CREATE(vtkUnstructuredGrid, ug);
ug = vtkUnstructuredGrid::SafeDownCast(reader->GetOutput());
vtkDataArray* normals = ug->GetPointData()->GetArray("Normals");
//vtkDataArray* normals = ug->GetPointData()->GetNormals();

VTK_CREATE(vtkDataSetMapper, modelMapper);
modelMapper->SetInputData(ug);
modelActor->SetMapper(modelMapper);

// ...

因为法线必须是零指针。

然而,最终渲染的灯光效果似乎是合理的。所以这是我的问题:(1)VTK如何计算点法线以获得正确的光效? (2)我应该怎么做才能得到正常点?

1 个答案:

答案 0 :(得分:1)

VTK获得了vtkPolyDataNormals算法类,允许您处理多边形网格中的法线。

VTK_CREATE(vtkGenericDataObjectReader, reader);
reader->SetFileName(file.toStdString().c_str());

VTK_CREATE(vtkPolyDataNormals, norms)
norms->SetInputConnection (reader->GetOutputPort ());
// the output of the vtkPolyDataNormals instance is a PolyData with normals calculated

norms->Update()
VTK_CREATE(vtkDataSetMapper, modelMapper);
modelMapper->SetInputData( norms->GetOutput () );
  

vtkPolyDataNormals是一个过滤器,用于计算多边形网格的点和/或单元法线。

在这种情况下,输出PolyData必须有一个非零法线指针。