WFiberDrawable.h 6.27 KB
Newer Older
schurade's avatar
schurade committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25 26 27 28
#ifndef WFIBERDRAWABLE_H
#define WFIBERDRAWABLE_H

#include <vector>
schurade's avatar
schurade committed
29 30 31 32 33 34 35 36 37 38

#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp>

#include <osg/Drawable>
#include <osg/ShapeDrawable>
#include <osg/Group>

/**
39
 * Class implements an osg::Drawable that paints fiber representations either using lines or tubes
schurade's avatar
schurade committed
40
 */
41
class WFiberDrawable: public osg::Drawable
schurade's avatar
schurade committed
42 43 44 45 46 47 48 49 50 51
{
public:

    /**
    * The constructor here does nothing. One thing that may be necessary is
    * disabling display lists. This can be done by calling
    * setSupportsDisplayList (false);
    * Display lists should be disabled for 'Drawable's that can change over
    * time (that is, the vertices drawn change from time to time).
    */
52
    WFiberDrawable();
schurade's avatar
schurade committed
53 54 55 56 57 58 59 60

    /**
     * I can't say much about the methods below, but OSG seems to expect
     * that we implement them.
     *
     * \param pg
     * \param copyop
     */
61
    WFiberDrawable( const WFiberDrawable& pg, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY );
schurade's avatar
schurade committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    /**
     * no clue why it's here and wehat it does
     */
    virtual osg::Object* cloneType() const;

    /**
     * clones it
     *
     * \param copyop
     */
    virtual osg::Object* clone( const osg::CopyOp& copyop ) const;

    /**
    * Real work is done here. THERE IS A VERY IMPORTANT THING TO NOTE HERE:
    * the 'drawImplementation()' method receives an 'osg::State' as
    * parameter. This can be used to change the OpenGL state, but changing
    * the OpenGL state here is something to be avoided as much as possible.
    * Do this *only* if it is *absolutely* necessary to make your rendering
    * algorithm work. The "right" (most efficient and flexible) way to change
    * the OpenGL state in OSG is by attaching 'StateSet's to 'Node's and
    * 'Drawable's.
    * That said, the example below shows how to change the OpenGL state in
    * these rare cases in which it is necessary. But always keep in mind:
    * *Change the OpenGL state only if strictly necessary*.
    *
    * \param renderInfo
    */
90
    virtual void drawImplementation( osg::RenderInfo& renderInfo ) const; //NOLINT
schurade's avatar
schurade committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    /**
     * sets the dataset pointer which provides access to the necessary array
     *
     * \param dataset
     */
    void setDataset( boost::shared_ptr< const WDataSetFibers > dataset );

    /**
     * toggles drawing of tubes
     *
     * \param flag
     */
    void setUseTubes( bool flag );

106
    /**
107 108
     * Set the bounding box of all fibers.
     * \param bb The new bounding box.
109
     */
110
    void setBoundingBox( const osg::BoundingBox & bb );
111

schurade's avatar
schurade committed
112
    /**
113 114
     * setter
     * \param bitField selected fibers to draw
schurade's avatar
schurade committed
115
     */
116
    void setBitfield( boost::shared_ptr< std::vector< bool > > bitField );
schurade's avatar
schurade committed
117

118
    /**
119 120
     * setter
     * \param idx
121
     */
122
    void setStartIndexes( boost::shared_ptr< std::vector< size_t > > idx );
123

124
    /**
125 126
     * setter
     * \param ppl
127
     */
128
    void setPointsPerLine( boost::shared_ptr< std::vector< size_t > > ppl );
129

130 131
    /**
     * setter
132
     * \param verts
133
     */
134
    void setVerts( boost::shared_ptr< std::vector< float > > verts );
135 136 137

    /**
     * setter
138
     * \param tangents
139
     */
140
    void setTangents( boost::shared_ptr< std::vector< float > > tangents );
141

142 143
    /**
     * setter
144
     * \param color
145
     */
146
    void setColor( boost::shared_ptr< std::vector< float > > color );
147

schurade's avatar
schurade committed
148 149 150
protected:
private:
    /**
151
     * Draw fibers as ordinary lines.
152 153
     *
     * \param renderInfo
schurade's avatar
schurade committed
154
     */
155
    void drawFibers( osg::RenderInfo& renderInfo ) const; //NOLINT
schurade's avatar
schurade committed
156 157

    /**
158
     * Draw fibers as fake tubes.
schurade's avatar
schurade committed
159 160 161 162 163 164 165 166
     */
    void drawTubes() const;

    boost::shared_ptr< const WDataSetFibers > m_dataset; //!< stores pointer to dataset

    boost::shared_mutex m_recalcLock; //!< lock

    bool m_useTubes; //!< flag
167

168 169
    boost::shared_ptr< std::vector< bool > > m_active; //!< pointer to the bitfield of active fibers

170 171 172 173 174
    boost::shared_ptr< std::vector< size_t > > m_startIndexes; //!< pointer to the field of line start indexes
    boost::shared_ptr< std::vector< size_t > > m_pointsPerLine; //!< pointer to the field of points per line
    boost::shared_ptr< std::vector< float > > m_verts; //!< pointer to the field of vertexes
    boost::shared_ptr< std::vector< float > > m_tangents; //!< pointer to the field of line tangents
    boost::shared_ptr< std::vector< float > > m_colors; //!< pointer to the field of colors per vertex
schurade's avatar
schurade committed
175 176
};

177
inline void WFiberDrawable::setUseTubes( bool flag )
178
{
179
    m_useTubes = flag;
180 181
}

182
inline void WFiberDrawable::setBoundingBox( const osg::BoundingBox & bb )
183
{
184
    setBound( bb );
185 186
}

187
inline void WFiberDrawable::setBitfield( boost::shared_ptr< std::vector< bool > > bitField )
188
{
189
    m_active = bitField;
190 191
}

192
inline void WFiberDrawable::setStartIndexes( boost::shared_ptr< std::vector< size_t > > idx )
193
{
194
    m_startIndexes = idx;
195 196
}

197
inline void WFiberDrawable::setPointsPerLine( boost::shared_ptr< std::vector< size_t > > ppl )
198
{
199
    m_pointsPerLine = ppl;
200 201
}

202
inline void WFiberDrawable::setVerts( boost::shared_ptr< std::vector< float > > verts )
203
{
204
    m_verts = verts;
205 206
}

207
inline void WFiberDrawable::setTangents( boost::shared_ptr< std::vector< float > > tangents )
208
{
209
    m_tangents = tangents;
210 211
}

212
inline void WFiberDrawable::setColor( boost::shared_ptr< std::vector< float > > color )
213
{
214
    m_colors = color;
215 216 217
}

#endif  // WFIBERDRAWABLE_H