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