Salome HOME
Update copyrights in the documentation.
[modules/smesh.git] / src / DriverSTL / DriverSTL_W_SMDS_Mesh.cxx
index cd33b025338f4f5f47d92dbee97d3fa09b0f3d3f..333c72d96af80d11dfb031cad24825458e0aecba 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -176,7 +176,7 @@ static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile)
 {
   union {
     Standard_ShortReal f;
-    char c[4]; 
+    char c[4];
   } u;
 
   u.f = theVal;
@@ -252,9 +252,10 @@ namespace
       gp_XY p = _prev->_xy - v->_xy;
       gp_XY t =  this->_xy - v->_xy;
       gp_XY n = _next->_xy - v->_xy;
-      return (( p ^ t ) > 0 &&
-              ( t ^ n ) > 0 &&
-              ( n ^ p ) > 0 );
+      const double tol = -1e-12;
+      return (( p ^ t ) >= tol &&
+              ( t ^ n ) >= tol &&
+              ( n ^ p ) >= tol );
       // return ( Area( _prev, this, v ) > 0 &&
       //          Area( this, _next, v ) > 0 &&
       //          Area( _next, _prev, v ) > 0 );
@@ -338,7 +339,42 @@ namespace
         ++nbBadTria;
       }
     }
-    return false;
+
+    // the polygon is invalid; add triangles with positive area
+    nbBadTria = 0;
+    while ( nbBadTria < nbVertices )
+    {
+      isGoodTria = v->TriaArea() > minArea;
+      if ( isGoodTria )
+      {
+        v->GetTriaNodes( &nodes[ iN ] );
+        iN += 3;
+        v = v->Delete();
+        if ( --nbVertices == 3 )
+        {
+          // last triangle remains
+          v->GetTriaNodes( &nodes[ iN ] );
+          return true;
+        }
+        nbBadTria = 0;
+      }
+      else
+      {
+        v = v->_next;
+        ++nbBadTria;
+      }
+    }
+
+    // add all the rest triangles
+    while ( nbVertices >= 3 )
+    {
+      v->GetTriaNodes( &nodes[ iN ] );
+      iN += 3;
+      v = v->Delete();
+      --nbVertices;
+    }
+
+    return true;
 
   } // triangulate()
 } // namespace
@@ -394,15 +430,15 @@ static int getTriangles( const SMDS_MeshElement*             face,
   case SMDSEntity_BiQuad_Quadrangle:
     nbTria = ( type == SMDSEntity_BiQuad_Triangle ) ? 6 : 8;
     nodes[ i++ ] = face->GetNode( nbTria );
-    while ( i < 3*(nbTria-1) )
+    for ( i = 3; i < 3*(nbTria-1); i += 3 )
     {
-      nodes[ i++ ] = nodes[ i-2 ];
-      nodes[ i++ ] = nIt->next();
-      nodes[ i++ ] = nodes[ 2 ];
+      nodes[ i+0 ] = nodes[ i-2 ];
+      nodes[ i+1 ] = nIt->next();
+      nodes[ i+2 ] = nodes[ 2 ];
     }
-    nodes[ i++ ] = nodes[ i-2 ];
-    nodes[ i++ ] = nodes[ 0 ];
-    nodes[ i++ ] = nodes[ 2 ];
+    nodes[ i+0 ] = nodes[ i-2 ];
+    nodes[ i+1 ] = nodes[ 0 ];
+    nodes[ i+2 ] = nodes[ 2 ];
     break;
   case SMDSEntity_Triangle:
     nbTria = 1;
@@ -449,7 +485,8 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
   SMESH_File aFile( myFile, /*openForReading=*/false );
   aFile.openForWriting();
 
-  std::string buf("solid\n");
+  std::string buf("solid ");
+  buf += myName + "\n";
   aFile.writeRaw( buf.c_str(), buf.size() );
 
   char sval[128];
@@ -484,7 +521,8 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
                       " endfacet\n", 21 );
     }
   }
-  aFile.writeRaw ("endsolid\n" , 9 );
+  buf = "endsolid " + myName + "\n";
+  aFile.writeRaw( buf.c_str(), buf.size() );
 
   return aResult;
 }
@@ -518,6 +556,11 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
     }
   }
   std::string sval( LABEL_SIZE, ' ' );
+  if ( !myName.empty() )
+  {
+    sval = "name: " + myName;
+    sval.resize( LABEL_SIZE, ' ' );
+  }
   aFile.write( sval.c_str(), LABEL_SIZE );
 
   // write number of triangles