Salome HOME
Merge multi-study removal branch.
[plugins/hexablockplugin.git] / src / HEXABLOCKPlugin / HEXABLOCKPlugin_HEXABLOCK.cxx
1 // Copyright (C) 2009-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 //=============================================================================
21 // File      : HEXABLOCKPlugin_HEXABLOCK.cxx
22 // Created   : 
23 // Author    : Lioka RAZAFINDRAZAKA (CEA)
24 // Project   : SALOME
25 // $Header$
26 //=============================================================================
27 //
28 #include "HEXABLOCKPlugin_HEXABLOCK.hxx"
29 #include "HEXABLOCKPlugin_Hypothesis.hxx"
30
31 #include "TopExp_Explorer.hxx"
32
33 #include <Basics_Utils.hxx>
34
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMesh.hxx"
40
41 #include "HEXABLOCKPlugin_mesh.hxx"
42  
43 #include "HexQuad.hxx"
44 #include "HexEdge.hxx"
45 #include "HexVertex.hxx"
46 #include "HexPropagation.hxx"
47
48 #include "utilities.h"
49
50 using namespace std;
51
52 #ifdef _DEBUG_
53 static int MYDEBUG = HEXA_NS::on_debug ();
54 #else
55 static int MYDEBUG = 0;
56 #endif
57
58 //=============================================================================
59 /*!
60  *  
61  */
62 //=============================================================================
63
64 HEXABLOCKPlugin_HEXABLOCK::HEXABLOCKPlugin_HEXABLOCK(int hypId, SMESH_Gen* gen)
65   : SMESH_3D_Algo(hypId, gen)
66 {
67   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::HEXABLOCKPlugin_HEXABLOCK");
68   _name = "HEXABLOCK_3D";
69   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
70   _compatibleHypothesis.push_back("HEXABLOCK_Parameters");
71   _requireShape = false; // can work without shape
72   _requireDiscreteBoundary = false;
73   _hyp = NULL;
74   _supportSubmeshes = false;
75   _iShape  = 0;
76   _nbShape = 0;
77 }
78
79 //=============================================================================
80 /*!
81  *  
82  */
83 //=============================================================================
84
85 HEXABLOCKPlugin_HEXABLOCK::~HEXABLOCKPlugin_HEXABLOCK()
86 {
87   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::~HEXABLOCKPlugin_HEXABLOCK");
88 }
89
90 //=============================================================================
91 /*!
92  *  
93  */
94 //=============================================================================
95
96 bool HEXABLOCKPlugin_HEXABLOCK::CheckHypothesis ( SMESH_Mesh& aMesh,
97                                           const TopoDS_Shape& aShape,
98                                           Hypothesis_Status&  aStatus )
99 {
100   aStatus = SMESH_Hypothesis::HYP_OK;
101
102   // there is only one compatible Hypothesis so far
103   _hyp = 0;
104   const list <const SMESHDS_Hypothesis * >& hyps = GetUsedHypothesis(aMesh, aShape);
105   if ( !hyps.empty() )
106     _hyp = static_cast<const HEXABLOCKPlugin_Hypothesis*> ( hyps.front() );
107
108   return true;
109 }
110
111 //=============================================================================
112 /*!
113  *Here we are going to use the HEXABLOCK mesher
114  */
115 //=============================================================================
116
117 bool HEXABLOCKPlugin_HEXABLOCK::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape) {
118   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute with a shape");
119
120   SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
121   if ( (_iShape == 0) && (_nbShape == 0) ) {
122     TopExp_Explorer expShape ( meshDS->ShapeToMesh(), TopAbs_SOLID );
123     for ( ; expShape.More(); expShape.Next() ) {
124       _nbShape++;
125     }
126   }
127
128   // to prevent from displaying error message after computing,
129   for ( int i = 0; i < _nbShape; ++i )
130     if ( SMESH_subMesh* sm = theMesh.GetSubMeshContaining( theShape ))
131     {
132       SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
133                                                                /*complexShapeFirst=*/false);
134       while ( smIt->more() )
135       {
136         sm = smIt->next();
137         if ( !sm->IsMeshComputed() )
138           sm->SetIsAlwaysComputed( true );
139       }
140     }
141
142
143   _iShape++;
144
145   if ( _iShape == _nbShape ) {
146     _nbShape = 0;
147     _iShape  = 0;
148
149     switch (_hyp->GetDimension()) {
150       case 0 : return( Compute0D(theMesh) );
151       case 1 : return( Compute1D(theMesh) );
152       case 2 : return( Compute2D(theMesh) );
153       default: return( Compute3D(theMesh) );
154     };
155   }
156   return false;
157 }
158
159 //=============================================================================
160 /*!
161  *Here we are going to use the HEXABLOCK mesher w/o geometry
162  */
163 //=============================================================================
164
165 bool HEXABLOCKPlugin_HEXABLOCK::Compute(SMESH_Mesh& theMesh,
166                                 SMESH_MesherHelper* aHelper)
167 {
168   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute without a shape");
169
170   switch (_hyp->GetDimension()) {
171     case 0 : return( Compute0D(theMesh) );
172     case 1 : return( Compute1D(theMesh) );
173     case 2 : return( Compute2D(theMesh) );
174     default: return( Compute3D(theMesh) );
175   }
176 }
177
178 //=============================================================================
179 /*!
180  *  
181  */
182 //=============================================================================
183 bool HEXABLOCKPlugin_HEXABLOCK::Evaluate(SMESH_Mesh& aMesh,
184                                  const TopoDS_Shape& aShape,
185                                  MapShapeNbElems& aResMap)
186 {
187   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Evaluate: do nothing");
188
189   return true;
190 }
191
192 //=============================================================================
193 /*!
194  *  Generate hexehedral
195  */
196 //=============================================================================
197
198 bool HEXABLOCKPlugin_HEXABLOCK::Compute3D(SMESH_Mesh& theMesh) {
199   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute 3D Begin");
200
201   SMESH_HexaBlocks hexaBuilder(theMesh);
202
203   HEXA_NS::Document* doc = _hyp->GetDocument();
204   // doc->reorderFaces ();                 // 0) Abu 06/03/2012
205
206   hexaBuilder.computeDoc(doc);
207   hexaBuilder.buildGroups(doc); 
208
209   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute 3D End");
210   return true;
211 }
212
213 //=============================================================================
214 /*!
215  *  Generate quadrangles
216  */
217 //=============================================================================
218
219 bool HEXABLOCKPlugin_HEXABLOCK::Compute2D(SMESH_Mesh& theMesh)
220 {
221   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute 2D");
222
223   HEXA_NS::Document* doc = _hyp->GetDocument();
224   // doc->reorderFaces ();                 // 0) Abu 06/03/2012
225
226   SMESH_HexaBlocks hexaBuilder(theMesh);
227
228   // A) Vertex computation
229   int nVertex = doc->countUsedVertex();
230   HEXA_NS::Vertex* vertex = NULL;
231   for ( int j=0; j <nVertex; ++j ){ //Computing each vertex of the document
232     vertex = doc->getUsedVertex(j);
233     hexaBuilder.computeVertex(*vertex);
234   };
235
236   // B) Edges computation
237   int nbPropa = 0;
238   HEXA_NS::Propagation* propa = NULL;
239   HEXA_NS::Law*         law   = NULL;
240   HEXA_NS::Edges        edges;
241
242   nbPropa = doc->countPropagation();
243   for (int j=0; j < nbPropa; ++j ){ //Computing each edge's propagations of the document
244     propa = doc->getPropagation(j);
245     edges = propa->getEdges();
246     law   = propa->getLaw();
247     if (law == NULL){
248       law = doc->getLaw(0); // default law
249     };
250     for( HEXA_NS::Edges::const_iterator iter = edges.begin(); iter != edges.end(); ++iter ) {
251       hexaBuilder.computeEdge(**iter, *law);
252     };
253   };
254
255   // C) Quad computation
256   std::map<HEXA_NS::Quad*, bool>  quadWays = hexaBuilder.computeQuadWays(doc);
257   int nQuad = doc->countUsedQuad();
258   HEXA_NS::Quad* quad = NULL;
259   for (int j=0; j <nQuad; ++j ){ //Computing each quad of the document
260     quad = doc->getUsedQuad(j);
261     int id = quad->getId();
262     if ( quadWays.count(quad) > 0 )
263       hexaBuilder.computeQuad(*quad, quadWays[quad]);
264     else
265       if(MYDEBUG) MESSAGE("NO QUAD WAY ID = "<<id);
266   };
267
268   // D) build Groups
269   hexaBuilder.buildGroups(doc);
270
271     return true;
272 }
273
274 // SMESH::SMESH_Mesh_ptr SMESH_Gen_i::HexaBlocksQuad( HEXABLOCK_ORB::Document_ptr docIn, ::CORBA::Long quadID )
275 // {
276 //   try {
277 //     SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
278 //     if ( !aNewMesh->_is_nil() ) {
279 //       SMESH_Mesh_i* aNewImpl    = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
280 //       Document_impl* docServant = dynamic_cast<Document_impl*>( GetServant( docIn ).in() );
281 //       ASSERT( aNewImpl );
282 //       ASSERT( docServant );
283
284 //       HEXA_NS::Document* doc = docServant->GetImpl();
285 //       SMESH_HexaBlocks hexaBuilder(aNewImpl);
286
287 //       // A) Vertex computation
288 //       int nVertex = doc->countVertex();
289 //       HEXA_NS::Vertex* vertex = NULL;
290 //       for ( int j=0; j <nVertex; ++j ){ //Computing each vertex of the document
291 //         vertex = doc->getUsedVertex(j);
292 //         hexaBuilder.computeVertex(*vertex);
293 //       }
294
295 //       // B) Edges computation
296 //       int nbPropa = 0;
297 //       HEXA_NS::Propagation* propa = NULL;
298 //       HEXA_NS::Law*         law   = NULL;
299 //       HEXA_NS::Edges        edges;
300
301 //       nbPropa = doc->countPropagation();
302 //       for (int j=0; j < nbPropa; ++j ){//Computing each edge's propagations of the document
303 //         propa = doc->getPropagation(j);
304 //         edges = propa->getEdges();
305 //         law   = propa->getLaw();
306 // //         ASSERT( law );
307 //         if (law == NULL){
308 //           law = doc->getLaw(0); // default law
309 //         }
310 //         for( HEXA_NS::Edges::const_iterator iter = edges.begin();
311 //             iter != edges.end();
312 //             ++iter ){
313 //             hexaBuilder.computeEdge(**iter, *law);
314 //         }
315 //       }
316
317 //       // C) Quad computation
318 //       std::map<HEXA_NS::Quad*, bool>  quadWays = hexaBuilder.computeQuadWays(*doc);
319 //       int nQuad = doc->countQuad();
320 //       HEXA_NS::Quad* quad = NULL;
321 //       for (int j=0; j <nQuad; ++j ){ //Computing each quad of the document
322 //         quad = doc->getQuad(j);
323 //         int id = quad->getId();
324 //         if ( quadID == id and (quadWays.count(quad) > 0) )
325 //           hexaBuilder.computeQuad( *quad, quadWays[quad] );
326
327 //         if ( quadWays.count(quad) ==  0 )
328 //             if(MYDEBUG) MESSAGE("NO QUAD WAY ID = "<<id);
329
330 //       }
331
332 //       // D) build Groups
333 //       hexaBuilder.buildGroups(doc);
334
335 //     }
336 //     return aNewMesh._retn();
337 //   } catch (SALOME_Exception& S_ex) {
338 //     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
339 //   }
340 // }
341
342 bool HEXABLOCKPlugin_HEXABLOCK::Compute1D(SMESH_Mesh& theMesh)
343 {
344   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute 1D");
345
346   HEXA_NS::Document* doc = _hyp->GetDocument();
347   // doc->reorderFaces ();                 // 0) Abu 06/03/2012
348
349   SMESH_HexaBlocks hexaBuilder(theMesh);
350
351   // A) Vertex computation
352   int nVertex = doc->countUsedVertex();
353   HEXA_NS::Vertex* vertex = NULL;
354   for ( int j=0; j <nVertex; ++j ){ //Computing each vertex of the document
355     vertex = doc->getUsedVertex(j);
356     hexaBuilder.computeVertex(*vertex);
357   };
358
359   // B) Edges computation
360   int nbPropa = 0;
361   HEXA_NS::Propagation* propa = NULL;
362   HEXA_NS::Law*         law   = NULL;
363   HEXA_NS::Edges        edges;
364
365   nbPropa = doc->countPropagation();
366   for (int j=0; j < nbPropa; ++j ){//Computing each edge's propagations of the document
367     propa = doc->getPropagation(j);
368     edges = propa->getEdges();
369     law   = propa->getLaw();
370     //         ASSERT( law );
371     if (law == NULL){
372       law = doc->getLaw(0); // default law
373     };
374     for( HEXA_NS::Edges::const_iterator iter = edges.begin(); iter != edges.end(); ++iter ) {
375       hexaBuilder.computeEdge(**iter, *law);
376     };
377   };
378
379   // C) build Groups
380   hexaBuilder.buildGroups(doc);
381
382     return true;
383 }
384
385 bool HEXABLOCKPlugin_HEXABLOCK::Compute0D(SMESH_Mesh& theMesh)
386 {
387   if(MYDEBUG) MESSAGE("HEXABLOCKPlugin_HEXABLOCK::Compute 0D");
388
389   HEXA_NS::Document* doc = _hyp->GetDocument();
390   // doc->reorderFaces ();                 // 0) Abu 06/03/2012
391
392   SMESH_HexaBlocks hexaBuilder(theMesh);
393
394   // A) Vertex computation
395   int nVertex = doc->countUsedVertex();
396   HEXA_NS::Vertex* vertex = NULL;
397   for ( int j=0; j <nVertex; ++j ){ //Computing each vertex of the document
398     vertex = doc->getUsedVertex(j);
399     hexaBuilder.computeVertex(*vertex);
400   };
401
402   // B) build Groups
403   hexaBuilder.buildGroups(doc);
404
405   return true;
406 }