Salome HOME
PAL13460 (PAL EDF 301 force the mesh to go through a point)
[modules/smesh.git] / src / StdMeshers / StdMeshers_Prism_3D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 // File      : StdMeshers_Prism_3D.cxx
25 // Module    : SMESH
26 // Created   : Fri Oct 20 11:37:07 2006
27 // Author    : Edward AGAPOV (eap)
28
29
30 #include "StdMeshers_Prism_3D.hxx"
31
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "SMESH_MeshEditor.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMDS_VolumeOfNodes.hxx"
36 #include "SMDS_EdgePosition.hxx"
37
38 #include "utilities.h"
39
40 #include <BRepAdaptor_Curve.hxx>
41 #include <BRepAdaptor_Curve2d.hxx>
42 #include <BRep_Builder.hxx>
43 #include <BRep_Tool.hxx>
44 #include <Geom2dAdaptor_Curve.hxx>
45 #include <Geom2d_Line.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47
48 using namespace std;
49
50 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
51 #define gpXYZ(n) gp_XYZ(n->X(),n->Y(),n->Z())
52 #define SHOWYXZ(msg, xyz) // {\
53 // gp_Pnt p (xyz); \
54 // cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl;\
55 // }
56
57 typedef StdMeshers_ProjectionUtils TAssocTool;
58
59 enum { ID_BOT_FACE = SMESH_Block::ID_Fxy0,
60        ID_TOP_FACE = SMESH_Block::ID_Fxy1,
61        BOTTOM_EDGE = 0, TOP_EDGE, V0_EDGE, V1_EDGE, // edge IDs in face
62        NB_WALL_FACES = 4 }; //
63
64 namespace {
65
66   //================================================================================
67   /*!
68    * \brief Return iterator pointing to node column for the given parameter
69    * \param columnsMap - node column map
70    * \param parameter - parameter
71    * \retval TParam2ColumnMap::iterator - result
72    *
73    * it returns closest left column
74    */
75   //================================================================================
76
77   TParam2ColumnIt getColumn( const TParam2ColumnMap* columnsMap,
78                              const double            parameter )
79   {
80     TParam2ColumnIt u_col = columnsMap->upper_bound( parameter );
81     if ( u_col != columnsMap->begin() )
82       --u_col;
83     return u_col; // return left column
84   }
85
86   //================================================================================
87   /*!
88    * \brief Return nodes around given parameter and a ratio
89    * \param column - node column
90    * \param param - parameter
91    * \param node1 - lower node
92    * \param node2 - upper node
93    * \retval double - ratio
94    */
95   //================================================================================
96
97   double getRAndNodes( const TNodeColumn*     column,
98                        const double           param,
99                        const SMDS_MeshNode* & node1,
100                        const SMDS_MeshNode* & node2)
101   {
102     if ( param == 1.0 || column->size() == 1) {
103       node1 = node2 = column->back();
104       return 0;
105     }
106
107     int i = int( param * ( column->size() - 1 ));
108     double u0 = double( i )/ double( column->size() - 1 );
109     double r = ( param - u0 ) * ( column->size() - 1 );
110
111     node1 = (*column)[ i ];
112     node2 = (*column)[ i + 1];
113     return r;
114   }
115
116   //================================================================================
117   /*!
118    * \brief Compute boundary parameters of face parts
119     * \param nbParts - nb of parts to split columns into
120     * \param columnsMap - node columns of the face to split
121     * \param params - computed parameters
122    */
123   //================================================================================
124
125   void splitParams( const int               nbParts,
126                     const TParam2ColumnMap* columnsMap,
127                     vector< double > &      params)
128   {
129     params.clear();
130     params.reserve( nbParts + 1 );
131     TParam2ColumnIt last_par_col = --columnsMap->end();
132     double par = columnsMap->begin()->first; // 0.
133     double parLast = last_par_col->first;
134     params.push_back( par );
135     for ( int i = 0; i < nbParts - 1; ++ i )
136     {
137       double partSize = ( parLast - par ) / double ( nbParts - i );
138       TParam2ColumnIt par_col = getColumn( columnsMap, par + partSize );
139       if ( par_col->first == par ) {
140         ++par_col;
141         if ( par_col == last_par_col ) {
142           while ( i < nbParts - 1 )
143             params.push_back( par + partSize * i++ );
144           break;
145         }
146       }
147       par = par_col->first;
148       params.push_back( par );
149     }
150     params.push_back( parLast ); // 1.
151   }
152 }
153
154 //=======================================================================
155 //function : StdMeshers_Prism_3D
156 //purpose  : 
157 //=======================================================================
158
159 StdMeshers_Prism_3D::StdMeshers_Prism_3D(int hypId, int studyId, SMESH_Gen* gen)
160   :SMESH_3D_Algo(hypId, studyId, gen)
161 {
162   _name = "Prism_3D";
163   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit per shape type
164   myProjectTriangles = false;
165 }
166
167 //================================================================================
168 /*!
169  * \brief Destructor
170  */
171 //================================================================================
172
173 StdMeshers_Prism_3D::~StdMeshers_Prism_3D()
174 {}
175
176 //=======================================================================
177 //function : CheckHypothesis
178 //purpose  : 
179 //=======================================================================
180
181 bool StdMeshers_Prism_3D::CheckHypothesis(SMESH_Mesh&                          aMesh,
182                                           const TopoDS_Shape&                  aShape,
183                                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
184 {
185   // Check shape geometry
186
187   aStatus = SMESH_Hypothesis::HYP_BAD_GEOMETRY;
188
189   // find not quadrangle faces
190   list< TopoDS_Shape > notQuadFaces;
191   int nbEdge, nbWire, nbFace = 0;
192   TopExp_Explorer exp( aShape, TopAbs_FACE );
193   for ( ; exp.More(); exp.Next() ) {
194     ++nbFace;
195     const TopoDS_Shape& face = exp.Current();
196     nbEdge = TAssocTool::Count( face, TopAbs_EDGE, 0 );
197     nbWire = TAssocTool::Count( face, TopAbs_WIRE, 0 );
198     if (  nbEdge!= 4 || nbWire!= 1 ) {
199       if ( !notQuadFaces.empty() ) {
200         if ( TAssocTool::Count( notQuadFaces.back(), TopAbs_EDGE, 0 ) != nbEdge ||
201              TAssocTool::Count( notQuadFaces.back(), TopAbs_WIRE, 0 ) != nbWire )
202           RETURN_BAD_RESULT("Different not quad faces");
203       }
204       notQuadFaces.push_back( face );
205     }
206   }
207   if ( !notQuadFaces.empty() )
208   {
209     if ( notQuadFaces.size() != 2 )
210       RETURN_BAD_RESULT("Bad nb not quad faces: " << notQuadFaces.size());
211
212     // check total nb faces
213     nbEdge = TAssocTool::Count( notQuadFaces.back(), TopAbs_EDGE, 0 );
214     if ( nbFace != nbEdge + 2 )
215       RETURN_BAD_RESULT("Bad nb of faces: " << nbFace << " but must be " << nbEdge + 2);
216   }
217
218   // no hypothesis
219   aStatus = SMESH_Hypothesis::HYP_OK;
220   return true;
221 }
222
223 //=======================================================================
224 //function : Compute
225 //purpose  : 
226 //=======================================================================
227
228 bool StdMeshers_Prism_3D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
229 {
230   myHelper = new SMESH_MesherHelper( theMesh );
231   // to delete helper at exit from Compute()
232   std::auto_ptr<SMESH_MesherHelper> helperDeleter( myHelper );
233
234   myHelper->IsQuadraticSubMesh( theShape );
235
236   // Analyse mesh and geomerty to find block subshapes and submeshes
237   if ( !myBlock.Init( myHelper, theShape ))
238     return false;
239
240   SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
241
242   int volumeID = meshDS->ShapeToIndex( theShape );
243
244
245   // To compute coordinates of a node inside a block, it is necessary to know
246   // 1. normalized parameters of the node by which
247   // 2. coordinates of node projections on all block sub-shapes are computed
248
249   // So we fill projections on vertices at once as they are same for all nodes
250   myShapeXYZ.resize( myBlock.NbSubShapes() );
251   for ( int iV = SMESH_Block::ID_FirstV; iV < SMESH_Block::ID_FirstE; ++iV ) {
252     myBlock.VertexPoint( iV, myShapeXYZ[ iV ]);
253     SHOWYXZ("V point " <<iV << " ", myShapeXYZ[ iV ]);
254   }
255
256   // Projections on the top and bottom faces are taken from nodes existing
257   // on these faces; find correspondence between bottom and top nodes
258   myBotToColumnMap.clear();
259   if ( !assocOrProjBottom2Top() ) // it also fill myBotToColumnMap
260     return false;
261
262
263   // Create nodes inside the block
264
265   // loop on nodes inside the bottom face
266   TNode2ColumnMap::iterator bot_column = myBotToColumnMap.begin();
267   for ( ; bot_column != myBotToColumnMap.end(); ++bot_column )
268   {
269     const TNode& tBotNode = bot_column->first; // bottom TNode
270     if ( tBotNode.GetPositionType() != SMDS_TOP_FACE )
271       continue; // node is not inside face 
272
273     // column nodes; middle part of the column are zero pointers
274     TNodeColumn& column = bot_column->second;
275
276     // bottom node parameters and coords
277     gp_XYZ botParams          = tBotNode.GetParams();
278     myShapeXYZ[ ID_BOT_FACE ] = tBotNode.GetCoords();
279
280     // compute top node parameters
281     gp_XYZ topParams;
282     myShapeXYZ[ ID_TOP_FACE ] = gpXYZ( column.back() );
283     gp_Pnt topCoords = myShapeXYZ[ ID_TOP_FACE ];
284     if ( !myBlock.ComputeParameters( topCoords, topParams, ID_TOP_FACE ))
285       RETURN_BAD_RESULT("ComputeParameters() on the top face failed");
286
287     // vertical loop
288     TNodeColumn::iterator columnNodes = column.begin();
289     for ( int z = 0; columnNodes != column.end(); ++columnNodes, ++z)
290     {
291       const SMDS_MeshNode* & node = *columnNodes;
292       if ( node ) continue; // skip bottom or top node
293
294       // params of a node to create
295       double rz = (double) z / (double) ( column.size() - 1 );
296       gp_XYZ params = botParams * ( 1 - rz ) + topParams * rz;
297
298       // set coords on all faces and nodes
299       const int nbSideFaces = 4;
300       int sideFaceIDs[nbSideFaces] = { SMESH_Block::ID_Fx0z,
301                                        SMESH_Block::ID_Fx1z,
302                                        SMESH_Block::ID_F0yz,
303                                        SMESH_Block::ID_F1yz };
304       for ( int iF = 0; iF < nbSideFaces; ++iF )
305         if ( !setFaceAndEdgesXYZ( sideFaceIDs[ iF ], params, z ))
306           return false;
307
308       // compute coords for a new node
309       gp_XYZ coords;
310       if ( !SMESH_Block::ShellPoint( params, myShapeXYZ, coords ))
311         RETURN_BAD_RESULT("SMESH_Block::ShellPoint() failed");
312
313       // create a node
314       node = meshDS->AddNode( coords.X(), coords.Y(), coords.Z() );
315       meshDS->SetNodeInVolume( node, volumeID );
316     }
317   } // loop on bottom nodes
318
319
320   // Create volumes
321
322   SMESHDS_SubMesh* smDS = myBlock.SubMeshDS( ID_BOT_FACE );
323   if ( !smDS ) RETURN_BAD_RESULT("Null submesh");
324
325   // loop on bottom mesh faces
326   SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
327   while ( faceIt->more() )
328   {
329     const SMDS_MeshElement* face = faceIt->next();
330     if ( !face || face->GetType() != SMDSAbs_Face )
331       continue;
332     int nbNodes = face->NbNodes();
333     if ( face->IsQuadratic() )
334       nbNodes /= 2;
335
336     // find node columns for each node
337     vector< const TNodeColumn* > columns( nbNodes );
338     for ( int i = 0; i < nbNodes; ++i )
339     {
340       const SMDS_MeshNode* n = face->GetNode( i );
341       if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
342         bot_column = myBotToColumnMap.find( n );
343         if ( bot_column == myBotToColumnMap.end() )
344           RETURN_BAD_RESULT(" node column for a node not found");
345         columns[ i ] = & bot_column->second;
346       }
347       else {
348         columns[ i ] = myBlock.GetNodeColumn( n );
349         if ( !columns[ i ] )
350           RETURN_BAD_RESULT(" node column not found for a node " << n->GetID() );
351       }
352     }
353     // create prisms
354     AddPrisms( columns, myHelper );
355
356   } // loop on bottom mesh faces
357         
358   return true;
359 }
360
361 //================================================================================
362 /*!
363  * \brief Create prisms
364  * \param columns - columns of nodes generated from nodes of a mesh face
365  * \param helper - helper initialized by mesh and shape to add prisms to
366  */
367 //================================================================================
368
369 void StdMeshers_Prism_3D::AddPrisms( vector<const TNodeColumn*> & columns,
370                                      SMESH_MesherHelper*          helper)
371 {
372   SMESHDS_Mesh * meshDS = helper->GetMeshDS();
373   int shapeID = helper->GetSubShapeID();
374
375   int nbNodes = columns.size();
376
377   // vertical loop on columns
378   for ( int z = 1; z < columns[0]->size(); ++z)
379   {
380     SMDS_MeshElement* vol = 0;
381     SMDS_VolumeTool vTool;
382     switch ( nbNodes ) {
383
384     case 3: {
385       const SMDS_MeshNode* botNodes[3] = { (*columns[0])[z-1],
386                                            (*columns[1])[z-1],
387                                            (*columns[2])[z-1] };
388       const SMDS_MeshNode* topNodes[3] = { (*columns[0])[z],
389                                            (*columns[1])[z],
390                                            (*columns[2])[z] };
391       // assure good orientation
392       SMDS_VolumeOfNodes tmpVol ( botNodes[0], botNodes[1], botNodes[2],
393                                   topNodes[0], topNodes[1], topNodes[2]);
394       vTool.Set( &tmpVol );
395       if ( vTool.IsForward() )
396         vol = helper->AddVolume( botNodes[0], botNodes[1], botNodes[2],
397                                  topNodes[0], topNodes[1], topNodes[2]);
398       else
399         vol = helper->AddVolume( topNodes[0], topNodes[1], topNodes[2],
400                                  botNodes[0], botNodes[1], botNodes[2]);
401       break;
402       }
403     case 4: {
404       const SMDS_MeshNode* botNodes[4] = { (*columns[0])[z-1], (*columns[1])[z-1],
405                                            (*columns[2])[z-1], (*columns[3])[z-1] };
406       const SMDS_MeshNode* topNodes[4] = { (*columns[0])[z], (*columns[1])[z],
407                                            (*columns[2])[z], (*columns[3])[z] };
408       // assure good orientation
409       SMDS_VolumeOfNodes tmpVol ( botNodes[0], botNodes[1], botNodes[2], botNodes[3],
410                                   topNodes[0], topNodes[1], topNodes[2], topNodes[3]);
411       vTool.Set( &tmpVol );
412       if ( vTool.IsForward() )
413         vol = helper->AddVolume( botNodes[0], botNodes[1], botNodes[2], botNodes[3],
414                                  topNodes[0], topNodes[1], topNodes[2], topNodes[3]);
415       else
416         vol = helper->AddVolume( topNodes[0], topNodes[1], topNodes[2], topNodes[3],
417                                  botNodes[0], botNodes[1], botNodes[2], botNodes[3]);
418       break;
419       }
420     default:
421       // polyhedron
422       vector<const SMDS_MeshNode*> nodes( 2*nbNodes + 4*nbNodes);
423       vector<int> quantities( 2 + nbNodes, 4 );
424       quantities[0] = quantities[1] = nbNodes;
425       columns.resize( nbNodes + 1 );
426       columns[ nbNodes ] = columns[ 0 ];
427       for ( int i = 0; i < nbNodes; ++i ) {
428         nodes[ i         ] = (*columns[ i ])[z-1]; // bottom
429         nodes[ i+nbNodes ] = (*columns[ i ])[z  ]; // top
430         // side
431         int di = 2*nbNodes + 4*i - 1;
432         nodes[ di   ] = (*columns[i  ])[z-1];
433         nodes[ di+1 ] = (*columns[i+1])[z-1];
434         nodes[ di+2 ] = (*columns[i+1])[z  ];
435         nodes[ di+3 ] = (*columns[i  ])[z  ];
436       }
437       vol = meshDS->AddPolyhedralVolume( nodes, quantities );
438     }
439     if ( vol && shapeID > 0 )
440       meshDS->SetMeshElementOnShape( vol, shapeID );
441   }
442 }
443
444 //================================================================================
445 /*!
446  * \brief Find correspondence between bottom and top nodes
447  *  If elements on the bottom and top faces are topologically different,
448  *  and projection is possible and allowed, perform the projection
449  *  \retval bool - is a success or not
450  */
451 //================================================================================
452
453 bool StdMeshers_Prism_3D::assocOrProjBottom2Top()
454 {
455   SMESH_subMesh * botSM = myBlock.SubMesh( ID_BOT_FACE );
456   SMESH_subMesh * topSM = myBlock.SubMesh( ID_TOP_FACE );
457
458   SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
459   SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
460
461   if ( !botSMDS || botSMDS->NbElements() == 0 )
462     RETURN_BAD_RESULT("Empty horiz submesh");
463
464   bool needProject = false;
465   if ( !topSMDS || 
466        botSMDS->NbElements() != topSMDS->NbElements() ||
467        botSMDS->NbNodes()    != topSMDS->NbNodes())
468   {
469     if ( myBlock.HasNotQuadElemOnTop() )
470       RETURN_BAD_RESULT("Different triangles on 2 sides");
471     needProject = true;
472   }
473
474   if ( 0/*needProject && !myProjectTriangles*/ )
475     RETURN_BAD_RESULT("Need to project but not allowed");
476
477   if ( needProject )
478   {
479     return projectBottomToTop();
480   }
481
482   TopoDS_Face botFace = TopoDS::Face( myBlock.Shape( ID_BOT_FACE ));
483   TopoDS_Face topFace = TopoDS::Face( myBlock.Shape( ID_TOP_FACE ));
484   // associate top and bottom faces
485   TAssocTool::TShapeShapeMap shape2ShapeMap;
486   if ( !TAssocTool::FindSubShapeAssociation( botFace, myBlock.Mesh(),
487                                              topFace, myBlock.Mesh(),
488                                              shape2ShapeMap) )
489     RETURN_BAD_RESULT("FindSubShapeAssociation failed");
490
491   // Find matching nodes of top and bottom faces
492   TNodeNodeMap n2nMap;
493   if ( ! TAssocTool::FindMatchingNodesOnFaces( botFace, myBlock.Mesh(),
494                                                topFace, myBlock.Mesh(),
495                                                shape2ShapeMap, n2nMap ))
496     RETURN_BAD_RESULT("Different mesh on top and bottom faces");
497
498   // Fill myBotToColumnMap
499
500   int zSize = myBlock.VerticalSize();
501   TNodeNodeMap::iterator bN_tN = n2nMap.begin();
502   for ( ; bN_tN != n2nMap.end(); ++bN_tN )
503   {
504     const SMDS_MeshNode* botNode = bN_tN->first;
505     const SMDS_MeshNode* topNode = bN_tN->second;
506     if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
507       continue; // wall columns are contained in myBlock
508     // compute bottom node params
509     TNode bN( botNode );
510     if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(), ID_BOT_FACE ))
511       RETURN_BAD_RESULT("ComputeParameters() on the bottom face failed");
512     // create node column
513     TNode2ColumnMap::iterator bN_col = 
514       myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
515     TNodeColumn & column = bN_col->second;
516     column.resize( zSize );
517     column.front() = botNode;
518     column.back()  = topNode;
519   }
520   return true;
521 }
522
523 //================================================================================
524 /*!
525  * \brief Remove quadrangles from the top face and
526  * create triangles there by projection from the bottom
527  * \retval bool - a success or not
528  */
529 //================================================================================
530
531 bool StdMeshers_Prism_3D::projectBottomToTop()
532 {
533   SMESH_subMesh * botSM = myBlock.SubMesh( ID_BOT_FACE );
534   SMESH_subMesh * topSM = myBlock.SubMesh( ID_TOP_FACE );
535
536   SMESHDS_SubMesh * botSMDS = botSM->GetSubMeshDS();
537   SMESHDS_SubMesh * topSMDS = topSM->GetSubMeshDS();
538
539   if ( topSMDS )
540     topSM->ComputeStateEngine( SMESH_subMesh::CLEAN );
541
542   SMESHDS_Mesh* meshDS = myBlock.MeshDS();
543   int shapeID = myHelper->GetSubShapeID();
544   int topFaceID = meshDS->ShapeToIndex( topSM->GetSubShape() );
545
546   // Fill myBotToColumnMap
547
548   int zSize = myBlock.VerticalSize();
549   SMDS_NodeIteratorPtr nIt = botSMDS->GetNodes();
550   while ( nIt->more() )
551   {
552     const SMDS_MeshNode* botNode = nIt->next();
553     if ( botNode->GetPosition()->GetTypeOfPosition() != SMDS_TOP_FACE )
554       continue; // strange
555     // compute bottom node params
556     TNode bN( botNode );
557     if ( !myBlock.ComputeParameters( bN.GetCoords(), bN.ChangeParams(), ID_BOT_FACE ))
558       RETURN_BAD_RESULT("ComputeParameters() on the bottom face failed");
559     // compute top node coords
560     gp_XYZ topXYZ; gp_XY topUV;
561     if ( !myBlock.FacePoint( ID_TOP_FACE, bN.GetParams(), topXYZ ) ||
562          !myBlock.FaceUV   ( ID_TOP_FACE, bN.GetParams(), topUV ))
563       RETURN_BAD_RESULT("SMESH_Block::FacePoint() on the top face failed");
564     SMDS_MeshNode * topNode = meshDS->AddNode( topXYZ.X(),topXYZ.Y(),topXYZ.Z() );
565     meshDS->SetNodeOnFace( topNode, topFaceID, topUV.X(), topUV.Y() );
566     // create node column
567     TNode2ColumnMap::iterator bN_col = 
568       myBotToColumnMap.insert( make_pair ( bN, TNodeColumn() )).first;
569     TNodeColumn & column = bN_col->second;
570     column.resize( zSize );
571     column.front() = botNode;
572     column.back()  = topNode;
573   }
574
575   // Create top faces
576
577   // loop on bottom mesh faces
578   SMDS_ElemIteratorPtr faceIt = botSMDS->GetElements();
579   while ( faceIt->more() )
580   {
581     const SMDS_MeshElement* face = faceIt->next();
582     if ( !face || face->GetType() != SMDSAbs_Face )
583       continue;
584     int nbNodes = face->NbNodes();
585     if ( face->IsQuadratic() )
586       nbNodes /= 2;
587
588     // find top node in columns for each bottom node
589     vector< const SMDS_MeshNode* > nodes( nbNodes );
590     for ( int i = 0; i < nbNodes; ++i )
591     {
592       const SMDS_MeshNode* n = face->GetNode( nbNodes - i - 1 );
593       if ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
594         TNode2ColumnMap::iterator bot_column = myBotToColumnMap.find( n );
595         if ( bot_column == myBotToColumnMap.end() )
596           RETURN_BAD_RESULT(" node column for a node not found");
597         nodes[ i ] = bot_column->second.back();
598       }
599       else {
600         const TNodeColumn* column = myBlock.GetNodeColumn( n );
601         if ( !column )
602           RETURN_BAD_RESULT(" node column not found for a node " << n->GetID() );
603         nodes[ i ] = column->back();
604       }
605     }
606     // create a face, with reversed orientation
607     SMDS_MeshElement* newFace = 0;
608     switch ( nbNodes ) {
609
610     case 3: {
611       newFace = myHelper->AddFace(nodes[0], nodes[1], nodes[2]);
612       break;
613       }
614     case 4: {
615       newFace = myHelper->AddFace( nodes[0], nodes[1], nodes[2], nodes[3] );
616       break;
617       }
618     default:
619       newFace = meshDS->AddPolygonalFace( nodes );
620     }
621     if ( newFace && shapeID > 0 )
622       meshDS->SetMeshElementOnShape( newFace, shapeID );
623   }
624
625   return true;
626 }
627
628 //================================================================================
629 /*!
630  * \brief Set projection coordinates of a node to a face and it's subshapes
631  * \param faceID - the face given by in-block ID
632  * \param params - node normalized parameters
633  * \retval bool - is a success
634  */
635 //================================================================================
636
637 bool StdMeshers_Prism_3D::setFaceAndEdgesXYZ( const int faceID, const gp_XYZ& params, int z )
638 {
639   // find base and top edges of the face
640   enum { BASE = 0, TOP, LEFT, RIGHT };
641   vector< int > edgeVec; // 0-base, 1-top
642   SMESH_Block::GetFaceEdgesIDs( faceID, edgeVec );
643
644   myBlock.EdgePoint( edgeVec[ BASE ], params, myShapeXYZ[ edgeVec[ BASE ]]);
645   myBlock.EdgePoint( edgeVec[ TOP ], params, myShapeXYZ[ edgeVec[ TOP ]]);
646
647   SHOWYXZ("\nparams ", params);
648   SHOWYXZ("TOP is "<<edgeVec[ TOP], myShapeXYZ[ edgeVec[ TOP]]);
649   SHOWYXZ("BASE is "<<edgeVec[ BASE], myShapeXYZ[ edgeVec[ BASE]]);
650
651   if ( faceID == SMESH_Block::ID_Fx0z || faceID == SMESH_Block::ID_Fx1z )
652   {
653     myBlock.EdgePoint( edgeVec[ LEFT ], params, myShapeXYZ[ edgeVec[ LEFT ]]);
654     myBlock.EdgePoint( edgeVec[ RIGHT ], params, myShapeXYZ[ edgeVec[ RIGHT ]]);
655
656     SHOWYXZ("VER "<<edgeVec[ LEFT], myShapeXYZ[ edgeVec[ LEFT]]);
657     SHOWYXZ("VER "<<edgeVec[ RIGHT], myShapeXYZ[ edgeVec[ RIGHT]]);
658   }
659   myBlock.FacePoint( faceID, params, myShapeXYZ[ faceID ]);
660   SHOWYXZ("FacePoint "<<faceID, myShapeXYZ[ faceID]);
661
662   return true;
663 }
664
665 //================================================================================
666 /*!
667  * \brief Constructor. Initialization is needed
668  */
669 //================================================================================
670
671 StdMeshers_PrismAsBlock::StdMeshers_PrismAsBlock()
672 {
673   mySide = 0;
674 }
675
676 StdMeshers_PrismAsBlock::~StdMeshers_PrismAsBlock()
677 {
678   if ( mySide ) {
679     delete mySide; mySide = 0;
680   }
681 }
682
683 //================================================================================
684 /*!
685  * \brief Initialization.
686  * \param helper - helper loaded with mesh and 3D shape
687  * \param shape3D - a closed shell or solid
688  * \retval bool - false if a mesh or a shape are KO
689  */
690 //================================================================================
691
692 bool StdMeshers_PrismAsBlock::Init(SMESH_MesherHelper* helper,
693                                    const TopoDS_Shape& shape3D)
694 {
695   if ( mySide ) {
696     delete mySide; mySide = 0;
697   }
698   vector< TSideFace* > sideFaces( NB_WALL_FACES, 0 );
699   vector< pair< double, double> > params ( NB_WALL_FACES );
700   mySide = new TSideFace( sideFaces, params );
701
702   myHelper = helper;
703   SMESHDS_Mesh* meshDS = myHelper->GetMeshDS();
704
705   SMESH_Block::init();
706   myShapeIDMap.Clear();
707   myShapeIndex2ColumnMap.clear();
708   
709   int wallFaceIds[ NB_WALL_FACES ] = { // to walk around a block
710     SMESH_Block::ID_Fx0z, SMESH_Block::ID_F1yz,
711     SMESH_Block::ID_Fx1z, SMESH_Block::ID_F0yz
712   };
713
714   // -------------------------------------------------------------
715   // Look for top and bottom faces: not quadrangle ones or meshed
716   // with not quadrangle elements
717   // -------------------------------------------------------------
718
719   list< SMESH_subMesh* > notQuadGeomSubMesh;
720   list< SMESH_subMesh* > notQuadElemSubMesh;
721   int nbFaces = 0;
722   //
723   SMESH_subMesh* mainSubMesh = myHelper->GetMesh()->GetSubMeshContaining( shape3D );
724   if ( !mainSubMesh ) RETURN_BAD_RESULT("Null submesh of shape3D");
725
726   // analyse face submeshes
727   const map< int, SMESH_subMesh * >& subSM = mainSubMesh->DependsOn();
728   map< int, SMESH_subMesh * >::const_iterator i_subSM = subSM.begin();
729   for ( ; i_subSM != subSM.end(); ++i_subSM )
730   {
731     SMESH_subMesh* sm = i_subSM->second;
732     const TopoDS_Shape& face = sm->GetSubShape();
733     if ( face.ShapeType() != TopAbs_FACE )
734       continue;
735     nbFaces++;
736
737     // is quadrangle face?
738     list< TopoDS_Edge > orderedEdges;
739     list< int >         nbEdgesInWires;
740     TopoDS_Vertex       V000;
741     int nbWires = GetOrderedEdges( TopoDS::Face( face ),
742                                    V000, orderedEdges, nbEdgesInWires );
743     if ( nbWires != 1 || nbEdgesInWires.front() != 4 )
744       notQuadGeomSubMesh.push_back( sm );
745
746     // look for not quadrangle mesh elements
747     if ( SMESHDS_SubMesh* smDS = sm->GetSubMeshDS() ) {
748       bool hasNotQuad = false;
749       SMDS_ElemIteratorPtr eIt = smDS->GetElements();
750       while ( eIt->more() && !hasNotQuad ) {
751         const SMDS_MeshElement* elem = eIt->next();
752         if ( elem->GetType() == SMDSAbs_Face ) {
753           int nbNodes = elem->NbNodes();
754           if ( elem->IsQuadratic() )
755             nbNodes /= 2;
756           hasNotQuad = ( nbNodes != 4 );
757         }
758       }
759       if ( hasNotQuad )
760         notQuadElemSubMesh.push_back( sm );
761     }
762     else {
763       RETURN_BAD_RESULT("not meshed face");
764     }
765     // check if a quadrangle face is meshed with a quadranglar grid
766     if ( notQuadGeomSubMesh.back() != sm &&
767          notQuadElemSubMesh.back() != sm )
768     {
769       // count nb edges on face sides
770       vector< int > nbEdges;
771       nbEdges.reserve( nbEdgesInWires.front() );
772       for ( list< TopoDS_Edge >::iterator edge = orderedEdges.begin();
773             edge != orderedEdges.end(); ++edge )
774       {
775         if ( SMESHDS_SubMesh* smDS = meshDS->MeshElements( *edge ))
776           nbEdges.push_back ( smDS->NbElements() );
777         else
778           nbEdges.push_back ( 0 );
779       }
780       int nbQuads = sm->GetSubMeshDS()->NbElements();
781       if ( nbEdges[0] *  nbEdges[1] != nbQuads ||
782            nbEdges[0] != nbEdges[2] ||
783            nbEdges[1] != nbEdges[3] )
784         notQuadElemSubMesh.push_back( sm );
785     }
786   }
787
788   // ----------------------------------------------------------------------
789   // Analyse faces mesh and topology: choose the bottom submesh.
790   // If there are not quadrangle geom faces, they are top and bottom ones.
791   // Not quadrangle geom faces must be only on top and bottom.
792   // ----------------------------------------------------------------------
793
794   SMESH_subMesh * botSM = 0;
795   SMESH_subMesh * topSM = 0;
796
797   int nbNotQuad       = notQuadGeomSubMesh.size();
798   int nbNotQuadMeshed = notQuadElemSubMesh.size();
799   bool hasNotQuad = ( nbNotQuad || nbNotQuadMeshed );
800
801   // detect bad cases
802   if ( nbNotQuad > 0 && nbNotQuad != 2 )
803     RETURN_BAD_RESULT("Wrong shape geometry");
804   if ( nbNotQuadMeshed > 2 )
805     RETURN_BAD_RESULT("More then 2 faces meshed with not quadrangle elements");
806
807   // get found submeshes
808   if ( hasNotQuad )
809   {
810     if ( nbNotQuadMeshed > 0 ) botSM = notQuadElemSubMesh.front();
811     else                       botSM = notQuadGeomSubMesh.front();
812     if ( nbNotQuadMeshed > 1 ) topSM = notQuadElemSubMesh.back();
813     else if ( nbNotQuad  > 1 ) topSM = notQuadGeomSubMesh.back();
814   }
815   // detect other bad cases
816   if ( nbNotQuad == 2 && nbNotQuadMeshed > 0 ) {
817     bool ok = false;
818     if ( nbNotQuadMeshed == 1 )
819       ok = ( find( notQuadGeomSubMesh.begin(),
820                    notQuadGeomSubMesh.end(), botSM ) != notQuadGeomSubMesh.end() );
821     else
822       ok = ( notQuadGeomSubMesh == notQuadElemSubMesh );
823     if ( !ok )
824       RETURN_BAD_RESULT("Side face meshed with not quadrangle elements");
825   }
826
827   myNotQuadOnTop = ( nbNotQuadMeshed > 1 );
828     
829   // ----------------------------------------------------------
830
831   if ( nbNotQuad == 0 ) // Standard block of 6 quadrangle faces ?
832   {
833     // SMESH_Block will perform geometry analysis, we need just to find 2
834     // connected vertices on top and bottom
835
836     TopoDS_Vertex Vbot, Vtop;
837     if ( nbNotQuadMeshed > 0 ) // Look for vertices
838     {
839       TopTools_IndexedMapOfShape edgeMap;
840       TopExp::MapShapes( botSM->GetSubShape(), TopAbs_EDGE, edgeMap );
841       // vertex 1 is any vertex of the bottom face
842       Vbot = TopExp::FirstVertex( TopoDS::Edge( edgeMap( 1 )));
843       // vertex 2 is end vertex of edge sharing Vbot and not belonging to the bottom face
844       TopTools_ListIteratorOfListOfShape ancestIt = Mesh()->GetAncestors( Vbot );
845       for ( ; Vtop.IsNull() && ancestIt.More(); ancestIt.Next() )
846       {
847         const TopoDS_Shape & ancestor = ancestIt.Value();
848         if ( ancestor.ShapeType() == TopAbs_EDGE && !edgeMap.FindIndex( ancestor ))
849         {
850           TopoDS_Vertex V1, V2;
851           TopExp::Vertices( TopoDS::Edge( ancestor ), V1, V2);
852           if      ( Vbot.IsSame ( V1 )) Vtop = V2;
853           else if ( Vbot.IsSame ( V2 )) Vtop = V1;
854           // check that Vtop belongs to shape3D
855           TopExp_Explorer exp( shape3D, TopAbs_VERTEX );
856           for ( ; exp.More(); exp.Next() )
857             if ( Vtop.IsSame( exp.Current() ))
858               break;
859           if ( !exp.More() )
860             Vtop.Nullify();
861         }
862       }
863     }
864     // get shell from shape3D
865     TopoDS_Shell shell;
866     TopExp_Explorer exp( shape3D, TopAbs_SHELL );
867     int nbShell = 0;
868     for ( ; exp.More(); exp.Next(), ++nbShell )
869       shell = TopoDS::Shell( exp.Current() );
870 //     if ( nbShell != 1 )
871 //       RETURN_BAD_RESULT("There must be 1 shell in the block");
872
873     // Load geometry in SMESH_Block
874     if ( !SMESH_Block::FindBlockShapes( shell, Vbot, Vtop, myShapeIDMap )) {
875       if ( !hasNotQuad )
876         RETURN_BAD_RESULT("Can not detect top and bottom");
877     }
878     else {
879       if ( !botSM ) botSM = Mesh()->GetSubMeshContaining( myShapeIDMap( ID_BOT_FACE ));
880       if ( !topSM ) topSM = Mesh()->GetSubMeshContaining( myShapeIDMap( ID_TOP_FACE ));
881     }
882
883   } // end  Standard block of 6 quadrangle faces
884   // --------------------------------------------------------
885
886   // Here the top and bottom faces are found
887   if ( nbNotQuadMeshed == 2 ) // roughly check correspondence of horiz meshes
888   {
889 //     SMESHDS_SubMesh* topSMDS = topSM->GetSubMeshDS();
890 //     SMESHDS_SubMesh* botSMDS = botSM->GetSubMeshDS();
891 //     if ( topSMDS->NbNodes() != botSMDS->NbNodes() ||
892 //          topSMDS->NbElements() != botSMDS->NbElements() )
893 //       RETURN_BAD_RESULT("Top mesh doesn't correspond to bottom one");
894   }
895
896   // ---------------------------------------------------------
897   // If there are not quadrangle geom faces, we emulate
898   // a block of 6 quadrangle faces.
899   // Load SMESH_Block with faces and edges geometry
900   // ---------------------------------------------------------
901
902   
903   // find vertex 000 - the one with smallest coordinates (for easy DEBUG :-)
904   TopoDS_Vertex V000;
905   double minVal = DBL_MAX, minX, val;
906   for ( TopExp_Explorer exp( botSM->GetSubShape(), TopAbs_VERTEX );
907         exp.More(); exp.Next() )
908   {
909     const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
910     gp_Pnt P = BRep_Tool::Pnt( v );
911     val = P.X() + P.Y() + P.Z();
912     if ( val < minVal || ( val == minVal && P.X() < minX )) {
913       V000 = v;
914       minVal = val;
915       minX = P.X();
916     }
917   }
918
919   // Get ordered bottom edges
920   list< TopoDS_Edge > orderedEdges;
921   list< int >         nbVertexInWires;
922   SMESH_Block::GetOrderedEdges( TopoDS::Face( botSM->GetSubShape().Reversed() ),
923                                 V000, orderedEdges, nbVertexInWires );
924 //   if ( nbVertexInWires.size() != 1 )
925 //     RETURN_BAD_RESULT("Wrong prism geometry");
926
927   // Get Wall faces corresponding to the ordered bottom edges
928   list< TopoDS_Face > wallFaces;
929   if ( !GetWallFaces( Mesh(), shape3D, botSM->GetSubShape(), orderedEdges, wallFaces))
930     RETURN_BAD_RESULT("GetWallFaces() failed");
931
932   // Find columns of wall nodes and calculate edges' lengths
933   // --------------------------------------------------------
934
935   myParam2ColumnMaps.clear();
936   myParam2ColumnMaps.resize( orderedEdges.size() ); // total nb edges
937
938   int iE, nbEdges = nbVertexInWires.front(); // nb outer edges
939   vector< double > edgeLength( nbEdges );
940   map< double, int > len2edgeMap;
941
942   list< TopoDS_Edge >::iterator edgeIt = orderedEdges.begin();
943   list< TopoDS_Face >::iterator faceIt = wallFaces.begin();
944   for ( iE = 0; iE < nbEdges; ++edgeIt, ++faceIt )
945   {
946     TParam2ColumnMap & faceColumns = myParam2ColumnMaps[ iE ];
947     if ( !myHelper->LoadNodeColumns( faceColumns, *faceIt, *edgeIt, meshDS ))
948       RETURN_BAD_RESULT("SMESH_MesherHelper::LoadNodeColumns() failed");
949
950     SHOWYXZ("\np1 F "<<iE, gpXYZ(faceColumns.begin()->second.front() ));
951     SHOWYXZ("p2 F "<<iE, gpXYZ(faceColumns.rbegin()->second.front() ));
952     SHOWYXZ("V First "<<iE, BRep_Tool::Pnt( TopExp::FirstVertex(*edgeIt,true )));
953
954     edgeLength[ iE ] = SMESH_Algo::EdgeLength( *edgeIt );
955
956     if ( nbEdges < NB_WALL_FACES ) // fill map used to split faces
957     {
958       SMESHDS_SubMesh* smDS = meshDS->MeshElements( *edgeIt);
959       if ( !smDS )
960         RETURN_BAD_RESULT("Null submesh on a bottom edge");
961       // assure length uniqueness
962       edgeLength[ iE ] *= smDS->NbNodes() + edgeLength[ iE ] / ( 1000 + iE );
963       len2edgeMap[ edgeLength[ iE ]] = iE;
964     }
965     ++iE;
966   }
967   // Load columns of internal edges (forming holes)
968   // and fill map ShapeIndex to TParam2ColumnMap for them
969   for ( ; edgeIt != orderedEdges.end() ; ++edgeIt, ++faceIt )
970   {
971     TParam2ColumnMap & faceColumns = myParam2ColumnMaps[ iE ];
972     if ( !myHelper->LoadNodeColumns( faceColumns, *faceIt, *edgeIt, meshDS ))
973       RETURN_BAD_RESULT("SMESH_MesherHelper::LoadNodeColumns() failed");
974     // edge columns
975     int id = MeshDS()->ShapeToIndex( *edgeIt );
976     bool isForward = true; // meaningless for intenal wires
977     myShapeIndex2ColumnMap[ id ] = make_pair( & faceColumns, isForward );
978     // columns for vertices
979     // 1
980     const SMDS_MeshNode* n0 = faceColumns.begin()->second.front();
981     id = n0->GetPosition()->GetShapeId();
982     myShapeIndex2ColumnMap[ id ] = make_pair( & faceColumns, isForward );
983     // 2
984     const SMDS_MeshNode* n1 = faceColumns.rbegin()->second.front();
985     id = n1->GetPosition()->GetShapeId();
986     myShapeIndex2ColumnMap[ id ] = make_pair( & faceColumns, isForward );
987 //     SHOWYXZ("\np1 F "<<iE, gpXYZ(faceColumns.begin()->second.front() ));
988 //     SHOWYXZ("p2 F "<<iE, gpXYZ(faceColumns.rbegin()->second.front() ));
989 //     SHOWYXZ("V First "<<iE, BRep_Tool::Pnt( TopExp::FirstVertex(*edgeIt,true )));
990     ++iE;
991   }
992
993   // Create 4 wall faces of a block
994   // -------------------------------
995
996   if ( nbEdges <= NB_WALL_FACES ) // ************* Split faces if necessary
997   {
998     map< int, int > iE2nbSplit;
999     if ( nbEdges != NB_WALL_FACES ) // define how to split
1000     {
1001       if ( len2edgeMap.size() != nbEdges )
1002         RETURN_BAD_RESULT("Uniqueness of edge lengths not assured");
1003       map< double, int >::reverse_iterator maxLen_i = len2edgeMap.rbegin();
1004       map< double, int >::reverse_iterator midLen_i = ++len2edgeMap.rbegin();
1005       double maxLen = maxLen_i->first;
1006       double midLen = ( len2edgeMap.size() == 1 ) ? 0 : midLen_i->first;
1007       switch ( nbEdges ) {
1008       case 1: // 0-th edge is split into 4 parts
1009         iE2nbSplit.insert( make_pair( 0, 4 )); break;
1010       case 2: // either the longest edge is split into 3 parts, or both edges into halves
1011         if ( maxLen / 3 > midLen / 2 ) {
1012           iE2nbSplit.insert( make_pair( maxLen_i->second, 3 ));
1013         }
1014         else {
1015           iE2nbSplit.insert( make_pair( maxLen_i->second, 2 ));
1016           iE2nbSplit.insert( make_pair( midLen_i->second, 2 ));
1017         }
1018         break;
1019       case 3:
1020         // split longest into halves
1021         iE2nbSplit.insert( make_pair( maxLen_i->second, 2 ));
1022       }
1023     }
1024     // Create TSideFace's
1025     faceIt = wallFaces.begin();
1026     edgeIt = orderedEdges.begin();
1027     int iSide = 0;
1028     for ( iE = 0; iE < nbEdges; ++edgeIt, ++faceIt )
1029     {
1030      // split?
1031       map< int, int >::iterator i_nb = iE2nbSplit.find( iE );
1032       if ( i_nb != iE2nbSplit.end() ) {
1033         // split!
1034         int nbSplit = i_nb->second;
1035         vector< double > params;
1036         splitParams( nbSplit, &myParam2ColumnMaps[ iE ], params );
1037         bool isForward = ( edgeIt->Orientation() == TopAbs_FORWARD );
1038         for ( int i = 0; i < nbSplit; ++i ) {
1039           double f = ( isForward ? params[ i ] : params[ nbSplit - i-1 ]);
1040           double l = ( isForward ? params[ i+1 ] : params[ nbSplit - i ]);
1041           TSideFace* comp = new TSideFace( myHelper, wallFaceIds[ iSide ],
1042                                            *faceIt, *edgeIt,
1043                                            &myParam2ColumnMaps[ iE ], f, l );
1044           mySide->SetComponent( iSide++, comp );
1045         }
1046       }
1047       else {
1048         TSideFace* comp = new TSideFace( myHelper, wallFaceIds[ iSide ],
1049                                          *faceIt, *edgeIt,
1050                                          &myParam2ColumnMaps[ iE ]);
1051         mySide->SetComponent( iSide++, comp );
1052       }
1053       ++iE;
1054     }
1055   }
1056   else { // **************************** Unite faces
1057
1058     // unite first faces
1059     int nbExraFaces = nbEdges - 3;
1060     int iSide = 0, iE;
1061     double u0 = 0, sumLen = 0;
1062     for ( iE = 0; iE < nbExraFaces; ++iE )
1063       sumLen += edgeLength[ iE ];
1064
1065     vector< TSideFace* > components( nbExraFaces );
1066     vector< pair< double, double> > params( nbExraFaces );
1067     faceIt = wallFaces.begin();
1068     edgeIt = orderedEdges.begin();
1069     for ( iE = 0; iE < nbExraFaces; ++edgeIt, ++faceIt )
1070     {
1071       components[ iE ] = new TSideFace( myHelper, wallFaceIds[ iSide ],
1072                                         *faceIt, *edgeIt,
1073                                         &myParam2ColumnMaps[ iE ]);
1074       double u1 = u0 + edgeLength[ iE ] / sumLen;
1075       params[ iE ] = make_pair( u0 , u1 );
1076       u0 = u1;
1077       ++iE;
1078     }
1079     mySide->SetComponent( iSide++, new TSideFace( components, params ));
1080
1081     // fill the rest faces
1082     for ( ; iE < nbEdges; ++faceIt, ++edgeIt )
1083     {
1084       TSideFace* comp = new TSideFace( myHelper, wallFaceIds[ iSide ],
1085                                        *faceIt, *edgeIt,
1086                                        &myParam2ColumnMaps[ iE ]);
1087       mySide->SetComponent( iSide++, comp );
1088       ++iE;
1089     }
1090   }
1091
1092
1093   // Fill geometry fields of SMESH_Block
1094   // ------------------------------------
1095
1096   TopoDS_Face botF = TopoDS::Face( botSM->GetSubShape() );
1097   TopoDS_Face topF = TopoDS::Face( topSM->GetSubShape() );
1098
1099   vector< int > botEdgeIdVec;
1100   SMESH_Block::GetFaceEdgesIDs( ID_BOT_FACE, botEdgeIdVec );
1101
1102   bool isForward[NB_WALL_FACES] = { true, true, true, true };
1103   Adaptor2d_Curve2d* botPcurves[NB_WALL_FACES];
1104   Adaptor2d_Curve2d* topPcurves[NB_WALL_FACES];
1105
1106   for ( int iF = 0; iF < NB_WALL_FACES; ++iF )
1107   {
1108     TSideFace * sideFace = mySide->GetComponent( iF );
1109     if ( !sideFace )
1110       RETURN_BAD_RESULT("NULL TSideFace");
1111     int fID = sideFace->FaceID();
1112
1113     // fill myShapeIDMap
1114     if ( sideFace->InsertSubShapes( myShapeIDMap ) != 8 &&
1115          !sideFace->IsComplex())
1116       MESSAGE( ": Warning : InsertSubShapes() < 8 on side " << iF );
1117
1118     // side faces geometry
1119     Adaptor2d_Curve2d* pcurves[NB_WALL_FACES];
1120     if ( !sideFace->GetPCurves( pcurves ))
1121       RETURN_BAD_RESULT("TSideFace::GetPCurves() failed");
1122
1123     SMESH_Block::TFace& tFace = myFace[ fID - ID_FirstF ];
1124     tFace.Set( fID, sideFace->Surface(), pcurves, isForward );
1125
1126     SHOWYXZ( endl<<"F "<< iF << " id " << fID << " FRW " << sideFace->IsForward(), );
1127     // edges 3D geometry
1128     vector< int > edgeIdVec;
1129     SMESH_Block::GetFaceEdgesIDs( fID, edgeIdVec );
1130     for ( int isMax = 0; isMax < 2; ++isMax ) {
1131       {
1132         int eID = edgeIdVec[ isMax ];
1133         SMESH_Block::TEdge& tEdge = myEdge[ eID - ID_FirstE ];
1134         tEdge.Set( eID, sideFace->HorizCurve(isMax), true);
1135         SHOWYXZ(eID<<" HOR"<<isMax<<"(0)", sideFace->HorizCurve(isMax)->Value(0));
1136         SHOWYXZ(eID<<" HOR"<<isMax<<"(1)", sideFace->HorizCurve(isMax)->Value(1));
1137       }
1138       {
1139         int eID = edgeIdVec[ isMax+2 ];
1140         SMESH_Block::TEdge& tEdge = myEdge[ eID - ID_FirstE  ];
1141         tEdge.Set( eID, sideFace->VertiCurve(isMax), true);
1142         SHOWYXZ(eID<<" VER"<<isMax<<"(0)", sideFace->VertiCurve(isMax)->Value(0));
1143         SHOWYXZ(eID<<" VER"<<isMax<<"(1)", sideFace->VertiCurve(isMax)->Value(1));
1144
1145         // corner points
1146         vector< int > vertexIdVec;
1147         SMESH_Block::GetEdgeVertexIDs( eID, vertexIdVec );
1148         myPnt[ vertexIdVec[0] - ID_FirstV ] = tEdge.GetCurve()->Value(0).XYZ();
1149         myPnt[ vertexIdVec[1] - ID_FirstV ] = tEdge.GetCurve()->Value(1).XYZ();
1150       }
1151     }
1152     // pcurves on horizontal faces
1153     for ( iE = 0; iE < NB_WALL_FACES; ++iE ) {
1154       if ( edgeIdVec[ BOTTOM_EDGE ] == botEdgeIdVec[ iE ] ) {
1155         botPcurves[ iE ] = sideFace->HorizPCurve( false, botF );
1156         topPcurves[ iE ] = sideFace->HorizPCurve( true,  topF );
1157         break;
1158       }
1159     }
1160   }
1161   // horizontal faces geometry
1162   {
1163     SMESH_Block::TFace& tFace = myFace[ ID_BOT_FACE - ID_FirstF ];
1164     tFace.Set( ID_BOT_FACE, new BRepAdaptor_Surface( botF ), botPcurves, isForward );
1165     SMESH_Block::Insert( botF, ID_BOT_FACE, myShapeIDMap );
1166   }
1167   {
1168     SMESH_Block::TFace& tFace = myFace[ ID_TOP_FACE - ID_FirstF ];
1169     tFace.Set( ID_TOP_FACE, new BRepAdaptor_Surface( topF ), topPcurves, isForward );
1170     SMESH_Block::Insert( topF, ID_TOP_FACE, myShapeIDMap );
1171   }
1172
1173   // Fill map ShapeIndex to TParam2ColumnMap
1174   // ----------------------------------------
1175
1176   list< TSideFace* > fList;
1177   list< TSideFace* >::iterator fListIt;
1178   fList.push_back( mySide );
1179   for ( fListIt = fList.begin(); fListIt != fList.end(); ++fListIt)
1180   {
1181     int nb = (*fListIt)->NbComponents();
1182     for ( int i = 0; i < nb; ++i ) {
1183       if ( TSideFace* comp = (*fListIt)->GetComponent( i ))
1184         fList.push_back( comp );
1185     }
1186     if ( TParam2ColumnMap* cols = (*fListIt)->GetColumns()) {
1187       // columns for a base edge
1188       int id = MeshDS()->ShapeToIndex( (*fListIt)->BaseEdge() );
1189       bool isForward = (*fListIt)->IsForward();
1190       myShapeIndex2ColumnMap[ id ] = make_pair( cols, isForward );
1191
1192       // columns for vertices
1193       const SMDS_MeshNode* n0 = cols->begin()->second.front();
1194       id = n0->GetPosition()->GetShapeId();
1195       myShapeIndex2ColumnMap[ id ] = make_pair( cols, isForward );
1196
1197       const SMDS_MeshNode* n1 = cols->rbegin()->second.front();
1198       id = n1->GetPosition()->GetShapeId();
1199       myShapeIndex2ColumnMap[ id ] = make_pair( cols, !isForward );
1200     }
1201   }
1202
1203 //   gp_XYZ testPar(0.25, 0.25, 0), testCoord;
1204 //   if ( !FacePoint( ID_BOT_FACE, testPar, testCoord ))
1205 //     RETURN_BAD_RESULT("TEST FacePoint() FAILED");
1206 //   SHOWYXZ("IN TEST PARAM" , testPar);
1207 //   SHOWYXZ("OUT TEST CORD" , testCoord);
1208 //   if ( !ComputeParameters( testCoord, testPar , ID_BOT_FACE))
1209 //     RETURN_BAD_RESULT("TEST ComputeParameters() FAILED");
1210 //   SHOWYXZ("OUT TEST PARAM" , testPar);
1211
1212   return true;
1213 }
1214
1215 //================================================================================
1216 /*!
1217  * \brief Return pointer to column of nodes
1218  * \param node - bottom node from which the returned column goes up
1219  * \retval const TNodeColumn* - the found column
1220  */
1221 //================================================================================
1222
1223 const TNodeColumn* StdMeshers_PrismAsBlock::GetNodeColumn(const SMDS_MeshNode* node) const
1224 {
1225   int sID = node->GetPosition()->GetShapeId();
1226
1227   map<int, pair< TParam2ColumnMap*, bool > >::const_iterator col_frw =
1228     myShapeIndex2ColumnMap.find( sID );
1229   if ( col_frw != myShapeIndex2ColumnMap.end() ) {
1230     const TParam2ColumnMap* cols = col_frw->second.first;
1231     TParam2ColumnIt u_col = cols->begin();
1232     for ( ; u_col != cols->end(); ++u_col )
1233       if ( u_col->second[ 0 ] == node )
1234         return & u_col->second;
1235   }
1236   return 0;
1237 }
1238
1239 //================================================================================
1240 /*!
1241  * \brief Check curve orientation of a bootom edge
1242   * \param meshDS - mesh DS
1243   * \param columnsMap - node columns map of side face
1244   * \param bottomEdge - the bootom edge
1245   * \param sideFaceID - side face in-block ID
1246   * \retval bool - true if orientation coinside with in-block froward orientation
1247  */
1248 //================================================================================
1249
1250 bool StdMeshers_PrismAsBlock::IsForwardEdge(SMESHDS_Mesh*           meshDS,
1251                                             const TParam2ColumnMap& columnsMap,
1252                                             const TopoDS_Edge &     bottomEdge,
1253                                             const int               sideFaceID)
1254 {
1255   bool isForward = false;
1256   if ( TAssocTool::IsClosedEdge( bottomEdge ))
1257   {
1258     isForward = ( bottomEdge.Orientation() == TopAbs_FORWARD );
1259   }
1260   else
1261   {
1262     const TNodeColumn& firstCol = columnsMap.begin()->second;
1263     const SMDS_MeshNode* bottomNode = firstCol[0];
1264     TopoDS_Shape firstVertex = SMESH_MesherHelper::GetSubShapeByNode( bottomNode, meshDS );
1265     isForward = ( firstVertex.IsSame( TopExp::FirstVertex( bottomEdge, true )));
1266   }
1267   // on 2 of 4 sides first vertex is end
1268   if ( sideFaceID == ID_Fx1z || sideFaceID == ID_F0yz )
1269     isForward = !isForward;
1270   return isForward;
1271 }
1272
1273 //================================================================================
1274   /*!
1275    * \brief Find wall faces by bottom edges
1276     * \param mesh - the mesh
1277     * \param mainShape - the prism
1278     * \param bottomFace - the bottom face
1279     * \param bottomEdges - edges bounding the bottom face
1280     * \param wallFaces - faces list to fill in
1281    */
1282 //================================================================================
1283
1284 bool StdMeshers_PrismAsBlock::GetWallFaces( SMESH_Mesh*                     mesh,
1285                                             const TopoDS_Shape &            mainShape,
1286                                             const TopoDS_Shape &            bottomFace,
1287                                             const std::list< TopoDS_Edge >& bottomEdges,
1288                                             std::list< TopoDS_Face >&       wallFaces)
1289 {
1290   wallFaces.clear();
1291
1292   TopTools_IndexedMapOfShape faceMap;
1293   TopExp::MapShapes( mainShape, TopAbs_FACE, faceMap );
1294
1295   list< TopoDS_Edge >::const_iterator edge = bottomEdges.begin();
1296   for ( ; edge != bottomEdges.end(); ++edge )
1297   {
1298     TopTools_ListIteratorOfListOfShape ancestIt = mesh->GetAncestors( *edge );
1299     for ( ; ancestIt.More(); ancestIt.Next() )
1300     {
1301       const TopoDS_Shape& ancestor = ancestIt.Value();
1302       if ( ancestor.ShapeType() == TopAbs_FACE && // face
1303            !bottomFace.IsSame( ancestor ) &&      // not bottom
1304            faceMap.FindIndex( ancestor ))         // belongs to the prism
1305       {
1306         wallFaces.push_back( TopoDS::Face( ancestor ));
1307         break;
1308       }
1309     }
1310   }
1311   return ( wallFaces.size() == bottomEdges.size() );
1312 }
1313
1314 //================================================================================
1315 /*!
1316  * \brief Constructor
1317   * \param faceID - in-block ID
1318   * \param face - geom face
1319   * \param columnsMap - map of node columns
1320   * \param first - first normalized param
1321   * \param last - last normalized param
1322  */
1323 //================================================================================
1324
1325 StdMeshers_PrismAsBlock::TSideFace::TSideFace(SMESH_MesherHelper* helper,
1326                                               const int           faceID,
1327                                               const TopoDS_Face&  face,
1328                                               const TopoDS_Edge&  baseEdge,
1329                                               TParam2ColumnMap*   columnsMap,
1330                                               const double        first,
1331                                               const double        last):
1332   myID( faceID ),
1333   myParamToColumnMap( columnsMap ),
1334   myBaseEdge( baseEdge ),
1335   myHelper( helper )
1336 {
1337   mySurface.Initialize( face );
1338   myParams.resize( 1 );
1339   myParams[ 0 ] = make_pair( first, last );
1340   myIsForward = StdMeshers_PrismAsBlock::IsForwardEdge( myHelper->GetMeshDS(),
1341                                                         *myParamToColumnMap,
1342                                                         myBaseEdge, myID );
1343 }
1344
1345 //================================================================================
1346 /*!
1347  * \brief Constructor of complex side face
1348  */
1349 //================================================================================
1350
1351 StdMeshers_PrismAsBlock::TSideFace::
1352 TSideFace(const vector< TSideFace* >&             components,
1353           const vector< pair< double, double> > & params)
1354   :myID( components[0] ? components[0]->myID : 0 ),
1355    myParamToColumnMap( 0 ),
1356    myParams( params ),
1357    myIsForward( true ),
1358    myComponents( components ),
1359    myHelper( components[0] ? components[0]->myHelper : 0 )
1360 {}
1361 //================================================================================
1362 /*!
1363  * \brief Copy constructor
1364   * \param other - other side
1365  */
1366 //================================================================================
1367
1368 StdMeshers_PrismAsBlock::TSideFace::TSideFace( const TSideFace& other )
1369 {
1370   myID               = other.myID;
1371   mySurface          = other.mySurface;
1372   myBaseEdge         = other.myBaseEdge;
1373   myParams           = other.myParams;
1374   myIsForward        = other.myIsForward;
1375   myHelper           = other.myHelper;
1376   myParamToColumnMap = other.myParamToColumnMap;
1377
1378   myComponents.resize( other.myComponents.size());
1379   for (int i = 0 ; i < myComponents.size(); ++i )
1380     myComponents[ i ] = new TSideFace( *other.myComponents[ i ]);
1381 }
1382
1383 //================================================================================
1384 /*!
1385  * \brief Deletes myComponents
1386  */
1387 //================================================================================
1388
1389 StdMeshers_PrismAsBlock::TSideFace::~TSideFace()
1390 {
1391   for (int i = 0 ; i < myComponents.size(); ++i )
1392     if ( myComponents[ i ] )
1393       delete myComponents[ i ];
1394 }
1395
1396 //================================================================================
1397 /*!
1398  * \brief Return geometry of the vertical curve
1399   * \param isMax - true means curve located closer to (1,1,1) block point
1400   * \retval Adaptor3d_Curve* - curve adaptor
1401  */
1402 //================================================================================
1403
1404 Adaptor3d_Curve* StdMeshers_PrismAsBlock::TSideFace::VertiCurve(const bool isMax) const
1405 {
1406   if ( !myComponents.empty() ) {
1407     if ( isMax )
1408       return myComponents.back()->VertiCurve(isMax);
1409     else
1410       return myComponents.front()->VertiCurve(isMax);
1411   }
1412   double f = myParams[0].first, l = myParams[0].second;
1413   if ( !myIsForward ) std::swap( f, l );
1414   return new TVerticalEdgeAdaptor( myParamToColumnMap, isMax ? l : f );
1415 }
1416
1417 //================================================================================
1418 /*!
1419  * \brief Return geometry of the top or bottom curve
1420   * \param isTop - 
1421   * \retval Adaptor3d_Curve* - 
1422  */
1423 //================================================================================
1424
1425 Adaptor3d_Curve* StdMeshers_PrismAsBlock::TSideFace::HorizCurve(const bool isTop) const
1426 {
1427   return new THorizontalEdgeAdaptor( this, isTop );
1428 }
1429
1430 //================================================================================
1431 /*!
1432  * \brief Return pcurves
1433   * \param pcurv - array of 4 pcurves
1434   * \retval bool - is a success
1435  */
1436 //================================================================================
1437
1438 bool StdMeshers_PrismAsBlock::TSideFace::GetPCurves(Adaptor2d_Curve2d* pcurv[4]) const
1439 {
1440   int iEdge[ 4 ] = { BOTTOM_EDGE, TOP_EDGE, V0_EDGE, V1_EDGE };
1441
1442   for ( int i = 0 ; i < 4 ; ++i ) {
1443     Handle(Geom2d_Line) line;
1444     switch ( iEdge[ i ] ) {
1445     case TOP_EDGE:
1446       line = new Geom2d_Line( gp_Pnt2d( 0, 1 ), gp::DX2d() ); break;
1447     case BOTTOM_EDGE:
1448       line = new Geom2d_Line( gp::Origin2d(), gp::DX2d() ); break;
1449     case V0_EDGE:
1450       line = new Geom2d_Line( gp::Origin2d(), gp::DY2d() ); break;
1451     case V1_EDGE:
1452       line = new Geom2d_Line( gp_Pnt2d( 1, 0 ), gp::DY2d() ); break;
1453     }
1454     pcurv[ i ] = new Geom2dAdaptor_Curve( line, 0, 1 );
1455   }
1456   return true;
1457 }
1458
1459 //================================================================================
1460 /*!
1461  * \brief Returns geometry of pcurve on a horizontal face
1462   * \param isTop - is top or bottom face
1463   * \param horFace - a horizontal face
1464   * \retval Adaptor2d_Curve2d* - curve adaptor
1465  */
1466 //================================================================================
1467
1468 Adaptor2d_Curve2d*
1469 StdMeshers_PrismAsBlock::TSideFace::HorizPCurve(const bool         isTop,
1470                                                 const TopoDS_Face& horFace) const
1471 {
1472   return new TPCurveOnHorFaceAdaptor( this, isTop, horFace );
1473 }
1474
1475 //================================================================================
1476 /*!
1477  * \brief Return a component corresponding to parameter
1478   * \param U - parameter along a horizontal size
1479   * \param localU - parameter along a horizontal size of a component
1480   * \retval TSideFace* - found component
1481  */
1482 //================================================================================
1483
1484 StdMeshers_PrismAsBlock::TSideFace*
1485 StdMeshers_PrismAsBlock::TSideFace::GetComponent(const double U,double & localU) const
1486 {
1487   localU = U;
1488   if ( myComponents.empty() )
1489     return const_cast<TSideFace*>( this );
1490
1491   int i;
1492   for ( i = 0; i < myComponents.size(); ++i )
1493     if ( U < myParams[ i ].second )
1494       break;
1495   if ( i >= myComponents.size() )
1496     i = myComponents.size() - 1;
1497
1498   double f = myParams[ i ].first, l = myParams[ i ].second;
1499   localU = ( U - f ) / ( l - f );
1500   return myComponents[ i ];
1501 }
1502
1503 //================================================================================
1504 /*!
1505  * \brief Find node columns for a parameter
1506   * \param U - parameter along a horizontal edge
1507   * \param col1 - the 1st found column
1508   * \param col2 - the 2nd found column
1509   * \retval r - normalized position of U between the found columns
1510  */
1511 //================================================================================
1512
1513 double StdMeshers_PrismAsBlock::TSideFace::GetColumns(const double      U,
1514                                                       TParam2ColumnIt & col1,
1515                                                       TParam2ColumnIt & col2) const
1516 {
1517   double u = U, r = 0;
1518   if ( !myComponents.empty() ) {
1519     TSideFace * comp = GetComponent(U,u);
1520     return comp->GetColumns( u, col1, col2 );
1521   }
1522
1523   if ( !myIsForward )
1524     u = 1 - u;
1525   double f = myParams[0].first, l = myParams[0].second;
1526   u = f + u * ( l - f );
1527
1528   col1 = col2 = getColumn( myParamToColumnMap, u );
1529   if ( ++col2 == myParamToColumnMap->end() ) {
1530     --col2;
1531     r = 0.5;
1532   }
1533   else {
1534 //     if ( !myIsForward )
1535 //       std::swap( col1, col2 );
1536     double uf = col1->first;
1537     double ul = col2->first;
1538     r = ( u - uf ) / ( ul - uf );
1539   }
1540   return r;
1541 }
1542
1543 //================================================================================
1544 /*!
1545  * \brief Return coordinates by normalized params
1546   * \param U - horizontal param
1547   * \param V - vertical param
1548   * \retval gp_Pnt - result point
1549  */
1550 //================================================================================
1551
1552 gp_Pnt StdMeshers_PrismAsBlock::TSideFace::Value(const Standard_Real U,
1553                                                  const Standard_Real V) const
1554 {
1555   double u;
1556   if ( !myComponents.empty() ) {
1557     TSideFace * comp = GetComponent(U,u);
1558     return comp->Value( u, V );
1559   }
1560
1561   TParam2ColumnIt u_col1, u_col2;
1562   double vR, hR = GetColumns( U, u_col1, u_col2 );
1563
1564   const SMDS_MeshNode* n1 = 0;
1565   const SMDS_MeshNode* n2 = 0;
1566   const SMDS_MeshNode* n3 = 0;
1567   const SMDS_MeshNode* n4 = 0;
1568   gp_XYZ pnt;
1569
1570   vR = getRAndNodes( & u_col1->second, V, n1, n2 );
1571   vR = getRAndNodes( & u_col2->second, V, n3, n4 );
1572   
1573   gp_XY uv1 = myHelper->GetNodeUV( mySurface.Face(), n1, n4);
1574   gp_XY uv2 = myHelper->GetNodeUV( mySurface.Face(), n2, n3);
1575   gp_XY uv12 = uv1 * ( 1 - vR ) + uv2 * vR;
1576
1577   gp_XY uv3 = myHelper->GetNodeUV( mySurface.Face(), n3, n2);
1578   gp_XY uv4 = myHelper->GetNodeUV( mySurface.Face(), n4, n1);
1579   gp_XY uv34 = uv3 * ( 1 - vR ) + uv4 * vR;
1580
1581   gp_XY uv = uv12 * ( 1 - hR ) + uv34 * hR;
1582   
1583   return mySurface.Value( uv.X(), uv.Y() );
1584 }
1585
1586
1587 //================================================================================
1588 /*!
1589  * \brief Return boundary edge
1590   * \param edge - edge index
1591   * \retval TopoDS_Edge - found edge
1592  */
1593 //================================================================================
1594
1595 TopoDS_Edge StdMeshers_PrismAsBlock::TSideFace::GetEdge(const int iEdge) const
1596 {
1597   if ( !myComponents.empty() ) {
1598     switch ( iEdge ) {
1599     case V0_EDGE : return myComponents.front()->GetEdge( iEdge );
1600     case V1_EDGE : return myComponents.back() ->GetEdge( iEdge );
1601     default: return TopoDS_Edge();
1602     }
1603   }
1604   TopoDS_Shape edge;
1605   const SMDS_MeshNode* node = 0;
1606   SMESHDS_Mesh * meshDS = myHelper->GetMesh()->GetMeshDS();
1607   TNodeColumn* column;
1608
1609   switch ( iEdge ) {
1610   case TOP_EDGE:
1611   case BOTTOM_EDGE:
1612     column = & (( ++myParamToColumnMap->begin())->second );
1613     node = ( iEdge == TOP_EDGE ) ? column->back() : column->front();
1614     edge = myHelper->GetSubShapeByNode ( node, meshDS );
1615     if ( edge.ShapeType() == TopAbs_VERTEX ) {
1616       column = & ( myParamToColumnMap->begin()->second );
1617       node = ( iEdge == TOP_EDGE ) ? column->back() : column->front();
1618     }
1619     break;
1620   case V0_EDGE:
1621   case V1_EDGE: {
1622     bool back = ( iEdge == V1_EDGE );
1623     if ( !myIsForward ) back = !back;
1624     if ( back )
1625       column = & ( myParamToColumnMap->rbegin()->second );
1626     else
1627       column = & ( myParamToColumnMap->begin()->second );
1628     if ( column->size() > 0 )
1629       edge = myHelper->GetSubShapeByNode( (*column)[ 1 ], meshDS );
1630     if ( edge.IsNull() || edge.ShapeType() == TopAbs_VERTEX )
1631       node = column->front();
1632     break;
1633   }
1634   default:;
1635   }
1636   if ( !edge.IsNull() && edge.ShapeType() == TopAbs_EDGE )
1637     return TopoDS::Edge( edge );
1638
1639   // find edge by 2 vertices
1640   TopoDS_Shape V1 = edge;
1641   TopoDS_Shape V2 = myHelper->GetSubShapeByNode( node, meshDS );
1642   if ( V2.ShapeType() == TopAbs_VERTEX && !V2.IsSame( V1 ))
1643   {
1644     TopTools_ListIteratorOfListOfShape ancestIt =
1645       myHelper->GetMesh()->GetAncestors( V1 );
1646     for ( ; ancestIt.More(); ancestIt.Next() )
1647     {
1648       const TopoDS_Shape & ancestor = ancestIt.Value();
1649       if ( ancestor.ShapeType() == TopAbs_EDGE )
1650         for ( TopExp_Explorer e( ancestor, TopAbs_VERTEX ); e.More(); e.Next() )
1651           if ( V2.IsSame( e.Current() ))
1652             return TopoDS::Edge( ancestor );
1653     }
1654   }
1655   return TopoDS_Edge();
1656 }
1657
1658 //================================================================================
1659 /*!
1660  * \brief Fill block subshapes
1661   * \param shapeMap - map to fill in
1662   * \retval int - nb inserted subshapes
1663  */
1664 //================================================================================
1665
1666 int StdMeshers_PrismAsBlock::TSideFace::InsertSubShapes(TBlockShapes& shapeMap) const
1667 {
1668   int nbInserted = 0;
1669
1670   // Insert edges
1671   vector< int > edgeIdVec;
1672   SMESH_Block::GetFaceEdgesIDs( myID, edgeIdVec );
1673
1674   for ( int i = BOTTOM_EDGE; i <=V1_EDGE ; ++i ) {
1675     TopoDS_Edge e = GetEdge( i );
1676     if ( !e.IsNull() ) {
1677       nbInserted += SMESH_Block::Insert( e, edgeIdVec[ i ], shapeMap);
1678     }
1679   }
1680
1681   // Insert corner vertices
1682
1683   TParam2ColumnIt col1, col2 ;
1684   vector< int > vertIdVec;
1685
1686   // from V0 column
1687   SMESH_Block::GetEdgeVertexIDs( edgeIdVec[ V0_EDGE ], vertIdVec);
1688   GetColumns(0, col1, col2 );
1689   const SMDS_MeshNode* node0 = col1->second.front();
1690   const SMDS_MeshNode* node1 = col1->second.back();
1691   TopoDS_Shape v0 = myHelper->GetSubShapeByNode( node0, myHelper->GetMeshDS());
1692   TopoDS_Shape v1 = myHelper->GetSubShapeByNode( node1, myHelper->GetMeshDS());
1693   if ( v0.ShapeType() == TopAbs_VERTEX ) {
1694     nbInserted += SMESH_Block::Insert( v0, vertIdVec[ 0 ], shapeMap);
1695   }
1696   if ( v1.ShapeType() == TopAbs_VERTEX ) {
1697     nbInserted += SMESH_Block::Insert( v1, vertIdVec[ 1 ], shapeMap);
1698   }
1699   
1700   // from V1 column
1701   SMESH_Block::GetEdgeVertexIDs( edgeIdVec[ V1_EDGE ], vertIdVec);
1702   GetColumns(1, col1, col2 );
1703   node0 = col2->second.front();
1704   node1 = col2->second.back();
1705   v0 = myHelper->GetSubShapeByNode( node0, myHelper->GetMeshDS());
1706   v1 = myHelper->GetSubShapeByNode( node1, myHelper->GetMeshDS());
1707   if ( v0.ShapeType() == TopAbs_VERTEX ) {
1708     nbInserted += SMESH_Block::Insert( v0, vertIdVec[ 0 ], shapeMap);
1709   }
1710   if ( v1.ShapeType() == TopAbs_VERTEX ) {
1711     nbInserted += SMESH_Block::Insert( v1, vertIdVec[ 1 ], shapeMap);
1712   }
1713
1714 //   TopoDS_Vertex V0, V1, Vcom;
1715 //   TopExp::Vertices( myBaseEdge, V0, V1, true );
1716 //   if ( !myIsForward ) std::swap( V0, V1 );
1717
1718 //   // bottom vertex IDs
1719 //   SMESH_Block::GetEdgeVertexIDs( edgeIdVec[ _u0 ], vertIdVec);
1720 //   SMESH_Block::Insert( V0, vertIdVec[ 0 ], shapeMap);
1721 //   SMESH_Block::Insert( V1, vertIdVec[ 1 ], shapeMap);
1722
1723 //   TopoDS_Edge sideEdge = GetEdge( V0_EDGE );
1724 //   if ( sideEdge.IsNull() || !TopExp::CommonVertex( botEdge, sideEdge, Vcom ))
1725 //     return false;
1726
1727 //   // insert one side edge
1728 //   int edgeID;
1729 //   if ( Vcom.IsSame( V0 )) edgeID = edgeIdVec[ _v0 ];
1730 //   else                    edgeID = edgeIdVec[ _v1 ];
1731 //   SMESH_Block::Insert( sideEdge, edgeID, shapeMap);
1732
1733 //   // top vertex of the side edge
1734 //   SMESH_Block::GetEdgeVertexIDs( edgeID, vertIdVec);
1735 //   TopoDS_Vertex Vtop = TopExp::FirstVertex( sideEdge );
1736 //   if ( Vcom.IsSame( Vtop ))
1737 //     Vtop = TopExp::LastVertex( sideEdge );
1738 //   SMESH_Block::Insert( Vtop, vertIdVec[ 1 ], shapeMap);
1739
1740 //   // other side edge
1741 //   sideEdge = GetEdge( V1_EDGE );
1742 //   if ( sideEdge.IsNull() )
1743 //     return false;
1744 //   if ( edgeID = edgeIdVec[ _v1 ]) edgeID = edgeIdVec[ _v0 ];
1745 //   else                            edgeID = edgeIdVec[ _v1 ];
1746 //   SMESH_Block::Insert( sideEdge, edgeID, shapeMap);
1747   
1748 //   // top edge
1749 //   TopoDS_Edge topEdge = GetEdge( TOP_EDGE );
1750 //   SMESH_Block::Insert( topEdge, edgeIdVec[ _u1 ], shapeMap);
1751
1752 //   // top vertex of the other side edge
1753 //   if ( !TopExp::CommonVertex( topEdge, sideEdge, Vcom ))
1754 //     return false;
1755 //   SMESH_Block::GetEdgeVertexIDs( edgeID, vertIdVec );
1756 //   SMESH_Block::Insert( Vcom, vertIdVec[ 1 ], shapeMap);
1757
1758   return nbInserted;
1759 }
1760
1761 //================================================================================
1762 /*!
1763  * \brief Creates TVerticalEdgeAdaptor 
1764   * \param columnsMap - node column map
1765   * \param parameter - normalized parameter
1766  */
1767 //================================================================================
1768
1769 StdMeshers_PrismAsBlock::TVerticalEdgeAdaptor::
1770 TVerticalEdgeAdaptor( const TParam2ColumnMap* columnsMap, const double parameter)
1771 {
1772   myNodeColumn = & getColumn( columnsMap, parameter )->second;
1773 }
1774
1775 //================================================================================
1776 /*!
1777  * \brief Return coordinates for the given normalized parameter
1778   * \param U - normalized parameter
1779   * \retval gp_Pnt - coordinates
1780  */
1781 //================================================================================
1782
1783 gp_Pnt StdMeshers_PrismAsBlock::TVerticalEdgeAdaptor::Value(const Standard_Real U) const
1784 {
1785   const SMDS_MeshNode* n1;
1786   const SMDS_MeshNode* n2;
1787   double r = getRAndNodes( myNodeColumn, U, n1, n2 );
1788   return gpXYZ(n1) * ( 1 - r ) + gpXYZ(n2) * r;
1789 }
1790
1791 //================================================================================
1792 /*!
1793  * \brief Return coordinates for the given normalized parameter
1794   * \param U - normalized parameter
1795   * \retval gp_Pnt - coordinates
1796  */
1797 //================================================================================
1798
1799 gp_Pnt StdMeshers_PrismAsBlock::THorizontalEdgeAdaptor::Value(const Standard_Real U) const
1800 {
1801   return mySide->TSideFace::Value( U, myV );
1802 }
1803
1804 //================================================================================
1805 /*!
1806  * \brief Return UV on pcurve for the given normalized parameter
1807   * \param U - normalized parameter
1808   * \retval gp_Pnt - coordinates
1809  */
1810 //================================================================================
1811
1812 gp_Pnt2d StdMeshers_PrismAsBlock::TPCurveOnHorFaceAdaptor::Value(const Standard_Real U) const
1813 {
1814   TParam2ColumnIt u_col1, u_col2;
1815   double r = mySide->GetColumns( U, u_col1, u_col2 );
1816   gp_XY uv1 = mySide->GetNodeUV( myFace, u_col1->second[ myZ ]);
1817   gp_XY uv2 = mySide->GetNodeUV( myFace, u_col2->second[ myZ ]);
1818   return uv1 * ( 1 - r ) + uv2 * r;
1819 }