Salome HOME
Join modifications from V3_2_0_maintainance (V3_2_6pre4 - T32x_16Aug2007_16h00m)
[plugins/ghs3dplugin.git] / src / GHS3DPlugin_GHS3D.cxx
index 478c2bdb3484e8df9d1efb848ff06a7e49e3d975..547149056d9230829c391c4244684cefd3fbd578 100644 (file)
@@ -29,6 +29,7 @@ using namespace std;
 #include "GHS3DPlugin_GHS3D.hxx"
 #include "SMESH_Gen.hxx"
 #include "SMESH_Mesh.hxx"
+#include "SMESH_Comment.hxx"
 
 #include "SMDS_MeshElement.hxx"
 #include "SMDS_MeshNode.hxx"
@@ -53,6 +54,8 @@ using namespace std;
 #include <GProp_GProps.hxx>
 #include <Precision.hxx>
 
+#define castToNode(n) static_cast<const SMDS_MeshNode *>( n );
+
 #ifdef _DEBUG_
 #define DUMP(txt) \
 //  cout << txt
@@ -60,8 +63,15 @@ using namespace std;
 #define DUMP(txt)
 #endif
 
-// include for mmap
-#include "HDFconvert.hxx"
+extern "C"
+{
+#ifndef WNT
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
+#include <sys/stat.h>
+#include <fcntl.h>
+}
 
 //=============================================================================
 /*!
@@ -75,8 +85,9 @@ GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D(int hypId, int studyId, SMESH_Gen* gen)
   MESSAGE("GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D");
   _name = "GHS3D_3D";
   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
-  _iShape=0;
-  _nbShape=0;
+  _onlyUnaryInput = false; // Compute() will be called on a compound of solids
+//   _iShape=0;
+//   _nbShape=0;
 }
 
 //=============================================================================
@@ -105,14 +116,16 @@ bool GHS3DPlugin_GHS3D::CheckHypothesis ( SMESH_Mesh&                          a
   return true;
 }
 
-//=======================================================================
-//function : writeFaces
-//purpose  : 
-//=======================================================================
+//================================================================================
+/*!
+ * \brief Write faces bounding theShape to file
+ */
+//================================================================================
 
-static bool writeFaces (ofstream &            theFile,
-                        SMESHDS_Mesh *        theMesh,
-                        const map <int,int> & theSmdsToGhs3dIdMap)
+static bool writeFaces (ofstream &                      theFile,
+                        SMESHDS_Mesh *                  theMesh,
+                        const TopoDS_Shape&             theShape,
+                        vector <const SMDS_MeshNode*> & theNodeByGhs3dId)
 {
   // record structure:
   //
@@ -122,10 +135,15 @@ static bool writeFaces (ofstream &            theFile,
 
   // get all faces bound to theShape
 
+  // Solids in the ShapeToMesh() can be meshed by different meshers,
+  // so we take faces only from the given shape
+  //TopoDS_Shape theShape = theMesh->ShapeToMesh();
   int nbFaces = 0;
-  TopoDS_Shape theShape = theMesh->ShapeToMesh();
   list< const SMDS_MeshElement* > faces;
-  TopExp_Explorer fExp( theShape, TopAbs_FACE );
+  // Use TopTools_IndexedMapOfShape in order not to take twice mesh faces from
+  // a geom face shared by two solids
+  TopTools_IndexedMapOfShape faceMap;
+  TopExp::MapShapes( theShape, TopAbs_FACE, faceMap );
   SMESHDS_SubMesh* sm;
   SMDS_ElemIteratorPtr eIt;
 
@@ -133,14 +151,14 @@ static bool writeFaces (ofstream &            theFile,
   const int   dummyint = 0;
 
   list< const SMDS_MeshElement* >::iterator f;
-  map<int,int>::const_iterator it;
+  map< const SMDS_MeshNode*,int >::iterator it;
   SMDS_ElemIteratorPtr nodeIt;
   const SMDS_MeshElement* elem;
   int nbNodes;
-  int aSmdsID;
+  //int aSmdsID;
 
-  for ( ; fExp.More(); fExp.Next() ) {
-    sm = theMesh->MeshElements( fExp.Current() );
+  for ( int i = 0; i < faceMap.Extent(); ++i ) {
+    sm = theMesh->MeshElements( faceMap( i+1 ) );
     if ( sm ) {
       eIt = sm->GetElements();
       while ( eIt->more() ) {
@@ -153,14 +171,14 @@ static bool writeFaces (ofstream &            theFile,
   if ( nbFaces == 0 )
     return false;
 
-  cout << "  " << nbFaces << " triangles" << endl;
-  cout << endl;
+  cout << "The initial 2D mesh contains " << nbFaces << " faces and ";
 
   // NB_ELEMS DUMMY_INT
   theFile << space << nbFaces << space << dummyint << endl;
 
   // Loop from 1 to NB_ELEMS
 
+  map<const SMDS_MeshNode*,int> aNodeToGhs3dIdMap;
   f = faces.begin();
   for ( ; f != faces.end(); ++f )
   {
@@ -174,10 +192,10 @@ static bool writeFaces (ofstream &            theFile,
     while ( nodeIt->more() )
     {
       // find GHS3D ID
-      aSmdsID = nodeIt->next()->GetID();
-      it = theSmdsToGhs3dIdMap.find( aSmdsID );
-      ASSERT( it != theSmdsToGhs3dIdMap.end() );
-      theFile << space << (*it).second;
+      const SMDS_MeshNode* node = castToNode( nodeIt->next() );
+      int newId = aNodeToGhs3dIdMap.size() + 1; // ghs3d ids count from 1
+      it = aNodeToGhs3dIdMap.insert( make_pair( node, newId )).first;
+      theFile << space << it->second;
     }
 
     // (NB_NODES + 1) times: DUMMY_INT
@@ -187,6 +205,14 @@ static bool writeFaces (ofstream &            theFile,
     theFile << endl;
   }
 
+  // put nodes to theNodeByGhs3dId vector
+  theNodeByGhs3dId.resize( aNodeToGhs3dIdMap.size() );
+  map<const SMDS_MeshNode*,int>::const_iterator n2id = aNodeToGhs3dIdMap.begin();
+  for ( ; n2id != aNodeToGhs3dIdMap.end(); ++ n2id)
+  {
+    theNodeByGhs3dId[ n2id->second - 1 ] = n2id->first; // ghs3d ids count from 1
+  }
+
   return true;
 }
 
@@ -195,10 +221,9 @@ static bool writeFaces (ofstream &            theFile,
 //purpose  : 
 //=======================================================================
 
-static bool writePoints (ofstream &                       theFile,
-                         SMESHDS_Mesh *                   theMesh,
-                         map <int,int> &                  theSmdsToGhs3dIdMap,
-                         map <int,const SMDS_MeshNode*> & theGhs3dIdToNodeMap)
+static bool writePoints (ofstream &                            theFile,
+                         SMESHDS_Mesh *                        theMesh,
+                         const vector <const SMDS_MeshNode*> & theNodeByGhs3dId)
 {
   // record structure:
   //
@@ -206,30 +231,27 @@ static bool writePoints (ofstream &                       theFile,
   // Loop from 1 to NB_NODES
   //   X Y Z DUMMY_INT
 
-  int nbNodes = theMesh->NbNodes();
+  //int nbNodes = theMesh->NbNodes();
+  int nbNodes = theNodeByGhs3dId.size();
   if ( nbNodes == 0 )
     return false;
 
   const char* space    = "  ";
   const int   dummyint = 0;
 
-  int aGhs3dID = 1;
-  SMDS_NodeIteratorPtr it = theMesh->nodesIterator();
   const SMDS_MeshNode* node;
 
   // NB_NODES
   theFile << space << nbNodes << endl;
-  cout << "The 2D mesh contains :" << endl;
-  cout << "  " << nbNodes << " nodes" << endl;
+  cout << nbNodes << " nodes" << endl;
 
   // Loop from 1 to NB_NODES
 
-  while ( it->more() )
+  vector<const SMDS_MeshNode*>::const_iterator nodeIt = theNodeByGhs3dId.begin();
+  vector<const SMDS_MeshNode*>::const_iterator after  = theNodeByGhs3dId.end();
+  for ( ; nodeIt != after; ++nodeIt )
   {
-    node = it->next();
-    theSmdsToGhs3dIdMap.insert( map <int,int>::value_type( node->GetID(), aGhs3dID ));
-    theGhs3dIdToNodeMap.insert (map <int,const SMDS_MeshNode*>::value_type( aGhs3dID, node ));
-    aGhs3dID++;
+    node = *nodeIt;
 
     // X Y Z DUMMY_INT
     theFile
@@ -285,47 +307,55 @@ static TopoDS_Shape findSolid(const SMDS_MeshNode *aNode[],
 //=======================================================================
 
 static char* readMapIntLine(char* ptr, int tab[]) {
-  char* ptrRet = "\n";
   long int intVal;
-  int i = 1;
   cout << endl;
 
-  while ( *ptr != *ptrRet ) {
+  for ( int i=0; i<17; i++ ) {
     intVal = strtol(ptr, &ptr, 10);
-    if ( i < 4 )
-      tab[i-1] = intVal;
-    i++;
+    if ( i < 3 )
+      tab[i] = intVal;
   }
   return ptr;
 }
 
+//=======================================================================
+//function : readLine
+//purpose  : 
+//=======================================================================
+
+#define GHS3DPlugin_BUFLENGTH 256
+#define GHS3DPlugin_ReadLine(aPtr,aBuf,aFile,aLineNb) \
+{  aPtr = fgets( aBuf, GHS3DPlugin_BUFLENGTH - 2, aFile ); aLineNb++; DUMP(endl); }
+
+#include <list>
 //=======================================================================
 //function : readResultFile
 //purpose  : 
 //=======================================================================
 
-static bool readResultFile(const int                       fileOpen,
-                           SMESHDS_Mesh*                   theMeshDS,
-                           TopoDS_Shape                    tabShape[],
-                           double                          tabBox[][6],
-                           const int                       nShape,
-                           map <int,const SMDS_MeshNode*>& theGhs3dIdToNodeMap) {
+static bool readResultFile(const int                      fileOpen,
+                           SMESHDS_Mesh*                  theMeshDS,
+                           TopoDS_Shape                   tabShape[],
+                           double                         tabBox[][6],
+                           const int                      nShape,
+                           vector <const SMDS_MeshNode*>& theNodeByGhs3dId) {
 
   struct stat  status;
   size_t       length;
 
-  char *ptr;
+  char *ptr, *mapPtr;
   char *tetraPtr;
   char *shapePtr;
 
-  int fileStat, fileClose;
+  int fileStat;
   int nbElems, nbNodes, nbInputNodes;
   int nodeId, triangleId;
-  int tab[3], tabID[nShape];
+  int tab[3]/*, tabID[nShape]*/;
   int nbTriangle;
   int ID, shapeID, ghs3dShapeID;
 
   double coord [3];
+  vector< int > tabID (nShape, 0);
 
   TopoDS_Shape aSolid;
   SMDS_MeshNode * aNewNode;
@@ -333,16 +363,16 @@ static bool readResultFile(const int                       fileOpen,
   map <int,const SMDS_MeshNode*>::iterator IdNode;
   SMDS_MeshElement* aTet;
 
-  for (int i=0; i<nShape; i++)
-    tabID[i] = 0;
+//   for (int i=0; i<nShape; i++)
+//     tabID[i] = 0;
 
   // Read the file state
   fileStat = fstat(fileOpen, &status);
   length   = status.st_size;
-  
+
   // Mapping the result file into memory
   ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
-  fileClose = close(fileOpen);
+  mapPtr = ptr;
 
   ptr      = readMapIntLine(ptr, tab);
   tetraPtr = ptr;
@@ -351,6 +381,8 @@ static bool readResultFile(const int                       fileOpen,
   nbNodes      = tab[1];
   nbInputNodes = tab[2];
 
+  theNodeByGhs3dId.resize( nbNodes );
+
   // Reading the nodeId
   for (int i=0; i < 4*nbElems; i++)
     nodeId = strtol(ptr, &ptr, 10);
@@ -361,7 +393,7 @@ static bool readResultFile(const int                       fileOpen,
       coord[ iCoor ] = strtod(ptr, &ptr);
     if ((iNode+1) > nbInputNodes) {
       aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
-      theGhs3dIdToNodeMap.insert(make_pair( (iNode+1), aNewNode ));
+      theNodeByGhs3dId[ iNode ] = aNewNode;
     }
   }
 
@@ -373,26 +405,38 @@ static bool readResultFile(const int                       fileOpen,
 
   shapePtr = ptr;
 
-  // Associating the tetrahedrons with the shapes
+  // Associating the tetrahedrons to the shapes
   for (int iElem = 0; iElem < nbElems; iElem++) {
     for (int iNode = 0; iNode < 4; iNode++) {
       ID = strtol(tetraPtr, &tetraPtr, 10);
-      IdNode = theGhs3dIdToNodeMap.find(ID);
-      node[ iNode ] = IdNode->second;
+      node[ iNode ] = theNodeByGhs3dId[ ID-1 ];
     }
     aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
     ghs3dShapeID = strtol(shapePtr, &shapePtr, 10);
+    if ( !ghs3dShapeID ) ghs3dShapeID = 1;
     if ( tabID[ ghs3dShapeID - 1 ] == 0 ) {
       if (iElem == 0)
         aSolid = tabShape[0];
-      aSolid = findSolid(node, aSolid, tabShape, tabBox, nbTriangle);
+      aSolid = findSolid(node, aSolid, tabShape, tabBox, nShape /*nbTriangle*/);
       shapeID = theMeshDS->ShapeToIndex( aSolid );
       tabID[ ghs3dShapeID - 1] = shapeID;
     }
-    else
+    else {
       shapeID = tabID[ ghs3dShapeID - 1];
+    }
     theMeshDS->SetMeshElementOnShape( aTet, shapeID );
+    // set new nodes on to the shape
+    SMDS_ElemIteratorPtr nodeIt = aTet->nodesIterator();
+    while ( nodeIt->more() ) {
+      const SMDS_MeshNode * n = castToNode( nodeIt->next() );
+      if ( !n->GetPosition()->GetShapeId() )
+        theMeshDS->SetNodeInVolume( n, shapeID );
+    }
   }
+  if ( nbElems )
+    cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl;
+  munmap(mapPtr, length);
+  close(fileOpen);
   return true;
 }
 
@@ -424,6 +468,36 @@ static TCollection_AsciiString getTmpDir()
   return aTmpDir;
 }
 
+//================================================================================
+/*!
+ * \brief Look for a line containing a text in a file
+  * \retval bool - true if the line is found
+ */
+//================================================================================
+
+static bool findLineContaing(const TCollection_AsciiString& theText,
+                             const TCollection_AsciiString& theFile,
+                             TCollection_AsciiString &      theFoundLine)
+{
+  bool found = false;
+  if ( FILE * aFile = fopen( theFile.ToCString(), "r" ))
+  {
+    char * aPtr;
+    char aBuffer[ GHS3DPlugin_BUFLENGTH ];
+    int aLineNb = 0;
+    do {
+      GHS3DPlugin_ReadLine( aPtr, aBuffer, aFile, aLineNb );
+      if ( aPtr ) {
+        theFoundLine = aPtr;
+        found = theFoundLine.Search( theText ) >= 0;
+      }
+    } while ( aPtr && !found );
+
+    fclose( aFile );
+  }
+  return found;
+}
+
 //=============================================================================
 /*!
  *Here we are going to use the GHS3D mesher
@@ -433,22 +507,25 @@ static TCollection_AsciiString getTmpDir()
 bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh&         theMesh,
                                 const TopoDS_Shape& theShape)
 {
-  bool Ok;
+  // theShape is a compound of solids as _onlyUnaryInput = false
+  bool Ok(false);
   SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
 
-  if (_iShape == 0 && _nbShape == 0) {
+  int _nbShape = 0;
+  /*if (_iShape == 0 && _nbShape == 0)*/ {
     cout << endl;
     cout << "Ghs3d execution..." << endl;
     cout << endl;
-
-    TopExp_Explorer exp (meshDS->ShapeToMesh(), TopAbs_SOLID);
+    
+    //TopExp_Explorer exp (meshDS->ShapeToMesh(), TopAbs_SOLID);
+    TopExp_Explorer exp (theShape, TopAbs_SOLID);
     for (; exp.More(); exp.Next())
       _nbShape++;
   }
+  
+  //_iShape++;
 
-  _iShape++;
-
-  if ( _iShape == _nbShape ) {
+  /*if ( _iShape == _nbShape )*/ {
 
     // create bounding box for every shape
 
@@ -457,7 +534,7 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh&         theMesh,
     double tabBox[_nbShape][6];
     Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
 
-    TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
+    TopExp_Explorer expBox (theShape, TopAbs_SOLID);
     for (; expBox.More(); expBox.Next()) {
       tabShape[iShape] = expBox.Current();
       Bnd_Box BoundingBox;
@@ -504,15 +581,13 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh&         theMesh,
 #else
       aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
 #endif
-    if (!Ok) {
-      INFOS( "Can't write into " << aTmpDir.ToCString());
-      return false;
-    }
-    map <int,int> aSmdsToGhs3dIdMap;
-    map <int,const SMDS_MeshNode*> aGhs3dIdToNodeMap;
+    if (!Ok)
+       return error(SMESH_Comment("Can't write into ") << aTmpDir);
 
-    Ok = writePoints( aPointsFile, meshDS, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap ) &&
-         writeFaces ( aFacesFile,  meshDS, aSmdsToGhs3dIdMap );
+    vector <const SMDS_MeshNode*> aNodeByGhs3dId;
+
+    Ok = ( writeFaces ( aFacesFile,  meshDS, theShape, aNodeByGhs3dId ) &&
+           writePoints( aPointsFile, meshDS, aNodeByGhs3dId ));
 
     aFacesFile.close();
     aPointsFile.close();
@@ -522,7 +597,7 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh&         theMesh,
         OSD_File( aFacesFileName ).Remove();
         OSD_File( aPointsFileName ).Remove();
       }
-      return false;
+      return error(COMPERR_BAD_INPUT_MESH);
     }
 
     // -----------------
@@ -534,23 +609,28 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh&         theMesh,
     // so allow to use about all available memory
 
     TCollection_AsciiString memory;
-#ifndef WIN32
+#ifdef WIN32
+    // ????
+#else
     struct sysinfo si;
     int err = sysinfo( &si );
-    if ( err == 0 ) {
+    if ( !err ) {
       int freeMem = si.totalram * si.mem_unit / 1024 / 1024;
       memory = "-m ";
       memory += int( 0.7 * freeMem );
     }
 #endif
 
-    MESSAGE("GHS3DPlugin_GHS3D::Compute");
+    OSD_File( aResultFileName ).Remove(); // old file prevents writing a new one
+
     TCollection_AsciiString cmd( "ghs3d " ); // command to run
     cmd +=
-      memory +                     // memory
-      " -c0 -f " + aGenericName +  // file to read
-           " 1>" + aLogFileName;   // dump into file
+      memory +                // memory
+      " -c 0"                 // 0 - mesh all components, 1 - keep only the main component
+      " -f " + aGenericName + // file to read
+      " 1>" + aLogFileName;   // dump into file
 
+    MESSAGE("GHS3DPlugin_GHS3D::Compute() " << cmd );
     system( cmd.ToCString() ); // run
 
     cout << endl;
@@ -570,84 +650,67 @@ bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh&         theMesh,
       cout << endl;
       Ok = false;
     }
-    else
-      Ok = readResultFile( fileOpen, meshDS, tabShape, tabBox, _nbShape, aGhs3dIdToNodeMap );
+    else {
+      Ok = readResultFile( fileOpen, meshDS, tabShape, tabBox, _nbShape, aNodeByGhs3dId );
+    }
 
-    // ---------------------
-    // remove working files
-    // ---------------------
+  // ---------------------
+  // remove working files
+  // ---------------------
 
-    if ( Ok ) {
+    if ( Ok )
+    {
       OSD_File( aLogFileName ).Remove();
     }
-    else if ( OSD_File( aLogFileName ).Size() > 0 ) {
-      INFOS( "GHS3D Error: see " << aLogFileName.ToCString() );
+    else if ( OSD_File( aLogFileName ).Size() > 0 )
+    {
+      // get problem description from the log file
+      SMESH_Comment comment;
+      TCollection_AsciiString foundLine;
+      if ( findLineContaing( "has expired",aLogFileName,foundLine) &&
+           foundLine.Search("Licence") >= 0)
+      {
+        foundLine.LeftAdjust();
+        comment << foundLine;
+      }
+      if ( findLineContaing( "%% ERROR",aLogFileName,foundLine))
+      {
+        foundLine.LeftAdjust();
+        comment << foundLine;
+      }
+      if ( findLineContaing( "%% NO SAVING OPERATION",aLogFileName,foundLine))
+      {
+        comment << "Too many elements generated for a trial version.\n";
+      }
+      if ( comment.empty() )
+        comment << "See " << aLogFileName << " for problem description";
+      else
+        comment << "See " << aLogFileName << " for more information";
+      error(COMPERR_ALGO_FAILED, comment);
     }
-    else {
+    else
+    {
+      // the log file is empty
       OSD_File( aLogFileName ).Remove();
-      INFOS( "GHS3D Error: command '" << cmd.ToCString() << "' failed" );
+      error(COMPERR_ALGO_FAILED, "ghs3d: command not found" );
     }
 
-    if ( !getenv("GHS3D_KEEP_FILES") ) {
+    if ( !getenv("GHS3D_KEEP_FILES") )
+    {
       OSD_File( aFacesFileName ).Remove();
       OSD_File( aPointsFileName ).Remove();
       OSD_File( aResultFileName ).Remove();
       OSD_File( aBadResFileName ).Remove();
       OSD_File( aBbResFileName ).Remove();
     }
-    if ( _iShape == _nbShape ) {
-      cout << "Output file " << aResultFileName.ToCString();
+    /*if ( _iShape == _nbShape )*/ {
+      cout << aResultFileName.ToCString() << " Output file ";
       if ( !Ok )
-        cout << " not treated !" << endl;
-      else
-        cout << " treated !" << endl;
+        cout << "not ";
+      cout << "treated !" << endl;
       cout << endl;
     }
   }
-  return Ok;
-}
-
-
-//=============================================================================
-/*!
- *  
- */
-//=============================================================================
-
-ostream & GHS3DPlugin_GHS3D::SaveTo(ostream & save)
-{
-  return save;
-}
-
-//=============================================================================
-/*!
- *  
- */
-//=============================================================================
-
-istream & GHS3DPlugin_GHS3D::LoadFrom(istream & load)
-{
-  return load;
-}
-
-//=============================================================================
-/*!
- *  
- */
-//=============================================================================
-
-ostream & operator << (ostream & save, GHS3DPlugin_GHS3D & hyp)
-{
-  return hyp.SaveTo( save );
-}
-
-//=============================================================================
-/*!
- *  
- */
-//=============================================================================
 
-istream & operator >> (istream & load, GHS3DPlugin_GHS3D & hyp)
-{
-  return hyp.LoadFrom( load );
+  return Ok;
 }