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