Hello! Hennig
there is another question, I hope you can help me, Thanks!
The question is:
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,
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();
shape.Modified();
//shape.Checked();
double const deflection = 0.5;
double const angulardeflection = 0.5;
BRepTools::Clean(shape);
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)
{
triangles(j).Get(v1,v3,v2);
mesh.add_face( OpenMesh::VertexHandle(index+v1-1), OpenMesh::VertexHandle(index+v2-1), OpenMesh::VertexHandle(index+v3-1));
}
else
{
triangles(j).Get(v1,v2,v3);
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();
else
{
// 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;
}
}