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