1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dan.guilliams@gmail.com *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
40 /***************************************************************************\
42 \***************************************************************************/
44 #include "OSGTravMaskGraphOp.h"
45 #include "OSGGraphOpFactory.h"
46 #include "OSGNameAttachment.h"
52 /***************************************************************************\
54 \***************************************************************************/
56 /*! \class TravMaskGraphOp
57 \ingroup GrpSystemNodeCoresDrawablesGeometry
59 A class used to change traversal masks of nodes meeting certain criteria.
65 //! Register the GraphOp with the factory
66 static bool registerOp(void)
68 GraphOpRefPtr newOp
= TravMaskGraphOp::create();
70 GraphOpFactory::the()->registerOp(newOp
);
74 static StaticInitFuncWrapper
registerOpWrapper(registerOp
);
78 /***************************************************************************\
80 \***************************************************************************/
82 /*-------------------------------------------------------------------------*\
84 \*-------------------------------------------------------------------------*/
87 /*------------- constructors & destructors --------------------------------*/
89 TravMaskGraphOp::TravMaskGraphOp(void) :
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 ),
106 mApplyNewMaskOperation (BIT_EQUAL
),
110 mMatchRegex
= boost::xpressive::cregex::compile(
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(
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
)
211 ps("newtraversalmask", mNewTravMask
);
212 ps("applymasktoalldecendents", mApplyMaskToAllDecendents
);
213 ps("applytononmatching", mApplyToNonMatching
);
214 //ps("ApplyNewMaskOperation", mApplyNewMaskOperation);
218 ps("matchname", mMatchName
);
220 std::string MatchRegex
;
222 ps("matchregex", MatchRegex
);
223 ps("matchwholename", mMatchWholeName
);
225 mMatchRegex
= boost::xpressive::cregex::compile(
227 boost::xpressive::regex_constants::icase
);
230 ps("matchnodecoretype", mMatchNodeCoreType
);
231 ps("matchderivedcoretypes", mMatchDerivedCoreTypes
);
233 std::string NodeCoreTypeName
;
235 ps("nodecoretypename", NodeCoreTypeName
);
238 FieldContainerFactory::the()->findType(NodeCoreTypeName
.c_str());
241 ps("matchcurtravmask", mMatchCurTravMask
);
242 ps("matchcurtravmaskvalue", mMatchCurTravMaskValue
);
243 //ps("MatchMaskCondition",mMatchMaskCondition);
245 std::string out
= ps
.getUnusedParams();
249 FWARNING(("TravMaskGraphOp doesn't have parameters '%s'.\n",
254 std::string
TravMaskGraphOp::usage(void)
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)
279 /*-------------------------------------------------------------------------*\
281 \*-------------------------------------------------------------------------*/
284 /*-------------------------------------------------------------------------*\
286 \*-------------------------------------------------------------------------*/
288 Action::ResultE
TravMaskGraphOp::traverseEnter(Node
* const node
)
295 const Char8
*namePtr
= OSG::getName(node
);
304 setMask
= boost::xpressive::regex_match (namePtr
, mMatchRegex
);
308 setMask
= boost::xpressive::regex_search(namePtr
, mMatchRegex
);
314 if(mMatchNodeCoreType
&& mNodeCoreType
!= NULL
)
316 if(mMatchDerivedCoreTypes
)
318 if(node
->getCore()->getType().isDerivedFrom(*mNodeCoreType
))
325 if(node
->getCore()->getType() == *mNodeCoreType
)
333 if(mMatchCurTravMask
)
337 switch(mMatchMaskCondition
)
340 BitTest
= 0x0000 != (node
->getTravMask() &
341 mMatchCurTravMaskValue
);
344 BitTest
= 0x0000 != (node
->getTravMask() |
345 mMatchCurTravMaskValue
);
348 BitTest
= 0x0000 != (node
->getTravMask() ^
349 mMatchCurTravMaskValue
);
353 BitTest
= (node
->getTravMask() != mNewTravMask
);
357 BitTest
= (node
->getTravMask() == mNewTravMask
);
367 //If the mask should be applied to non-matching
369 if(mApplyToNonMatching
)
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();
392 //Apply the new traversal mask
395 switch(mApplyNewMaskOperation
)
398 NewMask
= node
->getTravMask() & mNewTravMask
;
401 NewMask
= node
->getTravMask() | mNewTravMask
;
404 NewMask
= node
->getTravMask() ^ mNewTravMask
;
407 NewMask
= ~node
->getTravMask();
411 NewMask
= mNewTravMask
;
414 node
->setTravMask(NewMask
);
420 return Action::Continue
;
423 Action::ResultE
TravMaskGraphOp::traverseLeave(Node
* const node
,
424 Action::ResultE res
)