mmentityseed.hh 2.93 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1 2 3 4 5 6 7 8 9 10 11 12
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_MULTIMESH_ENTITYSEED_HH
#define DUNE_MULTIMESH_ENTITYSEED_HH

namespace Dune
{
  /**
   * \brief The EntitySeed class provides the minimal information needed to restore an Entity using the grid.
   * \ingroup MultiMesh
   *
   */
Praetorius, Simon's avatar
Praetorius, Simon committed
13
  template <int codim, class HostGrid>
Praetorius, Simon's avatar
Praetorius, Simon committed
14 15 16 17
  class MultiMeshEntitySeed
  {
  protected:

18
    /// Entity type of the hostgrid
Praetorius, Simon's avatar
Praetorius, Simon committed
19
    using HostEntity = typename HostGrid::Traits::template Codim<codim>::Entity;
Praetorius, Simon's avatar
Praetorius, Simon committed
20

21
    /// EntitySeed type of the hostgrid
Praetorius, Simon's avatar
Praetorius, Simon committed
22
    using HostEntitySeed = typename HostGrid::Traits::template Codim<codim>::EntitySeed;
Praetorius, Simon's avatar
Praetorius, Simon committed
23 24 25 26

  public:
    enum { codimension = codim };

27
    /// Construct an empty (i.e. isValid() == false) seed.
Praetorius, Simon's avatar
Praetorius, Simon committed
28 29 30
    MultiMeshEntitySeed()
    {}

31
    /// \brief Create EntitySeed from hostgrid Entity
Praetorius, Simon's avatar
Praetorius, Simon committed
32 33 34 35 36 37 38 39 40 41
    /**
     * We call hostEntity.seed() directly in the constructor
     * of MultiMeshEntitySeed to allow for return value optimization.
     */
    MultiMeshEntitySeed (const HostEntity& hostEntity, std::size_t gridIdx)
      : hostEntitySeed_(hostEntity.seed())
      , gridIdx_(gridIdx)
    {}

    /// Get stored HostEntitySeed
42
    const HostEntitySeed& hostEntitySeed () const
Praetorius, Simon's avatar
Praetorius, Simon committed
43 44 45 46 47
    {
      return hostEntitySeed_;
    }

    /// Check whether it is safe to create an Entity from this Seed
48
    bool isValid () const
Praetorius, Simon's avatar
Praetorius, Simon committed
49 50 51 52 53
    {
      return hostEntitySeed_.isValid();
    }

    /// Return the index of the grid the entity belongs to
54
    std::size_t gridIndex () const
Praetorius, Simon's avatar
Praetorius, Simon committed
55 56 57 58 59 60 61 62 63
    {
      return gridIdx_;
    }

  private:
    HostEntitySeed hostEntitySeed_;
    std::size_t gridIdx_;
  };

64 65 66 67 68

  template <class HG>
  class MultiEntity;

  // An entity seed for a vector of entities
Praetorius, Simon's avatar
Praetorius, Simon committed
69
  template <int codim, class HostGrid>
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  class MultiEntitySeed
  {
  protected:

    /// Entity type of the hostgrid
    using HostEntity = typename HostGrid::Traits::template Codim<codim>::Entity;

    /// EntitySeed type of the hostgrid
    using HostEntitySeed = typename HostGrid::Traits::template Codim<codim>::EntitySeed;

  public:
    enum { codimension = codim };

    /// Construct an empty (i.e. isValid() == false) seed.
    MultiEntitySeed()
    {}

    /// \brief Create EntitySeed from hostgrid Entity
Praetorius, Simon's avatar
Praetorius, Simon committed
88 89
    template <class MultiEntity>
    MultiEntitySeed (const MultiEntity& multiEntity)
90
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
91 92
      for (auto const& e : multiEntity)
        hostEntitySeeds_.emplace_back(e.seed());
93 94 95 96 97 98 99 100 101 102 103
    }

    /// Get stored HostEntitySeed
    const std::vector<HostEntitySeed>& hostEntitySeeds () const
    {
      return hostEntitySeeds_;
    }

    /// Check whether it is safe to create an Entity from this Seed
    bool isValid () const
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
104 105 106
      return hostEntitySeeds_.empty() ? false :
        std::all_of(hostEntitySeeds_.begin(), hostEntitySeeds_.end(),
          [](auto const& seed) { return seed.isValid(); });
107 108 109 110 111 112
    }

  private:
    std::vector<HostEntitySeed> hostEntitySeeds_;
  };

Praetorius, Simon's avatar
Praetorius, Simon committed
113 114 115
} // end namespace Dune

#endif  // DUNE_MULTIMESH_ENTITYSEED_HH