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