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