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