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