Salome HOME
Using "long double" instead i"nt"
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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     //BUG SWP12743
880     if ( A < Precision::Angular() )
881       return 0.;
882
883     return A * 180. / PI;
884   }
885 }
886
887 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
888 {
889   // the skew is in the range [0.0,PI/2].
890   // 0.0 = good
891   // PI/2 = bad
892   return Value;
893 }
894
895 SMDSAbs_ElementType Skew::GetType() const
896 {
897   return SMDSAbs_Face;
898 }
899
900
901 /*
902   Class       : Area
903   Description : Functor for calculating area
904 */
905 double Area::GetValue( const TSequenceOfXYZ& P )
906 {
907   gp_Vec aVec1( P(2) - P(1) );
908   gp_Vec aVec2( P(3) - P(1) );
909   gp_Vec SumVec = aVec1 ^ aVec2;
910   for (int i=4; i<=P.size(); i++) {
911     gp_Vec aVec1( P(i-1) - P(1) );
912     gp_Vec aVec2( P(i) - P(1) );
913     gp_Vec tmp = aVec1 ^ aVec2;
914     SumVec.Add(tmp);
915   }
916   return SumVec.Magnitude() * 0.5;
917 }
918
919 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
920 {
921   // meaningless as it is not a quality control functor
922   return Value;
923 }
924
925 SMDSAbs_ElementType Area::GetType() const
926 {
927   return SMDSAbs_Face;
928 }
929
930
931 /*
932   Class       : Length
933   Description : Functor for calculating length off edge
934 */
935 double Length::GetValue( const TSequenceOfXYZ& P )
936 {
937   switch ( P.size() ) {
938   case 2:  return getDistance( P( 1 ), P( 2 ) );
939   case 3:  return getDistance( P( 1 ), P( 2 ) ) + getDistance( P( 2 ), P( 3 ) );
940   default: return 0.;
941   }
942 }
943
944 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
945 {
946   // meaningless as it is not quality control functor
947   return Value;
948 }
949
950 SMDSAbs_ElementType Length::GetType() const
951 {
952   return SMDSAbs_Edge;
953 }
954
955 /*
956   Class       : Length2D
957   Description : Functor for calculating length of edge
958 */
959
960 double Length2D::GetValue( long theElementId)
961 {
962   TSequenceOfXYZ P;
963
964   //cout<<"Length2D::GetValue"<<endl;
965   if (GetPoints(theElementId,P)){
966     //for(int jj=1; jj<=P.size(); jj++)
967     //  cout<<"jj="<<jj<<" P("<<P(jj).X()<<","<<P(jj).Y()<<","<<P(jj).Z()<<")"<<endl;
968
969     double  aVal;// = GetValue( P );
970     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
971     SMDSAbs_ElementType aType = aElem->GetType();
972
973     int len = P.size();
974
975     switch (aType){
976     case SMDSAbs_All:
977     case SMDSAbs_Node:
978     case SMDSAbs_Edge:
979       if (len == 2){
980         aVal = getDistance( P( 1 ), P( 2 ) );
981         break;
982       }
983       else if (len == 3){ // quadratic edge
984         aVal = getDistance(P( 1 ),P( 3 )) + getDistance(P( 3 ),P( 2 ));
985         break;
986       }
987     case SMDSAbs_Face:
988       if (len == 3){ // triangles
989         double L1 = getDistance(P( 1 ),P( 2 ));
990         double L2 = getDistance(P( 2 ),P( 3 ));
991         double L3 = getDistance(P( 3 ),P( 1 ));
992         aVal = Max(L1,Max(L2,L3));
993         break;
994       }
995       else if (len == 4){ // quadrangles
996         double L1 = getDistance(P( 1 ),P( 2 ));
997         double L2 = getDistance(P( 2 ),P( 3 ));
998         double L3 = getDistance(P( 3 ),P( 4 ));
999         double L4 = getDistance(P( 4 ),P( 1 ));
1000         aVal = Max(Max(L1,L2),Max(L3,L4));
1001         break;
1002       }
1003       if (len == 6){ // quadratic triangles
1004         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1005         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1006         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
1007         aVal = Max(L1,Max(L2,L3));
1008         //cout<<"L1="<<L1<<" L2="<<L2<<"L3="<<L3<<" aVal="<<aVal<<endl;
1009         break;
1010       }
1011       else if (len == 8){ // quadratic quadrangles
1012         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1013         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1014         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
1015         double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1016         aVal = Max(Max(L1,L2),Max(L3,L4));
1017         break;
1018       }
1019     case SMDSAbs_Volume:
1020       if (len == 4){ // tetraidrs
1021         double L1 = getDistance(P( 1 ),P( 2 ));
1022         double L2 = getDistance(P( 2 ),P( 3 ));
1023         double L3 = getDistance(P( 3 ),P( 1 ));
1024         double L4 = getDistance(P( 1 ),P( 4 ));
1025         double L5 = getDistance(P( 2 ),P( 4 ));
1026         double L6 = getDistance(P( 3 ),P( 4 ));
1027         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1028         break;
1029       }
1030       else if (len == 5){ // piramids
1031         double L1 = getDistance(P( 1 ),P( 2 ));
1032         double L2 = getDistance(P( 2 ),P( 3 ));
1033         double L3 = getDistance(P( 3 ),P( 1 ));
1034         double L4 = getDistance(P( 4 ),P( 1 ));
1035         double L5 = getDistance(P( 1 ),P( 5 ));
1036         double L6 = getDistance(P( 2 ),P( 5 ));
1037         double L7 = getDistance(P( 3 ),P( 5 ));
1038         double L8 = getDistance(P( 4 ),P( 5 ));
1039
1040         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1041         aVal = Max(aVal,Max(L7,L8));
1042         break;
1043       }
1044       else if (len == 6){ // pentaidres
1045         double L1 = getDistance(P( 1 ),P( 2 ));
1046         double L2 = getDistance(P( 2 ),P( 3 ));
1047         double L3 = getDistance(P( 3 ),P( 1 ));
1048         double L4 = getDistance(P( 4 ),P( 5 ));
1049         double L5 = getDistance(P( 5 ),P( 6 ));
1050         double L6 = getDistance(P( 6 ),P( 4 ));
1051         double L7 = getDistance(P( 1 ),P( 4 ));
1052         double L8 = getDistance(P( 2 ),P( 5 ));
1053         double L9 = getDistance(P( 3 ),P( 6 ));
1054
1055         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1056         aVal = Max(aVal,Max(Max(L7,L8),L9));
1057         break;
1058       }
1059       else if (len == 8){ // hexaider
1060         double L1 = getDistance(P( 1 ),P( 2 ));
1061         double L2 = getDistance(P( 2 ),P( 3 ));
1062         double L3 = getDistance(P( 3 ),P( 4 ));
1063         double L4 = getDistance(P( 4 ),P( 1 ));
1064         double L5 = getDistance(P( 5 ),P( 6 ));
1065         double L6 = getDistance(P( 6 ),P( 7 ));
1066         double L7 = getDistance(P( 7 ),P( 8 ));
1067         double L8 = getDistance(P( 8 ),P( 5 ));
1068         double L9 = getDistance(P( 1 ),P( 5 ));
1069         double L10= getDistance(P( 2 ),P( 6 ));
1070         double L11= getDistance(P( 3 ),P( 7 ));
1071         double L12= getDistance(P( 4 ),P( 8 ));
1072
1073         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1074         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1075         aVal = Max(aVal,Max(L11,L12));
1076         break;
1077
1078       }
1079
1080       if (len == 10){ // quadratic tetraidrs
1081         double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
1082         double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
1083         double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
1084         double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1085         double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
1086         double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
1087         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1088         break;
1089       }
1090       else if (len == 13){ // quadratic piramids
1091         double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
1092         double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
1093         double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1094         double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1095         double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1096         double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
1097         double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
1098         double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
1099         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1100         aVal = Max(aVal,Max(L7,L8));
1101         break;
1102       }
1103       else if (len == 15){ // quadratic pentaidres
1104         double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
1105         double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
1106         double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1107         double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1108         double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
1109         double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
1110         double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
1111         double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
1112         double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
1113         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1114         aVal = Max(aVal,Max(Max(L7,L8),L9));
1115         break;
1116       }
1117       else if (len == 20){ // quadratic hexaider
1118         double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
1119         double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
1120         double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
1121         double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
1122         double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
1123         double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
1124         double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
1125         double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
1126         double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
1127         double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
1128         double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
1129         double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
1130         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1131         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1132         aVal = Max(aVal,Max(L11,L12));
1133         break;
1134
1135       }
1136
1137     default: aVal=-1;
1138     }
1139
1140     if (aVal <0){
1141       return 0.;
1142     }
1143
1144     if ( myPrecision >= 0 )
1145     {
1146       double prec = pow( 10., (double)( myPrecision ) );
1147       aVal = floor( aVal * prec + 0.5 ) / prec;
1148     }
1149
1150     return aVal;
1151
1152   }
1153   return 0.;
1154 }
1155
1156 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1157 {
1158   // meaningless as it is not quality control functor
1159   return Value;
1160 }
1161
1162 SMDSAbs_ElementType Length2D::GetType() const
1163 {
1164   return SMDSAbs_Face;
1165 }
1166
1167 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
1168   myLength(theLength)
1169 {
1170   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1171   if(thePntId1 > thePntId2){
1172     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1173   }
1174 }
1175
1176 bool Length2D::Value::operator<(const Length2D::Value& x) const{
1177   if(myPntId[0] < x.myPntId[0]) return true;
1178   if(myPntId[0] == x.myPntId[0])
1179     if(myPntId[1] < x.myPntId[1]) return true;
1180   return false;
1181 }
1182
1183 void Length2D::GetValues(TValues& theValues){
1184   TValues aValues;
1185   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1186   for(; anIter->more(); ){
1187     const SMDS_MeshFace* anElem = anIter->next();
1188
1189     if(anElem->IsQuadratic()) {
1190       const SMDS_QuadraticFaceOfNodes* F =
1191         static_cast<const SMDS_QuadraticFaceOfNodes*>(anElem);
1192       // use special nodes iterator
1193       SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
1194       long aNodeId[4];
1195       gp_Pnt P[4];
1196
1197       double aLength;
1198       const SMDS_MeshElement* aNode;
1199       if(anIter->more()){
1200         aNode = anIter->next();
1201         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1202         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1203         aNodeId[0] = aNodeId[1] = aNode->GetID();
1204         aLength = 0;
1205       }
1206       for(; anIter->more(); ){
1207         const SMDS_MeshNode* N1 = static_cast<const SMDS_MeshNode*> (anIter->next());
1208         P[2] = gp_Pnt(N1->X(),N1->Y(),N1->Z());
1209         aNodeId[2] = N1->GetID();
1210         aLength = P[1].Distance(P[2]);
1211         if(!anIter->more()) break;
1212         const SMDS_MeshNode* N2 = static_cast<const SMDS_MeshNode*> (anIter->next());
1213         P[3] = gp_Pnt(N2->X(),N2->Y(),N2->Z());
1214         aNodeId[3] = N2->GetID();
1215         aLength += P[2].Distance(P[3]);
1216         Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1217         Value aValue2(aLength,aNodeId[2],aNodeId[3]);
1218         P[1] = P[3];
1219         aNodeId[1] = aNodeId[3];
1220         theValues.insert(aValue1);
1221         theValues.insert(aValue2);
1222       }
1223       aLength += P[2].Distance(P[0]);
1224       Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1225       Value aValue2(aLength,aNodeId[2],aNodeId[0]);
1226       theValues.insert(aValue1);
1227       theValues.insert(aValue2);
1228     }
1229     else {
1230       SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1231       long aNodeId[2];
1232       gp_Pnt P[3];
1233
1234       double aLength;
1235       const SMDS_MeshElement* aNode;
1236       if(aNodesIter->more()){
1237         aNode = aNodesIter->next();
1238         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1239         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1240         aNodeId[0] = aNodeId[1] = aNode->GetID();
1241         aLength = 0;
1242       }
1243       for(; aNodesIter->more(); ){
1244         aNode = aNodesIter->next();
1245         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1246         long anId = aNode->GetID();
1247         
1248         P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1249         
1250         aLength = P[1].Distance(P[2]);
1251         
1252         Value aValue(aLength,aNodeId[1],anId);
1253         aNodeId[1] = anId;
1254         P[1] = P[2];
1255         theValues.insert(aValue);
1256       }
1257
1258       aLength = P[0].Distance(P[1]);
1259
1260       Value aValue(aLength,aNodeId[0],aNodeId[1]);
1261       theValues.insert(aValue);
1262     }
1263   }
1264 }
1265
1266 /*
1267   Class       : MultiConnection
1268   Description : Functor for calculating number of faces conneted to the edge
1269 */
1270 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1271 {
1272   return 0;
1273 }
1274 double MultiConnection::GetValue( long theId )
1275 {
1276   return getNbMultiConnection( myMesh, theId );
1277 }
1278
1279 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1280 {
1281   // meaningless as it is not quality control functor
1282   return Value;
1283 }
1284
1285 SMDSAbs_ElementType MultiConnection::GetType() const
1286 {
1287   return SMDSAbs_Edge;
1288 }
1289
1290 /*
1291   Class       : MultiConnection2D
1292   Description : Functor for calculating number of faces conneted to the edge
1293 */
1294 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1295 {
1296   return 0;
1297 }
1298
1299 double MultiConnection2D::GetValue( long theElementId )
1300 {
1301   int aResult = 0;
1302
1303   const SMDS_MeshElement* aFaceElem = myMesh->FindElement(theElementId);
1304   SMDSAbs_ElementType aType = aFaceElem->GetType();
1305
1306   switch (aType) {
1307   case SMDSAbs_Face:
1308     {
1309       int i = 0, len = aFaceElem->NbNodes();
1310       SMDS_ElemIteratorPtr anIter = aFaceElem->nodesIterator();
1311       if (!anIter) break;
1312
1313       const SMDS_MeshNode *aNode, *aNode0;
1314       TColStd_MapOfInteger aMap, aMapPrev;
1315
1316       for (i = 0; i <= len; i++) {
1317         aMapPrev = aMap;
1318         aMap.Clear();
1319
1320         int aNb = 0;
1321         if (anIter->more()) {
1322           aNode = (SMDS_MeshNode*)anIter->next();
1323         } else {
1324           if (i == len)
1325             aNode = aNode0;
1326           else
1327             break;
1328         }
1329         if (!aNode) break;
1330         if (i == 0) aNode0 = aNode;
1331
1332         SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1333         while (anElemIter->more()) {
1334           const SMDS_MeshElement* anElem = anElemIter->next();
1335           if (anElem != 0 && anElem->GetType() == SMDSAbs_Face) {
1336             int anId = anElem->GetID();
1337
1338             aMap.Add(anId);
1339             if (aMapPrev.Contains(anId)) {
1340               aNb++;
1341             }
1342           }
1343         }
1344         aResult = Max(aResult, aNb);
1345       }
1346     }
1347     break;
1348   default:
1349     aResult = 0;
1350   }
1351
1352   return aResult;
1353 }
1354
1355 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1356 {
1357   // meaningless as it is not quality control functor
1358   return Value;
1359 }
1360
1361 SMDSAbs_ElementType MultiConnection2D::GetType() const
1362 {
1363   return SMDSAbs_Face;
1364 }
1365
1366 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1367 {
1368   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1369   if(thePntId1 > thePntId2){
1370     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1371   }
1372 }
1373
1374 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1375   if(myPntId[0] < x.myPntId[0]) return true;
1376   if(myPntId[0] == x.myPntId[0])
1377     if(myPntId[1] < x.myPntId[1]) return true;
1378   return false;
1379 }
1380
1381 void MultiConnection2D::GetValues(MValues& theValues){
1382   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1383   for(; anIter->more(); ){
1384     const SMDS_MeshFace* anElem = anIter->next();
1385     SMDS_ElemIteratorPtr aNodesIter;
1386     if ( anElem->IsQuadratic() )
1387       aNodesIter = static_cast<const SMDS_QuadraticFaceOfNodes*>
1388         (anElem)->interlacedNodesElemIterator();
1389     else
1390       aNodesIter = anElem->nodesIterator();
1391     long aNodeId[3];
1392
1393     //int aNbConnects=0;
1394     const SMDS_MeshNode* aNode0;
1395     const SMDS_MeshNode* aNode1;
1396     const SMDS_MeshNode* aNode2;
1397     if(aNodesIter->more()){
1398       aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1399       aNode1 = aNode0;
1400       const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1401       aNodeId[0] = aNodeId[1] = aNodes->GetID();
1402     }
1403     for(; aNodesIter->more(); ) {
1404       aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1405       long anId = aNode2->GetID();
1406       aNodeId[2] = anId;
1407
1408       Value aValue(aNodeId[1],aNodeId[2]);
1409       MValues::iterator aItr = theValues.find(aValue);
1410       if (aItr != theValues.end()){
1411         aItr->second += 1;
1412         //aNbConnects = nb;
1413       }
1414       else {
1415         theValues[aValue] = 1;
1416         //aNbConnects = 1;
1417       }
1418       //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1419       aNodeId[1] = aNodeId[2];
1420       aNode1 = aNode2;
1421     }
1422     Value aValue(aNodeId[0],aNodeId[2]);
1423     MValues::iterator aItr = theValues.find(aValue);
1424     if (aItr != theValues.end()) {
1425       aItr->second += 1;
1426       //aNbConnects = nb;
1427     }
1428     else {
1429       theValues[aValue] = 1;
1430       //aNbConnects = 1;
1431     }
1432     //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1433   }
1434
1435 }
1436
1437 /*
1438                             PREDICATES
1439 */
1440
1441 /*
1442   Class       : BadOrientedVolume
1443   Description : Predicate bad oriented volumes
1444 */
1445
1446 BadOrientedVolume::BadOrientedVolume()
1447 {
1448   myMesh = 0;
1449 }
1450
1451 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1452 {
1453   myMesh = theMesh;
1454 }
1455
1456 bool BadOrientedVolume::IsSatisfy( long theId )
1457 {
1458   if ( myMesh == 0 )
1459     return false;
1460
1461   SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1462   return !vTool.IsForward();
1463 }
1464
1465 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1466 {
1467   return SMDSAbs_Volume;
1468 }
1469
1470
1471
1472 /*
1473   Class       : FreeBorders
1474   Description : Predicate for free borders
1475 */
1476
1477 FreeBorders::FreeBorders()
1478 {
1479   myMesh = 0;
1480 }
1481
1482 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1483 {
1484   myMesh = theMesh;
1485 }
1486
1487 bool FreeBorders::IsSatisfy( long theId )
1488 {
1489   return getNbMultiConnection( myMesh, theId ) == 1;
1490 }
1491
1492 SMDSAbs_ElementType FreeBorders::GetType() const
1493 {
1494   return SMDSAbs_Edge;
1495 }
1496
1497
1498 /*
1499   Class       : FreeEdges
1500   Description : Predicate for free Edges
1501 */
1502 FreeEdges::FreeEdges()
1503 {
1504   myMesh = 0;
1505 }
1506
1507 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1508 {
1509   myMesh = theMesh;
1510 }
1511
1512 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  )
1513 {
1514   TColStd_MapOfInteger aMap;
1515   for ( int i = 0; i < 2; i++ )
1516   {
1517     SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1518     while( anElemIter->more() )
1519     {
1520       const SMDS_MeshElement* anElem = anElemIter->next();
1521       if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1522       {
1523         int anId = anElem->GetID();
1524
1525         if ( i == 0 )
1526           aMap.Add( anId );
1527         else if ( aMap.Contains( anId ) && anId != theFaceId )
1528           return false;
1529       }
1530     }
1531   }
1532   return true;
1533 }
1534
1535 bool FreeEdges::IsSatisfy( long theId )
1536 {
1537   if ( myMesh == 0 )
1538     return false;
1539
1540   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1541   if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1542     return false;
1543
1544   SMDS_ElemIteratorPtr anIter;
1545   if ( aFace->IsQuadratic() ) {
1546     anIter = static_cast<const SMDS_QuadraticFaceOfNodes*>
1547       (aFace)->interlacedNodesElemIterator();
1548   }
1549   else {
1550     anIter = aFace->nodesIterator();
1551   }
1552   if ( anIter == 0 )
1553     return false;
1554
1555   int i = 0, nbNodes = aFace->NbNodes();
1556   vector <const SMDS_MeshNode*> aNodes( nbNodes+1 );
1557   while( anIter->more() )
1558   {
1559     const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1560     if ( aNode == 0 )
1561       return false;
1562     aNodes[ i++ ] = aNode;
1563   }
1564   aNodes[ nbNodes ] = aNodes[ 0 ];
1565
1566   for ( i = 0; i < nbNodes; i++ )
1567     if ( IsFreeEdge( &aNodes[ i ], theId ) )
1568       return true;
1569
1570   return false;
1571 }
1572
1573 SMDSAbs_ElementType FreeEdges::GetType() const
1574 {
1575   return SMDSAbs_Face;
1576 }
1577
1578 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1579   myElemId(theElemId)
1580 {
1581   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1582   if(thePntId1 > thePntId2){
1583     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1584   }
1585 }
1586
1587 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1588   if(myPntId[0] < x.myPntId[0]) return true;
1589   if(myPntId[0] == x.myPntId[0])
1590     if(myPntId[1] < x.myPntId[1]) return true;
1591   return false;
1592 }
1593
1594 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1595                           FreeEdges::TBorders& theRegistry,
1596                           FreeEdges::TBorders& theContainer)
1597 {
1598   if(theRegistry.find(theBorder) == theRegistry.end()){
1599     theRegistry.insert(theBorder);
1600     theContainer.insert(theBorder);
1601   }else{
1602     theContainer.erase(theBorder);
1603   }
1604 }
1605
1606 void FreeEdges::GetBoreders(TBorders& theBorders)
1607 {
1608   TBorders aRegistry;
1609   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1610   for(; anIter->more(); ){
1611     const SMDS_MeshFace* anElem = anIter->next();
1612     long anElemId = anElem->GetID();
1613     SMDS_ElemIteratorPtr aNodesIter;
1614     if ( anElem->IsQuadratic() )
1615       aNodesIter = static_cast<const SMDS_QuadraticFaceOfNodes*>(anElem)->
1616         interlacedNodesElemIterator();
1617     else
1618       aNodesIter = anElem->nodesIterator();
1619     long aNodeId[2];
1620     const SMDS_MeshElement* aNode;
1621     if(aNodesIter->more()){
1622       aNode = aNodesIter->next();
1623       aNodeId[0] = aNodeId[1] = aNode->GetID();
1624     }
1625     for(; aNodesIter->more(); ){
1626       aNode = aNodesIter->next();
1627       long anId = aNode->GetID();
1628       Border aBorder(anElemId,aNodeId[1],anId);
1629       aNodeId[1] = anId;
1630       //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1631       UpdateBorders(aBorder,aRegistry,theBorders);
1632     }
1633     Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1634     //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1635     UpdateBorders(aBorder,aRegistry,theBorders);
1636   }
1637   //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1638 }
1639
1640 /*
1641   Class       : RangeOfIds
1642   Description : Predicate for Range of Ids.
1643                 Range may be specified with two ways.
1644                 1. Using AddToRange method
1645                 2. With SetRangeStr method. Parameter of this method is a string
1646                    like as "1,2,3,50-60,63,67,70-"
1647 */
1648
1649 //=======================================================================
1650 // name    : RangeOfIds
1651 // Purpose : Constructor
1652 //=======================================================================
1653 RangeOfIds::RangeOfIds()
1654 {
1655   myMesh = 0;
1656   myType = SMDSAbs_All;
1657 }
1658
1659 //=======================================================================
1660 // name    : SetMesh
1661 // Purpose : Set mesh
1662 //=======================================================================
1663 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1664 {
1665   myMesh = theMesh;
1666 }
1667
1668 //=======================================================================
1669 // name    : AddToRange
1670 // Purpose : Add ID to the range
1671 //=======================================================================
1672 bool RangeOfIds::AddToRange( long theEntityId )
1673 {
1674   myIds.Add( theEntityId );
1675   return true;
1676 }
1677
1678 //=======================================================================
1679 // name    : GetRangeStr
1680 // Purpose : Get range as a string.
1681 //           Example: "1,2,3,50-60,63,67,70-"
1682 //=======================================================================
1683 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1684 {
1685   theResStr.Clear();
1686
1687   TColStd_SequenceOfInteger     anIntSeq;
1688   TColStd_SequenceOfAsciiString aStrSeq;
1689
1690   TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1691   for ( ; anIter.More(); anIter.Next() )
1692   {
1693     int anId = anIter.Key();
1694     TCollection_AsciiString aStr( anId );
1695     anIntSeq.Append( anId );
1696     aStrSeq.Append( aStr );
1697   }
1698
1699   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1700   {
1701     int aMinId = myMin( i );
1702     int aMaxId = myMax( i );
1703
1704     TCollection_AsciiString aStr;
1705     if ( aMinId != IntegerFirst() )
1706       aStr += aMinId;
1707
1708     aStr += "-";
1709
1710     if ( aMaxId != IntegerLast() )
1711       aStr += aMaxId;
1712
1713     // find position of the string in result sequence and insert string in it
1714     if ( anIntSeq.Length() == 0 )
1715     {
1716       anIntSeq.Append( aMinId );
1717       aStrSeq.Append( aStr );
1718     }
1719     else
1720     {
1721       if ( aMinId < anIntSeq.First() )
1722       {
1723         anIntSeq.Prepend( aMinId );
1724         aStrSeq.Prepend( aStr );
1725       }
1726       else if ( aMinId > anIntSeq.Last() )
1727       {
1728         anIntSeq.Append( aMinId );
1729         aStrSeq.Append( aStr );
1730       }
1731       else
1732         for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1733           if ( aMinId < anIntSeq( j ) )
1734           {
1735             anIntSeq.InsertBefore( j, aMinId );
1736             aStrSeq.InsertBefore( j, aStr );
1737             break;
1738           }
1739     }
1740   }
1741
1742   if ( aStrSeq.Length() == 0 )
1743     return;
1744
1745   theResStr = aStrSeq( 1 );
1746   for ( int j = 2, k = aStrSeq.Length(); j <= k; j++  )
1747   {
1748     theResStr += ",";
1749     theResStr += aStrSeq( j );
1750   }
1751 }
1752
1753 //=======================================================================
1754 // name    : SetRangeStr
1755 // Purpose : Define range with string
1756 //           Example of entry string: "1,2,3,50-60,63,67,70-"
1757 //=======================================================================
1758 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1759 {
1760   myMin.Clear();
1761   myMax.Clear();
1762   myIds.Clear();
1763
1764   TCollection_AsciiString aStr = theStr;
1765   aStr.RemoveAll( ' ' );
1766   aStr.RemoveAll( '\t' );
1767
1768   for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1769     aStr.Remove( aPos, 2 );
1770
1771   TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1772   int i = 1;
1773   while ( tmpStr != "" )
1774   {
1775     tmpStr = aStr.Token( ",", i++ );
1776     int aPos = tmpStr.Search( '-' );
1777
1778     if ( aPos == -1 )
1779     {
1780       if ( tmpStr.IsIntegerValue() )
1781         myIds.Add( tmpStr.IntegerValue() );
1782       else
1783         return false;
1784     }
1785     else
1786     {
1787       TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1788       TCollection_AsciiString aMinStr = tmpStr;
1789
1790       while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1791       while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1792
1793       if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1794            !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1795         return false;
1796
1797       myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1798       myMax.Append( aMaxStr.IsEmpty() ? IntegerLast()  : aMaxStr.IntegerValue() );
1799     }
1800   }
1801
1802   return true;
1803 }
1804
1805 //=======================================================================
1806 // name    : GetType
1807 // Purpose : Get type of supported entities
1808 //=======================================================================
1809 SMDSAbs_ElementType RangeOfIds::GetType() const
1810 {
1811   return myType;
1812 }
1813
1814 //=======================================================================
1815 // name    : SetType
1816 // Purpose : Set type of supported entities
1817 //=======================================================================
1818 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1819 {
1820   myType = theType;
1821 }
1822
1823 //=======================================================================
1824 // name    : IsSatisfy
1825 // Purpose : Verify whether entity satisfies to this rpedicate
1826 //=======================================================================
1827 bool RangeOfIds::IsSatisfy( long theId )
1828 {
1829   if ( !myMesh )
1830     return false;
1831
1832   if ( myType == SMDSAbs_Node )
1833   {
1834     if ( myMesh->FindNode( theId ) == 0 )
1835       return false;
1836   }
1837   else
1838   {
1839     const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1840     if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1841       return false;
1842   }
1843
1844   if ( myIds.Contains( theId ) )
1845     return true;
1846
1847   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1848     if ( theId >= myMin( i ) && theId <= myMax( i ) )
1849       return true;
1850
1851   return false;
1852 }
1853
1854 /*
1855   Class       : Comparator
1856   Description : Base class for comparators
1857 */
1858 Comparator::Comparator():
1859   myMargin(0)
1860 {}
1861
1862 Comparator::~Comparator()
1863 {}
1864
1865 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1866 {
1867   if ( myFunctor )
1868     myFunctor->SetMesh( theMesh );
1869 }
1870
1871 void Comparator::SetMargin( double theValue )
1872 {
1873   myMargin = theValue;
1874 }
1875
1876 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1877 {
1878   myFunctor = theFunct;
1879 }
1880
1881 SMDSAbs_ElementType Comparator::GetType() const
1882 {
1883   return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1884 }
1885
1886 double Comparator::GetMargin()
1887 {
1888   return myMargin;
1889 }
1890
1891
1892 /*
1893   Class       : LessThan
1894   Description : Comparator "<"
1895 */
1896 bool LessThan::IsSatisfy( long theId )
1897 {
1898   return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1899 }
1900
1901
1902 /*
1903   Class       : MoreThan
1904   Description : Comparator ">"
1905 */
1906 bool MoreThan::IsSatisfy( long theId )
1907 {
1908   return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1909 }
1910
1911
1912 /*
1913   Class       : EqualTo
1914   Description : Comparator "="
1915 */
1916 EqualTo::EqualTo():
1917   myToler(Precision::Confusion())
1918 {}
1919
1920 bool EqualTo::IsSatisfy( long theId )
1921 {
1922   return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1923 }
1924
1925 void EqualTo::SetTolerance( double theToler )
1926 {
1927   myToler = theToler;
1928 }
1929
1930 double EqualTo::GetTolerance()
1931 {
1932   return myToler;
1933 }
1934
1935 /*
1936   Class       : LogicalNOT
1937   Description : Logical NOT predicate
1938 */
1939 LogicalNOT::LogicalNOT()
1940 {}
1941
1942 LogicalNOT::~LogicalNOT()
1943 {}
1944
1945 bool LogicalNOT::IsSatisfy( long theId )
1946 {
1947   return myPredicate && !myPredicate->IsSatisfy( theId );
1948 }
1949
1950 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1951 {
1952   if ( myPredicate )
1953     myPredicate->SetMesh( theMesh );
1954 }
1955
1956 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1957 {
1958   myPredicate = thePred;
1959 }
1960
1961 SMDSAbs_ElementType LogicalNOT::GetType() const
1962 {
1963   return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1964 }
1965
1966
1967 /*
1968   Class       : LogicalBinary
1969   Description : Base class for binary logical predicate
1970 */
1971 LogicalBinary::LogicalBinary()
1972 {}
1973
1974 LogicalBinary::~LogicalBinary()
1975 {}
1976
1977 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1978 {
1979   if ( myPredicate1 )
1980     myPredicate1->SetMesh( theMesh );
1981
1982   if ( myPredicate2 )
1983     myPredicate2->SetMesh( theMesh );
1984 }
1985
1986 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1987 {
1988   myPredicate1 = thePredicate;
1989 }
1990
1991 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1992 {
1993   myPredicate2 = thePredicate;
1994 }
1995
1996 SMDSAbs_ElementType LogicalBinary::GetType() const
1997 {
1998   if ( !myPredicate1 || !myPredicate2 )
1999     return SMDSAbs_All;
2000
2001   SMDSAbs_ElementType aType1 = myPredicate1->GetType();
2002   SMDSAbs_ElementType aType2 = myPredicate2->GetType();
2003
2004   return aType1 == aType2 ? aType1 : SMDSAbs_All;
2005 }
2006
2007
2008 /*
2009   Class       : LogicalAND
2010   Description : Logical AND
2011 */
2012 bool LogicalAND::IsSatisfy( long theId )
2013 {
2014   return
2015     myPredicate1 &&
2016     myPredicate2 &&
2017     myPredicate1->IsSatisfy( theId ) &&
2018     myPredicate2->IsSatisfy( theId );
2019 }
2020
2021
2022 /*
2023   Class       : LogicalOR
2024   Description : Logical OR
2025 */
2026 bool LogicalOR::IsSatisfy( long theId )
2027 {
2028   return
2029     myPredicate1 &&
2030     myPredicate2 &&
2031     myPredicate1->IsSatisfy( theId ) ||
2032     myPredicate2->IsSatisfy( theId );
2033 }
2034
2035
2036 /*
2037                               FILTER
2038 */
2039
2040 Filter::Filter()
2041 {}
2042
2043 Filter::~Filter()
2044 {}
2045
2046 void Filter::SetPredicate( PredicatePtr thePredicate )
2047 {
2048   myPredicate = thePredicate;
2049 }
2050
2051 template<class TElement, class TIterator, class TPredicate>
2052 inline void FillSequence(const TIterator& theIterator,
2053                          TPredicate& thePredicate,
2054                          Filter::TIdSequence& theSequence)
2055 {
2056   if ( theIterator ) {
2057     while( theIterator->more() ) {
2058       TElement anElem = theIterator->next();
2059       long anId = anElem->GetID();
2060       if ( thePredicate->IsSatisfy( anId ) )
2061         theSequence.push_back( anId );
2062     }
2063   }
2064 }
2065
2066 void
2067 Filter::
2068 GetElementsId( const SMDS_Mesh* theMesh,
2069                PredicatePtr thePredicate,
2070                TIdSequence& theSequence )
2071 {
2072   theSequence.clear();
2073
2074   if ( !theMesh || !thePredicate )
2075     return;
2076
2077   thePredicate->SetMesh( theMesh );
2078
2079   SMDSAbs_ElementType aType = thePredicate->GetType();
2080   switch(aType){
2081   case SMDSAbs_Node:
2082     FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
2083     break;
2084   case SMDSAbs_Edge:
2085     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
2086     break;
2087   case SMDSAbs_Face:
2088     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
2089     break;
2090   case SMDSAbs_Volume:
2091     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
2092     break;
2093   case SMDSAbs_All:
2094     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
2095     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
2096     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
2097     break;
2098   }
2099 }
2100
2101 void
2102 Filter::GetElementsId( const SMDS_Mesh* theMesh,
2103                        Filter::TIdSequence& theSequence )
2104 {
2105   GetElementsId(theMesh,myPredicate,theSequence);
2106 }
2107
2108 /*
2109                               ManifoldPart
2110 */
2111
2112 typedef std::set<SMDS_MeshFace*>                    TMapOfFacePtr;
2113
2114 /*
2115    Internal class Link
2116 */
2117
2118 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
2119                           SMDS_MeshNode* theNode2 )
2120 {
2121   myNode1 = theNode1;
2122   myNode2 = theNode2;
2123 }
2124
2125 ManifoldPart::Link::~Link()
2126 {
2127   myNode1 = 0;
2128   myNode2 = 0;
2129 }
2130
2131 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
2132 {
2133   if ( myNode1 == theLink.myNode1 &&
2134        myNode2 == theLink.myNode2 )
2135     return true;
2136   else if ( myNode1 == theLink.myNode2 &&
2137             myNode2 == theLink.myNode1 )
2138     return true;
2139   else
2140     return false;
2141 }
2142
2143 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
2144 {
2145   if(myNode1 < x.myNode1) return true;
2146   if(myNode1 == x.myNode1)
2147     if(myNode2 < x.myNode2) return true;
2148   return false;
2149 }
2150
2151 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
2152                             const ManifoldPart::Link& theLink2 )
2153 {
2154   return theLink1.IsEqual( theLink2 );
2155 }
2156
2157 ManifoldPart::ManifoldPart()
2158 {
2159   myMesh = 0;
2160   myAngToler = Precision::Angular();
2161   myIsOnlyManifold = true;
2162 }
2163
2164 ManifoldPart::~ManifoldPart()
2165 {
2166   myMesh = 0;
2167 }
2168
2169 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
2170 {
2171   myMesh = theMesh;
2172   process();
2173 }
2174
2175 SMDSAbs_ElementType ManifoldPart::GetType() const
2176 { return SMDSAbs_Face; }
2177
2178 bool ManifoldPart::IsSatisfy( long theElementId )
2179 {
2180   return myMapIds.Contains( theElementId );
2181 }
2182
2183 void ManifoldPart::SetAngleTolerance( const double theAngToler )
2184 { myAngToler = theAngToler; }
2185
2186 double ManifoldPart::GetAngleTolerance() const
2187 { return myAngToler; }
2188
2189 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
2190 { myIsOnlyManifold = theIsOnly; }
2191
2192 void ManifoldPart::SetStartElem( const long  theStartId )
2193 { myStartElemId = theStartId; }
2194
2195 bool ManifoldPart::process()
2196 {
2197   myMapIds.Clear();
2198   myMapBadGeomIds.Clear();
2199
2200   myAllFacePtr.clear();
2201   myAllFacePtrIntDMap.clear();
2202   if ( !myMesh )
2203     return false;
2204
2205   // collect all faces into own map
2206   SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
2207   for (; anFaceItr->more(); )
2208   {
2209     SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
2210     myAllFacePtr.push_back( aFacePtr );
2211     myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
2212   }
2213
2214   SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
2215   if ( !aStartFace )
2216     return false;
2217
2218   // the map of non manifold links and bad geometry
2219   TMapOfLink aMapOfNonManifold;
2220   TColStd_MapOfInteger aMapOfTreated;
2221
2222   // begin cycle on faces from start index and run on vector till the end
2223   //  and from begin to start index to cover whole vector
2224   const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
2225   bool isStartTreat = false;
2226   for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
2227   {
2228     if ( fi == aStartIndx )
2229       isStartTreat = true;
2230     // as result next time when fi will be equal to aStartIndx
2231
2232     SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
2233     if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
2234       continue;
2235
2236     aMapOfTreated.Add( aFacePtr->GetID() );
2237     TColStd_MapOfInteger aResFaces;
2238     if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
2239                          aMapOfNonManifold, aResFaces ) )
2240       continue;
2241     TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
2242     for ( ; anItr.More(); anItr.Next() )
2243     {
2244       int aFaceId = anItr.Key();
2245       aMapOfTreated.Add( aFaceId );
2246       myMapIds.Add( aFaceId );
2247     }
2248
2249     if ( fi == ( myAllFacePtr.size() - 1 ) )
2250       fi = 0;
2251   } // end run on vector of faces
2252   return !myMapIds.IsEmpty();
2253 }
2254
2255 static void getLinks( const SMDS_MeshFace* theFace,
2256                       ManifoldPart::TVectorOfLink& theLinks )
2257 {
2258   int aNbNode = theFace->NbNodes();
2259   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2260   int i = 1;
2261   SMDS_MeshNode* aNode = 0;
2262   for ( ; aNodeItr->more() && i <= aNbNode; )
2263   {
2264
2265     SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
2266     if ( i == 1 )
2267       aNode = aN1;
2268     i++;
2269     SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
2270     i++;
2271     ManifoldPart::Link aLink( aN1, aN2 );
2272     theLinks.push_back( aLink );
2273   }
2274 }
2275
2276 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
2277 {
2278   gp_XYZ n;
2279   int aNbNode = theFace->NbNodes();
2280   TColgp_Array1OfXYZ anArrOfXYZ(1,4);
2281   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2282   int i = 1;
2283   for ( ; aNodeItr->more() && i <= 4; i++ ) {
2284     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2285     anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2286   }
2287
2288   gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2289   gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2290   n  = q1 ^ q2;
2291   if ( aNbNode > 3 ) {
2292     gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2293     n += q2 ^ q3;
2294   }
2295   double len = n.Modulus();
2296   if ( len > 0 )
2297     n /= len;
2298
2299   return n;
2300 }
2301
2302 bool ManifoldPart::findConnected
2303                  ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2304                   SMDS_MeshFace*                           theStartFace,
2305                   ManifoldPart::TMapOfLink&                theNonManifold,
2306                   TColStd_MapOfInteger&                    theResFaces )
2307 {
2308   theResFaces.Clear();
2309   if ( !theAllFacePtrInt.size() )
2310     return false;
2311
2312   if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2313   {
2314     myMapBadGeomIds.Add( theStartFace->GetID() );
2315     return false;
2316   }
2317
2318   ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2319   ManifoldPart::TVectorOfLink aSeqOfBoundary;
2320   theResFaces.Add( theStartFace->GetID() );
2321   ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2322
2323   expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2324                  aDMapLinkFace, theNonManifold, theStartFace );
2325
2326   bool isDone = false;
2327   while ( !isDone && aMapOfBoundary.size() != 0 )
2328   {
2329     bool isToReset = false;
2330     ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2331     for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2332     {
2333       ManifoldPart::Link aLink = *pLink;
2334       if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2335         continue;
2336       // each link could be treated only once
2337       aMapToSkip.insert( aLink );
2338
2339       ManifoldPart::TVectorOfFacePtr aFaces;
2340       // find next
2341       if ( myIsOnlyManifold &&
2342            (theNonManifold.find( aLink ) != theNonManifold.end()) )
2343         continue;
2344       else
2345       {
2346         getFacesByLink( aLink, aFaces );
2347         // filter the element to keep only indicated elements
2348         ManifoldPart::TVectorOfFacePtr aFiltered;
2349         ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2350         for ( ; pFace != aFaces.end(); ++pFace )
2351         {
2352           SMDS_MeshFace* aFace = *pFace;
2353           if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2354             aFiltered.push_back( aFace );
2355         }
2356         aFaces = aFiltered;
2357         if ( aFaces.size() < 2 )  // no neihgbour faces
2358           continue;
2359         else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2360         {
2361           theNonManifold.insert( aLink );
2362           continue;
2363         }
2364       }
2365
2366       // compare normal with normals of neighbor element
2367       SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2368       ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2369       for ( ; pFace != aFaces.end(); ++pFace )
2370       {
2371         SMDS_MeshFace* aNextFace = *pFace;
2372         if ( aPrevFace == aNextFace )
2373           continue;
2374         int anNextFaceID = aNextFace->GetID();
2375         if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2376          // should not be with non manifold restriction. probably bad topology
2377           continue;
2378         // check if face was treated and skipped
2379         if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2380              !isInPlane( aPrevFace, aNextFace ) )
2381           continue;
2382         // add new element to connected and extend the boundaries.
2383         theResFaces.Add( anNextFaceID );
2384         expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2385                         aDMapLinkFace, theNonManifold, aNextFace );
2386         isToReset = true;
2387       }
2388     }
2389     isDone = !isToReset;
2390   }
2391
2392   return !theResFaces.IsEmpty();
2393 }
2394
2395 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2396                               const SMDS_MeshFace* theFace2 )
2397 {
2398   gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2399   gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2400   if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2401   {
2402     myMapBadGeomIds.Add( theFace2->GetID() );
2403     return false;
2404   }
2405   if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2406     return true;
2407
2408   return false;
2409 }
2410
2411 void ManifoldPart::expandBoundary
2412                    ( ManifoldPart::TMapOfLink&            theMapOfBoundary,
2413                      ManifoldPart::TVectorOfLink&         theSeqOfBoundary,
2414                      ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2415                      ManifoldPart::TMapOfLink&            theNonManifold,
2416                      SMDS_MeshFace*                       theNextFace ) const
2417 {
2418   ManifoldPart::TVectorOfLink aLinks;
2419   getLinks( theNextFace, aLinks );
2420   int aNbLink = (int)aLinks.size();
2421   for ( int i = 0; i < aNbLink; i++ )
2422   {
2423     ManifoldPart::Link aLink = aLinks[ i ];
2424     if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2425       continue;
2426     if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2427     {
2428       if ( myIsOnlyManifold )
2429       {
2430         // remove from boundary
2431         theMapOfBoundary.erase( aLink );
2432         ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2433         for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2434         {
2435           ManifoldPart::Link aBoundLink = *pLink;
2436           if ( aBoundLink.IsEqual( aLink ) )
2437           {
2438             theSeqOfBoundary.erase( pLink );
2439             break;
2440           }
2441         }
2442       }
2443     }
2444     else
2445     {
2446       theMapOfBoundary.insert( aLink );
2447       theSeqOfBoundary.push_back( aLink );
2448       theDMapLinkFacePtr[ aLink ] = theNextFace;
2449     }
2450   }
2451 }
2452
2453 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2454                                    ManifoldPart::TVectorOfFacePtr& theFaces ) const
2455 {
2456   SMDS_Mesh::SetOfFaces aSetOfFaces;
2457   // take all faces that shared first node
2458   SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2459   for ( ; anItr->more(); )
2460   {
2461     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2462     if ( !aFace )
2463       continue;
2464     aSetOfFaces.Add( aFace );
2465   }
2466   // take all faces that shared second node
2467   anItr = theLink.myNode2->facesIterator();
2468   // find the common part of two sets
2469   for ( ; anItr->more(); )
2470   {
2471     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2472     if ( aSetOfFaces.Contains( aFace ) )
2473       theFaces.push_back( aFace );
2474   }
2475 }
2476
2477
2478 /*
2479    ElementsOnSurface
2480 */
2481
2482 ElementsOnSurface::ElementsOnSurface()
2483 {
2484   myMesh = 0;
2485   myIds.Clear();
2486   myType = SMDSAbs_All;
2487   mySurf.Nullify();
2488   myToler = Precision::Confusion();
2489 }
2490
2491 ElementsOnSurface::~ElementsOnSurface()
2492 {
2493   myMesh = 0;
2494 }
2495
2496 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2497 {
2498   if ( myMesh == theMesh )
2499     return;
2500   myMesh = theMesh;
2501   myIds.Clear();
2502   process();
2503 }
2504
2505 bool ElementsOnSurface::IsSatisfy( long theElementId )
2506 {
2507   return myIds.Contains( theElementId );
2508 }
2509
2510 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2511 { return myType; }
2512
2513 void ElementsOnSurface::SetTolerance( const double theToler )
2514 { myToler = theToler; }
2515
2516 double ElementsOnSurface::GetTolerance() const
2517 {
2518   return myToler;
2519 }
2520
2521 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2522                                     const SMDSAbs_ElementType theType )
2523 {
2524   myType = theType;
2525   mySurf.Nullify();
2526   if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2527   {
2528     mySurf.Nullify();
2529     return;
2530   }
2531   TopoDS_Face aFace = TopoDS::Face( theShape );
2532   mySurf = BRep_Tool::Surface( aFace );
2533 }
2534
2535 void ElementsOnSurface::process()
2536 {
2537   myIds.Clear();
2538   if ( mySurf.IsNull() )
2539     return;
2540
2541   if ( myMesh == 0 )
2542     return;
2543
2544   if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2545   {
2546     SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2547     for(; anIter->more(); )
2548       process( anIter->next() );
2549   }
2550
2551   if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2552   {
2553     SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2554     for(; anIter->more(); )
2555       process( anIter->next() );
2556   }
2557
2558   if ( myType == SMDSAbs_Node )
2559   {
2560     SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2561     for(; anIter->more(); )
2562       process( anIter->next() );
2563   }
2564 }
2565
2566 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2567 {
2568   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2569   bool isSatisfy = true;
2570   for ( ; aNodeItr->more(); )
2571   {
2572     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2573     if ( !isOnSurface( aNode ) )
2574     {
2575       isSatisfy = false;
2576       break;
2577     }
2578   }
2579   if ( isSatisfy )
2580     myIds.Add( theElemPtr->GetID() );
2581 }
2582
2583 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2584 {
2585   if ( mySurf.IsNull() )
2586     return false;
2587
2588   gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2589   double aToler2 = myToler * myToler;
2590   if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2591   {
2592     gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2593     if ( aPln.SquareDistance( aPnt ) > aToler2 )
2594       return false;
2595   }
2596   else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2597   {
2598     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2599     double aRad = aCyl.Radius();
2600     gp_Ax3 anAxis = aCyl.Position();
2601     gp_XYZ aLoc = aCyl.Location().XYZ();
2602     double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2603     double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2604     if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )
2605       return false;
2606   }
2607   else
2608     return false;
2609
2610   return true;
2611 }