OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WFiberDrawable.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 WFIBERDRAWABLE_H
26 #define WFIBERDRAWABLE_H
27 
28 #include <vector>
29 
30 #include <boost/shared_ptr.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/thread.hpp>
33 
34 #include <osg/Drawable>
35 #include <osg/ShapeDrawable>
36 #include <osg/Group>
37 
38 #include "WExportWGE.h"
39 
40 /**
41  * Class implements an osg::Drawable that paints fiber representations either using lines or tubes
42  */
43 class WGE_EXPORT WFiberDrawable: public osg::Drawable // NOLINT
44 {
45 public:
46 
47  /**
48  * The constructor here does nothing. One thing that may be necessary is
49  * disabling display lists. This can be done by calling
50  * setSupportsDisplayList (false);
51  * Display lists should be disabled for 'Drawable's that can change over
52  * time (that is, the vertices drawn change from time to time).
53  */
55 
56  /**
57  * I can't say much about the methods below, but OSG seems to expect
58  * that we implement them.
59  *
60  * \param pg
61  * \param copyop
62  */
63  WFiberDrawable( const WFiberDrawable& pg, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY );
64 
65  /**
66  * See osg documentation for this.
67  *
68  * \return the cloned object
69  */
70  virtual osg::Object* cloneType() const;
71 
72  /**
73  * clones it
74  *
75  * \param copyop copy operation. See osg doc for details
76  * \return the cloned object
77  */
78  virtual osg::Object* clone( const osg::CopyOp& copyop ) const;
79 
80  /**
81  * Real work is done here. THERE IS A VERY IMPORTANT THING TO NOTE HERE:
82  * the \ref drawImplementation method receives an state as
83  * parameter. This can be used to change the OpenGL state, but changing
84  * the OpenGL state here is something to be avoided as much as possible.
85  * Do this *only* if it is *absolutely* necessary to make your rendering
86  * algorithm work. The "right" (most efficient and flexible) way to change
87  * the OpenGL state in OSG is by attaching 'StateSet's to 'Node's and
88  * 'Drawable's.
89  * That said, the example below shows how to change the OpenGL state in
90  * these rare cases in which it is necessary. But always keep in mind:
91  * *Change the OpenGL state only if strictly necessary*.
92  *
93  * \param renderInfo the render info object. See osg doc for details
94  */
95  virtual void drawImplementation( osg::RenderInfo& renderInfo ) const; //NOLINT
96 
97  /**
98  * toggles drawing of tubes
99  *
100  * \param flag
101  */
102  void setUseTubes( bool flag );
103 
104  using osg::Drawable::setBound;
105 
106  /**
107  * setter
108  * \param bitField selected fibers to draw
109  */
110  void setBitfield( boost::shared_ptr< std::vector< bool > > bitField );
111 
112  /**
113  * setter
114  * \param idx
115  */
116  void setStartIndexes( boost::shared_ptr< std::vector< size_t > > idx );
117 
118  /**
119  * setter
120  * \param ppl
121  */
122  void setPointsPerLine( boost::shared_ptr< std::vector< size_t > > ppl );
123 
124  /**
125  * setter
126  * \param verts
127  */
128  void setVerts( boost::shared_ptr< std::vector< float > > verts );
129 
130  /**
131  * setter
132  * \param tangents
133  */
134  void setTangents( boost::shared_ptr< std::vector< float > > tangents );
135 
136  /**
137  * setter
138  * \param color
139  */
140  void setColor( boost::shared_ptr< std::vector< float > > color );
141 
142 protected:
143 private:
144  /**
145  * Draw fibers as ordinary lines.
146  *
147  * \param renderInfo
148  */
149  void drawFibers( osg::RenderInfo& renderInfo ) const; //NOLINT
150 
151  /**
152  * Draw fibers as fake tubes.
153  */
154  void drawTubes() const;
155 
156  boost::shared_mutex m_recalcLock; //!< lock
157 
158  bool m_useTubes; //!< flag
159 
160  boost::shared_ptr< std::vector< bool > > m_active; //!< pointer to the bitfield of active fibers
161 
162  boost::shared_ptr< std::vector< size_t > > m_startIndexes; //!< pointer to the field of line start indexes
163  boost::shared_ptr< std::vector< size_t > > m_pointsPerLine; //!< pointer to the field of points per line
164  boost::shared_ptr< std::vector< float > > m_verts; //!< pointer to the field of vertexes
165  boost::shared_ptr< std::vector< float > > m_tangents; //!< pointer to the field of line tangents
166  boost::shared_ptr< std::vector< float > > m_colors; //!< pointer to the field of colors per vertex
167 };
168 
169 inline void WFiberDrawable::setUseTubes( bool flag )
170 {
171  m_useTubes = flag;
172 }
173 
174 inline void WFiberDrawable::setBitfield( boost::shared_ptr< std::vector< bool > > bitField )
175 {
176  m_active = bitField;
177 }
178 
179 inline void WFiberDrawable::setStartIndexes( boost::shared_ptr< std::vector< size_t > > idx )
180 {
181  m_startIndexes = idx;
182 }
183 
184 inline void WFiberDrawable::setPointsPerLine( boost::shared_ptr< std::vector< size_t > > ppl )
185 {
186  m_pointsPerLine = ppl;
187 }
188 
189 inline void WFiberDrawable::setVerts( boost::shared_ptr< std::vector< float > > verts )
190 {
191  m_verts = verts;
192 }
193 
194 inline void WFiberDrawable::setTangents( boost::shared_ptr< std::vector< float > > tangents )
195 {
196  m_tangents = tangents;
197 }
198 
199 inline void WFiberDrawable::setColor( boost::shared_ptr< std::vector< float > > color )
200 {
201  m_colors = color;
202 }
203 
204 #endif // WFIBERDRAWABLE_H