OpenWalnut  1.5.0dev
WGELinearTranslationCallback.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGELINEARTRANSLATIONCALLBACK_H
26 #define WGELINEARTRANSLATIONCALLBACK_H
27 
28 #include <memory>
29 
30 #include <osg/MatrixTransform>
31 #include <osg/Node>
32 #include <osg/TexMat>
33 #include <osg/Uniform>
34 
35 #include "../../common/WProperties.h"
36 
37 
38 /**
39  * This class is an OSG Callback which allows simple linear translation of a matrix transform node along a specified axis. It is controlled by a
40  * WPropDouble. This way, one can simply implement movable slices and similar.
41  *
42  * \tparam T the type used as control mechanism. Typically, this should be an property whose type is cast-able to double. The type specified must
43  * support access via T->get(). Specialize the class if you do not specify a pointer.
44  */
45 template< typename T >
46 class WGELinearTranslationCallback: public osg::NodeCallback
47 {
48 public:
49  /**
50  * Constructor. Creates the callback. You still need to add it to the desired node.
51  *
52  * \param axe the axe to translate along. Should be normalized. If not, it scales the translation.
53  * \param property the property containing the value
54  * \param texMatrix optional pointer to a texture matrix which can be modified too to contain the normalized translation.
55  * \param scaler scales the property by this value before creating the matrix.
56  */
57  WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix, double scaler = 1.0 );
58 
59  /**
60  * Constructor. Creates the callback. You still need to add it to the desired node.
61  *
62  * \param axe the axe to translate along. Should be normalized. If not, it scales the translation.
63  * \param property the property containing the value
64  * \param uniform optional pointer to a uniform that will contain the matrix. Useful if no tex-matrix is available anymore. The matrix is the
65  * matrix that is NOT scaled to be in texture space.
66  * \param scaler scales the property by this value before creating the matrix.
67  */
68  WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform, double scaler = 1.0 );
69 
70  /**
71  * Constructor. Creates the callback. You still need to add it to the desired node.
72  *
73  * \param axe the axe to translate along. Should be normalized. If not, it scales the translation.
74  * \param property the property containing the value
75  * \param scaler scales the property by this value before creating the matrix.
76  */
77  WGELinearTranslationCallback( osg::Vec3 axe, T property, double scaler = 1.0 );
78 
79  /**
80  * Destructor.
81  */
83 
84  /**
85  * This operator gets called by OSG every update cycle. It moves the underlying MatrixTransform according to the specified axis and value.
86  *
87  * \param node the osg node
88  * \param nv the node visitor
89  */
90  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
91 
92 protected:
93  /**
94  * The axis to transform along.
95  */
96  osg::Vec3 m_axe;
97 
98  /**
99  * The position
100  */
101  T m_pos;
102 
103  /**
104  * Cache the old position for proper update
105  */
106  double m_oldPos;
107 
108  /**
109  * Texture matrix that contains normalized translation.
110  */
111  osg::ref_ptr< osg::TexMat > m_texMat;
112 
113  /**
114  * The uniform to set the matrix to.
115  */
116  osg::ref_ptr< osg::Uniform > m_uniform;
117 
118  /**
119  * Scale the property prior to creating the matrix.
120  */
121  double m_scaler;
122 private:
123 };
124 
125 template< typename T >
126 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix, double scaler ):
127  osg::NodeCallback(),
128  m_axe( axe ),
129  m_pos( property ),
130  m_oldPos( -1.0 ),
131  m_texMat( texMatrix ),
132  m_scaler( scaler )
133 {
134  // initialize members
135 }
136 
137 template< typename T >
138 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, osg::ref_ptr< osg::Uniform > uniform, double scaler ):
139  osg::NodeCallback(),
140  m_axe( axe ),
141  m_pos( property ),
142  m_oldPos( -1.0 ),
143  m_uniform( uniform ),
144  m_scaler( scaler )
145 {
146  // initialize members
147 }
148 
149 template< typename T >
150 WGELinearTranslationCallback< T >::WGELinearTranslationCallback( osg::Vec3 axe, T property, double scaler ):
151  osg::NodeCallback(),
152  m_axe( axe ),
153  m_pos( property ),
154  m_oldPos( -1.0 ),
155  m_scaler( scaler )
156 {
157  // initialize members
158 }
159 
160 template< typename T >
162 {
163  // cleanup
164 }
165 
166 template< typename T >
167 void WGELinearTranslationCallback< T >::operator()( osg::Node* node, osg::NodeVisitor* nv )
168 {
169  // this node is a MatrixTransform
170  float newPos = m_pos->get();
171  if( newPos != m_oldPos )
172  {
173  m_oldPos = newPos;
174  osg::MatrixTransform* m = dynamic_cast< osg::MatrixTransform* >( node );
175  if( m )
176  {
177  std::shared_ptr< WPropertyConstraintMax< typename T::element_type::DataType > > mx = m_pos->getMax();
178  std::shared_ptr< WPropertyConstraintMin< typename T::element_type::DataType > > mn = m_pos->getMin();
179 
180  if( mn && mx )
181  {
182  float max = mx->getMax();
183  float min = mn->getMin();
184  float size = max - min;
185  float axeLen = m_axe.length();
186 
187  osg::Vec3 translation = m_axe * m_scaler * static_cast< float >( m_oldPos - min );
188 
189  // set both matrices
190  if( m_texMat )
191  {
192  m_texMat->setMatrix( osg::Matrix::translate( translation / size / axeLen ) );
193  }
194  if( m_uniform )
195  {
196  m_uniform->set( osg::Matrix::translate( translation ) );
197  }
198 
199  m->setMatrix( osg::Matrix::translate( translation ) );
200  }
201  }
202  }
203 
204  traverse( node, nv );
205 }
206 
207 #endif // WGELINEARTRANSLATIONCALLBACK_H
208 
This class is an OSG Callback which allows simple linear translation of a matrix transform node along...
WGELinearTranslationCallback(osg::Vec3 axe, T property, osg::ref_ptr< osg::TexMat > texMatrix, double scaler=1.0)
Constructor.
virtual ~WGELinearTranslationCallback()
Destructor.
double m_scaler
Scale the property prior to creating the matrix.
double m_oldPos
Cache the old position for proper update.
osg::ref_ptr< osg::TexMat > m_texMat
Texture matrix that contains normalized translation.
osg::ref_ptr< osg::Uniform > m_uniform
The uniform to set the matrix to.
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
This operator gets called by OSG every update cycle.
osg::Vec3 m_axe
The axis to transform along.