Salome HOME
b00c87e68d56cb87ab6aa425c65c3fcabc2ecbbb
[modules/smesh.git] / src / Controls / SMESH_Controls.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SMESH_ControlsDef.hxx"
24
25 #include "SMDS_BallElement.hxx"
26 #include "SMDS_Iterator.hxx"
27 #include "SMDS_Mesh.hxx"
28 #include "SMDS_MeshElement.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_QuadraticEdge.hxx"
31 #include "SMDS_QuadraticFaceOfNodes.hxx"
32 #include "SMDS_VolumeTool.hxx"
33 #include "SMESHDS_GroupBase.hxx"
34 #include "SMESHDS_GroupOnFilter.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_MeshAlgos.hxx"
37 #include "SMESH_OctreeNode.hxx"
38
39 #include <Basics_Utils.hxx>
40
41 #include <BRepAdaptor_Surface.hxx>
42 #include <BRepBndLib.hxx>
43 #include <BRepClass_FaceClassifier.hxx>
44 #include <BRep_Tool.hxx>
45 #include <Geom_CylindricalSurface.hxx>
46 #include <Geom_Plane.hxx>
47 #include <Geom_Surface.hxx>
48 #include <NCollection_Map.hxx>
49 #include <Precision.hxx>
50 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
51 #include <TColStd_MapOfInteger.hxx>
52 #include <TColStd_SequenceOfAsciiString.hxx>
53 #include <TColgp_Array1OfXYZ.hxx>
54 #include <TopAbs.hxx>
55 #include <TopExp.hxx>
56 #include <TopoDS.hxx>
57 #include <TopoDS_Edge.hxx>
58 #include <TopoDS_Face.hxx>
59 #include <TopoDS_Iterator.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <gp_Ax3.hxx>
63 #include <gp_Cylinder.hxx>
64 #include <gp_Dir.hxx>
65 #include <gp_Pln.hxx>
66 #include <gp_Pnt.hxx>
67 #include <gp_Vec.hxx>
68 #include <gp_XYZ.hxx>
69
70 #include <vtkMeshQuality.h>
71
72 #include <set>
73 #include <limits>
74
75 /*
76                             AUXILIARY METHODS
77 */
78
79 namespace {
80
81   const double theEps = 1e-100;
82   const double theInf = 1e+100;
83
84   inline gp_XYZ gpXYZ(const SMDS_MeshNode* aNode )
85   {
86     return gp_XYZ(aNode->X(), aNode->Y(), aNode->Z() );
87   }
88
89   inline double getAngle( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
90   {
91     gp_Vec v1( P1 - P2 ), v2( P3 - P2 );
92
93     return v1.Magnitude() < gp::Resolution() ||
94       v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
95   }
96
97   inline double getArea( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
98   {
99     gp_Vec aVec1( P2 - P1 );
100     gp_Vec aVec2( P3 - P1 );
101     return ( aVec1 ^ aVec2 ).Magnitude() * 0.5;
102   }
103
104   inline double getArea( const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3 )
105   {
106     return getArea( P1.XYZ(), P2.XYZ(), P3.XYZ() );
107   }
108
109
110
111   inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
112   {
113     double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
114     return aDist;
115   }
116
117   int getNbMultiConnection( const SMDS_Mesh* theMesh, const int theId )
118   {
119     if ( theMesh == 0 )
120       return 0;
121
122     const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
123     if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge/* || anEdge->NbNodes() != 2 */)
124       return 0;
125
126     // for each pair of nodes in anEdge (there are 2 pairs in a quadratic edge)
127     // count elements containing both nodes of the pair.
128     // Note that there may be such cases for a quadratic edge (a horizontal line):
129     //
130     //  Case 1          Case 2
131     //  |     |      |        |      |
132     //  |     |      |        |      |
133     //  +-----+------+  +-----+------+ 
134     //  |            |  |            |
135     //  |            |  |            |
136     // result sould be 2 in both cases
137     //
138     int aResult0 = 0, aResult1 = 0;
139      // last node, it is a medium one in a quadratic edge
140     const SMDS_MeshNode* aLastNode = anEdge->GetNode( anEdge->NbNodes() - 1 );
141     const SMDS_MeshNode*    aNode0 = anEdge->GetNode( 0 );
142     const SMDS_MeshNode*    aNode1 = anEdge->GetNode( 1 );
143     if ( aNode1 == aLastNode ) aNode1 = 0;
144
145     SMDS_ElemIteratorPtr anElemIter = aLastNode->GetInverseElementIterator();
146     while( anElemIter->more() ) {
147       const SMDS_MeshElement* anElem = anElemIter->next();
148       if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
149         SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
150         while ( anIter->more() ) {
151           if ( const SMDS_MeshElement* anElemNode = anIter->next() ) {
152             if ( anElemNode == aNode0 ) {
153               aResult0++;
154               if ( !aNode1 ) break; // not a quadratic edge
155             }
156             else if ( anElemNode == aNode1 )
157               aResult1++;
158           }
159         }
160       }
161     }
162     int aResult = std::max ( aResult0, aResult1 );
163
164     return aResult;
165   }
166
167   gp_XYZ getNormale( const SMDS_MeshFace* theFace, bool* ok=0 )
168   {
169     int aNbNode = theFace->NbNodes();
170
171     gp_XYZ q1 = gpXYZ( theFace->GetNode(1)) - gpXYZ( theFace->GetNode(0));
172     gp_XYZ q2 = gpXYZ( theFace->GetNode(2)) - gpXYZ( theFace->GetNode(0));
173     gp_XYZ n  = q1 ^ q2;
174     if ( aNbNode > 3 ) {
175       gp_XYZ q3 = gpXYZ( theFace->GetNode(3)) - gpXYZ( theFace->GetNode(0));
176       n += q2 ^ q3;
177     }
178     double len = n.Modulus();
179     bool zeroLen = ( len <= std::numeric_limits<double>::min());
180     if ( !zeroLen )
181       n /= len;
182
183     if (ok) *ok = !zeroLen;
184
185     return n;
186   }
187 }
188
189
190
191 using namespace SMESH::Controls;
192
193 /*
194  *                               FUNCTORS
195  */
196
197 //================================================================================
198 /*
199   Class       : NumericalFunctor
200   Description : Base class for numerical functors
201 */
202 //================================================================================
203
204 NumericalFunctor::NumericalFunctor():
205   myMesh(NULL)
206 {
207   myPrecision = -1;
208 }
209
210 void NumericalFunctor::SetMesh( const SMDS_Mesh* theMesh )
211 {
212   myMesh = theMesh;
213 }
214
215 bool NumericalFunctor::GetPoints(const int       theId,
216                                  TSequenceOfXYZ& theRes ) const
217 {
218   theRes.clear();
219
220   if ( myMesh == 0 )
221     return false;
222
223   const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
224   if ( !anElem || anElem->GetType() != this->GetType() )
225     return false;
226
227   return GetPoints( anElem, theRes );
228 }
229
230 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
231                                  TSequenceOfXYZ&         theRes )
232 {
233   theRes.clear();
234
235   if ( anElem == 0 )
236     return false;
237
238   theRes.reserve( anElem->NbNodes() );
239   theRes.setElement( anElem );
240
241   // Get nodes of the element
242   SMDS_ElemIteratorPtr anIter;
243
244   if ( anElem->IsQuadratic() ) {
245     switch ( anElem->GetType() ) {
246     case SMDSAbs_Edge:
247       anIter = dynamic_cast<const SMDS_VtkEdge*>
248         (anElem)->interlacedNodesElemIterator();
249       break;
250     case SMDSAbs_Face:
251       anIter = dynamic_cast<const SMDS_VtkFace*>
252         (anElem)->interlacedNodesElemIterator();
253       break;
254     default:
255       anIter = anElem->nodesIterator();
256     }
257   }
258   else {
259     anIter = anElem->nodesIterator();
260   }
261
262   if ( anIter ) {
263     double xyz[3];
264     while( anIter->more() ) {
265       if ( const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( anIter->next() ))
266       {
267         aNode->GetXYZ( xyz );
268         theRes.push_back( gp_XYZ( xyz[0], xyz[1], xyz[2] ));
269       }
270     }
271   }
272
273   return true;
274 }
275
276 long  NumericalFunctor::GetPrecision() const
277 {
278   return myPrecision;
279 }
280
281 void  NumericalFunctor::SetPrecision( const long thePrecision )
282 {
283   myPrecision = thePrecision;
284   myPrecisionValue = pow( 10., (double)( myPrecision ) );
285 }
286
287 double NumericalFunctor::GetValue( long theId )
288 {
289   double aVal = 0;
290
291   myCurrElement = myMesh->FindElement( theId );
292
293   TSequenceOfXYZ P;
294   if ( GetPoints( theId, P )) // elem type is checked here
295     aVal = Round( GetValue( P ));
296
297   return aVal;
298 }
299
300 double NumericalFunctor::Round( const double & aVal )
301 {
302   return ( myPrecision >= 0 ) ? floor( aVal * myPrecisionValue + 0.5 ) / myPrecisionValue : aVal;
303 }
304
305 //================================================================================
306 /*!
307  * \brief Return histogram of functor values
308  *  \param nbIntervals - number of intervals
309  *  \param nbEvents - number of mesh elements having values within i-th interval
310  *  \param funValues - boundaries of intervals
311  *  \param elements - elements to check vulue of; empty list means "of all"
312  *  \param minmax - boundaries of diapason of values to divide into intervals
313  */
314 //================================================================================
315
316 void NumericalFunctor::GetHistogram(int                     nbIntervals,
317                                     std::vector<int>&       nbEvents,
318                                     std::vector<double>&    funValues,
319                                     const std::vector<int>& elements,
320                                     const double*           minmax,
321                                     const bool              isLogarithmic)
322 {
323   if ( nbIntervals < 1 ||
324        !myMesh ||
325        !myMesh->GetMeshInfo().NbElements( GetType() ))
326     return;
327   nbEvents.resize( nbIntervals, 0 );
328   funValues.resize( nbIntervals+1 );
329
330   // get all values sorted
331   std::multiset< double > values;
332   if ( elements.empty() )
333   {
334     SMDS_ElemIteratorPtr elemIt = myMesh->elementsIterator( GetType() );
335     while ( elemIt->more() )
336       values.insert( GetValue( elemIt->next()->GetID() ));
337   }
338   else
339   {
340     std::vector<int>::const_iterator id = elements.begin();
341     for ( ; id != elements.end(); ++id )
342       values.insert( GetValue( *id ));
343   }
344
345   if ( minmax )
346   {
347     funValues[0] = minmax[0];
348     funValues[nbIntervals] = minmax[1];
349   }
350   else
351   {
352     funValues[0] = *values.begin();
353     funValues[nbIntervals] = *values.rbegin();
354   }
355   // case nbIntervals == 1
356   if ( nbIntervals == 1 )
357   {
358     nbEvents[0] = values.size();
359     return;
360   }
361   // case of 1 value
362   if (funValues.front() == funValues.back())
363   {
364     nbEvents.resize( 1 );
365     nbEvents[0] = values.size();
366     funValues[1] = funValues.back();
367     funValues.resize( 2 );
368   }
369   // generic case
370   std::multiset< double >::iterator min = values.begin(), max;
371   for ( int i = 0; i < nbIntervals; ++i )
372   {
373     // find end value of i-th interval
374     double r = (i+1) / double(nbIntervals);
375     if (isLogarithmic && funValues.front() > 1e-07 && funValues.back() > 1e-07) {
376       double logmin = log10(funValues.front());
377       double lval = logmin + r * (log10(funValues.back()) - logmin);
378       funValues[i+1] = pow(10.0, lval);
379     }
380     else {
381       funValues[i+1] = funValues.front() * (1-r) + funValues.back() * r;
382     }
383
384     // count values in the i-th interval if there are any
385     if ( min != values.end() && *min <= funValues[i+1] )
386     {
387       // find the first value out of the interval
388       max = values.upper_bound( funValues[i+1] ); // max is greater than funValues[i+1], or end()
389       nbEvents[i] = std::distance( min, max );
390       min = max;
391     }
392   }
393   // add values larger than minmax[1]
394   nbEvents.back() += std::distance( min, values.end() );
395 }
396
397 //=======================================================================
398 /*
399   Class       : Volume
400   Description : Functor calculating volume of a 3D element
401 */
402 //================================================================================
403
404 double Volume::GetValue( long theElementId )
405 {
406   if ( theElementId && myMesh ) {
407     SMDS_VolumeTool aVolumeTool;
408     if ( aVolumeTool.Set( myMesh->FindElement( theElementId )))
409       return aVolumeTool.GetSize();
410   }
411   return 0;
412 }
413
414 double Volume::GetBadRate( double Value, int /*nbNodes*/ ) const
415 {
416   return Value;
417 }
418
419 SMDSAbs_ElementType Volume::GetType() const
420 {
421   return SMDSAbs_Volume;
422 }
423
424 //=======================================================================
425 /*
426   Class       : MaxElementLength2D
427   Description : Functor calculating maximum length of 2D element
428 */
429 //================================================================================
430
431 double MaxElementLength2D::GetValue( const TSequenceOfXYZ& P )
432 {
433   if(P.size() == 0)
434     return 0.;
435   double aVal = 0;
436   int len = P.size();
437   if( len == 3 ) { // triangles
438     double L1 = getDistance(P( 1 ),P( 2 ));
439     double L2 = getDistance(P( 2 ),P( 3 ));
440     double L3 = getDistance(P( 3 ),P( 1 ));
441     aVal = Max(L1,Max(L2,L3));
442   }
443   else if( len == 4 ) { // quadrangles
444     double L1 = getDistance(P( 1 ),P( 2 ));
445     double L2 = getDistance(P( 2 ),P( 3 ));
446     double L3 = getDistance(P( 3 ),P( 4 ));
447     double L4 = getDistance(P( 4 ),P( 1 ));
448     double D1 = getDistance(P( 1 ),P( 3 ));
449     double D2 = getDistance(P( 2 ),P( 4 ));
450     aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(D1,D2));
451   }
452   else if( len == 6 ) { // quadratic triangles
453     double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
454     double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
455     double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
456     aVal = Max(L1,Max(L2,L3));
457   }
458   else if( len == 8 || len == 9 ) { // quadratic quadrangles
459     double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
460     double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
461     double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
462     double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
463     double D1 = getDistance(P( 1 ),P( 5 ));
464     double D2 = getDistance(P( 3 ),P( 7 ));
465     aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(D1,D2));
466   }
467   // Diagonals are undefined for concave polygons
468   // else if ( P.getElementEntity() == SMDSEntity_Quad_Polygon && P.size() > 2 ) // quad polygon
469   // {
470   //   // sides
471   //   aVal = getDistance( P( 1 ), P( P.size() )) + getDistance( P( P.size() ), P( P.size()-1 ));
472   //   for ( size_t i = 1; i < P.size()-1; i += 2 )
473   //   {
474   //     double L = getDistance( P( i ), P( i+1 )) + getDistance( P( i+1 ), P( i+2 ));
475   //     aVal = Max( aVal, L );
476   //   }
477   //   // diagonals
478   //   for ( int i = P.size()-5; i > 0; i -= 2 )
479   //     for ( int j = i + 4; j < P.size() + i - 2; i += 2 )
480   //     {
481   //       double D = getDistance( P( i ), P( j ));
482   //       aVal = Max( aVal, D );
483   //     }
484   // }
485   // { // polygons
486     
487   // }
488
489   if( myPrecision >= 0 )
490   {
491     double prec = pow( 10., (double)myPrecision );
492     aVal = floor( aVal * prec + 0.5 ) / prec;
493   }
494   return aVal;
495 }
496
497 double MaxElementLength2D::GetValue( long theElementId )
498 {
499   TSequenceOfXYZ P;
500   return GetPoints( theElementId, P ) ? GetValue(P) : 0.0;
501 }
502
503 double MaxElementLength2D::GetBadRate( double Value, int /*nbNodes*/ ) const
504 {
505   return Value;
506 }
507
508 SMDSAbs_ElementType MaxElementLength2D::GetType() const
509 {
510   return SMDSAbs_Face;
511 }
512
513 //=======================================================================
514 /*
515   Class       : MaxElementLength3D
516   Description : Functor calculating maximum length of 3D element
517 */
518 //================================================================================
519
520 double MaxElementLength3D::GetValue( long theElementId )
521 {
522   TSequenceOfXYZ P;
523   if( GetPoints( theElementId, P ) ) {
524     double aVal = 0;
525     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
526     SMDSAbs_EntityType      aType = aElem->GetEntityType();
527     int len = P.size();
528     switch ( aType ) {
529     case SMDSEntity_Tetra: { // tetras
530       double L1 = getDistance(P( 1 ),P( 2 ));
531       double L2 = getDistance(P( 2 ),P( 3 ));
532       double L3 = getDistance(P( 3 ),P( 1 ));
533       double L4 = getDistance(P( 1 ),P( 4 ));
534       double L5 = getDistance(P( 2 ),P( 4 ));
535       double L6 = getDistance(P( 3 ),P( 4 ));
536       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
537       break;
538     }
539     case SMDSEntity_Pyramid: { // pyramids
540       double L1 = getDistance(P( 1 ),P( 2 ));
541       double L2 = getDistance(P( 2 ),P( 3 ));
542       double L3 = getDistance(P( 3 ),P( 4 ));
543       double L4 = getDistance(P( 4 ),P( 1 ));
544       double L5 = getDistance(P( 1 ),P( 5 ));
545       double L6 = getDistance(P( 2 ),P( 5 ));
546       double L7 = getDistance(P( 3 ),P( 5 ));
547       double L8 = getDistance(P( 4 ),P( 5 ));
548       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
549       aVal = Max(aVal,Max(L7,L8));
550       break;
551     }
552     case SMDSEntity_Penta: { // pentas
553       double L1 = getDistance(P( 1 ),P( 2 ));
554       double L2 = getDistance(P( 2 ),P( 3 ));
555       double L3 = getDistance(P( 3 ),P( 1 ));
556       double L4 = getDistance(P( 4 ),P( 5 ));
557       double L5 = getDistance(P( 5 ),P( 6 ));
558       double L6 = getDistance(P( 6 ),P( 4 ));
559       double L7 = getDistance(P( 1 ),P( 4 ));
560       double L8 = getDistance(P( 2 ),P( 5 ));
561       double L9 = getDistance(P( 3 ),P( 6 ));
562       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
563       aVal = Max(aVal,Max(Max(L7,L8),L9));
564       break;
565     }
566     case SMDSEntity_Hexa: { // hexas
567       double L1 = getDistance(P( 1 ),P( 2 ));
568       double L2 = getDistance(P( 2 ),P( 3 ));
569       double L3 = getDistance(P( 3 ),P( 4 ));
570       double L4 = getDistance(P( 4 ),P( 1 ));
571       double L5 = getDistance(P( 5 ),P( 6 ));
572       double L6 = getDistance(P( 6 ),P( 7 ));
573       double L7 = getDistance(P( 7 ),P( 8 ));
574       double L8 = getDistance(P( 8 ),P( 5 ));
575       double L9 = getDistance(P( 1 ),P( 5 ));
576       double L10= getDistance(P( 2 ),P( 6 ));
577       double L11= getDistance(P( 3 ),P( 7 ));
578       double L12= getDistance(P( 4 ),P( 8 ));
579       double D1 = getDistance(P( 1 ),P( 7 ));
580       double D2 = getDistance(P( 2 ),P( 8 ));
581       double D3 = getDistance(P( 3 ),P( 5 ));
582       double D4 = getDistance(P( 4 ),P( 6 ));
583       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
584       aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
585       aVal = Max(aVal,Max(L11,L12));
586       aVal = Max(aVal,Max(Max(D1,D2),Max(D3,D4)));
587       break;
588     }
589     case SMDSEntity_Hexagonal_Prism: { // hexagonal prism
590       for ( int i1 = 1; i1 < 12; ++i1 )
591         for ( int i2 = i1+1; i1 <= 12; ++i1 )
592           aVal = Max( aVal, getDistance(P( i1 ),P( i2 )));
593       break;
594     }
595     case SMDSEntity_Quad_Tetra: { // quadratic tetras
596       double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
597       double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
598       double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
599       double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
600       double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
601       double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
602       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
603       break;
604     }
605     case SMDSEntity_Quad_Pyramid: { // quadratic pyramids
606       double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
607       double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
608       double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
609       double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
610       double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
611       double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
612       double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
613       double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
614       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
615       aVal = Max(aVal,Max(L7,L8));
616       break;
617     }
618     case SMDSEntity_Quad_Penta: { // quadratic pentas
619       double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
620       double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
621       double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
622       double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
623       double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
624       double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
625       double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
626       double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
627       double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
628       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
629       aVal = Max(aVal,Max(Max(L7,L8),L9));
630       break;
631     }
632     case SMDSEntity_Quad_Hexa:
633     case SMDSEntity_TriQuad_Hexa: { // quadratic hexas
634       double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
635       double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
636       double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
637       double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
638       double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
639       double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
640       double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
641       double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
642       double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
643       double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
644       double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
645       double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
646       double D1 = getDistance(P( 1 ),P( 7 ));
647       double D2 = getDistance(P( 2 ),P( 8 ));
648       double D3 = getDistance(P( 3 ),P( 5 ));
649       double D4 = getDistance(P( 4 ),P( 6 ));
650       aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
651       aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
652       aVal = Max(aVal,Max(L11,L12));
653       aVal = Max(aVal,Max(Max(D1,D2),Max(D3,D4)));
654       break;
655     }
656     case SMDSEntity_Quad_Polyhedra:
657     case SMDSEntity_Polyhedra: { // polys
658       // get the maximum distance between all pairs of nodes
659       for( int i = 1; i <= len; i++ ) {
660         for( int j = 1; j <= len; j++ ) {
661           if( j > i ) { // optimization of the loop
662             double D = getDistance( P(i), P(j) );
663             aVal = Max( aVal, D );
664           }
665         }
666       }
667       break;
668     }
669     case SMDSEntity_Node:
670     case SMDSEntity_0D:
671     case SMDSEntity_Edge:
672     case SMDSEntity_Quad_Edge:
673     case SMDSEntity_Triangle:
674     case SMDSEntity_Quad_Triangle:
675     case SMDSEntity_BiQuad_Triangle:
676     case SMDSEntity_Quadrangle:
677     case SMDSEntity_Quad_Quadrangle:
678     case SMDSEntity_BiQuad_Quadrangle:
679     case SMDSEntity_Polygon:
680     case SMDSEntity_Quad_Polygon:
681     case SMDSEntity_Ball:
682     case SMDSEntity_Last: return 0;
683     } // switch ( aType )
684
685     if( myPrecision >= 0 )
686     {
687       double prec = pow( 10., (double)myPrecision );
688       aVal = floor( aVal * prec + 0.5 ) / prec;
689     }
690     return aVal;
691   }
692   return 0.;
693 }
694
695 double MaxElementLength3D::GetBadRate( double Value, int /*nbNodes*/ ) const
696 {
697   return Value;
698 }
699
700 SMDSAbs_ElementType MaxElementLength3D::GetType() const
701 {
702   return SMDSAbs_Volume;
703 }
704
705 //=======================================================================
706 /*
707   Class       : MinimumAngle
708   Description : Functor for calculation of minimum angle
709 */
710 //================================================================================
711
712 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
713 {
714   double aMin;
715
716   if (P.size() <3)
717     return 0.;
718
719   aMin = getAngle(P( P.size() ), P( 1 ), P( 2 ));
720   aMin = Min(aMin,getAngle(P( P.size()-1 ), P( P.size() ), P( 1 )));
721
722   for ( size_t i = 2; i < P.size(); i++ )
723   {
724     double A0 = getAngle( P( i-1 ), P( i ), P( i+1 ) );
725     aMin = Min(aMin,A0);
726   }
727
728   return aMin * 180.0 / M_PI;
729 }
730
731 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
732 {
733   //const double aBestAngle = PI / nbNodes;
734   const double aBestAngle = 180.0 - ( 360.0 / double(nbNodes) );
735   return ( fabs( aBestAngle - Value ));
736 }
737
738 SMDSAbs_ElementType MinimumAngle::GetType() const
739 {
740   return SMDSAbs_Face;
741 }
742
743
744 //================================================================================
745 /*
746   Class       : AspectRatio
747   Description : Functor for calculating aspect ratio
748 */
749 //================================================================================
750
751 double AspectRatio::GetValue( long theId )
752 {
753   double aVal = 0;
754   myCurrElement = myMesh->FindElement( theId );
755   if ( myCurrElement && myCurrElement->GetVtkType() == VTK_QUAD )
756   {
757     // issue 21723
758     vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myCurrElement->getMeshId()]->getGrid();
759     if ( vtkCell* avtkCell = grid->GetCell( myCurrElement->getVtkId() ))
760       aVal = Round( vtkMeshQuality::QuadAspectRatio( avtkCell ));
761   }
762   else
763   {
764     TSequenceOfXYZ P;
765     if ( GetPoints( myCurrElement, P ))
766       aVal = Round( GetValue( P ));
767   }
768   return aVal;
769 }
770
771 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
772 {
773   // According to "Mesh quality control" by Nadir Bouhamau referring to
774   // Pascal Jean Frey and Paul-Louis George. Maillages, applications aux elements finis.
775   // Hermes Science publications, Paris 1999 ISBN 2-7462-0024-4
776   // PAL10872
777
778   int nbNodes = P.size();
779
780   if ( nbNodes < 3 )
781     return 0;
782
783   // Compute aspect ratio
784
785   if ( nbNodes == 3 ) {
786     // Compute lengths of the sides
787     std::vector< double > aLen (nbNodes);
788     for ( int i = 0; i < nbNodes - 1; i++ )
789       aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
790     aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
791     // Q = alfa * h * p / S, where
792     //
793     // alfa = sqrt( 3 ) / 6
794     // h - length of the longest edge
795     // p - half perimeter
796     // S - triangle surface
797     const double alfa = sqrt( 3. ) / 6.;
798     double maxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
799     double half_perimeter = ( aLen[0] + aLen[1] + aLen[2] ) / 2.;
800     double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
801     if ( anArea <= theEps  )
802       return theInf;
803     return alfa * maxLen * half_perimeter / anArea;
804   }
805   else if ( nbNodes == 6 ) { // quadratic triangles
806     // Compute lengths of the sides
807     std::vector< double > aLen (3);
808     aLen[0] = getDistance( P(1), P(3) );
809     aLen[1] = getDistance( P(3), P(5) );
810     aLen[2] = getDistance( P(5), P(1) );
811     // Q = alfa * h * p / S, where
812     //
813     // alfa = sqrt( 3 ) / 6
814     // h - length of the longest edge
815     // p - half perimeter
816     // S - triangle surface
817     const double alfa = sqrt( 3. ) / 6.;
818     double maxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
819     double half_perimeter = ( aLen[0] + aLen[1] + aLen[2] ) / 2.;
820     double anArea = getArea( P(1), P(3), P(5) );
821     if ( anArea <= theEps )
822       return theInf;
823     return alfa * maxLen * half_perimeter / anArea;
824   }
825   else if( nbNodes == 4 ) { // quadrangle
826     // Compute lengths of the sides
827     std::vector< double > aLen (4);
828     aLen[0] = getDistance( P(1), P(2) );
829     aLen[1] = getDistance( P(2), P(3) );
830     aLen[2] = getDistance( P(3), P(4) );
831     aLen[3] = getDistance( P(4), P(1) );
832     // Compute lengths of the diagonals
833     std::vector< double > aDia (2);
834     aDia[0] = getDistance( P(1), P(3) );
835     aDia[1] = getDistance( P(2), P(4) );
836     // Compute areas of all triangles which can be built
837     // taking three nodes of the quadrangle
838     std::vector< double > anArea (4);
839     anArea[0] = getArea( P(1), P(2), P(3) );
840     anArea[1] = getArea( P(1), P(2), P(4) );
841     anArea[2] = getArea( P(1), P(3), P(4) );
842     anArea[3] = getArea( P(2), P(3), P(4) );
843     // Q = alpha * L * C1 / C2, where
844     //
845     // alpha = sqrt( 1/32 )
846     // L = max( L1, L2, L3, L4, D1, D2 )
847     // C1 = sqrt( ( L1^2 + L1^2 + L1^2 + L1^2 ) / 4 )
848     // C2 = min( S1, S2, S3, S4 )
849     // Li - lengths of the edges
850     // Di - lengths of the diagonals
851     // Si - areas of the triangles
852     const double alpha = sqrt( 1 / 32. );
853     double L = Max( aLen[ 0 ],
854                  Max( aLen[ 1 ],
855                    Max( aLen[ 2 ],
856                      Max( aLen[ 3 ],
857                        Max( aDia[ 0 ], aDia[ 1 ] ) ) ) ) );
858     double C1 = sqrt( ( aLen[0] * aLen[0] +
859                         aLen[1] * aLen[1] +
860                         aLen[2] * aLen[2] +
861                         aLen[3] * aLen[3] ) / 4. );
862     double C2 = Min( anArea[ 0 ],
863                   Min( anArea[ 1 ],
864                     Min( anArea[ 2 ], anArea[ 3 ] ) ) );
865     if ( C2 <= theEps )
866       return theInf;
867     return alpha * L * C1 / C2;
868   }
869   else if( nbNodes == 8 || nbNodes == 9 ) { // nbNodes==8 - quadratic quadrangle
870     // Compute lengths of the sides
871     std::vector< double > aLen (4);
872     aLen[0] = getDistance( P(1), P(3) );
873     aLen[1] = getDistance( P(3), P(5) );
874     aLen[2] = getDistance( P(5), P(7) );
875     aLen[3] = getDistance( P(7), P(1) );
876     // Compute lengths of the diagonals
877     std::vector< double > aDia (2);
878     aDia[0] = getDistance( P(1), P(5) );
879     aDia[1] = getDistance( P(3), P(7) );
880     // Compute areas of all triangles which can be built
881     // taking three nodes of the quadrangle
882     std::vector< double > anArea (4);
883     anArea[0] = getArea( P(1), P(3), P(5) );
884     anArea[1] = getArea( P(1), P(3), P(7) );
885     anArea[2] = getArea( P(1), P(5), P(7) );
886     anArea[3] = getArea( P(3), P(5), P(7) );
887     // Q = alpha * L * C1 / C2, where
888     //
889     // alpha = sqrt( 1/32 )
890     // L = max( L1, L2, L3, L4, D1, D2 )
891     // C1 = sqrt( ( L1^2 + L1^2 + L1^2 + L1^2 ) / 4 )
892     // C2 = min( S1, S2, S3, S4 )
893     // Li - lengths of the edges
894     // Di - lengths of the diagonals
895     // Si - areas of the triangles
896     const double alpha = sqrt( 1 / 32. );
897     double L = Max( aLen[ 0 ],
898                  Max( aLen[ 1 ],
899                    Max( aLen[ 2 ],
900                      Max( aLen[ 3 ],
901                        Max( aDia[ 0 ], aDia[ 1 ] ) ) ) ) );
902     double C1 = sqrt( ( aLen[0] * aLen[0] +
903                         aLen[1] * aLen[1] +
904                         aLen[2] * aLen[2] +
905                         aLen[3] * aLen[3] ) / 4. );
906     double C2 = Min( anArea[ 0 ],
907                   Min( anArea[ 1 ],
908                     Min( anArea[ 2 ], anArea[ 3 ] ) ) );
909     if ( C2 <= theEps )
910       return theInf;
911     return alpha * L * C1 / C2;
912   }
913   return 0;
914 }
915
916 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
917 {
918   // the aspect ratio is in the range [1.0,infinity]
919   // < 1.0 = very bad, zero area
920   // 1.0 = good
921   // infinity = bad
922   return ( Value < 0.9 ) ? 1000 : Value / 1000.;
923 }
924
925 SMDSAbs_ElementType AspectRatio::GetType() const
926 {
927   return SMDSAbs_Face;
928 }
929
930
931 //================================================================================
932 /*
933   Class       : AspectRatio3D
934   Description : Functor for calculating aspect ratio
935 */
936 //================================================================================
937
938 namespace{
939
940   inline double getHalfPerimeter(double theTria[3]){
941     return (theTria[0] + theTria[1] + theTria[2])/2.0;
942   }
943
944   inline double getArea(double theHalfPerim, double theTria[3]){
945     return sqrt(theHalfPerim*
946                 (theHalfPerim-theTria[0])*
947                 (theHalfPerim-theTria[1])*
948                 (theHalfPerim-theTria[2]));
949   }
950
951   inline double getVolume(double theLen[6]){
952     double a2 = theLen[0]*theLen[0];
953     double b2 = theLen[1]*theLen[1];
954     double c2 = theLen[2]*theLen[2];
955     double d2 = theLen[3]*theLen[3];
956     double e2 = theLen[4]*theLen[4];
957     double f2 = theLen[5]*theLen[5];
958     double P = 4.0*a2*b2*d2;
959     double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
960     double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
961     return sqrt(P-Q+R)/12.0;
962   }
963
964   inline double getVolume2(double theLen[6]){
965     double a2 = theLen[0]*theLen[0];
966     double b2 = theLen[1]*theLen[1];
967     double c2 = theLen[2]*theLen[2];
968     double d2 = theLen[3]*theLen[3];
969     double e2 = theLen[4]*theLen[4];
970     double f2 = theLen[5]*theLen[5];
971
972     double P = a2*e2*(b2+c2+d2+f2-a2-e2);
973     double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
974     double R = c2*d2*(a2+b2+e2+f2-c2-d2);
975     double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
976
977     return sqrt(P+Q+R-S)/12.0;
978   }
979
980   inline double getVolume(const TSequenceOfXYZ& P){
981     gp_Vec aVec1( P( 2 ) - P( 1 ) );
982     gp_Vec aVec2( P( 3 ) - P( 1 ) );
983     gp_Vec aVec3( P( 4 ) - P( 1 ) );
984     gp_Vec anAreaVec( aVec1 ^ aVec2 );
985     return fabs(aVec3 * anAreaVec) / 6.0;
986   }
987
988   inline double getMaxHeight(double theLen[6])
989   {
990     double aHeight = std::max(theLen[0],theLen[1]);
991     aHeight = std::max(aHeight,theLen[2]);
992     aHeight = std::max(aHeight,theLen[3]);
993     aHeight = std::max(aHeight,theLen[4]);
994     aHeight = std::max(aHeight,theLen[5]);
995     return aHeight;
996   }
997
998 }
999
1000 double AspectRatio3D::GetValue( long theId )
1001 {
1002   double aVal = 0;
1003   myCurrElement = myMesh->FindElement( theId );
1004   if ( myCurrElement && myCurrElement->GetVtkType() == VTK_TETRA )
1005   {
1006     // Action from CoTech | ACTION 31.3:
1007     // EURIWARE BO: Homogenize the formulas used to calculate the Controls in SMESH to fit with
1008     // those of ParaView. The library used by ParaView for those calculations can be reused in SMESH.
1009     vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myCurrElement->getMeshId()]->getGrid();
1010     if ( vtkCell* avtkCell = grid->GetCell( myCurrElement->getVtkId() ))
1011       aVal = Round( vtkMeshQuality::TetAspectRatio( avtkCell ));
1012   }
1013   else
1014   {
1015     TSequenceOfXYZ P;
1016     if ( GetPoints( myCurrElement, P ))
1017       aVal = Round( GetValue( P ));
1018   }
1019   return aVal;
1020 }
1021
1022 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
1023 {
1024   double aQuality = 0.0;
1025   if(myCurrElement->IsPoly()) return aQuality;
1026
1027   int nbNodes = P.size();
1028
1029   if(myCurrElement->IsQuadratic()) {
1030     if(nbNodes==10) nbNodes=4; // quadratic tetrahedron
1031     else if(nbNodes==13) nbNodes=5; // quadratic pyramid
1032     else if(nbNodes==15) nbNodes=6; // quadratic pentahedron
1033     else if(nbNodes==20) nbNodes=8; // quadratic hexahedron
1034     else if(nbNodes==27) nbNodes=8; // quadratic hexahedron
1035     else return aQuality;
1036   }
1037
1038   switch(nbNodes) {
1039   case 4:{
1040     double aLen[6] = {
1041       getDistance(P( 1 ),P( 2 )), // a
1042       getDistance(P( 2 ),P( 3 )), // b
1043       getDistance(P( 3 ),P( 1 )), // c
1044       getDistance(P( 2 ),P( 4 )), // d
1045       getDistance(P( 3 ),P( 4 )), // e
1046       getDistance(P( 1 ),P( 4 ))  // f
1047     };
1048     double aTria[4][3] = {
1049       {aLen[0],aLen[1],aLen[2]}, // abc
1050       {aLen[0],aLen[3],aLen[5]}, // adf
1051       {aLen[1],aLen[3],aLen[4]}, // bde
1052       {aLen[2],aLen[4],aLen[5]}  // cef
1053     };
1054     double aSumArea = 0.0;
1055     double aHalfPerimeter = getHalfPerimeter(aTria[0]);
1056     double anArea = getArea(aHalfPerimeter,aTria[0]);
1057     aSumArea += anArea;
1058     aHalfPerimeter = getHalfPerimeter(aTria[1]);
1059     anArea = getArea(aHalfPerimeter,aTria[1]);
1060     aSumArea += anArea;
1061     aHalfPerimeter = getHalfPerimeter(aTria[2]);
1062     anArea = getArea(aHalfPerimeter,aTria[2]);
1063     aSumArea += anArea;
1064     aHalfPerimeter = getHalfPerimeter(aTria[3]);
1065     anArea = getArea(aHalfPerimeter,aTria[3]);
1066     aSumArea += anArea;
1067     double aVolume = getVolume(P);
1068     //double aVolume = getVolume(aLen);
1069     double aHeight = getMaxHeight(aLen);
1070     static double aCoeff = sqrt(2.0)/12.0;
1071     if ( aVolume > DBL_MIN )
1072       aQuality = aCoeff*aHeight*aSumArea/aVolume;
1073     break;
1074   }
1075   case 5:{
1076     {
1077       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
1078       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1079     }
1080     {
1081       gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
1082       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1083     }
1084     {
1085       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
1086       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1087     }
1088     {
1089       gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
1090       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1091     }
1092     break;
1093   }
1094   case 6:{
1095     {
1096       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
1097       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1098     }
1099     {
1100       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
1101       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1102     }
1103     {
1104       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
1105       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1106     }
1107     {
1108       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
1109       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1110     }
1111     {
1112       gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
1113       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1114     }
1115     {
1116       gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
1117       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1118     }
1119     break;
1120   }
1121   case 8:{
1122     {
1123       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
1124       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1125     }
1126     {
1127       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
1128       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1129     }
1130     {
1131       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
1132       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1133     }
1134     {
1135       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
1136       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1137     }
1138     {
1139       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
1140       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1141     }
1142     {
1143       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
1144       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1145     }
1146     {
1147       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
1148       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1149     }
1150     {
1151       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
1152       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1153     }
1154     {
1155       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
1156       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1157     }
1158     {
1159       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
1160       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1161     }
1162     {
1163       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
1164       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1165     }
1166     {
1167       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
1168       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1169     }
1170     {
1171       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
1172       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1173     }
1174     {
1175       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
1176       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1177     }
1178     {
1179       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
1180       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1181     }
1182     {
1183       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
1184       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1185     }
1186     {
1187       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
1188       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1189     }
1190     {
1191       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
1192       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1193     }
1194     {
1195       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
1196       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1197     }
1198     {
1199       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
1200       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1201     }
1202     {
1203       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
1204       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1205     }
1206     {
1207       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
1208       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1209     }
1210     {
1211       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
1212       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1213     }
1214     {
1215       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
1216       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1217     }
1218     {
1219       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
1220       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1221     }
1222     {
1223       gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
1224       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1225     }
1226     {
1227       gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
1228       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1229     }
1230     {
1231       gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
1232       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1233     }
1234     {
1235       gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
1236       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1237     }
1238     {
1239       gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
1240       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1241     }
1242     {
1243       gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
1244       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1245     }
1246     {
1247       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
1248       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1249     }
1250     {
1251       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
1252       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1253     }
1254     break;
1255   }
1256   case 12:
1257     {
1258       gp_XYZ aXYZ[8] = {P( 1 ),P( 2 ),P( 4 ),P( 5 ),P( 7 ),P( 8 ),P( 10 ),P( 11 )};
1259       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[8])),aQuality);
1260     }
1261     {
1262       gp_XYZ aXYZ[8] = {P( 2 ),P( 3 ),P( 5 ),P( 6 ),P( 8 ),P( 9 ),P( 11 ),P( 12 )};
1263       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[8])),aQuality);
1264     }
1265     {
1266       gp_XYZ aXYZ[8] = {P( 3 ),P( 4 ),P( 6 ),P( 1 ),P( 9 ),P( 10 ),P( 12 ),P( 7 )};
1267       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[8])),aQuality);
1268     }
1269     break;
1270   } // switch(nbNodes)
1271
1272   if ( nbNodes > 4 ) {
1273     // avaluate aspect ratio of quadranle faces
1274     AspectRatio aspect2D;
1275     SMDS_VolumeTool::VolumeType type = SMDS_VolumeTool::GetType( nbNodes );
1276     int nbFaces = SMDS_VolumeTool::NbFaces( type );
1277     TSequenceOfXYZ points(4);
1278     for ( int i = 0; i < nbFaces; ++i ) { // loop on faces of a volume
1279       if ( SMDS_VolumeTool::NbFaceNodes( type, i ) != 4 )
1280         continue;
1281       const int* pInd = SMDS_VolumeTool::GetFaceNodesIndices( type, i, true );
1282       for ( int p = 0; p < 4; ++p ) // loop on nodes of a quadranle face
1283         points( p + 1 ) = P( pInd[ p ] + 1 );
1284       aQuality = std::max( aQuality, aspect2D.GetValue( points ));
1285     }
1286   }
1287   return aQuality;
1288 }
1289
1290 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
1291 {
1292   // the aspect ratio is in the range [1.0,infinity]
1293   // 1.0 = good
1294   // infinity = bad
1295   return Value / 1000.;
1296 }
1297
1298 SMDSAbs_ElementType AspectRatio3D::GetType() const
1299 {
1300   return SMDSAbs_Volume;
1301 }
1302
1303
1304 //================================================================================
1305 /*
1306   Class       : Warping
1307   Description : Functor for calculating warping
1308 */
1309 //================================================================================
1310
1311 double Warping::GetValue( const TSequenceOfXYZ& P )
1312 {
1313   if ( P.size() != 4 )
1314     return 0;
1315
1316   gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4.;
1317
1318   double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
1319   double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
1320   double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
1321   double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
1322
1323   double val = Max( Max( A1, A2 ), Max( A3, A4 ) );
1324
1325   const double eps = 0.1; // val is in degrees
1326
1327   return val < eps ? 0. : val;
1328 }
1329
1330 double Warping::ComputeA( const gp_XYZ& thePnt1,
1331                           const gp_XYZ& thePnt2,
1332                           const gp_XYZ& thePnt3,
1333                           const gp_XYZ& theG ) const
1334 {
1335   double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
1336   double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
1337   double L = Min( aLen1, aLen2 ) * 0.5;
1338   if ( L < theEps )
1339     return theInf;
1340
1341   gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
1342   gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
1343   gp_XYZ N  = GI.Crossed( GJ );
1344
1345   if ( N.Modulus() < gp::Resolution() )
1346     return M_PI / 2;
1347
1348   N.Normalize();
1349
1350   double H = ( thePnt2 - theG ).Dot( N );
1351   return asin( fabs( H / L ) ) * 180. / M_PI;
1352 }
1353
1354 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
1355 {
1356   // the warp is in the range [0.0,PI/2]
1357   // 0.0 = good (no warp)
1358   // PI/2 = bad  (face pliee)
1359   return Value;
1360 }
1361
1362 SMDSAbs_ElementType Warping::GetType() const
1363 {
1364   return SMDSAbs_Face;
1365 }
1366
1367
1368 //================================================================================
1369 /*
1370   Class       : Taper
1371   Description : Functor for calculating taper
1372 */
1373 //================================================================================
1374
1375 double Taper::GetValue( const TSequenceOfXYZ& P )
1376 {
1377   if ( P.size() != 4 )
1378     return 0.;
1379
1380   // Compute taper
1381   double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) );
1382   double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) );
1383   double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) );
1384   double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) );
1385
1386   double JA = 0.25 * ( J1 + J2 + J3 + J4 );
1387   if ( JA <= theEps )
1388     return theInf;
1389
1390   double T1 = fabs( ( J1 - JA ) / JA );
1391   double T2 = fabs( ( J2 - JA ) / JA );
1392   double T3 = fabs( ( J3 - JA ) / JA );
1393   double T4 = fabs( ( J4 - JA ) / JA );
1394
1395   double val = Max( Max( T1, T2 ), Max( T3, T4 ) );
1396
1397   const double eps = 0.01;
1398
1399   return val < eps ? 0. : val;
1400 }
1401
1402 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
1403 {
1404   // the taper is in the range [0.0,1.0]
1405   // 0.0 = good (no taper)
1406   // 1.0 = bad  (les cotes opposes sont allignes)
1407   return Value;
1408 }
1409
1410 SMDSAbs_ElementType Taper::GetType() const
1411 {
1412   return SMDSAbs_Face;
1413 }
1414
1415 //================================================================================
1416 /*
1417   Class       : Skew
1418   Description : Functor for calculating skew in degrees
1419 */
1420 //================================================================================
1421
1422 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
1423 {
1424   gp_XYZ p12 = ( p2 + p1 ) / 2.;
1425   gp_XYZ p23 = ( p3 + p2 ) / 2.;
1426   gp_XYZ p31 = ( p3 + p1 ) / 2.;
1427
1428   gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
1429
1430   return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0. : v1.Angle( v2 );
1431 }
1432
1433 double Skew::GetValue( const TSequenceOfXYZ& P )
1434 {
1435   if ( P.size() != 3 && P.size() != 4 )
1436     return 0.;
1437
1438   // Compute skew
1439   const double PI2 = M_PI / 2.;
1440   if ( P.size() == 3 )
1441   {
1442     double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
1443     double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
1444     double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
1445
1446     return Max( A0, Max( A1, A2 ) ) * 180. / M_PI;
1447   }
1448   else
1449   {
1450     gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2.;
1451     gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2.;
1452     gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2.;
1453     gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2.;
1454
1455     gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
1456     double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
1457       ? 0. : fabs( PI2 - v1.Angle( v2 ) );
1458
1459     double val = A * 180. / M_PI;
1460
1461     const double eps = 0.1; // val is in degrees
1462
1463     return val < eps ? 0. : val;
1464   }
1465 }
1466
1467 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
1468 {
1469   // the skew is in the range [0.0,PI/2].
1470   // 0.0 = good
1471   // PI/2 = bad
1472   return Value;
1473 }
1474
1475 SMDSAbs_ElementType Skew::GetType() const
1476 {
1477   return SMDSAbs_Face;
1478 }
1479
1480
1481 //================================================================================
1482 /*
1483   Class       : Area
1484   Description : Functor for calculating area
1485 */
1486 //================================================================================
1487
1488 double Area::GetValue( const TSequenceOfXYZ& P )
1489 {
1490   double val = 0.0;
1491   if ( P.size() > 2 )
1492   {
1493     gp_Vec aVec1( P(2) - P(1) );
1494     gp_Vec aVec2( P(3) - P(1) );
1495     gp_Vec SumVec = aVec1 ^ aVec2;
1496
1497     for (size_t i=4; i<=P.size(); i++)
1498     {
1499       gp_Vec aVec1( P(i-1) - P(1) );
1500       gp_Vec aVec2( P(i  ) - P(1) );
1501       gp_Vec tmp = aVec1 ^ aVec2;
1502       SumVec.Add(tmp);
1503     }
1504     val = SumVec.Magnitude() * 0.5;
1505   }
1506   return val;
1507 }
1508
1509 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
1510 {
1511   // meaningless as it is not a quality control functor
1512   return Value;
1513 }
1514
1515 SMDSAbs_ElementType Area::GetType() const
1516 {
1517   return SMDSAbs_Face;
1518 }
1519
1520 //================================================================================
1521 /*
1522   Class       : Length
1523   Description : Functor for calculating length of edge
1524 */
1525 //================================================================================
1526
1527 double Length::GetValue( const TSequenceOfXYZ& P )
1528 {
1529   switch ( P.size() ) {
1530   case 2:  return getDistance( P( 1 ), P( 2 ) );
1531   case 3:  return getDistance( P( 1 ), P( 2 ) ) + getDistance( P( 2 ), P( 3 ) );
1532   default: return 0.;
1533   }
1534 }
1535
1536 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
1537 {
1538   // meaningless as it is not quality control functor
1539   return Value;
1540 }
1541
1542 SMDSAbs_ElementType Length::GetType() const
1543 {
1544   return SMDSAbs_Edge;
1545 }
1546
1547 //================================================================================
1548 /*
1549   Class       : Length2D
1550   Description : Functor for calculating minimal length of edge
1551 */
1552 //================================================================================
1553
1554 double Length2D::GetValue( long theElementId )
1555 {
1556   TSequenceOfXYZ P;
1557
1558   if ( GetPoints( theElementId, P ))
1559   {
1560     double aVal = 0;
1561     int len = P.size();
1562     SMDSAbs_EntityType aType = P.getElementEntity();
1563
1564     switch (aType) {
1565     case SMDSEntity_Edge:
1566       if (len == 2)
1567         aVal = getDistance( P( 1 ), P( 2 ) );
1568       break;
1569     case SMDSEntity_Quad_Edge:
1570       if (len == 3) // quadratic edge
1571         aVal = getDistance(P( 1 ),P( 3 )) + getDistance(P( 3 ),P( 2 ));
1572       break;
1573     case SMDSEntity_Triangle:
1574       if (len == 3){ // triangles
1575         double L1 = getDistance(P( 1 ),P( 2 ));
1576         double L2 = getDistance(P( 2 ),P( 3 ));
1577         double L3 = getDistance(P( 3 ),P( 1 ));
1578         aVal = Min(L1,Min(L2,L3));
1579       }
1580       break;
1581     case SMDSEntity_Quadrangle:
1582       if (len == 4){ // quadrangles
1583         double L1 = getDistance(P( 1 ),P( 2 ));
1584         double L2 = getDistance(P( 2 ),P( 3 ));
1585         double L3 = getDistance(P( 3 ),P( 4 ));
1586         double L4 = getDistance(P( 4 ),P( 1 ));
1587         aVal = Min(Min(L1,L2),Min(L3,L4));
1588       }
1589       break;
1590     case SMDSEntity_Quad_Triangle:
1591     case SMDSEntity_BiQuad_Triangle:
1592       if (len >= 6){ // quadratic triangles
1593         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1594         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1595         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
1596         aVal = Min(L1,Min(L2,L3));
1597       }
1598       break;
1599     case SMDSEntity_Quad_Quadrangle:
1600     case SMDSEntity_BiQuad_Quadrangle:
1601       if (len >= 8){ // quadratic quadrangles
1602         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1603         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1604         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
1605         double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1606         aVal = Min(Min(L1,L2),Min(L3,L4));
1607       }
1608       break;
1609     case SMDSEntity_Tetra:
1610       if (len == 4){ // tetrahedra
1611         double L1 = getDistance(P( 1 ),P( 2 ));
1612         double L2 = getDistance(P( 2 ),P( 3 ));
1613         double L3 = getDistance(P( 3 ),P( 1 ));
1614         double L4 = getDistance(P( 1 ),P( 4 ));
1615         double L5 = getDistance(P( 2 ),P( 4 ));
1616         double L6 = getDistance(P( 3 ),P( 4 ));
1617         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1618       }
1619       break;
1620     case SMDSEntity_Pyramid:
1621       if (len == 5){ // piramids
1622         double L1 = getDistance(P( 1 ),P( 2 ));
1623         double L2 = getDistance(P( 2 ),P( 3 ));
1624         double L3 = getDistance(P( 3 ),P( 4 ));
1625         double L4 = getDistance(P( 4 ),P( 1 ));
1626         double L5 = getDistance(P( 1 ),P( 5 ));
1627         double L6 = getDistance(P( 2 ),P( 5 ));
1628         double L7 = getDistance(P( 3 ),P( 5 ));
1629         double L8 = getDistance(P( 4 ),P( 5 ));
1630
1631         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1632         aVal = Min(aVal,Min(L7,L8));
1633       }
1634       break;
1635     case SMDSEntity_Penta:
1636       if (len == 6) { // pentaidres
1637         double L1 = getDistance(P( 1 ),P( 2 ));
1638         double L2 = getDistance(P( 2 ),P( 3 ));
1639         double L3 = getDistance(P( 3 ),P( 1 ));
1640         double L4 = getDistance(P( 4 ),P( 5 ));
1641         double L5 = getDistance(P( 5 ),P( 6 ));
1642         double L6 = getDistance(P( 6 ),P( 4 ));
1643         double L7 = getDistance(P( 1 ),P( 4 ));
1644         double L8 = getDistance(P( 2 ),P( 5 ));
1645         double L9 = getDistance(P( 3 ),P( 6 ));
1646
1647         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1648         aVal = Min(aVal,Min(Min(L7,L8),L9));
1649       }
1650       break;
1651     case SMDSEntity_Hexa:
1652       if (len == 8){ // hexahedron
1653         double L1 = getDistance(P( 1 ),P( 2 ));
1654         double L2 = getDistance(P( 2 ),P( 3 ));
1655         double L3 = getDistance(P( 3 ),P( 4 ));
1656         double L4 = getDistance(P( 4 ),P( 1 ));
1657         double L5 = getDistance(P( 5 ),P( 6 ));
1658         double L6 = getDistance(P( 6 ),P( 7 ));
1659         double L7 = getDistance(P( 7 ),P( 8 ));
1660         double L8 = getDistance(P( 8 ),P( 5 ));
1661         double L9 = getDistance(P( 1 ),P( 5 ));
1662         double L10= getDistance(P( 2 ),P( 6 ));
1663         double L11= getDistance(P( 3 ),P( 7 ));
1664         double L12= getDistance(P( 4 ),P( 8 ));
1665
1666         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1667         aVal = Min(aVal,Min(Min(L7,L8),Min(L9,L10)));
1668         aVal = Min(aVal,Min(L11,L12));
1669       }
1670       break;
1671     case SMDSEntity_Quad_Tetra:
1672       if (len == 10){ // quadratic tetraidrs
1673         double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
1674         double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
1675         double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
1676         double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1677         double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
1678         double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
1679         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1680       }
1681       break;
1682     case SMDSEntity_Quad_Pyramid:
1683       if (len == 13){ // quadratic piramids
1684         double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
1685         double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
1686         double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1687         double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1688         double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1689         double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
1690         double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
1691         double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
1692         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1693         aVal = Min(aVal,Min(L7,L8));
1694       }
1695       break;
1696     case SMDSEntity_Quad_Penta:
1697       if (len == 15){ // quadratic pentaidres
1698         double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
1699         double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
1700         double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1701         double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1702         double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
1703         double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
1704         double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
1705         double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
1706         double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
1707         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1708         aVal = Min(aVal,Min(Min(L7,L8),L9));
1709       }
1710       break;
1711     case SMDSEntity_Quad_Hexa:
1712     case SMDSEntity_TriQuad_Hexa:
1713       if (len >= 20) { // quadratic hexaider
1714         double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
1715         double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
1716         double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
1717         double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
1718         double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
1719         double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
1720         double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
1721         double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
1722         double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
1723         double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
1724         double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
1725         double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
1726         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1727         aVal = Min(aVal,Min(Min(L7,L8),Min(L9,L10)));
1728         aVal = Min(aVal,Min(L11,L12));
1729       }
1730       break;
1731     case SMDSEntity_Polygon:
1732       if ( len > 1 ) {
1733         aVal = getDistance( P(1), P( P.size() ));
1734         for ( size_t i = 1; i < P.size(); ++i )
1735           aVal = Min( aVal, getDistance( P( i ), P( i+1 )));
1736       }
1737       break;
1738     case SMDSEntity_Quad_Polygon:
1739       if ( len > 2 ) {
1740         aVal = getDistance( P(1), P( P.size() )) + getDistance( P(P.size()), P( P.size()-1 ));
1741         for ( size_t i = 1; i < P.size()-1; i += 2 )
1742           aVal = Min( aVal, getDistance( P( i ), P( i+1 )) + getDistance( P( i+1 ), P( i+2 )));
1743       }
1744       break;
1745     case SMDSEntity_Hexagonal_Prism:
1746       if (len == 12) { // hexagonal prism
1747         double L1 = getDistance(P( 1 ),P( 2 ));
1748         double L2 = getDistance(P( 2 ),P( 3 ));
1749         double L3 = getDistance(P( 3 ),P( 4 ));
1750         double L4 = getDistance(P( 4 ),P( 5 ));
1751         double L5 = getDistance(P( 5 ),P( 6 ));
1752         double L6 = getDistance(P( 6 ),P( 1 ));
1753
1754         double L7 = getDistance(P( 7 ), P( 8 ));
1755         double L8 = getDistance(P( 8 ), P( 9 ));
1756         double L9 = getDistance(P( 9 ), P( 10 ));
1757         double L10= getDistance(P( 10 ),P( 11 ));
1758         double L11= getDistance(P( 11 ),P( 12 ));
1759         double L12= getDistance(P( 12 ),P( 7 ));
1760
1761         double L13 = getDistance(P( 1 ),P( 7 ));
1762         double L14 = getDistance(P( 2 ),P( 8 ));
1763         double L15 = getDistance(P( 3 ),P( 9 ));
1764         double L16 = getDistance(P( 4 ),P( 10 ));
1765         double L17 = getDistance(P( 5 ),P( 11 ));
1766         double L18 = getDistance(P( 6 ),P( 12 ));
1767         aVal = Min(Min(Min(L1,L2),Min(L3,L4)),Min(L5,L6));
1768         aVal = Min(aVal, Min(Min(Min(L7,L8),Min(L9,L10)),Min(L11,L12)));
1769         aVal = Min(aVal, Min(Min(Min(L13,L14),Min(L15,L16)),Min(L17,L18)));
1770       }
1771       break;
1772     case SMDSEntity_Polyhedra:
1773     {
1774     }
1775     break;
1776     default:
1777       return 0;
1778     }
1779
1780     if (aVal < 0 ) {
1781       return 0.;
1782     }
1783
1784     if ( myPrecision >= 0 )
1785     {
1786       double prec = pow( 10., (double)( myPrecision ) );
1787       aVal = floor( aVal * prec + 0.5 ) / prec;
1788     }
1789
1790     return aVal;
1791
1792   }
1793   return 0.;
1794 }
1795
1796 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1797 {
1798   // meaningless as it is not a quality control functor
1799   return Value;
1800 }
1801
1802 SMDSAbs_ElementType Length2D::GetType() const
1803 {
1804   return SMDSAbs_Face;
1805 }
1806
1807 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
1808   myLength(theLength)
1809 {
1810   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1811   if(thePntId1 > thePntId2){
1812     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1813   }
1814 }
1815
1816 bool Length2D::Value::operator<(const Length2D::Value& x) const
1817 {
1818   if(myPntId[0] < x.myPntId[0]) return true;
1819   if(myPntId[0] == x.myPntId[0])
1820     if(myPntId[1] < x.myPntId[1]) return true;
1821   return false;
1822 }
1823
1824 void Length2D::GetValues(TValues& theValues)
1825 {
1826   TValues aValues;
1827   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1828   for(; anIter->more(); ){
1829     const SMDS_MeshFace* anElem = anIter->next();
1830
1831     if(anElem->IsQuadratic()) {
1832       const SMDS_VtkFace* F =
1833         dynamic_cast<const SMDS_VtkFace*>(anElem);
1834       // use special nodes iterator
1835       SMDS_ElemIteratorPtr anIter = F->interlacedNodesElemIterator();
1836       long aNodeId[4] = { 0,0,0,0 };
1837       gp_Pnt P[4];
1838
1839       double aLength = 0;
1840       const SMDS_MeshElement* aNode;
1841       if(anIter->more()){
1842         aNode = anIter->next();
1843         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1844         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1845         aNodeId[0] = aNodeId[1] = aNode->GetID();
1846         aLength = 0;
1847       }
1848       for(; anIter->more(); ){
1849         const SMDS_MeshNode* N1 = static_cast<const SMDS_MeshNode*> (anIter->next());
1850         P[2] = gp_Pnt(N1->X(),N1->Y(),N1->Z());
1851         aNodeId[2] = N1->GetID();
1852         aLength = P[1].Distance(P[2]);
1853         if(!anIter->more()) break;
1854         const SMDS_MeshNode* N2 = static_cast<const SMDS_MeshNode*> (anIter->next());
1855         P[3] = gp_Pnt(N2->X(),N2->Y(),N2->Z());
1856         aNodeId[3] = N2->GetID();
1857         aLength += P[2].Distance(P[3]);
1858         Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1859         Value aValue2(aLength,aNodeId[2],aNodeId[3]);
1860         P[1] = P[3];
1861         aNodeId[1] = aNodeId[3];
1862         theValues.insert(aValue1);
1863         theValues.insert(aValue2);
1864       }
1865       aLength += P[2].Distance(P[0]);
1866       Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1867       Value aValue2(aLength,aNodeId[2],aNodeId[0]);
1868       theValues.insert(aValue1);
1869       theValues.insert(aValue2);
1870     }
1871     else {
1872       SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1873       long aNodeId[2] = {0,0};
1874       gp_Pnt P[3];
1875
1876       double aLength;
1877       const SMDS_MeshElement* aNode;
1878       if(aNodesIter->more()){
1879         aNode = aNodesIter->next();
1880         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1881         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1882         aNodeId[0] = aNodeId[1] = aNode->GetID();
1883         aLength = 0;
1884       }
1885       for(; aNodesIter->more(); ){
1886         aNode = aNodesIter->next();
1887         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1888         long anId = aNode->GetID();
1889         
1890         P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1891         
1892         aLength = P[1].Distance(P[2]);
1893         
1894         Value aValue(aLength,aNodeId[1],anId);
1895         aNodeId[1] = anId;
1896         P[1] = P[2];
1897         theValues.insert(aValue);
1898       }
1899
1900       aLength = P[0].Distance(P[1]);
1901
1902       Value aValue(aLength,aNodeId[0],aNodeId[1]);
1903       theValues.insert(aValue);
1904     }
1905   }
1906 }
1907
1908 //================================================================================
1909 /*
1910   Class       : MultiConnection
1911   Description : Functor for calculating number of faces conneted to the edge
1912 */
1913 //================================================================================
1914
1915 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1916 {
1917   return 0;
1918 }
1919 double MultiConnection::GetValue( long theId )
1920 {
1921   return getNbMultiConnection( myMesh, theId );
1922 }
1923
1924 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1925 {
1926   // meaningless as it is not quality control functor
1927   return Value;
1928 }
1929
1930 SMDSAbs_ElementType MultiConnection::GetType() const
1931 {
1932   return SMDSAbs_Edge;
1933 }
1934
1935 //================================================================================
1936 /*
1937   Class       : MultiConnection2D
1938   Description : Functor for calculating number of faces conneted to the edge
1939 */
1940 //================================================================================
1941
1942 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1943 {
1944   return 0;
1945 }
1946
1947 double MultiConnection2D::GetValue( long theElementId )
1948 {
1949   int aResult = 0;
1950
1951   const SMDS_MeshElement* aFaceElem = myMesh->FindElement(theElementId);
1952   SMDSAbs_ElementType aType = aFaceElem->GetType();
1953
1954   switch (aType) {
1955   case SMDSAbs_Face:
1956     {
1957       int i = 0, len = aFaceElem->NbNodes();
1958       SMDS_ElemIteratorPtr anIter = aFaceElem->nodesIterator();
1959       if (!anIter) break;
1960
1961       const SMDS_MeshNode *aNode, *aNode0 = 0;
1962       TColStd_MapOfInteger aMap, aMapPrev;
1963
1964       for (i = 0; i <= len; i++) {
1965         aMapPrev = aMap;
1966         aMap.Clear();
1967
1968         int aNb = 0;
1969         if (anIter->more()) {
1970           aNode = (SMDS_MeshNode*)anIter->next();
1971         } else {
1972           if (i == len)
1973             aNode = aNode0;
1974           else
1975             break;
1976         }
1977         if (!aNode) break;
1978         if (i == 0) aNode0 = aNode;
1979
1980         SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1981         while (anElemIter->more()) {
1982           const SMDS_MeshElement* anElem = anElemIter->next();
1983           if (anElem != 0 && anElem->GetType() == SMDSAbs_Face) {
1984             int anId = anElem->GetID();
1985
1986             aMap.Add(anId);
1987             if (aMapPrev.Contains(anId)) {
1988               aNb++;
1989             }
1990           }
1991         }
1992         aResult = Max(aResult, aNb);
1993       }
1994     }
1995     break;
1996   default:
1997     aResult = 0;
1998   }
1999
2000   return aResult;
2001 }
2002
2003 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
2004 {
2005   // meaningless as it is not quality control functor
2006   return Value;
2007 }
2008
2009 SMDSAbs_ElementType MultiConnection2D::GetType() const
2010 {
2011   return SMDSAbs_Face;
2012 }
2013
2014 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
2015 {
2016   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
2017   if(thePntId1 > thePntId2){
2018     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
2019   }
2020 }
2021
2022 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const
2023 {
2024   if(myPntId[0] < x.myPntId[0]) return true;
2025   if(myPntId[0] == x.myPntId[0])
2026     if(myPntId[1] < x.myPntId[1]) return true;
2027   return false;
2028 }
2029
2030 void MultiConnection2D::GetValues(MValues& theValues)
2031 {
2032   if ( !myMesh ) return;
2033   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2034   for(; anIter->more(); ){
2035     const SMDS_MeshFace* anElem = anIter->next();
2036     SMDS_ElemIteratorPtr aNodesIter;
2037     if ( anElem->IsQuadratic() )
2038       aNodesIter = dynamic_cast<const SMDS_VtkFace*>
2039         (anElem)->interlacedNodesElemIterator();
2040     else
2041       aNodesIter = anElem->nodesIterator();
2042     long aNodeId[3] = {0,0,0};
2043
2044     //int aNbConnects=0;
2045     const SMDS_MeshNode* aNode0;
2046     const SMDS_MeshNode* aNode1;
2047     const SMDS_MeshNode* aNode2;
2048     if(aNodesIter->more()){
2049       aNode0 = (SMDS_MeshNode*) aNodesIter->next();
2050       aNode1 = aNode0;
2051       const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
2052       aNodeId[0] = aNodeId[1] = aNodes->GetID();
2053     }
2054     for(; aNodesIter->more(); ) {
2055       aNode2 = (SMDS_MeshNode*) aNodesIter->next();
2056       long anId = aNode2->GetID();
2057       aNodeId[2] = anId;
2058
2059       Value aValue(aNodeId[1],aNodeId[2]);
2060       MValues::iterator aItr = theValues.find(aValue);
2061       if (aItr != theValues.end()){
2062         aItr->second += 1;
2063         //aNbConnects = nb;
2064       }
2065       else {
2066         theValues[aValue] = 1;
2067         //aNbConnects = 1;
2068       }
2069       //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
2070       aNodeId[1] = aNodeId[2];
2071       aNode1 = aNode2;
2072     }
2073     Value aValue(aNodeId[0],aNodeId[2]);
2074     MValues::iterator aItr = theValues.find(aValue);
2075     if (aItr != theValues.end()) {
2076       aItr->second += 1;
2077       //aNbConnects = nb;
2078     }
2079     else {
2080       theValues[aValue] = 1;
2081       //aNbConnects = 1;
2082     }
2083     //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
2084   }
2085
2086 }
2087
2088 //================================================================================
2089 /*
2090   Class       : BallDiameter
2091   Description : Functor returning diameter of a ball element
2092 */
2093 //================================================================================
2094
2095 double BallDiameter::GetValue( long theId )
2096 {
2097   double diameter = 0;
2098
2099   if ( const SMDS_BallElement* ball =
2100        dynamic_cast<const SMDS_BallElement*>( myMesh->FindElement( theId )))
2101   {
2102     diameter = ball->GetDiameter();
2103   }
2104   return diameter;
2105 }
2106
2107 double BallDiameter::GetBadRate( double Value, int /*nbNodes*/ ) const
2108 {
2109   // meaningless as it is not a quality control functor
2110   return Value;
2111 }
2112
2113 SMDSAbs_ElementType BallDiameter::GetType() const
2114 {
2115   return SMDSAbs_Ball;
2116 }
2117
2118
2119 /*
2120                             PREDICATES
2121 */
2122
2123 //================================================================================
2124 /*
2125   Class       : BadOrientedVolume
2126   Description : Predicate bad oriented volumes
2127 */
2128 //================================================================================
2129
2130 BadOrientedVolume::BadOrientedVolume()
2131 {
2132   myMesh = 0;
2133 }
2134
2135 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
2136 {
2137   myMesh = theMesh;
2138 }
2139
2140 bool BadOrientedVolume::IsSatisfy( long theId )
2141 {
2142   if ( myMesh == 0 )
2143     return false;
2144
2145   SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
2146   return !vTool.IsForward();
2147 }
2148
2149 SMDSAbs_ElementType BadOrientedVolume::GetType() const
2150 {
2151   return SMDSAbs_Volume;
2152 }
2153
2154 /*
2155   Class       : BareBorderVolume
2156 */
2157
2158 bool BareBorderVolume::IsSatisfy(long theElementId )
2159 {
2160   SMDS_VolumeTool  myTool;
2161   if ( myTool.Set( myMesh->FindElement(theElementId)))
2162   {
2163     for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
2164       if ( myTool.IsFreeFace( iF ))
2165       {
2166         const SMDS_MeshNode** n = myTool.GetFaceNodes(iF);
2167         std::vector< const SMDS_MeshNode*> nodes( n, n+myTool.NbFaceNodes(iF));
2168         if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
2169           return true;
2170       }
2171   }
2172   return false;
2173 }
2174
2175 //================================================================================
2176 /*
2177   Class       : BareBorderFace
2178 */
2179 //================================================================================
2180
2181 bool BareBorderFace::IsSatisfy(long theElementId )
2182 {
2183   bool ok = false;
2184   if ( const SMDS_MeshElement* face = myMesh->FindElement(theElementId))
2185   {
2186     if ( face->GetType() == SMDSAbs_Face )
2187     {
2188       int nbN = face->NbCornerNodes();
2189       for ( int i = 0; i < nbN && !ok; ++i )
2190       {
2191         // check if a link is shared by another face
2192         const SMDS_MeshNode* n1 = face->GetNode( i );
2193         const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbN );
2194         SMDS_ElemIteratorPtr fIt = n1->GetInverseElementIterator( SMDSAbs_Face );
2195         bool isShared = false;
2196         while ( !isShared && fIt->more() )
2197         {
2198           const SMDS_MeshElement* f = fIt->next();
2199           isShared = ( f != face && f->GetNodeIndex(n2) != -1 );
2200         }
2201         if ( !isShared )
2202         {
2203           const int iQuad = face->IsQuadratic();
2204           myLinkNodes.resize( 2 + iQuad);
2205           myLinkNodes[0] = n1;
2206           myLinkNodes[1] = n2;
2207           if ( iQuad )
2208             myLinkNodes[2] = face->GetNode( i+nbN );
2209           ok = !myMesh->FindElement( myLinkNodes, SMDSAbs_Edge, /*noMedium=*/false);
2210         }
2211       }
2212     }
2213   }
2214   return ok;
2215 }
2216
2217 //================================================================================
2218 /*
2219   Class       : OverConstrainedVolume
2220 */
2221 //================================================================================
2222
2223 bool OverConstrainedVolume::IsSatisfy(long theElementId )
2224 {
2225   // An element is over-constrained if it has N-1 free borders where
2226   // N is the number of edges/faces for a 2D/3D element.
2227   SMDS_VolumeTool  myTool;
2228   if ( myTool.Set( myMesh->FindElement(theElementId)))
2229   {
2230     int nbSharedFaces = 0;
2231     for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
2232       if ( !myTool.IsFreeFace( iF ) && ++nbSharedFaces > 1 )
2233         break;
2234     return ( nbSharedFaces == 1 );
2235   }
2236   return false;
2237 }
2238
2239 //================================================================================
2240 /*
2241   Class       : OverConstrainedFace
2242 */
2243 //================================================================================
2244
2245 bool OverConstrainedFace::IsSatisfy(long theElementId )
2246 {
2247   // An element is over-constrained if it has N-1 free borders where
2248   // N is the number of edges/faces for a 2D/3D element.
2249   if ( const SMDS_MeshElement* face = myMesh->FindElement(theElementId))
2250     if ( face->GetType() == SMDSAbs_Face )
2251     {
2252       int nbSharedBorders = 0;
2253       int nbN = face->NbCornerNodes();
2254       for ( int i = 0; i < nbN; ++i )
2255       {
2256         // check if a link is shared by another face
2257         const SMDS_MeshNode* n1 = face->GetNode( i );
2258         const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbN );
2259         SMDS_ElemIteratorPtr fIt = n1->GetInverseElementIterator( SMDSAbs_Face );
2260         bool isShared = false;
2261         while ( !isShared && fIt->more() )
2262         {
2263           const SMDS_MeshElement* f = fIt->next();
2264           isShared = ( f != face && f->GetNodeIndex(n2) != -1 );
2265         }
2266         if ( isShared && ++nbSharedBorders > 1 )
2267           break;
2268       }
2269       return ( nbSharedBorders == 1 );
2270     }
2271   return false;
2272 }
2273
2274 //================================================================================
2275 /*
2276   Class       : CoincidentNodes
2277   Description : Predicate of Coincident nodes
2278 */
2279 //================================================================================
2280
2281 CoincidentNodes::CoincidentNodes()
2282 {
2283   myToler = 1e-5;
2284 }
2285
2286 bool CoincidentNodes::IsSatisfy( long theElementId )
2287 {
2288   return myCoincidentIDs.Contains( theElementId );
2289 }
2290
2291 SMDSAbs_ElementType CoincidentNodes::GetType() const
2292 {
2293   return SMDSAbs_Node;
2294 }
2295
2296 void CoincidentNodes::SetMesh( const SMDS_Mesh* theMesh )
2297 {
2298   myMeshModifTracer.SetMesh( theMesh );
2299   if ( myMeshModifTracer.IsMeshModified() )
2300   {
2301     TIDSortedNodeSet nodesToCheck;
2302     SMDS_NodeIteratorPtr nIt = theMesh->nodesIterator(/*idInceasingOrder=*/true);
2303     while ( nIt->more() )
2304       nodesToCheck.insert( nodesToCheck.end(), nIt->next() );
2305
2306     std::list< std::list< const SMDS_MeshNode*> > nodeGroups;
2307     SMESH_OctreeNode::FindCoincidentNodes ( nodesToCheck, &nodeGroups, myToler );
2308
2309     myCoincidentIDs.Clear();
2310     std::list< std::list< const SMDS_MeshNode*> >::iterator groupIt = nodeGroups.begin();
2311     for ( ; groupIt != nodeGroups.end(); ++groupIt )
2312     {
2313       std::list< const SMDS_MeshNode*>& coincNodes = *groupIt;
2314       std::list< const SMDS_MeshNode*>::iterator n = coincNodes.begin();
2315       for ( ; n != coincNodes.end(); ++n )
2316         myCoincidentIDs.Add( (*n)->GetID() );
2317     }
2318   }
2319 }
2320
2321 //================================================================================
2322 /*
2323   Class       : CoincidentElements
2324   Description : Predicate of Coincident Elements
2325   Note        : This class is suitable only for visualization of Coincident Elements
2326 */
2327 //================================================================================
2328
2329 CoincidentElements::CoincidentElements()
2330 {
2331   myMesh = 0;
2332 }
2333
2334 void CoincidentElements::SetMesh( const SMDS_Mesh* theMesh )
2335 {
2336   myMesh = theMesh;
2337 }
2338
2339 bool CoincidentElements::IsSatisfy( long theElementId )
2340 {
2341   if ( !myMesh ) return false;
2342
2343   if ( const SMDS_MeshElement* e = myMesh->FindElement( theElementId ))
2344   {
2345     if ( e->GetType() != GetType() ) return false;
2346     std::set< const SMDS_MeshNode* > elemNodes( e->begin_nodes(), e->end_nodes() );
2347     const int nbNodes = e->NbNodes();
2348     SMDS_ElemIteratorPtr invIt = (*elemNodes.begin())->GetInverseElementIterator( GetType() );
2349     while ( invIt->more() )
2350     {
2351       const SMDS_MeshElement* e2 = invIt->next();
2352       if ( e2 == e || e2->NbNodes() != nbNodes ) continue;
2353
2354       bool sameNodes = true;
2355       for ( size_t i = 0; i < elemNodes.size() && sameNodes; ++i )
2356         sameNodes = ( elemNodes.count( e2->GetNode( i )));
2357       if ( sameNodes )
2358         return true;
2359     }
2360   }
2361   return false;
2362 }
2363
2364 SMDSAbs_ElementType CoincidentElements1D::GetType() const
2365 {
2366   return SMDSAbs_Edge;
2367 }
2368 SMDSAbs_ElementType CoincidentElements2D::GetType() const
2369 {
2370   return SMDSAbs_Face;
2371 }
2372 SMDSAbs_ElementType CoincidentElements3D::GetType() const
2373 {
2374   return SMDSAbs_Volume;
2375 }
2376
2377
2378 //================================================================================
2379 /*
2380   Class       : FreeBorders
2381   Description : Predicate for free borders
2382 */
2383 //================================================================================
2384
2385 FreeBorders::FreeBorders()
2386 {
2387   myMesh = 0;
2388 }
2389
2390 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
2391 {
2392   myMesh = theMesh;
2393 }
2394
2395 bool FreeBorders::IsSatisfy( long theId )
2396 {
2397   return getNbMultiConnection( myMesh, theId ) == 1;
2398 }
2399
2400 SMDSAbs_ElementType FreeBorders::GetType() const
2401 {
2402   return SMDSAbs_Edge;
2403 }
2404
2405
2406 //================================================================================
2407 /*
2408   Class       : FreeEdges
2409   Description : Predicate for free Edges
2410 */
2411 //================================================================================
2412
2413 FreeEdges::FreeEdges()
2414 {
2415   myMesh = 0;
2416 }
2417
2418 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
2419 {
2420   myMesh = theMesh;
2421 }
2422
2423 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  )
2424 {
2425   TColStd_MapOfInteger aMap;
2426   for ( int i = 0; i < 2; i++ )
2427   {
2428     SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator(SMDSAbs_Face);
2429     while( anElemIter->more() )
2430     {
2431       if ( const SMDS_MeshElement* anElem = anElemIter->next())
2432       {
2433         const int anId = anElem->GetID();
2434         if ( anId != theFaceId && !aMap.Add( anId ))
2435           return false;
2436       }
2437     }
2438   }
2439   return true;
2440 }
2441
2442 bool FreeEdges::IsSatisfy( long theId )
2443 {
2444   if ( myMesh == 0 )
2445     return false;
2446
2447   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
2448   if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
2449     return false;
2450
2451   SMDS_NodeIteratorPtr anIter = aFace->interlacedNodesIterator();
2452   if ( !anIter )
2453     return false;
2454
2455   int i = 0, nbNodes = aFace->NbNodes();
2456   std::vector <const SMDS_MeshNode*> aNodes( nbNodes+1 );
2457   while( anIter->more() )
2458     if ( ! ( aNodes[ i++ ] = anIter->next() ))
2459       return false;
2460   aNodes[ nbNodes ] = aNodes[ 0 ];
2461
2462   for ( i = 0; i < nbNodes; i++ )
2463     if ( IsFreeEdge( &aNodes[ i ], theId ) )
2464       return true;
2465
2466   return false;
2467 }
2468
2469 SMDSAbs_ElementType FreeEdges::GetType() const
2470 {
2471   return SMDSAbs_Face;
2472 }
2473
2474 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
2475   myElemId(theElemId)
2476 {
2477   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
2478   if(thePntId1 > thePntId2){
2479     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
2480   }
2481 }
2482
2483 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
2484   if(myPntId[0] < x.myPntId[0]) return true;
2485   if(myPntId[0] == x.myPntId[0])
2486     if(myPntId[1] < x.myPntId[1]) return true;
2487   return false;
2488 }
2489
2490 inline void UpdateBorders(const FreeEdges::Border& theBorder,
2491                           FreeEdges::TBorders& theRegistry,
2492                           FreeEdges::TBorders& theContainer)
2493 {
2494   if(theRegistry.find(theBorder) == theRegistry.end()){
2495     theRegistry.insert(theBorder);
2496     theContainer.insert(theBorder);
2497   }else{
2498     theContainer.erase(theBorder);
2499   }
2500 }
2501
2502 void FreeEdges::GetBoreders(TBorders& theBorders)
2503 {
2504   TBorders aRegistry;
2505   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2506   for(; anIter->more(); ){
2507     const SMDS_MeshFace* anElem = anIter->next();
2508     long anElemId = anElem->GetID();
2509     SMDS_ElemIteratorPtr aNodesIter;
2510     if ( anElem->IsQuadratic() )
2511       aNodesIter = static_cast<const SMDS_VtkFace*>(anElem)->
2512         interlacedNodesElemIterator();
2513     else
2514       aNodesIter = anElem->nodesIterator();
2515     long aNodeId[2] = {0,0};
2516     const SMDS_MeshElement* aNode;
2517     if(aNodesIter->more()){
2518       aNode = aNodesIter->next();
2519       aNodeId[0] = aNodeId[1] = aNode->GetID();
2520     }
2521     for(; aNodesIter->more(); ){
2522       aNode = aNodesIter->next();
2523       long anId = aNode->GetID();
2524       Border aBorder(anElemId,aNodeId[1],anId);
2525       aNodeId[1] = anId;
2526       UpdateBorders(aBorder,aRegistry,theBorders);
2527     }
2528     Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
2529     UpdateBorders(aBorder,aRegistry,theBorders);
2530   }
2531 }
2532
2533 //================================================================================
2534 /*
2535   Class       : FreeNodes
2536   Description : Predicate for free nodes
2537 */
2538 //================================================================================
2539
2540 FreeNodes::FreeNodes()
2541 {
2542   myMesh = 0;
2543 }
2544
2545 void FreeNodes::SetMesh( const SMDS_Mesh* theMesh )
2546 {
2547   myMesh = theMesh;
2548 }
2549
2550 bool FreeNodes::IsSatisfy( long theNodeId )
2551 {
2552   const SMDS_MeshNode* aNode = myMesh->FindNode( theNodeId );
2553   if (!aNode)
2554     return false;
2555
2556   return (aNode->NbInverseElements() < 1);
2557 }
2558
2559 SMDSAbs_ElementType FreeNodes::GetType() const
2560 {
2561   return SMDSAbs_Node;
2562 }
2563
2564
2565 //================================================================================
2566 /*
2567   Class       : FreeFaces
2568   Description : Predicate for free faces
2569 */
2570 //================================================================================
2571
2572 FreeFaces::FreeFaces()
2573 {
2574   myMesh = 0;
2575 }
2576
2577 void FreeFaces::SetMesh( const SMDS_Mesh* theMesh )
2578 {
2579   myMesh = theMesh;
2580 }
2581
2582 bool FreeFaces::IsSatisfy( long theId )
2583 {
2584   if (!myMesh) return false;
2585   // check that faces nodes refers to less than two common volumes
2586   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
2587   if ( !aFace || aFace->GetType() != SMDSAbs_Face )
2588     return false;
2589
2590   int nbNode = aFace->NbNodes();
2591
2592   // collect volumes to check that number of volumes with count equal nbNode not less than 2
2593   typedef std::map< SMDS_MeshElement*, int > TMapOfVolume; // map of volume counters
2594   typedef std::map< SMDS_MeshElement*, int >::iterator TItrMapOfVolume; // iterator
2595   TMapOfVolume mapOfVol;
2596
2597   SMDS_ElemIteratorPtr nodeItr = aFace->nodesIterator();
2598   while ( nodeItr->more() )
2599   {
2600     const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(nodeItr->next());
2601     if ( !aNode ) continue;
2602     SMDS_ElemIteratorPtr volItr = aNode->GetInverseElementIterator(SMDSAbs_Volume);
2603     while ( volItr->more() )
2604     {
2605       SMDS_MeshElement* aVol = (SMDS_MeshElement*)volItr->next();
2606       TItrMapOfVolume    itr = mapOfVol.insert( std::make_pair( aVol, 0 )).first;
2607       (*itr).second++;
2608     } 
2609   }
2610   int nbVol = 0;
2611   TItrMapOfVolume volItr = mapOfVol.begin();
2612   TItrMapOfVolume volEnd = mapOfVol.end();
2613   for ( ; volItr != volEnd; ++volItr )
2614     if ( (*volItr).second >= nbNode )
2615        nbVol++;
2616   // face is not free if number of volumes constructed on thier nodes more than one
2617   return (nbVol < 2);
2618 }
2619
2620 SMDSAbs_ElementType FreeFaces::GetType() const
2621 {
2622   return SMDSAbs_Face;
2623 }
2624
2625 //================================================================================
2626 /*
2627   Class       : LinearOrQuadratic
2628   Description : Predicate to verify whether a mesh element is linear
2629 */
2630 //================================================================================
2631
2632 LinearOrQuadratic::LinearOrQuadratic()
2633 {
2634   myMesh = 0;
2635 }
2636
2637 void LinearOrQuadratic::SetMesh( const SMDS_Mesh* theMesh )
2638 {
2639   myMesh = theMesh;
2640 }
2641
2642 bool LinearOrQuadratic::IsSatisfy( long theId )
2643 {
2644   if (!myMesh) return false;
2645   const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
2646   if ( !anElem || (myType != SMDSAbs_All && anElem->GetType() != myType) )
2647     return false;
2648   return (!anElem->IsQuadratic());
2649 }
2650
2651 void LinearOrQuadratic::SetType( SMDSAbs_ElementType theType )
2652 {
2653   myType = theType;
2654 }
2655
2656 SMDSAbs_ElementType LinearOrQuadratic::GetType() const
2657 {
2658   return myType;
2659 }
2660
2661 //================================================================================
2662 /*
2663   Class       : GroupColor
2664   Description : Functor for check color of group to whic mesh element belongs to
2665 */
2666 //================================================================================
2667
2668 GroupColor::GroupColor()
2669 {
2670 }
2671
2672 bool GroupColor::IsSatisfy( long theId )
2673 {
2674   return myIDs.count( theId );
2675 }
2676
2677 void GroupColor::SetType( SMDSAbs_ElementType theType )
2678 {
2679   myType = theType;
2680 }
2681
2682 SMDSAbs_ElementType GroupColor::GetType() const
2683 {
2684   return myType;
2685 }
2686
2687 static bool isEqual( const Quantity_Color& theColor1,
2688                      const Quantity_Color& theColor2 )
2689 {
2690   // tolerance to compare colors
2691   const double tol = 5*1e-3;
2692   return ( fabs( theColor1.Red()   - theColor2.Red() )   < tol &&
2693            fabs( theColor1.Green() - theColor2.Green() ) < tol &&
2694            fabs( theColor1.Blue()  - theColor2.Blue() )  < tol );
2695 }
2696
2697 void GroupColor::SetMesh( const SMDS_Mesh* theMesh )
2698 {
2699   myIDs.clear();
2700
2701   const SMESHDS_Mesh* aMesh = dynamic_cast<const SMESHDS_Mesh*>(theMesh);
2702   if ( !aMesh )
2703     return;
2704
2705   int nbGrp = aMesh->GetNbGroups();
2706   if ( !nbGrp )
2707     return;
2708
2709   // iterates on groups and find necessary elements ids
2710   const std::set<SMESHDS_GroupBase*>&       aGroups = aMesh->GetGroups();
2711   std::set<SMESHDS_GroupBase*>::const_iterator GrIt = aGroups.begin();
2712   for (; GrIt != aGroups.end(); GrIt++)
2713   {
2714     SMESHDS_GroupBase* aGrp = (*GrIt);
2715     if ( !aGrp )
2716       continue;
2717     // check type and color of group
2718     if ( !isEqual( myColor, aGrp->GetColor() ))
2719       continue;
2720
2721     // IPAL52867 (prevent infinite recursion via GroupOnFilter)
2722     if ( SMESHDS_GroupOnFilter * gof = dynamic_cast< SMESHDS_GroupOnFilter* >( aGrp ))
2723       if ( gof->GetPredicate().get() == this )
2724         continue;
2725
2726     SMDSAbs_ElementType aGrpElType = (SMDSAbs_ElementType)aGrp->GetType();
2727     if ( myType == aGrpElType || (myType == SMDSAbs_All && aGrpElType != SMDSAbs_Node) ) {
2728       // add elements IDS into control
2729       int aSize = aGrp->Extent();
2730       for (int i = 0; i < aSize; i++)
2731         myIDs.insert( aGrp->GetID(i+1) );
2732     }
2733   }
2734 }
2735
2736 void GroupColor::SetColorStr( const TCollection_AsciiString& theStr )
2737 {
2738   Kernel_Utils::Localizer loc;
2739   TCollection_AsciiString aStr = theStr;
2740   aStr.RemoveAll( ' ' );
2741   aStr.RemoveAll( '\t' );
2742   for ( int aPos = aStr.Search( ";;" ); aPos != -1; aPos = aStr.Search( ";;" ) )
2743     aStr.Remove( aPos, 2 );
2744   Standard_Real clr[3];
2745   clr[0] = clr[1] = clr[2] = 0.;
2746   for ( int i = 0; i < 3; i++ ) {
2747     TCollection_AsciiString tmpStr = aStr.Token( ";", i+1 );
2748     if ( !tmpStr.IsEmpty() && tmpStr.IsRealValue() )
2749       clr[i] = tmpStr.RealValue();
2750   }
2751   myColor = Quantity_Color( clr[0], clr[1], clr[2], Quantity_TOC_RGB );
2752 }
2753
2754 //=======================================================================
2755 // name    : GetRangeStr
2756 // Purpose : Get range as a string.
2757 //           Example: "1,2,3,50-60,63,67,70-"
2758 //=======================================================================
2759
2760 void GroupColor::GetColorStr( TCollection_AsciiString& theResStr ) const
2761 {
2762   theResStr.Clear();
2763   theResStr += TCollection_AsciiString( myColor.Red() );
2764   theResStr += TCollection_AsciiString( ";" ) + TCollection_AsciiString( myColor.Green() );
2765   theResStr += TCollection_AsciiString( ";" ) + TCollection_AsciiString( myColor.Blue() );
2766 }
2767
2768 //================================================================================
2769 /*
2770   Class       : ElemGeomType
2771   Description : Predicate to check element geometry type
2772 */
2773 //================================================================================
2774
2775 ElemGeomType::ElemGeomType()
2776 {
2777   myMesh = 0;
2778   myType = SMDSAbs_All;
2779   myGeomType = SMDSGeom_TRIANGLE;
2780 }
2781
2782 void ElemGeomType::SetMesh( const SMDS_Mesh* theMesh )
2783 {
2784   myMesh = theMesh;
2785 }
2786
2787 bool ElemGeomType::IsSatisfy( long theId )
2788 {
2789   if (!myMesh) return false;
2790   const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
2791   if ( !anElem )
2792     return false;
2793   const SMDSAbs_ElementType anElemType = anElem->GetType();
2794   if ( myType != SMDSAbs_All && anElemType != myType )
2795     return false;
2796   bool isOk = ( anElem->GetGeomType() == myGeomType );
2797   return isOk;
2798 }
2799
2800 void ElemGeomType::SetType( SMDSAbs_ElementType theType )
2801 {
2802   myType = theType;
2803 }
2804
2805 SMDSAbs_ElementType ElemGeomType::GetType() const
2806 {
2807   return myType;
2808 }
2809
2810 void ElemGeomType::SetGeomType( SMDSAbs_GeometryType theType )
2811 {
2812   myGeomType = theType;
2813 }
2814
2815 SMDSAbs_GeometryType ElemGeomType::GetGeomType() const
2816 {
2817   return myGeomType;
2818 }
2819
2820 //================================================================================
2821 /*
2822   Class       : ElemEntityType
2823   Description : Predicate to check element entity type
2824 */
2825 //================================================================================
2826
2827 ElemEntityType::ElemEntityType():
2828   myMesh( 0 ),
2829   myType( SMDSAbs_All ),
2830   myEntityType( SMDSEntity_0D )
2831 {
2832 }
2833
2834 void ElemEntityType::SetMesh( const SMDS_Mesh* theMesh )
2835 {
2836   myMesh = theMesh;
2837 }
2838
2839 bool ElemEntityType::IsSatisfy( long theId )
2840 {
2841   if ( !myMesh ) return false;
2842   if ( myType == SMDSAbs_Node )
2843     return myMesh->FindNode( theId );
2844   const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
2845   return ( anElem &&
2846            myEntityType == anElem->GetEntityType() );
2847 }
2848
2849 void ElemEntityType::SetType( SMDSAbs_ElementType theType )
2850 {
2851   myType = theType;
2852 }
2853
2854 SMDSAbs_ElementType ElemEntityType::GetType() const
2855 {
2856   return myType;
2857 }
2858
2859 void ElemEntityType::SetElemEntityType( SMDSAbs_EntityType theEntityType )
2860 {
2861   myEntityType = theEntityType;
2862 }
2863
2864 SMDSAbs_EntityType ElemEntityType::GetElemEntityType() const
2865 {
2866   return myEntityType;
2867 }
2868
2869 //================================================================================
2870 /*!
2871  * \brief Class ConnectedElements
2872  */
2873 //================================================================================
2874
2875 ConnectedElements::ConnectedElements():
2876   myNodeID(0), myType( SMDSAbs_All ), myOkIDsReady( false ) {}
2877
2878 SMDSAbs_ElementType ConnectedElements::GetType() const
2879 { return myType; }
2880
2881 int ConnectedElements::GetNode() const
2882 { return myXYZ.empty() ? myNodeID : 0; } // myNodeID can be found by myXYZ
2883
2884 std::vector<double> ConnectedElements::GetPoint() const
2885 { return myXYZ; }
2886
2887 void ConnectedElements::clearOkIDs()
2888 { myOkIDsReady = false; myOkIDs.clear(); }
2889
2890 void ConnectedElements::SetType( SMDSAbs_ElementType theType )
2891 {
2892   if ( myType != theType || myMeshModifTracer.IsMeshModified() )
2893     clearOkIDs();
2894   myType = theType;
2895 }
2896
2897 void ConnectedElements::SetMesh( const SMDS_Mesh* theMesh )
2898 {
2899   myMeshModifTracer.SetMesh( theMesh );
2900   if ( myMeshModifTracer.IsMeshModified() )
2901   {
2902     clearOkIDs();
2903     if ( !myXYZ.empty() )
2904       SetPoint( myXYZ[0], myXYZ[1], myXYZ[2] ); // find a node near myXYZ it in a new mesh
2905   }
2906 }
2907
2908 void ConnectedElements::SetNode( int nodeID )
2909 {
2910   myNodeID = nodeID;
2911   myXYZ.clear();
2912
2913   bool isSameDomain = false;
2914   if ( myOkIDsReady && myMeshModifTracer.GetMesh() && !myMeshModifTracer.IsMeshModified() )
2915     if ( const SMDS_MeshNode* n = myMeshModifTracer.GetMesh()->FindNode( myNodeID ))
2916     {
2917       SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator( myType );
2918       while ( !isSameDomain && eIt->more() )
2919         isSameDomain = IsSatisfy( eIt->next()->GetID() );
2920     }
2921   if ( !isSameDomain )
2922     clearOkIDs();
2923 }
2924
2925 void ConnectedElements::SetPoint( double x, double y, double z )
2926 {
2927   myXYZ.resize(3);
2928   myXYZ[0] = x;
2929   myXYZ[1] = y;
2930   myXYZ[2] = z;
2931   myNodeID = 0;
2932
2933   bool isSameDomain = false;
2934
2935   // find myNodeID by myXYZ if possible
2936   if ( myMeshModifTracer.GetMesh() )
2937   {
2938     SMESHUtils::Deleter<SMESH_ElementSearcher> searcher
2939       ( SMESH_MeshAlgos::GetElementSearcher( (SMDS_Mesh&) *myMeshModifTracer.GetMesh() ));
2940
2941     std::vector< const SMDS_MeshElement* > foundElems;
2942     searcher->FindElementsByPoint( gp_Pnt(x,y,z), SMDSAbs_All, foundElems );
2943
2944     if ( !foundElems.empty() )
2945     {
2946       myNodeID = foundElems[0]->GetNode(0)->GetID();
2947       if ( myOkIDsReady && !myMeshModifTracer.IsMeshModified() )
2948         isSameDomain = IsSatisfy( foundElems[0]->GetID() );
2949     }
2950   }
2951   if ( !isSameDomain )
2952     clearOkIDs();
2953 }
2954
2955 bool ConnectedElements::IsSatisfy( long theElementId )
2956 {
2957   // Here we do NOT check if the mesh has changed, we do it in Set...() only!!!
2958
2959   if ( !myOkIDsReady )
2960   {
2961     if ( !myMeshModifTracer.GetMesh() )
2962       return false;
2963     const SMDS_MeshNode* node0 = myMeshModifTracer.GetMesh()->FindNode( myNodeID );
2964     if ( !node0 )
2965       return false;
2966
2967     std::list< const SMDS_MeshNode* > nodeQueue( 1, node0 );
2968     std::set< int > checkedNodeIDs;
2969     // algo:
2970     // foreach node in nodeQueue:
2971     //   foreach element sharing a node:
2972     //     add ID of an element of myType to myOkIDs;
2973     //     push all element nodes absent from checkedNodeIDs to nodeQueue;
2974     while ( !nodeQueue.empty() )
2975     {
2976       const SMDS_MeshNode* node = nodeQueue.front();
2977       nodeQueue.pop_front();
2978
2979       // loop on elements sharing the node
2980       SMDS_ElemIteratorPtr eIt = node->GetInverseElementIterator();
2981       while ( eIt->more() )
2982       {
2983         // keep elements of myType
2984         const SMDS_MeshElement* element = eIt->next();
2985         if ( element->GetType() == myType )
2986           myOkIDs.insert( myOkIDs.end(), element->GetID() );
2987
2988         // enqueue nodes of the element
2989         SMDS_ElemIteratorPtr nIt = element->nodesIterator();
2990         while ( nIt->more() )
2991         {
2992           const SMDS_MeshNode* n = static_cast< const SMDS_MeshNode* >( nIt->next() );
2993           if ( checkedNodeIDs.insert( n->GetID() ).second )
2994             nodeQueue.push_back( n );
2995         }
2996       }
2997     }
2998     if ( myType == SMDSAbs_Node )
2999       std::swap( myOkIDs, checkedNodeIDs );
3000
3001     size_t totalNbElems = myMeshModifTracer.GetMesh()->GetMeshInfo().NbElements( myType );
3002     if ( myOkIDs.size() == totalNbElems )
3003       myOkIDs.clear();
3004
3005     myOkIDsReady = true;
3006   }
3007
3008   return myOkIDs.empty() ? true : myOkIDs.count( theElementId );
3009 }
3010
3011 //================================================================================
3012 /*!
3013  * \brief Class CoplanarFaces
3014  */
3015 //================================================================================
3016
3017 namespace
3018 {
3019   inline bool isLessAngle( const gp_Vec& v1, const gp_Vec& v2, const double cos )
3020   {
3021     double dot = v1 * v2; // cos * |v1| * |v2|
3022     double l1  = v1.SquareMagnitude();
3023     double l2  = v2.SquareMagnitude();
3024     return (( dot * cos >= 0 ) && 
3025             ( dot * dot ) / l1 / l2 >= ( cos * cos ));
3026   }
3027 }
3028 CoplanarFaces::CoplanarFaces()
3029   : myFaceID(0), myToler(0)
3030 {
3031 }
3032 void CoplanarFaces::SetMesh( const SMDS_Mesh* theMesh )
3033 {
3034   myMeshModifTracer.SetMesh( theMesh );
3035   if ( myMeshModifTracer.IsMeshModified() )
3036   {
3037     // Build a set of coplanar face ids
3038
3039     myCoplanarIDs.Clear();
3040
3041     if ( !myMeshModifTracer.GetMesh() || !myFaceID || !myToler )
3042       return;
3043
3044     const SMDS_MeshElement* face = myMeshModifTracer.GetMesh()->FindElement( myFaceID );
3045     if ( !face || face->GetType() != SMDSAbs_Face )
3046       return;
3047
3048     bool normOK;
3049     gp_Vec myNorm = getNormale( static_cast<const SMDS_MeshFace*>(face), &normOK );
3050     if (!normOK)
3051       return;
3052
3053     const double cosTol = Cos( myToler * M_PI / 180. );
3054     NCollection_Map< SMESH_TLink, SMESH_TLink > checkedLinks;
3055
3056     std::list< std::pair< const SMDS_MeshElement*, gp_Vec > > faceQueue;
3057     faceQueue.push_back( std::make_pair( face, myNorm ));
3058     while ( !faceQueue.empty() )
3059     {
3060       face   = faceQueue.front().first;
3061       myNorm = faceQueue.front().second;
3062       faceQueue.pop_front();
3063
3064       for ( int i = 0, nbN = face->NbCornerNodes(); i < nbN; ++i )
3065       {
3066         const SMDS_MeshNode*  n1 = face->GetNode( i );
3067         const SMDS_MeshNode*  n2 = face->GetNode(( i+1 )%nbN);
3068         if ( !checkedLinks.Add( SMESH_TLink( n1, n2 )))
3069           continue;
3070         SMDS_ElemIteratorPtr fIt = n1->GetInverseElementIterator(SMDSAbs_Face);
3071         while ( fIt->more() )
3072         {
3073           const SMDS_MeshElement* f = fIt->next();
3074           if ( f->GetNodeIndex( n2 ) > -1 )
3075           {
3076             gp_Vec norm = getNormale( static_cast<const SMDS_MeshFace*>(f), &normOK );
3077             if (!normOK || isLessAngle( myNorm, norm, cosTol))
3078             {
3079               myCoplanarIDs.Add( f->GetID() );
3080               faceQueue.push_back( std::make_pair( f, norm ));
3081             }
3082           }
3083         }
3084       }
3085     }
3086   }
3087 }
3088 bool CoplanarFaces::IsSatisfy( long theElementId )
3089 {
3090   return myCoplanarIDs.Contains( theElementId );
3091 }
3092
3093 /*
3094  *Class       : RangeOfIds
3095   *Description : Predicate for Range of Ids.
3096   *              Range may be specified with two ways.
3097   *              1. Using AddToRange method
3098   *              2. With SetRangeStr method. Parameter of this method is a string
3099   *                 like as "1,2,3,50-60,63,67,70-"
3100 */
3101
3102 //=======================================================================
3103 // name    : RangeOfIds
3104 // Purpose : Constructor
3105 //=======================================================================
3106 RangeOfIds::RangeOfIds()
3107 {
3108   myMesh = 0;
3109   myType = SMDSAbs_All;
3110 }
3111
3112 //=======================================================================
3113 // name    : SetMesh
3114 // Purpose : Set mesh
3115 //=======================================================================
3116 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
3117 {
3118   myMesh = theMesh;
3119 }
3120
3121 //=======================================================================
3122 // name    : AddToRange
3123 // Purpose : Add ID to the range
3124 //=======================================================================
3125 bool RangeOfIds::AddToRange( long theEntityId )
3126 {
3127   myIds.Add( theEntityId );
3128   return true;
3129 }
3130
3131 //=======================================================================
3132 // name    : GetRangeStr
3133 // Purpose : Get range as a string.
3134 //           Example: "1,2,3,50-60,63,67,70-"
3135 //=======================================================================
3136 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
3137 {
3138   theResStr.Clear();
3139
3140   TColStd_SequenceOfInteger     anIntSeq;
3141   TColStd_SequenceOfAsciiString aStrSeq;
3142
3143   TColStd_MapIteratorOfMapOfInteger anIter( myIds );
3144   for ( ; anIter.More(); anIter.Next() )
3145   {
3146     int anId = anIter.Key();
3147     TCollection_AsciiString aStr( anId );
3148     anIntSeq.Append( anId );
3149     aStrSeq.Append( aStr );
3150   }
3151
3152   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
3153   {
3154     int aMinId = myMin( i );
3155     int aMaxId = myMax( i );
3156
3157     TCollection_AsciiString aStr;
3158     if ( aMinId != IntegerFirst() )
3159       aStr += aMinId;
3160
3161     aStr += "-";
3162
3163     if ( aMaxId != IntegerLast() )
3164       aStr += aMaxId;
3165
3166     // find position of the string in result sequence and insert string in it
3167     if ( anIntSeq.Length() == 0 )
3168     {
3169       anIntSeq.Append( aMinId );
3170       aStrSeq.Append( aStr );
3171     }
3172     else
3173     {
3174       if ( aMinId < anIntSeq.First() )
3175       {
3176         anIntSeq.Prepend( aMinId );
3177         aStrSeq.Prepend( aStr );
3178       }
3179       else if ( aMinId > anIntSeq.Last() )
3180       {
3181         anIntSeq.Append( aMinId );
3182         aStrSeq.Append( aStr );
3183       }
3184       else
3185         for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
3186           if ( aMinId < anIntSeq( j ) )
3187           {
3188             anIntSeq.InsertBefore( j, aMinId );
3189             aStrSeq.InsertBefore( j, aStr );
3190             break;
3191           }
3192     }
3193   }
3194
3195   if ( aStrSeq.Length() == 0 )
3196     return;
3197
3198   theResStr = aStrSeq( 1 );
3199   for ( int j = 2, k = aStrSeq.Length(); j <= k; j++  )
3200   {
3201     theResStr += ",";
3202     theResStr += aStrSeq( j );
3203   }
3204 }
3205
3206 //=======================================================================
3207 // name    : SetRangeStr
3208 // Purpose : Define range with string
3209 //           Example of entry string: "1,2,3,50-60,63,67,70-"
3210 //=======================================================================
3211 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
3212 {
3213   myMin.Clear();
3214   myMax.Clear();
3215   myIds.Clear();
3216
3217   TCollection_AsciiString aStr = theStr;
3218   for ( int i = 1; i <= aStr.Length(); ++i )
3219   {
3220     char c = aStr.Value( i );
3221     if ( !isdigit( c ) && c != ',' && c != '-' )
3222       aStr.SetValue( i, ' ');
3223   }
3224   aStr.RemoveAll( ' ' );
3225
3226   TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
3227   int i = 1;
3228   while ( tmpStr != "" )
3229   {
3230     tmpStr = aStr.Token( ",", i++ );
3231     int aPos = tmpStr.Search( '-' );
3232
3233     if ( aPos == -1 )
3234     {
3235       if ( tmpStr.IsIntegerValue() )
3236         myIds.Add( tmpStr.IntegerValue() );
3237       else
3238         return false;
3239     }
3240     else
3241     {
3242       TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
3243       TCollection_AsciiString aMinStr = tmpStr;
3244
3245       while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
3246       while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
3247
3248       if ( (!aMinStr.IsEmpty() && !aMinStr.IsIntegerValue()) ||
3249            (!aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue()) )
3250         return false;
3251
3252       myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
3253       myMax.Append( aMaxStr.IsEmpty() ? IntegerLast()  : aMaxStr.IntegerValue() );
3254     }
3255   }
3256
3257   return true;
3258 }
3259
3260 //=======================================================================
3261 // name    : GetType
3262 // Purpose : Get type of supported entities
3263 //=======================================================================
3264 SMDSAbs_ElementType RangeOfIds::GetType() const
3265 {
3266   return myType;
3267 }
3268
3269 //=======================================================================
3270 // name    : SetType
3271 // Purpose : Set type of supported entities
3272 //=======================================================================
3273 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
3274 {
3275   myType = theType;
3276 }
3277
3278 //=======================================================================
3279 // name    : IsSatisfy
3280 // Purpose : Verify whether entity satisfies to this rpedicate
3281 //=======================================================================
3282 bool RangeOfIds::IsSatisfy( long theId )
3283 {
3284   if ( !myMesh )
3285     return false;
3286
3287   if ( myType == SMDSAbs_Node )
3288   {
3289     if ( myMesh->FindNode( theId ) == 0 )
3290       return false;
3291   }
3292   else
3293   {
3294     const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
3295     if ( anElem == 0 || (myType != anElem->GetType() && myType != SMDSAbs_All ))
3296       return false;
3297   }
3298
3299   if ( myIds.Contains( theId ) )
3300     return true;
3301
3302   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
3303     if ( theId >= myMin( i ) && theId <= myMax( i ) )
3304       return true;
3305
3306   return false;
3307 }
3308
3309 /*
3310   Class       : Comparator
3311   Description : Base class for comparators
3312 */
3313 Comparator::Comparator():
3314   myMargin(0)
3315 {}
3316
3317 Comparator::~Comparator()
3318 {}
3319
3320 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
3321 {
3322   if ( myFunctor )
3323     myFunctor->SetMesh( theMesh );
3324 }
3325
3326 void Comparator::SetMargin( double theValue )
3327 {
3328   myMargin = theValue;
3329 }
3330
3331 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
3332 {
3333   myFunctor = theFunct;
3334 }
3335
3336 SMDSAbs_ElementType Comparator::GetType() const
3337 {
3338   return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
3339 }
3340
3341 double Comparator::GetMargin()
3342 {
3343   return myMargin;
3344 }
3345
3346
3347 /*
3348   Class       : LessThan
3349   Description : Comparator "<"
3350 */
3351 bool LessThan::IsSatisfy( long theId )
3352 {
3353   return myFunctor && myFunctor->GetValue( theId ) < myMargin;
3354 }
3355
3356
3357 /*
3358   Class       : MoreThan
3359   Description : Comparator ">"
3360 */
3361 bool MoreThan::IsSatisfy( long theId )
3362 {
3363   return myFunctor && myFunctor->GetValue( theId ) > myMargin;
3364 }
3365
3366
3367 /*
3368   Class       : EqualTo
3369   Description : Comparator "="
3370 */
3371 EqualTo::EqualTo():
3372   myToler(Precision::Confusion())
3373 {}
3374
3375 bool EqualTo::IsSatisfy( long theId )
3376 {
3377   return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
3378 }
3379
3380 void EqualTo::SetTolerance( double theToler )
3381 {
3382   myToler = theToler;
3383 }
3384
3385 double EqualTo::GetTolerance()
3386 {
3387   return myToler;
3388 }
3389
3390 /*
3391   Class       : LogicalNOT
3392   Description : Logical NOT predicate
3393 */
3394 LogicalNOT::LogicalNOT()
3395 {}
3396
3397 LogicalNOT::~LogicalNOT()
3398 {}
3399
3400 bool LogicalNOT::IsSatisfy( long theId )
3401 {
3402   return myPredicate && !myPredicate->IsSatisfy( theId );
3403 }
3404
3405 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
3406 {
3407   if ( myPredicate )
3408     myPredicate->SetMesh( theMesh );
3409 }
3410
3411 void LogicalNOT::SetPredicate( PredicatePtr thePred )
3412 {
3413   myPredicate = thePred;
3414 }
3415
3416 SMDSAbs_ElementType LogicalNOT::GetType() const
3417 {
3418   return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
3419 }
3420
3421
3422 /*
3423   Class       : LogicalBinary
3424   Description : Base class for binary logical predicate
3425 */
3426 LogicalBinary::LogicalBinary()
3427 {}
3428
3429 LogicalBinary::~LogicalBinary()
3430 {}
3431
3432 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
3433 {
3434   if ( myPredicate1 )
3435     myPredicate1->SetMesh( theMesh );
3436
3437   if ( myPredicate2 )
3438     myPredicate2->SetMesh( theMesh );
3439 }
3440
3441 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
3442 {
3443   myPredicate1 = thePredicate;
3444 }
3445
3446 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
3447 {
3448   myPredicate2 = thePredicate;
3449 }
3450
3451 SMDSAbs_ElementType LogicalBinary::GetType() const
3452 {
3453   if ( !myPredicate1 || !myPredicate2 )
3454     return SMDSAbs_All;
3455
3456   SMDSAbs_ElementType aType1 = myPredicate1->GetType();
3457   SMDSAbs_ElementType aType2 = myPredicate2->GetType();
3458
3459   return aType1 == aType2 ? aType1 : SMDSAbs_All;
3460 }
3461
3462
3463 /*
3464   Class       : LogicalAND
3465   Description : Logical AND
3466 */
3467 bool LogicalAND::IsSatisfy( long theId )
3468 {
3469   return
3470     myPredicate1 &&
3471     myPredicate2 &&
3472     myPredicate1->IsSatisfy( theId ) &&
3473     myPredicate2->IsSatisfy( theId );
3474 }
3475
3476
3477 /*
3478   Class       : LogicalOR
3479   Description : Logical OR
3480 */
3481 bool LogicalOR::IsSatisfy( long theId )
3482 {
3483   return
3484     myPredicate1 &&
3485     myPredicate2 &&
3486     (myPredicate1->IsSatisfy( theId ) ||
3487     myPredicate2->IsSatisfy( theId ));
3488 }
3489
3490
3491 /*
3492                               FILTER
3493 */
3494
3495 // #ifdef WITH_TBB
3496 // #include <tbb/parallel_for.h>
3497 // #include <tbb/enumerable_thread_specific.h>
3498
3499 // namespace Parallel
3500 // {
3501 //   typedef tbb::enumerable_thread_specific< TIdSequence > TIdSeq;
3502
3503 //   struct Predicate
3504 //   {
3505 //     const SMDS_Mesh* myMesh;
3506 //     PredicatePtr     myPredicate;
3507 //     TIdSeq &         myOKIds;
3508 //     Predicate( const SMDS_Mesh* m, PredicatePtr p, TIdSeq & ids ):
3509 //       myMesh(m), myPredicate(p->Duplicate()), myOKIds(ids) {}
3510 //     void operator() ( const tbb::blocked_range<size_t>& r ) const
3511 //     {
3512 //       for ( size_t i = r.begin(); i != r.end(); ++i )
3513 //         if ( myPredicate->IsSatisfy( i ))
3514 //           myOKIds.local().push_back();
3515 //     }
3516 //   }
3517 // }
3518 // #endif
3519
3520 Filter::Filter()
3521 {}
3522
3523 Filter::~Filter()
3524 {}
3525
3526 void Filter::SetPredicate( PredicatePtr thePredicate )
3527 {
3528   myPredicate = thePredicate;
3529 }
3530
3531 void Filter::GetElementsId( const SMDS_Mesh* theMesh,
3532                             PredicatePtr     thePredicate,
3533                             TIdSequence&     theSequence )
3534 {
3535   theSequence.clear();
3536
3537   if ( !theMesh || !thePredicate )
3538     return;
3539
3540   thePredicate->SetMesh( theMesh );
3541
3542   SMDS_ElemIteratorPtr elemIt = theMesh->elementsIterator( thePredicate->GetType() );
3543   if ( elemIt ) {
3544     while ( elemIt->more() ) {
3545       const SMDS_MeshElement* anElem = elemIt->next();
3546       long anId = anElem->GetID();
3547       if ( thePredicate->IsSatisfy( anId ) )
3548         theSequence.push_back( anId );
3549     }
3550   }
3551 }
3552
3553 void Filter::GetElementsId( const SMDS_Mesh*     theMesh,
3554                             Filter::TIdSequence& theSequence )
3555 {
3556   GetElementsId(theMesh,myPredicate,theSequence);
3557 }
3558
3559 /*
3560                               ManifoldPart
3561 */
3562
3563 typedef std::set<SMDS_MeshFace*>                    TMapOfFacePtr;
3564
3565 /*
3566    Internal class Link
3567 */
3568
3569 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
3570                           SMDS_MeshNode* theNode2 )
3571 {
3572   myNode1 = theNode1;
3573   myNode2 = theNode2;
3574 }
3575
3576 ManifoldPart::Link::~Link()
3577 {
3578   myNode1 = 0;
3579   myNode2 = 0;
3580 }
3581
3582 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
3583 {
3584   if ( myNode1 == theLink.myNode1 &&
3585        myNode2 == theLink.myNode2 )
3586     return true;
3587   else if ( myNode1 == theLink.myNode2 &&
3588             myNode2 == theLink.myNode1 )
3589     return true;
3590   else
3591     return false;
3592 }
3593
3594 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
3595 {
3596   if(myNode1 < x.myNode1) return true;
3597   if(myNode1 == x.myNode1)
3598     if(myNode2 < x.myNode2) return true;
3599   return false;
3600 }
3601
3602 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
3603                             const ManifoldPart::Link& theLink2 )
3604 {
3605   return theLink1.IsEqual( theLink2 );
3606 }
3607
3608 ManifoldPart::ManifoldPart()
3609 {
3610   myMesh = 0;
3611   myAngToler = Precision::Angular();
3612   myIsOnlyManifold = true;
3613 }
3614
3615 ManifoldPart::~ManifoldPart()
3616 {
3617   myMesh = 0;
3618 }
3619
3620 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
3621 {
3622   myMesh = theMesh;
3623   process();
3624 }
3625
3626 SMDSAbs_ElementType ManifoldPart::GetType() const
3627 { return SMDSAbs_Face; }
3628
3629 bool ManifoldPart::IsSatisfy( long theElementId )
3630 {
3631   return myMapIds.Contains( theElementId );
3632 }
3633
3634 void ManifoldPart::SetAngleTolerance( const double theAngToler )
3635 { myAngToler = theAngToler; }
3636
3637 double ManifoldPart::GetAngleTolerance() const
3638 { return myAngToler; }
3639
3640 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
3641 { myIsOnlyManifold = theIsOnly; }
3642
3643 void ManifoldPart::SetStartElem( const long  theStartId )
3644 { myStartElemId = theStartId; }
3645
3646 bool ManifoldPart::process()
3647 {
3648   myMapIds.Clear();
3649   myMapBadGeomIds.Clear();
3650
3651   myAllFacePtr.clear();
3652   myAllFacePtrIntDMap.clear();
3653   if ( !myMesh )
3654     return false;
3655
3656   // collect all faces into own map
3657   SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
3658   for (; anFaceItr->more(); )
3659   {
3660     SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
3661     myAllFacePtr.push_back( aFacePtr );
3662     myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
3663   }
3664
3665   SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
3666   if ( !aStartFace )
3667     return false;
3668
3669   // the map of non manifold links and bad geometry
3670   TMapOfLink aMapOfNonManifold;
3671   TColStd_MapOfInteger aMapOfTreated;
3672
3673   // begin cycle on faces from start index and run on vector till the end
3674   //  and from begin to start index to cover whole vector
3675   const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
3676   bool isStartTreat = false;
3677   for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
3678   {
3679     if ( fi == aStartIndx )
3680       isStartTreat = true;
3681     // as result next time when fi will be equal to aStartIndx
3682
3683     SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
3684     if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
3685       continue;
3686
3687     aMapOfTreated.Add( aFacePtr->GetID() );
3688     TColStd_MapOfInteger aResFaces;
3689     if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
3690                          aMapOfNonManifold, aResFaces ) )
3691       continue;
3692     TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
3693     for ( ; anItr.More(); anItr.Next() )
3694     {
3695       int aFaceId = anItr.Key();
3696       aMapOfTreated.Add( aFaceId );
3697       myMapIds.Add( aFaceId );
3698     }
3699
3700     if ( fi == int( myAllFacePtr.size() - 1 ))
3701       fi = 0;
3702   } // end run on vector of faces
3703   return !myMapIds.IsEmpty();
3704 }
3705
3706 static void getLinks( const SMDS_MeshFace* theFace,
3707                       ManifoldPart::TVectorOfLink& theLinks )
3708 {
3709   int aNbNode = theFace->NbNodes();
3710   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
3711   int i = 1;
3712   SMDS_MeshNode* aNode = 0;
3713   for ( ; aNodeItr->more() && i <= aNbNode; )
3714   {
3715
3716     SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
3717     if ( i == 1 )
3718       aNode = aN1;
3719     i++;
3720     SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
3721     i++;
3722     ManifoldPart::Link aLink( aN1, aN2 );
3723     theLinks.push_back( aLink );
3724   }
3725 }
3726
3727 bool ManifoldPart::findConnected
3728                  ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
3729                   SMDS_MeshFace*                           theStartFace,
3730                   ManifoldPart::TMapOfLink&                theNonManifold,
3731                   TColStd_MapOfInteger&                    theResFaces )
3732 {
3733   theResFaces.Clear();
3734   if ( !theAllFacePtrInt.size() )
3735     return false;
3736
3737   if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
3738   {
3739     myMapBadGeomIds.Add( theStartFace->GetID() );
3740     return false;
3741   }
3742
3743   ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
3744   ManifoldPart::TVectorOfLink aSeqOfBoundary;
3745   theResFaces.Add( theStartFace->GetID() );
3746   ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
3747
3748   expandBoundary( aMapOfBoundary, aSeqOfBoundary,
3749                  aDMapLinkFace, theNonManifold, theStartFace );
3750
3751   bool isDone = false;
3752   while ( !isDone && aMapOfBoundary.size() != 0 )
3753   {
3754     bool isToReset = false;
3755     ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
3756     for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
3757     {
3758       ManifoldPart::Link aLink = *pLink;
3759       if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
3760         continue;
3761       // each link could be treated only once
3762       aMapToSkip.insert( aLink );
3763
3764       ManifoldPart::TVectorOfFacePtr aFaces;
3765       // find next
3766       if ( myIsOnlyManifold &&
3767            (theNonManifold.find( aLink ) != theNonManifold.end()) )
3768         continue;
3769       else
3770       {
3771         getFacesByLink( aLink, aFaces );
3772         // filter the element to keep only indicated elements
3773         ManifoldPart::TVectorOfFacePtr aFiltered;
3774         ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
3775         for ( ; pFace != aFaces.end(); ++pFace )
3776         {
3777           SMDS_MeshFace* aFace = *pFace;
3778           if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
3779             aFiltered.push_back( aFace );
3780         }
3781         aFaces = aFiltered;
3782         if ( aFaces.size() < 2 )  // no neihgbour faces
3783           continue;
3784         else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
3785         {
3786           theNonManifold.insert( aLink );
3787           continue;
3788         }
3789       }
3790
3791       // compare normal with normals of neighbor element
3792       SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
3793       ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
3794       for ( ; pFace != aFaces.end(); ++pFace )
3795       {
3796         SMDS_MeshFace* aNextFace = *pFace;
3797         if ( aPrevFace == aNextFace )
3798           continue;
3799         int anNextFaceID = aNextFace->GetID();
3800         if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
3801          // should not be with non manifold restriction. probably bad topology
3802           continue;
3803         // check if face was treated and skipped
3804         if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
3805              !isInPlane( aPrevFace, aNextFace ) )
3806           continue;
3807         // add new element to connected and extend the boundaries.
3808         theResFaces.Add( anNextFaceID );
3809         expandBoundary( aMapOfBoundary, aSeqOfBoundary,
3810                         aDMapLinkFace, theNonManifold, aNextFace );
3811         isToReset = true;
3812       }
3813     }
3814     isDone = !isToReset;
3815   }
3816
3817   return !theResFaces.IsEmpty();
3818 }
3819
3820 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
3821                               const SMDS_MeshFace* theFace2 )
3822 {
3823   gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
3824   gp_XYZ aNorm2XYZ = getNormale( theFace2 );
3825   if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
3826   {
3827     myMapBadGeomIds.Add( theFace2->GetID() );
3828     return false;
3829   }
3830   if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
3831     return true;
3832
3833   return false;
3834 }
3835
3836 void ManifoldPart::expandBoundary
3837                    ( ManifoldPart::TMapOfLink&            theMapOfBoundary,
3838                      ManifoldPart::TVectorOfLink&         theSeqOfBoundary,
3839                      ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
3840                      ManifoldPart::TMapOfLink&            theNonManifold,
3841                      SMDS_MeshFace*                       theNextFace ) const
3842 {
3843   ManifoldPart::TVectorOfLink aLinks;
3844   getLinks( theNextFace, aLinks );
3845   int aNbLink = (int)aLinks.size();
3846   for ( int i = 0; i < aNbLink; i++ )
3847   {
3848     ManifoldPart::Link aLink = aLinks[ i ];
3849     if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
3850       continue;
3851     if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
3852     {
3853       if ( myIsOnlyManifold )
3854       {
3855         // remove from boundary
3856         theMapOfBoundary.erase( aLink );
3857         ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
3858         for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
3859         {
3860           ManifoldPart::Link aBoundLink = *pLink;
3861           if ( aBoundLink.IsEqual( aLink ) )
3862           {
3863             theSeqOfBoundary.erase( pLink );
3864             break;
3865           }
3866         }
3867       }
3868     }
3869     else
3870     {
3871       theMapOfBoundary.insert( aLink );
3872       theSeqOfBoundary.push_back( aLink );
3873       theDMapLinkFacePtr[ aLink ] = theNextFace;
3874     }
3875   }
3876 }
3877
3878 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
3879                                    ManifoldPart::TVectorOfFacePtr& theFaces ) const
3880 {
3881   std::set<SMDS_MeshCell *> aSetOfFaces;
3882   // take all faces that shared first node
3883   SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
3884   for ( ; anItr->more(); )
3885   {
3886     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
3887     if ( !aFace )
3888       continue;
3889     aSetOfFaces.insert( aFace );
3890   }
3891   // take all faces that shared second node
3892   anItr = theLink.myNode2->facesIterator();
3893   // find the common part of two sets
3894   for ( ; anItr->more(); )
3895   {
3896     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
3897     if ( aSetOfFaces.count( aFace ) )
3898       theFaces.push_back( aFace );
3899   }
3900 }
3901
3902 /*
3903   Class       : BelongToMeshGroup
3904   Description : Verify whether a mesh element is included into a mesh group
3905 */
3906 BelongToMeshGroup::BelongToMeshGroup(): myGroup( 0 )
3907 {
3908 }
3909
3910 void BelongToMeshGroup::SetGroup( SMESHDS_GroupBase* g )
3911 {
3912   myGroup = g;
3913 }
3914
3915 void BelongToMeshGroup::SetStoreName( const std::string& sn )
3916 {
3917   myStoreName = sn;
3918 }
3919
3920 void BelongToMeshGroup::SetMesh( const SMDS_Mesh* theMesh )
3921 {
3922   if ( myGroup && myGroup->GetMesh() != theMesh )
3923   {
3924     myGroup = 0;
3925   }
3926   if ( !myGroup && !myStoreName.empty() )
3927   {
3928     if ( const SMESHDS_Mesh* aMesh = dynamic_cast<const SMESHDS_Mesh*>(theMesh))
3929     {
3930       const std::set<SMESHDS_GroupBase*>& grps = aMesh->GetGroups();
3931       std::set<SMESHDS_GroupBase*>::const_iterator g = grps.begin();
3932       for ( ; g != grps.end() && !myGroup; ++g )
3933         if ( *g && myStoreName == (*g)->GetStoreName() )
3934           myGroup = *g;
3935     }
3936   }
3937   if ( myGroup )
3938   {
3939     myGroup->IsEmpty(); // make GroupOnFilter update its predicate
3940   }
3941 }
3942
3943 bool BelongToMeshGroup::IsSatisfy( long theElementId )
3944 {
3945   return myGroup ? myGroup->Contains( theElementId ) : false;
3946 }
3947
3948 SMDSAbs_ElementType BelongToMeshGroup::GetType() const
3949 {
3950   return myGroup ? myGroup->GetType() : SMDSAbs_All;
3951 }
3952
3953 /*
3954   ElementsOnSurface
3955 */
3956
3957 ElementsOnSurface::ElementsOnSurface()
3958 {
3959   myIds.Clear();
3960   myType = SMDSAbs_All;
3961   mySurf.Nullify();
3962   myToler = Precision::Confusion();
3963   myUseBoundaries = false;
3964 }
3965
3966 ElementsOnSurface::~ElementsOnSurface()
3967 {
3968 }
3969
3970 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
3971 {
3972   myMeshModifTracer.SetMesh( theMesh );
3973   if ( myMeshModifTracer.IsMeshModified())
3974     process();
3975 }
3976
3977 bool ElementsOnSurface::IsSatisfy( long theElementId )
3978 {
3979   return myIds.Contains( theElementId );
3980 }
3981
3982 SMDSAbs_ElementType ElementsOnSurface::GetType() const
3983 { return myType; }
3984
3985 void ElementsOnSurface::SetTolerance( const double theToler )
3986 {
3987   if ( myToler != theToler )
3988     myIds.Clear();
3989   myToler = theToler;
3990 }
3991
3992 double ElementsOnSurface::GetTolerance() const
3993 { return myToler; }
3994
3995 void ElementsOnSurface::SetUseBoundaries( bool theUse )
3996 {
3997   if ( myUseBoundaries != theUse ) {
3998     myUseBoundaries = theUse;
3999     SetSurface( mySurf, myType );
4000   }
4001 }
4002
4003 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
4004                                     const SMDSAbs_ElementType theType )
4005 {
4006   myIds.Clear();
4007   myType = theType;
4008   mySurf.Nullify();
4009   if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
4010     return;
4011   mySurf = TopoDS::Face( theShape );
4012   BRepAdaptor_Surface SA( mySurf, myUseBoundaries );
4013   Standard_Real
4014     u1 = SA.FirstUParameter(),
4015     u2 = SA.LastUParameter(),
4016     v1 = SA.FirstVParameter(),
4017     v2 = SA.LastVParameter();
4018   Handle(Geom_Surface) surf = BRep_Tool::Surface( mySurf );
4019   myProjector.Init( surf, u1,u2, v1,v2 );
4020   process();
4021 }
4022
4023 void ElementsOnSurface::process()
4024 {
4025   myIds.Clear();
4026   if ( mySurf.IsNull() )
4027     return;
4028
4029   if ( !myMeshModifTracer.GetMesh() )
4030     return;
4031
4032   myIds.ReSize( myMeshModifTracer.GetMesh()->GetMeshInfo().NbElements( myType ));
4033
4034   SMDS_ElemIteratorPtr anIter = myMeshModifTracer.GetMesh()->elementsIterator( myType );
4035   for(; anIter->more(); )
4036     process( anIter->next() );
4037 }
4038
4039 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
4040 {
4041   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
4042   bool isSatisfy = true;
4043   for ( ; aNodeItr->more(); )
4044   {
4045     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
4046     if ( !isOnSurface( aNode ) )
4047     {
4048       isSatisfy = false;
4049       break;
4050     }
4051   }
4052   if ( isSatisfy )
4053     myIds.Add( theElemPtr->GetID() );
4054 }
4055
4056 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode )
4057 {
4058   if ( mySurf.IsNull() )
4059     return false;
4060
4061   gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
4062   //  double aToler2 = myToler * myToler;
4063 //   if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
4064 //   {
4065 //     gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
4066 //     if ( aPln.SquareDistance( aPnt ) > aToler2 )
4067 //       return false;
4068 //   }
4069 //   else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
4070 //   {
4071 //     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
4072 //     double aRad = aCyl.Radius();
4073 //     gp_Ax3 anAxis = aCyl.Position();
4074 //     gp_XYZ aLoc = aCyl.Location().XYZ();
4075 //     double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
4076 //     double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
4077 //     if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )
4078 //       return false;
4079 //   }
4080 //   else
4081 //     return false;
4082   myProjector.Perform( aPnt );
4083   bool isOn = ( myProjector.IsDone() && myProjector.LowerDistance() <= myToler );
4084
4085   return isOn;
4086 }
4087
4088
4089 /*
4090   ElementsOnShape
4091 */
4092
4093 struct ElementsOnShape::Classifier
4094 {
4095   Classifier(const TopoDS_Shape& s, double tol) { Init(s,tol); }
4096   void Init(const TopoDS_Shape& s, double tol);
4097   bool IsOut(const gp_Pnt& p)        { return myIsChecked = true, (this->*myIsOutFun)( p ); }
4098   TopAbs_ShapeEnum ShapeType() const { return myShape.ShapeType(); }
4099   Bnd_B3d* GetBndBox()               { return & myBox; }
4100   bool& IsChecked()                  { return myIsChecked; }
4101 private:
4102   bool isOutOfSolid (const gp_Pnt& p);
4103   bool isOutOfBox   (const gp_Pnt& p);
4104   bool isOutOfFace  (const gp_Pnt& p);
4105   bool isOutOfEdge  (const gp_Pnt& p);
4106   bool isOutOfVertex(const gp_Pnt& p);
4107   bool isBox        (const TopoDS_Shape& s);
4108
4109   bool (Classifier::* myIsOutFun)(const gp_Pnt& p);
4110   BRepClass3d_SolidClassifier mySolidClfr;
4111   Bnd_B3d                     myBox;
4112   GeomAPI_ProjectPointOnSurf  myProjFace;
4113   GeomAPI_ProjectPointOnCurve myProjEdge;
4114   gp_Pnt                      myVertexXYZ;
4115   TopoDS_Shape                myShape;
4116   double                      myTol;
4117   bool                        myIsChecked;
4118 };
4119
4120 struct ElementsOnShape::OctreeClassifier : public SMESH_Octree
4121 {
4122   OctreeClassifier( const std::vector< ElementsOnShape::Classifier* >& classifiers );
4123   void GetClassifiersAtPoint( const gp_XYZ& p,
4124                               std::vector< ElementsOnShape::Classifier* >& classifiers );
4125 protected:
4126   OctreeClassifier() {}
4127   SMESH_Octree* newChild() const { return new OctreeClassifier; }
4128   void          buildChildrenData();
4129   Bnd_B3d*      buildRootBox();
4130
4131   std::vector< ElementsOnShape::Classifier* > myClassifiers;
4132 };
4133
4134
4135 ElementsOnShape::ElementsOnShape():
4136   myOctree(0),
4137   myType(SMDSAbs_All),
4138   myToler(Precision::Confusion()),
4139   myAllNodesFlag(false)
4140 {
4141 }
4142
4143 ElementsOnShape::~ElementsOnShape()
4144 {
4145   clearClassifiers();
4146 }
4147
4148 SMDSAbs_ElementType ElementsOnShape::GetType() const
4149 {
4150   return myType;
4151 }
4152
4153 void ElementsOnShape::SetTolerance (const double theToler)
4154 {
4155   if (myToler != theToler) {
4156     myToler = theToler;
4157     SetShape(myShape, myType);
4158   }
4159 }
4160
4161 double ElementsOnShape::GetTolerance() const
4162 {
4163   return myToler;
4164 }
4165
4166 void ElementsOnShape::SetAllNodes (bool theAllNodes)
4167 {
4168   myAllNodesFlag = theAllNodes;
4169 }
4170
4171 void ElementsOnShape::SetMesh (const SMDS_Mesh* theMesh)
4172 {
4173   myMeshModifTracer.SetMesh( theMesh );
4174   if ( myMeshModifTracer.IsMeshModified())
4175   {
4176     size_t nbNodes = theMesh ? theMesh->NbNodes() : 0;
4177     if ( myNodeIsChecked.size() == nbNodes )
4178     {
4179       std::fill( myNodeIsChecked.begin(), myNodeIsChecked.end(), false );
4180     }
4181     else
4182     {
4183       SMESHUtils::FreeVector( myNodeIsChecked );
4184       SMESHUtils::FreeVector( myNodeIsOut );
4185       myNodeIsChecked.resize( nbNodes, false );
4186       myNodeIsOut.resize( nbNodes );
4187     }
4188   }
4189 }
4190
4191 bool ElementsOnShape::getNodeIsOut( const SMDS_MeshNode* n, bool& isOut )
4192 {
4193   if ( n->GetID() >= (int) myNodeIsChecked.size() ||
4194        !myNodeIsChecked[ n->GetID() ])
4195     return false;
4196
4197   isOut = myNodeIsOut[ n->GetID() ];
4198   return true;
4199 }
4200
4201 void ElementsOnShape::setNodeIsOut( const SMDS_MeshNode* n, bool  isOut )
4202 {
4203   if ( n->GetID() < (int) myNodeIsChecked.size() )
4204   {
4205     myNodeIsChecked[ n->GetID() ] = true;
4206     myNodeIsOut    [ n->GetID() ] = isOut;
4207   }
4208 }
4209
4210 void ElementsOnShape::SetShape (const TopoDS_Shape&       theShape,
4211                                 const SMDSAbs_ElementType theType)
4212 {
4213   bool shapeChanges = ( myShape != theShape );
4214   myType  = theType;
4215   myShape = theShape;
4216   if ( myShape.IsNull() ) return;
4217
4218   if ( shapeChanges )
4219   {
4220     TopTools_IndexedMapOfShape shapesMap;
4221     TopAbs_ShapeEnum shapeTypes[4] = { TopAbs_SOLID, TopAbs_FACE, TopAbs_EDGE, TopAbs_VERTEX };
4222     TopExp_Explorer sub;
4223     for ( int i = 0; i < 4; ++i )
4224     {
4225       if ( shapesMap.IsEmpty() )
4226         for ( sub.Init( myShape, shapeTypes[i] ); sub.More(); sub.Next() )
4227           shapesMap.Add( sub.Current() );
4228       if ( i > 0 )
4229         for ( sub.Init( myShape, shapeTypes[i], shapeTypes[i-1] ); sub.More(); sub.Next() )
4230           shapesMap.Add( sub.Current() );
4231     }
4232
4233     clearClassifiers();
4234     myClassifiers.resize( shapesMap.Extent() );
4235     for ( int i = 0; i < shapesMap.Extent(); ++i )
4236       myClassifiers[ i ] = new Classifier( shapesMap( i+1 ), myToler );
4237   }
4238
4239   if ( theType == SMDSAbs_Node )
4240   {
4241     SMESHUtils::FreeVector( myNodeIsChecked );
4242     SMESHUtils::FreeVector( myNodeIsOut );
4243   }
4244   else
4245   {
4246     std::fill( myNodeIsChecked.begin(), myNodeIsChecked.end(), false );
4247   }
4248 }
4249
4250 void ElementsOnShape::clearClassifiers()
4251 {
4252   for ( size_t i = 0; i < myClassifiers.size(); ++i )
4253     delete myClassifiers[ i ];
4254   myClassifiers.clear();
4255
4256   delete myOctree;
4257   myOctree = 0;
4258 }
4259
4260 bool ElementsOnShape::IsSatisfy (long elemId)
4261 {
4262   const SMDS_Mesh*        mesh = myMeshModifTracer.GetMesh();
4263   const SMDS_MeshElement* elem =
4264     ( myType == SMDSAbs_Node ? mesh->FindNode( elemId ) : mesh->FindElement( elemId ));
4265   if ( !elem || myClassifiers.empty() )
4266     return false;
4267
4268   bool isSatisfy = myAllNodesFlag, isNodeOut;
4269
4270   gp_XYZ centerXYZ (0, 0, 0);
4271
4272   if ( !myOctree && myClassifiers.size() > 5 )
4273     myOctree = new OctreeClassifier( myClassifiers );
4274
4275   std::vector< Classifier* >& classifiers = myOctree ? myWorkClassifiers : myClassifiers;
4276
4277   SMDS_ElemIteratorPtr aNodeItr = elem->nodesIterator();
4278   while (aNodeItr->more() && (isSatisfy == myAllNodesFlag))
4279   {
4280     SMESH_TNodeXYZ aPnt( aNodeItr->next() );
4281     centerXYZ += aPnt;
4282
4283     isNodeOut = true;
4284     if ( !getNodeIsOut( aPnt._node, isNodeOut ))
4285     {
4286       if ( myOctree )
4287       {
4288         myWorkClassifiers.clear();
4289         myOctree->GetClassifiersAtPoint( aPnt, myWorkClassifiers );
4290       }
4291       for ( size_t i = 0; i < classifiers.size(); ++i )
4292         classifiers[i]->IsChecked() = false;
4293
4294       for ( size_t i = 0; i < classifiers.size() && isNodeOut; ++i )
4295         if ( !classifiers[i]->IsChecked() )
4296           isNodeOut = classifiers[i]->IsOut( aPnt );
4297
4298       setNodeIsOut( aPnt._node, isNodeOut );
4299     }
4300     isSatisfy = !isNodeOut;
4301   }
4302
4303   // Check the center point for volumes MantisBug 0020168
4304   if (isSatisfy &&
4305       myAllNodesFlag &&
4306       myClassifiers[0]->ShapeType() == TopAbs_SOLID)
4307   {
4308     centerXYZ /= elem->NbNodes();
4309     isSatisfy = false;
4310     for ( size_t i = 0; i < classifiers.size() && !isSatisfy; ++i )
4311       isSatisfy = ! classifiers[i]->IsOut( centerXYZ );
4312   }
4313
4314   return isSatisfy;
4315 }
4316
4317 void ElementsOnShape::Classifier::Init (const TopoDS_Shape& theShape, double theTol)
4318 {
4319   myShape = theShape;
4320   myTol   = theTol;
4321
4322   bool isShapeBox = false;
4323   switch ( myShape.ShapeType() )
4324   {
4325   case TopAbs_SOLID:
4326   {
4327     if (( isShapeBox = isBox( theShape )))
4328     {
4329       myIsOutFun = & ElementsOnShape::Classifier::isOutOfBox;
4330     }
4331     else
4332     {
4333       mySolidClfr.Load(theShape);
4334       myIsOutFun = & ElementsOnShape::Classifier::isOutOfSolid;
4335     }
4336     break;
4337   }
4338   case TopAbs_FACE:
4339   {
4340     Standard_Real u1,u2,v1,v2;
4341     Handle(Geom_Surface) surf = BRep_Tool::Surface( TopoDS::Face( theShape ));
4342     surf->Bounds( u1,u2,v1,v2 );
4343     myProjFace.Init(surf, u1,u2, v1,v2, myTol );
4344     myIsOutFun = & ElementsOnShape::Classifier::isOutOfFace;
4345     break;
4346   }
4347   case TopAbs_EDGE:
4348   {
4349     Standard_Real u1, u2;
4350     Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( theShape ), u1, u2);
4351     myProjEdge.Init(curve, u1, u2);
4352     myIsOutFun = & ElementsOnShape::Classifier::isOutOfEdge;
4353     break;
4354   }
4355   case TopAbs_VERTEX:
4356   {
4357     myVertexXYZ = BRep_Tool::Pnt( TopoDS::Vertex( theShape ) );
4358     myIsOutFun = & ElementsOnShape::Classifier::isOutOfVertex;
4359     break;
4360   }
4361   default:
4362     throw SALOME_Exception("Programmer error in usage of ElementsOnShape::Classifier");
4363   }
4364
4365   if ( !isShapeBox )
4366   {
4367     Bnd_Box box;
4368     BRepBndLib::Add( myShape, box );
4369     myBox.Clear();
4370     myBox.Add( box.CornerMin() );
4371     myBox.Add( box.CornerMax() );
4372     gp_XYZ halfSize = 0.5 * ( box.CornerMax().XYZ() - box.CornerMin().XYZ() );
4373     for ( int iDim = 1; iDim <= 3; ++iDim )
4374     {
4375       double x = halfSize.Coord( iDim );
4376       halfSize.SetCoord( iDim, x + Max( myTol, 1e-2 * x ));
4377     }
4378     myBox.SetHSize( halfSize );
4379   }
4380 }
4381
4382 bool ElementsOnShape::Classifier::isOutOfSolid (const gp_Pnt& p)
4383 {
4384   mySolidClfr.Perform( p, myTol );
4385   return ( mySolidClfr.State() != TopAbs_IN && mySolidClfr.State() != TopAbs_ON );
4386 }
4387
4388 bool ElementsOnShape::Classifier::isOutOfBox (const gp_Pnt& p)
4389 {
4390   return myBox.IsOut( p.XYZ() );
4391 }
4392
4393 bool ElementsOnShape::Classifier::isOutOfFace  (const gp_Pnt& p)
4394 {
4395   myProjFace.Perform( p );
4396   if ( myProjFace.IsDone() && myProjFace.LowerDistance() <= myTol )
4397   {
4398     // check relatively to the face
4399     Quantity_Parameter u, v;
4400     myProjFace.LowerDistanceParameters(u, v);
4401     gp_Pnt2d aProjPnt (u, v);
4402     BRepClass_FaceClassifier aClsf ( TopoDS::Face( myShape ), aProjPnt, myTol );
4403     if ( aClsf.State() == TopAbs_IN || aClsf.State() == TopAbs_ON )
4404       return false;
4405   }
4406   return true;
4407 }
4408
4409 bool ElementsOnShape::Classifier::isOutOfEdge  (const gp_Pnt& p)
4410 {
4411   myProjEdge.Perform( p );
4412   return ! ( myProjEdge.NbPoints() > 0 && myProjEdge.LowerDistance() <= myTol );
4413 }
4414
4415 bool ElementsOnShape::Classifier::isOutOfVertex(const gp_Pnt& p)
4416 {
4417   return ( myVertexXYZ.Distance( p ) > myTol );
4418 }
4419
4420 bool ElementsOnShape::Classifier::isBox (const TopoDS_Shape& theShape)
4421 {
4422   TopTools_IndexedMapOfShape vMap;
4423   TopExp::MapShapes( theShape, TopAbs_VERTEX, vMap );
4424   if ( vMap.Extent() != 8 )
4425     return false;
4426
4427   myBox.Clear();
4428   for ( int i = 1; i <= 8; ++i )
4429     myBox.Add( BRep_Tool::Pnt( TopoDS::Vertex( vMap( i ))).XYZ() );
4430
4431   gp_XYZ pMin = myBox.CornerMin(), pMax = myBox.CornerMax();
4432   for ( int i = 1; i <= 8; ++i )
4433   {
4434     gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( vMap( i )));
4435     for ( int iC = 1; iC <= 3; ++ iC )
4436     {
4437       double d1 = Abs( pMin.Coord( iC ) - p.Coord( iC ));
4438       double d2 = Abs( pMax.Coord( iC ) - p.Coord( iC ));
4439       if ( Min( d1, d2 ) > myTol )
4440         return false;
4441     }
4442   }
4443   myBox.Enlarge( myTol );
4444   return true;
4445 }
4446
4447 ElementsOnShape::
4448 OctreeClassifier::OctreeClassifier( const std::vector< ElementsOnShape::Classifier* >& classifiers )
4449   :SMESH_Octree( new SMESH_TreeLimit )
4450 {
4451   myClassifiers = classifiers;
4452   compute();
4453 }
4454
4455 void ElementsOnShape::
4456 OctreeClassifier::GetClassifiersAtPoint( const gp_XYZ& point,
4457                                          std::vector< ElementsOnShape::Classifier* >& result )
4458 {
4459   if ( getBox()->IsOut( point ))
4460     return;
4461
4462   if ( isLeaf() )
4463   {
4464     for ( size_t i = 0; i < myClassifiers.size(); ++i )
4465       if ( !myClassifiers[i]->GetBndBox()->IsOut( point ))
4466         result.push_back( myClassifiers[i] );
4467   }
4468   else
4469   {
4470     for (int i = 0; i < nbChildren(); i++)
4471       ((OctreeClassifier*) myChildren[i])->GetClassifiersAtPoint( point, result );
4472   }
4473 }
4474
4475 void ElementsOnShape::OctreeClassifier::buildChildrenData()
4476 {
4477   // distribute myClassifiers among myChildren
4478   for ( size_t i = 0; i < myClassifiers.size(); ++i )
4479   {
4480     for (int j = 0; j < nbChildren(); j++)
4481     {
4482       if ( !myClassifiers[i]->GetBndBox()->IsOut( *myChildren[j]->getBox() ))
4483       {
4484         ((OctreeClassifier*)myChildren[j])->myClassifiers.push_back( myClassifiers[i]);
4485       }
4486     }
4487   }
4488   SMESHUtils::FreeVector( myClassifiers );
4489
4490   // define if a child isLeaf()
4491   for (int i = 0; i < nbChildren(); i++)
4492   {
4493     OctreeClassifier* child = static_cast<OctreeClassifier*>( myChildren[i]);
4494     child->myIsLeaf = ( child->myClassifiers.size() <= 5  );
4495
4496     if ( child->myClassifiers.capacity() - child->myClassifiers.size() > 100 )
4497       SMESHUtils::CompactVector( child->myClassifiers );
4498   }
4499 }
4500
4501 Bnd_B3d* ElementsOnShape::OctreeClassifier::buildRootBox()
4502 {
4503   Bnd_B3d* box = new Bnd_B3d;
4504   for ( size_t i = 0; i < myClassifiers.size(); ++i )
4505     box->Add( *myClassifiers[i]->GetBndBox() );
4506   return box;
4507 }
4508
4509 /*
4510   Class       : BelongToGeom
4511   Description : Predicate for verifying whether entity belongs to
4512                 specified geometrical support
4513 */
4514
4515 BelongToGeom::BelongToGeom()
4516   : myMeshDS(NULL),
4517     myType(SMDSAbs_All),
4518     myIsSubshape(false),
4519     myTolerance(Precision::Confusion())
4520 {}
4521
4522 void BelongToGeom::SetMesh( const SMDS_Mesh* theMesh )
4523 {
4524   myMeshDS = dynamic_cast<const SMESHDS_Mesh*>(theMesh);
4525   init();
4526 }
4527
4528 void BelongToGeom::SetGeom( const TopoDS_Shape& theShape )
4529 {
4530   myShape = theShape;
4531   init();
4532 }
4533
4534 static bool IsSubShape (const TopTools_IndexedMapOfShape& theMap,
4535                         const TopoDS_Shape& theShape)
4536 {
4537   if (theMap.Contains(theShape)) return true;
4538
4539   if (theShape.ShapeType() == TopAbs_COMPOUND ||
4540       theShape.ShapeType() == TopAbs_COMPSOLID)
4541   {
4542     TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
4543     for (; anIt.More(); anIt.Next())
4544     {
4545       if (!IsSubShape(theMap, anIt.Value())) {
4546         return false;
4547       }
4548     }
4549     return true;
4550   }
4551
4552   return false;
4553 }
4554
4555 void BelongToGeom::init()
4556 {
4557   if (!myMeshDS || myShape.IsNull()) return;
4558
4559   // is sub-shape of main shape?
4560   TopoDS_Shape aMainShape = myMeshDS->ShapeToMesh();
4561   if (aMainShape.IsNull()) {
4562     myIsSubshape = false;
4563   }
4564   else {
4565     TopTools_IndexedMapOfShape aMap;
4566     TopExp::MapShapes(aMainShape, aMap);
4567     myIsSubshape = IsSubShape(aMap, myShape);
4568   }
4569
4570   //if (!myIsSubshape) // to be always ready to check an element not bound to geometry
4571   {
4572     if ( !myElementsOnShapePtr )
4573       myElementsOnShapePtr.reset( new ElementsOnShape() );
4574     myElementsOnShapePtr->SetTolerance( myTolerance );
4575     myElementsOnShapePtr->SetAllNodes( true ); // "belong", while false means "lays on"
4576     myElementsOnShapePtr->SetMesh( myMeshDS );
4577     myElementsOnShapePtr->SetShape( myShape, myType );
4578   }
4579 }
4580
4581 static bool IsContains( const SMESHDS_Mesh*     theMeshDS,
4582                         const TopoDS_Shape&     theShape,
4583                         const SMDS_MeshElement* theElem,
4584                         TopAbs_ShapeEnum        theFindShapeEnum,
4585                         TopAbs_ShapeEnum        theAvoidShapeEnum = TopAbs_SHAPE )
4586 {
4587   TopExp_Explorer anExp( theShape,theFindShapeEnum,theAvoidShapeEnum );
4588
4589   while( anExp.More() )
4590   {
4591     const TopoDS_Shape& aShape = anExp.Current();
4592     if( SMESHDS_SubMesh* aSubMesh = theMeshDS->MeshElements( aShape ) ){
4593       if( aSubMesh->Contains( theElem ) )
4594         return true;
4595     }
4596     anExp.Next();
4597   }
4598   return false;
4599 }
4600
4601 bool BelongToGeom::IsSatisfy (long theId)
4602 {
4603   if (myMeshDS == 0 || myShape.IsNull())
4604     return false;
4605
4606   if (!myIsSubshape)
4607   {
4608     return myElementsOnShapePtr->IsSatisfy(theId);
4609   }
4610
4611   // Case of sub-mesh
4612
4613   if (myType == SMDSAbs_Node)
4614   {
4615     if( const SMDS_MeshNode* aNode = myMeshDS->FindNode( theId ) )
4616     {
4617       if ( aNode->getshapeId() < 1 )
4618         return myElementsOnShapePtr->IsSatisfy(theId);
4619
4620       const SMDS_PositionPtr& aPosition = aNode->GetPosition();
4621       SMDS_TypeOfPosition aTypeOfPosition = aPosition->GetTypeOfPosition();
4622       switch( aTypeOfPosition )
4623       {
4624       case SMDS_TOP_VERTEX : return ( IsContains( myMeshDS,myShape,aNode,TopAbs_VERTEX ));
4625       case SMDS_TOP_EDGE   : return ( IsContains( myMeshDS,myShape,aNode,TopAbs_EDGE ));
4626       case SMDS_TOP_FACE   : return ( IsContains( myMeshDS,myShape,aNode,TopAbs_FACE ));
4627       case SMDS_TOP_3DSPACE: return ( IsContains( myMeshDS,myShape,aNode,TopAbs_SOLID ) ||
4628                                       IsContains( myMeshDS,myShape,aNode,TopAbs_SHELL ));
4629       default:;
4630       }
4631     }
4632   }
4633   else
4634   {
4635     if ( const SMDS_MeshElement* anElem = myMeshDS->FindElement( theId ))
4636     {
4637       if ( anElem->getshapeId() < 1 )
4638         return myElementsOnShapePtr->IsSatisfy(theId);
4639
4640       if( myType == SMDSAbs_All )
4641       {
4642         return ( IsContains( myMeshDS,myShape,anElem,TopAbs_EDGE ) ||
4643                  IsContains( myMeshDS,myShape,anElem,TopAbs_FACE ) ||
4644                  IsContains( myMeshDS,myShape,anElem,TopAbs_SOLID )||
4645                  IsContains( myMeshDS,myShape,anElem,TopAbs_SHELL ));
4646       }
4647       else if( myType == anElem->GetType() )
4648       {
4649         switch( myType )
4650         {
4651         case SMDSAbs_Edge  : return ( IsContains( myMeshDS,myShape,anElem,TopAbs_EDGE ));
4652         case SMDSAbs_Face  : return ( IsContains( myMeshDS,myShape,anElem,TopAbs_FACE ));
4653         case SMDSAbs_Volume: return ( IsContains( myMeshDS,myShape,anElem,TopAbs_SOLID )||
4654                                       IsContains( myMeshDS,myShape,anElem,TopAbs_SHELL ));
4655         default:;
4656         }
4657       }
4658     }
4659   }
4660
4661   return false;
4662 }
4663
4664 void BelongToGeom::SetType (SMDSAbs_ElementType theType)
4665 {
4666   myType = theType;
4667   init();
4668 }
4669
4670 SMDSAbs_ElementType BelongToGeom::GetType() const
4671 {
4672   return myType;
4673 }
4674
4675 TopoDS_Shape BelongToGeom::GetShape()
4676 {
4677   return myShape;
4678 }
4679
4680 const SMESHDS_Mesh* BelongToGeom::GetMeshDS() const
4681 {
4682   return myMeshDS;
4683 }
4684
4685 void BelongToGeom::SetTolerance (double theTolerance)
4686 {
4687   myTolerance = theTolerance;
4688   if (!myIsSubshape)
4689     init();
4690 }
4691
4692 double BelongToGeom::GetTolerance()
4693 {
4694   return myTolerance;
4695 }
4696
4697 /*
4698   Class       : LyingOnGeom
4699   Description : Predicate for verifying whether entiy lying or partially lying on
4700                 specified geometrical support
4701 */
4702
4703 LyingOnGeom::LyingOnGeom()
4704   : myMeshDS(NULL),
4705     myType(SMDSAbs_All),
4706     myIsSubshape(false),
4707     myTolerance(Precision::Confusion())
4708 {}
4709
4710 void LyingOnGeom::SetMesh( const SMDS_Mesh* theMesh )
4711 {
4712   myMeshDS = dynamic_cast<const SMESHDS_Mesh*>(theMesh);
4713   init();
4714 }
4715
4716 void LyingOnGeom::SetGeom( const TopoDS_Shape& theShape )
4717 {
4718   myShape = theShape;
4719   init();
4720 }
4721
4722 void LyingOnGeom::init()
4723 {
4724   if (!myMeshDS || myShape.IsNull()) return;
4725
4726   // is sub-shape of main shape?
4727   TopoDS_Shape aMainShape = myMeshDS->ShapeToMesh();
4728   if (aMainShape.IsNull()) {
4729     myIsSubshape = false;
4730   }
4731   else {
4732     myIsSubshape = myMeshDS->IsGroupOfSubShapes( myShape );
4733   }
4734
4735   if (myIsSubshape)
4736   {
4737     TopTools_IndexedMapOfShape shapes;
4738     TopExp::MapShapes( myShape, shapes );
4739     mySubShapesIDs.Clear();
4740     for ( int i = 1; i <= shapes.Extent(); ++i )
4741     {
4742       int subID = myMeshDS->ShapeToIndex( shapes( i ));
4743       if ( subID > 0 )
4744         mySubShapesIDs.Add( subID );
4745     }
4746   }
4747   // else // to be always ready to check an element not bound to geometry
4748   {
4749     if ( !myElementsOnShapePtr )
4750       myElementsOnShapePtr.reset( new ElementsOnShape() );
4751     myElementsOnShapePtr->SetTolerance( myTolerance );
4752     myElementsOnShapePtr->SetAllNodes( false ); // lays on, while true means "belong"
4753     myElementsOnShapePtr->SetMesh( myMeshDS );
4754     myElementsOnShapePtr->SetShape( myShape, myType );
4755   }
4756 }
4757
4758 bool LyingOnGeom::IsSatisfy( long theId )
4759 {
4760   if ( myMeshDS == 0 || myShape.IsNull() )
4761     return false;
4762
4763   if (!myIsSubshape)
4764   {
4765     return myElementsOnShapePtr->IsSatisfy(theId);
4766   }
4767
4768   // Case of sub-mesh
4769
4770   const SMDS_MeshElement* elem =
4771     ( myType == SMDSAbs_Node ) ? myMeshDS->FindNode( theId ) : myMeshDS->FindElement( theId );
4772
4773   if ( mySubShapesIDs.Contains( elem->getshapeId() ))
4774     return true;
4775
4776   if ( elem->GetType() != SMDSAbs_Node )
4777   {
4778     SMDS_ElemIteratorPtr nodeItr = elem->nodesIterator();
4779     while ( nodeItr->more() )
4780     {
4781       const SMDS_MeshElement* aNode = nodeItr->next();
4782       if ( mySubShapesIDs.Contains( aNode->getshapeId() ))
4783         return true;
4784     }
4785   }
4786
4787   return false;
4788 }
4789
4790 void LyingOnGeom::SetType( SMDSAbs_ElementType theType )
4791 {
4792   myType = theType;
4793   init();
4794 }
4795
4796 SMDSAbs_ElementType LyingOnGeom::GetType() const
4797 {
4798   return myType;
4799 }
4800
4801 TopoDS_Shape LyingOnGeom::GetShape()
4802 {
4803   return myShape;
4804 }
4805
4806 const SMESHDS_Mesh* LyingOnGeom::GetMeshDS() const
4807 {
4808   return myMeshDS;
4809 }
4810
4811 void LyingOnGeom::SetTolerance (double theTolerance)
4812 {
4813   myTolerance = theTolerance;
4814   if (!myIsSubshape)
4815     init();
4816 }
4817
4818 double LyingOnGeom::GetTolerance()
4819 {
4820   return myTolerance;
4821 }
4822
4823 bool LyingOnGeom::Contains( const SMESHDS_Mesh*     theMeshDS,
4824                             const TopoDS_Shape&     theShape,
4825                             const SMDS_MeshElement* theElem,
4826                             TopAbs_ShapeEnum        theFindShapeEnum,
4827                             TopAbs_ShapeEnum        theAvoidShapeEnum )
4828 {
4829   // if (IsContains(theMeshDS, theShape, theElem, theFindShapeEnum, theAvoidShapeEnum))
4830   //   return true;
4831
4832   // TopTools_MapOfShape aSubShapes;
4833   // TopExp_Explorer exp( theShape, theFindShapeEnum, theAvoidShapeEnum );
4834   // for ( ; exp.More(); exp.Next() )
4835   // {
4836   //   const TopoDS_Shape& aShape = exp.Current();
4837   //   if ( !aSubShapes.Add( aShape )) continue;
4838
4839   //   if ( SMESHDS_SubMesh* aSubMesh = theMeshDS->MeshElements( aShape ))
4840   //   {
4841   //     if ( aSubMesh->Contains( theElem ))
4842   //       return true;
4843
4844   //     SMDS_ElemIteratorPtr nodeItr = theElem->nodesIterator();
4845   //     while ( nodeItr->more() )
4846   //     {
4847   //       const SMDS_MeshElement* aNode = nodeItr->next();
4848   //       if ( aSubMesh->Contains( aNode ))
4849   //         return true;
4850   //     }
4851   //   }
4852   // }
4853   return false;
4854 }
4855
4856 TSequenceOfXYZ::TSequenceOfXYZ(): myElem(0)
4857 {}
4858
4859 TSequenceOfXYZ::TSequenceOfXYZ(size_type n) : myArray(n), myElem(0)
4860 {}
4861
4862 TSequenceOfXYZ::TSequenceOfXYZ(size_type n, const gp_XYZ& t) : myArray(n,t), myElem(0)
4863 {}
4864
4865 TSequenceOfXYZ::TSequenceOfXYZ(const TSequenceOfXYZ& theSequenceOfXYZ) : myArray(theSequenceOfXYZ.myArray), myElem(theSequenceOfXYZ.myElem)
4866 {}
4867
4868 template <class InputIterator>
4869 TSequenceOfXYZ::TSequenceOfXYZ(InputIterator theBegin, InputIterator theEnd): myArray(theBegin,theEnd), myElem(0)
4870 {}
4871
4872 TSequenceOfXYZ::~TSequenceOfXYZ()
4873 {}
4874
4875 TSequenceOfXYZ& TSequenceOfXYZ::operator=(const TSequenceOfXYZ& theSequenceOfXYZ)
4876 {
4877   myArray = theSequenceOfXYZ.myArray;
4878   myElem  = theSequenceOfXYZ.myElem;
4879   return *this;
4880 }
4881
4882 gp_XYZ& TSequenceOfXYZ::operator()(size_type n)
4883 {
4884   return myArray[n-1];
4885 }
4886
4887 const gp_XYZ& TSequenceOfXYZ::operator()(size_type n) const
4888 {
4889   return myArray[n-1];
4890 }
4891
4892 void TSequenceOfXYZ::clear()
4893 {
4894   myArray.clear();
4895 }
4896
4897 void TSequenceOfXYZ::reserve(size_type n)
4898 {
4899   myArray.reserve(n);
4900 }
4901
4902 void TSequenceOfXYZ::push_back(const gp_XYZ& v)
4903 {
4904   myArray.push_back(v);
4905 }
4906
4907 TSequenceOfXYZ::size_type TSequenceOfXYZ::size() const
4908 {
4909   return myArray.size();
4910 }
4911
4912 SMDSAbs_EntityType TSequenceOfXYZ::getElementEntity() const
4913 {
4914   return myElem ? myElem->GetEntityType() : SMDSEntity_Last;
4915 }
4916
4917 TMeshModifTracer::TMeshModifTracer():
4918   myMeshModifTime(0), myMesh(0)
4919 {
4920 }
4921 void TMeshModifTracer::SetMesh( const SMDS_Mesh* theMesh )
4922 {
4923   if ( theMesh != myMesh )
4924     myMeshModifTime = 0;
4925   myMesh = theMesh;
4926 }
4927 bool TMeshModifTracer::IsMeshModified()
4928 {
4929   bool modified = false;
4930   if ( myMesh )
4931   {
4932     modified = ( myMeshModifTime != myMesh->GetMTime() );
4933     myMeshModifTime = myMesh->GetMTime();
4934   }
4935   return modified;
4936 }