Salome HOME
7b2c5a8676b06798450fdf0b79d0d3526727c8e7
[plugins/ghs3dplugin.git] / src / GHS3DPlugin / MG_Tetra_API.cxx
1 // Copyright (C) 2004-2016  CEA/DEN, EDF R&D
2 //
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, or (at your option) any later version.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MG_Tetra_API.hxx"
21
22 #ifdef WIN32
23 #define NOMINMAX
24 #endif
25 #include <SMESH_Comment.hxx>
26 #include <SMESH_File.hxx>
27 #include <Utils_SALOME_Exception.hxx>
28
29 #include <vector>
30 #include <iterator>
31 #include <cstring>
32
33 #ifdef USE_MG_LIBS
34
35 extern "C"{
36 #include <meshgems/meshgems.h>
37 #include <meshgems/tetra.h>
38 }
39
40 struct MG_Tetra_API::LibData
41 {
42   // MG objects
43   context_t *       _context;
44   tetra_session_t * _session;
45   mesh_t *          _tria_mesh;
46   sizemap_t *       _sizemap;
47   mesh_t *          _tetra_mesh;
48
49   // data to pass to MG
50   std::vector<double> _xyz;
51   std::vector<double> _nodeSize; // required nodes
52   std::vector<int>    _edgeNodes;
53   int                 _nbRequiredEdges;
54   std::vector<int>    _triaNodes;
55   int                 _nbRequiredTria;
56
57   int                 _count;
58   volatile bool&      _cancelled_flag;
59   std::string         _errorStr;
60   double&             _progress;
61
62   LibData( volatile bool & cancelled_flag, double& progress )
63     : _context(0), _session(0), _tria_mesh(0), _sizemap(0), _tetra_mesh(0),
64       _nbRequiredEdges(0), _nbRequiredTria(0),
65       _cancelled_flag( cancelled_flag ), _progress( progress )
66   {
67   }
68   // methods setting callbacks implemented after callback definitions
69   void Init();
70   bool Compute();
71
72   ~LibData()
73   {
74     if ( _tetra_mesh )
75       tetra_regain_mesh( _session, _tetra_mesh );
76     if ( _session )
77       tetra_session_delete( _session );
78     if ( _tria_mesh )
79       mesh_delete( _tria_mesh );
80     if ( _sizemap )
81       sizemap_delete( _sizemap );
82     if ( _context )
83       context_delete( _context );
84
85     _tetra_mesh = 0;
86     _session = 0;
87     _tria_mesh = 0;
88     _sizemap = 0;
89     _context = 0;
90   }
91
92   void AddError( const char *txt )
93   {
94     if ( txt )
95       _errorStr += txt;
96   }
97
98   const std::string& GetErrors()
99   {
100     return _errorStr;
101   }
102
103   void MG_Error(const char* txt="")
104   {
105     SMESH_Comment msg("\nMeshGems error. ");
106     msg << txt << "\n";
107     AddError( msg.c_str() );
108   }
109
110   bool SetParam( const std::string& param, const std::string& value )
111   {
112     status_t ret = tetra_set_param( _session, param.c_str(), value.c_str() );
113 #ifdef _DEBUG_
114     //std::cout << param << " = " << value << std::endl;
115 #endif
116     return ( ret == STATUS_OK );
117   }
118
119   bool Cancelled()
120   {
121     return _cancelled_flag;
122   }
123
124   int ReadNbSubDomains()
125   {
126     integer nb = 0;
127     status_t ret = mesh_get_subdomain_count( _tetra_mesh, & nb );
128
129     if ( ret != STATUS_OK ) MG_Error("mesh_get_subdomain_count problem");
130     return nb;
131   }
132
133   int ReadNbNodes()
134   {
135     integer nb = 0;
136     status_t ret = mesh_get_vertex_count( _tetra_mesh, & nb );
137
138     if ( ret != STATUS_OK ) MG_Error("mesh_get_vertex_count problem");
139     return nb;
140   }
141
142   int ReadNbEdges()
143   {
144     integer nb = 0;
145     status_t ret = mesh_get_edge_count( _tetra_mesh, & nb );
146
147     if ( ret != STATUS_OK ) MG_Error("mesh_get_edge_count problem");
148     return nb;
149   }
150
151   int ReadNbTria()
152   {
153     integer nb = 0;
154     status_t ret = mesh_get_triangle_count( _tetra_mesh, & nb );
155
156     if ( ret != STATUS_OK ) MG_Error("mesh_get_triangle_count problem");
157     return nb;
158   }
159
160   int ReadNbQuads()
161   {
162     integer nb = 0;
163     status_t ret = mesh_get_quadrangle_count( _tetra_mesh, & nb );
164
165     if ( ret != STATUS_OK ) MG_Error("mesh_get_quadrangle_count problem");
166     return nb;
167   }
168
169   int ReadNbTetra()
170   {
171     integer nb = 0;
172     status_t ret = mesh_get_tetrahedron_count( _tetra_mesh, & nb );
173
174     if ( ret != STATUS_OK ) MG_Error("mesh_get_tetrahedron_count problem");
175     return nb;
176   }
177
178   int ReadNbHexa()
179   {
180     integer nb = 0;
181     status_t ret = mesh_get_hexahedron_count( _tetra_mesh, & nb );
182
183     if ( ret != STATUS_OK ) MG_Error("mesh_get_hexahedron_count problem");
184     return nb;
185   }
186
187   void ResetCounter()
188   {
189     _count = 1;
190   }
191
192   void ReadSubDomain( int* nbNodes, int* faceInd, int* ori, int* domain )
193   {
194     integer tag, seed_type, seed_idx, seed_orientation;
195     status_t ret = mesh_get_subdomain_description( _tetra_mesh, _count,
196                                                    &tag, &seed_type, &seed_idx, &seed_orientation);
197
198     if ( ret != STATUS_OK ) MG_Error( "unable to get a sub-domain description");
199
200     *nbNodes = 3;
201     *faceInd = seed_idx;
202     *domain  = tag;
203     *ori     = seed_orientation;
204
205     ++_count;
206   }
207   void ReadNodeXYZ( double* x, double* y, double *z, int* /*domain*/  )
208   {
209     real coo[3];
210     status_t ret = mesh_get_vertex_coordinates( _tetra_mesh, _count, coo );
211     if ( ret != STATUS_OK ) MG_Error( "unable to get resulting vertices" );
212
213     *x = coo[0];
214     *y = coo[1];
215     *z = coo[2];
216
217     ++_count;
218   }
219
220   void ReadEdgeNodes( int* node1, int* node2, int* domain )
221   {
222     integer vtx[2], tag;
223     status_t ret = mesh_get_edge_vertices( _tetra_mesh, _count, vtx);
224     if (ret != STATUS_OK) MG_Error( "unable to get resulting edge" );
225
226     *node1 = vtx[0];
227     *node2 = vtx[1];
228
229     ret = mesh_get_edge_tag( _tetra_mesh, _count, &tag );
230     if (ret != STATUS_OK) MG_Error( "unable to get resulting edge tag" );
231
232     *domain = tag;
233
234     ++_count;
235   }
236
237   void ReadTriaNodes( int* node1, int* node2, int* node3, int* domain )
238   {
239     integer vtx[3], tag;
240     status_t ret = mesh_get_triangle_vertices( _tetra_mesh, _count, vtx);
241     if (ret != STATUS_OK) MG_Error( "unable to get resulting triangle" );
242
243     *node1 = vtx[0];
244     *node2 = vtx[1];
245     *node3 = vtx[2];
246
247     ret = mesh_get_triangle_tag( _tetra_mesh, _count, &tag );
248     if (ret != STATUS_OK) MG_Error( "unable to get resulting triangle tag" );
249
250     *domain = tag;
251
252     ++_count;
253   }
254
255   void ReadQuadNodes( int* node1, int* node2, int* node3, int* node4, int* domain )
256   {
257     integer vtx[4], tag;
258     status_t ret = mesh_get_quadrangle_vertices( _tetra_mesh, _count, vtx);
259     if (ret != STATUS_OK) MG_Error( "unable to get resulting quadrangle" );
260
261     *node1 = vtx[0];
262     *node2 = vtx[1];
263     *node3 = vtx[2];
264     *node4 = vtx[3];
265
266     ret = mesh_get_quadrangle_tag( _tetra_mesh, _count, &tag );
267     if (ret != STATUS_OK) MG_Error( "unable to get resulting quadrangle tag" );
268
269     *domain = tag;
270
271     ++_count;
272   }
273
274   void ReadTetraNodes( int* node1, int* node2, int* node3, int* node4, int* domain )
275   {
276     integer vtx[4], tag;
277     status_t ret = mesh_get_tetrahedron_vertices( _tetra_mesh, _count, vtx);
278     if (ret != STATUS_OK) MG_Error( "unable to get resulting tetrahedron" );
279
280     *node1 = vtx[0];
281     *node2 = vtx[1];
282     *node3 = vtx[2];
283     *node4 = vtx[3];
284
285     ret = mesh_get_tetrahedron_tag( _tetra_mesh, _count, &tag );
286     if (ret != STATUS_OK) MG_Error( "unable to get resulting tetrahedron tag" );
287
288     *domain = tag;
289
290     ++_count;
291   }
292
293   void ReadHexaNodes( int* node1, int* node2, int* node3, int* node4,
294                      int* node5, int* node6, int* node7, int* node8, int* domain )
295   {
296     integer vtx[8], tag;
297     status_t ret = mesh_get_hexahedron_vertices( _tetra_mesh, _count, vtx);
298     if (ret != STATUS_OK) MG_Error( "unable to get resulting hexahedron" );
299
300     *node1 = vtx[0];
301     *node2 = vtx[1];
302     *node3 = vtx[2];
303     *node4 = vtx[3];
304     *node5 = vtx[4];
305     *node6 = vtx[5];
306     *node7 = vtx[6];
307     *node8 = vtx[7];
308
309     ret = mesh_get_hexahedron_tag( _tetra_mesh, _count, &tag );
310     if (ret != STATUS_OK) MG_Error( "unable to get resulting hexahedron tag" );
311
312     *domain = tag;
313
314     ++_count;
315   }
316
317   void SetNbVertices( int nb )
318   {
319     _xyz.reserve( _xyz.capacity() + nb );
320   }
321
322   void SetNbEdges( int nb )
323   {
324     _edgeNodes.reserve( nb * 2 );
325   }
326
327   void SetNbTria( int nb )
328   {
329     _triaNodes.reserve( nb * 3 );
330   }
331
332   void SetNbReqVertices( int nb )
333   {
334     _nodeSize.reserve( nb );
335   }
336
337   void SetNbReqEdges( int nb )
338   {
339     _nbRequiredEdges = nb;
340   }
341
342   void SetNbReqTria( int nb )
343   {
344     _nbRequiredTria = nb;
345   }
346
347   void AddNode( double x, double y, double z, int domain )
348   {
349     _xyz.push_back( x );
350     _xyz.push_back( y );
351     _xyz.push_back( z );
352   }
353
354   void AddSizeAtNode( double size )
355   {
356     _nodeSize.push_back( size );
357   }
358   
359   void AddEdgeNodes( int node1, int node2, int domain )
360   {
361     _edgeNodes.push_back( node1 );
362     _edgeNodes.push_back( node2 );
363   }
364   
365   void AddTriaNodes( int node1, int node2, int node3, int domain )
366   {
367     _triaNodes.push_back( node1 );
368     _triaNodes.push_back( node2 );
369     _triaNodes.push_back( node3 );
370   }
371
372   int NbNodes()
373   {
374     return _xyz.size() / 3;
375   }
376
377   double* NodeCoord( int iNode )
378   {
379     return & _xyz[ iNode * 3 ];
380   }
381
382   int NbEdges()
383   {
384     return _edgeNodes.size() / 2;
385   }
386
387   int* GetEdgeNodes( int iEdge )
388   {
389     return & _edgeNodes[ iEdge * 2 ];
390   }
391
392   int NbTriangles()
393   {
394     return _triaNodes.size() / 3;
395   }
396
397   int * GetTriaNodes( int iTria )
398   {
399     return & _triaNodes[ iTria * 3 ];
400   }
401
402   int IsVertexRequired( int iNode )
403   {
404     return ! ( iNode < int( NbNodes() - _nodeSize.size() ));
405   }
406
407   double GetSizeAtVertex( int iNode )
408   {
409     return IsVertexRequired( iNode ) ? _nodeSize[ iNode - NbNodes() + _nodeSize.size() ] : 0.;
410   }
411 };
412
413 namespace // functions called by MG library to exchange with the application
414 {
415   status_t get_vertex_count(integer * nbvtx, void *user_data)
416   {
417     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
418     *nbvtx = data->NbNodes();
419
420     return STATUS_OK;
421   }
422
423   status_t get_vertex_coordinates(integer ivtx, real * xyz, void *user_data)
424   {
425     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
426     double* coord = data->NodeCoord( ivtx-1 );
427     for (int j = 0; j < 3; j++)
428       xyz[j] = coord[j];
429
430     return STATUS_OK;
431   }
432   status_t get_edge_count(integer * nbedge, void *user_data)
433   {
434     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
435     *nbedge = data->NbEdges();
436
437     return STATUS_OK;
438   }
439
440   status_t get_edge_vertices(integer iedge, integer * vedge, void *user_data)
441   {
442     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
443     int* nodes = data->GetEdgeNodes( iedge-1 );
444     vedge[0] = nodes[0];
445     vedge[1] = nodes[1];
446
447     return STATUS_OK;
448   }
449
450   status_t get_triangle_count(integer * nbtri, void *user_data)
451   {
452     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
453     *nbtri = data->NbTriangles();
454
455     return STATUS_OK;
456   }
457
458   status_t get_triangle_vertices(integer itri, integer * vtri, void *user_data)
459   {
460     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
461     int* nodes = data->GetTriaNodes( itri-1 );
462     vtri[0] = nodes[0];
463     vtri[1] = nodes[1];
464     vtri[2] = nodes[2];
465
466     return STATUS_OK;
467   }
468
469   // status_t get_triangle_extra_vertices(integer itri, integer * typetri,
470   //                                      integer * evtri, void *user_data)
471   // {
472   //   int j;
473   //   MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
474
475   //   if (1) {
476   //     /* We want to describe a linear "3 nodes" triangle */
477   //     *typetri = MESHGEMS_MESH_ELEMENT_TYPE_TRIA3;
478   //   } else {
479   //     /* We want to describe a quadratic "6 nodes" triangle */
480   //     *typetri = MESHGEMS_MESH_ELEMENT_TYPE_TRIA6;
481   //     for (j = 0; j < 3; j++)
482   //       evtri[j] = 0;             /* the j'th quadratic vertex index of the itri'th triangle */
483   //   }
484
485   //   return STATUS_OK;
486   // }
487
488   // status_t get_tetrahedron_count(integer * nbtetra, void *user_data)
489   // {
490   //   MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
491
492   //   *nbtetra = 0;                 /* the number of tetra in your input mesh (0 if you describe a surface mesh) */
493
494   //   return STATUS_OK;
495   // }
496
497   // status_t get_tetrahedron_vertices(integer itetra, integer * vtetra,
498   //                                   void *user_data)
499   // {
500   //   int j;
501   //   MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
502
503   //   for (j = 0; j < 4; j++)
504   //     vtetra[j] = 0;              /* the j'th vertex index of the itetra'th tetrahedron */
505
506   //   return STATUS_OK;
507   // }
508
509   status_t get_vertex_required_property(integer ivtx, integer * rvtx, void *user_data)
510   {
511     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
512     *rvtx = data->IsVertexRequired( ivtx - 1 );
513
514     return STATUS_OK;
515   }
516
517   status_t get_vertex_weight(integer ivtx, real * wvtx, void *user_data)
518   {
519     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
520     *wvtx = data->GetSizeAtVertex( ivtx - 1 );
521
522     return STATUS_OK;
523   }
524
525   status_t my_message_cb(message_t * msg, void *user_data)
526   {
527     char *desc;
528     message_get_description(msg, &desc);
529
530     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
531     data->AddError( desc );
532
533 #ifdef _DEBUG_
534     //std::cout << desc << std::endl;
535 #endif
536
537     // Compute progress
538     // corresponding messages are:
539     // "  -- PHASE 1 COMPLETED"               => 10 %
540     // "  -- PHASE 2 COMPLETED"               => 25 %
541     // "     ** ITERATION   1"                => 25.* %
542     // "     ** ITERATION   2"                => 25.* %
543     // "  -- PHASE 3 COMPLETED"               => 70 %
544     // "  -- PHASE 4 COMPLETED"               => 98 %
545
546     if ( strncmp( "-- PHASE ", desc + 2, 9 ) == 0 && desc[ 13 ] == 'C' )
547     {
548       const double progress[] = { 10., 25., 70., 98., 100., 100., 100. };
549       int       phase = atoi( desc + 11 );
550       data->_progress = std::max( data->_progress, progress[ phase - 1 ] / 100. );
551     }
552     else if ( strncmp( "** ITERATION ", desc + 5, 13 ) == 0 )
553     {
554       int         iter = atoi( desc + 20 );
555       double   percent = 25. + iter * ( 70 - 25 ) / 20.;
556       data->_progress = std::max( data->_progress, ( percent / 100. ));
557     }
558
559     return STATUS_OK;
560   }
561
562   status_t my_interrupt_callback(integer *interrupt_status, void *user_data)
563   {
564     MG_Tetra_API::LibData* data = (MG_Tetra_API::LibData *) user_data;
565     *interrupt_status = ( data->Cancelled() ? INTERRUPT_STOP : INTERRUPT_CONTINUE );
566
567
568     return STATUS_OK;
569   }
570
571 } // end namespace
572
573
574 void MG_Tetra_API::LibData::Init()
575 {
576   status_t ret;
577
578   // Create the meshgems working context
579   _context = context_new();
580   if ( !_context ) MG_Error( "unable to create a new context" );
581
582   // Set the message callback for the _context.
583   ret = context_set_message_callback( _context, my_message_cb, this );
584   if ( ret != STATUS_OK ) MG_Error("in context_set_message_callback");
585
586   // Create the structure holding the callbacks giving access to triangle mesh
587   _tria_mesh = mesh_new( _context );
588   if ( !_tria_mesh ) MG_Error("unable to create a new mesh");
589
590   // Set callbacks to provide triangle mesh data
591   mesh_set_get_vertex_count( _tria_mesh, get_vertex_count, this );
592   mesh_set_get_vertex_coordinates( _tria_mesh, get_vertex_coordinates, this );
593   mesh_set_get_vertex_required_property( _tria_mesh, get_vertex_required_property, this );
594   mesh_set_get_edge_count( _tria_mesh, get_edge_count, this);
595   mesh_set_get_edge_vertices( _tria_mesh, get_edge_vertices, this );
596   mesh_set_get_triangle_count( _tria_mesh, get_triangle_count, this );
597   mesh_set_get_triangle_vertices( _tria_mesh, get_triangle_vertices, this );
598
599   // Create a tetra session
600   _session = tetra_session_new( _context );
601   if ( !_session ) MG_Error( "unable to create a new tetra session");
602
603   ret = tetra_set_interrupt_callback( _session, my_interrupt_callback, this );
604   if ( ret != STATUS_OK ) MG_Error("in tetra_set_interrupt_callback");
605
606 }
607
608 bool MG_Tetra_API::LibData::Compute()
609 {
610   // Set surface mesh
611   status_t ret = tetra_set_surface_mesh( _session, _tria_mesh );
612   if ( ret != STATUS_OK ) MG_Error( "unable to set surface mesh");
613
614   // Set a sizemap
615   if ( !_nodeSize.empty() )
616   {
617     _sizemap = meshgems_sizemap_new( _tria_mesh, meshgems_sizemap_type_iso_mesh_vertex,
618                                      (void*) &get_vertex_weight, this );
619     if ( !_sizemap ) MG_Error("unable to create a new sizemap");
620
621     ret = tetra_set_sizemap( _session, _sizemap );
622     if ( ret != STATUS_OK ) MG_Error( "unable to set sizemap");
623   }
624
625   //////////////////////////////////////////////////////////////////////////////////////////
626   // const char* file  =  "/tmp/ghs3d_IN.mesh";
627   // mesh_write_mesh( _tria_mesh,file);
628   // std::cout << std::endl << std::endl << "Write " << file << std::endl << std::endl << std::endl;
629
630   ret = tetra_compute_mesh( _session );
631   if ( ret != STATUS_OK ) return false;
632
633   ret = tetra_get_mesh( _session, &_tetra_mesh);
634   if (ret != STATUS_OK) MG_Error( "unable to get resulting mesh");
635
636   //////////////////////////////////////////////////////////////////////////////////////////
637   // file  =  "/tmp/ghs3d_OUT.mesh";
638   // mesh_write_mesh( _tetra_mesh,file);
639   // std::cout << std::endl << std::endl << "Write " << file << std::endl << std::endl << std::endl;
640
641   return true;
642 }
643
644
645 #endif // ifdef USE_MG_LIBS
646
647
648 //================================================================================
649 /*!
650  * \brief Constructor
651  */
652 //================================================================================
653
654 MG_Tetra_API::MG_Tetra_API(volatile bool& cancelled_flag, double& progress)
655 {
656 #ifdef USE_MG_LIBS
657   _useLib = true;
658   _libData = new LibData( cancelled_flag, progress );
659   _libData->Init();
660 #endif
661   if ( getenv("MG_TETRA_USE_EXE"))
662     _useLib = false;
663 }
664
665 //================================================================================
666 /*!
667  * \brief Destructor
668  */
669 //================================================================================
670
671 MG_Tetra_API::~MG_Tetra_API()
672 {
673 #ifdef USE_MG_LIBS
674   delete _libData;
675   _libData = 0;
676 #endif
677   std::set<int>::iterator id = _openFiles.begin();
678   for ( ; id != _openFiles.end(); ++id )
679     ::GmfCloseMesh( *id );
680   _openFiles.clear();
681 }
682
683 //================================================================================
684 /*!
685  * \brief Return the way of MG usage
686  */
687 //================================================================================
688
689 bool MG_Tetra_API::IsLibrary()
690 {
691   return _useLib;
692 }
693
694 //================================================================================
695 /*!
696  * \brief Switch to usage of MG-Tetra executable
697  */
698 //================================================================================
699
700 void MG_Tetra_API::SetUseExecutable()
701 {
702   _useLib = false;
703 }
704
705 //================================================================================
706 /*!
707  * \brief Compute the tetra mesh
708  *  \param [in] cmdLine - a command to run mg_tetra.exe
709  *  \return bool - Ok or not
710  */
711 //================================================================================
712
713 bool MG_Tetra_API::Compute( const std::string& cmdLine, std::string& errStr )
714 {
715   if ( _useLib ) {
716 #ifdef USE_MG_LIBS
717
718     // split cmdLine
719     std::istringstream strm( cmdLine );
720     std::istream_iterator<std::string> sIt( strm ), sEnd;
721     std::vector< std::string > args( sIt, sEnd );
722
723     // set parameters
724     std::string param, value;
725     for ( size_t i = 1; i < args.size(); ++i )
726     {
727       // look for a param name; it starts from "-"
728       param = args[i];
729       if ( param.size() < 2 || param[0] != '-')
730         continue;
731       while ( param[0] == '-')
732         param = param.substr( 1 );
733
734       value = "";
735       while ( i+1 < args.size() && args[i+1][0] != '-' )
736       {
737         if ( !value.empty() ) value += " ";
738         value += args[++i];
739       }
740       if ( !_libData->SetParam( param, value ))
741         std::cout << "Warning: wrong param: '" << param <<"' = '" << value << "'" << std::endl;
742     }
743
744     // compute
745     bool ok =  _libData->Compute();
746
747     GetLog(); // write a log file
748     _logFile = ""; // not to write it again
749
750     return ok;
751 #endif
752   }
753
754   int err = system( cmdLine.c_str() ); // run
755
756   if ( err )
757     errStr = SMESH_Comment("system(mg-tetra.exe ...) command failed with error: ")
758       << strerror( errno );
759
760   return !err;
761
762 }
763
764 //================================================================================
765 /*!
766  * \brief Prepare for reading a mesh data
767  */
768 //================================================================================
769
770 int  MG_Tetra_API::GmfOpenMesh(const char* theFile, int rdOrWr, int * ver, int * dim)
771 {
772   if ( _useLib ) {
773 #ifdef USE_MG_LIBS
774     return 1;
775 #endif
776   }
777   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim );
778   _openFiles.insert( id );
779   return id;
780 }
781
782 //================================================================================
783 /*!
784  * \brief Return nb of entities
785  */
786 //================================================================================
787
788 int MG_Tetra_API::GmfStatKwd( int iMesh, GmfKwdCod what )
789 {
790   if ( _useLib ) {
791 #ifdef USE_MG_LIBS
792     switch ( what )
793     {
794     case GmfSubDomainFromGeom: return _libData->ReadNbSubDomains();
795     case GmfVertices:          return _libData->ReadNbNodes();
796     case GmfEdges:             return _libData->ReadNbEdges();
797     case GmfTriangles:         return _libData->ReadNbTria();
798     case GmfQuadrilaterals:    return _libData->ReadNbQuads();
799     case GmfTetrahedra:        return _libData->ReadNbTetra();
800     case GmfHexahedra:         return _libData->ReadNbHexa();
801     default:                   return 0;
802     }
803     return 0;
804 #endif
805   }
806   return ::GmfStatKwd( iMesh, what );
807 }
808
809 //================================================================================
810 /*!
811  * \brief Prepare for reading some data
812  */
813 //================================================================================
814
815 void MG_Tetra_API::GmfGotoKwd( int iMesh, GmfKwdCod what )
816 {
817   if ( _useLib ) {
818 #ifdef USE_MG_LIBS
819     _libData->ResetCounter();
820     return;
821 #endif
822   }
823   ::GmfGotoKwd( iMesh, what );
824 }
825
826 //================================================================================
827 /*!
828  * \brief Return index of a domain identified by a triangle normal
829  *  \param [in] iMesh - mesh file index
830  *  \param [in] what - must be GmfSubDomainFromGeom
831  *  \param [out] nbNodes - nb nodes in a face
832  *  \param [out] faceInd - face index
833  *  \param [out] ori - face orientation
834  *  \param [out] domain - domain index
835  *  \param [in] dummy - an artificial param used to discriminate from GmfGetLin() reading
836  *              a triangle
837  */
838 //================================================================================
839
840 void MG_Tetra_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int dummy )
841 {
842   if ( _useLib ) {
843 #ifdef USE_MG_LIBS
844     _libData->ReadSubDomain( nbNodes, faceInd, ori, domain );
845     return;
846 #endif
847   }
848   ::GmfGetLin( iMesh, what, nbNodes, faceInd, ori, domain );
849 }
850
851 //================================================================================
852 /*!
853  * \brief Return coordinates of a next node
854  */
855 //================================================================================
856
857 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what,
858                              double* x, double* y, double *z, int* domain )
859 {
860   if ( _useLib ) {
861 #ifdef USE_MG_LIBS
862     _libData->ReadNodeXYZ( x, y, z, domain );
863     return;
864 #endif
865   }
866   ::GmfGetLin(iMesh, what, x, y, z, domain );
867 }
868
869 //================================================================================
870 /*!
871  * \brief Return coordinates of a next node
872  */
873 //================================================================================
874
875 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what,
876                              float* x, float* y, float *z, int* domain )
877 {
878   if ( _useLib ) {
879 #ifdef USE_MG_LIBS
880     double X,Y,Z;
881     _libData->ReadNodeXYZ( &X, &Y, &Z, domain );
882     *x = X;
883     *y = Y;
884     *z = Z;
885     return;
886 #endif
887   }
888   ::GmfGetLin(iMesh, what, x, y, z, domain );
889 }
890
891 //================================================================================
892 /*!
893  * \brief Return node index of a next corner
894  */
895 //================================================================================
896
897 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node )
898 {
899   if ( _useLib ) {
900 #ifdef USE_MG_LIBS
901     node = 0;
902     return;
903 #endif
904   }
905   ::GmfGetLin(iMesh, what, node );
906 }
907
908 //================================================================================
909 /*!
910  * \brief Return node indices of a next edge
911  */
912 //================================================================================
913
914 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node1, int* node2, int* domain )
915 {
916   if ( _useLib ) {
917 #ifdef USE_MG_LIBS
918     _libData->ReadEdgeNodes( node1, node2, domain );
919     return;
920 #endif
921   }
922   ::GmfGetLin( iMesh, what, node1, node2, domain );
923 }
924
925 //================================================================================
926 /*!
927  * \brief Return node indices of a next triangle
928  */
929 //================================================================================
930
931 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what,
932                              int* node1, int* node2, int* node3, int* domain )
933 {
934   if ( _useLib ) {
935 #ifdef USE_MG_LIBS
936     _libData->ReadTriaNodes( node1, node2, node3, domain );
937     return;
938 #endif
939   }
940   ::GmfGetLin(iMesh, what, node1, node2, node3, domain );
941 }
942
943 //================================================================================
944 /*!
945  * \brief Return node indices of a next tetrahedron or quadrangle
946  */
947 //================================================================================
948
949 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what,
950                              int* node1, int* node2, int* node3, int* node4, int* domain )
951 {
952   if ( _useLib ) {
953 #ifdef USE_MG_LIBS
954     if ( what == GmfQuadrilaterals )
955       _libData->ReadQuadNodes( node1, node2, node3, node4, domain );
956     else
957       _libData->ReadTetraNodes( node1, node2, node3, node4, domain );
958     return;
959 #endif
960   }
961   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, domain );
962 }
963
964 //================================================================================
965 /*!
966  * \brief Return node indices of a next hexahedron
967  */
968 //================================================================================
969
970 void MG_Tetra_API::GmfGetLin(int iMesh, GmfKwdCod what,
971                              int* node1, int* node2, int* node3, int* node4,
972                              int* node5, int* node6, int* node7, int* node8,
973                              int* domain )
974 {
975   if ( _useLib ) {
976 #ifdef USE_MG_LIBS
977     _libData->ReadHexaNodes( node1, node2, node3, node4,
978                              node5, node6, node7, node8, domain );
979     return;
980 #endif
981   }
982   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, node5, node6, node7, node8, domain );
983 }
984
985 //================================================================================
986 /*!
987  * \brief Prepare for passing data to MeshGems
988  */
989 //================================================================================
990
991 int  MG_Tetra_API::GmfOpenMesh(const char* theFile, int rdOrWr, int ver, int dim)
992 {
993   if ( _useLib ) {
994 #ifdef USE_MG_LIBS
995     return 1;
996 #endif
997   }
998   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim);
999   _openFiles.insert( id );
1000   return id;
1001 }
1002
1003 //================================================================================
1004 /*!
1005  * \brief Set number of entities
1006  */
1007 //================================================================================
1008
1009 void MG_Tetra_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
1010 {
1011   if ( _useLib ) {
1012 #ifdef USE_MG_LIBS
1013     switch ( what ) {
1014     case GmfVertices:          _libData->SetNbVertices( nb ); break;
1015     case GmfEdges:             _libData->SetNbEdges   ( nb ); break;
1016     case GmfRequiredEdges:     _libData->SetNbReqEdges( nb ); break;
1017     case GmfTriangles:         _libData->SetNbTria    ( nb ); break;
1018     case GmfRequiredTriangles: _libData->SetNbReqTria ( nb ); break;
1019     default:;
1020     }
1021     return;
1022 #endif
1023   }
1024   ::GmfSetKwd(iMesh, what, nb );
1025 }
1026
1027 //================================================================================
1028 /*!
1029  * \brief Add coordinates of a node
1030  */
1031 //================================================================================
1032
1033 void MG_Tetra_API::GmfSetLin(int iMesh, GmfKwdCod what, double x, double y, double z, int domain)
1034 {
1035   if ( _useLib ) {
1036 #ifdef USE_MG_LIBS
1037     _libData->AddNode( x, y, z, domain );
1038     return;
1039 #endif
1040   }
1041   ::GmfSetLin(iMesh, what, x, y, z, domain);
1042 }
1043
1044 //================================================================================
1045 /*!
1046  * \brief Set number of field entities
1047  *  \param [in] iMesh - solution file index
1048  *  \param [in] what - solution type
1049  *  \param [in] nbNodes - nb of entities
1050  *  \param [in] nbTypes - nb of data entries in each entity
1051  *  \param [in] type - types of the data entries
1052  *
1053  * Used to prepare to storing size at nodes
1054  */
1055 //================================================================================
1056
1057 void MG_Tetra_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nbNodes, int dummy, int type[] )
1058 {
1059   if ( _useLib ) {
1060 #ifdef USE_MG_LIBS
1061     if ( what == GmfSolAtVertices ) _libData->SetNbReqVertices( nbNodes );
1062     return;
1063 #endif
1064   }
1065   ::GmfSetKwd(iMesh, what, nbNodes, dummy, type );
1066 }
1067
1068 //================================================================================
1069 /*!
1070  * \brief Add solution data
1071  */
1072 //================================================================================
1073
1074 void MG_Tetra_API::GmfSetLin(int iMesh, GmfKwdCod what, double vals[])
1075 {
1076   if ( _useLib ) {
1077 #ifdef USE_MG_LIBS
1078     _libData->AddSizeAtNode( vals[0] );
1079     return;
1080 #endif
1081   }
1082   ::GmfSetLin(iMesh, what, vals);
1083 }
1084
1085 //================================================================================
1086 /*!
1087  * \brief Add edge nodes
1088  */
1089 //================================================================================
1090
1091 void MG_Tetra_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int domain )
1092 {
1093   if ( _useLib ) {
1094 #ifdef USE_MG_LIBS
1095     _libData->AddEdgeNodes( node1, node2, domain );
1096     return;
1097 #endif
1098   }
1099   ::GmfSetLin(iMesh, what, node1, node2, domain );
1100 }
1101
1102 //================================================================================
1103 /*!
1104  * \brief Add a 'required' flag
1105  */
1106 //================================================================================
1107
1108 void MG_Tetra_API::GmfSetLin(int iMesh, GmfKwdCod what, int id )
1109 {
1110   if ( _useLib ) {
1111 #ifdef USE_MG_LIBS
1112     return;
1113 #endif
1114   }
1115   ::GmfSetLin(iMesh, what, id );
1116 }
1117
1118 //================================================================================
1119 /*!
1120  * \brief Add triangle nodes
1121  */
1122 //================================================================================
1123
1124 void MG_Tetra_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int node3, int domain )
1125 {
1126   if ( _useLib ) {
1127 #ifdef USE_MG_LIBS
1128     _libData->AddTriaNodes( node1, node2, node3, domain );
1129     return;
1130 #endif
1131   }
1132   ::GmfSetLin(iMesh, what, node1, node2, node3, domain );
1133 }
1134
1135 //================================================================================
1136 /*!
1137  * \brief Close a file
1138  */
1139 //================================================================================
1140
1141 void MG_Tetra_API::GmfCloseMesh( int iMesh )
1142 {
1143   if ( _useLib ) {
1144 #ifdef USE_MG_LIBS
1145     return;
1146 #endif
1147   }
1148   ::GmfCloseMesh( iMesh );
1149   _openFiles.erase( iMesh );
1150 }
1151
1152 //================================================================================
1153 /*!
1154  * \brief Return true if the log is not empty
1155  */
1156 //================================================================================
1157
1158 bool MG_Tetra_API::HasLog()
1159 {
1160   if ( _useLib ) {
1161 #ifdef USE_MG_LIBS
1162     return !_libData->GetErrors().empty();
1163 #endif
1164   }
1165   SMESH_File file( _logFile );
1166   return file.size() > 0;
1167 }
1168
1169 //================================================================================
1170 /*!
1171  * \brief Return log contents
1172  */
1173 //================================================================================
1174
1175 std::string MG_Tetra_API::GetLog()
1176 {
1177   if ( _useLib ) {
1178 #ifdef USE_MG_LIBS
1179     const std::string& err = _libData->GetErrors();
1180     if ( !_logFile.empty() && !err.empty() )
1181     {
1182       SMESH_File file( _logFile, /*openForReading=*/false );
1183       file.openForWriting();
1184       file.write( err.c_str(), err.size() );
1185     }
1186     return err;
1187 #endif
1188   }
1189   SMESH_File file( _logFile );
1190   return file.getPos();
1191 }