33 #ifndef __H__UG_raster_impl
34 #define __H__UG_raster_impl
50 template <
class T,
int TDIM>
55 template <
class T,
int TDIM>
62 template <
class T,
int TDIM>
69 template <
class T,
int TDIM>
73 for(
int d = 0; d < TDIM; ++d)
77 template <
class T,
int TDIM>
84 template <
class T,
int TDIM>
95 template <
class T,
int TDIM>
100 template <
class T,
int TDIM>
107 template <
class T,
int TDIM>
111 for(
int d = 0; d < TDIM; ++d)
115 template <
class T,
int TDIM>
122 template <
class T,
int TDIM>
126 for(
int d = 0; d < TDIM; ++d)
131 template <
class T,
int TDIM>
138 template <
class T,
int TDIM>
145 template <
class T,
int TDIM>
149 for(
int d = 0; d < TDIM; ++d)
153 template <
class T,
int TDIM>
157 for(
int d = 0; d < TDIM; ++d)
161 template <
class T,
int TDIM>
165 for(
int d = 0; d < TDIM; ++d)
174 template <
class T,
int TDIM>
192 template <
class T,
int TDIM>
196 m_numNodes(raster.m_numNodes),
197 m_selNode(raster.m_selNode),
198 m_minCorner(raster.m_minCorner),
199 m_extension(raster.m_extension),
200 m_cellExtension(raster.m_cellExtension),
201 m_cursor(raster.m_cursor),
202 m_numNodesTotal(raster.m_numNodesTotal),
203 m_noDataValue(raster.m_noDataValue)
213 template <
class T,
int TDIM>
217 m_numNodes(numNodes),
224 m_noDataValue(
std::numeric_limits<T>::max())
228 for(
int d = 0; d < TDIM; ++d)
236 template <
class T,
int TDIM>
242 m_numNodes(numNodes),
244 m_minCorner(minCorner),
245 m_extension(extension),
249 m_noDataValue(
std::numeric_limits<T>::max())
257 template <
class T,
int TDIM>
265 template <
class T,
int TDIM>
276 update_num_nodes_total();
277 update_cell_extension();
281 memcpy(m_data, raster.
m_data, num_nodes_total() *
sizeof(T));
286 template <
class T,
int TDIM>
293 template <
class T,
int TDIM>
297 m_numNodes[
dim] = num;
298 update_num_nodes_total();
299 update_cell_extension(
dim);
302 template <
class T,
int TDIM>
307 update_num_nodes_total();
308 update_cell_extension();
311 template <
class T,
int TDIM>
315 return m_numNodesTotal;
318 template <
class T,
int TDIM>
322 return m_numNodes[
dim];
325 template <
class T,
int TDIM>
333 template <
class T,
int TDIM>
342 update_num_nodes_total();
344 const size_t num = num_nodes_total();
351 template <
class T,
int TDIM>
355 return m_data[data_index(mi)];
358 template <
class T,
int TDIM>
362 return m_data[data_index(mi)];
366 template <
class T,
int TDIM>
370 m_minCorner[
dim] = coord;
373 template <
class T,
int TDIM>
380 template <
class T,
int TDIM>
387 template <
class T,
int TDIM>
391 return m_minCorner[
dim];
394 template <
class T,
int TDIM>
398 m_extension[
dim] = ext;
399 update_cell_extension(
dim);
402 template <
class T,
int TDIM>
407 update_cell_extension();
410 template <
class T,
int TDIM>
417 template <
class T,
int TDIM>
421 return m_extension[
dim];
425 template <
class T,
int TDIM>
432 for(
size_t d = 0; d < TDIM; ++d)
434 mi[d] =
static_cast<int>(0.5 + (coord[d] - m_minCorner[d]) / m_cellExtension[d]);
435 if(mi[d] < 0) mi[d] = 0;
436 else if(mi[d] >= num_nodes(d)) mi[d] = num_nodes(d) - 1;
438 return node_value(mi);
444 for(
size_t d = 0; d < TDIM; ++d)
446 mi[d] =
static_cast<int>((coord[d] - m_minCorner[d]) / m_cellExtension[d]);
451 else if(mi[d] + 1 >= num_nodes(d)){
452 mi[d] = num_nodes(d) - 2;
457 - ((
number)mi[d] * m_cellExtension[d]
459 / m_cellExtension[d];
463 return interpolate_linear(mi, lc);
467 UG_THROW(
"Raster::interpolate(): Unsupported interpolation order: " << order);
469 return m_noDataValue;
473 template <
class T,
int TDIM>
480 template <
class T,
int TDIM>
484 return m_noDataValue;
488 template <
class T,
int TDIM>
495 for(
size_t iiter = 0; iiter < iterations; ++iiter)
496 run_on_all (blurKernel);
500 template <
class T,
int TDIM>
501 template <
class TKernel>
507 return kernel.result();
510 template <
class T,
int TDIM>
511 template <
class TKernel>
519 template <
class T,
int TDIM>
520 template <
class TKernel>
525 const size_t numNodes = num_nodes(curDim);
526 for(
MultiIndex cur = start; cur[curDim] < numNodes; ++cur[curDim]){
527 run_on_all(cur, kernel, curDim - 1);
531 const size_t numNodes = num_nodes(0);
532 for(
MultiIndex cur = start; cur[0] < numNodes; ++cur[0]){
539 template <
class T,
int TDIM>
540 template <
class TKernel>
545 run_on_nbrs(center, kernel, TDIM - 1);
546 return kernel.result();
550 template <
class T,
int TDIM>
551 template <
class TKernel>
555 run_on_nbrs(center, kernel, TDIM - 1);
559 template <
class T,
int TDIM>
560 template <
class TKernel>
565 run_on_nbrs(center, kernel, curDim - 1);
567 if(center[curDim] > 0){
573 if(center[curDim] + 1 < num_nodes(curDim)){
581 template <
class T,
int TDIM>
585 m_selNode[
dim] = index;
588 template <
class T,
int TDIM>
596 template <
class T,
int TDIM>
600 node_value(m_selNode) = val;
603 template <
class T,
int TDIM>
607 return node_value(m_selNode);
611 template <
class T,
int TDIM>
615 m_cursor[
dim] = coord;
618 template <
class T,
int TDIM>
626 template <
class T,
int TDIM>
630 return interpolate(m_cursor, order);
634 template <
class T,
int TDIM>
640 #define LFA_ERR_WHERE "Error in Raster::load_from_asc('" << filename << "'): "
642 #define LFA_CHECK_DIM(d, line)\
643 UG_COND_THROW(d >= TDIM, LFA_ERR_WHERE << "Bad dimension '" << d <<\
644 "' in line " << line << " of file " << filename << "," <<\
645 "while trying to read a " << TDIM << "d raster.");
649 LFA_ERR_WHERE <<
"Couldn't find the specified file in any of the standard paths.");
651 ifstream in(fullFileName.c_str());
670 if(TDIM == 1) headerLen = 4;
671 else if(TDIM == 2) headerLen = 6;
672 else if(TDIM == 3) headerLen = 8;
674 UG_THROW(
"Raster::load_from_asc only supports 1, 2, and 3 dimensions\n");
677 for(
int i = 0; i < headerLen; ++i){
682 "Couldn't parse expected name-value pair in row " << i);
686 if(
name.compare(
"ncols") == 0){
688 numNodes[0] = (int)value;
690 else if(
name.compare(
"nrows") == 0){
692 numNodes[1] = (int)value;
694 else if(
name.compare(
"nstacks") == 0){
696 numNodes[2] = (int)value;
699 else if(
name.compare(
"xllcenter") == 0){
702 minCoordIsCenter[0] = 1;
704 else if(
name.compare(
"yllcenter") == 0){
707 minCoordIsCenter[1] = 1;
709 else if(
name.compare(
"zllcenter") == 0){
712 minCoordIsCenter[2] = 1;
714 else if(
name.compare(
"xllcorner") == 0){
718 else if(
name.compare(
"yllcorner") == 0){
722 else if(
name.compare(
"zllcorner") == 0){
727 else if(
name.compare(
"cellsize") == 0){
728 for(
int d = 0; d < TDIM; ++d)
732 else if(
name.compare(
"xcellsize") == 0){
735 headerLen += (TDIM - 1);
738 else if(
name.compare(
"ycellsize") == 0){
742 else if(
name.compare(
"zcellsize") == 0){
747 else if(
name.compare(
"nodata_value") == 0){
756 for(
int d = 0; d < TDIM; ++d){
757 if(minCoordIsCenter[d])
758 minCoord[d] -= 0.5 * cellSize[d];
762 for(
int d = 0; d < TDIM; ++d){
767 set_num_nodes(numNodes);
768 set_min_corner(minCoord);
770 for(
int d = 0; d < TDIM; ++d)
771 extension[d] *= (
number)(numNodes[d] - 1);
772 set_extension(extension);
773 set_no_data_value(noDataValue);
779 size_t num[3] = {0, 1, 1};
780 for(
size_t i = 0; i < TDIM; ++i)
781 num[i] = m_numNodes[i];
783 for(
size_t iz = 0; iz < num[2]; ++iz){
784 for(
size_t iy = 0; iy < num[1]; ++iy){
785 for(
size_t ix = 0; ix < num[0]; ++ix)
787 const size_t ty = num[1] - 1 - iy;
788 const size_t tz = num[2] - 1 - iz;
789 in >> m_data[ix + num[0] * (ty + num[1] * tz)];
791 << ix <<
", " << iy <<
", " << iz <<
")");
797 template <
class T,
int TDIM>
802 #define STA_ERR_WHERE "Error in Raster::save_to_asc('" << filename << "'): "
805 "Please call 'create' or 'load_from_asc' first.");
807 ofstream out(filename);
810 out <<
"ncols " << num_nodes(0) << endl;
812 out <<
"nrows " << num_nodes(1) << endl;
814 out <<
"nstacks " << num_nodes(2) << endl;
816 out <<
"xllcorner " << setprecision(16) << min_corner(0) << endl;
818 out <<
"yllcorner " << setprecision(16) << min_corner(1) << endl;
820 out <<
"zllcorner " << setprecision(16) << min_corner(2) << endl;
822 bool equlateralCells =
true;
823 for(
int d = 1; d < TDIM; ++d){
824 if(m_cellExtension[d] != m_cellExtension[0]){
825 equlateralCells =
false;
831 out <<
"cellsize " << m_cellExtension[0] << endl;
833 out <<
"xcellsize " << m_cellExtension[0] << endl;
835 out <<
"ycellsize " << m_cellExtension[1] << endl;
837 out <<
"zcellsize " << m_cellExtension[2] << endl;
840 out <<
"NODATA_value " << no_data_value() << endl;
844 size_t num[3] = {0, 1, 1};
845 for(
size_t i = 0; i < TDIM; ++i)
846 num[i] = num_nodes(i);
848 for(
size_t iz = 0; iz < num[2]; ++iz){
849 for(
size_t iy = 0; iy < num[1]; ++iy){
850 for(
size_t ix = 0; ix < num[0]; ++ix)
854 const size_t ty = num[1] - 1 - iy;
855 const size_t tz = num[2] - 1 - iz;
856 out << m_data[ix + num[0] * (ty + num[1] * tz)];
867 template <
class T,
int TDIM>
872 return curVal + mi[0];
874 return data_index(mi, curDim - 1, m_numNodes[curDim - 1] * (mi[curDim] + curVal));
878 template <
class T,
int TDIM>
883 for(
int d = 0; d < TDIM; ++d)
884 m_numNodesTotal *= num_nodes(d);
887 template <
class T,
int TDIM>
891 for(
int d = 0; d < TDIM; ++d)
892 update_cell_extension(d);
895 template <
class T,
int TDIM>
899 if(m_numNodes[
dim] > 1 && m_extension[
dim] > 0)
900 m_cellExtension[
dim] = m_extension[
dim] / (m_numNodes[
dim] - 1);
902 m_cellExtension[
dim] = 1;
906 template <
class T,
int TDIM>
914 return node_value(minNodeInd);
917 miMax[curDim - 1] += 1;
919 T val0 = interpolate_linear(minNodeInd, localCoord, curDim - 1);
920 T val1 = interpolate_linear(miMax, localCoord, curDim - 1);
923 val0 *= (1. - localCoord[curDim - 1]);
924 val1 *= localCoord[curDim - 1];
location name
Definition: checkpoint_util.lua:128
a mathematical Vector with N entries.
Definition: math_vector.h:97
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
Coordinate & operator+=(const Coordinate &c)
Definition: raster_impl.hpp:147
int dim() const
Definition: raster_impl.hpp:117
void set(number c)
Definition: raster_impl.hpp:124
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 ¢er)
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
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
Kernel which blurs all values of a raster it was called on.
Definition: raster_kernels.h:134
std::string FindFileInStandardPaths(const char *filename)
searches the file in the standard paths.
Definition: file_util.cpp:196
#define UG_THROW(msg)
Definition: error.h:57
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
double number
Definition: types.h:124
function util LuaCallbackHelper create(func)
Definition: smart_pointer.h:814
string ToLower(string str)
Definition: string_util.cpp:269
#define LFA_CHECK_DIM(d, line)