]> SALOME platform Git repositories - modules/smesh.git/blob - src/Controls/SMESH_Controls.cxx
Salome HOME
Fix on - Bug PAL7220: DEVELOPMENT : 3D Aspect Ratio
[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
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   inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
77   {
78     double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
79     return aDist;
80   }
81
82   int getNbMultiConnection( SMDS_Mesh* theMesh, const int theId )
83   {
84     if ( theMesh == 0 )
85       return 0;
86     
87     const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
88     if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge || anEdge->NbNodes() != 2 )
89       return 0;
90     
91     TColStd_MapOfInteger aMap;
92     
93     int aResult = 0;
94     SMDS_ElemIteratorPtr anIter = anEdge->nodesIterator();
95     if ( anIter != 0 ) {
96       while( anIter->more() ) {
97         const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
98         if ( aNode == 0 )
99           return 0;
100         SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
101         while( anElemIter->more() ) {
102           const SMDS_MeshElement* anElem = anElemIter->next();
103           if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
104             int anId = anElem->GetID();
105             
106             if ( anIter->more() )              // i.e. first node
107               aMap.Add( anId );
108             else if ( aMap.Contains( anId ) )
109               aResult++;
110           }
111         }
112       }
113     }
114     
115     return aResult;
116   }
117
118 }  
119   
120 using namespace SMESH::Controls;
121
122 /*
123                                 FUNCTORS
124 */
125
126 /*
127   Class       : NumericalFunctor
128   Description : Base class for numerical functors
129 */
130 NumericalFunctor::NumericalFunctor():
131   myMesh(NULL)
132 {
133   myPrecision = -1;
134 }
135
136 void NumericalFunctor::SetMesh( SMDS_Mesh* theMesh )
137 {
138   myMesh = theMesh;
139 }
140
141 bool NumericalFunctor::GetPoints(const int theId,
142                                  TSequenceOfXYZ& theRes) const
143 {
144   theRes.clear();
145
146   if ( myMesh == 0 )
147     return false;
148
149   return GetPoints( myMesh->FindElement( theId ), theRes );
150 }
151
152 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
153                                  TSequenceOfXYZ& theRes)
154 {
155   theRes.clear();
156
157   if ( anElem == 0)
158     return false;
159
160   // Get nodes of the element
161   SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
162   if ( anIter != 0 )
163   {
164     while( anIter->more() )
165     {
166       const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
167       if ( aNode != 0 ){
168         theRes.push_back( gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
169       }
170     }
171   }
172
173   return true;
174 }
175
176 long  NumericalFunctor::GetPrecision() const
177 {
178   return myPrecision;
179 }
180
181 void  NumericalFunctor::SetPrecision( const long thePrecision )
182 {
183   myPrecision = thePrecision;
184 }
185
186 double NumericalFunctor::GetValue( long theId )
187 {
188   TSequenceOfXYZ P;
189   if ( GetPoints( theId, P ))
190   {
191     double aVal = GetValue( P );
192     if ( myPrecision >= 0 )
193     {
194       double prec = pow( 10., (double)( myPrecision ) );
195       aVal = floor( aVal * prec + 0.5 ) / prec;
196     }
197     return aVal;
198   }
199
200   return 0.;
201 }
202
203 /*
204   Class       : MinimumAngle
205   Description : Functor for calculation of minimum angle
206 */
207
208 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
209 {
210   double aMin;
211
212   if ( P.size() == 3 )
213   {
214     double A0 = getAngle( P( 3 ), P( 1 ), P( 2 ) );
215     double A1 = getAngle( P( 1 ), P( 2 ), P( 3 ) );
216     double A2 = getAngle( P( 2 ), P( 3 ), P( 1 ) );
217
218     aMin = Min( A0, Min( A1, A2 ) );
219   }
220   else if ( P.size() == 4 )
221   {
222     double A0 = getAngle( P( 4 ), P( 1 ), P( 2 ) );
223     double A1 = getAngle( P( 1 ), P( 2 ), P( 3 ) );
224     double A2 = getAngle( P( 2 ), P( 3 ), P( 4 ) );
225     double A3 = getAngle( P( 3 ), P( 4 ), P( 1 ) );
226     
227     aMin = Min( Min( A0, A1 ), Min( A2, A3 ) );
228   }
229   else
230     return 0.;
231   
232   return aMin * 180 / PI;
233 }
234
235 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
236 {
237   const double aBestAngle = PI / nbNodes;
238   return ( fabs( aBestAngle - Value ));
239 }
240
241 SMDSAbs_ElementType MinimumAngle::GetType() const
242 {
243   return SMDSAbs_Face;
244 }
245
246
247 /*
248   Class       : AspectRatio
249   Description : Functor for calculating aspect ratio
250 */
251 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
252 {
253   int nbNodes = P.size();
254
255   if ( nbNodes != 3 && nbNodes != 4 )
256     return 0;
257
258   // Compute lengths of the sides
259
260   double aLen[ nbNodes ];
261   for ( int i = 0; i < nbNodes - 1; i++ )
262     aLen[ i ] = getDistance( P(i+1), P(i+2) );
263   aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P(nbNodes) );
264
265   // Compute aspect ratio
266
267   if ( nbNodes == 3 ) 
268   {
269     double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
270     if ( anArea <= Precision::Confusion() )
271       return 0.;
272     double aMaxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
273     static double aCoef = sqrt( 3. ) / 4;
274
275     return aCoef * aMaxLen * aMaxLen / anArea;
276   }
277   else
278   {
279     double aMinLen = Min( Min( aLen[ 0 ], aLen[ 1 ] ), Min( aLen[ 2 ], aLen[ 3 ] ) );
280     if ( aMinLen <= Precision::Confusion() )
281       return 0.;
282     double aMaxLen = Max( Max( aLen[ 0 ], aLen[ 1 ] ), Max( aLen[ 2 ], aLen[ 3 ] ) );
283     
284     return aMaxLen / aMinLen;
285   }
286 }
287
288 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
289 {
290   // the aspect ratio is in the range [1.0,infinity]
291   // 1.0 = good
292   // infinity = bad
293   return Value / 1000.;
294 }
295
296 SMDSAbs_ElementType AspectRatio::GetType() const
297 {
298   return SMDSAbs_Face;
299 }
300
301
302 /*
303   Class       : AspectRatio3D
304   Description : Functor for calculating aspect ratio
305 */
306
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 abs(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   if ( P.size() == 3 )
772     return getArea( P( 1 ), P( 2 ), P( 3 ) );
773   else if ( P.size() == 4 )
774     return getArea( P( 1 ), P( 2 ), P( 3 ) ) + getArea( P( 1 ), P( 3 ), P( 4 ) );
775   else
776     return 0;
777 }
778
779 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
780 {
781   return Value;
782 }
783
784 SMDSAbs_ElementType Area::GetType() const
785 {
786   return SMDSAbs_Face;
787 }
788
789
790 /*
791   Class       : Length
792   Description : Functor for calculating length off edge
793 */
794 double Length::GetValue( const TSequenceOfXYZ& P )
795 {
796   return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
797 }
798
799 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
800 {
801   return Value;
802 }
803
804 SMDSAbs_ElementType Length::GetType() const
805 {
806   return SMDSAbs_Edge;
807 }
808
809
810 /*
811   Class       : MultiConnection
812   Description : Functor for calculating number of faces conneted to the edge
813 */
814 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
815 {
816   return 0;
817 }
818 double MultiConnection::GetValue( long theId )
819 {
820   return getNbMultiConnection( myMesh, theId );
821 }
822
823 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
824 {
825   return Value;
826 }
827
828 SMDSAbs_ElementType MultiConnection::GetType() const
829 {
830   return SMDSAbs_Edge;
831 }
832
833
834 /*
835                             PREDICATES
836 */
837
838 /*
839   Class       : FreeBorders
840   Description : Predicate for free borders
841 */
842
843 FreeBorders::FreeBorders()
844 {
845   myMesh = 0;
846 }
847
848 void FreeBorders::SetMesh( SMDS_Mesh* theMesh )
849 {
850   myMesh = theMesh;
851 }
852
853 bool FreeBorders::IsSatisfy( long theId )
854 {
855   return getNbMultiConnection( myMesh, theId ) == 1;
856 }
857
858 SMDSAbs_ElementType FreeBorders::GetType() const
859 {
860   return SMDSAbs_Edge;
861 }
862
863
864 /*
865   Class       : FreeEdges
866   Description : Predicate for free Edges
867 */
868 FreeEdges::FreeEdges()
869 {
870   myMesh = 0;
871 }
872
873 void FreeEdges::SetMesh( SMDS_Mesh* theMesh )
874 {
875   myMesh = theMesh;
876 }
877
878 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  )
879 {
880   TColStd_MapOfInteger aMap;
881   for ( int i = 0; i < 2; i++ )
882   {
883     SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
884     while( anElemIter->more() )
885     {
886       const SMDS_MeshElement* anElem = anElemIter->next();
887       if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
888       {
889         int anId = anElem->GetID();
890
891         if ( i == 0 ) 
892           aMap.Add( anId );
893         else if ( aMap.Contains( anId ) && anId != theFaceId )
894           return false;
895       }
896     }
897   }
898   return true;
899 }
900
901 bool FreeEdges::IsSatisfy( long theId )
902 {
903   if ( myMesh == 0 )
904     return false;
905
906   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
907   if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
908     return false;
909
910   int nbNodes = aFace->NbNodes();
911   const SMDS_MeshNode* aNodes[ nbNodes ];
912   int i = 0;
913   SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
914   if ( anIter != 0 )
915   {
916     while( anIter->more() )
917     {
918       const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
919       if ( aNode == 0 )
920         return false;
921       aNodes[ i++ ] = aNode;
922     }
923   }
924
925   for ( int i = 0; i < nbNodes - 1; i++ )
926     if ( IsFreeEdge( &aNodes[ i ], theId ) )
927       return true;
928
929   aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
930   
931   return IsFreeEdge( &aNodes[ 0 ], theId );
932
933 }
934
935 SMDSAbs_ElementType FreeEdges::GetType() const
936 {
937   return SMDSAbs_Face;
938 }
939
940 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
941   myElemId(theElemId)
942 {
943   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
944   if(thePntId1 > thePntId2){
945     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
946   }
947 }
948
949 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
950   if(myPntId[0] < x.myPntId[0]) return true;
951   if(myPntId[0] == x.myPntId[0])
952     if(myPntId[1] < x.myPntId[1]) return true;
953   return false;
954 }
955
956 inline void UpdateBorders(const FreeEdges::Border& theBorder,
957                           FreeEdges::TBorders& theRegistry, 
958                           FreeEdges::TBorders& theContainer)
959 {
960   if(theRegistry.find(theBorder) == theRegistry.end()){
961     theRegistry.insert(theBorder);
962     theContainer.insert(theBorder);
963   }else{
964     theContainer.erase(theBorder);
965   }
966 }
967
968 void FreeEdges::GetBoreders(TBorders& theBorders)
969 {
970   TBorders aRegistry;
971   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
972   for(; anIter->more(); ){
973     const SMDS_MeshFace* anElem = anIter->next();
974     long anElemId = anElem->GetID();
975     SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
976     long aNodeId[2];
977     const SMDS_MeshElement* aNode;
978     if(aNodesIter->more()){
979       aNode = aNodesIter->next();
980       aNodeId[0] = aNodeId[1] = aNode->GetID();
981     }   
982     for(; aNodesIter->more(); ){
983       aNode = aNodesIter->next();
984       long anId = aNode->GetID();
985       Border aBorder(anElemId,aNodeId[1],anId);
986       aNodeId[1] = anId;
987       //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
988       UpdateBorders(aBorder,aRegistry,theBorders);
989     }
990     Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
991     //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
992     UpdateBorders(aBorder,aRegistry,theBorders);
993   }
994   //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
995 }
996
997 /*
998   Class       : RangeOfIds
999   Description : Predicate for Range of Ids.
1000                 Range may be specified with two ways.
1001                 1. Using AddToRange method
1002                 2. With SetRangeStr method. Parameter of this method is a string
1003                    like as "1,2,3,50-60,63,67,70-"
1004 */
1005
1006 //=======================================================================
1007 // name    : RangeOfIds
1008 // Purpose : Constructor
1009 //=======================================================================
1010 RangeOfIds::RangeOfIds()
1011 {
1012   myMesh = 0;
1013   myType = SMDSAbs_All;
1014 }
1015
1016 //=======================================================================
1017 // name    : SetMesh
1018 // Purpose : Set mesh 
1019 //=======================================================================
1020 void RangeOfIds::SetMesh( SMDS_Mesh* theMesh )
1021 {
1022   myMesh = theMesh;
1023 }
1024
1025 //=======================================================================
1026 // name    : AddToRange
1027 // Purpose : Add ID to the range
1028 //=======================================================================
1029 bool RangeOfIds::AddToRange( long theEntityId )
1030 {
1031   myIds.Add( theEntityId );
1032   return true;
1033 }
1034
1035 //=======================================================================
1036 // name    : GetRangeStr
1037 // Purpose : Get range as a string.
1038 //           Example: "1,2,3,50-60,63,67,70-"
1039 //=======================================================================
1040 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1041 {
1042   theResStr.Clear();
1043
1044   TColStd_SequenceOfInteger     anIntSeq;
1045   TColStd_SequenceOfAsciiString aStrSeq;
1046
1047   TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1048   for ( ; anIter.More(); anIter.Next() )
1049   {
1050     int anId = anIter.Key();
1051     TCollection_AsciiString aStr( anId );
1052     anIntSeq.Append( anId );
1053     aStrSeq.Append( aStr );
1054   }
1055
1056   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1057   {
1058     int aMinId = myMin( i );
1059     int aMaxId = myMax( i );
1060
1061     TCollection_AsciiString aStr;
1062     if ( aMinId != IntegerFirst() )
1063       aStr += aMinId;
1064       
1065     aStr += "-";
1066       
1067     if ( aMaxId != IntegerLast() )
1068       aStr += aMaxId;
1069
1070     // find position of the string in result sequence and insert string in it
1071     if ( anIntSeq.Length() == 0 )
1072     {
1073       anIntSeq.Append( aMinId );
1074       aStrSeq.Append( aStr );
1075     }
1076     else
1077     {
1078       if ( aMinId < anIntSeq.First() )
1079       {
1080         anIntSeq.Prepend( aMinId );
1081         aStrSeq.Prepend( aStr );
1082       }
1083       else if ( aMinId > anIntSeq.Last() )
1084       {
1085         anIntSeq.Append( aMinId );
1086         aStrSeq.Append( aStr );
1087       }
1088       else
1089         for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1090           if ( aMinId < anIntSeq( j ) )
1091           {
1092             anIntSeq.InsertBefore( j, aMinId );
1093             aStrSeq.InsertBefore( j, aStr );
1094             break;
1095           }
1096     }
1097   }
1098
1099   if ( aStrSeq.Length() == 0 )
1100     return;
1101
1102   theResStr = aStrSeq( 1 );
1103   for ( int j = 2, k = aStrSeq.Length(); j <= k; j++  )
1104   {
1105     theResStr += ",";
1106     theResStr += aStrSeq( j );
1107   }
1108 }
1109
1110 //=======================================================================
1111 // name    : SetRangeStr
1112 // Purpose : Define range with string
1113 //           Example of entry string: "1,2,3,50-60,63,67,70-"
1114 //=======================================================================
1115 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1116 {
1117   myMin.Clear();
1118   myMax.Clear();
1119   myIds.Clear();
1120
1121   TCollection_AsciiString aStr = theStr;
1122   aStr.RemoveAll( ' ' );
1123   aStr.RemoveAll( '\t' );
1124
1125   for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1126     aStr.Remove( aPos, 2 );
1127
1128   TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1129   int i = 1;
1130   while ( tmpStr != "" )
1131   {
1132     tmpStr = aStr.Token( ",", i++ );
1133     int aPos = tmpStr.Search( '-' );
1134     
1135     if ( aPos == -1 )
1136     {
1137       if ( tmpStr.IsIntegerValue() )
1138         myIds.Add( tmpStr.IntegerValue() );
1139       else
1140         return false;
1141     }
1142     else
1143     {
1144       TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1145       TCollection_AsciiString aMinStr = tmpStr;
1146       
1147       while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1148       while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1149
1150       if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1151            !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1152         return false;
1153            
1154       myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1155       myMax.Append( aMaxStr.IsEmpty() ? IntegerLast()  : aMaxStr.IntegerValue() );
1156     }
1157   }
1158
1159   return true;
1160 }
1161
1162 //=======================================================================
1163 // name    : GetType
1164 // Purpose : Get type of supported entities
1165 //=======================================================================
1166 SMDSAbs_ElementType RangeOfIds::GetType() const
1167 {
1168   return myType;
1169 }
1170
1171 //=======================================================================
1172 // name    : SetType
1173 // Purpose : Set type of supported entities
1174 //=======================================================================
1175 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1176 {
1177   myType = theType;
1178 }
1179
1180 //=======================================================================
1181 // name    : IsSatisfy
1182 // Purpose : Verify whether entity satisfies to this rpedicate
1183 //=======================================================================
1184 bool RangeOfIds::IsSatisfy( long theId )
1185 {
1186   if ( !myMesh )
1187     return false;
1188
1189   if ( myType == SMDSAbs_Node )
1190   {
1191     if ( myMesh->FindNode( theId ) == 0 )
1192       return false;
1193   }
1194   else
1195   {
1196     const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1197     if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1198       return false;
1199   }
1200     
1201   if ( myIds.Contains( theId ) )
1202     return true;
1203
1204   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1205     if ( theId >= myMin( i ) && theId <= myMax( i ) )
1206       return true;
1207
1208   return false;
1209 }
1210
1211 /*
1212   Class       : Comparator
1213   Description : Base class for comparators
1214 */
1215 Comparator::Comparator():
1216   myMargin(0)
1217 {}
1218
1219 Comparator::~Comparator()
1220 {}
1221
1222 void Comparator::SetMesh( SMDS_Mesh* theMesh )
1223 {
1224   if ( myFunctor )
1225     myFunctor->SetMesh( theMesh );
1226 }
1227
1228 void Comparator::SetMargin( double theValue )
1229 {
1230   myMargin = theValue;
1231 }
1232
1233 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1234 {
1235   myFunctor = theFunct;
1236 }
1237
1238 SMDSAbs_ElementType Comparator::GetType() const
1239 {
1240   return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1241 }
1242
1243 double Comparator::GetMargin()
1244 {
1245   return myMargin;
1246 }
1247
1248
1249 /*
1250   Class       : LessThan
1251   Description : Comparator "<"
1252 */
1253 bool LessThan::IsSatisfy( long theId )
1254 {
1255   return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1256 }
1257
1258
1259 /*
1260   Class       : MoreThan
1261   Description : Comparator ">"
1262 */
1263 bool MoreThan::IsSatisfy( long theId )
1264 {
1265   return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1266 }
1267
1268
1269 /*
1270   Class       : EqualTo
1271   Description : Comparator "="
1272 */
1273 EqualTo::EqualTo():
1274   myToler(Precision::Confusion())
1275 {}
1276
1277 bool EqualTo::IsSatisfy( long theId )
1278 {
1279   return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1280 }
1281
1282 void EqualTo::SetTolerance( double theToler )
1283 {
1284   myToler = theToler;
1285 }
1286
1287 double EqualTo::GetTolerance()
1288 {
1289   return myToler;
1290 }
1291
1292 /*
1293   Class       : LogicalNOT
1294   Description : Logical NOT predicate
1295 */
1296 LogicalNOT::LogicalNOT()
1297 {}
1298
1299 LogicalNOT::~LogicalNOT()
1300 {}
1301
1302 bool LogicalNOT::IsSatisfy( long theId )
1303 {
1304   return myPredicate && !myPredicate->IsSatisfy( theId );
1305 }
1306
1307 void LogicalNOT::SetMesh( SMDS_Mesh* theMesh )
1308 {
1309   if ( myPredicate )
1310     myPredicate->SetMesh( theMesh );
1311 }
1312
1313 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1314 {
1315   myPredicate = thePred;
1316 }
1317
1318 SMDSAbs_ElementType LogicalNOT::GetType() const
1319 {
1320   return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1321 }
1322
1323
1324 /*
1325   Class       : LogicalBinary
1326   Description : Base class for binary logical predicate
1327 */
1328 LogicalBinary::LogicalBinary()
1329 {}
1330
1331 LogicalBinary::~LogicalBinary()
1332 {}
1333
1334 void LogicalBinary::SetMesh( SMDS_Mesh* theMesh )
1335 {
1336   if ( myPredicate1 )
1337     myPredicate1->SetMesh( theMesh );
1338
1339   if ( myPredicate2 )
1340     myPredicate2->SetMesh( theMesh );
1341 }
1342
1343 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1344 {
1345   myPredicate1 = thePredicate;
1346 }
1347
1348 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1349 {
1350   myPredicate2 = thePredicate;
1351 }
1352
1353 SMDSAbs_ElementType LogicalBinary::GetType() const
1354 {
1355   if ( !myPredicate1 || !myPredicate2 )
1356     return SMDSAbs_All;
1357
1358   SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1359   SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1360
1361   return aType1 == aType2 ? aType1 : SMDSAbs_All;
1362 }
1363
1364
1365 /*
1366   Class       : LogicalAND
1367   Description : Logical AND
1368 */
1369 bool LogicalAND::IsSatisfy( long theId )
1370 {
1371   return 
1372     myPredicate1 && 
1373     myPredicate2 && 
1374     myPredicate1->IsSatisfy( theId ) && 
1375     myPredicate2->IsSatisfy( theId );
1376 }
1377
1378
1379 /*
1380   Class       : LogicalOR
1381   Description : Logical OR
1382 */
1383 bool LogicalOR::IsSatisfy( long theId )
1384 {
1385   return 
1386     myPredicate1 && 
1387     myPredicate2 && 
1388     myPredicate1->IsSatisfy( theId ) || 
1389     myPredicate2->IsSatisfy( theId );
1390 }
1391
1392
1393 /*
1394                               FILTER
1395 */
1396
1397 Filter::Filter()
1398 {}
1399
1400 Filter::~Filter()
1401 {}
1402
1403 void Filter::SetPredicate( PredicatePtr thePredicate )
1404 {
1405   myPredicate = thePredicate;
1406 }
1407
1408
1409 template<class TElement, class TIterator, class TPredicate> 
1410 void FillSequence(const TIterator& theIterator,
1411                   TPredicate& thePredicate,
1412                   Filter::TIdSequence& theSequence)
1413 {
1414   if ( theIterator ) {
1415     while( theIterator->more() ) {
1416       TElement anElem = theIterator->next();
1417       long anId = anElem->GetID();
1418       if ( thePredicate->IsSatisfy( anId ) )
1419         theSequence.push_back( anId );
1420     }
1421   }
1422 }
1423
1424 Filter::TIdSequence
1425 Filter::GetElementsId( SMDS_Mesh* theMesh )
1426 {
1427   TIdSequence aSequence;
1428   if ( !theMesh || !myPredicate ) return aSequence;
1429
1430   myPredicate->SetMesh( theMesh );
1431
1432   SMDSAbs_ElementType aType = myPredicate->GetType();
1433   switch(aType){
1434   case SMDSAbs_Node:{
1435     FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),myPredicate,aSequence);
1436     break;
1437   }
1438   case SMDSAbs_Edge:{
1439     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),myPredicate,aSequence);
1440     break;
1441   }
1442   case SMDSAbs_Face:{
1443     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),myPredicate,aSequence);
1444     break;
1445   }
1446   case SMDSAbs_Volume:{
1447     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),myPredicate,aSequence);
1448     break;
1449   }
1450   case SMDSAbs_All:{
1451     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),myPredicate,aSequence);
1452     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),myPredicate,aSequence);
1453     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),myPredicate,aSequence);
1454     break;
1455   }
1456   }
1457   return aSequence;
1458 }
1459
1460 /*
1461                               ManifoldPart
1462 */
1463
1464 typedef std::set<SMDS_MeshFace*>                    TMapOfFacePtr;
1465
1466 /*  
1467    Internal class Link
1468 */ 
1469
1470 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1471                           SMDS_MeshNode* theNode2 )
1472 {
1473   myNode1 = theNode1;
1474   myNode2 = theNode2;
1475 }
1476
1477 ManifoldPart::Link::~Link()
1478 {
1479   myNode1 = 0;
1480   myNode2 = 0;
1481 }
1482
1483 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1484 {
1485   if ( myNode1 == theLink.myNode1 &&
1486        myNode2 == theLink.myNode2 )
1487     return true;
1488   else if ( myNode1 == theLink.myNode2 &&
1489             myNode2 == theLink.myNode1 )
1490     return true;
1491   else
1492     return false;
1493 }
1494
1495 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1496 {
1497   if(myNode1 < x.myNode1) return true;
1498   if(myNode1 == x.myNode1)
1499     if(myNode2 < x.myNode2) return true;
1500   return false;
1501 }
1502
1503 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1504                             const ManifoldPart::Link& theLink2 )
1505
1506   return theLink1.IsEqual( theLink2 );
1507 }
1508
1509 ManifoldPart::ManifoldPart()
1510 {
1511   myMesh = 0;
1512   myAngToler = Precision::Angular();
1513   myIsOnlyManifold = true;
1514 }
1515
1516 ManifoldPart::~ManifoldPart()
1517 {
1518   myMesh = 0;
1519 }
1520
1521 void ManifoldPart::SetMesh( SMDS_Mesh* theMesh )
1522 {
1523   myMesh = theMesh;
1524   process();
1525 }
1526
1527 SMDSAbs_ElementType ManifoldPart::GetType() const
1528 { return SMDSAbs_Face; }
1529
1530 bool ManifoldPart::IsSatisfy( long theElementId )
1531 {
1532   return myMapIds.Contains( theElementId );
1533 }
1534
1535 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1536 { myAngToler = theAngToler; }
1537
1538 double ManifoldPart::GetAngleTolerance() const
1539 { return myAngToler; }
1540
1541 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1542 { myIsOnlyManifold = theIsOnly; }
1543
1544 void ManifoldPart::SetStartElem( const long  theStartId )
1545 { myStartElemId = theStartId; }
1546
1547 bool ManifoldPart::process()
1548 {
1549   myMapIds.Clear();
1550   myMapBadGeomIds.Clear();
1551   
1552   myAllFacePtr.clear();
1553   myAllFacePtrIntDMap.clear();
1554   if ( !myMesh )
1555     return false;
1556
1557   // collect all faces into own map
1558   SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1559   for (; anFaceItr->more(); )
1560   {
1561     SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1562     myAllFacePtr.push_back( aFacePtr );
1563     myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1564   }
1565
1566   SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1567   if ( !aStartFace )
1568     return false;
1569
1570   // the map of non manifold links and bad geometry
1571   TMapOfLink aMapOfNonManifold;
1572   TColStd_MapOfInteger aMapOfTreated;
1573
1574   // begin cycle on faces from start index and run on vector till the end
1575   //  and from begin to start index to cover whole vector
1576   const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1577   bool isStartTreat = false;
1578   for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1579   {
1580     if ( fi == aStartIndx )
1581       isStartTreat = true;
1582     // as result next time when fi will be equal to aStartIndx
1583     
1584     SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1585     if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1586       continue;
1587
1588     aMapOfTreated.Add( aFacePtr->GetID() );
1589     TColStd_MapOfInteger aResFaces;
1590     if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1591                          aMapOfNonManifold, aResFaces ) )
1592       continue;
1593     TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1594     for ( ; anItr.More(); anItr.Next() )
1595     {
1596       int aFaceId = anItr.Key();
1597       aMapOfTreated.Add( aFaceId );
1598       myMapIds.Add( aFaceId );
1599     }
1600
1601     if ( fi == ( myAllFacePtr.size() - 1 ) )
1602       fi = 0;
1603   } // end run on vector of faces
1604   return !myMapIds.IsEmpty();
1605 }
1606
1607 static void getLinks( const SMDS_MeshFace* theFace,
1608                       ManifoldPart::TVectorOfLink& theLinks )
1609 {
1610   int aNbNode = theFace->NbNodes();
1611   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1612   int i = 1;
1613   SMDS_MeshNode* aNode = 0;
1614   for ( ; aNodeItr->more() && i <= aNbNode; )
1615   {
1616     
1617     SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
1618     if ( i == 1 )
1619       aNode = aN1;
1620     i++;
1621     SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
1622     i++;
1623     ManifoldPart::Link aLink( aN1, aN2 );
1624     theLinks.push_back( aLink );
1625   }
1626 }
1627
1628 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
1629 {
1630   gp_XYZ n;
1631   int aNbNode = theFace->NbNodes();
1632   TColgp_Array1OfXYZ anArrOfXYZ(1,4);
1633   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1634   int i = 1;
1635   for ( ; aNodeItr->more() && i <= 4; i++ )
1636   {
1637     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
1638     anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
1639   }
1640   
1641   gp_XYZ q1 = anArrOfXYZ.Value( 2 ) - anArrOfXYZ.Value( 1 );
1642   gp_XYZ q2 = anArrOfXYZ.Value( 3 ) - anArrOfXYZ.Value( 1 );
1643   n  = q1 ^ q2;
1644   if ( aNbNode > 3 )
1645   {
1646     gp_XYZ q3 = anArrOfXYZ.Value( 4 ) - anArrOfXYZ.Value( 1 );
1647     n += q2 ^ q3;
1648   }
1649   double len = n.Modulus();
1650   if ( len > 0 )
1651     n /= len;
1652
1653   return n;
1654 }
1655
1656 bool ManifoldPart::findConnected
1657                  ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
1658                   SMDS_MeshFace*                           theStartFace,
1659                   ManifoldPart::TMapOfLink&                theNonManifold,
1660                   TColStd_MapOfInteger&                    theResFaces )
1661 {
1662   theResFaces.Clear();
1663   if ( !theAllFacePtrInt.size() )
1664     return false;
1665   
1666   if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
1667   {
1668     myMapBadGeomIds.Add( theStartFace->GetID() );
1669     return false;
1670   }
1671
1672   ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
1673   ManifoldPart::TVectorOfLink aSeqOfBoundary;
1674   theResFaces.Add( theStartFace->GetID() );
1675   ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
1676
1677   expandBoundary( aMapOfBoundary, aSeqOfBoundary, 
1678                  aDMapLinkFace, theNonManifold, theStartFace );
1679
1680   bool isDone = false;
1681   while ( !isDone && aMapOfBoundary.size() != 0 )
1682   {
1683     bool isToReset = false;
1684     ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
1685     for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
1686     {
1687       ManifoldPart::Link aLink = *pLink;
1688       if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
1689         continue;
1690       // each link could be treated only once
1691       aMapToSkip.insert( aLink );
1692
1693       ManifoldPart::TVectorOfFacePtr aFaces;
1694       // find next
1695       if ( myIsOnlyManifold && 
1696            (theNonManifold.find( aLink ) != theNonManifold.end()) )
1697         continue;
1698       else
1699       {
1700         getFacesByLink( aLink, aFaces );
1701         // filter the element to keep only indicated elements
1702         ManifoldPart::TVectorOfFacePtr aFiltered;
1703         ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
1704         for ( ; pFace != aFaces.end(); ++pFace )
1705         {
1706           SMDS_MeshFace* aFace = *pFace;
1707           if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
1708             aFiltered.push_back( aFace );
1709         }
1710         aFaces = aFiltered;
1711         if ( aFaces.size() < 2 )  // no neihgbour faces
1712           continue;
1713         else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
1714         {
1715           theNonManifold.insert( aLink );
1716           continue;
1717         }
1718       }
1719       
1720       // compare normal with normals of neighbor element
1721       SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
1722       ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
1723       for ( ; pFace != aFaces.end(); ++pFace )
1724       {
1725         SMDS_MeshFace* aNextFace = *pFace;
1726         if ( aPrevFace == aNextFace )
1727           continue;
1728         int anNextFaceID = aNextFace->GetID();
1729         if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
1730          // should not be with non manifold restriction. probably bad topology
1731           continue;
1732         // check if face was treated and skipped
1733         if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
1734              !isInPlane( aPrevFace, aNextFace ) )
1735           continue;
1736         // add new element to connected and extend the boundaries.
1737         theResFaces.Add( anNextFaceID );
1738         expandBoundary( aMapOfBoundary, aSeqOfBoundary, 
1739                         aDMapLinkFace, theNonManifold, aNextFace );
1740         isToReset = true;
1741       }
1742     }
1743     isDone = !isToReset;
1744   }
1745
1746   return !theResFaces.IsEmpty();
1747 }
1748
1749 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
1750                               const SMDS_MeshFace* theFace2 )
1751 {
1752   gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
1753   gp_XYZ aNorm2XYZ = getNormale( theFace2 );
1754   if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
1755   {
1756     myMapBadGeomIds.Add( theFace2->GetID() );
1757     return false;
1758   }
1759   if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
1760     return true;
1761
1762   return false;
1763 }
1764
1765 void ManifoldPart::expandBoundary
1766                    ( ManifoldPart::TMapOfLink&            theMapOfBoundary,
1767                      ManifoldPart::TVectorOfLink&         theSeqOfBoundary,
1768                      ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
1769                      ManifoldPart::TMapOfLink&            theNonManifold,
1770                      SMDS_MeshFace*                       theNextFace ) const
1771 {
1772   ManifoldPart::TVectorOfLink aLinks;
1773   getLinks( theNextFace, aLinks );
1774   int aNbLink = aLinks.size();
1775   for ( int i = 0; i < aNbLink; i++ )
1776   {
1777     ManifoldPart::Link aLink = aLinks[ i ];
1778     if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
1779       continue;
1780     if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
1781     {
1782       if ( myIsOnlyManifold )
1783       {
1784         // remove from boundary
1785         theMapOfBoundary.erase( aLink );
1786         ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
1787         for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
1788         {
1789           ManifoldPart::Link aBoundLink = *pLink;
1790           if ( aBoundLink.IsEqual( aLink ) )
1791           {
1792             theSeqOfBoundary.erase( pLink );
1793             break;
1794           }
1795         }
1796       }
1797     }
1798     else
1799     {
1800       theMapOfBoundary.insert( aLink );
1801       theSeqOfBoundary.push_back( aLink );
1802       theDMapLinkFacePtr[ aLink ] = theNextFace;
1803     }
1804   }
1805 }
1806
1807 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
1808                                    ManifoldPart::TVectorOfFacePtr& theFaces ) const
1809 {
1810   SMDS_Mesh::SetOfFaces aSetOfFaces;
1811   // take all faces that shared first node
1812   SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
1813   for ( ; anItr->more(); )
1814   {
1815     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
1816     if ( !aFace )
1817       continue;
1818     aSetOfFaces.insert( aFace );
1819   }
1820   // take all faces that shared second node
1821   anItr = theLink.myNode2->facesIterator();
1822   // find the common part of two sets
1823   for ( ; anItr->more(); )
1824   {
1825     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
1826     if ( aSetOfFaces.find( aFace ) != aSetOfFaces.end() )
1827       theFaces.push_back( aFace );
1828   }
1829 }
1830
1831
1832 /*
1833    ElementsOnSurface
1834 */
1835
1836 ElementsOnSurface::ElementsOnSurface()
1837 {
1838   myMesh = 0;
1839   myIds.Clear();
1840   myType = SMDSAbs_All;
1841   mySurf.Nullify();
1842   myToler = Precision::Confusion();
1843 }
1844
1845 ElementsOnSurface::~ElementsOnSurface()
1846 {
1847   myMesh = 0;
1848 }
1849
1850 void ElementsOnSurface::SetMesh( SMDS_Mesh* theMesh )
1851
1852   if ( myMesh == theMesh )
1853     return;
1854   myMesh = theMesh;
1855   myIds.Clear();
1856   process();
1857 }
1858
1859 bool ElementsOnSurface::IsSatisfy( long theElementId )
1860 {
1861   return myIds.Contains( theElementId );
1862 }
1863
1864 SMDSAbs_ElementType ElementsOnSurface::GetType() const
1865 { return myType; }
1866
1867 void ElementsOnSurface::SetTolerance( const double theToler )
1868 { myToler = theToler; }
1869
1870 double ElementsOnSurface::GetTolerance() const
1871 {
1872   return myToler;
1873 }
1874
1875 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
1876                                     const SMDSAbs_ElementType theType )
1877 {
1878   myType = theType;
1879   mySurf.Nullify();
1880   if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
1881   {
1882     mySurf.Nullify();
1883     return;
1884   }
1885   TopoDS_Face aFace = TopoDS::Face( theShape );
1886   mySurf = BRep_Tool::Surface( aFace );
1887 }
1888
1889 void ElementsOnSurface::process()
1890 {
1891   myIds.Clear();
1892   if ( mySurf.IsNull() )
1893     return;
1894
1895   if ( myMesh == 0 )
1896     return;
1897
1898   if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
1899   {
1900     SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1901     for(; anIter->more(); )
1902       process( anIter->next() );
1903   }
1904
1905   if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
1906   {
1907     SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
1908     for(; anIter->more(); )
1909       process( anIter->next() );
1910   }
1911
1912   if ( myType == SMDSAbs_Node )
1913   {
1914     SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
1915     for(; anIter->more(); )
1916       process( anIter->next() );
1917   }
1918 }
1919
1920 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
1921 {
1922   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
1923   bool isSatisfy = true;
1924   for ( ; aNodeItr->more(); )
1925   {
1926     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
1927     if ( !isOnSurface( aNode ) )
1928     {
1929       isSatisfy = false;
1930       break;
1931     }
1932   }
1933   if ( isSatisfy )
1934     myIds.Add( theElemPtr->GetID() );
1935 }
1936
1937 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
1938 {
1939   if ( mySurf.IsNull() )
1940     return false;
1941
1942   gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
1943   double aToler2 = myToler * myToler;
1944   if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
1945   {
1946     gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
1947     if ( aPln.SquareDistance( aPnt ) > aToler2 )
1948       return false;
1949   }
1950   else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
1951   {
1952     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
1953     double aRad = aCyl.Radius();
1954     gp_Ax3 anAxis = aCyl.Position();
1955     gp_XYZ aLoc = aCyl.Location().XYZ();
1956     double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
1957     double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
1958     if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )
1959       return false;
1960   }
1961   else
1962     return false;
1963
1964   return true;
1965 }