Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/smesh.git] / src / SMDS / SMDS_MeshElement.cxx
1 //  SMESH SMDS : implementaion of Salome mesh data structure
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21
22 #include "SMDS_MeshElement.hxx"
23 #include "SMDS_MeshNode.hxx"
24 #include "SMDS_MeshEdge.hxx"
25 #include "SMDS_MeshFace.hxx"
26 #include "SMDS_MeshVolume.hxx"
27 #include "utilities.h"
28
29 using namespace std;
30
31 SMDS_MeshElement::SMDS_MeshElement(int ID):myID(ID)
32 {
33 }
34
35 void SMDS_MeshElement::Print(ostream & OS) const
36 {
37         OS << "dump of mesh element" << endl;
38 }
39
40 ostream & operator <<(ostream & OS, const SMDS_MeshElement * ME)
41 {
42         ME->Print(OS);
43         return OS;
44 }
45
46 ///////////////////////////////////////////////////////////////////////////////
47 /// Create an iterator which iterate on nodes owned by the element.
48 /// This method call elementsIterator().
49 ///////////////////////////////////////////////////////////////////////////////
50 SMDS_ElemIteratorPtr SMDS_MeshElement::nodesIterator() const
51 {
52         return elementsIterator(SMDSAbs_Node);
53 }
54
55 ///////////////////////////////////////////////////////////////////////////////
56 /// Create an iterator which iterate on edges linked with or owned by the element.
57 /// This method call elementsIterator().
58 ///////////////////////////////////////////////////////////////////////////////
59 SMDS_ElemIteratorPtr SMDS_MeshElement::edgesIterator() const
60 {
61         return elementsIterator(SMDSAbs_Edge);
62 }
63
64 ///////////////////////////////////////////////////////////////////////////////
65 /// Create an iterator which iterate on faces linked with or owned by the element.
66 /// This method call elementsIterator().
67 ///////////////////////////////////////////////////////////////////////////////
68 SMDS_ElemIteratorPtr SMDS_MeshElement::facesIterator() const
69 {
70         return elementsIterator(SMDSAbs_Face);
71 }
72
73 ///////////////////////////////////////////////////////////////////////////////
74 ///Return The number of nodes owned by the current element
75 ///////////////////////////////////////////////////////////////////////////////
76 int SMDS_MeshElement::NbNodes() const
77 {
78         int nbnodes=0;
79         SMDS_ElemIteratorPtr it=nodesIterator();
80         while(it->more())
81         {
82                 it->next();
83                 nbnodes++;
84         }
85         return nbnodes;
86 }
87
88 ///////////////////////////////////////////////////////////////////////////////
89 ///Return the number of edges owned by or linked with the current element
90 ///////////////////////////////////////////////////////////////////////////////
91 int SMDS_MeshElement::NbEdges() const
92 {
93         int nbedges=0;
94         SMDS_ElemIteratorPtr it=edgesIterator();
95         while(it->more())
96         {
97                 it->next();
98                 nbedges++;
99         }
100         return nbedges;
101 }
102
103 ///////////////////////////////////////////////////////////////////////////////
104 ///Return the number of faces owned by or linked with the current element
105 ///////////////////////////////////////////////////////////////////////////////
106 int SMDS_MeshElement::NbFaces() const
107 {
108         int nbfaces=0;
109         SMDS_ElemIteratorPtr it=facesIterator();
110         while(it->more())
111         {
112                 it->next();
113                 nbfaces++;
114         }
115         return nbfaces;
116 }
117
118 ///////////////////////////////////////////////////////////////////////////////
119 ///Create an iterator which iterate on elements linked with the current element.
120 ///@param type The of elements on which you want to iterate
121 ///@return A smart pointer to iterator, you are not to take care of freeing memory
122 ///////////////////////////////////////////////////////////////////////////////
123 class SMDS_MeshElement_MyIterator:public SMDS_ElemIterator
124 {
125   const SMDS_MeshElement * myElement;
126   bool myMore;
127  public:
128   SMDS_MeshElement_MyIterator(const SMDS_MeshElement * element):
129     myElement(element),myMore(true) {}
130
131   bool more()
132   {
133     return myMore;
134   }
135
136   const SMDS_MeshElement* next()
137   {
138     myMore=false;
139     return myElement;   
140   }     
141 };
142 SMDS_ElemIteratorPtr SMDS_MeshElement::
143         elementsIterator(SMDSAbs_ElementType type) const
144 {
145         /** @todo Check that iterator in the child classes return elements
146         in the same order for each different implementation (i.e: SMDS_VolumeOfNodes
147         and SMDS_VolumeOfFaces */
148         
149         if(type==GetType())
150           return SMDS_ElemIteratorPtr(new SMDS_MeshElement_MyIterator(this));
151         else 
152         {
153           MESSAGE("Iterator not implemented");
154           return SMDS_ElemIteratorPtr((SMDS_ElemIterator*)NULL);
155         }
156 }
157
158 ///////////////////////////////////////////////////////////////////////////////
159 ///Return the ID of the element
160 ///////////////////////////////////////////////////////////////////////////////
161 int SMDS_MeshElement::GetID() const
162 {
163         return myID;
164 }
165
166 bool operator<(const SMDS_MeshElement& e1, const SMDS_MeshElement& e2)
167 {
168         if(e1.GetType()!=e2.GetType()) return false;
169         switch(e1.GetType())
170         {
171         case SMDSAbs_Node:
172                 return static_cast<const SMDS_MeshNode &>(e1) <
173                         static_cast<const SMDS_MeshNode &>(e2);
174
175         case SMDSAbs_Edge:
176                 return static_cast<const SMDS_MeshEdge &>(e1) <
177                         static_cast<const SMDS_MeshEdge &>(e2);
178
179         case SMDSAbs_Face:
180                 return static_cast<const SMDS_MeshFace &>(e1) <
181                         static_cast<const SMDS_MeshFace &>(e2);
182
183         case SMDSAbs_Volume:
184                 return static_cast<const SMDS_MeshVolume &>(e1) <
185                         static_cast<const SMDS_MeshVolume &>(e2);
186
187         default : MESSAGE("Internal Error");
188         }
189         return false;
190 }