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