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