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