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