dune-vtk  0.2
discontinuousgridcreator.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <cstdint>
5 #include <limits>
6 #include <vector>
7 
8 #include <dune/common/exceptions.hh>
9 #include <dune/common/hybridutilities.hh>
10 #include <dune/grid/common/gridfactory.hh>
11 
12 #include <dune/vtk/types.hh>
14 
15 namespace Dune
16 {
17  namespace Vtk
18  {
19  // Create a grid where the input points are not connected and the connectivity
20  // describes separated elements.
21  template <class Grid>
23  : public GridCreatorInterface<Grid, DiscontinuousGridCreator<Grid>>
24  {
28 
29  struct CoordLess
30  {
31  template <class T, int N>
32  bool operator() (FieldVector<T,N> const& lhs, FieldVector<T,N> const& rhs) const
33  {
34  for (int i = 0; i < N; ++i) {
35  if (std::abs(lhs[i] - rhs[i]) < std::numeric_limits<T>::epsilon())
36  continue;
37  return lhs[i] < rhs[i];
38  }
39  return false;
40  }
41  };
42 
43  using Super::Super;
44  using Super::factory;
45 
46  void insertVerticesImpl (std::vector<GlobalCoordinate> const& points,
47  std::vector<std::uint64_t> const& /*point_ids*/)
48  {
49  points_ = &points;
50  uniquePoints_.clear();
51  std::size_t idx = 0;
52 
53  for (auto const& p : points) {
54  auto b = uniquePoints_.emplace(std::make_pair(p,idx));
55  if (b.second) {
56  factory().insertVertex(p);
57  ++idx;
58  }
59  }
60  }
61 
62  void insertElementsImpl (std::vector<std::uint8_t> const& types,
63  std::vector<std::int64_t> const& offsets,
64  std::vector<std::int64_t> const& connectivity)
65  {
66  assert(points_ != nullptr);
67  std::size_t idx = 0;
68  for (std::size_t i = 0; i < types.size(); ++i) {
69  auto type = Vtk::to_geometry(types[i]);
70  Vtk::CellType cellType{type};
71 
72  int nNodes = offsets[i] - (i == 0 ? 0 : offsets[i-1]);
73  assert(nNodes > 0);
74  std::vector<unsigned int> vtk_cell; vtk_cell.reserve(nNodes);
75  for (int j = 0; j < nNodes; ++j) {
76  std::size_t v_j = connectivity[idx++];
77  std::size_t new_idx = uniquePoints_[(*points_)[v_j]];
78  vtk_cell.push_back(new_idx);
79  }
80 
81  if (cellType.noPermutation()) {
82  factory().insertElement(type,vtk_cell);
83  } else {
84  // apply index permutation
85  std::vector<unsigned int> cell(nNodes);
86  for (int j = 0; j < nNodes; ++j)
87  cell[j] = vtk_cell[cellType.permutation(j)];
88  factory().insertElement(type,cell);
89  }
90  }
91  }
92 
93  private:
94  std::vector<GlobalCoordinate> const* points_ = nullptr;
95  std::map<GlobalCoordinate, std::size_t, CoordLess> uniquePoints_;
96  };
97 
98  // deduction guides
99  template <class Grid>
100  DiscontinuousGridCreator(GridFactory<Grid>&)
102 
103  } // end namespace Vtk
104 } // end namespace Dune
Definition: writer.hh:13
DiscontinuousGridCreator(GridFactory< Grid > &) -> DiscontinuousGridCreator< Grid >
GeometryType to_geometry(std::uint8_t cell)
Definition: types.cc:146
Definition: function.hh:18
Base class for grid creators in a CRTP style.
Definition: gridcreatorinterface.hh:25
typename Grid::template Codim< 0 >::Entity::Geometry::GlobalCoordinate GlobalCoordinate
Definition: gridcreatorinterface.hh:28
GridFactory< Grid > & factory()
Return the associated GridFactory.
Definition: gridcreatorinterface.hh:77
Definition: discontinuousgridcreator.hh:24
void insertElementsImpl(std::vector< std::uint8_t > const &types, std::vector< std::int64_t > const &offsets, std::vector< std::int64_t > const &connectivity)
Definition: discontinuousgridcreator.hh:62
void insertVerticesImpl(std::vector< GlobalCoordinate > const &points, std::vector< std::uint64_t > const &)
Definition: discontinuousgridcreator.hh:46
typename Super::GlobalCoordinate GlobalCoordinate
Definition: discontinuousgridcreator.hh:27
GridFactory< Grid > & factory()
Return the associated GridFactory.
Definition: gridcreatorinterface.hh:77
Definition: discontinuousgridcreator.hh:30
bool operator()(FieldVector< T, N > const &lhs, FieldVector< T, N > const &rhs) const
Definition: discontinuousgridcreator.hh:32
Mapping of Dune geometry types to VTK cell types.
Definition: types.hh:160