Salome HOME
Fix some controls
[modules/smesh.git] / src / Controls / SMESH_Controls.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19
20 #include "SMESH_ControlsDef.hxx"
21
22 #include <set>
23
24 #include <BRep_Tool.hxx>
25 #include <gp_Ax3.hxx>
26 #include <gp_Cylinder.hxx>
27 #include <gp_Dir.hxx>
28 #include <gp_Pnt.hxx>
29 #include <gp_Pln.hxx>
30 #include <gp_Vec.hxx>
31 #include <gp_XYZ.hxx>
32 #include <Geom_Plane.hxx>
33 #include <Geom_CylindricalSurface.hxx>
34 #include <Precision.hxx>
35 #include <TColgp_Array1OfXYZ.hxx>
36 #include <TColStd_MapOfInteger.hxx>
37 #include <TColStd_SequenceOfAsciiString.hxx>
38 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
39 #include <TopAbs.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
43
44 #include "SMDS_Mesh.hxx"
45 #include "SMDS_Iterator.hxx"
46 #include "SMDS_MeshElement.hxx"
47 #include "SMDS_MeshNode.hxx"
48 #include "SMDS_VolumeTool.hxx"
49 #include "SMDS_QuadraticFaceOfNodes.hxx"
50 #include "SMDS_QuadraticEdge.hxx"
51
52
53 /*
54                             AUXILIARY METHODS
55 */
56
57 namespace{
58   inline double getAngle( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
59   {
60     gp_Vec v1( P1 - P2 ), v2( P3 - P2 );
61
62     return v1.Magnitude() < gp::Resolution() ||
63       v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
64   }
65
66   inline double getArea( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
67   {
68     gp_Vec aVec1( P2 - P1 );
69     gp_Vec aVec2( P3 - P1 );
70     return ( aVec1 ^ aVec2 ).Magnitude() * 0.5;
71   }
72
73   inline double getArea( const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3 )
74   {
75     return getArea( P1.XYZ(), P2.XYZ(), P3.XYZ() );
76   }
77
78
79
80   inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
81   {
82     double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
83     return aDist;
84   }
85
86   int getNbMultiConnection( const SMDS_Mesh* theMesh, const int theId )
87   {
88     if ( theMesh == 0 )
89       return 0;
90
91     const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
92     if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge/* || anEdge->NbNodes() != 2 */)
93       return 0;
94
95     // for each pair of nodes in anEdge (there are 2 pairs in a quadratic edge)
96     // count elements containing both nodes of the pair.
97     // Note that there may be such cases for a quadratic edge (a horizontal line):
98     //
99     //  Case 1          Case 2
100     //  |     |      |        |      |
101     //  |     |      |        |      |
102     //  +-----+------+  +-----+------+ 
103     //  |            |  |            |
104     //  |            |  |            |
105     // result sould be 2 in both cases
106     //
107     int aResult0 = 0, aResult1 = 0;
108      // last node, it is a medium one in a quadratic edge
109     const SMDS_MeshNode* aLastNode = anEdge->GetNode( anEdge->NbNodes() - 1 );
110     const SMDS_MeshNode* aNode0 = anEdge->GetNode( 0 );
111     const SMDS_MeshNode* aNode1 = anEdge->GetNode( 1 );
112     if ( aNode1 == aLastNode ) aNode1 = 0;
113
114     SMDS_ElemIteratorPtr anElemIter = aLastNode->GetInverseElementIterator();
115     while( anElemIter->more() ) {
116       const SMDS_MeshElement* anElem = anElemIter->next();
117       if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
118         SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
119         while ( anIter->more() ) {
120           if ( const SMDS_MeshElement* anElemNode = anIter->next() ) {
121             if ( anElemNode == aNode0 ) {
122               aResult0++;
123               if ( !aNode1 ) break; // not a quadratic edge
124             }
125             else if ( anElemNode == aNode1 )
126               aResult1++;
127           }
128         }
129       }
130     }
131     int aResult = max ( aResult0, aResult1 );
132
133 //     TColStd_MapOfInteger aMap;
134
135 //     SMDS_ElemIteratorPtr anIter = anEdge->nodesIterator();
136 //     if ( anIter != 0 ) {
137 //       while( anIter->more() ) {
138 //      const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
139 //      if ( aNode == 0 )
140 //        return 0;
141 //      SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
142 //      while( anElemIter->more() ) {
143 //        const SMDS_MeshElement* anElem = anElemIter->next();
144 //        if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
145 //          int anId = anElem->GetID();
146
147 //          if ( anIter->more() )              // i.e. first node
148 //            aMap.Add( anId );
149 //          else if ( aMap.Contains( anId ) )
150 //            aResult++;
151 //        }
152 //      }
153 //       }
154 //     }
155
156     return aResult;
157   }
158
159 }
160
161
162
163 using namespace SMESH::Controls;
164
165 /*
166                                 FUNCTORS
167 */
168
169 /*
170   Class       : NumericalFunctor
171   Description : Base class for numerical functors
172 */
173 NumericalFunctor::NumericalFunctor():
174   myMesh(NULL)
175 {
176   myPrecision = -1;
177 }
178
179 void NumericalFunctor::SetMesh( const SMDS_Mesh* theMesh )
180 {
181   myMesh = theMesh;
182 }
183
184 bool NumericalFunctor::GetPoints(const int theId,
185                                  TSequenceOfXYZ& theRes ) const
186 {
187   theRes.clear();
188
189   if ( myMesh == 0 )
190     return false;
191
192   return GetPoints( myMesh->FindElement( theId ), theRes );
193 }
194
195 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
196                                  TSequenceOfXYZ&         theRes )
197 {
198   theRes.clear();
199
200   if ( anElem == 0)
201     return false;
202
203   theRes.reserve( anElem->NbNodes() );
204
205   // Get nodes of the element
206   SMDS_ElemIteratorPtr anIter;
207
208   if ( anElem->IsQuadratic() ) {
209     switch ( anElem->GetType() ) {
210     case SMDSAbs_Edge:
211       anIter = static_cast<const SMDS_QuadraticEdge*>
212         (anElem)->interlacedNodesElemIterator();
213       break;
214     case SMDSAbs_Face:
215       anIter = static_cast<const SMDS_QuadraticFaceOfNodes*>
216         (anElem)->interlacedNodesElemIterator();
217       break;
218     default:
219       anIter = anElem->nodesIterator();
220       //return false;
221     }
222   }
223   else {
224     anIter = anElem->nodesIterator();
225   }
226
227   if ( anIter ) {
228     while( anIter->more() ) {
229       if ( const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( anIter->next() ))
230         theRes.push_back( gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
231     }
232   }
233
234   return true;
235 }
236
237 long  NumericalFunctor::GetPrecision() const
238 {
239   return myPrecision;
240 }
241
242 void  NumericalFunctor::SetPrecision( const long thePrecision )
243 {
244   myPrecision = thePrecision;
245 }
246
247 double NumericalFunctor::GetValue( long theId )
248 {
249   TSequenceOfXYZ P;
250   if ( GetPoints( theId, P ))
251   {
252     double aVal = GetValue( P );
253     if ( myPrecision >= 0 )
254     {
255       double prec = pow( 10., (double)( myPrecision ) );
256       aVal = floor( aVal * prec + 0.5 ) / prec;
257     }
258     return aVal;
259   }
260
261   return 0.;
262 }
263
264 //=======================================================================
265 //function : GetValue
266 //purpose  : 
267 //=======================================================================
268
269 double Volume::GetValue( long theElementId )
270 {
271   if ( theElementId && myMesh ) {
272     SMDS_VolumeTool aVolumeTool;
273     if ( aVolumeTool.Set( myMesh->FindElement( theElementId )))
274       return aVolumeTool.GetSize();
275   }
276   return 0;
277 }
278
279 //=======================================================================
280 //function : GetBadRate
281 //purpose  : meaningless as it is not quality control functor
282 //=======================================================================
283
284 double Volume::GetBadRate( double Value, int /*nbNodes*/ ) const
285 {
286   return Value;
287 }
288
289 //=======================================================================
290 //function : GetType
291 //purpose  : 
292 //=======================================================================
293
294 SMDSAbs_ElementType Volume::GetType() const
295 {
296   return SMDSAbs_Volume;
297 }
298
299
300 /*
301   Class       : MinimumAngle
302   Description : Functor for calculation of minimum angle
303 */
304
305 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
306 {
307   double aMin;
308
309   if (P.size() <3)
310     return 0.;
311
312   aMin = getAngle(P( P.size() ), P( 1 ), P( 2 ));
313   aMin = Min(aMin,getAngle(P( P.size()-1 ), P( P.size() ), P( 1 )));
314
315   for (int i=2; i<P.size();i++){
316       double A0 = getAngle( P( i-1 ), P( i ), P( i+1 ) );
317     aMin = Min(aMin,A0);
318   }
319
320   return aMin * 180.0 / PI;
321 }
322
323 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
324 {
325   //const double aBestAngle = PI / nbNodes;
326   const double aBestAngle = 180.0 - ( 360.0 / double(nbNodes) );
327   return ( fabs( aBestAngle - Value ));
328 }
329
330 SMDSAbs_ElementType MinimumAngle::GetType() const
331 {
332   return SMDSAbs_Face;
333 }
334
335
336 /*
337   Class       : AspectRatio
338   Description : Functor for calculating aspect ratio
339 */
340 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
341 {
342   // According to "Mesh quality control" by Nadir Bouhamau referring to
343   // Pascal Jean Frey and Paul-Louis George. Maillages, applications aux elements finis.
344   // Hermes Science publications, Paris 1999 ISBN 2-7462-0024-4
345   // PAL10872
346
347   int nbNodes = P.size();
348
349   if ( nbNodes < 3 )
350     return 0;
351
352   // Compute lengths of the sides
353
354   vector< double > aLen (nbNodes);
355
356   for ( int i = 0; i < nbNodes - 1; i++ )
357     aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
358   aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
359
360   // Compute aspect ratio
361
362   if ( nbNodes == 3 )
363   {
364     // Q = alfa * h * p / S, where
365     //
366     // alfa = sqrt( 3 ) / 6
367     // h - length of the longest edge
368     // p - half perimeter
369     // S - triangle surface
370
371     const double alfa = sqrt( 3. ) / 6.;
372     double maxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
373     double half_perimeter = ( aLen[0] + aLen[1] + aLen[2] ) / 2.;
374     double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
375     if ( anArea <= Precision::Confusion() )
376       return 0.;
377
378     return alfa * maxLen * half_perimeter / anArea;
379   }
380   else
381   {
382     // return aspect ratio of the worst triange which can be built
383     // taking three nodes of the quadrangle
384     TSequenceOfXYZ triaPnts(3);
385     // triangle on nodes 1 3 2
386     triaPnts(1) = P(1);
387     triaPnts(2) = P(3);
388     triaPnts(3) = P(2);
389     double ar = GetValue( triaPnts );
390     // triangle on nodes 1 3 4
391     triaPnts(3) = P(4);
392     ar = Max ( ar, GetValue( triaPnts ));
393     // triangle on nodes 1 2 4
394     triaPnts(2) = P(2);
395     ar = Max ( ar, GetValue( triaPnts ));
396     // triangle on nodes 3 2 4
397     triaPnts(1) = P(3);
398     ar = Max ( ar, GetValue( triaPnts ));
399
400     return ar;
401   }
402 }
403
404 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
405 {
406   // the aspect ratio is in the range [1.0,infinity]
407   // 1.0 = good
408   // infinity = bad
409   return Value / 1000.;
410 }
411
412 SMDSAbs_ElementType AspectRatio::GetType() const
413 {
414   return SMDSAbs_Face;
415 }
416
417
418 /*
419   Class       : AspectRatio3D
420   Description : Functor for calculating aspect ratio
421 */
422 namespace{
423
424   inline double getHalfPerimeter(double theTria[3]){
425     return (theTria[0] + theTria[1] + theTria[2])/2.0;
426   }
427
428   inline double getArea(double theHalfPerim, double theTria[3]){
429     return sqrt(theHalfPerim*
430                 (theHalfPerim-theTria[0])*
431                 (theHalfPerim-theTria[1])*
432                 (theHalfPerim-theTria[2]));
433   }
434
435   inline double getVolume(double theLen[6]){
436     double a2 = theLen[0]*theLen[0];
437     double b2 = theLen[1]*theLen[1];
438     double c2 = theLen[2]*theLen[2];
439     double d2 = theLen[3]*theLen[3];
440     double e2 = theLen[4]*theLen[4];
441     double f2 = theLen[5]*theLen[5];
442     double P = 4.0*a2*b2*d2;
443     double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
444     double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
445     return sqrt(P-Q+R)/12.0;
446   }
447
448   inline double getVolume2(double theLen[6]){
449     double a2 = theLen[0]*theLen[0];
450     double b2 = theLen[1]*theLen[1];
451     double c2 = theLen[2]*theLen[2];
452     double d2 = theLen[3]*theLen[3];
453     double e2 = theLen[4]*theLen[4];
454     double f2 = theLen[5]*theLen[5];
455
456     double P = a2*e2*(b2+c2+d2+f2-a2-e2);
457     double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
458     double R = c2*d2*(a2+b2+e2+f2-c2-d2);
459     double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
460
461     return sqrt(P+Q+R-S)/12.0;
462   }
463
464   inline double getVolume(const TSequenceOfXYZ& P){
465     gp_Vec aVec1( P( 2 ) - P( 1 ) );
466     gp_Vec aVec2( P( 3 ) - P( 1 ) );
467     gp_Vec aVec3( P( 4 ) - P( 1 ) );
468     gp_Vec anAreaVec( aVec1 ^ aVec2 );
469     return fabs(aVec3 * anAreaVec) / 6.0;
470   }
471
472   inline double getMaxHeight(double theLen[6])
473   {
474     double aHeight = max(theLen[0],theLen[1]);
475     aHeight = max(aHeight,theLen[2]);
476     aHeight = max(aHeight,theLen[3]);
477     aHeight = max(aHeight,theLen[4]);
478     aHeight = max(aHeight,theLen[5]);
479     return aHeight;
480   }
481
482 }
483
484 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
485 {
486   double aQuality = 0.0;
487   int nbNodes = P.size();
488   switch(nbNodes){
489   case 4:{
490     double aLen[6] = {
491       getDistance(P( 1 ),P( 2 )), // a
492       getDistance(P( 2 ),P( 3 )), // b
493       getDistance(P( 3 ),P( 1 )), // c
494       getDistance(P( 2 ),P( 4 )), // d
495       getDistance(P( 3 ),P( 4 )), // e
496       getDistance(P( 1 ),P( 4 ))  // f
497     };
498     double aTria[4][3] = {
499       {aLen[0],aLen[1],aLen[2]}, // abc
500       {aLen[0],aLen[3],aLen[5]}, // adf
501       {aLen[1],aLen[3],aLen[4]}, // bde
502       {aLen[2],aLen[4],aLen[5]}  // cef
503     };
504     double aSumArea = 0.0;
505     double aHalfPerimeter = getHalfPerimeter(aTria[0]);
506     double anArea = getArea(aHalfPerimeter,aTria[0]);
507     aSumArea += anArea;
508     aHalfPerimeter = getHalfPerimeter(aTria[1]);
509     anArea = getArea(aHalfPerimeter,aTria[1]);
510     aSumArea += anArea;
511     aHalfPerimeter = getHalfPerimeter(aTria[2]);
512     anArea = getArea(aHalfPerimeter,aTria[2]);
513     aSumArea += anArea;
514     aHalfPerimeter = getHalfPerimeter(aTria[3]);
515     anArea = getArea(aHalfPerimeter,aTria[3]);
516     aSumArea += anArea;
517     double aVolume = getVolume(P);
518     //double aVolume = getVolume(aLen);
519     double aHeight = getMaxHeight(aLen);
520     static double aCoeff = sqrt(2.0)/12.0;
521     aQuality = aCoeff*aHeight*aSumArea/aVolume;
522     break;
523   }
524   case 5:{
525     {
526       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
527       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
528     }
529     {
530       gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
531       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
532     }
533     {
534       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
535       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
536     }
537     {
538       gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
539       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
540     }
541     break;
542   }
543   case 6:{
544     {
545       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
546       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
547     }
548     {
549       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
550       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
551     }
552     {
553       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
554       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
555     }
556     {
557       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
558       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
559     }
560     {
561       gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
562       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
563     }
564     {
565       gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
566       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
567     }
568     break;
569   }
570   case 8:{
571     {
572       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
573       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
574     }
575     {
576       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
577       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
578     }
579     {
580       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
581       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
582     }
583     {
584       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
585       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
586     }
587     {
588       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
589       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
590     }
591     {
592       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
593       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
594     }
595     {
596       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
597       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
598     }
599     {
600       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
601       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
602     }
603     {
604       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
605       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
606     }
607     {
608       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
609       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
610     }
611     {
612       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
613       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
614     }
615     {
616       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
617       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
618     }
619     {
620       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
621       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
622     }
623     {
624       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
625       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
626     }
627     {
628       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
629       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
630     }
631     {
632       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
633       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
634     }
635     {
636       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
637       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
638     }
639     {
640       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
641       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
642     }
643     {
644       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
645       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
646     }
647     {
648       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
649       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
650     }
651     {
652       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
653       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
654     }
655     {
656       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
657       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
658     }
659     {
660       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
661       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
662     }
663     {
664       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
665       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
666     }
667     {
668       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
669       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
670     }
671     {
672       gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
673       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
674     }
675     {
676       gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
677       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
678     }
679     {
680       gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
681       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
682     }
683     {
684       gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
685       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
686     }
687     {
688       gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
689       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
690     }
691     {
692       gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
693       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
694     }
695     {
696       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
697       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
698     }
699     {
700       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
701       aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
702     }
703     break;
704   }
705   }
706   if ( nbNodes > 4 ) {
707     // avaluate aspect ratio of quadranle faces
708     AspectRatio aspect2D;
709     SMDS_VolumeTool::VolumeType type = SMDS_VolumeTool::GetType( nbNodes );
710     int nbFaces = SMDS_VolumeTool::NbFaces( type );
711     TSequenceOfXYZ points(4);
712     for ( int i = 0; i < nbFaces; ++i ) { // loop on faces of a volume
713       if ( SMDS_VolumeTool::NbFaceNodes( type, i ) != 4 )
714         continue;
715       const int* pInd = SMDS_VolumeTool::GetFaceNodesIndices( type, i, true );
716       for ( int p = 0; p < 4; ++p ) // loop on nodes of a quadranle face
717         points( p + 1 ) = P( pInd[ p ] + 1 );
718       aQuality = max( aQuality, aspect2D.GetValue( points ));
719     }
720   }
721   return aQuality;
722 }
723
724 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
725 {
726   // the aspect ratio is in the range [1.0,infinity]
727   // 1.0 = good
728   // infinity = bad
729   return Value / 1000.;
730 }
731
732 SMDSAbs_ElementType AspectRatio3D::GetType() const
733 {
734   return SMDSAbs_Volume;
735 }
736
737
738 /*
739   Class       : Warping
740   Description : Functor for calculating warping
741 */
742 double Warping::GetValue( const TSequenceOfXYZ& P )
743 {
744   if ( P.size() != 4 )
745     return 0;
746
747   gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4;
748
749   double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
750   double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
751   double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
752   double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
753
754   return Max( Max( A1, A2 ), Max( A3, A4 ) );
755 }
756
757 double Warping::ComputeA( const gp_XYZ& thePnt1,
758                           const gp_XYZ& thePnt2,
759                           const gp_XYZ& thePnt3,
760                           const gp_XYZ& theG ) const
761 {
762   double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
763   double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
764   double L = Min( aLen1, aLen2 ) * 0.5;
765   if ( L < Precision::Confusion())
766     return 0.;
767
768   gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
769   gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
770   gp_XYZ N  = GI.Crossed( GJ );
771
772   if ( N.Modulus() < gp::Resolution() )
773     return PI / 2;
774
775   N.Normalize();
776
777   double H = ( thePnt2 - theG ).Dot( N );
778   return asin( fabs( H / L ) ) * 180 / PI;
779 }
780
781 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
782 {
783   // the warp is in the range [0.0,PI/2]
784   // 0.0 = good (no warp)
785   // PI/2 = bad  (face pliee)
786   return Value;
787 }
788
789 SMDSAbs_ElementType Warping::GetType() const
790 {
791   return SMDSAbs_Face;
792 }
793
794
795 /*
796   Class       : Taper
797   Description : Functor for calculating taper
798 */
799 double Taper::GetValue( const TSequenceOfXYZ& P )
800 {
801   if ( P.size() != 4 )
802     return 0;
803
804   // Compute taper
805   double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2;
806   double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2;
807   double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2;
808   double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2;
809
810   double JA = 0.25 * ( J1 + J2 + J3 + J4 );
811   if ( JA <= Precision::Confusion() )
812     return 0.;
813
814   double T1 = fabs( ( J1 - JA ) / JA );
815   double T2 = fabs( ( J2 - JA ) / JA );
816   double T3 = fabs( ( J3 - JA ) / JA );
817   double T4 = fabs( ( J4 - JA ) / JA );
818
819   return Max( Max( T1, T2 ), Max( T3, T4 ) );
820 }
821
822 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
823 {
824   // the taper is in the range [0.0,1.0]
825   // 0.0  = good (no taper)
826   // 1.0 = bad  (les cotes opposes sont allignes)
827   return Value;
828 }
829
830 SMDSAbs_ElementType Taper::GetType() const
831 {
832   return SMDSAbs_Face;
833 }
834
835
836 /*
837   Class       : Skew
838   Description : Functor for calculating skew in degrees
839 */
840 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
841 {
842   gp_XYZ p12 = ( p2 + p1 ) / 2;
843   gp_XYZ p23 = ( p3 + p2 ) / 2;
844   gp_XYZ p31 = ( p3 + p1 ) / 2;
845
846   gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
847
848   return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
849 }
850
851 double Skew::GetValue( const TSequenceOfXYZ& P )
852 {
853   if ( P.size() != 3 && P.size() != 4 )
854     return 0;
855
856   // Compute skew
857   static double PI2 = PI / 2;
858   if ( P.size() == 3 )
859   {
860     double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
861     double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
862     double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
863
864     return Max( A0, Max( A1, A2 ) ) * 180 / PI;
865   }
866   else
867   {
868     gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2;
869     gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2;
870     gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2;
871     gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2;
872
873     gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
874     double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
875       ? 0 : fabs( PI2 - v1.Angle( v2 ) );
876
877     return A * 180 / PI;
878   }
879 }
880
881 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
882 {
883   // the skew is in the range [0.0,PI/2].
884   // 0.0 = good
885   // PI/2 = bad
886   return Value;
887 }
888
889 SMDSAbs_ElementType Skew::GetType() const
890 {
891   return SMDSAbs_Face;
892 }
893
894
895 /*
896   Class       : Area
897   Description : Functor for calculating area
898 */
899 double Area::GetValue( const TSequenceOfXYZ& P )
900 {
901   gp_Vec aVec1( P(2) - P(1) );
902   gp_Vec aVec2( P(3) - P(1) );
903   gp_Vec SumVec = aVec1 ^ aVec2;
904   for (int i=4; i<=P.size(); i++) {
905     gp_Vec aVec1( P(i-1) - P(1) );
906     gp_Vec aVec2( P(i) - P(1) );
907     gp_Vec tmp = aVec1 ^ aVec2;
908     SumVec.Add(tmp);
909   }
910   return SumVec.Magnitude() * 0.5;
911 }
912
913 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
914 {
915   // meaningless as it is not a quality control functor
916   return Value;
917 }
918
919 SMDSAbs_ElementType Area::GetType() const
920 {
921   return SMDSAbs_Face;
922 }
923
924
925 /*
926   Class       : Length
927   Description : Functor for calculating length off edge
928 */
929 double Length::GetValue( const TSequenceOfXYZ& P )
930 {
931   switch ( P.size() ) {
932   case 2:  return getDistance( P( 1 ), P( 2 ) );
933   case 3:  return getDistance( P( 1 ), P( 2 ) ) + getDistance( P( 2 ), P( 3 ) );
934   default: return 0.;
935   }
936 }
937
938 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
939 {
940   // meaningless as it is not quality control functor
941   return Value;
942 }
943
944 SMDSAbs_ElementType Length::GetType() const
945 {
946   return SMDSAbs_Edge;
947 }
948
949 /*
950   Class       : Length2D
951   Description : Functor for calculating length of edge
952 */
953
954 double Length2D::GetValue( long theElementId)
955 {
956   TSequenceOfXYZ P;
957
958   //cout<<"Length2D::GetValue"<<endl;
959   if (GetPoints(theElementId,P)){
960     //for(int jj=1; jj<=P.size(); jj++)
961     //  cout<<"jj="<<jj<<" P("<<P(jj).X()<<","<<P(jj).Y()<<","<<P(jj).Z()<<")"<<endl;
962
963     double  aVal;// = GetValue( P );
964     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
965     SMDSAbs_ElementType aType = aElem->GetType();
966
967     int len = P.size();
968
969     switch (aType){
970     case SMDSAbs_All:
971     case SMDSAbs_Node:
972     case SMDSAbs_Edge:
973       if (len == 2){
974         aVal = getDistance( P( 1 ), P( 2 ) );
975         break;
976       }
977       else if (len == 3){ // quadratic edge
978         aVal = getDistance(P( 1 ),P( 3 )) + getDistance(P( 3 ),P( 2 ));
979         break;
980       }
981     case SMDSAbs_Face:
982       if (len == 3){ // triangles
983         double L1 = getDistance(P( 1 ),P( 2 ));
984         double L2 = getDistance(P( 2 ),P( 3 ));
985         double L3 = getDistance(P( 3 ),P( 1 ));
986         aVal = Max(L1,Max(L2,L3));
987         break;
988       }
989       else if (len == 4){ // quadrangles
990         double L1 = getDistance(P( 1 ),P( 2 ));
991         double L2 = getDistance(P( 2 ),P( 3 ));
992         double L3 = getDistance(P( 3 ),P( 4 ));
993         double L4 = getDistance(P( 4 ),P( 1 ));
994         aVal = Max(Max(L1,L2),Max(L3,L4));
995         break;
996       }
997       if (len == 6){ // quadratic triangles
998         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
999         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1000         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
1001         aVal = Max(L1,Max(L2,L3));
1002         //cout<<"L1="<<L1<<" L2="<<L2<<"L3="<<L3<<" aVal="<<aVal<<endl;
1003         break;
1004       }
1005       else if (len == 8){ // quadratic quadrangles
1006         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1007         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1008         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
1009         double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1010         aVal = Max(Max(L1,L2),Max(L3,L4));
1011         break;
1012       }
1013     case SMDSAbs_Volume:
1014       if (len == 4){ // tetraidrs
1015         double L1 = getDistance(P( 1 ),P( 2 ));
1016         double L2 = getDistance(P( 2 ),P( 3 ));
1017         double L3 = getDistance(P( 3 ),P( 1 ));
1018         double L4 = getDistance(P( 1 ),P( 4 ));
1019         double L5 = getDistance(P( 2 ),P( 4 ));
1020         double L6 = getDistance(P( 3 ),P( 4 ));
1021         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1022         break;
1023       }
1024       else if (len == 5){ // piramids
1025         double L1 = getDistance(P( 1 ),P( 2 ));
1026         double L2 = getDistance(P( 2 ),P( 3 ));
1027         double L3 = getDistance(P( 3 ),P( 1 ));
1028         double L4 = getDistance(P( 4 ),P( 1 ));
1029         double L5 = getDistance(P( 1 ),P( 5 ));
1030         double L6 = getDistance(P( 2 ),P( 5 ));
1031         double L7 = getDistance(P( 3 ),P( 5 ));
1032         double L8 = getDistance(P( 4 ),P( 5 ));
1033
1034         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1035         aVal = Max(aVal,Max(L7,L8));
1036         break;
1037       }
1038       else if (len == 6){ // pentaidres
1039         double L1 = getDistance(P( 1 ),P( 2 ));
1040         double L2 = getDistance(P( 2 ),P( 3 ));
1041         double L3 = getDistance(P( 3 ),P( 1 ));
1042         double L4 = getDistance(P( 4 ),P( 5 ));
1043         double L5 = getDistance(P( 5 ),P( 6 ));
1044         double L6 = getDistance(P( 6 ),P( 4 ));
1045         double L7 = getDistance(P( 1 ),P( 4 ));
1046         double L8 = getDistance(P( 2 ),P( 5 ));
1047         double L9 = getDistance(P( 3 ),P( 6 ));
1048
1049         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1050         aVal = Max(aVal,Max(Max(L7,L8),L9));
1051         break;
1052       }
1053       else if (len == 8){ // hexaider
1054         double L1 = getDistance(P( 1 ),P( 2 ));
1055         double L2 = getDistance(P( 2 ),P( 3 ));
1056         double L3 = getDistance(P( 3 ),P( 4 ));
1057         double L4 = getDistance(P( 4 ),P( 1 ));
1058         double L5 = getDistance(P( 5 ),P( 6 ));
1059         double L6 = getDistance(P( 6 ),P( 7 ));
1060         double L7 = getDistance(P( 7 ),P( 8 ));
1061         double L8 = getDistance(P( 8 ),P( 5 ));
1062         double L9 = getDistance(P( 1 ),P( 5 ));
1063         double L10= getDistance(P( 2 ),P( 6 ));
1064         double L11= getDistance(P( 3 ),P( 7 ));
1065         double L12= getDistance(P( 4 ),P( 8 ));
1066
1067         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1068         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1069         aVal = Max(aVal,Max(L11,L12));
1070         break;
1071
1072       }
1073
1074       if (len == 10){ // quadratic tetraidrs
1075         double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
1076         double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
1077         double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
1078         double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1079         double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
1080         double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
1081         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1082         break;
1083       }
1084       else if (len == 13){ // quadratic piramids
1085         double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
1086         double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
1087         double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1088         double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1089         double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1090         double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
1091         double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
1092         double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
1093         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1094         aVal = Max(aVal,Max(L7,L8));
1095         break;
1096       }
1097       else if (len == 15){ // quadratic pentaidres
1098         double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
1099         double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
1100         double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1101         double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1102         double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
1103         double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
1104         double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
1105         double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
1106         double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
1107         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1108         aVal = Max(aVal,Max(Max(L7,L8),L9));
1109         break;
1110       }
1111       else if (len == 20){ // quadratic hexaider
1112         double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
1113         double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
1114         double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
1115         double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
1116         double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
1117         double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
1118         double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
1119         double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
1120         double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
1121         double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
1122         double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
1123         double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
1124         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1125         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1126         aVal = Max(aVal,Max(L11,L12));
1127         break;
1128
1129       }
1130
1131     default: aVal=-1;
1132     }
1133
1134     if (aVal <0){
1135       return 0.;
1136     }
1137
1138     if ( myPrecision >= 0 )
1139     {
1140       double prec = pow( 10., (double)( myPrecision ) );
1141       aVal = floor( aVal * prec + 0.5 ) / prec;
1142     }
1143
1144     return aVal;
1145
1146   }
1147   return 0.;
1148 }
1149
1150 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1151 {
1152   // meaningless as it is not quality control functor
1153   return Value;
1154 }
1155
1156 SMDSAbs_ElementType Length2D::GetType() const
1157 {
1158   return SMDSAbs_Face;
1159 }
1160
1161 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
1162   myLength(theLength)
1163 {
1164   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1165   if(thePntId1 > thePntId2){
1166     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1167   }
1168 }
1169
1170 bool Length2D::Value::operator<(const Length2D::Value& x) const{
1171   if(myPntId[0] < x.myPntId[0]) return true;
1172   if(myPntId[0] == x.myPntId[0])
1173     if(myPntId[1] < x.myPntId[1]) return true;
1174   return false;
1175 }
1176
1177 void Length2D::GetValues(TValues& theValues){
1178   TValues aValues;
1179   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1180   for(; anIter->more(); ){
1181     const SMDS_MeshFace* anElem = anIter->next();
1182
1183     if(anElem->IsQuadratic()) {
1184       const SMDS_QuadraticFaceOfNodes* F =
1185         static_cast<const SMDS_QuadraticFaceOfNodes*>(anElem);
1186       // use special nodes iterator
1187       SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
1188       long aNodeId[4];
1189       gp_Pnt P[4];
1190
1191       double aLength;
1192       const SMDS_MeshElement* aNode;
1193       if(anIter->more()){
1194         aNode = anIter->next();
1195         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1196         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1197         aNodeId[0] = aNodeId[1] = aNode->GetID();
1198         aLength = 0;
1199       }
1200       for(; anIter->more(); ){
1201         const SMDS_MeshNode* N1 = static_cast<const SMDS_MeshNode*> (anIter->next());
1202         P[2] = gp_Pnt(N1->X(),N1->Y(),N1->Z());
1203         aNodeId[2] = N1->GetID();
1204         aLength = P[1].Distance(P[2]);
1205         if(!anIter->more()) break;
1206         const SMDS_MeshNode* N2 = static_cast<const SMDS_MeshNode*> (anIter->next());
1207         P[3] = gp_Pnt(N2->X(),N2->Y(),N2->Z());
1208         aNodeId[3] = N2->GetID();
1209         aLength += P[2].Distance(P[3]);
1210         Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1211         Value aValue2(aLength,aNodeId[2],aNodeId[3]);
1212         P[1] = P[3];
1213         aNodeId[1] = aNodeId[3];
1214         theValues.insert(aValue1);
1215         theValues.insert(aValue2);
1216       }
1217       aLength += P[2].Distance(P[0]);
1218       Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1219       Value aValue2(aLength,aNodeId[2],aNodeId[0]);
1220       theValues.insert(aValue1);
1221       theValues.insert(aValue2);
1222     }
1223     else {
1224       SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1225       long aNodeId[2];
1226       gp_Pnt P[3];
1227
1228       double aLength;
1229       const SMDS_MeshElement* aNode;
1230       if(aNodesIter->more()){
1231         aNode = aNodesIter->next();
1232         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1233         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1234         aNodeId[0] = aNodeId[1] = aNode->GetID();
1235         aLength = 0;
1236       }
1237       for(; aNodesIter->more(); ){
1238         aNode = aNodesIter->next();
1239         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1240         long anId = aNode->GetID();
1241         
1242         P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1243         
1244         aLength = P[1].Distance(P[2]);
1245         
1246         Value aValue(aLength,aNodeId[1],anId);
1247         aNodeId[1] = anId;
1248         P[1] = P[2];
1249         theValues.insert(aValue);
1250       }
1251
1252       aLength = P[0].Distance(P[1]);
1253
1254       Value aValue(aLength,aNodeId[0],aNodeId[1]);
1255       theValues.insert(aValue);
1256     }
1257   }
1258 }
1259
1260 /*
1261   Class       : MultiConnection
1262   Description : Functor for calculating number of faces conneted to the edge
1263 */
1264 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1265 {
1266   return 0;
1267 }
1268 double MultiConnection::GetValue( long theId )
1269 {
1270   return getNbMultiConnection( myMesh, theId );
1271 }
1272
1273 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1274 {
1275   // meaningless as it is not quality control functor
1276   return Value;
1277 }
1278
1279 SMDSAbs_ElementType MultiConnection::GetType() const
1280 {
1281   return SMDSAbs_Edge;
1282 }
1283
1284 /*
1285   Class       : MultiConnection2D
1286   Description : Functor for calculating number of faces conneted to the edge
1287 */
1288 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1289 {
1290   return 0;
1291 }
1292
1293 double MultiConnection2D::GetValue( long theElementId )
1294 {
1295   TSequenceOfXYZ P;
1296   int aResult = 0;
1297
1298   if (GetPoints(theElementId,P)){
1299     const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1300     SMDSAbs_ElementType aType = anFaceElem->GetType();
1301
1302     int len = P.size();
1303
1304     TColStd_MapOfInteger aMap;
1305     int aResult = 0;
1306
1307     switch (aType){
1308     case SMDSAbs_All:
1309     case SMDSAbs_Node:
1310     case SMDSAbs_Edge:
1311     case SMDSAbs_Face:
1312       if (len == 3){ // triangles
1313         int Nb[3] = {0,0,0};
1314
1315         int i=0;
1316         SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1317         if ( anIter != 0 ) {
1318           while( anIter->more() ) {
1319             const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1320             if ( aNode == 0 ){
1321               break;
1322             }
1323             SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1324             while( anElemIter->more() ) {
1325               const SMDS_MeshElement* anElem = anElemIter->next();
1326               if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1327                 int anId = anElem->GetID();
1328
1329                 if ( anIter->more() )              // i.e. first node
1330                   aMap.Add( anId );
1331                 else if ( aMap.Contains( anId ) ){
1332                   Nb[i]++;
1333                 }
1334               }
1335               else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1336             }
1337           }
1338         }
1339
1340         aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1341       }
1342       break;
1343     case SMDSAbs_Volume:
1344     default: aResult=0;
1345     }
1346
1347   }
1348   return aResult;//getNbMultiConnection( myMesh, theId );
1349 }
1350
1351 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1352 {
1353   // meaningless as it is not quality control functor
1354   return Value;
1355 }
1356
1357 SMDSAbs_ElementType MultiConnection2D::GetType() const
1358 {
1359   return SMDSAbs_Face;
1360 }
1361
1362 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1363 {
1364   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1365   if(thePntId1 > thePntId2){
1366     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1367   }
1368 }
1369
1370 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1371   if(myPntId[0] < x.myPntId[0]) return true;
1372   if(myPntId[0] == x.myPntId[0])
1373     if(myPntId[1] < x.myPntId[1]) return true;
1374   return false;
1375 }
1376
1377 void MultiConnection2D::GetValues(MValues& theValues){
1378   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1379   for(; anIter->more(); ){
1380     const SMDS_MeshFace* anElem = anIter->next();
1381     SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1382     long aNodeId[3];
1383
1384     //int aNbConnects=0;
1385     const SMDS_MeshNode* aNode0;
1386     const SMDS_MeshNode* aNode1;
1387     const SMDS_MeshNode* aNode2;
1388     if(aNodesIter->more()){
1389       aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1390       aNode1 = aNode0;
1391       const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1392       aNodeId[0] = aNodeId[1] = aNodes->GetID();
1393     }
1394     for(; aNodesIter->more(); ) {
1395       aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1396       long anId = aNode2->GetID();
1397       aNodeId[2] = anId;
1398
1399       Value aValue(aNodeId[1],aNodeId[2]);
1400       MValues::iterator aItr = theValues.find(aValue);
1401       if (aItr != theValues.end()){
1402         aItr->second += 1;
1403         //aNbConnects = nb;
1404       }
1405       else {
1406         theValues[aValue] = 1;
1407         //aNbConnects = 1;
1408       }
1409       //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1410       aNodeId[1] = aNodeId[2];
1411       aNode1 = aNode2;
1412     }
1413     Value aValue(aNodeId[0],aNodeId[2]);
1414     MValues::iterator aItr = theValues.find(aValue);
1415     if (aItr != theValues.end()) {
1416       aItr->second += 1;
1417       //aNbConnects = nb;
1418     }
1419     else {
1420       theValues[aValue] = 1;
1421       //aNbConnects = 1;
1422     }
1423     //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1424   }
1425
1426 }
1427
1428 /*
1429                             PREDICATES
1430 */
1431
1432 /*
1433   Class       : BadOrientedVolume
1434   Description : Predicate bad oriented volumes
1435 */
1436
1437 BadOrientedVolume::BadOrientedVolume()
1438 {
1439   myMesh = 0;
1440 }
1441
1442 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1443 {
1444   myMesh = theMesh;
1445 }
1446
1447 bool BadOrientedVolume::IsSatisfy( long theId )
1448 {
1449   if ( myMesh == 0 )
1450     return false;
1451
1452   SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1453   return !vTool.IsForward();
1454 }
1455
1456 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1457 {
1458   return SMDSAbs_Volume;
1459 }
1460
1461
1462
1463 /*
1464   Class       : FreeBorders
1465   Description : Predicate for free borders
1466 */
1467
1468 FreeBorders::FreeBorders()
1469 {
1470   myMesh = 0;
1471 }
1472
1473 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1474 {
1475   myMesh = theMesh;
1476 }
1477
1478 bool FreeBorders::IsSatisfy( long theId )
1479 {
1480   return getNbMultiConnection( myMesh, theId ) == 1;
1481 }
1482
1483 SMDSAbs_ElementType FreeBorders::GetType() const
1484 {
1485   return SMDSAbs_Edge;
1486 }
1487
1488
1489 /*
1490   Class       : FreeEdges
1491   Description : Predicate for free Edges
1492 */
1493 FreeEdges::FreeEdges()
1494 {
1495   myMesh = 0;
1496 }
1497
1498 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1499 {
1500   myMesh = theMesh;
1501 }
1502
1503 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  )
1504 {
1505   TColStd_MapOfInteger aMap;
1506   for ( int i = 0; i < 2; i++ )
1507   {
1508     SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1509     while( anElemIter->more() )
1510     {
1511       const SMDS_MeshElement* anElem = anElemIter->next();
1512       if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1513       {
1514         int anId = anElem->GetID();
1515
1516         if ( i == 0 )
1517           aMap.Add( anId );
1518         else if ( aMap.Contains( anId ) && anId != theFaceId )
1519           return false;
1520       }
1521     }
1522   }
1523   return true;
1524 }
1525
1526 bool FreeEdges::IsSatisfy( long theId )
1527 {
1528   if ( myMesh == 0 )
1529     return false;
1530
1531   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1532   if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1533     return false;
1534
1535   SMDS_ElemIteratorPtr anIter;
1536   if ( aFace->IsQuadratic() ) {
1537     anIter = static_cast<const SMDS_QuadraticFaceOfNodes*>
1538       (aFace)->interlacedNodesElemIterator();
1539   }
1540   else {
1541     anIter = aFace->nodesIterator();
1542   }
1543   if ( anIter != 0 )
1544     return false;
1545
1546   int i = 0, nbNodes = aFace->NbNodes();
1547   vector <const SMDS_MeshNode*> aNodes( nbNodes+1 );
1548   while( anIter->more() )
1549   {
1550     const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1551     if ( aNode == 0 )
1552       return false;
1553     aNodes[ i++ ] = aNode;
1554   }
1555   aNodes[ nbNodes ] = aNodes[ 0 ];
1556
1557   for ( i = 0; i < nbNodes; i++ )
1558     if ( IsFreeEdge( &aNodes[ i ], theId ) )
1559       return true;
1560
1561   return false;
1562 }
1563
1564 SMDSAbs_ElementType FreeEdges::GetType() const
1565 {
1566   return SMDSAbs_Face;
1567 }
1568
1569 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1570   myElemId(theElemId)
1571 {
1572   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1573   if(thePntId1 > thePntId2){
1574     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1575   }
1576 }
1577
1578 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1579   if(myPntId[0] < x.myPntId[0]) return true;
1580   if(myPntId[0] == x.myPntId[0])
1581     if(myPntId[1] < x.myPntId[1]) return true;
1582   return false;
1583 }
1584
1585 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1586                           FreeEdges::TBorders& theRegistry,
1587                           FreeEdges::TBorders& theContainer)
1588 {
1589   if(theRegistry.find(theBorder) == theRegistry.end()){
1590     theRegistry.insert(theBorder);
1591     theContainer.insert(theBorder);
1592   }else{
1593     theContainer.erase(theBorder);
1594   }
1595 }
1596
1597 void FreeEdges::GetBoreders(TBorders& theBorders)
1598 {
1599   TBorders aRegistry;
1600   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1601   for(; anIter->more(); ){
1602     const SMDS_MeshFace* anElem = anIter->next();
1603     long anElemId = anElem->GetID();
1604     SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1605     long aNodeId[2];
1606     const SMDS_MeshElement* aNode;
1607     if(aNodesIter->more()){
1608       aNode = aNodesIter->next();
1609       aNodeId[0] = aNodeId[1] = aNode->GetID();
1610     }
1611     for(; aNodesIter->more(); ){
1612       aNode = aNodesIter->next();
1613       long anId = aNode->GetID();
1614       Border aBorder(anElemId,aNodeId[1],anId);
1615       aNodeId[1] = anId;
1616       //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1617       UpdateBorders(aBorder,aRegistry,theBorders);
1618     }
1619     Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1620     //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1621     UpdateBorders(aBorder,aRegistry,theBorders);
1622   }
1623   //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1624 }
1625
1626 /*
1627   Class       : RangeOfIds
1628   Description : Predicate for Range of Ids.
1629                 Range may be specified with two ways.
1630                 1. Using AddToRange method
1631                 2. With SetRangeStr method. Parameter of this method is a string
1632                    like as "1,2,3,50-60,63,67,70-"
1633 */
1634
1635 //=======================================================================
1636 // name    : RangeOfIds
1637 // Purpose : Constructor
1638 //=======================================================================
1639 RangeOfIds::RangeOfIds()
1640 {
1641   myMesh = 0;
1642   myType = SMDSAbs_All;
1643 }
1644
1645 //=======================================================================
1646 // name    : SetMesh
1647 // Purpose : Set mesh
1648 //=======================================================================
1649 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1650 {
1651   myMesh = theMesh;
1652 }
1653
1654 //=======================================================================
1655 // name    : AddToRange
1656 // Purpose : Add ID to the range
1657 //=======================================================================
1658 bool RangeOfIds::AddToRange( long theEntityId )
1659 {
1660   myIds.Add( theEntityId );
1661   return true;
1662 }
1663
1664 //=======================================================================
1665 // name    : GetRangeStr
1666 // Purpose : Get range as a string.
1667 //           Example: "1,2,3,50-60,63,67,70-"
1668 //=======================================================================
1669 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1670 {
1671   theResStr.Clear();
1672
1673   TColStd_SequenceOfInteger     anIntSeq;
1674   TColStd_SequenceOfAsciiString aStrSeq;
1675
1676   TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1677   for ( ; anIter.More(); anIter.Next() )
1678   {
1679     int anId = anIter.Key();
1680     TCollection_AsciiString aStr( anId );
1681     anIntSeq.Append( anId );
1682     aStrSeq.Append( aStr );
1683   }
1684
1685   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1686   {
1687     int aMinId = myMin( i );
1688     int aMaxId = myMax( i );
1689
1690     TCollection_AsciiString aStr;
1691     if ( aMinId != IntegerFirst() )
1692       aStr += aMinId;
1693
1694     aStr += "-";
1695
1696     if ( aMaxId != IntegerLast() )
1697       aStr += aMaxId;
1698
1699     // find position of the string in result sequence and insert string in it
1700     if ( anIntSeq.Length() == 0 )
1701     {
1702       anIntSeq.Append( aMinId );
1703       aStrSeq.Append( aStr );
1704     }
1705     else
1706     {
1707       if ( aMinId < anIntSeq.First() )
1708       {
1709         anIntSeq.Prepend( aMinId );
1710         aStrSeq.Prepend( aStr );
1711       }
1712       else if ( aMinId > anIntSeq.Last() )
1713       {
1714         anIntSeq.Append( aMinId );
1715         aStrSeq.Append( aStr );
1716       }
1717       else
1718         for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1719           if ( aMinId < anIntSeq( j ) )
1720           {
1721             anIntSeq.InsertBefore( j, aMinId );
1722             aStrSeq.InsertBefore( j, aStr );
1723             break;
1724           }
1725     }
1726   }
1727
1728   if ( aStrSeq.Length() == 0 )
1729     return;
1730
1731   theResStr = aStrSeq( 1 );
1732   for ( int j = 2, k = aStrSeq.Length(); j <= k; j++  )
1733   {
1734     theResStr += ",";
1735     theResStr += aStrSeq( j );
1736   }
1737 }
1738
1739 //=======================================================================
1740 // name    : SetRangeStr
1741 // Purpose : Define range with string
1742 //           Example of entry string: "1,2,3,50-60,63,67,70-"
1743 //=======================================================================
1744 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1745 {
1746   myMin.Clear();
1747   myMax.Clear();
1748   myIds.Clear();
1749
1750   TCollection_AsciiString aStr = theStr;
1751   aStr.RemoveAll( ' ' );
1752   aStr.RemoveAll( '\t' );
1753
1754   for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1755     aStr.Remove( aPos, 2 );
1756
1757   TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1758   int i = 1;
1759   while ( tmpStr != "" )
1760   {
1761     tmpStr = aStr.Token( ",", i++ );
1762     int aPos = tmpStr.Search( '-' );
1763
1764     if ( aPos == -1 )
1765     {
1766       if ( tmpStr.IsIntegerValue() )
1767         myIds.Add( tmpStr.IntegerValue() );
1768       else
1769         return false;
1770     }
1771     else
1772     {
1773       TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1774       TCollection_AsciiString aMinStr = tmpStr;
1775
1776       while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1777       while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1778
1779       if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1780            !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1781         return false;
1782
1783       myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1784       myMax.Append( aMaxStr.IsEmpty() ? IntegerLast()  : aMaxStr.IntegerValue() );
1785     }
1786   }
1787
1788   return true;
1789 }
1790
1791 //=======================================================================
1792 // name    : GetType
1793 // Purpose : Get type of supported entities
1794 //=======================================================================
1795 SMDSAbs_ElementType RangeOfIds::GetType() const
1796 {
1797   return myType;
1798 }
1799
1800 //=======================================================================
1801 // name    : SetType
1802 // Purpose : Set type of supported entities
1803 //=======================================================================
1804 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1805 {
1806   myType = theType;
1807 }
1808
1809 //=======================================================================
1810 // name    : IsSatisfy
1811 // Purpose : Verify whether entity satisfies to this rpedicate
1812 //=======================================================================
1813 bool RangeOfIds::IsSatisfy( long theId )
1814 {
1815   if ( !myMesh )
1816     return false;
1817
1818   if ( myType == SMDSAbs_Node )
1819   {
1820     if ( myMesh->FindNode( theId ) == 0 )
1821       return false;
1822   }
1823   else
1824   {
1825     const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1826     if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1827       return false;
1828   }
1829
1830   if ( myIds.Contains( theId ) )
1831     return true;
1832
1833   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1834     if ( theId >= myMin( i ) && theId <= myMax( i ) )
1835       return true;
1836
1837   return false;
1838 }
1839
1840 /*
1841   Class       : Comparator
1842   Description : Base class for comparators
1843 */
1844 Comparator::Comparator():
1845   myMargin(0)
1846 {}
1847
1848 Comparator::~Comparator()
1849 {}
1850
1851 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1852 {
1853   if ( myFunctor )
1854     myFunctor->SetMesh( theMesh );
1855 }
1856
1857 void Comparator::SetMargin( double theValue )
1858 {
1859   myMargin = theValue;
1860 }
1861
1862 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1863 {
1864   myFunctor = theFunct;
1865 }
1866
1867 SMDSAbs_ElementType Comparator::GetType() const
1868 {
1869   return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1870 }
1871
1872 double Comparator::GetMargin()
1873 {
1874   return myMargin;
1875 }
1876
1877
1878 /*
1879   Class       : LessThan
1880   Description : Comparator "<"
1881 */
1882 bool LessThan::IsSatisfy( long theId )
1883 {
1884   return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1885 }
1886
1887
1888 /*
1889   Class       : MoreThan
1890   Description : Comparator ">"
1891 */
1892 bool MoreThan::IsSatisfy( long theId )
1893 {
1894   return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1895 }
1896
1897
1898 /*
1899   Class       : EqualTo
1900   Description : Comparator "="
1901 */
1902 EqualTo::EqualTo():
1903   myToler(Precision::Confusion())
1904 {}
1905
1906 bool EqualTo::IsSatisfy( long theId )
1907 {
1908   return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1909 }
1910
1911 void EqualTo::SetTolerance( double theToler )
1912 {
1913   myToler = theToler;
1914 }
1915
1916 double EqualTo::GetTolerance()
1917 {
1918   return myToler;
1919 }
1920
1921 /*
1922   Class       : LogicalNOT
1923   Description : Logical NOT predicate
1924 */
1925 LogicalNOT::LogicalNOT()
1926 {}
1927
1928 LogicalNOT::~LogicalNOT()
1929 {}
1930
1931 bool LogicalNOT::IsSatisfy( long theId )
1932 {
1933   return myPredicate && !myPredicate->IsSatisfy( theId );
1934 }
1935
1936 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1937 {
1938   if ( myPredicate )
1939     myPredicate->SetMesh( theMesh );
1940 }
1941
1942 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1943 {
1944   myPredicate = thePred;
1945 }
1946
1947 SMDSAbs_ElementType LogicalNOT::GetType() const
1948 {
1949   return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1950 }
1951
1952
1953 /*
1954   Class       : LogicalBinary
1955   Description : Base class for binary logical predicate
1956 */
1957 LogicalBinary::LogicalBinary()
1958 {}
1959
1960 LogicalBinary::~LogicalBinary()
1961 {}
1962
1963 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1964 {
1965   if ( myPredicate1 )
1966     myPredicate1->SetMesh( theMesh );
1967
1968   if ( myPredicate2 )
1969     myPredicate2->SetMesh( theMesh );
1970 }
1971
1972 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1973 {
1974   myPredicate1 = thePredicate;
1975 }
1976
1977 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1978 {
1979   myPredicate2 = thePredicate;
1980 }
1981
1982 SMDSAbs_ElementType LogicalBinary::GetType() const
1983 {
1984   if ( !myPredicate1 || !myPredicate2 )
1985     return SMDSAbs_All;
1986
1987   SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1988   SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1989
1990   return aType1 == aType2 ? aType1 : SMDSAbs_All;
1991 }
1992
1993
1994 /*
1995   Class       : LogicalAND
1996   Description : Logical AND
1997 */
1998 bool LogicalAND::IsSatisfy( long theId )
1999 {
2000   return
2001     myPredicate1 &&
2002     myPredicate2 &&
2003     myPredicate1->IsSatisfy( theId ) &&
2004     myPredicate2->IsSatisfy( theId );
2005 }
2006
2007
2008 /*
2009   Class       : LogicalOR
2010   Description : Logical OR
2011 */
2012 bool LogicalOR::IsSatisfy( long theId )
2013 {
2014   return
2015     myPredicate1 &&
2016     myPredicate2 &&
2017     myPredicate1->IsSatisfy( theId ) ||
2018     myPredicate2->IsSatisfy( theId );
2019 }
2020
2021
2022 /*
2023                               FILTER
2024 */
2025
2026 Filter::Filter()
2027 {}
2028
2029 Filter::~Filter()
2030 {}
2031
2032 void Filter::SetPredicate( PredicatePtr thePredicate )
2033 {
2034   myPredicate = thePredicate;
2035 }
2036
2037 template<class TElement, class TIterator, class TPredicate>
2038 inline void FillSequence(const TIterator& theIterator,
2039                          TPredicate& thePredicate,
2040                          Filter::TIdSequence& theSequence)
2041 {
2042   if ( theIterator ) {
2043     while( theIterator->more() ) {
2044       TElement anElem = theIterator->next();
2045       long anId = anElem->GetID();
2046       if ( thePredicate->IsSatisfy( anId ) )
2047         theSequence.push_back( anId );
2048     }
2049   }
2050 }
2051
2052 void
2053 Filter::
2054 GetElementsId( const SMDS_Mesh* theMesh,
2055                PredicatePtr thePredicate,
2056                TIdSequence& theSequence )
2057 {
2058   theSequence.clear();
2059
2060   if ( !theMesh || !thePredicate )
2061     return;
2062
2063   thePredicate->SetMesh( theMesh );
2064
2065   SMDSAbs_ElementType aType = thePredicate->GetType();
2066   switch(aType){
2067   case SMDSAbs_Node:
2068     FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
2069     break;
2070   case SMDSAbs_Edge:
2071     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
2072     break;
2073   case SMDSAbs_Face:
2074     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
2075     break;
2076   case SMDSAbs_Volume:
2077     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
2078     break;
2079   case SMDSAbs_All:
2080     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
2081     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
2082     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
2083     break;
2084   }
2085 }
2086
2087 void
2088 Filter::GetElementsId( const SMDS_Mesh* theMesh,
2089                        Filter::TIdSequence& theSequence )
2090 {
2091   GetElementsId(theMesh,myPredicate,theSequence);
2092 }
2093
2094 /*
2095                               ManifoldPart
2096 */
2097
2098 typedef std::set<SMDS_MeshFace*>                    TMapOfFacePtr;
2099
2100 /*
2101    Internal class Link
2102 */
2103
2104 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
2105                           SMDS_MeshNode* theNode2 )
2106 {
2107   myNode1 = theNode1;
2108   myNode2 = theNode2;
2109 }
2110
2111 ManifoldPart::Link::~Link()
2112 {
2113   myNode1 = 0;
2114   myNode2 = 0;
2115 }
2116
2117 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
2118 {
2119   if ( myNode1 == theLink.myNode1 &&
2120        myNode2 == theLink.myNode2 )
2121     return true;
2122   else if ( myNode1 == theLink.myNode2 &&
2123             myNode2 == theLink.myNode1 )
2124     return true;
2125   else
2126     return false;
2127 }
2128
2129 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
2130 {
2131   if(myNode1 < x.myNode1) return true;
2132   if(myNode1 == x.myNode1)
2133     if(myNode2 < x.myNode2) return true;
2134   return false;
2135 }
2136
2137 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
2138                             const ManifoldPart::Link& theLink2 )
2139 {
2140   return theLink1.IsEqual( theLink2 );
2141 }
2142
2143 ManifoldPart::ManifoldPart()
2144 {
2145   myMesh = 0;
2146   myAngToler = Precision::Angular();
2147   myIsOnlyManifold = true;
2148 }
2149
2150 ManifoldPart::~ManifoldPart()
2151 {
2152   myMesh = 0;
2153 }
2154
2155 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
2156 {
2157   myMesh = theMesh;
2158   process();
2159 }
2160
2161 SMDSAbs_ElementType ManifoldPart::GetType() const
2162 { return SMDSAbs_Face; }
2163
2164 bool ManifoldPart::IsSatisfy( long theElementId )
2165 {
2166   return myMapIds.Contains( theElementId );
2167 }
2168
2169 void ManifoldPart::SetAngleTolerance( const double theAngToler )
2170 { myAngToler = theAngToler; }
2171
2172 double ManifoldPart::GetAngleTolerance() const
2173 { return myAngToler; }
2174
2175 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
2176 { myIsOnlyManifold = theIsOnly; }
2177
2178 void ManifoldPart::SetStartElem( const long  theStartId )
2179 { myStartElemId = theStartId; }
2180
2181 bool ManifoldPart::process()
2182 {
2183   myMapIds.Clear();
2184   myMapBadGeomIds.Clear();
2185
2186   myAllFacePtr.clear();
2187   myAllFacePtrIntDMap.clear();
2188   if ( !myMesh )
2189     return false;
2190
2191   // collect all faces into own map
2192   SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
2193   for (; anFaceItr->more(); )
2194   {
2195     SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
2196     myAllFacePtr.push_back( aFacePtr );
2197     myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
2198   }
2199
2200   SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
2201   if ( !aStartFace )
2202     return false;
2203
2204   // the map of non manifold links and bad geometry
2205   TMapOfLink aMapOfNonManifold;
2206   TColStd_MapOfInteger aMapOfTreated;
2207
2208   // begin cycle on faces from start index and run on vector till the end
2209   //  and from begin to start index to cover whole vector
2210   const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
2211   bool isStartTreat = false;
2212   for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
2213   {
2214     if ( fi == aStartIndx )
2215       isStartTreat = true;
2216     // as result next time when fi will be equal to aStartIndx
2217
2218     SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
2219     if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
2220       continue;
2221
2222     aMapOfTreated.Add( aFacePtr->GetID() );
2223     TColStd_MapOfInteger aResFaces;
2224     if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
2225                          aMapOfNonManifold, aResFaces ) )
2226       continue;
2227     TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
2228     for ( ; anItr.More(); anItr.Next() )
2229     {
2230       int aFaceId = anItr.Key();
2231       aMapOfTreated.Add( aFaceId );
2232       myMapIds.Add( aFaceId );
2233     }
2234
2235     if ( fi == ( myAllFacePtr.size() - 1 ) )
2236       fi = 0;
2237   } // end run on vector of faces
2238   return !myMapIds.IsEmpty();
2239 }
2240
2241 static void getLinks( const SMDS_MeshFace* theFace,
2242                       ManifoldPart::TVectorOfLink& theLinks )
2243 {
2244   int aNbNode = theFace->NbNodes();
2245   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2246   int i = 1;
2247   SMDS_MeshNode* aNode = 0;
2248   for ( ; aNodeItr->more() && i <= aNbNode; )
2249   {
2250
2251     SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
2252     if ( i == 1 )
2253       aNode = aN1;
2254     i++;
2255     SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
2256     i++;
2257     ManifoldPart::Link aLink( aN1, aN2 );
2258     theLinks.push_back( aLink );
2259   }
2260 }
2261
2262 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
2263 {
2264   gp_XYZ n;
2265   int aNbNode = theFace->NbNodes();
2266   TColgp_Array1OfXYZ anArrOfXYZ(1,4);
2267   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2268   int i = 1;
2269   for ( ; aNodeItr->more() && i <= 4; i++ ) {
2270     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2271     anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2272   }
2273
2274   gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2275   gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2276   n  = q1 ^ q2;
2277   if ( aNbNode > 3 ) {
2278     gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2279     n += q2 ^ q3;
2280   }
2281   double len = n.Modulus();
2282   if ( len > 0 )
2283     n /= len;
2284
2285   return n;
2286 }
2287
2288 bool ManifoldPart::findConnected
2289                  ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2290                   SMDS_MeshFace*                           theStartFace,
2291                   ManifoldPart::TMapOfLink&                theNonManifold,
2292                   TColStd_MapOfInteger&                    theResFaces )
2293 {
2294   theResFaces.Clear();
2295   if ( !theAllFacePtrInt.size() )
2296     return false;
2297
2298   if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2299   {
2300     myMapBadGeomIds.Add( theStartFace->GetID() );
2301     return false;
2302   }
2303
2304   ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2305   ManifoldPart::TVectorOfLink aSeqOfBoundary;
2306   theResFaces.Add( theStartFace->GetID() );
2307   ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2308
2309   expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2310                  aDMapLinkFace, theNonManifold, theStartFace );
2311
2312   bool isDone = false;
2313   while ( !isDone && aMapOfBoundary.size() != 0 )
2314   {
2315     bool isToReset = false;
2316     ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2317     for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2318     {
2319       ManifoldPart::Link aLink = *pLink;
2320       if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2321         continue;
2322       // each link could be treated only once
2323       aMapToSkip.insert( aLink );
2324
2325       ManifoldPart::TVectorOfFacePtr aFaces;
2326       // find next
2327       if ( myIsOnlyManifold &&
2328            (theNonManifold.find( aLink ) != theNonManifold.end()) )
2329         continue;
2330       else
2331       {
2332         getFacesByLink( aLink, aFaces );
2333         // filter the element to keep only indicated elements
2334         ManifoldPart::TVectorOfFacePtr aFiltered;
2335         ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2336         for ( ; pFace != aFaces.end(); ++pFace )
2337         {
2338           SMDS_MeshFace* aFace = *pFace;
2339           if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2340             aFiltered.push_back( aFace );
2341         }
2342         aFaces = aFiltered;
2343         if ( aFaces.size() < 2 )  // no neihgbour faces
2344           continue;
2345         else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2346         {
2347           theNonManifold.insert( aLink );
2348           continue;
2349         }
2350       }
2351
2352       // compare normal with normals of neighbor element
2353       SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2354       ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2355       for ( ; pFace != aFaces.end(); ++pFace )
2356       {
2357         SMDS_MeshFace* aNextFace = *pFace;
2358         if ( aPrevFace == aNextFace )
2359           continue;
2360         int anNextFaceID = aNextFace->GetID();
2361         if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2362          // should not be with non manifold restriction. probably bad topology
2363           continue;
2364         // check if face was treated and skipped
2365         if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2366              !isInPlane( aPrevFace, aNextFace ) )
2367           continue;
2368         // add new element to connected and extend the boundaries.
2369         theResFaces.Add( anNextFaceID );
2370         expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2371                         aDMapLinkFace, theNonManifold, aNextFace );
2372         isToReset = true;
2373       }
2374     }
2375     isDone = !isToReset;
2376   }
2377
2378   return !theResFaces.IsEmpty();
2379 }
2380
2381 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2382                               const SMDS_MeshFace* theFace2 )
2383 {
2384   gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2385   gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2386   if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2387   {
2388     myMapBadGeomIds.Add( theFace2->GetID() );
2389     return false;
2390   }
2391   if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2392     return true;
2393
2394   return false;
2395 }
2396
2397 void ManifoldPart::expandBoundary
2398                    ( ManifoldPart::TMapOfLink&            theMapOfBoundary,
2399                      ManifoldPart::TVectorOfLink&         theSeqOfBoundary,
2400                      ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2401                      ManifoldPart::TMapOfLink&            theNonManifold,
2402                      SMDS_MeshFace*                       theNextFace ) const
2403 {
2404   ManifoldPart::TVectorOfLink aLinks;
2405   getLinks( theNextFace, aLinks );
2406   int aNbLink = (int)aLinks.size();
2407   for ( int i = 0; i < aNbLink; i++ )
2408   {
2409     ManifoldPart::Link aLink = aLinks[ i ];
2410     if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2411       continue;
2412     if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2413     {
2414       if ( myIsOnlyManifold )
2415       {
2416         // remove from boundary
2417         theMapOfBoundary.erase( aLink );
2418         ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2419         for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2420         {
2421           ManifoldPart::Link aBoundLink = *pLink;
2422           if ( aBoundLink.IsEqual( aLink ) )
2423           {
2424             theSeqOfBoundary.erase( pLink );
2425             break;
2426           }
2427         }
2428       }
2429     }
2430     else
2431     {
2432       theMapOfBoundary.insert( aLink );
2433       theSeqOfBoundary.push_back( aLink );
2434       theDMapLinkFacePtr[ aLink ] = theNextFace;
2435     }
2436   }
2437 }
2438
2439 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2440                                    ManifoldPart::TVectorOfFacePtr& theFaces ) const
2441 {
2442   SMDS_Mesh::SetOfFaces aSetOfFaces;
2443   // take all faces that shared first node
2444   SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2445   for ( ; anItr->more(); )
2446   {
2447     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2448     if ( !aFace )
2449       continue;
2450     aSetOfFaces.Add( aFace );
2451   }
2452   // take all faces that shared second node
2453   anItr = theLink.myNode2->facesIterator();
2454   // find the common part of two sets
2455   for ( ; anItr->more(); )
2456   {
2457     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2458     if ( aSetOfFaces.Contains( aFace ) )
2459       theFaces.push_back( aFace );
2460   }
2461 }
2462
2463
2464 /*
2465    ElementsOnSurface
2466 */
2467
2468 ElementsOnSurface::ElementsOnSurface()
2469 {
2470   myMesh = 0;
2471   myIds.Clear();
2472   myType = SMDSAbs_All;
2473   mySurf.Nullify();
2474   myToler = Precision::Confusion();
2475 }
2476
2477 ElementsOnSurface::~ElementsOnSurface()
2478 {
2479   myMesh = 0;
2480 }
2481
2482 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2483 {
2484   if ( myMesh == theMesh )
2485     return;
2486   myMesh = theMesh;
2487   myIds.Clear();
2488   process();
2489 }
2490
2491 bool ElementsOnSurface::IsSatisfy( long theElementId )
2492 {
2493   return myIds.Contains( theElementId );
2494 }
2495
2496 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2497 { return myType; }
2498
2499 void ElementsOnSurface::SetTolerance( const double theToler )
2500 { myToler = theToler; }
2501
2502 double ElementsOnSurface::GetTolerance() const
2503 {
2504   return myToler;
2505 }
2506
2507 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2508                                     const SMDSAbs_ElementType theType )
2509 {
2510   myType = theType;
2511   mySurf.Nullify();
2512   if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2513   {
2514     mySurf.Nullify();
2515     return;
2516   }
2517   TopoDS_Face aFace = TopoDS::Face( theShape );
2518   mySurf = BRep_Tool::Surface( aFace );
2519 }
2520
2521 void ElementsOnSurface::process()
2522 {
2523   myIds.Clear();
2524   if ( mySurf.IsNull() )
2525     return;
2526
2527   if ( myMesh == 0 )
2528     return;
2529
2530   if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2531   {
2532     SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2533     for(; anIter->more(); )
2534       process( anIter->next() );
2535   }
2536
2537   if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2538   {
2539     SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2540     for(; anIter->more(); )
2541       process( anIter->next() );
2542   }
2543
2544   if ( myType == SMDSAbs_Node )
2545   {
2546     SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2547     for(; anIter->more(); )
2548       process( anIter->next() );
2549   }
2550 }
2551
2552 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2553 {
2554   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2555   bool isSatisfy = true;
2556   for ( ; aNodeItr->more(); )
2557   {
2558     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2559     if ( !isOnSurface( aNode ) )
2560     {
2561       isSatisfy = false;
2562       break;
2563     }
2564   }
2565   if ( isSatisfy )
2566     myIds.Add( theElemPtr->GetID() );
2567 }
2568
2569 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2570 {
2571   if ( mySurf.IsNull() )
2572     return false;
2573
2574   gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2575   double aToler2 = myToler * myToler;
2576   if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2577   {
2578     gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2579     if ( aPln.SquareDistance( aPnt ) > aToler2 )
2580       return false;
2581   }
2582   else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2583   {
2584     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2585     double aRad = aCyl.Radius();
2586     gp_Ax3 anAxis = aCyl.Position();
2587     gp_XYZ aLoc = aCyl.Location().XYZ();
2588     double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2589     double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2590     if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )
2591       return false;
2592   }
2593   else
2594     return false;
2595
2596   return true;
2597 }