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