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