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