dune-grid  2.6-git
psurfaceboundary.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_GRID_IO_FILE_AMIRAMESH_PSURFACE_BOUNDARY_HH
4 #define DUNE_GRID_IO_FILE_AMIRAMESH_PSURFACE_BOUNDARY_HH
5 
9 #include <memory>
10 
12 
13 #if HAVE_PSURFACE
14 #include <psurface/PSurface.h>
15 #include "psurface/AmiraMeshIO.h"
16 #if HAVE_PSURFACE_2_0
17 #include <psurface/Hdf5IO.h>
18 #endif
19 
20 #if HAVE_AMIRAMESH
21 #include <amiramesh/AmiraMesh.h>
22 #endif
23 
24 
25 namespace Dune {
26 
37  template <int dim>
38  class PSurfaceBoundary
39  {
40  static_assert((dim==1 or dim==2), "PSurfaceBoundaries can only have dimensions 1 or 2!");
41 
42  public:
43 
45  class PSurfaceBoundarySegment : public Dune::BoundarySegment<dim+1>
46  {
47  public:
48 
54  PSurfaceBoundarySegment(const std::shared_ptr<PSurfaceBoundary<dim> >& psurfaceBoundary, int segment)
55  : psurfaceBoundary_(psurfaceBoundary),
56  segment_(segment)
57  {}
58 
60  virtual Dune::FieldVector<double, dim+1> operator()(const Dune::FieldVector<double,dim>& local) const {
61 
62  Dune::FieldVector<double, dim+1> result;
63 
64  // Transform local to barycentric coordinates
65  psurface::StaticVector<float,dim> barCoords;
66 
67  if (dim==2) {
68  barCoords[0] = 1 - local[0] - local[1];
69  barCoords[1] = local[0];
70  } else { // dim==1
71  barCoords[0] = 1 - local[0];
72  }
73 
74  psurface::StaticVector<float,dim+1> r;
75 
76  if (!psurfaceBoundary_->getPSurfaceObject()->positionMap(segment_, barCoords, r))
77  DUNE_THROW(Dune::GridError, "psurface::positionMap returned error code");
78 
79  for (int i=0; i<dim+1; i++)
80  result[i] = r[i];
81 
82  return result;
83  }
84 
85  std::shared_ptr<PSurfaceBoundary<dim> > psurfaceBoundary_;
86  int segment_;
87  };
88 
89 
90 
92  PSurfaceBoundary(psurface::PSurface<dim,float>* psurface)
93  : psurface_(psurface)
94  {}
95 
103  psurface::PSurface<dim,float>* getPSurfaceObject()
104  {
105  return psurface_.get();
106  }
107 
115  static std::shared_ptr<PSurfaceBoundary<dim> > read(const std::string& filename)
116  {
117  psurface::PSurface<dim,float>* newDomain;
118 
119 #if HAVE_PSURFACE_2_0
120  // Try to read the file as an hdf5 file
121  if (filename.find(".h5")==filename.length()-3) {
122  newDomain = psurface::Hdf5IO<float,dim>::read(filename);
123  if (newDomain)
124  return std::make_shared<PSurfaceBoundary<dim> >(newDomain);
125  }
126 #endif
127 
128 #if HAVE_AMIRAMESH
129  std::unique_ptr<AmiraMesh> am(AmiraMesh::read(filename.c_str()));
130 
131  if (!am.get())
132  DUNE_THROW(IOError, "An error has occurred while reading " << filename);
133 
134  newDomain
135  = (psurface::PSurface<dim,float>*) psurface::AmiraMeshIO<float>::readAmiraMesh(am.get(), filename.c_str());
136 
137  if (!newDomain)
138  DUNE_THROW(IOError, "An error has occurred while reading " << filename);
139 
140  return std::make_shared<PSurfaceBoundary<dim> >(newDomain);
141 #else
142  DUNE_THROW(IOError, "The given file is not in a supported format!");
143 #endif
144  }
145 
146  private:
147 
148  std::unique_ptr<psurface::PSurface<dim,float> > psurface_;
149 
150  };
151 
152 }
153 
154 #endif // #if HAVE_PSURFACE
155 #endif // #ifndef DUNE_GRID_IO_FILE_AMIRAMESH_PSURFACE_BOUNDARY_HH
Include standard header files.
Definition: agrid.hh:58
Base class for exceptions in Dune grid modules.
Definition: exceptions.hh:16
Provide a generic factory class for unstructured grids.
Base class for classes implementing geometries of boundary segments.
Definition: boundarysegment.hh:29