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