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