Panda3D
physxGroupsMask.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file physxGroupsMask.cxx
10  * @author enn0x
11  * @date 2009-10-21
12  */
13 
14 #include "physxGroupsMask.h"
15 
16 using std::string;
17 
18 /**
19  * Returns a PhysxGroupsMask whose bits are all on.
20  */
22 all_on() {
23 
24  PhysxGroupsMask mask;
25  mask._mask.bits0 = 0xffffffff;
26  mask._mask.bits1 = 0xffffffff;
27  mask._mask.bits2 = 0xffffffff;
28  mask._mask.bits3 = 0xffffffff;
29  return mask;
30 }
31 
32 /**
33  * Returns a PhysxGroupsMask whose bits are all off.
34  */
37 
38  PhysxGroupsMask mask;
39 
40  mask._mask.bits0 = 0x0000000;
41  mask._mask.bits1 = 0x0000000;
42  mask._mask.bits2 = 0x0000000;
43  mask._mask.bits3 = 0x0000000;
44 
45  return mask;
46 }
47 
48 /**
49  * Sets the nth bit on. Index must be in the range [0, 127].
50  */
52 set_bit(unsigned int idx) {
53 
54  nassertv_always(idx >= 0 && idx <= 127);
55 
56  NxU32 bits = 1 << (idx % 32);
57 
58  if (idx < 32) {
59  _mask.bits0 |= bits;
60  }
61  else if (idx < 64) {
62  _mask.bits1 |= bits;
63  }
64  else if (idx < 96) {
65  _mask.bits2 |= bits;
66  }
67  else {
68  _mask.bits3 |= bits;
69  }
70 }
71 
72 /**
73  * Sets the nth bit off. Index must be in the range [0, 127].
74  */
76 clear_bit(unsigned int idx) {
77 
78  nassertv_always(idx >= 0 && idx <= 127);
79 
80  NxU32 bits = 1 << (idx % 32);
81 
82  if (idx < 32) {
83  _mask.bits0 = _mask.bits0 & ~bits;
84  }
85  else if (idx < 64) {
86  _mask.bits1 = _mask.bits1 & ~bits;
87  }
88  else if (idx < 96) {
89  _mask.bits2 = _mask.bits2 & ~bits;
90  }
91  else {
92  _mask.bits3 = _mask.bits3 & ~bits;
93  }
94 }
95 
96 /**
97  * Returns true if the nth bit is set, false if it is cleared. Index must be
98  * in the range [0, 127].
99  */
101 get_bit(unsigned int idx) const {
102 
103  nassertr_always(idx >= 0 && idx <= 127, false);
104 
105  NxU32 bits = 1 << (idx % 32);
106 
107  if (idx < 32) {
108  return (_mask.bits0 & bits) ? true : false;
109  }
110  else if (idx < 64) {
111  return (_mask.bits1 & bits) ? true : false;
112  }
113  else if (idx < 96) {
114  return (_mask.bits2 & bits) ? true : false;
115  }
116  else {
117  return (_mask.bits3 & bits) ? true : false;
118  }
119 }
120 
121 /**
122  * Writes the PhysxGroupsMask out as a list of ones and zeros.
123  */
125 output(std::ostream &out) const {
126 
127  string name0;
128  string name1;
129  string name2;
130  string name3;
131 
132  for (int i=0; i<32; i++) {
133  name0 += (_mask.bits0 & (1 << i)) ? '1' : '0';
134  name1 += (_mask.bits1 & (1 << i)) ? '1' : '0';
135  name2 += (_mask.bits2 & (1 << i)) ? '1' : '0';
136  name3 += (_mask.bits3 & (1 << i)) ? '1' : '0';
137  }
138 
139  out << "/" << name0 << "-" << name1 << "-" << name2 << "-" << name3 << "/";
140 }
static PhysxGroupsMask all_on()
Returns a PhysxGroupsMask whose bits are all on.
void clear_bit(unsigned int idx)
Sets the nth bit off.
void set_bit(unsigned int idx)
Sets the nth bit on.
static PhysxGroupsMask all_off()
Returns a PhysxGroupsMask whose bits are all off.
void output(std::ostream &out) const
Writes the PhysxGroupsMask out as a list of ones and zeros.
bool get_bit(unsigned int idx) const
Returns true if the nth bit is set, false if it is cleared.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
128-bit bitmask class.