Salome HOME
Copyright update 2021
[plugins/hybridplugin.git] / src / HYBRIDPlugin / MG_HYBRID_API.cxx
1 // Copyright (C) 2004-2021  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 ReadPyramidNodes( int* node1, int* node2, int* node3, int* node4, int* node5, int* domain )
366   {
367     integer vtx[6];
368     status_t ret = mesh_get_pyramid_vertices( _hybrid_mesh, _count, vtx);
369     if (ret != STATUS_OK) MG_Error( "unable to get resulting pyramid" );
370
371     *node1 = vtx[0];
372     *node2 = vtx[1];
373     *node3 = vtx[2];
374     *node4 = vtx[3];
375     *node5 = vtx[4];
376
377     integer tag;
378     ret = mesh_get_pyramid_tag( _hybrid_mesh, _count, &tag );
379     if (ret != STATUS_OK) MG_Error( "unable to get resulting pyramid tag" );
380
381     *domain = tag;
382
383     ++_count;
384   }
385
386   void ReadCorner( int* node )
387   {
388     if ( _count <= ReadNbCorners() )
389       *node = _corners[ _count - 1 ];
390     else
391       *node = 0;
392
393     ++_count;
394   }
395
396   void SetNbVertices( int nb )
397   {
398     _xyz.reserve( _xyz.capacity() + nb );
399   }
400
401   void SetNbEdges( int nb )
402   {
403     _edgeNodesTags.reserve( nb * 3 );
404   }
405
406   void SetNbTria( int nb )
407   {
408     _triaNodesTags.reserve( nb * 4 );
409   }
410
411   void SetNbQuads( int nb )
412   {
413     _quadNodes.reserve( nb * 4 );
414   }
415
416   void SetNbReqVertices( int nb )
417   {
418     _nodeSize.reserve( nb );
419   }
420
421   void SetNbReqEdges( int nb )
422   {
423     _nbRequiredEdges = nb;
424   }
425
426   void SetNbReqTria( int nb )
427   {
428     _nbRequiredTria = nb;
429   }
430
431   void AddNode( double x, double y, double z, int /*domain*/ )
432   {
433     _xyz.push_back( x );
434     _xyz.push_back( y );
435     _xyz.push_back( z );
436   }
437
438   void AddSizeAtNode( double size )
439   {
440     _nodeSize.push_back( size );
441   }
442   
443   void AddEdgeNodes( int node1, int node2, int domain )
444   {
445     _edgeNodesTags.push_back( node1 );
446     _edgeNodesTags.push_back( node2 );
447     _edgeNodesTags.push_back( domain );
448   }
449   
450   void AddTriaNodes( int node1, int node2, int node3, int domain )
451   {
452     _triaNodesTags.push_back( node1 );
453     _triaNodesTags.push_back( node2 );
454     _triaNodesTags.push_back( node3 );
455     _triaNodesTags.push_back( domain );
456   }
457
458   void AddQuadNodes( int node1, int node2, int node3, int node4 )
459   {
460     _quadNodes.push_back( node1 );
461     _quadNodes.push_back( node2 );
462     _quadNodes.push_back( node3 );
463     _quadNodes.push_back( node4 );
464   }
465
466   int NbNodes()
467   {
468     return _xyz.size() / 3;
469   }
470
471   double* NodeCoord( int iNode )
472   {
473     return & _xyz[ iNode * 3 ];
474   }
475
476   int NbEdges()
477   {
478     return _edgeNodesTags.size() / 3;
479   }
480
481   int* GetEdgeNodes( int iEdge )
482   {
483     return & _edgeNodesTags[ iEdge * 3 ];
484   }
485
486   int GetEdgeTag( int iEdge )
487   {
488     return _edgeNodesTags[ iEdge * 3 + 2 ];
489   }
490
491   int NbTriangles()
492   {
493     return _triaNodesTags.size() / 4;
494   }
495
496   int * GetTriaNodes( int iTria )
497   {
498     return & _triaNodesTags[ iTria * 4 ];
499   }
500
501   int GetTriaTag( int iTria )
502   {
503     return _triaNodesTags[ iTria * 4 + 3 ];
504   }
505
506   int NbQuads()
507   {
508     return _quadNodes.size() / 4;
509   }
510
511   int * GetQuadNodes( int iQ )
512   {
513     return & _quadNodes[ iQ * 4 ];
514   }
515
516   int IsVertexRequired( int iNode )
517   {
518     return ! ( iNode < int( NbNodes() - _nodeSize.size() ));
519   }
520
521   double GetSizeAtVertex( int iNode )
522   {
523     return IsVertexRequired( iNode ) ? _nodeSize[ iNode - NbNodes() + _nodeSize.size() ] : 0.;
524   }
525 };
526
527 namespace // functions called by MG library to exchange with the application
528 {
529   status_t get_vertex_count(integer * nbvtx, void *user_data)
530   {
531     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
532     *nbvtx = data->NbNodes();
533
534     return STATUS_OK;
535   }
536
537   status_t get_vertex_coordinates(integer ivtx, real * xyz, void *user_data)
538   {
539     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
540     double* coord = data->NodeCoord( ivtx-1 );
541     for (int j = 0; j < 3; j++)
542       xyz[j] = coord[j];
543
544     return STATUS_OK;
545   }
546   status_t get_edge_count(integer * nbedge, void *user_data)
547   {
548     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
549     *nbedge = data->NbEdges();
550
551     return STATUS_OK;
552   }
553
554   status_t get_edge_vertices(integer iedge, integer * vedge, void *user_data)
555   {
556     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
557     int* nodes = data->GetEdgeNodes( iedge-1 );
558     vedge[0] = nodes[0];
559     vedge[1] = nodes[1];
560
561     return STATUS_OK;
562   }
563
564   status_t get_edge_tag(integer iedge, integer * tag, void *user_data)
565   {
566     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
567     * tag = data->GetEdgeTag( iedge-1 );
568
569     return STATUS_OK;
570   }
571
572   status_t get_triangle_count(integer * nbtri, void *user_data)
573   {
574     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
575     *nbtri = data->NbTriangles();
576
577     return STATUS_OK;
578   }
579
580   status_t get_triangle_vertices(integer itri, integer * vtri, void *user_data)
581   {
582     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
583     int* nodes = data->GetTriaNodes( itri-1 );
584     vtri[0] = nodes[0];
585     vtri[1] = nodes[1];
586     vtri[2] = nodes[2];
587
588     return STATUS_OK;
589   }
590
591   status_t get_triangle_tag(integer itri, integer * tag, void *user_data)
592   {
593     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
594     * tag = data->GetTriaTag( itri-1 );
595
596     return STATUS_OK;
597   }
598
599   status_t get_quadrangle_count(integer * nbq, void *user_data)
600   {
601     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
602     *nbq = data->NbQuads();
603
604     return STATUS_OK;
605   }
606
607   status_t get_quadrangle_vertices(integer iq, integer * vq, void *user_data)
608   {
609     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
610     int* nodes = data->GetQuadNodes( iq-1 );
611     vq[0] = nodes[0];
612     vq[1] = nodes[1];
613     vq[2] = nodes[2];
614     vq[3] = nodes[3];
615
616     return STATUS_OK;
617   }
618
619   status_t get_vertex_required_property(integer ivtx, integer * rvtx, void *user_data)
620   {
621     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
622     *rvtx = data->IsVertexRequired( ivtx - 1 );
623
624     return STATUS_OK;
625   }
626
627   status_t get_vertex_weight(integer ivtx, real * wvtx, void *user_data)
628   {
629     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
630     *wvtx = data->GetSizeAtVertex( ivtx - 1 );
631
632     return STATUS_OK;
633   }
634
635   status_t my_message_cb(message_t * msg, void *user_data)
636   {
637     char *desc;
638     message_get_description(msg, &desc);
639
640     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
641     data->AddError( desc );
642
643     return STATUS_OK;
644   }
645
646   status_t my_interrupt_callback(integer *interrupt_status, void *user_data)
647   {
648     MG_HYBRID_API::LibData* data = (MG_HYBRID_API::LibData *) user_data;
649     *interrupt_status = ( data->Cancelled() ? INTERRUPT_STOP : INTERRUPT_CONTINUE );
650
651     return STATUS_OK;
652   }
653
654 } // end namespace
655
656
657 void MG_HYBRID_API::LibData::Init()
658 {
659   status_t ret;
660
661   // Create the meshgems working context
662   _context = context_new();
663   if ( !_context ) MG_Error( "unable to create a new context" );
664
665   // Set the message callback for the _context.
666   ret = context_set_message_callback( _context, my_message_cb, this );
667   if ( ret != STATUS_OK ) MG_Error("in context_set_message_callback");
668
669   // Create the structure holding the callbacks giving access to triangle mesh
670   _surf_mesh = mesh_new( _context );
671   if ( !_surf_mesh ) MG_Error("unable to create a new mesh");
672
673   // Set callbacks to provide triangle mesh data
674   mesh_set_get_vertex_count( _surf_mesh, get_vertex_count, this );
675   mesh_set_get_vertex_coordinates( _surf_mesh, get_vertex_coordinates, this );
676   mesh_set_get_vertex_required_property( _surf_mesh, get_vertex_required_property, this );
677   mesh_set_get_edge_count( _surf_mesh, get_edge_count, this);
678   mesh_set_get_edge_vertices( _surf_mesh, get_edge_vertices, this );
679   mesh_set_get_edge_tag( _surf_mesh, get_edge_tag, this );
680   mesh_set_get_triangle_count( _surf_mesh, get_triangle_count, this );
681   mesh_set_get_triangle_vertices( _surf_mesh, get_triangle_vertices, this );
682   mesh_set_get_triangle_tag( _surf_mesh, get_triangle_tag, this );
683   mesh_set_get_quadrangle_count( _surf_mesh, get_quadrangle_count, this );
684   mesh_set_get_quadrangle_vertices( _surf_mesh, get_quadrangle_vertices, this );
685
686   // Create a hybrid session
687   _session = hybrid_session_new( _context );
688   if ( !_session ) MG_Error( "unable to create a new hybrid session");
689
690   ret = hybrid_set_interrupt_callback( _session, my_interrupt_callback, this );
691   if ( ret != STATUS_OK ) MG_Error("in hybrid_set_interrupt_callback");
692
693 }
694
695 bool MG_HYBRID_API::LibData::Compute()
696 {
697   // Set surface mesh
698   status_t ret = hybrid_set_surface_mesh( _session, _surf_mesh );
699   if ( ret != STATUS_OK ) MG_Error( "unable to set surface mesh");
700
701 #ifndef WIN32
702   // Set a sizemap
703   if ( !_nodeSize.empty() )
704   {
705     _sizemap = meshgems_sizemap_new( _surf_mesh, meshgems_sizemap_type_iso_mesh_vertex,
706                                      (void*) &get_vertex_weight, this );
707     if ( !_sizemap ) MG_Error("unable to create a new sizemap");
708
709     ret = hybrid_set_sizemap( _session, _sizemap );
710     if ( ret != STATUS_OK ) MG_Error( "unable to set sizemap");
711   }
712 #endif
713
714   //////////////////////////////////////////////////////////////////////////////////////////
715   // const char* file  =  "/tmp/ghs3d_IN.mesh";
716   // mesh_write_mesh( _surf_mesh,file);
717   // std::cout << std::endl << std::endl << "Write " << file << std::endl << std::endl << std::endl;
718
719   ret = hybrid_compute_mesh( _session );
720   if ( ret != STATUS_OK ) return false;
721
722   ret = hybrid_get_mesh( _session, &_hybrid_mesh);
723   if (ret != STATUS_OK) MG_Error( "unable to get resulting mesh");
724
725   //////////////////////////////////////////////////////////////////////////////////////////
726   // file  =  "/tmp/ghs3d_OUT.mesh";
727   // mesh_write_mesh( _hybrid_mesh,file);
728   // std::cout << std::endl << std::endl << "Write " << file << std::endl << std::endl << std::endl;
729
730   return true;
731 }
732
733 #else // ifdef USE_MG_LIBS
734
735 struct MG_HYBRID_API::LibData // to avoid compiler warnings
736 {
737   volatile bool& _cancelled_flag;
738   double& _progress;
739   LibData(volatile bool& cancelled_flag, double& progress): _cancelled_flag{cancelled_flag}, _progress{progress} {}
740 };
741
742 #endif // ifdef USE_MG_LIBS
743
744
745 //================================================================================
746 /*!
747  * \brief Constructor
748  */
749 //================================================================================
750
751 MG_HYBRID_API::MG_HYBRID_API(volatile bool& cancelled_flag, double& progress)
752 {
753   _useLib = false;
754   _libData = new LibData( cancelled_flag, progress );
755 #ifdef USE_MG_LIBS
756   _useLib = true;
757   _libData->Init();
758   if ( getenv("MG_HYBRID_USE_EXE"))
759     _useLib = false;
760 #endif
761 }
762
763 //================================================================================
764 /*!
765  * \brief Destructor
766  */
767 //================================================================================
768
769 MG_HYBRID_API::~MG_HYBRID_API()
770 {
771 #ifdef USE_MG_LIBS
772   delete _libData;
773   _libData = 0;
774 #endif
775   std::set<int>::iterator id = _openFiles.begin();
776   for ( ; id != _openFiles.end(); ++id )
777     ::GmfCloseMesh( *id );
778   _openFiles.clear();
779 }
780
781 //================================================================================
782 /*!
783  * \brief Return the way of MG usage
784  */
785 //================================================================================
786
787 bool MG_HYBRID_API::IsLibrary()
788 {
789   return _useLib;
790 }
791
792 //================================================================================
793 /*!
794  * \brief Switch to usage of MG-HYBRID executable
795  */
796 //================================================================================
797
798 void MG_HYBRID_API::SetUseExecutable()
799 {
800   _useLib = false;
801 }
802
803 //================================================================================
804 /*!
805  * \brief Compute the hybrid mesh
806  *  \param [in] cmdLine - a command to run mg_hybrid.exe
807  *  \return bool - Ok or not
808  */
809 //================================================================================
810
811 bool MG_HYBRID_API::Compute( const std::string& cmdLine, std::string& errStr )
812 {
813   if ( _useLib ) {
814 #ifdef USE_MG_LIBS
815
816     // split cmdLine
817     std::istringstream strm( cmdLine );
818     std::istream_iterator<std::string> sIt( strm ), sEnd;
819     std::vector< std::string > args( sIt, sEnd );
820
821     // set parameters
822     std::string param, value;
823     for ( size_t i = 1; i < args.size(); ++i )
824     {
825       // look for a param name; it starts from "-"
826       param = args[i];
827       if ( param.size() < 2 || param[0] != '-')
828         continue;
829       while ( param[0] == '-')
830         param = param.substr( 1 );
831
832       value = "";
833       while ( i+1 < args.size() && args[i+1][0] != '-' )
834       {
835         if ( strncmp( "1>", args[i+1].c_str(), 2 ) == 0 )
836           break;
837         if ( !value.empty() ) value += " ";
838         value += args[++i];
839       }
840       if ( !_libData->SetParam( param, value ))
841         std::cout << "Warning: wrong param: '" << param <<"' = '" << value << "'" << std::endl;
842     }
843
844     // compute
845     bool ok =  _libData->Compute();
846
847     GetLog(); // write a log file
848     _logFile = ""; // not to write it again
849
850     return ok;
851 #endif
852   }
853
854   int err = system( cmdLine.c_str() ); // run
855
856   if ( err )
857     errStr = SMESH_Comment("system(mg-hybrid.exe ...) command failed with error: ")
858       << strerror( errno );
859
860   return !err;
861
862 }
863
864 //================================================================================
865 /*!
866  * \brief Prepare for reading a mesh data
867  */
868 //================================================================================
869
870 int  MG_HYBRID_API::GmfOpenMesh(const char* theFile, int rdOrWr, int * ver, int * dim)
871 {
872   if ( _useLib ) {
873 #ifdef USE_MG_LIBS
874     return 1;
875 #endif
876   }
877   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim );
878   _openFiles.insert( id );
879   return id;
880 }
881
882 //================================================================================
883 /*!
884  * \brief Return nb of entities
885  */
886 //================================================================================
887
888 int MG_HYBRID_API::GmfStatKwd( int iMesh, GmfKwdCod what )
889 {
890   if ( _useLib ) {
891 #ifdef USE_MG_LIBS
892     switch ( what )
893     {
894     case GmfSubDomainFromGeom: return _libData->ReadNbSubDomains();
895     case GmfVertices:          return _libData->ReadNbNodes();
896     case GmfEdges:             return _libData->ReadNbEdges();
897     case GmfTriangles:         return _libData->ReadNbTria();
898     case GmfQuadrilaterals:    return _libData->ReadNbQuads();
899     case GmfTetrahedra:        return _libData->ReadNbTetra();
900     case GmfPrisms:            return _libData->ReadNbPrisms();
901     case GmfHexahedra:         return _libData->ReadNbHexa();
902     case GmfCorners:           return _libData->ReadNbCorners();
903     default:                   return 0;
904     }
905     return 0;
906 #endif
907   }
908   return ::GmfStatKwd( iMesh, what );
909 }
910
911 //================================================================================
912 /*!
913  * \brief Prepare for reading some data
914  */
915 //================================================================================
916
917 void MG_HYBRID_API::GmfGotoKwd( int iMesh, GmfKwdCod what )
918 {
919   if ( _useLib ) {
920 #ifdef USE_MG_LIBS
921     _libData->ResetCounter();
922     return;
923 #endif
924   }
925   ::GmfGotoKwd( iMesh, what );
926 }
927
928 //================================================================================
929 /*!
930  * \brief Return index of a domain identified by a triangle normal
931  *  \param [in] iMesh - mesh file index
932  *  \param [in] what - must be GmfSubDomainFromGeom
933  *  \param [out] nbNodes - nb nodes in a face
934  *  \param [out] faceInd - face index
935  *  \param [out] ori - face orientation
936  *  \param [out] domain - domain index
937  *  \param [in] dummy - an artificial param used to discriminate from GmfGetLin() reading
938  *              a triangle
939  */
940 //================================================================================
941
942 void MG_HYBRID_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int /*dummy*/ )
943 {
944   if ( _useLib ) {
945 #ifdef USE_MG_LIBS
946     _libData->ReadSubDomain( nbNodes, faceInd, ori, domain );
947     return;
948 #endif
949   }
950   ::GmfGetLin( iMesh, what, nbNodes, faceInd, ori, domain );
951 }
952
953 //================================================================================
954 /*!
955  * \brief Return coordinates of a next node
956  */
957 //================================================================================
958
959 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
960                              double* x, double* y, double *z, int* domain )
961 {
962   if ( _useLib ) {
963 #ifdef USE_MG_LIBS
964     _libData->ReadNodeXYZ( x, y, z, domain );
965     return;
966 #endif
967   }
968   ::GmfGetLin(iMesh, what, x, y, z, domain );
969 }
970
971 //================================================================================
972 /*!
973  * \brief Return coordinates of a next node
974  */
975 //================================================================================
976
977 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
978                              float* x, float* y, float *z, int* domain )
979 {
980   if ( _useLib ) {
981 #ifdef USE_MG_LIBS
982     double X,Y,Z;
983     _libData->ReadNodeXYZ( &X, &Y, &Z, domain );
984     *x = X;
985     *y = Y;
986     *z = Z;
987     return;
988 #endif
989   }
990   ::GmfGetLin(iMesh, what, x, y, z, domain );
991 }
992
993 //================================================================================
994 /*!
995  * \brief Return node index of a next corner
996  */
997 //================================================================================
998
999 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node )
1000 {
1001   if ( _useLib ) {
1002 #ifdef USE_MG_LIBS
1003     _libData->ReadCorner( node );
1004     return;
1005 #endif
1006   }
1007   ::GmfGetLin(iMesh, what, node );
1008 }
1009
1010 //================================================================================
1011 /*!
1012  * \brief Return node indices of a next edge
1013  */
1014 //================================================================================
1015
1016 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node1, int* node2, int* domain )
1017 {
1018   if ( _useLib ) {
1019 #ifdef USE_MG_LIBS
1020     _libData->ReadEdgeNodes( node1, node2, domain );
1021     return;
1022 #endif
1023   }
1024   ::GmfGetLin( iMesh, what, node1, node2, domain );
1025 }
1026
1027 //================================================================================
1028 /*!
1029  * \brief Return node indices of a next triangle
1030  */
1031 //================================================================================
1032
1033 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
1034                              int* node1, int* node2, int* node3, int* domain )
1035 {
1036   if ( _useLib ) {
1037 #ifdef USE_MG_LIBS
1038     _libData->ReadTriaNodes( node1, node2, node3, domain );
1039     return;
1040 #endif
1041   }
1042   ::GmfGetLin(iMesh, what, node1, node2, node3, domain );
1043 }
1044
1045 //================================================================================
1046 /*!
1047  * \brief Return node indices of a next tetrahedron or quadrangle
1048  */
1049 //================================================================================
1050
1051 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
1052                              int* node1, int* node2, int* node3, int* node4, int* domain )
1053 {
1054   if ( _useLib ) {
1055 #ifdef USE_MG_LIBS
1056     if ( what == GmfQuadrilaterals )
1057       _libData->ReadQuadNodes( node1, node2, node3, node4, domain );
1058     else
1059       _libData->ReadTetraNodes( node1, node2, node3, node4, domain );
1060     return;
1061 #endif
1062   }
1063   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, domain );
1064 }
1065
1066 //================================================================================
1067 /*!
1068  * \brief Return node indices of a next hexahedron
1069  */
1070 //================================================================================
1071
1072 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
1073                              int* node1, int* node2, int* node3, int* node4,
1074                              int* node5, int* node6, int* node7, int* node8,
1075                              int* domain )
1076 {
1077   if ( _useLib ) {
1078 #ifdef USE_MG_LIBS
1079     _libData->ReadHexaNodes( node1, node2, node3, node4, node5, node6, node7, node8, domain );
1080     return;
1081 #endif
1082   }
1083   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, node5, node6, node7, node8, domain );
1084 }
1085
1086 //================================================================================
1087 /*!
1088  * \brief Return node indices of a next pyramid
1089  */
1090 //================================================================================
1091
1092 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
1093                               int* node1, int* node2, int* node3, int* node4,
1094                               int* node5, int* domain )
1095 {
1096   if ( _useLib ) {
1097 #ifdef USE_MG_LIBS
1098     _libData->ReadPyramidNodes( node1, node2, node3, node4, node5, domain );
1099     return;
1100 #endif
1101   }
1102   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, node5, domain );
1103 }
1104
1105 //================================================================================
1106 /*!
1107  * \brief Return node indices of a next prism
1108  */
1109 //================================================================================
1110
1111 void MG_HYBRID_API::GmfGetLin(int iMesh, GmfKwdCod what,
1112                               int* node1, int* node2, int* node3, int* node4,
1113                               int* node5, int* node6, int* domain )
1114 {
1115   if ( _useLib ) {
1116 #ifdef USE_MG_LIBS
1117     _libData->ReadPrismNodes( node1, node2, node3, node4, node5, node6, domain );
1118     return;
1119 #endif
1120   }
1121   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, node5, node6, domain );
1122 }
1123
1124 //================================================================================
1125 /*!
1126  * \brief Prepare for passing data to MeshGems
1127  */
1128 //================================================================================
1129
1130 int  MG_HYBRID_API::GmfOpenMesh(const char* theFile, int rdOrWr, int ver, int dim)
1131 {
1132   if ( _useLib ) {
1133 #ifdef USE_MG_LIBS
1134     return 1;
1135 #endif
1136   }
1137   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim);
1138   _openFiles.insert( id );
1139   return id;
1140 }
1141
1142 //================================================================================
1143 /*!
1144  * \brief Set number of entities
1145  */
1146 //================================================================================
1147
1148 void MG_HYBRID_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
1149 {
1150   if ( _useLib ) {
1151 #ifdef USE_MG_LIBS
1152     switch ( what ) {
1153     case GmfVertices:          _libData->SetNbVertices( nb ); break;
1154     case GmfEdges:             _libData->SetNbEdges   ( nb ); break;
1155     case GmfRequiredEdges:     _libData->SetNbReqEdges( nb ); break;
1156     case GmfTriangles:         _libData->SetNbTria    ( nb ); break;
1157     case GmfRequiredTriangles: _libData->SetNbReqTria ( nb ); break;
1158     default:;
1159     }
1160     return;
1161 #endif
1162   }
1163   ::GmfSetKwd(iMesh, what, nb );
1164 }
1165
1166 //================================================================================
1167 /*!
1168  * \brief Add coordinates of a node
1169  */
1170 //================================================================================
1171
1172 void MG_HYBRID_API::GmfSetLin(int iMesh, GmfKwdCod what, double x, double y, double z, int domain)
1173 {
1174   if ( _useLib ) {
1175 #ifdef USE_MG_LIBS
1176     _libData->AddNode( x, y, z, domain );
1177     return;
1178 #endif
1179   }
1180   ::GmfSetLin(iMesh, what, x, y, z, domain);
1181 }
1182
1183 //================================================================================
1184 /*!
1185  * \brief Set number of field entities
1186  *  \param [in] iMesh - solution file index
1187  *  \param [in] what - solution type
1188  *  \param [in] nbNodes - nb of entities
1189  *  \param [in] nbTypes - nb of data entries in each entity
1190  *  \param [in] type - types of the data entries
1191  *
1192  * Used to prepare to storing size at nodes
1193  */
1194 //================================================================================
1195
1196 void MG_HYBRID_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nbNodes, int dummy, int type[] )
1197 {
1198   if ( _useLib ) {
1199 #ifdef USE_MG_LIBS
1200     if ( what == GmfSolAtVertices ) _libData->SetNbReqVertices( nbNodes );
1201     return;
1202 #endif
1203   }
1204   ::GmfSetKwd(iMesh, what, nbNodes, dummy, type );
1205 }
1206
1207 //================================================================================
1208 /*!
1209  * \brief Add solution data
1210  */
1211 //================================================================================
1212
1213 void MG_HYBRID_API::GmfSetLin(int iMesh, GmfKwdCod what, double vals[])
1214 {
1215   if ( _useLib ) {
1216 #ifdef USE_MG_LIBS
1217     _libData->AddSizeAtNode( vals[0] );
1218     return;
1219 #endif
1220   }
1221   ::GmfSetLin(iMesh, what, vals);
1222 }
1223
1224 //================================================================================
1225 /*!
1226  * \brief Add edge nodes
1227  */
1228 //================================================================================
1229
1230 void MG_HYBRID_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int domain )
1231 {
1232   if ( _useLib ) {
1233 #ifdef USE_MG_LIBS
1234     _libData->AddEdgeNodes( node1, node2, domain );
1235     return;
1236 #endif
1237   }
1238   ::GmfSetLin(iMesh, what, node1, node2, domain );
1239 }
1240
1241 //================================================================================
1242 /*!
1243  * \brief Add a 'required' flag
1244  */
1245 //================================================================================
1246
1247 void MG_HYBRID_API::GmfSetLin(int iMesh, GmfKwdCod what, int id )
1248 {
1249   if ( _useLib ) {
1250 #ifdef USE_MG_LIBS
1251     return;
1252 #endif
1253   }
1254   ::GmfSetLin(iMesh, what, id );
1255 }
1256
1257 //================================================================================
1258 /*!
1259  * \brief Add triangle nodes
1260  */
1261 //================================================================================
1262
1263 void MG_HYBRID_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int node3, int domain )
1264 {
1265   if ( _useLib ) {
1266 #ifdef USE_MG_LIBS
1267     _libData->AddTriaNodes( node1, node2, node3, domain );
1268     return;
1269 #endif
1270   }
1271   ::GmfSetLin(iMesh, what, node1, node2, node3, domain );
1272 }
1273
1274 //================================================================================
1275 /*!
1276  * \brief Add quadrangle nodes
1277  */
1278 //================================================================================
1279
1280 void MG_HYBRID_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int node3, int node4, int domain )
1281 {
1282   if ( _useLib ) {
1283 #ifdef USE_MG_LIBS
1284     _libData->AddQuadNodes( node1, node2, node3, node4 );
1285     return;
1286 #endif
1287   }
1288   ::GmfSetLin(iMesh, what, node1, node2, node3, node4, domain );
1289 }
1290
1291 //================================================================================
1292 /*!
1293  * \brief Close a file
1294  */
1295 //================================================================================
1296
1297 void MG_HYBRID_API::GmfCloseMesh( int iMesh )
1298 {
1299   if ( _useLib ) {
1300 #ifdef USE_MG_LIBS
1301     return;
1302 #endif
1303   }
1304   ::GmfCloseMesh( iMesh );
1305   _openFiles.erase( iMesh );
1306 }
1307
1308 //================================================================================
1309 /*!
1310  * \brief Return true if the log is not empty
1311  */
1312 //================================================================================
1313
1314 bool MG_HYBRID_API::HasLog()
1315 {
1316   if ( _useLib ) {
1317 #ifdef USE_MG_LIBS
1318     return !_libData->GetErrors().empty();
1319 #endif
1320   }
1321   SMESH_File file( _logFile );
1322   return file.size() > 0;
1323 }
1324
1325 //================================================================================
1326 /*!
1327  * \brief Return log contents
1328  */
1329 //================================================================================
1330
1331 std::string MG_HYBRID_API::GetLog()
1332 {
1333   if ( _useLib ) {
1334 #ifdef USE_MG_LIBS
1335     const std::string& err = _libData->GetErrors();
1336     if ( !_logFile.empty() && !err.empty() )
1337     {
1338       SMESH_File file( _logFile, /*openForReading=*/false );
1339       file.openForWriting();
1340       file.write( err.c_str(), err.size() );
1341     }
1342     return err;
1343 #endif
1344   }
1345   SMESH_File file( _logFile );
1346   return file.getPos();
1347 }