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