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