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