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