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