1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_ONEDGRID_FACTORY_HH
5 #define DUNE_ONEDGRID_FACTORY_HH
6 
7 /** \file
8     \brief The specialization of the generic GridFactory for OneDGrid
9     \author Oliver Sander
10  */
11 
12 #include <array>
13 #include <cmath>
14 #include <map>
15 #include <memory>
16 #include <tuple>
17 #include <utility>
18 #include <vector>
19 
20 #include <dune/common/fvector.hh>
21 
22 #include <dune/grid/common/gridfactory.hh>
23 #include <dune/grid/onedgrid.hh>
24 
25 namespace Dune {
26 
27   /** \brief Specialization of the generic GridFactory for OneDGrid
28 
29    */
30   template <>
31   class GridFactory<OneDGrid> : public GridFactoryInterface<OneDGrid> {
32 
33     typedef GridFactoryInterface<OneDGrid> Base;
34 
35     /** \brief Type used by the grid for coordinates */
36     typedef OneDGrid::ctype ctype;
37 
38   public:
39 
40     // use default implementation from base class
41     using Base::insertionIndex;
42 
43     /** \brief Default constructor */
44     GridFactory();
45 
46     /** \brief Constructor for a given grid object
47 
48        If you already have your grid object constructed you can
49        hand it over using this constructor.
50 
51        If you construct your factory class using this constructor
52        the pointer handed over to you by the method createGrid() is
53        the one you supplied here.
54      */
55     GridFactory(OneDGrid* grid);
56 
57     /** \brief Destructor */
58     ~GridFactory();
59 
60     /** \brief Insert a vertex into the coarse grid */
61     virtual void insertVertex(const FieldVector<ctype,1>& pos);
62 
63     /** \brief Insert an element into the coarse grid
64         \param type The GeometryType of the new element
65         \param vertices The vertices of the new element, using the DUNE numbering
66      */
67     virtual void insertElement(const GeometryType& type,
68                                const std::vector<unsigned int>& vertices);
69 
70     /** \brief Insert a boundary segment (== a point).
71         This influences the ordering of the boundary segments
72      */
73     virtual void insertBoundarySegment(const std::vector<unsigned int>& vertices);
74 
75     /** \brief Insert a boundary segment (== a point) and the boundary segment geometry
76      *
77         This influences the ordering of the boundary segments.
78         The BoundarySegment object does not actually have any effect.
79      */
80     virtual void insertBoundarySegment(const std::vector<unsigned int>& vertices,
81                                        const std::shared_ptr<BoundarySegment<1> >& boundarySegment);
82 
83     /** \brief Return true if the intersection has been explictily insterted into the factory */
84     virtual bool wasInserted(const typename OneDGrid::LeafIntersection& intersection) const;
85 
86     /** \brief Return the number of the intersection in the order of insertion into the factory */
87     virtual unsigned int insertionIndex(const typename OneDGrid::LeafIntersection& intersection) const;
88 
89     /** \brief Finalize grid creation and hand over the grid
90         The receiver takes responsibility of the memory allocated for the grid
91      */
92     virtual std::unique_ptr<OneDGrid> createGrid();
93 
94   private:
95 
96     // Initialize the grid structure in UG
97     void createBegin();
98 
99     // Pointer to the grid being built
100     OneDGrid* grid_;
101 
102     // True if the factory allocated the grid itself, false if the
103     // grid was handed over from the outside
104     bool factoryOwnsGrid_;
105 
106     /** \brief While inserting the elements this array records the vertices
107         of the elements. */
108     std::vector<std::array<unsigned int, 2> > elements_;
109 
110     /** \brief Buffer the vertices until createGrid() is called */
111     std::map<FieldVector<ctype,1>, unsigned int > vertexPositions_;
112 
113     /** \brief Counter that creates the vertex indices */
114     unsigned int vertexIndex_;
115 
116     /** \brief Store the explicitly given boundary segments until createGrid() is called */
117     std::vector<unsigned int> boundarySegments_;
118 
119     /** \brief Store vertex positions sorted by index */
120     std::vector<ctype> vertexPositionsByIndex_;
121   };
122 
123 }
124 
125 #endif
126