Salome HOME
ObjectPool and SMDS_Position
[modules/smesh.git] / src / SMDS / SMDS_MeshElement.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SMESH SMDS : implementaion of Salome mesh data structure
23 //
24 #ifdef _MSC_VER
25 #pragma warning(disable:4786)
26 #endif
27
28 #include "SMDS_MeshElement.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_MeshEdge.hxx"
31 #include "SMDS_MeshFace.hxx"
32 #include "SMDS_MeshVolume.hxx"
33 #include "utilities.h"
34
35 using namespace std;
36
37 int SMDS_MeshCell::nbCells = 0;
38
39 SMDS_MeshElement::SMDS_MeshElement(int ID):myID(ID), myMeshId(-1), myShapeId(-1)
40 {
41 }
42
43 SMDS_MeshElement::SMDS_MeshElement(int id, UShortType meshId, ShortType shapeId):
44   myID(id), myMeshId(meshId), myShapeId(shapeId)
45 {
46 }
47
48 void SMDS_MeshElement::Print(ostream & OS) const
49 {
50         OS << "dump of mesh element" << endl;
51 }
52
53 ostream & operator <<(ostream & OS, const SMDS_MeshElement * ME)
54 {
55         ME->Print(OS);
56         return OS;
57 }
58
59 ///////////////////////////////////////////////////////////////////////////////
60 /// Create an iterator which iterate on nodes owned by the element.
61 /// This method call elementsIterator().
62 ///////////////////////////////////////////////////////////////////////////////
63 SMDS_ElemIteratorPtr SMDS_MeshElement::nodesIterator() const
64 {
65         return elementsIterator(SMDSAbs_Node);
66 }
67
68 ///////////////////////////////////////////////////////////////////////////////
69 /// Create an iterator which iterate on edges linked with or owned by the element.
70 /// This method call elementsIterator().
71 ///////////////////////////////////////////////////////////////////////////////
72 SMDS_ElemIteratorPtr SMDS_MeshElement::edgesIterator() const
73 {
74         return elementsIterator(SMDSAbs_Edge);
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////
78 /// Create an iterator which iterate on faces linked with or owned by the element.
79 /// This method call elementsIterator().
80 ///////////////////////////////////////////////////////////////////////////////
81 SMDS_ElemIteratorPtr SMDS_MeshElement::facesIterator() const
82 {
83         return elementsIterator(SMDSAbs_Face);
84 }
85
86 ///////////////////////////////////////////////////////////////////////////////
87 ///Return The number of nodes owned by the current element
88 ///////////////////////////////////////////////////////////////////////////////
89 int SMDS_MeshElement::NbNodes() const
90 {
91         int nbnodes=0;
92         SMDS_ElemIteratorPtr it=nodesIterator();
93         while(it->more())
94         {
95                 it->next();
96                 nbnodes++;
97         }
98         return nbnodes;
99 }
100
101 ///////////////////////////////////////////////////////////////////////////////
102 ///Return the number of edges owned by or linked with the current element
103 ///////////////////////////////////////////////////////////////////////////////
104 int SMDS_MeshElement::NbEdges() const
105 {
106         int nbedges=0;
107         SMDS_ElemIteratorPtr it=edgesIterator();
108         while(it->more())
109         {
110                 it->next();
111                 nbedges++;
112         }
113         return nbedges;
114 }
115
116 ///////////////////////////////////////////////////////////////////////////////
117 ///Return the number of faces owned by or linked with the current element
118 ///////////////////////////////////////////////////////////////////////////////
119 int SMDS_MeshElement::NbFaces() const
120 {
121         int nbfaces=0;
122         SMDS_ElemIteratorPtr it=facesIterator();
123         while(it->more())
124         {
125                 it->next();
126                 nbfaces++;
127         }
128         return nbfaces;
129 }
130
131 ///////////////////////////////////////////////////////////////////////////////
132 ///Create an iterator which iterate on elements linked with the current element.
133 ///@param type The of elements on which you want to iterate
134 ///@return A smart pointer to iterator, you are not to take care of freeing memory
135 ///////////////////////////////////////////////////////////////////////////////
136 class SMDS_MeshElement_MyIterator:public SMDS_ElemIterator
137 {
138   const SMDS_MeshElement * myElement;
139   bool myMore;
140  public:
141   SMDS_MeshElement_MyIterator(const SMDS_MeshElement * element):
142     myElement(element),myMore(true) {}
143
144   bool more()
145   {
146     return myMore;
147   }
148
149   const SMDS_MeshElement* next()
150   {
151     myMore=false;
152     return myElement;   
153   }     
154 };
155 SMDS_ElemIteratorPtr SMDS_MeshElement::
156         elementsIterator(SMDSAbs_ElementType type) const
157 {
158         /** @todo Check that iterator in the child classes return elements
159         in the same order for each different implementation (i.e: SMDS_VolumeOfNodes
160         and SMDS_VolumeOfFaces */
161         
162         if(type==GetType())
163           return SMDS_ElemIteratorPtr(new SMDS_MeshElement_MyIterator(this));
164         else 
165         {
166           MESSAGE("Iterator not implemented");
167           return SMDS_ElemIteratorPtr((SMDS_ElemIterator*)NULL);
168         }
169 }
170
171 ///////////////////////////////////////////////////////////////////////////////
172 ///Return the ID of the element
173 ///////////////////////////////////////////////////////////////////////////////
174 int SMDS_MeshElement::GetID() const
175 {
176         return myID;
177 }
178
179 bool operator<(const SMDS_MeshElement& e1, const SMDS_MeshElement& e2)
180 {
181         if(e1.GetType()!=e2.GetType()) return false;
182         switch(e1.GetType())
183         {
184         case SMDSAbs_Node:
185                 return static_cast<const SMDS_MeshNode &>(e1) <
186                         static_cast<const SMDS_MeshNode &>(e2);
187
188         case SMDSAbs_Edge:
189                 return static_cast<const SMDS_MeshEdge &>(e1) <
190                         static_cast<const SMDS_MeshEdge &>(e2);
191
192         case SMDSAbs_Face:
193                 return static_cast<const SMDS_MeshFace &>(e1) <
194                         static_cast<const SMDS_MeshFace &>(e2);
195
196         case SMDSAbs_Volume:
197                 return static_cast<const SMDS_MeshVolume &>(e1) <
198                         static_cast<const SMDS_MeshVolume &>(e2);
199
200         default : MESSAGE("Internal Error");
201         }
202         return false;
203 }
204
205 bool SMDS_MeshElement::IsValidIndex(const int ind) const
206 {
207   return ( ind>-1 && ind<NbNodes() );
208 }
209
210 const SMDS_MeshNode* SMDS_MeshElement::GetNode(const int ind) const
211 {
212   if ( ind >= 0 ) {
213     SMDS_ElemIteratorPtr it = nodesIterator();
214     for ( int i = 0; i < ind; ++i )
215       it->next();
216     if ( it->more() )
217       return static_cast<const SMDS_MeshNode*> (it->next());
218   }
219   return 0;
220 }
221
222 bool SMDS_MeshElement::IsQuadratic() const
223 {
224   return false;
225 }
226
227 bool SMDS_MeshElement::IsMediumNode(const SMDS_MeshNode* node) const
228 {
229   return false;
230 }
231
232 //================================================================================
233   /*!
234    * \brief Check if a node belongs to the element
235     * \param node - the node to check
236     * \retval int - node index within the element, -1 if not found
237    */
238 //================================================================================
239
240 int SMDS_MeshElement::GetNodeIndex( const SMDS_MeshNode* node ) const
241 {
242   SMDS_ElemIteratorPtr nIt = nodesIterator();
243   for ( int i = 0; nIt->more(); ++i )
244     if ( nIt->next() == node )
245       return i;
246   return -1;
247 }
248
249 SMDS_MeshCell::SMDS_MeshCell()
250 {
251   nbCells++;
252   myVtkID = -1;
253 };
254
255 SMDS_MeshCell::~SMDS_MeshCell()
256 {
257   nbCells--;
258 }