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