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