Salome HOME
Merge from V5_1_4_BR 07/05/2010
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.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_MeshNode.hxx"
30 #include "SMDS_SpacePosition.hxx"
31 #include "SMDS_IteratorOfElements.hxx"
32
33 using namespace std;
34
35 //=======================================================================
36 //function : SMDS_MeshNode
37 //purpose  : 
38 //=======================================================================
39
40 SMDS_MeshNode::SMDS_MeshNode(double x, double y, double z):
41         myX(x), myY(y), myZ(z),
42         myPosition(SMDS_SpacePosition::originSpacePosition())
43 {
44 }
45
46 //=======================================================================
47 //function : RemoveInverseElement
48 //purpose  : 
49 //=======================================================================
50
51 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * parent)
52 {
53   NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
54   while (it.More()) {
55     const SMDS_MeshElement* elem = it.Value();
56     if (elem == parent)
57       myInverseElements.Remove(it);
58     else
59       it.Next();
60   }
61 }
62
63 //=======================================================================
64 //function : Print
65 //purpose  : 
66 //=======================================================================
67
68 void SMDS_MeshNode::Print(ostream & OS) const
69 {
70         OS << "Node <" << GetID() << "> : X = " << myX << " Y = "
71                 << myY << " Z = " << myZ << endl;
72 }
73
74 //=======================================================================
75 //function : SetPosition
76 //purpose  : 
77 //=======================================================================
78
79 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos)
80 {
81         myPosition = aPos;
82 }
83
84 //=======================================================================
85 //function : GetPosition
86 //purpose  : 
87 //=======================================================================
88
89 const SMDS_PositionPtr& SMDS_MeshNode::GetPosition() const
90 {
91         return myPosition;
92 }
93
94 //=======================================================================
95 /*!
96  * \brief Iterator on list of elements
97  */
98 //=======================================================================
99
100 class SMDS_MeshNode_MyInvIterator:public SMDS_ElemIterator
101 {
102   NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
103   SMDSAbs_ElementType                                 myType;
104  public:
105   SMDS_MeshNode_MyInvIterator(const NCollection_List<const SMDS_MeshElement*>& s,
106                               SMDSAbs_ElementType type):
107     myIterator(s), myType(type)
108   {}
109
110   bool more()
111   {
112     if ( myType != SMDSAbs_All ) {
113       while ( myIterator.More() && myIterator.Value()->GetType() != myType)
114         myIterator.Next();
115     }
116     return myIterator.More() != Standard_False;
117   }
118
119   const SMDS_MeshElement* next()
120   {
121     if ( !more() ) return 0;
122     const SMDS_MeshElement* current=myIterator.Value();
123     myIterator.Next();
124     return current;
125   }     
126 };
127
128 SMDS_ElemIteratorPtr SMDS_MeshNode::
129         GetInverseElementIterator(SMDSAbs_ElementType type) const
130 {
131   return SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyInvIterator(myInverseElements,type));
132 }
133
134 // Same as GetInverseElementIterator but the create iterator only return
135 // wanted type elements.
136 class SMDS_MeshNode_MyIterator:public SMDS_ElemIterator
137 {
138   NCollection_List<const SMDS_MeshElement*> mySet;
139   NCollection_List<const SMDS_MeshElement*>::Iterator myIterator;
140  public:
141   SMDS_MeshNode_MyIterator(SMDSAbs_ElementType type,
142                            const NCollection_List<const SMDS_MeshElement*>& s)
143   {
144     const SMDS_MeshElement * e;
145     bool toInsert;
146     NCollection_List<const SMDS_MeshElement*>::Iterator it(s);
147     for(; it.More(); it.Next())
148     {
149       e=it.Value();
150       switch(type)
151       {
152       case SMDSAbs_Edge: toInsert=true; break;
153       case SMDSAbs_Face: toInsert=(e->GetType()!=SMDSAbs_Edge); break;
154       case SMDSAbs_Volume: toInsert=(e->GetType()==SMDSAbs_Volume); break;
155       }
156       if(toInsert) mySet.Append(e);
157     }
158     myIterator.Init(mySet);
159   }
160
161   bool more()
162   {
163     return myIterator.More() != Standard_False;
164   }
165
166   const SMDS_MeshElement* next()
167   {
168     const SMDS_MeshElement* current=myIterator.Value();
169     myIterator.Next();
170     return current;
171   }
172 };
173
174 SMDS_ElemIteratorPtr SMDS_MeshNode::
175         elementsIterator(SMDSAbs_ElementType type) const
176 {
177   if(type==SMDSAbs_Node)
178     return SMDS_MeshElement::elementsIterator(SMDSAbs_Node); 
179   else
180     return SMDS_ElemIteratorPtr
181       (new SMDS_IteratorOfElements
182        (this,type,
183         SMDS_ElemIteratorPtr(new SMDS_MeshNode_MyIterator(type, myInverseElements))));
184 }
185
186 int SMDS_MeshNode::NbNodes() const
187 {
188         return 1;
189 }
190
191 double SMDS_MeshNode::X() const
192 {
193         return myX;
194 }
195
196 double SMDS_MeshNode::Y() const
197 {
198         return myY;
199 }
200
201 double SMDS_MeshNode::Z() const
202 {
203         return myZ;
204 }
205
206 void SMDS_MeshNode::setXYZ(double x, double y, double z)
207 {
208         myX=x;
209         myY=y;
210         myZ=z;  
211 }
212
213 SMDSAbs_ElementType SMDS_MeshNode::GetType() const
214 {
215         return SMDSAbs_Node;
216 }
217
218 //=======================================================================
219 //function : AddInverseElement
220 //purpose  :
221 //=======================================================================
222 void SMDS_MeshNode::AddInverseElement(const SMDS_MeshElement* ME)
223 {
224   NCollection_List<const SMDS_MeshElement*>::Iterator it(myInverseElements);
225   for (; it.More(); it.Next()) {
226     const SMDS_MeshElement* elem = it.Value();
227     if (elem == ME)
228       return;
229   }
230   myInverseElements.Append(ME);
231 }
232
233 //=======================================================================
234 //function : ClearInverseElements
235 //purpose  :
236 //=======================================================================
237 void SMDS_MeshNode::ClearInverseElements()
238 {
239   myInverseElements.Clear();
240 }
241
242 bool SMDS_MeshNode::emptyInverseElements()
243 {
244   return myInverseElements.IsEmpty() != Standard_False;
245 }
246
247 //================================================================================
248 /*!
249  * \brief Count inverse elements of given type
250  */
251 //================================================================================
252
253 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
254 {
255   if ( type == SMDSAbs_All )
256     return myInverseElements.Extent();
257   int nb = 0;
258   NCollection_List<const SMDS_MeshElement*>::Iterator it( myInverseElements );
259   for ( ; it.More(); it.Next() )
260     if ( it.Value()->GetType() == type )
261       nb++;
262   return nb;
263 }
264
265 ///////////////////////////////////////////////////////////////////////////////
266 /// To be used with STL set
267 ///////////////////////////////////////////////////////////////////////////////
268 bool operator<(const SMDS_MeshNode& e1, const SMDS_MeshNode& e2)
269 {
270         return e1.GetID()<e2.GetID();
271         /*if(e1.myX<e2.myX) return true;
272         else if(e1.myX==e2.myX)
273         {
274                 if(e1.myY<e2.myY) return true;
275                 else if(e1.myY==e2.myY) return (e1.myZ<e2.myZ);
276                 else return false;
277         }
278         else return false;*/
279 }
280