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