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