Salome HOME
53538: Salome application crashes after computing mesh
[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 ( !value.empty() ) value += " ";
794         value += args[++i];
795       }
796       if ( !_libData->SetParam( param, value ))
797         std::cout << "Warning: wrong param: '" << param <<"' = '" << value << "'" << std::endl;
798     }
799
800     // compute
801     bool ok =  _libData->Compute();
802
803     GetLog(); // write a log file
804     _logFile = ""; // not to write it again
805
806     return ok;
807 #endif
808   }
809
810   int err = system( cmdLine.c_str() ); // run
811
812   if ( err )
813     errStr = SMESH_Comment("system(mg-hexa.exe ...) command failed with error: ")
814       << strerror( errno );
815
816   return !err;
817
818 }
819
820 //================================================================================
821 /*!
822  * \brief Prepare for reading a mesh data
823  */
824 //================================================================================
825
826 int  MG_Hexotic_API::GmfOpenMesh(const char* theFile, int rdOrWr, int * ver, int * dim)
827 {
828   if ( _useLib ) {
829 #ifdef USE_MG_LIBS
830     return 1;
831 #endif
832   }
833   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim );
834   _openFiles.insert( id );
835   return id;
836 }
837
838 //================================================================================
839 /*!
840  * \brief Return nb of entities
841  */
842 //================================================================================
843
844 int MG_Hexotic_API::GmfStatKwd( int iMesh, GmfKwdCod what )
845 {
846   if ( _useLib ) {
847 #ifdef USE_MG_LIBS
848     switch ( what )
849     {
850     case GmfSubDomainFromGeom: return _libData->ReadNbSubDomains();
851     case GmfVertices:          return _libData->ReadNbNodes();
852     case GmfEdges:             return _libData->ReadNbEdges();
853     case GmfTriangles:         return _libData->ReadNbTria();
854     case GmfQuadrilaterals:    return _libData->ReadNbQuads();
855     case GmfTetrahedra:        return _libData->ReadNbTetra();
856     case GmfHexahedra:         return _libData->ReadNbHexa();
857     case GmfCorners:           return _libData->ReadNbCorners();
858     default:                   return 0;
859     }
860     return 0;
861 #endif
862   }
863   return ::GmfStatKwd( iMesh, what );
864 }
865
866 //================================================================================
867 /*!
868  * \brief Prepare for reading some data
869  */
870 //================================================================================
871
872 void MG_Hexotic_API::GmfGotoKwd( int iMesh, GmfKwdCod what )
873 {
874   if ( _useLib ) {
875 #ifdef USE_MG_LIBS
876     _libData->ResetCounter();
877     return;
878 #endif
879   }
880   ::GmfGotoKwd( iMesh, what );
881 }
882
883 //================================================================================
884 /*!
885  * \brief Return index of a domain identified by a triangle normal
886  *  \param [in] iMesh - mesh file index
887  *  \param [in] what - must be GmfSubDomainFromGeom
888  *  \param [out] nbNodes - nb nodes in a face
889  *  \param [out] faceInd - face index
890  *  \param [out] ori - face orientation
891  *  \param [out] domain - domain index
892  *  \param [in] dummy - an artificial param used to discriminate from GmfGetLin() reading
893  *              a triangle
894  */
895 //================================================================================
896
897 void MG_Hexotic_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int dummy )
898 {
899   if ( _useLib ) {
900 #ifdef USE_MG_LIBS
901     _libData->ReadSubDomain( nbNodes, faceInd, ori, domain );
902     return;
903 #endif
904   }
905   ::GmfGetLin( iMesh, what, nbNodes, faceInd, ori, domain );
906 }
907
908 //================================================================================
909 /*!
910  * \brief Return coordinates of a next node
911  */
912 //================================================================================
913
914 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
915                              double* x, double* y, double *z, int* domain )
916 {
917   if ( _useLib ) {
918 #ifdef USE_MG_LIBS
919     _libData->ReadNodeXYZ( x, y, z, domain );
920     return;
921 #endif
922   }
923   ::GmfGetLin(iMesh, what, x, y, z, domain );
924 }
925
926 //================================================================================
927 /*!
928  * \brief Return coordinates of a next node
929  */
930 //================================================================================
931
932 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
933                              float* x, float* y, float *z, int* domain )
934 {
935   if ( _useLib ) {
936 #ifdef USE_MG_LIBS
937     double X,Y,Z;
938     _libData->ReadNodeXYZ( &X, &Y, &Z, domain );
939     *x = X;
940     *y = Y;
941     *z = Z;
942     return;
943 #endif
944   }
945   ::GmfGetLin(iMesh, what, x, y, z, domain );
946 }
947
948 //================================================================================
949 /*!
950  * \brief Return node index of a next corner
951  */
952 //================================================================================
953
954 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node )
955 {
956   if ( _useLib ) {
957 #ifdef USE_MG_LIBS
958     _libData->ReadCorner( node );
959     return;
960 #endif
961   }
962   ::GmfGetLin(iMesh, what, node );
963 }
964
965 //================================================================================
966 /*!
967  * \brief Return node indices of a next edge
968  */
969 //================================================================================
970
971 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node1, int* node2, int* domain )
972 {
973   if ( _useLib ) {
974 #ifdef USE_MG_LIBS
975     _libData->ReadEdgeNodes( node1, node2, domain );
976     return;
977 #endif
978   }
979   ::GmfGetLin( iMesh, what, node1, node2, domain );
980 }
981
982 //================================================================================
983 /*!
984  * \brief Return node indices of a next triangle
985  */
986 //================================================================================
987
988 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
989                              int* node1, int* node2, int* node3, int* domain )
990 {
991   if ( _useLib ) {
992 #ifdef USE_MG_LIBS
993     _libData->ReadTriaNodes( node1, node2, node3, domain );
994     return;
995 #endif
996   }
997   ::GmfGetLin(iMesh, what, node1, node2, node3, domain );
998 }
999
1000 //================================================================================
1001 /*!
1002  * \brief Return node indices of a next tetrahedron or quadrangle
1003  */
1004 //================================================================================
1005
1006 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
1007                              int* node1, int* node2, int* node3, int* node4, int* domain )
1008 {
1009   if ( _useLib ) {
1010 #ifdef USE_MG_LIBS
1011     if ( what == GmfQuadrilaterals )
1012       _libData->ReadQuadNodes( node1, node2, node3, node4, domain );
1013     else
1014       _libData->ReadTetraNodes( node1, node2, node3, node4, domain );
1015     return;
1016 #endif
1017   }
1018   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, domain );
1019 }
1020
1021 //================================================================================
1022 /*!
1023  * \brief Return node indices of a next hexahedron
1024  */
1025 //================================================================================
1026
1027 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
1028                              int* node1, int* node2, int* node3, int* node4,
1029                              int* node5, int* node6, int* node7, int* node8,
1030                              int* domain )
1031 {
1032   if ( _useLib ) {
1033 #ifdef USE_MG_LIBS
1034     _libData->ReadHexaNodes( node1, node2, node3, node4,
1035                              node5, node6, node7, node8, domain );
1036     return;
1037 #endif
1038   }
1039   ::GmfGetLin(iMesh, what, node1, node2, node3, node4, node5, node6, node7, node8, domain );
1040 }
1041
1042 //================================================================================
1043 /*!
1044  * \brief Prepare for passing data to MeshGems
1045  */
1046 //================================================================================
1047
1048 int  MG_Hexotic_API::GmfOpenMesh(const char* theFile, int rdOrWr, int ver, int dim)
1049 {
1050   if ( _useLib ) {
1051 #ifdef USE_MG_LIBS
1052     return 1;
1053 #endif
1054   }
1055   int id = ::GmfOpenMesh(theFile, rdOrWr, ver, dim);
1056   _openFiles.insert( id );
1057   return id;
1058 }
1059
1060 //================================================================================
1061 /*!
1062  * \brief Set number of entities
1063  */
1064 //================================================================================
1065
1066 void MG_Hexotic_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
1067 {
1068   if ( _useLib ) {
1069 #ifdef USE_MG_LIBS
1070     switch ( what ) {
1071     case GmfVertices:          _libData->SetNbVertices( nb ); break;
1072     case GmfEdges:             _libData->SetNbEdges   ( nb ); break;
1073     case GmfRequiredEdges:     _libData->SetNbReqEdges( nb ); break;
1074     case GmfTriangles:         _libData->SetNbTria    ( nb ); break;
1075     case GmfRequiredTriangles: _libData->SetNbReqTria ( nb ); break;
1076     default:;
1077     }
1078     return;
1079 #endif
1080   }
1081   ::GmfSetKwd(iMesh, what, nb );
1082 }
1083
1084 //================================================================================
1085 /*!
1086  * \brief Add coordinates of a node
1087  */
1088 //================================================================================
1089
1090 void MG_Hexotic_API::GmfSetLin(int iMesh, GmfKwdCod what, double x, double y, double z, int domain)
1091 {
1092   if ( _useLib ) {
1093 #ifdef USE_MG_LIBS
1094     _libData->AddNode( x, y, z, domain );
1095     return;
1096 #endif
1097   }
1098   ::GmfSetLin(iMesh, what, x, y, z, domain);
1099 }
1100
1101 //================================================================================
1102 /*!
1103  * \brief Set number of field entities
1104  *  \param [in] iMesh - solution file index
1105  *  \param [in] what - solution type
1106  *  \param [in] nbNodes - nb of entities
1107  *  \param [in] nbTypes - nb of data entries in each entity
1108  *  \param [in] type - types of the data entries
1109  *
1110  * Used to prepare to storing size at nodes
1111  */
1112 //================================================================================
1113
1114 void MG_Hexotic_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nbNodes, int dummy, int type[] )
1115 {
1116   if ( _useLib ) {
1117 #ifdef USE_MG_LIBS
1118     if ( what == GmfSolAtVertices ) _libData->SetNbReqVertices( nbNodes );
1119     return;
1120 #endif
1121   }
1122   ::GmfSetKwd(iMesh, what, nbNodes, dummy, type );
1123 }
1124
1125 //================================================================================
1126 /*!
1127  * \brief Add solution data
1128  */
1129 //================================================================================
1130
1131 void MG_Hexotic_API::GmfSetLin(int iMesh, GmfKwdCod what, double vals[])
1132 {
1133   if ( _useLib ) {
1134 #ifdef USE_MG_LIBS
1135     _libData->AddSizeAtNode( vals[0] );
1136     return;
1137 #endif
1138   }
1139   ::GmfSetLin(iMesh, what, vals);
1140 }
1141
1142 //================================================================================
1143 /*!
1144  * \brief Add edge nodes
1145  */
1146 //================================================================================
1147
1148 void MG_Hexotic_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int domain )
1149 {
1150   if ( _useLib ) {
1151 #ifdef USE_MG_LIBS
1152     _libData->AddEdgeNodes( node1, node2, domain );
1153     return;
1154 #endif
1155   }
1156   ::GmfSetLin(iMesh, what, node1, node2, domain );
1157 }
1158
1159 //================================================================================
1160 /*!
1161  * \brief Add a 'required' flag
1162  */
1163 //================================================================================
1164
1165 void MG_Hexotic_API::GmfSetLin(int iMesh, GmfKwdCod what, int id )
1166 {
1167   if ( _useLib ) {
1168 #ifdef USE_MG_LIBS
1169     return;
1170 #endif
1171   }
1172   ::GmfSetLin(iMesh, what, id );
1173 }
1174
1175 //================================================================================
1176 /*!
1177  * \brief Add triangle nodes
1178  */
1179 //================================================================================
1180
1181 void MG_Hexotic_API::GmfSetLin(int iMesh, GmfKwdCod what, int node1, int node2, int node3, int domain )
1182 {
1183   if ( _useLib ) {
1184 #ifdef USE_MG_LIBS
1185     _libData->AddTriaNodes( node1, node2, node3, domain );
1186     return;
1187 #endif
1188   }
1189   ::GmfSetLin(iMesh, what, node1, node2, node3, domain );
1190 }
1191
1192 //================================================================================
1193 /*!
1194  * \brief Close a file
1195  */
1196 //================================================================================
1197
1198 void MG_Hexotic_API::GmfCloseMesh( int iMesh )
1199 {
1200   if ( _useLib ) {
1201 #ifdef USE_MG_LIBS
1202     return;
1203 #endif
1204   }
1205   ::GmfCloseMesh( iMesh );
1206   _openFiles.erase( iMesh );
1207 }
1208
1209 //================================================================================
1210 /*!
1211  * \brief Return true if the log is not empty
1212  */
1213 //================================================================================
1214
1215 bool MG_Hexotic_API::HasLog()
1216 {
1217   if ( _useLib ) {
1218 #ifdef USE_MG_LIBS
1219     return !_libData->GetErrors().empty();
1220 #endif
1221   }
1222   SMESH_File file( _logFile );
1223   return file.size() > 0;
1224 }
1225
1226 //================================================================================
1227 /*!
1228  * \brief Return log contents
1229  */
1230 //================================================================================
1231
1232 std::string MG_Hexotic_API::GetLog()
1233 {
1234   if ( _useLib ) {
1235 #ifdef USE_MG_LIBS
1236     const std::string& err = _libData->GetErrors();
1237     if ( !_logFile.empty() && !err.empty() )
1238     {
1239       SMESH_File file( _logFile, /*openForReading=*/false );
1240       file.openForWriting();
1241       file.write( err.c_str(), err.size() );
1242     }
1243     return err;
1244 #endif
1245   }
1246   SMESH_File file( _logFile );
1247   return file.getPos();
1248 }