Open CASCADE, the 3D modelling kernel
3D modeling & numerical simulation

Search the Forums
See All Topics

Consistency of TopoDS_Face normals

Consistency of TopoDS_Face normals
Thomas Krijnen 2012/07/29 18:17
I am importing large amounts of data into Open Cascade. I can not seem to figure out what the way Open Cascade assigns the orientation of normals to TopoDS_Faces that are constructed from a closed wire.

In the following example two faces are constructed from points in a CounterClockWise order, but are assigned normals pointing in opposite directions. Does the assignment of normals adheres to any convention or rationale? Or is there any way to make the normals consistent in relation to each other?

This is the way the points are located:

p36641 --- p36640 --- p36639
| | |
p36609 --- p36608 --- p36607

These are the normals that get assigned:


Thanks for your help.

Kind regards,

PS: This is the code to reproduce my problem:

#include <stdio.h>

#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>

#include <TopoDS_Wire.hxx>
#include <TopoDS_Face.hxx>

#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>

#include <BRepGProp_Face.hxx>

#include <BRepMesh.hxx>

#include <TopAbs_Orientation.hxx>

void create_loop(const gp_Pnt& p1, const gp_Pnt& p2, const gp_Pnt& p3, const gp_Pnt& p4, TopoDS_Wire& wire) {
BRepBuilderAPI_MakeWire w;
wire = w.Wire();

void face_normal(const TopoDS_Face& face) {
BRepGProp_Face prop(face);
double u1,u2,v1,v2;
double u = (u1+u2)/2;
double v = (v1+v2)/2;
gp_Vec vec;
gp_Pnt pnt;
char buff[64];
std::cout << buff << std::endl;

int main(int argc, char** argv)
// The imported information is scaled, hence the scale factor f
// Even changing this factor has an influence on the orientation of the normals.
const float f = 0.3048;
gp_Pnt p36607(-3.234400499461643*f,1.316409965570497*f,7.333333333333272*f);
gp_Pnt p36608(-3.595952453545237*f,1.316409965570497*f,7.313029011113542*f);
gp_Pnt p36609(-3.952957682028909*f,1.316409965570496*f,7.252371383087095*f);
gp_Pnt p36639(-3.234400499461643*f,2.33333333333334*f ,7.333333333333272*f);
gp_Pnt p36640(-3.595952453545237*f,2.333333333333339*f,7.313029011113542*f);
gp_Pnt p36641(-3.952957682028909*f,2.333333333333339*f,7.252371383087095*f);

TopoDS_Wire w36733;
TopoDS_Wire w36766;

TopoDS_Face f36735 = BRepBuilderAPI_MakeFace(w36733);
TopoDS_Face f36768 = BRepBuilderAPI_MakeFace(w36766);


TopAbs_Orientation f36735_orientation = f36735.Orientation();
TopAbs_Orientation f36768_orientation = f36768.Orientation();

Thomas Krijnen 2012/08/11 17:30
Eventually I ended up writing a routine that flips the face orientation if the face normal does not align with a normal vector computed with Newell's method.
I still feel I am doing something wrong or there must be another way. Doesn't anybody have an idea? Thanks,

Kind regards,

Here's the code:

     // Check the orientation of the face by comparing the
     // normal of the topological surface to the Newell's Method's
     // normal. Newell's Method is used for the normal calculation
     // as a simple edge cross product can give opposite results
     // for a concave face boundary.
     // Reference: Graphics Gems III p. 231
     BRepGProp_Face prop(face);
     gp_Vec normal_direction;
     gp_Pnt center;
     double u1,u2,v1,v2;
     gp_Dir face_normal1 = gp_Dir(normal_direction.XYZ());

     double x = 0, y = 0, z = 0;
     gp_Pnt current, previous, first;
     int n = 0;
     // Iterate over the vertices of the outer wire (discarding
     // any potential holes)
     for ( TopExp_Explorer exp(outer_wire,TopAbs_VERTEX);; exp.Next()) {
          unsigned has_more = exp.More();
          if ( has_more ) {
               const TopoDS_Vertex& v = TopoDS::Vertex(exp.Current());
               current = BRep_Tool::Pnt(v);
          } else {
               current = first;               
          if ( n ) {
               const double& xn = previous.X();
               const double& yn = previous.Y();
               const double& zn = previous.Z();
               const double& xn1 = current.X();
               const double& yn1 = current.Y();
               const double& zn1 = current.Z();
               x += (yn-yn1)*(zn+zn1);
               y += (xn+xn1)*(zn-zn1);
               z += (xn-xn1)*(yn+yn1);
          } else {
               first = current;
          if ( !has_more ) {
          previous = current;

     // If Newell's normal does not point in the same direction
     // as the topological face normal the face orientation is
     // reversed
     gp_Vec face_normal2(x,y,z);
     if ( face_normal1.Dot(face_normal2) < 0 ) {
          TopAbs_Orientation o = face.Orientation();
          face.Orientation(o == TopAbs_FORWARD ? TopAbs_REVERSED : TopAbs_FORWARD);
jelle 2012/08/13 14:39
interesting contribution, thanks for sharing Thomas!
Ling 2013/01/30 06:22
hello , i am a student. and i have a task that iges/step to stl.

when iges to stl , i find that some face's normal is reversed, and the problem i think is flow:
face.Orientation() is always TopAbs_FORWARD, WHy? What's wrong with this?

but , when step to stl , it's fine. the face normal is right.
can you give me some advise ,,thank you very much.

this is my code:
TopoDS_Shape shape = Reader.OneShape();
                         double const deflection = 0.5;
                         double const angulardeflection = 0.5;
                         BRepMesh_IncrementalMesh discr(shape, deflection, false, angulardeflection); //shape or face

                         TopoDS_Face face;
                         for (TopExp_Explorer ex(shape, TopAbs_FACE); ex.More(); ex.Next())
                              TopoDS_Face face = TopoDS::Face(ex.Current());                                                                                          
                              TopLoc_Location loc;
                              BRepAdaptor_Surface sf(face, Standard_False);
                              BRepLProp_SLProps prop(sf, 1, 1e-5);

                              Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation (face, loc);

                              //Handle(Poly_Triangulation) triangulation1 = BRepTools::Triangulation(face, 0.01);

                              if (!triangulation.IsNull())
                                   gp_Pnt2d uv;
                                   gp_Pnt pnt;
                                   gp_Vec nn;

                                   int ntriangles = triangulation -> NbTriangles();
                                   int npoints = triangulation->NbNodes();

                                   const TColgp_Array1OfPnt& aNodes = triangulation->Nodes();
                                   int num = aNodes.Length();

                                   TColgp_Array1OfPnt aPoints(1, aNodes.Length());
                                   for( Standard_Integer i = 1; i < aNodes.Length()+1; i++)
                                        aPoints(i) = aNodes(i).Transformed(loc);

                                        mesh.add_vertex( TNOMS3D::OPoint( aPoints(i).X(), aPoints(i).Y(), aPoints(i).Z() ) );

                                   const Poly_Array1OfTriangle& triangles =triangulation ->Triangles();

                                   Standard_Integer v1,v2,v3;
                                   for (int j = 1; j <= ntriangles; j++)
                                        if (face.Orientation() == TopAbs_REVERSED)
                                             mesh.add_face( OpenMesh::VertexHandle(index+v1-1), OpenMesh::VertexHandle(index+v2-1), OpenMesh::VertexHandle(index+v3-1));
                                             mesh.add_face( OpenMesh::VertexHandle(index+v1-1), OpenMesh::VertexHandle(index+v2-1), OpenMesh::VertexHandle(index+v3-1));
                                        //mesh.add_face( OpenMesh::VertexHandle(v1-1), OpenMesh::VertexHandle(v2-1), OpenMesh::VertexHandle(v3-1));
                                        Poly_Triangle triangle = (triangulation -> Triangles())(j);
                                        //gp_Vec nn[3];
                                        for(int k=1; k<=3; k++)
                                             uv = (triangulation -> UVNodes())(triangle(k));
                     prop.SetParameters (uv.X(), uv.Y());

                                             if (prop.IsNormalDefined())
                                                  nn = prop.Normal();
                                                  //           n = gp_Vec (0,0,0);
                                                  gp_Vec a(aPoints(v1),aPoints(v2));
                                                  gp_Vec b(aPoints(v1),aPoints(v3));
                                                  nn = b^a;
                                             if (face.Orientation() == TopAbs_REVERSED) nn *= -1;

                                             if( k==1 )
                                                  mesh.set_normal( OpenMesh::VertexHandle(index+v1-1),TNOMS3D::ONormal(nn.X(),nn.Y(),nn.Z()) );
                                             if( k==2 )
                                                  mesh.set_normal( OpenMesh::VertexHandle(index+v2-1),TNOMS3D::ONormal(nn.X(),nn.Y(),nn.Z()) );
                                             if( k==3 )
                                                  mesh.set_normal( OpenMesh::VertexHandle(index+v3-1),TNOMS3D::ONormal(nn.X(),nn.Y(),nn.Z()) );

                                   index = index+npoints;

xiaoxiong 2013/01/21 10:00
Ling 2013/01/23 11:24
     // Iterate over the vertices of the outer wire (discarding
// any potential holes)
for ( TopExp_Explorer exp(outer_wire,TopAbs_VERTEX);; exp.Next()) {

i want to ask:
what is the outer_wire, how can i get the outer.
Junjie Xue 2014/02/26 16:41
Ling | reply     2013/01/23 11:24
// Iterate over the vertices of the outer wire (discarding
// any potential holes)
for ( TopExp_Explorer exp(outer_wire,TopAbs_VERTEX);; exp.Next()) {

i want to ask:
what is the outer_wire, how can i get the outer.

Thomas Krijnen 2014/03/03 23:28

Sorry for the late reply. The code posted above is a snippet from the IfcOpenShell source code ( In this case the TopoDS_Wire outer_wire is used to construct the face. But I can also imagine you can use TopExp_Explorer and TopAbs_WIRE to iterate over the wires and find the wire you are interested in.

Hope that helps,

Kind regards,
Latest news
  • Open CASCADE CAD Assistant for iOS platform is available!
  • Open CASCADE Technology 6.9.0 is available for download!
  • JT Assistant for Android

  • © OPEN CASCADE 2000 - 2015  |  Search  |  Contacts   |  Site map