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