1 // Copyright (C) 2010-2011 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SMDS_VtkVolume.hxx"
21 #include "SMDS_MeshNode.hxx"
22 #include "SMDS_Mesh.hxx"
23 #include "SMDS_VtkCellIterator.hxx"
25 #include "utilities.h"
29 SMDS_VtkVolume::SMDS_VtkVolume()
33 SMDS_VtkVolume::SMDS_VtkVolume(std::vector<vtkIdType> nodeIds, SMDS_Mesh* mesh)
38 * typed used are vtk types (@see vtkCellType.h)
39 * see GetEntityType() for conversion in SMDS type (@see SMDSAbs_ElementType.hxx)
41 void SMDS_VtkVolume::init(std::vector<vtkIdType> nodeIds, SMDS_Mesh* mesh)
43 SMDS_MeshVolume::init();
44 vtkUnstructuredGrid* grid = mesh->getGrid();
45 myMeshId = mesh->getMeshId();
46 vtkIdType aType = VTK_TETRA;
47 switch (nodeIds.size())
59 aType = VTK_HEXAHEDRON;
62 aType = VTK_QUADRATIC_TETRA;
65 aType = VTK_QUADRATIC_PYRAMID;
68 aType = VTK_QUADRATIC_WEDGE;
71 aType = VTK_QUADRATIC_HEXAHEDRON;
74 aType = VTK_HEXAHEDRON;
77 myVtkID = grid->InsertNextLinkedCell(aType, nodeIds.size(), &nodeIds[0]);
78 mesh->setMyModified();
79 //MESSAGE("SMDS_VtkVolume::init myVtkID " << myVtkID);
82 //#ifdef VTK_HAVE_POLYHEDRON
83 void SMDS_VtkVolume::initPoly(std::vector<vtkIdType> nodeIds, std::vector<int> nbNodesPerFace, SMDS_Mesh* mesh)
85 SMDS_MeshVolume::init();
86 //MESSAGE("SMDS_VtkVolume::initPoly");
87 SMDS_UnstructuredGrid* grid = mesh->getGrid();
89 this->gravityCenter(grid, &nodeIds[0], nodeIds.size(), ¢er[0]);
90 vector<vtkIdType> ptIds;
92 vtkIdType nbFaces = nbNodesPerFace.size();
94 for (int i = 0; i < nbFaces; i++)
96 int nf = nbNodesPerFace[i];
101 grid->GetPoints()->GetPoint(nodeIds[k], a);
102 grid->GetPoints()->GetPoint(nodeIds[k + 1], b);
103 grid->GetPoints()->GetPoint(nodeIds[k + 2], c);
104 bool isFaceForward = this->isForward(a, b, c, center);
105 //MESSAGE("isFaceForward " << i << " " << isFaceForward);
106 vtkIdType *facePts = &nodeIds[k];
108 for (int n = 0; n < nf; n++)
109 ptIds.push_back(facePts[n]);
111 for (int n = nf - 1; n >= 0; n--)
112 ptIds.push_back(facePts[n]);
115 myVtkID = grid->InsertNextLinkedCell(VTK_POLYHEDRON, nbFaces, &ptIds[0]);
116 mesh->setMyModified();
120 bool SMDS_VtkVolume::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
122 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
125 grid->GetCellPoints(myVtkID, npts, pts);
128 MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
131 for (int i = 0; i < nbNodes; i++)
133 pts[i] = nodes[i]->getVtkId();
135 SMDS_Mesh::_meshList[myMeshId]->setMyModified();
140 * Reorder in VTK order a list of nodes given in SMDS order.
141 * To be used before ChangeNodes: lists are given or computed in SMDS order.
143 bool SMDS_VtkVolume::vtkOrder(const SMDS_MeshNode* nodes[], const int nbNodes)
145 if (nbNodes != this->NbNodes())
147 MESSAGE("vtkOrder, wrong number of nodes " << nbNodes << " instead of "<< this->NbNodes());
150 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
151 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
155 this->exchange(nodes, 1, 2);
157 case VTK_QUADRATIC_TETRA:
158 this->exchange(nodes, 1, 2);
159 this->exchange(nodes, 4, 6);
160 this->exchange(nodes, 8, 9);
163 this->exchange(nodes, 1, 3);
167 case VTK_QUADRATIC_PYRAMID:
168 this->exchange(nodes, 1, 3);
169 this->exchange(nodes, 5, 8);
170 this->exchange(nodes, 6, 7);
171 this->exchange(nodes, 10, 12);
173 case VTK_QUADRATIC_WEDGE:
176 this->exchange(nodes, 1, 3);
177 this->exchange(nodes, 5, 7);
179 case VTK_QUADRATIC_HEXAHEDRON:
180 this->exchange(nodes, 1, 3);
181 this->exchange(nodes, 5, 7);
182 this->exchange(nodes, 8, 11);
183 this->exchange(nodes, 9, 10);
184 this->exchange(nodes, 12, 15);
185 this->exchange(nodes, 13, 14);
186 this->exchange(nodes, 17, 19);
195 SMDS_VtkVolume::~SMDS_VtkVolume()
199 void SMDS_VtkVolume::Print(ostream & OS) const
201 OS << "volume <" << GetID() << "> : ";
204 int SMDS_VtkVolume::NbFaces() const
206 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
207 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
212 case VTK_QUADRATIC_TETRA:
217 case VTK_QUADRATIC_PYRAMID:
218 case VTK_QUADRATIC_WEDGE:
222 case VTK_QUADRATIC_HEXAHEDRON:
227 vtkIdType nFaces = 0;
228 vtkIdType* ptIds = 0;
229 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
234 MESSAGE("invalid volume type")
242 int SMDS_VtkVolume::NbNodes() const
244 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
245 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
247 if (aVtkType != VTK_POLYHEDRON)
249 nbPoints = grid->GetCell(myVtkID)->GetNumberOfPoints();
253 vtkIdType nFaces = 0;
254 vtkIdType* ptIds = 0;
255 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
257 for (int i = 0; i < nFaces; i++)
259 int nodesInFace = ptIds[id];
260 nbPoints += nodesInFace;
261 id += (nodesInFace + 1);
267 int SMDS_VtkVolume::NbEdges() const
269 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
270 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
275 case VTK_QUADRATIC_TETRA:
279 case VTK_QUADRATIC_PYRAMID:
283 case VTK_QUADRATIC_WEDGE:
287 case VTK_QUADRATIC_HEXAHEDRON:
292 vtkIdType nFaces = 0;
293 vtkIdType* ptIds = 0;
294 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
297 for (int i = 0; i < nFaces; i++)
299 int edgesInFace = ptIds[id];
300 id += (edgesInFace + 1);
301 nbEdges += edgesInFace;
303 nbEdges = nbEdges / 2;
307 MESSAGE("invalid volume type")
316 * 1 <= face_ind <= NbFaces()
318 int SMDS_VtkVolume::NbFaceNodes(const int face_ind) const
320 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
321 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
323 if (aVtkType == VTK_POLYHEDRON)
325 vtkIdType nFaces = 0;
326 vtkIdType* ptIds = 0;
327 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
329 for (int i = 0; i < nFaces; i++)
331 int nodesInFace = ptIds[id];
332 id += (nodesInFace + 1);
333 if (i == face_ind - 1)
335 nbNodes = nodesInFace;
344 * 1 <= face_ind <= NbFaces()
345 * 1 <= node_ind <= NbFaceNodes()
347 const SMDS_MeshNode* SMDS_VtkVolume::GetFaceNode(const int face_ind, const int node_ind) const
349 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
350 vtkUnstructuredGrid* grid = mesh->getGrid();
351 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
352 const SMDS_MeshNode* node = 0;
353 if (aVtkType == VTK_POLYHEDRON)
355 vtkIdType nFaces = 0;
356 vtkIdType* ptIds = 0;
357 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
359 for (int i = 0; i < nFaces; i++)
361 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
362 if (i == face_ind - 1) // first face is number 1
364 if ((node_ind > 0) && (node_ind <= nodesInFace))
365 node = mesh->FindNodeVtk(ptIds[id + node_ind]); // ptIds[id+1] : first node
368 id += (nodesInFace + 1);
375 * return number of nodes for each face
377 const std::vector<int> SMDS_VtkVolume::GetQuantities() const
379 vector<int> quantities;
381 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
382 vtkUnstructuredGrid* grid = mesh->getGrid();
383 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
384 if (aVtkType == VTK_POLYHEDRON)
386 vtkIdType nFaces = 0;
387 vtkIdType* ptIds = 0;
388 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
390 for (int i = 0; i < nFaces; i++)
392 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
393 quantities.push_back(nodesInFace);
394 id += (nodesInFace + 1);
400 SMDS_ElemIteratorPtr SMDS_VtkVolume::elementsIterator(SMDSAbs_ElementType type) const
406 SMDSAbs_EntityType aType = this->GetEntityType();
407 if (aType == SMDSEntity_Polyhedra)
408 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorPolyH(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
410 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
413 MESSAGE("ERROR : Iterator not implemented");
414 return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
418 SMDS_ElemIteratorPtr SMDS_VtkVolume::nodesIteratorToUNV() const
420 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
423 SMDS_ElemIteratorPtr SMDS_VtkVolume::interlacedNodesElemIterator() const
425 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
428 SMDSAbs_ElementType SMDS_VtkVolume::GetType() const
430 return SMDSAbs_Volume;
434 * \brief Return node by its index
435 * \param ind - node index
436 * \retval const SMDS_MeshNode* - the node
438 const SMDS_MeshNode* SMDS_VtkVolume::GetNode(const int ind) const
440 // TODO optimize if possible (vtkCellIterator)
441 return SMDS_MeshElement::GetNode(ind);
444 bool SMDS_VtkVolume::IsQuadratic() const
446 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
447 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
448 // TODO quadratic polyhedrons ?
451 case VTK_QUADRATIC_TETRA:
452 case VTK_QUADRATIC_PYRAMID:
453 case VTK_QUADRATIC_WEDGE:
454 case VTK_QUADRATIC_HEXAHEDRON:
462 bool SMDS_VtkVolume::IsPoly() const
464 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
465 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
466 return (aVtkType == VTK_POLYHEDRON);
469 bool SMDS_VtkVolume::IsMediumNode(const SMDS_MeshNode* node) const
471 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
472 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
473 int rankFirstMedium = 0;
476 case VTK_QUADRATIC_TETRA:
477 rankFirstMedium = 4; // medium nodes are of rank 4 to 9
479 case VTK_QUADRATIC_PYRAMID:
480 rankFirstMedium = 5; // medium nodes are of rank 5 to 12
482 case VTK_QUADRATIC_WEDGE:
483 rankFirstMedium = 6; // medium nodes are of rank 6 to 14
485 case VTK_QUADRATIC_HEXAHEDRON:
486 rankFirstMedium = 8; // medium nodes are of rank 8 to 19
493 grid->GetCellPoints(myVtkID, npts, pts);
494 vtkIdType nodeId = node->getVtkId();
495 for (int rank = 0; rank < npts; rank++)
497 if (pts[rank] == nodeId)
499 if (rank < rankFirstMedium)
505 //throw SALOME_Exception(LOCALIZED("node does not belong to this element"));
506 MESSAGE("======================================================");
507 MESSAGE("= IsMediumNode: node does not belong to this element =");
508 MESSAGE("======================================================");
512 SMDSAbs_EntityType SMDS_VtkVolume::GetEntityType() const
514 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
515 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
517 SMDSAbs_EntityType aType = SMDSEntity_Tetra;
521 aType = SMDSEntity_Tetra;
524 aType = SMDSEntity_Pyramid;
527 aType = SMDSEntity_Penta;
530 aType = SMDSEntity_Hexa;
532 case VTK_QUADRATIC_TETRA:
533 aType = SMDSEntity_Quad_Tetra;
535 case VTK_QUADRATIC_PYRAMID:
536 aType = SMDSEntity_Quad_Pyramid;
538 case VTK_QUADRATIC_WEDGE:
539 aType = SMDSEntity_Quad_Penta;
541 case VTK_QUADRATIC_HEXAHEDRON:
542 aType = SMDSEntity_Quad_Hexa;
544 //#ifdef VTK_HAVE_POLYHEDRON
546 aType = SMDSEntity_Polyhedra;
550 aType = SMDSEntity_Polyhedra;
556 vtkIdType SMDS_VtkVolume::GetVtkType() const
558 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
559 vtkIdType aType = grid->GetCellType(myVtkID);
563 void SMDS_VtkVolume::gravityCenter(SMDS_UnstructuredGrid* grid, vtkIdType *nodeIds, int nbNodes, double* result)
565 for (int j = 0; j < 3; j++)
569 for (int i = 0; i < nbNodes; i++)
571 double *coords = grid->GetPoint(nodeIds[i]);
572 for (int j = 0; j < 3; j++)
573 result[j] += coords[j];
575 for (int j = 0; j < 3; j++)
576 result[j] = result[j] / nbNodes;
577 //MESSAGE("center " << result[0] << " " << result[1] << " " << result[2]);
581 bool SMDS_VtkVolume::isForward(double* a, double* b, double* c, double* d)
583 double u[3], v[3], w[3];
584 for (int j = 0; j < 3; j++)
586 //MESSAGE("a,b,c,d " << a[j] << " " << b[j] << " " << c[j] << " " << d[j]);
590 //MESSAGE("u,v,w " << u[j] << " " << v[j] << " " << w[j]);
592 double prodmixte = (u[1]*v[2] - u[2]*v[1]) * w[0]
593 + (u[2]*v[0] - u[0]*v[2]) * w[1]
594 + (u[0]*v[1] - u[1]*v[0]) * w[2];
595 return (prodmixte < 0);
598 /*! For polyhedron only
599 * @return actual number of nodes (not the sum of nodes of all faces)
601 int SMDS_VtkVolume::NbUniqueNodes() const
603 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
604 return grid->GetCell(myVtkID)->GetNumberOfPoints();
607 /*! For polyhedron use only
608 * @return iterator on actual nodes (not through the faces)
610 SMDS_ElemIteratorPtr SMDS_VtkVolume::uniqueNodesIterator() const
612 MESSAGE("uniqueNodesIterator");
613 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));