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