Salome HOME
f365a30f213d078c1d2795da8887e32fa97a8550
[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   vtkIdType *pts, npts;
103   grid->GetCellPoints( myVtkID, npts, pts );
104   assert(npts >= 2);
105   return npts;
106 }
107
108 int SMDS_VtkEdge::NbEdges() const
109 {
110   return 1;
111 }
112
113 SMDSAbs_EntityType SMDS_VtkEdge::GetEntityType() const
114 {
115   if (NbNodes() == 2)
116     return SMDSEntity_Edge;
117   else
118     return SMDSEntity_Quad_Edge;
119 }
120
121 vtkIdType SMDS_VtkEdge::GetVtkType() const
122 {
123   if (NbNodes() == 2)
124     return VTK_LINE;
125   else
126     return VTK_QUADRATIC_EDGE;
127
128 }
129
130 /*!
131  * \brief Return node by its index
132  * \param ind - node index
133  * \retval const SMDS_MeshNode* - the node
134  */
135 const SMDS_MeshNode*
136 SMDS_VtkEdge::GetNode(const int ind) const
137 {
138   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
139   vtkIdType npts, *pts;
140   grid->GetCellPoints( this->myVtkID, npts, pts );
141   return SMDS_Mesh::_meshList[myMeshId]->FindNodeVtk( pts[ ind ]);
142 }
143
144 bool SMDS_VtkEdge::IsQuadratic() const
145 {
146   if (this->NbNodes() > 2)
147     return true;
148   else
149     return false;
150 }
151
152 SMDS_ElemIteratorPtr SMDS_VtkEdge::elementsIterator(SMDSAbs_ElementType type) const
153 {
154   switch (type)
155   {
156     case SMDSAbs_Node:
157       return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
158     default:
159       MESSAGE("ERROR : Iterator not implemented");
160       return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
161   }
162 }
163
164 SMDS_NodeIteratorPtr SMDS_VtkEdge::nodesIteratorToUNV() const
165 {
166   return SMDS_NodeIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
167 }
168
169 SMDS_NodeIteratorPtr SMDS_VtkEdge::interlacedNodesIterator() const
170 {
171   return nodesIteratorToUNV();
172 }