Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMESH / SMESH_Block.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 // File      : SMESH_Pattern.hxx
21 // Created   : Mon Aug  2 10:30:00 2004
22 // Author    : Edward AGAPOV (eap)
23
24 using namespace std;
25
26 #include "SMESH_Block.hxx"
27
28 #include <BRepAdaptor_Curve.hxx>
29 #include <BRepAdaptor_Curve2d.hxx>
30 #include <BRepAdaptor_Surface.hxx>
31 #include <BRepTools.hxx>
32 #include <BRepTools_WireExplorer.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Bnd_Box.hxx>
36 #include <Extrema_ExtPC.hxx>
37 #include <Extrema_POnCurv.hxx>
38 #include <Geom2d_Curve.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
41 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopoDS.hxx>
44 #include <TopoDS_Compound.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Iterator.hxx>
47 #include <TopoDS_Wire.hxx>
48 #include <gp_Trsf.hxx>
49 #include <gp_Vec.hxx>
50 #include <math_FunctionSetRoot.hxx>
51 #include <math_Matrix.hxx>
52 #include <math_Vector.hxx>
53
54 #include "SMDS_MeshNode.hxx"
55 #include "SMDS_MeshVolume.hxx"
56 #include "SMDS_VolumeTool.hxx"
57 #include "utilities.h"
58
59 #include <list>
60
61 using namespace std;
62
63 //#define DEBUG_PARAM_COMPUTE
64
65 //================================================================================
66 /*!
67  * \brief Set edge data
68   * \param edgeID - block subshape ID
69   * \param curve - edge geometry
70   * \param isForward - is curve orientation coincides with edge orientation in the block
71  */
72 //================================================================================
73
74 void SMESH_Block::TEdge::Set( const int edgeID, Adaptor3d_Curve* curve, const bool isForward )
75 {
76   myCoordInd = SMESH_Block::GetCoordIndOnEdge( edgeID );
77   if ( myC3d ) delete myC3d;
78   myC3d = curve;
79   myFirst = curve->FirstParameter();
80   myLast = curve->LastParameter();
81   if ( !isForward )
82     std::swap( myFirst, myLast );
83 }
84
85 //================================================================================
86 /*!
87  * \brief Set coordinates of nodes at edge ends to work with mesh block
88   * \param edgeID - block subshape ID
89   * \param node1 - coordinates of node with lower ID
90   * \param node2 - coordinates of node with upper ID
91  */
92 //================================================================================
93
94 void SMESH_Block::TEdge::Set( const int edgeID, const gp_XYZ& node1, const gp_XYZ& node2 )
95 {
96   myCoordInd = SMESH_Block::GetCoordIndOnEdge( edgeID );
97   myNodes[ 0 ] = node1;
98   myNodes[ 1 ] = node2;
99
100   if ( myC3d ) delete myC3d;
101   myC3d = 0;
102 }
103
104 //=======================================================================
105 //function : SMESH_Block::TEdge::GetU
106 //purpose  : 
107 //=======================================================================
108
109 double SMESH_Block::TEdge::GetU( const gp_XYZ& theParams ) const
110 {
111   double u = theParams.Coord( myCoordInd );
112   if ( !myC3d ) // if mesh block
113     return u;
114   return ( 1 - u ) * myFirst + u * myLast;
115 }
116
117 //=======================================================================
118 //function : SMESH_Block::TEdge::Point
119 //purpose  : 
120 //=======================================================================
121
122 gp_XYZ SMESH_Block::TEdge::Point( const gp_XYZ& theParams ) const
123 {
124   double u = GetU( theParams );
125   if ( myC3d ) return myC3d->Value( u ).XYZ();
126   // mesh block
127   return myNodes[0] * ( 1 - u ) + myNodes[1] * u;
128 }
129
130 //================================================================================
131 /*!
132  * \brief Destructor
133  */
134 //================================================================================
135
136 SMESH_Block::TEdge::~TEdge()
137 {
138   if ( myC3d ) delete myC3d;
139 }
140
141 //================================================================================
142 /*!
143  * \brief Set face data
144   * \param faceID - block subshape ID
145   * \param S - face surface geometry
146   * \param c2d - 4 pcurves in the order as returned by GetFaceEdgesIDs(faceID)
147   * \param isForward - orientation of pcurves comparing with block edge direction
148  */
149 //================================================================================
150
151 void SMESH_Block::TFace::Set( const int          faceID,
152                               Adaptor3d_Surface* S,
153                               Adaptor2d_Curve2d* c2D[4],
154                               const bool         isForward[4] )
155 {
156   if ( myS ) delete myS;
157   myS = S;
158   // pcurves
159   vector< int > edgeIdVec;
160   GetFaceEdgesIDs( faceID, edgeIdVec );
161   for ( int iE = 0; iE < edgeIdVec.size(); iE++ ) // loop on 4 edges
162   {
163     myCoordInd[ iE ] = GetCoordIndOnEdge( edgeIdVec[ iE ] );
164     if ( myC2d[ iE ]) delete myC2d[ iE ];
165     myC2d[ iE ] = c2D[ iE ];
166     myFirst[ iE ] = myC2d[ iE ]->FirstParameter();
167     myLast [ iE ] = myC2d[ iE ]->LastParameter();
168     if ( !isForward[ iE ])
169       std::swap( myFirst[ iE ], myLast[ iE ] );
170   }
171   // 2d corners
172   myCorner[ 0 ] = myC2d[ 0 ]->Value( myFirst[0] ).XY();
173   myCorner[ 1 ] = myC2d[ 0 ]->Value( myLast[0] ).XY();
174   myCorner[ 2 ] = myC2d[ 1 ]->Value( myLast[1] ).XY();
175   myCorner[ 3 ] = myC2d[ 1 ]->Value( myFirst[1] ).XY();
176 }
177
178 //================================================================================
179 /*!
180  * \brief Set face data to work with mesh block
181   * \param faceID - block subshape ID
182   * \param edgeU0 - filled data of edge u0 = GetFaceEdgesIDs(faceID)[ 0 ]
183   * \param edgeU1 - filled data of edge u1 = GetFaceEdgesIDs(faceID)[ 1 ]
184  */
185 //================================================================================
186
187 void SMESH_Block::TFace::Set( const int faceID, const TEdge& edgeU0, const TEdge& edgeU1 )
188 {
189   vector< int > edgeIdVec;
190   GetFaceEdgesIDs( faceID, edgeIdVec );
191   myNodes[ 0 ] = edgeU0.NodeXYZ( 1 );
192   myNodes[ 1 ] = edgeU0.NodeXYZ( 0 );
193   myNodes[ 2 ] = edgeU1.NodeXYZ( 0 );
194   myNodes[ 3 ] = edgeU1.NodeXYZ( 1 );
195   myCoordInd[ 0 ] = GetCoordIndOnEdge( edgeIdVec[ 0 ] );
196   myCoordInd[ 1 ] = GetCoordIndOnEdge( edgeIdVec[ 1 ] );
197   myCoordInd[ 2 ] = GetCoordIndOnEdge( edgeIdVec[ 2 ] );
198   myCoordInd[ 3 ] = GetCoordIndOnEdge( edgeIdVec[ 3 ] );
199   if ( myS ) delete myS;
200   myS = 0;
201 }
202
203 //================================================================================
204 /*!
205  * \brief Destructor
206  */
207 //================================================================================
208
209 SMESH_Block::TFace::~TFace()
210 {
211   if ( myS ) delete myS;
212   for ( int i = 0 ; i < 4; ++i )
213     if ( myC2d[ i ]) delete myC2d[ i ];
214 }
215
216 //=======================================================================
217 //function : SMESH_Block::TFace::GetCoefs
218 //purpose  : return coefficients for addition of [0-3]-th edge and vertex
219 //=======================================================================
220
221 void SMESH_Block::TFace::GetCoefs(int           iE,
222                                   const gp_XYZ& theParams,
223                                   double&       Ecoef,
224                                   double&       Vcoef ) const
225 {
226   double dU = theParams.Coord( GetUInd() );
227   double dV = theParams.Coord( GetVInd() );
228   switch ( iE ) {
229   case 0:
230     Ecoef = ( 1 - dV ); // u0
231     Vcoef = ( 1 - dU ) * ( 1 - dV ); break; // 00
232   case 1:
233     Ecoef = dV; // u1
234     Vcoef = dU * ( 1 - dV ); break; // 10
235   case 2:
236     Ecoef = ( 1 - dU ); // 0v
237     Vcoef = dU * dV  ; break; // 11
238   case 3:
239     Ecoef = dU  ; // 1v
240     Vcoef = ( 1 - dU ) * dV  ; break; // 01
241   default: ASSERT(0);
242   }
243 }
244
245 //=======================================================================
246 //function : SMESH_Block::TFace::GetUV
247 //purpose  : 
248 //=======================================================================
249
250 gp_XY SMESH_Block::TFace::GetUV( const gp_XYZ& theParams ) const
251 {
252   gp_XY uv(0.,0.);
253   for ( int iE = 0; iE < 4; iE++ ) // loop on 4 edges
254   {
255     double Ecoef = 0, Vcoef = 0;
256     GetCoefs( iE, theParams, Ecoef, Vcoef );
257     // edge addition
258     double u = theParams.Coord( myCoordInd[ iE ] );
259     u = ( 1 - u ) * myFirst[ iE ] + u * myLast[ iE ];
260     uv += Ecoef * myC2d[ iE ]->Value( u ).XY();
261     // corner addition
262     uv -= Vcoef * myCorner[ iE ];
263   }
264   return uv;
265 }
266
267 //=======================================================================
268 //function : SMESH_Block::TFace::Point
269 //purpose  : 
270 //=======================================================================
271
272 gp_XYZ SMESH_Block::TFace::Point( const gp_XYZ& theParams ) const
273 {
274   gp_XYZ p(0.,0.,0.);
275   if ( !myS ) // if mesh block
276   {
277     for ( int iE = 0; iE < 4; iE++ ) // loop on 4 edges
278     {
279       double Ecoef = 0, Vcoef = 0;
280       GetCoefs( iE, theParams, Ecoef, Vcoef );
281       // edge addition
282       double u = theParams.Coord( myCoordInd[ iE ] );
283       int i1 = 0, i2 = 1;
284       switch ( iE ) {
285       case 1: i1 = 3; i2 = 2; break;
286       case 2: i1 = 1; i2 = 2; break;
287       case 3: i1 = 0; i2 = 3; break;
288       }
289       p += Ecoef * ( myNodes[ i1 ] * ( 1 - u ) + myNodes[ i2 ] * u );
290       // corner addition
291       p -= Vcoef * myNodes[ iE ];
292     }
293     
294   }
295   else // shape block
296   {
297     gp_XY uv = GetUV( theParams );
298     p = myS->Value( uv.X(), uv.Y() ).XYZ();
299   }
300   return p;
301 }
302
303 //=======================================================================
304 //function : GetShapeCoef
305 //purpose  : 
306 //=======================================================================
307
308 double* SMESH_Block::GetShapeCoef (const int theShapeID)
309 {
310   static double shapeCoef[][3] = {
311     //    V000,        V100,        V010,         V110
312     { -1,-1,-1 }, {  1,-1,-1 }, { -1, 1,-1 }, {  1, 1,-1 },
313     //    V001,        V101,        V011,         V111,
314     { -1,-1, 1 }, {  1,-1, 1 }, { -1, 1, 1 }, {  1, 1, 1 },
315     //    Ex00,        Ex10,        Ex01,         Ex11,
316     {  0,-1,-1 }, {  0, 1,-1 }, {  0,-1, 1 }, {  0, 1, 1 },
317     //    E0y0,        E1y0,        E0y1,         E1y1,
318     { -1, 0,-1 }, {  1, 0,-1 }, { -1, 0, 1 }, {  1, 0, 1 },
319     //    E00z,        E10z,        E01z,         E11z,
320     { -1,-1, 0 }, {  1,-1, 0 }, { -1, 1, 0 }, {  1, 1, 0 },
321     //    Fxy0,        Fxy1,        Fx0z,         Fx1z,         F0yz,           F1yz,
322     {  0, 0,-1 }, {  0, 0, 1 }, {  0,-1, 0 }, {  0, 1, 0 }, { -1, 0, 0 }, {  1, 0, 0 },
323     // ID_Shell
324     {  0, 0, 0 }
325   };
326   if ( theShapeID < ID_V000 || theShapeID > ID_F1yz )
327     return shapeCoef[ ID_Shell - 1 ];
328
329   return shapeCoef[ theShapeID - 1 ];
330 }
331
332 //=======================================================================
333 //function : ShellPoint
334 //purpose  : return coordinates of a point in shell
335 //=======================================================================
336
337 bool SMESH_Block::ShellPoint( const gp_XYZ& theParams, gp_XYZ& thePoint ) const
338 {
339   thePoint.SetCoord( 0., 0., 0. );
340   for ( int shapeID = ID_V000; shapeID < ID_Shell; shapeID++ )
341   {
342     // coef
343     double* coefs = GetShapeCoef( shapeID );
344     double k = 1;
345     for ( int iCoef = 0; iCoef < 3; iCoef++ ) {
346       if ( coefs[ iCoef ] != 0 ) {
347         if ( coefs[ iCoef ] < 0 )
348           k *= ( 1. - theParams.Coord( iCoef + 1 ));
349         else
350           k *= theParams.Coord( iCoef + 1 );
351       }
352     }
353     // add point on a shape
354     if ( fabs( k ) > DBL_MIN )
355     {
356       gp_XYZ Ps;
357       if ( shapeID < ID_Ex00 ) // vertex
358         VertexPoint( shapeID, Ps );
359       else if ( shapeID < ID_Fxy0 ) { // edge
360         EdgePoint( shapeID, theParams, Ps );
361         k = -k;
362       } else // face
363         FacePoint( shapeID, theParams, Ps );
364
365       thePoint += k * Ps;
366     }
367   }
368   return true;
369 }
370
371 //=======================================================================
372 //function : ShellPoint
373 //purpose  : computes coordinates of a point in shell by points on sub-shapes;
374 //           thePointOnShape[ subShapeID ] must be a point on a subShape
375 //=======================================================================
376
377 bool SMESH_Block::ShellPoint(const gp_XYZ&         theParams,
378                              const vector<gp_XYZ>& thePointOnShape,
379                              gp_XYZ&               thePoint )
380 {
381   if ( thePointOnShape.size() < ID_F1yz )
382     return false;
383
384   double x = theParams.X(), y = theParams.Y(), z = theParams.Z();
385   double x1 = 1. - x,       y1 = 1. - y,       z1 = 1. - z;
386   const vector<gp_XYZ>& p = thePointOnShape;
387
388   thePoint = 
389     x1 * p[ID_F0yz] + x * p[ID_F1yz] +
390     y1 * p[ID_Fx0z] + y * p[ID_Fx1z] +
391     z1 * p[ID_Fxy0] + z * p[ID_Fxy1] +
392     x1 * (y1 * (z1 * p[ID_V000] + z * p[ID_V001])  +
393           y  * (z1 * p[ID_V010] + z * p[ID_V011])) +
394     x  * (y1 * (z1 * p[ID_V100] + z * p[ID_V101])  +
395           y  * (z1 * p[ID_V110] + z * p[ID_V111]));
396   thePoint -=
397     x1 * (y1 * p[ID_E00z] + y * p[ID_E01z]) + 
398     x  * (y1 * p[ID_E10z] + y * p[ID_E11z]) + 
399     y1 * (z1 * p[ID_Ex00] + z * p[ID_Ex01]) + 
400     y  * (z1 * p[ID_Ex10] + z * p[ID_Ex11]) + 
401     z1 * (x1 * p[ID_E0y0] + x * p[ID_E1y0]) + 
402     z  * (x1 * p[ID_E0y1] + x * p[ID_E1y1]);
403
404   return true;
405 }
406
407 //=======================================================================
408 //function : Constructor
409 //purpose  : 
410 //=======================================================================
411
412 SMESH_Block::SMESH_Block():
413   myNbIterations(0),
414   mySumDist(0.),
415   myTolerance(-1.) // to be re-initialized
416 {
417 }
418
419
420 //=======================================================================
421 //function : NbVariables
422 //purpose  : 
423 //=======================================================================
424
425 Standard_Integer SMESH_Block::NbVariables() const
426 {
427   return 3;
428 }
429
430 //=======================================================================
431 //function : NbEquations
432 //purpose  : 
433 //=======================================================================
434
435 Standard_Integer SMESH_Block::NbEquations() const
436 {
437   return 1;
438 }
439
440 //=======================================================================
441 //function : Value
442 //purpose  : 
443 //=======================================================================
444
445 Standard_Boolean SMESH_Block::Value(const math_Vector& theXYZ, math_Vector& theFxyz) 
446 {
447   gp_XYZ P, params( theXYZ(1), theXYZ(2), theXYZ(3) );
448   if ( params.IsEqual( myParam, DBL_MIN )) { // same param
449     theFxyz( 1 ) = funcValue( myValues[ SQUARE_DIST ]);
450   }
451   else {
452     ShellPoint( params, P );
453     gp_Vec dP( P - myPoint );
454     theFxyz(1) = funcValue( dP.SquareMagnitude() );
455   }
456   return true;
457 }
458
459 //=======================================================================
460 //function : Derivatives
461 //purpose  : 
462 //=======================================================================
463
464 Standard_Boolean SMESH_Block::Derivatives(const math_Vector& XYZ,math_Matrix& Df) 
465 {
466   math_Vector F(1,3);
467   return Values(XYZ,F,Df);
468 }
469
470 //=======================================================================
471 //function : GetStateNumber
472 //purpose  : 
473 //=======================================================================
474
475 Standard_Integer SMESH_Block::GetStateNumber ()
476 {
477   return 0; //myValues[0] < 1e-1;
478 }
479
480 //=======================================================================
481 //function : Values
482 //purpose  : 
483 //=======================================================================
484
485 Standard_Boolean SMESH_Block::Values(const math_Vector& theXYZ,
486                                      math_Vector&       theFxyz,
487                                      math_Matrix&       theDf) 
488 {
489   gp_XYZ P, params( theXYZ(1), theXYZ(2), theXYZ(3) );
490   if ( params.IsEqual( myParam, DBL_MIN )) { // same param
491     theFxyz( 1 )      = funcValue( myValues[ SQUARE_DIST ] );
492     theDf( 1, DRV_1 ) = myValues[ DRV_1 ];
493     theDf( 1, DRV_2 ) = myValues[ DRV_2 ];
494     theDf( 1, DRV_3 ) = myValues[ DRV_3 ];
495     return true;
496   }
497 #ifdef DEBUG_PARAM_COMPUTE
498   cout << "PARAM GUESS: " << params.X() << " "<< params.Y() << " "<< params.X() << endl;
499   myNbIterations++; // how many times call ShellPoint()
500 #endif
501   ShellPoint( params, P );
502
503   gp_Vec dP( myPoint, P );
504   double sqDist = dP.SquareMagnitude();
505   theFxyz(1) = funcValue( sqDist );
506
507   if ( sqDist < myTolerance * myTolerance ) { // a solution found
508     myParam = params;
509     myValues[ SQUARE_DIST ] = sqDist;
510     theFxyz(1)  = theDf( 1,1 ) = theDf( 1,2 ) = theDf( 1,3 ) = 0;
511     return true;
512   }
513
514   if ( sqDist < myValues[ SQUARE_DIST ] ) // a better guess
515   {
516     // 3 partial derivatives
517     gp_Vec drv[ 3 ]; // where we move with a small step in each direction
518     for ( int iP = 1; iP <= 3; iP++ ) {
519       if ( iP == myFaceIndex ) {
520         drv[ iP - 1 ] = gp_Vec(0,0,0);
521         continue;
522       }
523       gp_XYZ Pi;
524       bool onEdge = ( theXYZ( iP ) + 0.001 > 1. );
525       if ( onEdge )
526         params.SetCoord( iP, theXYZ( iP ) - 0.001 );
527       else
528         params.SetCoord( iP, theXYZ( iP ) + 0.001 );
529       ShellPoint( params, Pi );
530       params.SetCoord( iP, theXYZ( iP ) ); // restore params
531       gp_Vec dPi ( P, Pi );
532       if ( onEdge ) dPi *= -1.;
533       double mag = dPi.Magnitude();
534       if ( mag > DBL_MIN )
535         dPi /= mag;
536       drv[ iP - 1 ] = dPi;
537     }
538     for ( int iP = 0; iP < 3; iP++ ) {
539 #if 1
540       theDf( 1, iP + 1 ) = dP * drv[iP];
541 #else
542       // Distance from P to plane passing through myPoint and defined
543       // by the 2 other derivative directions:
544       // like IntAna_IntConicQuad::Perform (const gp_Lin& L, const gp_Pln& P)
545       // where L is (P -> myPoint), P is defined by the 2 other derivative direction
546       int iPrev = ( iP ? iP - 1 : 2 );
547       int iNext = ( iP == 2 ? 0 : iP + 1 );
548       gp_Vec plnNorm = drv[ iPrev ].Crossed( drv [ iNext ] );
549       double Direc = plnNorm * drv[ iP ];
550       if ( Abs(Direc) <= DBL_MIN )
551         theDf( 1, iP + 1 ) = dP * drv[ iP ];
552       else {
553         double Dis = plnNorm * P - plnNorm * myPoint;
554         theDf( 1, iP + 1 ) = Dis/Direc;
555       }
556 #endif
557     }
558 #ifdef DEBUG_PARAM_COMPUTE
559     cout << "F = " << theFxyz(1) <<
560       " DRV: " << theDf(1,1) << " " << theDf(1,2) << " " << theDf(1,3)  << endl;
561     myNbIterations +=3; // how many times call ShellPoint()
562 #endif
563
564     // store better values
565     myParam              = params;
566     myValues[SQUARE_DIST]= sqDist;
567     myValues[DRV_1]      = theDf(1,DRV_1);
568     myValues[DRV_2]      = theDf(1,DRV_2);
569     myValues[DRV_3]      = theDf(1,DRV_3);
570   }
571
572   return true;
573 }
574
575 //============================================================================
576 //function : computeParameters
577 //purpose  : compute point parameters in the block using math_FunctionSetRoot
578 //============================================================================
579
580 bool SMESH_Block::computeParameters(const gp_Pnt& thePoint,
581                                     gp_XYZ&       theParams,
582                                     const gp_XYZ& theParamsHint)
583 {
584   myPoint = thePoint.XYZ();
585
586   myParam.SetCoord( -1,-1,-1 );
587   myValues[ SQUARE_DIST ] = 1e100;
588
589   math_Vector low  ( 1, 3, 0.0 );
590   math_Vector up   ( 1, 3, 1.0 );
591   math_Vector tol  ( 1, 3, 1e-4 );
592   math_Vector start( 1, 3, 0.0 );
593   start( 1 ) = theParamsHint.X();
594   start( 2 ) = theParamsHint.Y();
595   start( 3 ) = theParamsHint.Z();
596
597   math_FunctionSetRoot paramSearch( *this, tol );
598
599   mySquareFunc = 0; // large approaching steps
600   //if ( hasHint ) mySquareFunc = 1; // small approaching steps
601
602   double loopTol = 10 * myTolerance;
603   int nbLoops = 0;
604   while ( distance() > loopTol && nbLoops <= 3 )
605   {
606     paramSearch.Perform ( *static_cast<math_FunctionSetWithDerivatives*>(this),
607                           start, low, up );
608     start( 1 ) = myParam.X();
609     start( 2 ) = myParam.Y();
610     start( 3 ) = myParam.Z();
611     mySquareFunc = !mySquareFunc;
612     nbLoops++;
613   }
614 #ifdef DEBUG_PARAM_COMPUTE
615   mySumDist += distance();
616   cout << " ------ SOLUTION: ( "<< myParam.X() <<" "<< myParam.Y() <<" "<< myParam.Z() <<" )"<<endl
617        << " ------ DIST : " << distance() << "\t Tol=" << myTolerance << "\t Nb LOOPS=" << nbLoops << endl
618        << " ------ NB IT: " << myNbIterations << ",  SUM DIST: " << mySumDist << endl;
619 #endif
620
621   theParams = myParam;
622
623   if ( myFaceIndex > 0 )
624     theParams.SetCoord( myFaceIndex, myFaceParam );
625
626   return true;
627 }
628
629 //=======================================================================
630 //function : ComputeParameters
631 //purpose  : compute point parameters in the block
632 //=======================================================================
633
634 bool SMESH_Block::ComputeParameters(const gp_Pnt& thePoint,
635                                     gp_XYZ&       theParams,
636                                     const int     theShapeID,
637                                     const gp_XYZ& theParamsHint)
638 {
639   if ( VertexParameters( theShapeID, theParams ))
640     return true;
641
642   if ( IsEdgeID( theShapeID )) {
643     TEdge& e = myEdge[ theShapeID - ID_FirstE ];
644     Adaptor3d_Curve* curve = e.GetCurve();
645     Extrema_ExtPC anExtPC( thePoint, *curve, curve->FirstParameter(), curve->LastParameter() );
646     int i, nb = anExtPC.IsDone() ? anExtPC.NbExt() : 0;
647     for ( i = 1; i <= nb; i++ ) {
648       if ( anExtPC.IsMin( i ))
649         return EdgeParameters( theShapeID, anExtPC.Point( i ).Parameter(), theParams );
650     }
651     return false;
652   }
653
654   const bool isOnFace = IsFaceID( theShapeID );
655   double * coef = GetShapeCoef( theShapeID );
656
657   // Find the first guess paremeters
658
659   gp_XYZ start(0, 0, 0);
660
661   bool hasHint = ( 0 <= theParamsHint.X() && theParamsHint.X() <= 1 &&
662                    0 <= theParamsHint.Y() && theParamsHint.Y() <= 1 &&
663                    0 <= theParamsHint.Y() && theParamsHint.Y() <= 1 );
664   if ( !hasHint && !myGridComputed )
665   {
666     // define the first guess by thePoint projection on lines
667     // connecting vertices
668     bool needGrid = false;
669     gp_XYZ par000( 0, 0, 0 ), par111( 1, 1, 1 );
670     double zero = DBL_MIN * DBL_MIN;
671     for ( int iEdge = 0, iParam = 1; iParam <= 3 && !needGrid; iParam++ )
672     {
673       if ( isOnFace && coef[ iParam - 1 ] != 0 ) {
674         iEdge += 4;
675         continue;
676       }
677       double sumParam = 0;
678       for ( int iE = 0; iE < 4; iE++, iEdge++ ) { // loop on 4 parallel edges
679         gp_Pnt p0 = myEdge[ iEdge ].Point( par000 );
680         gp_Pnt p1 = myEdge[ iEdge ].Point( par111 );
681         gp_Vec v01( p0, p1 ), v0P( p0, thePoint );
682         double len2 = v01.SquareMagnitude();
683         double par = 0;
684         if ( len2 > zero ) {
685           par = v0P.Dot( v01 ) / len2;
686           if ( par < 0 || par > 1 ) { // projection falls out of line ends => needGrid
687             needGrid = true;
688             break;
689           }
690         }
691         sumParam += par;
692       }
693       start.SetCoord( iParam, sumParam / 4.);
694     }
695     if ( needGrid ) {
696       // compute nodes of 3 x 3 x 3 grid
697       int iNode = 0;
698       Bnd_Box box;
699       for ( double x = 0.25; x < 0.9; x += 0.25 )
700         for ( double y = 0.25; y < 0.9; y += 0.25 )
701           for ( double z = 0.25; z < 0.9; z += 0.25 ) {
702             TxyzPair & prmPtn = my3x3x3GridNodes[ iNode++ ];
703             prmPtn.first.SetCoord( x, y, z );
704             ShellPoint( prmPtn.first, prmPtn.second );
705             box.Add( gp_Pnt( prmPtn.second ));
706           }
707       myGridComputed = true;
708       myTolerance = sqrt( box.SquareExtent() ) * 1e-5;
709     }
710   }
711
712   if ( hasHint )
713   {
714     start = theParamsHint;
715   }
716   else if ( myGridComputed )
717   {
718     double minDist = DBL_MAX;
719     gp_XYZ* bestParam = 0;
720     for ( int iNode = 0; iNode < 27; iNode++ ) {
721       TxyzPair & prmPtn = my3x3x3GridNodes[ iNode ];
722       double dist = ( thePoint.XYZ() - prmPtn.second ).SquareModulus();
723       if ( dist < minDist ) {
724         minDist = dist;
725         bestParam = & prmPtn.first;
726       }
727     }
728     start = *bestParam;
729   }
730
731   myFaceIndex = -1;
732   myFaceParam = 0.;
733   if ( isOnFace ) {
734     // put a point on the face
735     for ( int iCoord = 0; iCoord < 3; iCoord++ )
736       if ( coef[ iCoord ] ) {
737         myFaceIndex = iCoord + 1;
738         myFaceParam = ( coef[ iCoord ] < 0.5 ) ? 0.0 : 1.0;
739         start.SetCoord( myFaceIndex, myFaceParam );
740       }
741   }
742
743 #ifdef DEBUG_PARAM_COMPUTE
744   cout << " #### POINT " <<thePoint.X()<<" "<<thePoint.Y()<<" "<<thePoint.Z()<<" ####"<< endl;
745 #endif
746
747   if ( myTolerance < 0 ) myTolerance = 1e-6;
748
749   const double parDelta = 1e-4;
750   const double sqTolerance = myTolerance * myTolerance;
751
752   gp_XYZ solution = start, params = start;
753   double sqDistance = 1e100; 
754   int nbLoops = 0, nbGetWorst = 0;
755
756   while ( nbLoops <= 100 )
757   {
758     gp_XYZ P, Pi;
759     ShellPoint( params, P );
760
761     gp_Vec dP( thePoint, P );
762     double sqDist = dP.SquareMagnitude();
763
764     if ( sqDist > sqDistance ) { // solution get worse
765       if ( ++nbGetWorst > 2 )
766         return computeParameters( thePoint, theParams, solution );
767     }
768 #ifdef DEBUG_PARAM_COMPUTE
769     cout << "PARAMS: ( " << params.X() <<" "<< params.Y() <<" "<< params.Z() <<" )"<< endl;
770     cout << "DIST: " << sqrt( sqDist ) << endl;
771 #endif
772
773     if ( sqDist < sqDistance ) { // get better
774       sqDistance = sqDist;
775       solution   = params;
776       nbGetWorst = 0;
777       if ( sqDistance < sqTolerance ) // a solution found
778         break;
779     }
780
781         // look for a next better solution
782     for ( int iP = 1; iP <= 3; iP++ ) {
783       if ( iP == myFaceIndex )
784         continue;
785       // see where we move with a small (=parDelta) step in this direction
786       gp_XYZ nearParams = params;
787       bool onEdge = ( params.Coord( iP ) + parDelta > 1. );
788       if ( onEdge )
789         nearParams.SetCoord( iP, params.Coord( iP ) - parDelta );
790       else
791         nearParams.SetCoord( iP, params.Coord( iP ) + parDelta );
792       ShellPoint( nearParams, Pi );
793       gp_Vec dPi ( P, Pi );
794       if ( onEdge ) dPi *= -1.;
795       // modify a parameter
796       double mag = dPi.Magnitude();
797       if ( mag < DBL_MIN )
798         continue;
799       gp_Vec dir = dPi / mag; // dir we move modifying the parameter
800       double dist = dir * dP; // where we should get to
801       double dPar = dist / mag * parDelta; // predict parameter change
802       double curPar = params.Coord( iP );
803       double par = curPar - dPar; // new parameter value
804       while ( par > 1 || par < 0 ) {
805         dPar /= 2.;
806         par = curPar - dPar;
807       }
808       params.SetCoord( iP, par );
809     }
810
811     nbLoops++;
812   }
813 #ifdef DEBUG_PARAM_COMPUTE
814   myNbIterations += nbLoops*4; // how many times ShellPoint called
815   mySumDist += sqrt( sqDistance );
816   cout << " ------ SOLUTION: ( "<<solution.X()<<" "<<solution.Y()<<" "<<solution.Z()<<" )"<<endl
817        << " ------ DIST : " << sqrt( sqDistance ) << "\t Tol=" << myTolerance << "\t Nb LOOPS=" << nbLoops << endl
818        << " ------ NB IT: " << myNbIterations << ",  SUM DIST: " << mySumDist << endl;
819 #endif
820
821   theParams = solution;
822
823   if ( myFaceIndex > 0 )
824     theParams.SetCoord( myFaceIndex, myFaceParam );
825
826   return true;
827 }
828
829 //=======================================================================
830 //function : VertexParameters
831 //purpose  : return parameters of a vertex given by TShapeID
832 //=======================================================================
833
834 bool SMESH_Block::VertexParameters(const int theVertexID, gp_XYZ& theParams)
835 {
836   switch ( theVertexID ) {
837   case ID_V000: theParams.SetCoord(0., 0., 0.); return true;
838   case ID_V100: theParams.SetCoord(1., 0., 0.); return true;
839   case ID_V110: theParams.SetCoord(1., 1., 0.); return true;
840   case ID_V010: theParams.SetCoord(0., 1., 0.); return true;
841   default:;
842   }
843   return false;
844 }
845
846 //=======================================================================
847 //function : EdgeParameters
848 //purpose  : return parameters of a point given by theU on edge
849 //=======================================================================
850
851 bool SMESH_Block::EdgeParameters(const int theEdgeID, const double theU, gp_XYZ& theParams)
852 {
853   if ( IsEdgeID( theEdgeID )) {
854     vector< int > vertexVec;
855     GetEdgeVertexIDs( theEdgeID, vertexVec );
856     VertexParameters( vertexVec[0], theParams );
857     TEdge& e = myEdge[ theEdgeID - ID_Ex00 ];
858     double param = ( theU - e.EndParam(0) ) / ( e.EndParam(1) - e.EndParam(0) );
859     theParams.SetCoord( e.CoordInd(), param );
860     return true;
861   }
862   return false;
863 }
864
865 //=======================================================================
866 //function : DumpShapeID
867 //purpose  : debug an id of a block sub-shape
868 //=======================================================================
869
870 #define CASEDUMP(id,strm) case id: strm << #id; break;
871
872 ostream& SMESH_Block::DumpShapeID (const int id, ostream& stream)
873 {
874   switch ( id ) {
875   CASEDUMP( ID_V000, stream );
876   CASEDUMP( ID_V100, stream );
877   CASEDUMP( ID_V010, stream );
878   CASEDUMP( ID_V110, stream );
879   CASEDUMP( ID_V001, stream );
880   CASEDUMP( ID_V101, stream );
881   CASEDUMP( ID_V011, stream );
882   CASEDUMP( ID_V111, stream );
883   CASEDUMP( ID_Ex00, stream );
884   CASEDUMP( ID_Ex10, stream );
885   CASEDUMP( ID_Ex01, stream );
886   CASEDUMP( ID_Ex11, stream );
887   CASEDUMP( ID_E0y0, stream );
888   CASEDUMP( ID_E1y0, stream );
889   CASEDUMP( ID_E0y1, stream );
890   CASEDUMP( ID_E1y1, stream );
891   CASEDUMP( ID_E00z, stream );
892   CASEDUMP( ID_E10z, stream );
893   CASEDUMP( ID_E01z, stream );
894   CASEDUMP( ID_E11z, stream );
895   CASEDUMP( ID_Fxy0, stream );
896   CASEDUMP( ID_Fxy1, stream );
897   CASEDUMP( ID_Fx0z, stream );
898   CASEDUMP( ID_Fx1z, stream );
899   CASEDUMP( ID_F0yz, stream );
900   CASEDUMP( ID_F1yz, stream );
901   CASEDUMP( ID_Shell, stream );
902   default: stream << "ID_INVALID";
903   }
904   return stream;
905 }
906
907 //=======================================================================
908 //function : GetShapeIDByParams
909 //purpose  : define an id of the block sub-shape by normlized point coord
910 //=======================================================================
911
912 int SMESH_Block::GetShapeIDByParams ( const gp_XYZ& theCoord )
913 {
914   //   id ( 0 - 26 ) computation:
915
916   //   vertex     ( 0 - 7 )  : id = 1*x + 2*y + 4*z
917
918   //   edge || X  ( 8 - 11 ) : id = 8   + 1*y + 2*z
919   //   edge || Y  ( 12 - 15 ): id = 1*x + 12  + 2*z
920   //   edge || Z  ( 16 - 19 ): id = 1*x + 2*y + 16 
921
922   //   face || XY ( 20 - 21 ): id = 8   + 12  + 1*z - 0
923   //   face || XZ ( 22 - 23 ): id = 8   + 1*y + 16  - 2
924   //   face || YZ ( 24 - 25 ): id = 1*x + 12  + 16  - 4
925
926   static int iAddBnd[]    = { 1, 2, 4 };
927   static int iAddNotBnd[] = { 8, 12, 16 };
928   static int iFaceSubst[] = { 0, 2, 4 };
929
930   int id = 0;
931   int iOnBoundary = 0;
932   for ( int iCoord = 0; iCoord < 3; iCoord++ )
933   {
934     double val = theCoord.Coord( iCoord + 1 );
935     if ( val == 0.0 )
936       iOnBoundary++;
937     else if ( val == 1.0 )
938       id += iAddBnd[ iOnBoundary++ ];
939     else
940       id += iAddNotBnd[ iCoord ];
941   }
942   if ( iOnBoundary == 1 ) // face
943     id -= iFaceSubst[ (id - 20) / 4 ];
944   else if ( iOnBoundary == 0 ) // shell
945     id = 26;
946
947   if ( id > 26 || id < 0 ) {
948     MESSAGE( "GetShapeIDByParams() = " << id
949             <<" "<< theCoord.X() <<" "<< theCoord.Y() <<" "<< theCoord.Z() );
950   }
951
952   return id + 1; // shape ids start at 1
953 }
954
955 //=======================================================================
956 //function : GetOrderedEdges
957 //purpose  : return nb wires and a list of oredered edges
958 //=======================================================================
959
960 int SMESH_Block::GetOrderedEdges (const TopoDS_Face&   theFace,
961                                   TopoDS_Vertex        theFirstVertex,
962                                   list< TopoDS_Edge >& theEdges,
963                                   list< int >  &       theNbVertexInWires)
964 {
965   // put wires in a list, so that an outer wire comes first
966   list<TopoDS_Wire> aWireList;
967   TopoDS_Wire anOuterWire = BRepTools::OuterWire( theFace );
968   aWireList.push_back( anOuterWire );
969   for ( TopoDS_Iterator wIt (theFace); wIt.More(); wIt.Next() )
970     if ( !anOuterWire.IsSame( wIt.Value() ))
971       aWireList.push_back( TopoDS::Wire( wIt.Value() ));
972
973   // loop on edges of wires
974   theNbVertexInWires.clear();
975   list<TopoDS_Wire>::iterator wlIt = aWireList.begin();
976   for ( ; wlIt != aWireList.end(); wlIt++ )
977   {
978     int iE;
979     BRepTools_WireExplorer wExp( *wlIt, theFace );
980     for ( iE = 0; wExp.More(); wExp.Next(), iE++ )
981     {
982       TopoDS_Edge edge = wExp.Current();
983       edge = TopoDS::Edge( edge.Oriented( wExp.Orientation() ));
984       theEdges.push_back( edge );
985     }
986     theNbVertexInWires.push_back( iE );
987     iE = 0;
988     if ( wlIt == aWireList.begin() && theEdges.size() > 1 ) { // the outer wire
989       // orient closed edges
990       list< TopoDS_Edge >::iterator eIt, eIt2;
991       for ( eIt = theEdges.begin(); eIt != theEdges.end(); eIt++ )
992       {
993         TopoDS_Edge& edge = *eIt;
994         if ( TopExp::FirstVertex( edge ).IsSame( TopExp::LastVertex( edge ) ))
995         {
996           eIt2 = eIt;
997           bool isNext = ( eIt2 == theEdges.begin() );
998           TopoDS_Edge edge2 = isNext ? *(++eIt2) : *(--eIt2);
999           double f1,l1,f2,l2;
1000           Handle(Geom2d_Curve) c1 = BRep_Tool::CurveOnSurface( edge, theFace, f1,l1 );
1001           Handle(Geom2d_Curve) c2 = BRep_Tool::CurveOnSurface( edge2, theFace, f2,l2 );
1002           gp_Pnt2d pf = c1->Value( edge.Orientation() == TopAbs_FORWARD ? f1 : l1 );
1003           gp_Pnt2d pl = c1->Value( edge.Orientation() == TopAbs_FORWARD ? l1 : f1 );
1004           bool isFirst = ( edge2.Orientation() == TopAbs_FORWARD ? isNext : !isNext );
1005           gp_Pnt2d p2 = c2->Value( isFirst ? f2 : l2 );
1006           isFirst = ( p2.SquareDistance( pf ) < p2.SquareDistance( pl ));
1007           if ( isNext ? isFirst : !isFirst )
1008             edge.Reverse();
1009           // to make a seam go first
1010           if ( theFirstVertex.IsNull() )
1011             theFirstVertex = TopExp::FirstVertex( edge, true );
1012         }
1013       }
1014       // rotate theEdges until it begins from theFirstVertex
1015       if ( ! theFirstVertex.IsNull() ) {
1016         TopoDS_Vertex vv[2];
1017         TopExp::Vertices( theEdges.front(), vv[0], vv[1], true );
1018         // on closed face, make seam edge the first in the list
1019         while ( !vv[0].IsSame( theFirstVertex ) || vv[0].IsSame( vv[1] ))
1020         {
1021           theEdges.splice(theEdges.end(), theEdges,
1022                           theEdges.begin(), ++theEdges.begin());
1023           TopExp::Vertices( theEdges.front(), vv[0], vv[1], true );
1024           if ( iE++ > theNbVertexInWires.back() ) {
1025 #ifdef _DEBUG_
1026             gp_Pnt p = BRep_Tool::Pnt( theFirstVertex );
1027             cout << " : Warning : vertex "<< theFirstVertex.TShape().operator->()
1028                  << " ( " << p.X() << " " << p.Y() << " " << p.Z() << " )" 
1029                  << " not found in outer wire of face "<< theFace.TShape().operator->()
1030                  << " with vertices: " <<  endl;
1031             wExp.Init( *wlIt, theFace );
1032             for ( int i = 0; wExp.More(); wExp.Next(), i++ )
1033             {
1034               TopoDS_Edge edge = wExp.Current();
1035               edge = TopoDS::Edge( edge.Oriented( wExp.Orientation() ));
1036               TopoDS_Vertex v = TopExp::FirstVertex( edge, true );
1037               gp_Pnt p = BRep_Tool::Pnt( v );
1038               cout << i << " " << v.TShape().operator->() << " "
1039                    << p.X() << " " << p.Y() << " " << p.Z() << " " << endl;
1040             }
1041 #endif
1042             break; // break infinite loop
1043           }
1044         }
1045       }
1046     } // end outer wire
1047   }
1048
1049   return aWireList.size();
1050 }
1051 //================================================================================
1052 /*!
1053  * \brief Call it after geometry initialisation
1054  */
1055 //================================================================================
1056
1057 void SMESH_Block::init()
1058 {
1059   myNbIterations = 0;
1060   mySumDist = 0;
1061   myGridComputed = false;
1062 }
1063
1064 //=======================================================================
1065 //function : LoadMeshBlock
1066 //purpose  : prepare to work with theVolume
1067 //=======================================================================
1068
1069 #define gpXYZ(n) gp_XYZ(n->X(),n->Y(),n->Z())
1070
1071 bool SMESH_Block::LoadMeshBlock(const SMDS_MeshVolume*        theVolume,
1072                                 const int                     theNode000Index,
1073                                 const int                     theNode001Index,
1074                                 vector<const SMDS_MeshNode*>& theOrderedNodes)
1075 {
1076   MESSAGE(" ::LoadMeshBlock()");
1077   init();
1078
1079   SMDS_VolumeTool vTool;
1080   if (!vTool.Set( theVolume ) || vTool.NbNodes() != 8 ||
1081       !vTool.IsLinked( theNode000Index, theNode001Index )) {
1082     MESSAGE(" Bad arguments ");
1083     return false;
1084   }
1085   vTool.SetExternalNormal();
1086   // In terms of indices used for access to nodes and faces in SMDS_VolumeTool:
1087   int V000, V100, V010, V110, V001, V101, V011, V111; // 8 vertices
1088   int Fxy0, Fxy1; // bottom and top faces
1089   // vertices of faces
1090   vector<int> vFxy0, vFxy1;
1091
1092   V000 = theNode000Index;
1093   V001 = theNode001Index;
1094
1095   // get faces sharing V000 and V001
1096   list<int> fV000, fV001;
1097   int i, iF, iE, iN;
1098   for ( iF = 0; iF < vTool.NbFaces(); ++iF ) {
1099     const int* nid = vTool.GetFaceNodesIndices( iF );
1100     for ( iN = 0; iN < 4; ++iN )
1101       if ( nid[ iN ] == V000 ) {
1102         fV000.push_back( iF );
1103       } else if ( nid[ iN ] == V001 ) {
1104         fV001.push_back( iF );
1105       }
1106   }
1107
1108   // find the bottom (Fxy0), the top (Fxy1) faces
1109   list<int>::iterator fIt1, fIt2, Fxy0Pos;
1110   for ( fIt1 = fV000.begin(); fIt1 != fV000.end(); fIt1++) {
1111     fIt2 = std::find( fV001.begin(), fV001.end(), *fIt1 );
1112     if ( fIt2 != fV001.end() ) { // *fIt1 is in the both lists
1113       fV001.erase( fIt2 ); // erase Fx0z or F0yz from fV001
1114     } else { // *fIt1 is in fV000 only
1115       Fxy0Pos = fIt1; // points to Fxy0
1116     }
1117   }
1118   Fxy0 = *Fxy0Pos;
1119   Fxy1 = fV001.front();
1120   const SMDS_MeshNode** nn = vTool.GetNodes();
1121
1122   // find bottom veritices, their order is that a face normal is external
1123   vFxy0.resize(4);
1124   const int* nid = vTool.GetFaceNodesIndices( Fxy0 );
1125   for ( i = 0; i < 4; ++i )
1126     if ( nid[ i ] == V000 )
1127       break;
1128   for ( iN = 0; iN < 4; ++iN, ++i ) {
1129     if ( i == 4 ) i = 0;
1130     vFxy0[ iN ] = nid[ i ];
1131   }
1132   // find top veritices, their order is that a face normal is external
1133   vFxy1.resize(4);
1134   nid = vTool.GetFaceNodesIndices( Fxy1 );
1135   for ( i = 0; i < 4; ++i )
1136     if ( nid[ i ] == V001 )
1137       break;
1138   for ( iN = 0; iN < 4; ++iN, ++i ) {
1139     if ( i == 4 ) i = 0;
1140     vFxy1[ iN ] = nid[ i ];
1141   }
1142   // find indices of the rest veritices 
1143   V100 = vFxy0[3];
1144   V010 = vFxy0[1];
1145   V110 = vFxy0[2];
1146   V101 = vFxy1[1];
1147   V011 = vFxy1[3];
1148   V111 = vFxy1[2];
1149
1150   // set points coordinates
1151   myPnt[ ID_V000 - 1 ] = gpXYZ( nn[ V000 ] );
1152   myPnt[ ID_V100 - 1 ] = gpXYZ( nn[ V100 ] );
1153   myPnt[ ID_V010 - 1 ] = gpXYZ( nn[ V010 ] );
1154   myPnt[ ID_V110 - 1 ] = gpXYZ( nn[ V110 ] );
1155   myPnt[ ID_V001 - 1 ] = gpXYZ( nn[ V001 ] );
1156   myPnt[ ID_V101 - 1 ] = gpXYZ( nn[ V101 ] );
1157   myPnt[ ID_V011 - 1 ] = gpXYZ( nn[ V011 ] );
1158   myPnt[ ID_V111 - 1 ] = gpXYZ( nn[ V111 ] );
1159
1160   // fill theOrderedNodes
1161   theOrderedNodes.resize( 8 );
1162   theOrderedNodes[ 0 ] = nn[ V000 ];
1163   theOrderedNodes[ 1 ] = nn[ V100 ];
1164   theOrderedNodes[ 2 ] = nn[ V010 ];
1165   theOrderedNodes[ 3 ] = nn[ V110 ];
1166   theOrderedNodes[ 4 ] = nn[ V001 ];
1167   theOrderedNodes[ 5 ] = nn[ V101 ];
1168   theOrderedNodes[ 6 ] = nn[ V011 ];
1169   theOrderedNodes[ 7 ] = nn[ V111 ];
1170   
1171   // fill edges
1172   vector< int > vertexVec;
1173   for ( iE = 0; iE < NbEdges(); ++iE ) {
1174     GetEdgeVertexIDs(( iE + ID_FirstE ), vertexVec );
1175     myEdge[ iE ].Set(( iE + ID_FirstE ),
1176                      myPnt[ vertexVec[0] - 1 ],
1177                      myPnt[ vertexVec[1] - 1 ]);
1178   }
1179
1180   // fill faces' corners
1181   for ( iF = ID_Fxy0; iF < ID_Shell; ++iF )
1182   {
1183     TFace& tFace = myFace[ iF - ID_FirstF ];
1184     vector< int > edgeIdVec(4, -1);
1185     GetFaceEdgesIDs( iF, edgeIdVec );
1186     tFace.Set( iF, myEdge[ edgeIdVec [ 0 ] - ID_Ex00], myEdge[ edgeIdVec [ 1 ] - ID_Ex00]);
1187   }
1188
1189   return true;
1190 }
1191
1192 //=======================================================================
1193 //function : LoadBlockShapes
1194 //purpose  : Initialize block geometry with theShell,
1195 //           add sub-shapes of theBlock to theShapeIDMap so that they get
1196 //           IDs acoording to enum TShapeID
1197 //=======================================================================
1198
1199 bool SMESH_Block::LoadBlockShapes(const TopoDS_Shell&         theShell,
1200                                   const TopoDS_Vertex&        theVertex000,
1201                                   const TopoDS_Vertex&        theVertex001,
1202                                   TopTools_IndexedMapOfOrientedShape& theShapeIDMap )
1203 {
1204   MESSAGE(" ::LoadBlockShapes()");
1205   return ( FindBlockShapes( theShell, theVertex000, theVertex001, theShapeIDMap ) &&
1206            LoadBlockShapes( theShapeIDMap ));
1207 }
1208
1209 //=======================================================================
1210 //function : LoadBlockShapes
1211 //purpose  : add sub-shapes of theBlock to theShapeIDMap so that they get
1212 //           IDs acoording to enum TShapeID
1213 //=======================================================================
1214
1215 bool SMESH_Block::FindBlockShapes(const TopoDS_Shell&         theShell,
1216                                   const TopoDS_Vertex&        theVertex000,
1217                                   const TopoDS_Vertex&        theVertex001,
1218                                   TopTools_IndexedMapOfOrientedShape& theShapeIDMap )
1219 {
1220   MESSAGE(" ::FindBlockShapes()");
1221
1222   // 8 vertices
1223   TopoDS_Shape V000, V100, V010, V110, V001, V101, V011, V111;
1224   // 12 edges
1225   TopoDS_Shape Ex00, Ex10, Ex01, Ex11;
1226   TopoDS_Shape E0y0, E1y0, E0y1, E1y1;
1227   TopoDS_Shape E00z, E10z, E01z, E11z;
1228   // 6 faces
1229   TopoDS_Shape Fxy0, Fx0z, F0yz, Fxy1, Fx1z, F1yz;
1230
1231   // nb of faces bound to a vertex in TopTools_IndexedDataMapOfShapeListOfShape
1232   // filled by TopExp::MapShapesAndAncestors()
1233   const int NB_FACES_BY_VERTEX = 6;
1234
1235   TopTools_IndexedDataMapOfShapeListOfShape vfMap;
1236   TopExp::MapShapesAndAncestors( theShell, TopAbs_VERTEX, TopAbs_FACE, vfMap );
1237   if ( vfMap.Extent() != 8 ) {
1238     MESSAGE(" Wrong nb of vertices in the block: " << vfMap.Extent() );
1239     return false;
1240   }
1241
1242   V000 = theVertex000;
1243   V001 = theVertex001;
1244
1245   if ( V000.IsNull() ) {
1246     // find vertex 000 - the one with smallest coordinates
1247     double minVal = DBL_MAX, minX, val;
1248     for ( int i = 1; i <= 8; i++ ) {
1249       const TopoDS_Vertex& v = TopoDS::Vertex( vfMap.FindKey( i ));
1250       gp_Pnt P = BRep_Tool::Pnt( v );
1251       val = P.X() + P.Y() + P.Z();
1252       if ( val < minVal || ( val == minVal && P.X() < minX )) {
1253         V000 = v;
1254         minVal = val;
1255         minX = P.X();
1256       }
1257     }
1258     // find vertex 001 - the one on the most vertical edge passing through V000
1259     TopTools_IndexedDataMapOfShapeListOfShape veMap;
1260     TopExp::MapShapesAndAncestors( theShell, TopAbs_VERTEX, TopAbs_EDGE, veMap );
1261     gp_Vec dir001 = gp::DZ();
1262     gp_Pnt p000 = BRep_Tool::Pnt( TopoDS::Vertex( V000 ));
1263     double maxVal = -DBL_MAX;
1264     TopTools_ListIteratorOfListOfShape eIt ( veMap.FindFromKey( V000 ));
1265     for (  ; eIt.More(); eIt.Next() ) {
1266       const TopoDS_Edge& e = TopoDS::Edge( eIt.Value() );
1267       TopoDS_Vertex v = TopExp::FirstVertex( e );
1268       if ( v.IsSame( V000 ))
1269         v = TopExp::LastVertex( e );
1270       val = dir001 * gp_Vec( p000, BRep_Tool::Pnt( v )).Normalized();
1271       if ( val > maxVal ) {
1272         V001 = v;
1273         maxVal = val;
1274       }
1275     }
1276   }
1277
1278   // find the bottom (Fxy0), Fx0z and F0yz faces
1279
1280   const TopTools_ListOfShape& f000List = vfMap.FindFromKey( V000 );
1281   const TopTools_ListOfShape& f001List = vfMap.FindFromKey( V001 );
1282   if (f000List.Extent() != NB_FACES_BY_VERTEX ||
1283       f001List.Extent() != NB_FACES_BY_VERTEX ) {
1284     MESSAGE(" LoadBlockShapes() " << f000List.Extent() << " " << f001List.Extent());
1285     return false;
1286   }
1287   TopTools_ListIteratorOfListOfShape f001It, f000It ( f000List );
1288   int i, j, iFound1, iFound2;
1289   for ( j = 0; f000It.More(); f000It.Next(), j++ )
1290   {
1291     if ( NB_FACES_BY_VERTEX == 6 && j % 2 ) continue; // each face encounters twice
1292     const TopoDS_Shape& F = f000It.Value();
1293     for ( i = 0, f001It.Initialize( f001List ); f001It.More(); f001It.Next(), i++ ) {
1294       if ( NB_FACES_BY_VERTEX == 6 && i % 2 ) continue; // each face encounters twice
1295       if ( F.IsSame( f001It.Value() ))
1296         break;
1297     }
1298     if ( f001It.More() ) // Fx0z or F0yz found
1299       if ( Fx0z.IsNull() ) {
1300         Fx0z = F;
1301         iFound1 = i;
1302       } else {
1303         F0yz = F;
1304         iFound2 = i;
1305       }
1306     else // F is the bottom face
1307       Fxy0 = F;
1308   }
1309   if ( Fxy0.IsNull() || Fx0z.IsNull() || F0yz.IsNull() ) {
1310     MESSAGE( Fxy0.IsNull() <<" "<< Fx0z.IsNull() <<" "<< F0yz.IsNull() );
1311     return false;
1312   }
1313
1314   // choose the top face (Fxy1)
1315   for ( i = 0, f001It.Initialize( f001List ); f001It.More(); f001It.Next(), i++ ) {
1316     if ( NB_FACES_BY_VERTEX == 6 && i % 2 ) continue; // each face encounters twice
1317     if ( i != iFound1 && i != iFound2 )
1318       break;
1319   }
1320   Fxy1 = f001It.Value();
1321   if ( Fxy1.IsNull() ) {
1322     MESSAGE(" LoadBlockShapes() error ");
1323     return false;
1324   }
1325
1326   // find bottom edges and veritices
1327   list< TopoDS_Edge > eList;
1328   list< int >         nbVertexInWires;
1329   GetOrderedEdges( TopoDS::Face( Fxy0 ), TopoDS::Vertex( V000 ), eList, nbVertexInWires );
1330   if ( nbVertexInWires.size() != 1 || nbVertexInWires.front() != 4 ) {
1331     MESSAGE(" LoadBlockShapes() error ");
1332     return false;
1333   }
1334   list< TopoDS_Edge >::iterator elIt = eList.begin();
1335   for ( i = 0; elIt != eList.end(); elIt++, i++ )
1336     switch ( i ) {
1337     case 0: E0y0 = *elIt; V010 = TopExp::LastVertex( *elIt, true ); break;
1338     case 1: Ex10 = *elIt; V110 = TopExp::LastVertex( *elIt, true ); break;
1339     case 2: E1y0 = *elIt; V100 = TopExp::LastVertex( *elIt, true ); break;
1340     case 3: Ex00 = *elIt; break;
1341     default:;
1342     }
1343   if ( i != 4 || E0y0.IsNull() || Ex10.IsNull() || E1y0.IsNull() || Ex00.IsNull() ) {
1344     MESSAGE(" LoadBlockShapes() error, eList.size()=" << eList.size());
1345     return false;
1346   }
1347
1348
1349   // find top edges and veritices
1350   eList.clear();
1351   GetOrderedEdges( TopoDS::Face( Fxy1 ), TopoDS::Vertex( V001 ), eList, nbVertexInWires );
1352   if ( nbVertexInWires.size() != 1 || nbVertexInWires.front() != 4 ) {
1353     MESSAGE(" LoadBlockShapes() error ");
1354     return false;
1355   }
1356   for ( i = 0, elIt = eList.begin(); elIt != eList.end(); elIt++, i++ )
1357     switch ( i ) {
1358     case 0: Ex01 = *elIt; V101 = TopExp::LastVertex( *elIt, true ); break;
1359     case 1: E1y1 = *elIt; V111 = TopExp::LastVertex( *elIt, true ); break;
1360     case 2: Ex11 = *elIt; V011 = TopExp::LastVertex( *elIt, true ); break;
1361     case 3: E0y1 = *elIt; break;
1362     default:;
1363     }
1364   if ( i != 4 || Ex01.IsNull() || E1y1.IsNull() || Ex11.IsNull() || E0y1.IsNull() ) {
1365     MESSAGE(" LoadBlockShapes() error, eList.size()=" << eList.size());
1366     return false;
1367   }
1368
1369   // swap Fx0z and F0yz if necessary
1370   TopExp_Explorer exp( Fx0z, TopAbs_VERTEX );
1371   for ( ; exp.More(); exp.Next() ) // Fx0z shares V101 and V100
1372     if ( V101.IsSame( exp.Current() ) || V100.IsSame( exp.Current() ))
1373       break; // V101 or V100 found
1374   if ( !exp.More() ) { // not found
1375     std::swap( Fx0z, F0yz);
1376   }
1377
1378   // find Fx1z and F1yz faces
1379   const TopTools_ListOfShape& f111List = vfMap.FindFromKey( V111 );
1380   const TopTools_ListOfShape& f110List = vfMap.FindFromKey( V110 );
1381   if (f111List.Extent() != NB_FACES_BY_VERTEX ||
1382       f110List.Extent() != NB_FACES_BY_VERTEX ) {
1383     MESSAGE(" LoadBlockShapes() " << f111List.Extent() << " " << f110List.Extent());
1384     return false;
1385   }
1386   TopTools_ListIteratorOfListOfShape f111It, f110It ( f110List);
1387   for ( j = 0 ; f110It.More(); f110It.Next(), j++ ) {
1388     if ( NB_FACES_BY_VERTEX == 6 && j % 2 ) continue; // each face encounters twice
1389     const TopoDS_Shape& F = f110It.Value();
1390     for ( i = 0, f111It.Initialize( f111List ); f111It.More(); f111It.Next(), i++ ) {
1391       if ( NB_FACES_BY_VERTEX == 6 && i % 2 ) continue; // each face encounters twice
1392       if ( F.IsSame( f111It.Value() )) { // Fx1z or F1yz found
1393         if ( Fx1z.IsNull() )
1394           Fx1z = F;
1395         else
1396           F1yz = F;
1397       }
1398     }
1399   }
1400   if ( Fx1z.IsNull() || F1yz.IsNull() ) {
1401     MESSAGE(" LoadBlockShapes() error ");
1402     return false;
1403   }
1404
1405   // swap Fx1z and F1yz if necessary
1406   for ( exp.Init( Fx1z, TopAbs_VERTEX ); exp.More(); exp.Next() )
1407     if ( V010.IsSame( exp.Current() ) || V011.IsSame( exp.Current() ))
1408       break;
1409   if ( !exp.More() ) {
1410     std::swap( Fx1z, F1yz);
1411   }
1412
1413   // find vertical edges
1414   for ( exp.Init( Fx0z, TopAbs_EDGE ); exp.More(); exp.Next() ) {
1415     const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
1416     const TopoDS_Shape& vFirst = TopExp::FirstVertex( edge, true );
1417     if ( vFirst.IsSame( V001 ))
1418       E00z = edge;
1419     else if ( vFirst.IsSame( V100 ))
1420       E10z = edge;
1421   }
1422   if ( E00z.IsNull() || E10z.IsNull() ) {
1423     MESSAGE(" LoadBlockShapes() error ");
1424     return false;
1425   }
1426   for ( exp.Init( Fx1z, TopAbs_EDGE ); exp.More(); exp.Next() ) {
1427     const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
1428     const TopoDS_Shape& vFirst = TopExp::FirstVertex( edge, true );
1429     if ( vFirst.IsSame( V111 ))
1430       E11z = edge;
1431     else if ( vFirst.IsSame( V010 ))
1432       E01z = edge;
1433   }
1434   if ( E01z.IsNull() || E11z.IsNull() ) {
1435     MESSAGE(" LoadBlockShapes() error ");
1436     return false;
1437   }
1438
1439   // load shapes in theShapeIDMap
1440
1441   theShapeIDMap.Clear();
1442   
1443   theShapeIDMap.Add(V000.Oriented( TopAbs_FORWARD ));
1444   theShapeIDMap.Add(V100.Oriented( TopAbs_FORWARD ));
1445   theShapeIDMap.Add(V010.Oriented( TopAbs_FORWARD ));
1446   theShapeIDMap.Add(V110.Oriented( TopAbs_FORWARD ));
1447   theShapeIDMap.Add(V001.Oriented( TopAbs_FORWARD ));
1448   theShapeIDMap.Add(V101.Oriented( TopAbs_FORWARD ));
1449   theShapeIDMap.Add(V011.Oriented( TopAbs_FORWARD ));
1450   theShapeIDMap.Add(V111.Oriented( TopAbs_FORWARD ));
1451
1452   theShapeIDMap.Add(Ex00);
1453   theShapeIDMap.Add(Ex10);
1454   theShapeIDMap.Add(Ex01);
1455   theShapeIDMap.Add(Ex11);
1456
1457   theShapeIDMap.Add(E0y0);
1458   theShapeIDMap.Add(E1y0);
1459   theShapeIDMap.Add(E0y1);
1460   theShapeIDMap.Add(E1y1);
1461
1462   theShapeIDMap.Add(E00z);
1463   theShapeIDMap.Add(E10z);
1464   theShapeIDMap.Add(E01z);
1465   theShapeIDMap.Add(E11z);
1466
1467   theShapeIDMap.Add(Fxy0);
1468   theShapeIDMap.Add(Fxy1);
1469   theShapeIDMap.Add(Fx0z);
1470   theShapeIDMap.Add(Fx1z);
1471   theShapeIDMap.Add(F0yz);
1472   theShapeIDMap.Add(F1yz);
1473   
1474   theShapeIDMap.Add(theShell);
1475
1476   return true;
1477 }
1478
1479 //================================================================================
1480 /*!
1481  * \brief Initialize block geometry with shapes from theShapeIDMap
1482   * \param theShapeIDMap - map of block subshapes
1483   * \retval bool - is a success
1484  */
1485 //================================================================================
1486
1487 bool SMESH_Block::LoadBlockShapes(const TopTools_IndexedMapOfOrientedShape& theShapeIDMap)
1488 {
1489   init();
1490
1491   // store shapes geometry
1492   for ( int shapeID = 1; shapeID < theShapeIDMap.Extent(); shapeID++ )
1493   {
1494     const TopoDS_Shape& S = theShapeIDMap( shapeID );
1495     switch ( S.ShapeType() )
1496     {
1497     case TopAbs_VERTEX: {
1498
1499       if ( !IsVertexID( ID_V111 )) return false;
1500       myPnt[ shapeID - ID_V000 ] = BRep_Tool::Pnt( TopoDS::Vertex( S )).XYZ();
1501       break;
1502     }
1503     case TopAbs_EDGE: {
1504
1505       if ( !IsEdgeID( shapeID )) return false;
1506       const TopoDS_Edge& edge = TopoDS::Edge( S );
1507       TEdge& tEdge = myEdge[ shapeID - ID_FirstE ];
1508       tEdge.Set( shapeID,
1509                  new BRepAdaptor_Curve( edge ),
1510                  IsForwardEdge( edge, theShapeIDMap ));
1511       break;
1512     }
1513     case TopAbs_FACE: {
1514
1515       if ( !LoadFace( TopoDS::Face( S ), shapeID, theShapeIDMap ))
1516         return false;
1517       break;
1518     }
1519     default: break;
1520     }
1521   } // loop on shapes in theShapeIDMap
1522
1523   return true;
1524 }
1525
1526 //================================================================================
1527 /*!
1528  * \brief Load face geometry
1529   * \param theFace - face
1530   * \param theFaceID - face in-block ID
1531   * \param theShapeIDMap - map of block subshapes
1532   * \retval bool - is a success
1533  * 
1534  * It is enough to compute params or coordinates on the face.
1535  * Face subshapes must be loaded into theShapeIDMap before
1536  */
1537 //================================================================================
1538
1539 bool SMESH_Block::LoadFace(const TopoDS_Face& theFace,
1540                            const int          theFaceID,
1541                            const TopTools_IndexedMapOfOrientedShape& theShapeIDMap)
1542 {
1543   if ( !IsFaceID( theFaceID ) ) return false;
1544   // pcurves
1545   Adaptor2d_Curve2d* c2d[4];
1546   bool isForward[4];
1547   vector< int > edgeIdVec;
1548   GetFaceEdgesIDs( theFaceID, edgeIdVec );
1549   for ( int iE = 0; iE < edgeIdVec.size(); iE++ ) // loop on 4 edges
1550   {
1551     if ( edgeIdVec[ iE ] > theShapeIDMap.Extent() )
1552       return false;
1553     const TopoDS_Edge& edge = TopoDS::Edge( theShapeIDMap( edgeIdVec[ iE ]));
1554     c2d[ iE ] = new BRepAdaptor_Curve2d( edge, theFace );
1555     isForward[ iE ] = IsForwardEdge( edge, theShapeIDMap );
1556   }
1557   TFace& tFace = myFace[ theFaceID - ID_FirstF ];
1558   tFace.Set( theFaceID, new BRepAdaptor_Surface( theFace ), c2d, isForward );
1559   return true;
1560 }
1561
1562 //================================================================================
1563 /*!
1564  * \brief/ Insert theShape into theShapeIDMap with theShapeID
1565   * \param theShape - shape to insert
1566   * \param theShapeID - shape in-block ID
1567   * \param theShapeIDMap - map of block subshapes
1568  */
1569 //================================================================================
1570
1571 bool SMESH_Block::Insert(const TopoDS_Shape& theShape,
1572                          const int           theShapeID,
1573                          TopTools_IndexedMapOfOrientedShape& theShapeIDMap)
1574 {
1575   if ( !theShape.IsNull() && theShapeID > 0 )
1576   {
1577     if ( theShapeIDMap.Contains( theShape ))
1578       return ( theShapeIDMap.FindIndex( theShape ) == theShapeID );
1579
1580     if ( theShapeID <= theShapeIDMap.Extent() ) {
1581         theShapeIDMap.Substitute( theShapeID, theShape );
1582     }
1583     else {
1584       while ( theShapeIDMap.Extent() < theShapeID - 1 ) {
1585         TopoDS_Compound comp;
1586         BRep_Builder().MakeCompound( comp );
1587         theShapeIDMap.Add( comp );
1588       }
1589       theShapeIDMap.Add( theShape );
1590     }
1591     return true;
1592   }
1593   return false;
1594 }
1595
1596 //=======================================================================
1597 //function : GetFaceEdgesIDs
1598 //purpose  : return edges IDs in the order u0, u1, 0v, 1v
1599 //           u0 means "|| u, v == 0"
1600 //=======================================================================
1601
1602 void SMESH_Block::GetFaceEdgesIDs (const int faceID, vector< int >& edgeVec )
1603 {
1604   edgeVec.resize( 4 );
1605   switch ( faceID ) {
1606   case ID_Fxy0:
1607     edgeVec[ 0 ] = ID_Ex00;
1608     edgeVec[ 1 ] = ID_Ex10;
1609     edgeVec[ 2 ] = ID_E0y0;
1610     edgeVec[ 3 ] = ID_E1y0;
1611     break;
1612   case ID_Fxy1:
1613     edgeVec[ 0 ] = ID_Ex01;
1614     edgeVec[ 1 ] = ID_Ex11;
1615     edgeVec[ 2 ] = ID_E0y1;
1616     edgeVec[ 3 ] = ID_E1y1;
1617     break;
1618   case ID_Fx0z:
1619     edgeVec[ 0 ] = ID_Ex00;
1620     edgeVec[ 1 ] = ID_Ex01;
1621     edgeVec[ 2 ] = ID_E00z;
1622     edgeVec[ 3 ] = ID_E10z;
1623     break;
1624   case ID_Fx1z:
1625     edgeVec[ 0 ] = ID_Ex10;
1626     edgeVec[ 1 ] = ID_Ex11;
1627     edgeVec[ 2 ] = ID_E01z;
1628     edgeVec[ 3 ] = ID_E11z;
1629     break;
1630   case ID_F0yz:
1631     edgeVec[ 0 ] = ID_E0y0;
1632     edgeVec[ 1 ] = ID_E0y1;
1633     edgeVec[ 2 ] = ID_E00z;
1634     edgeVec[ 3 ] = ID_E01z;
1635     break;
1636   case ID_F1yz:
1637     edgeVec[ 0 ] = ID_E1y0;
1638     edgeVec[ 1 ] = ID_E1y1;
1639     edgeVec[ 2 ] = ID_E10z;
1640     edgeVec[ 3 ] = ID_E11z;
1641     break;
1642   default:
1643     MESSAGE(" GetFaceEdgesIDs(), wrong face ID: " << faceID );
1644   }
1645 }
1646
1647 //=======================================================================
1648 //function : GetEdgeVertexIDs
1649 //purpose  : return vertex IDs of an edge
1650 //=======================================================================
1651
1652 void SMESH_Block::GetEdgeVertexIDs (const int edgeID, vector< int >& vertexVec )
1653 {
1654   vertexVec.resize( 2 );
1655   switch ( edgeID ) {
1656
1657   case ID_Ex00:
1658     vertexVec[ 0 ] = ID_V000;
1659     vertexVec[ 1 ] = ID_V100;
1660     break;
1661   case ID_Ex10:
1662     vertexVec[ 0 ] = ID_V010;
1663     vertexVec[ 1 ] = ID_V110;
1664     break;
1665   case ID_Ex01:
1666     vertexVec[ 0 ] = ID_V001;
1667     vertexVec[ 1 ] = ID_V101;
1668     break;
1669   case ID_Ex11:
1670     vertexVec[ 0 ] = ID_V011;
1671     vertexVec[ 1 ] = ID_V111;
1672     break;
1673
1674   case ID_E0y0:
1675     vertexVec[ 0 ] = ID_V000;
1676     vertexVec[ 1 ] = ID_V010;
1677     break;
1678   case ID_E1y0:
1679     vertexVec[ 0 ] = ID_V100;
1680     vertexVec[ 1 ] = ID_V110;
1681     break;
1682   case ID_E0y1:
1683     vertexVec[ 0 ] = ID_V001;
1684     vertexVec[ 1 ] = ID_V011;
1685     break;
1686   case ID_E1y1:
1687     vertexVec[ 0 ] = ID_V101;
1688     vertexVec[ 1 ] = ID_V111;
1689     break;
1690
1691   case ID_E00z:
1692     vertexVec[ 0 ] = ID_V000;
1693     vertexVec[ 1 ] = ID_V001;
1694     break;
1695   case ID_E10z:
1696     vertexVec[ 0 ] = ID_V100;
1697     vertexVec[ 1 ] = ID_V101;
1698     break;
1699   case ID_E01z:
1700     vertexVec[ 0 ] = ID_V010;
1701     vertexVec[ 1 ] = ID_V011;
1702     break;
1703   case ID_E11z:
1704     vertexVec[ 0 ] = ID_V110;
1705     vertexVec[ 1 ] = ID_V111;
1706     break;
1707   default:
1708     vertexVec.resize(0);
1709     MESSAGE(" GetEdgeVertexIDs(), wrong edge ID: " << edgeID );
1710   }
1711 }