Salome HOME
Copyright update 2022
[modules/smesh.git] / src / SMDS / SMDS_MeshNode.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 : implementation of Salome mesh data structure
24 //
25 #ifdef _MSC_VER
26 #pragma warning(disable:4786)
27 #endif
28
29 #include "SMDS_MeshNode.hxx"
30
31 #include "SMDS_ElementFactory.hxx"
32 #include "SMDS_Mesh.hxx"
33 #include "SMDS_SetIterator.hxx"
34 #include "SMDS_SpacePosition.hxx"
35
36 #include <utilities.h>
37 #include <Utils_SALOME_Exception.hxx>
38 #include <cassert>
39 #include <smIdType.hxx>
40
41 #include <boost/make_shared.hpp>
42
43 void SMDS_MeshNode::init(double x, double y, double z)
44 {
45   SMDS_UnstructuredGrid * grid = getGrid();
46   vtkPoints *points = grid->GetPoints();
47   points->InsertPoint( GetVtkID(), x, y, z );
48   if ( grid->HasLinks() )
49     grid->GetLinks()->ResizeForPoint( GetVtkID() );
50 }
51
52 //=======================================================================
53 //function : RemoveInverseElement
54 //purpose  :
55 //=======================================================================
56
57 void SMDS_MeshNode::RemoveInverseElement(const SMDS_MeshElement * elem)
58 {
59   if ( getGrid()->HasLinks() )
60     getGrid()->RemoveReferenceToCell( GetVtkID(), elem->GetVtkID());
61 }
62
63 //=======================================================================
64 //function : Print
65 //purpose  :
66 //=======================================================================
67
68 void SMDS_MeshNode::Print(ostream & OS) const
69 {
70   OS << "Node <" << GetID() << "> : X = " << X() << " Y = "
71      << Y() << " Z = " << Z() << endl;
72 }
73
74 //=======================================================================
75 //function : SetPosition
76 //purpose  :
77 //=======================================================================
78
79 void SMDS_MeshNode::SetPosition(const SMDS_PositionPtr& aPos, int shapeID)
80 {
81   myHolder->SetPosition( this, aPos, shapeID );
82 }
83
84 //=======================================================================
85 //function : GetPosition
86 //purpose  : Return a position of this node on shape
87 //warning  : result is std::unique_ptr !
88 //=======================================================================
89
90 SMDS_PositionPtr SMDS_MeshNode::GetPosition() const
91 {
92   return myHolder->GetPosition( this );
93 }
94
95 //=======================================================================
96 /*!
97  * \brief Iterator on list of elements
98  */
99 //=======================================================================
100
101 namespace
102 {
103   struct InverseIterator: public SMDS_ElemIterator
104   {
105     const SMDS_Mesh*       myMesh;
106     size_t                 myIter;
107     std::vector<vtkIdType> myCellList;
108
109     InverseIterator(const SMDS_Mesh *   mesh = 0,
110                     const vtkIdType*    cells = 0,
111                     const int           ncells = 0,
112                     SMDSAbs_ElementType type = SMDSAbs_All)
113       : myMesh(mesh), myIter(0)
114     {
115       if ( ncells )
116       {
117         myCellList.reserve( ncells );
118         if (type == SMDSAbs_All)
119         {
120           myCellList.assign( cells, cells + ncells );
121         }
122         else
123         {
124           for (int i = 0; i < ncells; i++)
125           {
126             vtkIdType vtkId = cells[i];
127             smIdType smdsId = myMesh->FromVtkToSmds( vtkId );
128             const SMDS_MeshElement* elem = myMesh->FindElement( smdsId );
129             if ( elem->GetType() == type )
130             {
131               myCellList.push_back(vtkId);
132             }
133           }
134         }
135       }
136     }
137
138     bool more()
139     {
140       return ( myIter < myCellList.size() );
141     }
142
143     const SMDS_MeshElement* next()
144     {
145       vtkIdType vtkId = myCellList[ myIter++ ];
146       smIdType smdsId = myMesh->FromVtkToSmds( vtkId );
147       const SMDS_MeshElement* elem = myMesh->FindElement(smdsId);
148       if (!elem)
149       {
150         MESSAGE("InverseIterator problem Null element");
151         throw SALOME_Exception("InverseIterator problem Null element");
152       }
153       return elem;
154     }
155   };
156
157   //=======================================================================
158   /*!
159    * \brief Iterator on a node
160    */
161   //=======================================================================
162
163   template< class ELEM_ITERATOR >
164   struct Iterator : public ELEM_ITERATOR
165   {
166     typedef typename ELEM_ITERATOR::value_type element_type;
167     const SMDS_MeshNode* myNode;
168
169     Iterator( const SMDS_MeshNode* n ): myNode( n ) {}
170
171     virtual bool more()
172     {
173       return myNode;
174     }
175     virtual element_type next()
176     {
177       element_type res = static_cast<element_type>( myNode );
178       myNode = 0;
179       return res;
180     }
181   };
182 }
183
184 SMDS_ElemIteratorPtr SMDS_MeshNode::GetInverseElementIterator(SMDSAbs_ElementType type) const
185 {
186   if ( GetMesh()->NbElements() > 0 ) // avoid building links
187   {
188     vtkCellLinks::Link& l = getGrid()->GetLinks()->GetLink( GetVtkID() );
189     return boost::make_shared< InverseIterator >( GetMesh(), l.cells, l.ncells, type );
190   }
191   else
192   {
193     return boost::make_shared< InverseIterator >();
194   }
195 }
196
197 SMDS_ElemIteratorPtr SMDS_MeshNode::nodesIterator() const
198 {
199   return boost::make_shared< Iterator< SMDS_ElemIterator > >( this );
200 }
201
202 SMDS_NodeIteratorPtr SMDS_MeshNode::nodeIterator() const
203 {
204   return boost::make_shared< Iterator< SMDS_NodeIterator > >( this );
205 }
206
207 const SMDS_MeshNode* SMDS_MeshNode::GetNode(const int ind) const
208 {
209   return ind == 0 ? this : 0;
210 }
211
212 double* SMDS_MeshNode::getCoord() const
213 {
214   return getGrid()->GetPoint( GetVtkID() );
215 }
216
217 double SMDS_MeshNode::X() const
218 {
219   double *coord = getCoord();
220   return coord[0];
221 }
222
223 double SMDS_MeshNode::Y() const
224 {
225   double *coord = getCoord();
226   return coord[1];
227 }
228
229 double SMDS_MeshNode::Z() const
230 {
231   double *coord = getCoord();
232   return coord[2];
233 }
234
235 //================================================================================
236 /*!
237  * \brief thread safe getting coords
238  */
239 //================================================================================
240
241 void SMDS_MeshNode::GetXYZ(double xyz[3]) const
242 {
243   return getGrid()->GetPoint( GetVtkID(), xyz );
244 }
245
246 //================================================================================
247 void SMDS_MeshNode::setXYZ( double x, double y, double z )
248 {
249   vtkPoints *points = getGrid()->GetPoints();
250   points->InsertPoint( GetVtkID(), x, y, z );
251   //GetMesh()->adjustBoundingBox(x, y, z);
252   GetMesh()->setMyModified();
253 }
254
255 //=======================================================================
256 //function : AddInverseElement
257 //purpose  :
258 //=======================================================================
259 void SMDS_MeshNode::AddInverseElement( const SMDS_MeshElement* elem )
260 {
261   SMDS_UnstructuredGrid* grid = getGrid();
262   if ( grid->HasLinks() )
263   {
264     vtkCellLinks *Links = grid->GetLinks();
265     Links->ResizeCellList( GetVtkID(), 1 );
266     Links->AddCellReference( elem->GetVtkID(), GetVtkID() );
267   }
268 }
269
270 //=======================================================================
271 //function : ClearInverseElements
272 //purpose  :
273 //=======================================================================
274 void SMDS_MeshNode::ClearInverseElements()
275 {
276   getGrid()->ResizeCellList( GetVtkID(), 0);
277 }
278
279 //================================================================================
280 /*!
281  * \brief Count inverse elements of given type
282  */
283 //================================================================================
284
285 int SMDS_MeshNode::NbInverseElements(SMDSAbs_ElementType type) const
286 {
287   int nb = 0;
288   SMDS_Mesh *mesh = GetMesh();
289   if ( mesh->NbElements() > 0 ) // avoid building links
290   {
291     vtkCellLinks::Link& l = mesh->GetGrid()->GetLinks()->GetLink( GetVtkID() );
292
293     if ( type == SMDSAbs_All )
294       return l.ncells;
295
296     for ( int i = 0; i < l.ncells; i++ )
297     {
298       const SMDS_MeshElement* elem = mesh->FindElement( mesh->FromVtkToSmds( l.cells[i] ));
299       nb += ( elem->GetType() == type );
300     }
301   }
302   return nb;
303 }