1 // Copyright (C) 2004-2008 CEA/DEN, EDF 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
25 //=============================================================================
27 #include "GHS3DPlugin_GHS3D.hxx"
28 #include "GHS3DPlugin_Hypothesis.hxx"
31 #include <Basics_Utils.hxx>
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_Comment.hxx"
36 #include "SMESH_MesherHelper.hxx"
37 #include "SMESH_MeshEditor.hxx"
39 #include "SMDS_MeshElement.hxx"
40 #include "SMDS_MeshNode.hxx"
41 #include "SMDS_FaceOfNodes.hxx"
42 #include "SMDS_VolumeOfNodes.hxx"
44 #include <BRepAdaptor_Surface.hxx>
45 #include <BRepBndLib.hxx>
46 #include <BRepClass3d_SolidClassifier.hxx>
47 #include <BRepTools.hxx>
48 #include <BRep_Tool.hxx>
49 #include <Bnd_Box.hxx>
50 #include <GeomAPI_ProjectPointOnSurf.hxx>
51 #include <OSD_File.hxx>
52 #include <Precision.hxx>
53 #include <Quantity_Parameter.hxx>
54 #include <Standard_ProgramError.hxx>
55 #include <Standard_ErrorHandler.hxx>
56 #include <Standard_Failure.hxx>
58 #include <TopExp_Explorer.hxx>
59 #include <TopTools_IndexedMapOfShape.hxx>
60 #include <TopTools_ListIteratorOfListOfShape.hxx>
62 //#include <BRepClass_FaceClassifier.hxx>
63 #include <TopTools_MapOfShape.hxx>
64 #include <BRepGProp.hxx>
65 #include <GProp_GProps.hxx>
67 #include "utilities.h"
72 #include <sys/sysinfo.h>
77 //#include <Standard_Stream.hxx>
80 #define castToNode(n) static_cast<const SMDS_MeshNode *>( n );
101 static void removeFile( const TCollection_AsciiString& fileName )
104 OSD_File( fileName ).Remove();
106 catch ( Standard_ProgramError ) {
107 MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
111 //=============================================================================
115 //=============================================================================
117 GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D(int hypId, int studyId, SMESH_Gen* gen)
118 : SMESH_3D_Algo(hypId, studyId, gen)
120 MESSAGE("GHS3DPlugin_GHS3D::GHS3DPlugin_GHS3D");
122 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
123 _onlyUnaryInput = false; // Compute() will be called on a compound of solids
126 _compatibleHypothesis.push_back("GHS3D_Parameters");
127 _requireShape = false; // can work without shape
130 //=============================================================================
134 //=============================================================================
136 GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D()
138 MESSAGE("GHS3DPlugin_GHS3D::~GHS3DPlugin_GHS3D");
141 //=============================================================================
145 //=============================================================================
147 bool GHS3DPlugin_GHS3D::CheckHypothesis ( SMESH_Mesh& aMesh,
148 const TopoDS_Shape& aShape,
149 Hypothesis_Status& aStatus )
151 aStatus = SMESH_Hypothesis::HYP_OK;
153 // there is only one compatible Hypothesis so far
156 const list <const SMESHDS_Hypothesis * >& hyps = GetUsedHypothesis(aMesh, aShape);
158 _hyp = static_cast<const GHS3DPlugin_Hypothesis*> ( hyps.front() );
160 _keepFiles = _hyp->GetKeepFiles();
165 //=======================================================================
166 //function : findShape
168 //=======================================================================
170 static TopoDS_Shape findShape(const SMDS_MeshNode *aNode[],
172 const TopoDS_Shape shape[],
175 TopAbs_State * state = 0)
178 int j, iShape, nbNode = 4;
180 for ( j=0; j<nbNode; j++ ) {
181 gp_XYZ p ( aNode[j]->X(), aNode[j]->Y(), aNode[j]->Z() );
182 if ( aNode[j]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE ) {
189 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
190 if (state) *state = SC.State();
191 if ( SC.State() != TopAbs_IN || aShape.IsNull() || aShape.ShapeType() != TopAbs_SOLID) {
192 for (iShape = 0; iShape < nShape; iShape++) {
193 aShape = shape[iShape];
194 if ( !( aPnt.X() < box[iShape][0] || box[iShape][1] < aPnt.X() ||
195 aPnt.Y() < box[iShape][2] || box[iShape][3] < aPnt.Y() ||
196 aPnt.Z() < box[iShape][4] || box[iShape][5] < aPnt.Z()) ) {
197 BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
198 if (state) *state = SC.State();
199 if (SC.State() == TopAbs_IN)
207 //=======================================================================
208 //function : readMapIntLine
210 //=======================================================================
212 static char* readMapIntLine(char* ptr, int tab[]) {
214 std::cout << std::endl;
216 for ( int i=0; i<17; i++ ) {
217 intVal = strtol(ptr, &ptr, 10);
224 //=======================================================================
225 //function : countShape
227 //=======================================================================
229 template < class Mesh, class Shape >
230 static int countShape( Mesh* mesh, Shape shape ) {
231 TopExp_Explorer expShape ( mesh->ShapeToMesh(), shape );
233 for ( ; expShape.More(); expShape.Next() ) {
239 //=======================================================================
240 //function : writeFaces
242 //=======================================================================
244 static bool writeFaces (ofstream & theFile,
245 SMESHDS_Mesh * theMesh,
246 const map <int,int> & theSmdsToGhs3dIdMap)
250 // NB_ELEMS DUMMY_INT
251 // Loop from 1 to NB_ELEMS
252 // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT
254 int nbShape = countShape( theMesh, TopAbs_FACE );
256 int *tabID; tabID = new int[nbShape];
257 TopoDS_Shape *tabShape; tabShape = new TopoDS_Shape[nbShape];
259 SMESHDS_SubMesh* theSubMesh;
260 const SMDS_MeshElement* aFace;
261 const char* space = " ";
262 const int dummyint = 0;
263 map<int,int>::const_iterator itOnMap;
264 SMDS_ElemIteratorPtr itOnSubMesh, itOnSubFace;
265 int shapeID, nbNodes, aSmdsID;
268 std::cout << " " << theMesh->NbFaces() << " shapes of 2D dimension" << std::endl;
269 std::cout << std::endl;
271 theFile << space << theMesh->NbFaces() << space << dummyint << std::endl;
273 TopExp_Explorer expface( theMesh->ShapeToMesh(), TopAbs_FACE );
274 for ( int i = 0; expface.More(); expface.Next(), i++ ) {
276 aShape = expface.Current();
277 shapeID = theMesh->ShapeToIndex( aShape );
279 for ( int j=0; j<=i; j++) {
280 if ( shapeID == tabID[j] ) {
287 tabShape[i] = aShape;
290 for ( int i =0; i < nbShape; i++ ) {
291 if ( tabID[i] != 0 ) {
292 aShape = tabShape[i];
294 theSubMesh = theMesh->MeshElements( aShape );
295 if ( !theSubMesh ) continue;
296 itOnSubMesh = theSubMesh->GetElements();
297 while ( itOnSubMesh->more() ) {
298 aFace = itOnSubMesh->next();
299 nbNodes = aFace->NbNodes();
301 theFile << space << nbNodes;
303 itOnSubFace = aFace->nodesIterator();
304 while ( itOnSubFace->more() ) {
306 aSmdsID = itOnSubFace->next()->GetID();
307 itOnMap = theSmdsToGhs3dIdMap.find( aSmdsID );
308 // if ( itOnMap == theSmdsToGhs3dIdMap.end() ) {
309 // cout << "not found node: " << aSmdsID << endl;
312 ASSERT( itOnMap != theSmdsToGhs3dIdMap.end() );
314 theFile << space << (*itOnMap).second;
317 // (NB_NODES + 1) times: DUMMY_INT
318 for ( int j=0; j<=nbNodes; j++)
319 theFile << space << dummyint;
321 theFile << std::endl;
333 //=======================================================================
334 //function : writeFaces
335 //purpose : Write Faces in case if generate 3D mesh w/o geometry
336 //=======================================================================
338 static bool writeFaces (ofstream & theFile,
339 SMESHDS_Mesh * theMesh,
340 vector <const SMDS_MeshNode*> & theNodeByGhs3dId)
344 // NB_ELEMS DUMMY_INT
345 // Loop from 1 to NB_ELEMS
346 // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT
350 list< const SMDS_MeshElement* > faces;
351 list< const SMDS_MeshElement* >::iterator f;
352 map< const SMDS_MeshNode*,int >::iterator it;
353 SMDS_ElemIteratorPtr nodeIt;
354 const SMDS_MeshElement* elem;
357 const char* space = " ";
358 const int dummyint = 0;
360 //get all faces from mesh
361 SMDS_FaceIteratorPtr eIt = theMesh->facesIterator();
362 while ( eIt->more() ) {
363 const SMDS_MeshElement* elem = eIt->next();
366 faces.push_back( elem );
373 std::cout << "The initial 2D mesh contains " << nbFaces << " faces and ";
375 // NB_ELEMS DUMMY_INT
376 theFile << space << nbFaces << space << dummyint << std::endl;
378 // Loop from 1 to NB_ELEMS
380 map<const SMDS_MeshNode*,int> aNodeToGhs3dIdMap;
382 for ( ; f != faces.end(); ++f )
386 nbNodes = elem->NbNodes();
387 theFile << space << nbNodes;
389 // NODE_NB_1 NODE_NB_2 ...
390 nodeIt = elem->nodesIterator();
391 while ( nodeIt->more() )
394 const SMDS_MeshNode* node = castToNode( nodeIt->next() );
395 int newId = aNodeToGhs3dIdMap.size() + 1; // ghs3d ids count from 1
396 it = aNodeToGhs3dIdMap.insert( make_pair( node, newId )).first;
397 theFile << space << it->second;
400 // (NB_NODES + 1) times: DUMMY_INT
401 for ( int i=0; i<=nbNodes; i++)
402 theFile << space << dummyint;
404 theFile << std::endl;
407 // put nodes to theNodeByGhs3dId vector
408 theNodeByGhs3dId.resize( aNodeToGhs3dIdMap.size() );
409 map<const SMDS_MeshNode*,int>::const_iterator n2id = aNodeToGhs3dIdMap.begin();
410 for ( ; n2id != aNodeToGhs3dIdMap.end(); ++ n2id)
412 theNodeByGhs3dId[ n2id->second - 1 ] = n2id->first; // ghs3d ids count from 1
418 //=======================================================================
419 //function : writePoints
421 //=======================================================================
423 static bool writePoints (ofstream & theFile,
424 SMESH_MesherHelper& theHelper,
425 map <int,int> & theSmdsToGhs3dIdMap,
426 map <int,const SMDS_MeshNode*> & theGhs3dIdToNodeMap,
427 map<vector<double>,double> & theEnforcedVertices)
432 // Loop from 1 to NB_NODES
435 SMESHDS_Mesh * theMesh = theHelper.GetMeshDS();
436 int nbNodes = theMesh->NbNodes();
439 int nbEnforcedVertices = theEnforcedVertices.size();
441 // Issue 020674: EDF 870 SMESH: Mesh generated by Netgen not usable by GHS3D
442 // The problem is in nodes on degenerated edges, we need to skip them
443 if ( theHelper.HasDegeneratedEdges() )
445 // here we decrease total nb of nodes by nb of nodes on degenerated edges
447 for (TopExp_Explorer e(theMesh->ShapeToMesh(), TopAbs_EDGE ); e.More(); e.Next())
449 SMESH_subMesh* sm = theHelper.GetMesh()->GetSubMesh( e.Current() );
450 if ( checkedSM.insert( sm->GetId() ).second && theHelper.IsDegenShape(sm->GetId() )) {
451 if ( sm->GetSubMeshDS() )
452 nbNodes -= sm->GetSubMeshDS()->NbNodes();
456 const char* space = " ";
457 const int dummyint = 0;
460 SMDS_NodeIteratorPtr it = theMesh->nodesIterator();
461 const SMDS_MeshNode* node;
464 std::cout << std::endl;
465 std::cout << "The initial 2D mesh contains :" << std::endl;
466 std::cout << " " << nbNodes << " nodes" << std::endl;
467 if (nbEnforcedVertices > 0) {
468 std::cout << " " << nbEnforcedVertices << " enforced vertices" << std::endl;
470 std::cout << std::endl;
471 std::cout << "Start writing in 'points' file ..." << std::endl;
472 theFile << space << nbNodes << std::endl;
474 // Loop from 1 to NB_NODES
479 if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_EDGE &&
480 theHelper.IsDegenShape( node->GetPosition()->GetShapeId() )) // Issue 020674
483 theSmdsToGhs3dIdMap.insert( make_pair( node->GetID(), aGhs3dID ));
484 theGhs3dIdToNodeMap.insert( make_pair( aGhs3dID, node ));
489 << space << node->X()
490 << space << node->Y()
491 << space << node->Z()
492 << space << dummyint;
494 theFile << std::endl;
498 // Iterate over the enforced vertices
499 GHS3DPlugin_Hypothesis::TEnforcedVertexValues::const_iterator vertexIt;
500 const TopoDS_Shape shapeToMesh = theMesh->ShapeToMesh();
501 for(vertexIt = theEnforcedVertices.begin() ; vertexIt != theEnforcedVertices.end() ; ++vertexIt) {
502 double x = vertexIt->first[0];
503 double y = vertexIt->first[1];
504 double z = vertexIt->first[2];
505 // Test if point is inside shape to mesh
506 gp_Pnt myPoint(x,y,z);
507 BRepClass3d_SolidClassifier scl(shapeToMesh);
508 scl.Perform(myPoint, 1e-7);
509 TopAbs_State result = scl.State();
510 if ( result == TopAbs_IN ) {
511 MESSAGE("Adding enforced vertex (" << x << "," << y <<"," << z << ") = " << vertexIt->second);
512 // X Y Z PHY_SIZE DUMMY_INT
517 << space << vertexIt->second
518 << space << dummyint;
520 theFile << std::endl;
523 MESSAGE("Enforced vertex (" << x << "," << y <<"," << z << ") is not inside the geometry: it was not added ");
527 std::cout << std::endl;
528 std::cout << "End writing in 'points' file." << std::endl;
533 //=======================================================================
534 //function : writePoints
536 //=======================================================================
538 static bool writePoints (ofstream & theFile,
539 SMESHDS_Mesh * theMesh,
540 const vector <const SMDS_MeshNode*> & theNodeByGhs3dId,
541 map<vector<double>,double> & theEnforcedVertices)
546 // Loop from 1 to NB_NODES
549 //int nbNodes = theMesh->NbNodes();
550 int nbNodes = theNodeByGhs3dId.size();
554 int nbEnforcedVertices = theEnforcedVertices.size();
556 const char* space = " ";
557 const int dummyint = 0;
559 const SMDS_MeshNode* node;
562 std::cout << std::endl;
563 std::cout << "The initial 2D mesh contains :" << std::endl;
564 std::cout << " " << nbNodes << " nodes" << std::endl;
565 std::cout << " " << nbEnforcedVertices << " enforced vertices" << std::endl;
566 std::cout << std::endl;
567 std::cout << "Start writing in 'points' file ..." << std::endl;
568 theFile << space << nbNodes << std::endl;
570 // Loop from 1 to NB_NODES
572 vector<const SMDS_MeshNode*>::const_iterator nodeIt = theNodeByGhs3dId.begin();
573 vector<const SMDS_MeshNode*>::const_iterator after = theNodeByGhs3dId.end();
574 for ( ; nodeIt != after; ++nodeIt )
580 << space << node->X()
581 << space << node->Y()
582 << space << node->Z()
583 << space << dummyint;
585 theFile << std::endl;
589 // Iterate over the enforced vertices
590 GHS3DPlugin_Hypothesis::TEnforcedVertexValues::const_iterator vertexIt;
591 const TopoDS_Shape shapeToMesh = theMesh->ShapeToMesh();
592 for(vertexIt = theEnforcedVertices.begin() ; vertexIt != theEnforcedVertices.end() ; ++vertexIt) {
593 double x = vertexIt->first[0];
594 double y = vertexIt->first[1];
595 double z = vertexIt->first[2];
596 // Test if point is inside shape to mesh
597 gp_Pnt myPoint(x,y,z);
598 BRepClass3d_SolidClassifier scl(shapeToMesh);
599 scl.Perform(myPoint, 1e-7);
600 TopAbs_State result = scl.State();
601 if ( result == TopAbs_IN ) {
602 std::cout << "Adding enforced vertex (" << x << "," << y <<"," << z << ") = " << vertexIt->second << std::endl;
604 // X Y Z PHY_SIZE DUMMY_INT
609 << space << vertexIt->second
610 << space << dummyint;
612 theFile << std::endl;
615 std::cout << std::endl;
616 std::cout << "End writing in 'points' file." << std::endl;
621 //=======================================================================
622 //function : findShapeID
623 //purpose : find the solid corresponding to GHS3D sub-domain following
624 // the technique proposed in GHS3D manual in chapter
625 // "B.4 Subdomain (sub-region) assignment"
626 //=======================================================================
628 static int findShapeID(SMESH_Mesh& mesh,
629 const SMDS_MeshNode* node1,
630 const SMDS_MeshNode* node2,
631 const SMDS_MeshNode* node3,
632 const bool toMeshHoles)
634 const int invalidID = 0;
635 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
637 // face the nodes belong to
638 const SMDS_MeshElement * face = meshDS->FindFace(node1,node2,node3);
642 // geom face the face assigned to
643 SMESH_MeshEditor editor(&mesh);
644 int geomFaceID = editor.FindShape( face );
647 TopoDS_Shape shape = meshDS->IndexToShape( geomFaceID );
648 if ( shape.IsNull() || shape.ShapeType() != TopAbs_FACE )
650 TopoDS_Face geomFace = TopoDS::Face( shape );
652 // solids bounded by geom face
653 TopTools_IndexedMapOfShape solids, shells;
654 TopTools_ListIteratorOfListOfShape ansIt = mesh.GetAncestors(geomFace);
655 for ( ; ansIt.More(); ansIt.Next() ) {
656 switch ( ansIt.Value().ShapeType() ) {
658 solids.Add( ansIt.Value() ); break;
660 shells.Add( ansIt.Value() ); break;
664 // analyse found solids
665 if ( solids.Extent() == 0 || shells.Extent() == 0)
668 const TopoDS_Solid& solid1 = TopoDS::Solid( solids(1) );
669 if ( solids.Extent() == 1 )
672 return meshDS->ShapeToIndex( solid1 );
674 // - Are we at a hole boundary face?
675 if ( shells(1).IsSame( BRepTools::OuterShell( solid1 )) )
676 { // - No, but maybe a hole is bound by two shapes? Does shells(1) touches another shell?
678 TopExp_Explorer eExp( shells(1), TopAbs_EDGE );
679 // check if any edge of shells(1) belongs to another shell
680 for ( ; eExp.More() && !touch; eExp.Next() ) {
681 ansIt = mesh.GetAncestors( eExp.Current() );
682 for ( ; ansIt.More() && !touch; ansIt.Next() ) {
683 if ( ansIt.Value().ShapeType() == TopAbs_SHELL )
684 touch = ( !ansIt.Value().IsSame( shells(1) ));
688 return meshDS->ShapeToIndex( solid1 );
691 // find orientation of geom face within the first solid
692 TopExp_Explorer fExp( solid1, TopAbs_FACE );
693 for ( ; fExp.More(); fExp.Next() )
694 if ( geomFace.IsSame( fExp.Current() )) {
695 geomFace = TopoDS::Face( fExp.Current() );
699 return invalidID; // face not found
701 // normale to triangle
702 gp_Pnt node1Pnt ( node1->X(), node1->Y(), node1->Z() );
703 gp_Pnt node2Pnt ( node2->X(), node2->Y(), node2->Z() );
704 gp_Pnt node3Pnt ( node3->X(), node3->Y(), node3->Z() );
705 gp_Vec vec12( node1Pnt, node2Pnt );
706 gp_Vec vec13( node1Pnt, node3Pnt );
707 gp_Vec meshNormal = vec12 ^ vec13;
708 if ( meshNormal.SquareMagnitude() < DBL_MIN )
711 // find UV of node1 on geomFace
712 SMESH_MesherHelper helper( mesh ); helper.SetSubShape( geomFace );
713 const SMDS_MeshNode* nNotOnSeamEdge = 0;
714 if ( helper.IsSeamShape( node1->GetPosition()->GetShapeId() ))
715 if ( helper.IsSeamShape( node2->GetPosition()->GetShapeId() ))
716 nNotOnSeamEdge = node3;
718 nNotOnSeamEdge = node2;
720 gp_XY uv = helper.GetNodeUV( geomFace, node1, nNotOnSeamEdge, &uvOK );
721 // check that uv is correct
723 TopoDS_Shape nodeShape = helper.GetSubShapeByNode( node1, meshDS );
724 if ( !nodeShape.IsNull() )
725 switch ( nodeShape.ShapeType() )
727 case TopAbs_FACE: tol = BRep_Tool::Tolerance( TopoDS::Face( nodeShape )); break;
728 case TopAbs_EDGE: tol = BRep_Tool::Tolerance( TopoDS::Edge( nodeShape )); break;
729 case TopAbs_VERTEX: tol = BRep_Tool::Tolerance( TopoDS::Vertex( nodeShape )); break;
732 BRepAdaptor_Surface surface( geomFace );
733 if ( !uvOK || node1Pnt.Distance( surface.Value( uv.X(), uv.Y() )) > 2 * tol )
736 // normale to geomFace at UV
738 surface.D1( uv.X(), uv.Y(), node1Pnt, du, dv );
739 gp_Vec geomNormal = du ^ dv;
740 if ( geomNormal.SquareMagnitude() < DBL_MIN )
741 return findShapeID( mesh, node2, node3, node1, toMeshHoles );
742 if ( geomFace.Orientation() == TopAbs_REVERSED )
743 geomNormal.Reverse();
746 bool isReverse = ( meshNormal * geomNormal ) < 0;
748 return meshDS->ShapeToIndex( solid1 );
750 if ( solids.Extent() == 1 )
751 return HOLE_ID; // we are inside a hole
753 return meshDS->ShapeToIndex( solids(2) );
756 //=======================================================================
757 //function : readResultFile
759 //=======================================================================
761 static bool readResultFile(const int fileOpen,
763 const char* fileName,
766 TopoDS_Shape tabShape[],
769 map <int,const SMDS_MeshNode*>& theGhs3dIdToNodeMap,
771 int nbEnforcedVertices)
773 MESSAGE("GHS3DPlugin_GHS3D::readResultFile()");
774 Kernel_Utils::Localizer loc;
782 SMESHDS_Mesh* theMeshDS = theMesh.GetMeshDS();
785 int nbElems, nbNodes, nbInputNodes;
786 int nodeId/*, triangleId*/;
788 int ID, shapeID, ghs3dShapeID;
791 nbShape ? theMeshDS->ShapeToIndex( tabShape[0] ) : theMeshDS->ShapeToIndex( theMeshDS->ShapeToMesh() );
793 int *tab, *tabID, *nodeID, *nodeAssigne;
795 const SMDS_MeshNode **node;
798 //tabID = new int[nbShape];
800 coord = new double[3];
801 node = new const SMDS_MeshNode*[4];
804 SMDS_MeshNode * aNewNode;
805 map <int,const SMDS_MeshNode*>::iterator itOnNode;
806 SMDS_MeshElement* aTet;
811 // Read the file state
812 fileStat = fstat(fileOpen, &status);
813 length = status.st_size;
815 // Mapping the result file into memory
817 HANDLE fd = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ,
818 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
819 HANDLE hMapObject = CreateFileMapping(fd, NULL, PAGE_READONLY,
820 0, (DWORD)length, NULL);
821 ptr = ( char* ) MapViewOfFile(hMapObject, FILE_MAP_READ, 0, 0, 0 );
823 ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
827 ptr = readMapIntLine(ptr, tab);
832 nbInputNodes = tab[2];
834 nodeAssigne = new int[ nbNodes+1 ];
837 aSolid = tabShape[0];
839 // Reading the nodeId
840 for (int i=0; i < 4*nbElems; i++)
841 nodeId = strtol(ptr, &ptr, 10);
843 MESSAGE("nbInputNodes: "<<nbInputNodes);
844 MESSAGE("nbEnforcedVertices: "<<nbEnforcedVertices);
845 // Reading the nodeCoor and update the nodeMap
846 for (int iNode=1; iNode <= nbNodes; iNode++) {
847 for (int iCoor=0; iCoor < 3; iCoor++)
848 coord[ iCoor ] = strtod(ptr, &ptr);
849 nodeAssigne[ iNode ] = 1;
850 if ( iNode > (nbInputNodes-nbEnforcedVertices) ) {
851 // Creating SMESH nodes
852 // - for enforced vertices
853 // - for vertices of forced edges
855 nodeAssigne[ iNode ] = 0;
856 aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
857 theGhs3dIdToNodeMap.insert(theGhs3dIdToNodeMap.end(), make_pair( iNode, aNewNode ));
861 // Reading the number of triangles which corresponds to the number of sub-domains
862 nbTriangle = strtol(ptr, &ptr, 10);
864 tabID = new int[nbTriangle];
865 for (int i=0; i < nbTriangle; i++) {
867 // find the solid corresponding to GHS3D sub-domain following
868 // the technique proposed in GHS3D manual in chapter
869 // "B.4 Subdomain (sub-region) assignment"
870 int nodeId1 = strtol(ptr, &ptr, 10);
871 int nodeId2 = strtol(ptr, &ptr, 10);
872 int nodeId3 = strtol(ptr, &ptr, 10);
873 if ( nbTriangle > 1 ) {
874 const SMDS_MeshNode* n1 = theGhs3dIdToNodeMap[ nodeId1 ];
875 const SMDS_MeshNode* n2 = theGhs3dIdToNodeMap[ nodeId2 ];
876 const SMDS_MeshNode* n3 = theGhs3dIdToNodeMap[ nodeId3 ];
879 tabID[i] = findShapeID( theMesh, n1, n2, n3, toMeshHoles );
880 // -- 0020330: Pb with ghs3d as a submesh
881 // check that found shape is to be meshed
882 if ( tabID[i] > 0 ) {
883 const TopoDS_Shape& foundShape = theMeshDS->IndexToShape( tabID[i] );
884 bool isToBeMeshed = false;
885 for ( int iS = 0; !isToBeMeshed && iS < nbShape; ++iS )
886 isToBeMeshed = foundShape.IsSame( tabShape[ iS ]);
890 // END -- 0020330: Pb with ghs3d as a submesh
892 std::cout << i+1 << " subdomain: findShapeID() returns " << tabID[i] << std::endl;
894 } catch ( Standard_Failure ) {
901 if ( nbTriangle <= nbShape ) // no holes
902 toMeshHoles = true; // not avoid creating tetras in holes
904 // Associating the tetrahedrons to the shapes
905 shapeID = compoundID;
906 for (int iElem = 0; iElem < nbElems; iElem++) {
907 for (int iNode = 0; iNode < 4; iNode++) {
908 ID = strtol(tetraPtr, &tetraPtr, 10);
909 itOnNode = theGhs3dIdToNodeMap.find(ID);
910 node[ iNode ] = itOnNode->second;
911 nodeID[ iNode ] = ID;
913 // We always run GHS3D with "to mesh holes'==TRUE but we must not create
914 // tetras within holes depending on hypo option,
915 // so we first check if aTet is inside a hole and then create it
916 //aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
917 if ( nbTriangle > 1 ) {
918 shapeID = HOLE_ID; // negative shapeID means not to create tetras if !toMeshHoles
919 ghs3dShapeID = strtol(shapePtr, &shapePtr, 10) - IdShapeRef;
920 if ( tabID[ ghs3dShapeID ] == 0 ) {
922 aSolid = findShape(node, aSolid, tabShape, tabBox, nbShape, &state);
923 if ( toMeshHoles || state == TopAbs_IN )
924 shapeID = theMeshDS->ShapeToIndex( aSolid );
925 tabID[ ghs3dShapeID ] = shapeID;
928 shapeID = tabID[ ghs3dShapeID ];
930 else if ( nbShape > 1 ) {
931 // Case where nbTriangle == 1 while nbShape == 2 encountered
932 // with compound of 2 boxes and "To mesh holes"==False,
933 // so there are no subdomains specified for each tetrahedron.
934 // Try to guess a solid by a node already bound to shape
936 for ( int i=0; i<4 && shapeID==0; i++ ) {
937 if ( nodeAssigne[ nodeID[i] ] == 1 &&
938 node[i]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE &&
939 node[i]->GetPosition()->GetShapeId() > 1 )
941 shapeID = node[i]->GetPosition()->GetShapeId();
945 aSolid = findShape(node, aSolid, tabShape, tabBox, nbShape);
946 shapeID = theMeshDS->ShapeToIndex( aSolid );
949 // set new nodes and tetrahedron onto the shape
950 for ( int i=0; i<4; i++ ) {
951 if ( nodeAssigne[ nodeID[i] ] == 0 ) {
952 if ( shapeID != HOLE_ID )
953 theMeshDS->SetNodeInVolume( node[i], shapeID );
954 nodeAssigne[ nodeID[i] ] = shapeID;
957 if ( toMeshHoles || shapeID != HOLE_ID ) {
958 aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
959 theMeshDS->SetMeshElementOnShape( aTet, shapeID );
962 shapeIDs.insert( shapeID );
965 // Remove nodes of tetras inside holes if !toMeshHoles
966 if ( !toMeshHoles ) {
967 itOnNode = theGhs3dIdToNodeMap.find( nbInputNodes );
968 for ( ; itOnNode != theGhs3dIdToNodeMap.end(); ++itOnNode) {
969 ID = itOnNode->first;
970 if ( nodeAssigne[ ID ] == HOLE_ID )
971 theMeshDS->RemoveFreeNode( itOnNode->second, 0 );
976 cout << nbElems << " tetrahedrons have been associated to " << nbShape << " shapes" << endl;
978 UnmapViewOfFile(mapPtr);
979 CloseHandle(hMapObject);
982 munmap(mapPtr, length);
991 delete [] nodeAssigne;
994 if ( shapeIDs.size() != nbShape ) {
995 std::cout << "Only " << shapeIDs.size() << " solids of " << nbShape << " found" << std::endl;
996 for (int i=0; i<nbShape; i++) {
997 shapeID = theMeshDS->ShapeToIndex( tabShape[i] );
998 if ( shapeIDs.find( shapeID ) == shapeIDs.end() )
999 std::cout << " Solid #" << shapeID << " not found" << std::endl;
1007 //=======================================================================
1008 //function : readResultFile
1010 //=======================================================================
1012 static bool readResultFile(const int fileOpen,
1014 const char* fileName,
1016 SMESHDS_Mesh* theMeshDS,
1017 TopoDS_Shape aSolid,
1018 vector <const SMDS_MeshNode*>& theNodeByGhs3dId,
1019 int nbEnforcedVertices) {
1021 Kernel_Utils::Localizer loc;
1030 int nbElems, nbNodes, nbInputNodes;
1031 int nodeId, triangleId;
1037 const SMDS_MeshNode **node;
1040 coord = new double[3];
1041 node = new const SMDS_MeshNode*[4];
1043 SMDS_MeshNode * aNewNode;
1044 map <int,const SMDS_MeshNode*>::iterator IdNode;
1045 SMDS_MeshElement* aTet;
1047 // Read the file state
1048 fileStat = fstat(fileOpen, &status);
1049 length = status.st_size;
1051 // Mapping the result file into memory
1053 HANDLE fd = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ,
1054 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1055 HANDLE hMapObject = CreateFileMapping(fd, NULL, PAGE_READONLY,
1056 0, (DWORD)length, NULL);
1057 ptr = ( char* ) MapViewOfFile(hMapObject, FILE_MAP_READ, 0, 0, 0 );
1059 ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
1063 ptr = readMapIntLine(ptr, tab);
1068 nbInputNodes = tab[2];
1070 theNodeByGhs3dId.resize( nbNodes );
1072 // Reading the nodeId
1073 for (int i=0; i < 4*nbElems; i++)
1074 nodeId = strtol(ptr, &ptr, 10);
1076 // Reading the nodeCoor and update the nodeMap
1077 shapeID = theMeshDS->ShapeToIndex( aSolid );
1078 for (int iNode=0; iNode < nbNodes; iNode++) {
1079 for (int iCoor=0; iCoor < 3; iCoor++)
1080 coord[ iCoor ] = strtod(ptr, &ptr);
1081 if ((iNode+1) > (nbInputNodes-nbEnforcedVertices)) {
1082 aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
1083 theMeshDS->SetNodeInVolume( aNewNode, shapeID );
1084 theNodeByGhs3dId[ iNode ] = aNewNode;
1088 // Reading the triangles
1089 nbTriangle = strtol(ptr, &ptr, 10);
1091 for (int i=0; i < 3*nbTriangle; i++)
1092 triangleId = strtol(ptr, &ptr, 10);
1096 // Associating the tetrahedrons to the shapes
1097 for (int iElem = 0; iElem < nbElems; iElem++) {
1098 for (int iNode = 0; iNode < 4; iNode++) {
1099 ID = strtol(tetraPtr, &tetraPtr, 10);
1100 node[ iNode ] = theNodeByGhs3dId[ ID-1 ];
1102 aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
1103 shapeID = theMeshDS->ShapeToIndex( aSolid );
1104 theMeshDS->SetMeshElementOnShape( aTet, shapeID );
1107 cout << nbElems << " tetrahedrons have been associated to " << nbTriangle << " shapes" << endl;
1109 UnmapViewOfFile(mapPtr);
1110 CloseHandle(hMapObject);
1113 munmap(mapPtr, length);
1124 //=============================================================================
1126 *Here we are going to use the GHS3D mesher
1128 //=============================================================================
1130 bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh,
1131 const TopoDS_Shape& theShape)
1134 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
1136 // we count the number of shapes
1137 // _nbShape = countShape( meshDS, TopAbs_SOLID ); -- 0020330: Pb with ghs3d as a submesh
1139 TopExp_Explorer expBox ( theShape, TopAbs_SOLID );
1140 for ( ; expBox.More(); expBox.Next() )
1143 // create bounding box for every shape inside the compound
1146 TopoDS_Shape* tabShape;
1148 tabShape = new TopoDS_Shape[_nbShape];
1149 tabBox = new double*[_nbShape];
1150 for (int i=0; i<_nbShape; i++)
1151 tabBox[i] = new double[6];
1152 Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
1154 // TopExp_Explorer expBox (meshDS->ShapeToMesh(), TopAbs_SOLID); -- 0020330:...ghs3d as a submesh
1155 for (expBox.ReInit(); expBox.More(); expBox.Next()) {
1156 tabShape[iShape] = expBox.Current();
1157 Bnd_Box BoundingBox;
1158 BRepBndLib::Add(expBox.Current(), BoundingBox);
1159 BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1160 tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
1161 tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
1162 tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
1166 // a unique working file name
1167 // to avoid access to the same files by eg different users
1168 TCollection_AsciiString aGenericName
1169 = (char*) GHS3DPlugin_Hypothesis::GetFileName(_hyp).c_str();
1171 TCollection_AsciiString aFacesFileName, aPointsFileName, aResultFileName;
1172 TCollection_AsciiString aBadResFileName, aBbResFileName, aLogFileName;
1173 aFacesFileName = aGenericName + ".faces"; // in faces
1174 aPointsFileName = aGenericName + ".points"; // in points
1175 aResultFileName = aGenericName + ".noboite";// out points and volumes
1176 aBadResFileName = aGenericName + ".boite"; // out bad result
1177 aBbResFileName = aGenericName + ".bb"; // out vertex stepsize
1178 aLogFileName = aGenericName + ".log"; // log
1180 // -----------------
1182 // -----------------
1184 ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out);
1185 ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out);
1188 aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
1190 INFOS( "Can't write into " << aFacesFileName);
1191 return error(SMESH_Comment("Can't write into ") << aFacesFileName);
1193 map <int,int> aSmdsToGhs3dIdMap;
1194 map <int,const SMDS_MeshNode*> aGhs3dIdToNodeMap;
1195 map<vector<double>,double> enforcedVertices;
1196 int nbEnforcedVertices = 0;
1198 enforcedVertices = GHS3DPlugin_Hypothesis::GetEnforcedVertices(_hyp);
1199 nbEnforcedVertices = enforcedVertices.size();
1204 SMESH_MesherHelper helper( theMesh );
1205 helper.SetSubShape( theShape );
1207 Ok = writePoints( aPointsFile, helper, aSmdsToGhs3dIdMap, aGhs3dIdToNodeMap, enforcedVertices) &&
1208 writeFaces ( aFacesFile, meshDS, aSmdsToGhs3dIdMap );
1210 // Write aSmdsToGhs3dIdMap to temp file
1211 TCollection_AsciiString aSmdsToGhs3dIdMapFileName;
1212 aSmdsToGhs3dIdMapFileName = aGenericName + ".ids"; // ids relation
1213 ofstream aIdsFile ( aSmdsToGhs3dIdMapFileName.ToCString() , ios::out);
1215 aIdsFile.rdbuf()->is_open();
1217 INFOS( "Can't write into " << aSmdsToGhs3dIdMapFileName);
1218 return error(SMESH_Comment("Can't write into ") << aSmdsToGhs3dIdMapFileName);
1220 aIdsFile << "Smds Ghs3d" << std::endl;
1221 map <int,int>::const_iterator myit;
1222 for (myit=aSmdsToGhs3dIdMap.begin() ; myit != aSmdsToGhs3dIdMap.end() ; ++myit) {
1223 aIdsFile << myit->first << " " << myit->second << std::endl;
1227 aPointsFile.close();
1231 if ( !_keepFiles ) {
1232 removeFile( aFacesFileName );
1233 removeFile( aPointsFileName );
1234 removeFile( aSmdsToGhs3dIdMapFileName );
1236 return error(COMPERR_BAD_INPUT_MESH);
1238 removeFile( aResultFileName ); // needed for boundary recovery module usage
1240 // -----------------
1242 // -----------------
1244 TCollection_AsciiString cmd( (char*)GHS3DPlugin_Hypothesis::CommandToRun( _hyp ).c_str() );
1245 cmd += TCollection_AsciiString(" -f ") + aGenericName; // file to read
1246 cmd += TCollection_AsciiString(" 1>" ) + aLogFileName; // dump into file
1248 std::cout << std::endl;
1249 std::cout << "Ghs3d execution..." << std::endl;
1250 std::cout << cmd << std::endl;
1252 system( cmd.ToCString() ); // run
1254 std::cout << std::endl;
1255 std::cout << "End of Ghs3d execution !" << std::endl;
1261 // Mapping the result file
1264 fileOpen = open( aResultFileName.ToCString(), O_RDONLY);
1265 if ( fileOpen < 0 ) {
1266 std::cout << std::endl;
1267 std::cout << "Can't open the " << aResultFileName.ToCString() << " GHS3D output file" << std::endl;
1268 std::cout << "Log: " << aLogFileName << std::endl;
1273 _hyp ? _hyp->GetToMeshHoles(true) : GHS3DPlugin_Hypothesis::DefaultMeshHoles();
1274 Ok = readResultFile( fileOpen,
1276 aResultFileName.ToCString(),
1278 theMesh, tabShape, tabBox, _nbShape, aGhs3dIdToNodeMap,
1279 toMeshHoles, nbEnforcedVertices );
1282 // ---------------------
1283 // remove working files
1284 // ---------------------
1289 removeFile( aLogFileName );
1291 else if ( OSD_File( aLogFileName ).Size() > 0 )
1293 // get problem description from the log file
1294 _Ghs2smdsConvertor conv( aGhs3dIdToNodeMap );
1295 storeErrorDescription( aLogFileName, conv );
1299 // the log file is empty
1300 removeFile( aLogFileName );
1301 INFOS( "GHS3D Error, command '" << cmd.ToCString() << "' failed" );
1302 error(COMPERR_ALGO_FAILED, "ghs3d: command not found" );
1305 if ( !_keepFiles ) {
1306 removeFile( aFacesFileName );
1307 removeFile( aPointsFileName );
1308 removeFile( aResultFileName );
1309 removeFile( aBadResFileName );
1310 removeFile( aBbResFileName );
1311 removeFile( aSmdsToGhs3dIdMapFileName );
1313 std::cout << "<" << aResultFileName.ToCString() << "> GHS3D output file ";
1315 std::cout << "not ";
1316 std::cout << "treated !" << std::endl;
1317 std::cout << std::endl;
1319 _nbShape = 0; // re-initializing _nbShape for the next Compute() method call
1326 //=============================================================================
1328 *Here we are going to use the GHS3D mesher w/o geometry
1330 //=============================================================================
1331 bool GHS3DPlugin_GHS3D::Compute(SMESH_Mesh& theMesh,
1332 SMESH_MesherHelper* aHelper)
1334 MESSAGE("GHS3DPlugin_GHS3D::Compute()");
1336 SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
1337 TopoDS_Shape theShape = aHelper->GetSubShape();
1339 // a unique working file name
1340 // to avoid access to the same files by eg different users
1341 TCollection_AsciiString aGenericName
1342 = (char*) GHS3DPlugin_Hypothesis::GetFileName(_hyp).c_str();
1344 TCollection_AsciiString aFacesFileName, aPointsFileName, aResultFileName;
1345 TCollection_AsciiString aBadResFileName, aBbResFileName, aLogFileName;
1346 aFacesFileName = aGenericName + ".faces"; // in faces
1347 aPointsFileName = aGenericName + ".points"; // in points
1348 aResultFileName = aGenericName + ".noboite";// out points and volumes
1349 aBadResFileName = aGenericName + ".boite"; // out bad result
1350 aBbResFileName = aGenericName + ".bb"; // out vertex stepsize
1351 aLogFileName = aGenericName + ".log"; // log
1353 // -----------------
1355 // -----------------
1357 ofstream aFacesFile ( aFacesFileName.ToCString() , ios::out);
1358 ofstream aPointsFile ( aPointsFileName.ToCString() , ios::out);
1360 aFacesFile.rdbuf()->is_open() && aPointsFile.rdbuf()->is_open();
1363 return error( SMESH_Comment("Can't write into ") << aPointsFileName);
1365 GHS3DPlugin_Hypothesis::TEnforcedVertexValues enforcedVertices;
1366 int nbEnforcedVertices = 0;
1368 enforcedVertices = GHS3DPlugin_Hypothesis::GetEnforcedVertices(_hyp);
1369 nbEnforcedVertices = enforcedVertices.size();
1374 vector <const SMDS_MeshNode*> aNodeByGhs3dId;
1376 Ok = (writeFaces ( aFacesFile, meshDS, aNodeByGhs3dId ) &&
1377 writePoints( aPointsFile, meshDS, aNodeByGhs3dId,enforcedVertices));
1380 aPointsFile.close();
1383 if ( !_keepFiles ) {
1384 removeFile( aFacesFileName );
1385 removeFile( aPointsFileName );
1387 return error(COMPERR_BAD_INPUT_MESH);
1389 removeFile( aResultFileName ); // needed for boundary recovery module usage
1391 // -----------------
1393 // -----------------
1395 TCollection_AsciiString cmd =
1396 (char*)GHS3DPlugin_Hypothesis::CommandToRun( _hyp, false ).c_str();
1397 cmd += TCollection_AsciiString(" -f ") + aGenericName; // file to read
1398 cmd += TCollection_AsciiString(" 1>" ) + aLogFileName; // dump into file
1400 system( cmd.ToCString() ); // run
1406 fileOpen = open( aResultFileName.ToCString(), O_RDONLY);
1407 if ( fileOpen < 0 ) {
1408 std::cout << std::endl;
1409 std::cout << "Error when opening the " << aResultFileName.ToCString() << " file" << std::endl;
1410 std::cout << "Log: " << aLogFileName << std::endl;
1411 std::cout << std::endl;
1415 Ok = readResultFile( fileOpen,
1417 aResultFileName.ToCString(),
1419 meshDS, theShape ,aNodeByGhs3dId, nbEnforcedVertices );
1422 // ---------------------
1423 // remove working files
1424 // ---------------------
1429 removeFile( aLogFileName );
1431 else if ( OSD_File( aLogFileName ).Size() > 0 )
1433 // get problem description from the log file
1434 _Ghs2smdsConvertor conv( aNodeByGhs3dId );
1435 storeErrorDescription( aLogFileName, conv );
1438 // the log file is empty
1439 removeFile( aLogFileName );
1440 INFOS( "GHS3D Error, command '" << cmd.ToCString() << "' failed" );
1441 error(COMPERR_ALGO_FAILED, "ghs3d: command not found" );
1446 removeFile( aFacesFileName );
1447 removeFile( aPointsFileName );
1448 removeFile( aResultFileName );
1449 removeFile( aBadResFileName );
1450 removeFile( aBbResFileName );
1456 //================================================================================
1458 * \brief Provide human readable text by error code reported by ghs3d
1460 //================================================================================
1462 static string translateError(const int errNum)
1466 return "The surface mesh includes a face of type other than edge, "
1467 "triangle or quadrilateral. This face type is not supported.";
1469 return "Not enough memory for the face table.";
1471 return "Not enough memory.";
1473 return "Not enough memory.";
1475 return "Face is ignored.";
1477 return "End of file. Some data are missing in the file.";
1479 return "Read error on the file. There are wrong data in the file.";
1481 return "the metric file is inadequate (dimension other than 3).";
1483 return "the metric file is inadequate (values not per vertices).";
1485 return "the metric file contains more than one field.";
1487 return "the number of values in the \".bb\" (metric file) is incompatible with the expected"
1488 "value of number of mesh vertices in the \".noboite\" file.";
1490 return "Too many sub-domains.";
1492 return "the number of vertices is negative or null.";
1494 return "the number of faces is negative or null.";
1496 return "A face has a null vertex.";
1498 return "incompatible data.";
1500 return "the number of vertices is negative or null.";
1502 return "the number of vertices is negative or null (in the \".mesh\" file).";
1504 return "the number of faces is negative or null.";
1506 return "A face appears more than once in the input surface mesh.";
1508 return "An edge appears more than once in the input surface mesh.";
1510 return "A face has a vertex negative or null.";
1512 return "NOT ENOUGH MEMORY.";
1514 return "Not enough available memory.";
1516 return "Some initial points cannot be inserted. The surface mesh is probably very bad "
1517 "in terms of quality or the input list of points is wrong.";
1519 return "Some vertices are too close to one another or coincident.";
1521 return "Some vertices are too close to one another or coincident.";
1523 return "A vertex cannot be inserted.";
1525 return "There are at least two points considered as coincident.";
1527 return "Some vertices are too close to one another or coincident.";
1529 return "The surface mesh regeneration step has failed.";
1531 return "Constrained edge cannot be enforced.";
1533 return "Constrained face cannot be enforced.";
1535 return "Missing faces.";
1537 return "No guess to start the definition of the connected component(s).";
1539 return "The surface mesh includes at least one hole. The domain is not well defined.";
1541 return "Impossible to define a component.";
1543 return "The surface edge intersects another surface edge.";
1545 return "The surface edge intersects the surface face.";
1547 return "One boundary point lies within a surface face.";
1549 return "One surface edge intersects a surface face.";
1551 return "One boundary point lies within a surface edge.";
1553 return "Insufficient memory ressources detected due to a bad quality surface mesh leading "
1554 "to too many swaps.";
1556 return "Edge is unique (i.e., bounds a hole in the surface).";
1558 return "Presumably, the surface mesh is not compatible with the domain being processed.";
1560 return "Too many components, too many sub-domain.";
1562 return "The surface mesh includes at least one hole. "
1563 "Therefore there is no domain properly defined.";
1565 return "Statistics.";
1567 return "Statistics.";
1569 return "Warning, it is dramatically tedious to enforce the boundary items.";
1571 return "Not enough memory at this time, nevertheless, the program continues. "
1572 "The expected mesh will be correct but not really as large as required.";
1574 return "see above error code, resulting quality may be poor.";
1576 return "Not enough memory at this time, nevertheless, the program continues (warning).";
1578 return "Unknown face type.";
1581 return "End of file. Some data are missing in the file.";
1583 return "A too small volume element is detected.";
1585 return "There exists at least a null or negative volume element.";
1587 return "There exist null or negative volume elements.";
1589 return "A too small volume element is detected. A face is considered being degenerated.";
1591 return "Some element is suspected to be very bad shaped or wrong.";
1593 return "A too bad quality face is detected. This face is considered degenerated.";
1595 return "A too bad quality face is detected. This face is degenerated.";
1597 return "Presumably, the surface mesh is not compatible with the domain being processed.";
1599 return "Abnormal error occured, contact hotline.";
1601 return "Not enough memory for the face table.";
1603 return "The algorithm cannot run further. "
1604 "The surface mesh is probably very bad in terms of quality.";
1606 return "Bad vertex number.";
1611 //================================================================================
1613 * \brief Retrieve from a string given number of integers
1615 //================================================================================
1617 static char* getIds( char* ptr, int nbIds, vector<int>& ids )
1620 ids.reserve( nbIds );
1623 while ( !isdigit( *ptr )) ++ptr;
1624 if ( ptr[-1] == '-' ) --ptr;
1625 ids.push_back( strtol( ptr, &ptr, 10 ));
1631 //================================================================================
1633 * \brief Retrieve problem description form a log file
1634 * \retval bool - always false
1636 //================================================================================
1638 bool GHS3DPlugin_GHS3D::storeErrorDescription(const TCollection_AsciiString& logFile,
1639 const _Ghs2smdsConvertor & toSmdsConvertor )
1643 int file = ::_open (logFile.ToCString(), _O_RDONLY|_O_BINARY);
1645 int file = ::open (logFile.ToCString(), O_RDONLY);
1648 return error( SMESH_Comment("See ") << logFile << " for problem description");
1651 // struct stat status;
1652 // fstat(file, &status);
1653 // size_t length = status.st_size;
1654 off_t length = lseek( file, 0, SEEK_END);
1655 lseek( file, 0, SEEK_SET);
1658 vector< char > buf( length );
1659 int nBytesRead = ::read (file, & buf[0], length);
1661 char* ptr = & buf[0];
1662 char* bufEnd = ptr + nBytesRead;
1664 SMESH_Comment errDescription;
1666 enum { NODE = 1, EDGE, TRIA, VOL, ID = 1 };
1668 // look for errors "ERR #"
1670 set<string> foundErrorStr; // to avoid reporting same error several times
1671 set<int> elemErrorNums; // not to report different types of errors with bad elements
1672 while ( ++ptr < bufEnd )
1674 if ( strncmp( ptr, "ERR ", 4 ) != 0 )
1677 list<const SMDS_MeshElement*> badElems;
1678 vector<int> nodeIds;
1682 int errNum = strtol(ptr, &ptr, 10);
1683 switch ( errNum ) { // we treat errors enumerated in [SALOME platform 0019316] issue
1685 // The face number (numfac) with vertices (f 1, f 2, f 3) has a null vertex.
1686 ptr = getIds(ptr, NODE, nodeIds);
1687 ptr = getIds(ptr, TRIA, nodeIds);
1688 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1690 case 1000: // ERR 1000 : 1 3 2
1691 // Face (f 1, f 2, f 3) appears more than once in the input surface mesh.
1692 ptr = getIds(ptr, TRIA, nodeIds);
1693 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1696 // Edge (e1, e2) appears more than once in the input surface mesh
1697 ptr = getIds(ptr, EDGE, nodeIds);
1698 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1701 // Face (f 1, f 2, f 3) has a vertex negative or null
1702 ptr = getIds(ptr, TRIA, nodeIds);
1703 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1706 // Vertex v1 and vertex v2 are too close to one another or coincident (warning).
1707 ptr = getIds(ptr, NODE, nodeIds);
1708 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1709 ptr = getIds(ptr, NODE, nodeIds);
1710 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1713 // Vertex v1 cannot be inserted (warning).
1714 ptr = getIds(ptr, NODE, nodeIds);
1715 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1718 // There are at least two points whose distance is dist, i.e., considered as coincident
1719 case 2103: // ERR 2103 : 16 WITH 3
1720 // Vertex v1 and vertex v2 are too close to one another or coincident (warning).
1721 ptr = getIds(ptr, NODE, nodeIds);
1722 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1723 ptr = getIds(ptr, NODE, nodeIds);
1724 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1727 // Constrained edge (e1, e2) cannot be enforced (warning).
1728 ptr = getIds(ptr, EDGE, nodeIds);
1729 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1732 // Constrained face (f 1, f 2, f 3) cannot be enforced
1733 ptr = getIds(ptr, TRIA, nodeIds);
1734 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1736 case 3103: // ERR 3103 : 1 2 WITH 7 3
1737 // The surface edge (e1, e2) intersects another surface edge (e3, e4)
1738 ptr = getIds(ptr, EDGE, nodeIds);
1739 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1740 ptr = getIds(ptr, EDGE, nodeIds);
1741 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1743 case 3104: // ERR 3104 : 9 10 WITH 1 2 3
1744 // The surface edge (e1, e2) intersects the surface face (f 1, f 2, f 3)
1745 ptr = getIds(ptr, EDGE, nodeIds);
1746 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1747 ptr = getIds(ptr, TRIA, nodeIds);
1748 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1750 case 3105: // ERR 3105 : 8 IN 2 3 5
1751 // One boundary point (say p1) lies within a surface face (f 1, f 2, f 3)
1752 ptr = getIds(ptr, NODE, nodeIds);
1753 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1754 ptr = getIds(ptr, TRIA, nodeIds);
1755 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1758 // One surface edge (say e1, e2) intersects a surface face (f 1, f 2, f 3)
1759 ptr = getIds(ptr, EDGE, nodeIds);
1760 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1761 ptr = getIds(ptr, TRIA, nodeIds);
1762 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1764 case 3107: // ERR 3107 : 2 IN 4 1
1765 // One boundary point (say p1) lies within a surface edge (e1, e2) (stop).
1766 ptr = getIds(ptr, NODE, nodeIds);
1767 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1768 ptr = getIds(ptr, EDGE, nodeIds);
1769 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1771 case 3109: // ERR 3109 : EDGE 5 6 UNIQUE
1772 // Edge (e1, e2) is unique (i.e., bounds a hole in the surface)
1773 ptr = getIds(ptr, EDGE, nodeIds);
1774 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1776 case 9000: // ERR 9000
1777 // ELEMENT 261 WITH VERTICES : 7 396 -8 242
1778 // VOLUME : -1.11325045E+11 W.R.T. EPSILON 0.
1779 // A too small volume element is detected. Are reported the index of the element,
1780 // its four vertex indices, its volume and the tolerance threshold value
1781 ptr = getIds(ptr, ID, nodeIds);
1782 ptr = getIds(ptr, VOL, nodeIds);
1783 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1784 // even if all nodes found, volume it most probably invisible,
1785 // add its faces to demenstrate it anyhow
1787 vector<int> faceNodes( nodeIds.begin(), --nodeIds.end() ); // 012
1788 badElems.push_back( toSmdsConvertor.getElement(faceNodes));
1789 faceNodes[2] = nodeIds[3]; // 013
1790 badElems.push_back( toSmdsConvertor.getElement(faceNodes));
1791 faceNodes[1] = nodeIds[2]; // 023
1792 badElems.push_back( toSmdsConvertor.getElement(faceNodes));
1793 faceNodes[0] = nodeIds[1]; // 123
1794 badElems.push_back( toSmdsConvertor.getElement(faceNodes));
1797 case 9001: // ERR 9001
1798 // %% NUMBER OF NEGATIVE VOLUME TETS : 1
1799 // %% THE LARGEST NEGATIVE TET : 1.75376581E+11
1800 // %% NUMBER OF NULL VOLUME TETS : 0
1801 // There exists at least a null or negative volume element
1804 // There exist n null or negative volume elements
1807 // A too small volume element is detected
1810 // A too bad quality face is detected. This face is considered degenerated,
1811 // its index, its three vertex indices together with its quality value are reported
1812 break; // same as next
1813 case 9112: // ERR 9112
1814 // FACE 2 WITH VERTICES : 4 2 5
1815 // SMALL INRADIUS : 0.
1816 // A too bad quality face is detected. This face is degenerated,
1817 // its index, its three vertex indices together with its inradius are reported
1818 ptr = getIds(ptr, ID, nodeIds);
1819 ptr = getIds(ptr, TRIA, nodeIds);
1820 badElems.push_back( toSmdsConvertor.getElement(nodeIds));
1821 // add triangle edges as it most probably has zero area and hence invisible
1823 vector<int> edgeNodes(2);
1824 edgeNodes[0] = nodeIds[0]; edgeNodes[1] = nodeIds[1]; // 0-1
1825 badElems.push_back( toSmdsConvertor.getElement(edgeNodes));
1826 edgeNodes[1] = nodeIds[2]; // 0-2
1827 badElems.push_back( toSmdsConvertor.getElement(edgeNodes));
1828 edgeNodes[0] = nodeIds[1]; // 1-2
1829 badElems.push_back( toSmdsConvertor.getElement(edgeNodes));
1834 bool isNewError = foundErrorStr.insert( string( errBeg, ptr )).second;
1836 continue; // not to report same error several times
1838 // const SMDS_MeshElement* nullElem = 0;
1839 // bool allElemsOk = ( find( badElems.begin(), badElems.end(), nullElem) == badElems.end());
1841 // if ( allElemsOk && !badElems.empty() && !elemErrorNums.empty() ) {
1842 // bool oneMoreErrorType = elemErrorNums.insert( errNum ).second;
1843 // if ( oneMoreErrorType )
1844 // continue; // not to report different types of errors with bad elements
1847 // store bad elements
1848 //if ( allElemsOk ) {
1849 list<const SMDS_MeshElement*>::iterator elem = badElems.begin();
1850 for ( ; elem != badElems.end(); ++elem )
1851 addBadInputElement( *elem );
1855 string text = translateError( errNum );
1856 if ( errDescription.find( text ) == text.npos ) {
1857 if ( !errDescription.empty() )
1858 errDescription << "\n";
1859 errDescription << text;
1864 if ( errDescription.empty() ) { // no errors found
1865 char msg[] = "connection to server failed";
1866 if ( search( &buf[0], bufEnd, msg, msg + strlen(msg)) != bufEnd )
1867 errDescription << "Licence problems.";
1870 if ( errDescription.empty() )
1871 errDescription << "See " << logFile << " for problem description";
1873 errDescription << "\nSee " << logFile << " for more information";
1875 return error( errDescription );
1878 //================================================================================
1880 * \brief Creates _Ghs2smdsConvertor
1882 //================================================================================
1884 _Ghs2smdsConvertor::_Ghs2smdsConvertor( const map <int,const SMDS_MeshNode*> & ghs2NodeMap)
1885 :_ghs2NodeMap( & ghs2NodeMap ), _nodeByGhsId( 0 )
1889 //================================================================================
1891 * \brief Creates _Ghs2smdsConvertor
1893 //================================================================================
1895 _Ghs2smdsConvertor::_Ghs2smdsConvertor( const vector <const SMDS_MeshNode*> & nodeByGhsId)
1896 : _ghs2NodeMap( 0 ), _nodeByGhsId( &nodeByGhsId )
1900 //================================================================================
1902 * \brief Return SMDS element by ids of GHS3D nodes
1904 //================================================================================
1906 const SMDS_MeshElement* _Ghs2smdsConvertor::getElement(const vector<int>& ghsNodes) const
1908 size_t nbNodes = ghsNodes.size();
1909 vector<const SMDS_MeshNode*> nodes( nbNodes, 0 );
1910 for ( size_t i = 0; i < nbNodes; ++i ) {
1911 int ghsNode = ghsNodes[ i ];
1912 if ( _ghs2NodeMap ) {
1913 map <int,const SMDS_MeshNode*>::const_iterator in = _ghs2NodeMap->find( ghsNode);
1914 if ( in == _ghs2NodeMap->end() )
1916 nodes[ i ] = in->second;
1919 if ( ghsNode < 1 || ghsNode > _nodeByGhsId->size() )
1921 nodes[ i ] = (*_nodeByGhsId)[ ghsNode-1 ];
1927 if ( nbNodes == 2 ) {
1928 const SMDS_MeshElement* edge= SMDS_Mesh::FindEdge( nodes[0], nodes[1] );
1930 edge = new SMDS_MeshEdge( nodes[0], nodes[1] );
1933 if ( nbNodes == 3 ) {
1934 const SMDS_MeshElement* face = SMDS_Mesh::FindFace( nodes );
1936 face = new SMDS_FaceOfNodes( nodes[0], nodes[1], nodes[2] );
1940 return new SMDS_VolumeOfNodes( nodes[0], nodes[1], nodes[2], nodes[3] );
1946 //=============================================================================
1950 //=============================================================================
1951 bool GHS3DPlugin_GHS3D::Evaluate(SMESH_Mesh& aMesh,
1952 const TopoDS_Shape& aShape,
1953 MapShapeNbElems& aResMap)
1955 int nbtri = 0, nbqua = 0;
1956 double fullArea = 0.0;
1957 for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
1958 TopoDS_Face F = TopoDS::Face( exp.Current() );
1959 SMESH_subMesh *sm = aMesh.GetSubMesh(F);
1960 MapShapeNbElemsItr anIt = aResMap.find(sm);
1961 if( anIt==aResMap.end() ) {
1962 SMESH_ComputeErrorPtr& smError = sm->GetComputeError();
1963 smError.reset( new SMESH_ComputeError(COMPERR_ALGO_FAILED,
1964 "Submesh can not be evaluated",this));
1967 std::vector<int> aVec = (*anIt).second;
1968 nbtri += Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
1969 nbqua += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
1971 BRepGProp::SurfaceProperties(F,G);
1972 double anArea = G.Mass();
1976 // collect info from edges
1977 int nb0d_e = 0, nb1d_e = 0;
1978 bool IsQuadratic = false;
1979 bool IsFirst = true;
1980 TopTools_MapOfShape tmpMap;
1981 for (TopExp_Explorer exp(aShape, TopAbs_EDGE); exp.More(); exp.Next()) {
1982 TopoDS_Edge E = TopoDS::Edge(exp.Current());
1983 if( tmpMap.Contains(E) )
1986 SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
1987 MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
1988 std::vector<int> aVec = (*anIt).second;
1989 nb0d_e += aVec[SMDSEntity_Node];
1990 nb1d_e += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
1992 IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
1998 double ELen = sqrt(2.* ( fullArea/(nbtri+nbqua*2) ) / sqrt(3.0) );
2001 BRepGProp::VolumeProperties(aShape,G);
2002 double aVolume = G.Mass();
2003 double tetrVol = 0.1179*ELen*ELen*ELen;
2004 double CoeffQuality = 0.9;
2005 int nbVols = int(aVolume/tetrVol/CoeffQuality);
2006 int nb1d_f = (nbtri*3 + nbqua*4 - nb1d_e) / 2;
2007 int nb1d_in = (int) ( nbVols*6 - nb1d_e - nb1d_f ) / 5;
2008 std::vector<int> aVec(SMDSEntity_Last);
2009 for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
2011 aVec[SMDSEntity_Node] = nb1d_in/6 + 1 + nb1d_in;
2012 aVec[SMDSEntity_Quad_Tetra] = nbVols - nbqua*2;
2013 aVec[SMDSEntity_Quad_Pyramid] = nbqua;
2016 aVec[SMDSEntity_Node] = nb1d_in/6 + 1;
2017 aVec[SMDSEntity_Tetra] = nbVols - nbqua*2;
2018 aVec[SMDSEntity_Pyramid] = nbqua;
2020 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
2021 aResMap.insert(std::make_pair(sm,aVec));