vtkPolydata中取点的坑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (int i = 0; i < lines->GetNumberOfCells(); i++)
{
// 获取一条centerline
vtkIdType nbCellPoints;
vtkIdType* points;
lines->GetNextCell(nbCellPoints, points);

// 迭代该条centerline,获取小线段
for (int j = 0; j < nbCellPoints-1; j++)
{
vtkIdType pid1 = points[j];
vtkIdType pid2 = points[j + 1];

double* p1 = polydata->GetPoints()->GetPoint(pid1);
double* p2 = polydata->GetPoints()->GetPoint(pid1);

cout << "P1 : " << pid1 << " " << p1[0] / spacing[0] << "," << p1[1] / spacing[1] << "," << p1[2] / spacing[2] << endl;
cout << "P2 : " << pid2 << " " << p2[0] / spacing[0] << "," << p2[1] / spacing[1] << "," << p2[2] / spacing[2] << endl;
}
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
P1 : 131 111,177,121
P2 : 130 111,177,121
P1 : 130 112,178,120
P2 : 129 112,178,120
P1 : 129 112,178,119
P2 : 128 112,178,119
P1 : 128 112,177,118
P2 : 127 112,177,118
P1 : 127 112,176,117
P2 : 126 112,176,117
P1 : 126 111,175,116
...

发现每次输出P1跟P2坐标相同,好久没发现问题所在。追踪到源码,发现这么一段warning,细想一下,这样设计业很好理解。

1
2
3
4
5
6
7
8
9
10
11
// Description:
// Return a pointer to a double point x[3] for a specific id.
// WARNING: Just don't use this error-prone method, the returned pointer
// and its values are only valid as long as another method invocation is not
// performed. Prefer GetPoint() with the return value in argument.
double *GetPoint(vtkIdType id) { return this->Data->GetTuple(id); }

// Description:
// Copy point components into user provided array v[3] for specified
// id.
void GetPoint(vtkIdType id, double x[3]) { this->Data->GetTuple(id,x); }

更新程序使用

1
2
3
4
double p1[3];
double p2[3];
polydata->GetPoints()->GetPoint(pid1, p1);
polydata->GetPoints()->GetPoint(pid2, p2);

目标达成。

坚持原创技术分享,您的支持将鼓励我继续创作!