0021130: EDF 1746 SMESH: Issue with export in STL format

1) write not triangular faces as many triangles
   2) write triangles for missing faces of free facets of volumes
This commit is contained in:
eap 2011-01-27 16:48:44 +00:00
parent e3409934a4
commit d0b5d6090a
3 changed files with 172 additions and 65 deletions

View File

@ -24,22 +24,25 @@
#include "DriverSTL_W_SMDS_Mesh.h" #include "DriverSTL_W_SMDS_Mesh.h"
#include "SMDS_FaceOfNodes.hxx"
#include "SMDS_IteratorOnIterators.hxx"
#include "SMDS_Mesh.hxx" #include "SMDS_Mesh.hxx"
#include "SMDS_MeshElement.hxx" #include "SMDS_MeshElement.hxx"
#include "SMDS_MeshNode.hxx" #include "SMDS_MeshNode.hxx"
#include <gp_XYZ.hxx> #include "SMDS_SetIterator.hxx"
#include <OSD_Path.hxx> #include "SMDS_VolumeTool.hxx"
#include "SMESH_TypeDefs.hxx"
#include <OSD_File.hxx> #include <OSD_File.hxx>
#include <OSD_FromWhere.hxx> //#include <OSD_FromWhere.hxx>
#include <OSD_Path.hxx>
#include <OSD_Protection.hxx> #include <OSD_Protection.hxx>
#include <OSD_SingleProtection.hxx> //#include <OSD_SingleProtection.hxx>
#include <TCollection_AsciiString.hxx> #include <TCollection_AsciiString.hxx>
#include <TColgp_Array1OfXYZ.hxx> #include <gp_XYZ.hxx>
#include "utilities.h" #include "utilities.h"
//using namespace std;
// definition des constantes // definition des constantes
static const int LABEL_SIZE = 80; static const int LABEL_SIZE = 80;
@ -61,6 +64,7 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
fprintf(stderr, ">> ERROR : Mesh is null \n"); fprintf(stderr, ">> ERROR : Mesh is null \n");
return DRS_FAIL; return DRS_FAIL;
} }
findVolumeTriangles();
if ( myIsAscii ) if ( myIsAscii )
aResult = writeAscii(); aResult = writeAscii();
else else
@ -68,6 +72,68 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
return aResult; return aResult;
} }
//================================================================================
/*!
* \brief Destructor deletes temporary faces
*/
//================================================================================
DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh()
{
for ( unsigned i = 0; i < myVolumeTrias.size(); ++i )
delete myVolumeTrias[i];
}
//================================================================================
/*!
* \brief Finds free facets of volumes for which faces are missing in the mesh
*/
//================================================================================
void DriverSTL_W_SMDS_Mesh::findVolumeTriangles()
{
SMDS_VolumeTool myTool;
SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
while ( vIt->more() )
{
myTool.Set( vIt->next() );
for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
if ( myTool.IsFreeFace( iF ))
{
const SMDS_MeshNode** n = myTool.GetFaceNodes(iF);
int nbN = myTool.NbFaceNodes(iF);
std::vector< const SMDS_MeshNode*> nodes( n, n+nbN );
if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
{
int nbTria = nbN - 2;
for ( int iT = 0; iT < nbTria; ++iT )
{
myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[0], n[1+iT], n[2+iT] ));
}
}
}
}
}
//================================================================================
/*!
* \brief Return iterator on both faces in the mesh and on temporary faces
*/
//================================================================================
SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
{
SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeTrias.begin(),
myVolumeTrias.end()));
typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
TElemIterVector iters(2);
iters[0] = facesIter;
iters[1] = tmpTriaIter;
typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
return SMDS_ElemIteratorPtr( new TItersIter( iters ));
}
// static methods // static methods
@ -110,35 +176,45 @@ static void writeFloat ( const Standard_ShortReal& theVal,
ofile.Write((char *)&entier,sizeof(u.c)); ofile.Write((char *)&entier,sizeof(u.c));
} }
static gp_XYZ getNormale( const SMDS_MeshFace* theFace ) static gp_XYZ getNormale( const SMDS_MeshNode* n1,
const SMDS_MeshNode* n2,
const SMDS_MeshNode* n3)
{ {
gp_XYZ n; SMESH_TNodeXYZ xyz1( n1 );
int aNbNode = theFace->NbNodes(); SMESH_TNodeXYZ xyz2( n2 );
TColgp_Array1OfXYZ anArrOfXYZ(1,4); SMESH_TNodeXYZ xyz3( n3 );
gp_XYZ p1, p2, p3, p4; gp_XYZ q1 = xyz2 - xyz1;
SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator(); gp_XYZ q2 = xyz3 - xyz1;
int i = 1; gp_XYZ n = q1 ^ q2;
for ( ; aNodeItr->more() && i <= 4; i++ )
{
SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
}
gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
n = q1 ^ q2;
if ( aNbNode > 3 )
{
gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
n += q2 ^ q3;
}
double len = n.Modulus(); double len = n.Modulus();
if ( len > 0 ) if ( len > std::numeric_limits<double>::min() )
n /= len; n /= len;
return n; return n;
} }
//================================================================================
/*!
* \brief Decompose a mesh face into triangles
* \retval int - number of triangles
*/
//================================================================================
static int getTriangles( const SMDS_MeshElement* face,
const SMDS_MeshNode** nodes)
{
// WARNING: implementation must be coherent with counting triangles in writeBinary()
int nbN = face->NbCornerNodes();
const int nbTria = nbN-2;
for ( int i = 0; nbN > 1; --nbN )
{
nodes[ i++ ] = face->GetNode( 0 );
nodes[ i++ ] = face->GetNode( nbN-2 );
nodes[ i++ ] = face->GetNode( nbN-1 );
}
return nbTria;
}
// private methods // private methods
Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
@ -152,17 +228,24 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
OSD_File aFile = OSD_File(OSD_Path(aFileName)); OSD_File aFile = OSD_File(OSD_Path(aFileName));
aFile.Build(OSD_WriteOnly,OSD_Protection()); aFile.Build(OSD_WriteOnly,OSD_Protection());
char sval[16];
TCollection_AsciiString buf = TCollection_AsciiString ("solid\n"); TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
aFile.Write (buf,buf.Length());buf.Clear(); aFile.Write (buf,buf.Length());buf.Clear();
char sval[16];
SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); const SMDS_MeshNode* triaNodes[2048];
for (; itFaces->more() ;) { SMDS_ElemIteratorPtr itFaces = getFaces();
SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next(); while ( itFaces->more() )
{
const SMDS_MeshElement* aFace = itFaces->next();
int nbTria = getTriangles( aFace, triaNodes );
if (aFace->NbNodes() == 3) { for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
gp_XYZ normale = getNormale( aFace ); {
gp_XYZ normale = getNormale( triaNodes[iN],
triaNodes[iN+1],
triaNodes[iN+2] );
buf += " facet normal "; buf += " facet normal ";
sprintf (sval,"% 12e",normale.X()); sprintf (sval,"% 12e",normale.X());
@ -178,9 +261,8 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
buf += " outer loop\n"; buf += " outer loop\n";
aFile.Write (buf,buf.Length());buf.Clear(); aFile.Write (buf,buf.Length());buf.Clear();
SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator(); for ( int jN = 0; jN < 3; ++jN, ++iN ) {
for (; aNodeIter->more(); ) { const SMDS_MeshNode* node = triaNodes[iN];
SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
buf += " vertex "; buf += " vertex ";
sprintf (sval,"% 12e",node->X()); sprintf (sval,"% 12e",node->X());
buf += sval; buf += sval;
@ -219,38 +301,55 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
OSD_File aFile = OSD_File(OSD_Path(aFileName)); OSD_File aFile = OSD_File(OSD_Path(aFileName));
aFile.Build(OSD_WriteOnly,OSD_Protection()); aFile.Build(OSD_WriteOnly,OSD_Protection());
char sval[80];
Standard_Integer nbTri = 0;
SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
// we first count the number of triangles // we first count the number of triangles
for (;itFaces->more();) { // WARNING: counting triangles must be coherent with getTriangles()
SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next(); int nbTri = 0;
if (aFace->NbNodes() == 3) const SMDS_MeshInfo& info = myMesh->GetMeshInfo();
nbTri++; nbTri += info.NbTriangles();
nbTri += info.NbQuadrangles() * 2;
nbTri += myVolumeTrias.size();
if ( info.NbPolygons() > 0 )
{
SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
while ( itFaces->more() )
{
const SMDS_MeshElement* aFace = itFaces->next();
if ( aFace->IsPoly() )
nbTri += aFace->NbNodes() - 2;
}
} }
// char sval[80]; -- avoid writing not initialized memory
TCollection_AsciiString sval(LABEL_SIZE-1,' ');
aFile.Write((Standard_Address)sval.ToCString(),LABEL_SIZE);
// write number of triangles // write number of triangles
//unsigned int NBT = nbTri;
aFile.Write((Standard_Address)sval,LABEL_SIZE);
writeInteger(nbTri,aFile); writeInteger(nbTri,aFile);
// loop writing nodes. take face iterator again // Loop writing nodes
int dum=0; int dum=0;
itFaces = myMesh->facesIterator();
for (;itFaces->more();) { const SMDS_MeshNode* triaNodes[2048];
SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
if (aFace->NbNodes() == 3) { SMDS_ElemIteratorPtr itFaces = getFaces();
gp_XYZ aNorm = getNormale( aFace ); while ( itFaces->more() )
writeFloat(aNorm.X(),aFile); {
writeFloat(aNorm.Y(),aFile); const SMDS_MeshElement* aFace = itFaces->next();
writeFloat(aNorm.Z(),aFile); int nbTria = getTriangles( aFace, triaNodes );
SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator(); for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
for (; aNodeIter->more(); ) { {
SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next(); gp_XYZ normale = getNormale( triaNodes[iN],
triaNodes[iN+1],
triaNodes[iN+2] );
writeFloat(normale.X(),aFile);
writeFloat(normale.Y(),aFile);
writeFloat(normale.Z(),aFile);
for ( int jN = 0; jN < 3; ++jN, ++iN )
{
const SMDS_MeshNode* node = triaNodes[iN];
writeFloat(node->X(),aFile); writeFloat(node->X(),aFile);
writeFloat(node->Y(),aFile); writeFloat(node->Y(),aFile);
writeFloat(node->Z(),aFile); writeFloat(node->Z(),aFile);

View File

@ -30,13 +30,16 @@
#include "SMESH_DriverSTL.hxx" #include "SMESH_DriverSTL.hxx"
#include "Driver_SMDS_Mesh.h" #include "Driver_SMDS_Mesh.h"
#include <Standard_TypeDef.hxx> #include "SMDS_ElemIterator.hxx"
#include <vector>
class MESHDRIVERSTL_EXPORT DriverSTL_W_SMDS_Mesh: public Driver_SMDS_Mesh class MESHDRIVERSTL_EXPORT DriverSTL_W_SMDS_Mesh: public Driver_SMDS_Mesh
{ {
public: public:
DriverSTL_W_SMDS_Mesh(); DriverSTL_W_SMDS_Mesh();
~DriverSTL_W_SMDS_Mesh();
virtual Status Perform(); virtual Status Perform();
void SetIsAscii( const bool theIsAscii = false ); void SetIsAscii( const bool theIsAscii = false );
@ -44,10 +47,14 @@ class MESHDRIVERSTL_EXPORT DriverSTL_W_SMDS_Mesh: public Driver_SMDS_Mesh
// PRIVATE METHODS // PRIVATE METHODS
Status writeAscii () const; Status writeAscii () const;
Status writeBinary () const; Status writeBinary () const;
void findVolumeTriangles();
SMDS_ElemIteratorPtr getFaces() const;
private: private:
// PRIVATE FIELDS // PRIVATE FIELDS
bool myIsAscii; bool myIsAscii;
std::vector<const SMDS_MeshElement*> myVolumeTrias; // tmp triangles
}; };
#endif #endif

View File

@ -53,6 +53,7 @@ libMeshDriverSTL_la_CPPFLAGS = \
$(VTK_INCLUDES) \ $(VTK_INCLUDES) \
$(BOOST_CPPFLAGS) \ $(BOOST_CPPFLAGS) \
-I$(srcdir)/../Driver \ -I$(srcdir)/../Driver \
-I$(srcdir)/../SMESH \
-I$(srcdir)/../SMDS -I$(srcdir)/../SMDS
libMeshDriverSTL_la_LDFLAGS = \ libMeshDriverSTL_la_LDFLAGS = \