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