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