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