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