Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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