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