Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[modules/smesh.git] / src / SMDS / SMDS_VolumeTool.cxx
1 // File      : SMDS_VolumeTool.cxx
2 // Created   : Tue Jul 13 12:22:13 2004
3 // Author    : Edward AGAPOV (eap)
4 // Copyright : Open CASCADE
5
6
7 #include "SMDS_VolumeTool.hxx"
8
9 #include "SMDS_MeshElement.hxx"
10 #include "SMDS_MeshNode.hxx"
11 #include <map>
12 #include <float.h>
13
14 using namespace std;
15
16 /*
17 //           N3
18 //           +
19 //          /|\
20 //         / | \
21 //        /  |  \
22 //    N0 +---|---+ N2                TETRAHEDRON
23 //       \   |   /
24 //        \  |  /
25 //         \ | /
26 //          \|/
27 //           +
28 //           N1
29 */
30 static int Tetra_F [4][4] = { // FORWARD == REVERSED EXTERNAL
31   { 0, 1, 2, 0 },             // Bottom face has an internal normal, other - external
32   { 0, 1, 3, 0 },
33   { 1, 2, 3, 1 },
34   { 0, 3, 2, 0 }}; 
35 static int Tetra_R [4][4] = { // REVERSED == FORWARD EXTERNAL
36   { 0, 2, 1, 0 },              // All faces have  external normals
37   { 0, 1, 3, 0 },
38   { 1, 2, 3, 1 },
39   { 0, 3, 2, 0 }}; 
40 static int Tetra_nbN [] = { 3, 3, 3, 3 };
41
42 /*   
43 //            + N4
44 //           /|\
45 //          / | \
46 //         /  |  \
47 //        /   |   \
48 //    N3 +---------+ N5
49 //       |    |    |
50 //       |    + N1 |
51 //       |   / \   |                PENTAHEDRON
52 //       |  /   \  |
53 //       | /     \ |
54 //       |/       \|
55 //    N0 +---------+ N2
56 */
57 static int Penta_F [5][5] = { // FORWARD
58   { 0, 1, 2, 0, 0 },          // Top face has an internal normal, other - external
59   { 3, 4, 5, 3, 3 },          // 0 is bottom, 1 is top face
60   { 0, 2, 5, 3, 0 },
61   { 1, 2, 5, 4, 1 },
62   { 1, 0, 3, 4, 1 }}; 
63 static int Penta_R [5][5] = { // REVERSED
64   { 0, 2, 1, 0, 0 },          // Bottom face has an internal normal, other - external
65   { 3, 5, 4, 3, 3 },          // 0 is bottom, 1 is top face
66   { 0, 2, 5, 3, 0 },
67   { 1, 2, 5, 4, 1 },
68   { 1, 0, 3, 4, 1 }}; 
69 static int Penta_FE [5][5] = { // EXTERNAL
70   { 0, 1, 2, 0, 0 },
71   { 3, 5, 4, 3, 3 },
72   { 0, 2, 5, 3, 0 },
73   { 1, 2, 5, 4, 1 },
74   { 1, 0, 3, 4, 1 }}; 
75 static int Penta_RE [5][5] = { // REVERSED EXTERNAL
76   { 0, 0, 2, 1, 0 },
77   { 3, 3, 4, 5, 3 },
78   { 0, 2, 5, 3, 0 },
79   { 1, 2, 5, 4, 1 },
80   { 1, 0, 3, 4, 1 }}; 
81 static int Penta_nbN [] = { 3, 3, 4, 4, 4 };
82
83 /*
84 //         N7+----------+N6
85 //          /|         /|
86 //         / |        / |
87 //        /  |       /  |
88 //     N4+----------+N5 |
89 //       |   |      |   |           HEXAHEDRON
90 //       |   |      |   |
91 //       |   |      |   |
92 //       | N3+------|---+N2
93 //       |  /       |  /
94 //       | /        | /
95 //       |/         |/
96 //     N0+----------+N1
97 */
98 static int Hexa_F [6][5] = { // FORWARD
99   { 0, 1, 2, 3, 0 },         // opposite faces are neighbouring,
100   { 4, 5, 6, 7, 4 },         // even face normal is internal, odd - external
101   { 1, 0, 4, 5, 1 },         // same index nodes nodes of opposite faces are linked
102   { 2, 3, 7, 6, 2 }, 
103   { 0, 3, 7, 4, 0 }, 
104   { 1, 2, 6, 5, 1 }}; 
105 static int Hexa_R [6][5] = { // REVERSED
106   { 0, 3, 2, 1, 0 },         // opposite faces are neighbouring,
107   { 4, 7, 6, 5, 4 },         // even face normal is external, odd - internal
108   { 1, 5, 4, 0, 1 },         // same index nodes nodes of opposite faces are linked
109   { 2, 6, 7, 3, 2 }, 
110   { 0, 4, 7, 3, 0 }, 
111   { 1, 5, 6, 2, 1 }}; 
112 static int Hexa_FE [6][5] = { // EXTERNAL
113   { 0, 3, 2, 1, 0 },         // opposite faces are neighbouring,
114   { 4, 5, 6, 7, 4 },         // all face normals are external,
115   { 0, 1, 5, 4, 0 },         // links in opposite faces: 0-0, 1-3, 2-2, 3-1
116   { 3, 7, 6, 2, 3 }, 
117   { 1, 2, 6, 5, 1 }, 
118   { 0, 4, 7, 3, 0 }};
119 static int Hexa_RE [6][5] = { // REVERSED EXTERNAL
120   { 0, 1, 2, 3, 0 },         // opposite faces are neighbouring,
121   { 4, 7, 6, 5, 4 },         // all face normals are external,
122   { 0, 1, 5, 4, 0 },         // links in opposite faces: 0-0, 1-3, 2-2, 3-1
123   { 3, 7, 6, 2, 3 }, 
124   { 1, 2, 6, 5, 1 }, 
125   { 0, 4, 7, 3, 0 }};
126 static int Hexa_nbN [] = { 4, 4, 4, 4, 4, 4 };
127
128 // ========================================================
129 // to perform some calculations without linkage to CASCADE
130 // ========================================================
131 struct XYZ {
132   double x;
133   double y;
134   double z;
135   XYZ()                               { x = 0; y = 0; z = 0; }
136   XYZ( double X, double Y, double Z ) { x = X; y = Y; z = Z; }
137   XYZ( const XYZ& other )             { x = other.x; y = other.y; z = other.z; }
138   XYZ( const SMDS_MeshNode* n )       { x = n->X(); y = n->Y(); z = n->Z(); }
139   XYZ operator-( const XYZ& other );
140   XYZ Crossed( const XYZ& other );
141   double Dot( const XYZ& other );
142   double Magnitude();
143 };
144 XYZ XYZ::operator-( const XYZ& Right ) {
145   return XYZ(x - Right.x, y - Right.y, z - Right.z);
146 }
147 XYZ XYZ::Crossed( const XYZ& Right ) {
148   return XYZ (y * Right.z - z * Right.y,
149               z * Right.x - x * Right.z,
150               x * Right.y - y * Right.x);
151 }
152 double XYZ::Dot( const XYZ& Other ) {
153   return(x * Other.x + y * Other.y + z * Other.z);
154 }
155 double XYZ::Magnitude() {
156   return sqrt (x * x + y * y + z * z);
157 }
158
159 //=======================================================================
160 //function : SMDS_VolumeTool
161 //purpose  : 
162 //=======================================================================
163
164 SMDS_VolumeTool::SMDS_VolumeTool ()
165      : myVolume( 0 ),
166        myVolForward( true ),
167        myNbFaces( 0 ),
168        myVolumeNbNodes( 0 ),
169        myForwardFaces( false ),
170        myExternalFaces( false )
171 {
172 }
173 //=======================================================================
174 //function : SMDS_VolumeTool
175 //purpose  : 
176 //=======================================================================
177
178 SMDS_VolumeTool::SMDS_VolumeTool (const SMDS_MeshElement* theVolume)
179      : myForwardFaces( false ),
180        myExternalFaces( false )
181 {
182   Set( theVolume );
183 }
184
185 //=======================================================================
186 //function : SMDS_VolumeTool
187 //purpose  : 
188 //=======================================================================
189
190 SMDS_VolumeTool::~SMDS_VolumeTool()
191 {
192 }
193
194 //=======================================================================
195 //function : SetVolume
196 //purpose  : Set volume to iterate on
197 //=======================================================================
198
199 bool SMDS_VolumeTool::Set (const SMDS_MeshElement* theVolume)
200 {
201   myVolume = 0;
202   myVolForward = true;
203   myCurFace = -1;
204   myVolumeNbNodes = 0;
205   myNbFaces = 0;
206   if ( theVolume && theVolume->GetType() == SMDSAbs_Volume )
207   {
208     myVolumeNbNodes = theVolume->NbNodes();
209     switch ( myVolumeNbNodes ) {
210     case 4:
211     case 6:
212     case 8:
213       {
214       myVolume = theVolume;
215       myNbFaces = theVolume->NbFaces();
216
217       // set volume nodes
218       int iNode = 0;
219       SMDS_ElemIteratorPtr nodeIt = myVolume->nodesIterator();
220       while ( nodeIt->more() )
221         myVolumeNodes[ iNode++ ] = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
222
223       // nb nodes in each face
224       if ( myVolumeNbNodes == 4 )
225         myFaceNbNodes = Tetra_nbN;
226       else if ( myVolumeNbNodes == 6 )
227         myFaceNbNodes = Penta_nbN;
228       else
229         myFaceNbNodes = Hexa_nbN;
230       break;
231       // define volume orientation
232       XYZ botNormal;
233       GetFaceNormal( 0, botNormal.x, botNormal.y, botNormal.z );
234       const SMDS_MeshNode* topNode = myVolumeNodes[ myVolumeNbNodes - 1 ];
235       const SMDS_MeshNode* botNode = myVolumeNodes[ 0 ];
236       XYZ upDir (topNode->X() - botNode->X(),
237                  topNode->Y() - botNode->Y(),
238                  topNode->Z() - botNode->Z() );
239       bool diffDir = ( botNormal.Dot( upDir ) < 0 );
240       myVolForward = ( myVolumeNbNodes == 6 ? diffDir : !diffDir );
241
242     }
243     default: myVolume = 0;
244     }
245   }
246   return ( myVolume != 0 );
247 }
248
249 //=======================================================================
250 //function : GetInverseNodes
251 //purpose  : Return nodes vector of an inverse volume
252 //=======================================================================
253
254 #define SWAP_NODES(nodes,i1,i2)                         \
255 {                                                 \
256   const SMDS_MeshNode* tmp = nodes[ i1 ]; \
257   nodes[ i1 ] = nodes[ i2 ];      \
258   nodes[ i2 ] = tmp;                      \
259 }
260 void SMDS_VolumeTool::Inverse ()
261 {
262   if ( !myVolume ) return;
263
264   myVolForward = !myVolForward;
265   myCurFace = -1;
266
267   // inverse top and bottom faces
268   switch ( myVolumeNbNodes ) {
269   case 4:
270     SWAP_NODES( myVolumeNodes, 1, 2 );
271     break;
272   case 6:
273     SWAP_NODES( myVolumeNodes, 1, 2 );
274     SWAP_NODES( myVolumeNodes, 4, 5 );
275     break;
276   case 8:
277     SWAP_NODES( myVolumeNodes, 1, 3 );
278     SWAP_NODES( myVolumeNodes, 5, 7 );
279     break;
280   default:;
281   }
282 }
283
284 //=======================================================================
285 //function : GetSize
286 //purpose  : Return element volume
287 //=======================================================================
288
289 double SMDS_VolumeTool::GetSize() const
290 {
291   return 0;
292 }
293
294 //=======================================================================
295 //function : GetBaryCenter
296 //purpose  : 
297 //=======================================================================
298
299 bool SMDS_VolumeTool::GetBaryCenter(double & X, double & Y, double & Z) const
300 {
301   X = Y = Z = 0.;
302   if ( !myVolume )
303     return false;
304
305   for ( int i = 0; i < myVolumeNbNodes; i++ ) {
306     X += myVolumeNodes[ i ]->X();
307     Y += myVolumeNodes[ i ]->Y();
308     Z += myVolumeNodes[ i ]->Z();
309   }
310   X /= myVolumeNbNodes;
311   Y /= myVolumeNbNodes;
312   Z /= myVolumeNbNodes;
313
314   return true;
315 }
316
317 //=======================================================================
318 //function : SetForwardOrientation
319 //purpose  : Node order will be as for forward orientation
320 //=======================================================================
321
322 void SMDS_VolumeTool::SetForwardOrientation ()
323 {
324   myForwardFaces = true;
325 }
326
327 //=======================================================================
328 //function : SetExternalNormal
329 //purpose  : Node order will be so that faces normals are external
330 //=======================================================================
331
332 void SMDS_VolumeTool::SetExternalNormal ()
333 {
334   myExternalFaces = true;
335 }
336
337 //=======================================================================
338 //function : NbFaceNodes
339 //purpose  : Return number of nodes in the array of face nodes
340 //=======================================================================
341
342 int SMDS_VolumeTool::NbFaceNodes( int faceIndex )
343 {
344   if ( !setFace( faceIndex ))
345     return 0;
346   return myFaceNbNodes[ faceIndex ];
347 }
348
349 //=======================================================================
350 //function : GetFaceNodes
351 //purpose  : Return pointer to the array of face nodes.
352 //           To comfort link iteration, the array
353 //           length == NbFaceNodes( faceIndex ) + 1 and
354 //           the last node == the first one.
355 //=======================================================================
356
357 const SMDS_MeshNode** SMDS_VolumeTool::GetFaceNodes( int faceIndex )
358 {
359   if ( !setFace( faceIndex ))
360     return 0;
361   return myFaceNodes;
362 }
363
364 //=======================================================================
365 //function : GetFaceNodesIndices
366 //purpose  : Return pointer to the array of face nodes indices
367 //           To comfort link iteration, the array
368 //           length == NbFaceNodes( faceIndex ) + 1 and
369 //           the last node index == the first one.
370 //=======================================================================
371
372 const int* SMDS_VolumeTool::GetFaceNodesIndices( int faceIndex )
373 {
374   if ( !setFace( faceIndex ))
375     return 0;
376   return myFaceNodeIndices;
377 }
378
379 //=======================================================================
380 //function : GetFaceNodes
381 //purpose  : Return a set of face nodes.
382 //=======================================================================
383
384 bool SMDS_VolumeTool::GetFaceNodes (int faceIndex,
385                                     std::set<const SMDS_MeshNode*>& theFaceNodes )
386 {
387   if ( !setFace( faceIndex ))
388     return false;
389
390   theFaceNodes.clear();
391   int iNode, nbNode = myFaceNbNodes[ faceIndex ];
392   for ( int iNode = 0; iNode < nbNode; iNode++ )
393     theFaceNodes.insert( myFaceNodes[ iNode ]);
394   
395   return true;
396 }
397
398 //=======================================================================
399 //function : IsFaceExternal
400 //purpose  : Check normal orientation of a returned face
401 //=======================================================================
402
403 bool SMDS_VolumeTool::IsFaceExternal( int faceIndex )
404 {
405   if ( myExternalFaces || !myVolume )
406     return true;
407
408   bool reversed = ( !myForwardFaces && !myVolForward );
409   switch ( myVolumeNbNodes ) {
410   case 4:
411     // only the bottom of a forward tetrahedron can be internal
412     return ( reversed || faceIndex != 0 );
413   case 6:
414     // in a forward pentahedron, the top is internal, in a reversed one - bottom
415     return ( reversed ? faceIndex != 0 : faceIndex != 1 );
416   case 8: {
417     // in a forward hexahedron, odd face normal is external, else vice versa
418     bool odd = faceIndex % 2;
419     return ( reversed ? !odd : odd );
420   }
421   default:;
422   }
423   return false;
424 }
425
426 //=======================================================================
427 //function : GetFaceNormal
428 //purpose  : Return a normal to a face
429 //=======================================================================
430
431 bool SMDS_VolumeTool::GetFaceNormal (int faceIndex, double & X, double & Y, double & Z)
432 {
433   if ( !setFace( faceIndex ))
434     return false;
435
436   XYZ p1 ( myFaceNodes[0] );
437   XYZ p2 ( myFaceNodes[1] );
438   XYZ p3 ( myFaceNodes[2] );
439   XYZ aVec12( p2 - p1 );
440   XYZ aVec13( p3 - p1 );
441   XYZ cross = aVec12.Crossed( aVec13 );
442
443   double size = cross.Magnitude();
444   if ( size <= DBL_MIN )
445     return false;
446
447   X = cross.x / size;
448   Y = cross.y / size;
449   Z = cross.z / size;
450
451   return true;
452 }
453
454
455 //=======================================================================
456 //function : GetFaceArea
457 //purpose  : Return face area
458 //=======================================================================
459
460 double SMDS_VolumeTool::GetFaceArea( int faceIndex )
461 {
462   if ( !setFace( faceIndex ))
463     return 0;
464
465   XYZ p1 ( myFaceNodes[0] );
466   XYZ p2 ( myFaceNodes[1] );
467   XYZ p3 ( myFaceNodes[2] );
468   XYZ aVec12( p2 - p1 );
469   XYZ aVec13( p3 - p1 );
470   double area = aVec12.Crossed( aVec13 ).Magnitude() * 0.5;
471
472   if ( myFaceNbNodes[ faceIndex ] == 4 ) {
473     XYZ p4 ( myFaceNodes[3] );
474     XYZ aVec14( p4 - p1 );
475     area += aVec14.Crossed( aVec13 ).Magnitude() * 0.5;
476   }
477   return area;
478 }
479
480 //=======================================================================
481 //function : GetOppFaceIndex
482 //purpose  : Return index of the opposite face if it exists, else -1.
483 //=======================================================================
484
485 int SMDS_VolumeTool::GetOppFaceIndex( int faceIndex ) const
486 {
487   int ind = -1;
488   if ( faceIndex >= 0 && faceIndex < NbFaces() ) {
489     switch ( myVolumeNbNodes ) {
490     case 6:
491       if ( faceIndex == 0 || faceIndex == 1 )
492         ind = 1 - faceIndex;
493       break;
494     case 8:
495       ind = faceIndex + ( faceIndex % 2 ? -1 : 1 );
496       break;
497     default:;
498     }
499   }
500   return ind;
501 }
502
503 //=======================================================================
504 //function : IsLinked
505 //purpose  : return true if theNode1 is linked with theNode2
506 //=======================================================================
507
508 bool SMDS_VolumeTool::IsLinked (const SMDS_MeshNode* theNode1,
509                                 const SMDS_MeshNode* theNode2) const
510 {
511   if ( !myVolume )
512     return false;
513
514   // find nodes indices
515   int i1 = -1, i2 = -1;
516   for ( int i = 0; i < myVolumeNbNodes; i++ ) {
517     if ( myVolumeNodes[ i ] == theNode1 )
518       i1 = i;
519     else if ( myVolumeNodes[ i ] == theNode2 )
520       i2 = i;
521   }
522   return IsLinked( i1, i2 );
523 }
524
525 //=======================================================================
526 //function : IsLinked
527 //purpose  : return true if the node with theNode1Index is linked
528 //           with the node with theNode2Index
529 //=======================================================================
530
531 bool SMDS_VolumeTool::IsLinked (const int theNode1Index,
532                                 const int theNode2Index) const
533 {
534   int minInd = theNode1Index < theNode2Index ? theNode1Index : theNode2Index;
535   int maxInd = theNode1Index < theNode2Index ? theNode2Index : theNode1Index;
536
537   if ( minInd < 0 || maxInd > myVolumeNbNodes - 1 || maxInd == minInd )
538     return false;
539
540   switch ( myVolumeNbNodes ) {
541   case 4:
542     return true;
543   case 6:
544     switch ( maxInd - minInd ) {
545     case 1: return minInd != 2;
546     case 2: return minInd == 0 || minInd == 3;
547     case 3: return true;
548     default:;
549     }
550     break;
551   case 8:
552     switch ( maxInd - minInd ) {
553     case 1: return minInd != 3;
554     case 3: return minInd == 0 || minInd == 4;
555     case 4: return true;
556     default:;
557     }
558     break;
559   default:;
560   }
561   return false;
562 }
563
564 //=======================================================================
565 //function : GetNodeIndex
566 //purpose  : Return an index of theNode
567 //=======================================================================
568
569 int SMDS_VolumeTool::GetNodeIndex(const SMDS_MeshNode* theNode) const
570 {
571   if ( myVolume ) {
572     for ( int i = 0; i < myVolumeNbNodes; i++ ) {
573       if ( myVolumeNodes[ i ] == theNode )
574         return i;
575     }
576   }
577   return -1;
578 }
579
580
581 //=======================================================================
582 //function : IsFreeFace
583 //purpose  : check that only one volume is build on the face nodes
584 //=======================================================================
585
586 bool SMDS_VolumeTool::IsFreeFace( int faceIndex )
587 {
588   const int free = true;
589   if ( !setFace( faceIndex ))
590     return !free;
591
592   const SMDS_MeshNode** nodes = GetFaceNodes( faceIndex );
593   int nbFaceNodes = NbFaceNodes( faceIndex );
594
595   // evaluate nb of face nodes shared by other volume
596   int maxNbShared = -1;
597   typedef map< const SMDS_MeshElement*, int > TElemIntMap;
598   TElemIntMap volNbShared;
599   TElemIntMap::iterator vNbIt;
600   for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
601   {
602     const SMDS_MeshNode* n = nodes[ iNode ];
603     SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator();
604     while ( eIt->more() ) {
605       const SMDS_MeshElement* elem = eIt->next();
606       if ( elem != myVolume && elem->GetType() == SMDSAbs_Volume ) {
607         int nbShared = 1;
608         vNbIt = volNbShared.find( elem );
609         if ( vNbIt == volNbShared.end() )
610           volNbShared.insert ( TElemIntMap::value_type( elem, nbShared ));
611         else
612           nbShared = ++(*vNbIt).second;
613         if ( nbShared > maxNbShared )
614           maxNbShared = nbShared;
615       }
616     }
617   }
618   if ( maxNbShared < 3 )
619     return free; // is free
620
621   // find volumes laying on the opposite side of the face
622   // and sharing all nodes
623   XYZ intNormal; // internal normal
624   GetFaceNormal( faceIndex, intNormal.x, intNormal.y, intNormal.z );
625   if ( IsFaceExternal( faceIndex ))
626     intNormal = XYZ( -intNormal.x, -intNormal.y, -intNormal.z );
627   XYZ p0 ( nodes[0] ), baryCenter;
628   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
629   {
630     int nbShared = (*vNbIt).second;
631     if ( nbShared >= 3 ) {
632       SMDS_VolumeTool volume( (*vNbIt).first );
633       volume.GetBaryCenter( baryCenter.x, baryCenter.y, baryCenter.z );
634       XYZ intNormal2( baryCenter - p0 );
635       if ( intNormal.Dot( intNormal2 ) < 0 )
636         continue; // opposite side
637     }
638     // remove a volume from volNbShared map
639     volNbShared.erase( vNbIt );
640   }
641   // here volNbShared contains only volumes laying on the
642   // opposite side of the face
643   if ( volNbShared.empty() )
644     return free; // is free
645
646   // check if the whole area of a face is shared
647   bool isShared[] = { false, false, false, false }; // 4 triangle parts of a quadrangle
648   for ( vNbIt = volNbShared.begin(); vNbIt != volNbShared.end(); vNbIt++ )
649   {
650     SMDS_VolumeTool volume( (*vNbIt).first );
651     bool prevLinkShared = false;
652     int nbSharedLinks = 0;
653     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
654     {
655       bool linkShared = volume.IsLinked( nodes[ iNode ], nodes[ iNode + 1] );
656       if ( linkShared )
657         nbSharedLinks++;
658       if ( linkShared && prevLinkShared &&
659           volume.IsLinked( nodes[ iNode - 1 ], nodes[ iNode + 1] ))
660         isShared[ iNode ] = true;
661       prevLinkShared = linkShared;
662     }
663     if ( nbSharedLinks == nbFaceNodes )
664       return !free; // is not free
665     if ( nbFaceNodes == 4 ) {
666       // check traingle parts 1 & 3
667       if ( isShared[1] && isShared[3] )
668         return !free; // is not free
669       // check traingle parts 0 & 2;
670       // 0 part could not be checked in the loop; check it here
671       if ( isShared[2] && prevLinkShared &&
672           volume.IsLinked( nodes[ 0 ], nodes[ 1 ] ) &&
673           volume.IsLinked( nodes[ 1 ], nodes[ 3 ] ) )
674         return !free; // is not free
675     }
676   }
677   return free;
678 }
679
680 //=======================================================================
681 //function : GetFaceIndex
682 //purpose  : Return index of a face formed by theFaceNodes
683 //=======================================================================
684
685 int SMDS_VolumeTool::GetFaceIndex( const set<const SMDS_MeshNode*>& theFaceNodes )
686 {
687   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
688     const SMDS_MeshNode** nodes = GetFaceNodes( iFace );
689     int nbFaceNodes = NbFaceNodes( iFace );
690     set<const SMDS_MeshNode*> nodeSet;
691     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
692       nodeSet.insert( nodes[ iNode ] );
693     if ( theFaceNodes == nodeSet )
694       return iFace;
695   }
696   return -1;
697 }
698
699 //=======================================================================
700 //function : GetFaceIndex
701 //purpose  : Return index of a face formed by theFaceNodes
702 //=======================================================================
703
704 int SMDS_VolumeTool::GetFaceIndex( const set<int>& theFaceNodesIndices )
705 {
706   for ( int iFace = 0; iFace < myNbFaces; iFace++ ) {
707     const int* nodes = GetFaceNodesIndices( iFace );
708     int nbFaceNodes = NbFaceNodes( iFace );
709     set<int> nodeSet;
710     for ( int iNode = 0; iNode < nbFaceNodes; iNode++ )
711       nodeSet.insert( nodes[ iNode ] );
712     if ( theFaceNodesIndices == nodeSet )
713       return iFace;
714   }
715   return -1;
716 }
717
718 //=======================================================================
719 //function : setFace
720 //purpose  : 
721 //=======================================================================
722
723 bool SMDS_VolumeTool::setFace( int faceIndex )
724 {
725   if ( !myVolume )
726     return false;
727
728   if ( myCurFace == faceIndex )
729     return true;
730
731   if ( faceIndex < 0 || faceIndex >= NbFaces() )
732     return false;
733
734   // choose face node indices
735   switch ( myVolumeNbNodes ) {
736   case 4:
737     if ( myExternalFaces )
738       myFaceNodeIndices = myVolForward ? Tetra_R[ faceIndex ] : Tetra_F[ faceIndex ];
739     else if ( myForwardFaces )
740       myFaceNodeIndices = myVolForward ? Tetra_F[ faceIndex ] : Tetra_R[ faceIndex ];
741     else
742       myFaceNodeIndices = Tetra_F[ faceIndex ];
743     break;
744   case 6:
745     if ( myExternalFaces )
746       myFaceNodeIndices = myVolForward ? Penta_FE[ faceIndex ] : Penta_RE[ faceIndex ];
747     else if ( myForwardFaces )
748       myFaceNodeIndices = myVolForward ? Penta_F[ faceIndex ] : Penta_R[ faceIndex ];
749     else
750       myFaceNodeIndices = Penta_F[ faceIndex ];
751     break;
752   case 8:
753     if ( myExternalFaces )
754       myFaceNodeIndices = myVolForward ? Hexa_FE[ faceIndex ] : Hexa_RE[ faceIndex ];
755     else if ( myForwardFaces )
756       myFaceNodeIndices = myVolForward ? Hexa_F[ faceIndex ] : Hexa_R[ faceIndex ];
757     else
758       myFaceNodeIndices = Hexa_F[ faceIndex ];
759     break;
760   default: return false;
761   }
762
763   // set face nodes
764   int iNode, nbNode = myFaceNbNodes[ faceIndex ];
765   for ( iNode = 0; iNode <= nbNode; iNode++ )
766     myFaceNodes[ iNode ] = myVolumeNodes[ myFaceNodeIndices[ iNode ]];
767
768   myCurFace = faceIndex;
769
770   return true;
771 }