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