OpenWalnut 1.3.1
WDataSetFibers.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 WDATASETFIBERS_H
00026 #define WDATASETFIBERS_H
00027 
00028 #include <string>
00029 #include <utility>
00030 #include <vector>
00031 
00032 #include <boost/shared_ptr.hpp>
00033 #include <boost/tuple/tuple.hpp>
00034 
00035 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00036 #include "../common/WBoundingBox.h"
00037 #include "../common/WProperties.h"
00038 #include "WDataSet.h"
00039 
00040 
00041 // forward declarations
00042 class WFiber;
00043 
00044 /**
00045  * Represents a simple set of WFibers.
00046  */
00047 class WDataSetFibers : public WDataSet // NOLINT
00048 {
00049 public:
00050     // some type alias for the used arrays.
00051     /**
00052      * Pointer to dataset.
00053      */
00054     typedef boost::shared_ptr< WDataSetFibers > SPtr;
00055 
00056     /**
00057      * Pointer to const dataset.
00058      */
00059     typedef boost::shared_ptr< const WDataSetFibers > ConstSPtr;
00060 
00061     /**
00062      * List of vertex coordinates in term of components of vertices.
00063      */
00064     typedef boost::shared_ptr< std::vector< float > > VertexArray;
00065 
00066     /**
00067      * Index list indexing fibers in VertexArray in terms of vertex numbers.
00068      */
00069     typedef boost::shared_ptr< std::vector< size_t > > IndexArray;
00070 
00071     /**
00072      * Lengths of fibers in terms of verties.
00073      */
00074     typedef boost::shared_ptr< std::vector< size_t > > LengthArray;
00075 
00076     /**
00077      * Tangents at each vertex in VertexArray.
00078      */
00079     typedef boost::shared_ptr< std::vector< float > > TangentArray;
00080 
00081     /**
00082      * Colors for each vertex in VertexArray.
00083      */
00084     typedef boost::shared_ptr< std::vector< float > > ColorArray;
00085 
00086     /**
00087      * Item used in the selection below also containing color info.
00088      */
00089     class ColorScheme: public WItemSelectionItem
00090     {
00091     friend class WDataSetFibers;
00092     public:
00093         /**
00094          * different kinds of color arrays can be used in this class. This enum defines their possible types.
00095          */
00096         typedef enum
00097         {
00098             GRAY = 1,   //!< gray value per vertex
00099             RGB  = 3,   //!< rgb per vertex
00100             RGBA = 4    //!< rgba per vertex
00101         }
00102         ColorMode;
00103 
00104         /**
00105          * Constructor. Creates new item.
00106          *
00107          * \param name name, name of item.
00108          * \param description description of item. Can be empty.
00109          * \param icon icon, can be NULL
00110          * \param color the color array of this item.
00111          * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
00112          */
00113         ColorScheme( std::string name, std::string description, const char** icon, ColorArray color, ColorMode mode = RGB ):
00114             WItemSelectionItem( name, description, icon ),
00115             m_color( color ),
00116             m_mode( mode )
00117         {
00118         };
00119 
00120         /**
00121          * Get the color.
00122          *
00123          * \return the color array.
00124          */
00125         ColorArray getColor() const
00126         {
00127             return m_color;
00128         };
00129 
00130         /**
00131          * Returns the mode of the color scheme.
00132          *
00133          * \return the mode.
00134          */
00135         ColorMode getMode() const
00136         {
00137             return m_mode;
00138         };
00139 
00140     protected:
00141         /**
00142          * Sets the color array for this item.
00143          *
00144          * \param color the color to set.
00145          * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
00146          */
00147         void setColor( ColorArray color, ColorMode mode = RGB )
00148         {
00149             m_color = color;
00150             m_mode = mode;
00151         };
00152 
00153     private:
00154         /**
00155          * The color array associated with the item.
00156          */
00157         ColorArray m_color;
00158 
00159         /**
00160          * Coloring mode.
00161          */
00162         ColorMode m_mode;
00163     };
00164 
00165     /**
00166      * Constructs a new set of fibers.
00167      *
00168      * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
00169      * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
00170      * \param lineLengths how many vertices belong to a fiber
00171      * \param verticesReverse stores for each vertex the index of the corresponding fiber
00172      * \param boundingBox The bounding box of the fibers (first minimum, second maximum).
00173      */
00174     WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
00175                     boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
00176                     boost::shared_ptr< std::vector< size_t > > lineLengths,
00177                     boost::shared_ptr< std::vector< size_t > > verticesReverse,
00178                     WBoundingBox boundingBox );
00179 
00180     /**
00181      * Constructs a new set of fibers. This constructor determines the bounding box by using the coordinates of the vertices.
00182      *
00183      * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
00184      * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
00185      * \param lineLengths how many vertices belong to a fiber
00186      * \param verticesReverse stores for each vertex the index of the corresponding fiber
00187      */
00188     WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
00189                     boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
00190                     boost::shared_ptr< std::vector< size_t > > lineLengths,
00191                     boost::shared_ptr< std::vector< size_t > > verticesReverse );
00192 
00193     /**
00194      * Constructs a new set of tracts. The constructed instance is not usable but needed for prototype mechanism.
00195      */
00196     WDataSetFibers();
00197 
00198     /**
00199      * Get number of tracts in this data set.
00200      * \return number of fibers
00201      */
00202     size_t size() const;
00203 
00204     /**
00205      * Determines whether this dataset can be used as a texture.
00206      *
00207      * \return true if usable as texture.
00208      */
00209     virtual bool isTexture() const;
00210 
00211     /**
00212      * Gets the name of this prototype.
00213      *
00214      * \return the name.
00215      */
00216     virtual const std::string getName() const;
00217 
00218     /**
00219      * Gets the description for this prototype.
00220      *
00221      * \return the description
00222      */
00223     virtual const std::string getDescription() const;
00224 
00225     /**
00226      * Returns a prototype instantiated with the true type of the deriving class.
00227      *
00228      * \return the prototype.
00229      */
00230     static boost::shared_ptr< WPrototyped > getPrototype();
00231 
00232     /**
00233      * Getter for the lines' vertices
00234      * \return The vertices of the lines
00235      */
00236     VertexArray getVertices() const;
00237 
00238     /**
00239      * Return the indices that indicate at which vertex ID each line begins in the vertex array.
00240      * \return The start indices of the lines
00241      */
00242     IndexArray getLineStartIndexes() const;
00243 
00244     /**
00245      * Return the number of vertices for all lines.
00246      * \return The numbers of all lines' vertices
00247      */
00248     LengthArray getLineLengths() const;
00249 
00250     /**
00251      * Returns a reverse lookup table that allow do find out which vertex belongs to which line.
00252      * \return Lookup table from vertices to lines.
00253      */
00254     IndexArray getVerticesReverse() const;
00255 
00256     /**
00257      * Returns an array containing the tangents of the fibers at the vertices.
00258      * \return The tangents of the fibers.
00259      */
00260     TangentArray getTangents() const;
00261 
00262     /**
00263      * Reference to the vector storing the global colors.
00264      *
00265      * \return Pointer to the float array. This always is RGB.
00266      */
00267     ColorArray getGlobalColors() const;
00268 
00269     /**
00270      * Reference to the vector storing the local colors.
00271      *
00272      * \return Pointer to the float array. This always is RGB.
00273      */
00274     ColorArray getLocalColors() const;
00275 
00276     /**
00277      * This method adds a new color scheme to the list of available colors. The color scheme needs to have a name and description to allow the
00278      * user to identify which color has which meaning. If the specified color array already exists, only an update is triggered and the name and
00279      * description is ignored. It detects the type of colors by its size.
00280      *
00281      * \param colors the color array. Needs to have exactly getVertices()->size() items.
00282      * \param name name of the color scheme. Should be a telling name.
00283      * \param description description. How calculated and so on.
00284      */
00285     void addColorScheme( WDataSetFibers::ColorArray colors, std::string name, std::string description );
00286 
00287     /**
00288      * This method removes the specified color scheme from the list and triggers an update.
00289      *
00290      * \param colors the color array.
00291      */
00292     void removeColorScheme( WDataSetFibers::ColorArray colors );
00293 
00294     /**
00295      * Replaces the specified old color scheme by the new color scheme. If the old color scheme did not exist, nothing happens.
00296      *
00297      * \param oldColors old colors to remove
00298      * \param newColors new colors to set
00299      */
00300     void replaceColorScheme( WDataSetFibers::ColorArray oldColors, WDataSetFibers::ColorArray newColors );
00301 
00302     /**
00303      * Get the color scheme with the specified name. If it is not found, an exception gets thrown.
00304      *
00305      * \param name the name of the color scheme
00306      *
00307      * \return the color scheme
00308      * \throw WDHNoSuchDataSet if the name could not be found.
00309      */
00310     const boost::shared_ptr< ColorScheme > getColorScheme( std::string name ) const;
00311 
00312     /**
00313      * Get the color scheme with the specified ID. If the index is invalid, an exception gets thrown.
00314      *
00315      * \param idx the index
00316      *
00317      * \return the color scheme
00318      */
00319     const boost::shared_ptr< ColorScheme > getColorScheme( size_t idx ) const;
00320 
00321     /**
00322      * Convenience method returning the currently selected scheme. This is a comfortable alternative to using the color scheme selection
00323      * property.
00324      *
00325      * \return the current active color scheme
00326      */
00327     const boost::shared_ptr< ColorScheme > getColorScheme() const;
00328 
00329     /**
00330      * Returns the property controlling the color scheme selection.
00331      *
00332      * \return the property.
00333      */
00334     const WPropSelection getColorSchemeProperty() const;
00335 
00336     /**
00337      * returns the position in space for a vertex of a given fiber
00338      *
00339      * \param fiber Index of fiber
00340      * \param vertex Index of vertex in fiber.
00341      *
00342      * \return Position of the given vertex of the also given fiber
00343      */
00344     WPosition getPosition( size_t fiber, size_t vertex ) const;
00345 
00346     /**
00347      * calculates the tangent for a point on the fiber
00348      *
00349      * \param fiber Index of fiber
00350      * \param vertex Index of vertex in fiber
00351      *
00352      * \return Tangent of the given vertex of the also given fiber
00353      */
00354     WPosition getTangent( size_t fiber, size_t vertex ) const;
00355 
00356     /**
00357      * Get the bounding box.
00358      * \return The bounding box of all lines.
00359      */
00360     WBoundingBox getBoundingBox() const;
00361 
00362     /**
00363      * Constructs a WFiber out of the given tract number.
00364      *
00365      * \param numTract Number of the tract to generate a WFiber object for
00366      *
00367      * \return The WFiber object. Attention: copy by value!
00368      */
00369     WFiber operator[]( size_t numTract ) const;
00370 
00371 protected:
00372     /**
00373      * The prototype as singleton.
00374      */
00375     static boost::shared_ptr< WPrototyped > m_prototype;
00376 
00377 private:
00378     /**
00379      * This does the common initialisation of the constructors.
00380      */
00381     void init();
00382 
00383     /**
00384      * Point vector for all fibers
00385      */
00386     VertexArray m_vertices;
00387 
00388     /**
00389      * Point vector for tangents at each vertex, used for fake tubes
00390      */
00391     TangentArray m_tangents;
00392 
00393     // the following typedefs are for convenience.
00394 
00395     /**
00396      * An array of color arrays. The first two elements are: 0: global color, 1: local color
00397      */
00398     boost::shared_ptr< WItemSelection > m_colors;
00399 
00400     /**
00401      * Property keeping track of the active color in m_colors.
00402      */
00403     WPropSelection m_colorProp;
00404 
00405     /**
00406      * Line vector that contains the start index of its first point for each line.
00407      * \warning The index returned cannot be used in the vertices array until
00408      * the number of components for each point is multiplied.
00409      */
00410     IndexArray m_lineStartIndexes;
00411 
00412     /**
00413      * Line vector that contains the number of vertices for each line
00414      */
00415     LengthArray m_lineLengths;
00416 
00417     /**
00418      * Reverse lookup table for which point belongs to which fiber
00419      */
00420     IndexArray m_verticesReverse;
00421 
00422     /**
00423      * Axis aligned bounding box for all tract-vertices of this dataset.
00424      */
00425     WBoundingBox m_bb;
00426 };
00427 
00428 #endif  // WDATASETFIBERS_H