如何获取TopoDS_Face的重心坐标?

时间:2019-02-27 09:00:32

标签: c++ vtk opencascade

对于TopoDS_Vertex,我们还可以通过以下方式获取坐标:

int i=0;
exp0.Init(shape, TopAbs_VERTEX);
for(exp0.Init(shape, TopAbs_VERTEX); exp0.More(); exp0.Next()) {
    TopoDS_Vertex vertex = TopoDS::Vertex(exp0.Current());
    gp_Pnt pnt = BRep_Tool::Pnt(vertex);
    cout <<"Edge " << i << ": X: " << pnt.X() << " - Y:" << pnt.Y() << " - Z: " << pnt.Z();
    i++;
}

我想知道是否有类似的机制来获取TopoDS_Face的坐标。

编辑

在@jaba的建议下,这就是我计算一系列点的质心的方法:

vtkSmartPointer<vtkPoints> facePoints = vtkSmartPointer<vtkPoints>::New();
int i=0;
for(exp0.Init(shape, TopAbs_FACE); exp0.More(); exp0.Next(), i++, count++) {
    facePoints->Reset();
    TopoDS_Face aFace = TopoDS::Face(exp0.Current());
    for (TopExp_Explorer Vex(aFace, TopAbs_VERTEX); Vex.More(); Vex.Next()) {
        TopoDS_Vertex vertex = TopoDS::Vertex(Vex.Current());
        gp_Pnt pnt = BRep_Tool::Pnt(vertex);
        facePoints->InsertNextPoint(p);
    }
    vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(facePoints);

    // Compute the center of mass
    vtkSmartPointer<vtkCenterOfMass> centerOfMassFilter = vtkSmartPointer<vtkCenterOfMass>::New();
    centerOfMassFilter->SetInputData(polydata);
    centerOfMassFilter->SetUseScalarsAsWeights(false);
    centerOfMassFilter->Update();
    double center[3];
    centerOfMassFilter->GetCenter(center);
}

2 个答案:

答案 0 :(得分:3)

计算TopoDS_Face质心的Open CASCADE方法是

TopoDS_Face face = ...;

GProp_GProps shellProps;
BRepGProp::SurfaceProperties(face, shellProps);

if (shellProps.Mass() < Precision::Confusion())
{
    throw Exception("Failed to Calculate the area of the face.", __TRACE__);
}

const gp_Pnt centerOfMass = shellProps.CentreOfMass();

这应该给出正确的结果。如果人脸没有被以人的顶点为多边形点的凸多边形所限制,则此代码仍应起作用,但是多边形方法可能会失败。

答案 1 :(得分:1)

您需要从脸部获得一个顶点,因为脸部没有一个坐标。 您所做的工作看起来还不错,我只是添加了代码段,因为您的代码不是独立的。

for (TopExp_Explorer vertexEx(face, TopAbs_VERTEX); vertexEx.More(); vertexEx.Next())
{
    TopoDS_Vertex vertex = TopoDS::Vertex(vertexEx.Current());
    gp_Pnt currentPnt = BRep_Tool::Pnt(vertex);
}

如果只想要一个位置,则可以根据接收到的顶点计算中心(请参见Compute the Centroid of a 3D Planar Polygon

如果这不是您想要的,请指定您的问题。