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"
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() );
76 inline double getDistance( const gp_XYZ& P1, const gp_XYZ& P2 )
78 double aDist = gp_Pnt( P1 ).Distance( gp_Pnt( P2 ) );
82 int getNbMultiConnection( SMDS_Mesh* theMesh, const int theId )
87 const SMDS_MeshElement* anEdge = theMesh->FindElement( theId );
88 if ( anEdge == 0 || anEdge->GetType() != SMDSAbs_Edge || anEdge->NbNodes() != 2 )
91 TColStd_MapOfInteger aMap;
94 SMDS_ElemIteratorPtr anIter = anEdge->nodesIterator();
96 while( anIter->more() ) {
97 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
100 SMDS_ElemIteratorPtr anElemIter = aNode->GetInverseElementIterator();
101 while( anElemIter->more() ) {
102 const SMDS_MeshElement* anElem = anElemIter->next();
103 if ( anElem != 0 && anElem->GetType() != SMDSAbs_Edge ) {
104 int anId = anElem->GetID();
106 if ( anIter->more() ) // i.e. first node
108 else if ( aMap.Contains( anId ) )
120 using namespace SMESH::Controls;
127 Class : NumericalFunctor
128 Description : Base class for numerical functors
130 NumericalFunctor::NumericalFunctor():
136 void NumericalFunctor::SetMesh( SMDS_Mesh* theMesh )
141 bool NumericalFunctor::GetPoints(const int theId,
142 TSequenceOfXYZ& theRes) const
149 return GetPoints( myMesh->FindElement( theId ), theRes );
152 bool NumericalFunctor::GetPoints(const SMDS_MeshElement* anElem,
153 TSequenceOfXYZ& theRes)
160 // Get nodes of the element
161 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
164 while( anIter->more() )
166 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
168 theRes.push_back( gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
176 long NumericalFunctor::GetPrecision() const
181 void NumericalFunctor::SetPrecision( const long thePrecision )
183 myPrecision = thePrecision;
186 double NumericalFunctor::GetValue( long theId )
189 if ( GetPoints( theId, P ))
191 double aVal = GetValue( P );
192 if ( myPrecision >= 0 )
194 double prec = pow( 10., (double)( myPrecision ) );
195 aVal = floor( aVal * prec + 0.5 ) / prec;
205 Description : Functor for calculation of minimum angle
208 double MinimumAngle::GetValue( const TSequenceOfXYZ& P )
214 double A0 = getAngle( P( 3 ), P( 1 ), P( 2 ) );
215 double A1 = getAngle( P( 1 ), P( 2 ), P( 3 ) );
216 double A2 = getAngle( P( 2 ), P( 3 ), P( 1 ) );
218 aMin = Min( A0, Min( A1, A2 ) );
220 else if ( P.size() == 4 )
222 double A0 = getAngle( P( 4 ), P( 1 ), P( 2 ) );
223 double A1 = getAngle( P( 1 ), P( 2 ), P( 3 ) );
224 double A2 = getAngle( P( 2 ), P( 3 ), P( 4 ) );
225 double A3 = getAngle( P( 3 ), P( 4 ), P( 1 ) );
227 aMin = Min( Min( A0, A1 ), Min( A2, A3 ) );
232 return aMin * 180 / PI;
235 double MinimumAngle::GetBadRate( double Value, int nbNodes ) const
237 const double aBestAngle = PI / nbNodes;
238 return ( fabs( aBestAngle - Value ));
241 SMDSAbs_ElementType MinimumAngle::GetType() const
249 Description : Functor for calculating aspect ratio
251 double AspectRatio::GetValue( const TSequenceOfXYZ& P )
253 int nbNodes = P.size();
255 if ( nbNodes != 3 && nbNodes != 4 )
258 // Compute lengths of the sides
260 double aLen[ nbNodes ];
261 for ( int i = 0; i < nbNodes - 1; i++ )
262 aLen[ i ] = getDistance( P(i+1), P(i+2) );
263 aLen[ nbNodes - 1 ] = getDistance( P( 1 ), P(nbNodes) );
265 // Compute aspect ratio
269 double anArea = getArea( P( 1 ), P( 2 ), P( 3 ) );
270 if ( anArea <= Precision::Confusion() )
272 double aMaxLen = Max( aLen[ 0 ], Max( aLen[ 1 ], aLen[ 2 ] ) );
273 static double aCoef = sqrt( 3. ) / 4;
275 return aCoef * aMaxLen * aMaxLen / anArea;
279 double aMinLen = Min( Min( aLen[ 0 ], aLen[ 1 ] ), Min( aLen[ 2 ], aLen[ 3 ] ) );
280 if ( aMinLen <= Precision::Confusion() )
282 double aMaxLen = Max( Max( aLen[ 0 ], aLen[ 1 ] ), Max( aLen[ 2 ], aLen[ 3 ] ) );
284 return aMaxLen / aMinLen;
288 double AspectRatio::GetBadRate( double Value, int /*nbNodes*/ ) const
290 // the aspect ratio is in the range [1.0,infinity]
293 return Value / 1000.;
296 SMDSAbs_ElementType AspectRatio::GetType() const
303 Class : AspectRatio3D
304 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 abs(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 )
772 return getArea( P( 1 ), P( 2 ), P( 3 ) );
773 else if ( P.size() == 4 )
774 return getArea( P( 1 ), P( 2 ), P( 3 ) ) + getArea( P( 1 ), P( 3 ), P( 4 ) );
779 double Area::GetBadRate( double Value, int /*nbNodes*/ ) const
784 SMDSAbs_ElementType Area::GetType() const
792 Description : Functor for calculating length off edge
794 double Length::GetValue( const TSequenceOfXYZ& P )
796 return ( P.size() == 2 ? getDistance( P( 1 ), P( 2 ) ) : 0 );
799 double Length::GetBadRate( double Value, int /*nbNodes*/ ) const
804 SMDSAbs_ElementType Length::GetType() const
811 Class : MultiConnection
812 Description : Functor for calculating number of faces conneted to the edge
814 double MultiConnection::GetValue( const TSequenceOfXYZ& P )
818 double MultiConnection::GetValue( long theId )
820 return getNbMultiConnection( myMesh, theId );
823 double MultiConnection::GetBadRate( double Value, int /*nbNodes*/ ) const
828 SMDSAbs_ElementType MultiConnection::GetType() const
840 Description : Predicate for free borders
843 FreeBorders::FreeBorders()
848 void FreeBorders::SetMesh( SMDS_Mesh* theMesh )
853 bool FreeBorders::IsSatisfy( long theId )
855 return getNbMultiConnection( myMesh, theId ) == 1;
858 SMDSAbs_ElementType FreeBorders::GetType() const
866 Description : Predicate for free Edges
868 FreeEdges::FreeEdges()
873 void FreeEdges::SetMesh( SMDS_Mesh* theMesh )
878 bool FreeEdges::IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId )
880 TColStd_MapOfInteger aMap;
881 for ( int i = 0; i < 2; i++ )
883 SMDS_ElemIteratorPtr anElemIter = theNodes[ i ]->GetInverseElementIterator();
884 while( anElemIter->more() )
886 const SMDS_MeshElement* anElem = anElemIter->next();
887 if ( anElem != 0 && anElem->GetType() == SMDSAbs_Face )
889 int anId = anElem->GetID();
893 else if ( aMap.Contains( anId ) && anId != theFaceId )
901 bool FreeEdges::IsSatisfy( long theId )
906 const SMDS_MeshElement* aFace = myMesh->FindElement( theId );
907 if ( aFace == 0 || aFace->GetType() != SMDSAbs_Face || aFace->NbNodes() < 3 )
910 int nbNodes = aFace->NbNodes();
911 const SMDS_MeshNode* aNodes[ nbNodes ];
913 SMDS_ElemIteratorPtr anIter = aFace->nodesIterator();
916 while( anIter->more() )
918 const SMDS_MeshNode* aNode = (SMDS_MeshNode*)anIter->next();
921 aNodes[ i++ ] = aNode;
925 for ( int i = 0; i < nbNodes - 1; i++ )
926 if ( IsFreeEdge( &aNodes[ i ], theId ) )
929 aNodes[ 1 ] = aNodes[ nbNodes - 1 ];
931 return IsFreeEdge( &aNodes[ 0 ], theId );
935 SMDSAbs_ElementType FreeEdges::GetType() const
940 FreeEdges::Border::Border(long theElemId, long thePntId1, long thePntId2):
943 myPntId[0] = thePntId1; myPntId[1] = thePntId2;
944 if(thePntId1 > thePntId2){
945 myPntId[1] = thePntId1; myPntId[0] = thePntId2;
949 bool FreeEdges::Border::operator<(const FreeEdges::Border& x) const{
950 if(myPntId[0] < x.myPntId[0]) return true;
951 if(myPntId[0] == x.myPntId[0])
952 if(myPntId[1] < x.myPntId[1]) return true;
956 inline void UpdateBorders(const FreeEdges::Border& theBorder,
957 FreeEdges::TBorders& theRegistry,
958 FreeEdges::TBorders& theContainer)
960 if(theRegistry.find(theBorder) == theRegistry.end()){
961 theRegistry.insert(theBorder);
962 theContainer.insert(theBorder);
964 theContainer.erase(theBorder);
968 void FreeEdges::GetBoreders(TBorders& theBorders)
971 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
972 for(; anIter->more(); ){
973 const SMDS_MeshFace* anElem = anIter->next();
974 long anElemId = anElem->GetID();
975 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
977 const SMDS_MeshElement* aNode;
978 if(aNodesIter->more()){
979 aNode = aNodesIter->next();
980 aNodeId[0] = aNodeId[1] = aNode->GetID();
982 for(; aNodesIter->more(); ){
983 aNode = aNodesIter->next();
984 long anId = aNode->GetID();
985 Border aBorder(anElemId,aNodeId[1],anId);
987 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
988 UpdateBorders(aBorder,aRegistry,theBorders);
990 Border aBorder(anElemId,aNodeId[0],aNodeId[1]);
991 //std::cout<<aBorder.myPntId[0]<<"; "<<aBorder.myPntId[1]<<"; "<<aBorder.myElemId<<endl;
992 UpdateBorders(aBorder,aRegistry,theBorders);
994 //std::cout<<"theBorders.size() = "<<theBorders.size()<<endl;
999 Description : Predicate for Range of Ids.
1000 Range may be specified with two ways.
1001 1. Using AddToRange method
1002 2. With SetRangeStr method. Parameter of this method is a string
1003 like as "1,2,3,50-60,63,67,70-"
1006 //=======================================================================
1007 // name : RangeOfIds
1008 // Purpose : Constructor
1009 //=======================================================================
1010 RangeOfIds::RangeOfIds()
1013 myType = SMDSAbs_All;
1016 //=======================================================================
1018 // Purpose : Set mesh
1019 //=======================================================================
1020 void RangeOfIds::SetMesh( SMDS_Mesh* theMesh )
1025 //=======================================================================
1026 // name : AddToRange
1027 // Purpose : Add ID to the range
1028 //=======================================================================
1029 bool RangeOfIds::AddToRange( long theEntityId )
1031 myIds.Add( theEntityId );
1035 //=======================================================================
1036 // name : GetRangeStr
1037 // Purpose : Get range as a string.
1038 // Example: "1,2,3,50-60,63,67,70-"
1039 //=======================================================================
1040 void RangeOfIds::GetRangeStr( TCollection_AsciiString& theResStr )
1044 TColStd_SequenceOfInteger anIntSeq;
1045 TColStd_SequenceOfAsciiString aStrSeq;
1047 TColStd_MapIteratorOfMapOfInteger anIter( myIds );
1048 for ( ; anIter.More(); anIter.Next() )
1050 int anId = anIter.Key();
1051 TCollection_AsciiString aStr( anId );
1052 anIntSeq.Append( anId );
1053 aStrSeq.Append( aStr );
1056 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1058 int aMinId = myMin( i );
1059 int aMaxId = myMax( i );
1061 TCollection_AsciiString aStr;
1062 if ( aMinId != IntegerFirst() )
1067 if ( aMaxId != IntegerLast() )
1070 // find position of the string in result sequence and insert string in it
1071 if ( anIntSeq.Length() == 0 )
1073 anIntSeq.Append( aMinId );
1074 aStrSeq.Append( aStr );
1078 if ( aMinId < anIntSeq.First() )
1080 anIntSeq.Prepend( aMinId );
1081 aStrSeq.Prepend( aStr );
1083 else if ( aMinId > anIntSeq.Last() )
1085 anIntSeq.Append( aMinId );
1086 aStrSeq.Append( aStr );
1089 for ( int j = 1, k = anIntSeq.Length(); j <= k; j++ )
1090 if ( aMinId < anIntSeq( j ) )
1092 anIntSeq.InsertBefore( j, aMinId );
1093 aStrSeq.InsertBefore( j, aStr );
1099 if ( aStrSeq.Length() == 0 )
1102 theResStr = aStrSeq( 1 );
1103 for ( int j = 2, k = aStrSeq.Length(); j <= k; j++ )
1106 theResStr += aStrSeq( j );
1110 //=======================================================================
1111 // name : SetRangeStr
1112 // Purpose : Define range with string
1113 // Example of entry string: "1,2,3,50-60,63,67,70-"
1114 //=======================================================================
1115 bool RangeOfIds::SetRangeStr( const TCollection_AsciiString& theStr )
1121 TCollection_AsciiString aStr = theStr;
1122 aStr.RemoveAll( ' ' );
1123 aStr.RemoveAll( '\t' );
1125 for ( int aPos = aStr.Search( ",," ); aPos != -1; aPos = aStr.Search( ",," ) )
1126 aStr.Remove( aPos, 2 );
1128 TCollection_AsciiString tmpStr = aStr.Token( ",", 1 );
1130 while ( tmpStr != "" )
1132 tmpStr = aStr.Token( ",", i++ );
1133 int aPos = tmpStr.Search( '-' );
1137 if ( tmpStr.IsIntegerValue() )
1138 myIds.Add( tmpStr.IntegerValue() );
1144 TCollection_AsciiString aMaxStr = tmpStr.Split( aPos );
1145 TCollection_AsciiString aMinStr = tmpStr;
1147 while ( aMinStr.Search( "-" ) != -1 ) aMinStr.RemoveAll( '-' );
1148 while ( aMaxStr.Search( "-" ) != -1 ) aMaxStr.RemoveAll( '-' );
1150 if ( !aMinStr.IsEmpty() && !aMinStr.IsIntegerValue() ||
1151 !aMaxStr.IsEmpty() && !aMaxStr.IsIntegerValue() )
1154 myMin.Append( aMinStr.IsEmpty() ? IntegerFirst() : aMinStr.IntegerValue() );
1155 myMax.Append( aMaxStr.IsEmpty() ? IntegerLast() : aMaxStr.IntegerValue() );
1162 //=======================================================================
1164 // Purpose : Get type of supported entities
1165 //=======================================================================
1166 SMDSAbs_ElementType RangeOfIds::GetType() const
1171 //=======================================================================
1173 // Purpose : Set type of supported entities
1174 //=======================================================================
1175 void RangeOfIds::SetType( SMDSAbs_ElementType theType )
1180 //=======================================================================
1182 // Purpose : Verify whether entity satisfies to this rpedicate
1183 //=======================================================================
1184 bool RangeOfIds::IsSatisfy( long theId )
1189 if ( myType == SMDSAbs_Node )
1191 if ( myMesh->FindNode( theId ) == 0 )
1196 const SMDS_MeshElement* anElem = myMesh->FindElement( theId );
1197 if ( anElem == 0 || myType != anElem->GetType() && myType != SMDSAbs_All )
1201 if ( myIds.Contains( theId ) )
1204 for ( int i = 1, n = myMin.Length(); i <= n; i++ )
1205 if ( theId >= myMin( i ) && theId <= myMax( i ) )
1213 Description : Base class for comparators
1215 Comparator::Comparator():
1219 Comparator::~Comparator()
1222 void Comparator::SetMesh( SMDS_Mesh* theMesh )
1225 myFunctor->SetMesh( theMesh );
1228 void Comparator::SetMargin( double theValue )
1230 myMargin = theValue;
1233 void Comparator::SetNumFunctor( NumericalFunctorPtr theFunct )
1235 myFunctor = theFunct;
1238 SMDSAbs_ElementType Comparator::GetType() const
1240 return myFunctor ? myFunctor->GetType() : SMDSAbs_All;
1243 double Comparator::GetMargin()
1251 Description : Comparator "<"
1253 bool LessThan::IsSatisfy( long theId )
1255 return myFunctor && myFunctor->GetValue( theId ) < myMargin;
1261 Description : Comparator ">"
1263 bool MoreThan::IsSatisfy( long theId )
1265 return myFunctor && myFunctor->GetValue( theId ) > myMargin;
1271 Description : Comparator "="
1274 myToler(Precision::Confusion())
1277 bool EqualTo::IsSatisfy( long theId )
1279 return myFunctor && fabs( myFunctor->GetValue( theId ) - myMargin ) < myToler;
1282 void EqualTo::SetTolerance( double theToler )
1287 double EqualTo::GetTolerance()
1294 Description : Logical NOT predicate
1296 LogicalNOT::LogicalNOT()
1299 LogicalNOT::~LogicalNOT()
1302 bool LogicalNOT::IsSatisfy( long theId )
1304 return myPredicate && !myPredicate->IsSatisfy( theId );
1307 void LogicalNOT::SetMesh( SMDS_Mesh* theMesh )
1310 myPredicate->SetMesh( theMesh );
1313 void LogicalNOT::SetPredicate( PredicatePtr thePred )
1315 myPredicate = thePred;
1318 SMDSAbs_ElementType LogicalNOT::GetType() const
1320 return myPredicate ? myPredicate->GetType() : SMDSAbs_All;
1325 Class : LogicalBinary
1326 Description : Base class for binary logical predicate
1328 LogicalBinary::LogicalBinary()
1331 LogicalBinary::~LogicalBinary()
1334 void LogicalBinary::SetMesh( SMDS_Mesh* theMesh )
1337 myPredicate1->SetMesh( theMesh );
1340 myPredicate2->SetMesh( theMesh );
1343 void LogicalBinary::SetPredicate1( PredicatePtr thePredicate )
1345 myPredicate1 = thePredicate;
1348 void LogicalBinary::SetPredicate2( PredicatePtr thePredicate )
1350 myPredicate2 = thePredicate;
1353 SMDSAbs_ElementType LogicalBinary::GetType() const
1355 if ( !myPredicate1 || !myPredicate2 )
1358 SMDSAbs_ElementType aType1 = myPredicate1->GetType();
1359 SMDSAbs_ElementType aType2 = myPredicate2->GetType();
1361 return aType1 == aType2 ? aType1 : SMDSAbs_All;
1367 Description : Logical AND
1369 bool LogicalAND::IsSatisfy( long theId )
1374 myPredicate1->IsSatisfy( theId ) &&
1375 myPredicate2->IsSatisfy( theId );
1381 Description : Logical OR
1383 bool LogicalOR::IsSatisfy( long theId )
1388 myPredicate1->IsSatisfy( theId ) ||
1389 myPredicate2->IsSatisfy( theId );
1403 void Filter::SetPredicate( PredicatePtr thePredicate )
1405 myPredicate = thePredicate;
1409 template<class TElement, class TIterator, class TPredicate>
1410 void FillSequence(const TIterator& theIterator,
1411 TPredicate& thePredicate,
1412 Filter::TIdSequence& theSequence)
1414 if ( theIterator ) {
1415 while( theIterator->more() ) {
1416 TElement anElem = theIterator->next();
1417 long anId = anElem->GetID();
1418 if ( thePredicate->IsSatisfy( anId ) )
1419 theSequence.push_back( anId );
1425 Filter::GetElementsId( SMDS_Mesh* theMesh )
1427 TIdSequence aSequence;
1428 if ( !theMesh || !myPredicate ) return aSequence;
1430 myPredicate->SetMesh( theMesh );
1432 SMDSAbs_ElementType aType = myPredicate->GetType();
1435 FillSequence<const SMDS_MeshNode*>(theMesh->nodesIterator(),myPredicate,aSequence);
1439 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),myPredicate,aSequence);
1443 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),myPredicate,aSequence);
1446 case SMDSAbs_Volume:{
1447 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),myPredicate,aSequence);
1451 FillSequence<const SMDS_MeshElement*>(theMesh->edgesIterator(),myPredicate,aSequence);
1452 FillSequence<const SMDS_MeshElement*>(theMesh->facesIterator(),myPredicate,aSequence);
1453 FillSequence<const SMDS_MeshElement*>(theMesh->volumesIterator(),myPredicate,aSequence);
1464 typedef std::set<SMDS_MeshFace*> TMapOfFacePtr;
1470 ManifoldPart::Link::Link( SMDS_MeshNode* theNode1,
1471 SMDS_MeshNode* theNode2 )
1477 ManifoldPart::Link::~Link()
1483 bool ManifoldPart::Link::IsEqual( const ManifoldPart::Link& theLink ) const
1485 if ( myNode1 == theLink.myNode1 &&
1486 myNode2 == theLink.myNode2 )
1488 else if ( myNode1 == theLink.myNode2 &&
1489 myNode2 == theLink.myNode1 )
1495 bool ManifoldPart::Link::operator<( const ManifoldPart::Link& x ) const
1497 if(myNode1 < x.myNode1) return true;
1498 if(myNode1 == x.myNode1)
1499 if(myNode2 < x.myNode2) return true;
1503 bool ManifoldPart::IsEqual( const ManifoldPart::Link& theLink1,
1504 const ManifoldPart::Link& theLink2 )
1506 return theLink1.IsEqual( theLink2 );
1509 ManifoldPart::ManifoldPart()
1512 myAngToler = Precision::Angular();
1513 myIsOnlyManifold = true;
1516 ManifoldPart::~ManifoldPart()
1521 void ManifoldPart::SetMesh( SMDS_Mesh* theMesh )
1527 SMDSAbs_ElementType ManifoldPart::GetType() const
1528 { return SMDSAbs_Face; }
1530 bool ManifoldPart::IsSatisfy( long theElementId )
1532 return myMapIds.Contains( theElementId );
1535 void ManifoldPart::SetAngleTolerance( const double theAngToler )
1536 { myAngToler = theAngToler; }
1538 double ManifoldPart::GetAngleTolerance() const
1539 { return myAngToler; }
1541 void ManifoldPart::SetIsOnlyManifold( const bool theIsOnly )
1542 { myIsOnlyManifold = theIsOnly; }
1544 void ManifoldPart::SetStartElem( const long theStartId )
1545 { myStartElemId = theStartId; }
1547 bool ManifoldPart::process()
1550 myMapBadGeomIds.Clear();
1552 myAllFacePtr.clear();
1553 myAllFacePtrIntDMap.clear();
1557 // collect all faces into own map
1558 SMDS_FaceIteratorPtr anFaceItr = myMesh->facesIterator();
1559 for (; anFaceItr->more(); )
1561 SMDS_MeshFace* aFacePtr = (SMDS_MeshFace*)anFaceItr->next();
1562 myAllFacePtr.push_back( aFacePtr );
1563 myAllFacePtrIntDMap[aFacePtr] = myAllFacePtr.size()-1;
1566 SMDS_MeshFace* aStartFace = (SMDS_MeshFace*)myMesh->FindElement( myStartElemId );
1570 // the map of non manifold links and bad geometry
1571 TMapOfLink aMapOfNonManifold;
1572 TColStd_MapOfInteger aMapOfTreated;
1574 // begin cycle on faces from start index and run on vector till the end
1575 // and from begin to start index to cover whole vector
1576 const int aStartIndx = myAllFacePtrIntDMap[aStartFace];
1577 bool isStartTreat = false;
1578 for ( int fi = aStartIndx; !isStartTreat || fi != aStartIndx ; fi++ )
1580 if ( fi == aStartIndx )
1581 isStartTreat = true;
1582 // as result next time when fi will be equal to aStartIndx
1584 SMDS_MeshFace* aFacePtr = myAllFacePtr[ fi ];
1585 if ( aMapOfTreated.Contains( aFacePtr->GetID() ) )
1588 aMapOfTreated.Add( aFacePtr->GetID() );
1589 TColStd_MapOfInteger aResFaces;
1590 if ( !findConnected( myAllFacePtrIntDMap, aFacePtr,
1591 aMapOfNonManifold, aResFaces ) )
1593 TColStd_MapIteratorOfMapOfInteger anItr( aResFaces );
1594 for ( ; anItr.More(); anItr.Next() )
1596 int aFaceId = anItr.Key();
1597 aMapOfTreated.Add( aFaceId );
1598 myMapIds.Add( aFaceId );
1601 if ( fi == ( myAllFacePtr.size() - 1 ) )
1603 } // end run on vector of faces
1604 return !myMapIds.IsEmpty();
1607 static void getLinks( const SMDS_MeshFace* theFace,
1608 ManifoldPart::TVectorOfLink& theLinks )
1610 int aNbNode = theFace->NbNodes();
1611 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1613 SMDS_MeshNode* aNode = 0;
1614 for ( ; aNodeItr->more() && i <= aNbNode; )
1617 SMDS_MeshNode* aN1 = (SMDS_MeshNode*)aNodeItr->next();
1621 SMDS_MeshNode* aN2 = ( i >= aNbNode ) ? aNode : (SMDS_MeshNode*)aNodeItr->next();
1623 ManifoldPart::Link aLink( aN1, aN2 );
1624 theLinks.push_back( aLink );
1628 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
1631 int aNbNode = theFace->NbNodes();
1632 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
1633 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
1635 for ( ; aNodeItr->more() && i <= 4; i++ )
1637 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
1638 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
1641 gp_XYZ q1 = anArrOfXYZ.Value( 2 ) - anArrOfXYZ.Value( 1 );
1642 gp_XYZ q2 = anArrOfXYZ.Value( 3 ) - anArrOfXYZ.Value( 1 );
1646 gp_XYZ q3 = anArrOfXYZ.Value( 4 ) - anArrOfXYZ.Value( 1 );
1649 double len = n.Modulus();
1656 bool ManifoldPart::findConnected
1657 ( const ManifoldPart::TDataMapFacePtrInt& theAllFacePtrInt,
1658 SMDS_MeshFace* theStartFace,
1659 ManifoldPart::TMapOfLink& theNonManifold,
1660 TColStd_MapOfInteger& theResFaces )
1662 theResFaces.Clear();
1663 if ( !theAllFacePtrInt.size() )
1666 if ( getNormale( theStartFace ).SquareModulus() <= gp::Resolution() )
1668 myMapBadGeomIds.Add( theStartFace->GetID() );
1672 ManifoldPart::TMapOfLink aMapOfBoundary, aMapToSkip;
1673 ManifoldPart::TVectorOfLink aSeqOfBoundary;
1674 theResFaces.Add( theStartFace->GetID() );
1675 ManifoldPart::TDataMapOfLinkFacePtr aDMapLinkFace;
1677 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
1678 aDMapLinkFace, theNonManifold, theStartFace );
1680 bool isDone = false;
1681 while ( !isDone && aMapOfBoundary.size() != 0 )
1683 bool isToReset = false;
1684 ManifoldPart::TVectorOfLink::iterator pLink = aSeqOfBoundary.begin();
1685 for ( ; !isToReset && pLink != aSeqOfBoundary.end(); ++pLink )
1687 ManifoldPart::Link aLink = *pLink;
1688 if ( aMapToSkip.find( aLink ) != aMapToSkip.end() )
1690 // each link could be treated only once
1691 aMapToSkip.insert( aLink );
1693 ManifoldPart::TVectorOfFacePtr aFaces;
1695 if ( myIsOnlyManifold &&
1696 (theNonManifold.find( aLink ) != theNonManifold.end()) )
1700 getFacesByLink( aLink, aFaces );
1701 // filter the element to keep only indicated elements
1702 ManifoldPart::TVectorOfFacePtr aFiltered;
1703 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
1704 for ( ; pFace != aFaces.end(); ++pFace )
1706 SMDS_MeshFace* aFace = *pFace;
1707 if ( myAllFacePtrIntDMap.find( aFace ) != myAllFacePtrIntDMap.end() )
1708 aFiltered.push_back( aFace );
1711 if ( aFaces.size() < 2 ) // no neihgbour faces
1713 else if ( myIsOnlyManifold && aFaces.size() > 2 ) // non manifold case
1715 theNonManifold.insert( aLink );
1720 // compare normal with normals of neighbor element
1721 SMDS_MeshFace* aPrevFace = aDMapLinkFace[ aLink ];
1722 ManifoldPart::TVectorOfFacePtr::iterator pFace = aFaces.begin();
1723 for ( ; pFace != aFaces.end(); ++pFace )
1725 SMDS_MeshFace* aNextFace = *pFace;
1726 if ( aPrevFace == aNextFace )
1728 int anNextFaceID = aNextFace->GetID();
1729 if ( myIsOnlyManifold && theResFaces.Contains( anNextFaceID ) )
1730 // should not be with non manifold restriction. probably bad topology
1732 // check if face was treated and skipped
1733 if ( myMapBadGeomIds.Contains( anNextFaceID ) ||
1734 !isInPlane( aPrevFace, aNextFace ) )
1736 // add new element to connected and extend the boundaries.
1737 theResFaces.Add( anNextFaceID );
1738 expandBoundary( aMapOfBoundary, aSeqOfBoundary,
1739 aDMapLinkFace, theNonManifold, aNextFace );
1743 isDone = !isToReset;
1746 return !theResFaces.IsEmpty();
1749 bool ManifoldPart::isInPlane( const SMDS_MeshFace* theFace1,
1750 const SMDS_MeshFace* theFace2 )
1752 gp_Dir aNorm1 = gp_Dir( getNormale( theFace1 ) );
1753 gp_XYZ aNorm2XYZ = getNormale( theFace2 );
1754 if ( aNorm2XYZ.SquareModulus() <= gp::Resolution() )
1756 myMapBadGeomIds.Add( theFace2->GetID() );
1759 if ( aNorm1.IsParallel( gp_Dir( aNorm2XYZ ), myAngToler ) )
1765 void ManifoldPart::expandBoundary
1766 ( ManifoldPart::TMapOfLink& theMapOfBoundary,
1767 ManifoldPart::TVectorOfLink& theSeqOfBoundary,
1768 ManifoldPart::TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
1769 ManifoldPart::TMapOfLink& theNonManifold,
1770 SMDS_MeshFace* theNextFace ) const
1772 ManifoldPart::TVectorOfLink aLinks;
1773 getLinks( theNextFace, aLinks );
1774 int aNbLink = aLinks.size();
1775 for ( int i = 0; i < aNbLink; i++ )
1777 ManifoldPart::Link aLink = aLinks[ i ];
1778 if ( myIsOnlyManifold && (theNonManifold.find( aLink ) != theNonManifold.end()) )
1780 if ( theMapOfBoundary.find( aLink ) != theMapOfBoundary.end() )
1782 if ( myIsOnlyManifold )
1784 // remove from boundary
1785 theMapOfBoundary.erase( aLink );
1786 ManifoldPart::TVectorOfLink::iterator pLink = theSeqOfBoundary.begin();
1787 for ( ; pLink != theSeqOfBoundary.end(); ++pLink )
1789 ManifoldPart::Link aBoundLink = *pLink;
1790 if ( aBoundLink.IsEqual( aLink ) )
1792 theSeqOfBoundary.erase( pLink );
1800 theMapOfBoundary.insert( aLink );
1801 theSeqOfBoundary.push_back( aLink );
1802 theDMapLinkFacePtr[ aLink ] = theNextFace;
1807 void ManifoldPart::getFacesByLink( const ManifoldPart::Link& theLink,
1808 ManifoldPart::TVectorOfFacePtr& theFaces ) const
1810 SMDS_Mesh::SetOfFaces aSetOfFaces;
1811 // take all faces that shared first node
1812 SMDS_ElemIteratorPtr anItr = theLink.myNode1->facesIterator();
1813 for ( ; anItr->more(); )
1815 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
1818 aSetOfFaces.insert( aFace );
1820 // take all faces that shared second node
1821 anItr = theLink.myNode2->facesIterator();
1822 // find the common part of two sets
1823 for ( ; anItr->more(); )
1825 SMDS_MeshFace* aFace = (SMDS_MeshFace*)anItr->next();
1826 if ( aSetOfFaces.find( aFace ) != aSetOfFaces.end() )
1827 theFaces.push_back( aFace );
1836 ElementsOnSurface::ElementsOnSurface()
1840 myType = SMDSAbs_All;
1842 myToler = Precision::Confusion();
1845 ElementsOnSurface::~ElementsOnSurface()
1850 void ElementsOnSurface::SetMesh( SMDS_Mesh* theMesh )
1852 if ( myMesh == theMesh )
1859 bool ElementsOnSurface::IsSatisfy( long theElementId )
1861 return myIds.Contains( theElementId );
1864 SMDSAbs_ElementType ElementsOnSurface::GetType() const
1867 void ElementsOnSurface::SetTolerance( const double theToler )
1868 { myToler = theToler; }
1870 double ElementsOnSurface::GetTolerance() const
1875 void ElementsOnSurface::SetSurface( const TopoDS_Shape& theShape,
1876 const SMDSAbs_ElementType theType )
1880 if ( theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE )
1885 TopoDS_Face aFace = TopoDS::Face( theShape );
1886 mySurf = BRep_Tool::Surface( aFace );
1889 void ElementsOnSurface::process()
1892 if ( mySurf.IsNull() )
1898 if ( myType == SMDSAbs_Face || myType == SMDSAbs_All )
1900 SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
1901 for(; anIter->more(); )
1902 process( anIter->next() );
1905 if ( myType == SMDSAbs_Edge || myType == SMDSAbs_All )
1907 SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
1908 for(; anIter->more(); )
1909 process( anIter->next() );
1912 if ( myType == SMDSAbs_Node )
1914 SMDS_NodeIteratorPtr anIter = myMesh->nodesIterator();
1915 for(; anIter->more(); )
1916 process( anIter->next() );
1920 void ElementsOnSurface::process( const SMDS_MeshElement* theElemPtr )
1922 SMDS_ElemIteratorPtr aNodeItr = theElemPtr->nodesIterator();
1923 bool isSatisfy = true;
1924 for ( ; aNodeItr->more(); )
1926 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
1927 if ( !isOnSurface( aNode ) )
1934 myIds.Add( theElemPtr->GetID() );
1937 bool ElementsOnSurface::isOnSurface( const SMDS_MeshNode* theNode ) const
1939 if ( mySurf.IsNull() )
1942 gp_Pnt aPnt( theNode->X(), theNode->Y(), theNode->Z() );
1943 double aToler2 = myToler * myToler;
1944 if ( mySurf->IsKind(STANDARD_TYPE(Geom_Plane)))
1946 gp_Pln aPln = Handle(Geom_Plane)::DownCast(mySurf)->Pln();
1947 if ( aPln.SquareDistance( aPnt ) > aToler2 )
1950 else if ( mySurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
1952 gp_Cylinder aCyl = Handle(Geom_CylindricalSurface)::DownCast(mySurf)->Cylinder();
1953 double aRad = aCyl.Radius();
1954 gp_Ax3 anAxis = aCyl.Position();
1955 gp_XYZ aLoc = aCyl.Location().XYZ();
1956 double aXDist = anAxis.XDirection().XYZ() * ( aPnt.XYZ() - aLoc );
1957 double aYDist = anAxis.YDirection().XYZ() * ( aPnt.XYZ() - aLoc );
1958 if ( fabs(aXDist*aXDist + aYDist*aYDist - aRad*aRad) > aToler2 )