Salome HOME
0021180: EDF 1772 SMESH: Set of hypothesis in 1D
[modules/smesh.git] / src / Controls / SMESH_Controls.cxx
1 //  Copyright (C) 2007-2010  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.
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 <set>
26 #include <limits>
27
28 #include <BRepAdaptor_Surface.hxx>
29 #include <BRepClass_FaceClassifier.hxx>
30 #include <BRep_Tool.hxx>
31
32 #include <TopAbs.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopoDS_Iterator.hxx>
39
40 #include <Geom_CylindricalSurface.hxx>
41 #include <Geom_Plane.hxx>
42 #include <Geom_Surface.hxx>
43
44 #include <Precision.hxx>
45 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
46 #include <TColStd_MapOfInteger.hxx>
47 #include <TColStd_SequenceOfAsciiString.hxx>
48 #include <TColgp_Array1OfXYZ.hxx>
49
50 #include <gp_Ax3.hxx>
51 #include <gp_Cylinder.hxx>
52 #include <gp_Dir.hxx>
53 #include <gp_Pln.hxx>
54 #include <gp_Pnt.hxx>
55 #include <gp_Vec.hxx>
56 #include <gp_XYZ.hxx>
57
58 #include "SMDS_Mesh.hxx"
59 #include "SMDS_Iterator.hxx"
60 #include "SMDS_MeshElement.hxx"
61 #include "SMDS_MeshNode.hxx"
62 #include "SMDS_VolumeTool.hxx"
63 #include "SMDS_QuadraticFaceOfNodes.hxx"
64 #include "SMDS_QuadraticEdge.hxx"
65
66 #include "SMESHDS_Mesh.hxx"
67 #include "SMESHDS_GroupBase.hxx"
68
69 /*
70                             AUXILIARY METHODS
71 */
72
73 namespace{
74
75   inline gp_XYZ gpXYZ(const SMDS_MeshNode* aNode )
76   {
77     return gp_XYZ(aNode->X(), aNode->Y(), aNode->Z() );
78   }
79
80   inline double getAngle( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
81   {
82     gp_Vec v1( P1 - P2 ), v2( P3 - P2 );
83
84     return v1.Magnitude() < gp::Resolution() ||
85       v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
86   }
87
88   inline double getArea( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
89   {
90     gp_Vec aVec1( P2 - P1 );
91     gp_Vec aVec2( P3 - P1 );
92     return ( aVec1 ^ aVec2 ).Magnitude() * 0.5;
93   }
94
95   inline double getArea( const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3 )
96   {
97     return getArea( P1.XYZ(), P2.XYZ(), P3.XYZ() );
98   }
99
100
101
102   inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
103   {
104     double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
105     return aDist;
106   }
107
108   int getNbMultiConnection( const SMDS_Mesh* theMesh, const int theId )
109   {
110     if ( theMesh == 0 )
111       return 0;
112
113     const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
114     if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge/* || anEdge->NbNodes() != 2 */)
115       return 0;
116
117     // for each pair of nodes in anEdge (there are 2 pairs in a quadratic edge)
118     // count elements containing both nodes of the pair.
119     // Note that there may be such cases for a quadratic edge (a horizontal line):
120     //
121     //  Case 1          Case 2
122     //  |     |      |        |      |
123     //  |     |      |        |      |
124     //  +-----+------+  +-----+------+ 
125     //  |            |  |            |
126     //  |            |  |            |
127     // result sould be 2 in both cases
128     //
129     int aResult0 = 0, aResult1 = 0;
130      // last node, it is a medium one in a quadratic edge
131     const SMDS_MeshNode* aLastNode = anEdge->GetNode( anEdge->NbNodes() - 1 );
132     const SMDS_MeshNode* aNode0 = anEdge->GetNode( 0 );
133     const SMDS_MeshNode* aNode1 = anEdge->GetNode( 1 );
134     if ( aNode1 == aLastNode ) aNode1 = 0;
135
136     SMDS_ElemIteratorPtr anElemIter = aLastNode->GetInverseElementIterator();
137     while( anElemIter->more() ) {
138       const SMDS_MeshElement* anElem = anElemIter->next();
139       if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
140         SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
141         while ( anIter->more() ) {
142           if ( const SMDS_MeshElement* anElemNode = anIter->next() ) {
143             if ( anElemNode == aNode0 ) {
144               aResult0++;
145               if ( !aNode1 ) break; // not a quadratic edge
146             }
147             else if ( anElemNode == aNode1 )
148               aResult1++;
149           }
150         }
151       }
152     }
153     int aResult = std::max ( aResult0, aResult1 );
154
155 //     TColStd_MapOfInteger aMap;
156
157 //     SMDS_ElemIteratorPtr anIter = anEdge->nodesIterator();
158 //     if ( anIter != 0 ) {
159 //       while( anIter->more() ) {
160 //      const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
161 //      if ( aNode == 0 )
162 //        return 0;
163 //      SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
164 //      while( anElemIter->more() ) {
165 //        const SMDS_MeshElement* anElem = anElemIter->next();
166 //        if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
167 //          int anId = anElem->GetID();
168
169 //          if ( anIter->more() )              // i.e. first node
170 //            aMap.Add( anId );
171 //          else if ( aMap.Contains( anId ) )
172 //            aResult++;
173 //        }
174 //      }
175 //       }
176 //     }
177
178     return aResult;
179   }
180
181   gp_XYZ getNormale( const SMDS_MeshFace* theFace, bool* ok=0 )
182   {
183     int aNbNode = theFace->NbNodes();
184
185     gp_XYZ q1 = gpXYZ( theFace->GetNode(1)) - gpXYZ( theFace->GetNode(0));
186     gp_XYZ q2 = gpXYZ( theFace->GetNode(2)) - gpXYZ( theFace->GetNode(0));
187     gp_XYZ n  = q1 ^ q2;
188     if ( aNbNode > 3 ) {
189       gp_XYZ q3 = gpXYZ( theFace->GetNode(3)) - gpXYZ( theFace->GetNode(0));
190       n += q2 ^ q3;
191     }
192     double len = n.Modulus();
193     bool zeroLen = ( len <= numeric_limits<double>::min());
194     if ( !zeroLen )
195       n /= len;
196
197     if (ok) *ok = !zeroLen;
198
199     return n;
200   }
201 }
202
203
204
205 using namespace SMESH::Controls;
206
207 /*
208  *                               FUNCTORS
209  */
210
211 /*
212   Class       : NumericalFunctor
213   Description : Base class for numerical functors
214 */
215 NumericalFunctor::NumericalFunctor():
216   myMesh(NULL)
217 {
218   myPrecision = -1;
219 }
220
221 void NumericalFunctor::SetMesh( const SMDS_Mesh* theMesh )
222 {
223   myMesh = theMesh;
224 }
225
226 bool NumericalFunctor::GetPoints(const int theId,
227                                  TSequenceOfXYZ& theRes ) const
228 {
229   theRes.clear();
230
231   if ( myMesh == 0 )
232     return false;
233
234   return GetPoints( myMesh->FindElement( theId ), theRes );
235 }
236
237 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
238                                  TSequenceOfXYZ&         theRes )
239 {
240   theRes.clear();
241
242   if ( anElem == 0)
243     return false;
244
245   theRes.reserve( anElem->NbNodes() );
246
247   // Get nodes of the element
248   SMDS_ElemIteratorPtr anIter;
249
250   if ( anElem->IsQuadratic() ) {
251     switch ( anElem->GetType() ) {
252     case SMDSAbs_Edge:
253       anIter = dynamic_cast<const SMDS_VtkEdge*>
254         (anElem)->interlacedNodesElemIterator();
255       break;
256     case SMDSAbs_Face:
257       anIter = dynamic_cast<const SMDS_VtkFace*>
258         (anElem)->interlacedNodesElemIterator();
259       break;
260     default:
261       anIter = anElem->nodesIterator();
262       //return false;
263     }
264   }
265   else {
266     anIter = anElem->nodesIterator();
267   }
268
269   if ( anIter ) {
270     while( anIter->more() ) {
271       if ( const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>( anIter->next() ))
272         theRes.push_back( gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
273     }
274   }
275
276   return true;
277 }
278
279 long  NumericalFunctor::GetPrecision() const
280 {
281   return myPrecision;
282 }
283
284 void  NumericalFunctor::SetPrecision( const long thePrecision )
285 {
286   myPrecision = thePrecision;
287 }
288
289 double NumericalFunctor::GetValue( long theId )
290 {
291   myCurrElement = myMesh->FindElement( theId );
292   TSequenceOfXYZ P;
293   if ( GetPoints( theId, P ))
294   {
295     double aVal = GetValue( P );
296     if ( myPrecision >= 0 )
297     {
298       double prec = pow( 10., (double)( myPrecision ) );
299       aVal = floor( aVal * prec + 0.5 ) / prec;
300     }
301     return aVal;
302   }
303
304   return 0.;
305 }
306
307 //================================================================================
308 /*!
309  * \brief Return histogram of functor values
310  *  \param nbIntervals - number of intervals
311  *  \param nbEvents - number of mesh elements having values within i-th interval
312  *  \param funValues - boundaries of intervals
313  *  \param elements - elements to check vulue of; empty list means "of all"
314  *  \param minmax - boundaries of diapason of values to divide into intervals
315  */
316 //================================================================================
317
318 void NumericalFunctor::GetHistogram(int                  nbIntervals,
319                                     std::vector<int>&    nbEvents,
320                                     std::vector<double>& funValues,
321                                     const vector<int>&   elements,
322                                     const double*        minmax)
323 {
324   if ( nbIntervals < 1 ||
325        !myMesh ||
326        !myMesh->GetMeshInfo().NbElements( GetType() ))
327     return;
328   nbEvents.resize( nbIntervals, 0 );
329   funValues.resize( nbIntervals+1 );
330
331   // get all values sorted
332   std::multiset< double > values;
333   if ( elements.empty() )
334   {
335     SMDS_ElemIteratorPtr elemIt = myMesh->elementsIterator(GetType());
336     while ( elemIt->more() )
337       values.insert( GetValue( elemIt->next()->GetID() ));
338   }
339   else
340   {
341     vector<int>::const_iterator id = elements.begin();
342     for ( ; id != elements.end(); ++id )
343       values.insert( GetValue( *id ));
344   }
345
346   if ( minmax )
347   {
348     funValues[0] = minmax[0];
349     funValues[nbIntervals] = minmax[1];
350   }
351   else
352   {
353     funValues[0] = *values.begin();
354     funValues[nbIntervals] = *values.rbegin();
355   }
356   // case nbIntervals == 1
357   if ( nbIntervals == 1 )
358   {
359     nbEvents[0] = values.size();
360     return;
361   }
362   // case of 1 value
363   if (funValues.front() == funValues.back())
364   {
365     nbEvents.resize( 1 );
366     nbEvents[0] = values.size();
367     funValues[1] = funValues.back();
368     funValues.resize( 2 );
369   }
370   // generic case
371   std::multiset< double >::iterator min = values.begin(), max;
372   for ( int i = 0; i < nbIntervals; ++i )
373   {
374     // find end value of i-th interval
375     double r = (i+1) / double( nbIntervals );
376     funValues[i+1] = funValues.front() * (1-r) + funValues.back() * r;
377
378     // count values in the i-th interval if there are any
379     if ( min != values.end() && *min <= funValues[i+1] )
380     {
381       // find the first value out of the interval
382       max = values.upper_bound( funValues[i+1] ); // max is greater than funValues[i+1], or end()
383       nbEvents[i] = std::distance( min, max );
384       min = max;
385     }
386   }
387   // add values larger than minmax[1]
388   nbEvents.back() += std::distance( min, values.end() );
389 }
390
391 //=======================================================================
392 //function : GetValue
393 //purpose  : 
394 //=======================================================================
395
396 double Volume::GetValue( long theElementId )
397 {
398   if ( theElementId && myMesh ) {
399     SMDS_VolumeTool aVolumeTool;
400     if ( aVolumeTool.Set( myMesh->FindElement( theElementId )))
401       return aVolumeTool.GetSize();
402   }
403   return 0;
404 }
405
406 //=======================================================================
407 //function : GetBadRate
408 //purpose  : meaningless as it is not quality control functor
409 //=======================================================================
410
411 double Volume::GetBadRate( double Value, int /*nbNodes*/ ) const
412 {
413   return Value;
414 }
415
416 //=======================================================================
417 //function : GetType
418 //purpose  : 
419 //=======================================================================
420
421 SMDSAbs_ElementType Volume::GetType() const
422 {
423   return SMDSAbs_Volume;
424 }
425
426
427 /*
428   Class       : MaxElementLength2D
429   Description : Functor calculating maximum length of 2D element
430 */
431
432 double MaxElementLength2D::GetValue( long theElementId )
433 {
434   TSequenceOfXYZ P;
435   if( GetPoints( theElementId, P ) ) {
436     double aVal = 0;
437     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
438     SMDSAbs_ElementType aType = aElem->GetType();
439     int len = P.size();
440     switch( aType ) {
441     case SMDSAbs_Face:
442       if( len == 3 ) { // triangles
443         double L1 = getDistance(P( 1 ),P( 2 ));
444         double L2 = getDistance(P( 2 ),P( 3 ));
445         double L3 = getDistance(P( 3 ),P( 1 ));
446         aVal = Max(L1,Max(L2,L3));
447         break;
448       }
449       else if( len == 4 ) { // quadrangles
450         double L1 = getDistance(P( 1 ),P( 2 ));
451         double L2 = getDistance(P( 2 ),P( 3 ));
452         double L3 = getDistance(P( 3 ),P( 4 ));
453         double L4 = getDistance(P( 4 ),P( 1 ));
454         double D1 = getDistance(P( 1 ),P( 3 ));
455         double D2 = getDistance(P( 2 ),P( 4 ));
456         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(D1,D2));
457         break;
458       }
459       else if( len == 6 ) { // quadratic triangles
460         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
461         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
462         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
463         aVal = Max(L1,Max(L2,L3));
464         break;
465       }
466       else if( len == 8 ) { // quadratic quadrangles
467         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
468         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
469         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
470         double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
471         double D1 = getDistance(P( 1 ),P( 5 ));
472         double D2 = getDistance(P( 3 ),P( 7 ));
473         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(D1,D2));
474         break;
475       }
476     }
477
478     if( myPrecision >= 0 )
479     {
480       double prec = pow( 10., (double)myPrecision );
481       aVal = floor( aVal * prec + 0.5 ) / prec;
482     }
483     return aVal;
484   }
485   return 0.;
486 }
487
488 double MaxElementLength2D::GetBadRate( double Value, int /*nbNodes*/ ) const
489 {
490   return Value;
491 }
492
493 SMDSAbs_ElementType MaxElementLength2D::GetType() const
494 {
495   return SMDSAbs_Face;
496 }
497
498 /*
499   Class       : MaxElementLength3D
500   Description : Functor calculating maximum length of 3D element
501 */
502
503 double MaxElementLength3D::GetValue( long theElementId )
504 {
505   TSequenceOfXYZ P;
506   if( GetPoints( theElementId, P ) ) {
507     double aVal = 0;
508     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
509     SMDSAbs_ElementType aType = aElem->GetType();
510     int len = P.size();
511     switch( aType ) {
512     case SMDSAbs_Volume:
513       if( len == 4 ) { // tetras
514         double L1 = getDistance(P( 1 ),P( 2 ));
515         double L2 = getDistance(P( 2 ),P( 3 ));
516         double L3 = getDistance(P( 3 ),P( 1 ));
517         double L4 = getDistance(P( 1 ),P( 4 ));
518         double L5 = getDistance(P( 2 ),P( 4 ));
519         double L6 = getDistance(P( 3 ),P( 4 ));
520         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
521         break;
522       }
523       else if( len == 5 ) { // pyramids
524         double L1 = getDistance(P( 1 ),P( 2 ));
525         double L2 = getDistance(P( 2 ),P( 3 ));
526         double L3 = getDistance(P( 3 ),P( 4 ));
527         double L4 = getDistance(P( 4 ),P( 1 ));
528         double L5 = getDistance(P( 1 ),P( 5 ));
529         double L6 = getDistance(P( 2 ),P( 5 ));
530         double L7 = getDistance(P( 3 ),P( 5 ));
531         double L8 = getDistance(P( 4 ),P( 5 ));
532         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
533         aVal = Max(aVal,Max(L7,L8));
534         break;
535       }
536       else if( len == 6 ) { // pentas
537         double L1 = getDistance(P( 1 ),P( 2 ));
538         double L2 = getDistance(P( 2 ),P( 3 ));
539         double L3 = getDistance(P( 3 ),P( 1 ));
540         double L4 = getDistance(P( 4 ),P( 5 ));
541         double L5 = getDistance(P( 5 ),P( 6 ));
542         double L6 = getDistance(P( 6 ),P( 4 ));
543         double L7 = getDistance(P( 1 ),P( 4 ));
544         double L8 = getDistance(P( 2 ),P( 5 ));
545         double L9 = getDistance(P( 3 ),P( 6 ));
546         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
547         aVal = Max(aVal,Max(Max(L7,L8),L9));
548         break;
549       }
550       else if( len == 8 ) { // hexas
551         double L1 = getDistance(P( 1 ),P( 2 ));
552         double L2 = getDistance(P( 2 ),P( 3 ));
553         double L3 = getDistance(P( 3 ),P( 4 ));
554         double L4 = getDistance(P( 4 ),P( 1 ));
555         double L5 = getDistance(P( 5 ),P( 6 ));
556         double L6 = getDistance(P( 6 ),P( 7 ));
557         double L7 = getDistance(P( 7 ),P( 8 ));
558         double L8 = getDistance(P( 8 ),P( 5 ));
559         double L9 = getDistance(P( 1 ),P( 5 ));
560         double L10= getDistance(P( 2 ),P( 6 ));
561         double L11= getDistance(P( 3 ),P( 7 ));
562         double L12= getDistance(P( 4 ),P( 8 ));
563         double D1 = getDistance(P( 1 ),P( 7 ));
564         double D2 = getDistance(P( 2 ),P( 8 ));
565         double D3 = getDistance(P( 3 ),P( 5 ));
566         double D4 = getDistance(P( 4 ),P( 6 ));
567         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
568         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
569         aVal = Max(aVal,Max(L11,L12));
570         aVal = Max(aVal,Max(Max(D1,D2),Max(D3,D4)));
571         break;
572       }
573       else if( len == 10 ) { // quadratic tetras
574         double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
575         double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
576         double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
577         double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
578         double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
579         double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
580         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
581         break;
582       }
583       else if( len == 13 ) { // quadratic pyramids
584         double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
585         double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
586         double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
587         double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
588         double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
589         double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
590         double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
591         double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
592         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
593         aVal = Max(aVal,Max(L7,L8));
594         break;
595       }
596       else if( len == 15 ) { // quadratic pentas
597         double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
598         double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
599         double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
600         double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
601         double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
602         double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
603         double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
604         double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
605         double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
606         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
607         aVal = Max(aVal,Max(Max(L7,L8),L9));
608         break;
609       }
610       else if( len == 20 ) { // quadratic hexas
611         double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
612         double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
613         double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
614         double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
615         double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
616         double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
617         double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
618         double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
619         double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
620         double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
621         double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
622         double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
623         double D1 = getDistance(P( 1 ),P( 7 ));
624         double D2 = getDistance(P( 2 ),P( 8 ));
625         double D3 = getDistance(P( 3 ),P( 5 ));
626         double D4 = getDistance(P( 4 ),P( 6 ));
627         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
628         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
629         aVal = Max(aVal,Max(L11,L12));
630         aVal = Max(aVal,Max(Max(D1,D2),Max(D3,D4)));
631         break;
632       }
633       else if( len > 1 && aElem->IsPoly() ) { // polys
634         // get the maximum distance between all pairs of nodes
635         for( int i = 1; i <= len; i++ ) {
636           for( int j = 1; j <= len; j++ ) {
637             if( j > i ) { // optimization of the loop
638               double D = getDistance( P(i), P(j) );
639               aVal = Max( aVal, D );
640             }
641           }
642         }
643       }
644     }
645
646     if( myPrecision >= 0 )
647     {
648       double prec = pow( 10., (double)myPrecision );
649       aVal = floor( aVal * prec + 0.5 ) / prec;
650     }
651     return aVal;
652   }
653   return 0.;
654 }
655
656 double MaxElementLength3D::GetBadRate( double Value, int /*nbNodes*/ ) const
657 {
658   return Value;
659 }
660
661 SMDSAbs_ElementType MaxElementLength3D::GetType() const
662 {
663   return SMDSAbs_Volume;
664 }
665
666
667 /*
668   Class       : MinimumAngle
669   Description : Functor for calculation of minimum angle
670 */
671
672 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
673 {
674   double aMin;
675
676   if (P.size() <3)
677     return 0.;
678
679   aMin = getAngle(P( P.size() ), P( 1 ), P( 2 ));
680   aMin = Min(aMin,getAngle(P( P.size()-1 ), P( P.size() ), P( 1 )));
681
682   for (int i=2; i<P.size();i++){
683       double A0 = getAngle( P( i-1 ), P( i ), P( i+1 ) );
684     aMin = Min(aMin,A0);
685   }
686
687   return aMin * 180.0 / PI;
688 }
689
690 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
691 {
692   //const double aBestAngle = PI / nbNodes;
693   const double aBestAngle = 180.0 - ( 360.0 / double(nbNodes) );
694   return ( fabs( aBestAngle - Value ));
695 }
696
697 SMDSAbs_ElementType MinimumAngle::GetType() const
698 {
699   return SMDSAbs_Face;
700 }
701
702
703 /*
704   Class       : AspectRatio
705   Description : Functor for calculating aspect ratio
706 */
707 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
708 {
709   // According to "Mesh quality control" by Nadir Bouhamau referring to
710   // Pascal Jean Frey and Paul-Louis George. Maillages, applications aux elements finis.
711   // Hermes Science publications, Paris 1999 ISBN 2-7462-0024-4
712   // PAL10872
713
714   int nbNodes = P.size();
715
716   if ( nbNodes < 3 )
717     return 0;
718
719   // Compute aspect ratio
720
721   if ( nbNodes == 3 ) {
722     // Compute lengths of the sides
723     std::vector< double > aLen (nbNodes);
724     for ( int i = 0; i < nbNodes - 1; i++ )
725       aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
726     aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
727     // Q = alfa * h * p / S, where
728     //
729     // alfa = sqrt( 3 ) / 6
730     // h - length of the longest edge
731     // p - half perimeter
732     // S - triangle surface
733     const double alfa = sqrt( 3. ) / 6.;
734     double maxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
735     double half_perimeter = ( aLen[0] + aLen[1] + aLen[2] ) / 2.;
736     double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
737     if ( anArea <= Precision::Confusion() )
738       return 0.;
739     return alfa * maxLen * half_perimeter / anArea;
740   }
741   else if ( nbNodes == 6 ) { // quadratic triangles
742     // Compute lengths of the sides
743     std::vector< double > aLen (3);
744     aLen[0] = getDistance( P(1), P(3) );
745     aLen[1] = getDistance( P(3), P(5) );
746     aLen[2] = getDistance( P(5), P(1) );
747     // Q = alfa * h * p / S, where
748     //
749     // alfa = sqrt( 3 ) / 6
750     // h - length of the longest edge
751     // p - half perimeter
752     // S - triangle surface
753     const double alfa = sqrt( 3. ) / 6.;
754     double maxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
755     double half_perimeter = ( aLen[0] + aLen[1] + aLen[2] ) / 2.;
756     double anArea = getArea( P(1), P(3), P(5) );
757     if ( anArea <= Precision::Confusion() )
758       return 0.;
759     return alfa * maxLen * half_perimeter / anArea;
760   }
761   else if( nbNodes == 4 ) { // quadrangle
762     // Compute lengths of the sides
763     std::vector< double > aLen (4);
764     aLen[0] = getDistance( P(1), P(2) );
765     aLen[1] = getDistance( P(2), P(3) );
766     aLen[2] = getDistance( P(3), P(4) );
767     aLen[3] = getDistance( P(4), P(1) );
768     // Compute lengths of the diagonals
769     std::vector< double > aDia (2);
770     aDia[0] = getDistance( P(1), P(3) );
771     aDia[1] = getDistance( P(2), P(4) );
772     // Compute areas of all triangles which can be built
773     // taking three nodes of the quadrangle
774     std::vector< double > anArea (4);
775     anArea[0] = getArea( P(1), P(2), P(3) );
776     anArea[1] = getArea( P(1), P(2), P(4) );
777     anArea[2] = getArea( P(1), P(3), P(4) );
778     anArea[3] = getArea( P(2), P(3), P(4) );
779     // Q = alpha * L * C1 / C2, where
780     //
781     // alpha = sqrt( 1/32 )
782     // L = max( L1, L2, L3, L4, D1, D2 )
783     // C1 = sqrt( ( L1^2 + L1^2 + L1^2 + L1^2 ) / 4 )
784     // C2 = min( S1, S2, S3, S4 )
785     // Li - lengths of the edges
786     // Di - lengths of the diagonals
787     // Si - areas of the triangles
788     const double alpha = sqrt( 1 / 32. );
789     double L = Max( aLen[ 0 ],
790                  Max( aLen[ 1 ],
791                    Max( aLen[ 2 ],
792                      Max( aLen[ 3 ],
793                        Max( aDia[ 0 ], aDia[ 1 ] ) ) ) ) );
794     double C1 = sqrt( ( aLen[0] * aLen[0] +
795                         aLen[1] * aLen[1] +
796                         aLen[2] * aLen[2] +
797                         aLen[3] * aLen[3] ) / 4. );
798     double C2 = Min( anArea[ 0 ],
799                   Min( anArea[ 1 ],
800                     Min( anArea[ 2 ], anArea[ 3 ] ) ) );
801     if ( C2 <= Precision::Confusion() )
802       return 0.;
803     return alpha * L * C1 / C2;
804   }
805   else if( nbNodes == 8 ){ // nbNodes==8 - quadratic quadrangle
806     // Compute lengths of the sides
807     std::vector< double > aLen (4);
808     aLen[0] = getDistance( P(1), P(3) );
809     aLen[1] = getDistance( P(3), P(5) );
810     aLen[2] = getDistance( P(5), P(7) );
811     aLen[3] = getDistance( P(7), P(1) );
812     // Compute lengths of the diagonals
813     std::vector< double > aDia (2);
814     aDia[0] = getDistance( P(1), P(5) );
815     aDia[1] = getDistance( P(3), P(7) );
816     // Compute areas of all triangles which can be built
817     // taking three nodes of the quadrangle
818     std::vector< double > anArea (4);
819     anArea[0] = getArea( P(1), P(3), P(5) );
820     anArea[1] = getArea( P(1), P(3), P(7) );
821     anArea[2] = getArea( P(1), P(5), P(7) );
822     anArea[3] = getArea( P(3), P(5), P(7) );
823     // Q = alpha * L * C1 / C2, where
824     //
825     // alpha = sqrt( 1/32 )
826     // L = max( L1, L2, L3, L4, D1, D2 )
827     // C1 = sqrt( ( L1^2 + L1^2 + L1^2 + L1^2 ) / 4 )
828     // C2 = min( S1, S2, S3, S4 )
829     // Li - lengths of the edges
830     // Di - lengths of the diagonals
831     // Si - areas of the triangles
832     const double alpha = sqrt( 1 / 32. );
833     double L = Max( aLen[ 0 ],
834                  Max( aLen[ 1 ],
835                    Max( aLen[ 2 ],
836                      Max( aLen[ 3 ],
837                        Max( aDia[ 0 ], aDia[ 1 ] ) ) ) ) );
838     double C1 = sqrt( ( aLen[0] * aLen[0] +
839                         aLen[1] * aLen[1] +
840                         aLen[2] * aLen[2] +
841                         aLen[3] * aLen[3] ) / 4. );
842     double C2 = Min( anArea[ 0 ],
843                   Min( anArea[ 1 ],
844                     Min( anArea[ 2 ], anArea[ 3 ] ) ) );
845     if ( C2 <= Precision::Confusion() )
846       return 0.;
847     return alpha * L * C1 / C2;
848   }
849   return 0;
850 }
851
852 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
853 {
854   // the aspect ratio is in the range [1.0,infinity]
855   // 1.0 = good
856   // infinity = bad
857   return Value / 1000.;
858 }
859
860 SMDSAbs_ElementType AspectRatio::GetType() const
861 {
862   return SMDSAbs_Face;
863 }
864
865
866 /*
867   Class       : AspectRatio3D
868   Description : Functor for calculating aspect ratio
869 */
870 namespace{
871
872   inline double getHalfPerimeter(double theTria[3]){
873     return (theTria[0] + theTria[1] + theTria[2])/2.0;
874   }
875
876   inline double getArea(double theHalfPerim, double theTria[3]){
877     return sqrt(theHalfPerim*
878                 (theHalfPerim-theTria[0])*
879                 (theHalfPerim-theTria[1])*
880                 (theHalfPerim-theTria[2]));
881   }
882
883   inline double getVolume(double theLen[6]){
884     double a2 = theLen[0]*theLen[0];
885     double b2 = theLen[1]*theLen[1];
886     double c2 = theLen[2]*theLen[2];
887     double d2 = theLen[3]*theLen[3];
888     double e2 = theLen[4]*theLen[4];
889     double f2 = theLen[5]*theLen[5];
890     double P = 4.0*a2*b2*d2;
891     double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
892     double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
893     return sqrt(P-Q+R)/12.0;
894   }
895
896   inline double getVolume2(double theLen[6]){
897     double a2 = theLen[0]*theLen[0];
898     double b2 = theLen[1]*theLen[1];
899     double c2 = theLen[2]*theLen[2];
900     double d2 = theLen[3]*theLen[3];
901     double e2 = theLen[4]*theLen[4];
902     double f2 = theLen[5]*theLen[5];
903
904     double P = a2*e2*(b2+c2+d2+f2-a2-e2);
905     double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
906     double R = c2*d2*(a2+b2+e2+f2-c2-d2);
907     double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
908
909     return sqrt(P+Q+R-S)/12.0;
910   }
911
912   inline double getVolume(const TSequenceOfXYZ& P){
913     gp_Vec aVec1( P( 2 ) - P( 1 ) );
914     gp_Vec aVec2( P( 3 ) - P( 1 ) );
915     gp_Vec aVec3( P( 4 ) - P( 1 ) );
916     gp_Vec anAreaVec( aVec1 ^ aVec2 );
917     return fabs(aVec3 * anAreaVec) / 6.0;
918   }
919
920   inline double getMaxHeight(double theLen[6])
921   {
922     double aHeight = std::max(theLen[0],theLen[1]);
923     aHeight = std::max(aHeight,theLen[2]);
924     aHeight = std::max(aHeight,theLen[3]);
925     aHeight = std::max(aHeight,theLen[4]);
926     aHeight = std::max(aHeight,theLen[5]);
927     return aHeight;
928   }
929
930 }
931
932 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
933 {
934   double aQuality = 0.0;
935   if(myCurrElement->IsPoly()) return aQuality;
936
937   int nbNodes = P.size();
938
939   if(myCurrElement->IsQuadratic()) {
940     if(nbNodes==10) nbNodes=4; // quadratic tetrahedron
941     else if(nbNodes==13) nbNodes=5; // quadratic pyramid
942     else if(nbNodes==15) nbNodes=6; // quadratic pentahedron
943     else if(nbNodes==20) nbNodes=8; // quadratic hexahedron
944     else return aQuality;
945   }
946
947   switch(nbNodes){
948   case 4:{
949     double aLen[6] = {
950       getDistance(P( 1 ),P( 2 )), // a
951       getDistance(P( 2 ),P( 3 )), // b
952       getDistance(P( 3 ),P( 1 )), // c
953       getDistance(P( 2 ),P( 4 )), // d
954       getDistance(P( 3 ),P( 4 )), // e
955       getDistance(P( 1 ),P( 4 ))  // f
956     };
957     double aTria[4][3] = {
958       {aLen[0],aLen[1],aLen[2]}, // abc
959       {aLen[0],aLen[3],aLen[5]}, // adf
960       {aLen[1],aLen[3],aLen[4]}, // bde
961       {aLen[2],aLen[4],aLen[5]}  // cef
962     };
963     double aSumArea = 0.0;
964     double aHalfPerimeter = getHalfPerimeter(aTria[0]);
965     double anArea = getArea(aHalfPerimeter,aTria[0]);
966     aSumArea += anArea;
967     aHalfPerimeter = getHalfPerimeter(aTria[1]);
968     anArea = getArea(aHalfPerimeter,aTria[1]);
969     aSumArea += anArea;
970     aHalfPerimeter = getHalfPerimeter(aTria[2]);
971     anArea = getArea(aHalfPerimeter,aTria[2]);
972     aSumArea += anArea;
973     aHalfPerimeter = getHalfPerimeter(aTria[3]);
974     anArea = getArea(aHalfPerimeter,aTria[3]);
975     aSumArea += anArea;
976     double aVolume = getVolume(P);
977     //double aVolume = getVolume(aLen);
978     double aHeight = getMaxHeight(aLen);
979     static double aCoeff = sqrt(2.0)/12.0;
980     if ( aVolume > DBL_MIN )
981       aQuality = aCoeff*aHeight*aSumArea/aVolume;
982     break;
983   }
984   case 5:{
985     {
986       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
987       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
988     }
989     {
990       gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
991       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
992     }
993     {
994       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
995       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
996     }
997     {
998       gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
999       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1000     }
1001     break;
1002   }
1003   case 6:{
1004     {
1005       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
1006       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1007     }
1008     {
1009       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
1010       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1011     }
1012     {
1013       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
1014       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1015     }
1016     {
1017       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
1018       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1019     }
1020     {
1021       gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
1022       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1023     }
1024     {
1025       gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
1026       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1027     }
1028     break;
1029   }
1030   case 8:{
1031     {
1032       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
1033       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1034     }
1035     {
1036       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
1037       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1038     }
1039     {
1040       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
1041       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1042     }
1043     {
1044       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
1045       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1046     }
1047     {
1048       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
1049       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1050     }
1051     {
1052       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
1053       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1054     }
1055     {
1056       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
1057       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1058     }
1059     {
1060       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
1061       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1062     }
1063     {
1064       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
1065       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1066     }
1067     {
1068       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
1069       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1070     }
1071     {
1072       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
1073       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1074     }
1075     {
1076       gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
1077       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1078     }
1079     {
1080       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
1081       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1082     }
1083     {
1084       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
1085       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1086     }
1087     {
1088       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
1089       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1090     }
1091     {
1092       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
1093       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1094     }
1095     {
1096       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
1097       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1098     }
1099     {
1100       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
1101       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1102     }
1103     {
1104       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
1105       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1106     }
1107     {
1108       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
1109       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1110     }
1111     {
1112       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
1113       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1114     }
1115     {
1116       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
1117       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1118     }
1119     {
1120       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
1121       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1122     }
1123     {
1124       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
1125       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1126     }
1127     {
1128       gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
1129       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1130     }
1131     {
1132       gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
1133       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1134     }
1135     {
1136       gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
1137       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1138     }
1139     {
1140       gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
1141       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1142     }
1143     {
1144       gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
1145       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1146     }
1147     {
1148       gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
1149       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1150     }
1151     {
1152       gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
1153       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1154     }
1155     {
1156       gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
1157       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1158     }
1159     {
1160       gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
1161       aQuality = std::max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
1162     }
1163     break;
1164   }
1165   }
1166   if ( nbNodes > 4 ) {
1167     // avaluate aspect ratio of quadranle faces
1168     AspectRatio aspect2D;
1169     SMDS_VolumeTool::VolumeType type = SMDS_VolumeTool::GetType( nbNodes );
1170     int nbFaces = SMDS_VolumeTool::NbFaces( type );
1171     TSequenceOfXYZ points(4);
1172     for ( int i = 0; i < nbFaces; ++i ) { // loop on faces of a volume
1173       if ( SMDS_VolumeTool::NbFaceNodes( type, i ) != 4 )
1174         continue;
1175       const int* pInd = SMDS_VolumeTool::GetFaceNodesIndices( type, i, true );
1176       for ( int p = 0; p < 4; ++p ) // loop on nodes of a quadranle face
1177         points( p + 1 ) = P( pInd[ p ] + 1 );
1178       aQuality = std::max( aQuality, aspect2D.GetValue( points ));
1179     }
1180   }
1181   return aQuality;
1182 }
1183
1184 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
1185 {
1186   // the aspect ratio is in the range [1.0,infinity]
1187   // 1.0 = good
1188   // infinity = bad
1189   return Value / 1000.;
1190 }
1191
1192 SMDSAbs_ElementType AspectRatio3D::GetType() const
1193 {
1194   return SMDSAbs_Volume;
1195 }
1196
1197
1198 /*
1199   Class       : Warping
1200   Description : Functor for calculating warping
1201 */
1202 double Warping::GetValue( const TSequenceOfXYZ& P )
1203 {
1204   if ( P.size() != 4 )
1205     return 0;
1206
1207   gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4.;
1208
1209   double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
1210   double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
1211   double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
1212   double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
1213
1214   return Max( Max( A1, A2 ), Max( A3, A4 ) );
1215 }
1216
1217 double Warping::ComputeA( const gp_XYZ& thePnt1,
1218                           const gp_XYZ& thePnt2,
1219                           const gp_XYZ& thePnt3,
1220                           const gp_XYZ& theG ) const
1221 {
1222   double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
1223   double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
1224   double L = Min( aLen1, aLen2 ) * 0.5;
1225   if ( L < Precision::Confusion())
1226     return 0.;
1227
1228   gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
1229   gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
1230   gp_XYZ N  = GI.Crossed( GJ );
1231
1232   if ( N.Modulus() < gp::Resolution() )
1233     return PI / 2;
1234
1235   N.Normalize();
1236
1237   double H = ( thePnt2 - theG ).Dot( N );
1238   return asin( fabs( H / L ) ) * 180. / PI;
1239 }
1240
1241 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
1242 {
1243   // the warp is in the range [0.0,PI/2]
1244   // 0.0 = good (no warp)
1245   // PI/2 = bad  (face pliee)
1246   return Value;
1247 }
1248
1249 SMDSAbs_ElementType Warping::GetType() const
1250 {
1251   return SMDSAbs_Face;
1252 }
1253
1254
1255 /*
1256   Class       : Taper
1257   Description : Functor for calculating taper
1258 */
1259 double Taper::GetValue( const TSequenceOfXYZ& P )
1260 {
1261   if ( P.size() != 4 )
1262     return 0.;
1263
1264   // Compute taper
1265   double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2.;
1266   double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2.;
1267   double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2.;
1268   double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2.;
1269
1270   double JA = 0.25 * ( J1 + J2 + J3 + J4 );
1271   if ( JA <= Precision::Confusion() )
1272     return 0.;
1273
1274   double T1 = fabs( ( J1 - JA ) / JA );
1275   double T2 = fabs( ( J2 - JA ) / JA );
1276   double T3 = fabs( ( J3 - JA ) / JA );
1277   double T4 = fabs( ( J4 - JA ) / JA );
1278
1279   return Max( Max( T1, T2 ), Max( T3, T4 ) );
1280 }
1281
1282 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
1283 {
1284   // the taper is in the range [0.0,1.0]
1285   // 0.0  = good (no taper)
1286   // 1.0 = bad  (les cotes opposes sont allignes)
1287   return Value;
1288 }
1289
1290 SMDSAbs_ElementType Taper::GetType() const
1291 {
1292   return SMDSAbs_Face;
1293 }
1294
1295
1296 /*
1297   Class       : Skew
1298   Description : Functor for calculating skew in degrees
1299 */
1300 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
1301 {
1302   gp_XYZ p12 = ( p2 + p1 ) / 2.;
1303   gp_XYZ p23 = ( p3 + p2 ) / 2.;
1304   gp_XYZ p31 = ( p3 + p1 ) / 2.;
1305
1306   gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
1307
1308   return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0. : v1.Angle( v2 );
1309 }
1310
1311 double Skew::GetValue( const TSequenceOfXYZ& P )
1312 {
1313   if ( P.size() != 3 && P.size() != 4 )
1314     return 0.;
1315
1316   // Compute skew
1317   static double PI2 = PI / 2.;
1318   if ( P.size() == 3 )
1319   {
1320     double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
1321     double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
1322     double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
1323
1324     return Max( A0, Max( A1, A2 ) ) * 180. / PI;
1325   }
1326   else
1327   {
1328     gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2.;
1329     gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2.;
1330     gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2.;
1331     gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2.;
1332
1333     gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
1334     double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
1335       ? 0. : fabs( PI2 - v1.Angle( v2 ) );
1336
1337     //BUG SWP12743
1338     if ( A < Precision::Angular() )
1339       return 0.;
1340
1341     return A * 180. / PI;
1342   }
1343 }
1344
1345 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
1346 {
1347   // the skew is in the range [0.0,PI/2].
1348   // 0.0 = good
1349   // PI/2 = bad
1350   return Value;
1351 }
1352
1353 SMDSAbs_ElementType Skew::GetType() const
1354 {
1355   return SMDSAbs_Face;
1356 }
1357
1358
1359 /*
1360   Class       : Area
1361   Description : Functor for calculating area
1362 */
1363 double Area::GetValue( const TSequenceOfXYZ& P )
1364 {
1365   double val = 0.0;
1366   if ( P.size() > 2 ) {
1367     gp_Vec aVec1( P(2) - P(1) );
1368     gp_Vec aVec2( P(3) - P(1) );
1369     gp_Vec SumVec = aVec1 ^ aVec2;
1370     for (int i=4; i<=P.size(); i++) {
1371       gp_Vec aVec1( P(i-1) - P(1) );
1372       gp_Vec aVec2( P(i) - P(1) );
1373       gp_Vec tmp = aVec1 ^ aVec2;
1374       SumVec.Add(tmp);
1375     }
1376     val = SumVec.Magnitude() * 0.5;
1377   }
1378   return val;
1379 }
1380
1381 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
1382 {
1383   // meaningless as it is not a quality control functor
1384   return Value;
1385 }
1386
1387 SMDSAbs_ElementType Area::GetType() const
1388 {
1389   return SMDSAbs_Face;
1390 }
1391
1392
1393 /*
1394   Class       : Length
1395   Description : Functor for calculating length off edge
1396 */
1397 double Length::GetValue( const TSequenceOfXYZ& P )
1398 {
1399   switch ( P.size() ) {
1400   case 2:  return getDistance( P( 1 ), P( 2 ) );
1401   case 3:  return getDistance( P( 1 ), P( 2 ) ) + getDistance( P( 2 ), P( 3 ) );
1402   default: return 0.;
1403   }
1404 }
1405
1406 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
1407 {
1408   // meaningless as it is not quality control functor
1409   return Value;
1410 }
1411
1412 SMDSAbs_ElementType Length::GetType() const
1413 {
1414   return SMDSAbs_Edge;
1415 }
1416
1417 /*
1418   Class       : Length2D
1419   Description : Functor for calculating length of edge
1420 */
1421
1422 double Length2D::GetValue( long theElementId)
1423 {
1424   TSequenceOfXYZ P;
1425
1426   //cout<<"Length2D::GetValue"<<endl;
1427   if (GetPoints(theElementId,P)){
1428     //for(int jj=1; jj<=P.size(); jj++)
1429     //  cout<<"jj="<<jj<<" P("<<P(jj).X()<<","<<P(jj).Y()<<","<<P(jj).Z()<<")"<<endl;
1430
1431     double  aVal;// = GetValue( P );
1432     const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
1433     SMDSAbs_ElementType aType = aElem->GetType();
1434
1435     int len = P.size();
1436
1437     switch (aType){
1438     case SMDSAbs_All:
1439     case SMDSAbs_Node:
1440     case SMDSAbs_Edge:
1441       if (len == 2){
1442         aVal = getDistance( P( 1 ), P( 2 ) );
1443         break;
1444       }
1445       else if (len == 3){ // quadratic edge
1446         aVal = getDistance(P( 1 ),P( 3 )) + getDistance(P( 3 ),P( 2 ));
1447         break;
1448       }
1449     case SMDSAbs_Face:
1450       if (len == 3){ // triangles
1451         double L1 = getDistance(P( 1 ),P( 2 ));
1452         double L2 = getDistance(P( 2 ),P( 3 ));
1453         double L3 = getDistance(P( 3 ),P( 1 ));
1454         aVal = Max(L1,Max(L2,L3));
1455         break;
1456       }
1457       else if (len == 4){ // quadrangles
1458         double L1 = getDistance(P( 1 ),P( 2 ));
1459         double L2 = getDistance(P( 2 ),P( 3 ));
1460         double L3 = getDistance(P( 3 ),P( 4 ));
1461         double L4 = getDistance(P( 4 ),P( 1 ));
1462         aVal = Max(Max(L1,L2),Max(L3,L4));
1463         break;
1464       }
1465       if (len == 6){ // quadratic triangles
1466         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1467         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1468         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 1 ));
1469         aVal = Max(L1,Max(L2,L3));
1470         //cout<<"L1="<<L1<<" L2="<<L2<<"L3="<<L3<<" aVal="<<aVal<<endl;
1471         break;
1472       }
1473       else if (len == 8){ // quadratic quadrangles
1474         double L1 = getDistance(P( 1 ),P( 2 )) + getDistance(P( 2 ),P( 3 ));
1475         double L2 = getDistance(P( 3 ),P( 4 )) + getDistance(P( 4 ),P( 5 ));
1476         double L3 = getDistance(P( 5 ),P( 6 )) + getDistance(P( 6 ),P( 7 ));
1477         double L4 = getDistance(P( 7 ),P( 8 )) + getDistance(P( 8 ),P( 1 ));
1478         aVal = Max(Max(L1,L2),Max(L3,L4));
1479         break;
1480       }
1481     case SMDSAbs_Volume:
1482       if (len == 4){ // tetraidrs
1483         double L1 = getDistance(P( 1 ),P( 2 ));
1484         double L2 = getDistance(P( 2 ),P( 3 ));
1485         double L3 = getDistance(P( 3 ),P( 1 ));
1486         double L4 = getDistance(P( 1 ),P( 4 ));
1487         double L5 = getDistance(P( 2 ),P( 4 ));
1488         double L6 = getDistance(P( 3 ),P( 4 ));
1489         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1490         break;
1491       }
1492       else if (len == 5){ // piramids
1493         double L1 = getDistance(P( 1 ),P( 2 ));
1494         double L2 = getDistance(P( 2 ),P( 3 ));
1495         double L3 = getDistance(P( 3 ),P( 4 ));
1496         double L4 = getDistance(P( 4 ),P( 1 ));
1497         double L5 = getDistance(P( 1 ),P( 5 ));
1498         double L6 = getDistance(P( 2 ),P( 5 ));
1499         double L7 = getDistance(P( 3 ),P( 5 ));
1500         double L8 = getDistance(P( 4 ),P( 5 ));
1501
1502         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1503         aVal = Max(aVal,Max(L7,L8));
1504         break;
1505       }
1506       else if (len == 6){ // pentaidres
1507         double L1 = getDistance(P( 1 ),P( 2 ));
1508         double L2 = getDistance(P( 2 ),P( 3 ));
1509         double L3 = getDistance(P( 3 ),P( 1 ));
1510         double L4 = getDistance(P( 4 ),P( 5 ));
1511         double L5 = getDistance(P( 5 ),P( 6 ));
1512         double L6 = getDistance(P( 6 ),P( 4 ));
1513         double L7 = getDistance(P( 1 ),P( 4 ));
1514         double L8 = getDistance(P( 2 ),P( 5 ));
1515         double L9 = getDistance(P( 3 ),P( 6 ));
1516
1517         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1518         aVal = Max(aVal,Max(Max(L7,L8),L9));
1519         break;
1520       }
1521       else if (len == 8){ // hexaider
1522         double L1 = getDistance(P( 1 ),P( 2 ));
1523         double L2 = getDistance(P( 2 ),P( 3 ));
1524         double L3 = getDistance(P( 3 ),P( 4 ));
1525         double L4 = getDistance(P( 4 ),P( 1 ));
1526         double L5 = getDistance(P( 5 ),P( 6 ));
1527         double L6 = getDistance(P( 6 ),P( 7 ));
1528         double L7 = getDistance(P( 7 ),P( 8 ));
1529         double L8 = getDistance(P( 8 ),P( 5 ));
1530         double L9 = getDistance(P( 1 ),P( 5 ));
1531         double L10= getDistance(P( 2 ),P( 6 ));
1532         double L11= getDistance(P( 3 ),P( 7 ));
1533         double L12= getDistance(P( 4 ),P( 8 ));
1534
1535         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1536         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1537         aVal = Max(aVal,Max(L11,L12));
1538         break;
1539
1540       }
1541
1542       if (len == 10){ // quadratic tetraidrs
1543         double L1 = getDistance(P( 1 ),P( 5 )) + getDistance(P( 5 ),P( 2 ));
1544         double L2 = getDistance(P( 2 ),P( 6 )) + getDistance(P( 6 ),P( 3 ));
1545         double L3 = getDistance(P( 3 ),P( 7 )) + getDistance(P( 7 ),P( 1 ));
1546         double L4 = getDistance(P( 1 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1547         double L5 = getDistance(P( 2 ),P( 9 )) + getDistance(P( 9 ),P( 4 ));
1548         double L6 = getDistance(P( 3 ),P( 10 )) + getDistance(P( 10 ),P( 4 ));
1549         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1550         break;
1551       }
1552       else if (len == 13){ // quadratic piramids
1553         double L1 = getDistance(P( 1 ),P( 6 )) + getDistance(P( 6 ),P( 2 ));
1554         double L2 = getDistance(P( 2 ),P( 7 )) + getDistance(P( 7 ),P( 3 ));
1555         double L3 = getDistance(P( 3 ),P( 8 )) + getDistance(P( 8 ),P( 4 ));
1556         double L4 = getDistance(P( 4 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1557         double L5 = getDistance(P( 1 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1558         double L6 = getDistance(P( 2 ),P( 11 )) + getDistance(P( 11 ),P( 5 ));
1559         double L7 = getDistance(P( 3 ),P( 12 )) + getDistance(P( 12 ),P( 5 ));
1560         double L8 = getDistance(P( 4 ),P( 13 )) + getDistance(P( 13 ),P( 5 ));
1561         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1562         aVal = Max(aVal,Max(L7,L8));
1563         break;
1564       }
1565       else if (len == 15){ // quadratic pentaidres
1566         double L1 = getDistance(P( 1 ),P( 7 )) + getDistance(P( 7 ),P( 2 ));
1567         double L2 = getDistance(P( 2 ),P( 8 )) + getDistance(P( 8 ),P( 3 ));
1568         double L3 = getDistance(P( 3 ),P( 9 )) + getDistance(P( 9 ),P( 1 ));
1569         double L4 = getDistance(P( 4 ),P( 10 )) + getDistance(P( 10 ),P( 5 ));
1570         double L5 = getDistance(P( 5 ),P( 11 )) + getDistance(P( 11 ),P( 6 ));
1571         double L6 = getDistance(P( 6 ),P( 12 )) + getDistance(P( 12 ),P( 4 ));
1572         double L7 = getDistance(P( 1 ),P( 13 )) + getDistance(P( 13 ),P( 4 ));
1573         double L8 = getDistance(P( 2 ),P( 14 )) + getDistance(P( 14 ),P( 5 ));
1574         double L9 = getDistance(P( 3 ),P( 15 )) + getDistance(P( 15 ),P( 6 ));
1575         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1576         aVal = Max(aVal,Max(Max(L7,L8),L9));
1577         break;
1578       }
1579       else if (len == 20){ // quadratic hexaider
1580         double L1 = getDistance(P( 1 ),P( 9 )) + getDistance(P( 9 ),P( 2 ));
1581         double L2 = getDistance(P( 2 ),P( 10 )) + getDistance(P( 10 ),P( 3 ));
1582         double L3 = getDistance(P( 3 ),P( 11 )) + getDistance(P( 11 ),P( 4 ));
1583         double L4 = getDistance(P( 4 ),P( 12 )) + getDistance(P( 12 ),P( 1 ));
1584         double L5 = getDistance(P( 5 ),P( 13 )) + getDistance(P( 13 ),P( 6 ));
1585         double L6 = getDistance(P( 6 ),P( 14 )) + getDistance(P( 14 ),P( 7 ));
1586         double L7 = getDistance(P( 7 ),P( 15 )) + getDistance(P( 15 ),P( 8 ));
1587         double L8 = getDistance(P( 8 ),P( 16 )) + getDistance(P( 16 ),P( 5 ));
1588         double L9 = getDistance(P( 1 ),P( 17 )) + getDistance(P( 17 ),P( 5 ));
1589         double L10= getDistance(P( 2 ),P( 18 )) + getDistance(P( 18 ),P( 6 ));
1590         double L11= getDistance(P( 3 ),P( 19 )) + getDistance(P( 19 ),P( 7 ));
1591         double L12= getDistance(P( 4 ),P( 20 )) + getDistance(P( 20 ),P( 8 ));
1592         aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
1593         aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
1594         aVal = Max(aVal,Max(L11,L12));
1595         break;
1596
1597       }
1598
1599     default: aVal=-1;
1600     }
1601
1602     if (aVal <0){
1603       return 0.;
1604     }
1605
1606     if ( myPrecision >= 0 )
1607     {
1608       double prec = pow( 10., (double)( myPrecision ) );
1609       aVal = floor( aVal * prec + 0.5 ) / prec;
1610     }
1611
1612     return aVal;
1613
1614   }
1615   return 0.;
1616 }
1617
1618 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1619 {
1620   // meaningless as it is not quality control functor
1621   return Value;
1622 }
1623
1624 SMDSAbs_ElementType Length2D::GetType() const
1625 {
1626   return SMDSAbs_Face;
1627 }
1628
1629 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
1630   myLength(theLength)
1631 {
1632   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1633   if(thePntId1 > thePntId2){
1634     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1635   }
1636 }
1637
1638 bool Length2D::Value::operator<(const Length2D::Value& x) const{
1639   if(myPntId[0] < x.myPntId[0]) return true;
1640   if(myPntId[0] == x.myPntId[0])
1641     if(myPntId[1] < x.myPntId[1]) return true;
1642   return false;
1643 }
1644
1645 void Length2D::GetValues(TValues& theValues){
1646   TValues aValues;
1647   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1648   for(; anIter->more(); ){
1649     const SMDS_MeshFace* anElem = anIter->next();
1650
1651     if(anElem->IsQuadratic()) {
1652       const SMDS_VtkFace* F =
1653         dynamic_cast<const SMDS_VtkFace*>(anElem);
1654       // use special nodes iterator
1655       SMDS_ElemIteratorPtr anIter = F->interlacedNodesElemIterator();
1656       long aNodeId[4];
1657       gp_Pnt P[4];
1658
1659       double aLength;
1660       const SMDS_MeshElement* aNode;
1661       if(anIter->more()){
1662         aNode = anIter->next();
1663         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1664         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1665         aNodeId[0] = aNodeId[1] = aNode->GetID();
1666         aLength = 0;
1667       }
1668       for(; anIter->more(); ){
1669         const SMDS_MeshNode* N1 = static_cast<const SMDS_MeshNode*> (anIter->next());
1670         P[2] = gp_Pnt(N1->X(),N1->Y(),N1->Z());
1671         aNodeId[2] = N1->GetID();
1672         aLength = P[1].Distance(P[2]);
1673         if(!anIter->more()) break;
1674         const SMDS_MeshNode* N2 = static_cast<const SMDS_MeshNode*> (anIter->next());
1675         P[3] = gp_Pnt(N2->X(),N2->Y(),N2->Z());
1676         aNodeId[3] = N2->GetID();
1677         aLength += P[2].Distance(P[3]);
1678         Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1679         Value aValue2(aLength,aNodeId[2],aNodeId[3]);
1680         P[1] = P[3];
1681         aNodeId[1] = aNodeId[3];
1682         theValues.insert(aValue1);
1683         theValues.insert(aValue2);
1684       }
1685       aLength += P[2].Distance(P[0]);
1686       Value aValue1(aLength,aNodeId[1],aNodeId[2]);
1687       Value aValue2(aLength,aNodeId[2],aNodeId[0]);
1688       theValues.insert(aValue1);
1689       theValues.insert(aValue2);
1690     }
1691     else {
1692       SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1693       long aNodeId[2];
1694       gp_Pnt P[3];
1695
1696       double aLength;
1697       const SMDS_MeshElement* aNode;
1698       if(aNodesIter->more()){
1699         aNode = aNodesIter->next();
1700         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1701         P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1702         aNodeId[0] = aNodeId[1] = aNode->GetID();
1703         aLength = 0;
1704       }
1705       for(; aNodesIter->more(); ){
1706         aNode = aNodesIter->next();
1707         const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1708         long anId = aNode->GetID();
1709         
1710         P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1711         
1712         aLength = P[1].Distance(P[2]);
1713         
1714         Value aValue(aLength,aNodeId[1],anId);
1715         aNodeId[1] = anId;
1716         P[1] = P[2];
1717         theValues.insert(aValue);
1718       }
1719
1720       aLength = P[0].Distance(P[1]);
1721
1722       Value aValue(aLength,aNodeId[0],aNodeId[1]);
1723       theValues.insert(aValue);
1724     }
1725   }
1726 }
1727
1728 /*
1729   Class       : MultiConnection
1730   Description : Functor for calculating number of faces conneted to the edge
1731 */
1732 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1733 {
1734   return 0;
1735 }
1736 double MultiConnection::GetValue( long theId )
1737 {
1738   return getNbMultiConnection( myMesh, theId );
1739 }
1740
1741 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1742 {
1743   // meaningless as it is not quality control functor
1744   return Value;
1745 }
1746
1747 SMDSAbs_ElementType MultiConnection::GetType() const
1748 {
1749   return SMDSAbs_Edge;
1750 }
1751
1752 /*
1753   Class       : MultiConnection2D
1754   Description : Functor for calculating number of faces conneted to the edge
1755 */
1756 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1757 {
1758   return 0;
1759 }
1760
1761 double MultiConnection2D::GetValue( long theElementId )
1762 {
1763   int aResult = 0;
1764
1765   const SMDS_MeshElement* aFaceElem = myMesh->FindElement(theElementId);
1766   SMDSAbs_ElementType aType = aFaceElem->GetType();
1767
1768   switch (aType) {
1769   case SMDSAbs_Face:
1770     {
1771       int i = 0, len = aFaceElem->NbNodes();
1772       SMDS_ElemIteratorPtr anIter = aFaceElem->nodesIterator();
1773       if (!anIter) break;
1774
1775       const SMDS_MeshNode *aNode, *aNode0;
1776       TColStd_MapOfInteger aMap, aMapPrev;
1777
1778       for (i = 0; i <= len; i++) {
1779         aMapPrev = aMap;
1780         aMap.Clear();
1781
1782         int aNb = 0;
1783         if (anIter->more()) {
1784           aNode = (SMDS_MeshNode*)anIter->next();
1785         } else {
1786           if (i == len)
1787             aNode = aNode0;
1788           else
1789             break;
1790         }
1791         if (!aNode) break;
1792         if (i == 0) aNode0 = aNode;
1793
1794         SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1795         while (anElemIter->more()) {
1796           const SMDS_MeshElement* anElem = anElemIter->next();
1797           if (anElem != 0 && anElem->GetType() == SMDSAbs_Face) {
1798             int anId = anElem->GetID();
1799
1800             aMap.Add(anId);
1801             if (aMapPrev.Contains(anId)) {
1802               aNb++;
1803             }
1804           }
1805         }
1806         aResult = Max(aResult, aNb);
1807       }
1808     }
1809     break;
1810   default:
1811     aResult = 0;
1812   }
1813
1814   return aResult;
1815 }
1816
1817 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1818 {
1819   // meaningless as it is not quality control functor
1820   return Value;
1821 }
1822
1823 SMDSAbs_ElementType MultiConnection2D::GetType() const
1824 {
1825   return SMDSAbs_Face;
1826 }
1827
1828 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1829 {
1830   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
1831   if(thePntId1 > thePntId2){
1832     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
1833   }
1834 }
1835
1836 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1837   if(myPntId[0] < x.myPntId[0]) return true;
1838   if(myPntId[0] == x.myPntId[0])
1839     if(myPntId[1] < x.myPntId[1]) return true;
1840   return false;
1841 }
1842
1843 void MultiConnection2D::GetValues(MValues& theValues){
1844   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1845   for(; anIter->more(); ){
1846     const SMDS_MeshFace* anElem = anIter->next();
1847     SMDS_ElemIteratorPtr aNodesIter;
1848     if ( anElem->IsQuadratic() )
1849       aNodesIter = dynamic_cast<const SMDS_VtkFace*>
1850         (anElem)->interlacedNodesElemIterator();
1851     else
1852       aNodesIter = anElem->nodesIterator();
1853     long aNodeId[3];
1854
1855     //int aNbConnects=0;
1856     const SMDS_MeshNode* aNode0;
1857     const SMDS_MeshNode* aNode1;
1858     const SMDS_MeshNode* aNode2;
1859     if(aNodesIter->more()){
1860       aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1861       aNode1 = aNode0;
1862       const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1863       aNodeId[0] = aNodeId[1] = aNodes->GetID();
1864     }
1865     for(; aNodesIter->more(); ) {
1866       aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1867       long anId = aNode2->GetID();
1868       aNodeId[2] = anId;
1869
1870       Value aValue(aNodeId[1],aNodeId[2]);
1871       MValues::iterator aItr = theValues.find(aValue);
1872       if (aItr != theValues.end()){
1873         aItr->second += 1;
1874         //aNbConnects = nb;
1875       }
1876       else {
1877         theValues[aValue] = 1;
1878         //aNbConnects = 1;
1879       }
1880       //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1881       aNodeId[1] = aNodeId[2];
1882       aNode1 = aNode2;
1883     }
1884     Value aValue(aNodeId[0],aNodeId[2]);
1885     MValues::iterator aItr = theValues.find(aValue);
1886     if (aItr != theValues.end()) {
1887       aItr->second += 1;
1888       //aNbConnects = nb;
1889     }
1890     else {
1891       theValues[aValue] = 1;
1892       //aNbConnects = 1;
1893     }
1894     //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1895   }
1896
1897 }
1898
1899 /*
1900                             PREDICATES
1901 */
1902
1903 /*
1904   Class       : BadOrientedVolume
1905   Description : Predicate bad oriented volumes
1906 */
1907
1908 BadOrientedVolume::BadOrientedVolume()
1909 {
1910   myMesh = 0;
1911 }
1912
1913 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1914 {
1915   myMesh = theMesh;
1916 }
1917
1918 bool BadOrientedVolume::IsSatisfy( long theId )
1919 {
1920   if ( myMesh == 0 )
1921     return false;
1922
1923   SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1924   return !vTool.IsForward();
1925 }
1926
1927 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1928 {
1929   return SMDSAbs_Volume;
1930 }
1931
1932 /*
1933   Class       : BareBorderVolume
1934 */
1935
1936 bool BareBorderVolume::IsSatisfy(long theElementId )
1937 {
1938   SMDS_VolumeTool  myTool;
1939   if ( myTool.Set( myMesh->FindElement(theElementId)))
1940   {
1941     for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
1942       if ( myTool.IsFreeFace( iF ))
1943       {
1944         const SMDS_MeshNode** n = myTool.GetFaceNodes(iF);
1945         vector< const SMDS_MeshNode*> nodes( n, n+myTool.NbFaceNodes(iF));
1946         if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
1947           return true;
1948       }
1949   }
1950   return false;
1951 }
1952
1953 /*
1954   Class       : BareBorderFace
1955 */
1956
1957 bool BareBorderFace::IsSatisfy(long theElementId )
1958 {
1959   bool ok = false;
1960   if ( const SMDS_MeshElement* face = myMesh->FindElement(theElementId))
1961   {
1962     if ( face->GetType() == SMDSAbs_Face )
1963     {
1964       int nbN = face->NbCornerNodes();
1965       for ( int i = 0; i < nbN && !ok; ++i )
1966       {
1967         // check if a link is shared by another face
1968         const SMDS_MeshNode* n1 = face->GetNode( i );
1969         const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbN );
1970         SMDS_ElemIteratorPtr fIt = n1->GetInverseElementIterator( SMDSAbs_Face );
1971         bool isShared = false;
1972         while ( !isShared && fIt->more() )
1973         {
1974           const SMDS_MeshElement* f = fIt->next();
1975           isShared = ( f != face && f->GetNodeIndex(n2) != -1 );
1976         }
1977         if ( !isShared )
1978         {
1979           myLinkNodes.resize( 2 + face->IsQuadratic());
1980           myLinkNodes[0] = n1;
1981           myLinkNodes[1] = n2;
1982           if ( face->IsQuadratic() )
1983             myLinkNodes[2] = face->GetNode( i+nbN );
1984           ok = !myMesh->FindElement( myLinkNodes, SMDSAbs_Edge, /*noMedium=*/false);
1985         }
1986       }
1987     }
1988   }
1989   return ok;
1990 }
1991
1992 /*
1993   Class       : OverConstrainedVolume
1994 */
1995
1996 bool OverConstrainedVolume::IsSatisfy(long theElementId )
1997 {
1998   // An element is over-constrained if it has N-1 free borders where
1999   // N is the number of edges/faces for a 2D/3D element.
2000   SMDS_VolumeTool  myTool;
2001   if ( myTool.Set( myMesh->FindElement(theElementId)))
2002   {
2003     int nbSharedFaces = 0;
2004     for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
2005       if ( !myTool.IsFreeFace( iF ) && ++nbSharedFaces > 1 )
2006         break;
2007     return ( nbSharedFaces == 1 );
2008   }
2009   return false;
2010 }
2011
2012 /*
2013   Class       : OverConstrainedFace
2014 */
2015
2016 bool OverConstrainedFace::IsSatisfy(long theElementId )
2017 {
2018   // An element is over-constrained if it has N-1 free borders where
2019   // N is the number of edges/faces for a 2D/3D element.
2020   if ( const SMDS_MeshElement* face = myMesh->FindElement(theElementId))
2021     if ( face->GetType() == SMDSAbs_Face )
2022     {
2023       int nbSharedBorders = 0;
2024       int nbN = face->NbCornerNodes();
2025       for ( int i = 0; i < nbN; ++i )
2026       {
2027         // check if a link is shared by another face
2028         const SMDS_MeshNode* n1 = face->GetNode( i );
2029         const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbN );
2030         SMDS_ElemIteratorPtr fIt = n1->GetInverseElementIterator( SMDSAbs_Face );
2031         bool isShared = false;
2032         while ( !isShared && fIt->more() )
2033         {
2034           const SMDS_MeshElement* f = fIt->next();
2035           isShared = ( f != face && f->GetNodeIndex(n2) != -1 );
2036         }
2037         if ( isShared && ++nbSharedBorders > 1 )
2038           break;
2039       }
2040       return ( nbSharedBorders == 1 );
2041     }
2042   return false;
2043 }
2044
2045 /*
2046   Class       : FreeBorders
2047   Description : Predicate for free borders
2048 */
2049
2050 FreeBorders::FreeBorders()
2051 {
2052   myMesh = 0;
2053 }
2054
2055 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
2056 {
2057   myMesh = theMesh;
2058 }
2059
2060 bool FreeBorders::IsSatisfy( long theId )
2061 {
2062   return getNbMultiConnection( myMesh, theId ) == 1;
2063 }
2064
2065 SMDSAbs_ElementType FreeBorders::GetType() const
2066 {
2067   return SMDSAbs_Edge;
2068 }
2069
2070
2071 /*
2072   Class       : FreeEdges
2073   Description : Predicate for free Edges
2074 */
2075 FreeEdges::FreeEdges()
2076 {
2077   myMesh = 0;
2078 }
2079
2080 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
2081 {
2082   myMesh = theMesh;
2083 }
2084
2085 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  )
2086 {
2087   TColStd_MapOfInteger aMap;
2088   for ( int i = 0; i < 2; i++ )
2089   {
2090     SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
2091     while( anElemIter->more() )
2092     {
2093       const SMDS_MeshElement* anElem = anElemIter->next();
2094       if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
2095       {
2096         int anId = anElem->GetID();
2097
2098         if ( i == 0 )
2099           aMap.Add( anId );
2100         else if ( aMap.Contains( anId ) && anId != theFaceId )
2101           return false;
2102       }
2103     }
2104   }
2105   return true;
2106 }
2107
2108 bool FreeEdges::IsSatisfy( long theId )
2109 {
2110   if ( myMesh == 0 )
2111     return false;
2112
2113   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
2114   if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
2115     return false;
2116
2117   SMDS_ElemIteratorPtr anIter;
2118   if ( aFace->IsQuadratic() ) {
2119     anIter = dynamic_cast<const SMDS_VtkFace*>
2120       (aFace)->interlacedNodesElemIterator();
2121   }
2122   else {
2123     anIter = aFace->nodesIterator();
2124   }
2125   if ( anIter == 0 )
2126     return false;
2127
2128   int i = 0, nbNodes = aFace->NbNodes();
2129   std::vector <const SMDS_MeshNode*> aNodes( nbNodes+1 );
2130   while( anIter->more() )
2131   {
2132     const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
2133     if ( aNode == 0 )
2134       return false;
2135     aNodes[ i++ ] = aNode;
2136   }
2137   aNodes[ nbNodes ] = aNodes[ 0 ];
2138
2139   for ( i = 0; i < nbNodes; i++ )
2140     if ( IsFreeEdge( &aNodes[ i ], theId ) )
2141       return true;
2142
2143   return false;
2144 }
2145
2146 SMDSAbs_ElementType FreeEdges::GetType() const
2147 {
2148   return SMDSAbs_Face;
2149 }
2150
2151 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
2152   myElemId(theElemId)
2153 {
2154   myPntId[0] = thePntId1;  myPntId[1] = thePntId2;
2155   if(thePntId1 > thePntId2){
2156     myPntId[1] = thePntId1;  myPntId[0] = thePntId2;
2157   }
2158 }
2159
2160 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
2161   if(myPntId[0] < x.myPntId[0]) return true;
2162   if(myPntId[0] == x.myPntId[0])
2163     if(myPntId[1] < x.myPntId[1]) return true;
2164   return false;
2165 }
2166
2167 inline void UpdateBorders(const FreeEdges::Border& theBorder,
2168                           FreeEdges::TBorders& theRegistry,
2169                           FreeEdges::TBorders& theContainer)
2170 {
2171   if(theRegistry.find(theBorder) == theRegistry.end()){
2172     theRegistry.insert(theBorder);
2173     theContainer.insert(theBorder);
2174   }else{
2175     theContainer.erase(theBorder);
2176   }
2177 }
2178
2179 void FreeEdges::GetBoreders(TBorders& theBorders)
2180 {
2181   TBorders aRegistry;
2182   SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2183   for(; anIter->more(); ){
2184     const SMDS_MeshFace* anElem = anIter->next();
2185     long anElemId = anElem->GetID();
2186     SMDS_ElemIteratorPtr aNodesIter;
2187     if ( anElem->IsQuadratic() )
2188       aNodesIter = static_cast<const SMDS_VtkFace*>(anElem)->
2189         interlacedNodesElemIterator();
2190     else
2191       aNodesIter = anElem->nodesIterator();
2192     long aNodeId[2];
2193     const SMDS_MeshElement* aNode;
2194     if(aNodesIter->more()){
2195       aNode = aNodesIter->next();
2196       aNodeId[0] = aNodeId[1] = aNode->GetID();
2197     }
2198     for(; aNodesIter->more(); ){
2199       aNode = aNodesIter->next();
2200       long anId = aNode->GetID();
2201       Border aBorder(anElemId,aNodeId[1],anId);
2202       aNodeId[1] = anId;
2203       //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
2204       UpdateBorders(aBorder,aRegistry,theBorders);
2205     }
2206     Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
2207     //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
2208     UpdateBorders(aBorder,aRegistry,theBorders);
2209   }
2210   //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
2211 }
2212
2213
2214 /*
2215   Class       : FreeNodes
2216   Description : Predicate for free nodes
2217 */
2218
2219 FreeNodes::FreeNodes()
2220 {
2221   myMesh = 0;
2222 }
2223
2224 void FreeNodes::SetMesh( const SMDS_Mesh* theMesh )
2225 {
2226   myMesh = theMesh;
2227 }
2228
2229 bool FreeNodes::IsSatisfy( long theNodeId )
2230 {
2231   const SMDS_MeshNode* aNode = myMesh->FindNode( theNodeId );
2232   if (!aNode)
2233     return false;
2234
2235   return (aNode->NbInverseElements() < 1);
2236 }
2237
2238 SMDSAbs_ElementType FreeNodes::GetType() const
2239 {
2240   return SMDSAbs_Node;
2241 }
2242
2243
2244 /*
2245   Class       : FreeFaces
2246   Description : Predicate for free faces
2247 */
2248
2249 FreeFaces::FreeFaces()
2250 {
2251   myMesh = 0;
2252 }
2253
2254 void FreeFaces::SetMesh( const SMDS_Mesh* theMesh )
2255 {
2256   myMesh = theMesh;
2257 }
2258
2259 bool FreeFaces::IsSatisfy( long theId )
2260 {
2261   if (!myMesh) return false;
2262   // check that faces nodes refers to less than two common volumes
2263   const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
2264   if ( !aFace || aFace->GetType() != SMDSAbs_Face )
2265     return false;
2266
2267   int nbNode = aFace->NbNodes();
2268
2269   // collect volumes check that number of volumss with count equal nbNode not less than 2
2270   typedef map< SMDS_MeshElement*, int > TMapOfVolume; // map of volume counters
2271   typedef map< SMDS_MeshElement*, int >::iterator TItrMapOfVolume; // iterator
2272   TMapOfVolume mapOfVol;
2273
2274   SMDS_ElemIteratorPtr nodeItr = aFace->nodesIterator();
2275   while ( nodeItr->more() ) {
2276     const SMDS_MeshNode* aNode = static_cast<const SMDS_MeshNode*>(nodeItr->next());
2277     if ( !aNode ) continue;
2278     SMDS_ElemIteratorPtr volItr = aNode->GetInverseElementIterator(SMDSAbs_Volume);
2279     while ( volItr->more() ) {
2280       SMDS_MeshElement* aVol = (SMDS_MeshElement*)volItr->next();
2281       TItrMapOfVolume itr = mapOfVol.insert(make_pair(aVol, 0)).first;
2282       (*itr).second++;
2283     } 
2284   }
2285   int nbVol = 0;
2286   TItrMapOfVolume volItr = mapOfVol.begin();
2287   TItrMapOfVolume volEnd = mapOfVol.end();
2288   for ( ; volItr != volEnd; ++volItr )
2289     if ( (*volItr).second >= nbNode )
2290        nbVol++;
2291   // face is not free if number of volumes constructed on thier nodes more than one
2292   return (nbVol < 2);
2293 }
2294
2295 SMDSAbs_ElementType FreeFaces::GetType() const
2296 {
2297   return SMDSAbs_Face;
2298 }
2299
2300 /*
2301   Class       : LinearOrQuadratic
2302   Description : Predicate to verify whether a mesh element is linear
2303 */
2304
2305 LinearOrQuadratic::LinearOrQuadratic()
2306 {
2307   myMesh = 0;
2308 }
2309
2310 void LinearOrQuadratic::SetMesh( const SMDS_Mesh* theMesh )
2311 {
2312   myMesh = theMesh;
2313 }
2314
2315 bool LinearOrQuadratic::IsSatisfy( long theId )
2316 {
2317   if (!myMesh) return false;
2318   const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
2319   if ( !anElem || (myType != SMDSAbs_All && anElem->GetType() != myType) )
2320     return false;
2321   return (!anElem->IsQuadratic());
2322 }
2323
2324 void LinearOrQuadratic::SetType( SMDSAbs_ElementType theType )
2325 {
2326   myType = theType;
2327 }
2328
2329 SMDSAbs_ElementType LinearOrQuadratic::GetType() const
2330 {
2331   return myType;
2332 }
2333
2334 /*
2335   Class       : GroupColor
2336   Description : Functor for check color of group to whic mesh element belongs to
2337 */
2338
2339 GroupColor::GroupColor()
2340 {
2341 }
2342
2343 bool GroupColor::IsSatisfy( long theId )
2344 {
2345   return (myIDs.find( theId ) != myIDs.end());
2346 }
2347
2348 void GroupColor::SetType( SMDSAbs_ElementType theType )
2349 {
2350   myType = theType;
2351 }
2352
2353 SMDSAbs_ElementType GroupColor::GetType() const
2354 {
2355   return myType;
2356 }
2357
2358 static bool isEqual( const Quantity_Color& theColor1,
2359                      const Quantity_Color& theColor2 )
2360 {
2361   // tolerance to compare colors
2362   const double tol = 5*1e-3;
2363   return ( fabs( theColor1.Red() - theColor2.Red() ) < tol &&
2364            fabs( theColor1.Green() - theColor2.Green() ) < tol &&
2365            fabs( theColor1.Blue() - theColor2.Blue() ) < tol );
2366 }
2367
2368
2369 void GroupColor::SetMesh( const SMDS_Mesh* theMesh )
2370 {
2371   myIDs.clear();
2372   
2373   const SMESHDS_Mesh* aMesh = dynamic_cast<const SMESHDS_Mesh*>(theMesh);
2374   if ( !aMesh )
2375     return;
2376
2377   int nbGrp = aMesh->GetNbGroups();
2378   if ( !nbGrp )
2379     return;
2380   
2381   // iterates on groups and find necessary elements ids
2382   const std::set<SMESHDS_GroupBase*>& aGroups = aMesh->GetGroups();
2383   set<SMESHDS_GroupBase*>::const_iterator GrIt = aGroups.begin();
2384   for (; GrIt != aGroups.end(); GrIt++) {
2385     SMESHDS_GroupBase* aGrp = (*GrIt);
2386     if ( !aGrp )
2387       continue;
2388     // check type and color of group
2389     if ( !isEqual( myColor, aGrp->GetColor() ) )
2390       continue;
2391     if ( myType != SMDSAbs_All && myType != (SMDSAbs_ElementType)aGrp->GetType() )
2392       continue;
2393
2394     SMDSAbs_ElementType aGrpElType = (SMDSAbs_ElementType)aGrp->GetType();
2395     if ( myType == aGrpElType || (myType == SMDSAbs_All && aGrpElType != SMDSAbs_Node) ) {
2396       // add elements IDS into control
2397       int aSize = aGrp->Extent();
2398       for (int i = 0; i < aSize; i++)
2399         myIDs.insert( aGrp->GetID(i+1) );
2400     }
2401   }
2402 }
2403
2404 void GroupColor::SetColorStr( const TCollection_AsciiString& theStr )
2405 {
2406   TCollection_AsciiString aStr = theStr;
2407   aStr.RemoveAll( ' ' );
2408   aStr.RemoveAll( '\t' );
2409   for ( int aPos = aStr.Search( ";;" ); aPos != -1; aPos = aStr.Search( ";;" ) )
2410     aStr.Remove( aPos, 2 );
2411   Standard_Real clr[3];
2412   clr[0] = clr[1] = clr[2] = 0.;
2413   for ( int i = 0; i < 3; i++ ) {
2414     TCollection_AsciiString tmpStr = aStr.Token( ";", i+1 );
2415     if ( !tmpStr.IsEmpty() && tmpStr.IsRealValue() )
2416       clr[i] = tmpStr.RealValue();
2417   }
2418   myColor = Quantity_Color( clr[0], clr[1], clr[2], Quantity_TOC_RGB );
2419 }
2420
2421 //=======================================================================
2422 // name    : GetRangeStr
2423 // Purpose : Get range as a string.
2424 //           Example: "1,2,3,50-60,63,67,70-"
2425 //=======================================================================
2426 void GroupColor::GetColorStr( TCollection_AsciiString& theResStr ) const
2427 {
2428   theResStr.Clear();
2429   theResStr += TCollection_AsciiString( myColor.Red() );
2430   theResStr += TCollection_AsciiString( ";" ) + TCollection_AsciiString( myColor.Green() );
2431   theResStr += TCollection_AsciiString( ";" ) + TCollection_AsciiString( myColor.Blue() );
2432 }
2433
2434 /*
2435   Class       : ElemGeomType
2436   Description : Predicate to check element geometry type
2437 */
2438
2439 ElemGeomType::ElemGeomType()
2440 {
2441   myMesh = 0;
2442   myType = SMDSAbs_All;
2443   myGeomType = SMDSGeom_TRIANGLE;
2444 }
2445
2446 void ElemGeomType::SetMesh( const SMDS_Mesh* theMesh )
2447 {
2448   myMesh = theMesh;
2449 }
2450
2451 bool ElemGeomType::IsSatisfy( long theId )
2452 {
2453   if (!myMesh) return false;
2454   const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
2455   if ( !anElem )
2456     return false;
2457   const SMDSAbs_ElementType anElemType = anElem->GetType();
2458   if ( myType != SMDSAbs_All && anElemType != myType )
2459     return false;
2460   const int aNbNode = anElem->NbNodes();
2461   bool isOk = false;
2462   switch( anElemType )
2463   {
2464   case SMDSAbs_Node:
2465     isOk = (myGeomType == SMDSGeom_POINT);
2466     break;
2467
2468   case SMDSAbs_Edge:
2469     isOk = (myGeomType == SMDSGeom_EDGE);
2470     break;
2471
2472   case SMDSAbs_Face:
2473     if ( myGeomType == SMDSGeom_TRIANGLE )
2474       isOk = (!anElem->IsPoly() && (anElem->IsQuadratic() ? aNbNode == 6 : aNbNode == 3));
2475     else if ( myGeomType == SMDSGeom_QUADRANGLE )
2476       isOk = (!anElem->IsPoly() && (anElem->IsQuadratic() ? aNbNode == 8 : aNbNode == 4));
2477     else if ( myGeomType == SMDSGeom_POLYGON )
2478       isOk = anElem->IsPoly();
2479     break;
2480
2481   case SMDSAbs_Volume:
2482     if ( myGeomType == SMDSGeom_TETRA )
2483       isOk = (!anElem->IsPoly() && (anElem->IsQuadratic() ? aNbNode == 10 : aNbNode == 4));
2484     else if ( myGeomType == SMDSGeom_PYRAMID )
2485       isOk = (!anElem->IsPoly() && (anElem->IsQuadratic() ? aNbNode == 13 : aNbNode == 5));
2486     else if ( myGeomType == SMDSGeom_PENTA )
2487       isOk = (!anElem->IsPoly() && (anElem->IsQuadratic() ? aNbNode == 15 : aNbNode == 6));
2488     else if ( myGeomType == SMDSGeom_HEXA )
2489       isOk = (!anElem->IsPoly() && (anElem->IsQuadratic() ? aNbNode == 20 : aNbNode == 8));
2490      else if ( myGeomType == SMDSGeom_POLYHEDRA )
2491       isOk = anElem->IsPoly();
2492     break;
2493     default: break;
2494   }
2495   return isOk;
2496 }
2497
2498 void ElemGeomType::SetType( SMDSAbs_ElementType theType )
2499 {
2500   myType = theType;
2501 }
2502
2503 SMDSAbs_ElementType ElemGeomType::GetType() const
2504 {
2505   return myType;
2506 }
2507
2508 void ElemGeomType::SetGeomType( SMDSAbs_GeometryType theType )
2509 {
2510   myGeomType = theType;
2511 }
2512
2513 SMDSAbs_GeometryType ElemGeomType::GetGeomType() const
2514 {
2515   return myGeomType;
2516 }
2517
2518 //================================================================================
2519 /*!
2520  * \brief Class CoplanarFaces
2521  */
2522 //================================================================================
2523
2524 CoplanarFaces::CoplanarFaces()
2525   : myMesh(0), myFaceID(0), myToler(0)
2526 {
2527 }
2528 bool CoplanarFaces::IsSatisfy( long theElementId )
2529 {
2530   if ( myCoplanarIDs.empty() )
2531   {
2532     // Build a set of coplanar face ids
2533
2534     if ( !myMesh || !myFaceID || !myToler )
2535       return false;
2536
2537     const SMDS_MeshElement* face = myMesh->FindElement( myFaceID );
2538     if ( !face || face->GetType() != SMDSAbs_Face )
2539       return false;
2540
2541     bool normOK;
2542     gp_Vec myNorm = getNormale( static_cast<const SMDS_MeshFace*>(face), &normOK );
2543     if (!normOK)
2544       return false;
2545
2546     const double radianTol = myToler * PI180;
2547     typedef SMDS_StdIterator< const SMDS_MeshElement*, SMDS_ElemIteratorPtr > TFaceIt;
2548     std::set<const SMDS_MeshElement*> checkedFaces, checkedNodes;
2549     std::list<const SMDS_MeshElement*> faceQueue( 1, face );
2550     while ( !faceQueue.empty() )
2551     {
2552       face = faceQueue.front();
2553       if ( checkedFaces.insert( face ).second )
2554       {
2555         gp_Vec norm = getNormale( static_cast<const SMDS_MeshFace*>(face), &normOK );
2556         if (!normOK || myNorm.Angle( norm ) <= radianTol)
2557         {
2558           myCoplanarIDs.insert( face->GetID() );
2559           std::set<const SMDS_MeshElement*> neighborFaces;
2560           for ( int i = 0; i < face->NbCornerNodes(); ++i )
2561           {
2562             const SMDS_MeshNode* n = face->GetNode( i );
2563             if ( checkedNodes.insert( n ).second )
2564               neighborFaces.insert( TFaceIt( n->GetInverseElementIterator(SMDSAbs_Face)),
2565                                     TFaceIt());
2566           }
2567           faceQueue.insert( faceQueue.end(), neighborFaces.begin(), neighborFaces.end() );
2568         }
2569       }
2570       faceQueue.pop_front();
2571     }
2572   }
2573   return myCoplanarIDs.count( theElementId );
2574 }
2575
2576 /*
2577   *Class       : RangeOfIds
2578   *Description : Predicate for Range of Ids.
2579   *              Range may be specified with two ways.
2580   *              1. Using AddToRange method
2581   *              2. With SetRangeStr method. Parameter of this method is a string
2582   *                 like as "1,2,3,50-60,63,67,70-"
2583 */
2584
2585 //=======================================================================
2586 // name    : RangeOfIds
2587 // Purpose : Constructor
2588 //=======================================================================
2589 RangeOfIds::RangeOfIds()
2590 {
2591   myMesh = 0;
2592   myType = SMDSAbs_All;
2593 }
2594
2595 //=======================================================================
2596 // name    : SetMesh
2597 // Purpose : Set mesh
2598 //=======================================================================
2599 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
2600 {
2601   myMesh = theMesh;
2602 }
2603
2604 //=======================================================================
2605 // name    : AddToRange
2606 // Purpose : Add ID to the range
2607 //=======================================================================
2608 bool RangeOfIds::AddToRange( long theEntityId )
2609 {
2610   myIds.Add( theEntityId );
2611   return true;
2612 }
2613
2614 //=======================================================================
2615 // name    : GetRangeStr
2616 // Purpose : Get range as a string.
2617 //           Example: "1,2,3,50-60,63,67,70-"
2618 //=======================================================================
2619 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
2620 {
2621   theResStr.Clear();
2622
2623   TColStd_SequenceOfInteger     anIntSeq;
2624   TColStd_SequenceOfAsciiString aStrSeq;
2625
2626   TColStd_MapIteratorOfMapOfInteger anIter( myIds );
2627   for ( ; anIter.More(); anIter.Next() )
2628   {
2629     int anId = anIter.Key();
2630     TCollection_AsciiString aStr( anId );
2631     anIntSeq.Append( anId );
2632     aStrSeq.Append( aStr );
2633   }
2634
2635   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
2636   {
2637     int aMinId = myMin( i );
2638     int aMaxId = myMax( i );
2639
2640     TCollection_AsciiString aStr;
2641     if ( aMinId != IntegerFirst() )
2642       aStr += aMinId;
2643
2644     aStr += "-";
2645
2646     if ( aMaxId != IntegerLast() )
2647       aStr += aMaxId;
2648
2649     // find position of the string in result sequence and insert string in it
2650     if ( anIntSeq.Length() == 0 )
2651     {
2652       anIntSeq.Append( aMinId );
2653       aStrSeq.Append( aStr );
2654     }
2655     else
2656     {
2657       if ( aMinId < anIntSeq.First() )
2658       {
2659         anIntSeq.Prepend( aMinId );
2660         aStrSeq.Prepend( aStr );
2661       }
2662       else if ( aMinId > anIntSeq.Last() )
2663       {
2664         anIntSeq.Append( aMinId );
2665         aStrSeq.Append( aStr );
2666       }
2667       else
2668         for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
2669           if ( aMinId < anIntSeq( j ) )
2670           {
2671             anIntSeq.InsertBefore( j, aMinId );
2672             aStrSeq.InsertBefore( j, aStr );
2673             break;
2674           }
2675     }
2676   }
2677
2678   if ( aStrSeq.Length() == 0 )
2679     return;
2680
2681   theResStr = aStrSeq( 1 );
2682   for ( int j = 2, k = aStrSeq.Length(); j <= k; j++  )
2683   {
2684     theResStr += ",";
2685     theResStr += aStrSeq( j );
2686   }
2687 }
2688
2689 //=======================================================================
2690 // name    : SetRangeStr
2691 // Purpose : Define range with string
2692 //           Example of entry string: "1,2,3,50-60,63,67,70-"
2693 //=======================================================================
2694 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
2695 {
2696   myMin.Clear();
2697   myMax.Clear();
2698   myIds.Clear();
2699
2700   TCollection_AsciiString aStr = theStr;
2701   aStr.RemoveAll( ' ' );
2702   aStr.RemoveAll( '\t' );
2703
2704   for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
2705     aStr.Remove( aPos, 2 );
2706
2707   TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
2708   int i = 1;
2709   while ( tmpStr != "" )
2710   {
2711     tmpStr = aStr.Token( ",", i++ );
2712     int aPos = tmpStr.Search( '-' );
2713
2714     if ( aPos == -1 )
2715     {
2716       if ( tmpStr.IsIntegerValue() )
2717         myIds.Add( tmpStr.IntegerValue() );
2718       else
2719         return false;
2720     }
2721     else
2722     {
2723       TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
2724       TCollection_AsciiString aMinStr = tmpStr;
2725
2726       while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
2727       while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
2728
2729       if ( (!aMinStr.IsEmpty() && !aMinStr.IsIntegerValue()) ||
2730            (!aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue()) )
2731         return false;
2732
2733       myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
2734       myMax.Append( aMaxStr.IsEmpty() ? IntegerLast()  : aMaxStr.IntegerValue() );
2735     }
2736   }
2737
2738   return true;
2739 }
2740
2741 //=======================================================================
2742 // name    : GetType
2743 // Purpose : Get type of supported entities
2744 //=======================================================================
2745 SMDSAbs_ElementType RangeOfIds::GetType() const
2746 {
2747   return myType;
2748 }
2749
2750 //=======================================================================
2751 // name    : SetType
2752 // Purpose : Set type of supported entities
2753 //=======================================================================
2754 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
2755 {
2756   myType = theType;
2757 }
2758
2759 //=======================================================================
2760 // name    : IsSatisfy
2761 // Purpose : Verify whether entity satisfies to this rpedicate
2762 //=======================================================================
2763 bool RangeOfIds::IsSatisfy( long theId )
2764 {
2765   if ( !myMesh )
2766     return false;
2767
2768   if ( myType == SMDSAbs_Node )
2769   {
2770     if ( myMesh->FindNode( theId ) == 0 )
2771       return false;
2772   }
2773   else
2774   {
2775     const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
2776     if ( anElem == 0 || (myType != anElem->GetType() && myType != SMDSAbs_All ))
2777       return false;
2778   }
2779
2780   if ( myIds.Contains( theId ) )
2781     return true;
2782
2783   for ( int i = 1, n = myMin.Length(); i <= n; i++ )
2784     if ( theId >= myMin( i ) && theId <= myMax( i ) )
2785       return true;
2786
2787   return false;
2788 }
2789
2790 /*
2791   Class       : Comparator
2792   Description : Base class for comparators
2793 */
2794 Comparator::Comparator():
2795   myMargin(0)
2796 {}
2797
2798 Comparator::~Comparator()
2799 {}
2800
2801 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
2802 {
2803   if ( myFunctor )
2804     myFunctor->SetMesh( theMesh );
2805 }
2806
2807 void Comparator::SetMargin( double theValue )
2808 {
2809   myMargin = theValue;
2810 }
2811
2812 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
2813 {
2814   myFunctor = theFunct;
2815 }
2816
2817 SMDSAbs_ElementType Comparator::GetType() const
2818 {
2819   return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
2820 }
2821
2822 double Comparator::GetMargin()
2823 {
2824   return myMargin;
2825 }
2826
2827
2828 /*
2829   Class       : LessThan
2830   Description : Comparator "<"
2831 */
2832 bool LessThan::IsSatisfy( long theId )
2833 {
2834   return myFunctor && myFunctor->GetValue( theId ) < myMargin;
2835 }
2836
2837
2838 /*
2839   Class       : MoreThan
2840   Description : Comparator ">"
2841 */
2842 bool MoreThan::IsSatisfy( long theId )
2843 {
2844   return myFunctor && myFunctor->GetValue( theId ) > myMargin;
2845 }
2846
2847
2848 /*
2849   Class       : EqualTo
2850   Description : Comparator "="
2851 */
2852 EqualTo::EqualTo():
2853   myToler(Precision::Confusion())
2854 {}
2855
2856 bool EqualTo::IsSatisfy( long theId )
2857 {
2858   return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
2859 }
2860
2861 void EqualTo::SetTolerance( double theToler )
2862 {
2863   myToler = theToler;
2864 }
2865
2866 double EqualTo::GetTolerance()
2867 {
2868   return myToler;
2869 }
2870
2871 /*
2872   Class       : LogicalNOT
2873   Description : Logical NOT predicate
2874 */
2875 LogicalNOT::LogicalNOT()
2876 {}
2877
2878 LogicalNOT::~LogicalNOT()
2879 {}
2880
2881 bool LogicalNOT::IsSatisfy( long theId )
2882 {
2883   return myPredicate && !myPredicate->IsSatisfy( theId );
2884 }
2885
2886 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
2887 {
2888   if ( myPredicate )
2889     myPredicate->SetMesh( theMesh );
2890 }
2891
2892 void LogicalNOT::SetPredicate( PredicatePtr thePred )
2893 {
2894   myPredicate = thePred;
2895 }
2896
2897 SMDSAbs_ElementType LogicalNOT::GetType() const
2898 {
2899   return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
2900 }
2901
2902
2903 /*
2904   Class       : LogicalBinary
2905   Description : Base class for binary logical predicate
2906 */
2907 LogicalBinary::LogicalBinary()
2908 {}
2909
2910 LogicalBinary::~LogicalBinary()
2911 {}
2912
2913 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
2914 {
2915   if ( myPredicate1 )
2916     myPredicate1->SetMesh( theMesh );
2917
2918   if ( myPredicate2 )
2919     myPredicate2->SetMesh( theMesh );
2920 }
2921
2922 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
2923 {
2924   myPredicate1 = thePredicate;
2925 }
2926
2927 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
2928 {
2929   myPredicate2 = thePredicate;
2930 }
2931
2932 SMDSAbs_ElementType LogicalBinary::GetType() const
2933 {
2934   if ( !myPredicate1 || !myPredicate2 )
2935     return SMDSAbs_All;
2936
2937   SMDSAbs_ElementType aType1 = myPredicate1->GetType();
2938   SMDSAbs_ElementType aType2 = myPredicate2->GetType();
2939
2940   return aType1 == aType2 ? aType1 : SMDSAbs_All;
2941 }
2942
2943
2944 /*
2945   Class       : LogicalAND
2946   Description : Logical AND
2947 */
2948 bool LogicalAND::IsSatisfy( long theId )
2949 {
2950   return
2951     myPredicate1 &&
2952     myPredicate2 &&
2953     myPredicate1->IsSatisfy( theId ) &&
2954     myPredicate2->IsSatisfy( theId );
2955 }
2956
2957
2958 /*
2959   Class       : LogicalOR
2960   Description : Logical OR
2961 */
2962 bool LogicalOR::IsSatisfy( long theId )
2963 {
2964   return
2965     myPredicate1 &&
2966     myPredicate2 &&
2967     (myPredicate1->IsSatisfy( theId ) ||
2968     myPredicate2->IsSatisfy( theId ));
2969 }
2970
2971
2972 /*
2973                               FILTER
2974 */
2975
2976 Filter::Filter()
2977 {}
2978
2979 Filter::~Filter()
2980 {}
2981
2982 void Filter::SetPredicate( PredicatePtr thePredicate )
2983 {
2984   myPredicate = thePredicate;
2985 }
2986
2987 template<class TElement, class TIterator, class TPredicate>
2988 inline void FillSequence(const TIterator& theIterator,
2989                          TPredicate& thePredicate,
2990                          Filter::TIdSequence& theSequence)
2991 {
2992   if ( theIterator ) {
2993     while( theIterator->more() ) {
2994       TElement anElem = theIterator->next();
2995       long anId = anElem->GetID();
2996       if ( thePredicate->IsSatisfy( anId ) )
2997         theSequence.push_back( anId );
2998     }
2999   }
3000 }
3001
3002 void
3003 Filter::
3004 GetElementsId( const SMDS_Mesh* theMesh,
3005                PredicatePtr thePredicate,
3006                TIdSequence& theSequence )
3007 {
3008   theSequence.clear();
3009
3010   if ( !theMesh || !thePredicate )
3011     return;
3012
3013   thePredicate->SetMesh( theMesh );
3014
3015   SMDSAbs_ElementType aType = thePredicate->GetType();
3016   switch(aType){
3017   case SMDSAbs_Node:
3018     FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
3019     break;
3020   case SMDSAbs_Edge:
3021     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
3022     break;
3023   case SMDSAbs_Face:
3024     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
3025     break;
3026   case SMDSAbs_Volume:
3027     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
3028     break;
3029   case SMDSAbs_All:
3030     FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
3031     FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
3032     FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
3033     break;
3034   }
3035 }
3036
3037 void
3038 Filter::GetElementsId( const SMDS_Mesh* theMesh,
3039                        Filter::TIdSequence& theSequence )
3040 {
3041   GetElementsId(theMesh,myPredicate,theSequence);
3042 }
3043
3044 /*
3045                               ManifoldPart
3046 */
3047
3048 typedef std::set<SMDS_MeshFace*>                    TMapOfFacePtr;
3049
3050 /*
3051    Internal class Link
3052 */
3053
3054 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
3055                           SMDS_MeshNode* theNode2 )
3056 {
3057   myNode1 = theNode1;
3058   myNode2 = theNode2;
3059 }
3060
3061 ManifoldPart::Link::~Link()
3062 {
3063   myNode1 = 0;
3064   myNode2 = 0;
3065 }
3066
3067 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
3068 {
3069   if ( myNode1 == theLink.myNode1 &&
3070        myNode2 == theLink.myNode2 )
3071     return true;
3072   else if ( myNode1 == theLink.myNode2 &&
3073             myNode2 == theLink.myNode1 )
3074     return true;
3075   else
3076     return false;
3077 }
3078
3079 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
3080 {
3081   if(myNode1 < x.myNode1) return true;
3082   if(myNode1 == x.myNode1)
3083     if(myNode2 < x.myNode2) return true;
3084   return false;
3085 }
3086
3087 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
3088                             const ManifoldPart::Link& theLink2 )
3089 {
3090   return theLink1.IsEqual( theLink2 );
3091 }
3092
3093 ManifoldPart::ManifoldPart()
3094 {
3095   myMesh = 0;
3096   myAngToler = Precision::Angular();
3097   myIsOnlyManifold = true;
3098 }
3099
3100 ManifoldPart::~ManifoldPart()
3101 {
3102   myMesh = 0;
3103 }
3104
3105 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
3106 {
3107   myMesh = theMesh;
3108   process();
3109 }
3110
3111 SMDSAbs_ElementType ManifoldPart::GetType() const
3112 { return SMDSAbs_Face; }
3113
3114 bool ManifoldPart::IsSatisfy( long theElementId )
3115 {
3116   return myMapIds.Contains( theElementId );
3117 }
3118
3119 void ManifoldPart::SetAngleTolerance( const double theAngToler )
3120 { myAngToler = theAngToler; }
3121
3122 double ManifoldPart::GetAngleTolerance() const
3123 { return myAngToler; }
3124
3125 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
3126 { myIsOnlyManifold = theIsOnly; }
3127
3128 void ManifoldPart::SetStartElem( const long  theStartId )
3129 { myStartElemId = theStartId; }
3130
3131 bool ManifoldPart::process()
3132 {
3133   myMapIds.Clear();
3134   myMapBadGeomIds.Clear();
3135
3136   myAllFacePtr.clear();
3137   myAllFacePtrIntDMap.clear();
3138   if ( !myMesh )
3139     return false;
3140
3141   // collect all faces into own map
3142   SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
3143   for (; anFaceItr->more(); )
3144   {
3145     SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
3146     myAllFacePtr.push_back( aFacePtr );
3147     myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
3148   }
3149
3150   SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
3151   if ( !aStartFace )
3152     return false;
3153
3154   // the map of non manifold links and bad geometry
3155   TMapOfLink aMapOfNonManifold;
3156   TColStd_MapOfInteger aMapOfTreated;
3157
3158   // begin cycle on faces from start index and run on vector till the end
3159   //  and from begin to start index to cover whole vector
3160   const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
3161   bool isStartTreat = false;
3162   for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
3163   {
3164     if ( fi == aStartIndx )
3165       isStartTreat = true;
3166     // as result next time when fi will be equal to aStartIndx
3167
3168     SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
3169     if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
3170       continue;
3171
3172     aMapOfTreated.Add( aFacePtr->GetID() );
3173     TColStd_MapOfInteger aResFaces;
3174     if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
3175                          aMapOfNonManifold, aResFaces ) )
3176       continue;
3177     TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
3178     for ( ; anItr.More(); anItr.Next() )
3179     {
3180       int aFaceId = anItr.Key();
3181       aMapOfTreated.Add( aFaceId );
3182       myMapIds.Add( aFaceId );
3183     }
3184
3185     if ( fi == ( myAllFacePtr.size() - 1 ) )
3186       fi = 0;
3187   } // end run on vector of faces
3188   return !myMapIds.IsEmpty();
3189 }
3190
3191 static void getLinks( const SMDS_MeshFace* theFace,
3192                       ManifoldPart::TVectorOfLink& theLinks )
3193 {
3194   int aNbNode = theFace->NbNodes();
3195   SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
3196   int i = 1;
3197   SMDS_MeshNode* aNode = 0;
3198   for ( ; aNodeItr->more() && i <= aNbNode; )
3199   {
3200
3201     SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
3202     if ( i == 1 )
3203       aNode = aN1;
3204     i++;
3205     SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
3206     i++;
3207     ManifoldPart::Link aLink( aN1, aN2 );
3208     theLinks.push_back( aLink );
3209   }
3210 }
3211
3212 bool ManifoldPart::findConnected
3213                  ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
3214                   SMDS_MeshFace*                           theStartFace,
3215                   ManifoldPart::TMapOfLink&                theNonManifold,
3216                   TColStd_MapOfInteger&                    theResFaces )
3217 {
3218   theResFaces.Clear();
3219   if ( !theAllFacePtrInt.size() )
3220     return false;
3221
3222   if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
3223   {
3224     myMapBadGeomIds.Add( theStartFace->GetID() );
3225     return false;
3226   }
3227
3228   ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
3229   ManifoldPart::TVectorOfLink aSeqOfBoundary;
3230   theResFaces.Add( theStartFace->GetID() );
3231   ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
3232
3233   expandBoundary( aMapOfBoundary, aSeqOfBoundary,
3234                  aDMapLinkFace, theNonManifold, theStartFace );
3235
3236   bool isDone = false;
3237   while ( !isDone && aMapOfBoundary.size() != 0 )
3238   {
3239     bool isToReset = false;
3240     ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
3241     for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
3242     {
3243       ManifoldPart::Link aLink = *pLink;
3244       if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
3245         continue;
3246       // each link could be treated only once
3247       aMapToSkip.insert( aLink );
3248
3249       ManifoldPart::TVectorOfFacePtr aFaces;
3250       // find next
3251       if ( myIsOnlyManifold &&
3252            (theNonManifold.find( aLink ) != theNonManifold.end()) )
3253         continue;
3254       else
3255       {
3256         getFacesByLink( aLink, aFaces );
3257         // filter the element to keep only indicated elements
3258         ManifoldPart::TVectorOfFacePtr aFiltered;
3259         ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
3260         for ( ; pFace != aFaces.end(); ++pFace )
3261         {
3262           SMDS_MeshFace* aFace = *pFace;
3263           if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
3264             aFiltered.push_back( aFace );
3265         }
3266         aFaces = aFiltered;
3267         if ( aFaces.size() < 2 )  // no neihgbour faces
3268           continue;
3269         else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
3270         {
3271           theNonManifold.insert( aLink );
3272           continue;
3273         }
3274       }
3275
3276       // compare normal with normals of neighbor element
3277       SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
3278       ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
3279       for ( ; pFace != aFaces.end(); ++pFace )
3280       {
3281         SMDS_MeshFace* aNextFace = *pFace;
3282         if ( aPrevFace == aNextFace )
3283           continue;
3284         int anNextFaceID = aNextFace->GetID();
3285         if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
3286          // should not be with non manifold restriction. probably bad topology
3287           continue;
3288         // check if face was treated and skipped
3289         if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
3290              !isInPlane( aPrevFace, aNextFace ) )
3291           continue;
3292         // add new element to connected and extend the boundaries.
3293         theResFaces.Add( anNextFaceID );
3294         expandBoundary( aMapOfBoundary, aSeqOfBoundary,
3295                         aDMapLinkFace, theNonManifold, aNextFace );
3296         isToReset = true;
3297       }
3298     }
3299     isDone = !isToReset;
3300   }
3301
3302   return !theResFaces.IsEmpty();
3303 }
3304
3305 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
3306                               const SMDS_MeshFace* theFace2 )
3307 {
3308   gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
3309   gp_XYZ aNorm2XYZ = getNormale( theFace2 );
3310   if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
3311   {
3312     myMapBadGeomIds.Add( theFace2->GetID() );
3313     return false;
3314   }
3315   if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
3316     return true;
3317
3318   return false;
3319 }
3320
3321 void ManifoldPart::expandBoundary
3322                    ( ManifoldPart::TMapOfLink&            theMapOfBoundary,
3323                      ManifoldPart::TVectorOfLink&         theSeqOfBoundary,
3324                      ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
3325                      ManifoldPart::TMapOfLink&            theNonManifold,
3326                      SMDS_MeshFace*                       theNextFace ) const
3327 {
3328   ManifoldPart::TVectorOfLink aLinks;
3329   getLinks( theNextFace, aLinks );
3330   int aNbLink = (int)aLinks.size();
3331   for ( int i = 0; i < aNbLink; i++ )
3332   {
3333     ManifoldPart::Link aLink = aLinks[ i ];
3334     if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
3335       continue;
3336     if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
3337     {
3338       if ( myIsOnlyManifold )
3339       {
3340         // remove from boundary
3341         theMapOfBoundary.erase( aLink );
3342         ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
3343         for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
3344         {
3345           ManifoldPart::Link aBoundLink = *pLink;
3346           if ( aBoundLink.IsEqual( aLink ) )
3347           {
3348             theSeqOfBoundary.erase( pLink );
3349             break;
3350           }
3351         }
3352       }
3353     }
3354     else
3355     {
3356       theMapOfBoundary.insert( aLink );
3357       theSeqOfBoundary.push_back( aLink );
3358       theDMapLinkFacePtr[ aLink ] = theNextFace;
3359     }
3360   }
3361 }
3362
3363 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
3364                                    ManifoldPart::TVectorOfFacePtr& theFaces ) const
3365 {
3366   std::set<SMDS_MeshCell *> aSetOfFaces;
3367   // take all faces that shared first node
3368   SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
3369   for ( ; anItr->more(); )
3370   {
3371     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
3372     if ( !aFace )
3373       continue;
3374     aSetOfFaces.insert( aFace );
3375   }
3376   // take all faces that shared second node
3377   anItr = theLink.myNode2->facesIterator();
3378   // find the common part of two sets
3379   for ( ; anItr->more(); )
3380   {
3381     SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
3382     if ( aSetOfFaces.count( aFace ) )
3383       theFaces.push_back( aFace );
3384   }
3385 }
3386
3387
3388 /*
3389    ElementsOnSurface
3390 */
3391
3392 ElementsOnSurface::ElementsOnSurface()
3393 {
3394   myMesh = 0;
3395   myIds.Clear();
3396   myType = SMDSAbs_All;
3397   mySurf.Nullify();
3398   myToler = Precision::Confusion();
3399   myUseBoundaries = false;
3400 }
3401
3402 ElementsOnSurface::~ElementsOnSurface()
3403 {
3404   myMesh = 0;
3405 }
3406
3407 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
3408 {
3409   if ( myMesh == theMesh )
3410     return;
3411   myMesh = theMesh;
3412   process();
3413 }
3414
3415 bool ElementsOnSurface::IsSatisfy( long theElementId )
3416 {
3417   return myIds.Contains( theElementId );
3418 }
3419
3420 SMDSAbs_ElementType ElementsOnSurface::GetType() const
3421 { return myType; }
3422
3423 void ElementsOnSurface::SetTolerance( const double theToler )
3424 {
3425   if ( myToler != theToler )
3426     myIds.Clear();
3427   myToler = theToler;
3428 }
3429
3430 double ElementsOnSurface::GetTolerance() const
3431 { return myToler; }
3432
3433 void ElementsOnSurface::SetUseBoundaries( bool theUse )
3434 {
3435   if ( myUseBoundaries != theUse ) {
3436     myUseBoundaries = theUse;
3437     SetSurface( mySurf, myType );
3438   }
3439 }
3440
3441 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
3442                                     const SMDSAbs_ElementType theType )
3443 {
3444   myIds.Clear();
3445   myType = theType;
3446   mySurf.Nullify();
3447   if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
3448     return;
3449   mySurf = TopoDS::Face( theShape );
3450   BRepAdaptor_Surface SA( mySurf, myUseBoundaries );
3451   Standard_Real
3452     u1 = SA.FirstUParameter(),
3453     u2 = SA.LastUParameter(),
3454     v1 = SA.FirstVParameter(),
3455     v2 = SA.LastVParameter();
3456   Handle(Geom_Surface) surf = BRep_Tool::Surface( mySurf );
3457   myProjector.Init( surf, u1,u2, v1,v2 );
3458   process();
3459 }
3460
3461 void ElementsOnSurface::process()
3462 {
3463   myIds.Clear();
3464   if ( mySurf.IsNull() )
3465     return;
3466
3467   if ( myMesh == 0 )
3468     return;
3469
3470   if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
3471   {
3472     myIds.ReSize( myMesh->NbFaces() );
3473     SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
3474     for(; anIter->more(); )
3475       process( anIter->next() );
3476   }
3477
3478   if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
3479   {
3480     myIds.ReSize( myIds.Extent() + myMesh->NbEdges() );
3481     SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
3482     for(; anIter->more(); )
3483       process( anIter->next() );
3484   }
3485
3486   if ( myType == SMDSAbs_Node )
3487   {
3488     myIds.ReSize( myMesh->NbNodes() );
3489     SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
3490     for(; anIter->more(); )
3491       process( anIter->next() );
3492   }
3493 }
3494
3495 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
3496 {
3497   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
3498   bool isSatisfy = true;
3499   for ( ; aNodeItr->more(); )
3500   {
3501     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
3502     if ( !isOnSurface( aNode ) )
3503     {
3504       isSatisfy = false;
3505       break;
3506     }
3507   }
3508   if ( isSatisfy )
3509     myIds.Add( theElemPtr->GetID() );
3510 }
3511
3512 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode )
3513 {
3514   if ( mySurf.IsNull() )
3515     return false;
3516
3517   gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
3518   //  double aToler2 = myToler * myToler;
3519 //   if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
3520 //   {
3521 //     gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
3522 //     if ( aPln.SquareDistance( aPnt ) > aToler2 )
3523 //       return false;
3524 //   }
3525 //   else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
3526 //   {
3527 //     gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
3528 //     double aRad = aCyl.Radius();
3529 //     gp_Ax3 anAxis = aCyl.Position();
3530 //     gp_XYZ aLoc = aCyl.Location().XYZ();
3531 //     double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
3532 //     double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
3533 //     if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )
3534 //       return false;
3535 //   }
3536 //   else
3537 //     return false;
3538   myProjector.Perform( aPnt );
3539   bool isOn = ( myProjector.IsDone() && myProjector.LowerDistance() <= myToler );
3540
3541   return isOn;
3542 }
3543
3544
3545 /*
3546   ElementsOnShape
3547 */
3548
3549 ElementsOnShape::ElementsOnShape()
3550   : myMesh(0),
3551     myType(SMDSAbs_All),
3552     myToler(Precision::Confusion()),
3553     myAllNodesFlag(false)
3554 {
3555   myCurShapeType = TopAbs_SHAPE;
3556 }
3557
3558 ElementsOnShape::~ElementsOnShape()
3559 {
3560 }
3561
3562 void ElementsOnShape::SetMesh (const SMDS_Mesh* theMesh)
3563 {
3564   if (myMesh != theMesh) {
3565     myMesh = theMesh;
3566     SetShape(myShape, myType);
3567   }
3568 }
3569
3570 bool ElementsOnShape::IsSatisfy (long theElementId)
3571 {
3572   return myIds.Contains(theElementId);
3573 }
3574
3575 SMDSAbs_ElementType ElementsOnShape::GetType() const
3576 {
3577   return myType;
3578 }
3579
3580 void ElementsOnShape::SetTolerance (const double theToler)
3581 {
3582   if (myToler != theToler) {
3583     myToler = theToler;
3584     SetShape(myShape, myType);
3585   }
3586 }
3587
3588 double ElementsOnShape::GetTolerance() const
3589 {
3590   return myToler;
3591 }
3592
3593 void ElementsOnShape::SetAllNodes (bool theAllNodes)
3594 {
3595   if (myAllNodesFlag != theAllNodes) {
3596     myAllNodesFlag = theAllNodes;
3597     SetShape(myShape, myType);
3598   }
3599 }
3600
3601 void ElementsOnShape::SetShape (const TopoDS_Shape&       theShape,
3602                                 const SMDSAbs_ElementType theType)
3603 {
3604   myType = theType;
3605   myShape = theShape;
3606   myIds.Clear();
3607
3608   if (myMesh == 0) return;
3609
3610   switch (myType)
3611   {
3612   case SMDSAbs_All:
3613     myIds.ReSize(myMesh->NbEdges() + myMesh->NbFaces() + myMesh->NbVolumes());
3614     break;
3615   case SMDSAbs_Node:
3616     myIds.ReSize(myMesh->NbNodes());
3617     break;
3618   case SMDSAbs_Edge:
3619     myIds.ReSize(myMesh->NbEdges());
3620     break;
3621   case SMDSAbs_Face:
3622     myIds.ReSize(myMesh->NbFaces());
3623     break;
3624   case SMDSAbs_Volume:
3625     myIds.ReSize(myMesh->NbVolumes());
3626     break;
3627   default:
3628     break;
3629   }
3630
3631   myShapesMap.Clear();
3632   addShape(myShape);
3633 }
3634
3635 void ElementsOnShape::addShape (const TopoDS_Shape& theShape)
3636 {
3637   if (theShape.IsNull() || myMesh == 0)
3638     return;
3639
3640   if (!myShapesMap.Add(theShape)) return;
3641
3642   myCurShapeType = theShape.ShapeType();
3643   switch (myCurShapeType)
3644   {
3645   case TopAbs_COMPOUND:
3646   case TopAbs_COMPSOLID:
3647   case TopAbs_SHELL:
3648   case TopAbs_WIRE:
3649     {
3650       TopoDS_Iterator anIt (theShape, Standard_True, Standard_True);
3651       for (; anIt.More(); anIt.Next()) addShape(anIt.Value());
3652     }
3653     break;
3654   case TopAbs_SOLID:
3655     {
3656       myCurSC.Load(theShape);
3657       process();
3658     }
3659     break;
3660   case TopAbs_FACE:
3661     {
3662       TopoDS_Face aFace = TopoDS::Face(theShape);
3663       BRepAdaptor_Surface SA (aFace, true);
3664       Standard_Real
3665         u1 = SA.FirstUParameter(),
3666         u2 = SA.LastUParameter(),
3667         v1 = SA.FirstVParameter(),
3668         v2 = SA.LastVParameter();
3669       Handle(Geom_Surface) surf = BRep_Tool::Surface(aFace);
3670       myCurProjFace.Init(surf, u1,u2, v1,v2);
3671       myCurFace = aFace;
3672       process();
3673     }
3674     break;
3675   case TopAbs_EDGE:
3676     {
3677       TopoDS_Edge anEdge = TopoDS::Edge(theShape);
3678       Standard_Real u1, u2;
3679       Handle(Geom_Curve) curve = BRep_Tool::Curve(anEdge, u1, u2);
3680       myCurProjEdge.Init(curve, u1, u2);
3681       process();
3682     }
3683     break;
3684   case TopAbs_VERTEX:
3685     {
3686       TopoDS_Vertex aV = TopoDS::Vertex(theShape);
3687       myCurPnt = BRep_Tool::Pnt(aV);
3688       process();
3689     }
3690     break;
3691   default:
3692     break;
3693   }
3694 }
3695
3696 void ElementsOnShape::process()
3697 {
3698   if (myShape.IsNull() || myMesh == 0)
3699     return;
3700
3701   if (myType == SMDSAbs_Node)
3702   {
3703     SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
3704     while (anIter->more())
3705       process(anIter->next());
3706   }
3707   else
3708   {
3709     if (myType == SMDSAbs_Edge || myType == SMDSAbs_All)
3710     {
3711       SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
3712       while (anIter->more())
3713         process(anIter->next());
3714     }
3715
3716     if (myType == SMDSAbs_Face || myType == SMDSAbs_All)
3717     {
3718       SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
3719       while (anIter->more()) {
3720         process(anIter->next());
3721       }
3722     }
3723
3724     if (myType == SMDSAbs_Volume || myType == SMDSAbs_All)
3725     {
3726       SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
3727       while (anIter->more())
3728         process(anIter->next());
3729     }
3730   }
3731 }
3732
3733 void ElementsOnShape::process (const SMDS_MeshElement* theElemPtr)
3734 {
3735   if (myShape.IsNull())
3736     return;
3737
3738   SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
3739   bool isSatisfy = myAllNodesFlag;
3740
3741   gp_XYZ centerXYZ (0, 0, 0);
3742
3743   while (aNodeItr->more() && (isSatisfy == myAllNodesFlag))
3744   {
3745     SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
3746     gp_Pnt aPnt (aNode->X(), aNode->Y(), aNode->Z());
3747     centerXYZ += aPnt.XYZ();
3748
3749     switch (myCurShapeType)
3750     {
3751     case TopAbs_SOLID:
3752       {
3753         myCurSC.Perform(aPnt, myToler);
3754         isSatisfy = (myCurSC.State() == TopAbs_IN || myCurSC.State() == TopAbs_ON);
3755       }
3756       break;
3757     case TopAbs_FACE:
3758       {
3759         myCurProjFace.Perform(aPnt);
3760         isSatisfy = (myCurProjFace.IsDone() && myCurProjFace.LowerDistance() <= myToler);
3761         if (isSatisfy)
3762         {
3763           // check relatively the face
3764           Quantity_Parameter u, v;
3765           myCurProjFace.LowerDistanceParameters(u, v);
3766           gp_Pnt2d aProjPnt (u, v);
3767           BRepClass_FaceClassifier aClsf (myCurFace, aProjPnt, myToler);
3768           isSatisfy = (aClsf.State() == TopAbs_IN || aClsf.State() == TopAbs_ON);
3769         }
3770       }
3771       break;
3772     case TopAbs_EDGE:
3773       {
3774         myCurProjEdge.Perform(aPnt);
3775         isSatisfy = (myCurProjEdge.NbPoints() > 0 && myCurProjEdge.LowerDistance() <= myToler);
3776       }
3777       break;
3778     case TopAbs_VERTEX:
3779       {
3780         isSatisfy = (aPnt.Distance(myCurPnt) <= myToler);
3781       }
3782       break;
3783     default:
3784       {
3785         isSatisfy = false;
3786       }
3787     }
3788   }
3789
3790   if (isSatisfy && myCurShapeType == TopAbs_SOLID) { // Check the center point for volumes MantisBug 0020168
3791     centerXYZ /= theElemPtr->NbNodes();
3792     gp_Pnt aCenterPnt (centerXYZ);
3793     myCurSC.Perform(aCenterPnt, myToler);
3794     if ( !(myCurSC.State() == TopAbs_IN || myCurSC.State() == TopAbs_ON))
3795       isSatisfy = false;
3796   }
3797
3798   if (isSatisfy)
3799     myIds.Add(theElemPtr->GetID());
3800 }
3801
3802 TSequenceOfXYZ::TSequenceOfXYZ()
3803 {}
3804
3805 TSequenceOfXYZ::TSequenceOfXYZ(size_type n) : myArray(n)
3806 {}
3807
3808 TSequenceOfXYZ::TSequenceOfXYZ(size_type n, const gp_XYZ& t) : myArray(n,t)
3809 {}
3810
3811 TSequenceOfXYZ::TSequenceOfXYZ(const TSequenceOfXYZ& theSequenceOfXYZ) : myArray(theSequenceOfXYZ.myArray)
3812 {}
3813
3814 template <class InputIterator>
3815 TSequenceOfXYZ::TSequenceOfXYZ(InputIterator theBegin, InputIterator theEnd): myArray(theBegin,theEnd)
3816 {}
3817
3818 TSequenceOfXYZ::~TSequenceOfXYZ()
3819 {}
3820
3821 TSequenceOfXYZ& TSequenceOfXYZ::operator=(const TSequenceOfXYZ& theSequenceOfXYZ)
3822 {
3823   myArray = theSequenceOfXYZ.myArray;
3824   return *this;
3825 }
3826
3827 gp_XYZ& TSequenceOfXYZ::operator()(size_type n)
3828 {
3829   return myArray[n-1];
3830 }
3831
3832 const gp_XYZ& TSequenceOfXYZ::operator()(size_type n) const
3833 {
3834   return myArray[n-1];
3835 }
3836
3837 void TSequenceOfXYZ::clear()
3838 {
3839   myArray.clear();
3840 }
3841
3842 void TSequenceOfXYZ::reserve(size_type n)
3843 {
3844   myArray.reserve(n);
3845 }
3846
3847 void TSequenceOfXYZ::push_back(const gp_XYZ& v)
3848 {
3849   myArray.push_back(v);
3850 }
3851
3852 TSequenceOfXYZ::size_type TSequenceOfXYZ::size() const
3853 {
3854   return myArray.size();
3855 }