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