vtkPolydata中取点的坑

来源:互联网 发布:淘宝详情页好做吗 编辑:程序博客网 时间:2024/06/04 17:57
    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;        }    }

输出结果:

P1 : 131 111,177,121P2 : 130 111,177,121P1 : 130 112,178,120P2 : 129 112,178,120P1 : 129 112,178,119P2 : 128 112,178,119P1 : 128 112,177,118P2 : 127 112,177,118P1 : 127 112,176,117P2 : 126 112,176,117P1 : 126 111,175,116...

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

  // 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); }

更新程序使用

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

目标达成。