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.0 / PI;
230 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
232 //const double aBestAngle = PI / nbNodes;
233 const double aBestAngle = 180.0 - ( 360.0 / double(nbNodes) );
234 return ( fabs( aBestAngle - Value ));
237 SMDSAbs_ElementType MinimumAngle::GetType() const
245 Description : Functor for calculating aspect ratio
247 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
249 int nbNodes = P.size();
254 // Compute lengths of the sides
256 //double aLen[ nbNodes ];
258 double aLen [nbNodes];
260 double* aLen = (double *)new double[nbNodes];
263 for ( int i = 0; i < nbNodes - 1; i++ )
264 aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
265 aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
267 // Compute aspect ratio
271 double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
272 if ( anArea <= Precision::Confusion() )
274 double aMaxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
275 static double aCoef = sqrt( 3. ) / 4;
277 return aCoef * aMaxLen * aMaxLen / anArea;
281 double aMinLen = aLen[ 0 ];
282 double aMaxLen = aLen[ 0 ];
284 for(int i = 1; i < nbNodes ; i++ ){
285 aMinLen = Min( aMinLen, aLen[ i ] );
286 aMaxLen = Max( aMaxLen, aLen[ i ] );
291 if ( aMinLen <= Precision::Confusion() )
294 return aMaxLen / aMinLen;
298 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
300 // the aspect ratio is in the range [1.0,infinity]
303 return Value / 1000.;
306 SMDSAbs_ElementType AspectRatio::GetType() const
313 Class : AspectRatio3D
314 Description : Functor for calculating aspect ratio
318 inline double getHalfPerimeter(double theTria[3]){
319 return (theTria[0] + theTria[1] + theTria[2])/2.0;
322 inline double getArea(double theHalfPerim, double theTria[3]){
323 return sqrt(theHalfPerim*
324 (theHalfPerim-theTria[0])*
325 (theHalfPerim-theTria[1])*
326 (theHalfPerim-theTria[2]));
329 inline double getVolume(double theLen[6]){
330 double a2 = theLen[0]*theLen[0];
331 double b2 = theLen[1]*theLen[1];
332 double c2 = theLen[2]*theLen[2];
333 double d2 = theLen[3]*theLen[3];
334 double e2 = theLen[4]*theLen[4];
335 double f2 = theLen[5]*theLen[5];
336 double P = 4.0*a2*b2*d2;
337 double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
338 double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
339 return sqrt(P-Q+R)/12.0;
342 inline double getVolume2(double theLen[6]){
343 double a2 = theLen[0]*theLen[0];
344 double b2 = theLen[1]*theLen[1];
345 double c2 = theLen[2]*theLen[2];
346 double d2 = theLen[3]*theLen[3];
347 double e2 = theLen[4]*theLen[4];
348 double f2 = theLen[5]*theLen[5];
350 double P = a2*e2*(b2+c2+d2+f2-a2-e2);
351 double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
352 double R = c2*d2*(a2+b2+e2+f2-c2-d2);
353 double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
355 return sqrt(P+Q+R-S)/12.0;
358 inline double getVolume(const TSequenceOfXYZ& P){
359 gp_Vec aVec1( P( 2 ) - P( 1 ) );
360 gp_Vec aVec2( P( 3 ) - P( 1 ) );
361 gp_Vec aVec3( P( 4 ) - P( 1 ) );
362 gp_Vec anAreaVec( aVec1 ^ aVec2 );
363 return fabs(aVec3 * anAreaVec) / 6.0;
366 inline double getMaxHeight(double theLen[6])
368 double aHeight = max(theLen[0],theLen[1]);
369 aHeight = max(aHeight,theLen[2]);
370 aHeight = max(aHeight,theLen[3]);
371 aHeight = max(aHeight,theLen[4]);
372 aHeight = max(aHeight,theLen[5]);
378 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
380 double aQuality = 0.0;
381 int nbNodes = P.size();
385 getDistance(P( 1 ),P( 2 )), // a
386 getDistance(P( 2 ),P( 3 )), // b
387 getDistance(P( 3 ),P( 1 )), // c
388 getDistance(P( 2 ),P( 4 )), // d
389 getDistance(P( 3 ),P( 4 )), // e
390 getDistance(P( 1 ),P( 4 )) // f
392 double aTria[4][3] = {
393 {aLen[0],aLen[1],aLen[2]}, // abc
394 {aLen[0],aLen[3],aLen[5]}, // adf
395 {aLen[1],aLen[3],aLen[4]}, // bde
396 {aLen[2],aLen[4],aLen[5]} // cef
398 double aSumArea = 0.0;
399 double aHalfPerimeter = getHalfPerimeter(aTria[0]);
400 double anArea = getArea(aHalfPerimeter,aTria[0]);
402 aHalfPerimeter = getHalfPerimeter(aTria[1]);
403 anArea = getArea(aHalfPerimeter,aTria[1]);
405 aHalfPerimeter = getHalfPerimeter(aTria[2]);
406 anArea = getArea(aHalfPerimeter,aTria[2]);
408 aHalfPerimeter = getHalfPerimeter(aTria[3]);
409 anArea = getArea(aHalfPerimeter,aTria[3]);
411 double aVolume = getVolume(P);
412 //double aVolume = getVolume(aLen);
413 double aHeight = getMaxHeight(aLen);
414 static double aCoeff = sqrt(6.0)/36.0;
415 aQuality = aCoeff*aHeight*aSumArea/aVolume;
420 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
421 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
424 gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
425 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
428 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
429 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
432 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
433 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
439 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
440 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
443 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
444 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
447 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
448 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
451 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
452 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
455 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
456 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
459 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
460 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
466 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
467 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
470 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
471 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
474 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
475 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
478 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
479 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
482 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
483 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
486 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
487 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
490 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
491 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
494 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
495 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
498 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
499 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
502 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
503 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
506 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
507 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
510 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
511 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
514 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
515 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
518 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
519 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
522 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
523 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
526 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
527 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
530 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
531 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
534 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
535 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
538 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
539 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
542 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
543 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
546 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
547 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
550 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
551 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
554 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
555 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
558 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
559 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
562 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
563 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
566 gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
567 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
570 gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
571 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
574 gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
575 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
578 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
579 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
582 gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
583 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
586 gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
587 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
590 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
591 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
594 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
595 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
603 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
605 // the aspect ratio is in the range [1.0,infinity]
608 return Value / 1000.;
611 SMDSAbs_ElementType AspectRatio3D::GetType() const
613 return SMDSAbs_Volume;
619 Description : Functor for calculating warping
621 double Warping::GetValue( const TSequenceOfXYZ& P )
626 gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4;
628 double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
629 double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
630 double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
631 double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
633 return Max( Max( A1, A2 ), Max( A3, A4 ) );
636 double Warping::ComputeA( const gp_XYZ& thePnt1,
637 const gp_XYZ& thePnt2,
638 const gp_XYZ& thePnt3,
639 const gp_XYZ& theG ) const
641 double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
642 double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
643 double L = Min( aLen1, aLen2 ) * 0.5;
644 if ( L < Precision::Confusion())
647 gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
648 gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
649 gp_XYZ N = GI.Crossed( GJ );
651 if ( N.Modulus() < gp::Resolution() )
656 double H = ( thePnt2 - theG ).Dot( N );
657 return asin( fabs( H / L ) ) * 180 / PI;
660 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
662 // the warp is in the range [0.0,PI/2]
663 // 0.0 = good (no warp)
664 // PI/2 = bad (face pliee)
668 SMDSAbs_ElementType Warping::GetType() const
676 Description : Functor for calculating taper
678 double Taper::GetValue( const TSequenceOfXYZ& P )
684 double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2;
685 double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2;
686 double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2;
687 double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2;
689 double JA = 0.25 * ( J1 + J2 + J3 + J4 );
690 if ( JA <= Precision::Confusion() )
693 double T1 = fabs( ( J1 - JA ) / JA );
694 double T2 = fabs( ( J2 - JA ) / JA );
695 double T3 = fabs( ( J3 - JA ) / JA );
696 double T4 = fabs( ( J4 - JA ) / JA );
698 return Max( Max( T1, T2 ), Max( T3, T4 ) );
701 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
703 // the taper is in the range [0.0,1.0]
704 // 0.0 = good (no taper)
705 // 1.0 = bad (les cotes opposes sont allignes)
709 SMDSAbs_ElementType Taper::GetType() const
717 Description : Functor for calculating skew in degrees
719 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
721 gp_XYZ p12 = ( p2 + p1 ) / 2;
722 gp_XYZ p23 = ( p3 + p2 ) / 2;
723 gp_XYZ p31 = ( p3 + p1 ) / 2;
725 gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
727 return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
730 double Skew::GetValue( const TSequenceOfXYZ& P )
732 if ( P.size() != 3 && P.size() != 4 )
736 static double PI2 = PI / 2;
739 double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
740 double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
741 double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
743 return Max( A0, Max( A1, A2 ) ) * 180 / PI;
747 gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2;
748 gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2;
749 gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2;
750 gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2;
752 gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
753 double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
754 ? 0 : fabs( PI2 - v1.Angle( v2 ) );
760 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
762 // the skew is in the range [0.0,PI/2].
768 SMDSAbs_ElementType Skew::GetType() const
776 Description : Functor for calculating area
778 double Area::GetValue( const TSequenceOfXYZ& P )
782 return getArea( P( 1 ), P( 2 ), P( 3 ) );
783 else if (P.size() > 3)
784 aArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
788 for (int i=4; i<=P.size(); i++)
789 aArea += getArea(P(1),P(i-1),P(i));
793 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
798 SMDSAbs_ElementType Area::GetType() const
806 Description : Functor for calculating length off edge
808 double Length::GetValue( const TSequenceOfXYZ& P )
810 return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
813 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
818 SMDSAbs_ElementType Length::GetType() const
825 Description : Functor for calculating length of edge
828 double Length2D::GetValue( long theElementId)
832 if (GetPoints(theElementId,P)){
834 double aVal;// = GetValue( P );
835 const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
836 SMDSAbs_ElementType aType = aElem->GetType();
845 aVal = getDistance( P( 1 ), P( 2 ) );
849 if (len == 3){ // triangles
850 double L1 = getDistance(P( 1 ),P( 2 ));
851 double L2 = getDistance(P( 2 ),P( 3 ));
852 double L3 = getDistance(P( 3 ),P( 1 ));
853 aVal = Max(L1,Max(L2,L3));
856 else if (len == 4){ // quadrangles
857 double L1 = getDistance(P( 1 ),P( 2 ));
858 double L2 = getDistance(P( 2 ),P( 3 ));
859 double L3 = getDistance(P( 3 ),P( 4 ));
860 double L4 = getDistance(P( 4 ),P( 1 ));
861 aVal = Max(Max(L1,L2),Max(L3,L4));
865 if (len == 4){ // tetraidrs
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( 1 ),P( 4 ));
870 double L5 = getDistance(P( 2 ),P( 4 ));
871 double L6 = getDistance(P( 3 ),P( 4 ));
872 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
875 else if (len == 5){ // piramids
876 double L1 = getDistance(P( 1 ),P( 2 ));
877 double L2 = getDistance(P( 2 ),P( 3 ));
878 double L3 = getDistance(P( 3 ),P( 1 ));
879 double L4 = getDistance(P( 4 ),P( 1 ));
880 double L5 = getDistance(P( 1 ),P( 5 ));
881 double L6 = getDistance(P( 2 ),P( 5 ));
882 double L7 = getDistance(P( 3 ),P( 5 ));
883 double L8 = getDistance(P( 4 ),P( 5 ));
885 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
886 aVal = Max(aVal,Max(L7,L8));
889 else if (len == 6){ // pentaidres
890 double L1 = getDistance(P( 1 ),P( 2 ));
891 double L2 = getDistance(P( 2 ),P( 3 ));
892 double L3 = getDistance(P( 3 ),P( 1 ));
893 double L4 = getDistance(P( 4 ),P( 5 ));
894 double L5 = getDistance(P( 5 ),P( 6 ));
895 double L6 = getDistance(P( 6 ),P( 4 ));
896 double L7 = getDistance(P( 1 ),P( 4 ));
897 double L8 = getDistance(P( 2 ),P( 5 ));
898 double L9 = getDistance(P( 3 ),P( 6 ));
900 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
901 aVal = Max(aVal,Max(Max(L7,L8),L9));
904 else if (len == 8){ // hexaider
905 double L1 = getDistance(P( 1 ),P( 2 ));
906 double L2 = getDistance(P( 2 ),P( 3 ));
907 double L3 = getDistance(P( 3 ),P( 4 ));
908 double L4 = getDistance(P( 4 ),P( 1 ));
909 double L5 = getDistance(P( 5 ),P( 6 ));
910 double L6 = getDistance(P( 6 ),P( 7 ));
911 double L7 = getDistance(P( 7 ),P( 8 ));
912 double L8 = getDistance(P( 8 ),P( 5 ));
913 double L9 = getDistance(P( 1 ),P( 5 ));
914 double L10= getDistance(P( 2 ),P( 6 ));
915 double L11= getDistance(P( 3 ),P( 7 ));
916 double L12= getDistance(P( 4 ),P( 8 ));
918 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
919 aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
920 aVal = Max(aVal,Max(L11,L12));
932 if ( myPrecision >= 0 )
934 double prec = pow( 10., (double)( myPrecision ) );
935 aVal = floor( aVal * prec + 0.5 ) / prec;
944 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
949 SMDSAbs_ElementType Length2D::GetType() const
954 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
957 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
958 if(thePntId1 > thePntId2){
959 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
963 bool Length2D::Value::operator<(const Length2D::Value& x) const{
964 if(myPntId[0] < x.myPntId[0]) return true;
965 if(myPntId[0] == x.myPntId[0])
966 if(myPntId[1] < x.myPntId[1]) return true;
970 void Length2D::GetValues(TValues& theValues){
972 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
973 for(; anIter->more(); ){
974 const SMDS_MeshFace* anElem = anIter->next();
975 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
980 const SMDS_MeshElement* aNode;
981 if(aNodesIter->more()){
982 aNode = aNodesIter->next();
983 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
984 P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
985 aNodeId[0] = aNodeId[1] = aNode->GetID();
988 for(; aNodesIter->more(); ){
989 aNode = aNodesIter->next();
990 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
991 long anId = aNode->GetID();
993 P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
995 aLength = P[1].Distance(P[2]);
997 Value aValue(aLength,aNodeId[1],anId);
1000 theValues.insert(aValue);
1003 aLength = P[0].Distance(P[1]);
1005 Value aValue(aLength,aNodeId[0],aNodeId[1]);
1006 theValues.insert(aValue);
1011 Class : MultiConnection
1012 Description : Functor for calculating number of faces conneted to the edge
1014 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1018 double MultiConnection::GetValue( long theId )
1020 return getNbMultiConnection( myMesh, theId );
1023 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1028 SMDSAbs_ElementType MultiConnection::GetType() const
1030 return SMDSAbs_Edge;
1034 Class : MultiConnection2D
1035 Description : Functor for calculating number of faces conneted to the edge
1037 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1042 double MultiConnection2D::GetValue( long theElementId )
1047 if (GetPoints(theElementId,P)){
1048 const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1049 SMDSAbs_ElementType aType = anFaceElem->GetType();
1053 TColStd_MapOfInteger aMap;
1061 if (len == 3){ // triangles
1062 int Nb[3] = {0,0,0};
1065 SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1066 if ( anIter != 0 ) {
1067 while( anIter->more() ) {
1068 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1072 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1073 while( anElemIter->more() ) {
1074 const SMDS_MeshElement* anElem = anElemIter->next();
1075 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1076 int anId = anElem->GetID();
1078 if ( anIter->more() ) // i.e. first node
1080 else if ( aMap.Contains( anId ) ){
1084 else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1089 aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1092 case SMDSAbs_Volume:
1097 return aResult;//getNbMultiConnection( myMesh, theId );
1100 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1105 SMDSAbs_ElementType MultiConnection2D::GetType() const
1107 return SMDSAbs_Face;
1110 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1112 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1113 if(thePntId1 > thePntId2){
1114 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1118 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1119 if(myPntId[0] < x.myPntId[0]) return true;
1120 if(myPntId[0] == x.myPntId[0])
1121 if(myPntId[1] < x.myPntId[1]) return true;
1125 void MultiConnection2D::GetValues(MValues& theValues){
1126 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1127 for(; anIter->more(); ){
1128 const SMDS_MeshFace* anElem = anIter->next();
1129 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1132 //int aNbConnects=0;
1133 const SMDS_MeshNode* aNode0;
1134 const SMDS_MeshNode* aNode1;
1135 const SMDS_MeshNode* aNode2;
1136 if(aNodesIter->more()){
1137 aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1139 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1140 aNodeId[0] = aNodeId[1] = aNodes->GetID();
1142 for(; aNodesIter->more(); ){
1143 aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1144 long anId = aNode2->GetID();
1147 Value aValue(aNodeId[1],aNodeId[2]);
1148 MValues::iterator aItr = theValues.find(aValue);
1149 if (aItr != theValues.end()){
1153 theValues[aValue] = 1;
1156 //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1157 aNodeId[1] = aNodeId[2];
1160 Value aValue(aNodeId[0],aNodeId[2]);
1161 MValues::iterator aItr = theValues.find(aValue);
1162 if (aItr != theValues.end()){
1166 theValues[aValue] = 1;
1169 //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1179 Class : BadOrientedVolume
1180 Description : Predicate bad oriented volumes
1183 BadOrientedVolume::BadOrientedVolume()
1188 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1193 bool BadOrientedVolume::IsSatisfy( long theId )
1198 SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1199 return !vTool.IsForward();
1202 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1204 return SMDSAbs_Volume;
1211 Description : Predicate for free borders
1214 FreeBorders::FreeBorders()
1219 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1224 bool FreeBorders::IsSatisfy( long theId )
1226 return getNbMultiConnection( myMesh, theId ) == 1;
1229 SMDSAbs_ElementType FreeBorders::GetType() const
1231 return SMDSAbs_Edge;
1237 Description : Predicate for free Edges
1239 FreeEdges::FreeEdges()
1244 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1249 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
1251 TColStd_MapOfInteger aMap;
1252 for ( int i = 0; i < 2; i++ )
1254 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1255 while( anElemIter->more() )
1257 const SMDS_MeshElement* anElem = anElemIter->next();
1258 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1260 int anId = anElem->GetID();
1264 else if ( aMap.Contains( anId ) && anId != theFaceId )
1272 bool FreeEdges::IsSatisfy( long theId )
1277 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1278 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1281 int nbNodes = aFace->NbNodes();
1282 //const SMDS_MeshNode* aNodes[ nbNodes ];
1284 const SMDS_MeshNode* aNodes [nbNodes];
1286 const SMDS_MeshNode** aNodes = (const SMDS_MeshNode **)new SMDS_MeshNode*[nbNodes];
1289 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
1292 while( anIter->more() )
1294 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1297 aNodes[ i++ ] = aNode;
1301 for ( int i = 0; i < nbNodes - 1; i++ )
1302 if ( IsFreeEdge( &aNodes[ i ], theId ) ) {
1309 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
1310 const Standard_Boolean isFree = IsFreeEdge( &aNodes[ 0 ], theId );
1318 SMDSAbs_ElementType FreeEdges::GetType() const
1320 return SMDSAbs_Face;
1323 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1326 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1327 if(thePntId1 > thePntId2){
1328 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1332 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1333 if(myPntId[0] < x.myPntId[0]) return true;
1334 if(myPntId[0] == x.myPntId[0])
1335 if(myPntId[1] < x.myPntId[1]) return true;
1339 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1340 FreeEdges::TBorders& theRegistry,
1341 FreeEdges::TBorders& theContainer)
1343 if(theRegistry.find(theBorder) == theRegistry.end()){
1344 theRegistry.insert(theBorder);
1345 theContainer.insert(theBorder);
1347 theContainer.erase(theBorder);
1351 void FreeEdges::GetBoreders(TBorders& theBorders)
1354 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1355 for(; anIter->more(); ){
1356 const SMDS_MeshFace* anElem = anIter->next();
1357 long anElemId = anElem->GetID();
1358 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1360 const SMDS_MeshElement* aNode;
1361 if(aNodesIter->more()){
1362 aNode = aNodesIter->next();
1363 aNodeId[0] = aNodeId[1] = aNode->GetID();
1365 for(; aNodesIter->more(); ){
1366 aNode = aNodesIter->next();
1367 long anId = aNode->GetID();
1368 Border aBorder(anElemId,aNodeId[1],anId);
1370 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1371 UpdateBorders(aBorder,aRegistry,theBorders);
1373 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1374 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1375 UpdateBorders(aBorder,aRegistry,theBorders);
1377 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1382 Description : Predicate for Range of Ids.
1383 Range may be specified with two ways.
1384 1. Using AddToRange method
1385 2. With SetRangeStr method. Parameter of this method is a string
1386 like as "1,2,3,50-60,63,67,70-"
1389 //=======================================================================
1390 // name : RangeOfIds
1391 // Purpose : Constructor
1392 //=======================================================================
1393 RangeOfIds::RangeOfIds()
1396 myType = SMDSAbs_All;
1399 //=======================================================================
1401 // Purpose : Set mesh
1402 //=======================================================================
1403 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1408 //=======================================================================
1409 // name : AddToRange
1410 // Purpose : Add ID to the range
1411 //=======================================================================
1412 bool RangeOfIds::AddToRange( long theEntityId )
1414 myIds.Add( theEntityId );
1418 //=======================================================================
1419 // name : GetRangeStr
1420 // Purpose : Get range as a string.
1421 // Example: "1,2,3,50-60,63,67,70-"
1422 //=======================================================================
1423 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1427 TColStd_SequenceOfInteger anIntSeq;
1428 TColStd_SequenceOfAsciiString aStrSeq;
1430 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1431 for ( ; anIter.More(); anIter.Next() )
1433 int anId = anIter.Key();
1434 TCollection_AsciiString aStr( anId );
1435 anIntSeq.Append( anId );
1436 aStrSeq.Append( aStr );
1439 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1441 int aMinId = myMin( i );
1442 int aMaxId = myMax( i );
1444 TCollection_AsciiString aStr;
1445 if ( aMinId != IntegerFirst() )
1450 if ( aMaxId != IntegerLast() )
1453 // find position of the string in result sequence and insert string in it
1454 if ( anIntSeq.Length() == 0 )
1456 anIntSeq.Append( aMinId );
1457 aStrSeq.Append( aStr );
1461 if ( aMinId < anIntSeq.First() )
1463 anIntSeq.Prepend( aMinId );
1464 aStrSeq.Prepend( aStr );
1466 else if ( aMinId > anIntSeq.Last() )
1468 anIntSeq.Append( aMinId );
1469 aStrSeq.Append( aStr );
1472 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1473 if ( aMinId < anIntSeq( j ) )
1475 anIntSeq.InsertBefore( j, aMinId );
1476 aStrSeq.InsertBefore( j, aStr );
1482 if ( aStrSeq.Length() == 0 )
1485 theResStr = aStrSeq( 1 );
1486 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1489 theResStr += aStrSeq( j );
1493 //=======================================================================
1494 // name : SetRangeStr
1495 // Purpose : Define range with string
1496 // Example of entry string: "1,2,3,50-60,63,67,70-"
1497 //=======================================================================
1498 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1504 TCollection_AsciiString aStr = theStr;
1505 aStr.RemoveAll( ' ' );
1506 aStr.RemoveAll( '\t' );
1508 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1509 aStr.Remove( aPos, 2 );
1511 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1513 while ( tmpStr != "" )
1515 tmpStr = aStr.Token( ",", i++ );
1516 int aPos = tmpStr.Search( '-' );
1520 if ( tmpStr.IsIntegerValue() )
1521 myIds.Add( tmpStr.IntegerValue() );
1527 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1528 TCollection_AsciiString aMinStr = tmpStr;
1530 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1531 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1533 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1534 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1537 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1538 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1545 //=======================================================================
1547 // Purpose : Get type of supported entities
1548 //=======================================================================
1549 SMDSAbs_ElementType RangeOfIds::GetType() const
1554 //=======================================================================
1556 // Purpose : Set type of supported entities
1557 //=======================================================================
1558 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1563 //=======================================================================
1565 // Purpose : Verify whether entity satisfies to this rpedicate
1566 //=======================================================================
1567 bool RangeOfIds::IsSatisfy( long theId )
1572 if ( myType == SMDSAbs_Node )
1574 if ( myMesh->FindNode( theId ) == 0 )
1579 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1580 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1584 if ( myIds.Contains( theId ) )
1587 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1588 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1596 Description : Base class for comparators
1598 Comparator::Comparator():
1602 Comparator::~Comparator()
1605 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1608 myFunctor->SetMesh( theMesh );
1611 void Comparator::SetMargin( double theValue )
1613 myMargin = theValue;
1616 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1618 myFunctor = theFunct;
1621 SMDSAbs_ElementType Comparator::GetType() const
1623 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1626 double Comparator::GetMargin()
1634 Description : Comparator "<"
1636 bool LessThan::IsSatisfy( long theId )
1638 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1644 Description : Comparator ">"
1646 bool MoreThan::IsSatisfy( long theId )
1648 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1654 Description : Comparator "="
1657 myToler(Precision::Confusion())
1660 bool EqualTo::IsSatisfy( long theId )
1662 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1665 void EqualTo::SetTolerance( double theToler )
1670 double EqualTo::GetTolerance()
1677 Description : Logical NOT predicate
1679 LogicalNOT::LogicalNOT()
1682 LogicalNOT::~LogicalNOT()
1685 bool LogicalNOT::IsSatisfy( long theId )
1687 return myPredicate && !myPredicate->IsSatisfy( theId );
1690 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1693 myPredicate->SetMesh( theMesh );
1696 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1698 myPredicate = thePred;
1701 SMDSAbs_ElementType LogicalNOT::GetType() const
1703 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1708 Class : LogicalBinary
1709 Description : Base class for binary logical predicate
1711 LogicalBinary::LogicalBinary()
1714 LogicalBinary::~LogicalBinary()
1717 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1720 myPredicate1->SetMesh( theMesh );
1723 myPredicate2->SetMesh( theMesh );
1726 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1728 myPredicate1 = thePredicate;
1731 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1733 myPredicate2 = thePredicate;
1736 SMDSAbs_ElementType LogicalBinary::GetType() const
1738 if ( !myPredicate1 || !myPredicate2 )
1741 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1742 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1744 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1750 Description : Logical AND
1752 bool LogicalAND::IsSatisfy( long theId )
1757 myPredicate1->IsSatisfy( theId ) &&
1758 myPredicate2->IsSatisfy( theId );
1764 Description : Logical OR
1766 bool LogicalOR::IsSatisfy( long theId )
1771 myPredicate1->IsSatisfy( theId ) ||
1772 myPredicate2->IsSatisfy( theId );
1786 void Filter::SetPredicate( PredicatePtr thePredicate )
1788 myPredicate = thePredicate;
1791 template<class TElement, class TIterator, class TPredicate>
1792 inline void FillSequence(const TIterator& theIterator,
1793 TPredicate& thePredicate,
1794 Filter::TIdSequence& theSequence)
1796 if ( theIterator ) {
1797 while( theIterator->more() ) {
1798 TElement anElem = theIterator->next();
1799 long anId = anElem->GetID();
1800 if ( thePredicate->IsSatisfy( anId ) )
1801 theSequence.push_back( anId );
1808 GetElementsId( const SMDS_Mesh* theMesh,
1809 PredicatePtr thePredicate,
1810 TIdSequence& theSequence )
1812 theSequence.clear();
1814 if ( !theMesh || !thePredicate )
1817 thePredicate->SetMesh( theMesh );
1819 SMDSAbs_ElementType aType = thePredicate->GetType();
1822 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
1825 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1828 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1830 case SMDSAbs_Volume:
1831 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1834 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1835 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1836 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1842 Filter::GetElementsId( const SMDS_Mesh* theMesh,
1843 Filter::TIdSequence& theSequence )
1845 GetElementsId(theMesh,myPredicate,theSequence);
1852 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1858 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1859 SMDS_MeshNode* theNode2 )
1865 ManifoldPart::Link::~Link()
1871 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1873 if ( myNode1 == theLink.myNode1 &&
1874 myNode2 == theLink.myNode2 )
1876 else if ( myNode1 == theLink.myNode2 &&
1877 myNode2 == theLink.myNode1 )
1883 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1885 if(myNode1 < x.myNode1) return true;
1886 if(myNode1 == x.myNode1)
1887 if(myNode2 < x.myNode2) return true;
1891 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1892 const ManifoldPart::Link& theLink2 )
1894 return theLink1.IsEqual( theLink2 );
1897 ManifoldPart::ManifoldPart()
1900 myAngToler = Precision::Angular();
1901 myIsOnlyManifold = true;
1904 ManifoldPart::~ManifoldPart()
1909 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
1915 SMDSAbs_ElementType ManifoldPart::GetType() const
1916 { return SMDSAbs_Face; }
1918 bool ManifoldPart::IsSatisfy( long theElementId )
1920 return myMapIds.Contains( theElementId );
1923 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1924 { myAngToler = theAngToler; }
1926 double ManifoldPart::GetAngleTolerance() const
1927 { return myAngToler; }
1929 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1930 { myIsOnlyManifold = theIsOnly; }
1932 void ManifoldPart::SetStartElem( const long theStartId )
1933 { myStartElemId = theStartId; }
1935 bool ManifoldPart::process()
1938 myMapBadGeomIds.Clear();
1940 myAllFacePtr.clear();
1941 myAllFacePtrIntDMap.clear();
1945 // collect all faces into own map
1946 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1947 for (; anFaceItr->more(); )
1949 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1950 myAllFacePtr.push_back( aFacePtr );
1951 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1954 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1958 // the map of non manifold links and bad geometry
1959 TMapOfLink aMapOfNonManifold;
1960 TColStd_MapOfInteger aMapOfTreated;
1962 // begin cycle on faces from start index and run on vector till the end
1963 // and from begin to start index to cover whole vector
1964 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1965 bool isStartTreat = false;
1966 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1968 if ( fi == aStartIndx )
1969 isStartTreat = true;
1970 // as result next time when fi will be equal to aStartIndx
1972 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1973 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1976 aMapOfTreated.Add( aFacePtr->GetID() );
1977 TColStd_MapOfInteger aResFaces;
1978 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1979 aMapOfNonManifold, aResFaces ) )
1981 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1982 for ( ; anItr.More(); anItr.Next() )
1984 int aFaceId = anItr.Key();
1985 aMapOfTreated.Add( aFaceId );
1986 myMapIds.Add( aFaceId );
1989 if ( fi == ( myAllFacePtr.size() - 1 ) )
1991 } // end run on vector of faces
1992 return !myMapIds.IsEmpty();
1995 static void getLinks( const SMDS_MeshFace* theFace,
1996 ManifoldPart::TVectorOfLink& theLinks )
1998 int aNbNode = theFace->NbNodes();
1999 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2001 SMDS_MeshNode* aNode = 0;
2002 for ( ; aNodeItr->more() && i <= aNbNode; )
2005 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
2009 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
2011 ManifoldPart::Link aLink( aN1, aN2 );
2012 theLinks.push_back( aLink );
2016 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
2019 int aNbNode = theFace->NbNodes();
2020 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
2021 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2023 for ( ; aNodeItr->more() && i <= 4; i++ )
2025 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2026 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2029 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2030 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2034 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2037 double len = n.Modulus();
2044 bool ManifoldPart::findConnected
2045 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2046 SMDS_MeshFace* theStartFace,
2047 ManifoldPart::TMapOfLink& theNonManifold,
2048 TColStd_MapOfInteger& theResFaces )
2050 theResFaces.Clear();
2051 if ( !theAllFacePtrInt.size() )
2054 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2056 myMapBadGeomIds.Add( theStartFace->GetID() );
2060 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2061 ManifoldPart::TVectorOfLink aSeqOfBoundary;
2062 theResFaces.Add( theStartFace->GetID() );
2063 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2065 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2066 aDMapLinkFace, theNonManifold, theStartFace );
2068 bool isDone = false;
2069 while ( !isDone && aMapOfBoundary.size() != 0 )
2071 bool isToReset = false;
2072 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2073 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2075 ManifoldPart::Link aLink = *pLink;
2076 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2078 // each link could be treated only once
2079 aMapToSkip.insert( aLink );
2081 ManifoldPart::TVectorOfFacePtr aFaces;
2083 if ( myIsOnlyManifold &&
2084 (theNonManifold.find( aLink ) != theNonManifold.end()) )
2088 getFacesByLink( aLink, aFaces );
2089 // filter the element to keep only indicated elements
2090 ManifoldPart::TVectorOfFacePtr aFiltered;
2091 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2092 for ( ; pFace != aFaces.end(); ++pFace )
2094 SMDS_MeshFace* aFace = *pFace;
2095 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2096 aFiltered.push_back( aFace );
2099 if ( aFaces.size() < 2 ) // no neihgbour faces
2101 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2103 theNonManifold.insert( aLink );
2108 // compare normal with normals of neighbor element
2109 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2110 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2111 for ( ; pFace != aFaces.end(); ++pFace )
2113 SMDS_MeshFace* aNextFace = *pFace;
2114 if ( aPrevFace == aNextFace )
2116 int anNextFaceID = aNextFace->GetID();
2117 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2118 // should not be with non manifold restriction. probably bad topology
2120 // check if face was treated and skipped
2121 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2122 !isInPlane( aPrevFace, aNextFace ) )
2124 // add new element to connected and extend the boundaries.
2125 theResFaces.Add( anNextFaceID );
2126 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2127 aDMapLinkFace, theNonManifold, aNextFace );
2131 isDone = !isToReset;
2134 return !theResFaces.IsEmpty();
2137 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2138 const SMDS_MeshFace* theFace2 )
2140 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2141 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2142 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2144 myMapBadGeomIds.Add( theFace2->GetID() );
2147 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2153 void ManifoldPart::expandBoundary
2154 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
2155 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
2156 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2157 ManifoldPart::TMapOfLink& theNonManifold,
2158 SMDS_MeshFace* theNextFace ) const
2160 ManifoldPart::TVectorOfLink aLinks;
2161 getLinks( theNextFace, aLinks );
2162 int aNbLink = (int)aLinks.size();
2163 for ( int i = 0; i < aNbLink; i++ )
2165 ManifoldPart::Link aLink = aLinks[ i ];
2166 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2168 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2170 if ( myIsOnlyManifold )
2172 // remove from boundary
2173 theMapOfBoundary.erase( aLink );
2174 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2175 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2177 ManifoldPart::Link aBoundLink = *pLink;
2178 if ( aBoundLink.IsEqual( aLink ) )
2180 theSeqOfBoundary.erase( pLink );
2188 theMapOfBoundary.insert( aLink );
2189 theSeqOfBoundary.push_back( aLink );
2190 theDMapLinkFacePtr[ aLink ] = theNextFace;
2195 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2196 ManifoldPart::TVectorOfFacePtr& theFaces ) const
2198 SMDS_Mesh::SetOfFaces aSetOfFaces;
2199 // take all faces that shared first node
2200 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2201 for ( ; anItr->more(); )
2203 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2206 aSetOfFaces.Add( aFace );
2208 // take all faces that shared second node
2209 anItr = theLink.myNode2->facesIterator();
2210 // find the common part of two sets
2211 for ( ; anItr->more(); )
2213 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2214 if ( aSetOfFaces.Contains( aFace ) )
2215 theFaces.push_back( aFace );
2224 ElementsOnSurface::ElementsOnSurface()
2228 myType = SMDSAbs_All;
2230 myToler = Precision::Confusion();
2233 ElementsOnSurface::~ElementsOnSurface()
2238 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2240 if ( myMesh == theMesh )
2247 bool ElementsOnSurface::IsSatisfy( long theElementId )
2249 return myIds.Contains( theElementId );
2252 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2255 void ElementsOnSurface::SetTolerance( const double theToler )
2256 { myToler = theToler; }
2258 double ElementsOnSurface::GetTolerance() const
2263 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2264 const SMDSAbs_ElementType theType )
2268 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2273 TopoDS_Face aFace = TopoDS::Face( theShape );
2274 mySurf = BRep_Tool::Surface( aFace );
2277 void ElementsOnSurface::process()
2280 if ( mySurf.IsNull() )
2286 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2288 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2289 for(; anIter->more(); )
2290 process( anIter->next() );
2293 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2295 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2296 for(; anIter->more(); )
2297 process( anIter->next() );
2300 if ( myType == SMDSAbs_Node )
2302 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2303 for(; anIter->more(); )
2304 process( anIter->next() );
2308 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2310 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2311 bool isSatisfy = true;
2312 for ( ; aNodeItr->more(); )
2314 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2315 if ( !isOnSurface( aNode ) )
2322 myIds.Add( theElemPtr->GetID() );
2325 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2327 if ( mySurf.IsNull() )
2330 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2331 double aToler2 = myToler * myToler;
2332 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2334 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2335 if ( aPln.SquareDistance( aPnt ) > aToler2 )
2338 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2340 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2341 double aRad = aCyl.Radius();
2342 gp_Ax3 anAxis = aCyl.Position();
2343 gp_XYZ aLoc = aCyl.Location().XYZ();
2344 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2345 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2346 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )