Line data Source code
1 : /*
2 : * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : ////////////////////////////////////////////////////////////////////////
34 : ////////////////////////////////////////////////////////////////////////
35 : // ...
36 : ////////////////////////////////////////////////////////////////////////
37 :
38 : #ifndef __H__LIBGRID__SELECTOR_GRID__
39 : #define __H__LIBGRID__SELECTOR_GRID__
40 :
41 : #include <cassert>
42 : #include "selector_interface.h"
43 :
44 : namespace ug
45 : {
46 :
47 : /** \ingroup lib_grid_tools
48 : * \{ */
49 :
50 : ////////////////////////////////////////////////////////////////////////
51 : ////////////////////////////////////////////////////////////////////////
52 : // Selector
53 : /// specialization of ISelector for a grid of class Grid.
54 : /** A selector is a useful class, that allows the user to mark
55 : * elements of a grid as selected or deselected.
56 : * The selection status is maintained even if new elements
57 : * are created or old ones deleted. Features like
58 : * autoselection and selection_inheritance allow users to
59 : * follow the creation and removal of elements in all kind of
60 : * algorithms.
61 : *
62 : * Please note that the selector has to be registered at a
63 : * grid before it may be used. You may register it using the constructor
64 : * or the method assign_grid.
65 : *
66 : * This is a specialization of ISelector for the Grid class.
67 : *
68 : * The following methods are the most used:
69 : * - select, deselect, is_selected (see ISelector)
70 : * - begin, end, num, clear.
71 : *
72 : * You may specify the element-type on which begin, end, num and clear
73 : * operate via a template parameter:
74 : *
75 : *
76 : * \code
77 : * Grid g;
78 : * Selector sel(g);
79 : *
80 : * // ... create elements and select some
81 : *
82 : * // number of selected vertices
83 : * int nSelVrts = sel.num<Vertex>();
84 : *
85 : * // number of selected triangles
86 : * int nSelTris = sel.num<Triangle>();
87 : *
88 : * // iteration over all faces
89 : * for(FaceIterator iter = sel.begin<Face>();
90 : * iter != sel.end<Face>(); ++iter){
91 : * // ...
92 : * }
93 : * \endcode
94 : */
95 : class UG_API Selector : public ISelector
96 : {
97 : public:
98 : typedef ISelector BaseClass;
99 : typedef Grid grid_type;
100 :
101 : /// The traits class holds some important types for each element-type
102 : template <class TElem>
103 : struct traits{
104 : typedef typename geometry_traits<TElem>::iterator iterator;
105 : typedef typename geometry_traits<TElem>::const_iterator const_iterator;
106 : typedef typename geometry_traits<TElem>::iterator level_iterator;
107 : typedef typename geometry_traits<TElem>::const_iterator const_level_iterator;
108 : };
109 :
110 : public:
111 : Selector(uint supportedElements = SE_ALL);
112 : Selector(Grid& grid, uint supportedElements = SE_ALL);
113 : virtual ~Selector();
114 :
115 : void assign_grid(Grid& grid);
116 : void assign_grid(Grid* grid);
117 :
118 : /// set the type of elements that shall be handled by the Selector.
119 : /** Pass an or-combination of constants enumerated in SelectorElements.
120 : * \sa Selector::enable_element_support*/
121 : // forwards to protected ISelector method. This rather complicated setup
122 : // is required to avoid virtual method calls during construction.
123 : void set_supported_elements(uint shElements);
124 :
125 : /// enable support for element-types. Does not invalidate previous settings.
126 : /** pass an or-combination of constants enumerated in SelectorElements.*/
127 : // forwards to protected ISelector method. This rather complicated setup
128 : // is required to avoid virtual method calls during construction.
129 : void enable_element_support(uint shElements);
130 :
131 : /// disable support for element-types.
132 : /** pass an or-combination of constants enumerated in SelectorElements.*/
133 : // forwards to protected ISelector method. This rather complicated setup
134 : // is required to avoid virtual method calls during construction.
135 : void disable_element_support(uint shElements);
136 :
137 : virtual void clear();
138 :
139 : template <class TElem>
140 : inline void clear();
141 :
142 : template <class TElem>
143 : inline size_t num() const;
144 :
145 : inline size_t num() const;
146 :
147 : // empty
148 : inline bool empty() const;
149 :
150 : template <class TElem>
151 : inline bool empty() const;
152 :
153 : // begin
154 : template <class TElem>
155 : inline typename geometry_traits<TElem>::iterator
156 : begin();
157 :
158 : template <class TElem>
159 : inline typename geometry_traits<TElem>::const_iterator
160 : begin() const;
161 :
162 : // end
163 : template <class TElem>
164 : inline typename geometry_traits<TElem>::iterator
165 : end();
166 :
167 : template <class TElem>
168 : inline typename geometry_traits<TElem>::const_iterator
169 : end() const;
170 :
171 : // convenience begin and end
172 0 : inline VertexIterator vertices_begin() {return begin<Vertex>();}
173 : inline VertexIterator vertices_end() {return end<Vertex>();}
174 0 : inline EdgeIterator edges_begin() {return begin<Edge>();}
175 : inline EdgeIterator edges_end() {return end<Edge>();}
176 0 : inline FaceIterator faces_begin() {return begin<Face>();}
177 : inline FaceIterator faces_end() {return end<Face>();}
178 0 : inline VolumeIterator volumes_begin() {return begin<Volume>();}
179 : inline VolumeIterator volumes_end() {return end<Volume>();}
180 :
181 : /// returns the first selected element of the given type.
182 : /** Make sure that elements of the given type exist!
183 : * Behaviour is undefined, if not.*/
184 : template <class TElem> TElem* front();
185 :
186 : /// returns the last selected element of the given type.
187 : /** Make sure that elements of the given type exist!
188 : * Behaviour is undefined, if not.*/
189 : template <class TElem> TElem* back();
190 :
191 : // geometric-object-collection
192 : virtual GridObjectCollection get_grid_objects() const;
193 :
194 : // callbacks that allows us to clean-up
195 : // derived from GridObserver
196 : //virtual void unregistered_from_grid(Grid* grid);
197 : virtual void grid_to_be_destroyed(Grid* grid);
198 :
199 : ////////////////////////////////////////
200 : // for compatibility with MGSelector
201 : /// always returns 1
202 : inline size_t num_levels() const;
203 :
204 : /// calls num();
205 : inline uint num(size_t) const;
206 : /// calls num<TElem>();
207 : template <class TElem> inline size_t num(size_t) const;
208 :
209 : // empty
210 : /// calls empty();
211 : inline bool empty(size_t) const;
212 : // calls empty<TElem>();
213 : template <class TElem>
214 : inline bool empty(size_t) const;
215 :
216 : // begin
217 : /// calls begin<TElem>();
218 : template <class TElem>
219 : inline typename geometry_traits<TElem>::iterator
220 : begin(size_t);
221 :
222 : // end
223 : /// calls end<TElem>();
224 : template <class TElem>
225 : inline typename geometry_traits<TElem>::iterator
226 : end(size_t);
227 :
228 : /// returns true if the selector contains vertices
229 0 : virtual bool contains_vertices() const {return num<Vertex>() > 0;}
230 :
231 : /// returns true if the selector contains edges
232 0 : virtual bool contains_edges() const {return num<Edge>() > 0;}
233 :
234 : /// returns true if the selector contains faces
235 0 : virtual bool contains_faces() const {return num<Face>() > 0;}
236 :
237 : /// returns true if the selector contains volumes
238 0 : virtual bool contains_volumes() const {return num<Volume>() > 0;}
239 :
240 : protected:
241 : using ISelector::AttachedVertexList;
242 : using ISelector::AttachedEdgeList;
243 : using ISelector::AttachedFaceList;
244 : using ISelector::AttachedVolumeList;
245 :
246 : using ISelector::VertexSectionContainer;
247 : using ISelector::EdgeSectionContainer;
248 : using ISelector::FaceSectionContainer;
249 : using ISelector::VolumeSectionContainer;
250 :
251 : protected:
252 : void clear_lists();
253 :
254 : virtual void add_to_list(Vertex* elem);
255 : virtual void add_to_list(Edge* elem);
256 : virtual void add_to_list(Face* elem);
257 : virtual void add_to_list(Volume* elem);
258 :
259 : virtual void erase_from_list(Vertex* elem);
260 : virtual void erase_from_list(Edge* elem);
261 : virtual void erase_from_list(Face* elem);
262 : virtual void erase_from_list(Volume* elem);
263 :
264 : /// returns the iterator at which the given element lies in the section container
265 : /** This method may only be called if the element is indeed selected
266 : * \{
267 : */
268 : inline VertexSectionContainer::iterator
269 : get_iterator(Vertex* o)
270 : {
271 : assert(is_selected(o) && "object not selected.");
272 : return section_container<Vertex>().get_container().get_iterator(o);
273 : }
274 :
275 : inline EdgeSectionContainer::iterator
276 : get_iterator(Edge* o)
277 : {
278 : assert(is_selected(o) && "object not selected");
279 : return section_container<Edge>().get_container().get_iterator(o);
280 : }
281 :
282 : inline FaceSectionContainer::iterator
283 : get_iterator(Face* o)
284 : {
285 : assert(is_selected(o) && "object not selected");
286 : return section_container<Face>().get_container().get_iterator(o);
287 : }
288 :
289 : inline VolumeSectionContainer::iterator
290 : get_iterator(Volume* o)
291 : {
292 : assert(is_selected(o) && "object not selected");
293 : return section_container<Volume>().get_container().get_iterator(o);
294 : }
295 : /** \} */
296 :
297 : /// returns the section container for the given type, subset and level
298 : template <class TElem> inline
299 : typename Grid::traits<TElem>::SectionContainer&
300 : section_container();
301 :
302 : /// returns the const section container for the given type, subset and level
303 : template <class TElem> inline
304 : const typename Grid::traits<TElem>::SectionContainer&
305 : section_container() const;
306 :
307 : template <class TElem>
308 : inline int get_section_index() const;
309 :
310 : private:
311 : Selector(const Selector& sel){};///< Copy Constructor not yet implemented!
312 :
313 : protected:
314 : VertexSectionContainer m_vertices;
315 : EdgeSectionContainer m_edges;
316 : FaceSectionContainer m_faces;
317 : VolumeSectionContainer m_volumes;
318 : };
319 :
320 : /** \} */
321 : }// end of namespace
322 :
323 : ////////////////////////////////
324 : // include implementation
325 : #include "selector_grid_impl.hpp"
326 :
327 : #endif
|