Salome HOME
a92e94b84d0c6a73c5ea9c65fc10f34bf61530a5
[modules/smesh.git] / src / StdMeshers / StdMeshers_Hexa_3D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : StdMeshers_Hexa_3D.cxx
25 //           Moved here from SMESH_Hexa_3D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //  $Header$
29
30 #include "StdMeshers_Hexa_3D.hxx"
31 #include "StdMeshers_Quadrangle_2D.hxx"
32 #include "StdMeshers_FaceSide.hxx"
33 #include "StdMeshers_Penta_3D.hxx"
34 #include "StdMeshers_Prism_3D.hxx"
35
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_subMesh.hxx"
39 #include "SMESH_Comment.hxx"
40
41 #include "SMDS_MeshElement.hxx"
42 #include "SMDS_MeshNode.hxx"
43 #include "SMDS_FacePosition.hxx"
44 #include "SMDS_VolumeTool.hxx"
45 #include "SMDS_VolumeOfNodes.hxx"
46
47 #include <TopExp.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TColStd_MapOfInteger.hxx>
52
53 #include <BRep_Tool.hxx>
54 #include <Geom_Surface.hxx>
55 #include <gp_Pnt2d.hxx>
56
57 #include "utilities.h"
58 #include "Utils_ExceptHandlers.hxx"
59
60 typedef SMESH_Comment TComm;
61
62 using namespace std;
63
64 static SMESH_ComputeErrorPtr ComputePentahedralMesh(SMESH_Mesh &, const TopoDS_Shape &);
65
66 //=============================================================================
67 /*!
68  *  
69  */
70 //=============================================================================
71
72 StdMeshers_Hexa_3D::StdMeshers_Hexa_3D(int hypId, int studyId, SMESH_Gen * gen)
73   :SMESH_3D_Algo(hypId, studyId, gen)
74 {
75   MESSAGE("StdMeshers_Hexa_3D::StdMeshers_Hexa_3D");
76   _name = "Hexa_3D";
77   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit /shape type
78 }
79
80 //=============================================================================
81 /*!
82  *  
83  */
84 //=============================================================================
85
86 StdMeshers_Hexa_3D::~StdMeshers_Hexa_3D()
87 {
88   MESSAGE("StdMeshers_Hexa_3D::~StdMeshers_Hexa_3D");
89 }
90
91 //================================================================================
92 /*!
93  * \brief Clear fields and return the argument
94   * \param res - the value to return
95   * \retval bool - the argument value
96  */
97 //================================================================================
98
99 bool StdMeshers_Hexa_3D::ClearAndReturn(FaceQuadStruct* theQuads[6], const bool res)
100 {
101   for (int i = 0; i < 6; i++) {
102     delete theQuads[i];
103     theQuads[i] = NULL;
104   }
105   return res;
106 }
107
108
109 //=============================================================================
110 /*!
111  *  
112  */
113 //=============================================================================
114
115 bool StdMeshers_Hexa_3D::CheckHypothesis
116                          (SMESH_Mesh&                          aMesh,
117                           const TopoDS_Shape&                  aShape,
118                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
119 {
120   // check nb of faces in the shape
121   aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
122   int nbFaces = 0;
123   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next())
124     if ( ++nbFaces > 6 )
125       break;
126   if ( nbFaces != 6 )
127     return false;
128
129   aStatus = SMESH_Hypothesis::HYP_OK;
130   return true;
131 }
132
133 //=======================================================================
134 //function : findIJ
135 //purpose  : return i,j of the node
136 //=======================================================================
137
138 static bool findIJ (const SMDS_MeshNode* node, const FaceQuadStruct * quad, int& I, int& J)
139 {
140   I = J = 0;
141   const SMDS_FacePosition* fpos =
142     static_cast<const SMDS_FacePosition*>(node->GetPosition().get());
143   if ( ! fpos ) return false;
144   gp_Pnt2d uv( fpos->GetUParameter(), fpos->GetVParameter() );
145
146   double minDist = DBL_MAX;
147   int nbhoriz  = Min(quad->side[0]->NbPoints(), quad->side[2]->NbPoints());
148   int nbvertic = Min(quad->side[1]->NbPoints(), quad->side[3]->NbPoints());
149   for (int i = 1; i < nbhoriz - 1; i++) {
150     for (int j = 1; j < nbvertic - 1; j++) {
151       int ij = j * nbhoriz + i;
152       gp_Pnt2d uv2( quad->uv_grid[ij].u, quad->uv_grid[ij].v );
153       double dist = uv.SquareDistance( uv2 );
154       if ( dist < minDist ) {
155         minDist = dist;
156         I = i;
157         J = j;
158       }
159     }
160   }
161   return true;
162 }
163
164
165 //=============================================================================
166 /*!
167  * Hexahedron mesh on hexaedron like form
168  * -0.  - shape and face mesh verification
169  * -1.  - identify faces and vertices of the "cube"
170  * -2.  - Algorithm from:
171  * "Application de l'interpolation transfinie à la création de maillages
172  *  C0 ou G1 continus sur des triangles, quadrangles, tetraedres, pentaedres
173  *  et hexaedres déformés."
174  * Alain PERONNET - 8 janvier 1999
175  */
176 //=============================================================================
177
178 bool StdMeshers_Hexa_3D::Compute(SMESH_Mesh &         aMesh,
179                                  const TopoDS_Shape & aShape) throw(SALOME_Exception)
180 {
181   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 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     }
257     catch(SALOME_Exception & S_ex) {
258       return ClearAndReturn( aQuads, error(COMPERR_SLM_EXCEPTION,TComm(S_ex.what()) <<
259                                            " Raised by StdMeshers_Quadrangle_2D "
260                                            " on face #" << meshDS->ShapeToIndex( aFace )));
261     }
262
263     // 0.2.1 - number of points on the opposite edges must be the same
264     if (aQuads[i]->side[0]->NbPoints() != aQuads[i]->side[2]->NbPoints() ||
265         aQuads[i]->side[1]->NbPoints() != aQuads[i]->side[3]->NbPoints()
266         /*aQuads[i]->side[0]->NbEdges() != 1 ||
267         aQuads[i]->side[1]->NbEdges() != 1 ||
268         aQuads[i]->side[2]->NbEdges() != 1 ||
269         aQuads[i]->side[3]->NbEdges() != 1*/) {
270       MESSAGE("different number of points on the opposite edges of face " << i);
271       // Try to go into penta algorithm 'cause it has been improved.
272       SMESH_ComputeErrorPtr err = ComputePentahedralMesh(aMesh, aShape);
273       return ClearAndReturn( aQuads, error(err));
274     }
275   }
276
277   // 1.  - identify faces and vertices of the "cube"
278   // 1.1 - ancestor maps vertex->edges in the cube
279
280   TopTools_IndexedDataMapOfShapeListOfShape MS;
281   TopExp::MapShapesAndAncestors(aShape, TopAbs_VERTEX, TopAbs_EDGE, MS);
282
283   // 1.2 - first face is choosen as face Y=0 of the unit cube
284
285   const TopoDS_Shape & aFace = meshFaces[0]->GetSubShape();
286   const TopoDS_Face & F = TopoDS::Face(aFace);
287
288   // 1.3 - identify the 4 vertices of the face Y=0: V000, V100, V101, V001
289
290   aCube.V000 = aQuads[0]->side[0]->FirstVertex(); // will be (0,0,0) on the unit cube
291   aCube.V100 = aQuads[0]->side[0]->LastVertex();  // will be (1,0,0) on the unit cube
292   aCube.V001 = aQuads[0]->side[2]->FirstVertex(); // will be (0,0,1) on the unit cube
293   aCube.V101 = aQuads[0]->side[2]->LastVertex();  // will be (1,0,1) on the unit cube
294
295   TopTools_IndexedMapOfShape MV0;
296   TopExp::MapShapes(F, TopAbs_VERTEX, MV0);
297
298   aCube.V010 = OppositeVertex( aCube.V000, MV0, aQuads);
299   aCube.V110 = OppositeVertex( aCube.V100, MV0, aQuads);
300   aCube.V011 = OppositeVertex( aCube.V001, MV0, aQuads);
301   aCube.V111 = OppositeVertex( aCube.V101, MV0, aQuads);
302
303   // 1.6 - find remaining faces given 4 vertices
304
305   int _indY0 = 0;
306   aCube.quad_Y0 = aQuads[_indY0];
307
308   int _indY1 = GetFaceIndex(aMesh, aShape, meshFaces,
309                             aCube.V010, aCube.V011, aCube.V110, aCube.V111);
310   aCube.quad_Y1 = aQuads[_indY1];
311
312   int _indZ0 = GetFaceIndex(aMesh, aShape, meshFaces,
313                             aCube.V000, aCube.V010, aCube.V100, aCube.V110);
314   aCube.quad_Z0 = aQuads[_indZ0];
315
316   int _indZ1 = GetFaceIndex(aMesh, aShape, meshFaces,
317                             aCube.V001, aCube.V011, aCube.V101, aCube.V111);
318   aCube.quad_Z1 = aQuads[_indZ1];
319
320   int _indX0 = GetFaceIndex(aMesh, aShape, meshFaces,
321                             aCube.V000, aCube.V001, aCube.V010, aCube.V011);
322   aCube.quad_X0 = aQuads[_indX0];
323
324   int _indX1 = GetFaceIndex(aMesh, aShape, meshFaces,
325                             aCube.V100, aCube.V101, aCube.V110, aCube.V111);
326   aCube.quad_X1 = aQuads[_indX1];
327
328   // 1.7 - get convertion coefs from face 2D normalized to 3D normalized
329
330   Conv2DStruct cx0;                     // for face X=0
331   Conv2DStruct cx1;                     // for face X=1
332   Conv2DStruct cy0;
333   Conv2DStruct cy1;
334   Conv2DStruct cz0;
335   Conv2DStruct cz1;
336
337   GetConv2DCoefs(*aCube.quad_X0, meshFaces[_indX0]->GetSubShape(),
338                  aCube.V000, aCube.V010, aCube.V011, aCube.V001, cx0);
339   GetConv2DCoefs(*aCube.quad_X1, meshFaces[_indX1]->GetSubShape(),
340                  aCube.V100, aCube.V110, aCube.V111, aCube.V101, cx1);
341   GetConv2DCoefs(*aCube.quad_Y0, meshFaces[_indY0]->GetSubShape(),
342                  aCube.V000, aCube.V100, aCube.V101, aCube.V001, cy0);
343   GetConv2DCoefs(*aCube.quad_Y1, meshFaces[_indY1]->GetSubShape(),
344                  aCube.V010, aCube.V110, aCube.V111, aCube.V011, cy1);
345   GetConv2DCoefs(*aCube.quad_Z0, meshFaces[_indZ0]->GetSubShape(),
346                  aCube.V000, aCube.V100, aCube.V110, aCube.V010, cz0);
347   GetConv2DCoefs(*aCube.quad_Z1, meshFaces[_indZ1]->GetSubShape(),
348                  aCube.V001, aCube.V101, aCube.V111, aCube.V011, cz1);
349
350   // 1.8 - create a 3D structure for normalized values
351   
352   int nbx = aCube.quad_Z0->side[0]->NbPoints();
353   if (cz0.a1 == 0.) nbx = aCube.quad_Z0->side[1]->NbPoints();
354  
355   int nby = aCube.quad_X0->side[0]->NbPoints();
356   if (cx0.a1 == 0.) nby = aCube.quad_X0->side[1]->NbPoints();
357  
358   int nbz = aCube.quad_Y0->side[0]->NbPoints();
359   if (cy0.a1 != 0.) nbz = aCube.quad_Y0->side[1]->NbPoints();
360
361   int i1, j1, nbxyz = nbx * nby * nbz;
362   Point3DStruct *np = new Point3DStruct[nbxyz];
363
364   // 1.9 - store node indexes of faces
365
366   {
367     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indX0]->GetSubShape());
368
369     faceQuadStruct *quad = aCube.quad_X0;
370     int i = 0;                          // j = x/face , k = y/face
371     int nbdown = quad->side[0]->NbPoints();
372     int nbright = quad->side[1]->NbPoints();
373
374     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
375                         
376     while(itf->more()) {
377       const SMDS_MeshNode * node = itf->next();
378       if(aTool.IsMedium(node))
379         continue;
380       if ( !findIJ( node, quad, i1, j1 ))
381         return ClearAndReturn( aQuads, false );
382       int ij1 = j1 * nbdown + i1;
383       quad->uv_grid[ij1].node = node;
384     }
385
386     for (int i1 = 0; i1 < nbdown; i1++)
387       for (int j1 = 0; j1 < nbright; j1++) {
388         int ij1 = j1 * nbdown + i1;
389         int j = cx0.ia * i1 + cx0.ib * j1 + cx0.ic;     // j = x/face
390         int k = cx0.ja * i1 + cx0.jb * j1 + cx0.jc;     // k = y/face
391         int ijk = k * nbx * nby + j * nbx + i;
392         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
393         np[ijk].node = quad->uv_grid[ij1].node;
394         //SCRUTE(np[ijk].nodeId);
395       }
396   }
397
398   {
399     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indX1]->GetSubShape());
400
401     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
402
403     faceQuadStruct *quad = aCube.quad_X1;
404     int i = nbx - 1;            // j = x/face , k = y/face
405     int nbdown = quad->side[0]->NbPoints();
406     int nbright = quad->side[1]->NbPoints();
407
408     while(itf->more()) {
409       const SMDS_MeshNode * node = itf->next();
410       if(aTool.IsMedium(node))
411         continue;
412       if ( !findIJ( node, quad, i1, j1 ))
413         return ClearAndReturn( aQuads, false );
414       int ij1 = j1 * nbdown + i1;
415       quad->uv_grid[ij1].node = node;
416     }
417
418     for (int i1 = 0; i1 < nbdown; i1++)
419       for (int j1 = 0; j1 < nbright; j1++) {
420         int ij1 = j1 * nbdown + i1;
421         int j = cx1.ia * i1 + cx1.ib * j1 + cx1.ic;     // j = x/face
422         int k = cx1.ja * i1 + cx1.jb * j1 + cx1.jc;     // k = y/face
423         int ijk = k * nbx * nby + j * nbx + i;
424         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
425         np[ijk].node = quad->uv_grid[ij1].node;
426         //SCRUTE(np[ijk].nodeId);
427       }
428   }
429
430   {
431     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indY0]->GetSubShape());
432
433     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
434
435     faceQuadStruct *quad = aCube.quad_Y0;
436     int j = 0;                          // i = x/face , k = y/face
437     int nbdown = quad->side[0]->NbPoints();
438     int nbright = quad->side[1]->NbPoints();
439
440     while(itf->more()) {
441       const SMDS_MeshNode * node = itf->next();
442       if(aTool.IsMedium(node))
443         continue;
444       if ( !findIJ( node, quad, i1, j1 ))
445         return ClearAndReturn( aQuads, false );
446       int ij1 = j1 * nbdown + i1;
447       quad->uv_grid[ij1].node = node;
448     }
449
450     for (int i1 = 0; i1 < nbdown; i1++)
451       for (int j1 = 0; j1 < nbright; j1++) {
452         int ij1 = j1 * nbdown + i1;
453         int i = cy0.ia * i1 + cy0.ib * j1 + cy0.ic;     // i = x/face
454         int k = cy0.ja * i1 + cy0.jb * j1 + cy0.jc;     // k = y/face
455         int ijk = k * nbx * nby + j * nbx + i;
456         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
457         np[ijk].node = quad->uv_grid[ij1].node;
458         //SCRUTE(np[ijk].nodeId);
459       }
460   }
461
462   {
463     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indY1]->GetSubShape());
464
465     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
466
467     faceQuadStruct *quad = aCube.quad_Y1;
468     int j = nby - 1;            // i = x/face , k = y/face
469     int nbdown = quad->side[0]->NbPoints();
470     int nbright = quad->side[1]->NbPoints();
471
472     while(itf->more()) {
473       const SMDS_MeshNode * node = itf->next();
474       if(aTool.IsMedium(node))
475         continue;
476       if ( !findIJ( node, quad, i1, j1 ))
477         return ClearAndReturn( aQuads, false );
478       int ij1 = j1 * nbdown + i1;
479       quad->uv_grid[ij1].node = node;
480     }
481
482     for (int i1 = 0; i1 < nbdown; i1++)
483       for (int j1 = 0; j1 < nbright; j1++) {
484         int ij1 = j1 * nbdown + i1;
485         int i = cy1.ia * i1 + cy1.ib * j1 + cy1.ic;     // i = x/face
486         int k = cy1.ja * i1 + cy1.jb * j1 + cy1.jc;     // k = y/face
487         int ijk = k * nbx * nby + j * nbx + i;
488         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
489         np[ijk].node = quad->uv_grid[ij1].node;
490         //SCRUTE(np[ijk].nodeId);
491       }
492   }
493
494   {
495     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indZ0]->GetSubShape());
496
497     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
498
499     faceQuadStruct *quad = aCube.quad_Z0;
500     int k = 0;                          // i = x/face , j = y/face
501     int nbdown = quad->side[0]->NbPoints();
502     int nbright = quad->side[1]->NbPoints();
503
504     while(itf->more()) {
505       const SMDS_MeshNode * node = itf->next();
506       if(aTool.IsMedium(node))
507         continue;
508       if ( !findIJ( node, quad, i1, j1 ))
509         return ClearAndReturn( aQuads, false );
510       int ij1 = j1 * nbdown + i1;
511       quad->uv_grid[ij1].node = node;
512     }
513
514     for (int i1 = 0; i1 < nbdown; i1++)
515       for (int j1 = 0; j1 < nbright; j1++) {
516         int ij1 = j1 * nbdown + i1;
517         int i = cz0.ia * i1 + cz0.ib * j1 + cz0.ic;     // i = x/face
518         int j = cz0.ja * i1 + cz0.jb * j1 + cz0.jc;     // j = y/face
519         int ijk = k * nbx * nby + j * nbx + i;
520         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
521         np[ijk].node = quad->uv_grid[ij1].node;
522         //SCRUTE(np[ijk].nodeId);
523       }
524   }
525
526   {
527     const TopoDS_Face & F = TopoDS::Face(meshFaces[_indZ1]->GetSubShape());
528
529     SMDS_NodeIteratorPtr itf= aMesh.GetSubMesh(F)->GetSubMeshDS()->GetNodes();
530
531     faceQuadStruct *quad = aCube.quad_Z1;
532     int k = nbz - 1;            // i = x/face , j = y/face
533     int nbdown = quad->side[0]->NbPoints();
534     int nbright = quad->side[1]->NbPoints();
535     
536     while(itf->more()) {
537       const SMDS_MeshNode * node = itf->next();
538       if(aTool.IsMedium(node))
539         continue;
540       if ( !findIJ( node, quad, i1, j1 ))
541         return ClearAndReturn( aQuads, false );
542       int ij1 = j1 * nbdown + i1;
543       quad->uv_grid[ij1].node = node;
544     }
545
546     for (int i1 = 0; i1 < nbdown; i1++)
547       for (int j1 = 0; j1 < nbright; j1++) {
548         int ij1 = j1 * nbdown + i1;
549         int i = cz1.ia * i1 + cz1.ib * j1 + cz1.ic;     // i = x/face
550         int j = cz1.ja * i1 + cz1.jb * j1 + cz1.jc;     // j = y/face
551         int ijk = k * nbx * nby + j * nbx + i;
552         //MESSAGE(" "<<ij1<<" "<<i<<" "<<j<<" "<<ijk);
553         np[ijk].node = quad->uv_grid[ij1].node;
554         //SCRUTE(np[ijk].nodeId);
555       }
556   }
557
558   // 2.0 - for each node of the cube:
559   //       - get the 8 points 3D = 8 vertices of the cube
560   //       - get the 12 points 3D on the 12 edges of the cube
561   //       - get the 6 points 3D on the 6 faces with their ID
562   //       - compute the point 3D
563   //       - store the point 3D in SMESHDS, store its ID in 3D structure
564
565   int shapeID = meshDS->ShapeToIndex( aShape );
566
567   Pt3 p000, p001, p010, p011, p100, p101, p110, p111;
568   Pt3 px00, px01, px10, px11;
569   Pt3 p0y0, p0y1, p1y0, p1y1;
570   Pt3 p00z, p01z, p10z, p11z;
571   Pt3 pxy0, pxy1, px0z, px1z, p0yz, p1yz;
572
573   GetPoint(p000, 0, 0, 0, nbx, nby, nbz, np, meshDS);
574   GetPoint(p001, 0, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
575   GetPoint(p010, 0, nby - 1, 0, nbx, nby, nbz, np, meshDS);
576   GetPoint(p011, 0, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
577   GetPoint(p100, nbx - 1, 0, 0, nbx, nby, nbz, np, meshDS);
578   GetPoint(p101, nbx - 1, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
579   GetPoint(p110, nbx - 1, nby - 1, 0, nbx, nby, nbz, np, meshDS);
580   GetPoint(p111, nbx - 1, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
581
582   for (int i = 1; i < nbx - 1; i++) {
583     for (int j = 1; j < nby - 1; j++) {
584       for (int k = 1; k < nbz - 1; k++) {
585         // *** seulement maillage regulier
586         // 12 points on edges
587         GetPoint(px00, i, 0, 0, nbx, nby, nbz, np, meshDS);
588         GetPoint(px01, i, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
589         GetPoint(px10, i, nby - 1, 0, nbx, nby, nbz, np, meshDS);
590         GetPoint(px11, i, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
591
592         GetPoint(p0y0, 0, j, 0, nbx, nby, nbz, np, meshDS);
593         GetPoint(p0y1, 0, j, nbz - 1, nbx, nby, nbz, np, meshDS);
594         GetPoint(p1y0, nbx - 1, j, 0, nbx, nby, nbz, np, meshDS);
595         GetPoint(p1y1, nbx - 1, j, nbz - 1, nbx, nby, nbz, np, meshDS);
596
597         GetPoint(p00z, 0, 0, k, nbx, nby, nbz, np, meshDS);
598         GetPoint(p01z, 0, nby - 1, k, nbx, nby, nbz, np, meshDS);
599         GetPoint(p10z, nbx - 1, 0, k, nbx, nby, nbz, np, meshDS);
600         GetPoint(p11z, nbx - 1, nby - 1, k, nbx, nby, nbz, np, meshDS);
601
602         // 12 points on faces
603         GetPoint(pxy0, i, j, 0, nbx, nby, nbz, np, meshDS);
604         GetPoint(pxy1, i, j, nbz - 1, nbx, nby, nbz, np, meshDS);
605         GetPoint(px0z, i, 0, k, nbx, nby, nbz, np, meshDS);
606         GetPoint(px1z, i, nby - 1, k, nbx, nby, nbz, np, meshDS);
607         GetPoint(p0yz, 0, j, k, nbx, nby, nbz, np, meshDS);
608         GetPoint(p1yz, nbx - 1, j, k, nbx, nby, nbz, np, meshDS);
609
610         int ijk = k * nbx * nby + j * nbx + i;
611         double x = double (i) / double (nbx - 1);       // *** seulement
612         double y = double (j) / double (nby - 1);       // *** maillage
613         double z = double (k) / double (nbz - 1);       // *** regulier
614
615         Pt3 X;
616         for (int i = 0; i < 3; i++) {
617           X[i] = (1 - x) * p0yz[i] + x * p1yz[i]
618                  + (1 - y) * px0z[i] + y * px1z[i]
619                  + (1 - z) * pxy0[i] + z * pxy1[i]
620                  - (1 - x) * ((1 - y) * p00z[i] + y * p01z[i])
621                  - x * ((1 - y) * p10z[i] + y * p11z[i])
622                  - (1 - y) * ((1 - z) * px00[i] + z * px01[i])
623                  - y * ((1 - z) * px10[i] + z * px11[i])
624                  - (1 - z) * ((1 - x) * p0y0[i] + x * p1y0[i])
625                  - z * ((1 - x) * p0y1[i] + x * p1y1[i])
626                  + (1 - x) * ((1 - y) * ((1 - z) * p000[i] + z * p001[i])
627                  + y * ((1 - z) * p010[i] + z * p011[i]))
628                  + x * ((1 - y) * ((1 - z) * p100[i] + z * p101[i])
629                  + y * ((1 - z) * p110[i] + z * p111[i]));
630         }
631
632         SMDS_MeshNode * node = meshDS->AddNode(X[0], X[1], X[2]);
633         np[ijk].node = node;
634         meshDS->SetNodeInVolume(node, shapeID);
635       }
636     }
637   }
638
639   // find orientation of furute volumes according to MED convention
640   vector< bool > forward( nbx * nby );
641   SMDS_VolumeTool vTool;
642   for (int i = 0; i < nbx - 1; i++) {
643     for (int j = 0; j < nby - 1; j++) {
644       int n1 = j * nbx + i;
645       int n2 = j * nbx + i + 1;
646       int n3 = (j + 1) * nbx + i + 1;
647       int n4 = (j + 1) * nbx + i;
648       int n5 = nbx * nby + j * nbx + i;
649       int n6 = nbx * nby + j * nbx + i + 1;
650       int n7 = nbx * nby + (j + 1) * nbx + i + 1;
651       int n8 = nbx * nby + (j + 1) * nbx + i;
652
653       SMDS_VolumeOfNodes tmpVol (np[n1].node,np[n2].node,np[n3].node,np[n4].node,
654                                  np[n5].node,np[n6].node,np[n7].node,np[n8].node);
655       vTool.Set( &tmpVol );
656       forward[ n1 ] = vTool.IsForward();
657     }
658   }
659
660   //2.1 - for each node of the cube (less 3 *1 Faces):
661   //      - store hexahedron in SMESHDS
662   MESSAGE("Storing hexahedron into the DS");
663   for (int i = 0; i < nbx - 1; i++) {
664     for (int j = 0; j < nby - 1; j++) {
665       bool isForw = forward.at( j * nbx + i );
666       for (int k = 0; k < nbz - 1; k++) {
667         int n1 = k * nbx * nby + j * nbx + i;
668         int n2 = k * nbx * nby + j * nbx + i + 1;
669         int n3 = k * nbx * nby + (j + 1) * nbx + i + 1;
670         int n4 = k * nbx * nby + (j + 1) * nbx + i;
671         int n5 = (k + 1) * nbx * nby + j * nbx + i;
672         int n6 = (k + 1) * nbx * nby + j * nbx + i + 1;
673         int n7 = (k + 1) * nbx * nby + (j + 1) * nbx + i + 1;
674         int n8 = (k + 1) * nbx * nby + (j + 1) * nbx + i;
675
676         SMDS_MeshVolume * elt;
677         if ( isForw ) {
678           elt = aTool.AddVolume(np[n1].node, np[n2].node,
679                                 np[n3].node, np[n4].node,
680                                 np[n5].node, np[n6].node,
681                                 np[n7].node, np[n8].node);
682         }
683         else {
684           elt = aTool.AddVolume(np[n1].node, np[n4].node,
685                                 np[n3].node, np[n2].node,
686                                 np[n5].node, np[n8].node,
687                                 np[n7].node, np[n6].node);
688         }
689         
690         meshDS->SetMeshElementOnShape(elt, shapeID);
691       }
692     }
693   }
694   if ( np ) delete [] np;
695   return ClearAndReturn( aQuads, true );
696 }
697
698 //=============================================================================
699 /*!
700  *  
701  */
702 //=============================================================================
703
704 void StdMeshers_Hexa_3D::GetPoint(Pt3 p, int i, int j, int k, int nbx, int nby, int nbz,
705                                   Point3DStruct * np, const SMESHDS_Mesh * meshDS)
706 {
707         int ijk = k * nbx * nby + j * nbx + i;
708         const SMDS_MeshNode * node = np[ijk].node;
709         p[0] = node->X();
710         p[1] = node->Y();
711         p[2] = node->Z();
712         //MESSAGE(" "<<i<<" "<<j<<" "<<k<<" "<<p[0]<<" "<<p[1]<<" "<<p[2]);
713 }
714
715 //=============================================================================
716 /*!
717  *  
718  */
719 //=============================================================================
720
721 int StdMeshers_Hexa_3D::GetFaceIndex(SMESH_Mesh & aMesh,
722         const TopoDS_Shape & aShape,
723         const vector < SMESH_subMesh * >&meshFaces,
724         const TopoDS_Vertex & V0,
725         const TopoDS_Vertex & V1,
726         const TopoDS_Vertex & V2, const TopoDS_Vertex & V3)
727 {
728         //MESSAGE("StdMeshers_Hexa_3D::GetFaceIndex");
729         int faceIndex = -1;
730         for (int i = 1; i < 6; i++)
731         {
732                 const TopoDS_Shape & aFace = meshFaces[i]->GetSubShape();
733                 //const TopoDS_Face& F = TopoDS::Face(aFace);
734                 TopTools_IndexedMapOfShape M;
735                 TopExp::MapShapes(aFace, TopAbs_VERTEX, M);
736                 bool verticesInShape = false;
737                 if (M.Contains(V0))
738                         if (M.Contains(V1))
739                                 if (M.Contains(V2))
740                                         if (M.Contains(V3))
741                                                 verticesInShape = true;
742                 if (verticesInShape)
743                 {
744                         faceIndex = i;
745                         break;
746                 }
747         }
748         ASSERT(faceIndex > 0);
749         //SCRUTE(faceIndex);
750         return faceIndex;
751 }
752
753 //=============================================================================
754 /*!
755  *  
756  */
757 //=============================================================================
758
759 TopoDS_Edge
760         StdMeshers_Hexa_3D::EdgeNotInFace(SMESH_Mesh & aMesh,
761         const TopoDS_Shape & aShape,
762         const TopoDS_Face & aFace,
763         const TopoDS_Vertex & aVertex,
764         const TopTools_IndexedDataMapOfShapeListOfShape & MS)
765 {
766         //MESSAGE("StdMeshers_Hexa_3D::EdgeNotInFace");
767         TopTools_IndexedDataMapOfShapeListOfShape MF;
768         TopExp::MapShapesAndAncestors(aFace, TopAbs_VERTEX, TopAbs_EDGE, MF);
769         const TopTools_ListOfShape & ancestorsInSolid = MS.FindFromKey(aVertex);
770         const TopTools_ListOfShape & ancestorsInFace = MF.FindFromKey(aVertex);
771 //      SCRUTE(ancestorsInSolid.Extent());
772 //      SCRUTE(ancestorsInFace.Extent());
773         ASSERT(ancestorsInSolid.Extent() == 6); // 6 (edges doublees)
774         ASSERT(ancestorsInFace.Extent() == 2);
775
776         TopoDS_Edge E;
777         E.Nullify();
778         TopTools_ListIteratorOfListOfShape its(ancestorsInSolid);
779         for (; its.More(); its.Next())
780         {
781                 TopoDS_Shape ancestor = its.Value();
782                 TopTools_ListIteratorOfListOfShape itf(ancestorsInFace);
783                 bool isInFace = false;
784                 for (; itf.More(); itf.Next())
785                 {
786                         TopoDS_Shape ancestorInFace = itf.Value();
787                         if (ancestorInFace.IsSame(ancestor))
788                         {
789                                 isInFace = true;
790                                 break;
791                         }
792                 }
793                 if (!isInFace)
794                 {
795                         E = TopoDS::Edge(ancestor);
796                         break;
797                 }
798         }
799         return E;
800 }
801
802 //=============================================================================
803 /*!
804  *  
805  */
806 //=============================================================================
807
808 void StdMeshers_Hexa_3D::GetConv2DCoefs(const faceQuadStruct & quad,
809         const TopoDS_Shape & aShape,
810         const TopoDS_Vertex & V0,
811         const TopoDS_Vertex & V1,
812         const TopoDS_Vertex & V2, const TopoDS_Vertex & V3, Conv2DStruct & conv)
813 {
814 //      MESSAGE("StdMeshers_Hexa_3D::GetConv2DCoefs");
815 //      const TopoDS_Face & F = TopoDS::Face(aShape);
816 //      TopoDS_Edge E = quad.edge[0];
817 //      double f, l;
818 //      Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
819 //      TopoDS_Vertex VFirst, VLast;
820 //      TopExp::Vertices(E, VFirst, VLast);     // corresponds to f and l
821 //      bool isForward = (((l - f) * (quad.last[0] - quad.first[0])) > 0);
822   TopoDS_Vertex VA, VB;
823 //      if (isForward)
824 //      {
825 //              VA = VFirst;
826 //              VB = VLast;
827 //      }
828 //      else
829 //      {
830 //              VA = VLast;
831 //              VB = VFirst;
832 //      }
833   VA = quad.side[0]->FirstVertex();
834   VB = quad.side[0]->LastVertex();
835   
836         int a1, b1, c1, a2, b2, c2;
837         if (VA.IsSame(V0))
838                 if (VB.IsSame(V1))
839                 {
840                         a1 = 1;
841                         b1 = 0;
842                         c1 = 0;                         // x
843                         a2 = 0;
844                         b2 = 1;
845                         c2 = 0;                         // y
846                 }
847                 else
848                 {
849                         ASSERT(VB.IsSame(V3));
850                         a1 = 0;
851                         b1 = 1;
852                         c1 = 0;                         // y
853                         a2 = 1;
854                         b2 = 0;
855                         c2 = 0;                         // x
856                 }
857         if (VA.IsSame(V1))
858                 if (VB.IsSame(V2))
859                 {
860                         a1 = 0;
861                         b1 = -1;
862                         c1 = 1;                         // 1-y
863                         a2 = 1;
864                         b2 = 0;
865                         c2 = 0;                         // x
866                 }
867                 else
868                 {
869                         ASSERT(VB.IsSame(V0));
870                         a1 = -1;
871                         b1 = 0;
872                         c1 = 1;                         // 1-x
873                         a2 = 0;
874                         b2 = 1;
875                         c2 = 0;                         // y
876                 }
877         if (VA.IsSame(V2))
878                 if (VB.IsSame(V3))
879                 {
880                         a1 = -1;
881                         b1 = 0;
882                         c1 = 1;                         // 1-x
883                         a2 = 0;
884                         b2 = -1;
885                         c2 = 1;                         // 1-y
886                 }
887                 else
888                 {
889                         ASSERT(VB.IsSame(V1));
890                         a1 = 0;
891                         b1 = -1;
892                         c1 = 1;                         // 1-y
893                         a2 = -1;
894                         b2 = 0;
895                         c2 = 1;                         // 1-x
896                 }
897         if (VA.IsSame(V3))
898                 if (VB.IsSame(V0))
899                 {
900                         a1 = 0;
901                         b1 = 1;
902                         c1 = 0;                         // y
903                         a2 = -1;
904                         b2 = 0;
905                         c2 = 1;                         // 1-x
906                 }
907                 else
908                 {
909                         ASSERT(VB.IsSame(V2));
910                         a1 = 1;
911                         b1 = 0;
912                         c1 = 0;                         // x
913                         a2 = 0;
914                         b2 = -1;
915                         c2 = 1;                         // 1-y
916                 }
917 //      MESSAGE("X = " << c1 << "+ " << a1 << "*x + " << b1 << "*y");
918 //      MESSAGE("Y = " << c2 << "+ " << a2 << "*x + " << b2 << "*y");
919         conv.a1 = a1;
920         conv.b1 = b1;
921         conv.c1 = c1;
922         conv.a2 = a2;
923         conv.b2 = b2;
924         conv.c2 = c2;
925
926         int nbdown = quad.side[0]->NbPoints();
927         int nbright = quad.side[1]->NbPoints();
928         conv.ia = int (a1);
929         conv.ib = int (b1);
930         conv.ic =
931                 int (c1 * a1 * a1) * (nbdown - 1) + int (c1 * b1 * b1) * (nbright - 1);
932         conv.ja = int (a2);
933         conv.jb = int (b2);
934         conv.jc =
935                 int (c2 * a2 * a2) * (nbdown - 1) + int (c2 * b2 * b2) * (nbright - 1);
936 //      MESSAGE("I " << conv.ia << " " << conv.ib << " " << conv.ic);
937 //      MESSAGE("J " << conv.ja << " " << conv.jb << " " << conv.jc);
938 }
939
940 //================================================================================
941 /*!
942  * \brief Find a vertex opposite to the given vertex of aQuads[0]
943   * \param aVertex - the vertex
944   * \param aFace - the face aVertex belongs to
945   * \param aQuads - quads
946   * \retval TopoDS_Vertex - found vertex
947  */
948 //================================================================================
949
950 TopoDS_Vertex StdMeshers_Hexa_3D::OppositeVertex(const TopoDS_Vertex& aVertex,
951                                                  const TopTools_IndexedMapOfShape& aQuads0Vertices,
952                                                  FaceQuadStruct* aQuads[6])
953 {
954   int i, j;
955   for ( i = 1; i < 6; ++i )
956   {
957     TopoDS_Vertex VV[] = { aQuads[i]->side[0]->FirstVertex(),
958                            aQuads[i]->side[0]->LastVertex() , 
959                            aQuads[i]->side[2]->LastVertex() ,
960                            aQuads[i]->side[2]->FirstVertex() };
961     for ( j = 0; j < 4; ++j )
962       if ( aVertex.IsSame( VV[ j ]))
963         break;
964     if ( j < 4 ) {
965       int jPrev = j ? j - 1 : 3;
966       int jNext = (j + 1) % 4;
967       if ( aQuads0Vertices.Contains( VV[ jPrev ] ))
968         return VV[ jNext ];
969       else
970         return VV[ jPrev ];
971     }
972   }
973   return TopoDS_Vertex();
974 }
975
976 //modified by NIZNHY-PKV Wed Nov 17 15:34:13 2004 f
977 ///////////////////////////////////////////////////////////////////////////////
978 //ZZ
979 //#include <stdio.h>
980
981 //=======================================================================
982 //function : ComputePentahedralMesh
983 //purpose  : 
984 //=======================================================================
985
986 SMESH_ComputeErrorPtr ComputePentahedralMesh(SMESH_Mesh &         aMesh,
987                                              const TopoDS_Shape & aShape)
988 {
989   //printf(" ComputePentahedralMesh HERE\n");
990   //
991   bool bOK;
992   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
993   //int iErr;
994   StdMeshers_Penta_3D anAlgo;
995   //
996   bOK=anAlgo.Compute(aMesh, aShape);
997   //
998   err = anAlgo.GetComputeError();
999   //
1000   if ( !bOK && anAlgo.ErrorStatus() == 5 )
1001   {
1002     static StdMeshers_Prism_3D * aPrism3D = 0;
1003     if ( !aPrism3D ) {
1004       SMESH_Gen* gen = aMesh.GetGen();
1005       aPrism3D = new StdMeshers_Prism_3D( gen->GetANewId(), 0, gen );
1006     }
1007     SMESH_Hypothesis::Hypothesis_Status aStatus;
1008     if ( aPrism3D->CheckHypothesis( aMesh, aShape, aStatus ) ) {
1009       bOK = aPrism3D->Compute( aMesh, aShape );
1010       err = aPrism3D->GetComputeError();
1011     }
1012   }
1013   return err;
1014 }
1015
1016