1

As well as for TopoDS_Vertex we can get the coordinates with:

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++;
}

I would like to know if there is a similar mechanism to get the coordinates of a TopoDS_Face.

EDIT

With the suggestion of @jaba, that's what I did to calculate the center of mass of a series of points:

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);
}
Bub Espinja
  • 4,029
  • 2
  • 29
  • 46
  • Next time please at least think about the title of your question before you post it. – jaba Mar 05 '19 at 13:16

2 Answers2

3

The Open CASCADE way of computing the center of mass of a TopoDS_Face is

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();

This should give the right result. If faces are not bound by convex polygons where the vertices of the face are the polygon points, this code should still work, but polygon approaches might fail.

Benjamin Bihler
  • 1,612
  • 11
  • 32
1

You need to get a vertex from the face as a face does not have one coordinate. What you do looks quite fine, I just added my snippet because your code is not self-contained.

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

If you want only one location you can maybe calculate the center from the vertices you receive (see Compute the Centroid of a 3D Planar Polygon)

If this is not what you want please specify your question.

jaba
  • 735
  • 7
  • 18