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