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