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