-
Notifications
You must be signed in to change notification settings - Fork 10
/
NodeMask.cpp
174 lines (152 loc) · 4.43 KB
/
NodeMask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "NodeMask.h"
#include <QDebug>
#include <osg/NodeVisitor>
#include <osg/Group>
#include <osg/Drawable>
#include <bitset>
//static const int bitsForDisplayMask = 12;
static const unsigned defaultMask = 0x0fff;
const char * const NodeMask::yadda = "hello";
const char * const NodeMask::bitNames[] = {
"Unselected", // 0
"Selected", // 1
"Hidden", // 2
"", // 3
"", // 4
"", // 5
"", // 6
"Shotline", // 7
"Group", // 8
"Axis", // 9
"CuttingPlane", // 10
"PointIndicator"// 11
};
class GroupLeafNodeSetVisitor : public osg::NodeVisitor
{
public:
GroupLeafNodeSetVisitor(unsigned int mask=defaultMask,
TraversalMode tm=TRAVERSE_ALL_CHILDREN)
: NodeVisitor(tm)
, m_mask(mask)
{ // force traversal of all nodes
_traversalMask = _nodeMaskOverride = ~0;
}
virtual void apply (osg::Group &node) {
node.setNodeMask(NodeMask::GROUP|m_mask);
traverse(node);
}
virtual void apply (osg::Drawable &node) {
node.setNodeMask(m_mask);
this->traverse(node);
}
private:
unsigned m_mask;
};
/// Assign a nodemask to nodes
/// This overrides any nodeMask that has been set entirely
class NodeMaskSetVisitor : public osg::NodeVisitor
{
public:
NodeMaskSetVisitor(unsigned int mask=defaultMask,
TraversalMode tm=TRAVERSE_ALL_CHILDREN)
: NodeVisitor(tm)
, m_mask(mask)
{ // force traversal of all nodes
_traversalMask = _nodeMaskOverride = ~0;
}
virtual void apply (osg::Node &node) {
node.setNodeMask(m_mask);
this->traverse(node);
}
private:
unsigned m_mask;
};
/// Set individual bits of the NodeMask in a tree by doing an OR with the
/// provided bitmask. This is usefull for turning on additional bits in
/// the nodemask
class NodeMaskOrBitVisitor : public osg::NodeVisitor
{
public:
NodeMaskOrBitVisitor(unsigned int mask=defaultMask,
TraversalMode tm=TRAVERSE_ALL_CHILDREN)
: NodeVisitor(tm)
, m_mask(mask)
{ // force traversal of all nodes
_traversalMask = _nodeMaskOverride = ~0;
}
virtual void apply (osg::Node &node) {
this->traverse(node);
unsigned int before = node.getNodeMask();
unsigned int after = before | m_mask;
node.setNodeMask(after);
}
private:
unsigned m_mask;
};
/// Clear individual bits of the nodemask in a tree by doing
/// and AND with the inverse of the bits set in the mask provided.
class NodeMaskAndBitVisitor : public osg::NodeVisitor
{
public:
NodeMaskAndBitVisitor(unsigned int mask=defaultMask,
TraversalMode tm=TRAVERSE_ALL_CHILDREN)
: NodeVisitor(tm)
, m_mask(mask)
{ // force traversal of all nodes
_traversalMask = _nodeMaskOverride = ~0;
}
virtual void apply (osg::Node &node) {
this->traverse(node);
unsigned int before = node.getNodeMask();
unsigned int after = before & m_mask;
node.setNodeMask(after);
}
private:
unsigned m_mask;
};
QString NodeMask::maskToString(unsigned mask)
{
std::bitset<bitsForDisplayMask> bits(mask);
QString s = QString::fromStdString(bits.to_string());
s.insert(8, ' ');
s.insert(4, ' ');
return s;
}
unsigned NodeMask::stringToMask(QString s)
{
s.remove(' ');
std::bitset<bitsForDisplayMask> bits(s.toStdString());
return (unsigned)bits.to_ulong();
}
void NodeMask::setNodeMasksOnHeirarchy(osg::Node *n, NodeMaskValue mask)
{
NodeMaskSetVisitor nmsv(mask);
n->accept(nmsv);
}
void NodeMask::setNodeMasksBitOnHeirarchy(osg::Node *n, NodeMaskValue mask)
{
unsigned settingMask = mask & 0x0ff;
NodeMaskOrBitVisitor nmbsv(settingMask);
n->accept(nmbsv);
}
void NodeMask::clearNodeMasksBitOnHeirarchy(osg::Node *n, NodeMaskValue mask)
{
unsigned clearingMask = ~(mask & 0x0ff);
NodeMaskAndBitVisitor nmcbv(clearingMask);
n->accept(nmcbv);
}
void NodeMask::markGroups(osg::Node *n)
{
GroupLeafNodeSetVisitor gv;
n->accept(gv);
}
void NodeMask::markGroupsAndLeafNodes(osg::Node *n, NodeMask::NodeMaskValue mask)
{
GroupLeafNodeSetVisitor gv(mask);
n->accept(gv);
}
#include <QRegExpValidator>
QRegExpValidator *NodeMask::createValidator()
{
return new QRegExpValidator(QRegExp("([01]* )*[01]+"));
}