fixed: compile issue
[opensg.git] / Source / System / GraphOp / OSGTravMaskGraphOp.cpp
blob11f4b618ce2cb2140a6cb58c40ae1e62e3639279
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dan.guilliams@gmail.com *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
40 /***************************************************************************\
41 * Includes *
42 \***************************************************************************/
44 #include "OSGTravMaskGraphOp.h"
45 #include "OSGGraphOpFactory.h"
46 #include "OSGNameAttachment.h"
47 #include <string>
50 OSG_USING_NAMESPACE
52 /***************************************************************************\
53 * Description *
54 \***************************************************************************/
56 /*! \class TravMaskGraphOp
57 \ingroup GrpSystemNodeCoresDrawablesGeometry
59 A class used to change traversal masks of nodes meeting certain criteria.
63 namespace
65 //! Register the GraphOp with the factory
66 static bool registerOp(void)
68 GraphOpRefPtr newOp = TravMaskGraphOp::create();
70 GraphOpFactory::the()->registerOp(newOp);
71 return true;
74 static StaticInitFuncWrapper registerOpWrapper(registerOp);
76 } // namespace
78 /***************************************************************************\
79 * Instance methods *
80 \***************************************************************************/
82 /*-------------------------------------------------------------------------*\
83 - public -
84 \*-------------------------------------------------------------------------*/
87 /*------------- constructors & destructors --------------------------------*/
89 TravMaskGraphOp::TravMaskGraphOp(void) :
90 Inherited ( ),
91 mMatchName (true ),
92 mMatchRegex ( ),
93 mMatchWholeName (true ),
95 mMatchNodeCoreType (false ),
96 mNodeCoreType (NULL ),
97 mMatchDerivedCoreTypes (true ),
99 mMatchCurTravMask (false ),
100 mMatchCurTravMaskValue (1 ),
101 mMatchMaskCondition (BIT_EQUAL),
103 mApplyMaskToAllDecendents(false ),
104 mApplyToNonMatching (false ),
105 mNewTravMask (1 ),
106 mApplyNewMaskOperation (BIT_EQUAL),
108 mNumChanged (0 )
110 mMatchRegex = boost::xpressive::cregex::compile(
111 ".*",
112 boost::xpressive::regex_constants::icase);
115 TravMaskGraphOp::~TravMaskGraphOp(void)
119 TravMaskGraphOpTransitPtr TravMaskGraphOp::create(void)
121 return TravMaskGraphOpTransitPtr(new TravMaskGraphOp());
124 GraphOpTransitPtr TravMaskGraphOp::clone(void)
126 return GraphOpTransitPtr(new TravMaskGraphOp());
129 bool TravMaskGraphOp::traverse(Node *root)
131 return GraphOp::traverse(root);
134 void TravMaskGraphOp::setMatchWholeName(bool value)
136 mMatchWholeName = value;
139 void TravMaskGraphOp::setMatchDerivedCoreTypes(bool value)
141 mMatchDerivedCoreTypes = value;
144 void TravMaskGraphOp::setNewTravMaskOperation(UInt8 ApplyNewMaskOperation)
146 mApplyNewMaskOperation = ApplyNewMaskOperation;
149 void TravMaskGraphOp::setMatchMaskCondition(UInt8 MatchMaskCondition)
151 mMatchMaskCondition = MatchMaskCondition;
154 void TravMaskGraphOp::setApplyMaskToAllDecendents(bool ApplyMaskToAllDecendents)
156 mApplyMaskToAllDecendents = ApplyMaskToAllDecendents;
159 void TravMaskGraphOp::setApplyToNonMatching(bool ApplyToNonMatching)
161 mApplyToNonMatching = ApplyToNonMatching;
165 void TravMaskGraphOp::setMatchRegex(const std::string& MatchName)
167 mMatchRegex = boost::xpressive::cregex::compile(
168 MatchName,
169 boost::xpressive::regex_constants::icase);
172 void TravMaskGraphOp::setMatchRegex(const boost::xpressive::cregex &MatchRegex)
174 mMatchRegex = MatchRegex;
177 void TravMaskGraphOp::setNodeCoreType(const std::string &TypeName)
179 mNodeCoreType = FieldContainerFactory::the()->findType(TypeName.c_str());
182 void TravMaskGraphOp::setNewTravMask(UInt32 NewTraversalMask)
184 mNewTravMask = NewTraversalMask;
187 void TravMaskGraphOp::setCurrentTravMaskValue(UInt32 CurrentTraversalMask)
189 mMatchCurTravMaskValue = CurrentTraversalMask;
192 void TravMaskGraphOp::setMatchName(bool MatchName)
194 mMatchName = MatchName;
197 void TravMaskGraphOp::setMatchNodeCoreType(bool MatchCore)
199 mMatchNodeCoreType = MatchCore;
202 void TravMaskGraphOp::setMatchCurrentTravMask(bool MatchCurMask)
204 mMatchCurTravMask = MatchCurMask;
207 void TravMaskGraphOp::setParams(const std::string params)
209 ParamSet ps(params);
211 ps("newtraversalmask", mNewTravMask );
212 ps("applymasktoalldecendents", mApplyMaskToAllDecendents);
213 ps("applytononmatching", mApplyToNonMatching );
214 //ps("ApplyNewMaskOperation", mApplyNewMaskOperation);
217 //Name Matching
218 ps("matchname", mMatchName);
220 std::string MatchRegex;
222 ps("matchregex", MatchRegex );
223 ps("matchwholename", mMatchWholeName);
225 mMatchRegex = boost::xpressive::cregex::compile(
226 MatchRegex,
227 boost::xpressive::regex_constants::icase);
229 //Type Matching
230 ps("matchnodecoretype", mMatchNodeCoreType );
231 ps("matchderivedcoretypes", mMatchDerivedCoreTypes);
233 std::string NodeCoreTypeName;
235 ps("nodecoretypename", NodeCoreTypeName);
237 mNodeCoreType =
238 FieldContainerFactory::the()->findType(NodeCoreTypeName.c_str());
240 //Mask Matching
241 ps("matchcurtravmask", mMatchCurTravMask );
242 ps("matchcurtravmaskvalue", mMatchCurTravMaskValue);
243 //ps("MatchMaskCondition",mMatchMaskCondition);
245 std::string out = ps.getUnusedParams();
247 if(out.length())
249 FWARNING(("TravMaskGraphOp doesn't have parameters '%s'.\n",
250 out.c_str()));
254 std::string TravMaskGraphOp::usage(void)
256 return
257 "NodeNameTravMask: Changes traversal masks of nodes based on certain criteria.\n"
258 "Params: name (type, default)\n"
259 " NewTraversalMask (UInt32, 0): Value to set the traversal mask to if it meets the matching criteria\n"
260 " ApplyMaskToAllDecendents (bool, false): Applies the mask operation to all decendents of a matching node\n"
261 " ApplyToNonMatching (bool, false): Applies the mask operation to all non-matching nodes\n"
263 " MatchName (string, \"\"): Name to search for in the node's name.\n"
264 " MatchWholeName (bool, true): Matches only if the entire name matches the search regex\n"
266 " NodeCoreTypeID (UInt32, 0): NodeCoreTypeID to check for.\n"
267 " MatchDerivedCoreTypes (bool, true): Will match all nodes whos type is derived from the searched NodeCore type\n"
269 " CurTraversalMask (UInt32, 1): Current Traversal mask to check for.\n";
273 UInt32 TravMaskGraphOp::getNumChanged(void)
275 return mNumChanged;
279 /*-------------------------------------------------------------------------*\
280 - protected -
281 \*-------------------------------------------------------------------------*/
284 /*-------------------------------------------------------------------------*\
285 - private -
286 \*-------------------------------------------------------------------------*/
288 Action::ResultE TravMaskGraphOp::traverseEnter(Node * const node)
290 bool setMask(false);
292 //Name Matching
293 if(mMatchName)
295 const Char8 *namePtr = OSG::getName(node);
297 if(namePtr == NULL)
299 namePtr = "";
302 if(mMatchWholeName)
304 setMask = boost::xpressive::regex_match (namePtr, mMatchRegex);
306 else
308 setMask = boost::xpressive::regex_search(namePtr, mMatchRegex);
313 //Type Matching
314 if(mMatchNodeCoreType && mNodeCoreType != NULL)
316 if(mMatchDerivedCoreTypes)
318 if(node->getCore()->getType().isDerivedFrom(*mNodeCoreType))
320 setMask = true;
323 else
325 if(node->getCore()->getType() == *mNodeCoreType)
327 setMask = true;
332 //Mask Matching
333 if(mMatchCurTravMask)
335 bool BitTest(false);
337 switch(mMatchMaskCondition)
339 case BIT_AND:
340 BitTest = 0x0000 != (node->getTravMask() &
341 mMatchCurTravMaskValue);
342 break;
343 case BIT_OR:
344 BitTest = 0x0000 != (node->getTravMask() |
345 mMatchCurTravMaskValue);
346 break;
347 case BIT_XOR:
348 BitTest = 0x0000 != (node->getTravMask() ^
349 mMatchCurTravMaskValue);
350 break;
351 case BIT_NOT:
352 case BIT_NOT_EQUAL:
353 BitTest = (node->getTravMask() != mNewTravMask);
354 break;
355 case BIT_EQUAL:
356 default:
357 BitTest = (node->getTravMask() == mNewTravMask);
358 break;
361 if(BitTest)
363 setMask = true;
367 //If the mask should be applied to non-matching
368 //then flip setMask
369 if(mApplyToNonMatching)
371 setMask = !setMask;
374 if(setMask)
376 if(mApplyMaskToAllDecendents)
378 TravMaskGraphOpRefPtr colMeshGrOp = TravMaskGraphOp::create();
380 colMeshGrOp->setMatchRegex(boost::xpressive::cregex::compile(".*"));
382 colMeshGrOp->setNewTravMask (mNewTravMask );
383 colMeshGrOp->setNewTravMaskOperation(mApplyNewMaskOperation);
384 colMeshGrOp->traverse (node );
386 mNumChanged += colMeshGrOp->getNumChanged();
388 return Action::Skip;
390 else
392 //Apply the new traversal mask
393 UInt32 NewMask;
395 switch(mApplyNewMaskOperation)
397 case BIT_AND:
398 NewMask = node->getTravMask() & mNewTravMask;
399 break;
400 case BIT_OR:
401 NewMask = node->getTravMask() | mNewTravMask;
402 break;
403 case BIT_XOR:
404 NewMask = node->getTravMask() ^ mNewTravMask;
405 break;
406 case BIT_NOT:
407 NewMask = ~node->getTravMask();
408 break;
409 case BIT_EQUAL:
410 default:
411 NewMask = mNewTravMask;
412 break;
414 node->setTravMask(NewMask);
416 ++mNumChanged;
420 return Action::Continue;
423 Action::ResultE TravMaskGraphOp::traverseLeave(Node * const node,
424 Action::ResultE res )
426 return res;