Subversion Repositories gelsvn

Rev

Rev 448 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 448 Rev 595
-
 
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
 
1
#ifndef __UTIL_GRID2D_H
12
#ifndef __UTIL_GRID2D_H
2
#define __UTIL_GRID2D_H
13
#define __UTIL_GRID2D_H
3
 
14
 
4
#include <vector>
15
#include <vector>
5
#include "CGLA/Vec2i.h"
16
#include "CGLA/Vec2i.h"
6
 
17
 
7
namespace Util
18
namespace Util
8
{
19
{
9
 
20
 
10
	template<class T>
21
	template<class T>
11
	class Grid2D
22
	class Grid2D
12
	{
23
	{
13
		int XDIM, YDIM;
24
		int XDIM, YDIM;
14
		std::vector<T> pixels;
25
		std::vector<T> pixels;
15
 
26
 
16
	public:
27
	public:
17
 
28
 
18
		~Grid2D() {}
29
		~Grid2D() {}
19
		
30
		
20
		Grid2D(int i, int j, const T& val): XDIM(i), YDIM(j), pixels(i*j, val) {}
31
		Grid2D(int i, int j, const T& val): XDIM(i), YDIM(j), pixels(i*j, val) {}
21
		Grid2D(int i, int j): XDIM(i), YDIM(j), pixels(i*j) {}
32
		Grid2D(int i, int j): XDIM(i), YDIM(j), pixels(i*j) {}
22
		Grid2D(): XDIM(0), YDIM(0) {}
33
		Grid2D(): XDIM(0), YDIM(0) {}
23
 
34
 
24
		void resize(int i, int j) 
35
		void resize(int i, int j) 
25
				{
36
				{
26
						XDIM = i;
37
						XDIM = i;
27
						YDIM = j;
38
						YDIM = j;
28
						pixels.resize(i*j);
39
						pixels.resize(i*j);
29
				}
40
				}
30
 
41
 
31
		
42
		
32
		const T& operator()(int i, int j) const 
43
		const T& operator()(int i, int j) const 
33
		{
44
		{
34
		  assert(i>=0 && i< XDIM);
45
		  assert(i>=0 && i< XDIM);
35
		  assert(j>=0 && j< YDIM);
46
		  assert(j>=0 && j< YDIM);
36
		  return pixels[j*XDIM+i];
47
		  return pixels[j*XDIM+i];
37
		}
48
		}
38
 
49
 
39
		T& operator()(int i, int j) 
50
		T& operator()(int i, int j) 
40
		{
51
		{
41
		  assert(i>=0 && i< XDIM);
52
		  assert(i>=0 && i< XDIM);
42
		  assert(j>=0 && j< YDIM);
53
		  assert(j>=0 && j< YDIM);
43
		  return pixels[j*XDIM+i];
54
		  return pixels[j*XDIM+i];
44
		}
55
		}
45
 
56
 
46
		const T& operator()(const CGLA::Vec2i& p) const 
57
		const T& operator()(const CGLA::Vec2i& p) const 
47
		{
58
		{
48
		  assert(p[0]>=0 && p[0]< XDIM);
59
		  assert(p[0]>=0 && p[0]< XDIM);
49
		  assert(p[1]>=0 && p[1]< YDIM);
60
		  assert(p[1]>=0 && p[1]< YDIM);
50
		  return pixels[p[1]*XDIM+p[0]];
61
		  return pixels[p[1]*XDIM+p[0]];
51
		}
62
		}
52
 
63
 
53
		T& operator()(const CGLA::Vec2i& p) 
64
		T& operator()(const CGLA::Vec2i& p) 
54
		{
65
		{
55
		  assert(p[0]>=0 && p[0]< XDIM);
66
		  assert(p[0]>=0 && p[0]< XDIM);
56
		  assert(p[1]>=0 && p[1]< YDIM);
67
		  assert(p[1]>=0 && p[1]< YDIM);
57
		  return pixels[p[1]*XDIM+p[0]];
68
		  return pixels[p[1]*XDIM+p[0]];
58
		}
69
		}
59
 
70
 
60
		int get_xdim() const {return XDIM;}
71
		int get_xdim() const {return XDIM;}
61
		int get_ydim() const {return YDIM;}
72
		int get_ydim() const {return YDIM;}
62
		
73
		
63
	};
74
	};
64
}
75
}
65
 
76
 
66
#endif
77
#endif
67
 
78