OpenWalnut  1.5.0dev
WGEColormapping.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 WGECOLORMAPPING_H
26 #define WGECOLORMAPPING_H
27 
28 #include <algorithm>
29 #include <functional>
30 #include <map>
31 #include <memory>
32 #include <string>
33 #include <vector>
34 
35 #include <boost/function.hpp>
36 #include <boost/signals2/signal.hpp>
37 #include <osg/Node>
38 
39 #include "../common/WBoundingBox.h"
40 #include "../common/WSharedAssociativeContainer.h"
41 #include "../common/WSharedSequenceContainer.h"
42 #include "WGETexture.h"
43 #include "callbacks/WGEFunctorCallback.h"
44 #include "shaders/WGEShader.h"
45 
46 
47 /**
48  * Class implements a manager for multiple 3D textures. They can be applied to arbitrary osg::Node. This allows very comfortable use of dataset
49  * based colormapping. The only requirement is that your geometry/node needs to specify texture coordinates in Object Space. That means: the
50  * texture coordinates equal the regular 3D grid of the texture.
51  */
52 class WGEColormapping // NOLINT
53 {
54 public:
55  /**
56  * The alias for a shared container.
57  */
59 
60  /**
61  * Iterator to access the texture list.
62  */
64 
65  /**
66  * Const iterator to access the texture list.
67  */
69 
70  /**
71  * The type of handler used for being notified about added textures.
72  */
73  typedef boost::function< void ( osg::ref_ptr< WGETexture3D > ) > TextureRegisterHandler;
74 
75  /**
76  * The type of handler used for being notified about removed textures.
77  */
79 
80  /**
81  * The type of handler used for being notified about replaced textures.
82  */
83  typedef boost::function< void ( osg::ref_ptr< WGETexture3D >, osg::ref_ptr< WGETexture3D > ) > TextureReplaceHandler;
84 
85  /**
86  * The type of handler called whenever the texture list got resorted.
87  */
88  typedef boost::function< void ( void ) > TextureSortHandler;
89 
90  /**
91  * Destructor.
92  */
93  virtual ~WGEColormapping();
94 
95  /**
96  * Returns instance of the module factory to use to create modules.
97  *
98  * \return the running module factory.
99  */
100  static std::shared_ptr< WGEColormapping > instance();
101 
102  /**
103  * a bunch of nodes.
104  */
105  typedef std::vector< osg::ref_ptr< osg::Node > > NodeList;
106 
107  /**
108  * Apply the colormapping to the specified node.
109  *
110  * \param node the node.
111  * \param shader the shader to use for colormapping. Provide your own shader here to let WGEColormap set some defines needed. If not
112  * specified, a default shader is used.
113  * \param preTransform Transformation matrix getting applied to your texture coordinates before applying texture matrices. This allows you to
114  * specify any kind of texture coordinates as long as you use this matrix to transform them to the right space.
115  * \param startTexUnit the first texture unit allowed to be used
116  */
117  static void apply( osg::ref_ptr< osg::Node > node, WMatrix4d preTransform = WMatrix4d::identity(),
118  osg::ref_ptr< WGEShader > shader = osg::ref_ptr< WGEShader >(), size_t startTexUnit = 0 );
119 
120  /**
121  * Apply the colormapping to a list of nodes using the same shader.
122  *
123  * \param nodes the node-list.
124  * \param shader the shader to use for colormapping. Provide your own shader here to let WGEColormap set some defines needed. If not
125  * specified, a default shader is used.
126  * \param preTransform Transformation matrix getting applied to your texture coordinates before applying texture matrices. This allows you to
127  * specify any kind of texture coordinates as long as you use this matrix to transform them to the right space.
128  * \param startTexUnit the first texture unit allowed to be used
129  */
130  static void apply( NodeList nodes, WMatrix4d preTransform = WMatrix4d::identity(),
131  osg::ref_ptr< WGEShader > shader = osg::ref_ptr< WGEShader >(), size_t startTexUnit = 0 );
132 
133  /**
134  * Apply the colormapping to the specified node.
135  *
136  * \param node the node.
137  * \param shader the shader to use for colormapping. Provide your own shader here to let WGEColormap set some defines needed. If not
138  * specified, a default shader is used.
139  * \param startTexUnit the first texture unit allowed to be used
140  */
141  static void apply( osg::ref_ptr< osg::Node > node, osg::ref_ptr< WGEShader > shader = osg::ref_ptr< WGEShader >(), size_t startTexUnit = 0 );
142 
143  /**
144  * Apply the colormapping to a list of nodes which all use the same shader.
145  *
146  * \param nodes the node list.
147  * \param shader the shader to use for colormapping. Provide your own shader here to let WGEColormap set some defines needed. If not
148  * specified, a default shader is used.
149  * \param startTexUnit the first texture unit allowed to be used
150  */
151  static void apply( NodeList nodes,
152  osg::ref_ptr< WGEShader > shader = osg::ref_ptr< WGEShader >(), size_t startTexUnit = 0 );
153 
154  /**
155  * Register the specified texture to the colormapper. The registered texture is the automatically applied to all users of WGEColormapping.
156  * The texture gets inserted at the beginning of the texture list.
157  *
158  * \param texture the texture to add
159  * \param name the name of the texture to add
160  */
161  static void registerTexture( osg::ref_ptr< WGETexture3D > texture, std::string name = "" );
162 
163  /**
164  * De-register the specified texture to the colormapper. The texture is the automatically removed from all users of WGEColormapping. If the
165  * texture is not in the list, nothing happens.
166  *
167  * \param texture the texture to remove
168  */
169  static void deregisterTexture( osg::ref_ptr< WGETexture3D > texture );
170 
171  /**
172  * Replaces the specified texture with the given new one. If the old texture does not exist, the new one gets inserted at the front of the
173  * list as \ref registerTexture does.
174  *
175  * \param old the texture to remove
176  * \param newTex the new texture to put at the position of the old one
177  * \param name the name of the texture.
178  */
179  static void replaceTexture( osg::ref_ptr< WGETexture3D > old, osg::ref_ptr< WGETexture3D > newTex, std::string name = "" );
180 
181  /**
182  * Resorts the texture list using the specified comparator.
183  *
184  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
185  * \param comp the comparator
186  */
187  template < typename Comparator >
188  void sort( Comparator comp );
189 
190  /**
191  * Resorts the texture list using the specified comparator using a stable sorting algorithm.
192  *
193  * \tparam Comparator the comparator type. Usually a boost::function or class providing the operator().
194  * \param comp the comparator
195  */
196  template < typename Comparator >
197  void stableSort( Comparator comp );
198 
199  /**
200  * Sort the texture list by the indices that have been stored in each texture's sortIndex.
201  */
202  void sortByIndex();
203 
204  /**
205  * This function sets the index of a texture in the list to this texture's WGETexture::sortIndex(). This can be used later using
206  * sortByIndex().
207  */
208  void setSortIndices();
209 
210  /**
211  * Reset all sort indices. This can be useful when loading new project files with new sort indices.
212  */
213  void resetSortIndices();
214 
215  /**
216  * Move the specified texture up in the list, directly to the top. Causes the sort signal to fire.
217  *
218  * \param texture the texture swapped with its ascendant
219  * \return true if swap was successful. False if not (texture not found, texture already at beginning).
220  */
221  bool moveToTop( osg::ref_ptr< WGETexture3D > texture );
222 
223  /**
224  * Move the specified texture down in the list, directly to the bottom. Causes the sort signal to fire.
225  *
226  * \param texture the texture swapped with its descendant
227  * \return true if swap was successful. False if not (texture not found, texture already at end).
228  */
229  bool moveToBottom( osg::ref_ptr< WGETexture3D > texture );
230 
231  /**
232  * Move the specified texture one item up in the list. Causes the sort signal to fire.
233  *
234  * \param texture the texture swapped with its ascendant
235  * \return true if swap was successful. False if not (texture not found, texture already at beginning).
236  */
237  bool moveUp( osg::ref_ptr< WGETexture3D > texture );
238 
239  /**
240  * Move the specified texture one item down in the list. Causes the sort signal to fire.
241  *
242  * \param texture the texture swapped with its descendant
243  *
244  * \return true if swap was successful. False if not (texture not found, texture already at end).
245  */
246  bool moveDown( osg::ref_ptr< WGETexture3D > texture );
247 
248  /**
249  * Move the texture to the specified index. If the texture is not in the list, nothing happens.
250  *
251  * \param texture the texture to move
252  * \param idx the target index
253  *
254  * \return true if the operation was successful.
255  */
256  bool moveTo( osg::ref_ptr< WGETexture3D > texture, size_t idx );
257 
258  /**
259  * Counts the number of textures in the colormapper.
260  *
261  * \return the number of textures.
262  */
263  size_t size() const;
264 
265  /**
266  * Possible signals that can be subscribed for being notified about texture list changes.
267  */
268  typedef enum
269  {
270  Registered = 0, //!< texture got added
271  Deregistered, //!< texture got removed
272  Replaced, //!< texture got replaced
273  Sorted //!< texture list was resorted
274  }
276 
277  /**
278  * Subscribe to the specified signal. See \ref TextureListSignal for details about their meaning.
279  *
280  * \param signal the signal to subscribe
281  * \param notifier the notifier
282  *
283  * \return the connection. Keep this and disconnect it properly!
284  */
285  boost::signals2::connection subscribeSignal( TextureListSignal signal, TextureRegisterHandler notifier );
286 
287  /**
288  * Subscribe to the specified signal. See \ref TextureListSignal for details about their meaning.
289  *
290  * \param signal the signal to subscribe
291  * \param notifier the notifier
292  *
293  * \return the connection. Keep this and disconnect it properly!
294  */
295  boost::signals2::connection subscribeSignal( TextureListSignal signal, TextureReplaceHandler notifier );
296 
297  /**
298  * Subscribe to the specified signal. See \ref TextureListSignal for details about their meaning.
299  *
300  * \param signal the signal to subscribe
301  * \param notifier the notifier
302  *
303  * \return the connection. Keep this and disconnect it properly!
304  */
305  boost::signals2::connection subscribeSignal( TextureListSignal signal, TextureSortHandler notifier );
306 
307  /**
308  * Returns a read ticket to the texture array. Useful to iterate the textures.
309  *
310  * \return the read ticket
311  */
313 
314  /**
315  * This returns the bounding box of all the data textures. This is very useful if you implement an universal color-mapped exploration tool.
316  * It returns a copy of the current bounding box. Please note that this can change any moment.
317  *
318  * \return the bounding box.
319  */
321 
322  /**
323  * Returns the condition firing if the texture list changes (sort, replace, add or remove). If you are interested in a certain event only,
324  * use \ref subscribeSignal.
325  *
326  * \return the change condition
327  */
329 
330 protected:
331  /**
332  * Default constructor.
333  */
334  WGEColormapping();
335 
336  /**
337  * Apply the colormapping to the specified nodes.
338  *
339  * \param nodes the nodes.
340  * \param preTransform Transformation matrix getting applied to your texture coordinates before applying texture matrices. This allows you to
341  * specify any kind of texture coordinates as long as you use this matrix to transform them to the right space.
342  * \param shader the shader to use for colormapping. Provide your own shader here to let WGEColormap set some defines needed. If not
343  * specified, a default shader is used.
344  * \param startTexUnit the first texture unit allowed to be used
345  */
346  void applyInst( NodeList nodes, WMatrix4d preTransform = WMatrix4d::identity(),
347  osg::ref_ptr< WGEShader > shader = osg::ref_ptr< WGEShader >(), size_t startTexUnit = 0 );
348 
349  /**
350  * Register the specified texture to the colormapper. The registered texture is the automatically applied to all users of WGEColormapping.
351  *
352  * \param texture the texture to add
353  * \param name the name of the texture.
354  */
355  void registerTextureInst( osg::ref_ptr< WGETexture3D > texture, std::string name );
356 
357  /**
358  * De-register the specified texture to the colormapper. The texture is the automatically removed from all users of WGEColormapping.
359  *
360  * \param texture the texture to remove
361  */
362  void deregisterTextureInst( osg::ref_ptr< WGETexture3D > texture );
363 
364  /**
365  * Replaces the specified texture with the given new one. If the old texture does not exist, the new one gets inserted at the front of the
366  * list as \ref registerTexture does.
367  *
368  * \param old the texture to remove
369  * \param newTex the new texture to put at the position of the old one
370  * \param name the name of the texture.
371  */
372  void replaceTextureInst( osg::ref_ptr< WGETexture3D > old, osg::ref_ptr< WGETexture3D > newTex, std::string name = "" );
373 
374  /**
375  * This callback handles all the updates needed. It is called by the callback instance every update cycle for each node using this
376  * WGEColormapping instance.
377  *
378  * \param node
379  */
380  void callback( osg::Node* node );
381 
382  /**
383  * Called whenever the texture list is updated.
384  */
385  void textureUpdate();
386 
387 private:
388  /**
389  * Singleton instance of WGEColormapping
390  */
391  static std::shared_ptr< WGEColormapping > m_instance;
392 
393  /**
394  * The textures managed by this instance.
395  */
397 
398  /**
399  * Simple structure to store some additional node-related info like texture units and so on.
400  */
401  struct NodeInfo
402  {
403  bool m_rebind; //!< true if the node has not been callback'ed before
404  size_t m_texUnitStart; //!< the start index of the texture unit to use
405  WMatrix4d m_preTransform; //!< matrix used for transforming arbitrary texture coordinates to the proper space.
406  };
407 
408  /**
409  * The alias for a shared container with a set of node-nodeInfo pairs
410  */
412 
413  /**
414  * This map is needed to keep track of several node specific settings
415  */
417 
418  /**
419  * Called whenever a texture got registered.
420  */
421  boost::signals2::signal< void( osg::ref_ptr< WGETexture3D > ) > m_registerSignal;
422 
423  /**
424  * Called whenever a texture got removed.
425  */
426  boost::signals2::signal< void( osg::ref_ptr< WGETexture3D > ) > m_deregisterSignal;
427 
428  /**
429  * Called whenever a texture got replaced.
430  */
431  boost::signals2::signal< void( osg::ref_ptr< WGETexture3D >, osg::ref_ptr< WGETexture3D > ) > m_replaceSignal;
432 
433  /**
434  * Called whenever the texture list got resorted
435  */
436  boost::signals2::signal< void( void ) > m_sortSignal;
437 
438  /**
439  * The bounding box of all the textures.
440  */
442 
443  /**
444  * Updates the bounding box information. This is called for every write-update in m_textures.
445  */
446  void updateBounds();
447 };
448 
449 template < typename Comparator >
450 void WGEColormapping::sort( Comparator comp )
451 {
452  m_textures.sort< Comparator >( comp );
453 }
454 
455 template < typename Comparator >
456 void WGEColormapping::stableSort( Comparator comp )
457 {
458  m_textures.stableSort< Comparator >( comp );
459 }
460 
461 #endif // WGECOLORMAPPING_H
462 
std::shared_ptr< WCondition > SPtr
Shared pointer type for WCondition.
Definition: WCondition.h:48
Class implements a manager for multiple 3D textures.
static void replaceTexture(osg::ref_ptr< WGETexture3D > old, osg::ref_ptr< WGETexture3D > newTex, std::string name="")
Replaces the specified texture with the given new one.
void sortByIndex()
Sort the texture list by the indices that have been stored in each texture's sortIndex.
void sort(Comparator comp)
Resorts the texture list using the specified comparator.
TextureRegisterHandler TextureDeregisterHandler
The type of handler used for being notified about removed textures.
void textureUpdate()
Called whenever the texture list is updated.
WSharedAssociativeContainer< std::map< osg::Node *, NodeInfo *, std::less< osg::Node * > > > NodeInfoContainerType
The alias for a shared container with a set of node-nodeInfo pairs.
boost::function< void(void) > TextureSortHandler
The type of handler called whenever the texture list got resorted.
boost::function< void(osg::ref_ptr< WGETexture3D >, osg::ref_ptr< WGETexture3D >) > TextureReplaceHandler
The type of handler used for being notified about replaced textures.
WBoundingBox getBoundingBox() const
This returns the bounding box of all the data textures.
void setSortIndices()
This function sets the index of a texture in the list to this texture's WGETexture::sortIndex().
void registerTextureInst(osg::ref_ptr< WGETexture3D > texture, std::string name)
Register the specified texture to the colormapper.
void stableSort(Comparator comp)
Resorts the texture list using the specified comparator using a stable sorting algorithm.
WCondition::SPtr getChangeCondition() const
Returns the condition firing if the texture list changes (sort, replace, add or remove).
TextureContainerType m_textures
The textures managed by this instance.
WSharedObject< WBoundingBox > m_boundingBox
The bounding box of all the textures.
static void registerTexture(osg::ref_ptr< WGETexture3D > texture, std::string name="")
Register the specified texture to the colormapper.
WGEColormapping()
Default constructor.
void applyInst(NodeList nodes, WMatrix4d preTransform=WMatrix4d::identity(), osg::ref_ptr< WGEShader > shader=osg::ref_ptr< WGEShader >(), size_t startTexUnit=0)
Apply the colormapping to the specified nodes.
static void apply(osg::ref_ptr< osg::Node > node, WMatrix4d preTransform=WMatrix4d::identity(), osg::ref_ptr< WGEShader > shader=osg::ref_ptr< WGEShader >(), size_t startTexUnit=0)
Apply the colormapping to the specified node.
TextureContainerType::ReadTicket getReadTicket()
Returns a read ticket to the texture array.
void callback(osg::Node *node)
This callback handles all the updates needed.
size_t size() const
Counts the number of textures in the colormapper.
boost::signals2::signal< void(osg::ref_ptr< WGETexture3D >, osg::ref_ptr< WGETexture3D >) > m_replaceSignal
Called whenever a texture got replaced.
bool moveToTop(osg::ref_ptr< WGETexture3D > texture)
Move the specified texture up in the list, directly to the top.
boost::signals2::signal< void(void) > m_sortSignal
Called whenever the texture list got resorted.
static std::shared_ptr< WGEColormapping > instance()
Returns instance of the module factory to use to create modules.
virtual ~WGEColormapping()
Destructor.
bool moveTo(osg::ref_ptr< WGETexture3D > texture, size_t idx)
Move the texture to the specified index.
TextureListSignal
Possible signals that can be subscribed for being notified about texture list changes.
@ Deregistered
texture got removed
@ Registered
texture got added
@ Sorted
texture list was resorted
@ Replaced
texture got replaced
static void deregisterTexture(osg::ref_ptr< WGETexture3D > texture)
De-register the specified texture to the colormapper.
TextureContainerType::Iterator TextureIterator
Iterator to access the texture list.
TextureContainerType::ConstIterator TextureConstIterator
Const iterator to access the texture list.
static std::shared_ptr< WGEColormapping > m_instance
Singleton instance of WGEColormapping.
WSharedSequenceContainer< std::vector< osg::ref_ptr< WGETexture3D > > > TextureContainerType
The alias for a shared container.
boost::function< void(osg::ref_ptr< WGETexture3D >) > TextureRegisterHandler
The type of handler used for being notified about added textures.
boost::signals2::signal< void(osg::ref_ptr< WGETexture3D >) > m_deregisterSignal
Called whenever a texture got removed.
void updateBounds()
Updates the bounding box information.
void replaceTextureInst(osg::ref_ptr< WGETexture3D > old, osg::ref_ptr< WGETexture3D > newTex, std::string name="")
Replaces the specified texture with the given new one.
void deregisterTextureInst(osg::ref_ptr< WGETexture3D > texture)
De-register the specified texture to the colormapper.
NodeInfoContainerType m_nodeInfo
This map is needed to keep track of several node specific settings.
bool moveToBottom(osg::ref_ptr< WGETexture3D > texture)
Move the specified texture down in the list, directly to the bottom.
void resetSortIndices()
Reset all sort indices.
bool moveDown(osg::ref_ptr< WGETexture3D > texture)
Move the specified texture one item down in the list.
std::vector< osg::ref_ptr< osg::Node > > NodeList
a bunch of nodes.
boost::signals2::signal< void(osg::ref_ptr< WGETexture3D >) > m_registerSignal
Called whenever a texture got registered.
boost::signals2::connection subscribeSignal(TextureListSignal signal, TextureRegisterHandler notifier)
Subscribe to the specified signal.
bool moveUp(osg::ref_ptr< WGETexture3D > texture)
Move the specified texture one item up in the list.
static MatrixType identity()
Returns an identity matrix.
Definition: WMatrixFixed.h:310
This class provides a common interface for thread-safe access to associative containers (set,...
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:45
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
This class provides a common interface for thread-safe access to sequence containers (list,...
void sort(Comparator comp)
Resorts the container using the specified comparator from its begin to its end.
void stableSort(Comparator comp)
Resorts the container using the specified comparator from its begin to its end.
S::iterator Iterator
A typedef for the correct iterator to traverse this sequence container.
S::const_iterator ConstIterator
A typedef for the correct const iterator useful to traverse this sequence container.
Simple structure to store some additional node-related info like texture units and so on.
WMatrix4d m_preTransform
matrix used for transforming arbitrary texture coordinates to the proper space.
size_t m_texUnitStart
the start index of the texture unit to use
bool m_rebind
true if the node has not been callback'ed before