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