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