Salome HOME
Update copyright
[modules/smesh.git] / src / SMDS / SMDS_VtkFace.cxx
1 // Copyright (C) 2010-2011  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.
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_VtkFace.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
29 using namespace std;
30
31 SMDS_VtkFace::SMDS_VtkFace()
32 {
33 }
34
35 SMDS_VtkFace::SMDS_VtkFace(std::vector<vtkIdType> nodeIds, SMDS_Mesh* mesh)
36 {
37   init(nodeIds, mesh);
38 }
39
40 SMDS_VtkFace::~SMDS_VtkFace()
41 {
42 }
43
44 void SMDS_VtkFace::init(std::vector<vtkIdType> nodeIds, SMDS_Mesh* mesh)
45 {
46   SMDS_MeshFace::init();
47   vtkUnstructuredGrid* grid = mesh->getGrid();
48   myMeshId = mesh->getMeshId();
49   vtkIdType aType = VTK_TRIANGLE;
50   switch (nodeIds.size())
51   {
52     case 3:
53       aType = VTK_TRIANGLE;
54       break;
55     case 4:
56       aType = VTK_QUAD;
57       break;
58     case 6:
59       aType = VTK_QUADRATIC_TRIANGLE;
60       break;
61     case 8:
62       aType = VTK_QUADRATIC_QUAD;
63       break;
64     default:
65       aType = VTK_POLYGON;
66       break;
67   }
68   myVtkID = grid->InsertNextLinkedCell(aType, nodeIds.size(), &nodeIds[0]);
69   mesh->setMyModified();
70   //MESSAGE("SMDS_VtkFace::init myVtkID " << myVtkID);
71 }
72
73 void SMDS_VtkFace::initPoly(std::vector<vtkIdType> nodeIds, SMDS_Mesh* mesh)
74 {
75   SMDS_MeshFace::init();
76   vtkUnstructuredGrid* grid = mesh->getGrid();
77   myMeshId = mesh->getMeshId();
78   myVtkID = grid->InsertNextLinkedCell(VTK_POLYGON, nodeIds.size(), &nodeIds[0]);
79   mesh->setMyModified();
80 }
81
82 bool SMDS_VtkFace::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
83 {
84   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
85   vtkIdType npts = 0;
86   vtkIdType* pts = 0;
87   grid->GetCellPoints(myVtkID, npts, pts);
88   if (nbNodes != npts)
89     {
90       MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
91       return false;
92     }
93   for (int i = 0; i < nbNodes; i++)
94     {
95       pts[i] = nodes[i]->getVtkId();
96     }
97   SMDS_Mesh::_meshList[myMeshId]->setMyModified();
98   return true;
99 }
100
101 void SMDS_VtkFace::Print(std::ostream & OS) const
102 {
103   OS << "face <" << GetID() << "> : ";
104 }
105
106 int SMDS_VtkFace::NbEdges() const
107 {
108   // TODO quadratic polygons ?
109   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
110   vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
111   int nbEdges = 3;
112   switch (aVtkType)
113   {
114     case VTK_TRIANGLE:
115     case VTK_QUADRATIC_TRIANGLE:
116       nbEdges = 3;
117       break;
118     case VTK_QUAD:
119     case VTK_QUADRATIC_QUAD:
120       nbEdges = 4;
121       break;
122     case VTK_POLYGON:
123     default:
124       nbEdges = grid->GetCell(myVtkID)->GetNumberOfPoints();
125       break;
126   }
127   return nbEdges;
128 }
129
130 int SMDS_VtkFace::NbFaces() const
131 {
132   return 1;
133 }
134
135 int SMDS_VtkFace::NbNodes() const
136 {
137   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
138   int nbPoints = grid->GetCell(myVtkID)->GetNumberOfPoints();
139   return nbPoints;
140 }
141
142 /*!
143  * \brief Return node by its index
144  * \param ind - node index
145  * \retval const SMDS_MeshNode* - the node
146  */
147 const SMDS_MeshNode*
148 SMDS_VtkFace::GetNode(const int ind) const
149 {
150   return SMDS_MeshElement::GetNode(ind); // --- a optimiser !
151 }
152
153 bool SMDS_VtkFace::IsQuadratic() const
154 {
155   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
156   vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
157   // TODO quadratic polygons ?
158   switch (aVtkType)
159   {
160     case VTK_QUADRATIC_TRIANGLE:
161     case VTK_QUADRATIC_QUAD:
162       return true;
163       break;
164     default:
165       return false;
166   }
167 }
168
169 bool SMDS_VtkFace::IsPoly() const
170 {
171   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
172   vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
173   return (aVtkType == VTK_POLYGON);
174 }
175
176 bool SMDS_VtkFace::IsMediumNode(const SMDS_MeshNode* node) const
177 {
178   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
179   vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
180   int rankFirstMedium = 0;
181   switch (aVtkType)
182   {
183     case VTK_QUADRATIC_TRIANGLE:
184       rankFirstMedium = 3; // medium nodes are of rank 3,4,5
185       break;
186     case VTK_QUADRATIC_QUAD:
187       rankFirstMedium = 4; // medium nodes are of rank 4,5,6,7
188       break;
189     default:
190       //MESSAGE("wrong element type " << aVtkType);
191       return false;
192   }
193   vtkIdType npts = 0;
194   vtkIdType* pts = 0;
195   grid->GetCellPoints(myVtkID, npts, pts);
196   vtkIdType nodeId = node->getVtkId();
197   for (int rank = 0; rank < npts; rank++)
198     {
199       if (pts[rank] == nodeId)
200         {
201           //MESSAGE("rank " << rank << " is medium node " << (rank < rankFirstMedium));
202           if (rank < rankFirstMedium)
203             return false;
204           else
205             return true;
206         }
207     }
208   //throw SALOME_Exception(LOCALIZED("node does not belong to this element"));
209   MESSAGE("======================================================");
210   MESSAGE("= IsMediumNode: node does not belong to this element =");
211   MESSAGE("======================================================");
212   return false;
213 }
214
215 SMDSAbs_EntityType SMDS_VtkFace::GetEntityType() const
216 {
217   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
218   vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
219   SMDSAbs_EntityType aType = SMDSEntity_Polygon;
220   switch (aVtkType)
221   {
222     case VTK_TRIANGLE:
223       aType = SMDSEntity_Triangle;
224       break;
225     case VTK_QUAD:
226       aType = SMDSEntity_Quadrangle;
227       break;
228     case VTK_QUADRATIC_TRIANGLE:
229       aType = SMDSEntity_Quad_Triangle;
230       break;
231     case VTK_QUADRATIC_QUAD:
232       aType = SMDSEntity_Quad_Quadrangle;
233       break;
234     default:
235       aType = SMDSEntity_Polygon;
236   }
237   return aType;
238 }
239
240 vtkIdType SMDS_VtkFace::GetVtkType() const
241 {
242   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
243   vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
244   return aVtkType;
245 }
246
247 SMDS_ElemIteratorPtr SMDS_VtkFace::elementsIterator(SMDSAbs_ElementType type) const
248 {
249   switch (type)
250   {
251     case SMDSAbs_Node:
252       return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
253     default:
254       MESSAGE("ERROR : Iterator not implemented")
255       ;
256       return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
257   }
258 }
259
260 SMDS_ElemIteratorPtr SMDS_VtkFace::nodesIteratorToUNV() const
261 {
262   return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
263 }
264
265 SMDS_ElemIteratorPtr SMDS_VtkFace::interlacedNodesElemIterator() const
266 {
267   return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
268 }
269
270 //! change only the first node, used for temporary triangles in quadrangle to triangle adaptor
271 void SMDS_VtkFace::ChangeApex(SMDS_MeshNode* node)
272 {
273   vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
274   vtkIdType npts = 0;
275   vtkIdType* pts = 0;
276   grid->GetCellPoints(myVtkID, npts, pts);
277   grid->RemoveReferenceToCell(pts[0], myVtkID);
278   pts[0] = node->getVtkId();
279   node->AddInverseElement(this),
280   SMDS_Mesh::_meshList[myMeshId]->setMyModified();
281 }