Salome HOME
Corrected for bug IPAL12571.
[modules/smesh.git] / src / StdMeshers / StdMeshers_Hexa_3D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : StdMeshers_Hexa_3D.cxx
25 //           Moved here from SMESH_Hexa_3D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //  $Header$
29
30 #include "StdMeshers_Hexa_3D.hxx"
31 #include "StdMeshers_Quadrangle_2D.hxx"
32 #include "StdMeshers_FaceSide.hxx"
33 #include "StdMeshers_Penta_3D.hxx"
34 #include "StdMeshers_Prism_3D.hxx"
35
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_subMesh.hxx"
39 #include "SMESH_Comment.hxx"
40
41 #include "SMDS_MeshElement.hxx"
42 #include "SMDS_MeshNode.hxx"
43 #include "SMDS_FacePosition.hxx"
44 #include "SMDS_VolumeTool.hxx"
45 #include "SMDS_VolumeOfNodes.hxx"
46
47 #include <TopExp.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TColStd_MapOfInteger.hxx>
52
53 #include <BRep_Tool.hxx>
54 #include <Geom_Surface.hxx>
55 #include <gp_Pnt2d.hxx>
56
57 #include "utilities.h"
58 #include "Utils_ExceptHandlers.hxx"
59
60 typedef SMESH_Comment TComm;
61
62 using namespace std;
63
64 static SMESH_ComputeErrorPtr ComputePentahedralMesh(SMESH_Mesh &, const TopoDS_Shape &);
65
66 //=============================================================================
67 /*!
68  *  
69  */
70 //=============================================================================
71
72 StdMeshers_Hexa_3D::StdMeshers_Hexa_3D(int hypId, int studyId, SMESH_Gen * gen)
73   :SMESH_3D_Algo(hypId, studyId, gen)
74 {
75   MESSAGE("StdMeshers_Hexa_3D::StdMeshers_Hexa_3D");
76   _name = "Hexa_3D";
77   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit /shape type
78 }
79
80 //=============================================================================
81 /*!
82  *  
83  */
84 //=============================================================================
85
86 StdMeshers_Hexa_3D::~StdMeshers_Hexa_3D()
87 {
88   MESSAGE("StdMeshers_Hexa_3D::~StdMeshers_Hexa_3D");
89 }
90
91 //================================================================================
92 /*!
93  * \brief Clear fields and return the argument
94   * \param res - the value to return
95   * \retval bool - the argument value
96  */
97 //================================================================================
98
99 bool StdMeshers_Hexa_3D::ClearAndReturn(FaceQuadStruct* theQuads[6], const bool res)
100 {
101   for (int i = 0; i < 6; i++) {
102     delete theQuads[i];
103     theQuads[i] = NULL;
104   }
105   return res;
106 }
107
108
109 //=============================================================================
110 /*!
111  *  
112  */
113 //=============================================================================
114
115 bool StdMeshers_Hexa_3D::CheckHypothesis
116                          (SMESH_Mesh&                          aMesh,
117                           const TopoDS_Shape&                  aShape,
118                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
119 {
120   // check nb of faces in the shape
121   aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
122   int nbFaces = 0;
123   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next())
124     if ( ++nbFaces > 6 )
125       break;
126   if ( nbFaces != 6 )
127     return false;
128
129   aStatus = SMESH_Hypothesis::HYP_OK;
130   return true;
131 }
132
133 //=======================================================================
134 //function : findIJ
135 //purpose  : return i,j of the node
136 //=======================================================================
137
138 static bool findIJ (const SMDS_MeshNode* node, const FaceQuadStruct * quad, int& I, int& J)
139 {
140   I = J = 0;
141   const SMDS_FacePosition* fpos =
142     static_cast<const SMDS_FacePosition*>(node->GetPosition().get());
143   if ( ! fpos ) return false;
144   gp_Pnt2d uv( fpos->GetUParameter(), fpos->GetVParameter() );
145
146   double minDist = DBL_MAX;
147   int nbhoriz  = Min(quad->side[0]->NbPoints(), quad->side[2]->NbPoints());
148   int nbvertic = Min(quad->side[1]->NbPoints(), quad->side[3]->NbPoints());
149   for (int i = 1; i < nbhoriz - 1; i++) {
150     for (int j = 1; j < nbvertic - 1; j++) {
151       int ij = j * nbhoriz + i;
152       gp_Pnt2d uv2( quad->uv_grid[ij].u, quad->uv_grid[ij].v );
153       double dist = uv.SquareDistance( uv2 );
154       if ( dist < minDist ) {
155         minDist = dist;
156         I = i;
157         J = j;
158       }
159     }
160   }
161   return true;
162 }
163
164
165 //=============================================================================
166 /*!
167  * Hexahedron mesh on hexaedron like form
168  * -0.  - shape and face mesh verification
169  * -1.  - identify faces and vertices of the "cube"
170  * -2.  - Algorithm from:
171  * "Application de l'interpolation transfinie à la création de maillages
172  *  C0 ou G1 continus sur des triangles, quadrangles, tetraedres, pentaedres
173  *  et hexaedres déformés."
174  * Alain PERONNET - 8 janvier 1999
175  */
176 //=============================================================================
177
178 bool StdMeshers_Hexa_3D::Compute(SMESH_Mesh &         aMesh,
179                                  const TopoDS_Shape & aShape)// throw(SALOME_Exception)
180 {
181   // PAL14921. Enable catching std::bad_alloc and Standard_OutOfMemory outside
182   //Unexpect aCatch(SalomeException);
183   MESSAGE("StdMeshers_Hexa_3D::Compute");
184   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
185   
186   // 0.  - shape and face mesh verification
187   // 0.1 - shape must be a solid (or a shell) with 6 faces
188
189   vector < SMESH_subMesh * >meshFaces;
190   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
191     SMESH_subMesh *aSubMesh = aMesh.GetSubMeshContaining(exp.Current());
192     ASSERT(aSubMesh);
193     meshFaces.push_back(aSubMesh);
194   }
195   if (meshFaces.size() != 6)
196     return error(COMPERR_BAD_SHAPE, TComm(meshFaces.size())<<" instead of 6 faces in block");
197
198   // 0.2 - is each face meshed with Quadrangle_2D? (so, with a wire of 4 edges)
199
200   // tool for working with quadratic elements
201   SMESH_MesherHelper aTool (aMesh);
202   _quadraticMesh = aTool.IsQuadraticSubMesh(aShape);
203
204   // cube structure
205   typedef struct cubeStruct
206   {
207     TopoDS_Vertex V000;
208     TopoDS_Vertex V001;
209     TopoDS_Vertex V010;
210     TopoDS_Vertex V011;
211     TopoDS_Vertex V100;
212     TopoDS_Vertex V101;
213     TopoDS_Vertex V110;
214     TopoDS_Vertex V111;
215     faceQuadStruct* quad_X0;
216     faceQuadStruct* quad_X1;
217     faceQuadStruct* quad_Y0;
218     faceQuadStruct* quad_Y1;
219     faceQuadStruct* quad_Z0;
220     faceQuadStruct* quad_Z1;
221     Point3DStruct* np; // normalised 3D coordinates
222   } CubeStruct;
223
224   CubeStruct aCube;
225
226   // bounding faces
227   FaceQuadStruct* aQuads[6];
228   for (int i = 0; i < 6; i++)
229     aQuads[i] = 0;
230
231   for (int i = 0; i < 6; i++)
232   {
233     TopoDS_Shape aFace = meshFaces[i]->GetSubShape();
234     SMESH_Algo *algo = _gen->GetAlgo(aMesh, aFace);
235     string algoName = algo->GetName();
236     bool isAllQuad = false;
237     if (algoName == "Quadrangle_2D") {
238       SMESHDS_SubMesh * sm = meshDS->MeshElements( aFace );
239       if ( sm ) {
240         isAllQuad = true;
241         SMDS_ElemIteratorPtr eIt = sm->GetElements();
242         while ( isAllQuad && eIt->more() ) {
243           const SMDS_MeshElement* elem =  eIt->next();
244           isAllQuad = ( elem->NbNodes()==4 ||(_quadraticMesh && elem->NbNodes()==8) );
245         }
246       }
247     }
248     if ( ! isAllQuad ) {
249       SMESH_ComputeErrorPtr err = ComputePentahedralMesh(aMesh, aShape);
250       return ClearAndReturn( aQuads, error(err));
251     }
252     StdMeshers_Quadrangle_2D *quadAlgo =
253       dynamic_cast < StdMeshers_Quadrangle_2D * >(algo);
254     ASSERT(quadAlgo);
255     try {
256       aQuads[i] = quadAlgo->CheckAnd2Dcompute(aMesh, aFace, _quadraticMesh);
257       if(!aQuads[i]) {
258         return error( quadAlgo->GetComputeError());
259       }
260     }
261     catch(SALOME_Exception & S_ex) {
262       return ClearAndReturn( aQuads, error(COMPERR_SLM_EXCEPTION,TComm(S_ex.what()) <<
263                                            " Raised by StdMeshers_Quadrangle_2D "
264                                            " on face #" << meshDS->ShapeToIndex( aFace )));
265     }
266
267     // 0.2.1 - number of points on the opposite edges must be the same
268     if (aQuads[i]->side[0]->NbPoints() != aQuads[i]->side[2]->NbPoints() ||
269         aQuads[i]->side[1]->NbPoints() != aQuads[i]->side[3]->NbPoints()
270         /*aQuads[i]->side[0]->NbEdges() != 1 ||
271         aQuads[i]->side[1]->NbEdges() != 1 ||
272         aQuads[i]->side[2]->NbEdges() != 1 ||
273         aQuads[i]->side[3]->NbEdges() != 1*/) {
274       MESSAGE("different number of points on the opposite edges of face " << i);
275       // Try to go into penta algorithm 'cause it has been improved.
276       SMESH_ComputeErrorPtr err = ComputePentahedralMesh(aMesh, aShape);
277       return ClearAndReturn( aQuads, error(err));
278     }
279   }
280
281   // 1.  - identify faces and vertices of the "cube"
282   // 1.1 - ancestor maps vertex->edges in the cube
283
284   TopTools_IndexedDataMapOfShapeListOfShape MS;
285   TopExp::MapShapesAndAncestors(aShape, TopAbs_VERTEX, TopAbs_EDGE, MS);
286
287   // 1.2 - first face is choosen as face Y=0 of the unit cube
288
289   const TopoDS_Shape & aFace = meshFaces[0]->GetSubShape();
290   const TopoDS_Face & F = TopoDS::Face(aFace);
291
292   // 1.3 - identify the 4 vertices of the face Y=0: V000, V100, V101, V001
293
294   aCube.V000 = aQuads[0]->side[0]->FirstVertex(); // will be (0,0,0) on the unit cube
295   aCube.V100 = aQuads[0]->side[0]->LastVertex();  // will be (1,0,0) on the unit cube
296   aCube.V001 = aQuads[0]->side[2]->FirstVertex(); // will be (0,0,1) on the unit cube
297   aCube.V101 = aQuads[0]->side[2]->LastVertex();  // will be (1,0,1) on the unit cube
298
299   TopTools_IndexedMapOfShape MV0;
300   TopExp::MapShapes(F, TopAbs_VERTEX, MV0);
301
302   aCube.V010 = OppositeVertex( aCube.V000, MV0, aQuads);
303   aCube.V110 = OppositeVertex( aCube.V100, MV0, aQuads);
304   aCube.V011 = OppositeVertex( aCube.V001, MV0, aQuads);
305   aCube.V111 = OppositeVertex( aCube.V101, MV0, aQuads);
306
307   // 1.6 - find remaining faces given 4 vertices
308
309   int _indY0 = 0;
310   aCube.quad_Y0 = aQuads[_indY0];
311
312   int _indY1 = GetFaceIndex(aMesh, aShape, meshFaces,
313                             aCube.V010, aCube.V011, aCube.V110, aCube.V111);
314   aCube.quad_Y1 = aQuads[_indY1];
315
316   int _indZ0 = GetFaceIndex(aMesh, aShape, meshFaces,
317                             aCube.V000, aCube.V010, aCube.V100, aCube.V110);
318   aCube.quad_Z0 = aQuads[_indZ0];
319
320   int _indZ1 = GetFaceIndex(aMesh, aShape, meshFaces,
321                             aCube.V001, aCube.V011, aCube.V101, aCube.V111);
322   aCube.quad_Z1 = aQuads[_indZ1];
323
324   int _indX0 = GetFaceIndex(aMesh, aShape, meshFaces,
325                             aCube.V000, aCube.V001, aCube.V010, aCube.V011);
326   aCube.quad_X0 = aQuads[_indX0];
327
328   int _indX1 = GetFaceIndex(aMesh, aShape, meshFaces,
329                             aCube.V100, aCube.V101, aCube.V110, aCube.V111);
330   aCube.quad_X1 = aQuads[_indX1];
331
332   // 1.7 - get convertion coefs from face 2D normalized to 3D normalized
333
334   Conv2DStruct cx0;                     // for face X=0
335   Conv2DStruct cx1;                     // for face X=1
336   Conv2DStruct cy0;
337   Conv2DStruct cy1;
338   Conv2DStruct cz0;
339   Conv2DStruct cz1;
340
341   GetConv2DCoefs(*aCube.quad_X0, meshFaces[_indX0]->GetSubShape(),
342                  aCube.V000, aCube.V010, aCube.V011, aCube.V001, cx0);
343   GetConv2DCoefs(*aCube.quad_X1, meshFaces[_indX1]->GetSubShape(),
344                  aCube.V100, aCube.V110, aCube.V111, aCube.V101, cx1);
345   GetConv2DCoefs(*aCube.quad_Y0, meshFaces[_indY0]->GetSubShape(),
346                  aCube.V000, aCube.V100, aCube.V101, aCube.V001, cy0);
347   GetConv2DCoefs(*aCube.quad_Y1, meshFaces[_indY1]->GetSubShape(),
348                  aCube.V010, aCube.V110, aCube.V111, aCube.V011, cy1);
349   GetConv2DCoefs(*aCube.quad_Z0, meshFaces[_indZ0]->GetSubShape(),
350                  aCube.V000, aCube.V100, aCube.V110, aCube.V010, cz0);
351   GetConv2DCoefs(*aCube.quad_Z1, meshFaces[_indZ1]->GetSubShape(),
352                  aCube.V001, aCube.V101, aCube.V111, aCube.V011, cz1);
353
354   // 1.8 - create a 3D structure for normalized values
355   
356   int nbx = aCube.quad_Z0->side[0]->NbPoints();
357   if (cz0.a1 == 0.) nbx = aCube.quad_Z0->side[1]->NbPoints();
358  
359   int nby = aCube.quad_X0->side[0]->NbPoints();
360   if (cx0.a1 == 0.) nby = aCube.quad_X0->side[1]->NbPoints();
361  
362   int nbz = aCube.quad_Y0->side[0]->NbPoints();
363   if (cy0.a1 != 0.) nbz = aCube.quad_Y0->side[1]->NbPoints();
364
365   int i1, j1, nbxyz = nbx * nby * nbz;
366   Point3DStruct *np = new Point3DStruct[nbxyz];
367
368   // 1.9 - store node indexes of faces
369
370   {
371     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indX0]->GetSubShape());
372
373     faceQuadStruct *quad = aCube.quad_X0;
374     int i = 0;                          // j = x/face , k = y/face
375     int nbdown = quad->side[0]->NbPoints();
376     int nbright = quad->side[1]->NbPoints();
377
378     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
379                         
380     while(itf->more()) {
381       const SMDS_MeshNode * node = itf->next();
382       if(aTool.IsMedium(node))
383         continue;
384       if ( !findIJ( node, quad, i1, j1 ))
385         return ClearAndReturn( aQuads, false );
386       int ij1 = j1 * nbdown + i1;
387       quad->uv_grid[ij1].node = node;
388     }
389
390     for (int i1 = 0; i1 < nbdown; i1++)
391       for (int j1 = 0; j1 < nbright; j1++) {
392         int ij1 = j1 * nbdown + i1;
393         int j = cx0.ia * i1 + cx0.ib * j1 + cx0.ic;     // j = x/face
394         int k = cx0.ja * i1 + cx0.jb * j1 + cx0.jc;     // k = y/face
395         int ijk = k * nbx * nby + j * nbx + i;
396         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
397         np[ijk].node = quad->uv_grid[ij1].node;
398         //SCRUTE(np[ijk].nodeId);
399       }
400   }
401
402   {
403     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indX1]->GetSubShape());
404
405     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
406
407     faceQuadStruct *quad = aCube.quad_X1;
408     int i = nbx - 1;            // j = x/face , k = y/face
409     int nbdown = quad->side[0]->NbPoints();
410     int nbright = quad->side[1]->NbPoints();
411
412     while(itf->more()) {
413       const SMDS_MeshNode * node = itf->next();
414       if(aTool.IsMedium(node))
415         continue;
416       if ( !findIJ( node, quad, i1, j1 ))
417         return ClearAndReturn( aQuads, false );
418       int ij1 = j1 * nbdown + i1;
419       quad->uv_grid[ij1].node = node;
420     }
421
422     for (int i1 = 0; i1 < nbdown; i1++)
423       for (int j1 = 0; j1 < nbright; j1++) {
424         int ij1 = j1 * nbdown + i1;
425         int j = cx1.ia * i1 + cx1.ib * j1 + cx1.ic;     // j = x/face
426         int k = cx1.ja * i1 + cx1.jb * j1 + cx1.jc;     // k = y/face
427         int ijk = k * nbx * nby + j * nbx + i;
428         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
429         np[ijk].node = quad->uv_grid[ij1].node;
430         //SCRUTE(np[ijk].nodeId);
431       }
432   }
433
434   {
435     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indY0]->GetSubShape());
436
437     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
438
439     faceQuadStruct *quad = aCube.quad_Y0;
440     int j = 0;                          // i = x/face , k = y/face
441     int nbdown = quad->side[0]->NbPoints();
442     int nbright = quad->side[1]->NbPoints();
443
444     while(itf->more()) {
445       const SMDS_MeshNode * node = itf->next();
446       if(aTool.IsMedium(node))
447         continue;
448       if ( !findIJ( node, quad, i1, j1 ))
449         return ClearAndReturn( aQuads, false );
450       int ij1 = j1 * nbdown + i1;
451       quad->uv_grid[ij1].node = node;
452     }
453
454     for (int i1 = 0; i1 < nbdown; i1++)
455       for (int j1 = 0; j1 < nbright; j1++) {
456         int ij1 = j1 * nbdown + i1;
457         int i = cy0.ia * i1 + cy0.ib * j1 + cy0.ic;     // i = x/face
458         int k = cy0.ja * i1 + cy0.jb * j1 + cy0.jc;     // k = y/face
459         int ijk = k * nbx * nby + j * nbx + i;
460         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
461         np[ijk].node = quad->uv_grid[ij1].node;
462         //SCRUTE(np[ijk].nodeId);
463       }
464   }
465
466   {
467     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indY1]->GetSubShape());
468
469     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
470
471     faceQuadStruct *quad = aCube.quad_Y1;
472     int j = nby - 1;            // i = x/face , k = y/face
473     int nbdown = quad->side[0]->NbPoints();
474     int nbright = quad->side[1]->NbPoints();
475
476     while(itf->more()) {
477       const SMDS_MeshNode * node = itf->next();
478       if(aTool.IsMedium(node))
479         continue;
480       if ( !findIJ( node, quad, i1, j1 ))
481         return ClearAndReturn( aQuads, false );
482       int ij1 = j1 * nbdown + i1;
483       quad->uv_grid[ij1].node = node;
484     }
485
486     for (int i1 = 0; i1 < nbdown; i1++)
487       for (int j1 = 0; j1 < nbright; j1++) {
488         int ij1 = j1 * nbdown + i1;
489         int i = cy1.ia * i1 + cy1.ib * j1 + cy1.ic;     // i = x/face
490         int k = cy1.ja * i1 + cy1.jb * j1 + cy1.jc;     // k = y/face
491         int ijk = k * nbx * nby + j * nbx + i;
492         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
493         np[ijk].node = quad->uv_grid[ij1].node;
494         //SCRUTE(np[ijk].nodeId);
495       }
496   }
497
498   {
499     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indZ0]->GetSubShape());
500
501     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
502
503     faceQuadStruct *quad = aCube.quad_Z0;
504     int k = 0;                          // i = x/face , j = y/face
505     int nbdown = quad->side[0]->NbPoints();
506     int nbright = quad->side[1]->NbPoints();
507
508     while(itf->more()) {
509       const SMDS_MeshNode * node = itf->next();
510       if(aTool.IsMedium(node))
511         continue;
512       if ( !findIJ( node, quad, i1, j1 ))
513         return ClearAndReturn( aQuads, false );
514       int ij1 = j1 * nbdown + i1;
515       quad->uv_grid[ij1].node = node;
516     }
517
518     for (int i1 = 0; i1 < nbdown; i1++)
519       for (int j1 = 0; j1 < nbright; j1++) {
520         int ij1 = j1 * nbdown + i1;
521         int i = cz0.ia * i1 + cz0.ib * j1 + cz0.ic;     // i = x/face
522         int j = cz0.ja * i1 + cz0.jb * j1 + cz0.jc;     // j = y/face
523         int ijk = k * nbx * nby + j * nbx + i;
524         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
525         np[ijk].node = quad->uv_grid[ij1].node;
526         //SCRUTE(np[ijk].nodeId);
527       }
528   }
529
530   {
531     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indZ1]->GetSubShape());
532
533     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
534
535     faceQuadStruct *quad = aCube.quad_Z1;
536     int k = nbz - 1;            // i = x/face , j = y/face
537     int nbdown = quad->side[0]->NbPoints();
538     int nbright = quad->side[1]->NbPoints();
539     
540     while(itf->more()) {
541       const SMDS_MeshNode * node = itf->next();
542       if(aTool.IsMedium(node))
543         continue;
544       if ( !findIJ( node, quad, i1, j1 ))
545         return ClearAndReturn( aQuads, false );
546       int ij1 = j1 * nbdown + i1;
547       quad->uv_grid[ij1].node = node;
548     }
549
550     for (int i1 = 0; i1 < nbdown; i1++)
551       for (int j1 = 0; j1 < nbright; j1++) {
552         int ij1 = j1 * nbdown + i1;
553         int i = cz1.ia * i1 + cz1.ib * j1 + cz1.ic;     // i = x/face
554         int j = cz1.ja * i1 + cz1.jb * j1 + cz1.jc;     // j = y/face
555         int ijk = k * nbx * nby + j * nbx + i;
556         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
557         np[ijk].node = quad->uv_grid[ij1].node;
558         //SCRUTE(np[ijk].nodeId);
559       }
560   }
561
562   // 2.0 - for each node of the cube:
563   //       - get the 8 points 3D = 8 vertices of the cube
564   //       - get the 12 points 3D on the 12 edges of the cube
565   //       - get the 6 points 3D on the 6 faces with their ID
566   //       - compute the point 3D
567   //       - store the point 3D in SMESHDS, store its ID in 3D structure
568
569   int shapeID = meshDS->ShapeToIndex( aShape );
570
571   Pt3 p000, p001, p010, p011, p100, p101, p110, p111;
572   Pt3 px00, px01, px10, px11;
573   Pt3 p0y0, p0y1, p1y0, p1y1;
574   Pt3 p00z, p01z, p10z, p11z;
575   Pt3 pxy0, pxy1, px0z, px1z, p0yz, p1yz;
576
577   GetPoint(p000, 0, 0, 0, nbx, nby, nbz, np, meshDS);
578   GetPoint(p001, 0, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
579   GetPoint(p010, 0, nby - 1, 0, nbx, nby, nbz, np, meshDS);
580   GetPoint(p011, 0, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
581   GetPoint(p100, nbx - 1, 0, 0, nbx, nby, nbz, np, meshDS);
582   GetPoint(p101, nbx - 1, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
583   GetPoint(p110, nbx - 1, nby - 1, 0, nbx, nby, nbz, np, meshDS);
584   GetPoint(p111, nbx - 1, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
585
586   for (int i = 1; i < nbx - 1; i++) {
587     for (int j = 1; j < nby - 1; j++) {
588       for (int k = 1; k < nbz - 1; k++) {
589         // *** seulement maillage regulier
590         // 12 points on edges
591         GetPoint(px00, i, 0, 0, nbx, nby, nbz, np, meshDS);
592         GetPoint(px01, i, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
593         GetPoint(px10, i, nby - 1, 0, nbx, nby, nbz, np, meshDS);
594         GetPoint(px11, i, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
595
596         GetPoint(p0y0, 0, j, 0, nbx, nby, nbz, np, meshDS);
597         GetPoint(p0y1, 0, j, nbz - 1, nbx, nby, nbz, np, meshDS);
598         GetPoint(p1y0, nbx - 1, j, 0, nbx, nby, nbz, np, meshDS);
599         GetPoint(p1y1, nbx - 1, j, nbz - 1, nbx, nby, nbz, np, meshDS);
600
601         GetPoint(p00z, 0, 0, k, nbx, nby, nbz, np, meshDS);
602         GetPoint(p01z, 0, nby - 1, k, nbx, nby, nbz, np, meshDS);
603         GetPoint(p10z, nbx - 1, 0, k, nbx, nby, nbz, np, meshDS);
604         GetPoint(p11z, nbx - 1, nby - 1, k, nbx, nby, nbz, np, meshDS);
605
606         // 12 points on faces
607         GetPoint(pxy0, i, j, 0, nbx, nby, nbz, np, meshDS);
608         GetPoint(pxy1, i, j, nbz - 1, nbx, nby, nbz, np, meshDS);
609         GetPoint(px0z, i, 0, k, nbx, nby, nbz, np, meshDS);
610         GetPoint(px1z, i, nby - 1, k, nbx, nby, nbz, np, meshDS);
611         GetPoint(p0yz, 0, j, k, nbx, nby, nbz, np, meshDS);
612         GetPoint(p1yz, nbx - 1, j, k, nbx, nby, nbz, np, meshDS);
613
614         int ijk = k * nbx * nby + j * nbx + i;
615         double x = double (i) / double (nbx - 1);       // *** seulement
616         double y = double (j) / double (nby - 1);       // *** maillage
617         double z = double (k) / double (nbz - 1);       // *** regulier
618
619         Pt3 X;
620         for (int i = 0; i < 3; i++) {
621           X[i] = (1 - x) * p0yz[i] + x * p1yz[i]
622                  + (1 - y) * px0z[i] + y * px1z[i]
623                  + (1 - z) * pxy0[i] + z * pxy1[i]
624                  - (1 - x) * ((1 - y) * p00z[i] + y * p01z[i])
625                  - x * ((1 - y) * p10z[i] + y * p11z[i])
626                  - (1 - y) * ((1 - z) * px00[i] + z * px01[i])
627                  - y * ((1 - z) * px10[i] + z * px11[i])
628                  - (1 - z) * ((1 - x) * p0y0[i] + x * p1y0[i])
629                  - z * ((1 - x) * p0y1[i] + x * p1y1[i])
630                  + (1 - x) * ((1 - y) * ((1 - z) * p000[i] + z * p001[i])
631                  + y * ((1 - z) * p010[i] + z * p011[i]))
632                  + x * ((1 - y) * ((1 - z) * p100[i] + z * p101[i])
633                  + y * ((1 - z) * p110[i] + z * p111[i]));
634         }
635
636         SMDS_MeshNode * node = meshDS->AddNode(X[0], X[1], X[2]);
637         np[ijk].node = node;
638         meshDS->SetNodeInVolume(node, shapeID);
639       }
640     }
641   }
642
643   // find orientation of furute volumes according to MED convention
644   vector< bool > forward( nbx * nby );
645   SMDS_VolumeTool vTool;
646   for (int i = 0; i < nbx - 1; i++) {
647     for (int j = 0; j < nby - 1; j++) {
648       int n1 = j * nbx + i;
649       int n2 = j * nbx + i + 1;
650       int n3 = (j + 1) * nbx + i + 1;
651       int n4 = (j + 1) * nbx + i;
652       int n5 = nbx * nby + j * nbx + i;
653       int n6 = nbx * nby + j * nbx + i + 1;
654       int n7 = nbx * nby + (j + 1) * nbx + i + 1;
655       int n8 = nbx * nby + (j + 1) * nbx + i;
656
657       SMDS_VolumeOfNodes tmpVol (np[n1].node,np[n2].node,np[n3].node,np[n4].node,
658                                  np[n5].node,np[n6].node,np[n7].node,np[n8].node);
659       vTool.Set( &tmpVol );
660       forward[ n1 ] = vTool.IsForward();
661     }
662   }
663
664   //2.1 - for each node of the cube (less 3 *1 Faces):
665   //      - store hexahedron in SMESHDS
666   MESSAGE("Storing hexahedron into the DS");
667   for (int i = 0; i < nbx - 1; i++) {
668     for (int j = 0; j < nby - 1; j++) {
669       bool isForw = forward.at( j * nbx + i );
670       for (int k = 0; k < nbz - 1; k++) {
671         int n1 = k * nbx * nby + j * nbx + i;
672         int n2 = k * nbx * nby + j * nbx + i + 1;
673         int n3 = k * nbx * nby + (j + 1) * nbx + i + 1;
674         int n4 = k * nbx * nby + (j + 1) * nbx + i;
675         int n5 = (k + 1) * nbx * nby + j * nbx + i;
676         int n6 = (k + 1) * nbx * nby + j * nbx + i + 1;
677         int n7 = (k + 1) * nbx * nby + (j + 1) * nbx + i + 1;
678         int n8 = (k + 1) * nbx * nby + (j + 1) * nbx + i;
679
680         SMDS_MeshVolume * elt;
681         if ( isForw ) {
682           elt = aTool.AddVolume(np[n1].node, np[n2].node,
683                                 np[n3].node, np[n4].node,
684                                 np[n5].node, np[n6].node,
685                                 np[n7].node, np[n8].node);
686         }
687         else {
688           elt = aTool.AddVolume(np[n1].node, np[n4].node,
689                                 np[n3].node, np[n2].node,
690                                 np[n5].node, np[n8].node,
691                                 np[n7].node, np[n6].node);
692         }
693         
694         meshDS->SetMeshElementOnShape(elt, shapeID);
695       }
696     }
697   }
698   if ( np ) delete [] np;
699   return ClearAndReturn( aQuads, true );
700 }
701
702 //=============================================================================
703 /*!
704  *  
705  */
706 //=============================================================================
707
708 void StdMeshers_Hexa_3D::GetPoint(Pt3 p, int i, int j, int k, int nbx, int nby, int nbz,
709                                   Point3DStruct * np, const SMESHDS_Mesh * meshDS)
710 {
711         int ijk = k * nbx * nby + j * nbx + i;
712         const SMDS_MeshNode * node = np[ijk].node;
713         p[0] = node->X();
714         p[1] = node->Y();
715         p[2] = node->Z();
716         //MESSAGE(" "<<i<<" "<<j<<" "<<k<<" "<<p[0]<<" "<<p[1]<<" "<<p[2]);
717 }
718
719 //=============================================================================
720 /*!
721  *  
722  */
723 //=============================================================================
724
725 int StdMeshers_Hexa_3D::GetFaceIndex(SMESH_Mesh & aMesh,
726         const TopoDS_Shape & aShape,
727         const vector < SMESH_subMesh * >&meshFaces,
728         const TopoDS_Vertex & V0,
729         const TopoDS_Vertex & V1,
730         const TopoDS_Vertex & V2, const TopoDS_Vertex & V3)
731 {
732         //MESSAGE("StdMeshers_Hexa_3D::GetFaceIndex");
733         int faceIndex = -1;
734         for (int i = 1; i < 6; i++)
735         {
736                 const TopoDS_Shape & aFace = meshFaces[i]->GetSubShape();
737                 //const TopoDS_Face& F = TopoDS::Face(aFace);
738                 TopTools_IndexedMapOfShape M;
739                 TopExp::MapShapes(aFace, TopAbs_VERTEX, M);
740                 bool verticesInShape = false;
741                 if (M.Contains(V0))
742                         if (M.Contains(V1))
743                                 if (M.Contains(V2))
744                                         if (M.Contains(V3))
745                                                 verticesInShape = true;
746                 if (verticesInShape)
747                 {
748                         faceIndex = i;
749                         break;
750                 }
751         }
752         ASSERT(faceIndex > 0);
753         //SCRUTE(faceIndex);
754         return faceIndex;
755 }
756
757 //=============================================================================
758 /*!
759  *  
760  */
761 //=============================================================================
762
763 TopoDS_Edge
764         StdMeshers_Hexa_3D::EdgeNotInFace(SMESH_Mesh & aMesh,
765         const TopoDS_Shape & aShape,
766         const TopoDS_Face & aFace,
767         const TopoDS_Vertex & aVertex,
768         const TopTools_IndexedDataMapOfShapeListOfShape & MS)
769 {
770         //MESSAGE("StdMeshers_Hexa_3D::EdgeNotInFace");
771         TopTools_IndexedDataMapOfShapeListOfShape MF;
772         TopExp::MapShapesAndAncestors(aFace, TopAbs_VERTEX, TopAbs_EDGE, MF);
773         const TopTools_ListOfShape & ancestorsInSolid = MS.FindFromKey(aVertex);
774         const TopTools_ListOfShape & ancestorsInFace = MF.FindFromKey(aVertex);
775 //      SCRUTE(ancestorsInSolid.Extent());
776 //      SCRUTE(ancestorsInFace.Extent());
777         ASSERT(ancestorsInSolid.Extent() == 6); // 6 (edges doublees)
778         ASSERT(ancestorsInFace.Extent() == 2);
779
780         TopoDS_Edge E;
781         E.Nullify();
782         TopTools_ListIteratorOfListOfShape its(ancestorsInSolid);
783         for (; its.More(); its.Next())
784         {
785                 TopoDS_Shape ancestor = its.Value();
786                 TopTools_ListIteratorOfListOfShape itf(ancestorsInFace);
787                 bool isInFace = false;
788                 for (; itf.More(); itf.Next())
789                 {
790                         TopoDS_Shape ancestorInFace = itf.Value();
791                         if (ancestorInFace.IsSame(ancestor))
792                         {
793                                 isInFace = true;
794                                 break;
795                         }
796                 }
797                 if (!isInFace)
798                 {
799                         E = TopoDS::Edge(ancestor);
800                         break;
801                 }
802         }
803         return E;
804 }
805
806 //=============================================================================
807 /*!
808  *  
809  */
810 //=============================================================================
811
812 void StdMeshers_Hexa_3D::GetConv2DCoefs(const faceQuadStruct & quad,
813         const TopoDS_Shape & aShape,
814         const TopoDS_Vertex & V0,
815         const TopoDS_Vertex & V1,
816         const TopoDS_Vertex & V2, const TopoDS_Vertex & V3, Conv2DStruct & conv)
817 {
818 //      MESSAGE("StdMeshers_Hexa_3D::GetConv2DCoefs");
819 //      const TopoDS_Face & F = TopoDS::Face(aShape);
820 //      TopoDS_Edge E = quad.edge[0];
821 //      double f, l;
822 //      Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
823 //      TopoDS_Vertex VFirst, VLast;
824 //      TopExp::Vertices(E, VFirst, VLast);     // corresponds to f and l
825 //      bool isForward = (((l - f) * (quad.last[0] - quad.first[0])) > 0);
826   TopoDS_Vertex VA, VB;
827 //      if (isForward)
828 //      {
829 //              VA = VFirst;
830 //              VB = VLast;
831 //      }
832 //      else
833 //      {
834 //              VA = VLast;
835 //              VB = VFirst;
836 //      }
837   VA = quad.side[0]->FirstVertex();
838   VB = quad.side[0]->LastVertex();
839   
840         int a1, b1, c1, a2, b2, c2;
841         if (VA.IsSame(V0))
842                 if (VB.IsSame(V1))
843                 {
844                         a1 = 1;
845                         b1 = 0;
846                         c1 = 0;                         // x
847                         a2 = 0;
848                         b2 = 1;
849                         c2 = 0;                         // y
850                 }
851                 else
852                 {
853                         ASSERT(VB.IsSame(V3));
854                         a1 = 0;
855                         b1 = 1;
856                         c1 = 0;                         // y
857                         a2 = 1;
858                         b2 = 0;
859                         c2 = 0;                         // x
860                 }
861         if (VA.IsSame(V1))
862                 if (VB.IsSame(V2))
863                 {
864                         a1 = 0;
865                         b1 = -1;
866                         c1 = 1;                         // 1-y
867                         a2 = 1;
868                         b2 = 0;
869                         c2 = 0;                         // x
870                 }
871                 else
872                 {
873                         ASSERT(VB.IsSame(V0));
874                         a1 = -1;
875                         b1 = 0;
876                         c1 = 1;                         // 1-x
877                         a2 = 0;
878                         b2 = 1;
879                         c2 = 0;                         // y
880                 }
881         if (VA.IsSame(V2))
882                 if (VB.IsSame(V3))
883                 {
884                         a1 = -1;
885                         b1 = 0;
886                         c1 = 1;                         // 1-x
887                         a2 = 0;
888                         b2 = -1;
889                         c2 = 1;                         // 1-y
890                 }
891                 else
892                 {
893                         ASSERT(VB.IsSame(V1));
894                         a1 = 0;
895                         b1 = -1;
896                         c1 = 1;                         // 1-y
897                         a2 = -1;
898                         b2 = 0;
899                         c2 = 1;                         // 1-x
900                 }
901         if (VA.IsSame(V3))
902                 if (VB.IsSame(V0))
903                 {
904                         a1 = 0;
905                         b1 = 1;
906                         c1 = 0;                         // y
907                         a2 = -1;
908                         b2 = 0;
909                         c2 = 1;                         // 1-x
910                 }
911                 else
912                 {
913                         ASSERT(VB.IsSame(V2));
914                         a1 = 1;
915                         b1 = 0;
916                         c1 = 0;                         // x
917                         a2 = 0;
918                         b2 = -1;
919                         c2 = 1;                         // 1-y
920                 }
921 //      MESSAGE("X = " << c1 << "+ " << a1 << "*x + " << b1 << "*y");
922 //      MESSAGE("Y = " << c2 << "+ " << a2 << "*x + " << b2 << "*y");
923         conv.a1 = a1;
924         conv.b1 = b1;
925         conv.c1 = c1;
926         conv.a2 = a2;
927         conv.b2 = b2;
928         conv.c2 = c2;
929
930         int nbdown = quad.side[0]->NbPoints();
931         int nbright = quad.side[1]->NbPoints();
932         conv.ia = int (a1);
933         conv.ib = int (b1);
934         conv.ic =
935                 int (c1 * a1 * a1) * (nbdown - 1) + int (c1 * b1 * b1) * (nbright - 1);
936         conv.ja = int (a2);
937         conv.jb = int (b2);
938         conv.jc =
939                 int (c2 * a2 * a2) * (nbdown - 1) + int (c2 * b2 * b2) * (nbright - 1);
940 //      MESSAGE("I " << conv.ia << " " << conv.ib << " " << conv.ic);
941 //      MESSAGE("J " << conv.ja << " " << conv.jb << " " << conv.jc);
942 }
943
944 //================================================================================
945 /*!
946  * \brief Find a vertex opposite to the given vertex of aQuads[0]
947   * \param aVertex - the vertex
948   * \param aFace - the face aVertex belongs to
949   * \param aQuads - quads
950   * \retval TopoDS_Vertex - found vertex
951  */
952 //================================================================================
953
954 TopoDS_Vertex StdMeshers_Hexa_3D::OppositeVertex(const TopoDS_Vertex& aVertex,
955                                                  const TopTools_IndexedMapOfShape& aQuads0Vertices,
956                                                  FaceQuadStruct* aQuads[6])
957 {
958   int i, j;
959   for ( i = 1; i < 6; ++i )
960   {
961     TopoDS_Vertex VV[] = { aQuads[i]->side[0]->FirstVertex(),
962                            aQuads[i]->side[0]->LastVertex() , 
963                            aQuads[i]->side[2]->LastVertex() ,
964                            aQuads[i]->side[2]->FirstVertex() };
965     for ( j = 0; j < 4; ++j )
966       if ( aVertex.IsSame( VV[ j ]))
967         break;
968     if ( j < 4 ) {
969       int jPrev = j ? j - 1 : 3;
970       int jNext = (j + 1) % 4;
971       if ( aQuads0Vertices.Contains( VV[ jPrev ] ))
972         return VV[ jNext ];
973       else
974         return VV[ jPrev ];
975     }
976   }
977   return TopoDS_Vertex();
978 }
979
980 //modified by NIZNHY-PKV Wed Nov 17 15:34:13 2004 f
981 ///////////////////////////////////////////////////////////////////////////////
982 //ZZ
983 //#include <stdio.h>
984
985 //=======================================================================
986 //function : ComputePentahedralMesh
987 //purpose  : 
988 //=======================================================================
989
990 SMESH_ComputeErrorPtr ComputePentahedralMesh(SMESH_Mesh &         aMesh,
991                                              const TopoDS_Shape & aShape)
992 {
993   //printf(" ComputePentahedralMesh HERE\n");
994   //
995   bool bOK;
996   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
997   //int iErr;
998   StdMeshers_Penta_3D anAlgo;
999   //
1000   bOK=anAlgo.Compute(aMesh, aShape);
1001   //
1002   err = anAlgo.GetComputeError();
1003   //
1004   if ( !bOK && anAlgo.ErrorStatus() == 5 )
1005   {
1006     static StdMeshers_Prism_3D * aPrism3D = 0;
1007     if ( !aPrism3D ) {
1008       SMESH_Gen* gen = aMesh.GetGen();
1009       aPrism3D = new StdMeshers_Prism_3D( gen->GetANewId(), 0, gen );
1010     }
1011     SMESH_Hypothesis::Hypothesis_Status aStatus;
1012     if ( aPrism3D->CheckHypothesis( aMesh, aShape, aStatus ) ) {
1013       bOK = aPrism3D->Compute( aMesh, aShape );
1014       err = aPrism3D->GetComputeError();
1015     }
1016   }
1017   return err;
1018 }
1019
1020