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();
250 if ( nbNodes != 3 && nbNodes != 4 )
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 = Min( Min( aLen[ 0 ], aLen[ 1 ] ), Min( aLen[ 2 ], aLen[ 3 ] ) );
275 if ( aMinLen <= Precision::Confusion() )
277 double aMaxLen = Max( Max( aLen[ 0 ], aLen[ 1 ] ), Max( aLen[ 2 ], aLen[ 3 ] ) );
279 return aMaxLen / aMinLen;
283 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
285 // the aspect ratio is in the range [1.0,infinity]
288 return Value / 1000.;
291 SMDSAbs_ElementType AspectRatio::GetType() const
298 Class : AspectRatio3D
299 Description : Functor for calculating aspect ratio
303 inline double getHalfPerimeter(double theTria[3]){
304 return (theTria[0] + theTria[1] + theTria[2])/2.0;
307 inline double getArea(double theHalfPerim, double theTria[3]){
308 return sqrt(theHalfPerim*
309 (theHalfPerim-theTria[0])*
310 (theHalfPerim-theTria[1])*
311 (theHalfPerim-theTria[2]));
314 inline double getVolume(double theLen[6]){
315 double a2 = theLen[0]*theLen[0];
316 double b2 = theLen[1]*theLen[1];
317 double c2 = theLen[2]*theLen[2];
318 double d2 = theLen[3]*theLen[3];
319 double e2 = theLen[4]*theLen[4];
320 double f2 = theLen[5]*theLen[5];
321 double P = 4.0*a2*b2*d2;
322 double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
323 double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
324 return sqrt(P-Q+R)/12.0;
327 inline double getVolume2(double theLen[6]){
328 double a2 = theLen[0]*theLen[0];
329 double b2 = theLen[1]*theLen[1];
330 double c2 = theLen[2]*theLen[2];
331 double d2 = theLen[3]*theLen[3];
332 double e2 = theLen[4]*theLen[4];
333 double f2 = theLen[5]*theLen[5];
335 double P = a2*e2*(b2+c2+d2+f2-a2-e2);
336 double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
337 double R = c2*d2*(a2+b2+e2+f2-c2-d2);
338 double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
340 return sqrt(P+Q+R-S)/12.0;
343 inline double getVolume(const TSequenceOfXYZ& P){
344 gp_Vec aVec1( P( 2 ) - P( 1 ) );
345 gp_Vec aVec2( P( 3 ) - P( 1 ) );
346 gp_Vec aVec3( P( 4 ) - P( 1 ) );
347 gp_Vec anAreaVec( aVec1 ^ aVec2 );
348 return abs(aVec3 * anAreaVec) / 6.0;
351 inline double getMaxHeight(double theLen[6])
353 double aHeight = max(theLen[0],theLen[1]);
354 aHeight = max(aHeight,theLen[2]);
355 aHeight = max(aHeight,theLen[3]);
356 aHeight = max(aHeight,theLen[4]);
357 aHeight = max(aHeight,theLen[5]);
363 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
365 double aQuality = 0.0;
366 int nbNodes = P.size();
370 getDistance(P( 1 ),P( 2 )), // a
371 getDistance(P( 2 ),P( 3 )), // b
372 getDistance(P( 3 ),P( 1 )), // c
373 getDistance(P( 2 ),P( 4 )), // d
374 getDistance(P( 3 ),P( 4 )), // e
375 getDistance(P( 1 ),P( 4 )) // f
377 double aTria[4][3] = {
378 {aLen[0],aLen[1],aLen[2]}, // abc
379 {aLen[0],aLen[3],aLen[5]}, // adf
380 {aLen[1],aLen[3],aLen[4]}, // bde
381 {aLen[2],aLen[4],aLen[5]} // cef
383 double aSumArea = 0.0;
384 double aHalfPerimeter = getHalfPerimeter(aTria[0]);
385 double anArea = getArea(aHalfPerimeter,aTria[0]);
387 aHalfPerimeter = getHalfPerimeter(aTria[1]);
388 anArea = getArea(aHalfPerimeter,aTria[1]);
390 aHalfPerimeter = getHalfPerimeter(aTria[2]);
391 anArea = getArea(aHalfPerimeter,aTria[2]);
393 aHalfPerimeter = getHalfPerimeter(aTria[3]);
394 anArea = getArea(aHalfPerimeter,aTria[3]);
396 double aVolume = getVolume(P);
397 //double aVolume = getVolume(aLen);
398 double aHeight = getMaxHeight(aLen);
399 static double aCoeff = sqrt(6.0)/36.0;
400 aQuality = aCoeff*aHeight*aSumArea/aVolume;
405 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
406 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
409 gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
410 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
413 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
414 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
417 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
418 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
424 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
425 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
428 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
429 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
432 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
433 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
436 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
437 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
440 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
441 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
444 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
445 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( 1 ),P( 2 ),P( 5 ),P( 4 )};
456 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
459 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
460 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
463 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
464 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
467 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
468 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
471 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
472 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
475 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
476 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
479 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
480 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
483 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
484 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
487 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
488 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
491 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
492 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
495 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
496 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
499 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
500 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
503 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
504 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
507 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
508 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
511 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
512 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
515 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
516 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
519 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
520 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
523 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
524 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
527 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
528 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
531 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
532 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
535 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
536 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
539 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
540 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
543 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
544 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
547 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
548 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
551 gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
552 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
555 gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
556 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
559 gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
560 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
563 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
564 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
567 gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
568 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
571 gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
572 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
575 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
576 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
579 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
580 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
588 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
590 // the aspect ratio is in the range [1.0,infinity]
593 return Value / 1000.;
596 SMDSAbs_ElementType AspectRatio3D::GetType() const
598 return SMDSAbs_Volume;
604 Description : Functor for calculating warping
606 double Warping::GetValue( const TSequenceOfXYZ& P )
611 gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4;
613 double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
614 double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
615 double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
616 double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
618 return Max( Max( A1, A2 ), Max( A3, A4 ) );
621 double Warping::ComputeA( const gp_XYZ& thePnt1,
622 const gp_XYZ& thePnt2,
623 const gp_XYZ& thePnt3,
624 const gp_XYZ& theG ) const
626 double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
627 double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
628 double L = Min( aLen1, aLen2 ) * 0.5;
629 if ( L < Precision::Confusion())
632 gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
633 gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
634 gp_XYZ N = GI.Crossed( GJ );
636 if ( N.Modulus() < gp::Resolution() )
641 double H = ( thePnt2 - theG ).Dot( N );
642 return asin( fabs( H / L ) ) * 180 / PI;
645 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
647 // the warp is in the range [0.0,PI/2]
648 // 0.0 = good (no warp)
649 // PI/2 = bad (face pliee)
653 SMDSAbs_ElementType Warping::GetType() const
661 Description : Functor for calculating taper
663 double Taper::GetValue( const TSequenceOfXYZ& P )
669 double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2;
670 double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2;
671 double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2;
672 double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2;
674 double JA = 0.25 * ( J1 + J2 + J3 + J4 );
675 if ( JA <= Precision::Confusion() )
678 double T1 = fabs( ( J1 - JA ) / JA );
679 double T2 = fabs( ( J2 - JA ) / JA );
680 double T3 = fabs( ( J3 - JA ) / JA );
681 double T4 = fabs( ( J4 - JA ) / JA );
683 return Max( Max( T1, T2 ), Max( T3, T4 ) );
686 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
688 // the taper is in the range [0.0,1.0]
689 // 0.0 = good (no taper)
690 // 1.0 = bad (les cotes opposes sont allignes)
694 SMDSAbs_ElementType Taper::GetType() const
702 Description : Functor for calculating skew in degrees
704 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
706 gp_XYZ p12 = ( p2 + p1 ) / 2;
707 gp_XYZ p23 = ( p3 + p2 ) / 2;
708 gp_XYZ p31 = ( p3 + p1 ) / 2;
710 gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
712 return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
715 double Skew::GetValue( const TSequenceOfXYZ& P )
717 if ( P.size() != 3 && P.size() != 4 )
721 static double PI2 = PI / 2;
724 double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
725 double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
726 double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
728 return Max( A0, Max( A1, A2 ) ) * 180 / PI;
732 gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2;
733 gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2;
734 gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2;
735 gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2;
737 gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
738 double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
739 ? 0 : fabs( PI2 - v1.Angle( v2 ) );
745 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
747 // the skew is in the range [0.0,PI/2].
753 SMDSAbs_ElementType Skew::GetType() const
761 Description : Functor for calculating area
763 double Area::GetValue( const TSequenceOfXYZ& P )
767 return getArea( P( 1 ), P( 2 ), P( 3 ) );
768 else if (P.size() > 3)
769 aArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
773 for (int i=4; i<=P.size(); i++)
774 aArea += getArea(P(1),P(i-1),P(i));
778 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
783 SMDSAbs_ElementType Area::GetType() const
791 Description : Functor for calculating length off edge
793 double Length::GetValue( const TSequenceOfXYZ& P )
795 return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
798 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
803 SMDSAbs_ElementType Length::GetType() const
810 Description : Functor for calculating length of edge
813 double Length2D::GetValue( long theElementId)
817 if (GetPoints(theElementId,P)){
819 double aVal;// = GetValue( P );
820 const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
821 SMDSAbs_ElementType aType = aElem->GetType();
830 aVal = getDistance( P( 1 ), P( 2 ) );
834 if (len == 3){ // triangles
835 double L1 = getDistance(P( 1 ),P( 2 ));
836 double L2 = getDistance(P( 2 ),P( 3 ));
837 double L3 = getDistance(P( 3 ),P( 1 ));
838 aVal = Max(L1,Max(L2,L3));
841 else if (len == 4){ // quadrangles
842 double L1 = getDistance(P( 1 ),P( 2 ));
843 double L2 = getDistance(P( 2 ),P( 3 ));
844 double L3 = getDistance(P( 3 ),P( 4 ));
845 double L4 = getDistance(P( 4 ),P( 1 ));
846 aVal = Max(Max(L1,L2),Max(L3,L4));
850 if (len == 4){ // tetraidrs
851 double L1 = getDistance(P( 1 ),P( 2 ));
852 double L2 = getDistance(P( 2 ),P( 3 ));
853 double L3 = getDistance(P( 3 ),P( 1 ));
854 double L4 = getDistance(P( 1 ),P( 4 ));
855 double L5 = getDistance(P( 2 ),P( 4 ));
856 double L6 = getDistance(P( 3 ),P( 4 ));
857 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
860 else if (len == 5){ // piramids
861 double L1 = getDistance(P( 1 ),P( 2 ));
862 double L2 = getDistance(P( 2 ),P( 3 ));
863 double L3 = getDistance(P( 3 ),P( 1 ));
864 double L4 = getDistance(P( 4 ),P( 1 ));
865 double L5 = getDistance(P( 1 ),P( 5 ));
866 double L6 = getDistance(P( 2 ),P( 5 ));
867 double L7 = getDistance(P( 3 ),P( 5 ));
868 double L8 = getDistance(P( 4 ),P( 5 ));
870 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
871 aVal = Max(aVal,Max(L7,L8));
874 else if (len == 6){ // pentaidres
875 double L1 = getDistance(P( 1 ),P( 2 ));
876 double L2 = getDistance(P( 2 ),P( 3 ));
877 double L3 = getDistance(P( 3 ),P( 1 ));
878 double L4 = getDistance(P( 4 ),P( 5 ));
879 double L5 = getDistance(P( 5 ),P( 6 ));
880 double L6 = getDistance(P( 6 ),P( 4 ));
881 double L7 = getDistance(P( 1 ),P( 4 ));
882 double L8 = getDistance(P( 2 ),P( 5 ));
883 double L9 = getDistance(P( 3 ),P( 6 ));
885 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
886 aVal = Max(aVal,Max(Max(L7,L8),L9));
889 else if (len == 8){ // hexaider
890 double L1 = getDistance(P( 1 ),P( 2 ));
891 double L2 = getDistance(P( 2 ),P( 3 ));
892 double L3 = getDistance(P( 3 ),P( 4 ));
893 double L4 = getDistance(P( 4 ),P( 1 ));
894 double L5 = getDistance(P( 5 ),P( 6 ));
895 double L6 = getDistance(P( 6 ),P( 7 ));
896 double L7 = getDistance(P( 7 ),P( 8 ));
897 double L8 = getDistance(P( 8 ),P( 5 ));
898 double L9 = getDistance(P( 1 ),P( 5 ));
899 double L10= getDistance(P( 2 ),P( 6 ));
900 double L11= getDistance(P( 3 ),P( 7 ));
901 double L12= getDistance(P( 4 ),P( 8 ));
903 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
904 aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
905 aVal = Max(aVal,Max(L11,L12));
917 if ( myPrecision >= 0 )
919 double prec = pow( 10., (double)( myPrecision ) );
920 aVal = floor( aVal * prec + 0.5 ) / prec;
929 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
934 SMDSAbs_ElementType Length2D::GetType() const
939 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
942 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
943 if(thePntId1 > thePntId2){
944 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
948 bool Length2D::Value::operator<(const Length2D::Value& x) const{
949 if(myPntId[0] < x.myPntId[0]) return true;
950 if(myPntId[0] == x.myPntId[0])
951 if(myPntId[1] < x.myPntId[1]) return true;
955 void Length2D::GetValues(TValues& theValues){
957 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
958 for(; anIter->more(); ){
959 const SMDS_MeshFace* anElem = anIter->next();
960 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
965 const SMDS_MeshElement* aNode;
966 if(aNodesIter->more()){
967 aNode = aNodesIter->next();
968 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
969 P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
970 aNodeId[0] = aNodeId[1] = aNode->GetID();
973 for(; aNodesIter->more(); ){
974 aNode = aNodesIter->next();
975 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
976 long anId = aNode->GetID();
978 P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
980 aLength = P[1].Distance(P[2]);
982 Value aValue(aLength,aNodeId[1],anId);
985 theValues.insert(aValue);
988 aLength = P[0].Distance(P[1]);
990 Value aValue(aLength,aNodeId[0],aNodeId[1]);
991 theValues.insert(aValue);
996 Class : MultiConnection
997 Description : Functor for calculating number of faces conneted to the edge
999 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1003 double MultiConnection::GetValue( long theId )
1005 return getNbMultiConnection( myMesh, theId );
1008 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1013 SMDSAbs_ElementType MultiConnection::GetType() const
1015 return SMDSAbs_Edge;
1019 Class : MultiConnection2D
1020 Description : Functor for calculating number of faces conneted to the edge
1022 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1027 double MultiConnection2D::GetValue( long theElementId )
1032 if (GetPoints(theElementId,P)){
1033 const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1034 SMDSAbs_ElementType aType = anFaceElem->GetType();
1038 TColStd_MapOfInteger aMap;
1046 if (len == 3){ // triangles
1047 int Nb[3] = {0,0,0};
1050 SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1051 if ( anIter != 0 ) {
1052 while( anIter->more() ) {
1053 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1057 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1058 while( anElemIter->more() ) {
1059 const SMDS_MeshElement* anElem = anElemIter->next();
1060 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1061 int anId = anElem->GetID();
1063 if ( anIter->more() ) // i.e. first node
1065 else if ( aMap.Contains( anId ) ){
1069 else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1074 aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1077 case SMDSAbs_Volume:
1082 return aResult;//getNbMultiConnection( myMesh, theId );
1085 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1090 SMDSAbs_ElementType MultiConnection2D::GetType() const
1092 return SMDSAbs_Face;
1095 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1097 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1098 if(thePntId1 > thePntId2){
1099 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1103 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1104 if(myPntId[0] < x.myPntId[0]) return true;
1105 if(myPntId[0] == x.myPntId[0])
1106 if(myPntId[1] < x.myPntId[1]) return true;
1110 void MultiConnection2D::GetValues(MValues& theValues){
1111 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1112 for(; anIter->more(); ){
1113 const SMDS_MeshFace* anElem = anIter->next();
1114 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1117 //int aNbConnects=0;
1118 const SMDS_MeshNode* aNode0;
1119 const SMDS_MeshNode* aNode1;
1120 const SMDS_MeshNode* aNode2;
1121 if(aNodesIter->more()){
1122 aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1124 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1125 aNodeId[0] = aNodeId[1] = aNodes->GetID();
1127 for(; aNodesIter->more(); ){
1128 aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1129 long anId = aNode2->GetID();
1132 Value aValue(aNodeId[1],aNodeId[2]);
1133 MValues::iterator aItr = theValues.find(aValue);
1134 if (aItr != theValues.end()){
1138 theValues[aValue] = 1;
1141 //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1142 aNodeId[1] = aNodeId[2];
1145 Value aValue(aNodeId[0],aNodeId[2]);
1146 MValues::iterator aItr = theValues.find(aValue);
1147 if (aItr != theValues.end()){
1151 theValues[aValue] = 1;
1154 //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1164 Class : BadOrientedVolume
1165 Description : Predicate bad oriented volumes
1168 BadOrientedVolume::BadOrientedVolume()
1173 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1178 bool BadOrientedVolume::IsSatisfy( long theId )
1183 SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1184 return !vTool.IsForward();
1187 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1189 return SMDSAbs_Volume;
1196 Description : Predicate for free borders
1199 FreeBorders::FreeBorders()
1204 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1209 bool FreeBorders::IsSatisfy( long theId )
1211 return getNbMultiConnection( myMesh, theId ) == 1;
1214 SMDSAbs_ElementType FreeBorders::GetType() const
1216 return SMDSAbs_Edge;
1222 Description : Predicate for free Edges
1224 FreeEdges::FreeEdges()
1229 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1234 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
1236 TColStd_MapOfInteger aMap;
1237 for ( int i = 0; i < 2; i++ )
1239 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1240 while( anElemIter->more() )
1242 const SMDS_MeshElement* anElem = anElemIter->next();
1243 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1245 int anId = anElem->GetID();
1249 else if ( aMap.Contains( anId ) && anId != theFaceId )
1257 bool FreeEdges::IsSatisfy( long theId )
1262 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1263 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1266 int nbNodes = aFace->NbNodes();
1267 const SMDS_MeshNode* aNodes[ nbNodes ];
1269 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
1272 while( anIter->more() )
1274 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1277 aNodes[ i++ ] = aNode;
1281 for ( int i = 0; i < nbNodes - 1; i++ )
1282 if ( IsFreeEdge( &aNodes[ i ], theId ) )
1285 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
1287 return IsFreeEdge( &aNodes[ 0 ], theId );
1291 SMDSAbs_ElementType FreeEdges::GetType() const
1293 return SMDSAbs_Face;
1296 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1299 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1300 if(thePntId1 > thePntId2){
1301 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1305 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1306 if(myPntId[0] < x.myPntId[0]) return true;
1307 if(myPntId[0] == x.myPntId[0])
1308 if(myPntId[1] < x.myPntId[1]) return true;
1312 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1313 FreeEdges::TBorders& theRegistry,
1314 FreeEdges::TBorders& theContainer)
1316 if(theRegistry.find(theBorder) == theRegistry.end()){
1317 theRegistry.insert(theBorder);
1318 theContainer.insert(theBorder);
1320 theContainer.erase(theBorder);
1324 void FreeEdges::GetBoreders(TBorders& theBorders)
1327 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1328 for(; anIter->more(); ){
1329 const SMDS_MeshFace* anElem = anIter->next();
1330 long anElemId = anElem->GetID();
1331 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1333 const SMDS_MeshElement* aNode;
1334 if(aNodesIter->more()){
1335 aNode = aNodesIter->next();
1336 aNodeId[0] = aNodeId[1] = aNode->GetID();
1338 for(; aNodesIter->more(); ){
1339 aNode = aNodesIter->next();
1340 long anId = aNode->GetID();
1341 Border aBorder(anElemId,aNodeId[1],anId);
1343 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1344 UpdateBorders(aBorder,aRegistry,theBorders);
1346 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1347 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1348 UpdateBorders(aBorder,aRegistry,theBorders);
1350 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1355 Description : Predicate for Range of Ids.
1356 Range may be specified with two ways.
1357 1. Using AddToRange method
1358 2. With SetRangeStr method. Parameter of this method is a string
1359 like as "1,2,3,50-60,63,67,70-"
1362 //=======================================================================
1363 // name : RangeOfIds
1364 // Purpose : Constructor
1365 //=======================================================================
1366 RangeOfIds::RangeOfIds()
1369 myType = SMDSAbs_All;
1372 //=======================================================================
1374 // Purpose : Set mesh
1375 //=======================================================================
1376 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1381 //=======================================================================
1382 // name : AddToRange
1383 // Purpose : Add ID to the range
1384 //=======================================================================
1385 bool RangeOfIds::AddToRange( long theEntityId )
1387 myIds.Add( theEntityId );
1391 //=======================================================================
1392 // name : GetRangeStr
1393 // Purpose : Get range as a string.
1394 // Example: "1,2,3,50-60,63,67,70-"
1395 //=======================================================================
1396 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1400 TColStd_SequenceOfInteger anIntSeq;
1401 TColStd_SequenceOfAsciiString aStrSeq;
1403 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1404 for ( ; anIter.More(); anIter.Next() )
1406 int anId = anIter.Key();
1407 TCollection_AsciiString aStr( anId );
1408 anIntSeq.Append( anId );
1409 aStrSeq.Append( aStr );
1412 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1414 int aMinId = myMin( i );
1415 int aMaxId = myMax( i );
1417 TCollection_AsciiString aStr;
1418 if ( aMinId != IntegerFirst() )
1423 if ( aMaxId != IntegerLast() )
1426 // find position of the string in result sequence and insert string in it
1427 if ( anIntSeq.Length() == 0 )
1429 anIntSeq.Append( aMinId );
1430 aStrSeq.Append( aStr );
1434 if ( aMinId < anIntSeq.First() )
1436 anIntSeq.Prepend( aMinId );
1437 aStrSeq.Prepend( aStr );
1439 else if ( aMinId > anIntSeq.Last() )
1441 anIntSeq.Append( aMinId );
1442 aStrSeq.Append( aStr );
1445 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1446 if ( aMinId < anIntSeq( j ) )
1448 anIntSeq.InsertBefore( j, aMinId );
1449 aStrSeq.InsertBefore( j, aStr );
1455 if ( aStrSeq.Length() == 0 )
1458 theResStr = aStrSeq( 1 );
1459 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1462 theResStr += aStrSeq( j );
1466 //=======================================================================
1467 // name : SetRangeStr
1468 // Purpose : Define range with string
1469 // Example of entry string: "1,2,3,50-60,63,67,70-"
1470 //=======================================================================
1471 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1477 TCollection_AsciiString aStr = theStr;
1478 aStr.RemoveAll( ' ' );
1479 aStr.RemoveAll( '\t' );
1481 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1482 aStr.Remove( aPos, 2 );
1484 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1486 while ( tmpStr != "" )
1488 tmpStr = aStr.Token( ",", i++ );
1489 int aPos = tmpStr.Search( '-' );
1493 if ( tmpStr.IsIntegerValue() )
1494 myIds.Add( tmpStr.IntegerValue() );
1500 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1501 TCollection_AsciiString aMinStr = tmpStr;
1503 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1504 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1506 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1507 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1510 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1511 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1518 //=======================================================================
1520 // Purpose : Get type of supported entities
1521 //=======================================================================
1522 SMDSAbs_ElementType RangeOfIds::GetType() const
1527 //=======================================================================
1529 // Purpose : Set type of supported entities
1530 //=======================================================================
1531 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1536 //=======================================================================
1538 // Purpose : Verify whether entity satisfies to this rpedicate
1539 //=======================================================================
1540 bool RangeOfIds::IsSatisfy( long theId )
1545 if ( myType == SMDSAbs_Node )
1547 if ( myMesh->FindNode( theId ) == 0 )
1552 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1553 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1557 if ( myIds.Contains( theId ) )
1560 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1561 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1569 Description : Base class for comparators
1571 Comparator::Comparator():
1575 Comparator::~Comparator()
1578 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1581 myFunctor->SetMesh( theMesh );
1584 void Comparator::SetMargin( double theValue )
1586 myMargin = theValue;
1589 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1591 myFunctor = theFunct;
1594 SMDSAbs_ElementType Comparator::GetType() const
1596 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1599 double Comparator::GetMargin()
1607 Description : Comparator "<"
1609 bool LessThan::IsSatisfy( long theId )
1611 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1617 Description : Comparator ">"
1619 bool MoreThan::IsSatisfy( long theId )
1621 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1627 Description : Comparator "="
1630 myToler(Precision::Confusion())
1633 bool EqualTo::IsSatisfy( long theId )
1635 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1638 void EqualTo::SetTolerance( double theToler )
1643 double EqualTo::GetTolerance()
1650 Description : Logical NOT predicate
1652 LogicalNOT::LogicalNOT()
1655 LogicalNOT::~LogicalNOT()
1658 bool LogicalNOT::IsSatisfy( long theId )
1660 return myPredicate && !myPredicate->IsSatisfy( theId );
1663 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1666 myPredicate->SetMesh( theMesh );
1669 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1671 myPredicate = thePred;
1674 SMDSAbs_ElementType LogicalNOT::GetType() const
1676 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1681 Class : LogicalBinary
1682 Description : Base class for binary logical predicate
1684 LogicalBinary::LogicalBinary()
1687 LogicalBinary::~LogicalBinary()
1690 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1693 myPredicate1->SetMesh( theMesh );
1696 myPredicate2->SetMesh( theMesh );
1699 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1701 myPredicate1 = thePredicate;
1704 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1706 myPredicate2 = thePredicate;
1709 SMDSAbs_ElementType LogicalBinary::GetType() const
1711 if ( !myPredicate1 || !myPredicate2 )
1714 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1715 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1717 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1723 Description : Logical AND
1725 bool LogicalAND::IsSatisfy( long theId )
1730 myPredicate1->IsSatisfy( theId ) &&
1731 myPredicate2->IsSatisfy( theId );
1737 Description : Logical OR
1739 bool LogicalOR::IsSatisfy( long theId )
1744 myPredicate1->IsSatisfy( theId ) ||
1745 myPredicate2->IsSatisfy( theId );
1759 void Filter::SetPredicate( PredicatePtr thePredicate )
1761 myPredicate = thePredicate;
1764 template<class TElement, class TIterator, class TPredicate>
1765 inline void FillSequence(const TIterator& theIterator,
1766 TPredicate& thePredicate,
1767 Filter::TIdSequence& theSequence)
1769 if ( theIterator ) {
1770 while( theIterator->more() ) {
1771 TElement anElem = theIterator->next();
1772 long anId = anElem->GetID();
1773 if ( thePredicate->IsSatisfy( anId ) )
1774 theSequence.push_back( anId );
1781 GetElementsId( const SMDS_Mesh* theMesh,
1782 PredicatePtr thePredicate,
1783 TIdSequence& theSequence )
1785 theSequence.clear();
1787 if ( !theMesh || !thePredicate )
1790 thePredicate->SetMesh( theMesh );
1792 SMDSAbs_ElementType aType = thePredicate->GetType();
1795 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
1798 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1801 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1803 case SMDSAbs_Volume:
1804 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1807 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1808 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1809 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1815 Filter::GetElementsId( const SMDS_Mesh* theMesh,
1816 Filter::TIdSequence& theSequence )
1818 GetElementsId(theMesh,myPredicate,theSequence);
1825 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1831 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1832 SMDS_MeshNode* theNode2 )
1838 ManifoldPart::Link::~Link()
1844 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1846 if ( myNode1 == theLink.myNode1 &&
1847 myNode2 == theLink.myNode2 )
1849 else if ( myNode1 == theLink.myNode2 &&
1850 myNode2 == theLink.myNode1 )
1856 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1858 if(myNode1 < x.myNode1) return true;
1859 if(myNode1 == x.myNode1)
1860 if(myNode2 < x.myNode2) return true;
1864 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1865 const ManifoldPart::Link& theLink2 )
1867 return theLink1.IsEqual( theLink2 );
1870 ManifoldPart::ManifoldPart()
1873 myAngToler = Precision::Angular();
1874 myIsOnlyManifold = true;
1877 ManifoldPart::~ManifoldPart()
1882 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
1888 SMDSAbs_ElementType ManifoldPart::GetType() const
1889 { return SMDSAbs_Face; }
1891 bool ManifoldPart::IsSatisfy( long theElementId )
1893 return myMapIds.Contains( theElementId );
1896 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1897 { myAngToler = theAngToler; }
1899 double ManifoldPart::GetAngleTolerance() const
1900 { return myAngToler; }
1902 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1903 { myIsOnlyManifold = theIsOnly; }
1905 void ManifoldPart::SetStartElem( const long theStartId )
1906 { myStartElemId = theStartId; }
1908 bool ManifoldPart::process()
1911 myMapBadGeomIds.Clear();
1913 myAllFacePtr.clear();
1914 myAllFacePtrIntDMap.clear();
1918 // collect all faces into own map
1919 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1920 for (; anFaceItr->more(); )
1922 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1923 myAllFacePtr.push_back( aFacePtr );
1924 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1927 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1931 // the map of non manifold links and bad geometry
1932 TMapOfLink aMapOfNonManifold;
1933 TColStd_MapOfInteger aMapOfTreated;
1935 // begin cycle on faces from start index and run on vector till the end
1936 // and from begin to start index to cover whole vector
1937 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1938 bool isStartTreat = false;
1939 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1941 if ( fi == aStartIndx )
1942 isStartTreat = true;
1943 // as result next time when fi will be equal to aStartIndx
1945 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1946 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1949 aMapOfTreated.Add( aFacePtr->GetID() );
1950 TColStd_MapOfInteger aResFaces;
1951 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1952 aMapOfNonManifold, aResFaces ) )
1954 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1955 for ( ; anItr.More(); anItr.Next() )
1957 int aFaceId = anItr.Key();
1958 aMapOfTreated.Add( aFaceId );
1959 myMapIds.Add( aFaceId );
1962 if ( fi == ( myAllFacePtr.size() - 1 ) )
1964 } // end run on vector of faces
1965 return !myMapIds.IsEmpty();
1968 static void getLinks( const SMDS_MeshFace* theFace,
1969 ManifoldPart::TVectorOfLink& theLinks )
1971 int aNbNode = theFace->NbNodes();
1972 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1974 SMDS_MeshNode* aNode = 0;
1975 for ( ; aNodeItr->more() && i <= aNbNode; )
1978 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
1982 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
1984 ManifoldPart::Link aLink( aN1, aN2 );
1985 theLinks.push_back( aLink );
1989 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
1992 int aNbNode = theFace->NbNodes();
1993 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
1994 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1996 for ( ; aNodeItr->more() && i <= 4; i++ )
1998 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
1999 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2002 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2003 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2007 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2010 double len = n.Modulus();
2017 bool ManifoldPart::findConnected
2018 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2019 SMDS_MeshFace* theStartFace,
2020 ManifoldPart::TMapOfLink& theNonManifold,
2021 TColStd_MapOfInteger& theResFaces )
2023 theResFaces.Clear();
2024 if ( !theAllFacePtrInt.size() )
2027 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2029 myMapBadGeomIds.Add( theStartFace->GetID() );
2033 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2034 ManifoldPart::TVectorOfLink aSeqOfBoundary;
2035 theResFaces.Add( theStartFace->GetID() );
2036 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2038 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2039 aDMapLinkFace, theNonManifold, theStartFace );
2041 bool isDone = false;
2042 while ( !isDone && aMapOfBoundary.size() != 0 )
2044 bool isToReset = false;
2045 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2046 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2048 ManifoldPart::Link aLink = *pLink;
2049 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2051 // each link could be treated only once
2052 aMapToSkip.insert( aLink );
2054 ManifoldPart::TVectorOfFacePtr aFaces;
2056 if ( myIsOnlyManifold &&
2057 (theNonManifold.find( aLink ) != theNonManifold.end()) )
2061 getFacesByLink( aLink, aFaces );
2062 // filter the element to keep only indicated elements
2063 ManifoldPart::TVectorOfFacePtr aFiltered;
2064 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2065 for ( ; pFace != aFaces.end(); ++pFace )
2067 SMDS_MeshFace* aFace = *pFace;
2068 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2069 aFiltered.push_back( aFace );
2072 if ( aFaces.size() < 2 ) // no neihgbour faces
2074 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2076 theNonManifold.insert( aLink );
2081 // compare normal with normals of neighbor element
2082 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2083 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2084 for ( ; pFace != aFaces.end(); ++pFace )
2086 SMDS_MeshFace* aNextFace = *pFace;
2087 if ( aPrevFace == aNextFace )
2089 int anNextFaceID = aNextFace->GetID();
2090 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2091 // should not be with non manifold restriction. probably bad topology
2093 // check if face was treated and skipped
2094 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2095 !isInPlane( aPrevFace, aNextFace ) )
2097 // add new element to connected and extend the boundaries.
2098 theResFaces.Add( anNextFaceID );
2099 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2100 aDMapLinkFace, theNonManifold, aNextFace );
2104 isDone = !isToReset;
2107 return !theResFaces.IsEmpty();
2110 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2111 const SMDS_MeshFace* theFace2 )
2113 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2114 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2115 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2117 myMapBadGeomIds.Add( theFace2->GetID() );
2120 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2126 void ManifoldPart::expandBoundary
2127 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
2128 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
2129 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2130 ManifoldPart::TMapOfLink& theNonManifold,
2131 SMDS_MeshFace* theNextFace ) const
2133 ManifoldPart::TVectorOfLink aLinks;
2134 getLinks( theNextFace, aLinks );
2135 int aNbLink = aLinks.size();
2136 for ( int i = 0; i < aNbLink; i++ )
2138 ManifoldPart::Link aLink = aLinks[ i ];
2139 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2141 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2143 if ( myIsOnlyManifold )
2145 // remove from boundary
2146 theMapOfBoundary.erase( aLink );
2147 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2148 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2150 ManifoldPart::Link aBoundLink = *pLink;
2151 if ( aBoundLink.IsEqual( aLink ) )
2153 theSeqOfBoundary.erase( pLink );
2161 theMapOfBoundary.insert( aLink );
2162 theSeqOfBoundary.push_back( aLink );
2163 theDMapLinkFacePtr[ aLink ] = theNextFace;
2168 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2169 ManifoldPart::TVectorOfFacePtr& theFaces ) const
2171 SMDS_Mesh::SetOfFaces aSetOfFaces;
2172 // take all faces that shared first node
2173 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2174 for ( ; anItr->more(); )
2176 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2179 aSetOfFaces.Add( aFace );
2181 // take all faces that shared second node
2182 anItr = theLink.myNode2->facesIterator();
2183 // find the common part of two sets
2184 for ( ; anItr->more(); )
2186 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2187 if ( aSetOfFaces.Contains( aFace ) )
2188 theFaces.push_back( aFace );
2197 ElementsOnSurface::ElementsOnSurface()
2201 myType = SMDSAbs_All;
2203 myToler = Precision::Confusion();
2206 ElementsOnSurface::~ElementsOnSurface()
2211 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2213 if ( myMesh == theMesh )
2220 bool ElementsOnSurface::IsSatisfy( long theElementId )
2222 return myIds.Contains( theElementId );
2225 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2228 void ElementsOnSurface::SetTolerance( const double theToler )
2229 { myToler = theToler; }
2231 double ElementsOnSurface::GetTolerance() const
2236 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2237 const SMDSAbs_ElementType theType )
2241 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2246 TopoDS_Face aFace = TopoDS::Face( theShape );
2247 mySurf = BRep_Tool::Surface( aFace );
2250 void ElementsOnSurface::process()
2253 if ( mySurf.IsNull() )
2259 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2261 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2262 for(; anIter->more(); )
2263 process( anIter->next() );
2266 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2268 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2269 for(; anIter->more(); )
2270 process( anIter->next() );
2273 if ( myType == SMDSAbs_Node )
2275 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2276 for(; anIter->more(); )
2277 process( anIter->next() );
2281 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2283 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2284 bool isSatisfy = true;
2285 for ( ; aNodeItr->more(); )
2287 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2288 if ( !isOnSurface( aNode ) )
2295 myIds.Add( theElemPtr->GetID() );
2298 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2300 if ( mySurf.IsNull() )
2303 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2304 double aToler2 = myToler * myToler;
2305 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2307 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2308 if ( aPln.SquareDistance( aPnt ) > aToler2 )
2311 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2313 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2314 double aRad = aCyl.Radius();
2315 gp_Ax3 anAxis = aCyl.Position();
2316 gp_XYZ aLoc = aCyl.Location().XYZ();
2317 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2318 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2319 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )