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