Salome HOME
0023544: SMESH's performance issues
[modules/smesh.git] / src / SMDS / SMDS_MeshElement.hxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMDS : implementation of Salome mesh data structure
24 //  File   : SMDS_MeshElement.hxx
25 //  Module : SMESH
26 //
27 #ifndef _SMDS_MeshElement_HeaderFile
28 #define _SMDS_MeshElement_HeaderFile
29
30 #include "SMESH_SMDS.hxx"
31         
32 #include "SMDSAbs_ElementType.hxx"
33 #include "SMDS_MeshObject.hxx"
34 #include "SMDS_ElemIterator.hxx"
35 #include "SMDS_StdIterator.hxx"
36
37 #include <iostream>
38
39 #include <vtkType.h>
40 #include <vtkCellType.h>
41
42 class SMDS_ElementChunk;
43 class SMDS_Mesh;
44 class SMDS_MeshNode;
45 class SMDS_UnstructuredGrid;
46
47 // ============================================================
48 /*!
49  * \brief Base class for elements
50  */
51 // ============================================================
52
53
54 class SMDS_EXPORT SMDS_MeshElement : public SMDS_MeshObject
55 {
56 public:
57
58   // ===========================
59   // Access to nodes
60   // ===========================
61   virtual SMDS_ElemIteratorPtr nodesIterator() const = 0;
62
63   virtual SMDS_NodeIteratorPtr nodeIterator() const = 0;
64   virtual SMDS_NodeIteratorPtr interlacedNodesIterator() const { return nodeIterator(); }
65   virtual SMDS_NodeIteratorPtr nodesIteratorToUNV() const  { return nodeIterator(); }
66
67   // std-like iteration on nodes
68   typedef SMDS_StdIterator< const SMDS_MeshNode*, SMDS_NodeIteratorPtr > iterator;
69   iterator begin_nodes() const { return iterator( nodeIterator() ); }
70   iterator end_nodes()   const { return iterator(); }
71
72   // ===========================
73   // Type of element
74   // ===========================
75   virtual int NbNodes() const = 0;
76   virtual int NbEdges() const = 0;
77   virtual int NbFaces() const = 0;
78
79   virtual SMDSAbs_ElementType  GetType() const = 0;
80   virtual SMDSAbs_EntityType   GetEntityType() const = 0;
81   virtual SMDSAbs_GeometryType GetGeomType() const = 0;
82   virtual VTKCellType          GetVtkType() const = 0;
83
84   virtual bool IsPoly() const = 0;
85   virtual bool IsQuadratic() const = 0;
86   virtual bool IsMediumNode(const SMDS_MeshNode* node) const;
87   virtual int  NbCornerNodes() const = 0;
88
89   // ===========================
90   //  Access to nodes by index
91   // ===========================
92   /*!
93    * \brief Return node by its index
94     * \param ind - node index
95     * \retval const SMDS_MeshNode* - the node
96    */
97   virtual const SMDS_MeshNode* GetNode(const int ind) const = 0;
98
99   /*!
100    * \brief Return node by its index
101     * \param ind - node index
102     * \retval const SMDS_MeshNode* - the node
103    * 
104    * Index is wrapped if it is out of a valid range of corner nodes
105    */
106   const SMDS_MeshNode* GetNodeWrap(const int ind) const { return GetNode( WrappedIndex( ind )); }
107
108   /*!
109    * \brief Return true if index of node is valid (0 <= ind < NbNodes())
110     * \param ind - node index
111     * \retval bool - index check result
112    */
113   virtual bool IsValidIndex(const int ind) const;
114
115   /*!
116    * \brief Return a valid corner node index, fixing the given one if necessary
117     * \param ind - node index
118     * \retval int - valid node index
119    */
120   int WrappedIndex(const int ind) const;
121
122   /*!
123    * \brief Check if a node belongs to the element
124     * \param node - the node to check
125     * \retval int - node index within the element, -1 if not found
126    */
127   virtual int GetNodeIndex( const SMDS_MeshNode* node ) const;
128
129
130   virtual int GetID() const;
131   virtual int GetVtkID()   const;
132   virtual int getshapeId() const { return GetShapeID(); }
133   virtual int GetShapeID() const;
134
135   // mark this element; to be used in algos
136   virtual void setIsMarked( bool is ) const;
137   virtual bool isMarked() const;
138
139   // element can be allocated but "not used"
140   bool IsNull() const { return myHolder == 0; }
141
142   SMDS_Mesh* GetMesh() const;
143
144   void Print(std::ostream & OS) const;
145
146   friend SMDS_EXPORT std::ostream & operator <<(std::ostream & OS, const SMDS_MeshElement *);
147   friend class SMDS_ElementFactory;
148   friend class SMESHDS_SubMesh;
149
150   /*!
151    * \brief Filters of elements, to be used with SMDS_SetIterator
152    */
153   struct Filter
154   {
155     virtual bool operator()(const SMDS_MeshElement* e) const = 0;
156     virtual ~Filter() {}
157   };
158   struct NonNullFilter: public Filter
159   {
160     bool operator()(const SMDS_MeshElement* e) const { return e; }
161   };
162   struct TypeFilter : public Filter
163   {
164     SMDSAbs_ElementType _type;
165     TypeFilter( SMDSAbs_ElementType t = SMDSAbs_NbElementTypes ):_type(t) {}
166     bool operator()(const SMDS_MeshElement* e) const { return e && e->GetType() == _type; }
167   };
168   struct EntityFilter : public Filter
169   {
170     SMDSAbs_EntityType _type;
171     EntityFilter( SMDSAbs_EntityType t = SMDSEntity_Last ):_type(t) {}
172     bool operator()(const SMDS_MeshElement* e) const { return e && e->GetEntityType() == _type; }
173   };
174   struct GeomFilter : public Filter
175   {
176     SMDSAbs_GeometryType _type;
177     GeomFilter( SMDSAbs_GeometryType t = SMDSGeom_NONE ):_type(t) {}
178     bool operator()(const SMDS_MeshElement* e) const { return e && e->GetGeomType() == _type; }
179   };
180
181  protected:
182
183   SMDS_MeshElement();
184
185   void setVtkID(const int vtkID );
186   virtual void setShapeID( const int shapeID ) const;
187
188   SMDS_UnstructuredGrid* getGrid() const;
189
190  protected:
191
192   SMDS_ElementChunk* myHolder;
193 };
194
195 // ============================================================
196 /*!
197  * \brief Comparator of elements by ID for usage in std containers
198  */
199 // ============================================================
200
201 struct TIDTypeCompare {
202   bool operator () (const SMDS_MeshElement* e1, const SMDS_MeshElement* e2) const
203   { return e1->GetType() == e2->GetType() ? e1->GetID() < e2->GetID() : e1->GetType() < e2->GetType(); }
204 };
205
206 // WARNING: this comparator makes impossible to store both nodes and elements in the same set
207 // because there are nodes and elements with the same ID. Use TIDTypeCompare for such containers.
208 struct TIDCompare {
209   bool operator () (const SMDS_MeshElement* e1, const SMDS_MeshElement* e2) const
210   { return e1->GetID() < e2->GetID(); }
211 };
212
213 #endif