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