Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / SMDS / SMDS_VtkEdge.cxx
1 // Copyright (C) 2010-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 #include "SMDS_VtkEdge.hxx"
21 #include "SMDS_MeshNode.hxx"
22 #include "SMDS_Mesh.hxx"
23 #include "SMDS_VtkCellIterator.hxx"
24
25 #include "utilities.h"
26
27 #include <vector>
28 #include <cassert>
29
30 using namespace std;
31
32 SMDS_VtkEdge::SMDS_VtkEdge()
33 {
34 }
35
36 SMDS_VtkEdge::SMDS_VtkEdge(std::vector<vtkIdType>& nodeIds, SMDS_Mesh* mesh)
37 {
38   init(nodeIds, mesh);
39 }
40
41 SMDS_VtkEdge::~SMDS_VtkEdge()
42 {
43 }
44
45 void SMDS_VtkEdge::init(std::vector<vtkIdType>& nodeIds, SMDS_Mesh* mesh)
46 {
47   SMDS_MeshEdge::init();
48   myMeshId = mesh->getMeshId();
49   vtkIdType aType = ( nodeIds.size() == 3 ) ? VTK_QUADRATIC_EDGE : VTK_LINE;
50   myVtkID = mesh->getGrid()->InsertNextLinkedCell(aType, nodeIds.size(), &nodeIds[0]);
51   mesh->setMyModified();
52 }
53
54 bool SMDS_VtkEdge::ChangeNodes(const SMDS_MeshNode * node1, const SMDS_MeshNode * node2)
55 {
56   const SMDS_MeshNode* nodes[] = { node1, node2 };
57   SMDS_Mesh::_meshList[myMeshId]->setMyModified();
58   return ChangeNodes(nodes, 2);
59 }
60
61 bool SMDS_VtkEdge::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
62 {
63   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
64   vtkIdType npts = 0;
65   vtkIdType* pts = 0;
66   grid->GetCellPoints(myVtkID, npts, pts);
67   if (nbNodes != npts)
68   {
69     MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
70     return false;
71   }
72   for (int i = 0; i < nbNodes; i++)
73   {
74     pts[i] = nodes[i]->getVtkId();
75   }
76   SMDS_Mesh::_meshList[myMeshId]->setMyModified();
77   return true;
78 }
79
80 bool SMDS_VtkEdge::IsMediumNode(const SMDS_MeshNode* node) const
81 {
82   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
83   vtkIdType npts = 0;
84   vtkIdType* pts = 0;
85   grid->GetCellPoints(myVtkID, npts, pts);
86   return ((npts == 3) && (node->getVtkId() == pts[2]));
87 }
88
89 void SMDS_VtkEdge::Print(std::ostream & OS) const
90 {
91   OS << "edge <" << GetID() << "> : ";
92 }
93
94 int SMDS_VtkEdge::NbNodes() const
95 {
96   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
97   vtkIdType *pts, npts;
98   grid->GetCellPoints( myVtkID, npts, pts );
99   assert(npts >= 2);
100   return npts;
101 }
102
103 int SMDS_VtkEdge::NbEdges() const
104 {
105   return 1;
106 }
107
108 SMDSAbs_EntityType SMDS_VtkEdge::GetEntityType() const
109 {
110   if (NbNodes() == 2)
111     return SMDSEntity_Edge;
112   else
113     return SMDSEntity_Quad_Edge;
114 }
115
116 vtkIdType SMDS_VtkEdge::GetVtkType() const
117 {
118   if (NbNodes() == 2)
119     return VTK_LINE;
120   else
121     return VTK_QUADRATIC_EDGE;
122
123 }
124
125 /*!
126  * \brief Return node by its index
127  * \param ind - node index
128  * \retval const SMDS_MeshNode* - the node
129  */
130 const SMDS_MeshNode*
131 SMDS_VtkEdge::GetNode(const int ind) const
132 {
133   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
134   vtkIdType npts, *pts;
135   grid->GetCellPoints( this->myVtkID, npts, pts );
136   return SMDS_Mesh::_meshList[myMeshId]->FindNodeVtk( pts[ ind ]);
137 }
138
139 bool SMDS_VtkEdge::IsQuadratic() const
140 {
141   if (this->NbNodes() > 2)
142     return true;
143   else
144     return false;
145 }
146
147 SMDS_ElemIteratorPtr SMDS_VtkEdge::elementsIterator(SMDSAbs_ElementType type) const
148 {
149   switch (type)
150   {
151     case SMDSAbs_Node:
152       return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
153     default:
154       MESSAGE("ERROR : Iterator not implemented");
155       return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
156   }
157 }
158
159 SMDS_NodeIteratorPtr SMDS_VtkEdge::nodesIteratorToUNV() const
160 {
161   return SMDS_NodeIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
162 }
163
164 SMDS_NodeIteratorPtr SMDS_VtkEdge::interlacedNodesIterator() const
165 {
166   return nodesIteratorToUNV();
167 }