1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
20 #include "SMESH_ControlsDef.hxx"
24 #include <BRep_Tool.hxx>
26 #include <gp_Cylinder.hxx>
32 #include <Geom_Plane.hxx>
33 #include <Geom_CylindricalSurface.hxx>
34 #include <Precision.hxx>
35 #include <TColgp_Array1OfXYZ.hxx>
36 #include <TColStd_MapOfInteger.hxx>
37 #include <TColStd_SequenceOfAsciiString.hxx>
38 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
44 #include "SMDS_Mesh.hxx"
45 #include "SMDS_Iterator.hxx"
46 #include "SMDS_MeshElement.hxx"
47 #include "SMDS_MeshNode.hxx"
48 #include "SMDS_VolumeTool.hxx"
56 inline double getAngle( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
58 gp_Vec v1( P1 - P2 ), v2( P3 - P2 );
60 return v1.Magnitude() < gp::Resolution() ||
61 v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
64 inline double getArea( const gp_XYZ& P1, const gp_XYZ& P2, const gp_XYZ& P3 )
66 gp_Vec aVec1( P2 - P1 );
67 gp_Vec aVec2( P3 - P1 );
68 return ( aVec1 ^ aVec2 ).Magnitude() * 0.5;
71 inline double getArea( const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3 )
73 return getArea( P1.XYZ(), P2.XYZ(), P3.XYZ() );
78 inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
80 double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
84 int getNbMultiConnection( const SMDS_Mesh* theMesh, const int theId )
89 const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
90 if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge || anEdge->NbNodes() != 2 )
93 TColStd_MapOfInteger aMap;
96 SMDS_ElemIteratorPtr anIter = anEdge->nodesIterator();
98 while( anIter->more() ) {
99 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
102 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
103 while( anElemIter->more() ) {
104 const SMDS_MeshElement* anElem = anElemIter->next();
105 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
106 int anId = anElem->GetID();
108 if ( anIter->more() ) // i.e. first node
110 else if ( aMap.Contains( anId ) )
124 using namespace SMESH::Controls;
131 Class : NumericalFunctor
132 Description : Base class for numerical functors
134 NumericalFunctor::NumericalFunctor():
140 void NumericalFunctor::SetMesh( const SMDS_Mesh* theMesh )
145 bool NumericalFunctor::GetPoints(const int theId,
146 TSequenceOfXYZ& theRes ) const
153 return GetPoints( myMesh->FindElement( theId ), theRes );
156 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
157 TSequenceOfXYZ& theRes )
164 // Get nodes of the element
165 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
168 while( anIter->more() )
170 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
172 theRes.push_back( gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
180 long NumericalFunctor::GetPrecision() const
185 void NumericalFunctor::SetPrecision( const long thePrecision )
187 myPrecision = thePrecision;
190 double NumericalFunctor::GetValue( long theId )
193 if ( GetPoints( theId, P ))
195 double aVal = GetValue( P );
196 if ( myPrecision >= 0 )
198 double prec = pow( 10., (double)( myPrecision ) );
199 aVal = floor( aVal * prec + 0.5 ) / prec;
209 Description : Functor for calculation of minimum angle
212 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
219 aMin = getAngle(P( P.size() ), P( 1 ), P( 2 ));
220 aMin = Min(aMin,getAngle(P( P.size()-1 ), P( P.size() ), P( 1 )));
222 for (int i=2; i<P.size();i++){
223 double A0 = getAngle( P( i-1 ), P( i ), P( i+1 ) );
227 return aMin * 180.0 / PI;
230 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
232 //const double aBestAngle = PI / nbNodes;
233 const double aBestAngle = 180.0 - ( 360.0 / double(nbNodes) );
234 return ( fabs( aBestAngle - Value ));
237 SMDSAbs_ElementType MinimumAngle::GetType() const
245 Description : Functor for calculating aspect ratio
247 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
249 int nbNodes = P.size();
254 // Compute lengths of the sides
256 double aLen[ nbNodes ];
257 for ( int i = 0; i < nbNodes - 1; i++ )
258 aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
259 aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
261 // Compute aspect ratio
265 double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
266 if ( anArea <= Precision::Confusion() )
268 double aMaxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
269 static double aCoef = sqrt( 3. ) / 4;
271 return aCoef * aMaxLen * aMaxLen / anArea;
275 double aMinLen = aLen[ 0 ];
276 double aMaxLen = aLen[ 0 ];
278 for(int i = 1; i < nbNodes ; i++ ){
279 aMinLen = Min( aMinLen, aLen[ i ] );
280 aMaxLen = Max( aMaxLen, aLen[ i ] );
282 if ( aMinLen <= Precision::Confusion() )
285 return aMaxLen / aMinLen;
289 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
291 // the aspect ratio is in the range [1.0,infinity]
294 return Value / 1000.;
297 SMDSAbs_ElementType AspectRatio::GetType() const
304 Class : AspectRatio3D
305 Description : Functor for calculating aspect ratio
309 inline double getHalfPerimeter(double theTria[3]){
310 return (theTria[0] + theTria[1] + theTria[2])/2.0;
313 inline double getArea(double theHalfPerim, double theTria[3]){
314 return sqrt(theHalfPerim*
315 (theHalfPerim-theTria[0])*
316 (theHalfPerim-theTria[1])*
317 (theHalfPerim-theTria[2]));
320 inline double getVolume(double theLen[6]){
321 double a2 = theLen[0]*theLen[0];
322 double b2 = theLen[1]*theLen[1];
323 double c2 = theLen[2]*theLen[2];
324 double d2 = theLen[3]*theLen[3];
325 double e2 = theLen[4]*theLen[4];
326 double f2 = theLen[5]*theLen[5];
327 double P = 4.0*a2*b2*d2;
328 double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
329 double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
330 return sqrt(P-Q+R)/12.0;
333 inline double getVolume2(double theLen[6]){
334 double a2 = theLen[0]*theLen[0];
335 double b2 = theLen[1]*theLen[1];
336 double c2 = theLen[2]*theLen[2];
337 double d2 = theLen[3]*theLen[3];
338 double e2 = theLen[4]*theLen[4];
339 double f2 = theLen[5]*theLen[5];
341 double P = a2*e2*(b2+c2+d2+f2-a2-e2);
342 double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
343 double R = c2*d2*(a2+b2+e2+f2-c2-d2);
344 double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
346 return sqrt(P+Q+R-S)/12.0;
349 inline double getVolume(const TSequenceOfXYZ& P){
350 gp_Vec aVec1( P( 2 ) - P( 1 ) );
351 gp_Vec aVec2( P( 3 ) - P( 1 ) );
352 gp_Vec aVec3( P( 4 ) - P( 1 ) );
353 gp_Vec anAreaVec( aVec1 ^ aVec2 );
354 return fabs(aVec3 * anAreaVec) / 6.0;
357 inline double getMaxHeight(double theLen[6])
359 double aHeight = max(theLen[0],theLen[1]);
360 aHeight = max(aHeight,theLen[2]);
361 aHeight = max(aHeight,theLen[3]);
362 aHeight = max(aHeight,theLen[4]);
363 aHeight = max(aHeight,theLen[5]);
369 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
371 double aQuality = 0.0;
372 int nbNodes = P.size();
376 getDistance(P( 1 ),P( 2 )), // a
377 getDistance(P( 2 ),P( 3 )), // b
378 getDistance(P( 3 ),P( 1 )), // c
379 getDistance(P( 2 ),P( 4 )), // d
380 getDistance(P( 3 ),P( 4 )), // e
381 getDistance(P( 1 ),P( 4 )) // f
383 double aTria[4][3] = {
384 {aLen[0],aLen[1],aLen[2]}, // abc
385 {aLen[0],aLen[3],aLen[5]}, // adf
386 {aLen[1],aLen[3],aLen[4]}, // bde
387 {aLen[2],aLen[4],aLen[5]} // cef
389 double aSumArea = 0.0;
390 double aHalfPerimeter = getHalfPerimeter(aTria[0]);
391 double anArea = getArea(aHalfPerimeter,aTria[0]);
393 aHalfPerimeter = getHalfPerimeter(aTria[1]);
394 anArea = getArea(aHalfPerimeter,aTria[1]);
396 aHalfPerimeter = getHalfPerimeter(aTria[2]);
397 anArea = getArea(aHalfPerimeter,aTria[2]);
399 aHalfPerimeter = getHalfPerimeter(aTria[3]);
400 anArea = getArea(aHalfPerimeter,aTria[3]);
402 double aVolume = getVolume(P);
403 //double aVolume = getVolume(aLen);
404 double aHeight = getMaxHeight(aLen);
405 static double aCoeff = sqrt(6.0)/36.0;
406 aQuality = aCoeff*aHeight*aSumArea/aVolume;
411 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
412 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
415 gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
416 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
419 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
420 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
423 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
424 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
430 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
431 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
434 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
435 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
438 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
439 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
442 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
443 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
446 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
447 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
450 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
451 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
457 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
458 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
461 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
462 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
465 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
466 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
469 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
470 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
473 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
474 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
477 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
478 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
481 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
482 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
485 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
486 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
489 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
490 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
493 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
494 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
497 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
498 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
501 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
502 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
505 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
506 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
509 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
510 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
513 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
514 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
517 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
518 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
521 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
522 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
525 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
526 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
529 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
530 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
533 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
534 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
537 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
538 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
541 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
542 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
545 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
546 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
549 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
550 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
553 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
554 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
557 gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
558 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
561 gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
562 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
565 gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
566 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
569 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
570 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
573 gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
574 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
577 gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
578 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
581 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
582 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
585 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
586 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
594 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
596 // the aspect ratio is in the range [1.0,infinity]
599 return Value / 1000.;
602 SMDSAbs_ElementType AspectRatio3D::GetType() const
604 return SMDSAbs_Volume;
610 Description : Functor for calculating warping
612 double Warping::GetValue( const TSequenceOfXYZ& P )
617 gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4;
619 double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
620 double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
621 double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
622 double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
624 return Max( Max( A1, A2 ), Max( A3, A4 ) );
627 double Warping::ComputeA( const gp_XYZ& thePnt1,
628 const gp_XYZ& thePnt2,
629 const gp_XYZ& thePnt3,
630 const gp_XYZ& theG ) const
632 double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
633 double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
634 double L = Min( aLen1, aLen2 ) * 0.5;
635 if ( L < Precision::Confusion())
638 gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
639 gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
640 gp_XYZ N = GI.Crossed( GJ );
642 if ( N.Modulus() < gp::Resolution() )
647 double H = ( thePnt2 - theG ).Dot( N );
648 return asin( fabs( H / L ) ) * 180 / PI;
651 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
653 // the warp is in the range [0.0,PI/2]
654 // 0.0 = good (no warp)
655 // PI/2 = bad (face pliee)
659 SMDSAbs_ElementType Warping::GetType() const
667 Description : Functor for calculating taper
669 double Taper::GetValue( const TSequenceOfXYZ& P )
675 double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2;
676 double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2;
677 double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2;
678 double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2;
680 double JA = 0.25 * ( J1 + J2 + J3 + J4 );
681 if ( JA <= Precision::Confusion() )
684 double T1 = fabs( ( J1 - JA ) / JA );
685 double T2 = fabs( ( J2 - JA ) / JA );
686 double T3 = fabs( ( J3 - JA ) / JA );
687 double T4 = fabs( ( J4 - JA ) / JA );
689 return Max( Max( T1, T2 ), Max( T3, T4 ) );
692 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
694 // the taper is in the range [0.0,1.0]
695 // 0.0 = good (no taper)
696 // 1.0 = bad (les cotes opposes sont allignes)
700 SMDSAbs_ElementType Taper::GetType() const
708 Description : Functor for calculating skew in degrees
710 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
712 gp_XYZ p12 = ( p2 + p1 ) / 2;
713 gp_XYZ p23 = ( p3 + p2 ) / 2;
714 gp_XYZ p31 = ( p3 + p1 ) / 2;
716 gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
718 return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
721 double Skew::GetValue( const TSequenceOfXYZ& P )
723 if ( P.size() != 3 && P.size() != 4 )
727 static double PI2 = PI / 2;
730 double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
731 double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
732 double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
734 return Max( A0, Max( A1, A2 ) ) * 180 / PI;
738 gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2;
739 gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2;
740 gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2;
741 gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2;
743 gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
744 double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
745 ? 0 : fabs( PI2 - v1.Angle( v2 ) );
751 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
753 // the skew is in the range [0.0,PI/2].
759 SMDSAbs_ElementType Skew::GetType() const
767 Description : Functor for calculating area
769 double Area::GetValue( const TSequenceOfXYZ& P )
773 return getArea( P( 1 ), P( 2 ), P( 3 ) );
774 else if (P.size() > 3)
775 aArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
779 for (int i=4; i<=P.size(); i++)
780 aArea += getArea(P(1),P(i-1),P(i));
784 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
789 SMDSAbs_ElementType Area::GetType() const
797 Description : Functor for calculating length off edge
799 double Length::GetValue( const TSequenceOfXYZ& P )
801 return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
804 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
809 SMDSAbs_ElementType Length::GetType() const
816 Description : Functor for calculating length of edge
819 double Length2D::GetValue( long theElementId)
823 if (GetPoints(theElementId,P)){
825 double aVal;// = GetValue( P );
826 const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
827 SMDSAbs_ElementType aType = aElem->GetType();
836 aVal = getDistance( P( 1 ), P( 2 ) );
840 if (len == 3){ // triangles
841 double L1 = getDistance(P( 1 ),P( 2 ));
842 double L2 = getDistance(P( 2 ),P( 3 ));
843 double L3 = getDistance(P( 3 ),P( 1 ));
844 aVal = Max(L1,Max(L2,L3));
847 else if (len == 4){ // quadrangles
848 double L1 = getDistance(P( 1 ),P( 2 ));
849 double L2 = getDistance(P( 2 ),P( 3 ));
850 double L3 = getDistance(P( 3 ),P( 4 ));
851 double L4 = getDistance(P( 4 ),P( 1 ));
852 aVal = Max(Max(L1,L2),Max(L3,L4));
856 if (len == 4){ // tetraidrs
857 double L1 = getDistance(P( 1 ),P( 2 ));
858 double L2 = getDistance(P( 2 ),P( 3 ));
859 double L3 = getDistance(P( 3 ),P( 1 ));
860 double L4 = getDistance(P( 1 ),P( 4 ));
861 double L5 = getDistance(P( 2 ),P( 4 ));
862 double L6 = getDistance(P( 3 ),P( 4 ));
863 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
866 else if (len == 5){ // piramids
867 double L1 = getDistance(P( 1 ),P( 2 ));
868 double L2 = getDistance(P( 2 ),P( 3 ));
869 double L3 = getDistance(P( 3 ),P( 1 ));
870 double L4 = getDistance(P( 4 ),P( 1 ));
871 double L5 = getDistance(P( 1 ),P( 5 ));
872 double L6 = getDistance(P( 2 ),P( 5 ));
873 double L7 = getDistance(P( 3 ),P( 5 ));
874 double L8 = getDistance(P( 4 ),P( 5 ));
876 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
877 aVal = Max(aVal,Max(L7,L8));
880 else if (len == 6){ // pentaidres
881 double L1 = getDistance(P( 1 ),P( 2 ));
882 double L2 = getDistance(P( 2 ),P( 3 ));
883 double L3 = getDistance(P( 3 ),P( 1 ));
884 double L4 = getDistance(P( 4 ),P( 5 ));
885 double L5 = getDistance(P( 5 ),P( 6 ));
886 double L6 = getDistance(P( 6 ),P( 4 ));
887 double L7 = getDistance(P( 1 ),P( 4 ));
888 double L8 = getDistance(P( 2 ),P( 5 ));
889 double L9 = getDistance(P( 3 ),P( 6 ));
891 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
892 aVal = Max(aVal,Max(Max(L7,L8),L9));
895 else if (len == 8){ // hexaider
896 double L1 = getDistance(P( 1 ),P( 2 ));
897 double L2 = getDistance(P( 2 ),P( 3 ));
898 double L3 = getDistance(P( 3 ),P( 4 ));
899 double L4 = getDistance(P( 4 ),P( 1 ));
900 double L5 = getDistance(P( 5 ),P( 6 ));
901 double L6 = getDistance(P( 6 ),P( 7 ));
902 double L7 = getDistance(P( 7 ),P( 8 ));
903 double L8 = getDistance(P( 8 ),P( 5 ));
904 double L9 = getDistance(P( 1 ),P( 5 ));
905 double L10= getDistance(P( 2 ),P( 6 ));
906 double L11= getDistance(P( 3 ),P( 7 ));
907 double L12= getDistance(P( 4 ),P( 8 ));
909 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
910 aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
911 aVal = Max(aVal,Max(L11,L12));
923 if ( myPrecision >= 0 )
925 double prec = pow( 10., (double)( myPrecision ) );
926 aVal = floor( aVal * prec + 0.5 ) / prec;
935 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
940 SMDSAbs_ElementType Length2D::GetType() const
945 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
948 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
949 if(thePntId1 > thePntId2){
950 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
954 bool Length2D::Value::operator<(const Length2D::Value& x) const{
955 if(myPntId[0] < x.myPntId[0]) return true;
956 if(myPntId[0] == x.myPntId[0])
957 if(myPntId[1] < x.myPntId[1]) return true;
961 void Length2D::GetValues(TValues& theValues){
963 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
964 for(; anIter->more(); ){
965 const SMDS_MeshFace* anElem = anIter->next();
966 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
971 const SMDS_MeshElement* aNode;
972 if(aNodesIter->more()){
973 aNode = aNodesIter->next();
974 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
975 P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
976 aNodeId[0] = aNodeId[1] = aNode->GetID();
979 for(; aNodesIter->more(); ){
980 aNode = aNodesIter->next();
981 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
982 long anId = aNode->GetID();
984 P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
986 aLength = P[1].Distance(P[2]);
988 Value aValue(aLength,aNodeId[1],anId);
991 theValues.insert(aValue);
994 aLength = P[0].Distance(P[1]);
996 Value aValue(aLength,aNodeId[0],aNodeId[1]);
997 theValues.insert(aValue);
1002 Class : MultiConnection
1003 Description : Functor for calculating number of faces conneted to the edge
1005 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1009 double MultiConnection::GetValue( long theId )
1011 return getNbMultiConnection( myMesh, theId );
1014 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1019 SMDSAbs_ElementType MultiConnection::GetType() const
1021 return SMDSAbs_Edge;
1025 Class : MultiConnection2D
1026 Description : Functor for calculating number of faces conneted to the edge
1028 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1033 double MultiConnection2D::GetValue( long theElementId )
1038 if (GetPoints(theElementId,P)){
1039 const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1040 SMDSAbs_ElementType aType = anFaceElem->GetType();
1044 TColStd_MapOfInteger aMap;
1052 if (len == 3){ // triangles
1053 int Nb[3] = {0,0,0};
1056 SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1057 if ( anIter != 0 ) {
1058 while( anIter->more() ) {
1059 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1063 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1064 while( anElemIter->more() ) {
1065 const SMDS_MeshElement* anElem = anElemIter->next();
1066 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1067 int anId = anElem->GetID();
1069 if ( anIter->more() ) // i.e. first node
1071 else if ( aMap.Contains( anId ) ){
1075 else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1080 aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1083 case SMDSAbs_Volume:
1088 return aResult;//getNbMultiConnection( myMesh, theId );
1091 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1096 SMDSAbs_ElementType MultiConnection2D::GetType() const
1098 return SMDSAbs_Face;
1101 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1103 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1104 if(thePntId1 > thePntId2){
1105 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1109 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1110 if(myPntId[0] < x.myPntId[0]) return true;
1111 if(myPntId[0] == x.myPntId[0])
1112 if(myPntId[1] < x.myPntId[1]) return true;
1116 void MultiConnection2D::GetValues(MValues& theValues){
1117 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1118 for(; anIter->more(); ){
1119 const SMDS_MeshFace* anElem = anIter->next();
1120 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1123 //int aNbConnects=0;
1124 const SMDS_MeshNode* aNode0;
1125 const SMDS_MeshNode* aNode1;
1126 const SMDS_MeshNode* aNode2;
1127 if(aNodesIter->more()){
1128 aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1130 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1131 aNodeId[0] = aNodeId[1] = aNodes->GetID();
1133 for(; aNodesIter->more(); ){
1134 aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1135 long anId = aNode2->GetID();
1138 Value aValue(aNodeId[1],aNodeId[2]);
1139 MValues::iterator aItr = theValues.find(aValue);
1140 if (aItr != theValues.end()){
1144 theValues[aValue] = 1;
1147 //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1148 aNodeId[1] = aNodeId[2];
1151 Value aValue(aNodeId[0],aNodeId[2]);
1152 MValues::iterator aItr = theValues.find(aValue);
1153 if (aItr != theValues.end()){
1157 theValues[aValue] = 1;
1160 //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1170 Class : BadOrientedVolume
1171 Description : Predicate bad oriented volumes
1174 BadOrientedVolume::BadOrientedVolume()
1179 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1184 bool BadOrientedVolume::IsSatisfy( long theId )
1189 SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1190 return !vTool.IsForward();
1193 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1195 return SMDSAbs_Volume;
1202 Description : Predicate for free borders
1205 FreeBorders::FreeBorders()
1210 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1215 bool FreeBorders::IsSatisfy( long theId )
1217 return getNbMultiConnection( myMesh, theId ) == 1;
1220 SMDSAbs_ElementType FreeBorders::GetType() const
1222 return SMDSAbs_Edge;
1228 Description : Predicate for free Edges
1230 FreeEdges::FreeEdges()
1235 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1240 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
1242 TColStd_MapOfInteger aMap;
1243 for ( int i = 0; i < 2; i++ )
1245 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1246 while( anElemIter->more() )
1248 const SMDS_MeshElement* anElem = anElemIter->next();
1249 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1251 int anId = anElem->GetID();
1255 else if ( aMap.Contains( anId ) && anId != theFaceId )
1263 bool FreeEdges::IsSatisfy( long theId )
1268 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1269 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1272 int nbNodes = aFace->NbNodes();
1273 const SMDS_MeshNode* aNodes[ nbNodes ];
1275 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
1278 while( anIter->more() )
1280 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1283 aNodes[ i++ ] = aNode;
1287 for ( int i = 0; i < nbNodes - 1; i++ )
1288 if ( IsFreeEdge( &aNodes[ i ], theId ) )
1291 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
1293 return IsFreeEdge( &aNodes[ 0 ], theId );
1297 SMDSAbs_ElementType FreeEdges::GetType() const
1299 return SMDSAbs_Face;
1302 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1305 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1306 if(thePntId1 > thePntId2){
1307 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1311 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1312 if(myPntId[0] < x.myPntId[0]) return true;
1313 if(myPntId[0] == x.myPntId[0])
1314 if(myPntId[1] < x.myPntId[1]) return true;
1318 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1319 FreeEdges::TBorders& theRegistry,
1320 FreeEdges::TBorders& theContainer)
1322 if(theRegistry.find(theBorder) == theRegistry.end()){
1323 theRegistry.insert(theBorder);
1324 theContainer.insert(theBorder);
1326 theContainer.erase(theBorder);
1330 void FreeEdges::GetBoreders(TBorders& theBorders)
1333 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1334 for(; anIter->more(); ){
1335 const SMDS_MeshFace* anElem = anIter->next();
1336 long anElemId = anElem->GetID();
1337 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1339 const SMDS_MeshElement* aNode;
1340 if(aNodesIter->more()){
1341 aNode = aNodesIter->next();
1342 aNodeId[0] = aNodeId[1] = aNode->GetID();
1344 for(; aNodesIter->more(); ){
1345 aNode = aNodesIter->next();
1346 long anId = aNode->GetID();
1347 Border aBorder(anElemId,aNodeId[1],anId);
1349 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1350 UpdateBorders(aBorder,aRegistry,theBorders);
1352 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1353 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1354 UpdateBorders(aBorder,aRegistry,theBorders);
1356 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1361 Description : Predicate for Range of Ids.
1362 Range may be specified with two ways.
1363 1. Using AddToRange method
1364 2. With SetRangeStr method. Parameter of this method is a string
1365 like as "1,2,3,50-60,63,67,70-"
1368 //=======================================================================
1369 // name : RangeOfIds
1370 // Purpose : Constructor
1371 //=======================================================================
1372 RangeOfIds::RangeOfIds()
1375 myType = SMDSAbs_All;
1378 //=======================================================================
1380 // Purpose : Set mesh
1381 //=======================================================================
1382 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1387 //=======================================================================
1388 // name : AddToRange
1389 // Purpose : Add ID to the range
1390 //=======================================================================
1391 bool RangeOfIds::AddToRange( long theEntityId )
1393 myIds.Add( theEntityId );
1397 //=======================================================================
1398 // name : GetRangeStr
1399 // Purpose : Get range as a string.
1400 // Example: "1,2,3,50-60,63,67,70-"
1401 //=======================================================================
1402 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1406 TColStd_SequenceOfInteger anIntSeq;
1407 TColStd_SequenceOfAsciiString aStrSeq;
1409 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1410 for ( ; anIter.More(); anIter.Next() )
1412 int anId = anIter.Key();
1413 TCollection_AsciiString aStr( anId );
1414 anIntSeq.Append( anId );
1415 aStrSeq.Append( aStr );
1418 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1420 int aMinId = myMin( i );
1421 int aMaxId = myMax( i );
1423 TCollection_AsciiString aStr;
1424 if ( aMinId != IntegerFirst() )
1429 if ( aMaxId != IntegerLast() )
1432 // find position of the string in result sequence and insert string in it
1433 if ( anIntSeq.Length() == 0 )
1435 anIntSeq.Append( aMinId );
1436 aStrSeq.Append( aStr );
1440 if ( aMinId < anIntSeq.First() )
1442 anIntSeq.Prepend( aMinId );
1443 aStrSeq.Prepend( aStr );
1445 else if ( aMinId > anIntSeq.Last() )
1447 anIntSeq.Append( aMinId );
1448 aStrSeq.Append( aStr );
1451 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1452 if ( aMinId < anIntSeq( j ) )
1454 anIntSeq.InsertBefore( j, aMinId );
1455 aStrSeq.InsertBefore( j, aStr );
1461 if ( aStrSeq.Length() == 0 )
1464 theResStr = aStrSeq( 1 );
1465 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1468 theResStr += aStrSeq( j );
1472 //=======================================================================
1473 // name : SetRangeStr
1474 // Purpose : Define range with string
1475 // Example of entry string: "1,2,3,50-60,63,67,70-"
1476 //=======================================================================
1477 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1483 TCollection_AsciiString aStr = theStr;
1484 aStr.RemoveAll( ' ' );
1485 aStr.RemoveAll( '\t' );
1487 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1488 aStr.Remove( aPos, 2 );
1490 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1492 while ( tmpStr != "" )
1494 tmpStr = aStr.Token( ",", i++ );
1495 int aPos = tmpStr.Search( '-' );
1499 if ( tmpStr.IsIntegerValue() )
1500 myIds.Add( tmpStr.IntegerValue() );
1506 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1507 TCollection_AsciiString aMinStr = tmpStr;
1509 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1510 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1512 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1513 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1516 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1517 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1524 //=======================================================================
1526 // Purpose : Get type of supported entities
1527 //=======================================================================
1528 SMDSAbs_ElementType RangeOfIds::GetType() const
1533 //=======================================================================
1535 // Purpose : Set type of supported entities
1536 //=======================================================================
1537 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1542 //=======================================================================
1544 // Purpose : Verify whether entity satisfies to this rpedicate
1545 //=======================================================================
1546 bool RangeOfIds::IsSatisfy( long theId )
1551 if ( myType == SMDSAbs_Node )
1553 if ( myMesh->FindNode( theId ) == 0 )
1558 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1559 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1563 if ( myIds.Contains( theId ) )
1566 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1567 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1575 Description : Base class for comparators
1577 Comparator::Comparator():
1581 Comparator::~Comparator()
1584 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1587 myFunctor->SetMesh( theMesh );
1590 void Comparator::SetMargin( double theValue )
1592 myMargin = theValue;
1595 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1597 myFunctor = theFunct;
1600 SMDSAbs_ElementType Comparator::GetType() const
1602 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1605 double Comparator::GetMargin()
1613 Description : Comparator "<"
1615 bool LessThan::IsSatisfy( long theId )
1617 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1623 Description : Comparator ">"
1625 bool MoreThan::IsSatisfy( long theId )
1627 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1633 Description : Comparator "="
1636 myToler(Precision::Confusion())
1639 bool EqualTo::IsSatisfy( long theId )
1641 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1644 void EqualTo::SetTolerance( double theToler )
1649 double EqualTo::GetTolerance()
1656 Description : Logical NOT predicate
1658 LogicalNOT::LogicalNOT()
1661 LogicalNOT::~LogicalNOT()
1664 bool LogicalNOT::IsSatisfy( long theId )
1666 return myPredicate && !myPredicate->IsSatisfy( theId );
1669 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1672 myPredicate->SetMesh( theMesh );
1675 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1677 myPredicate = thePred;
1680 SMDSAbs_ElementType LogicalNOT::GetType() const
1682 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1687 Class : LogicalBinary
1688 Description : Base class for binary logical predicate
1690 LogicalBinary::LogicalBinary()
1693 LogicalBinary::~LogicalBinary()
1696 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1699 myPredicate1->SetMesh( theMesh );
1702 myPredicate2->SetMesh( theMesh );
1705 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1707 myPredicate1 = thePredicate;
1710 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1712 myPredicate2 = thePredicate;
1715 SMDSAbs_ElementType LogicalBinary::GetType() const
1717 if ( !myPredicate1 || !myPredicate2 )
1720 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1721 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1723 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1729 Description : Logical AND
1731 bool LogicalAND::IsSatisfy( long theId )
1736 myPredicate1->IsSatisfy( theId ) &&
1737 myPredicate2->IsSatisfy( theId );
1743 Description : Logical OR
1745 bool LogicalOR::IsSatisfy( long theId )
1750 myPredicate1->IsSatisfy( theId ) ||
1751 myPredicate2->IsSatisfy( theId );
1765 void Filter::SetPredicate( PredicatePtr thePredicate )
1767 myPredicate = thePredicate;
1770 template<class TElement, class TIterator, class TPredicate>
1771 inline void FillSequence(const TIterator& theIterator,
1772 TPredicate& thePredicate,
1773 Filter::TIdSequence& theSequence)
1775 if ( theIterator ) {
1776 while( theIterator->more() ) {
1777 TElement anElem = theIterator->next();
1778 long anId = anElem->GetID();
1779 if ( thePredicate->IsSatisfy( anId ) )
1780 theSequence.push_back( anId );
1787 GetElementsId( const SMDS_Mesh* theMesh,
1788 PredicatePtr thePredicate,
1789 TIdSequence& theSequence )
1791 theSequence.clear();
1793 if ( !theMesh || !thePredicate )
1796 thePredicate->SetMesh( theMesh );
1798 SMDSAbs_ElementType aType = thePredicate->GetType();
1801 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
1804 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1807 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1809 case SMDSAbs_Volume:
1810 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1813 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1814 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1815 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1821 Filter::GetElementsId( const SMDS_Mesh* theMesh,
1822 Filter::TIdSequence& theSequence )
1824 GetElementsId(theMesh,myPredicate,theSequence);
1831 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1837 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1838 SMDS_MeshNode* theNode2 )
1844 ManifoldPart::Link::~Link()
1850 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1852 if ( myNode1 == theLink.myNode1 &&
1853 myNode2 == theLink.myNode2 )
1855 else if ( myNode1 == theLink.myNode2 &&
1856 myNode2 == theLink.myNode1 )
1862 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1864 if(myNode1 < x.myNode1) return true;
1865 if(myNode1 == x.myNode1)
1866 if(myNode2 < x.myNode2) return true;
1870 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1871 const ManifoldPart::Link& theLink2 )
1873 return theLink1.IsEqual( theLink2 );
1876 ManifoldPart::ManifoldPart()
1879 myAngToler = Precision::Angular();
1880 myIsOnlyManifold = true;
1883 ManifoldPart::~ManifoldPart()
1888 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
1894 SMDSAbs_ElementType ManifoldPart::GetType() const
1895 { return SMDSAbs_Face; }
1897 bool ManifoldPart::IsSatisfy( long theElementId )
1899 return myMapIds.Contains( theElementId );
1902 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1903 { myAngToler = theAngToler; }
1905 double ManifoldPart::GetAngleTolerance() const
1906 { return myAngToler; }
1908 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1909 { myIsOnlyManifold = theIsOnly; }
1911 void ManifoldPart::SetStartElem( const long theStartId )
1912 { myStartElemId = theStartId; }
1914 bool ManifoldPart::process()
1917 myMapBadGeomIds.Clear();
1919 myAllFacePtr.clear();
1920 myAllFacePtrIntDMap.clear();
1924 // collect all faces into own map
1925 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1926 for (; anFaceItr->more(); )
1928 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1929 myAllFacePtr.push_back( aFacePtr );
1930 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1933 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1937 // the map of non manifold links and bad geometry
1938 TMapOfLink aMapOfNonManifold;
1939 TColStd_MapOfInteger aMapOfTreated;
1941 // begin cycle on faces from start index and run on vector till the end
1942 // and from begin to start index to cover whole vector
1943 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1944 bool isStartTreat = false;
1945 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1947 if ( fi == aStartIndx )
1948 isStartTreat = true;
1949 // as result next time when fi will be equal to aStartIndx
1951 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1952 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1955 aMapOfTreated.Add( aFacePtr->GetID() );
1956 TColStd_MapOfInteger aResFaces;
1957 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1958 aMapOfNonManifold, aResFaces ) )
1960 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1961 for ( ; anItr.More(); anItr.Next() )
1963 int aFaceId = anItr.Key();
1964 aMapOfTreated.Add( aFaceId );
1965 myMapIds.Add( aFaceId );
1968 if ( fi == ( myAllFacePtr.size() - 1 ) )
1970 } // end run on vector of faces
1971 return !myMapIds.IsEmpty();
1974 static void getLinks( const SMDS_MeshFace* theFace,
1975 ManifoldPart::TVectorOfLink& theLinks )
1977 int aNbNode = theFace->NbNodes();
1978 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1980 SMDS_MeshNode* aNode = 0;
1981 for ( ; aNodeItr->more() && i <= aNbNode; )
1984 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
1988 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
1990 ManifoldPart::Link aLink( aN1, aN2 );
1991 theLinks.push_back( aLink );
1995 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
1998 int aNbNode = theFace->NbNodes();
1999 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
2000 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2002 for ( ; aNodeItr->more() && i <= 4; i++ )
2004 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2005 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2008 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2009 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2013 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2016 double len = n.Modulus();
2023 bool ManifoldPart::findConnected
2024 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2025 SMDS_MeshFace* theStartFace,
2026 ManifoldPart::TMapOfLink& theNonManifold,
2027 TColStd_MapOfInteger& theResFaces )
2029 theResFaces.Clear();
2030 if ( !theAllFacePtrInt.size() )
2033 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2035 myMapBadGeomIds.Add( theStartFace->GetID() );
2039 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2040 ManifoldPart::TVectorOfLink aSeqOfBoundary;
2041 theResFaces.Add( theStartFace->GetID() );
2042 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2044 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2045 aDMapLinkFace, theNonManifold, theStartFace );
2047 bool isDone = false;
2048 while ( !isDone && aMapOfBoundary.size() != 0 )
2050 bool isToReset = false;
2051 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2052 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2054 ManifoldPart::Link aLink = *pLink;
2055 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2057 // each link could be treated only once
2058 aMapToSkip.insert( aLink );
2060 ManifoldPart::TVectorOfFacePtr aFaces;
2062 if ( myIsOnlyManifold &&
2063 (theNonManifold.find( aLink ) != theNonManifold.end()) )
2067 getFacesByLink( aLink, aFaces );
2068 // filter the element to keep only indicated elements
2069 ManifoldPart::TVectorOfFacePtr aFiltered;
2070 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2071 for ( ; pFace != aFaces.end(); ++pFace )
2073 SMDS_MeshFace* aFace = *pFace;
2074 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2075 aFiltered.push_back( aFace );
2078 if ( aFaces.size() < 2 ) // no neihgbour faces
2080 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2082 theNonManifold.insert( aLink );
2087 // compare normal with normals of neighbor element
2088 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2089 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2090 for ( ; pFace != aFaces.end(); ++pFace )
2092 SMDS_MeshFace* aNextFace = *pFace;
2093 if ( aPrevFace == aNextFace )
2095 int anNextFaceID = aNextFace->GetID();
2096 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2097 // should not be with non manifold restriction. probably bad topology
2099 // check if face was treated and skipped
2100 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2101 !isInPlane( aPrevFace, aNextFace ) )
2103 // add new element to connected and extend the boundaries.
2104 theResFaces.Add( anNextFaceID );
2105 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2106 aDMapLinkFace, theNonManifold, aNextFace );
2110 isDone = !isToReset;
2113 return !theResFaces.IsEmpty();
2116 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2117 const SMDS_MeshFace* theFace2 )
2119 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2120 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2121 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2123 myMapBadGeomIds.Add( theFace2->GetID() );
2126 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2132 void ManifoldPart::expandBoundary
2133 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
2134 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
2135 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2136 ManifoldPart::TMapOfLink& theNonManifold,
2137 SMDS_MeshFace* theNextFace ) const
2139 ManifoldPart::TVectorOfLink aLinks;
2140 getLinks( theNextFace, aLinks );
2141 int aNbLink = aLinks.size();
2142 for ( int i = 0; i < aNbLink; i++ )
2144 ManifoldPart::Link aLink = aLinks[ i ];
2145 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2147 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2149 if ( myIsOnlyManifold )
2151 // remove from boundary
2152 theMapOfBoundary.erase( aLink );
2153 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2154 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2156 ManifoldPart::Link aBoundLink = *pLink;
2157 if ( aBoundLink.IsEqual( aLink ) )
2159 theSeqOfBoundary.erase( pLink );
2167 theMapOfBoundary.insert( aLink );
2168 theSeqOfBoundary.push_back( aLink );
2169 theDMapLinkFacePtr[ aLink ] = theNextFace;
2174 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2175 ManifoldPart::TVectorOfFacePtr& theFaces ) const
2177 SMDS_Mesh::SetOfFaces aSetOfFaces;
2178 // take all faces that shared first node
2179 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2180 for ( ; anItr->more(); )
2182 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2185 aSetOfFaces.Add( aFace );
2187 // take all faces that shared second node
2188 anItr = theLink.myNode2->facesIterator();
2189 // find the common part of two sets
2190 for ( ; anItr->more(); )
2192 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2193 if ( aSetOfFaces.Contains( aFace ) )
2194 theFaces.push_back( aFace );
2203 ElementsOnSurface::ElementsOnSurface()
2207 myType = SMDSAbs_All;
2209 myToler = Precision::Confusion();
2212 ElementsOnSurface::~ElementsOnSurface()
2217 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2219 if ( myMesh == theMesh )
2226 bool ElementsOnSurface::IsSatisfy( long theElementId )
2228 return myIds.Contains( theElementId );
2231 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2234 void ElementsOnSurface::SetTolerance( const double theToler )
2235 { myToler = theToler; }
2237 double ElementsOnSurface::GetTolerance() const
2242 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2243 const SMDSAbs_ElementType theType )
2247 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2252 TopoDS_Face aFace = TopoDS::Face( theShape );
2253 mySurf = BRep_Tool::Surface( aFace );
2256 void ElementsOnSurface::process()
2259 if ( mySurf.IsNull() )
2265 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2267 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2268 for(; anIter->more(); )
2269 process( anIter->next() );
2272 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2274 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2275 for(; anIter->more(); )
2276 process( anIter->next() );
2279 if ( myType == SMDSAbs_Node )
2281 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2282 for(; anIter->more(); )
2283 process( anIter->next() );
2287 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2289 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2290 bool isSatisfy = true;
2291 for ( ; aNodeItr->more(); )
2293 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2294 if ( !isOnSurface( aNode ) )
2301 myIds.Add( theElemPtr->GetID() );
2304 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2306 if ( mySurf.IsNull() )
2309 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2310 double aToler2 = myToler * myToler;
2311 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2313 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2314 if ( aPln.SquareDistance( aPnt ) > aToler2 )
2317 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2319 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2320 double aRad = aCyl.Radius();
2321 gp_Ax3 anAxis = aCyl.Position();
2322 gp_XYZ aLoc = aCyl.Location().XYZ();
2323 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2324 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2325 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )