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