Salome HOME
Implementation of Quadrangle (Mapping) for faces built on 3 edges (0018911 from Mantis).
[modules/smesh.git] / src / StdMeshers / StdMeshers_Penta_3D.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH StdMeshers_Penta_3D implementaion of SMESH idl descriptions
23 //  File   : StdMeshers_Penta_3D.cxx
24 //  Module : SMESH
25 //
26 #include "StdMeshers_Penta_3D.hxx"
27
28 #include "utilities.h"
29 #include "Utils_ExceptHandlers.hxx"
30
31 #include "SMDS_EdgePosition.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_VolumeOfNodes.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESHDS_SubMesh.hxx"
36 #include "SMESH_Mesh.hxx"
37 #include "SMESH_MesherHelper.hxx"
38 #include "SMESH_subMesh.hxx"
39 #include "SMESH_subMeshEventListener.hxx"
40 #include "SMESH_Comment.hxx"
41
42 #include <BRep_Tool.hxx>
43 #include <TopExp.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <TopTools_ListOfShape.hxx>
49 #include <TopTools_SequenceOfShape.hxx>
50 #include <TopTools_MapOfShape.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Shell.hxx>
54 #include <TopoDS_Vertex.hxx>
55 #include <gp_Pnt.hxx>
56
57 #include <stdio.h>
58 #include <algorithm>
59
60 using namespace std;
61
62 typedef map < int, int, less<int> >::iterator   \
63   StdMeshers_IteratorOfDataMapOfIntegerInteger;
64
65 enum { NB_WALL_FACES = 4 };
66
67 //=======================================================================
68 //function : StdMeshers_Penta_3D
69 //purpose  : 
70 //=======================================================================
71 StdMeshers_Penta_3D::StdMeshers_Penta_3D()
72 : myErrorStatus(SMESH_ComputeError::New())
73 {
74   myTol3D=0.1;
75   myWallNodesMaps.resize( SMESH_Block::NbFaces() );
76   myShapeXYZ.resize( SMESH_Block::NbSubShapes() );
77   myTool = 0;
78 }
79
80 //=======================================================================
81 //function : ~StdMeshers_Penta_3D
82 //purpose  : 
83 //=======================================================================
84
85 StdMeshers_Penta_3D::~StdMeshers_Penta_3D()
86 {
87 }
88
89 //=======================================================================
90 //function : Compute
91 //purpose  : 
92 //=======================================================================
93 bool StdMeshers_Penta_3D::Compute(SMESH_Mesh& aMesh, 
94                                   const TopoDS_Shape& aShape)
95 {
96   MESSAGE("StdMeshers_Penta_3D::Compute()");
97   //
98   bool bOK=false;
99   //
100   myShape=aShape;
101   SetMesh(aMesh);
102   //
103   CheckData();
104   if (!myErrorStatus->IsOK()) {
105     return bOK;
106   }
107
108   //
109   MakeBlock();
110   if (!myErrorStatus->IsOK()) {
111     return bOK;
112   }
113   //
114   ClearMeshOnFxy1();
115   if (!myErrorStatus->IsOK()) {
116     return bOK;
117   }
118
119   // now unnecessary faces removed, we can load medium nodes
120   SMESH_MesherHelper helper(aMesh);
121   myTool = &helper;
122   myCreateQuadratic = myTool->IsQuadraticSubMesh(aShape);
123
124   //
125   MakeNodes();
126   if (!myErrorStatus->IsOK()) {
127     return bOK;
128   }
129   //
130   MakeConnectingMap();
131   //
132   MakeMeshOnFxy1();
133   if (!myErrorStatus->IsOK()) {
134     return bOK;
135   }
136   //
137   MakeVolumeMesh();
138   //
139   return !bOK;
140 }
141
142 //=======================================================================
143 //function : MakeNodes
144 //purpose  : 
145 //=======================================================================
146 void StdMeshers_Penta_3D::MakeNodes()
147 {
148   const int aNbSIDs=9;
149   int i, j, k, ij, iNbN, aNodeID, aSize, iErr;
150   double aX, aY, aZ;
151   SMESH_Block::TShapeID aSID, aSIDs[aNbSIDs]={
152     SMESH_Block::ID_V000, SMESH_Block::ID_V100, 
153     SMESH_Block::ID_V110, SMESH_Block::ID_V010,
154     SMESH_Block::ID_Ex00, SMESH_Block::ID_E1y0, 
155     SMESH_Block::ID_Ex10, SMESH_Block::ID_E0y0,
156     SMESH_Block::ID_Fxy0
157   }; 
158   //
159   SMESH_Mesh* pMesh=GetMesh();
160   //
161   // 1. Define the sizes of mesh
162   //
163   // 1.1 Horizontal size
164   myJSize=0;
165   for (i=0; i<aNbSIDs; ++i) {
166     const TopoDS_Shape& aS = myBlock.Shape(aSIDs[i]);
167     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
168     ASSERT(aSubMesh);
169     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
170     if(!myCreateQuadratic) {
171       iNbN = aSM->NbNodes();
172     }
173     else {
174       iNbN = 0;
175       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
176       while(itn->more()) {
177         const SMDS_MeshNode* aNode = itn->next();
178         if(myTool->IsMedium(aNode))
179           continue;
180         iNbN++;
181       }
182     }
183     myJSize += iNbN;
184   }
185   //printf("***  Horizontal: number of nodes summary=%d\n", myJSize);
186   //
187   // 1.2 Vertical size
188   myISize=2;
189   {
190     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_E00z);
191     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aS);
192     ASSERT(aSubMesh);
193     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
194     if(!myCreateQuadratic) {
195       iNbN = aSM->NbNodes();
196     }
197     else {
198       iNbN = 0;
199       SMDS_NodeIteratorPtr itn = aSM->GetNodes();
200       while(itn->more()) {
201         const SMDS_MeshNode* aNode = itn->next();
202         if(myTool->IsMedium(aNode))
203           continue;
204         iNbN++;
205       }
206     }
207     myISize += iNbN;
208   }
209   //printf("***  Vertical: number of nodes on edges and vertices=%d\n", myISize);
210   //
211   aSize=myISize*myJSize;
212   myTNodes.resize(aSize);
213   //
214   StdMeshers_TNode aTNode;
215   gp_XYZ aCoords;
216   gp_Pnt aP3D;
217   //
218   // 2. Fill the repers on base face (Z=0)
219   i=0; j=0;
220   // vertices
221   for (k=0; k<aNbSIDs; ++k) {
222     aSID=aSIDs[k];
223     const TopoDS_Shape& aS = myBlock.Shape(aSID);
224     SMDS_NodeIteratorPtr ite = pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
225     while(ite->more()) {
226       const SMDS_MeshNode* aNode = ite->next();
227       if(myTool->IsMedium(aNode))
228         continue;
229       aNodeID=aNode->GetID();
230       //
231       aTNode.SetNode(aNode);
232       aTNode.SetShapeSupportID(aSID);
233       aTNode.SetBaseNodeID(aNodeID);
234       //
235       if ( SMESH_Block::IsEdgeID (aSID)) {
236         const SMDS_EdgePosition* epos =
237           static_cast<const SMDS_EdgePosition*>(aNode->GetPosition().get());
238         myBlock.ComputeParameters( epos->GetUParameter(), aS, aCoords );
239       }
240       else {
241         aX=aNode->X();
242         aY=aNode->Y();
243         aZ=aNode->Z();
244         aP3D.SetCoord(aX, aY, aZ);
245         myBlock.ComputeParameters(aP3D, aS, aCoords);
246       }
247       iErr = myBlock.ErrorStatus();
248       if (iErr) {
249         MESSAGE("StdMeshers_Penta_3D::MakeNodes()," <<
250                 "SMESHBlock: ComputeParameters operation failed");
251         myErrorStatus=myBlock.GetError();
252         return;
253       }
254       aTNode.SetNormCoord(aCoords);
255       ij=i*myJSize+j;
256       myTNodes[ij]=aTNode;
257       ++j;
258     }
259   }
260
261   // 3.1 Fill maps of wall nodes
262   SMESH_Block::TShapeID wallFaceID[ NB_WALL_FACES ] = {
263     SMESH_Block::ID_Fx0z, SMESH_Block::ID_Fx1z,
264     SMESH_Block::ID_F0yz, SMESH_Block::ID_F1yz
265     };
266   SMESH_Block::TShapeID baseEdgeID[ NB_WALL_FACES ] = {
267     SMESH_Block::ID_Ex00, SMESH_Block::ID_Ex10,
268     SMESH_Block::ID_E0y0, SMESH_Block::ID_E1y0
269     };
270   for ( i = 0; i < NB_WALL_FACES ; ++i ) {
271     int fIndex = SMESH_Block::ShapeIndex( wallFaceID[ i ]);
272     bool ok = LoadIJNodes (myWallNodesMaps[ fIndex ],
273                            TopoDS::Face( myBlock.Shape( wallFaceID[ i ] )),
274                            TopoDS::Edge( myBlock.Shape( baseEdgeID[ i ] )),
275                            pMesh->GetMeshDS());
276     if ( !ok ) {
277       myErrorStatus->myName = COMPERR_BAD_INPUT_MESH;
278       myErrorStatus->myComment = SMESH_Comment() <<
279         "Can't find regular quadrangle mesh on a side face #" <<
280         pMesh->GetMeshDS()->ShapeToIndex( myBlock.Shape( wallFaceID[ i ]));
281       return;
282     }
283   }
284
285   // 3.2 find node columns for vertical edges and edge IDs
286   vector<const SMDS_MeshNode*> * verticEdgeNodes[ NB_WALL_FACES ];
287   SMESH_Block::TShapeID          verticEdgeID   [ NB_WALL_FACES ];
288   for ( i = 0; i < NB_WALL_FACES ; ++i ) { // 4 first base nodes are nodes on vertices
289     // edge ID
290     SMESH_Block::TShapeID eID, vID = aSIDs[ i ];
291     ShapeSupportID(false, vID, eID);
292     verticEdgeID[ i ] = eID;
293     // column nodes
294     StdMeshers_TNode& aTNode = myTNodes[ i ];
295     verticEdgeNodes[ i ] = 0;
296     for ( j = 0; j < NB_WALL_FACES ; ++j ) { // loop on 4 wall faces
297       int fIndex = SMESH_Block::ShapeIndex( wallFaceID[ j ]);
298       StdMeshers_IJNodeMap & ijNodes= myWallNodesMaps[ fIndex ];
299       if ( ijNodes.begin()->second[0] == aTNode.Node() )
300         verticEdgeNodes[ i ] = & ijNodes.begin()->second;
301       else if ( ijNodes.rbegin()->second[0] == aTNode.Node() )
302         verticEdgeNodes[ i ] = & ijNodes.rbegin()->second;
303       if ( verticEdgeNodes[ i ] )
304         break;
305     }
306   }
307
308   // 3.3 set XYZ of vertices, and initialize of the rest
309   SMESHDS_Mesh* aMesh = GetMesh()->GetMeshDS();
310   for ( int id = SMESH_Block::ID_V000; id < SMESH_Block::ID_Shell; ++id ) {
311     if ( SMESH_Block::IsVertexID( id )) {
312       TopoDS_Shape V = myBlock.Shape( id );
313       SMESHDS_SubMesh* sm = aMesh->MeshElements( V );
314       const SMDS_MeshNode* n = sm->GetNodes()->next();
315       myShapeXYZ[ id ].SetCoord( n->X(), n->Y(), n->Z() );
316     }
317     else
318       myShapeXYZ[ id ].SetCoord( 0., 0., 0. );
319   }
320
321
322   // 4. Fill the rest repers
323   bool bIsUpperLayer;
324   int iBNID;
325   SMESH_Block::TShapeID aSSID, aBNSSID;
326   StdMeshers_TNode aTN;
327   //
328
329   // create top face and find UV for it's corners
330   const TopoDS_Face& TopFace = TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
331   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
332   int topfaceID = meshDS->ShapeToIndex(TopFace);
333   const TopoDS_Vertex& v001 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V001));
334   SMDS_NodeIteratorPtr itn = pMesh->GetSubMeshContaining(v001)->GetSubMeshDS()->GetNodes();
335   const SMDS_MeshNode* N = itn->next();
336   gp_XY UV001 = myTool->GetNodeUV(TopFace,N);
337   const TopoDS_Vertex& v101 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V101));
338   itn = pMesh->GetSubMeshContaining(v101)->GetSubMeshDS()->GetNodes();
339   N = itn->next();
340   gp_XY UV101 = myTool->GetNodeUV(TopFace,N);
341   const TopoDS_Vertex& v011 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V011));
342   itn = pMesh->GetSubMeshContaining(v011)->GetSubMeshDS()->GetNodes();
343   N = itn->next();
344   gp_XY UV011 = myTool->GetNodeUV(TopFace,N);
345   const TopoDS_Vertex& v111 = TopoDS::Vertex(myBlock.Shape(SMESH_Block::ID_V111));
346   itn = pMesh->GetSubMeshContaining(v111)->GetSubMeshDS()->GetNodes();
347   N = itn->next();
348   gp_XY UV111 = myTool->GetNodeUV(TopFace,N);
349
350   for (j=0; j<myJSize; ++j) { // loop on all nodes of the base face (ID_Fxy0)
351     // base node info
352     const StdMeshers_TNode& aBN = myTNodes[j];
353     aBNSSID = (SMESH_Block::TShapeID)aBN.ShapeSupportID();
354     iBNID = aBN.BaseNodeID();
355     const gp_XYZ& aBNXYZ = aBN.NormCoord();
356     bool createNode = ( aBNSSID == SMESH_Block::ID_Fxy0 ); // if base node is inside a bottom face
357     //
358     // set XYZ on horizontal edges and get node columns of faces:
359     // 2 columns for each face, between which a base node is located
360     vector<const SMDS_MeshNode*>* nColumns[8];
361     double ratio[ NB_WALL_FACES ]; // base node position between columns [0.-1.]
362     if ( createNode ) {
363       for ( k = 0; k < NB_WALL_FACES ; ++k ) {
364         ratio[ k ] = SetHorizEdgeXYZ (aBNXYZ, wallFaceID[ k ],
365                                       nColumns[k*2], nColumns[k*2+1]);
366       }
367     }
368     //
369     // XYZ on the bottom and top faces
370     const SMDS_MeshNode* n = aBN.Node();
371     myShapeXYZ[ SMESH_Block::ID_Fxy0 ].SetCoord( n->X(), n->Y(), n->Z() );
372     myShapeXYZ[ SMESH_Block::ID_Fxy1 ].SetCoord( 0., 0., 0. );
373     //
374     // first create or find a top node, then the rest ones in a column
375     for (i=myISize-1; i>0; --i) // vertical loop, from top to bottom
376     {
377       bIsUpperLayer = (i==(myISize-1));
378       gp_XY UV_Ex01, UV_Ex11, UV_E0y1, UV_E1y1;
379       if ( createNode ) // a base node is inside a top face
380       {
381         // set XYZ on vertical edges and faces
382         for ( k = 0; k < NB_WALL_FACES ; ++k ) {
383           // XYZ on a vertical edge 
384           const SMDS_MeshNode* n = (*verticEdgeNodes[ k ]) [ i ];
385           myShapeXYZ[ verticEdgeID[ k ] ].SetCoord( n->X(), n->Y(), n->Z() );
386           // XYZ on a face (part 1 from one column)
387           n = (*nColumns[k*2]) [ i ];
388           gp_XYZ xyz( n->X(), n->Y(), n->Z() );
389           myShapeXYZ[ wallFaceID[ k ]] = ( 1. - ratio[ k ]) * xyz;
390           gp_XY tmp1;
391           if( bIsUpperLayer ) {
392             tmp1 = myTool->GetNodeUV(TopFace,n);
393             tmp1 = ( 1. - ratio[ k ]) * tmp1;
394           }
395           // XYZ on a face (part 2 from other column)
396           n = (*nColumns[k*2+1]) [ i ];
397           xyz.SetCoord( n->X(), n->Y(), n->Z() );
398           myShapeXYZ[ wallFaceID[ k ]] += ratio[ k ] * xyz;
399           if( bIsUpperLayer ) {
400             gp_XY tmp2 = myTool->GetNodeUV(TopFace,n);
401             tmp1 +=  ratio[ k ] * tmp2;
402             if( k==0 )
403               UV_Ex01 = tmp1;
404             else if( k==1 )
405               UV_Ex11 = tmp1;
406             else if( k==2 )
407               UV_E0y1 = tmp1;
408             else
409               UV_E1y1 = tmp1;
410           }
411         }
412       }
413       // fill current node info
414       //   -index in aTNodes
415       ij=i*myJSize+j; 
416       //   -normalized coordinates  
417       aX=aBNXYZ.X();  
418       aY=aBNXYZ.Y();
419       //aZ=aZL[i];
420       aZ=(double)i/(double)(myISize-1);
421       aCoords.SetCoord(aX, aY, aZ);
422       //
423       //   suporting shape ID
424       ShapeSupportID(bIsUpperLayer, aBNSSID, aSSID);
425       if (!myErrorStatus->IsOK()) {
426         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
427         return;
428       }
429       //
430       aTN.SetShapeSupportID(aSSID);
431       aTN.SetNormCoord(aCoords);
432       aTN.SetBaseNodeID(iBNID);
433       //
434       if (aSSID!=SMESH_Block::ID_NONE){
435         // try to find the node
436         const TopoDS_Shape& aS=myBlock.Shape((int)aSSID);
437         FindNodeOnShape(aS, aCoords, i, aTN);
438       }
439       else{
440         // create node and get it id
441         CreateNode (bIsUpperLayer, aCoords, aTN);
442         //
443         if ( bIsUpperLayer ) {
444           const SMDS_MeshNode* n = aTN.Node();
445           myShapeXYZ[ SMESH_Block::ID_Fxy1 ].SetCoord( n->X(), n->Y(), n->Z() );
446           // set node on top face:
447           // find UV parameter for this node
448           //              UV_Ex11
449           //   UV011+-----+----------+UV111
450           //        |                |
451           //        |                |
452           // UV_E0y1+     +node      +UV_E1y1
453           //        |                |
454           //        |                |
455           //        |                |
456           //   UV001+-----+----------+UV101
457           //              UV_Ex01
458           gp_Pnt2d aP;
459           double u = aCoords.X(), v = aCoords.Y();
460           double u1 = ( 1. - u ), v1 = ( 1. - v );
461           aP.ChangeCoord()  = UV_Ex01 * v1;
462           aP.ChangeCoord() += UV_Ex11 * v;
463           aP.ChangeCoord() += UV_E0y1 * u1;
464           aP.ChangeCoord() += UV_E1y1 * u;
465           aP.ChangeCoord() -= UV001 * u1 * v1;
466           aP.ChangeCoord() -= UV101 * u  * v1;
467           aP.ChangeCoord() -= UV011 * u1 * v;
468           aP.ChangeCoord() -= UV111 * u  * v;
469           meshDS->SetNodeOnFace((SMDS_MeshNode*)n, topfaceID, aP.X(), aP.Y());
470         }
471       }
472       if (!myErrorStatus->IsOK()) {
473         MESSAGE("StdMeshers_Penta_3D::MakeNodes() ");
474         return;
475       }
476       //
477       myTNodes[ij]=aTN;
478     }
479   }
480   //DEB
481   /*
482   {
483     int iSSID, iBNID, aID;
484     //
485     for (i=0; i<myISize; ++i) {
486       printf(" Layer# %d\n", i);
487       for (j=0; j<myJSize; ++j) {
488         ij=i*myJSize+j; 
489         const StdMeshers_TNode& aTN=myTNodes[ij];
490         //const StdMeshers_TNode& aTN=aTNodes[ij];
491         const gp_XYZ& aXYZ=aTN.NormCoord();
492         iSSID=aTN.ShapeSupportID();
493         iBNID=aTN.BaseNodeID();
494         //
495         const SMDS_MeshNode* aNode=aTN.Node();
496         aID=aNode->GetID(); 
497         aX=aNode->X();
498         aY=aNode->Y();
499         aZ=aNode->Z();
500         printf("*** j:%d BNID#%d iSSID:%d ID:%d { %lf %lf %lf },  { %lf %lf %lf }\n",
501                j,  iBNID, iSSID, aID, aXYZ.X(),  aXYZ.Y(), aXYZ.Z(), aX, aY, aZ);
502       }
503     }
504   }
505   */
506   //DEB t
507 }
508
509
510 //=======================================================================
511 //function : FindNodeOnShape
512 //purpose  : 
513 //=======================================================================
514
515 void StdMeshers_Penta_3D::FindNodeOnShape(const TopoDS_Shape& aS,
516                                           const gp_XYZ&       aParams,
517                                           const int           z,
518                                           StdMeshers_TNode&   aTN)
519 {
520   double aX, aY, aZ, aD, aTol2, minD;
521   gp_Pnt aP1, aP2;
522   //
523   SMESH_Mesh* pMesh = GetMesh();
524   aTol2 = myTol3D*myTol3D;
525   minD = 1.e100;
526   SMDS_MeshNode* pNode = NULL;
527   //
528   if ( aS.ShapeType() == TopAbs_FACE ||
529        aS.ShapeType() == TopAbs_EDGE ) {
530     // find a face ID to which aTN belongs to
531     int faceID;
532     if ( aS.ShapeType() == TopAbs_FACE )
533       faceID = myBlock.ShapeID( aS );
534     else { // edge maybe vertical or top horizontal
535       gp_XYZ aCoord = aParams;
536       if ( aCoord.Z() == 1. )
537         aCoord.SetZ( 0.5 ); // move from top down
538       else
539         aCoord.SetX( 0.5 ); // move along X
540       faceID = SMESH_Block::GetShapeIDByParams( aCoord );
541     }
542     ASSERT( SMESH_Block::IsFaceID( faceID ));
543     int fIndex = SMESH_Block::ShapeIndex( faceID );
544     StdMeshers_IJNodeMap & ijNodes = myWallNodesMaps[ fIndex ];
545     // look for a base node in ijNodes
546     const SMDS_MeshNode* baseNode = pMesh->GetMeshDS()->FindNode( aTN.BaseNodeID() );
547     StdMeshers_IJNodeMap::const_iterator par_nVec = ijNodes.begin();
548     for ( ; par_nVec != ijNodes.end(); par_nVec++ )
549       if ( par_nVec->second[ 0 ] == baseNode ) {
550         pNode = (SMDS_MeshNode*)par_nVec->second.at( z );
551         aTN.SetNode(pNode);
552         return;
553       }
554   }
555   //
556   myBlock.Point(aParams, aS, aP1);
557   //
558   SMDS_NodeIteratorPtr ite=
559     pMesh->GetSubMeshContaining(aS)->GetSubMeshDS()->GetNodes();
560   while(ite->more()) {
561     const SMDS_MeshNode* aNode = ite->next();
562     if(myTool->IsMedium(aNode))
563       continue;
564     aX=aNode->X();
565     aY=aNode->Y();
566     aZ=aNode->Z();
567     aP2.SetCoord(aX, aY, aZ);
568     aD=(double)aP1.SquareDistance(aP2);
569     //printf("** D=%lf ", aD, aTol2);
570     if (aD < minD) {
571       pNode=(SMDS_MeshNode*)aNode;
572       aTN.SetNode(pNode);
573       minD = aD;
574       //printf(" Ok\n");
575       if (aD<aTol2)
576         return; 
577     }
578   }
579   //
580   //printf(" KO\n");
581   //aTN.SetNode(pNode);
582   //MESSAGE("StdMeshers_Penta_3D::FindNodeOnShape(), can not find the node");
583   //myErrorStatus=11; // can not find the node;
584 }
585
586
587 //=======================================================================
588 //function : SetHorizEdgeXYZ
589 //purpose  : 
590 //=======================================================================
591
592 double StdMeshers_Penta_3D::SetHorizEdgeXYZ(const gp_XYZ&                  aBaseNodeParams,
593                                             const int                      aFaceID,
594                                             vector<const SMDS_MeshNode*>*& aCol1,
595                                             vector<const SMDS_MeshNode*>*& aCol2)
596 {
597   // find base and top edges of the face
598   enum { BASE = 0, TOP };
599   vector< int > edgeVec; // 0-base, 1-top
600   SMESH_Block::GetFaceEdgesIDs( aFaceID, edgeVec );
601   //
602   int coord = SMESH_Block::GetCoordIndOnEdge( edgeVec[ BASE ] );
603   bool isForward = myBlock.IsForwadEdge( edgeVec[ BASE ] );
604
605   double param = aBaseNodeParams.Coord( coord );
606   if ( !isForward)
607     param = 1. - param;
608   //
609   // look for columns around param
610   StdMeshers_IJNodeMap & ijNodes =
611     myWallNodesMaps[ SMESH_Block::ShapeIndex( aFaceID )];
612   StdMeshers_IJNodeMap::iterator par_nVec_1 = ijNodes.begin();
613   while ( par_nVec_1->first < param )
614     par_nVec_1++;
615   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
616   //
617   double r = 0;
618   if ( par_nVec_1 != ijNodes.begin() ) {
619     par_nVec_1--;
620     r = ( param - par_nVec_1->first ) / ( par_nVec_2->first - par_nVec_1->first );
621   }
622   aCol1 = & par_nVec_1->second;
623   aCol2 = & par_nVec_2->second;
624
625   // top edge
626   if (1) {
627     // this variant is better for cases with curved edges and
628     // different nodes distribution on top and base edges
629     const SMDS_MeshNode* n1 = aCol1->back();
630     const SMDS_MeshNode* n2 = aCol2->back();
631     gp_XYZ xyz1( n1->X(), n1->Y(), n1->Z() );
632     gp_XYZ xyz2( n2->X(), n2->Y(), n2->Z() );
633     myShapeXYZ[ edgeVec[ 1 ] ] = ( 1. - r ) * xyz1 + r * xyz2;
634   }
635   else {
636     // this variant is better for other cases
637 //   SMESH_MesherHelper helper( *GetMesh() );
638 //   const TopoDS_Edge & edge = TopoDS::Edge( myBlock.Shape( edgeVec[ TOP ]));
639 //   double u1 = helper.GetNodeU( edge, n1 );
640 //   double u2 = helper.GetNodeU( edge, n2 );
641 //   double u = ( 1. - r ) * u1 + r * u2;
642 //   gp_XYZ topNodeParams;
643 //   myBlock.Block().EdgeParameters( edgeVec[ TOP ], u, topNodeParams );
644 //   myBlock.Block().EdgePoint( edgeVec[ TOP ],
645 //                              topNodeParams,
646 //                              myShapeXYZ[ edgeVec[ TOP ]]);
647   }
648
649   // base edge
650   myBlock.Block().EdgePoint( edgeVec[ BASE ],
651                              aBaseNodeParams,
652                              myShapeXYZ[ edgeVec[ BASE ]]);
653   return r;
654 }
655
656
657 //=======================================================================
658 //function : MakeVolumeMesh
659 //purpose  : 
660 //=======================================================================
661 void StdMeshers_Penta_3D::MakeVolumeMesh()
662 {
663   int i, j, ij, ik, i1, i2, aSSID; 
664   //
665   SMESH_Mesh*   pMesh = GetMesh();
666   SMESHDS_Mesh* meshDS = pMesh->GetMeshDS();
667   //
668   int shapeID = meshDS->ShapeToIndex( myShape );
669   //
670   // 1. Set Node In Volume
671   ik = myISize-1;
672   for (i=1; i<ik; ++i){
673     for (j=0; j<myJSize; ++j){
674       ij=i*myJSize+j;
675       const StdMeshers_TNode& aTN = myTNodes[ij];
676       aSSID=aTN.ShapeSupportID();
677       if (aSSID==SMESH_Block::ID_NONE) {
678         SMDS_MeshNode* aNode = (SMDS_MeshNode*)aTN.Node();
679         meshDS->SetNodeInVolume(aNode, shapeID);
680       }
681     }
682   }
683   //
684   // 2. Make pentahedrons
685   int aID0, k , aJ[3];
686   vector<const SMDS_MeshNode*> aN;
687   //
688   SMDS_ElemIteratorPtr itf, aItNodes;
689   //
690   const TopoDS_Face& aFxy0=
691     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
692   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
693   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
694   //
695   itf = aSM0->GetElements();
696   while(itf->more()) {
697     const SMDS_MeshElement* pE0 = itf->next();
698     //
699     int nbFaceNodes = pE0->NbNodes();
700     if(myCreateQuadratic)
701       nbFaceNodes = nbFaceNodes/2;
702     if ( aN.size() < nbFaceNodes * 2 )
703       aN.resize( nbFaceNodes * 2 );
704     //
705     k=0;
706     aItNodes=pE0->nodesIterator();
707     while (aItNodes->more()) {
708       //const SMDS_MeshElement* pNode = aItNodes->next();
709       const SMDS_MeshNode* pNode =
710         static_cast<const SMDS_MeshNode*> (aItNodes->next());
711       if(myTool->IsMedium(pNode))
712         continue;
713       aID0 = pNode->GetID();
714       aJ[k] = GetIndexOnLayer(aID0);
715       if (!myErrorStatus->IsOK()) {
716         MESSAGE("StdMeshers_Penta_3D::MakeVolumeMesh");
717         return;
718       }
719       //
720       ++k;
721     }
722     //
723     bool forward = true;
724     for (i=0; i<ik; ++i) {
725       i1=i;
726       i2=i+1;
727       for(j=0; j<nbFaceNodes; ++j) {
728         ij = i1*myJSize+aJ[j];
729         const StdMeshers_TNode& aTN1 = myTNodes[ij];
730         const SMDS_MeshNode* aN1 = aTN1.Node();
731         aN[j]=aN1;
732         //
733         ij=i2*myJSize+aJ[j];
734         const StdMeshers_TNode& aTN2 = myTNodes[ij];
735         const SMDS_MeshNode* aN2 = aTN2.Node();
736         aN[j+nbFaceNodes] = aN2;
737       }
738       // check if volume orientation will be ok
739       if ( i == 0 ) {
740         SMDS_VolumeTool vTool;
741         switch ( nbFaceNodes ) {
742         case 3: {
743           SMDS_VolumeOfNodes tmpVol (aN[0], aN[1], aN[2],
744                                      aN[3], aN[4], aN[5]);
745           vTool.Set( &tmpVol );
746           break;
747         }
748         case 4: {
749           SMDS_VolumeOfNodes tmpVol(aN[0], aN[1], aN[2], aN[3],
750                                     aN[4], aN[5], aN[6], aN[7]);
751           vTool.Set( &tmpVol );
752           break;
753         }
754         default:
755           continue;
756         }
757         forward = vTool.IsForward();
758       }
759       // add volume
760       SMDS_MeshVolume* aV = 0;
761       switch ( nbFaceNodes ) {
762       case 3:
763         if ( forward ) {
764           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2],
765           //                       aN[3], aN[4], aN[5]);
766           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3], aN[4], aN[5]);
767         }
768         else {
769           //aV = meshDS->AddVolume(aN[0], aN[2], aN[1],
770           //                       aN[3], aN[5], aN[4]);
771           aV = myTool->AddVolume(aN[0], aN[2], aN[1], aN[3], aN[5], aN[4]);
772         }
773         break;
774       case 4:
775         if ( forward ) {
776           //aV = meshDS->AddVolume(aN[0], aN[1], aN[2], aN[3],
777           //                       aN[4], aN[5], aN[6], aN[7]);
778           aV = myTool->AddVolume(aN[0], aN[1], aN[2], aN[3],
779                                  aN[4], aN[5], aN[6], aN[7]);
780         }
781         else {
782           //aV = meshDS->AddVolume(aN[0], aN[3], aN[2], aN[1],
783           //                       aN[4], aN[7], aN[6], aN[5]);
784           aV = myTool->AddVolume(aN[0], aN[3], aN[2], aN[1],
785                                  aN[4], aN[7], aN[6], aN[5]);
786         }
787         break;
788       default:
789         continue;
790       }
791       meshDS->SetMeshElementOnShape(aV, shapeID);
792     }
793   }
794 }
795
796 //=======================================================================
797 //function : MakeMeshOnFxy1
798 //purpose  : 
799 //=======================================================================
800 void StdMeshers_Penta_3D::MakeMeshOnFxy1()
801 {
802   int aID0, aJ, aLevel, ij, aNbNodes, k;
803   //
804   SMDS_NodeIteratorPtr itn;
805   SMDS_ElemIteratorPtr itf, aItNodes;
806   SMDSAbs_ElementType aElementType;
807   //
808   const TopoDS_Face& aFxy0=
809     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy0));
810   const TopoDS_Face& aFxy1=
811     TopoDS::Face(myBlock.Shape(SMESH_Block::ID_Fxy1));
812   //
813   SMESH_Mesh* pMesh = GetMesh();
814   SMESHDS_Mesh * meshDS = pMesh->GetMeshDS();
815   //
816   SMESH_subMesh *aSubMesh1 = pMesh->GetSubMeshContaining(aFxy1);
817   SMESH_subMesh *aSubMesh0 = pMesh->GetSubMeshContaining(aFxy0);
818   SMESHDS_SubMesh *aSM0 = aSubMesh0->GetSubMeshDS();
819   //
820   // set nodes on aFxy1
821   aLevel = myISize-1;
822   itn = aSM0->GetNodes();
823   aNbNodes = aSM0->NbNodes();
824   //printf("** aNbNodes=%d\n", aNbNodes);
825
826   //
827   // set elements on aFxy1
828   vector<const SMDS_MeshNode*> aNodes1;
829   //
830   itf = aSM0->GetElements();
831   while(itf->more()) {
832     const SMDS_MeshElement* pE0 = itf->next();
833     aElementType = pE0->GetType();
834     if (!aElementType==SMDSAbs_Face) {
835       continue;
836     }
837     aNbNodes = pE0->NbNodes();
838     if(myCreateQuadratic)
839       aNbNodes = aNbNodes/2;
840     if ( aNodes1.size() < aNbNodes )
841       aNodes1.resize( aNbNodes );
842     //
843     k = aNbNodes-1; // reverse a face
844     aItNodes = pE0->nodesIterator();
845     while (aItNodes->more()) {
846       const SMDS_MeshNode* pNode =
847         static_cast<const SMDS_MeshNode*> (aItNodes->next());
848       if(myTool->IsMedium(pNode))
849         continue;
850       aID0 = pNode->GetID();
851       aJ = GetIndexOnLayer(aID0);
852       if (!myErrorStatus->IsOK()) {
853         MESSAGE("StdMeshers_Penta_3D::MakeMeshOnFxy1() ");
854         return;
855       }
856       //
857       ij = aLevel*myJSize + aJ;
858       const StdMeshers_TNode& aTN1 = myTNodes[ij];
859       const SMDS_MeshNode* aN1 = aTN1.Node();
860       aNodes1[k] = aN1;
861       --k;
862     }
863     SMDS_MeshFace * face = 0;
864     switch ( aNbNodes ) {
865     case 3:
866       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2]);
867       break;
868     case 4:
869       face = myTool->AddFace(aNodes1[0], aNodes1[1], aNodes1[2], aNodes1[3]);
870       break;
871     default:
872       continue;
873     }
874     meshDS->SetMeshElementOnShape(face, aFxy1);
875   }
876
877   // update compute state of top face submesh
878   aSubMesh1->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
879
880   // assure that mesh on the top face will be cleaned when it is cleaned
881   // on the bottom face
882   SMESH_subMesh* volSM = pMesh->GetSubMesh( myTool->GetSubShape() );
883   volSM->SetEventListener( new SMESH_subMeshEventListener(true),
884                            SMESH_subMeshEventListenerData::MakeData( aSubMesh1 ),
885                            aSubMesh0 ); // translate CLEAN event of aSubMesh0 to aSubMesh1
886 }
887
888 //=======================================================================
889 //function : ClearMeshOnFxy1
890 //purpose  : 
891 //=======================================================================
892 void StdMeshers_Penta_3D::ClearMeshOnFxy1()
893 {
894   SMESH_subMesh* aSubMesh;
895   SMESH_Mesh* pMesh=GetMesh();
896   //
897   const TopoDS_Shape& aFxy1=myBlock.Shape(SMESH_Block::ID_Fxy1);
898   aSubMesh = pMesh->GetSubMeshContaining(aFxy1);
899   if (aSubMesh)
900     aSubMesh->ComputeStateEngine( SMESH_subMesh::CLEAN );
901 }
902
903 //=======================================================================
904 //function : GetIndexOnLayer
905 //purpose  : 
906 //=======================================================================
907 int StdMeshers_Penta_3D::GetIndexOnLayer(const int aID)
908 {
909   int j=-1;
910   StdMeshers_IteratorOfDataMapOfIntegerInteger aMapIt;
911   //
912   aMapIt=myConnectingMap.find(aID);
913   if (aMapIt==myConnectingMap.end()) {
914     myErrorStatus->myName    = 200;
915     myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
916     return j;
917   }
918   j=(*aMapIt).second;
919   return j;
920 }
921
922 //=======================================================================
923 //function : MakeConnectingMap
924 //purpose  : 
925 //=======================================================================
926 void StdMeshers_Penta_3D::MakeConnectingMap()
927 {
928   int j, aBNID;
929   //
930   for (j=0; j<myJSize; ++j) {
931     const StdMeshers_TNode& aBN=myTNodes[j];
932     aBNID=aBN.BaseNodeID();
933     myConnectingMap[aBNID]=j;
934   }
935 }
936
937 //=======================================================================
938 //function : CreateNode
939 //purpose  : 
940 //=======================================================================
941 void StdMeshers_Penta_3D::CreateNode(const bool bIsUpperLayer,
942                                      const gp_XYZ& aParams,
943                                      StdMeshers_TNode& aTN)
944 {
945   double aX, aY, aZ;
946   //
947   gp_Pnt aP;
948   //
949   SMDS_MeshNode* pNode=NULL; 
950   aTN.SetNode(pNode);  
951   //
952 //   if (bIsUpperLayer) {
953 //     // point on face Fxy1
954 //     const TopoDS_Shape& aS=myBlock.Shape(SMESH_Block::ID_Fxy1);
955 //     myBlock.Point(aParams, aS, aP);
956 //   }
957 //   else {
958 //     // point inside solid
959 //     myBlock.Point(aParams, aP);
960 //   }
961   if (bIsUpperLayer) {
962     double u = aParams.X(), v = aParams.Y();
963     double u1 = ( 1. - u ), v1 = ( 1. - v );
964     aP.ChangeCoord()  = myShapeXYZ[ SMESH_Block::ID_Ex01 ] * v1;
965     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_Ex11 ] * v;
966     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E0y1 ] * u1;
967     aP.ChangeCoord() += myShapeXYZ[ SMESH_Block::ID_E1y1 ] * u;
968
969     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V001 ] * u1 * v1;
970     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V101 ] * u  * v1;
971     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V011 ] * u1 * v;
972     aP.ChangeCoord() -= myShapeXYZ[ SMESH_Block::ID_V111 ] * u  * v;
973   }
974   else {
975     SMESH_Block::ShellPoint( aParams, myShapeXYZ, aP.ChangeCoord() );
976   }
977   //
978 //   iErr=myBlock.ErrorStatus();
979 //   if (iErr) {
980 //     myErrorStatus=12; // can not find the node point;
981 //     return;
982 //   }
983   //
984   aX=aP.X(); aY=aP.Y(); aZ=aP.Z(); 
985   //
986   SMESH_Mesh* pMesh = GetMesh();
987   SMESHDS_Mesh* pMeshDS = pMesh->GetMeshDS();
988   //
989   pNode = pMeshDS->AddNode(aX, aY, aZ);
990   
991   aTN.SetNode(pNode);
992 }
993
994 //=======================================================================
995 //function : ShapeSupportID
996 //purpose  : 
997 //=======================================================================
998 void StdMeshers_Penta_3D::ShapeSupportID(const bool bIsUpperLayer,
999                                          const SMESH_Block::TShapeID aBNSSID,
1000                                          SMESH_Block::TShapeID& aSSID)
1001 {
1002   switch (aBNSSID) {
1003     case SMESH_Block::ID_V000:
1004       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V001 : SMESH_Block::ID_E00z;
1005       break;
1006     case SMESH_Block::ID_V100:
1007       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V101 : SMESH_Block::ID_E10z;
1008       break; 
1009     case SMESH_Block::ID_V110:
1010       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V111 : SMESH_Block::ID_E11z;
1011       break;
1012     case SMESH_Block::ID_V010:
1013       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_V011 : SMESH_Block::ID_E01z;
1014       break;
1015     case SMESH_Block::ID_Ex00:
1016       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex01 : SMESH_Block::ID_Fx0z;
1017       break;
1018     case SMESH_Block::ID_Ex10:
1019       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_Ex11 : SMESH_Block::ID_Fx1z;
1020       break; 
1021     case SMESH_Block::ID_E0y0:
1022       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E0y1 : SMESH_Block::ID_F0yz;
1023       break; 
1024     case SMESH_Block::ID_E1y0:
1025       aSSID=(bIsUpperLayer) ?  SMESH_Block::ID_E1y1 : SMESH_Block::ID_F1yz;
1026       break; 
1027     case SMESH_Block::ID_Fxy0:
1028       aSSID=SMESH_Block::ID_NONE;//(bIsUpperLayer) ?  Shape_ID_Fxy1 : Shape_ID_NONE;
1029       break;   
1030     default:
1031       aSSID=SMESH_Block::ID_NONE;
1032       myErrorStatus->myName=10; // Can not find supporting shape ID
1033       myErrorStatus->myComment = "Internal error of StdMeshers_Penta_3D";
1034       break;
1035   }
1036   return;
1037 }
1038 //=======================================================================
1039 //function : MakeBlock
1040 //purpose  : 
1041 //=======================================================================
1042 void StdMeshers_Penta_3D::MakeBlock()
1043 {
1044   bool bFound;
1045   int i, j, iNbEV, iNbE, iErr, iCnt, iNbNodes, iNbF;
1046   //
1047   TopoDS_Vertex aV000, aV001;
1048   TopoDS_Shape aFTr;
1049   TopTools_IndexedDataMapOfShapeListOfShape aMVES;
1050   TopTools_IndexedMapOfShape aME ,aMEV, aM;
1051   TopTools_ListIteratorOfListOfShape aIt;
1052   //
1053   TopExp::MapShapes(myShape, TopAbs_FACE, aM);
1054   //
1055   // 0. Find triangulated face aFTr
1056   SMDSAbs_ElementType aElementType;
1057   SMESH_Mesh* pMesh=GetMesh();
1058   //
1059   iCnt = 0;
1060   iNbF = aM.Extent();
1061   for (i=1; i<=iNbF; ++i) {
1062     const TopoDS_Shape& aF = aM(i);
1063     SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1064     ASSERT(aSubMesh);
1065     SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1066     SMDS_ElemIteratorPtr itf = aSM->GetElements();
1067     while(itf->more()) {
1068       const SMDS_MeshElement * pElement = itf->next();
1069       aElementType = pElement->GetType();
1070       if (aElementType==SMDSAbs_Face) {
1071         iNbNodes = pElement->NbNodes();
1072         if ( iNbNodes==3 || (pElement->IsQuadratic() && iNbNodes==6) ) {
1073           aFTr = aF;
1074           ++iCnt;
1075           if (iCnt>1) {
1076             // \begin{E.A.}
1077             // The current algorithm fails if there is more that one
1078             // face wich contains triangles ...
1079             // In that case, replace return by break to try another
1080             // method (coded in "if (iCnt != 1) { ... }")
1081             //
1082             // MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1083             // myErrorStatus=5; // more than one face has triangulation
1084             // return;
1085             break;
1086             // \end{E.A.}
1087           }
1088           break; // next face
1089         }
1090       }
1091     }
1092   }
1093   //
1094   // \begin{E.A.}
1095   // The current algorithm fails if "iCnt != 1", the case "iCnt == 0"
1096   // was not reached 'cause it was not called from Hexa_3D ... Now it
1097   // can occurs and in my opinion, it is the most common case.
1098   //
1099   if (iCnt != 1) {
1100     // The suggested algorithm is the following :
1101     //
1102     // o Check that nb_of_faces == 6 and nb_of_edges == 12
1103     //   then the shape is tologically equivalent to a box
1104     // o In a box, there are three set of four // edges ...
1105     //   In the cascade notation, it seems to be the edges
1106     //   numbered : 
1107     //     - 1, 3, 5, 7
1108     //     - 2, 4, 6, 8
1109     //     - 9, 10, 11, 12
1110     // o For each one of this set, check if the four edges
1111     //   have the same number of element.
1112     // o If so, check if the "corresponding" // faces contains
1113     //   only quads. It's the faces numbered:
1114     //     - 1, 2, 3, 4
1115     //     - 1, 2, 5, 6
1116     //     - 3, 4, 5, 6
1117     // o If so, check if the opposite edges of each // faces
1118     //   have the same number of elements. It is the edges
1119     //   numbered :
1120     //     - 2 and 4, 6 and 8, 9 and 10, 11 and 12
1121     //     - 1 and 3, 5 and 7, 9 and 11, 10 and 12
1122     //     - 1 and 5, 3 and 7, 4 and 8, 2 and 6
1123     // o If so, check if the two other faces have the same
1124     //   number of elements. It is the faces numbered:
1125     //     - 5, 6
1126     //     - 3, 4
1127     //     - 1, 2
1128     //   This test should be improved to test if the nodes
1129     //   of the two faces are really "en face".
1130     // o If so, one of the two faces is a candidate to an extrusion,
1131     //   It is the faces numbered :
1132     //     - 5
1133     //     - 3
1134     //     - 1
1135     // o Finally, if there is only one candidate, let do the
1136     //   extrusion job for the corresponding face
1137     //
1138     int isOK = 0;
1139     //
1140     int iNbF = aM.Extent();
1141     if (iNbF == 6) {
1142       //
1143       int nb_f1 = pMesh->GetSubMeshContaining(aM(1))->GetSubMeshDS()->NbElements();
1144       int nb_f2 = pMesh->GetSubMeshContaining(aM(2))->GetSubMeshDS()->NbElements();
1145       int nb_f3 = pMesh->GetSubMeshContaining(aM(3))->GetSubMeshDS()->NbElements();
1146       int nb_f4 = pMesh->GetSubMeshContaining(aM(4))->GetSubMeshDS()->NbElements();
1147       int nb_f5 = pMesh->GetSubMeshContaining(aM(5))->GetSubMeshDS()->NbElements();
1148       int nb_f6 = pMesh->GetSubMeshContaining(aM(6))->GetSubMeshDS()->NbElements();
1149       //
1150       int has_only_quad_f1 = 1;
1151       int has_only_quad_f2 = 1;
1152       int has_only_quad_f3 = 1;
1153       int has_only_quad_f4 = 1;
1154       int has_only_quad_f5 = 1;
1155       int has_only_quad_f6 = 1;
1156       //
1157       for (i=1; i<=iNbF; ++i) {
1158         int ok = 1;
1159         const TopoDS_Shape& aF = aM(i);
1160         SMESH_subMesh *aSubMesh = pMesh->GetSubMeshContaining(aF);
1161         SMESHDS_SubMesh *aSM = aSubMesh->GetSubMeshDS();
1162         SMDS_ElemIteratorPtr itf = aSM->GetElements();
1163         while(itf->more()) {
1164           const SMDS_MeshElement * pElement = itf->next();
1165           aElementType = pElement->GetType();
1166           if (aElementType==SMDSAbs_Face) {
1167             iNbNodes = pElement->NbNodes();
1168             if ( iNbNodes!=4 ) {
1169               ok = 0;
1170               break ;
1171             }
1172           }
1173         }
1174         if (i==1) has_only_quad_f1 = ok ;
1175         if (i==2) has_only_quad_f2 = ok ;
1176         if (i==3) has_only_quad_f3 = ok ;
1177         if (i==4) has_only_quad_f4 = ok ;
1178         if (i==5) has_only_quad_f5 = ok ;
1179         if (i==6) has_only_quad_f6 = ok ;
1180       }
1181       //
1182       TopTools_IndexedMapOfShape aE;
1183       TopExp::MapShapes(myShape, TopAbs_EDGE, aE);
1184       int iNbE = aE.Extent();
1185       if (iNbE == 12) {
1186         //
1187         int nb_e01 = pMesh->GetSubMeshContaining(aE(1))->GetSubMeshDS()->NbElements();
1188         int nb_e02 = pMesh->GetSubMeshContaining(aE(2))->GetSubMeshDS()->NbElements();
1189         int nb_e03 = pMesh->GetSubMeshContaining(aE(3))->GetSubMeshDS()->NbElements();
1190         int nb_e04 = pMesh->GetSubMeshContaining(aE(4))->GetSubMeshDS()->NbElements();
1191         int nb_e05 = pMesh->GetSubMeshContaining(aE(5))->GetSubMeshDS()->NbElements();
1192         int nb_e06 = pMesh->GetSubMeshContaining(aE(6))->GetSubMeshDS()->NbElements();
1193         int nb_e07 = pMesh->GetSubMeshContaining(aE(7))->GetSubMeshDS()->NbElements();
1194         int nb_e08 = pMesh->GetSubMeshContaining(aE(8))->GetSubMeshDS()->NbElements();
1195         int nb_e09 = pMesh->GetSubMeshContaining(aE(9))->GetSubMeshDS()->NbElements();
1196         int nb_e10 = pMesh->GetSubMeshContaining(aE(10))->GetSubMeshDS()->NbElements();
1197         int nb_e11 = pMesh->GetSubMeshContaining(aE(11))->GetSubMeshDS()->NbElements();
1198         int nb_e12 = pMesh->GetSubMeshContaining(aE(12))->GetSubMeshDS()->NbElements();
1199         //
1200         int nb_ok = 0 ;
1201         //
1202         if ( (nb_e01==nb_e03) && (nb_e03==nb_e05) && (nb_e05==nb_e07) ) {
1203           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f3 && has_only_quad_f4 ) {
1204             if ( (nb_e09==nb_e10) && (nb_e08==nb_e06) && (nb_e11==nb_e12) && (nb_e04==nb_e02) ) {
1205               if (nb_f5==nb_f6) {
1206                 nb_ok += 1;
1207                 aFTr = aM(5);
1208               }
1209             }
1210           }
1211         }
1212         if ( (nb_e02==nb_e04) && (nb_e04==nb_e06) && (nb_e06==nb_e08) ) {
1213           if ( has_only_quad_f1 && has_only_quad_f2 && has_only_quad_f5 && has_only_quad_f6 ) {
1214             if ( (nb_e01==nb_e03) && (nb_e10==nb_e12) && (nb_e05==nb_e07) && (nb_e09==nb_e11) ) {
1215               if (nb_f3==nb_f4) {
1216                 nb_ok += 1;
1217                 aFTr = aM(3);
1218               }
1219             }
1220           }
1221         }
1222         if ( (nb_e09==nb_e10) && (nb_e10==nb_e11) && (nb_e11==nb_e12) ) {
1223           if ( has_only_quad_f3 && has_only_quad_f4 && has_only_quad_f5 && has_only_quad_f6 ) {
1224             if ( (nb_e01==nb_e05) && (nb_e02==nb_e06) && (nb_e03==nb_e07) && (nb_e04==nb_e08) ) {
1225               if (nb_f1==nb_f2) {
1226                 nb_ok += 1;
1227                 aFTr = aM(1);
1228               }
1229             }
1230           }
1231         }
1232         //
1233         if ( nb_ok == 1 ) {
1234           isOK = 1;
1235         }
1236         //
1237       }
1238     }
1239     if (!isOK) {
1240       myErrorStatus->myName=5; // more than one face has triangulation
1241       myErrorStatus->myComment="Incorrect input mesh";
1242       return;
1243     }
1244   }
1245   // \end{E.A.}
1246   // 
1247   // 1. Vetrices V00, V001;
1248   //
1249   TopExp::MapShapes(aFTr, TopAbs_EDGE, aME);
1250   TopExp::MapShapesAndAncestors(myShape, TopAbs_VERTEX, TopAbs_EDGE, aMVES);
1251   //
1252   // 1.1 Base vertex V000
1253   iNbE = aME.Extent();
1254   if (iNbE!= NB_WALL_FACES ){
1255     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1256     myErrorStatus->myName=7; // too few edges are in base face aFTr
1257     myErrorStatus->myComment=SMESH_Comment("Not a quadrilateral face #")
1258       <<pMesh->GetMeshDS()->ShapeToIndex( aFTr )<<": "<<iNbE<<" edges" ;
1259     return;
1260   }
1261   const TopoDS_Edge& aE1=TopoDS::Edge(aME(1));
1262   aV000=TopExp::FirstVertex(aE1);
1263   //
1264   const TopTools_ListOfShape& aLE=aMVES.FindFromKey(aV000);
1265   aIt.Initialize(aLE);
1266   for (; aIt.More(); aIt.Next()) {
1267     const TopoDS_Shape& aEx=aIt.Value();
1268     aMEV.Add(aEx);
1269   }
1270   iNbEV=aMEV.Extent();
1271   if (iNbEV!=3){
1272     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1273     myErrorStatus->myName=7; // too few edges meet in base vertex 
1274     myErrorStatus->myComment=SMESH_Comment("3 edges must share vertex #")
1275       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 )<<" but there are "<<iNbEV<<" edges";
1276     return;
1277   }
1278   //
1279   // 1.2 Vertex V001
1280   bFound=false;
1281   for (j=1; j<=iNbEV; ++j) {
1282     const TopoDS_Shape& aEx=aMEV(j);
1283     if (!aME.Contains(aEx)) {
1284       TopoDS_Vertex aV[2];
1285       //
1286       const TopoDS_Edge& aE=TopoDS::Edge(aEx);
1287       TopExp::Vertices(aE, aV[0], aV[1]);
1288       for (i=0; i<2; ++i) {
1289         if (!aV[i].IsSame(aV000)) {
1290           aV001=aV[i];
1291           bFound=!bFound;
1292           break;
1293         }
1294       }
1295     }
1296   }
1297   //
1298   if (!bFound) {
1299     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1300     myErrorStatus->myName=8; // can not find reper V001
1301     myErrorStatus->myComment=SMESH_Comment("Can't find opposite vertex for vertex #")
1302       <<pMesh->GetMeshDS()->ShapeToIndex( aV000 );
1303     return;
1304   }
1305   //DEB
1306   //gp_Pnt aP000, aP001;
1307   //
1308   //aP000=BRep_Tool::Pnt(TopoDS::Vertex(aV000));
1309   //printf("*** aP000 { %lf, %lf, %lf }\n", aP000.X(), aP000.Y(), aP000.Z());
1310   //aP001=BRep_Tool::Pnt(TopoDS::Vertex(aV001));
1311   //printf("*** aP001 { %lf, %lf, %lf }\n", aP001.X(), aP001.Y(), aP001.Z());
1312   //DEB
1313   //
1314   aME.Clear();
1315   TopExp::MapShapes(myShape, TopAbs_SHELL, aME);
1316   iNbE=aME.Extent();
1317   if (iNbE!=1) {
1318     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1319     myErrorStatus->myName=9; // number of shells in source shape !=1
1320     myErrorStatus->myComment=SMESH_Comment("Unexpected nb of shells ")<<iNbE;
1321     return;
1322   }
1323   //
1324   // 2. Load Block
1325   const TopoDS_Shell& aShell=TopoDS::Shell(aME(1));
1326   myBlock.Load(aShell, aV000, aV001);
1327   iErr = myBlock.ErrorStatus();
1328   if (iErr) {
1329     MESSAGE("StdMeshers_Penta_3D::MakeBlock() ");
1330     myErrorStatus=myBlock.GetError(); // SMESHBlock: Load operation failed
1331     return;
1332   }
1333 }
1334 //=======================================================================
1335 //function : CheckData
1336 //purpose  : 
1337 //=======================================================================
1338 void StdMeshers_Penta_3D::CheckData()
1339 {
1340   int i, iNb;
1341   int iNbEx[]={8, 12, 6};
1342   //
1343   TopAbs_ShapeEnum aST;
1344   TopAbs_ShapeEnum aSTEx[]={
1345     TopAbs_VERTEX, TopAbs_EDGE, TopAbs_FACE
1346   }; 
1347   TopTools_IndexedMapOfShape aM;
1348   //
1349   if (myShape.IsNull()){
1350     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1351     myErrorStatus->myName=2; // null shape
1352     myErrorStatus->myComment="Null shape";
1353     return;
1354   }
1355   //
1356   aST=myShape.ShapeType();
1357   if (!(aST==TopAbs_SOLID || aST==TopAbs_SHELL)) {
1358     MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1359     myErrorStatus->myName=3; // not compatible type of shape
1360     myErrorStatus->myComment=SMESH_Comment("Wrong shape type (TopAbs_ShapeEnum) ")<<aST;
1361     return;
1362   }
1363   //
1364   for (i=0; i<3; ++i) {
1365     aM.Clear();
1366     TopExp::MapShapes(myShape, aSTEx[i], aM);
1367     iNb=aM.Extent();
1368     if (iNb!=iNbEx[i]){
1369       MESSAGE("StdMeshers_Penta_3D::CheckData() ");
1370       myErrorStatus->myName=4; // number of subshape is not compatible
1371       myErrorStatus->myComment="Wrong number of subshapes of a block";
1372       return;
1373     }
1374   }
1375 }
1376
1377 //=======================================================================
1378 //function : LoadIJNodes
1379 //purpose  : Load nodes bound to theFace into column (vectors) and rows
1380 //           of theIJNodes.
1381 //           The value of theIJNodes map is a vector of ordered nodes so
1382 //           that the 0-the one lies on theBaseEdge.
1383 //           The key of theIJNodes map is a normalized parameter of each
1384 //           0-the node on theBaseEdge.
1385 //=======================================================================
1386
1387 bool StdMeshers_Penta_3D::LoadIJNodes(StdMeshers_IJNodeMap & theIJNodes,
1388                                       const TopoDS_Face&     theFace,
1389                                       const TopoDS_Edge&     theBaseEdge,
1390                                       SMESHDS_Mesh*          theMesh)
1391 {
1392   // get vertices of theBaseEdge
1393   TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
1394   TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
1395   TopExp::Vertices( eFrw, vfb, vlb );
1396
1397   // find the other edges of theFace and orientation of e1
1398   TopoDS_Edge e1, e2, eTop;
1399   bool rev1, CumOri = false;
1400   TopExp_Explorer exp( theFace, TopAbs_EDGE );
1401   int nbEdges = 0;
1402   for ( ; exp.More(); exp.Next() ) {
1403     if ( ++nbEdges > NB_WALL_FACES ) {
1404       return false; // more than 4 edges in theFace
1405     }
1406     TopoDS_Edge e = TopoDS::Edge( exp.Current() );
1407     if ( theBaseEdge.IsSame( e ))
1408       continue;
1409     TopoDS_Vertex vCommon;
1410     if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
1411       eTop = e;
1412     else if ( vCommon.IsSame( vfb )) {
1413       e1 = e;
1414       vft = TopExp::LastVertex( e1, CumOri );
1415       rev1 = vfb.IsSame( vft );
1416       if ( rev1 )
1417         vft = TopExp::FirstVertex( e1, CumOri );
1418     }
1419     else
1420       e2 = e;
1421   }
1422   if ( nbEdges < NB_WALL_FACES ) {
1423     return false; // less than 4 edges in theFace
1424   }
1425
1426   // submeshes corresponding to shapes
1427   SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
1428   SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
1429   SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
1430   SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
1431   SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
1432   SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
1433   SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
1434   SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
1435   if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
1436     MESSAGE( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
1437             sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
1438     return false;
1439   }
1440   if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
1441     MESSAGE(" Diff nb of nodes on opposite edges" );
1442     return false;
1443   }
1444   if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
1445     MESSAGE("Empty submesh of vertex");
1446     return false;
1447   }
1448   if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
1449     // check quadratic case
1450     if ( myCreateQuadratic ) {
1451       int n1 = sm1->NbNodes()/2;
1452       int n2 = smb->NbNodes()/2;
1453       int n3 = sm1->NbNodes() - n1;
1454       int n4 = smb->NbNodes() - n2;
1455       int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
1456       if( nf != smFace->NbNodes() ) {
1457         MESSAGE( "Wrong nb face nodes: " <<
1458                 sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1459         return false;
1460       }
1461     }
1462     else {
1463       MESSAGE( "Wrong nb face nodes: " <<
1464               sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
1465       return false;
1466     }
1467   }
1468   // IJ size
1469   int vsize = sm1->NbNodes() + 2;
1470   int hsize = smb->NbNodes() + 2;
1471   if(myCreateQuadratic) {
1472     vsize = vsize - sm1->NbNodes()/2 -1;
1473     hsize = hsize - smb->NbNodes()/2 -1;
1474   }
1475
1476   // load nodes from theBaseEdge
1477
1478   set<const SMDS_MeshNode*> loadedNodes;
1479   const SMDS_MeshNode* nullNode = 0;
1480
1481   vector<const SMDS_MeshNode*> & nVecf = theIJNodes[ 0.];
1482   nVecf.resize( vsize, nullNode );
1483   loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
1484
1485   vector<const SMDS_MeshNode*> & nVecl = theIJNodes[ 1.];
1486   nVecl.resize( vsize, nullNode );
1487   loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
1488
1489   double f, l;
1490   BRep_Tool::Range( eFrw, f, l );
1491   double range = l - f;
1492   SMDS_NodeIteratorPtr nIt = smb->GetNodes();
1493   const SMDS_MeshNode* node;
1494   while ( nIt->more() ) {
1495     node = nIt->next();
1496     if(myTool->IsMedium(node))
1497       continue;
1498     const SMDS_EdgePosition* pos =
1499       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1500     if ( !pos ) {
1501       return false;
1502     }
1503     double u = ( pos->GetUParameter() - f ) / range;
1504     vector<const SMDS_MeshNode*> & nVec = theIJNodes[ u ];
1505     nVec.resize( vsize, nullNode );
1506     loadedNodes.insert( nVec[ 0 ] = node );
1507   }
1508   if ( theIJNodes.size() != hsize ) {
1509     MESSAGE( "Wrong node positions on theBaseEdge" );
1510     return false;
1511   }
1512
1513   // load nodes from e1
1514
1515   map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1516   nIt = sm1->GetNodes();
1517   while ( nIt->more() ) {
1518     node = nIt->next();
1519     if(myTool->IsMedium(node))
1520       continue;
1521     const SMDS_EdgePosition* pos =
1522       dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1523     if ( !pos ) {
1524       return false;
1525     }
1526     sortedNodes.insert( make_pair( pos->GetUParameter(), node ));
1527   }
1528   loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1529   map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1530   int row = rev1 ? vsize - 1 : 0;
1531   for ( ; u_n != sortedNodes.end(); u_n++ ) {
1532     if ( rev1 ) row--;
1533     else        row++;
1534     loadedNodes.insert( nVecf[ row ] = u_n->second );
1535   }
1536
1537   // try to load the rest nodes
1538
1539   // get all faces from theFace
1540   TIDSortedElemSet allFaces, foundFaces;
1541   SMDS_ElemIteratorPtr eIt = smFace->GetElements();
1542   while ( eIt->more() ) {
1543     const SMDS_MeshElement* e = eIt->next();
1544     if ( e->GetType() == SMDSAbs_Face )
1545       allFaces.insert( e );
1546   }
1547   // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1548   // the nodes belong to, and between the nodes of the found face,
1549   // look for a not loaded node considering this node to be the next
1550   // in a column of the starting second node. Repeat, starting
1551   // from nodes next to the previous starting nodes in their columns,
1552   // and so on while a face can be found. Then go the the next pair
1553   // of nodes on theBaseEdge.
1554   StdMeshers_IJNodeMap::iterator par_nVec_1 = theIJNodes.begin();
1555   StdMeshers_IJNodeMap::iterator par_nVec_2 = par_nVec_1;
1556   // loop on columns
1557   int col = 0;
1558   for ( par_nVec_2++; par_nVec_2 != theIJNodes.end(); par_nVec_1++, par_nVec_2++ ) {
1559     col++;
1560     row = 0;
1561     const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1562     const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1563     const SMDS_MeshElement* face = 0;
1564     do {
1565       // look for a face by 2 nodes
1566       face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1567       if ( face ) {
1568         int nbFaceNodes = face->NbNodes();
1569         if ( (!myCreateQuadratic && nbFaceNodes>4) ||
1570              (myCreateQuadratic && nbFaceNodes>8) ) {
1571           MESSAGE(" Too many nodes in a face: " << nbFaceNodes );
1572           return false;
1573         }
1574         // look for a not loaded node of the <face>
1575         bool found = false;
1576         const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1577         eIt = face->nodesIterator() ;
1578         while ( !found && eIt->more() ) {
1579           node = static_cast<const SMDS_MeshNode*>( eIt->next() );
1580           if(myTool->IsMedium(node))
1581             continue;
1582           found = loadedNodes.insert( node ).second;
1583           if ( !found && node != n1 && node != n2 )
1584             n3 = node;
1585         }
1586         if ( found ) {
1587           if ( ++row > vsize - 1 ) {
1588             MESSAGE( "Too many nodes in column "<< col <<": "<< row+1);
1589             return false;
1590           }
1591           par_nVec_2->second[ row ] = node;
1592           foundFaces.insert( face );
1593           n2 = node;
1594           if ( nbFaceNodes==4 || (myCreateQuadratic && nbFaceNodes==8) ) {
1595             n1 = par_nVec_1->second[ row ];
1596           }
1597         }
1598         else if ( (nbFaceNodes==3 || (myCreateQuadratic && nbFaceNodes==6) )  &&
1599                  n3 == par_nVec_1->second[ row ] ) {
1600           n1 = n3;
1601         }
1602         else {
1603           MESSAGE( "Not quad mesh, column "<< col );
1604           return false;
1605         }
1606       }
1607     }
1608     while ( face && n1 && n2 );
1609
1610     if ( row < vsize - 1 ) {
1611       MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1612       MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1613       MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1614       MESSAGE( "Current node 1: "<< n1);
1615       MESSAGE( "Current node 2: "<< n2);
1616       MESSAGE( "first base node: "<< theIJNodes.begin()->second[0]);
1617       MESSAGE( "last base node: "<< theIJNodes.rbegin()->second[0]);
1618       return false;
1619     }
1620   } // loop on columns
1621
1622   return true;
1623 }
1624
1625 //////////////////////////////////////////////////////////////////////////
1626 //
1627 //   StdMeshers_SMESHBlock
1628 //
1629 //////////////////////////////////////////////////////////////////////////
1630
1631 //=======================================================================
1632 //function : StdMeshers_SMESHBlock
1633 //purpose  : 
1634 //=======================================================================
1635 StdMeshers_SMESHBlock::StdMeshers_SMESHBlock()
1636 {
1637   myErrorStatus=1;
1638   myIsEdgeForward.resize( SMESH_Block::NbEdges(), -1 );
1639 }
1640
1641 //=======================================================================
1642 //function : IsForwadEdge
1643 //purpose  : 
1644 //=======================================================================
1645
1646 bool StdMeshers_SMESHBlock::IsForwadEdge(const int theEdgeID)
1647 {
1648   int index = myTBlock.ShapeIndex( theEdgeID );
1649   if ( !myTBlock.IsEdgeID( theEdgeID ))
1650     return false;
1651
1652   if ( myIsEdgeForward[ index ] < 0 )
1653     myIsEdgeForward[ index ] =
1654       myTBlock.IsForwardEdge( TopoDS::Edge( Shape( theEdgeID )), myShapeIDMap );
1655
1656   return myIsEdgeForward[ index ];
1657 }
1658
1659 //=======================================================================
1660 //function : ErrorStatus
1661 //purpose  : 
1662 //=======================================================================
1663 int StdMeshers_SMESHBlock::ErrorStatus() const
1664 {
1665   return myErrorStatus;
1666 }
1667
1668 //================================================================================
1669 /*!
1670  * \brief Return problem description
1671  */
1672 //================================================================================
1673
1674 SMESH_ComputeErrorPtr StdMeshers_SMESHBlock::GetError() const
1675 {
1676   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New();
1677   string & text = err->myComment;
1678   switch ( myErrorStatus ) {
1679   case 2:
1680   case 3: text = "Internal error of StdMeshers_Penta_3D"; break; 
1681   case 4: text = "Can't compute normalized parameters of a point inside a block"; break;
1682   case 5: text = "Can't compute coordinates by normalized parameters inside a block"; break;
1683   case 6: text = "Can't detect block subshapes. Not a block?"; break;
1684   }
1685   if (!text.empty())
1686     err->myName = myErrorStatus;
1687   return err;
1688 }
1689
1690 //=======================================================================
1691 //function : Load
1692 //purpose  : 
1693 //=======================================================================
1694 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell)
1695 {
1696   TopoDS_Vertex aV000, aV001;
1697   //
1698   Load(theShell, aV000, aV001);
1699 }
1700
1701 //=======================================================================
1702 //function : Load
1703 //purpose  : 
1704 //=======================================================================
1705 void StdMeshers_SMESHBlock::Load(const TopoDS_Shell& theShell,
1706                                  const TopoDS_Vertex& theV000,
1707                                  const TopoDS_Vertex& theV001)
1708 {
1709   myErrorStatus=0;
1710   //
1711   myShell=theShell;
1712   //
1713   bool bOk;
1714   //
1715   myShapeIDMap.Clear();  
1716   bOk = myTBlock.LoadBlockShapes(myShell, theV000, theV001, myShapeIDMap);
1717   if (!bOk) {
1718     myErrorStatus=6;
1719     return;
1720   }
1721 }
1722
1723 //=======================================================================
1724 //function : ComputeParameters
1725 //purpose  : 
1726 //=======================================================================
1727 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt, 
1728                                               gp_XYZ& theXYZ)
1729 {
1730   ComputeParameters(thePnt, myShell, theXYZ);
1731 }
1732
1733 //=======================================================================
1734 //function : ComputeParameters
1735 //purpose  : 
1736 //=======================================================================
1737 void StdMeshers_SMESHBlock::ComputeParameters(const gp_Pnt& thePnt,
1738                                               const TopoDS_Shape& theShape,
1739                                               gp_XYZ& theXYZ)
1740 {
1741   myErrorStatus=0;
1742   //
1743   int aID;
1744   bool bOk;
1745   //
1746   aID = ShapeID(theShape);
1747   if (myErrorStatus) {
1748     return;
1749   }
1750   bOk = myTBlock.ComputeParameters(thePnt, theXYZ, aID);
1751   if (!bOk) {
1752     myErrorStatus=4; // problems with computation Parameters 
1753     return;
1754   }
1755 }
1756
1757 //=======================================================================
1758 //function : ComputeParameters
1759 //purpose  : 
1760 //=======================================================================
1761
1762 void StdMeshers_SMESHBlock::ComputeParameters(const double& theU,
1763                                               const TopoDS_Shape& theShape,
1764                                               gp_XYZ& theXYZ)
1765 {
1766   myErrorStatus=0;
1767   //
1768   int aID;
1769   bool bOk=false;
1770   //
1771   aID = ShapeID(theShape);
1772   if (myErrorStatus) {
1773     return;
1774   }
1775   if ( SMESH_Block::IsEdgeID( aID ))
1776       bOk = myTBlock.EdgeParameters( aID, theU, theXYZ );
1777   if (!bOk) {
1778     myErrorStatus=4; // problems with computation Parameters 
1779     return;
1780   }
1781 }
1782
1783 //=======================================================================
1784 //function : Point
1785 //purpose  : 
1786 //=======================================================================
1787  void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1788                                    gp_Pnt& aP3D)
1789 {
1790   TopoDS_Shape aS;
1791   //
1792   Point(theParams, aS, aP3D);
1793 }
1794
1795 //=======================================================================
1796 //function : Point
1797 //purpose  : 
1798 //=======================================================================
1799  void StdMeshers_SMESHBlock::Point(const gp_XYZ& theParams,
1800                                    const TopoDS_Shape& theShape,
1801                                    gp_Pnt& aP3D)
1802 {
1803   myErrorStatus = 0;
1804   //
1805   int aID;
1806   bool bOk = false;
1807   gp_XYZ aXYZ(99.,99.,99.);
1808   aP3D.SetXYZ(aXYZ);
1809   //
1810   if (theShape.IsNull()) {
1811     bOk = myTBlock.ShellPoint(theParams, aXYZ);
1812   }
1813   //
1814   else {
1815     aID=ShapeID(theShape);
1816     if (myErrorStatus) {
1817       return;
1818     }
1819     //
1820     if (SMESH_Block::IsVertexID(aID)) {
1821       bOk = myTBlock.VertexPoint(aID, aXYZ);
1822     }
1823     else if (SMESH_Block::IsEdgeID(aID)) {
1824       bOk = myTBlock.EdgePoint(aID, theParams, aXYZ);
1825     }
1826     //
1827     else if (SMESH_Block::IsFaceID(aID)) {
1828       bOk = myTBlock.FacePoint(aID, theParams, aXYZ);
1829     }
1830   }
1831   if (!bOk) {
1832     myErrorStatus=5; // problems with point computation 
1833     return;
1834   }
1835   aP3D.SetXYZ(aXYZ);
1836 }
1837
1838 //=======================================================================
1839 //function : ShapeID
1840 //purpose  : 
1841 //=======================================================================
1842 int StdMeshers_SMESHBlock::ShapeID(const TopoDS_Shape& theShape)
1843 {
1844   myErrorStatus=0;
1845   //
1846   int aID=-1;
1847   TopoDS_Shape aSF, aSR;
1848   //
1849   aSF=theShape;
1850   aSF.Orientation(TopAbs_FORWARD);
1851   aSR=theShape;
1852   aSR.Orientation(TopAbs_REVERSED);
1853   //
1854   if (myShapeIDMap.Contains(aSF)) {
1855     aID=myShapeIDMap.FindIndex(aSF);
1856     return aID;
1857   }
1858   if (myShapeIDMap.Contains(aSR)) {
1859     aID=myShapeIDMap.FindIndex(aSR);
1860     return aID;
1861   }
1862   myErrorStatus=2; // unknown shape;
1863   return aID;
1864 }
1865
1866 //=======================================================================
1867 //function : Shape
1868 //purpose  : 
1869 //=======================================================================
1870 const TopoDS_Shape& StdMeshers_SMESHBlock::Shape(const int theID)
1871 {
1872   myErrorStatus=0;
1873   //
1874   int aNb;
1875   //
1876   aNb=myShapeIDMap.Extent();
1877   if (theID<1 || theID>aNb) {
1878     myErrorStatus=3; // ID is out of range
1879     return myEmptyShape;
1880   }
1881   //
1882   const TopoDS_Shape& aS=myShapeIDMap.FindKey(theID);
1883   return aS;
1884 }
1885
1886
1887 //=======================================================================
1888 //function : Evaluate
1889 //purpose  : 
1890 //=======================================================================
1891 bool StdMeshers_Penta_3D::Evaluate(SMESH_Mesh& aMesh, 
1892                                    const TopoDS_Shape& aShape,
1893                                    MapShapeNbElems& aResMap)
1894 {
1895   MESSAGE("StdMeshers_Penta_3D::Evaluate()");
1896
1897   // find face contains only triangles
1898   vector < SMESH_subMesh * >meshFaces;
1899   TopTools_SequenceOfShape aFaces;
1900   int NumBase = 0, i = 0;
1901   for (TopExp_Explorer exp(aShape, TopAbs_FACE); exp.More(); exp.Next()) {
1902     i++;
1903     aFaces.Append(exp.Current());
1904     SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
1905     meshFaces.push_back(aSubMesh);
1906     MapShapeNbElemsItr anIt = aResMap.find(meshFaces[i]);
1907     std::vector<int> aVec = (*anIt).second;
1908     int nbtri = Max(aVec[3],aVec[4]);
1909     int nbqua = Max(aVec[5],aVec[6]);
1910     if( nbtri>0 && nbqua==0 ) {
1911       NumBase = i;
1912     }
1913   }
1914
1915   if(NumBase==0) return false;
1916
1917   // find number of 1d elems for base face
1918   int nb1d = 0;
1919   TopTools_MapOfShape Edges1;
1920   for (TopExp_Explorer exp(aFaces.Value(NumBase), TopAbs_EDGE); exp.More(); exp.Next()) {
1921     Edges1.Add(exp.Current());
1922     SMESH_subMesh *sm = aMesh.GetSubMesh(exp.Current());
1923     if( sm ) {
1924       MapShapeNbElemsItr anIt = aResMap.find(sm);
1925       if( anIt == aResMap.end() ) continue;
1926       std::vector<int> aVec = (*anIt).second;
1927       nb1d += Max(aVec[1],aVec[2]);
1928     }
1929   }
1930   // find face opposite to base face
1931   int OppNum = 0;
1932   for(i=1; i<=6; i++) {
1933     if(i==NumBase) continue;
1934     bool IsOpposite = true;
1935     for(TopExp_Explorer exp(aFaces.Value(i), TopAbs_EDGE); exp.More(); exp.Next()) {
1936       if( Edges1.Contains(exp.Current()) ) {
1937         IsOpposite = false;
1938         break;
1939       }
1940     }
1941     if(IsOpposite) {
1942       OppNum = i;
1943       break;
1944     }
1945   }
1946   // find number of 2d elems on side faces
1947   int nb2d = 0;
1948   for(i=1; i<=6; i++) {
1949     if( i==OppNum || i==NumBase ) continue;
1950     MapShapeNbElemsItr anIt = aResMap.find( meshFaces[i-1] );
1951     if( anIt == aResMap.end() ) continue;
1952     std::vector<int> aVec = (*anIt).second;
1953     nb2d += Max(aVec[5],aVec[6]);
1954   }
1955   
1956   MapShapeNbElemsItr anIt = aResMap.find( meshFaces[NumBase-1] );
1957   std::vector<int> aVec = (*anIt).second;
1958   int nb2d_face0 = Max(aVec[5],aVec[6]);
1959   int nb0d_face0 = aVec[0];
1960
1961   anIt = aResMap.find( meshFaces[OppNum-1] );
1962   for(i=0; i<17; i++)
1963     (*anIt).second[i] = aVec[i];
1964
1965   SMESH_MesherHelper aTool (aMesh);
1966   bool _quadraticMesh = aTool.IsQuadraticSubMesh(aShape);
1967
1968   std::vector<int> aResVec(17);
1969   for(int i=0; i<17; i++) aResVec[i] = 0;
1970   if(_quadraticMesh) {
1971     aResVec[13] = nb2d_face0 * ( nb2d/nb1d );
1972     aResVec[0] = nb0d_face0 * ( 2*nb2d/nb1d - 1 );
1973   }
1974   else {
1975     aResVec[0] = nb0d_face0 * ( nb2d/nb1d - 1 );
1976     aResVec[12] = nb2d_face0 * ( nb2d/nb1d );
1977   }
1978   SMESH_subMesh * sm = aMesh.GetSubMesh(aShape);
1979   aResMap.insert(std::make_pair(sm,aResVec));
1980
1981   return true;
1982 }
1983