Subversion Repositories gelsvn

Rev

Rev 595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/**
8
 * @file Grid2D.h
9
 * @brief A simple 2D grid template that is often useful.
10
 */
11
 
448 jab 12
#ifndef __UTIL_GRID2D_H
13
#define __UTIL_GRID2D_H
77 jab 14
 
15
#include <vector>
601 jab 16
#include "../CGLA/Vec2i.h"
77 jab 17
 
18
namespace Util
19
{
20
 
21
	template<class T>
22
	class Grid2D
23
	{
24
		int XDIM, YDIM;
25
		std::vector<T> pixels;
26
 
27
	public:
28
 
29
		~Grid2D() {}
30
 
31
		Grid2D(int i, int j, const T& val): XDIM(i), YDIM(j), pixels(i*j, val) {}
32
		Grid2D(int i, int j): XDIM(i), YDIM(j), pixels(i*j) {}
315 jab 33
		Grid2D(): XDIM(0), YDIM(0) {}
34
 
35
		void resize(int i, int j) 
36
				{
37
						XDIM = i;
38
						YDIM = j;
39
						pixels.resize(i*j);
40
				}
41
 
77 jab 42
 
43
		const T& operator()(int i, int j) const 
44
		{
45
		  assert(i>=0 && i< XDIM);
46
		  assert(j>=0 && j< YDIM);
47
		  return pixels[j*XDIM+i];
48
		}
49
 
50
		T& operator()(int i, int j) 
51
		{
52
		  assert(i>=0 && i< XDIM);
53
		  assert(j>=0 && j< YDIM);
54
		  return pixels[j*XDIM+i];
55
		}
56
 
57
		const T& operator()(const CGLA::Vec2i& p) const 
58
		{
59
		  assert(p[0]>=0 && p[0]< XDIM);
60
		  assert(p[1]>=0 && p[1]< YDIM);
61
		  return pixels[p[1]*XDIM+p[0]];
62
		}
63
 
64
		T& operator()(const CGLA::Vec2i& p) 
65
		{
66
		  assert(p[0]>=0 && p[0]< XDIM);
67
		  assert(p[1]>=0 && p[1]< YDIM);
68
		  return pixels[p[1]*XDIM+p[0]];
69
		}
70
 
71
		int get_xdim() const {return XDIM;}
72
		int get_ydim() const {return YDIM;}
73
 
74
	};
75
}
76
 
77
#endif