Salome HOME
PAL14419 (IMP: a filter predicate to find nodes/elements lying on any
[modules/smesh.git] / src / Controls / SMESH_ControlsDef.hxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
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. 
8 // 
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. 
13 // 
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 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 #ifndef _SMESH_CONTROLSDEF_HXX_
21 #define _SMESH_CONTROLSDEF_HXX_
22
23 #include <set>
24 #include <map>
25 #include <vector>
26 #include <boost/shared_ptr.hpp>
27 #include <gp_XYZ.hxx>
28 //#include <Geom_Surface.hxx>
29 #include <GeomAPI_ProjectPointOnSurf.hxx>
30 #include <TColStd_SequenceOfInteger.hxx>
31 #include <TColStd_MapOfInteger.hxx>
32 #include <TCollection_AsciiString.hxx>
33 #include <TopoDS_Face.hxx>
34
35 #include "SMDSAbs_ElementType.hxx"
36 #include "SMDS_MeshNode.hxx"
37
38 #include "SMESH_Controls.hxx"
39
40 class SMDS_MeshElement;
41 class SMDS_MeshFace;
42 class SMDS_MeshNode;
43 class SMDS_Mesh;
44
45 class SMESHDS_Mesh;
46 class SMESHDS_SubMesh;
47
48 class gp_Pnt;
49 //class TopoDS_Shape;
50
51
52 namespace SMESH{
53   namespace Controls{
54
55     class TSequenceOfXYZ: public std::vector<gp_XYZ>
56     {
57     public:
58       typedef std::vector<gp_XYZ> TSuperClass;
59       TSequenceOfXYZ()
60       {}
61
62       TSequenceOfXYZ(size_type n):
63         TSuperClass(n)
64       {}
65
66       TSequenceOfXYZ(size_type n, const value_type& t):
67         TSuperClass(n,t)
68       {}
69
70       TSequenceOfXYZ(const TSequenceOfXYZ& theSequenceOfXYZ):
71         TSuperClass(theSequenceOfXYZ)
72       {}
73
74       template <class InputIterator>
75       TSequenceOfXYZ(InputIterator theBegin, InputIterator theEnd):
76         TSuperClass(theBegin,theEnd)
77       {}
78
79       TSequenceOfXYZ& operator=(const TSequenceOfXYZ& theSequenceOfXYZ){
80         TSuperClass::operator=(theSequenceOfXYZ);
81         return *this;
82       }
83
84       reference operator()(size_type n){
85         return TSuperClass::operator[](n-1);
86       }
87
88       const_reference operator()(size_type n) const{
89         return TSuperClass::operator[](n-1);
90       }
91
92     private:
93       reference operator[](size_type n);
94
95       const_reference operator[](size_type n) const;
96     };
97
98     /*
99       Class       : Functor
100       Description : Root of all Functors
101     */
102     class Functor
103     {
104     public:
105       ~Functor(){}
106       virtual void SetMesh( const SMDS_Mesh* theMesh ) = 0;
107       virtual SMDSAbs_ElementType GetType() const = 0;
108     };
109
110     /*
111       Class       : NumericalFunctor
112       Description : Root of all Functors returning numeric value
113     */
114     class NumericalFunctor: public virtual Functor{
115     public:
116       NumericalFunctor();
117       virtual void SetMesh( const SMDS_Mesh* theMesh );
118       virtual double GetValue( long theElementId );
119       virtual double GetValue(const TSequenceOfXYZ& thePoints) { return -1.0;};
120       virtual SMDSAbs_ElementType GetType() const = 0;
121       virtual double GetBadRate( double Value, int nbNodes ) const = 0;
122       long  GetPrecision() const;
123       void  SetPrecision( const long thePrecision );
124       
125       bool GetPoints(const int theId, 
126                      TSequenceOfXYZ& theRes) const;
127       static bool GetPoints(const SMDS_MeshElement* theElem, 
128                             TSequenceOfXYZ& theRes);
129     protected:
130       const SMDS_Mesh* myMesh;
131       const SMDS_MeshElement* myCurrElement;
132       long       myPrecision;
133     };
134   
135   
136     /*
137       Class       : Volume
138       Description : Functor calculating volume of 3D mesh element
139     */
140     class Volume: public virtual NumericalFunctor{
141     public:
142       virtual double GetValue( long theElementId );
143       //virtual double GetValue( const TSequenceOfXYZ& thePoints );
144       virtual double GetBadRate( double Value, int nbNodes ) const;
145       virtual SMDSAbs_ElementType GetType() const;
146     };
147   
148   
149     /*
150       Class       : SMESH_MinimumAngle
151       Description : Functor for calculation of minimum angle
152     */
153     class MinimumAngle: public virtual NumericalFunctor{
154     public:
155       virtual double GetValue( const TSequenceOfXYZ& thePoints );
156       virtual double GetBadRate( double Value, int nbNodes ) const;
157       virtual SMDSAbs_ElementType GetType() const;
158     };
159   
160   
161     /*
162       Class       : AspectRatio
163       Description : Functor for calculating aspect ratio
164     */
165     class AspectRatio: public virtual NumericalFunctor{
166     public:
167       virtual double GetValue( const TSequenceOfXYZ& thePoints );
168       virtual double GetBadRate( double Value, int nbNodes ) const;
169       virtual SMDSAbs_ElementType GetType() const;
170     };
171   
172   
173     /*
174       Class       : AspectRatio3D
175       Description : Functor for calculating aspect ratio of 3D elems.
176     */
177     class AspectRatio3D: public virtual NumericalFunctor{
178     public:
179       virtual double GetValue( const TSequenceOfXYZ& thePoints );
180       virtual double GetBadRate( double Value, int nbNodes ) const;
181       virtual SMDSAbs_ElementType GetType() const;
182     };
183   
184   
185     /*
186       Class       : Warping
187       Description : Functor for calculating warping
188     */
189     class Warping: public virtual NumericalFunctor{
190     public:
191       virtual double GetValue( const TSequenceOfXYZ& thePoints );
192       virtual double GetBadRate( double Value, int nbNodes ) const;
193       virtual SMDSAbs_ElementType GetType() const;
194       
195     private:
196       double ComputeA( const gp_XYZ&, const gp_XYZ&, const gp_XYZ&, const gp_XYZ& ) const;
197     };
198   
199   
200     /*
201       Class       : Taper
202       Description : Functor for calculating taper
203     */
204     class Taper: public virtual NumericalFunctor{
205     public:
206       virtual double GetValue( const TSequenceOfXYZ& thePoints );
207       virtual double GetBadRate( double Value, int nbNodes ) const;
208       virtual SMDSAbs_ElementType GetType() const;
209     };
210     
211   
212     /*
213       Class       : Skew
214       Description : Functor for calculating skew in degrees
215     */
216     class Skew: public virtual NumericalFunctor{
217     public:
218       virtual double GetValue( const TSequenceOfXYZ& thePoints );
219       virtual double GetBadRate( double Value, int nbNodes ) const;
220       virtual SMDSAbs_ElementType GetType() const;
221     };
222   
223     
224     /*
225       Class       : Area
226       Description : Functor for calculating area
227     */
228     class Area: public virtual NumericalFunctor{
229     public:
230       virtual double GetValue( const TSequenceOfXYZ& thePoints );
231       virtual double GetBadRate( double Value, int nbNodes ) const;
232       virtual SMDSAbs_ElementType GetType() const;
233     };
234   
235   
236     /*
237       Class       : Length
238       Description : Functor for calculating length of edge
239     */
240     class Length: public virtual NumericalFunctor{
241     public:
242       virtual double GetValue( const TSequenceOfXYZ& thePoints );
243       virtual double GetBadRate( double Value, int nbNodes ) const;
244       virtual SMDSAbs_ElementType GetType() const;
245     };
246   
247     /*
248       Class       : Length2D
249       Description : Functor for calculating length of edge
250     */
251     class Length2D: public virtual NumericalFunctor{
252     public:
253       virtual double GetValue( long theElementId );
254       virtual double GetBadRate( double Value, int nbNodes ) const;
255       virtual SMDSAbs_ElementType GetType() const;
256       struct Value{
257         double myLength;
258         long myPntId[2];
259         Value(double theLength, long thePntId1, long thePntId2);
260         bool operator<(const Value& x) const;
261       };
262       typedef std::set<Value> TValues;
263       void GetValues(TValues& theValues);
264       
265     };
266     typedef boost::shared_ptr<Length2D> Length2DPtr;
267
268     /*
269       Class       : MultiConnection
270       Description : Functor for calculating number of faces conneted to the edge
271     */
272     class MultiConnection: public virtual NumericalFunctor{
273     public:
274       virtual double GetValue( long theElementId );
275       virtual double GetValue( const TSequenceOfXYZ& thePoints );
276       virtual double GetBadRate( double Value, int nbNodes ) const;
277       virtual SMDSAbs_ElementType GetType() const;
278     };
279     
280     /*
281       Class       : MultiConnection2D
282       Description : Functor for calculating number of faces conneted to the edge
283     */
284     class MultiConnection2D: public virtual NumericalFunctor{
285     public:
286       virtual double GetValue( long theElementId );
287       virtual double GetValue( const TSequenceOfXYZ& thePoints );
288       virtual double GetBadRate( double Value, int nbNodes ) const;
289       virtual SMDSAbs_ElementType GetType() const;
290       struct Value{
291         long myPntId[2];
292         Value(long thePntId1, long thePntId2);
293         bool operator<(const Value& x) const;
294       };
295       typedef std::map<Value,int> MValues;
296
297       void GetValues(MValues& theValues);
298     };
299     typedef boost::shared_ptr<MultiConnection2D> MultiConnection2DPtr;
300     /*
301       PREDICATES
302     */
303     /*
304       Class       : Predicate
305       Description : Base class for all predicates
306     */
307     class Predicate: public virtual Functor{
308     public:
309       virtual bool IsSatisfy( long theElementId ) = 0;
310       virtual SMDSAbs_ElementType GetType() const = 0;
311     };
312     
313   
314   
315     /*
316       Class       : FreeBorders
317       Description : Predicate for free borders
318     */
319     class FreeBorders: public virtual Predicate{
320     public:
321       FreeBorders();
322       virtual void SetMesh( const SMDS_Mesh* theMesh );
323       virtual bool IsSatisfy( long theElementId );
324       virtual SMDSAbs_ElementType GetType() const;
325             
326     protected:
327       const SMDS_Mesh* myMesh;
328     };
329    
330
331     /*
332       Class       : BadOrientedVolume
333       Description : Predicate bad oriented volumes
334     */
335     class BadOrientedVolume: public virtual Predicate{
336     public:
337       BadOrientedVolume();
338       virtual void SetMesh( const SMDS_Mesh* theMesh );
339       virtual bool IsSatisfy( long theElementId );
340       virtual SMDSAbs_ElementType GetType() const;
341             
342     protected:
343       const SMDS_Mesh* myMesh;
344     };
345    
346
347     /*
348       Class       : FreeEdges
349       Description : Predicate for free Edges
350     */
351     class FreeEdges: public virtual Predicate{
352     public:
353       FreeEdges();
354       virtual void SetMesh( const SMDS_Mesh* theMesh );
355       virtual bool IsSatisfy( long theElementId );
356       virtual SMDSAbs_ElementType GetType() const;
357       static bool IsFreeEdge( const SMDS_MeshNode** theNodes, const int theFaceId  );
358       typedef long TElemId;
359       struct Border{
360         TElemId myElemId;
361         TElemId myPntId[2];
362         Border(long theElemId, long thePntId1, long thePntId2);
363         bool operator<(const Border& x) const;
364       };
365       typedef std::set<Border> TBorders;
366       void GetBoreders(TBorders& theBorders);
367       
368     protected:
369       const SMDS_Mesh* myMesh;
370     };
371     typedef boost::shared_ptr<FreeEdges> FreeEdgesPtr;
372
373
374     /*
375       Class       : RangeOfIds
376       Description : Predicate for Range of Ids.
377                     Range may be specified with two ways.
378                     1. Using AddToRange method
379                     2. With SetRangeStr method. Parameter of this method is a string
380                        like as "1,2,3,50-60,63,67,70-"
381     */
382     class RangeOfIds: public virtual Predicate
383     {
384     public:
385                                     RangeOfIds();
386       virtual void                  SetMesh( const SMDS_Mesh* theMesh );
387       virtual bool                  IsSatisfy( long theNodeId );
388       virtual SMDSAbs_ElementType   GetType() const;
389       virtual void                  SetType( SMDSAbs_ElementType theType );
390
391       bool                          AddToRange( long theEntityId );
392       void                          GetRangeStr( TCollection_AsciiString& );
393       bool                          SetRangeStr( const TCollection_AsciiString& );
394
395     protected:
396       const SMDS_Mesh*              myMesh;
397
398       TColStd_SequenceOfInteger     myMin;
399       TColStd_SequenceOfInteger     myMax;
400       TColStd_MapOfInteger          myIds;
401
402       SMDSAbs_ElementType           myType;
403     };
404     
405     typedef boost::shared_ptr<RangeOfIds> RangeOfIdsPtr;
406    
407     
408     /*
409       Class       : Comparator
410       Description : Base class for comparators
411     */
412     class Comparator: public virtual Predicate{
413     public:
414       Comparator();
415       virtual ~Comparator();
416       virtual void SetMesh( const SMDS_Mesh* theMesh );
417       virtual void SetMargin(double theValue);
418       virtual void SetNumFunctor(NumericalFunctorPtr theFunct);
419       virtual bool IsSatisfy( long theElementId ) = 0;
420       virtual SMDSAbs_ElementType GetType() const;
421       double  GetMargin();
422   
423     protected:
424       double myMargin;
425       NumericalFunctorPtr myFunctor;
426     };
427     typedef boost::shared_ptr<Comparator> ComparatorPtr;
428   
429   
430     /*
431       Class       : LessThan
432       Description : Comparator "<"
433     */
434     class LessThan: public virtual Comparator{
435     public:
436       virtual bool IsSatisfy( long theElementId );
437     };
438   
439   
440     /*
441       Class       : MoreThan
442       Description : Comparator ">"
443     */
444     class MoreThan: public virtual Comparator{
445     public:
446       virtual bool IsSatisfy( long theElementId );
447     };
448   
449   
450     /*
451       Class       : EqualTo
452       Description : Comparator "="
453     */
454     class EqualTo: public virtual Comparator{
455     public:
456       EqualTo();
457       virtual bool IsSatisfy( long theElementId );
458       virtual void SetTolerance( double theTol );
459       virtual double GetTolerance();
460   
461     private:
462       double myToler;
463     };
464     typedef boost::shared_ptr<EqualTo> EqualToPtr;
465   
466     
467     /*
468       Class       : LogicalNOT
469       Description : Logical NOT predicate
470     */
471     class LogicalNOT: public virtual Predicate{
472     public:
473       LogicalNOT();
474       virtual ~LogicalNOT();
475       virtual bool IsSatisfy( long theElementId );
476       virtual void SetMesh( const SMDS_Mesh* theMesh );
477       virtual void SetPredicate(PredicatePtr thePred);
478       virtual SMDSAbs_ElementType GetType() const;
479   
480     private:
481       PredicatePtr myPredicate;
482     };
483     typedef boost::shared_ptr<LogicalNOT> LogicalNOTPtr;
484     
485   
486     /*
487       Class       : LogicalBinary
488       Description : Base class for binary logical predicate
489     */
490     class LogicalBinary: public virtual Predicate{
491     public:
492       LogicalBinary();
493       virtual ~LogicalBinary();
494       virtual void SetMesh( const SMDS_Mesh* theMesh );
495       virtual void SetPredicate1(PredicatePtr thePred);
496       virtual void SetPredicate2(PredicatePtr thePred);
497       virtual SMDSAbs_ElementType GetType() const;
498   
499     protected:
500       PredicatePtr myPredicate1;
501       PredicatePtr myPredicate2;
502     };
503     typedef boost::shared_ptr<LogicalBinary> LogicalBinaryPtr;
504   
505   
506     /*
507       Class       : LogicalAND
508       Description : Logical AND
509     */
510     class LogicalAND: public virtual LogicalBinary{
511     public:
512       virtual bool IsSatisfy( long theElementId );
513     };
514   
515   
516     /*
517       Class       : LogicalOR
518       Description : Logical OR
519     */
520     class LogicalOR: public virtual LogicalBinary{
521     public:
522       virtual bool IsSatisfy( long theElementId );
523     };
524   
525   
526     /*
527       Class       : ManifoldPart
528       Description : Predicate for manifold part of mesh
529     */
530     class ManifoldPart: public virtual Predicate{
531     public:
532
533       /* internal class for algorithm uses */
534       class Link
535       {
536       public:
537         Link( SMDS_MeshNode* theNode1,
538               SMDS_MeshNode* theNode2 );
539         ~Link();
540         
541         bool IsEqual( const ManifoldPart::Link& theLink ) const;
542         bool operator<(const ManifoldPart::Link& x) const;
543         
544         SMDS_MeshNode* myNode1;
545         SMDS_MeshNode* myNode2;
546       };
547
548       bool IsEqual( const ManifoldPart::Link& theLink1,
549                     const ManifoldPart::Link& theLink2 );
550       
551       typedef std::set<ManifoldPart::Link>                TMapOfLink;
552       typedef std::vector<SMDS_MeshFace*>                 TVectorOfFacePtr;
553       typedef std::vector<ManifoldPart::Link>             TVectorOfLink;
554       typedef std::map<SMDS_MeshFace*,int>                TDataMapFacePtrInt;
555       typedef std::map<ManifoldPart::Link,SMDS_MeshFace*> TDataMapOfLinkFacePtr;
556       
557       ManifoldPart();
558       ~ManifoldPart();
559       virtual void SetMesh( const SMDS_Mesh* theMesh );
560       // inoke when all parameters already set
561       virtual bool IsSatisfy( long theElementId );
562       virtual      SMDSAbs_ElementType GetType() const;
563
564       void    SetAngleTolerance( const double theAngToler );
565       double  GetAngleTolerance() const;
566       void    SetIsOnlyManifold( const bool theIsOnly );
567       void    SetStartElem( const long  theStartElemId );
568
569     private:
570       bool    process();
571       bool    findConnected( const TDataMapFacePtrInt& theAllFacePtrInt,
572                              SMDS_MeshFace*            theStartFace,
573                              TMapOfLink&               theNonManifold,
574                              TColStd_MapOfInteger&     theResFaces );
575       bool    isInPlane( const SMDS_MeshFace* theFace1,
576                           const SMDS_MeshFace* theFace2 );
577       void    expandBoundary( TMapOfLink&            theMapOfBoundary,
578                               TVectorOfLink&         theSeqOfBoundary,
579                               TDataMapOfLinkFacePtr& theDMapLinkFacePtr,
580                               TMapOfLink&            theNonManifold,
581                               SMDS_MeshFace*         theNextFace ) const;
582
583      void     getFacesByLink( const Link& theLink,
584                               TVectorOfFacePtr& theFaces ) const;
585
586     private:
587       const SMDS_Mesh*      myMesh;
588       TColStd_MapOfInteger  myMapIds;
589       TColStd_MapOfInteger  myMapBadGeomIds;
590       TVectorOfFacePtr      myAllFacePtr;
591       TDataMapFacePtrInt    myAllFacePtrIntDMap;
592       double                myAngToler;
593       bool                  myIsOnlyManifold;
594       long                  myStartElemId;
595
596     };
597     typedef boost::shared_ptr<ManifoldPart> ManifoldPartPtr;
598                          
599
600     /*
601       Class       : ElementsOnSurface
602       Description : Predicate elements that lying on indicated surface
603                     (plane or cylinder)
604     */
605     class ElementsOnSurface : public virtual Predicate {
606     public:
607       ElementsOnSurface();
608       ~ElementsOnSurface();
609       virtual void SetMesh( const SMDS_Mesh* theMesh );
610       virtual bool IsSatisfy( long theElementId );
611       virtual      SMDSAbs_ElementType GetType() const;
612
613       void    SetTolerance( const double theToler );
614       double  GetTolerance() const;
615       void    SetSurface( const TopoDS_Shape& theShape,
616                           const SMDSAbs_ElementType theType );
617       void    SetUseBoundaries( bool theUse );
618       bool    GetUseBoundaries() const { return myUseBoundaries; }
619
620     private:
621       void    process();
622       void    process( const SMDS_MeshElement* theElem  );
623       bool    isOnSurface( const SMDS_MeshNode* theNode );
624
625     private:
626       const SMDS_Mesh*      myMesh;
627       TColStd_MapOfInteger  myIds;
628       SMDSAbs_ElementType   myType;
629       //Handle(Geom_Surface)  mySurf;
630       TopoDS_Face           mySurf;
631       double                myToler;
632       bool                  myUseBoundaries;
633       GeomAPI_ProjectPointOnSurf myProjector;
634     };
635     
636     typedef boost::shared_ptr<ElementsOnSurface> ElementsOnSurfacePtr;
637       
638
639     /*
640       FILTER
641     */
642     class Filter{
643     public:
644       Filter();
645       virtual ~Filter();
646       virtual void SetPredicate(PredicatePtr thePred);
647
648       typedef std::vector<long> TIdSequence;
649
650       virtual 
651       void
652       GetElementsId( const SMDS_Mesh* theMesh,
653                      TIdSequence& theSequence );
654
655       static
656       void
657       GetElementsId( const SMDS_Mesh* theMesh, 
658                      PredicatePtr thePredicate,
659                      TIdSequence& theSequence );
660       
661     protected:
662       PredicatePtr myPredicate;
663     };
664   };  
665 };
666
667
668 #endif