ug4
raster.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016: 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 #ifndef __H__UG_raster
34 #define __H__UG_raster
35 
37 
38 namespace ug{
39 
41 
72 template <class T, int TDIM>
73 class Raster{
74  public:
75  class MultiIndex {
76  public:
77  MultiIndex();
78  MultiIndex(size_t i);
79  int dim () const;
80  void set (size_t i);
81  size_t& operator[] (int d);
82  size_t operator[] (int d) const;
83 
84  friend std::ostream& operator << (std::ostream& o, const MultiIndex& mi)
85  {
86  o << "(";
87  for(size_t d = 0; d < TDIM; ++d){
88  o << mi[d];
89  if(d + 1 < TDIM)
90  o << ", ";
91  }
92  o << ")";
93  return o;
94  }
95 
96  private:
97  size_t m_ind[TDIM];
98  };
99 
100  class Coordinate {
101  public:
102  Coordinate();
103  Coordinate(number c);
105 
106  int dim () const;
107  void set (number c);
108  number& operator[] (int d);
109  number operator[] (int d) const;
110  Coordinate& operator+= (const Coordinate& c);
111  Coordinate& operator-= (const Coordinate& c);
113 
114  friend std::ostream& operator << (std::ostream& o, const Coordinate& coord)
115  {
116  o << "(";
117  for(size_t d = 0; d < TDIM; ++d){
118  o << coord[d];
119  if(d + 1 < TDIM)
120  o << ", ";
121  }
122  o << ")";
123  return o;
124  }
125 
126  private:
128  };
129 
131 
133  Raster ();
134 
136  Raster (const Raster& raster);
137 
139 
144  Raster (const MultiIndex& numNodes);
145 
147 
149  Raster (const MultiIndex& numNodes,
150  const Coordinate& extension,
151  const Coordinate& minCorner = Coordinate(0));
152 
153  ~Raster ();
154 
155  Raster& operator= (const Raster& raster);
156 
157  void load_from_asc (const char* filename);
158  void save_to_asc (const char* filename) const;
159 
160  int dim () const;
161 
163 
165  void set_num_nodes (int dim, size_t num);
166  void set_num_nodes (const MultiIndex& mi);
167 
169  size_t num_nodes_total () const;
171  size_t num_nodes (int dim) const;
173  const MultiIndex& num_nodes () const;
174 
176  void create ();
177 
179  T& node_value (const MultiIndex& mi);
180 
182  T node_value (const MultiIndex& mi) const;
183 
185 
186  void set_min_corner (int dim, number coord);
187  void set_min_corner (const Coordinate& coord);
191  const Coordinate& min_corner () const;
192 
194  number min_corner (int dim) const;
195 
197 
198  void set_extension (int dim, number ext);
199  void set_extension (const Coordinate& coord);
203  const Coordinate& extension () const;
204 
206  number extension (int dim) const;
207 
209  void set_no_data_value(T val);
210 
212  T no_data_value() const;
213 
215  T interpolate (const Coordinate& coord, int order) const;
216 
218  void blur(T alpha, size_t iterations);
219 
220 
222 
231  template <class TKernel>
232  typename TKernel::result_t
233  run_on_all();
234 
236 
248  template <class TKernel>
249  void run_on_all(TKernel& kernel);
250 
251 
253 
262  template <class TKernel>
263  typename TKernel::result_t
264  run_on_nbrs(const MultiIndex& center);
265 
267 
279  template <class TKernel>
280  void run_on_nbrs(const MultiIndex& center, TKernel& kernel);
281 
283  // Convenience methods for easy scripting below.
284 
286 
287  void select_node (int dim, size_t index);
288  void select_node (const MultiIndex& mi);
292  void set_selected_node_value (T val);
293 
295  T selected_node_value () const;
296 
298 
299  void set_cursor (int dim, number coord);
300  void set_cursor (const Coordinate& coord);
304  T interpolate_at_cursor (int order) const;
305 
306  private:
307  template <class TKernel>
308  void run_on_all(const MultiIndex& start, TKernel& kernel, int curDim);
309 
310  template <class TKernel>
311  void run_on_nbrs(const MultiIndex& center, TKernel& kernel, int curDim);
312 
313  size_t data_index (
314  const MultiIndex& mi,
315  int curDim = TDIM - 1,
316  size_t curVal = 0) const;
317 
318  void update_num_nodes_total();
319  void update_cell_extension ();
320  void update_cell_extension (int dim);
321 
323  const MultiIndex& minNodeInd,
324  Coordinate& localCoord,
325  int curDim = TDIM) const;
326 
327  T* m_data;
336 };
337 
338 }// end of namespace
339 
341 // include implementation
342 #include "raster_impl.hpp"
343 
344 #endif //__H__UG_raster
parameterString s
a mathematical Vector with N entries.
Definition: math_vector.h:97
Definition: raster.h:100
Coordinate & operator-=(const Coordinate &c)
Definition: raster_impl.hpp:155
Coordinate()
Definition: raster_impl.hpp:97
Coordinate & operator*=(number s)
Definition: raster_impl.hpp:163
number & operator[](int d)
Definition: raster_impl.hpp:133
number m_coord[TDIM]
Definition: raster.h:127
Coordinate & operator+=(const Coordinate &c)
Definition: raster_impl.hpp:147
friend std::ostream & operator<<(std::ostream &o, const Coordinate &coord)
Definition: raster.h:114
int dim() const
Definition: raster_impl.hpp:117
void set(number c)
Definition: raster_impl.hpp:124
Definition: raster.h:75
size_t m_ind[TDIM]
Definition: raster.h:97
friend std::ostream & operator<<(std::ostream &o, const MultiIndex &mi)
Definition: raster.h:84
int dim() const
Definition: raster_impl.hpp:64
void set(size_t i)
Definition: raster_impl.hpp:71
MultiIndex()
Definition: raster_impl.hpp:52
size_t & operator[](int d)
Definition: raster_impl.hpp:79
Generic raster for arbitrary dimensions.
Definition: raster.h:73
void set_cursor(int dim, number coord)
Set the coordinate of the cursor. The cursor can be used to interpolate values.
Definition: raster_impl.hpp:613
T & node_value(const MultiIndex &mi)
returns the value at the given multi-index (read/write)
Definition: raster_impl.hpp:353
Raster()
Creates an empty raster that has to be initialized before use.
Definition: raster_impl.hpp:176
size_t num_nodes_total() const
returns the total number of nodes in the raster
Definition: raster_impl.hpp:313
void select_node(int dim, size_t index)
Select a node. 'selected_node_value' provides read/write access to the selected node.
Definition: raster_impl.hpp:583
const Coordinate & min_corner() const
returns the min-corner of the raster
Definition: raster_impl.hpp:382
void set_num_nodes(int dim, size_t num)
sets the number of nodes that shall be used by the raster.
Definition: raster_impl.hpp:295
MultiIndex m_selNode
Definition: raster.h:329
void blur(T alpha, size_t iterations)
blurs (smoothens) the values by repeatedly averaging between direct neighbors
Definition: raster_impl.hpp:490
Coordinate m_extension
Definition: raster.h:331
T interpolate_linear(const MultiIndex &minNodeInd, Coordinate &localCoord, int curDim=TDIM) const
Definition: raster_impl.hpp:908
void update_num_nodes_total()
Definition: raster_impl.hpp:880
Coordinate m_cursor
Definition: raster.h:333
T * m_data
Definition: raster.h:327
TKernel::result_t run_on_all()
Creates and runs the specified kernel on all nodes and returns its result.
Definition: raster_impl.hpp:503
TKernel::result_t run_on_nbrs(const MultiIndex &center)
Creates and runs the specified kernel on all direct neighbors of a node and returns its result.
Definition: raster_impl.hpp:542
void load_from_asc(const char *filename)
Definition: raster_impl.hpp:636
T no_data_value() const
returns the value that shall be considered 'no-data-value'
Definition: raster_impl.hpp:482
void set_min_corner(int dim, number coord)
sets the min corner of the raster. Used for interpolation at cursor.
Definition: raster_impl.hpp:368
T interpolate_at_cursor(int order) const
interpolates the value with the given order at the cursor position
Definition: raster_impl.hpp:628
MultiIndex m_numNodes
Definition: raster.h:328
void set_extension(int dim, number ext)
sets the extension of the raster. Used for interpolation at cursor.
Definition: raster_impl.hpp:396
Coordinate m_cellExtension
Definition: raster.h:332
void set_no_data_value(T val)
sets the value that shall be considered as 'no-data-value'
Definition: raster_impl.hpp:475
T m_noDataValue
Definition: raster.h:335
~Raster()
Definition: raster_impl.hpp:259
void create()
creates the raster. Call this method after 'set_num_nodes' has been called for each dimension.
Definition: raster_impl.hpp:335
size_t m_numNodesTotal
Definition: raster.h:334
void set_num_nodes(const MultiIndex &mi)
size_t data_index(const MultiIndex &mi, int curDim=TDIM - 1, size_t curVal=0) const
Definition: raster_impl.hpp:869
T selected_node_value() const
returns the value of the selected node
Definition: raster_impl.hpp:605
Coordinate m_minCorner
Definition: raster.h:330
T interpolate(const Coordinate &coord, int order) const
interpolates the value with the given order at the given coordinate
Definition: raster_impl.hpp:427
Raster & operator=(const Raster &raster)
Definition: raster_impl.hpp:267
void update_cell_extension()
Definition: raster_impl.hpp:889
const Coordinate & extension() const
returns the extension of the raster
Definition: raster_impl.hpp:412
const MultiIndex & num_nodes() const
returns the number of nodes for each dimension in a multi-index.
Definition: raster_impl.hpp:327
void set_selected_node_value(T val)
sets the value of the selected node
Definition: raster_impl.hpp:598
void save_to_asc(const char *filename) const
Definition: raster_impl.hpp:799
int dim() const
Definition: raster_impl.hpp:288
number alpha
double number
Definition: types.h:124
the ug namespace
typedefs for ugmath