Subversion Repositories gelsvn

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 89 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
/** @file BitMask.h
-
 
8
 * @brief BitMask class for selecting bit subsets ... possibly a bit dusty
-
 
9
 */
-
 
10
 
1
#ifndef __CGLA__BITMASK_H__
11
#ifndef __CGLA__BITMASK_H__
2
#define __CGLA__BITMASK_H__
12
#define __CGLA__BITMASK_H__
3
 
13
 
4
#include "Vec3i.h"
14
#include "Vec3i.h"
5
 
15
 
6
namespace CGLA
16
namespace CGLA
7
{
17
{
8
	const int MASKS[33] = 
18
	const int MASKS[33] = 
9
		{
19
		{
10
			0x00000000,
20
			0x00000000,
11
			0x00000001,
21
			0x00000001,
12
			0x00000003,
22
			0x00000003,
13
			0x00000007,
23
			0x00000007,
14
			0x0000000f,
24
			0x0000000f,
15
			0x0000001f,
25
			0x0000001f,
16
			0x0000003f,
26
			0x0000003f,
17
			0x0000007f,
27
			0x0000007f,
18
			0x000000ff,
28
			0x000000ff,
19
			0x000001ff,
29
			0x000001ff,
20
			0x000003ff,
30
			0x000003ff,
21
			0x000007ff,
31
			0x000007ff,
22
			0x00000fff,
32
			0x00000fff,
23
			0x00001fff,
33
			0x00001fff,
24
			0x00003fff,
34
			0x00003fff,
25
			0x00007fff,
35
			0x00007fff,
26
			0x0000ffff,
36
			0x0000ffff,
27
			0x0001ffff,
37
			0x0001ffff,
28
			0x0003ffff,
38
			0x0003ffff,
29
			0x0007ffff,
39
			0x0007ffff,
30
			0x000fffff,
40
			0x000fffff,
31
			0x001fffff,
41
			0x001fffff,
32
			0x003fffff,
42
			0x003fffff,
33
			0x007fffff,
43
			0x007fffff,
34
			0x00ffffff,
44
			0x00ffffff,
35
			0x01ffffff,
45
			0x01ffffff,
36
			0x03ffffff,
46
			0x03ffffff,
37
			0x07ffffff,
47
			0x07ffffff,
38
			0x0fffffff,
48
			0x0fffffff,
39
			0x1fffffff,
49
			0x1fffffff,
40
			0x3fffffff,
50
			0x3fffffff,
41
			0x7fffffff,
51
			0x7fffffff,
42
			0xffffffff
52
			static_cast<int>(0xffffffff)
43
		};
53
		};
44
 
54
 
45
	/** \brief The BitMask class is mostly a utility class.
55
	/** \brief The BitMask class is mostly a utility class.
46
 
56
 
47
			The main purpose is to be able to extract a set of bits from
57
			The main purpose is to be able to extract a set of bits from
48
			an integer. For instance this can be useful if we traverse
58
			an integer. For instance this can be useful if we traverse
49
			some tree structure and the integer is the index. */
59
			some tree structure and the integer is the index. */
50
	class BitMask
60
	class BitMask
51
	{
61
	{
52
		int fb, lb, bdiff;
62
		int fb, lb, bdiff;
53
		int msk;
63
		int msk;
54
 
64
 
55
	public:
65
	public:
56
 
66
 
57
		/** Mask _fb-_lb+1 bits beginning from _fb. First bit is 0.
67
		/** Mask _fb-_lb+1 bits beginning from _fb. First bit is 0.
58
				Say _fb=_lb=0. In this case, mask 1 bit namely 0.*/
68
				Say _fb=_lb=0. In this case, mask 1 bit namely 0.*/
59
		BitMask(int _fb, int _lb):
69
		BitMask(int _fb, int _lb):
60
			fb(_fb), lb(_lb), 
70
			fb(_fb), lb(_lb), 
61
			bdiff(lb-fb+1), msk(MASKS[bdiff]<<fb)
71
			bdiff(lb-fb+1), msk(MASKS[bdiff]<<fb)
62
		{}
72
		{}
63
	
73
	
64
		/// first bit is 0 mask num bits.
74
		/// first bit is 0 mask num bits.
65
		BitMask(int num):
75
		BitMask(int num):
66
			fb(0),lb(two_to_what_power(num)-1), 
76
			fb(0),lb(two_to_what_power(num)-1), 
67
			bdiff(lb-fb+1), msk(MASKS[bdiff]<<fb)
77
			bdiff(lb-fb+1), msk(MASKS[bdiff]<<fb)
68
		{}
78
		{}
69
	
79
	
70
		/// Mask everything.
80
		/// Mask everything.
71
		BitMask():
81
		BitMask():
72
			fb(0), lb(15),
82
			fb(0), lb(15),
73
			bdiff(lb-fb+1), msk(MASKS[bdiff]<<fb)
83
			bdiff(lb-fb+1), msk(MASKS[bdiff]<<fb)
74
		{}
84
		{}
75
 
85
 
76
		/// get number of first bit in mask
86
		/// get number of first bit in mask
77
		int first_bit() const {return fb;} 
87
		int first_bit() const {return fb;} 
78
 
88
 
79
		/// get number of last bit in mask
89
		/// get number of last bit in mask
80
 		int last_bit() const {return lb;}
90
 		int last_bit() const {return lb;}
81
 
91
 
82
		/// Return number of masked bits
92
		/// Return number of masked bits
83
		int no_bits() const {return bdiff;}
93
		int no_bits() const {return bdiff;}
84
 
94
 
85
		/// Mask a number
95
		/// Mask a number
86
		int mask(int var) const {return msk&var;}
96
		int mask(int var) const {return msk&var;}
87
		
97
		
88
		/** Mask a number and shift back so the first bit inside
98
		/** Mask a number and shift back so the first bit inside
89
				the mask becomes bit 0. */
99
				the mask becomes bit 0. */
90
		int mask_shift(int var) const {return (msk&var)>>fb;}
100
		int mask_shift(int var) const {return (msk&var)>>fb;}
91
 
101
 
92
		/** Mask a vector by masking each coordinate. */
102
		/** Mask a vector by masking each coordinate. */
93
		Vec3i mask(const Vec3i& v) const 
103
		Vec3i mask(const Vec3i& v) const 
94
		{
104
		{
95
			return Vec3i(mask(v[0]),mask(v[1]),mask(v[2]));
105
			return Vec3i(mask(v[0]),mask(v[1]),mask(v[2]));
96
		}
106
		}
97
  
107
  
98
		/** Mask each coord of a vector and shift */
108
		/** Mask each coord of a vector and shift */
99
		Vec3i maskshift(const Vec3i& v) const 
109
		Vec3i maskshift(const Vec3i& v) const 
100
		{
110
		{
101
			return Vec3i(mask_shift(v[0]),mask_shift(v[1]),mask_shift(v[2]));
111
			return Vec3i(mask_shift(v[0]),mask_shift(v[1]),mask_shift(v[2]));
102
		}
112
		}
103
  
113
  
104
	};
114
	};
105
 
115
 
106
}
116
}
107
#endif
117
#endif
108
 
118