CEDRIC  Revision_backup-2009-02
TypedTransform.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Boris Iven, Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef TYPEDTRANSFORM_H
10 #define TYPEDTRANSFORM_H
11 
12 #define TYPEDTRANSFORM_ADDCHILD_MACRO( OSGADDFUNC ) \
13  T* node = dynamic_cast<T*>( child ); \
14  if( node && !childrenAllowed() \
15  && !(node->getClassName().compare(getClassName())==0) )\
16  return false; \
17  if( !OSGADDFUNC ) return false; \
18  if( node ) updateType( node->getType() ); \
19  return true;
20 
21 #include <osg/MatrixTransform>
22 #include <osg/Node>
23 
24 #include <string>
25 
26 class TypedTransform : public osg::MatrixTransform
27 {
28 public:
29  typedef TypedTransform T;
30 
32 
33  TypedTransform( std::string className ): m_className(className) {}
34 
35  virtual bool childrenAllowed() { return true; }
36 
37  const std::string& getClassName() const { return m_className; }
38  int getType() const { return m_type; }
39 
40 
41  virtual bool addChild( osg::Node* child ) { TYPEDTRANSFORM_ADDCHILD_MACRO( MatrixTransform::addChild(child) ) }
42  virtual bool insertChild( unsigned int index, osg::Node *child ) { TYPEDTRANSFORM_ADDCHILD_MACRO( MatrixTransform::insertChild(index,child) ) }
43  virtual bool setChild( unsigned int index, osg::Node *child ) { TYPEDTRANSFORM_ADDCHILD_MACRO( MatrixTransform::setChild(index,child) ) }
44 
45  virtual bool replaceChild( osg::Node *origChild, osg::Node *child )
46  {
47  if( !MatrixTransform::replaceChild( origChild, child ) )
48  return false;
49 
50  updateType();
51  return true;
52  }
53 
54  virtual bool removeChildren( unsigned int pos, unsigned int numChildrenToRemove )
55  {
56  if( !( (pos+numChildrenToRemove-1) < _children.size() && numChildrenToRemove > 0 ) )
57  return false;
58 
59  bool atLeastOneTNodeRemoved = false;
60  for( unsigned int i=pos; i < pos+numChildrenToRemove; ++i )
61  {
62  T* child = dynamic_cast<T*>( getChild( i ) );
63  if( child )
64  {
65  atLeastOneTNodeRemoved = true;
66  break; // leave for-loop
67  }
68  }
69 
70  if( !MatrixTransform::removeChildren( pos, numChildrenToRemove ) )
71  return false;
72 
73  if( atLeastOneTNodeRemoved ) updateType();
74  return true;
75  }
76 
77 protected:
78  void updateType( int type=UNSPECIFIED )
79  {
80  // if type is not specified acquire it from children
81  if( type == UNSPECIFIED )
82  {
83  int acquired_type = ((getNumChildren()>0) ? GROUP : UNSPECIFIED);
84  for( unsigned int i=0; i < getNumChildren(); ++i )
85  {
86  T* child = dynamic_cast<T*>( getChild(i) );
87  if( child )
88  acquired_type |= child->getType();
89  }
90  m_type = acquired_type;
91  }
92  else
93  // add to own type
94  m_type |= type;
95 
96  // tell all parents
97  for( unsigned int i=0; i < getNumParents(); ++i )
98  {
99  T* parent = dynamic_cast<T*>( getParent(i) );
100  if( parent )
101  parent->updateType( m_type );
102  }
103  }
104 
105  int m_type;
106  std::string m_className;
107 };
108 
109 #endif
virtual bool insertChild(unsigned int index, osg::Node *child)
Definition: TypedTransform.h:42
virtual bool replaceChild(osg::Node *origChild, osg::Node *child)
Definition: TypedTransform.h:45
virtual bool childrenAllowed()
Definition: TypedTransform.h:35
const std::string & getClassName() const
Definition: TypedTransform.h:37
virtual bool removeChildren(unsigned int pos, unsigned int numChildrenToRemove)
Definition: TypedTransform.h:54
std::string m_className
Definition: TypedTransform.h:106
virtual bool setChild(unsigned int index, osg::Node *child)
Definition: TypedTransform.h:43
Definition: TypedTransform.h:31
#define TYPEDTRANSFORM_ADDCHILD_MACRO(OSGADDFUNC)
Definition: TypedTransform.h:12
Definition: TypedTransform.h:26
TypedTransform(std::string className)
Definition: TypedTransform.h:33
ReservedTypes
Definition: TypedTransform.h:31
Definition: TypedTransform.h:31
virtual bool addChild(osg::Node *child)
Definition: TypedTransform.h:41
TypedTransform T
Definition: TypedTransform.h:29
int m_type
Definition: TypedTransform.h:105
int getType() const
Definition: TypedTransform.h:38
void updateType(int type=UNSPECIFIED)
Definition: TypedTransform.h:78