1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //=============================================================================
20 // File : GHS3DPlugin_GHS3D.cxx
22 // Author : Edward AGAPOV, modified by Lioka RAZAFINDRAZAKA (CEA) 09/02/2007
24 // Copyright : CEA 2003
26 //=============================================================================
29 #include "GHS3DPlugin_GHS3D.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Mesh.hxx"
33 #include "SMDS_MeshElement.hxx"
34 #include "SMDS_MeshNode.hxx"
36 #include <TopExp_Explorer.hxx>
37 #include <OSD_File.hxx>
39 #include "utilities.h"
42 #include <sys/sysinfo.h>
45 //#include <Standard_Stream.hxx>
47 #include <BRepGProp.hxx>
48 #include <BRepBndLib.hxx>
49 #include <BRepClass_FaceClassifier.hxx>
50 #include <BRepClass3d_SolidClassifier.hxx>
52 #include <Bnd_Box.hxx>
53 #include <GProp_GProps.hxx>
54 #include <Precision.hxx>
73 //=============================================================================
77 //=============================================================================
79 GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D(int hypId, int studyId, SMESH_Gen* gen)
80 : SMESH_3D_Algo(hypId, studyId, gen)
82 MESSAGE("GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D");
84 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
89 //=============================================================================
93 //=============================================================================
95 GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D()
97 MESSAGE("GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D");
100 //=============================================================================
104 //=============================================================================
106 bool GHS3DPlugin_GHS3D::CheckHypothesis ( SMESH_Mesh& aMesh,
107 const TopoDS_Shape& aShape,
108 SMESH_Hypothesis::Hypothesis_Status& aStatus )
110 // MESSAGE("GHS3DPlugin_GHS3D::CheckHypothesis");
111 aStatus = SMESH_Hypothesis::HYP_OK;
115 //=======================================================================
116 //function : writeFaces
118 //=======================================================================
120 static bool writeFaces (ofstream & theFile,
121 SMESHDS_Mesh * theMesh,
122 const map <int,int> & theSmdsToGhs3dIdMap)
126 // NB_ELEMS DUMMY_INT
127 // Loop from 1 to NB_ELEMS
128 // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT
130 // get all faces bound to theShape
133 TopoDS_Shape theShape = theMesh->ShapeToMesh();
134 list< const SMDS_MeshElement* > faces;
135 TopExp_Explorer fExp( theShape, TopAbs_FACE );
137 SMDS_ElemIteratorPtr eIt;
139 const char* space = " ";
140 const int dummyint = 0;
142 list< const SMDS_MeshElement* >::iterator f;
143 map<int,int>::const_iterator it;
144 SMDS_ElemIteratorPtr nodeIt;
145 const SMDS_MeshElement* elem;
149 for ( ; fExp.More(); fExp.Next() ) {
150 sm = theMesh->MeshElements( fExp.Current() );
152 eIt = sm->GetElements();
153 while ( eIt->more() ) {
154 faces.push_back( eIt->next() );
163 cout << " " << nbFaces << " triangles" << endl;
166 // NB_ELEMS DUMMY_INT
167 theFile << space << nbFaces << space << dummyint << endl;
169 // Loop from 1 to NB_ELEMS
172 for ( ; f != faces.end(); ++f )
176 nbNodes = elem->NbNodes();
177 theFile << space << nbNodes;
179 // NODE_NB_1 NODE_NB_2 ...
180 nodeIt = elem->nodesIterator();
181 while ( nodeIt->more() )
184 aSmdsID = nodeIt->next()->GetID();
185 it = theSmdsToGhs3dIdMap.find( aSmdsID );
186 ASSERT( it != theSmdsToGhs3dIdMap.end() );
187 theFile << space << (*it).second;
190 // (NB_NODES + 1) times: DUMMY_INT
191 for ( int i=0; i<=nbNodes; i++)
192 theFile << space << dummyint;
200 //=======================================================================
201 //function : writePoints
203 //=======================================================================
205 static bool writePoints (ofstream & theFile,
206 SMESHDS_Mesh * theMesh,
207 map <int,int> & theSmdsToGhs3dIdMap,
208 map <int,const SMDS_MeshNode*> & theGhs3dIdToNodeMap)
213 // Loop from 1 to NB_NODES
216 int nbNodes = theMesh->NbNodes();
220 const char* space = " ";
221 const int dummyint = 0;
224 SMDS_NodeIteratorPtr it = theMesh->nodesIterator();
225 const SMDS_MeshNode* node;
228 theFile << space << nbNodes << endl;
229 cout << "The 2D mesh contains :" << endl;
230 cout << " " << nbNodes << " nodes" << endl;
232 // Loop from 1 to NB_NODES
237 theSmdsToGhs3dIdMap.insert( map <int,int>::value_type( node->GetID(), aGhs3dID ));
238 theGhs3dIdToNodeMap.insert (map <int,const SMDS_MeshNode*>::value_type( aGhs3dID, node ));
243 << space << node->X()
244 << space << node->Y()
245 << space << node->Z()
246 << space << dummyint;
254 //=======================================================================
255 //function : findSolid
257 //=======================================================================
259 static TopoDS_Shape findSolid(const SMDS_MeshNode *aNode[],
261 const TopoDS_Shape shape[],
262 const double box[][6],
265 Standard_Real PX, PY, PZ;
268 PX = ( aNode[0]->X() + aNode[1]->X() + aNode[2]->X() + aNode[3]->X() ) / 4.0;
269 PY = ( aNode[0]->Y() + aNode[1]->Y() + aNode[2]->Y() + aNode[3]->Y() ) / 4.0;
270 PZ = ( aNode[0]->Z() + aNode[1]->Z() + aNode[2]->Z() + aNode[3]->Z() ) / 4.0;
271 gp_Pnt aPnt(PX, PY, PZ);
273 BRepClass3d_SolidClassifier SC (aSolid, aPnt, Precision::Confusion());
274 if ( not(SC.State() == TopAbs_IN) ) {
275 for (iShape = 0; iShape < nShape; iShape++) {
276 aSolid = shape[iShape];
277 if ( not( PX < box[iShape][0] || box[iShape][1] < PX ||
278 PY < box[iShape][2] || box[iShape][3] < PY ||
279 PZ < box[iShape][4] || box[iShape][5] < PZ) ) {
280 BRepClass3d_SolidClassifier SC (aSolid, aPnt, Precision::Confusion());
281 if (SC.State() == TopAbs_IN)
289 //=======================================================================
290 //function : readMapIntLine
292 //=======================================================================
294 static char* readMapIntLine(char* ptr, int tab[]) {
300 while ( *ptr != *ptrRet ) {
301 intVal = strtol(ptr, &ptr, 10);
309 //=======================================================================
310 //function : readResultFile
312 //=======================================================================
314 static bool readResultFile(const int fileOpen,
315 SMESHDS_Mesh* theMeshDS,
316 TopoDS_Shape tabShape[],
319 map <int,const SMDS_MeshNode*>& theGhs3dIdToNodeMap) {
328 int fileStat, fileClose;
329 int nbElems, nbNodes, nbInputNodes;
330 int nodeId, triangleId;
331 int tab[3], tabID[nShape];
333 int ID, shapeID, ghs3dShapeID;
338 SMDS_MeshNode * aNewNode;
339 const SMDS_MeshNode * node[4];
340 map <int,const SMDS_MeshNode*>::iterator IdNode;
341 SMDS_MeshElement* aTet;
343 for (int i=0; i<nShape; i++)
346 // Read the file state
347 fileStat = fstat(fileOpen, &status);
348 length = status.st_size;
350 // Mapping the result file into memory
351 ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
352 fileClose = close(fileOpen);
354 ptr = readMapIntLine(ptr, tab);
359 nbInputNodes = tab[2];
361 // Reading the nodeId
362 for (int i=0; i < 4*nbElems; i++)
363 nodeId = strtol(ptr, &ptr, 10);
365 // Reading the nodeCoor and update the nodeMap
366 for (int iNode=0; iNode < nbNodes; iNode++) {
367 for (int iCoor=0; iCoor < 3; iCoor++)
368 coord[ iCoor ] = strtod(ptr, &ptr);
369 if ((iNode+1) > nbInputNodes) {
370 aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
371 theGhs3dIdToNodeMap.insert(make_pair( (iNode+1), aNewNode ));
375 // Reading the triangles
376 nbTriangle = strtol(ptr, &ptr, 10);
378 for (int i=0; i < 3*nbTriangle; i++)
379 triangleId = strtol(ptr, &ptr, 10);
383 // Associating the tetrahedrons with the shapes
384 for (int iElem = 0; iElem < nbElems; iElem++) {
385 for (int iNode = 0; iNode < 4; iNode++) {
386 ID = strtol(tetraPtr, &tetraPtr, 10);
387 IdNode = theGhs3dIdToNodeMap.find(ID);
388 node[ iNode ] = IdNode->second;
390 aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
391 ghs3dShapeID = strtol(shapePtr, &shapePtr, 10);
392 if ( tabID[ ghs3dShapeID - 1 ] == 0 ) {
394 aSolid = tabShape[0];
395 aSolid = findSolid(node, aSolid, tabShape, tabBox, nbTriangle);
396 shapeID = theMeshDS->ShapeToIndex( aSolid );
397 tabID[ ghs3dShapeID - 1] = shapeID;
400 shapeID = tabID[ ghs3dShapeID - 1];
401 theMeshDS->SetMeshElementOnShape( aTet, shapeID );
406 //=======================================================================
407 //function : getTmpDir
409 //=======================================================================
411 static TCollection_AsciiString getTmpDir()
413 TCollection_AsciiString aTmpDir;
415 char *Tmp_dir = getenv("SALOME_TMP_DIR");
416 if(Tmp_dir != NULL) {
419 if(aTmpDir.Value(aTmpDir.Length()) != '\\') aTmpDir+='\\';
421 if(aTmpDir.Value(aTmpDir.Length()) != '/') aTmpDir+='/';
426 aTmpDir = TCollection_AsciiString("C:\\");
428 aTmpDir = TCollection_AsciiString("/tmp/");
434 //=============================================================================
436 *Here we are going to use the GHS3D mesher
438 //=============================================================================
440 bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh,
441 const TopoDS_Shape& theShape)
444 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
446 if (_iShape == 0 && _nbShape == 0) {
448 cout << "Ghs3d execution..." << endl;
451 TopExp_Explorer exp (meshDS->ShapeToMesh(), TopAbs_SOLID);
452 for (; exp.More(); exp.Next())
458 if ( _iShape == _nbShape ) {
460 // create bounding box for every shape
463 TopoDS_Shape tabShape[_nbShape];
464 double tabBox[_nbShape][6];
465 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
467 TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID);
468 for (; expBox.More(); expBox.Next()) {
469 tabShape[iShape] = expBox.Current();
471 BRepBndLib::Add(expBox.Current(), BoundingBox);
472 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
473 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
474 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
475 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
479 // make a unique working file name
480 // to avoid access to the same files by eg different users
482 TCollection_AsciiString aGenericName, aTmpDir = getTmpDir();
483 aGenericName = aTmpDir + "GHS3D_";
485 aGenericName += GetCurrentProcessId();
487 aGenericName += getpid();
490 aGenericName += meshDS->ShapeToIndex( theShape );
492 TCollection_AsciiString aFacesFileName, aPointsFileName, aResultFileName;
493 TCollection_AsciiString aBadResFileName, aBbResFileName, aLogFileName;
494 aFacesFileName = aGenericName + ".faces"; // in faces
495 aPointsFileName = aGenericName + ".points"; // in points
496 aResultFileName = aGenericName + ".noboite";// out points and volumes
497 aBadResFileName = aGenericName + ".boite"; // out bad result
498 aBbResFileName = aGenericName + ".bb"; // out vertex stepsize
499 aLogFileName = aGenericName + ".log"; // log
505 ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out);
506 ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out);
510 aFacesFile->is_open() && aPointsFile->is_open();
512 aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
515 INFOS( "Can't write into " << aTmpDir.ToCString());
518 map <int,int> aSmdsToGhs3dIdMap;
519 map <int,const SMDS_MeshNode*> aGhs3dIdToNodeMap;
521 Ok = writePoints( aPointsFile, meshDS, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap ) &&
522 writeFaces ( aFacesFile, meshDS, aSmdsToGhs3dIdMap );
528 if ( !getenv("GHS3D_KEEP_FILES") ) {
529 OSD_File( aFacesFileName ).Remove();
530 OSD_File( aPointsFileName ).Remove();
536 // run ghs3d mesher WIN32???
539 // ghs3d need to know amount of memory it may use (MB).
540 // Default memory is defined at ghs3d installation but it may be not enough,
541 // so allow to use about all available memory
543 TCollection_AsciiString memory;
546 int err = sysinfo( &si );
548 int freeMem = si.totalram * si.mem_unit / 1024 / 1024;
550 memory += int( 0.7 * freeMem );
554 MESSAGE("GHS3DPlugin_GHS3D::Compute");
555 TCollection_AsciiString cmd( "ghs3d " ); // command to run
558 " -c0 -f " + aGenericName + // file to read
559 " 1>" + aLogFileName; // dump into file
561 system( cmd.ToCString() ); // run
564 cout << "End of Ghs3d execution !" << endl;
570 // Mapping the result file
573 fileOpen = open( aResultFileName.ToCString(), O_RDONLY);
574 if ( fileOpen < 0 ) {
576 cout << "Error when opening the " << aResultFileName.ToCString() << " file" << endl;
581 Ok = readResultFile( fileOpen, meshDS, tabShape, tabBox, _nbShape, aGhs3dIdToNodeMap );
583 // ---------------------
584 // remove working files
585 // ---------------------
588 OSD_File( aLogFileName ).Remove();
590 else if ( OSD_File( aLogFileName ).Size() > 0 ) {
591 INFOS( "GHS3D Error: see " << aLogFileName.ToCString() );
594 OSD_File( aLogFileName ).Remove();
595 INFOS( "GHS3D Error: command '" << cmd.ToCString() << "' failed" );
598 if ( !getenv("GHS3D_KEEP_FILES") ) {
599 OSD_File( aFacesFileName ).Remove();
600 OSD_File( aPointsFileName ).Remove();
601 OSD_File( aResultFileName ).Remove();
602 OSD_File( aBadResFileName ).Remove();
603 OSD_File( aBbResFileName ).Remove();
605 if ( _iShape == _nbShape ) {
606 cout << "Output file " << aResultFileName.ToCString();
608 cout << " not treated !" << endl;
610 cout << " treated !" << endl;
618 //=============================================================================
622 //=============================================================================
624 ostream & GHS3DPlugin_GHS3D::SaveTo(ostream & save)
629 //=============================================================================
633 //=============================================================================
635 istream & GHS3DPlugin_GHS3D::LoadFrom(istream & load)
640 //=============================================================================
644 //=============================================================================
646 ostream & operator << (ostream & save, GHS3DPlugin_GHS3D & hyp)
648 return hyp.SaveTo( save );
651 //=============================================================================
655 //=============================================================================
657 istream & operator >> (istream & load, GHS3DPlugin_GHS3D & hyp)
659 return hyp.LoadFrom( load );