Salome HOME
Color Number (Color Group) parameter is returned for compatibility
[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     //BUG SWP12743
927     if ( A < Precision::Angular() )
928       return 0.;
929
930     return A * 180. / PI;
931   }
932 }
933
934 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
935 {
936   // the skew is in the range [0.0,PI/2].
937   // 0.0 = good
938   // PI/2 = bad
939   return Value;
940 }
941
942 SMDSAbs_ElementType Skew::GetType() const
943 {
944   return SMDSAbs_Face;
945 }
946
947
948 /*
949   Class       : Area
950   Description : Functor for calculating area
951 */
952 double Area::GetValue( const TSequenceOfXYZ& P )
953 {
954   gp_Vec aVec1( P(2) - P(1) );
955   gp_Vec aVec2( P(3) - P(1) );
956   gp_Vec SumVec = aVec1 ^ aVec2;
957   for (int i=4; i<=P.size(); i++) {
958     gp_Vec aVec1( P(i-1) - P(1) );
959     gp_Vec aVec2( P(i) - P(1) );
960     gp_Vec tmp = aVec1 ^ aVec2;
961     SumVec.Add(tmp);
962   }
963   return SumVec.Magnitude() * 0.5;
964 }
965
966 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
967 {
968   // meaningless as it is not a quality control functor
969   return Value;
970 }
971
972 SMDSAbs_ElementType Area::GetType() const
973 {
974   return SMDSAbs_Face;
975 }
976
977
978 /*
979   Class       : Length
980   Description : Functor for calculating length off edge
981 */
982 double Length::GetValue( const TSequenceOfXYZ& P )
983 {
984   switch ( P.size() ) {
985   case 2:  return getDistance( P( 1 ), P( 2 ) );
986   case 3:  return getDistance( P( 1 ), P( 2 ) ) + getDistance( P( 2 ), P( 3 ) );
987   default: return 0.;
988   }
989 }
990
991 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
992 {
993   // meaningless as it is not quality control functor
994   return Value;
995 }
996
997 SMDSAbs_ElementType Length::GetType() const
998 {
999   return SMDSAbs_Edge;
1000 }
1001
1002 /*
1003   Class       : Length2D
1004   Description : Functor for calculating length of edge
1005 */
1006
1007 double Length2D::GetValue( long theElementId)
1008 {
1009   TSequenceOfXYZ P;
1010
1011   //cout<<"Length2D::GetValue"<<endl;
1012   if (GetPoints(theElementId,P)){
1013     //for(int jj=1; jj<=P.size(); jj++)
1014     //  cout<<"jj="<<jj<<" P("<<P(jj).X()<<","<<P(jj).Y()<<","<<P(jj).Z()<<")"<<endl;
1015
1016     double  aVal;// = GetValue( P );
1017     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
1018     SMDSAbs_ElementType aType = aElem->GetType();
1019
1020     int len = P.size();
1021
1022     switch (aType){
1023     case SMDSAbs_All:
1024     case SMDSAbs_Node:
1025     case SMDSAbs_Edge:
1026       if (len == 2){
1027         aVal = getDistance( P( 1 ), P( 2 ) );
1028         break;
1029       }
1030       else if (len == 3){ // quadratic edge
1031         aVal = getDistance(P( 1 ),P( 3 )) + getDistance(P( 3 ),P( 2 ));
1032         break;
1033       }
1034     case SMDSAbs_Face:
1035       if (len == 3){ // triangles
1036         double L1 = getDistance(P( 1 ),P( 2 ));
1037         double L2 = getDistance(P( 2 ),P( 3 ));
1038         double L3 = getDistance(P( 3 ),P( 1 ));
1039         aVal = Max(L1,Max(L2,L3));
1040         break;
1041       }
1042       else if (len == 4){ // quadrangles
1043         double L1 = getDistance(P( 1 ),P( 2 ));
1044         double L2 = getDistance(P( 2 ),P( 3 ));
1045         double L3 = getDistance(P( 3 ),P( 4 ));
1046         double L4 = getDistance(P( 4 ),P( 1 ));
1047         aVal = Max(Max(L1,L2),Max(L3,L4));
1048         break;
1049       }
1050       if (len == 6){ // quadratic triangles
1051         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1052         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1053         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
1054         aVal = Max(L1,Max(L2,L3));
1055         //cout<<"L1="<<L1<<" L2="<<L2<<"L3="<<L3<<" aVal="<<aVal<<endl;
1056         break;
1057       }
1058       else if (len == 8){ // quadratic quadrangles
1059         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1060         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1061         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
1062         double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1063         aVal = Max(Max(L1,L2),Max(L3,L4));
1064         break;
1065       }
1066     case SMDSAbs_Volume:
1067       if (len == 4){ // tetraidrs
1068         double L1 = getDistance(P( 1 ),P( 2 ));
1069         double L2 = getDistance(P( 2 ),P( 3 ));
1070         double L3 = getDistance(P( 3 ),P( 1 ));
1071         double L4 = getDistance(P( 1 ),P( 4 ));
1072         double L5 = getDistance(P( 2 ),P( 4 ));
1073         double L6 = getDistance(P( 3 ),P( 4 ));
1074         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1075         break;
1076       }
1077       else if (len == 5){ // piramids
1078         double L1 = getDistance(P( 1 ),P( 2 ));
1079         double L2 = getDistance(P( 2 ),P( 3 ));
1080         double L3 = getDistance(P( 3 ),P( 1 ));
1081         double L4 = getDistance(P( 4 ),P( 1 ));
1082         double L5 = getDistance(P( 1 ),P( 5 ));
1083         double L6 = getDistance(P( 2 ),P( 5 ));
1084         double L7 = getDistance(P( 3 ),P( 5 ));
1085         double L8 = getDistance(P( 4 ),P( 5 ));
1086
1087         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1088         aVal = Max(aVal,Max(L7,L8));
1089         break;
1090       }
1091       else if (len == 6){ // pentaidres
1092         double L1 = getDistance(P( 1 ),P( 2 ));
1093         double L2 = getDistance(P( 2 ),P( 3 ));
1094         double L3 = getDistance(P( 3 ),P( 1 ));
1095         double L4 = getDistance(P( 4 ),P( 5 ));
1096         double L5 = getDistance(P( 5 ),P( 6 ));
1097         double L6 = getDistance(P( 6 ),P( 4 ));
1098         double L7 = getDistance(P( 1 ),P( 4 ));
1099         double L8 = getDistance(P( 2 ),P( 5 ));
1100         double L9 = getDistance(P( 3 ),P( 6 ));
1101
1102         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1103         aVal = Max(aVal,Max(Max(L7,L8),L9));
1104         break;
1105       }
1106       else if (len == 8){ // hexaider
1107         double L1 = getDistance(P( 1 ),P( 2 ));
1108         double L2 = getDistance(P( 2 ),P( 3 ));
1109         double L3 = getDistance(P( 3 ),P( 4 ));
1110         double L4 = getDistance(P( 4 ),P( 1 ));
1111         double L5 = getDistance(P( 5 ),P( 6 ));
1112         double L6 = getDistance(P( 6 ),P( 7 ));
1113         double L7 = getDistance(P( 7 ),P( 8 ));
1114         double L8 = getDistance(P( 8 ),P( 5 ));
1115         double L9 = getDistance(P( 1 ),P( 5 ));
1116         double L10= getDistance(P( 2 ),P( 6 ));
1117         double L11= getDistance(P( 3 ),P( 7 ));
1118         double L12= getDistance(P( 4 ),P( 8 ));
1119
1120         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1121         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1122         aVal = Max(aVal,Max(L11,L12));
1123         break;
1124
1125       }
1126
1127       if (len == 10){ // quadratic tetraidrs
1128         double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
1129         double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
1130         double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
1131         double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1132         double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
1133         double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
1134         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1135         break;
1136       }
1137       else if (len == 13){ // quadratic piramids
1138         double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
1139         double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
1140         double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1141         double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1142         double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1143         double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
1144         double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
1145         double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
1146         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1147         aVal = Max(aVal,Max(L7,L8));
1148         break;
1149       }
1150       else if (len == 15){ // quadratic pentaidres
1151         double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
1152         double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
1153         double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1154         double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1155         double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
1156         double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
1157         double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
1158         double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
1159         double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
1160         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1161         aVal = Max(aVal,Max(Max(L7,L8),L9));
1162         break;
1163       }
1164       else if (len == 20){ // quadratic hexaider
1165         double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
1166         double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
1167         double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
1168         double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
1169         double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
1170         double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
1171         double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
1172         double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
1173         double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
1174         double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
1175         double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
1176         double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
1177         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1178         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1179         aVal = Max(aVal,Max(L11,L12));
1180         break;
1181
1182       }
1183
1184     default: aVal=-1;
1185     }
1186
1187     if (aVal <0){
1188       return 0.;
1189     }
1190
1191     if ( myPrecision >= 0 )
1192     {
1193       double prec = pow( 10., (double)( myPrecision ) );
1194       aVal = floor( aVal * prec + 0.5 ) / prec;
1195     }
1196
1197     return aVal;
1198
1199   }
1200   return 0.;
1201 }
1202
1203 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1204 {
1205   // meaningless as it is not quality control functor
1206   return Value;
1207 }
1208
1209 SMDSAbs_ElementType Length2D::GetType() const
1210 {
1211   return SMDSAbs_Face;
1212 }
1213
1214 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
1215   myLength(theLength)
1216 {
1217   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1218   if(thePntId1 > thePntId2){
1219     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1220   }
1221 }
1222
1223 bool Length2D::Value::operator<(const Length2D::Value& x) const{
1224   if(myPntId[0] < x.myPntId[0]) return true;
1225   if(myPntId[0] == x.myPntId[0])
1226     if(myPntId[1] < x.myPntId[1]) return true;
1227   return false;
1228 }
1229
1230 void Length2D::GetValues(TValues& theValues){
1231   TValues aValues;
1232   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1233   for(; anIter->more(); ){
1234     const SMDS_MeshFace* anElem = anIter->next();
1235
1236     if(anElem->IsQuadratic()) {
1237       const SMDS_QuadraticFaceOfNodes* F =
1238         static_cast<const SMDS_QuadraticFaceOfNodes*>(anElem);
1239       // use special nodes iterator
1240       SMDS_NodeIteratorPtr anIter = F->interlacedNodesIterator();
1241       long aNodeId[4];
1242       gp_Pnt P[4];
1243
1244       double aLength;
1245       const SMDS_MeshElement* aNode;
1246       if(anIter->more()){
1247         aNode = anIter->next();
1248         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1249         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1250         aNodeId[0] = aNodeId[1] = aNode->GetID();
1251         aLength = 0;
1252       }
1253       for(; anIter->more(); ){
1254         const SMDS_MeshNode* N1 = static_cast<const SMDS_MeshNode*> (anIter->next());
1255         P[2] = gp_Pnt(N1->X(),N1->Y(),N1->Z());
1256         aNodeId[2] = N1->GetID();
1257         aLength = P[1].Distance(P[2]);
1258         if(!anIter->more()) break;
1259         const SMDS_MeshNode* N2 = static_cast<const SMDS_MeshNode*> (anIter->next());
1260         P[3] = gp_Pnt(N2->X(),N2->Y(),N2->Z());
1261         aNodeId[3] = N2->GetID();
1262         aLength += P[2].Distance(P[3]);
1263         Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1264         Value aValue2(aLength,aNodeId[2],aNodeId[3]);
1265         P[1] = P[3];
1266         aNodeId[1] = aNodeId[3];
1267         theValues.insert(aValue1);
1268         theValues.insert(aValue2);
1269       }
1270       aLength += P[2].Distance(P[0]);
1271       Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1272       Value aValue2(aLength,aNodeId[2],aNodeId[0]);
1273       theValues.insert(aValue1);
1274       theValues.insert(aValue2);
1275     }
1276     else {
1277       SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1278       long aNodeId[2];
1279       gp_Pnt P[3];
1280
1281       double aLength;
1282       const SMDS_MeshElement* aNode;
1283       if(aNodesIter->more()){
1284         aNode = aNodesIter->next();
1285         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1286         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1287         aNodeId[0] = aNodeId[1] = aNode->GetID();
1288         aLength = 0;
1289       }
1290       for(; aNodesIter->more(); ){
1291         aNode = aNodesIter->next();
1292         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1293         long anId = aNode->GetID();
1294         
1295         P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1296         
1297         aLength = P[1].Distance(P[2]);
1298         
1299         Value aValue(aLength,aNodeId[1],anId);
1300         aNodeId[1] = anId;
1301         P[1] = P[2];
1302         theValues.insert(aValue);
1303       }
1304
1305       aLength = P[0].Distance(P[1]);
1306
1307       Value aValue(aLength,aNodeId[0],aNodeId[1]);
1308       theValues.insert(aValue);
1309     }
1310   }
1311 }
1312
1313 /*
1314   Class       : MultiConnection
1315   Description : Functor for calculating number of faces conneted to the edge
1316 */
1317 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1318 {
1319   return 0;
1320 }
1321 double MultiConnection::GetValue( long theId )
1322 {
1323   return getNbMultiConnection( myMesh, theId );
1324 }
1325
1326 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1327 {
1328   // meaningless as it is not quality control functor
1329   return Value;
1330 }
1331
1332 SMDSAbs_ElementType MultiConnection::GetType() const
1333 {
1334   return SMDSAbs_Edge;
1335 }
1336
1337 /*
1338   Class       : MultiConnection2D
1339   Description : Functor for calculating number of faces conneted to the edge
1340 */
1341 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1342 {
1343   return 0;
1344 }
1345
1346 double MultiConnection2D::GetValue( long theElementId )
1347 {
1348   int aResult = 0;
1349
1350   const SMDS_MeshElement* aFaceElem = myMesh->FindElement(theElementId);
1351   SMDSAbs_ElementType aType = aFaceElem->GetType();
1352
1353   switch (aType) {
1354   case SMDSAbs_Face:
1355     {
1356       int i = 0, len = aFaceElem->NbNodes();
1357       SMDS_ElemIteratorPtr anIter = aFaceElem->nodesIterator();
1358       if (!anIter) break;
1359
1360       const SMDS_MeshNode *aNode, *aNode0;
1361       TColStd_MapOfInteger aMap, aMapPrev;
1362
1363       for (i = 0; i <= len; i++) {
1364         aMapPrev = aMap;
1365         aMap.Clear();
1366
1367         int aNb = 0;
1368         if (anIter->more()) {
1369           aNode = (SMDS_MeshNode*)anIter->next();
1370         } else {
1371           if (i == len)
1372             aNode = aNode0;
1373           else
1374             break;
1375         }
1376         if (!aNode) break;
1377         if (i == 0) aNode0 = aNode;
1378
1379         SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1380         while (anElemIter->more()) {
1381           const SMDS_MeshElement* anElem = anElemIter->next();
1382           if (anElem != 0 && anElem->GetType() == SMDSAbs_Face) {
1383             int anId = anElem->GetID();
1384
1385             aMap.Add(anId);
1386             if (aMapPrev.Contains(anId)) {
1387               aNb++;
1388             }
1389           }
1390         }
1391         aResult = Max(aResult, aNb);
1392       }
1393     }
1394     break;
1395   default:
1396     aResult = 0;
1397   }
1398
1399   return aResult;
1400 }
1401
1402 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1403 {
1404   // meaningless as it is not quality control functor
1405   return Value;
1406 }
1407
1408 SMDSAbs_ElementType MultiConnection2D::GetType() const
1409 {
1410   return SMDSAbs_Face;
1411 }
1412
1413 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1414 {
1415   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1416   if(thePntId1 > thePntId2){
1417     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1418   }
1419 }
1420
1421 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1422   if(myPntId[0] < x.myPntId[0]) return true;
1423   if(myPntId[0] == x.myPntId[0])
1424     if(myPntId[1] < x.myPntId[1]) return true;
1425   return false;
1426 }
1427
1428 void MultiConnection2D::GetValues(MValues& theValues){
1429   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1430   for(; anIter->more(); ){
1431     const SMDS_MeshFace* anElem = anIter->next();
1432     SMDS_ElemIteratorPtr aNodesIter;
1433     if ( anElem->IsQuadratic() )
1434       aNodesIter = static_cast<const SMDS_QuadraticFaceOfNodes*>
1435         (anElem)->interlacedNodesElemIterator();
1436     else
1437       aNodesIter = anElem->nodesIterator();
1438     long aNodeId[3];
1439
1440     //int aNbConnects=0;
1441     const SMDS_MeshNode* aNode0;
1442     const SMDS_MeshNode* aNode1;
1443     const SMDS_MeshNode* aNode2;
1444     if(aNodesIter->more()){
1445       aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1446       aNode1 = aNode0;
1447       const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1448       aNodeId[0] = aNodeId[1] = aNodes->GetID();
1449     }
1450     for(; aNodesIter->more(); ) {
1451       aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1452       long anId = aNode2->GetID();
1453       aNodeId[2] = anId;
1454
1455       Value aValue(aNodeId[1],aNodeId[2]);
1456       MValues::iterator aItr = theValues.find(aValue);
1457       if (aItr != theValues.end()){
1458         aItr->second += 1;
1459         //aNbConnects = nb;
1460       }
1461       else {
1462         theValues[aValue] = 1;
1463         //aNbConnects = 1;
1464       }
1465       //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1466       aNodeId[1] = aNodeId[2];
1467       aNode1 = aNode2;
1468     }
1469     Value aValue(aNodeId[0],aNodeId[2]);
1470     MValues::iterator aItr = theValues.find(aValue);
1471     if (aItr != theValues.end()) {
1472       aItr->second += 1;
1473       //aNbConnects = nb;
1474     }
1475     else {
1476       theValues[aValue] = 1;
1477       //aNbConnects = 1;
1478     }
1479     //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1480   }
1481
1482 }
1483
1484 /*
1485                             PREDICATES
1486 */
1487
1488 /*
1489   Class       : BadOrientedVolume
1490   Description : Predicate bad oriented volumes
1491 */
1492
1493 BadOrientedVolume::BadOrientedVolume()
1494 {
1495   myMesh = 0;
1496 }
1497
1498 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1499 {
1500   myMesh = theMesh;
1501 }
1502
1503 bool BadOrientedVolume::IsSatisfy( long theId )
1504 {
1505   if ( myMesh == 0 )
1506     return false;
1507
1508   SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1509   return !vTool.IsForward();
1510 }
1511
1512 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1513 {
1514   return SMDSAbs_Volume;
1515 }
1516
1517
1518
1519 /*
1520   Class       : FreeBorders
1521   Description : Predicate for free borders
1522 */
1523
1524 FreeBorders::FreeBorders()
1525 {
1526   myMesh = 0;
1527 }
1528
1529 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1530 {
1531   myMesh = theMesh;
1532 }
1533
1534 bool FreeBorders::IsSatisfy( long theId )
1535 {
1536   return getNbMultiConnection( myMesh, theId ) == 1;
1537 }
1538
1539 SMDSAbs_ElementType FreeBorders::GetType() const
1540 {
1541   return SMDSAbs_Edge;
1542 }
1543
1544
1545 /*
1546   Class       : FreeEdges
1547   Description : Predicate for free Edges
1548 */
1549 FreeEdges::FreeEdges()
1550 {
1551   myMesh = 0;
1552 }
1553
1554 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1555 {
1556   myMesh = theMesh;
1557 }
1558
1559 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  )
1560 {
1561   TColStd_MapOfInteger aMap;
1562   for ( int i = 0; i < 2; i++ )
1563   {
1564     SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1565     while( anElemIter->more() )
1566     {
1567       const SMDS_MeshElement* anElem = anElemIter->next();
1568       if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1569       {
1570         int anId = anElem->GetID();
1571
1572         if ( i == 0 )
1573           aMap.Add( anId );
1574         else if ( aMap.Contains( anId ) && anId != theFaceId )
1575           return false;
1576       }
1577     }
1578   }
1579   return true;
1580 }
1581
1582 bool FreeEdges::IsSatisfy( long theId )
1583 {
1584   if ( myMesh == 0 )
1585     return false;
1586
1587   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1588   if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1589     return false;
1590
1591   SMDS_ElemIteratorPtr anIter;
1592   if ( aFace->IsQuadratic() ) {
1593     anIter = static_cast<const SMDS_QuadraticFaceOfNodes*>
1594       (aFace)->interlacedNodesElemIterator();
1595   }
1596   else {
1597     anIter = aFace->nodesIterator();
1598   }
1599   if ( anIter == 0 )
1600     return false;
1601
1602   int i = 0, nbNodes = aFace->NbNodes();
1603   vector <const SMDS_MeshNode*> aNodes( nbNodes+1 );
1604   while( anIter->more() )
1605   {
1606     const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1607     if ( aNode == 0 )
1608       return false;
1609     aNodes[ i++ ] = aNode;
1610   }
1611   aNodes[ nbNodes ] = aNodes[ 0 ];
1612
1613   for ( i = 0; i < nbNodes; i++ )
1614     if ( IsFreeEdge( &aNodes[ i ], theId ) )
1615       return true;
1616
1617   return false;
1618 }
1619
1620 SMDSAbs_ElementType FreeEdges::GetType() const
1621 {
1622   return SMDSAbs_Face;
1623 }
1624
1625 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1626   myElemId(theElemId)
1627 {
1628   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1629   if(thePntId1 > thePntId2){
1630     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1631   }
1632 }
1633
1634 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1635   if(myPntId[0] < x.myPntId[0]) return true;
1636   if(myPntId[0] == x.myPntId[0])
1637     if(myPntId[1] < x.myPntId[1]) return true;
1638   return false;
1639 }
1640
1641 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1642                           FreeEdges::TBorders& theRegistry,
1643                           FreeEdges::TBorders& theContainer)
1644 {
1645   if(theRegistry.find(theBorder) == theRegistry.end()){
1646     theRegistry.insert(theBorder);
1647     theContainer.insert(theBorder);
1648   }else{
1649     theContainer.erase(theBorder);
1650   }
1651 }
1652
1653 void FreeEdges::GetBoreders(TBorders& theBorders)
1654 {
1655   TBorders aRegistry;
1656   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1657   for(; anIter->more(); ){
1658     const SMDS_MeshFace* anElem = anIter->next();
1659     long anElemId = anElem->GetID();
1660     SMDS_ElemIteratorPtr aNodesIter;
1661     if ( anElem->IsQuadratic() )
1662       aNodesIter = static_cast<const SMDS_QuadraticFaceOfNodes*>(anElem)->
1663         interlacedNodesElemIterator();
1664     else
1665       aNodesIter = anElem->nodesIterator();
1666     long aNodeId[2];
1667     const SMDS_MeshElement* aNode;
1668     if(aNodesIter->more()){
1669       aNode = aNodesIter->next();
1670       aNodeId[0] = aNodeId[1] = aNode->GetID();
1671     }
1672     for(; aNodesIter->more(); ){
1673       aNode = aNodesIter->next();
1674       long anId = aNode->GetID();
1675       Border aBorder(anElemId,aNodeId[1],anId);
1676       aNodeId[1] = anId;
1677       //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1678       UpdateBorders(aBorder,aRegistry,theBorders);
1679     }
1680     Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1681     //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1682     UpdateBorders(aBorder,aRegistry,theBorders);
1683   }
1684   //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1685 }
1686
1687 /*
1688   Class       : RangeOfIds
1689   Description : Predicate for Range of Ids.
1690                 Range may be specified with two ways.
1691                 1. Using AddToRange method
1692                 2. With SetRangeStr method. Parameter of this method is a string
1693                    like as "1,2,3,50-60,63,67,70-"
1694 */
1695
1696 //=======================================================================
1697 // name    : RangeOfIds
1698 // Purpose : Constructor
1699 //=======================================================================
1700 RangeOfIds::RangeOfIds()
1701 {
1702   myMesh = 0;
1703   myType = SMDSAbs_All;
1704 }
1705
1706 //=======================================================================
1707 // name    : SetMesh
1708 // Purpose : Set mesh
1709 //=======================================================================
1710 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1711 {
1712   myMesh = theMesh;
1713 }
1714
1715 //=======================================================================
1716 // name    : AddToRange
1717 // Purpose : Add ID to the range
1718 //=======================================================================
1719 bool RangeOfIds::AddToRange( long theEntityId )
1720 {
1721   myIds.Add( theEntityId );
1722   return true;
1723 }
1724
1725 //=======================================================================
1726 // name    : GetRangeStr
1727 // Purpose : Get range as a string.
1728 //           Example: "1,2,3,50-60,63,67,70-"
1729 //=======================================================================
1730 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1731 {
1732   theResStr.Clear();
1733
1734   TColStd_SequenceOfInteger     anIntSeq;
1735   TColStd_SequenceOfAsciiString aStrSeq;
1736
1737   TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1738   for ( ; anIter.More(); anIter.Next() )
1739   {
1740     int anId = anIter.Key();
1741     TCollection_AsciiString aStr( anId );
1742     anIntSeq.Append( anId );
1743     aStrSeq.Append( aStr );
1744   }
1745
1746   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1747   {
1748     int aMinId = myMin( i );
1749     int aMaxId = myMax( i );
1750
1751     TCollection_AsciiString aStr;
1752     if ( aMinId != IntegerFirst() )
1753       aStr += aMinId;
1754
1755     aStr += "-";
1756
1757     if ( aMaxId != IntegerLast() )
1758       aStr += aMaxId;
1759
1760     // find position of the string in result sequence and insert string in it
1761     if ( anIntSeq.Length() == 0 )
1762     {
1763       anIntSeq.Append( aMinId );
1764       aStrSeq.Append( aStr );
1765     }
1766     else
1767     {
1768       if ( aMinId < anIntSeq.First() )
1769       {
1770         anIntSeq.Prepend( aMinId );
1771         aStrSeq.Prepend( aStr );
1772       }
1773       else if ( aMinId > anIntSeq.Last() )
1774       {
1775         anIntSeq.Append( aMinId );
1776         aStrSeq.Append( aStr );
1777       }
1778       else
1779         for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1780           if ( aMinId < anIntSeq( j ) )
1781           {
1782             anIntSeq.InsertBefore( j, aMinId );
1783             aStrSeq.InsertBefore( j, aStr );
1784             break;
1785           }
1786     }
1787   }
1788
1789   if ( aStrSeq.Length() == 0 )
1790     return;
1791
1792   theResStr = aStrSeq( 1 );
1793   for ( int j = 2, k = aStrSeq.Length(); j <= k; j++  )
1794   {
1795     theResStr += ",";
1796     theResStr += aStrSeq( j );
1797   }
1798 }
1799
1800 //=======================================================================
1801 // name    : SetRangeStr
1802 // Purpose : Define range with string
1803 //           Example of entry string: "1,2,3,50-60,63,67,70-"
1804 //=======================================================================
1805 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1806 {
1807   myMin.Clear();
1808   myMax.Clear();
1809   myIds.Clear();
1810
1811   TCollection_AsciiString aStr = theStr;
1812   aStr.RemoveAll( ' ' );
1813   aStr.RemoveAll( '\t' );
1814
1815   for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1816     aStr.Remove( aPos, 2 );
1817
1818   TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1819   int i = 1;
1820   while ( tmpStr != "" )
1821   {
1822     tmpStr = aStr.Token( ",", i++ );
1823     int aPos = tmpStr.Search( '-' );
1824
1825     if ( aPos == -1 )
1826     {
1827       if ( tmpStr.IsIntegerValue() )
1828         myIds.Add( tmpStr.IntegerValue() );
1829       else
1830         return false;
1831     }
1832     else
1833     {
1834       TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1835       TCollection_AsciiString aMinStr = tmpStr;
1836
1837       while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1838       while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1839
1840       if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1841            !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1842         return false;
1843
1844       myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1845       myMax.Append( aMaxStr.IsEmpty() ? IntegerLast()  : aMaxStr.IntegerValue() );
1846     }
1847   }
1848
1849   return true;
1850 }
1851
1852 //=======================================================================
1853 // name    : GetType
1854 // Purpose : Get type of supported entities
1855 //=======================================================================
1856 SMDSAbs_ElementType RangeOfIds::GetType() const
1857 {
1858   return myType;
1859 }
1860
1861 //=======================================================================
1862 // name    : SetType
1863 // Purpose : Set type of supported entities
1864 //=======================================================================
1865 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1866 {
1867   myType = theType;
1868 }
1869
1870 //=======================================================================
1871 // name    : IsSatisfy
1872 // Purpose : Verify whether entity satisfies to this rpedicate
1873 //=======================================================================
1874 bool RangeOfIds::IsSatisfy( long theId )
1875 {
1876   if ( !myMesh )
1877     return false;
1878
1879   if ( myType == SMDSAbs_Node )
1880   {
1881     if ( myMesh->FindNode( theId ) == 0 )
1882       return false;
1883   }
1884   else
1885   {
1886     const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1887     if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1888       return false;
1889   }
1890
1891   if ( myIds.Contains( theId ) )
1892     return true;
1893
1894   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1895     if ( theId >= myMin( i ) && theId <= myMax( i ) )
1896       return true;
1897
1898   return false;
1899 }
1900
1901 /*
1902   Class       : Comparator
1903   Description : Base class for comparators
1904 */
1905 Comparator::Comparator():
1906   myMargin(0)
1907 {}
1908
1909 Comparator::~Comparator()
1910 {}
1911
1912 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1913 {
1914   if ( myFunctor )
1915     myFunctor->SetMesh( theMesh );
1916 }
1917
1918 void Comparator::SetMargin( double theValue )
1919 {
1920   myMargin = theValue;
1921 }
1922
1923 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1924 {
1925   myFunctor = theFunct;
1926 }
1927
1928 SMDSAbs_ElementType Comparator::GetType() const
1929 {
1930   return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1931 }
1932
1933 double Comparator::GetMargin()
1934 {
1935   return myMargin;
1936 }
1937
1938
1939 /*
1940   Class       : LessThan
1941   Description : Comparator "<"
1942 */
1943 bool LessThan::IsSatisfy( long theId )
1944 {
1945   return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1946 }
1947
1948
1949 /*
1950   Class       : MoreThan
1951   Description : Comparator ">"
1952 */
1953 bool MoreThan::IsSatisfy( long theId )
1954 {
1955   return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1956 }
1957
1958
1959 /*
1960   Class       : EqualTo
1961   Description : Comparator "="
1962 */
1963 EqualTo::EqualTo():
1964   myToler(Precision::Confusion())
1965 {}
1966
1967 bool EqualTo::IsSatisfy( long theId )
1968 {
1969   return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1970 }
1971
1972 void EqualTo::SetTolerance( double theToler )
1973 {
1974   myToler = theToler;
1975 }
1976
1977 double EqualTo::GetTolerance()
1978 {
1979   return myToler;
1980 }
1981
1982 /*
1983   Class       : LogicalNOT
1984   Description : Logical NOT predicate
1985 */
1986 LogicalNOT::LogicalNOT()
1987 {}
1988
1989 LogicalNOT::~LogicalNOT()
1990 {}
1991
1992 bool LogicalNOT::IsSatisfy( long theId )
1993 {
1994   return myPredicate && !myPredicate->IsSatisfy( theId );
1995 }
1996
1997 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1998 {
1999   if ( myPredicate )
2000     myPredicate->SetMesh( theMesh );
2001 }
2002
2003 void LogicalNOT::SetPredicate( PredicatePtr thePred )
2004 {
2005   myPredicate = thePred;
2006 }
2007
2008 SMDSAbs_ElementType LogicalNOT::GetType() const
2009 {
2010   return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
2011 }
2012
2013
2014 /*
2015   Class       : LogicalBinary
2016   Description : Base class for binary logical predicate
2017 */
2018 LogicalBinary::LogicalBinary()
2019 {}
2020
2021 LogicalBinary::~LogicalBinary()
2022 {}
2023
2024 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
2025 {
2026   if ( myPredicate1 )
2027     myPredicate1->SetMesh( theMesh );
2028
2029   if ( myPredicate2 )
2030     myPredicate2->SetMesh( theMesh );
2031 }
2032
2033 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
2034 {
2035   myPredicate1 = thePredicate;
2036 }
2037
2038 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
2039 {
2040   myPredicate2 = thePredicate;
2041 }
2042
2043 SMDSAbs_ElementType LogicalBinary::GetType() const
2044 {
2045   if ( !myPredicate1 || !myPredicate2 )
2046     return SMDSAbs_All;
2047
2048   SMDSAbs_ElementType aType1 = myPredicate1->GetType();
2049   SMDSAbs_ElementType aType2 = myPredicate2->GetType();
2050
2051   return aType1 == aType2 ? aType1 : SMDSAbs_All;
2052 }
2053
2054
2055 /*
2056   Class       : LogicalAND
2057   Description : Logical AND
2058 */
2059 bool LogicalAND::IsSatisfy( long theId )
2060 {
2061   return
2062     myPredicate1 &&
2063     myPredicate2 &&
2064     myPredicate1->IsSatisfy( theId ) &&
2065     myPredicate2->IsSatisfy( theId );
2066 }
2067
2068
2069 /*
2070   Class       : LogicalOR
2071   Description : Logical OR
2072 */
2073 bool LogicalOR::IsSatisfy( long theId )
2074 {
2075   return
2076     myPredicate1 &&
2077     myPredicate2 &&
2078     myPredicate1->IsSatisfy( theId ) ||
2079     myPredicate2->IsSatisfy( theId );
2080 }
2081
2082
2083 /*
2084                               FILTER
2085 */
2086
2087 Filter::Filter()
2088 {}
2089
2090 Filter::~Filter()
2091 {}
2092
2093 void Filter::SetPredicate( PredicatePtr thePredicate )
2094 {
2095   myPredicate = thePredicate;
2096 }
2097
2098 template<class TElement, class TIterator, class TPredicate>
2099 inline void FillSequence(const TIterator& theIterator,
2100                          TPredicate& thePredicate,
2101                          Filter::TIdSequence& theSequence)
2102 {
2103   if ( theIterator ) {
2104     while( theIterator->more() ) {
2105       TElement anElem = theIterator->next();
2106       long anId = anElem->GetID();
2107       if ( thePredicate->IsSatisfy( anId ) )
2108         theSequence.push_back( anId );
2109     }
2110   }
2111 }
2112
2113 void
2114 Filter::
2115 GetElementsId( const SMDS_Mesh* theMesh,
2116                PredicatePtr thePredicate,
2117                TIdSequence& theSequence )
2118 {
2119   theSequence.clear();
2120
2121   if ( !theMesh || !thePredicate )
2122     return;
2123
2124   thePredicate->SetMesh( theMesh );
2125
2126   SMDSAbs_ElementType aType = thePredicate->GetType();
2127   switch(aType){
2128   case SMDSAbs_Node:
2129     FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
2130     break;
2131   case SMDSAbs_Edge:
2132     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
2133     break;
2134   case SMDSAbs_Face:
2135     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
2136     break;
2137   case SMDSAbs_Volume:
2138     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
2139     break;
2140   case SMDSAbs_All:
2141     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
2142     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
2143     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
2144     break;
2145   }
2146 }
2147
2148 void
2149 Filter::GetElementsId( const SMDS_Mesh* theMesh,
2150                        Filter::TIdSequence& theSequence )
2151 {
2152   GetElementsId(theMesh,myPredicate,theSequence);
2153 }
2154
2155 /*
2156                               ManifoldPart
2157 */
2158
2159 typedef std::set<SMDS_MeshFace*>                    TMapOfFacePtr;
2160
2161 /*
2162    Internal class Link
2163 */
2164
2165 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
2166                           SMDS_MeshNode* theNode2 )
2167 {
2168   myNode1 = theNode1;
2169   myNode2 = theNode2;
2170 }
2171
2172 ManifoldPart::Link::~Link()
2173 {
2174   myNode1 = 0;
2175   myNode2 = 0;
2176 }
2177
2178 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
2179 {
2180   if ( myNode1 == theLink.myNode1 &&
2181        myNode2 == theLink.myNode2 )
2182     return true;
2183   else if ( myNode1 == theLink.myNode2 &&
2184             myNode2 == theLink.myNode1 )
2185     return true;
2186   else
2187     return false;
2188 }
2189
2190 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
2191 {
2192   if(myNode1 < x.myNode1) return true;
2193   if(myNode1 == x.myNode1)
2194     if(myNode2 < x.myNode2) return true;
2195   return false;
2196 }
2197
2198 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
2199                             const ManifoldPart::Link& theLink2 )
2200 {
2201   return theLink1.IsEqual( theLink2 );
2202 }
2203
2204 ManifoldPart::ManifoldPart()
2205 {
2206   myMesh = 0;
2207   myAngToler = Precision::Angular();
2208   myIsOnlyManifold = true;
2209 }
2210
2211 ManifoldPart::~ManifoldPart()
2212 {
2213   myMesh = 0;
2214 }
2215
2216 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
2217 {
2218   myMesh = theMesh;
2219   process();
2220 }
2221
2222 SMDSAbs_ElementType ManifoldPart::GetType() const
2223 { return SMDSAbs_Face; }
2224
2225 bool ManifoldPart::IsSatisfy( long theElementId )
2226 {
2227   return myMapIds.Contains( theElementId );
2228 }
2229
2230 void ManifoldPart::SetAngleTolerance( const double theAngToler )
2231 { myAngToler = theAngToler; }
2232
2233 double ManifoldPart::GetAngleTolerance() const
2234 { return myAngToler; }
2235
2236 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
2237 { myIsOnlyManifold = theIsOnly; }
2238
2239 void ManifoldPart::SetStartElem( const long  theStartId )
2240 { myStartElemId = theStartId; }
2241
2242 bool ManifoldPart::process()
2243 {
2244   myMapIds.Clear();
2245   myMapBadGeomIds.Clear();
2246
2247   myAllFacePtr.clear();
2248   myAllFacePtrIntDMap.clear();
2249   if ( !myMesh )
2250     return false;
2251
2252   // collect all faces into own map
2253   SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
2254   for (; anFaceItr->more(); )
2255   {
2256     SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
2257     myAllFacePtr.push_back( aFacePtr );
2258     myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
2259   }
2260
2261   SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
2262   if ( !aStartFace )
2263     return false;
2264
2265   // the map of non manifold links and bad geometry
2266   TMapOfLink aMapOfNonManifold;
2267   TColStd_MapOfInteger aMapOfTreated;
2268
2269   // begin cycle on faces from start index and run on vector till the end
2270   //  and from begin to start index to cover whole vector
2271   const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
2272   bool isStartTreat = false;
2273   for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
2274   {
2275     if ( fi == aStartIndx )
2276       isStartTreat = true;
2277     // as result next time when fi will be equal to aStartIndx
2278
2279     SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
2280     if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
2281       continue;
2282
2283     aMapOfTreated.Add( aFacePtr->GetID() );
2284     TColStd_MapOfInteger aResFaces;
2285     if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
2286                          aMapOfNonManifold, aResFaces ) )
2287       continue;
2288     TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
2289     for ( ; anItr.More(); anItr.Next() )
2290     {
2291       int aFaceId = anItr.Key();
2292       aMapOfTreated.Add( aFaceId );
2293       myMapIds.Add( aFaceId );
2294     }
2295
2296     if ( fi == ( myAllFacePtr.size() - 1 ) )
2297       fi = 0;
2298   } // end run on vector of faces
2299   return !myMapIds.IsEmpty();
2300 }
2301
2302 static void getLinks( const SMDS_MeshFace* theFace,
2303                       ManifoldPart::TVectorOfLink& theLinks )
2304 {
2305   int aNbNode = theFace->NbNodes();
2306   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2307   int i = 1;
2308   SMDS_MeshNode* aNode = 0;
2309   for ( ; aNodeItr->more() && i <= aNbNode; )
2310   {
2311
2312     SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
2313     if ( i == 1 )
2314       aNode = aN1;
2315     i++;
2316     SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
2317     i++;
2318     ManifoldPart::Link aLink( aN1, aN2 );
2319     theLinks.push_back( aLink );
2320   }
2321 }
2322
2323 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
2324 {
2325   gp_XYZ n;
2326   int aNbNode = theFace->NbNodes();
2327   TColgp_Array1OfXYZ anArrOfXYZ(1,4);
2328   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2329   int i = 1;
2330   for ( ; aNodeItr->more() && i <= 4; i++ ) {
2331     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2332     anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2333   }
2334
2335   gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2336   gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2337   n  = q1 ^ q2;
2338   if ( aNbNode > 3 ) {
2339     gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2340     n += q2 ^ q3;
2341   }
2342   double len = n.Modulus();
2343   if ( len > 0 )
2344     n /= len;
2345
2346   return n;
2347 }
2348
2349 bool ManifoldPart::findConnected
2350                  ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2351                   SMDS_MeshFace*                           theStartFace,
2352                   ManifoldPart::TMapOfLink&                theNonManifold,
2353                   TColStd_MapOfInteger&                    theResFaces )
2354 {
2355   theResFaces.Clear();
2356   if ( !theAllFacePtrInt.size() )
2357     return false;
2358
2359   if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2360   {
2361     myMapBadGeomIds.Add( theStartFace->GetID() );
2362     return false;
2363   }
2364
2365   ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2366   ManifoldPart::TVectorOfLink aSeqOfBoundary;
2367   theResFaces.Add( theStartFace->GetID() );
2368   ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2369
2370   expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2371                  aDMapLinkFace, theNonManifold, theStartFace );
2372
2373   bool isDone = false;
2374   while ( !isDone && aMapOfBoundary.size() != 0 )
2375   {
2376     bool isToReset = false;
2377     ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2378     for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2379     {
2380       ManifoldPart::Link aLink = *pLink;
2381       if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2382         continue;
2383       // each link could be treated only once
2384       aMapToSkip.insert( aLink );
2385
2386       ManifoldPart::TVectorOfFacePtr aFaces;
2387       // find next
2388       if ( myIsOnlyManifold &&
2389            (theNonManifold.find( aLink ) != theNonManifold.end()) )
2390         continue;
2391       else
2392       {
2393         getFacesByLink( aLink, aFaces );
2394         // filter the element to keep only indicated elements
2395         ManifoldPart::TVectorOfFacePtr aFiltered;
2396         ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2397         for ( ; pFace != aFaces.end(); ++pFace )
2398         {
2399           SMDS_MeshFace* aFace = *pFace;
2400           if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2401             aFiltered.push_back( aFace );
2402         }
2403         aFaces = aFiltered;
2404         if ( aFaces.size() < 2 )  // no neihgbour faces
2405           continue;
2406         else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2407         {
2408           theNonManifold.insert( aLink );
2409           continue;
2410         }
2411       }
2412
2413       // compare normal with normals of neighbor element
2414       SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2415       ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2416       for ( ; pFace != aFaces.end(); ++pFace )
2417       {
2418         SMDS_MeshFace* aNextFace = *pFace;
2419         if ( aPrevFace == aNextFace )
2420           continue;
2421         int anNextFaceID = aNextFace->GetID();
2422         if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2423          // should not be with non manifold restriction. probably bad topology
2424           continue;
2425         // check if face was treated and skipped
2426         if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2427              !isInPlane( aPrevFace, aNextFace ) )
2428           continue;
2429         // add new element to connected and extend the boundaries.
2430         theResFaces.Add( anNextFaceID );
2431         expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2432                         aDMapLinkFace, theNonManifold, aNextFace );
2433         isToReset = true;
2434       }
2435     }
2436     isDone = !isToReset;
2437   }
2438
2439   return !theResFaces.IsEmpty();
2440 }
2441
2442 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2443                               const SMDS_MeshFace* theFace2 )
2444 {
2445   gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2446   gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2447   if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2448   {
2449     myMapBadGeomIds.Add( theFace2->GetID() );
2450     return false;
2451   }
2452   if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2453     return true;
2454
2455   return false;
2456 }
2457
2458 void ManifoldPart::expandBoundary
2459                    ( ManifoldPart::TMapOfLink&            theMapOfBoundary,
2460                      ManifoldPart::TVectorOfLink&         theSeqOfBoundary,
2461                      ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2462                      ManifoldPart::TMapOfLink&            theNonManifold,
2463                      SMDS_MeshFace*                       theNextFace ) const
2464 {
2465   ManifoldPart::TVectorOfLink aLinks;
2466   getLinks( theNextFace, aLinks );
2467   int aNbLink = (int)aLinks.size();
2468   for ( int i = 0; i < aNbLink; i++ )
2469   {
2470     ManifoldPart::Link aLink = aLinks[ i ];
2471     if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2472       continue;
2473     if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2474     {
2475       if ( myIsOnlyManifold )
2476       {
2477         // remove from boundary
2478         theMapOfBoundary.erase( aLink );
2479         ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2480         for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2481         {
2482           ManifoldPart::Link aBoundLink = *pLink;
2483           if ( aBoundLink.IsEqual( aLink ) )
2484           {
2485             theSeqOfBoundary.erase( pLink );
2486             break;
2487           }
2488         }
2489       }
2490     }
2491     else
2492     {
2493       theMapOfBoundary.insert( aLink );
2494       theSeqOfBoundary.push_back( aLink );
2495       theDMapLinkFacePtr[ aLink ] = theNextFace;
2496     }
2497   }
2498 }
2499
2500 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2501                                    ManifoldPart::TVectorOfFacePtr& theFaces ) const
2502 {
2503   SMDS_Mesh::SetOfFaces aSetOfFaces;
2504   // take all faces that shared first node
2505   SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2506   for ( ; anItr->more(); )
2507   {
2508     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2509     if ( !aFace )
2510       continue;
2511     aSetOfFaces.Add( aFace );
2512   }
2513   // take all faces that shared second node
2514   anItr = theLink.myNode2->facesIterator();
2515   // find the common part of two sets
2516   for ( ; anItr->more(); )
2517   {
2518     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2519     if ( aSetOfFaces.Contains( aFace ) )
2520       theFaces.push_back( aFace );
2521   }
2522 }
2523
2524
2525 /*
2526    ElementsOnSurface
2527 */
2528
2529 ElementsOnSurface::ElementsOnSurface()
2530 {
2531   myMesh = 0;
2532   myIds.Clear();
2533   myType = SMDSAbs_All;
2534   mySurf.Nullify();
2535   myToler = Precision::Confusion();
2536   myUseBoundaries = false;
2537 }
2538
2539 ElementsOnSurface::~ElementsOnSurface()
2540 {
2541   myMesh = 0;
2542 }
2543
2544 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2545 {
2546   if ( myMesh == theMesh )
2547     return;
2548   myMesh = theMesh;
2549   process();
2550 }
2551
2552 bool ElementsOnSurface::IsSatisfy( long theElementId )
2553 {
2554   return myIds.Contains( theElementId );
2555 }
2556
2557 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2558 { return myType; }
2559
2560 void ElementsOnSurface::SetTolerance( const double theToler )
2561 {
2562   if ( myToler != theToler )
2563     myIds.Clear();
2564   myToler = theToler;
2565 }
2566
2567 double ElementsOnSurface::GetTolerance() const
2568 { return myToler; }
2569
2570 void ElementsOnSurface::SetUseBoundaries( bool theUse )
2571 {
2572   if ( myUseBoundaries != theUse ) {
2573     myUseBoundaries = theUse;
2574     SetSurface( mySurf, myType );
2575   }
2576 }
2577
2578 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2579                                     const SMDSAbs_ElementType theType )
2580 {
2581   myIds.Clear();
2582   myType = theType;
2583   mySurf.Nullify();
2584   if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2585     return;
2586   mySurf = TopoDS::Face( theShape );
2587   BRepAdaptor_Surface SA( mySurf, myUseBoundaries );
2588   Standard_Real
2589     u1 = SA.FirstUParameter(),
2590     u2 = SA.LastUParameter(),
2591     v1 = SA.FirstVParameter(),
2592     v2 = SA.LastVParameter();
2593   Handle(Geom_Surface) surf = BRep_Tool::Surface( mySurf );
2594   myProjector.Init( surf, u1,u2, v1,v2 );
2595   process();
2596 }
2597
2598 void ElementsOnSurface::process()
2599 {
2600   myIds.Clear();
2601   if ( mySurf.IsNull() )
2602     return;
2603
2604   if ( myMesh == 0 )
2605     return;
2606
2607   if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2608   {
2609     myIds.ReSize( myMesh->NbFaces() );
2610     SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2611     for(; anIter->more(); )
2612       process( anIter->next() );
2613   }
2614
2615   if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2616   {
2617     myIds.ReSize( myMesh->NbEdges() );
2618     SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2619     for(; anIter->more(); )
2620       process( anIter->next() );
2621   }
2622
2623   if ( myType == SMDSAbs_Node )
2624   {
2625     myIds.ReSize( myMesh->NbNodes() );
2626     SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2627     for(; anIter->more(); )
2628       process( anIter->next() );
2629   }
2630 }
2631
2632 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2633 {
2634   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2635   bool isSatisfy = true;
2636   for ( ; aNodeItr->more(); )
2637   {
2638     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2639     if ( !isOnSurface( aNode ) )
2640     {
2641       isSatisfy = false;
2642       break;
2643     }
2644   }
2645   if ( isSatisfy )
2646     myIds.Add( theElemPtr->GetID() );
2647 }
2648
2649 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode )
2650 {
2651   if ( mySurf.IsNull() )
2652     return false;
2653
2654   gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2655   //  double aToler2 = myToler * myToler;
2656 //   if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2657 //   {
2658 //     gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2659 //     if ( aPln.SquareDistance( aPnt ) > aToler2 )
2660 //       return false;
2661 //   }
2662 //   else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2663 //   {
2664 //     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2665 //     double aRad = aCyl.Radius();
2666 //     gp_Ax3 anAxis = aCyl.Position();
2667 //     gp_XYZ aLoc = aCyl.Location().XYZ();
2668 //     double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2669 //     double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2670 //     if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )
2671 //       return false;
2672 //   }
2673 //   else
2674 //     return false;
2675   myProjector.Perform( aPnt );
2676   bool isOn = ( myProjector.IsDone() && myProjector.LowerDistance() <= myToler );
2677
2678   return isOn;
2679 }