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