1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
20 #include "SMESH_ControlsDef.hxx"
24 #include <BRep_Tool.hxx>
26 #include <gp_Cylinder.hxx>
32 #include <Geom_Plane.hxx>
33 #include <Geom_CylindricalSurface.hxx>
34 #include <Precision.hxx>
35 #include <TColgp_Array1OfXYZ.hxx>
36 #include <TColStd_MapOfInteger.hxx>
37 #include <TColStd_SequenceOfAsciiString.hxx>
38 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
44 #include "SMDS_Mesh.hxx"
45 #include "SMDS_Iterator.hxx"
46 #include "SMDS_MeshElement.hxx"
47 #include "SMDS_MeshNode.hxx"
48 #include "SMDS_VolumeTool.hxx"
56 inline double getAngle( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
58 gp_Vec v1( P1 - P2 ), v2( P3 - P2 );
60 return v1.Magnitude() < gp::Resolution() ||
61 v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
64 inline double getArea( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
66 gp_Vec aVec1( P2 - P1 );
67 gp_Vec aVec2( P3 - P1 );
68 return ( aVec1 ^ aVec2 ).Magnitude() * 0.5;
71 inline double getArea( const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3 )
73 return getArea( P1.XYZ(), P2.XYZ(), P3.XYZ() );
78 inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
80 double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
84 int getNbMultiConnection( const SMDS_Mesh* theMesh, const int theId )
89 const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
90 if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge || anEdge->NbNodes() != 2 )
93 TColStd_MapOfInteger aMap;
96 SMDS_ElemIteratorPtr anIter = anEdge->nodesIterator();
98 while( anIter->more() ) {
99 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
102 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
103 while( anElemIter->more() ) {
104 const SMDS_MeshElement* anElem = anElemIter->next();
105 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
106 int anId = anElem->GetID();
108 if ( anIter->more() ) // i.e. first node
110 else if ( aMap.Contains( anId ) )
124 using namespace SMESH::Controls;
131 Class : NumericalFunctor
132 Description : Base class for numerical functors
134 NumericalFunctor::NumericalFunctor():
140 void NumericalFunctor::SetMesh( const SMDS_Mesh* theMesh )
145 bool NumericalFunctor::GetPoints(const int theId,
146 TSequenceOfXYZ& theRes ) const
153 return GetPoints( myMesh->FindElement( theId ), theRes );
156 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
157 TSequenceOfXYZ& theRes )
164 // Get nodes of the element
165 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
168 while( anIter->more() )
170 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
172 theRes.push_back( gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
180 long NumericalFunctor::GetPrecision() const
185 void NumericalFunctor::SetPrecision( const long thePrecision )
187 myPrecision = thePrecision;
190 double NumericalFunctor::GetValue( long theId )
193 if ( GetPoints( theId, P ))
195 double aVal = GetValue( P );
196 if ( myPrecision >= 0 )
198 double prec = pow( 10., (double)( myPrecision ) );
199 aVal = floor( aVal * prec + 0.5 ) / prec;
209 Description : Functor for calculation of minimum angle
212 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
219 aMin = getAngle(P( P.size() ), P( 1 ), P( 2 ));
220 aMin = Min(aMin,getAngle(P( P.size()-1 ), P( P.size() ), P( 1 )));
222 for (int i=2; i<P.size();i++){
223 double A0 = getAngle( P( i-1 ), P( i ), P( i+1 ) );
227 return aMin * 180 / PI;
230 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
232 const double aBestAngle = PI / nbNodes;
233 return ( fabs( aBestAngle - Value ));
236 SMDSAbs_ElementType MinimumAngle::GetType() const
244 Description : Functor for calculating aspect ratio
246 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
248 int nbNodes = P.size();
253 // Compute lengths of the sides
255 double aLen[ nbNodes ];
256 for ( int i = 0; i < nbNodes - 1; i++ )
257 aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
258 aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
260 // Compute aspect ratio
264 double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
265 if ( anArea <= Precision::Confusion() )
267 double aMaxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
268 static double aCoef = sqrt( 3. ) / 4;
270 return aCoef * aMaxLen * aMaxLen / anArea;
274 double aMinLen = aLen[ 0 ];
275 double aMaxLen = aLen[ 0 ];
277 for(int i = 1; i < nbNodes ; i++ ){
278 aMinLen = Min( aMinLen, aLen[ i ] );
279 aMaxLen = Max( aMaxLen, aLen[ i ] );
281 if ( aMinLen <= Precision::Confusion() )
284 return aMaxLen / aMinLen;
288 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
290 // the aspect ratio is in the range [1.0,infinity]
293 return Value / 1000.;
296 SMDSAbs_ElementType AspectRatio::GetType() const
303 Class : AspectRatio3D
304 Description : Functor for calculating aspect ratio
308 inline double getHalfPerimeter(double theTria[3]){
309 return (theTria[0] + theTria[1] + theTria[2])/2.0;
312 inline double getArea(double theHalfPerim, double theTria[3]){
313 return sqrt(theHalfPerim*
314 (theHalfPerim-theTria[0])*
315 (theHalfPerim-theTria[1])*
316 (theHalfPerim-theTria[2]));
319 inline double getVolume(double theLen[6]){
320 double a2 = theLen[0]*theLen[0];
321 double b2 = theLen[1]*theLen[1];
322 double c2 = theLen[2]*theLen[2];
323 double d2 = theLen[3]*theLen[3];
324 double e2 = theLen[4]*theLen[4];
325 double f2 = theLen[5]*theLen[5];
326 double P = 4.0*a2*b2*d2;
327 double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
328 double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
329 return sqrt(P-Q+R)/12.0;
332 inline double getVolume2(double theLen[6]){
333 double a2 = theLen[0]*theLen[0];
334 double b2 = theLen[1]*theLen[1];
335 double c2 = theLen[2]*theLen[2];
336 double d2 = theLen[3]*theLen[3];
337 double e2 = theLen[4]*theLen[4];
338 double f2 = theLen[5]*theLen[5];
340 double P = a2*e2*(b2+c2+d2+f2-a2-e2);
341 double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
342 double R = c2*d2*(a2+b2+e2+f2-c2-d2);
343 double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
345 return sqrt(P+Q+R-S)/12.0;
348 inline double getVolume(const TSequenceOfXYZ& P){
349 gp_Vec aVec1( P( 2 ) - P( 1 ) );
350 gp_Vec aVec2( P( 3 ) - P( 1 ) );
351 gp_Vec aVec3( P( 4 ) - P( 1 ) );
352 gp_Vec anAreaVec( aVec1 ^ aVec2 );
353 return fabs(aVec3 * anAreaVec) / 6.0;
356 inline double getMaxHeight(double theLen[6])
358 double aHeight = max(theLen[0],theLen[1]);
359 aHeight = max(aHeight,theLen[2]);
360 aHeight = max(aHeight,theLen[3]);
361 aHeight = max(aHeight,theLen[4]);
362 aHeight = max(aHeight,theLen[5]);
368 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
370 double aQuality = 0.0;
371 int nbNodes = P.size();
375 getDistance(P( 1 ),P( 2 )), // a
376 getDistance(P( 2 ),P( 3 )), // b
377 getDistance(P( 3 ),P( 1 )), // c
378 getDistance(P( 2 ),P( 4 )), // d
379 getDistance(P( 3 ),P( 4 )), // e
380 getDistance(P( 1 ),P( 4 )) // f
382 double aTria[4][3] = {
383 {aLen[0],aLen[1],aLen[2]}, // abc
384 {aLen[0],aLen[3],aLen[5]}, // adf
385 {aLen[1],aLen[3],aLen[4]}, // bde
386 {aLen[2],aLen[4],aLen[5]} // cef
388 double aSumArea = 0.0;
389 double aHalfPerimeter = getHalfPerimeter(aTria[0]);
390 double anArea = getArea(aHalfPerimeter,aTria[0]);
392 aHalfPerimeter = getHalfPerimeter(aTria[1]);
393 anArea = getArea(aHalfPerimeter,aTria[1]);
395 aHalfPerimeter = getHalfPerimeter(aTria[2]);
396 anArea = getArea(aHalfPerimeter,aTria[2]);
398 aHalfPerimeter = getHalfPerimeter(aTria[3]);
399 anArea = getArea(aHalfPerimeter,aTria[3]);
401 double aVolume = getVolume(P);
402 //double aVolume = getVolume(aLen);
403 double aHeight = getMaxHeight(aLen);
404 static double aCoeff = sqrt(6.0)/36.0;
405 aQuality = aCoeff*aHeight*aSumArea/aVolume;
410 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
411 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
414 gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
415 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
418 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
419 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
422 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
423 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
429 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
430 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
433 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
434 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
437 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
438 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
441 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
442 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
445 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
446 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
449 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
450 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
456 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
457 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
460 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
461 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
464 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
465 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
468 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
469 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
472 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
473 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
476 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
477 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
480 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
481 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
484 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
485 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
488 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
489 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
492 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
493 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
496 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
497 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
500 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
501 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
504 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
505 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
508 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
509 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
512 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
513 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
516 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
517 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
520 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
521 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
524 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
525 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
528 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
529 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
532 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
533 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
536 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
537 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
540 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
541 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
544 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
545 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
548 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
549 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
552 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
553 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
556 gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
557 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
560 gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
561 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
564 gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
565 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
568 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
569 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
572 gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
573 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
576 gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
577 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
580 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
581 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
584 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
585 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
593 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
595 // the aspect ratio is in the range [1.0,infinity]
598 return Value / 1000.;
601 SMDSAbs_ElementType AspectRatio3D::GetType() const
603 return SMDSAbs_Volume;
609 Description : Functor for calculating warping
611 double Warping::GetValue( const TSequenceOfXYZ& P )
616 gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4;
618 double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
619 double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
620 double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
621 double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
623 return Max( Max( A1, A2 ), Max( A3, A4 ) );
626 double Warping::ComputeA( const gp_XYZ& thePnt1,
627 const gp_XYZ& thePnt2,
628 const gp_XYZ& thePnt3,
629 const gp_XYZ& theG ) const
631 double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
632 double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
633 double L = Min( aLen1, aLen2 ) * 0.5;
634 if ( L < Precision::Confusion())
637 gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
638 gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
639 gp_XYZ N = GI.Crossed( GJ );
641 if ( N.Modulus() < gp::Resolution() )
646 double H = ( thePnt2 - theG ).Dot( N );
647 return asin( fabs( H / L ) ) * 180 / PI;
650 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
652 // the warp is in the range [0.0,PI/2]
653 // 0.0 = good (no warp)
654 // PI/2 = bad (face pliee)
658 SMDSAbs_ElementType Warping::GetType() const
666 Description : Functor for calculating taper
668 double Taper::GetValue( const TSequenceOfXYZ& P )
674 double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2;
675 double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2;
676 double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2;
677 double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2;
679 double JA = 0.25 * ( J1 + J2 + J3 + J4 );
680 if ( JA <= Precision::Confusion() )
683 double T1 = fabs( ( J1 - JA ) / JA );
684 double T2 = fabs( ( J2 - JA ) / JA );
685 double T3 = fabs( ( J3 - JA ) / JA );
686 double T4 = fabs( ( J4 - JA ) / JA );
688 return Max( Max( T1, T2 ), Max( T3, T4 ) );
691 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
693 // the taper is in the range [0.0,1.0]
694 // 0.0 = good (no taper)
695 // 1.0 = bad (les cotes opposes sont allignes)
699 SMDSAbs_ElementType Taper::GetType() const
707 Description : Functor for calculating skew in degrees
709 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
711 gp_XYZ p12 = ( p2 + p1 ) / 2;
712 gp_XYZ p23 = ( p3 + p2 ) / 2;
713 gp_XYZ p31 = ( p3 + p1 ) / 2;
715 gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
717 return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
720 double Skew::GetValue( const TSequenceOfXYZ& P )
722 if ( P.size() != 3 && P.size() != 4 )
726 static double PI2 = PI / 2;
729 double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
730 double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
731 double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
733 return Max( A0, Max( A1, A2 ) ) * 180 / PI;
737 gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2;
738 gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2;
739 gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2;
740 gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2;
742 gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
743 double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
744 ? 0 : fabs( PI2 - v1.Angle( v2 ) );
750 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
752 // the skew is in the range [0.0,PI/2].
758 SMDSAbs_ElementType Skew::GetType() const
766 Description : Functor for calculating area
768 double Area::GetValue( const TSequenceOfXYZ& P )
772 return getArea( P( 1 ), P( 2 ), P( 3 ) );
773 else if (P.size() > 3)
774 aArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
778 for (int i=4; i<=P.size(); i++)
779 aArea += getArea(P(1),P(i-1),P(i));
783 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
788 SMDSAbs_ElementType Area::GetType() const
796 Description : Functor for calculating length off edge
798 double Length::GetValue( const TSequenceOfXYZ& P )
800 return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
803 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
808 SMDSAbs_ElementType Length::GetType() const
815 Description : Functor for calculating length of edge
818 double Length2D::GetValue( long theElementId)
822 if (GetPoints(theElementId,P)){
824 double aVal;// = GetValue( P );
825 const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
826 SMDSAbs_ElementType aType = aElem->GetType();
835 aVal = getDistance( P( 1 ), P( 2 ) );
839 if (len == 3){ // triangles
840 double L1 = getDistance(P( 1 ),P( 2 ));
841 double L2 = getDistance(P( 2 ),P( 3 ));
842 double L3 = getDistance(P( 3 ),P( 1 ));
843 aVal = Max(L1,Max(L2,L3));
846 else if (len == 4){ // quadrangles
847 double L1 = getDistance(P( 1 ),P( 2 ));
848 double L2 = getDistance(P( 2 ),P( 3 ));
849 double L3 = getDistance(P( 3 ),P( 4 ));
850 double L4 = getDistance(P( 4 ),P( 1 ));
851 aVal = Max(Max(L1,L2),Max(L3,L4));
855 if (len == 4){ // tetraidrs
856 double L1 = getDistance(P( 1 ),P( 2 ));
857 double L2 = getDistance(P( 2 ),P( 3 ));
858 double L3 = getDistance(P( 3 ),P( 1 ));
859 double L4 = getDistance(P( 1 ),P( 4 ));
860 double L5 = getDistance(P( 2 ),P( 4 ));
861 double L6 = getDistance(P( 3 ),P( 4 ));
862 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
865 else if (len == 5){ // piramids
866 double L1 = getDistance(P( 1 ),P( 2 ));
867 double L2 = getDistance(P( 2 ),P( 3 ));
868 double L3 = getDistance(P( 3 ),P( 1 ));
869 double L4 = getDistance(P( 4 ),P( 1 ));
870 double L5 = getDistance(P( 1 ),P( 5 ));
871 double L6 = getDistance(P( 2 ),P( 5 ));
872 double L7 = getDistance(P( 3 ),P( 5 ));
873 double L8 = getDistance(P( 4 ),P( 5 ));
875 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
876 aVal = Max(aVal,Max(L7,L8));
879 else if (len == 6){ // pentaidres
880 double L1 = getDistance(P( 1 ),P( 2 ));
881 double L2 = getDistance(P( 2 ),P( 3 ));
882 double L3 = getDistance(P( 3 ),P( 1 ));
883 double L4 = getDistance(P( 4 ),P( 5 ));
884 double L5 = getDistance(P( 5 ),P( 6 ));
885 double L6 = getDistance(P( 6 ),P( 4 ));
886 double L7 = getDistance(P( 1 ),P( 4 ));
887 double L8 = getDistance(P( 2 ),P( 5 ));
888 double L9 = getDistance(P( 3 ),P( 6 ));
890 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
891 aVal = Max(aVal,Max(Max(L7,L8),L9));
894 else if (len == 8){ // hexaider
895 double L1 = getDistance(P( 1 ),P( 2 ));
896 double L2 = getDistance(P( 2 ),P( 3 ));
897 double L3 = getDistance(P( 3 ),P( 4 ));
898 double L4 = getDistance(P( 4 ),P( 1 ));
899 double L5 = getDistance(P( 5 ),P( 6 ));
900 double L6 = getDistance(P( 6 ),P( 7 ));
901 double L7 = getDistance(P( 7 ),P( 8 ));
902 double L8 = getDistance(P( 8 ),P( 5 ));
903 double L9 = getDistance(P( 1 ),P( 5 ));
904 double L10= getDistance(P( 2 ),P( 6 ));
905 double L11= getDistance(P( 3 ),P( 7 ));
906 double L12= getDistance(P( 4 ),P( 8 ));
908 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
909 aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
910 aVal = Max(aVal,Max(L11,L12));
922 if ( myPrecision >= 0 )
924 double prec = pow( 10., (double)( myPrecision ) );
925 aVal = floor( aVal * prec + 0.5 ) / prec;
934 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
939 SMDSAbs_ElementType Length2D::GetType() const
944 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
947 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
948 if(thePntId1 > thePntId2){
949 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
953 bool Length2D::Value::operator<(const Length2D::Value& x) const{
954 if(myPntId[0] < x.myPntId[0]) return true;
955 if(myPntId[0] == x.myPntId[0])
956 if(myPntId[1] < x.myPntId[1]) return true;
960 void Length2D::GetValues(TValues& theValues){
962 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
963 for(; anIter->more(); ){
964 const SMDS_MeshFace* anElem = anIter->next();
965 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
970 const SMDS_MeshElement* aNode;
971 if(aNodesIter->more()){
972 aNode = aNodesIter->next();
973 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
974 P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
975 aNodeId[0] = aNodeId[1] = aNode->GetID();
978 for(; aNodesIter->more(); ){
979 aNode = aNodesIter->next();
980 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
981 long anId = aNode->GetID();
983 P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
985 aLength = P[1].Distance(P[2]);
987 Value aValue(aLength,aNodeId[1],anId);
990 theValues.insert(aValue);
993 aLength = P[0].Distance(P[1]);
995 Value aValue(aLength,aNodeId[0],aNodeId[1]);
996 theValues.insert(aValue);
1001 Class : MultiConnection
1002 Description : Functor for calculating number of faces conneted to the edge
1004 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1008 double MultiConnection::GetValue( long theId )
1010 return getNbMultiConnection( myMesh, theId );
1013 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1018 SMDSAbs_ElementType MultiConnection::GetType() const
1020 return SMDSAbs_Edge;
1024 Class : MultiConnection2D
1025 Description : Functor for calculating number of faces conneted to the edge
1027 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1032 double MultiConnection2D::GetValue( long theElementId )
1037 if (GetPoints(theElementId,P)){
1038 const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1039 SMDSAbs_ElementType aType = anFaceElem->GetType();
1043 TColStd_MapOfInteger aMap;
1051 if (len == 3){ // triangles
1052 int Nb[3] = {0,0,0};
1055 SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1056 if ( anIter != 0 ) {
1057 while( anIter->more() ) {
1058 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1062 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1063 while( anElemIter->more() ) {
1064 const SMDS_MeshElement* anElem = anElemIter->next();
1065 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1066 int anId = anElem->GetID();
1068 if ( anIter->more() ) // i.e. first node
1070 else if ( aMap.Contains( anId ) ){
1074 else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1079 aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1082 case SMDSAbs_Volume:
1087 return aResult;//getNbMultiConnection( myMesh, theId );
1090 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1095 SMDSAbs_ElementType MultiConnection2D::GetType() const
1097 return SMDSAbs_Face;
1100 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1102 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1103 if(thePntId1 > thePntId2){
1104 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1108 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1109 if(myPntId[0] < x.myPntId[0]) return true;
1110 if(myPntId[0] == x.myPntId[0])
1111 if(myPntId[1] < x.myPntId[1]) return true;
1115 void MultiConnection2D::GetValues(MValues& theValues){
1116 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1117 for(; anIter->more(); ){
1118 const SMDS_MeshFace* anElem = anIter->next();
1119 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1122 //int aNbConnects=0;
1123 const SMDS_MeshNode* aNode0;
1124 const SMDS_MeshNode* aNode1;
1125 const SMDS_MeshNode* aNode2;
1126 if(aNodesIter->more()){
1127 aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1129 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1130 aNodeId[0] = aNodeId[1] = aNodes->GetID();
1132 for(; aNodesIter->more(); ){
1133 aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1134 long anId = aNode2->GetID();
1137 Value aValue(aNodeId[1],aNodeId[2]);
1138 MValues::iterator aItr = theValues.find(aValue);
1139 if (aItr != theValues.end()){
1143 theValues[aValue] = 1;
1146 //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1147 aNodeId[1] = aNodeId[2];
1150 Value aValue(aNodeId[0],aNodeId[2]);
1151 MValues::iterator aItr = theValues.find(aValue);
1152 if (aItr != theValues.end()){
1156 theValues[aValue] = 1;
1159 //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1169 Class : BadOrientedVolume
1170 Description : Predicate bad oriented volumes
1173 BadOrientedVolume::BadOrientedVolume()
1178 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1183 bool BadOrientedVolume::IsSatisfy( long theId )
1188 SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1189 return !vTool.IsForward();
1192 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1194 return SMDSAbs_Volume;
1201 Description : Predicate for free borders
1204 FreeBorders::FreeBorders()
1209 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1214 bool FreeBorders::IsSatisfy( long theId )
1216 return getNbMultiConnection( myMesh, theId ) == 1;
1219 SMDSAbs_ElementType FreeBorders::GetType() const
1221 return SMDSAbs_Edge;
1227 Description : Predicate for free Edges
1229 FreeEdges::FreeEdges()
1234 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1239 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
1241 TColStd_MapOfInteger aMap;
1242 for ( int i = 0; i < 2; i++ )
1244 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1245 while( anElemIter->more() )
1247 const SMDS_MeshElement* anElem = anElemIter->next();
1248 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1250 int anId = anElem->GetID();
1254 else if ( aMap.Contains( anId ) && anId != theFaceId )
1262 bool FreeEdges::IsSatisfy( long theId )
1267 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1268 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1271 int nbNodes = aFace->NbNodes();
1272 const SMDS_MeshNode* aNodes[ nbNodes ];
1274 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
1277 while( anIter->more() )
1279 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1282 aNodes[ i++ ] = aNode;
1286 for ( int i = 0; i < nbNodes - 1; i++ )
1287 if ( IsFreeEdge( &aNodes[ i ], theId ) )
1290 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
1292 return IsFreeEdge( &aNodes[ 0 ], theId );
1296 SMDSAbs_ElementType FreeEdges::GetType() const
1298 return SMDSAbs_Face;
1301 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1304 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1305 if(thePntId1 > thePntId2){
1306 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1310 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1311 if(myPntId[0] < x.myPntId[0]) return true;
1312 if(myPntId[0] == x.myPntId[0])
1313 if(myPntId[1] < x.myPntId[1]) return true;
1317 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1318 FreeEdges::TBorders& theRegistry,
1319 FreeEdges::TBorders& theContainer)
1321 if(theRegistry.find(theBorder) == theRegistry.end()){
1322 theRegistry.insert(theBorder);
1323 theContainer.insert(theBorder);
1325 theContainer.erase(theBorder);
1329 void FreeEdges::GetBoreders(TBorders& theBorders)
1332 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1333 for(; anIter->more(); ){
1334 const SMDS_MeshFace* anElem = anIter->next();
1335 long anElemId = anElem->GetID();
1336 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1338 const SMDS_MeshElement* aNode;
1339 if(aNodesIter->more()){
1340 aNode = aNodesIter->next();
1341 aNodeId[0] = aNodeId[1] = aNode->GetID();
1343 for(; aNodesIter->more(); ){
1344 aNode = aNodesIter->next();
1345 long anId = aNode->GetID();
1346 Border aBorder(anElemId,aNodeId[1],anId);
1348 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1349 UpdateBorders(aBorder,aRegistry,theBorders);
1351 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1352 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1353 UpdateBorders(aBorder,aRegistry,theBorders);
1355 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1360 Description : Predicate for Range of Ids.
1361 Range may be specified with two ways.
1362 1. Using AddToRange method
1363 2. With SetRangeStr method. Parameter of this method is a string
1364 like as "1,2,3,50-60,63,67,70-"
1367 //=======================================================================
1368 // name : RangeOfIds
1369 // Purpose : Constructor
1370 //=======================================================================
1371 RangeOfIds::RangeOfIds()
1374 myType = SMDSAbs_All;
1377 //=======================================================================
1379 // Purpose : Set mesh
1380 //=======================================================================
1381 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1386 //=======================================================================
1387 // name : AddToRange
1388 // Purpose : Add ID to the range
1389 //=======================================================================
1390 bool RangeOfIds::AddToRange( long theEntityId )
1392 myIds.Add( theEntityId );
1396 //=======================================================================
1397 // name : GetRangeStr
1398 // Purpose : Get range as a string.
1399 // Example: "1,2,3,50-60,63,67,70-"
1400 //=======================================================================
1401 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1405 TColStd_SequenceOfInteger anIntSeq;
1406 TColStd_SequenceOfAsciiString aStrSeq;
1408 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1409 for ( ; anIter.More(); anIter.Next() )
1411 int anId = anIter.Key();
1412 TCollection_AsciiString aStr( anId );
1413 anIntSeq.Append( anId );
1414 aStrSeq.Append( aStr );
1417 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1419 int aMinId = myMin( i );
1420 int aMaxId = myMax( i );
1422 TCollection_AsciiString aStr;
1423 if ( aMinId != IntegerFirst() )
1428 if ( aMaxId != IntegerLast() )
1431 // find position of the string in result sequence and insert string in it
1432 if ( anIntSeq.Length() == 0 )
1434 anIntSeq.Append( aMinId );
1435 aStrSeq.Append( aStr );
1439 if ( aMinId < anIntSeq.First() )
1441 anIntSeq.Prepend( aMinId );
1442 aStrSeq.Prepend( aStr );
1444 else if ( aMinId > anIntSeq.Last() )
1446 anIntSeq.Append( aMinId );
1447 aStrSeq.Append( aStr );
1450 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1451 if ( aMinId < anIntSeq( j ) )
1453 anIntSeq.InsertBefore( j, aMinId );
1454 aStrSeq.InsertBefore( j, aStr );
1460 if ( aStrSeq.Length() == 0 )
1463 theResStr = aStrSeq( 1 );
1464 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1467 theResStr += aStrSeq( j );
1471 //=======================================================================
1472 // name : SetRangeStr
1473 // Purpose : Define range with string
1474 // Example of entry string: "1,2,3,50-60,63,67,70-"
1475 //=======================================================================
1476 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1482 TCollection_AsciiString aStr = theStr;
1483 aStr.RemoveAll( ' ' );
1484 aStr.RemoveAll( '\t' );
1486 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1487 aStr.Remove( aPos, 2 );
1489 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1491 while ( tmpStr != "" )
1493 tmpStr = aStr.Token( ",", i++ );
1494 int aPos = tmpStr.Search( '-' );
1498 if ( tmpStr.IsIntegerValue() )
1499 myIds.Add( tmpStr.IntegerValue() );
1505 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1506 TCollection_AsciiString aMinStr = tmpStr;
1508 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1509 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1511 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1512 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1515 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1516 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1523 //=======================================================================
1525 // Purpose : Get type of supported entities
1526 //=======================================================================
1527 SMDSAbs_ElementType RangeOfIds::GetType() const
1532 //=======================================================================
1534 // Purpose : Set type of supported entities
1535 //=======================================================================
1536 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1541 //=======================================================================
1543 // Purpose : Verify whether entity satisfies to this rpedicate
1544 //=======================================================================
1545 bool RangeOfIds::IsSatisfy( long theId )
1550 if ( myType == SMDSAbs_Node )
1552 if ( myMesh->FindNode( theId ) == 0 )
1557 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1558 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1562 if ( myIds.Contains( theId ) )
1565 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1566 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1574 Description : Base class for comparators
1576 Comparator::Comparator():
1580 Comparator::~Comparator()
1583 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1586 myFunctor->SetMesh( theMesh );
1589 void Comparator::SetMargin( double theValue )
1591 myMargin = theValue;
1594 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1596 myFunctor = theFunct;
1599 SMDSAbs_ElementType Comparator::GetType() const
1601 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1604 double Comparator::GetMargin()
1612 Description : Comparator "<"
1614 bool LessThan::IsSatisfy( long theId )
1616 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1622 Description : Comparator ">"
1624 bool MoreThan::IsSatisfy( long theId )
1626 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1632 Description : Comparator "="
1635 myToler(Precision::Confusion())
1638 bool EqualTo::IsSatisfy( long theId )
1640 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1643 void EqualTo::SetTolerance( double theToler )
1648 double EqualTo::GetTolerance()
1655 Description : Logical NOT predicate
1657 LogicalNOT::LogicalNOT()
1660 LogicalNOT::~LogicalNOT()
1663 bool LogicalNOT::IsSatisfy( long theId )
1665 return myPredicate && !myPredicate->IsSatisfy( theId );
1668 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1671 myPredicate->SetMesh( theMesh );
1674 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1676 myPredicate = thePred;
1679 SMDSAbs_ElementType LogicalNOT::GetType() const
1681 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1686 Class : LogicalBinary
1687 Description : Base class for binary logical predicate
1689 LogicalBinary::LogicalBinary()
1692 LogicalBinary::~LogicalBinary()
1695 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1698 myPredicate1->SetMesh( theMesh );
1701 myPredicate2->SetMesh( theMesh );
1704 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1706 myPredicate1 = thePredicate;
1709 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1711 myPredicate2 = thePredicate;
1714 SMDSAbs_ElementType LogicalBinary::GetType() const
1716 if ( !myPredicate1 || !myPredicate2 )
1719 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1720 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1722 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1728 Description : Logical AND
1730 bool LogicalAND::IsSatisfy( long theId )
1735 myPredicate1->IsSatisfy( theId ) &&
1736 myPredicate2->IsSatisfy( theId );
1742 Description : Logical OR
1744 bool LogicalOR::IsSatisfy( long theId )
1749 myPredicate1->IsSatisfy( theId ) ||
1750 myPredicate2->IsSatisfy( theId );
1764 void Filter::SetPredicate( PredicatePtr thePredicate )
1766 myPredicate = thePredicate;
1769 template<class TElement, class TIterator, class TPredicate>
1770 inline void FillSequence(const TIterator& theIterator,
1771 TPredicate& thePredicate,
1772 Filter::TIdSequence& theSequence)
1774 if ( theIterator ) {
1775 while( theIterator->more() ) {
1776 TElement anElem = theIterator->next();
1777 long anId = anElem->GetID();
1778 if ( thePredicate->IsSatisfy( anId ) )
1779 theSequence.push_back( anId );
1786 GetElementsId( const SMDS_Mesh* theMesh,
1787 PredicatePtr thePredicate,
1788 TIdSequence& theSequence )
1790 theSequence.clear();
1792 if ( !theMesh || !thePredicate )
1795 thePredicate->SetMesh( theMesh );
1797 SMDSAbs_ElementType aType = thePredicate->GetType();
1800 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
1803 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1806 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1808 case SMDSAbs_Volume:
1809 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1812 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1813 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1814 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1820 Filter::GetElementsId( const SMDS_Mesh* theMesh,
1821 Filter::TIdSequence& theSequence )
1823 GetElementsId(theMesh,myPredicate,theSequence);
1830 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1836 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1837 SMDS_MeshNode* theNode2 )
1843 ManifoldPart::Link::~Link()
1849 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1851 if ( myNode1 == theLink.myNode1 &&
1852 myNode2 == theLink.myNode2 )
1854 else if ( myNode1 == theLink.myNode2 &&
1855 myNode2 == theLink.myNode1 )
1861 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1863 if(myNode1 < x.myNode1) return true;
1864 if(myNode1 == x.myNode1)
1865 if(myNode2 < x.myNode2) return true;
1869 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1870 const ManifoldPart::Link& theLink2 )
1872 return theLink1.IsEqual( theLink2 );
1875 ManifoldPart::ManifoldPart()
1878 myAngToler = Precision::Angular();
1879 myIsOnlyManifold = true;
1882 ManifoldPart::~ManifoldPart()
1887 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
1893 SMDSAbs_ElementType ManifoldPart::GetType() const
1894 { return SMDSAbs_Face; }
1896 bool ManifoldPart::IsSatisfy( long theElementId )
1898 return myMapIds.Contains( theElementId );
1901 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1902 { myAngToler = theAngToler; }
1904 double ManifoldPart::GetAngleTolerance() const
1905 { return myAngToler; }
1907 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1908 { myIsOnlyManifold = theIsOnly; }
1910 void ManifoldPart::SetStartElem( const long theStartId )
1911 { myStartElemId = theStartId; }
1913 bool ManifoldPart::process()
1916 myMapBadGeomIds.Clear();
1918 myAllFacePtr.clear();
1919 myAllFacePtrIntDMap.clear();
1923 // collect all faces into own map
1924 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1925 for (; anFaceItr->more(); )
1927 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1928 myAllFacePtr.push_back( aFacePtr );
1929 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1932 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1936 // the map of non manifold links and bad geometry
1937 TMapOfLink aMapOfNonManifold;
1938 TColStd_MapOfInteger aMapOfTreated;
1940 // begin cycle on faces from start index and run on vector till the end
1941 // and from begin to start index to cover whole vector
1942 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1943 bool isStartTreat = false;
1944 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1946 if ( fi == aStartIndx )
1947 isStartTreat = true;
1948 // as result next time when fi will be equal to aStartIndx
1950 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1951 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1954 aMapOfTreated.Add( aFacePtr->GetID() );
1955 TColStd_MapOfInteger aResFaces;
1956 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1957 aMapOfNonManifold, aResFaces ) )
1959 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1960 for ( ; anItr.More(); anItr.Next() )
1962 int aFaceId = anItr.Key();
1963 aMapOfTreated.Add( aFaceId );
1964 myMapIds.Add( aFaceId );
1967 if ( fi == ( myAllFacePtr.size() - 1 ) )
1969 } // end run on vector of faces
1970 return !myMapIds.IsEmpty();
1973 static void getLinks( const SMDS_MeshFace* theFace,
1974 ManifoldPart::TVectorOfLink& theLinks )
1976 int aNbNode = theFace->NbNodes();
1977 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1979 SMDS_MeshNode* aNode = 0;
1980 for ( ; aNodeItr->more() && i <= aNbNode; )
1983 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
1987 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
1989 ManifoldPart::Link aLink( aN1, aN2 );
1990 theLinks.push_back( aLink );
1994 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
1997 int aNbNode = theFace->NbNodes();
1998 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
1999 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2001 for ( ; aNodeItr->more() && i <= 4; i++ )
2003 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2004 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2007 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2008 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2012 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2015 double len = n.Modulus();
2022 bool ManifoldPart::findConnected
2023 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2024 SMDS_MeshFace* theStartFace,
2025 ManifoldPart::TMapOfLink& theNonManifold,
2026 TColStd_MapOfInteger& theResFaces )
2028 theResFaces.Clear();
2029 if ( !theAllFacePtrInt.size() )
2032 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2034 myMapBadGeomIds.Add( theStartFace->GetID() );
2038 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2039 ManifoldPart::TVectorOfLink aSeqOfBoundary;
2040 theResFaces.Add( theStartFace->GetID() );
2041 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2043 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2044 aDMapLinkFace, theNonManifold, theStartFace );
2046 bool isDone = false;
2047 while ( !isDone && aMapOfBoundary.size() != 0 )
2049 bool isToReset = false;
2050 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2051 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2053 ManifoldPart::Link aLink = *pLink;
2054 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2056 // each link could be treated only once
2057 aMapToSkip.insert( aLink );
2059 ManifoldPart::TVectorOfFacePtr aFaces;
2061 if ( myIsOnlyManifold &&
2062 (theNonManifold.find( aLink ) != theNonManifold.end()) )
2066 getFacesByLink( aLink, aFaces );
2067 // filter the element to keep only indicated elements
2068 ManifoldPart::TVectorOfFacePtr aFiltered;
2069 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2070 for ( ; pFace != aFaces.end(); ++pFace )
2072 SMDS_MeshFace* aFace = *pFace;
2073 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2074 aFiltered.push_back( aFace );
2077 if ( aFaces.size() < 2 ) // no neihgbour faces
2079 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2081 theNonManifold.insert( aLink );
2086 // compare normal with normals of neighbor element
2087 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2088 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2089 for ( ; pFace != aFaces.end(); ++pFace )
2091 SMDS_MeshFace* aNextFace = *pFace;
2092 if ( aPrevFace == aNextFace )
2094 int anNextFaceID = aNextFace->GetID();
2095 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2096 // should not be with non manifold restriction. probably bad topology
2098 // check if face was treated and skipped
2099 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2100 !isInPlane( aPrevFace, aNextFace ) )
2102 // add new element to connected and extend the boundaries.
2103 theResFaces.Add( anNextFaceID );
2104 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2105 aDMapLinkFace, theNonManifold, aNextFace );
2109 isDone = !isToReset;
2112 return !theResFaces.IsEmpty();
2115 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2116 const SMDS_MeshFace* theFace2 )
2118 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2119 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2120 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2122 myMapBadGeomIds.Add( theFace2->GetID() );
2125 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2131 void ManifoldPart::expandBoundary
2132 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
2133 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
2134 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2135 ManifoldPart::TMapOfLink& theNonManifold,
2136 SMDS_MeshFace* theNextFace ) const
2138 ManifoldPart::TVectorOfLink aLinks;
2139 getLinks( theNextFace, aLinks );
2140 int aNbLink = aLinks.size();
2141 for ( int i = 0; i < aNbLink; i++ )
2143 ManifoldPart::Link aLink = aLinks[ i ];
2144 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2146 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2148 if ( myIsOnlyManifold )
2150 // remove from boundary
2151 theMapOfBoundary.erase( aLink );
2152 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2153 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2155 ManifoldPart::Link aBoundLink = *pLink;
2156 if ( aBoundLink.IsEqual( aLink ) )
2158 theSeqOfBoundary.erase( pLink );
2166 theMapOfBoundary.insert( aLink );
2167 theSeqOfBoundary.push_back( aLink );
2168 theDMapLinkFacePtr[ aLink ] = theNextFace;
2173 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2174 ManifoldPart::TVectorOfFacePtr& theFaces ) const
2176 SMDS_Mesh::SetOfFaces aSetOfFaces;
2177 // take all faces that shared first node
2178 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2179 for ( ; anItr->more(); )
2181 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2184 aSetOfFaces.Add( aFace );
2186 // take all faces that shared second node
2187 anItr = theLink.myNode2->facesIterator();
2188 // find the common part of two sets
2189 for ( ; anItr->more(); )
2191 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2192 if ( aSetOfFaces.Contains( aFace ) )
2193 theFaces.push_back( aFace );
2202 ElementsOnSurface::ElementsOnSurface()
2206 myType = SMDSAbs_All;
2208 myToler = Precision::Confusion();
2211 ElementsOnSurface::~ElementsOnSurface()
2216 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2218 if ( myMesh == theMesh )
2225 bool ElementsOnSurface::IsSatisfy( long theElementId )
2227 return myIds.Contains( theElementId );
2230 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2233 void ElementsOnSurface::SetTolerance( const double theToler )
2234 { myToler = theToler; }
2236 double ElementsOnSurface::GetTolerance() const
2241 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2242 const SMDSAbs_ElementType theType )
2246 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2251 TopoDS_Face aFace = TopoDS::Face( theShape );
2252 mySurf = BRep_Tool::Surface( aFace );
2255 void ElementsOnSurface::process()
2258 if ( mySurf.IsNull() )
2264 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2266 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2267 for(; anIter->more(); )
2268 process( anIter->next() );
2271 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2273 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2274 for(; anIter->more(); )
2275 process( anIter->next() );
2278 if ( myType == SMDSAbs_Node )
2280 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2281 for(; anIter->more(); )
2282 process( anIter->next() );
2286 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2288 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2289 bool isSatisfy = true;
2290 for ( ; aNodeItr->more(); )
2292 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2293 if ( !isOnSurface( aNode ) )
2300 myIds.Add( theElemPtr->GetID() );
2303 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2305 if ( mySurf.IsNull() )
2308 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2309 double aToler2 = myToler * myToler;
2310 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2312 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2313 if ( aPln.SquareDistance( aPnt ) > aToler2 )
2316 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2318 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2319 double aRad = aCyl.Radius();
2320 gp_Ax3 anAxis = aCyl.Position();
2321 gp_XYZ aLoc = aCyl.Location().XYZ();
2322 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2323 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2324 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )