OpenWalnut  1.4.0
WFiberDrawable.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WFIBERDRAWABLE_H
00026 #define WFIBERDRAWABLE_H
00027 
00028 #include <vector>
00029 
00030 #include <boost/shared_ptr.hpp>
00031 #include <boost/thread.hpp>
00032 #include <boost/thread/thread.hpp>
00033 
00034 #include <osg/Drawable>
00035 
00036 
00037 
00038 /**
00039  * Class implements an osg::Drawable that paints fiber representations either using lines or tubes
00040  */
00041 class WFiberDrawable: public osg::Drawable // NOLINT
00042 {
00043 public:
00044     /**
00045     * The constructor here does nothing. One thing that may be necessary is
00046     * disabling display lists. This can be done by calling
00047     * setSupportsDisplayList (false);
00048     * Display lists should be disabled for 'Drawable's that can change over
00049     * time (that is, the vertices drawn change from time to time).
00050     */
00051     WFiberDrawable();
00052 
00053     /**
00054      * I can't say much about the methods below, but OSG seems to expect
00055      * that we implement them.
00056      *
00057      * \param pg
00058      * \param copyop
00059      */
00060     WFiberDrawable( const WFiberDrawable& pg, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY );
00061 
00062     /**
00063      * See osg documentation for this.
00064      *
00065      * \return the cloned object
00066      */
00067     virtual osg::Object* cloneType() const;
00068 
00069     /**
00070      * clones it
00071      *
00072      * \param copyop copy operation. See osg doc for details
00073      * \return the cloned object
00074      */
00075     virtual osg::Object* clone( const osg::CopyOp& copyop ) const;
00076 
00077     /**
00078     * Real work is done here. THERE IS A VERY IMPORTANT THING TO NOTE HERE:
00079     * the \ref drawImplementation method receives an state as
00080     * parameter. This can be used to change the OpenGL state, but changing
00081     * the OpenGL state here is something to be avoided as much as possible.
00082     * Do this *only* if it is *absolutely* necessary to make your rendering
00083     * algorithm work. The "right" (most efficient and flexible) way to change
00084     * the OpenGL state in OSG is by attaching 'StateSet's to 'Node's and
00085     * 'Drawable's.
00086     * That said, the example below shows how to change the OpenGL state in
00087     * these rare cases in which it is necessary. But always keep in mind:
00088     * *Change the OpenGL state only if strictly necessary*.
00089     *
00090     * \param renderInfo the render info object. See osg doc for details
00091     */
00092     virtual void drawImplementation( osg::RenderInfo& renderInfo ) const; //NOLINT
00093 
00094     /**
00095      * toggles drawing of tubes
00096      *
00097      * \param flag
00098      */
00099     void setUseTubes( bool flag );
00100 
00101     using osg::Drawable::setBound;
00102 
00103     /**
00104      * setter
00105      * \param bitField selected fibers to draw
00106      */
00107     void setBitfield( boost::shared_ptr< std::vector< bool > > bitField );
00108 
00109     /**
00110      * setter
00111      * \param idx
00112      */
00113     void setStartIndexes( boost::shared_ptr< std::vector< size_t > > idx );
00114 
00115     /**
00116      * setter
00117      * \param ppl
00118      */
00119     void setPointsPerLine( boost::shared_ptr< std::vector< size_t > > ppl );
00120 
00121     /**
00122      * setter
00123      * \param verts
00124      */
00125     void setVerts( boost::shared_ptr< std::vector< float > > verts );
00126 
00127     /**
00128      * setter
00129      * \param tangents
00130      */
00131     void setTangents( boost::shared_ptr< std::vector< float > > tangents );
00132 
00133     /**
00134      * setter
00135      * \param color
00136      */
00137     void setColor( boost::shared_ptr< std::vector< float > > color );
00138 
00139 protected:
00140 private:
00141     /**
00142      * Draw fibers as ordinary lines.
00143      *
00144      * \param renderInfo
00145      */
00146     void drawFibers( osg::RenderInfo& renderInfo ) const; //NOLINT
00147 
00148     /**
00149      * Draw fibers as fake tubes.
00150      */
00151     void drawTubes() const;
00152 
00153     boost::shared_mutex m_recalcLock; //!< lock
00154 
00155     bool m_useTubes; //!< flag
00156 
00157     boost::shared_ptr< std::vector< bool > > m_active; //!< pointer to the bitfield of active fibers
00158 
00159     boost::shared_ptr< std::vector< size_t > > m_startIndexes; //!< pointer to the field of line start indexes
00160     boost::shared_ptr< std::vector< size_t > > m_pointsPerLine; //!< pointer to the field of points per line
00161     boost::shared_ptr< std::vector< float > > m_verts; //!< pointer to the field of vertexes
00162     boost::shared_ptr< std::vector< float > > m_tangents; //!< pointer to the field of line tangents
00163     boost::shared_ptr< std::vector< float > > m_colors; //!< pointer to the field of colors per vertex
00164 };
00165 
00166 inline void WFiberDrawable::setUseTubes( bool flag )
00167 {
00168     m_useTubes = flag;
00169 }
00170 
00171 inline void WFiberDrawable::setBitfield( boost::shared_ptr< std::vector< bool > > bitField )
00172 {
00173     m_active = bitField;
00174 }
00175 
00176 inline void WFiberDrawable::setStartIndexes( boost::shared_ptr< std::vector< size_t > > idx )
00177 {
00178     m_startIndexes = idx;
00179 }
00180 
00181 inline void WFiberDrawable::setPointsPerLine( boost::shared_ptr< std::vector< size_t > > ppl )
00182 {
00183     m_pointsPerLine = ppl;
00184 }
00185 
00186 inline void WFiberDrawable::setVerts( boost::shared_ptr< std::vector< float > > verts )
00187 {
00188     m_verts = verts;
00189 }
00190 
00191 inline void WFiberDrawable::setTangents( boost::shared_ptr< std::vector< float > > tangents )
00192 {
00193     m_tangents = tangents;
00194 }
00195 
00196 inline void WFiberDrawable::setColor( boost::shared_ptr< std::vector< float > > color )
00197 {
00198     m_colors = color;
00199 }
00200 
00201 #endif  // WFIBERDRAWABLE_H