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