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;
207 //=======================================================================
208 //function : GetValue
210 //=======================================================================
212 double Volume::GetValue( long theElementId )
214 if ( theElementId && myMesh ) {
215 SMDS_VolumeTool aVolumeTool;
216 if ( aVolumeTool.Set( myMesh->FindElement( theElementId )))
217 return aVolumeTool.GetSize();
222 //=======================================================================
223 //function : GetBadRate
224 //purpose : meaningless as it is not quality control functor
225 //=======================================================================
227 double Volume::GetBadRate( double Value, int /*nbNodes*/ ) const
232 //=======================================================================
235 //=======================================================================
237 SMDSAbs_ElementType Volume::GetType() const
239 return SMDSAbs_Volume;
245 Description : Functor for calculation of minimum angle
248 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
255 aMin = getAngle(P( P.size() ), P( 1 ), P( 2 ));
256 aMin = Min(aMin,getAngle(P( P.size()-1 ), P( P.size() ), P( 1 )));
258 for (int i=2; i<P.size();i++){
259 double A0 = getAngle( P( i-1 ), P( i ), P( i+1 ) );
263 return aMin * 180.0 / PI;
266 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
268 //const double aBestAngle = PI / nbNodes;
269 const double aBestAngle = 180.0 - ( 360.0 / double(nbNodes) );
270 return ( fabs( aBestAngle - Value ));
273 SMDSAbs_ElementType MinimumAngle::GetType() const
281 Description : Functor for calculating aspect ratio
283 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
285 // According to "Mesh quality control" by Nadir Bouhamau referring to
286 // Pascal Jean Frey and Paul-Louis George. Maillages, applications aux elements finis.
287 // Hermes Science publications, Paris 1999 ISBN 2-7462-0024-4
289 int nbNodes = P.size();
294 // Compute lengths of the sides
296 vector< double > aLen (nbNodes);
298 for ( int i = 0; i < nbNodes - 1; i++ )
299 aLen[ i ] = getDistance( P( i + 1 ), P( i + 2 ) );
300 aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P( nbNodes ) );
302 // Compute aspect ratio
306 // Q = alfa * h * p / S, where
308 // alfa = sqrt( 3 ) / 6
309 // h - length of the longest edge
310 // p - half perimeter
311 // S - triangle surface
313 const double alfa = sqrt( 3. ) / 6.;
314 double maxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
315 double half_perimeter = ( aLen[0] + aLen[1] + aLen[2] ) / 2.;
316 double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
317 if ( anArea <= Precision::Confusion() )
320 return alfa * maxLen * half_perimeter / anArea;
324 // return aspect ratio of the worst triange which can be built
325 // taking three nodes of the quadrangle
326 TSequenceOfXYZ triaPnts(3);
327 // triangle on nodes 1 3 2
331 double ar = GetValue( triaPnts );
332 // triangle on nodes 1 3 4
334 ar = Max ( ar, GetValue( triaPnts ));
335 // triangle on nodes 1 2 4
337 ar = Max ( ar, GetValue( triaPnts ));
338 // triangle on nodes 3 2 4
340 ar = Max ( ar, GetValue( triaPnts ));
346 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
348 // the aspect ratio is in the range [1.0,infinity]
351 return Value / 1000.;
354 SMDSAbs_ElementType AspectRatio::GetType() const
361 Class : AspectRatio3D
362 Description : Functor for calculating aspect ratio
366 inline double getHalfPerimeter(double theTria[3]){
367 return (theTria[0] + theTria[1] + theTria[2])/2.0;
370 inline double getArea(double theHalfPerim, double theTria[3]){
371 return sqrt(theHalfPerim*
372 (theHalfPerim-theTria[0])*
373 (theHalfPerim-theTria[1])*
374 (theHalfPerim-theTria[2]));
377 inline double getVolume(double theLen[6]){
378 double a2 = theLen[0]*theLen[0];
379 double b2 = theLen[1]*theLen[1];
380 double c2 = theLen[2]*theLen[2];
381 double d2 = theLen[3]*theLen[3];
382 double e2 = theLen[4]*theLen[4];
383 double f2 = theLen[5]*theLen[5];
384 double P = 4.0*a2*b2*d2;
385 double Q = a2*(b2+d2-e2)-b2*(a2+d2-f2)-d2*(a2+b2-c2);
386 double R = (b2+d2-e2)*(a2+d2-f2)*(a2+d2-f2);
387 return sqrt(P-Q+R)/12.0;
390 inline double getVolume2(double theLen[6]){
391 double a2 = theLen[0]*theLen[0];
392 double b2 = theLen[1]*theLen[1];
393 double c2 = theLen[2]*theLen[2];
394 double d2 = theLen[3]*theLen[3];
395 double e2 = theLen[4]*theLen[4];
396 double f2 = theLen[5]*theLen[5];
398 double P = a2*e2*(b2+c2+d2+f2-a2-e2);
399 double Q = b2*f2*(a2+c2+d2+e2-b2-f2);
400 double R = c2*d2*(a2+b2+e2+f2-c2-d2);
401 double S = a2*b2*d2+b2*c2*e2+a2*c2*f2+d2*e2*f2;
403 return sqrt(P+Q+R-S)/12.0;
406 inline double getVolume(const TSequenceOfXYZ& P){
407 gp_Vec aVec1( P( 2 ) - P( 1 ) );
408 gp_Vec aVec2( P( 3 ) - P( 1 ) );
409 gp_Vec aVec3( P( 4 ) - P( 1 ) );
410 gp_Vec anAreaVec( aVec1 ^ aVec2 );
411 return fabs(aVec3 * anAreaVec) / 6.0;
414 inline double getMaxHeight(double theLen[6])
416 double aHeight = max(theLen[0],theLen[1]);
417 aHeight = max(aHeight,theLen[2]);
418 aHeight = max(aHeight,theLen[3]);
419 aHeight = max(aHeight,theLen[4]);
420 aHeight = max(aHeight,theLen[5]);
426 double AspectRatio3D::GetValue( const TSequenceOfXYZ& P )
428 double aQuality = 0.0;
429 int nbNodes = P.size();
433 getDistance(P( 1 ),P( 2 )), // a
434 getDistance(P( 2 ),P( 3 )), // b
435 getDistance(P( 3 ),P( 1 )), // c
436 getDistance(P( 2 ),P( 4 )), // d
437 getDistance(P( 3 ),P( 4 )), // e
438 getDistance(P( 1 ),P( 4 )) // f
440 double aTria[4][3] = {
441 {aLen[0],aLen[1],aLen[2]}, // abc
442 {aLen[0],aLen[3],aLen[5]}, // adf
443 {aLen[1],aLen[3],aLen[4]}, // bde
444 {aLen[2],aLen[4],aLen[5]} // cef
446 double aSumArea = 0.0;
447 double aHalfPerimeter = getHalfPerimeter(aTria[0]);
448 double anArea = getArea(aHalfPerimeter,aTria[0]);
450 aHalfPerimeter = getHalfPerimeter(aTria[1]);
451 anArea = getArea(aHalfPerimeter,aTria[1]);
453 aHalfPerimeter = getHalfPerimeter(aTria[2]);
454 anArea = getArea(aHalfPerimeter,aTria[2]);
456 aHalfPerimeter = getHalfPerimeter(aTria[3]);
457 anArea = getArea(aHalfPerimeter,aTria[3]);
459 double aVolume = getVolume(P);
460 //double aVolume = getVolume(aLen);
461 double aHeight = getMaxHeight(aLen);
462 static double aCoeff = sqrt(2.0)/12.0;
463 aQuality = aCoeff*aHeight*aSumArea/aVolume;
468 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 3 ),P( 5 )};
469 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
472 gp_XYZ aXYZ[4] = {P( 1 ),P( 3 ),P( 4 ),P( 5 )};
473 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
476 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 5 )};
477 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
480 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 4 ),P( 5 )};
481 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
487 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 6 )};
488 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
491 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 3 )};
492 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
495 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 6 )};
496 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
499 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
500 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
503 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 6 )};
504 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
507 gp_XYZ aXYZ[4] = {P( 2 ),P( 5 ),P( 4 ),P( 3 )};
508 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
514 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 3 )};
515 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
518 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 4 )};
519 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
522 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 7 )};
523 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
526 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 5 ),P( 8 )};
527 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
530 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 3 )};
531 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
534 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 4 )};
535 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
538 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 7 )};
539 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
542 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 6 ),P( 8 )};
543 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
546 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 3 )};
547 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
550 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 4 )};
551 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
554 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 7 )};
555 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
558 gp_XYZ aXYZ[4] = {P( 2 ),P( 6 ),P( 5 ),P( 8 )};
559 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
562 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 1 )};
563 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
566 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 2 )};
567 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
570 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 5 )};
571 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
574 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 8 ),P( 6 )};
575 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
578 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 1 )};
579 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
582 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 2 )};
583 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
586 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 5 )};
587 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
590 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 7 ),P( 6 )};
591 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
594 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 1 )};
595 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
598 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
599 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
602 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 5 )};
603 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
606 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 6 )};
607 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
610 gp_XYZ aXYZ[4] = {P( 4 ),P( 8 ),P( 7 ),P( 2 )};
611 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
614 gp_XYZ aXYZ[4] = {P( 4 ),P( 5 ),P( 8 ),P( 2 )};
615 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
618 gp_XYZ aXYZ[4] = {P( 1 ),P( 4 ),P( 5 ),P( 3 )};
619 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
622 gp_XYZ aXYZ[4] = {P( 3 ),P( 6 ),P( 7 ),P( 1 )};
623 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
626 gp_XYZ aXYZ[4] = {P( 2 ),P( 3 ),P( 6 ),P( 4 )};
627 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
630 gp_XYZ aXYZ[4] = {P( 5 ),P( 6 ),P( 8 ),P( 3 )};
631 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
634 gp_XYZ aXYZ[4] = {P( 7 ),P( 8 ),P( 6 ),P( 1 )};
635 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
638 gp_XYZ aXYZ[4] = {P( 1 ),P( 2 ),P( 4 ),P( 7 )};
639 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
642 gp_XYZ aXYZ[4] = {P( 3 ),P( 4 ),P( 2 ),P( 5 )};
643 aQuality = max(GetValue(TSequenceOfXYZ(&aXYZ[0],&aXYZ[4])),aQuality);
649 // avaluate aspect ratio of quadranle faces
650 AspectRatio aspect2D;
651 SMDS_VolumeTool::VolumeType type = SMDS_VolumeTool::GetType( nbNodes );
652 int nbFaces = SMDS_VolumeTool::NbFaces( type );
653 TSequenceOfXYZ points(4);
654 for ( int i = 0; i < nbFaces; ++i ) { // loop on faces of a volume
655 if ( SMDS_VolumeTool::NbFaceNodes( type, i ) != 4 )
657 const int* pInd = SMDS_VolumeTool::GetFaceNodesIndices( type, i, true );
658 for ( int p = 0; p < 4; ++p ) // loop on nodes of a quadranle face
659 points( p + 1 ) = P( pInd[ p ] + 1 );
660 aQuality = max( aQuality, aspect2D.GetValue( points ));
666 double AspectRatio3D::GetBadRate( double Value, int /*nbNodes*/ ) const
668 // the aspect ratio is in the range [1.0,infinity]
671 return Value / 1000.;
674 SMDSAbs_ElementType AspectRatio3D::GetType() const
676 return SMDSAbs_Volume;
682 Description : Functor for calculating warping
684 double Warping::GetValue( const TSequenceOfXYZ& P )
689 gp_XYZ G = ( P( 1 ) + P( 2 ) + P( 3 ) + P( 4 ) ) / 4;
691 double A1 = ComputeA( P( 1 ), P( 2 ), P( 3 ), G );
692 double A2 = ComputeA( P( 2 ), P( 3 ), P( 4 ), G );
693 double A3 = ComputeA( P( 3 ), P( 4 ), P( 1 ), G );
694 double A4 = ComputeA( P( 4 ), P( 1 ), P( 2 ), G );
696 return Max( Max( A1, A2 ), Max( A3, A4 ) );
699 double Warping::ComputeA( const gp_XYZ& thePnt1,
700 const gp_XYZ& thePnt2,
701 const gp_XYZ& thePnt3,
702 const gp_XYZ& theG ) const
704 double aLen1 = gp_Pnt( thePnt1 ).Distance( gp_Pnt( thePnt2 ) );
705 double aLen2 = gp_Pnt( thePnt2 ).Distance( gp_Pnt( thePnt3 ) );
706 double L = Min( aLen1, aLen2 ) * 0.5;
707 if ( L < Precision::Confusion())
710 gp_XYZ GI = ( thePnt2 + thePnt1 ) / 2. - theG;
711 gp_XYZ GJ = ( thePnt3 + thePnt2 ) / 2. - theG;
712 gp_XYZ N = GI.Crossed( GJ );
714 if ( N.Modulus() < gp::Resolution() )
719 double H = ( thePnt2 - theG ).Dot( N );
720 return asin( fabs( H / L ) ) * 180 / PI;
723 double Warping::GetBadRate( double Value, int /*nbNodes*/ ) const
725 // the warp is in the range [0.0,PI/2]
726 // 0.0 = good (no warp)
727 // PI/2 = bad (face pliee)
731 SMDSAbs_ElementType Warping::GetType() const
739 Description : Functor for calculating taper
741 double Taper::GetValue( const TSequenceOfXYZ& P )
747 double J1 = getArea( P( 4 ), P( 1 ), P( 2 ) ) / 2;
748 double J2 = getArea( P( 3 ), P( 1 ), P( 2 ) ) / 2;
749 double J3 = getArea( P( 2 ), P( 3 ), P( 4 ) ) / 2;
750 double J4 = getArea( P( 3 ), P( 4 ), P( 1 ) ) / 2;
752 double JA = 0.25 * ( J1 + J2 + J3 + J4 );
753 if ( JA <= Precision::Confusion() )
756 double T1 = fabs( ( J1 - JA ) / JA );
757 double T2 = fabs( ( J2 - JA ) / JA );
758 double T3 = fabs( ( J3 - JA ) / JA );
759 double T4 = fabs( ( J4 - JA ) / JA );
761 return Max( Max( T1, T2 ), Max( T3, T4 ) );
764 double Taper::GetBadRate( double Value, int /*nbNodes*/ ) const
766 // the taper is in the range [0.0,1.0]
767 // 0.0 = good (no taper)
768 // 1.0 = bad (les cotes opposes sont allignes)
772 SMDSAbs_ElementType Taper::GetType() const
780 Description : Functor for calculating skew in degrees
782 static inline double skewAngle( const gp_XYZ& p1, const gp_XYZ& p2, const gp_XYZ& p3 )
784 gp_XYZ p12 = ( p2 + p1 ) / 2;
785 gp_XYZ p23 = ( p3 + p2 ) / 2;
786 gp_XYZ p31 = ( p3 + p1 ) / 2;
788 gp_Vec v1( p31 - p2 ), v2( p12 - p23 );
790 return v1.Magnitude() < gp::Resolution() || v2.Magnitude() < gp::Resolution() ? 0 : v1.Angle( v2 );
793 double Skew::GetValue( const TSequenceOfXYZ& P )
795 if ( P.size() != 3 && P.size() != 4 )
799 static double PI2 = PI / 2;
802 double A0 = fabs( PI2 - skewAngle( P( 3 ), P( 1 ), P( 2 ) ) );
803 double A1 = fabs( PI2 - skewAngle( P( 1 ), P( 2 ), P( 3 ) ) );
804 double A2 = fabs( PI2 - skewAngle( P( 2 ), P( 3 ), P( 1 ) ) );
806 return Max( A0, Max( A1, A2 ) ) * 180 / PI;
810 gp_XYZ p12 = ( P( 1 ) + P( 2 ) ) / 2;
811 gp_XYZ p23 = ( P( 2 ) + P( 3 ) ) / 2;
812 gp_XYZ p34 = ( P( 3 ) + P( 4 ) ) / 2;
813 gp_XYZ p41 = ( P( 4 ) + P( 1 ) ) / 2;
815 gp_Vec v1( p34 - p12 ), v2( p23 - p41 );
816 double A = v1.Magnitude() <= gp::Resolution() || v2.Magnitude() <= gp::Resolution()
817 ? 0 : fabs( PI2 - v1.Angle( v2 ) );
823 double Skew::GetBadRate( double Value, int /*nbNodes*/ ) const
825 // the skew is in the range [0.0,PI/2].
831 SMDSAbs_ElementType Skew::GetType() const
839 Description : Functor for calculating area
841 double Area::GetValue( const TSequenceOfXYZ& P )
845 return getArea( P( 1 ), P( 2 ), P( 3 ) );
846 else if (P.size() > 3)
847 aArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
851 for (int i=4; i<=P.size(); i++)
852 aArea += getArea(P(1),P(i-1),P(i));
856 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
858 // meaningless as it is not quality control functor
862 SMDSAbs_ElementType Area::GetType() const
870 Description : Functor for calculating length off edge
872 double Length::GetValue( const TSequenceOfXYZ& P )
874 return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
877 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
879 // meaningless as it is not quality control functor
883 SMDSAbs_ElementType Length::GetType() const
890 Description : Functor for calculating length of edge
893 double Length2D::GetValue( long theElementId)
897 if (GetPoints(theElementId,P)){
899 double aVal;// = GetValue( P );
900 const SMDS_MeshElement* aElem = myMesh->FindElement( theElementId );
901 SMDSAbs_ElementType aType = aElem->GetType();
910 aVal = getDistance( P( 1 ), P( 2 ) );
914 if (len == 3){ // triangles
915 double L1 = getDistance(P( 1 ),P( 2 ));
916 double L2 = getDistance(P( 2 ),P( 3 ));
917 double L3 = getDistance(P( 3 ),P( 1 ));
918 aVal = Max(L1,Max(L2,L3));
921 else if (len == 4){ // quadrangles
922 double L1 = getDistance(P( 1 ),P( 2 ));
923 double L2 = getDistance(P( 2 ),P( 3 ));
924 double L3 = getDistance(P( 3 ),P( 4 ));
925 double L4 = getDistance(P( 4 ),P( 1 ));
926 aVal = Max(Max(L1,L2),Max(L3,L4));
930 if (len == 4){ // tetraidrs
931 double L1 = getDistance(P( 1 ),P( 2 ));
932 double L2 = getDistance(P( 2 ),P( 3 ));
933 double L3 = getDistance(P( 3 ),P( 1 ));
934 double L4 = getDistance(P( 1 ),P( 4 ));
935 double L5 = getDistance(P( 2 ),P( 4 ));
936 double L6 = getDistance(P( 3 ),P( 4 ));
937 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
940 else if (len == 5){ // piramids
941 double L1 = getDistance(P( 1 ),P( 2 ));
942 double L2 = getDistance(P( 2 ),P( 3 ));
943 double L3 = getDistance(P( 3 ),P( 1 ));
944 double L4 = getDistance(P( 4 ),P( 1 ));
945 double L5 = getDistance(P( 1 ),P( 5 ));
946 double L6 = getDistance(P( 2 ),P( 5 ));
947 double L7 = getDistance(P( 3 ),P( 5 ));
948 double L8 = getDistance(P( 4 ),P( 5 ));
950 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
951 aVal = Max(aVal,Max(L7,L8));
954 else if (len == 6){ // pentaidres
955 double L1 = getDistance(P( 1 ),P( 2 ));
956 double L2 = getDistance(P( 2 ),P( 3 ));
957 double L3 = getDistance(P( 3 ),P( 1 ));
958 double L4 = getDistance(P( 4 ),P( 5 ));
959 double L5 = getDistance(P( 5 ),P( 6 ));
960 double L6 = getDistance(P( 6 ),P( 4 ));
961 double L7 = getDistance(P( 1 ),P( 4 ));
962 double L8 = getDistance(P( 2 ),P( 5 ));
963 double L9 = getDistance(P( 3 ),P( 6 ));
965 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
966 aVal = Max(aVal,Max(Max(L7,L8),L9));
969 else if (len == 8){ // hexaider
970 double L1 = getDistance(P( 1 ),P( 2 ));
971 double L2 = getDistance(P( 2 ),P( 3 ));
972 double L3 = getDistance(P( 3 ),P( 4 ));
973 double L4 = getDistance(P( 4 ),P( 1 ));
974 double L5 = getDistance(P( 5 ),P( 6 ));
975 double L6 = getDistance(P( 6 ),P( 7 ));
976 double L7 = getDistance(P( 7 ),P( 8 ));
977 double L8 = getDistance(P( 8 ),P( 5 ));
978 double L9 = getDistance(P( 1 ),P( 5 ));
979 double L10= getDistance(P( 2 ),P( 6 ));
980 double L11= getDistance(P( 3 ),P( 7 ));
981 double L12= getDistance(P( 4 ),P( 8 ));
983 aVal = Max(Max(Max(L1,L2),Max(L3,L4)),Max(L5,L6));
984 aVal = Max(aVal,Max(Max(L7,L8),Max(L9,L10)));
985 aVal = Max(aVal,Max(L11,L12));
997 if ( myPrecision >= 0 )
999 double prec = pow( 10., (double)( myPrecision ) );
1000 aVal = floor( aVal * prec + 0.5 ) / prec;
1009 double Length2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1011 // meaningless as it is not quality control functor
1015 SMDSAbs_ElementType Length2D::GetType() const
1017 return SMDSAbs_Face;
1020 Length2D::Value::Value(double theLength,long thePntId1, long thePntId2):
1023 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1024 if(thePntId1 > thePntId2){
1025 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1029 bool Length2D::Value::operator<(const Length2D::Value& x) const{
1030 if(myPntId[0] < x.myPntId[0]) return true;
1031 if(myPntId[0] == x.myPntId[0])
1032 if(myPntId[1] < x.myPntId[1]) return true;
1036 void Length2D::GetValues(TValues& theValues){
1038 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1039 for(; anIter->more(); ){
1040 const SMDS_MeshFace* anElem = anIter->next();
1041 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1046 const SMDS_MeshElement* aNode;
1047 if(aNodesIter->more()){
1048 aNode = aNodesIter->next();
1049 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1050 P[0] = P[1] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1051 aNodeId[0] = aNodeId[1] = aNode->GetID();
1054 for(; aNodesIter->more(); ){
1055 aNode = aNodesIter->next();
1056 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode;
1057 long anId = aNode->GetID();
1059 P[2] = gp_Pnt(aNodes->X(),aNodes->Y(),aNodes->Z());
1061 aLength = P[1].Distance(P[2]);
1063 Value aValue(aLength,aNodeId[1],anId);
1066 theValues.insert(aValue);
1069 aLength = P[0].Distance(P[1]);
1071 Value aValue(aLength,aNodeId[0],aNodeId[1]);
1072 theValues.insert(aValue);
1077 Class : MultiConnection
1078 Description : Functor for calculating number of faces conneted to the edge
1080 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
1084 double MultiConnection::GetValue( long theId )
1086 return getNbMultiConnection( myMesh, theId );
1089 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
1091 // meaningless as it is not quality control functor
1095 SMDSAbs_ElementType MultiConnection::GetType() const
1097 return SMDSAbs_Edge;
1101 Class : MultiConnection2D
1102 Description : Functor for calculating number of faces conneted to the edge
1104 double MultiConnection2D::GetValue( const TSequenceOfXYZ& P )
1109 double MultiConnection2D::GetValue( long theElementId )
1114 if (GetPoints(theElementId,P)){
1115 const SMDS_MeshElement* anFaceElem = myMesh->FindElement( theElementId );
1116 SMDSAbs_ElementType aType = anFaceElem->GetType();
1120 TColStd_MapOfInteger aMap;
1128 if (len == 3){ // triangles
1129 int Nb[3] = {0,0,0};
1132 SMDS_ElemIteratorPtr anIter = anFaceElem->nodesIterator();
1133 if ( anIter != 0 ) {
1134 while( anIter->more() ) {
1135 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1139 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
1140 while( anElemIter->more() ) {
1141 const SMDS_MeshElement* anElem = anElemIter->next();
1142 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
1143 int anId = anElem->GetID();
1145 if ( anIter->more() ) // i.e. first node
1147 else if ( aMap.Contains( anId ) ){
1151 else if ( anElem != 0 && anElem->GetType() == SMDSAbs_Edge ) i++;
1156 aResult = Max(Max(Nb[0],Nb[1]),Nb[2]);
1159 case SMDSAbs_Volume:
1164 return aResult;//getNbMultiConnection( myMesh, theId );
1167 double MultiConnection2D::GetBadRate( double Value, int /*nbNodes*/ ) const
1169 // meaningless as it is not quality control functor
1173 SMDSAbs_ElementType MultiConnection2D::GetType() const
1175 return SMDSAbs_Face;
1178 MultiConnection2D::Value::Value(long thePntId1, long thePntId2)
1180 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1181 if(thePntId1 > thePntId2){
1182 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1186 bool MultiConnection2D::Value::operator<(const MultiConnection2D::Value& x) const{
1187 if(myPntId[0] < x.myPntId[0]) return true;
1188 if(myPntId[0] == x.myPntId[0])
1189 if(myPntId[1] < x.myPntId[1]) return true;
1193 void MultiConnection2D::GetValues(MValues& theValues){
1194 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1195 for(; anIter->more(); ){
1196 const SMDS_MeshFace* anElem = anIter->next();
1197 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1200 //int aNbConnects=0;
1201 const SMDS_MeshNode* aNode0;
1202 const SMDS_MeshNode* aNode1;
1203 const SMDS_MeshNode* aNode2;
1204 if(aNodesIter->more()){
1205 aNode0 = (SMDS_MeshNode*) aNodesIter->next();
1207 const SMDS_MeshNode* aNodes = (SMDS_MeshNode*) aNode1;
1208 aNodeId[0] = aNodeId[1] = aNodes->GetID();
1210 for(; aNodesIter->more(); ){
1211 aNode2 = (SMDS_MeshNode*) aNodesIter->next();
1212 long anId = aNode2->GetID();
1215 Value aValue(aNodeId[1],aNodeId[2]);
1216 MValues::iterator aItr = theValues.find(aValue);
1217 if (aItr != theValues.end()){
1221 theValues[aValue] = 1;
1224 //cout << "NodeIds: "<<aNodeId[1]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1225 aNodeId[1] = aNodeId[2];
1228 Value aValue(aNodeId[0],aNodeId[2]);
1229 MValues::iterator aItr = theValues.find(aValue);
1230 if (aItr != theValues.end()){
1234 theValues[aValue] = 1;
1237 //cout << "NodeIds: "<<aNodeId[0]<<","<<aNodeId[2]<<" nbconn="<<aNbConnects<<endl;
1247 Class : BadOrientedVolume
1248 Description : Predicate bad oriented volumes
1251 BadOrientedVolume::BadOrientedVolume()
1256 void BadOrientedVolume::SetMesh( const SMDS_Mesh* theMesh )
1261 bool BadOrientedVolume::IsSatisfy( long theId )
1266 SMDS_VolumeTool vTool( myMesh->FindElement( theId ));
1267 return !vTool.IsForward();
1270 SMDSAbs_ElementType BadOrientedVolume::GetType() const
1272 return SMDSAbs_Volume;
1279 Description : Predicate for free borders
1282 FreeBorders::FreeBorders()
1287 void FreeBorders::SetMesh( const SMDS_Mesh* theMesh )
1292 bool FreeBorders::IsSatisfy( long theId )
1294 return getNbMultiConnection( myMesh, theId ) == 1;
1297 SMDSAbs_ElementType FreeBorders::GetType() const
1299 return SMDSAbs_Edge;
1305 Description : Predicate for free Edges
1307 FreeEdges::FreeEdges()
1312 void FreeEdges::SetMesh( const SMDS_Mesh* theMesh )
1317 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
1319 TColStd_MapOfInteger aMap;
1320 for ( int i = 0; i < 2; i++ )
1322 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
1323 while( anElemIter->more() )
1325 const SMDS_MeshElement* anElem = anElemIter->next();
1326 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
1328 int anId = anElem->GetID();
1332 else if ( aMap.Contains( anId ) && anId != theFaceId )
1340 bool FreeEdges::IsSatisfy( long theId )
1345 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
1346 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
1349 int nbNodes = aFace->NbNodes();
1350 //const SMDS_MeshNode* aNodes[ nbNodes ];
1352 const SMDS_MeshNode* aNodes [nbNodes];
1354 const SMDS_MeshNode** aNodes = (const SMDS_MeshNode **)new SMDS_MeshNode*[nbNodes];
1357 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
1360 while( anIter->more() )
1362 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
1365 aNodes[ i++ ] = aNode;
1369 for ( int i = 0; i < nbNodes - 1; i++ )
1370 if ( IsFreeEdge( &aNodes[ i ], theId ) ) {
1377 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
1378 const Standard_Boolean isFree = IsFreeEdge( &aNodes[ 0 ], theId );
1386 SMDSAbs_ElementType FreeEdges::GetType() const
1388 return SMDSAbs_Face;
1391 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
1394 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
1395 if(thePntId1 > thePntId2){
1396 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
1400 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
1401 if(myPntId[0] < x.myPntId[0]) return true;
1402 if(myPntId[0] == x.myPntId[0])
1403 if(myPntId[1] < x.myPntId[1]) return true;
1407 inline void UpdateBorders(const FreeEdges::Border& theBorder,
1408 FreeEdges::TBorders& theRegistry,
1409 FreeEdges::TBorders& theContainer)
1411 if(theRegistry.find(theBorder) == theRegistry.end()){
1412 theRegistry.insert(theBorder);
1413 theContainer.insert(theBorder);
1415 theContainer.erase(theBorder);
1419 void FreeEdges::GetBoreders(TBorders& theBorders)
1422 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1423 for(; anIter->more(); ){
1424 const SMDS_MeshFace* anElem = anIter->next();
1425 long anElemId = anElem->GetID();
1426 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
1428 const SMDS_MeshElement* aNode;
1429 if(aNodesIter->more()){
1430 aNode = aNodesIter->next();
1431 aNodeId[0] = aNodeId[1] = aNode->GetID();
1433 for(; aNodesIter->more(); ){
1434 aNode = aNodesIter->next();
1435 long anId = aNode->GetID();
1436 Border aBorder(anElemId,aNodeId[1],anId);
1438 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1439 UpdateBorders(aBorder,aRegistry,theBorders);
1441 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
1442 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
1443 UpdateBorders(aBorder,aRegistry,theBorders);
1445 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
1450 Description : Predicate for Range of Ids.
1451 Range may be specified with two ways.
1452 1. Using AddToRange method
1453 2. With SetRangeStr method. Parameter of this method is a string
1454 like as "1,2,3,50-60,63,67,70-"
1457 //=======================================================================
1458 // name : RangeOfIds
1459 // Purpose : Constructor
1460 //=======================================================================
1461 RangeOfIds::RangeOfIds()
1464 myType = SMDSAbs_All;
1467 //=======================================================================
1469 // Purpose : Set mesh
1470 //=======================================================================
1471 void RangeOfIds::SetMesh( const SMDS_Mesh* theMesh )
1476 //=======================================================================
1477 // name : AddToRange
1478 // Purpose : Add ID to the range
1479 //=======================================================================
1480 bool RangeOfIds::AddToRange( long theEntityId )
1482 myIds.Add( theEntityId );
1486 //=======================================================================
1487 // name : GetRangeStr
1488 // Purpose : Get range as a string.
1489 // Example: "1,2,3,50-60,63,67,70-"
1490 //=======================================================================
1491 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1495 TColStd_SequenceOfInteger anIntSeq;
1496 TColStd_SequenceOfAsciiString aStrSeq;
1498 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1499 for ( ; anIter.More(); anIter.Next() )
1501 int anId = anIter.Key();
1502 TCollection_AsciiString aStr( anId );
1503 anIntSeq.Append( anId );
1504 aStrSeq.Append( aStr );
1507 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1509 int aMinId = myMin( i );
1510 int aMaxId = myMax( i );
1512 TCollection_AsciiString aStr;
1513 if ( aMinId != IntegerFirst() )
1518 if ( aMaxId != IntegerLast() )
1521 // find position of the string in result sequence and insert string in it
1522 if ( anIntSeq.Length() == 0 )
1524 anIntSeq.Append( aMinId );
1525 aStrSeq.Append( aStr );
1529 if ( aMinId < anIntSeq.First() )
1531 anIntSeq.Prepend( aMinId );
1532 aStrSeq.Prepend( aStr );
1534 else if ( aMinId > anIntSeq.Last() )
1536 anIntSeq.Append( aMinId );
1537 aStrSeq.Append( aStr );
1540 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1541 if ( aMinId < anIntSeq( j ) )
1543 anIntSeq.InsertBefore( j, aMinId );
1544 aStrSeq.InsertBefore( j, aStr );
1550 if ( aStrSeq.Length() == 0 )
1553 theResStr = aStrSeq( 1 );
1554 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1557 theResStr += aStrSeq( j );
1561 //=======================================================================
1562 // name : SetRangeStr
1563 // Purpose : Define range with string
1564 // Example of entry string: "1,2,3,50-60,63,67,70-"
1565 //=======================================================================
1566 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1572 TCollection_AsciiString aStr = theStr;
1573 aStr.RemoveAll( ' ' );
1574 aStr.RemoveAll( '\t' );
1576 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1577 aStr.Remove( aPos, 2 );
1579 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1581 while ( tmpStr != "" )
1583 tmpStr = aStr.Token( ",", i++ );
1584 int aPos = tmpStr.Search( '-' );
1588 if ( tmpStr.IsIntegerValue() )
1589 myIds.Add( tmpStr.IntegerValue() );
1595 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1596 TCollection_AsciiString aMinStr = tmpStr;
1598 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1599 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1601 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1602 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1605 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1606 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1613 //=======================================================================
1615 // Purpose : Get type of supported entities
1616 //=======================================================================
1617 SMDSAbs_ElementType RangeOfIds::GetType() const
1622 //=======================================================================
1624 // Purpose : Set type of supported entities
1625 //=======================================================================
1626 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1631 //=======================================================================
1633 // Purpose : Verify whether entity satisfies to this rpedicate
1634 //=======================================================================
1635 bool RangeOfIds::IsSatisfy( long theId )
1640 if ( myType == SMDSAbs_Node )
1642 if ( myMesh->FindNode( theId ) == 0 )
1647 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1648 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1652 if ( myIds.Contains( theId ) )
1655 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1656 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1664 Description : Base class for comparators
1666 Comparator::Comparator():
1670 Comparator::~Comparator()
1673 void Comparator::SetMesh( const SMDS_Mesh* theMesh )
1676 myFunctor->SetMesh( theMesh );
1679 void Comparator::SetMargin( double theValue )
1681 myMargin = theValue;
1684 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1686 myFunctor = theFunct;
1689 SMDSAbs_ElementType Comparator::GetType() const
1691 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1694 double Comparator::GetMargin()
1702 Description : Comparator "<"
1704 bool LessThan::IsSatisfy( long theId )
1706 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1712 Description : Comparator ">"
1714 bool MoreThan::IsSatisfy( long theId )
1716 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1722 Description : Comparator "="
1725 myToler(Precision::Confusion())
1728 bool EqualTo::IsSatisfy( long theId )
1730 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1733 void EqualTo::SetTolerance( double theToler )
1738 double EqualTo::GetTolerance()
1745 Description : Logical NOT predicate
1747 LogicalNOT::LogicalNOT()
1750 LogicalNOT::~LogicalNOT()
1753 bool LogicalNOT::IsSatisfy( long theId )
1755 return myPredicate && !myPredicate->IsSatisfy( theId );
1758 void LogicalNOT::SetMesh( const SMDS_Mesh* theMesh )
1761 myPredicate->SetMesh( theMesh );
1764 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1766 myPredicate = thePred;
1769 SMDSAbs_ElementType LogicalNOT::GetType() const
1771 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1776 Class : LogicalBinary
1777 Description : Base class for binary logical predicate
1779 LogicalBinary::LogicalBinary()
1782 LogicalBinary::~LogicalBinary()
1785 void LogicalBinary::SetMesh( const SMDS_Mesh* theMesh )
1788 myPredicate1->SetMesh( theMesh );
1791 myPredicate2->SetMesh( theMesh );
1794 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1796 myPredicate1 = thePredicate;
1799 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1801 myPredicate2 = thePredicate;
1804 SMDSAbs_ElementType LogicalBinary::GetType() const
1806 if ( !myPredicate1 || !myPredicate2 )
1809 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1810 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1812 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1818 Description : Logical AND
1820 bool LogicalAND::IsSatisfy( long theId )
1825 myPredicate1->IsSatisfy( theId ) &&
1826 myPredicate2->IsSatisfy( theId );
1832 Description : Logical OR
1834 bool LogicalOR::IsSatisfy( long theId )
1839 myPredicate1->IsSatisfy( theId ) ||
1840 myPredicate2->IsSatisfy( theId );
1854 void Filter::SetPredicate( PredicatePtr thePredicate )
1856 myPredicate = thePredicate;
1859 template<class TElement, class TIterator, class TPredicate>
1860 inline void FillSequence(const TIterator& theIterator,
1861 TPredicate& thePredicate,
1862 Filter::TIdSequence& theSequence)
1864 if ( theIterator ) {
1865 while( theIterator->more() ) {
1866 TElement anElem = theIterator->next();
1867 long anId = anElem->GetID();
1868 if ( thePredicate->IsSatisfy( anId ) )
1869 theSequence.push_back( anId );
1876 GetElementsId( const SMDS_Mesh* theMesh,
1877 PredicatePtr thePredicate,
1878 TIdSequence& theSequence )
1880 theSequence.clear();
1882 if ( !theMesh || !thePredicate )
1885 thePredicate->SetMesh( theMesh );
1887 SMDSAbs_ElementType aType = thePredicate->GetType();
1890 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),thePredicate,theSequence);
1893 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1896 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1898 case SMDSAbs_Volume:
1899 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1902 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),thePredicate,theSequence);
1903 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),thePredicate,theSequence);
1904 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),thePredicate,theSequence);
1910 Filter::GetElementsId( const SMDS_Mesh* theMesh,
1911 Filter::TIdSequence& theSequence )
1913 GetElementsId(theMesh,myPredicate,theSequence);
1920 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1926 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1927 SMDS_MeshNode* theNode2 )
1933 ManifoldPart::Link::~Link()
1939 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1941 if ( myNode1 == theLink.myNode1 &&
1942 myNode2 == theLink.myNode2 )
1944 else if ( myNode1 == theLink.myNode2 &&
1945 myNode2 == theLink.myNode1 )
1951 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1953 if(myNode1 < x.myNode1) return true;
1954 if(myNode1 == x.myNode1)
1955 if(myNode2 < x.myNode2) return true;
1959 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1960 const ManifoldPart::Link& theLink2 )
1962 return theLink1.IsEqual( theLink2 );
1965 ManifoldPart::ManifoldPart()
1968 myAngToler = Precision::Angular();
1969 myIsOnlyManifold = true;
1972 ManifoldPart::~ManifoldPart()
1977 void ManifoldPart::SetMesh( const SMDS_Mesh* theMesh )
1983 SMDSAbs_ElementType ManifoldPart::GetType() const
1984 { return SMDSAbs_Face; }
1986 bool ManifoldPart::IsSatisfy( long theElementId )
1988 return myMapIds.Contains( theElementId );
1991 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1992 { myAngToler = theAngToler; }
1994 double ManifoldPart::GetAngleTolerance() const
1995 { return myAngToler; }
1997 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1998 { myIsOnlyManifold = theIsOnly; }
2000 void ManifoldPart::SetStartElem( const long theStartId )
2001 { myStartElemId = theStartId; }
2003 bool ManifoldPart::process()
2006 myMapBadGeomIds.Clear();
2008 myAllFacePtr.clear();
2009 myAllFacePtrIntDMap.clear();
2013 // collect all faces into own map
2014 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
2015 for (; anFaceItr->more(); )
2017 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
2018 myAllFacePtr.push_back( aFacePtr );
2019 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
2022 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
2026 // the map of non manifold links and bad geometry
2027 TMapOfLink aMapOfNonManifold;
2028 TColStd_MapOfInteger aMapOfTreated;
2030 // begin cycle on faces from start index and run on vector till the end
2031 // and from begin to start index to cover whole vector
2032 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
2033 bool isStartTreat = false;
2034 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
2036 if ( fi == aStartIndx )
2037 isStartTreat = true;
2038 // as result next time when fi will be equal to aStartIndx
2040 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
2041 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
2044 aMapOfTreated.Add( aFacePtr->GetID() );
2045 TColStd_MapOfInteger aResFaces;
2046 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
2047 aMapOfNonManifold, aResFaces ) )
2049 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
2050 for ( ; anItr.More(); anItr.Next() )
2052 int aFaceId = anItr.Key();
2053 aMapOfTreated.Add( aFaceId );
2054 myMapIds.Add( aFaceId );
2057 if ( fi == ( myAllFacePtr.size() - 1 ) )
2059 } // end run on vector of faces
2060 return !myMapIds.IsEmpty();
2063 static void getLinks( const SMDS_MeshFace* theFace,
2064 ManifoldPart::TVectorOfLink& theLinks )
2066 int aNbNode = theFace->NbNodes();
2067 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2069 SMDS_MeshNode* aNode = 0;
2070 for ( ; aNodeItr->more() && i <= aNbNode; )
2073 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
2077 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
2079 ManifoldPart::Link aLink( aN1, aN2 );
2080 theLinks.push_back( aLink );
2084 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
2087 int aNbNode = theFace->NbNodes();
2088 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
2089 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
2091 for ( ; aNodeItr->more() && i <= 4; i++ )
2093 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2094 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
2097 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
2098 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
2102 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
2105 double len = n.Modulus();
2112 bool ManifoldPart::findConnected
2113 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
2114 SMDS_MeshFace* theStartFace,
2115 ManifoldPart::TMapOfLink& theNonManifold,
2116 TColStd_MapOfInteger& theResFaces )
2118 theResFaces.Clear();
2119 if ( !theAllFacePtrInt.size() )
2122 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
2124 myMapBadGeomIds.Add( theStartFace->GetID() );
2128 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
2129 ManifoldPart::TVectorOfLink aSeqOfBoundary;
2130 theResFaces.Add( theStartFace->GetID() );
2131 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
2133 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2134 aDMapLinkFace, theNonManifold, theStartFace );
2136 bool isDone = false;
2137 while ( !isDone && aMapOfBoundary.size() != 0 )
2139 bool isToReset = false;
2140 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
2141 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
2143 ManifoldPart::Link aLink = *pLink;
2144 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
2146 // each link could be treated only once
2147 aMapToSkip.insert( aLink );
2149 ManifoldPart::TVectorOfFacePtr aFaces;
2151 if ( myIsOnlyManifold &&
2152 (theNonManifold.find( aLink ) != theNonManifold.end()) )
2156 getFacesByLink( aLink, aFaces );
2157 // filter the element to keep only indicated elements
2158 ManifoldPart::TVectorOfFacePtr aFiltered;
2159 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2160 for ( ; pFace != aFaces.end(); ++pFace )
2162 SMDS_MeshFace* aFace = *pFace;
2163 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
2164 aFiltered.push_back( aFace );
2167 if ( aFaces.size() < 2 ) // no neihgbour faces
2169 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
2171 theNonManifold.insert( aLink );
2176 // compare normal with normals of neighbor element
2177 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
2178 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
2179 for ( ; pFace != aFaces.end(); ++pFace )
2181 SMDS_MeshFace* aNextFace = *pFace;
2182 if ( aPrevFace == aNextFace )
2184 int anNextFaceID = aNextFace->GetID();
2185 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
2186 // should not be with non manifold restriction. probably bad topology
2188 // check if face was treated and skipped
2189 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
2190 !isInPlane( aPrevFace, aNextFace ) )
2192 // add new element to connected and extend the boundaries.
2193 theResFaces.Add( anNextFaceID );
2194 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
2195 aDMapLinkFace, theNonManifold, aNextFace );
2199 isDone = !isToReset;
2202 return !theResFaces.IsEmpty();
2205 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
2206 const SMDS_MeshFace* theFace2 )
2208 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
2209 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
2210 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
2212 myMapBadGeomIds.Add( theFace2->GetID() );
2215 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
2221 void ManifoldPart::expandBoundary
2222 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
2223 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
2224 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
2225 ManifoldPart::TMapOfLink& theNonManifold,
2226 SMDS_MeshFace* theNextFace ) const
2228 ManifoldPart::TVectorOfLink aLinks;
2229 getLinks( theNextFace, aLinks );
2230 int aNbLink = (int)aLinks.size();
2231 for ( int i = 0; i < aNbLink; i++ )
2233 ManifoldPart::Link aLink = aLinks[ i ];
2234 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
2236 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
2238 if ( myIsOnlyManifold )
2240 // remove from boundary
2241 theMapOfBoundary.erase( aLink );
2242 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
2243 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
2245 ManifoldPart::Link aBoundLink = *pLink;
2246 if ( aBoundLink.IsEqual( aLink ) )
2248 theSeqOfBoundary.erase( pLink );
2256 theMapOfBoundary.insert( aLink );
2257 theSeqOfBoundary.push_back( aLink );
2258 theDMapLinkFacePtr[ aLink ] = theNextFace;
2263 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
2264 ManifoldPart::TVectorOfFacePtr& theFaces ) const
2266 SMDS_Mesh::SetOfFaces aSetOfFaces;
2267 // take all faces that shared first node
2268 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
2269 for ( ; anItr->more(); )
2271 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2274 aSetOfFaces.Add( aFace );
2276 // take all faces that shared second node
2277 anItr = theLink.myNode2->facesIterator();
2278 // find the common part of two sets
2279 for ( ; anItr->more(); )
2281 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
2282 if ( aSetOfFaces.Contains( aFace ) )
2283 theFaces.push_back( aFace );
2292 ElementsOnSurface::ElementsOnSurface()
2296 myType = SMDSAbs_All;
2298 myToler = Precision::Confusion();
2301 ElementsOnSurface::~ElementsOnSurface()
2306 void ElementsOnSurface::SetMesh( const SMDS_Mesh* theMesh )
2308 if ( myMesh == theMesh )
2315 bool ElementsOnSurface::IsSatisfy( long theElementId )
2317 return myIds.Contains( theElementId );
2320 SMDSAbs_ElementType ElementsOnSurface::GetType() const
2323 void ElementsOnSurface::SetTolerance( const double theToler )
2324 { myToler = theToler; }
2326 double ElementsOnSurface::GetTolerance() const
2331 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
2332 const SMDSAbs_ElementType theType )
2336 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
2341 TopoDS_Face aFace = TopoDS::Face( theShape );
2342 mySurf = BRep_Tool::Surface( aFace );
2345 void ElementsOnSurface::process()
2348 if ( mySurf.IsNull() )
2354 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
2356 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
2357 for(; anIter->more(); )
2358 process( anIter->next() );
2361 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
2363 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
2364 for(; anIter->more(); )
2365 process( anIter->next() );
2368 if ( myType == SMDSAbs_Node )
2370 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
2371 for(; anIter->more(); )
2372 process( anIter->next() );
2376 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
2378 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
2379 bool isSatisfy = true;
2380 for ( ; aNodeItr->more(); )
2382 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
2383 if ( !isOnSurface( aNode ) )
2390 myIds.Add( theElemPtr->GetID() );
2393 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
2395 if ( mySurf.IsNull() )
2398 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
2399 double aToler2 = myToler * myToler;
2400 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
2402 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
2403 if ( aPln.SquareDistance( aPnt ) > aToler2 )
2406 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
2408 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
2409 double aRad = aCyl.Radius();
2410 gp_Ax3 anAxis = aCyl.Position();
2411 gp_XYZ aLoc = aCyl.Location().XYZ();
2412 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2413 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
2414 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )