Salome HOME
Merge branch 'master' into pre/penta18
[modules/smesh.git] / src / SMDS / SMDS_Mesh0DElement.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SMESH SMDS : implementaion of Salome mesh data structure
21 //  File   : SMDS_Mesh0DElement.cxx
22 //  Module : SMESH
23 //
24 #ifdef _MSC_VER
25 #pragma warning(disable:4786)
26 #endif
27
28 #include "SMDS_Mesh0DElement.hxx"
29 #include "SMDS_IteratorOfElements.hxx"
30 #include "SMDS_MeshNode.hxx"
31 #include "SMDS_Mesh.hxx"
32
33 #include "utilities.h"
34
35 using namespace std;
36
37 //=======================================================================
38 //function : SMDS_Mesh0DElement
39 //purpose  :
40 //=======================================================================
41 SMDS_Mesh0DElement::SMDS_Mesh0DElement (const SMDS_MeshNode * node)
42 {
43   myNode = node;
44 }
45
46 //=======================================================================
47 //function : Print
48 //purpose  :
49 //=======================================================================
50 void SMDS_Mesh0DElement::Print (ostream & OS) const
51 {
52   OS << "0D Element <" << GetID() << "> : (" << myNode << ") " << endl;
53 }
54
55 //=======================================================================
56 //function : NbNodes
57 //purpose  :
58 //=======================================================================
59 int SMDS_Mesh0DElement::NbNodes() const
60 {
61   return 1;
62 }
63
64 //=======================================================================
65 //function : NbEdges
66 //purpose  :
67 //=======================================================================
68 int SMDS_Mesh0DElement::NbEdges() const
69 {
70   return 0;
71 }
72
73 //=======================================================================
74 //function : GetType
75 //purpose  :
76 //=======================================================================
77 SMDSAbs_ElementType SMDS_Mesh0DElement::GetType() const
78 {
79   return SMDSAbs_0DElement;
80 }
81
82 vtkIdType SMDS_Mesh0DElement::GetVtkType() const
83 {
84   return VTK_VERTEX;
85 }
86
87 //=======================================================================
88 //function : elementsIterator
89 //purpose  :
90 //=======================================================================
91 class SMDS_Mesh0DElement_MyNodeIterator: public SMDS_ElemIterator
92 {
93   const SMDS_MeshNode * myNode;
94   int myIndex;
95  public:
96   SMDS_Mesh0DElement_MyNodeIterator(const SMDS_MeshNode * node):
97     myNode(node),myIndex(0) {}
98
99   bool more()
100   {
101     return myIndex < 1;
102   }
103
104   const SMDS_MeshElement* next()
105   {
106     myIndex++;
107     if (myIndex == 1)
108       return myNode;
109     return NULL;
110   }
111 };
112
113 SMDS_ElemIteratorPtr SMDS_Mesh0DElement::elementsIterator (SMDSAbs_ElementType type) const
114 {
115   switch(type)
116   {
117   case SMDSAbs_0DElement:
118     return SMDS_MeshElement::elementsIterator(SMDSAbs_0DElement);
119   case SMDSAbs_Node:
120     return SMDS_ElemIteratorPtr(new SMDS_Mesh0DElement_MyNodeIterator(myNode));
121   default:
122     return SMDS_ElemIteratorPtr
123       (new SMDS_IteratorOfElements
124        (this,type, SMDS_ElemIteratorPtr(new SMDS_Mesh0DElement_MyNodeIterator(myNode))));
125   }
126 }
127
128 /*!
129  * \brief Return node by its index
130  * \param ind - node index
131  * \retval const SMDS_MeshNode* - the node
132  */
133 const SMDS_MeshNode* SMDS_Mesh0DElement::GetNode(const int ind) const
134 {
135   if (ind == 0)
136     return myNode;
137   return NULL;
138 }
139
140 //=======================================================================
141 //function : ChangeNode
142 //purpose  :
143 //=======================================================================
144 bool SMDS_Mesh0DElement::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
145 {
146   if ( nbNodes == 1 )
147   {
148     vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
149     vtkIdType npts = 0;
150     vtkIdType* pts = 0;
151     grid->GetCellPoints(myVtkID, npts, pts);
152     if (nbNodes != npts)
153     {
154       MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
155       return false;
156     }
157     myNode = nodes[0];
158     pts[0] = myNode->getVtkId();
159
160     SMDS_Mesh::_meshList[myMeshId]->setMyModified();
161     return true;
162   }
163   return false;
164 }