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( 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( 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)){
1034 const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1035 SMDSAbs_ElementType aType = anFaceElem->GetType();
1039 TColStd_MapOfInteger aMap;
1047 if (len == 3){ // triangles
1048 int Nb[3] = {0,0,0};
1051 SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1052 if ( anIter != 0 ) {
1053 while( anIter->more() ) {
1054 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1058 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1059 while( anElemIter->more() ) {
1060 const SMDS_MeshElement* anElem = anElemIter->next();
1061 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1062 int anId = anElem->GetID();
1064 if ( anIter->more() ) // i.e. first node
1066 else if ( aMap.Contains( anId ) ){
1070 else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1075 aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1078 case SMDSAbs_Volume:
1083 return aResult;//getNbMultiConnection( myMesh, theId );
1086 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1091 SMDSAbs_ElementType MultiConnection2D::GetType() const
1093 return SMDSAbs_Face;
1096 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1098 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1099 if(thePntId1 > thePntId2){
1100 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1104 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1105 if(myPntId[0] < x.myPntId[0]) return true;
1106 if(myPntId[0] == x.myPntId[0])
1107 if(myPntId[1] < x.myPntId[1]) return true;
1111 void MultiConnection2D::GetValues(MValues& theValues){
1112 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1113 for(; anIter->more(); ){
1114 const SMDS_MeshFace* anElem = anIter->next();
1115 long anElemId = anElem->GetID();
1116 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1119 //int aNbConnects=0;
1120 const SMDS_MeshNode* aNode0;
1121 const SMDS_MeshNode* aNode1;
1122 const SMDS_MeshNode* aNode2;
1123 if(aNodesIter->more()){
1124 aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1126 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1127 aNodeId[0] = aNodeId[1] = aNodes->GetID();
1129 for(; aNodesIter->more(); ){
1130 aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1131 long anId = aNode2->GetID();
1134 Value aValue(aNodeId[1],aNodeId[2]);
1135 MValues::iterator aItr = theValues.find(aValue);
1136 if (aItr != theValues.end()){
1140 theValues[aValue] = 1;
1143 //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1144 aNodeId[1] = aNodeId[2];
1147 Value aValue(aNodeId[0],aNodeId[2]);
1148 MValues::iterator aItr = theValues.find(aValue);
1149 if (aItr != theValues.end()){
1153 theValues[aValue] = 1;
1156 //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1166 Class : BadOrientedVolume
1167 Description : Predicate bad oriented volumes
1170 BadOrientedVolume::BadOrientedVolume()
1175 void BadOrientedVolume::SetMesh( SMDS_Mesh* theMesh )
1180 bool BadOrientedVolume::IsSatisfy( long theId )
1185 SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1186 return !vTool.IsForward();
1189 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1191 return SMDSAbs_Volume;
1198 Description : Predicate for free borders
1201 FreeBorders::FreeBorders()
1206 void FreeBorders::SetMesh( SMDS_Mesh* theMesh )
1211 bool FreeBorders::IsSatisfy( long theId )
1213 return getNbMultiConnection( myMesh, theId ) == 1;
1216 SMDSAbs_ElementType FreeBorders::GetType() const
1218 return SMDSAbs_Edge;
1224 Description : Predicate for free Edges
1226 FreeEdges::FreeEdges()
1231 void FreeEdges::SetMesh( SMDS_Mesh* theMesh )
1236 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
1238 TColStd_MapOfInteger aMap;
1239 for ( int i = 0; i < 2; i++ )
1241 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1242 while( anElemIter->more() )
1244 const SMDS_MeshElement* anElem = anElemIter->next();
1245 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1247 int anId = anElem->GetID();
1251 else if ( aMap.Contains( anId ) && anId != theFaceId )
1259 bool FreeEdges::IsSatisfy( long theId )
1264 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1265 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1268 int nbNodes = aFace->NbNodes();
1269 const SMDS_MeshNode* aNodes[ nbNodes ];
1271 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
1274 while( anIter->more() )
1276 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1279 aNodes[ i++ ] = aNode;
1283 for ( int i = 0; i < nbNodes - 1; i++ )
1284 if ( IsFreeEdge( &aNodes[ i ], theId ) )
1287 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
1289 return IsFreeEdge( &aNodes[ 0 ], theId );
1293 SMDSAbs_ElementType FreeEdges::GetType() const
1295 return SMDSAbs_Face;
1298 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1301 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1302 if(thePntId1 > thePntId2){
1303 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1307 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1308 if(myPntId[0] < x.myPntId[0]) return true;
1309 if(myPntId[0] == x.myPntId[0])
1310 if(myPntId[1] < x.myPntId[1]) return true;
1314 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1315 FreeEdges::TBorders& theRegistry,
1316 FreeEdges::TBorders& theContainer)
1318 if(theRegistry.find(theBorder) == theRegistry.end()){
1319 theRegistry.insert(theBorder);
1320 theContainer.insert(theBorder);
1322 theContainer.erase(theBorder);
1326 void FreeEdges::GetBoreders(TBorders& theBorders)
1329 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1330 for(; anIter->more(); ){
1331 const SMDS_MeshFace* anElem = anIter->next();
1332 long anElemId = anElem->GetID();
1333 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1335 const SMDS_MeshElement* aNode;
1336 if(aNodesIter->more()){
1337 aNode = aNodesIter->next();
1338 aNodeId[0] = aNodeId[1] = aNode->GetID();
1340 for(; aNodesIter->more(); ){
1341 aNode = aNodesIter->next();
1342 long anId = aNode->GetID();
1343 Border aBorder(anElemId,aNodeId[1],anId);
1345 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1346 UpdateBorders(aBorder,aRegistry,theBorders);
1348 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1349 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1350 UpdateBorders(aBorder,aRegistry,theBorders);
1352 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1357 Description : Predicate for Range of Ids.
1358 Range may be specified with two ways.
1359 1. Using AddToRange method
1360 2. With SetRangeStr method. Parameter of this method is a string
1361 like as "1,2,3,50-60,63,67,70-"
1364 //=======================================================================
1365 // name : RangeOfIds
1366 // Purpose : Constructor
1367 //=======================================================================
1368 RangeOfIds::RangeOfIds()
1371 myType = SMDSAbs_All;
1374 //=======================================================================
1376 // Purpose : Set mesh
1377 //=======================================================================
1378 void RangeOfIds::SetMesh( SMDS_Mesh* theMesh )
1383 //=======================================================================
1384 // name : AddToRange
1385 // Purpose : Add ID to the range
1386 //=======================================================================
1387 bool RangeOfIds::AddToRange( long theEntityId )
1389 myIds.Add( theEntityId );
1393 //=======================================================================
1394 // name : GetRangeStr
1395 // Purpose : Get range as a string.
1396 // Example: "1,2,3,50-60,63,67,70-"
1397 //=======================================================================
1398 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1402 TColStd_SequenceOfInteger anIntSeq;
1403 TColStd_SequenceOfAsciiString aStrSeq;
1405 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1406 for ( ; anIter.More(); anIter.Next() )
1408 int anId = anIter.Key();
1409 TCollection_AsciiString aStr( anId );
1410 anIntSeq.Append( anId );
1411 aStrSeq.Append( aStr );
1414 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1416 int aMinId = myMin( i );
1417 int aMaxId = myMax( i );
1419 TCollection_AsciiString aStr;
1420 if ( aMinId != IntegerFirst() )
1425 if ( aMaxId != IntegerLast() )
1428 // find position of the string in result sequence and insert string in it
1429 if ( anIntSeq.Length() == 0 )
1431 anIntSeq.Append( aMinId );
1432 aStrSeq.Append( aStr );
1436 if ( aMinId < anIntSeq.First() )
1438 anIntSeq.Prepend( aMinId );
1439 aStrSeq.Prepend( aStr );
1441 else if ( aMinId > anIntSeq.Last() )
1443 anIntSeq.Append( aMinId );
1444 aStrSeq.Append( aStr );
1447 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1448 if ( aMinId < anIntSeq( j ) )
1450 anIntSeq.InsertBefore( j, aMinId );
1451 aStrSeq.InsertBefore( j, aStr );
1457 if ( aStrSeq.Length() == 0 )
1460 theResStr = aStrSeq( 1 );
1461 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1464 theResStr += aStrSeq( j );
1468 //=======================================================================
1469 // name : SetRangeStr
1470 // Purpose : Define range with string
1471 // Example of entry string: "1,2,3,50-60,63,67,70-"
1472 //=======================================================================
1473 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1479 TCollection_AsciiString aStr = theStr;
1480 aStr.RemoveAll( ' ' );
1481 aStr.RemoveAll( '\t' );
1483 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1484 aStr.Remove( aPos, 2 );
1486 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1488 while ( tmpStr != "" )
1490 tmpStr = aStr.Token( ",", i++ );
1491 int aPos = tmpStr.Search( '-' );
1495 if ( tmpStr.IsIntegerValue() )
1496 myIds.Add( tmpStr.IntegerValue() );
1502 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1503 TCollection_AsciiString aMinStr = tmpStr;
1505 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1506 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1508 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1509 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1512 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1513 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1520 //=======================================================================
1522 // Purpose : Get type of supported entities
1523 //=======================================================================
1524 SMDSAbs_ElementType RangeOfIds::GetType() const
1529 //=======================================================================
1531 // Purpose : Set type of supported entities
1532 //=======================================================================
1533 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1538 //=======================================================================
1540 // Purpose : Verify whether entity satisfies to this rpedicate
1541 //=======================================================================
1542 bool RangeOfIds::IsSatisfy( long theId )
1547 if ( myType == SMDSAbs_Node )
1549 if ( myMesh->FindNode( theId ) == 0 )
1554 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1555 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1559 if ( myIds.Contains( theId ) )
1562 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1563 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1571 Description : Base class for comparators
1573 Comparator::Comparator():
1577 Comparator::~Comparator()
1580 void Comparator::SetMesh( SMDS_Mesh* theMesh )
1583 myFunctor->SetMesh( theMesh );
1586 void Comparator::SetMargin( double theValue )
1588 myMargin = theValue;
1591 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1593 myFunctor = theFunct;
1596 SMDSAbs_ElementType Comparator::GetType() const
1598 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1601 double Comparator::GetMargin()
1609 Description : Comparator "<"
1611 bool LessThan::IsSatisfy( long theId )
1613 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1619 Description : Comparator ">"
1621 bool MoreThan::IsSatisfy( long theId )
1623 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1629 Description : Comparator "="
1632 myToler(Precision::Confusion())
1635 bool EqualTo::IsSatisfy( long theId )
1637 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1640 void EqualTo::SetTolerance( double theToler )
1645 double EqualTo::GetTolerance()
1652 Description : Logical NOT predicate
1654 LogicalNOT::LogicalNOT()
1657 LogicalNOT::~LogicalNOT()
1660 bool LogicalNOT::IsSatisfy( long theId )
1662 return myPredicate && !myPredicate->IsSatisfy( theId );
1665 void LogicalNOT::SetMesh( SMDS_Mesh* theMesh )
1668 myPredicate->SetMesh( theMesh );
1671 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1673 myPredicate = thePred;
1676 SMDSAbs_ElementType LogicalNOT::GetType() const
1678 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1683 Class : LogicalBinary
1684 Description : Base class for binary logical predicate
1686 LogicalBinary::LogicalBinary()
1689 LogicalBinary::~LogicalBinary()
1692 void LogicalBinary::SetMesh( SMDS_Mesh* theMesh )
1695 myPredicate1->SetMesh( theMesh );
1698 myPredicate2->SetMesh( theMesh );
1701 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1703 myPredicate1 = thePredicate;
1706 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1708 myPredicate2 = thePredicate;
1711 SMDSAbs_ElementType LogicalBinary::GetType() const
1713 if ( !myPredicate1 || !myPredicate2 )
1716 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1717 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1719 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1725 Description : Logical AND
1727 bool LogicalAND::IsSatisfy( long theId )
1732 myPredicate1->IsSatisfy( theId ) &&
1733 myPredicate2->IsSatisfy( theId );
1739 Description : Logical OR
1741 bool LogicalOR::IsSatisfy( long theId )
1746 myPredicate1->IsSatisfy( theId ) ||
1747 myPredicate2->IsSatisfy( theId );
1761 void Filter::SetPredicate( PredicatePtr thePredicate )
1763 myPredicate = thePredicate;
1767 template<class TElement, class TIterator, class TPredicate>
1768 void FillSequence(const TIterator& theIterator,
1769 TPredicate& thePredicate,
1770 Filter::TIdSequence& theSequence)
1772 if ( theIterator ) {
1773 while( theIterator->more() ) {
1774 TElement anElem = theIterator->next();
1775 long anId = anElem->GetID();
1776 if ( thePredicate->IsSatisfy( anId ) )
1777 theSequence.push_back( anId );
1783 Filter::GetElementsId( SMDS_Mesh* theMesh )
1785 TIdSequence aSequence;
1786 if ( !theMesh || !myPredicate ) return aSequence;
1788 myPredicate->SetMesh( theMesh );
1790 SMDSAbs_ElementType aType = myPredicate->GetType();
1793 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),myPredicate,aSequence);
1797 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),myPredicate,aSequence);
1801 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),myPredicate,aSequence);
1804 case SMDSAbs_Volume:{
1805 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),myPredicate,aSequence);
1809 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),myPredicate,aSequence);
1810 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),myPredicate,aSequence);
1811 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),myPredicate,aSequence);
1822 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1828 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1829 SMDS_MeshNode* theNode2 )
1835 ManifoldPart::Link::~Link()
1841 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1843 if ( myNode1 == theLink.myNode1 &&
1844 myNode2 == theLink.myNode2 )
1846 else if ( myNode1 == theLink.myNode2 &&
1847 myNode2 == theLink.myNode1 )
1853 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1855 if(myNode1 < x.myNode1) return true;
1856 if(myNode1 == x.myNode1)
1857 if(myNode2 < x.myNode2) return true;
1861 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1862 const ManifoldPart::Link& theLink2 )
1864 return theLink1.IsEqual( theLink2 );
1867 ManifoldPart::ManifoldPart()
1870 myAngToler = Precision::Angular();
1871 myIsOnlyManifold = true;
1874 ManifoldPart::~ManifoldPart()
1879 void ManifoldPart::SetMesh( SMDS_Mesh* theMesh )
1885 SMDSAbs_ElementType ManifoldPart::GetType() const
1886 { return SMDSAbs_Face; }
1888 bool ManifoldPart::IsSatisfy( long theElementId )
1890 return myMapIds.Contains( theElementId );
1893 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1894 { myAngToler = theAngToler; }
1896 double ManifoldPart::GetAngleTolerance() const
1897 { return myAngToler; }
1899 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1900 { myIsOnlyManifold = theIsOnly; }
1902 void ManifoldPart::SetStartElem( const long theStartId )
1903 { myStartElemId = theStartId; }
1905 bool ManifoldPart::process()
1908 myMapBadGeomIds.Clear();
1910 myAllFacePtr.clear();
1911 myAllFacePtrIntDMap.clear();
1915 // collect all faces into own map
1916 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1917 for (; anFaceItr->more(); )
1919 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1920 myAllFacePtr.push_back( aFacePtr );
1921 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1924 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1928 // the map of non manifold links and bad geometry
1929 TMapOfLink aMapOfNonManifold;
1930 TColStd_MapOfInteger aMapOfTreated;
1932 // begin cycle on faces from start index and run on vector till the end
1933 // and from begin to start index to cover whole vector
1934 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1935 bool isStartTreat = false;
1936 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1938 if ( fi == aStartIndx )
1939 isStartTreat = true;
1940 // as result next time when fi will be equal to aStartIndx
1942 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1943 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1946 aMapOfTreated.Add( aFacePtr->GetID() );
1947 TColStd_MapOfInteger aResFaces;
1948 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1949 aMapOfNonManifold, aResFaces ) )
1951 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1952 for ( ; anItr.More(); anItr.Next() )
1954 int aFaceId = anItr.Key();
1955 aMapOfTreated.Add( aFaceId );
1956 myMapIds.Add( aFaceId );
1959 if ( fi == ( myAllFacePtr.size() - 1 ) )
1961 } // end run on vector of faces
1962 return !myMapIds.IsEmpty();
1965 static void getLinks( const SMDS_MeshFace* theFace,
1966 ManifoldPart::TVectorOfLink& theLinks )
1968 int aNbNode = theFace->NbNodes();
1969 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1971 SMDS_MeshNode* aNode = 0;
1972 for ( ; aNodeItr->more() && i <= aNbNode; )
1975 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
1979 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
1981 ManifoldPart::Link aLink( aN1, aN2 );
1982 theLinks.push_back( aLink );
1986 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
1989 int aNbNode = theFace->NbNodes();
1990 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
1991 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1993 for ( ; aNodeItr->more() && i <= 4; i++ )
1995 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
1996 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
1999 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2000 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2004 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2007 double len = n.Modulus();
2014 bool ManifoldPart::findConnected
2015 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2016 SMDS_MeshFace* theStartFace,
2017 ManifoldPart::TMapOfLink& theNonManifold,
2018 TColStd_MapOfInteger& theResFaces )
2020 theResFaces.Clear();
2021 if ( !theAllFacePtrInt.size() )
2024 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2026 myMapBadGeomIds.Add( theStartFace->GetID() );
2030 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2031 ManifoldPart::TVectorOfLink aSeqOfBoundary;
2032 theResFaces.Add( theStartFace->GetID() );
2033 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2035 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2036 aDMapLinkFace, theNonManifold, theStartFace );
2038 bool isDone = false;
2039 while ( !isDone && aMapOfBoundary.size() != 0 )
2041 bool isToReset = false;
2042 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2043 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2045 ManifoldPart::Link aLink = *pLink;
2046 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2048 // each link could be treated only once
2049 aMapToSkip.insert( aLink );
2051 ManifoldPart::TVectorOfFacePtr aFaces;
2053 if ( myIsOnlyManifold &&
2054 (theNonManifold.find( aLink ) != theNonManifold.end()) )
2058 getFacesByLink( aLink, aFaces );
2059 // filter the element to keep only indicated elements
2060 ManifoldPart::TVectorOfFacePtr aFiltered;
2061 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2062 for ( ; pFace != aFaces.end(); ++pFace )
2064 SMDS_MeshFace* aFace = *pFace;
2065 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2066 aFiltered.push_back( aFace );
2069 if ( aFaces.size() < 2 ) // no neihgbour faces
2071 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2073 theNonManifold.insert( aLink );
2078 // compare normal with normals of neighbor element
2079 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2080 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2081 for ( ; pFace != aFaces.end(); ++pFace )
2083 SMDS_MeshFace* aNextFace = *pFace;
2084 if ( aPrevFace == aNextFace )
2086 int anNextFaceID = aNextFace->GetID();
2087 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2088 // should not be with non manifold restriction. probably bad topology
2090 // check if face was treated and skipped
2091 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2092 !isInPlane( aPrevFace, aNextFace ) )
2094 // add new element to connected and extend the boundaries.
2095 theResFaces.Add( anNextFaceID );
2096 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2097 aDMapLinkFace, theNonManifold, aNextFace );
2101 isDone = !isToReset;
2104 return !theResFaces.IsEmpty();
2107 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2108 const SMDS_MeshFace* theFace2 )
2110 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2111 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2112 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2114 myMapBadGeomIds.Add( theFace2->GetID() );
2117 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2123 void ManifoldPart::expandBoundary
2124 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
2125 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
2126 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2127 ManifoldPart::TMapOfLink& theNonManifold,
2128 SMDS_MeshFace* theNextFace ) const
2130 ManifoldPart::TVectorOfLink aLinks;
2131 getLinks( theNextFace, aLinks );
2132 int aNbLink = aLinks.size();
2133 for ( int i = 0; i < aNbLink; i++ )
2135 ManifoldPart::Link aLink = aLinks[ i ];
2136 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2138 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2140 if ( myIsOnlyManifold )
2142 // remove from boundary
2143 theMapOfBoundary.erase( aLink );
2144 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2145 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2147 ManifoldPart::Link aBoundLink = *pLink;
2148 if ( aBoundLink.IsEqual( aLink ) )
2150 theSeqOfBoundary.erase( pLink );
2158 theMapOfBoundary.insert( aLink );
2159 theSeqOfBoundary.push_back( aLink );
2160 theDMapLinkFacePtr[ aLink ] = theNextFace;
2165 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2166 ManifoldPart::TVectorOfFacePtr& theFaces ) const
2168 SMDS_Mesh::SetOfFaces aSetOfFaces;
2169 // take all faces that shared first node
2170 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2171 for ( ; anItr->more(); )
2173 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2176 aSetOfFaces.Add( aFace );
2178 // take all faces that shared second node
2179 anItr = theLink.myNode2->facesIterator();
2180 // find the common part of two sets
2181 for ( ; anItr->more(); )
2183 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2184 if ( aSetOfFaces.Contains( aFace ) )
2185 theFaces.push_back( aFace );
2194 ElementsOnSurface::ElementsOnSurface()
2198 myType = SMDSAbs_All;
2200 myToler = Precision::Confusion();
2203 ElementsOnSurface::~ElementsOnSurface()
2208 void ElementsOnSurface::SetMesh( SMDS_Mesh* theMesh )
2210 if ( myMesh == theMesh )
2217 bool ElementsOnSurface::IsSatisfy( long theElementId )
2219 return myIds.Contains( theElementId );
2222 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2225 void ElementsOnSurface::SetTolerance( const double theToler )
2226 { myToler = theToler; }
2228 double ElementsOnSurface::GetTolerance() const
2233 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2234 const SMDSAbs_ElementType theType )
2238 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2243 TopoDS_Face aFace = TopoDS::Face( theShape );
2244 mySurf = BRep_Tool::Surface( aFace );
2247 void ElementsOnSurface::process()
2250 if ( mySurf.IsNull() )
2256 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2258 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2259 for(; anIter->more(); )
2260 process( anIter->next() );
2263 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2265 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2266 for(; anIter->more(); )
2267 process( anIter->next() );
2270 if ( myType == SMDSAbs_Node )
2272 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2273 for(; anIter->more(); )
2274 process( anIter->next() );
2278 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2280 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2281 bool isSatisfy = true;
2282 for ( ; aNodeItr->more(); )
2284 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2285 if ( !isOnSurface( aNode ) )
2292 myIds.Add( theElemPtr->GetID() );
2295 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2297 if ( mySurf.IsNull() )
2300 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2301 double aToler2 = myToler * myToler;
2302 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2304 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2305 if ( aPln.SquareDistance( aPnt ) > aToler2 )
2308 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2310 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2311 double aRad = aCyl.Radius();
2312 gp_Ax3 anAxis = aCyl.Position();
2313 gp_XYZ aLoc = aCyl.Location().XYZ();
2314 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2315 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2316 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )