Salome HOME
Mesh redesine. New fields added to specify whether hypothesis is main or additional...
[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         TopoDS_Shell aShell;
610         TopExp_Explorer exp(aShape, TopAbs_SHELL);
611         if (exp.More())
612         {
613                 aShell = TopoDS::Shell(exp.Current());
614         }
615         else
616         {
617                 MESSAGE("no shell...");
618                 ASSERT(0);
619         }
620
621         Pt3 p000, p001, p010, p011, p100, p101, p110, p111;
622         Pt3 px00, px01, px10, px11;
623         Pt3 p0y0, p0y1, p1y0, p1y1;
624         Pt3 p00z, p01z, p10z, p11z;
625         Pt3 pxy0, pxy1, px0z, px1z, p0yz, p1yz;
626
627         GetPoint(p000, 0, 0, 0, nbx, nby, nbz, np, meshDS);
628         GetPoint(p001, 0, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
629         GetPoint(p010, 0, nby - 1, 0, nbx, nby, nbz, np, meshDS);
630         GetPoint(p011, 0, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
631         GetPoint(p100, nbx - 1, 0, 0, nbx, nby, nbz, np, meshDS);
632         GetPoint(p101, nbx - 1, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
633         GetPoint(p110, nbx - 1, nby - 1, 0, nbx, nby, nbz, np, meshDS);
634         GetPoint(p111, nbx - 1, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
635
636         for (int i = 1; i < nbx - 1; i++)
637         {
638                 for (int j = 1; j < nby - 1; j++)
639                 {
640                         for (int k = 1; k < nbz - 1; k++)
641                         {
642                                 // *** seulement maillage regulier
643                                 // 12 points on edges
644                                 GetPoint(px00, i, 0, 0, nbx, nby, nbz, np, meshDS);
645                                 GetPoint(px01, i, 0, nbz - 1, nbx, nby, nbz, np, meshDS);
646                                 GetPoint(px10, i, nby - 1, 0, nbx, nby, nbz, np, meshDS);
647                                 GetPoint(px11, i, nby - 1, nbz - 1, nbx, nby, nbz, np, meshDS);
648
649                                 GetPoint(p0y0, 0, j, 0, nbx, nby, nbz, np, meshDS);
650                                 GetPoint(p0y1, 0, j, nbz - 1, nbx, nby, nbz, np, meshDS);
651                                 GetPoint(p1y0, nbx - 1, j, 0, nbx, nby, nbz, np, meshDS);
652                                 GetPoint(p1y1, nbx - 1, j, nbz - 1, nbx, nby, nbz, np, meshDS);
653
654                                 GetPoint(p00z, 0, 0, k, nbx, nby, nbz, np, meshDS);
655                                 GetPoint(p01z, 0, nby - 1, k, nbx, nby, nbz, np, meshDS);
656                                 GetPoint(p10z, nbx - 1, 0, k, nbx, nby, nbz, np, meshDS);
657                                 GetPoint(p11z, nbx - 1, nby - 1, k, nbx, nby, nbz, np, meshDS);
658
659                                 // 12 points on faces
660                                 GetPoint(pxy0, i, j, 0, nbx, nby, nbz, np, meshDS);
661                                 GetPoint(pxy1, i, j, nbz - 1, nbx, nby, nbz, np, meshDS);
662                                 GetPoint(px0z, i, 0, k, nbx, nby, nbz, np, meshDS);
663                                 GetPoint(px1z, i, nby - 1, k, nbx, nby, nbz, np, meshDS);
664                                 GetPoint(p0yz, 0, j, k, nbx, nby, nbz, np, meshDS);
665                                 GetPoint(p1yz, nbx - 1, j, k, nbx, nby, nbz, np, meshDS);
666
667                                 int ijk = k * nbx * nby + j * nbx + i;
668                                 double x = double (i) / double (nbx - 1);       // *** seulement
669                                 double y = double (j) / double (nby - 1);       // *** maillage
670                                 double z = double (k) / double (nbz - 1);       // *** regulier
671
672                                 Pt3 X;
673                                 for (int i = 0; i < 3; i++)
674                                 {
675                                         X[i] =
676                                                 (1 - x) * p0yz[i] + x * p1yz[i]
677                                                 + (1 - y) * px0z[i] + y * px1z[i]
678                                                 + (1 - z) * pxy0[i] + z * pxy1[i]
679                                                 - (1 - x) * ((1 - y) * p00z[i] + y * p01z[i])
680                                                 - x * ((1 - y) * p10z[i] + y * p11z[i])
681                                                 - (1 - y) * ((1 - z) * px00[i] + z * px01[i])
682                                                 - y * ((1 - z) * px10[i] + z * px11[i])
683                                                 - (1 - z) * ((1 - x) * p0y0[i] + x * p1y0[i])
684                                                 - z * ((1 - x) * p0y1[i] + x * p1y1[i])
685                                                 + (1 - x) * ((1 - y) * ((1 - z) * p000[i] + z * p001[i])
686                                                 + y * ((1 - z) * p010[i] + z * p011[i]))
687                                                 + x * ((1 - y) * ((1 - z) * p100[i] + z * p101[i])
688                                                 + y * ((1 - z) * p110[i] + z * p111[i]));
689                                 }
690
691                                 SMDS_MeshNode * node = meshDS->AddNode(X[0], X[1], X[2]);
692                                 np[ijk].node = node;
693                                 //meshDS->SetNodeInVolume(node, TopoDS::Solid(aShape));
694                                 meshDS->SetNodeInVolume(node, aShell);
695                         }
696                 }
697         }
698
699   // find orientation of furute volumes according to MED convention
700   vector< bool > forward( nbx * nby );
701   SMDS_VolumeTool vTool;
702   for (int i = 0; i < nbx - 1; i++)
703     for (int j = 0; j < nby - 1; j++)
704     {
705       int n1 = j * nbx + i;
706       int n2 = j * nbx + i + 1;
707       int n3 = (j + 1) * nbx + i + 1;
708       int n4 = (j + 1) * nbx + i;
709       int n5 = nbx * nby + j * nbx + i;
710       int n6 = nbx * nby + j * nbx + i + 1;
711       int n7 = nbx * nby + (j + 1) * nbx + i + 1;
712       int n8 = nbx * nby + (j + 1) * nbx + i;
713
714       SMDS_VolumeOfNodes tmpVol (np[n1].node,np[n2].node,np[n3].node,np[n4].node,
715                                  np[n5].node,np[n6].node,np[n7].node,np[n8].node);
716       vTool.Set( &tmpVol );
717       forward[ n1 ] = vTool.IsForward();
718     }
719
720         //2.1 - for each node of the cube (less 3 *1 Faces):
721         //      - store hexahedron in SMESHDS
722         MESSAGE("Storing hexahedron into the DS");
723         for (int i = 0; i < nbx - 1; i++)
724                 for (int j = 0; j < nby - 1; j++)
725                 {
726                         bool isForw = forward.at( j * nbx + i );
727                         for (int k = 0; k < nbz - 1; k++)
728                         {
729                                 int n1 = k * nbx * nby + j * nbx + i;
730                                 int n2 = k * nbx * nby + j * nbx + i + 1;
731                                 int n3 = k * nbx * nby + (j + 1) * nbx + i + 1;
732                                 int n4 = k * nbx * nby + (j + 1) * nbx + i;
733                                 int n5 = (k + 1) * nbx * nby + j * nbx + i;
734                                 int n6 = (k + 1) * nbx * nby + j * nbx + i + 1;
735                                 int n7 = (k + 1) * nbx * nby + (j + 1) * nbx + i + 1;
736                                 int n8 = (k + 1) * nbx * nby + (j + 1) * nbx + i;
737
738                                 SMDS_MeshVolume * elt;
739                                 if ( isForw )
740                                   elt = meshDS->AddVolume(np[n1].node,
741                                                           np[n2].node,
742                                                           np[n3].node,
743                                                           np[n4].node,
744                                                           np[n5].node,
745                                                           np[n6].node,
746                                                           np[n7].node,
747                                                           np[n8].node);
748                                 else
749                                   elt = meshDS->AddVolume(np[n1].node,
750                                                           np[n4].node,
751                                                           np[n3].node,
752                                                           np[n2].node,
753                                                           np[n5].node,
754                                                           np[n8].node,
755                                                           np[n7].node,
756                                                           np[n6].node);
757
758                                 meshDS->SetMeshElementOnShape(elt, aShell);
759
760
761                               }
762                       }
763   if ( np ) delete [] np;
764         //MESSAGE("End of StdMeshers_Hexa_3D::Compute()");
765         return true;
766 }
767
768 //=============================================================================
769 /*!
770  *  
771  */
772 //=============================================================================
773
774 void StdMeshers_Hexa_3D::GetPoint(Pt3 p, int i, int j, int k, int nbx, int nby,
775         int nbz, Point3DStruct * np, const SMESHDS_Mesh * meshDS)
776 {
777         int ijk = k * nbx * nby + j * nbx + i;
778         const SMDS_MeshNode * node = np[ijk].node;
779         p[0] = node->X();
780         p[1] = node->Y();
781         p[2] = node->Z();
782         //MESSAGE(" "<<i<<" "<<j<<" "<<k<<" "<<p[0]<<" "<<p[1]<<" "<<p[2]);
783 }
784
785 //=============================================================================
786 /*!
787  *  
788  */
789 //=============================================================================
790
791 int StdMeshers_Hexa_3D::GetFaceIndex(SMESH_Mesh & aMesh,
792         const TopoDS_Shape & aShape,
793         const vector < SMESH_subMesh * >&meshFaces,
794         const TopoDS_Vertex & V0,
795         const TopoDS_Vertex & V1,
796         const TopoDS_Vertex & V2, const TopoDS_Vertex & V3)
797 {
798         //MESSAGE("StdMeshers_Hexa_3D::GetFaceIndex");
799         int faceIndex = -1;
800         for (int i = 1; i < 6; i++)
801         {
802                 const TopoDS_Shape & aFace = meshFaces[i]->GetSubShape();
803                 //const TopoDS_Face& F = TopoDS::Face(aFace);
804                 TopTools_IndexedMapOfShape M;
805                 TopExp::MapShapes(aFace, TopAbs_VERTEX, M);
806                 bool verticesInShape = false;
807                 if (M.Contains(V0))
808                         if (M.Contains(V1))
809                                 if (M.Contains(V2))
810                                         if (M.Contains(V3))
811                                                 verticesInShape = true;
812                 if (verticesInShape)
813                 {
814                         faceIndex = i;
815                         break;
816                 }
817         }
818         ASSERT(faceIndex > 0);
819         //SCRUTE(faceIndex);
820         return faceIndex;
821 }
822
823 //=============================================================================
824 /*!
825  *  
826  */
827 //=============================================================================
828
829 TopoDS_Edge
830         StdMeshers_Hexa_3D::EdgeNotInFace(SMESH_Mesh & aMesh,
831         const TopoDS_Shape & aShape,
832         const TopoDS_Face & aFace,
833         const TopoDS_Vertex & aVertex,
834         const TopTools_IndexedDataMapOfShapeListOfShape & MS)
835 {
836         //MESSAGE("StdMeshers_Hexa_3D::EdgeNotInFace");
837         TopTools_IndexedDataMapOfShapeListOfShape MF;
838         TopExp::MapShapesAndAncestors(aFace, TopAbs_VERTEX, TopAbs_EDGE, MF);
839         const TopTools_ListOfShape & ancestorsInSolid = MS.FindFromKey(aVertex);
840         const TopTools_ListOfShape & ancestorsInFace = MF.FindFromKey(aVertex);
841 //      SCRUTE(ancestorsInSolid.Extent());
842 //      SCRUTE(ancestorsInFace.Extent());
843         ASSERT(ancestorsInSolid.Extent() == 6); // 6 (edges doublees)
844         ASSERT(ancestorsInFace.Extent() == 2);
845
846         TopoDS_Edge E;
847         E.Nullify();
848         TopTools_ListIteratorOfListOfShape its(ancestorsInSolid);
849         for (; its.More(); its.Next())
850         {
851                 TopoDS_Shape ancestor = its.Value();
852                 TopTools_ListIteratorOfListOfShape itf(ancestorsInFace);
853                 bool isInFace = false;
854                 for (; itf.More(); itf.Next())
855                 {
856                         TopoDS_Shape ancestorInFace = itf.Value();
857                         if (ancestorInFace.IsSame(ancestor))
858                         {
859                                 isInFace = true;
860                                 break;
861                         }
862                 }
863                 if (!isInFace)
864                 {
865                         E = TopoDS::Edge(ancestor);
866                         break;
867                 }
868         }
869         return E;
870 }
871
872 //=============================================================================
873 /*!
874  *  
875  */
876 //=============================================================================
877
878 void StdMeshers_Hexa_3D::GetConv2DCoefs(const faceQuadStruct & quad,
879         const TopoDS_Shape & aShape,
880         const TopoDS_Vertex & V0,
881         const TopoDS_Vertex & V1,
882         const TopoDS_Vertex & V2, const TopoDS_Vertex & V3, Conv2DStruct & conv)
883 {
884 //      MESSAGE("StdMeshers_Hexa_3D::GetConv2DCoefs");
885         const TopoDS_Face & F = TopoDS::Face(aShape);
886         TopoDS_Edge E = quad.edge[0];
887         double f, l;
888         Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
889         TopoDS_Vertex VFirst, VLast;
890         TopExp::Vertices(E, VFirst, VLast);     // corresponds to f and l
891         bool isForward = (((l - f) * (quad.last[0] - quad.first[0])) > 0);
892         TopoDS_Vertex VA, VB;
893         if (isForward)
894         {
895                 VA = VFirst;
896                 VB = VLast;
897         }
898         else
899         {
900                 VA = VLast;
901                 VB = VFirst;
902         }
903         int a1, b1, c1, a2, b2, c2;
904         if (VA.IsSame(V0))
905                 if (VB.IsSame(V1))
906                 {
907                         a1 = 1;
908                         b1 = 0;
909                         c1 = 0;                         // x
910                         a2 = 0;
911                         b2 = 1;
912                         c2 = 0;                         // y
913                 }
914                 else
915                 {
916                         ASSERT(VB.IsSame(V3));
917                         a1 = 0;
918                         b1 = 1;
919                         c1 = 0;                         // y
920                         a2 = 1;
921                         b2 = 0;
922                         c2 = 0;                         // x
923                 }
924         if (VA.IsSame(V1))
925                 if (VB.IsSame(V2))
926                 {
927                         a1 = 0;
928                         b1 = -1;
929                         c1 = 1;                         // 1-y
930                         a2 = 1;
931                         b2 = 0;
932                         c2 = 0;                         // x
933                 }
934                 else
935                 {
936                         ASSERT(VB.IsSame(V0));
937                         a1 = -1;
938                         b1 = 0;
939                         c1 = 1;                         // 1-x
940                         a2 = 0;
941                         b2 = 1;
942                         c2 = 0;                         // y
943                 }
944         if (VA.IsSame(V2))
945                 if (VB.IsSame(V3))
946                 {
947                         a1 = -1;
948                         b1 = 0;
949                         c1 = 1;                         // 1-x
950                         a2 = 0;
951                         b2 = -1;
952                         c2 = 1;                         // 1-y
953                 }
954                 else
955                 {
956                         ASSERT(VB.IsSame(V1));
957                         a1 = 0;
958                         b1 = -1;
959                         c1 = 1;                         // 1-y
960                         a2 = -1;
961                         b2 = 0;
962                         c2 = 1;                         // 1-x
963                 }
964         if (VA.IsSame(V3))
965                 if (VB.IsSame(V0))
966                 {
967                         a1 = 0;
968                         b1 = 1;
969                         c1 = 0;                         // y
970                         a2 = -1;
971                         b2 = 0;
972                         c2 = 1;                         // 1-x
973                 }
974                 else
975                 {
976                         ASSERT(VB.IsSame(V2));
977                         a1 = 1;
978                         b1 = 0;
979                         c1 = 0;                         // x
980                         a2 = 0;
981                         b2 = -1;
982                         c2 = 1;                         // 1-y
983                 }
984 //      MESSAGE("X = " << c1 << "+ " << a1 << "*x + " << b1 << "*y");
985 //      MESSAGE("Y = " << c2 << "+ " << a2 << "*x + " << b2 << "*y");
986         conv.a1 = a1;
987         conv.b1 = b1;
988         conv.c1 = c1;
989         conv.a2 = a2;
990         conv.b2 = b2;
991         conv.c2 = c2;
992
993         int nbdown = quad.nbPts[0];
994         int nbright = quad.nbPts[1];
995         conv.ia = int (a1);
996         conv.ib = int (b1);
997         conv.ic =
998                 int (c1 * a1 * a1) * (nbdown - 1) + int (c1 * b1 * b1) * (nbright - 1);
999         conv.ja = int (a2);
1000         conv.jb = int (b2);
1001         conv.jc =
1002                 int (c2 * a2 * a2) * (nbdown - 1) + int (c2 * b2 * b2) * (nbright - 1);
1003 //      MESSAGE("I " << conv.ia << " " << conv.ib << " " << conv.ic);
1004 //      MESSAGE("J " << conv.ja << " " << conv.jb << " " << conv.jc);
1005 }
1006
1007 //=============================================================================
1008 /*!
1009  *  
1010  */
1011 //=============================================================================
1012
1013 ostream & StdMeshers_Hexa_3D::SaveTo(ostream & save)
1014 {
1015   return save;
1016 }
1017
1018 //=============================================================================
1019 /*!
1020  *  
1021  */
1022 //=============================================================================
1023
1024 istream & StdMeshers_Hexa_3D::LoadFrom(istream & load)
1025 {
1026   return load;
1027 }
1028
1029 //=============================================================================
1030 /*!
1031  *  
1032  */
1033 //=============================================================================
1034
1035 ostream & operator <<(ostream & save, StdMeshers_Hexa_3D & hyp)
1036 {
1037   return hyp.SaveTo( save );
1038 }
1039
1040 //=============================================================================
1041 /*!
1042  *  
1043  */
1044 //=============================================================================
1045
1046 istream & operator >>(istream & load, StdMeshers_Hexa_3D & hyp)
1047 {
1048   return hyp.LoadFrom( load );
1049 }
1050
1051 //modified by NIZNHY-PKV Wed Nov 17 15:34:13 2004 f
1052 ///////////////////////////////////////////////////////////////////////////////
1053 //ZZ
1054 //#include <stdio.h>
1055
1056 //=======================================================================
1057 //function : ComputePentahedralMesh
1058 //purpose  : 
1059 //=======================================================================
1060 bool ComputePentahedralMesh(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
1061 {
1062   //printf(" ComputePentahedralMesh HERE\n");
1063   //
1064   bool bOK;
1065   int iErr;
1066   StdMeshers_Penta_3D anAlgo;
1067   //
1068   bOK=anAlgo.Compute(aMesh, aShape);
1069   /*
1070   iErr=anAlgo.ErrorStatus();
1071   
1072   if (iErr) {
1073     printf("  *** Error# %d\n", iErr);
1074   }
1075   else {
1076     printf("  *** No errors# %d\n", iErr);
1077   }
1078   */
1079   return bOK;
1080 }
1081