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