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