1 // Copyright (C) 2010-2016 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, or (at your option) any later version.
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(const 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(const 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()) // cases are in order of usage frequency
53 aType = VTK_HEXAHEDRON;
62 aType = VTK_QUADRATIC_TETRA;
65 aType = VTK_QUADRATIC_HEXAHEDRON;
68 aType = VTK_QUADRATIC_PYRAMID;
71 aType = VTK_QUADRATIC_WEDGE;
74 aType = VTK_HEXAGONAL_PRISM;
77 aType = VTK_TRIQUADRATIC_HEXAHEDRON;
80 aType = VTK_HEXAHEDRON;
83 myVtkID = grid->InsertNextLinkedCell(aType, nodeIds.size(), (vtkIdType *) &nodeIds[0]);
84 mesh->setMyModified();
85 //MESSAGE("SMDS_VtkVolume::init myVtkID " << myVtkID);
88 //#ifdef VTK_HAVE_POLYHEDRON
89 void SMDS_VtkVolume::initPoly(const std::vector<vtkIdType>& nodeIds,
90 const std::vector<int>& nbNodesPerFace,
93 SMDS_MeshVolume::init();
94 //MESSAGE("SMDS_VtkVolume::initPoly");
95 SMDS_UnstructuredGrid* grid = mesh->getGrid();
97 //this->gravityCenter(grid, &nodeIds[0], nodeIds.size(), ¢er[0]);
98 std::vector<vtkIdType> ptIds;
99 vtkIdType nbFaces = nbNodesPerFace.size();
101 for (int i = 0; i < nbFaces; i++)
103 int nf = nbNodesPerFace[i];
105 // EAP: a right approach is:
106 // - either the user should care of order of nodes or
107 // - the user should use a service method arranging nodes if he
108 // don't want or can't to do it by him-self
109 // The method below works OK only with planar faces and convex polyhedrones
114 // grid->GetPoints()->GetPoint(nodeIds[k], a);
115 // grid->GetPoints()->GetPoint(nodeIds[k + 1], b);
116 // grid->GetPoints()->GetPoint(nodeIds[k + 2], c);
117 // bool isFaceForward = this->isForward(a, b, c, center);
118 //MESSAGE("isFaceForward " << i << " " << isFaceForward);
119 const vtkIdType *facePts = &nodeIds[k];
121 for (int n = 0; n < nf; n++)
122 ptIds.push_back(facePts[n]);
124 // for (int n = nf - 1; n >= 0; n--)
125 // ptIds.push_back(facePts[n]);
128 myVtkID = grid->InsertNextLinkedCell(VTK_POLYHEDRON, nbFaces, &ptIds[0]);
129 mesh->setMyModified();
133 bool SMDS_VtkVolume::ChangeNodes(const SMDS_MeshNode* nodes[], const int nbNodes)
135 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
138 grid->GetCellPoints(myVtkID, npts, pts);
141 MESSAGE("ChangeNodes problem: not the same number of nodes " << npts << " -> " << nbNodes);
144 for (int i = 0; i < nbNodes; i++)
146 pts[i] = nodes[i]->getVtkId();
148 SMDS_Mesh::_meshList[myMeshId]->setMyModified();
153 * Reorder in VTK order a list of nodes given in SMDS order.
154 * To be used before ChangeNodes: lists are given or computed in SMDS order.
156 bool SMDS_VtkVolume::vtkOrder(const SMDS_MeshNode* nodes[], const int nbNodes)
158 if (nbNodes != this->NbNodes())
160 MESSAGE("vtkOrder, wrong number of nodes " << nbNodes << " instead of "<< this->NbNodes());
163 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
164 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
165 const std::vector<int>& interlace = SMDS_MeshCell::toVtkOrder( VTKCellType( aVtkType ));
166 if ( !interlace.empty() )
168 ASSERT( (int)interlace.size() == nbNodes );
169 std::vector<const SMDS_MeshNode*> initNodes( nodes, nodes+nbNodes );
170 for ( size_t i = 0; i < interlace.size(); ++i )
171 nodes[i] = initNodes[ interlace[i] ];
176 SMDS_VtkVolume::~SMDS_VtkVolume()
180 void SMDS_VtkVolume::Print(ostream & OS) const
182 OS << "volume <" << GetID() << "> : ";
185 int SMDS_VtkVolume::NbFaces() const
187 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
188 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
193 case VTK_QUADRATIC_TETRA:
198 case VTK_QUADRATIC_PYRAMID:
199 case VTK_QUADRATIC_WEDGE:
203 case VTK_QUADRATIC_HEXAHEDRON:
204 case VTK_TRIQUADRATIC_HEXAHEDRON:
209 vtkIdType nFaces = 0;
210 vtkIdType* ptIds = 0;
211 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
215 case VTK_HEXAGONAL_PRISM:
219 MESSAGE("invalid volume type")
227 int SMDS_VtkVolume::NbNodes() const
229 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
230 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
232 if (aVtkType != VTK_POLYHEDRON)
234 nbPoints = grid->GetCell(myVtkID)->GetNumberOfPoints();
238 vtkIdType nFaces = 0;
239 vtkIdType* ptIds = 0;
240 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
242 for (int i = 0; i < nFaces; i++)
244 int nodesInFace = ptIds[id];
245 nbPoints += nodesInFace;
246 id += (nodesInFace + 1);
252 int SMDS_VtkVolume::NbEdges() const
254 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
255 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
260 case VTK_QUADRATIC_TETRA:
264 case VTK_QUADRATIC_PYRAMID:
268 case VTK_QUADRATIC_WEDGE:
272 case VTK_QUADRATIC_HEXAHEDRON:
273 case VTK_TRIQUADRATIC_HEXAHEDRON:
278 vtkIdType nFaces = 0;
279 vtkIdType* ptIds = 0;
280 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
283 for (int i = 0; i < nFaces; i++)
285 int edgesInFace = ptIds[id];
286 id += (edgesInFace + 1);
287 nbEdges += edgesInFace;
289 nbEdges = nbEdges / 2;
292 case VTK_HEXAGONAL_PRISM:
296 MESSAGE("invalid volume type")
305 * 1 <= face_ind <= NbFaces()
307 int SMDS_VtkVolume::NbFaceNodes(const int face_ind) const
309 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
310 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
312 if (aVtkType == VTK_POLYHEDRON)
314 vtkIdType nFaces = 0;
315 vtkIdType* ptIds = 0;
316 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
318 for (int i = 0; i < nFaces; i++)
320 int nodesInFace = ptIds[id];
321 id += (nodesInFace + 1);
322 if (i == face_ind - 1)
324 nbNodes = nodesInFace;
333 * 1 <= face_ind <= NbFaces()
334 * 1 <= node_ind <= NbFaceNodes()
336 const SMDS_MeshNode* SMDS_VtkVolume::GetFaceNode(const int face_ind, const int node_ind) const
338 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
339 vtkUnstructuredGrid* grid = mesh->getGrid();
340 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
341 const SMDS_MeshNode* node = 0;
342 if (aVtkType == VTK_POLYHEDRON)
344 vtkIdType nFaces = 0;
345 vtkIdType* ptIds = 0;
346 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
348 for (int i = 0; i < nFaces; i++)
350 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
351 if (i == face_ind - 1) // first face is number 1
353 if ((node_ind > 0) && (node_ind <= nodesInFace))
354 node = mesh->FindNodeVtk(ptIds[id + node_ind]); // ptIds[id+1] : first node
357 id += (nodesInFace + 1);
364 * return number of nodes for each face
366 std::vector<int> SMDS_VtkVolume::GetQuantities() const
368 std::vector<int> quantities;
369 SMDS_Mesh *mesh = SMDS_Mesh::_meshList[myMeshId];
370 vtkUnstructuredGrid* grid = mesh->getGrid();
371 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
372 if (aVtkType == VTK_POLYHEDRON)
374 vtkIdType nFaces = 0;
375 vtkIdType* ptIds = 0;
376 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
378 for (int i = 0; i < nFaces; i++)
380 int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
381 quantities.push_back(nodesInFace);
382 id += (nodesInFace + 1);
388 SMDS_ElemIteratorPtr SMDS_VtkVolume::elementsIterator(SMDSAbs_ElementType type) const
394 SMDSAbs_EntityType aType = this->GetEntityType();
395 if (aType == SMDSEntity_Polyhedra)
396 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIteratorPolyH(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
398 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, aType));
401 MESSAGE("ERROR : Iterator not implemented");
402 return SMDS_ElemIteratorPtr((SMDS_ElemIterator*) NULL);
406 SMDS_NodeIteratorPtr SMDS_VtkVolume::nodesIteratorToUNV() const
408 return SMDS_NodeIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
411 SMDS_NodeIteratorPtr SMDS_VtkVolume::interlacedNodesIterator() const
413 return SMDS_NodeIteratorPtr(new SMDS_VtkCellIteratorToUNV(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));
416 SMDSAbs_ElementType SMDS_VtkVolume::GetType() const
418 return SMDSAbs_Volume;
422 * \brief Return node by its index
423 * \param ind - node index
424 * \retval const SMDS_MeshNode* - the node
426 const SMDS_MeshNode* SMDS_VtkVolume::GetNode(const int ind) const
428 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
429 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
430 if ( aVtkType == VTK_POLYHEDRON)
432 vtkIdType nFaces = 0;
433 vtkIdType* ptIds = 0;
434 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
435 int id = 0, nbPoints = 0;
436 for (int i = 0; i < nFaces; i++)
438 int nodesInFace = ptIds[id];
439 if ( ind < nbPoints + nodesInFace )
440 return SMDS_Mesh::_meshList[myMeshId]->FindNodeVtk( ptIds[ ind + i ]);
441 nbPoints += nodesInFace;
442 id += (nodesInFace + 1);
446 vtkIdType npts, *pts;
447 grid->GetCellPoints( this->myVtkID, npts, pts );
448 const std::vector<int>& interlace = SMDS_MeshCell::fromVtkOrder( VTKCellType( aVtkType ));
449 return SMDS_Mesh::_meshList[myMeshId]->FindNodeVtk( pts[ interlace.empty() ? ind : interlace[ind]] );
452 * \brief Check if a node belongs to the element
453 * \param node - the node to check
454 * \retval int - node index within the element, -1 if not found
456 int SMDS_VtkVolume::GetNodeIndex( const SMDS_MeshNode* node ) const
458 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
459 const vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
460 if ( aVtkType == VTK_POLYHEDRON)
462 vtkIdType nFaces = 0;
463 vtkIdType* ptIds = 0;
464 grid->GetFaceStream(this->myVtkID, nFaces, ptIds);
466 for (int iF = 0; iF < nFaces; iF++)
468 int nodesInFace = ptIds[id];
469 for ( vtkIdType i = 0; i < nodesInFace; ++i )
470 if ( ptIds[id+i+1] == node->getVtkId() )
472 id += (nodesInFace + 1);
476 vtkIdType npts, *pts;
477 grid->GetCellPoints( this->myVtkID, npts, pts );
478 for ( vtkIdType i = 0; i < npts; ++i )
479 if ( pts[i] == node->getVtkId() )
481 const std::vector<int>& interlace = SMDS_MeshCell::toVtkOrder( VTKCellType( aVtkType ));
482 return interlace.empty() ? i : interlace[i];
487 bool SMDS_VtkVolume::IsQuadratic() const
489 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
490 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
491 // TODO quadratic polyhedrons ?
494 case VTK_QUADRATIC_TETRA:
495 case VTK_QUADRATIC_PYRAMID:
496 case VTK_QUADRATIC_WEDGE:
497 case VTK_QUADRATIC_HEXAHEDRON:
498 case VTK_TRIQUADRATIC_HEXAHEDRON:
506 bool SMDS_VtkVolume::IsPoly() const
508 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
509 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
510 return (aVtkType == VTK_POLYHEDRON);
513 bool SMDS_VtkVolume::IsMediumNode(const SMDS_MeshNode* node) const
515 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
516 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
517 int rankFirstMedium = 0;
520 case VTK_QUADRATIC_TETRA:
521 rankFirstMedium = 4; // medium nodes are of rank 4 to 9
523 case VTK_QUADRATIC_PYRAMID:
524 rankFirstMedium = 5; // medium nodes are of rank 5 to 12
526 case VTK_QUADRATIC_WEDGE:
527 rankFirstMedium = 6; // medium nodes are of rank 6 to 14
529 case VTK_QUADRATIC_HEXAHEDRON:
530 case VTK_TRIQUADRATIC_HEXAHEDRON:
531 rankFirstMedium = 8; // medium nodes are of rank 8 to 19
538 grid->GetCellPoints(myVtkID, npts, pts);
539 vtkIdType nodeId = node->getVtkId();
540 for (int rank = 0; rank < npts; rank++)
542 if (pts[rank] == nodeId)
544 if (rank < rankFirstMedium)
550 //throw SALOME_Exception(LOCALIZED("node does not belong to this element"));
551 MESSAGE("======================================================");
552 MESSAGE("= IsMediumNode: node does not belong to this element =");
553 MESSAGE("======================================================");
557 int SMDS_VtkVolume::NbCornerNodes() const
559 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
560 vtkIdType aVtkType = grid->GetCellType(myVtkID);
563 case VTK_QUADRATIC_TETRA: return 4;
564 case VTK_QUADRATIC_PYRAMID: return 5;
565 case VTK_QUADRATIC_WEDGE: return 6;
566 case VTK_QUADRATIC_HEXAHEDRON:
567 case VTK_TRIQUADRATIC_HEXAHEDRON: return 8;
573 SMDSAbs_EntityType SMDS_VtkVolume::GetEntityType() const
575 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
576 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
578 SMDSAbs_EntityType aType = SMDSEntity_Tetra;
582 aType = SMDSEntity_Tetra;
585 aType = SMDSEntity_Pyramid;
588 aType = SMDSEntity_Penta;
591 aType = SMDSEntity_Hexa;
593 case VTK_QUADRATIC_TETRA:
594 aType = SMDSEntity_Quad_Tetra;
596 case VTK_QUADRATIC_PYRAMID:
597 aType = SMDSEntity_Quad_Pyramid;
599 case VTK_QUADRATIC_WEDGE:
600 aType = SMDSEntity_Quad_Penta;
602 case VTK_QUADRATIC_HEXAHEDRON:
603 aType = SMDSEntity_Quad_Hexa;
605 case VTK_TRIQUADRATIC_HEXAHEDRON:
606 aType = SMDSEntity_TriQuad_Hexa;
608 case VTK_HEXAGONAL_PRISM:
609 aType = SMDSEntity_Hexagonal_Prism;
611 //#ifdef VTK_HAVE_POLYHEDRON
613 aType = SMDSEntity_Polyhedra;
617 aType = SMDSEntity_Polyhedra;
623 SMDSAbs_GeometryType SMDS_VtkVolume::GetGeomType() const
625 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
626 vtkIdType aVtkType = grid->GetCellType(this->myVtkID);
628 SMDSAbs_GeometryType aType = SMDSGeom_NONE;
632 case VTK_QUADRATIC_TETRA:
633 aType = SMDSGeom_TETRA;
636 case VTK_QUADRATIC_PYRAMID:
637 aType = SMDSGeom_PYRAMID;
640 case VTK_QUADRATIC_WEDGE:
641 aType = SMDSGeom_PENTA;
644 case VTK_QUADRATIC_HEXAHEDRON:
645 case VTK_TRIQUADRATIC_HEXAHEDRON:
646 aType = SMDSGeom_HEXA;
648 case VTK_HEXAGONAL_PRISM:
649 aType = SMDSGeom_HEXAGONAL_PRISM;
651 //#ifdef VTK_HAVE_POLYHEDRON
653 aType = SMDSGeom_POLYHEDRA;
657 aType = SMDSGeom_POLYHEDRA;
663 vtkIdType SMDS_VtkVolume::GetVtkType() const
665 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
666 vtkIdType aType = grid->GetCellType(myVtkID);
670 void SMDS_VtkVolume::gravityCenter(SMDS_UnstructuredGrid* grid,
671 const vtkIdType * nodeIds,
675 for (int j = 0; j < 3; j++)
679 for (int i = 0; i < nbNodes; i++)
681 double *coords = grid->GetPoint(nodeIds[i]);
682 for (int j = 0; j < 3; j++)
683 result[j] += coords[j];
685 for (int j = 0; j < 3; j++)
686 result[j] = result[j] / nbNodes;
687 //MESSAGE("center " << result[0] << " " << result[1] << " " << result[2]);
691 bool SMDS_VtkVolume::isForward(double* a, double* b, double* c, double* d)
693 double u[3], v[3], w[3];
694 for (int j = 0; j < 3; j++)
696 //MESSAGE("a,b,c,d " << a[j] << " " << b[j] << " " << c[j] << " " << d[j]);
700 //MESSAGE("u,v,w " << u[j] << " " << v[j] << " " << w[j]);
702 double prodmixte = (u[1]*v[2] - u[2]*v[1]) * w[0]
703 + (u[2]*v[0] - u[0]*v[2]) * w[1]
704 + (u[0]*v[1] - u[1]*v[0]) * w[2];
705 return (prodmixte < 0);
708 /*! For polyhedron only
709 * @return actual number of nodes (not the sum of nodes of all faces)
711 int SMDS_VtkVolume::NbUniqueNodes() const
713 vtkUnstructuredGrid* grid = SMDS_Mesh::_meshList[myMeshId]->getGrid();
714 return grid->GetCell(myVtkID)->GetNumberOfPoints();
717 /*! For polyhedron use only
718 * @return iterator on actual nodes (not through the faces)
720 SMDS_ElemIteratorPtr SMDS_VtkVolume::uniqueNodesIterator() const
722 //MESSAGE("uniqueNodesIterator");
723 return SMDS_ElemIteratorPtr(new SMDS_VtkCellIterator(SMDS_Mesh::_meshList[myMeshId], myVtkID, GetEntityType()));