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