Salome HOME
PR: synchro V7_main tag mergefrom_V6_main_28Feb13
[modules/smesh.git] / src / DriverGMF / DriverGMF_Read.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, Read to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : DriverGMF_Read.cxx
23 // Created   : Mon Sep 17 17:03:02 2012
24 // Author    : Edward AGAPOV (eap)
25
26 #include "DriverGMF_Read.hxx"
27 #include "DriverGMF.hxx"
28
29 #include "SMESHDS_Group.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Comment.hxx"
32
33 #include <Basics_Utils.hxx>
34
35 extern "C"
36 {
37 #include "libmesh5.h"
38 }
39
40 #include <stdarg.h>
41
42 // --------------------------------------------------------------------------------
43 DriverGMF_Read::DriverGMF_Read():
44   Driver_SMESHDS_Mesh(),
45   _makeRequiredGroups( true )
46 {
47 }
48 // --------------------------------------------------------------------------------
49 DriverGMF_Read::~DriverGMF_Read()
50 {
51 }
52
53 //================================================================================
54 /*!
55  * \brief Read a GMF file
56  */
57 //================================================================================
58
59 Driver_Mesh::Status DriverGMF_Read::Perform()
60 {
61   Kernel_Utils::Localizer loc;
62
63   Status status = DRS_OK;
64
65   int dim, version;
66
67   // open the file
68   int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim );
69   if ( !meshID )
70   {
71     if ( DriverGMF::isExtensionCorrect( myFile ))
72       return addMessage( SMESH_Comment("Can't open for reading ") << myFile, /*fatal=*/true );
73     else
74       return addMessage( SMESH_Comment("Not '.mesh' or '.meshb' extension of file ") << myFile, /*fatal=*/true );
75   }
76   DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction
77
78   // Read nodes
79
80   int nbNodes = GmfStatKwd(meshID, GmfVertices);
81   if ( nbNodes < 1 )
82     return addMessage( "No nodes in the mesh", /*fatal=*/true );
83
84   GmfGotoKwd(meshID, GmfVertices);
85
86   int ref;
87
88   const int nodeIDShift = myMesh->GetMeshInfo().NbNodes();
89   if ( version != GmfFloat )
90   {
91     double x, y, z;
92     for ( int i = 1; i <= nbNodes; ++i )
93     {
94       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
95       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
96     }
97   }
98   else
99   {
100     float x, y, z;
101     for ( int i = 1; i <= nbNodes; ++i )
102     {
103       GmfGetLin(meshID, GmfVertices, &x, &y, &z, &ref);
104       myMesh->AddNodeWithID( x,y,z, nodeIDShift + i);
105     }
106   }
107
108   // Read elements
109
110   int iN[28]; // 28 - nb nodes in HEX27 (+ 1 for safety :)
111
112   /* Read extra vertices for quadratic edges */
113   std::vector<int> quadNodesAtEdges;
114   int nbQuadEdges = 0;
115   if ( (nbQuadEdges = GmfStatKwd(meshID, GmfExtraVerticesAtEdges)) )
116   {
117     quadNodesAtEdges.reserve( nbQuadEdges );
118     GmfGotoKwd(meshID, GmfExtraVerticesAtEdges);
119     for ( int i = 1; i <= nbQuadEdges; ++i )
120     {
121       GmfGetLin(meshID, GmfExtraVerticesAtEdges, &iN[0], &iN[1], &iN[2]);
122       quadNodesAtEdges.push_back(iN[2]);
123     }
124   }
125
126   /* Read edges */
127   const int edgeIDShift = myMesh->GetMeshInfo().NbElements();
128   if ( int nbEdges = GmfStatKwd(meshID, GmfEdges))
129   {
130     const bool readQuadNodes = ( nbQuadEdges == nbEdges );
131     GmfGotoKwd(meshID, GmfEdges);
132     for ( int i = 1; i <= nbEdges; ++i )
133     {
134       GmfGetLin(meshID, GmfEdges, &iN[0], &iN[1], &ref);
135       if ( readQuadNodes )
136       {
137         const int midN = quadNodesAtEdges[i-1];
138         if ( !myMesh->AddEdgeWithID( iN[0], iN[1], midN, edgeIDShift + i ))
139           status = storeBadNodeIds( "GmfEdges + GmfExtraVerticesAtEdges",i, 3, iN[0],iN[1],midN);
140       }
141       else
142       {
143         if ( !myMesh->AddEdgeWithID( iN[0], iN[1], edgeIDShift + i ))
144           status = storeBadNodeIds( "GmfEdges",i, 2, iN[0], iN[1] );
145       }
146     }
147   }
148   // the vector of extra vertices at edges won't be used anymore so it is cleared
149   quadNodesAtEdges.clear();
150
151   /* Read extra vertices for quadratic triangles */
152   std::vector< std::vector<int> > quadNodesAtTriangles;
153   int nbQuadTria = 0;
154   if ( (nbQuadTria = GmfStatKwd(meshID, GmfExtraVerticesAtTriangles)) )
155   {
156     GmfGotoKwd(meshID, GmfExtraVerticesAtTriangles);
157     quadNodesAtTriangles.reserve( nbQuadTria );
158     std::vector<int> nodes(4);
159     for ( int i = 1; i <= nbQuadTria; ++i )
160     {
161       GmfGetLin(meshID, GmfExtraVerticesAtTriangles,
162                 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4],
163                 &iN[5]); // iN[5] - preview TRIA7
164       nodes.clear();
165       nodes.push_back(iN[2]);
166       nodes.push_back(iN[3]);
167       nodes.push_back(iN[4]);
168       nodes.push_back(iN[5]);
169       nodes.resize( iN[1] );
170
171       quadNodesAtTriangles.push_back(nodes);
172     }
173   }
174
175   /* Read triangles */
176   const int triaIDShift = myMesh->GetMeshInfo().NbElements();
177   if ( int nbTria = GmfStatKwd(meshID, GmfTriangles))
178   {
179     const bool readQuadNodes = (nbQuadTria == nbTria);
180     GmfGotoKwd(meshID, GmfTriangles);
181     for ( int i = 1; i <= nbTria; ++i )
182     {
183       GmfGetLin(meshID, GmfTriangles, &iN[0], &iN[1], &iN[2], &ref);
184       if ( readQuadNodes )
185       {
186         const std::vector<int>& midN = quadNodesAtTriangles[ i-1 ];
187         if ( !myMesh->AddFaceWithID( iN[0],iN[1],iN[2], midN[0],midN[1],midN[2],  triaIDShift + i ))
188           status = storeBadNodeIds( "GmfTriangles + GmfExtraVerticesAtTriangles",i, 6, 
189                                     iN[0],iN[1],iN[2], midN[0],midN[1],midN[2] );
190       }
191       else
192       {
193         if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], triaIDShift + i ))
194           status = storeBadNodeIds( "GmfTriangles",i, 3, iN[0], iN[1], iN[2] );
195       }
196     }
197   }
198   // the vector of extra vertices at triangles won't be used anymore so it is cleared
199   quadNodesAtTriangles.clear();
200
201   /* Read extra vertices for quadratic quadrangles */
202   std::vector< std::vector<int> > quadNodesAtQuadrilaterals;
203   int nbQuadQuad = 0;
204   if ( (nbQuadQuad = GmfStatKwd(meshID, GmfExtraVerticesAtQuadrilaterals)) )
205   {
206     GmfGotoKwd(meshID, GmfExtraVerticesAtQuadrilaterals);
207     quadNodesAtQuadrilaterals.reserve( nbQuadQuad );
208     std::vector<int> nodes( 5 );
209     for ( int i = 1; i <= nbQuadQuad; ++i )
210     {
211       GmfGetLin(meshID, GmfExtraVerticesAtQuadrilaterals,
212                 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6]);
213       nodes.clear();
214       nodes.push_back(iN[2]);
215       nodes.push_back(iN[3]);
216       nodes.push_back(iN[4]);
217       nodes.push_back(iN[5]);
218       nodes.push_back(iN[6]);
219       nodes.resize( iN[1] );
220
221       quadNodesAtQuadrilaterals.push_back(nodes);
222     }
223   }
224
225  /* Read quadrangles */
226   const int quadIDShift = myMesh->GetMeshInfo().NbElements();
227   if ( int nbQuad = GmfStatKwd(meshID, GmfQuadrilaterals))
228   {
229     const bool readQuadNodes = (nbQuadQuad == nbQuad);
230     GmfGotoKwd(meshID, GmfQuadrilaterals);
231     for ( int i = 1; i <= nbQuad; ++i )
232     {
233       GmfGetLin(meshID, GmfQuadrilaterals, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
234       if ( readQuadNodes )
235       {
236         const std::vector<int>& midN = quadNodesAtQuadrilaterals[ i-1 ];
237         if ( midN.size() == 4 )
238         {
239           if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3],
240                                        midN[0], midN[1], midN[2], midN[3],
241                                        quadIDShift + i ))
242             status = storeBadNodeIds( "GmfQuadrilaterals + GmfExtraVerticesAtQuadrilaterals",i, 8,
243                                       iN[0], iN[1],iN[2], iN[3],
244                                       midN[0], midN[1], midN[2], midN[3]);
245         }
246         else
247         {
248           if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3],
249                                        midN[0], midN[1], midN[2], midN[3], midN[4],
250                                        quadIDShift + i ))
251             status = storeBadNodeIds( "GmfQuadrilaterals + GmfExtraVerticesAtQuadrilaterals",i, 9,
252                                       iN[0], iN[1],iN[2], iN[3],
253                                       midN[0], midN[1], midN[2], midN[3], midN[4]);
254         }
255       }
256       else
257       {
258         if ( !myMesh->AddFaceWithID( iN[0], iN[1], iN[2], iN[3], quadIDShift + i ))
259           status = storeBadNodeIds( "GmfQuadrilaterals",i, 4, iN[0], iN[1],iN[2], iN[3] );
260       }
261     }
262   }
263   // the vector of extra vertices at quadrilaterals won't be used anymore so it is cleared
264   quadNodesAtQuadrilaterals.clear();
265
266   /* Read extra vertices for quadratic tetrahedra */
267   std::vector< std::vector<int> > quadNodesAtTetrahedra;
268   int nbQuadTetra = 0;
269   if ( (nbQuadTetra = GmfStatKwd(meshID, GmfExtraVerticesAtTetrahedra)) )
270   {
271     GmfGotoKwd(meshID, GmfExtraVerticesAtTetrahedra);
272     quadNodesAtTetrahedra.reserve( nbQuadTetra );
273     std::vector<int> nodes( 6 );
274     for ( int i = 1; i <= nbQuadTetra; ++i )
275     {
276       GmfGetLin(meshID, GmfExtraVerticesAtTetrahedra,
277                 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &iN[6], &iN[7]);
278       nodes.clear();
279       nodes.push_back(iN[2]);
280       nodes.push_back(iN[3]);
281       nodes.push_back(iN[4]);
282       nodes.push_back(iN[5]);
283       nodes.push_back(iN[6]);
284       nodes.push_back(iN[7]);
285       nodes.resize( iN[1] );
286
287       quadNodesAtTetrahedra.push_back(nodes);
288     }
289   }
290  
291   /* Read terahedra */
292   const int tetIDShift = myMesh->GetMeshInfo().NbElements();
293   if ( int nbTet = GmfStatKwd(meshID, GmfTetrahedra))
294   {
295     const bool readQuadNodes = (nbQuadTetra == nbTet);
296     GmfGotoKwd(meshID, GmfTetrahedra);
297     for ( int i = 1; i <= nbTet; ++i )
298     {
299       GmfGetLin(meshID, GmfTetrahedra, &iN[0], &iN[1], &iN[2], &iN[3], &ref);
300       if ( readQuadNodes )
301       {
302         const std::vector<int>& midN = quadNodesAtTetrahedra[ i-1 ];  
303         if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], 
304                                        midN[2], midN[1], midN[0], midN[3], midN[5], midN[4], tetIDShift + i ))
305           status = storeBadNodeIds( "GmfTetrahedra + GmfExtraVerticesAtTetrahedra",i, 10, iN[0], iN[2], iN[1], iN[3], 
306                                                                 midN[2], midN[1], midN[0], midN[3], midN[5], midN[4] );
307       }
308       else
309       {
310         if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], tetIDShift + i ) )
311           status = storeBadNodeIds( "GmfTetrahedra" ,i, 4, iN[0], iN[2], iN[1], iN[3] );
312       }
313     }
314   }
315   // the vector of extra vertices at tetrahedra won't be used anymore so it is cleared
316   quadNodesAtTetrahedra.clear();
317
318   /* Read pyramids */
319   const int pyrIDShift = myMesh->GetMeshInfo().NbElements();
320   if ( int nbPyr = GmfStatKwd(meshID, GmfPyramids))
321   {
322     GmfGotoKwd(meshID, GmfPyramids);
323     for ( int i = 1; i <= nbPyr; ++i )
324     {
325       GmfGetLin(meshID, GmfPyramids, &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &ref);
326       if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[4], pyrIDShift + i ))
327         status = storeBadNodeIds( "GmfPyramids",i, 5, iN[0], iN[1],iN[2], iN[3], iN[4] );
328     }
329   }
330
331   /* Read extra vertices for quadratic hexahedra */
332   std::vector< std::vector<int> > quadNodesAtHexahedra;
333   int nbQuadHexa = 0;
334   if ( (nbQuadHexa = GmfStatKwd(meshID, GmfExtraVerticesAtHexahedra)) )
335   {
336     GmfGotoKwd(meshID, GmfExtraVerticesAtHexahedra);
337     quadNodesAtHexahedra.reserve( nbQuadHexa );
338     std::vector<int> nodes( 19 );
339     for ( int i = 1; i <= nbQuadHexa; ++i )
340     {
341       GmfGetLin(meshID, GmfExtraVerticesAtHexahedra, &iN[0], &iN[1],    // Hexa Id, Nb of extra vertices
342                                                      &iN[2], &iN[3], &iN[4], &iN[5],
343                                                      &iN[6], &iN[7], &iN[8], &iN[9],
344                                                      &iN[10], &iN[11], &iN[12], &iN[13], // HEXA20
345                                                      &iN[14], 
346                                                      &iN[15], &iN[16], &iN[17], &iN[18], 
347                                                      &iN[19],
348                                                      &iN[20]);                          // HEXA27
349       nodes.clear();
350       nodes.push_back(iN[2]);
351       nodes.push_back(iN[3]);
352       nodes.push_back(iN[4]);
353       nodes.push_back(iN[5]);
354       nodes.push_back(iN[6]);
355       nodes.push_back(iN[7]);
356       nodes.push_back(iN[8]);
357       nodes.push_back(iN[9]);
358       nodes.push_back(iN[10]);
359       nodes.push_back(iN[11]);
360       nodes.push_back(iN[12]);
361       nodes.push_back(iN[13]);     
362       nodes.push_back(iN[14]);
363       nodes.push_back(iN[15]);
364       nodes.push_back(iN[16]);
365       nodes.push_back(iN[17]);
366       nodes.push_back(iN[18]);
367       nodes.push_back(iN[19]);
368       nodes.push_back(iN[20]);
369       nodes.resize( iN[1] );
370
371       quadNodesAtHexahedra.push_back(nodes);
372     }
373   }
374   
375   /* Read hexahedra */
376   const int hexIDShift = myMesh->GetMeshInfo().NbElements();
377   if ( int nbHex = GmfStatKwd(meshID, GmfHexahedra))
378   {
379     const bool readQuadNodes = (nbQuadHexa == nbHex);
380     GmfGotoKwd(meshID, GmfHexahedra);
381     for ( int i = 1; i <= nbHex; ++i )
382     {
383       GmfGetLin(meshID, GmfHexahedra, &iN[0], &iN[1], &iN[2], &iN[3],
384                                       &iN[4], &iN[5], &iN[6], &iN[7],&ref);
385       if ( readQuadNodes )
386       {
387         const std::vector<int>& midN = quadNodesAtHexahedra[ i-1 ];  
388         if ( midN.size() == 12 )  // HEXA20
389         {
390           if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
391                                          iN[4], iN[7], iN[6], iN[5],
392                                          midN[3], midN[2], midN[1], midN[0],
393                                          midN[7], midN[6], midN[5], midN[4],
394                                          midN[8], midN[11], midN[10], midN[9],
395                                          tetIDShift + i ))
396             status = storeBadNodeIds( "GmfHexahedra + GmfExtraVerticesAtHexahedra",i, 20, 
397                                        iN[0], iN[3], iN[2], iN[1],
398                                        iN[4], iN[7], iN[6], iN[5],
399                                        midN[3], midN[2], midN[1], midN[0],
400                                        midN[7], midN[6], midN[5], midN[4],
401                                        midN[8], midN[11], midN[10], midN[9]);
402         }
403         else                      // HEXA27
404         {
405            if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
406                                           iN[4], iN[7], iN[6], iN[5],
407                                           midN[3], midN[2], midN[1], midN[0],
408                                           midN[7], midN[6], midN[5], midN[4],
409                                           midN[8], midN[11], midN[10], midN[9],
410                                           midN[12],
411                                           midN[16], midN[15], midN[14], midN[13],
412                                           midN[17],
413                                           midN[18],                                                        
414                                           tetIDShift + i ))
415             status = storeBadNodeIds( "GmfHexahedra + GmfExtraVerticesAtHexahedra",i, 27, 
416                                        iN[0], iN[3], iN[2], iN[1],
417                                        iN[4], iN[7], iN[6], iN[5],
418                                        midN[3], midN[2], midN[1], midN[0],
419                                        midN[7], midN[6], midN[5], midN[4],
420                                        midN[8], midN[11], midN[10], midN[9],
421                                        midN[12],
422                                        midN[16], midN[15], midN[14], midN[13],
423                                        midN[17],
424                                        midN[18]);
425         }
426       }
427       else
428       {
429         if ( !myMesh->AddVolumeWithID( iN[0], iN[3], iN[2], iN[1],
430                                        iN[4], iN[7], iN[6], iN[5], hexIDShift + i ) )
431           status = storeBadNodeIds( "GmfHexahedra" ,i, 8, iN[0], iN[3], iN[2], iN[1],
432                                                           iN[4], iN[7], iN[6], iN[5] );
433       }
434     }
435   }
436   // the vector of extra vertices at tetrahedra won't be used anymore so it is cleared
437   quadNodesAtHexahedra.clear();
438
439   /* Read prism */
440   const int prismIDShift = myMesh->GetMeshInfo().NbElements();
441   if ( int nbPrism = GmfStatKwd(meshID, GmfPrisms))
442   {
443     GmfGotoKwd(meshID, GmfPrisms);
444     for ( int i = 1; i <= nbPrism; ++i )
445     {
446       GmfGetLin(meshID, GmfPrisms,
447                 &iN[0], &iN[1], &iN[2], &iN[3], &iN[4], &iN[5], &ref);
448       if ( !myMesh->AddVolumeWithID( iN[0], iN[2], iN[1], iN[3], iN[5], iN[4], prismIDShift + i))
449         status = storeBadNodeIds( "GmfPrisms",i,
450                                   6, iN[0], iN[1],iN[2], iN[3], iN[4], iN[5] );
451     }
452   }
453
454   // Read required entities into groups
455
456   if ( _makeRequiredGroups )
457   {
458     // get ids of existing groups
459     std::set< int > groupIDs;
460     const std::set<SMESHDS_GroupBase*>& groups = myMesh->GetGroups();
461     std::set<SMESHDS_GroupBase*>::const_iterator grIter = groups.begin();
462     for ( ; grIter != groups.end(); ++grIter )
463       groupIDs.insert( (*grIter)->GetID() );
464     if ( groupIDs.empty() ) groupIDs.insert( 0 );
465
466     const int kes[4][3] = { { GmfRequiredVertices,      SMDSAbs_Node, nodeIDShift },
467                             { GmfRequiredEdges,         SMDSAbs_Edge, edgeIDShift },
468                             { GmfRequiredTriangles,     SMDSAbs_Face, triaIDShift },
469                             { GmfRequiredQuadrilaterals,SMDSAbs_Face, quadIDShift }};
470     const char* names[4] = { "_required_Vertices"      ,
471                              "_required_Edges"         ,
472                              "_required_Triangles"     ,
473                              "_required_Quadrilaterals" };
474     for ( int i = 0; i < 4; ++i )
475     {
476       int                 gmfKwd = kes[i][0];
477       SMDSAbs_ElementType entity = (SMDSAbs_ElementType) kes[i][1];
478       int                 shift  = kes[i][2];
479       if ( int nb = GmfStatKwd(meshID, gmfKwd))
480       {
481         const int newID = *groupIDs.rbegin() + 1;
482         groupIDs.insert( newID );
483         SMESHDS_Group* group = new SMESHDS_Group( newID, myMesh, entity );
484         group->SetStoreName( names[i] );
485         myMesh->AddGroup( group );
486
487         GmfGotoKwd(meshID, gmfKwd);
488         for ( int i = 0; i < nb; ++i )
489         {
490           GmfGetLin(meshID, gmfKwd, &iN[0] );
491           group->Add( shift + iN[0] );
492         }
493       }
494     }
495   }
496
497   return status;
498 }
499
500 //================================================================================
501 /*!
502  * \brief Store a message about invalid IDs of nodes
503  */
504 //================================================================================
505
506 Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...)
507 {
508   if ( myStatus != DRS_OK )
509     return myStatus;
510
511   SMESH_Comment msg;
512
513   va_list VarArg;
514   va_start(VarArg, nb);
515
516   for ( int i = 0; i < nb; ++i )
517   {
518     int id = va_arg(VarArg, int );
519     if ( !myMesh->FindNode( id ))
520       msg << " " << id;
521   }
522   va_end(VarArg);
523
524   if ( !msg.empty() )
525   {
526     std::string nbStr;
527     const char* nbNames[] = { "1-st ", "2-nd ", "3-d " };
528     if ( elemNb < 3 ) nbStr = nbNames[ elemNb-1 ];
529     else              nbStr = SMESH_Comment(elemNb) << "-th ";
530
531     return addMessage
532       ( SMESH_Comment("Wrong node IDs of ")<< nbStr << gmfKwd << ":" << msg,
533         /*fatal=*/false );
534   }
535   return DRS_OK;
536 }