WDataSetFibers.h 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

#ifndef WDATASETFIBERS_H
#define WDATASETFIBERS_H

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
28
#include <string>
29
#include <utility>
30 31 32
#include <vector>

#include <boost/shared_ptr.hpp>
33
#include <boost/tuple/tuple.hpp>
34

35
#include "../common/math/linearAlgebra/WLinearAlgebra.h"
36
#include "../common/WBoundingBox.h"
37
#include "../common/WProperties.h"
38
#include "WDataSet.h"
39
#include "WExportDataHandler.h"
Mathias Goldau's avatar
Mathias Goldau committed
40

41 42
// forward declarations
class WFiber;
43 44 45 46

/**
 * Represents a simple set of WFibers.
 */
47
class OWDATAHANDLER_EXPORT WDataSetFibers : public WDataSet // NOLINT
48 49
{
public:
50 51 52 53

    // some type alias for the used arrays.

    /**
54
     * List of vertex coordinates in term of components of vertices.
55 56 57 58
     */
    typedef boost::shared_ptr< std::vector< float > > VertexArray;

    /**
59
     * Index list indexing fibers in VertexArray in terms of vertex numbers.
60 61 62 63
     */
    typedef boost::shared_ptr< std::vector< size_t > > IndexArray;

    /**
64
     * Lengths of fibers in terms of verties.
65 66 67 68 69 70 71 72 73 74 75 76 77
     */
    typedef boost::shared_ptr< std::vector< size_t > > LengthArray;

    /**
     * Tangents at each vertex in VertexArray.
     */
    typedef boost::shared_ptr< std::vector< float > > TangentArray;

    /**
     * Colors for each vertex in VertexArray.
     */
    typedef boost::shared_ptr< std::vector< float > > ColorArray;

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    /**
     * Item used in the selection below also containing color info.
     */
    class ColorScheme: public WItemSelectionItem
    {
    friend class WDataSetFibers;
    public:

        /**
         * different kinds of color arrays can be used in this class. This enum defines their possible types.
         */
        typedef enum
        {
            GRAY = 1,   //!< gray value per vertex
            RGB  = 3,   //!< rgb per vertex
            RGBA = 4    //!< rgba per vertex
        }
        ColorMode;

        /**
         * Constructor. Creates new item.
         *
         * \param name name, name of item.
         * \param description description of item. Can be empty.
         * \param icon icon, can be NULL
         * \param color the color array of this item.
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
104
         * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
         */
        ColorScheme( std::string name, std::string description, const char** icon, ColorArray color, ColorMode mode = RGB ):
            WItemSelectionItem( name, description, icon ),
            m_color( color ),
            m_mode( mode )
        {
        };

        /**
         * Get the color.
         *
         * \return the color array.
         */
        ColorArray getColor() const
        {
            return m_color;
        };

        /**
         * Returns the mode of the color scheme.
         *
         * \return the mode.
         */
        ColorMode getMode() const
        {
            return m_mode;
        };

    protected:

        /**
         * Sets the color array for this item.
         *
         * \param color the color to set.
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
139
         * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
         */
        void setColor( ColorArray color, ColorMode mode = RGB )
        {
            m_color = color;
            m_mode = mode;
        };

    private:
        /**
         * The color array associated with the item.
         */
        ColorArray m_color;

        /**
         * Coloring mode.
         */
        ColorMode m_mode;
    };

159
    /**
160
     * Constructs a new set of fibers.
161
     *
162 163 164 165
     * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
     * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
     * \param lineLengths how many vertices belong to a fiber
     * \param verticesReverse stores for each vertex the index of the corresponding fiber
166
     * \param boundingBox The bounding box of the fibers (first minimum, second maximum).
167
     */
Mathias Goldau's avatar
Mathias Goldau committed
168 169 170
    WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
                    boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
                    boost::shared_ptr< std::vector< size_t > > lineLengths,
171
                    boost::shared_ptr< std::vector< size_t > > verticesReverse,
172
                    WBoundingBox boundingBox );
173 174 175 176 177 178 179 180 181 182 183 184 185

    /**
     * Constructs a new set of fibers. This constructor determines the bounding box by using the coordinates of the vertices.
     *
     * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
     * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
     * \param lineLengths how many vertices belong to a fiber
     * \param verticesReverse stores for each vertex the index of the corresponding fiber
     */
    WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
                    boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
                    boost::shared_ptr< std::vector< size_t > > lineLengths,
                    boost::shared_ptr< std::vector< size_t > > verticesReverse );
186

187
    /**
188
     * Constructs a new set of tracts. The constructed instance is not usable but needed for prototype mechanism.
189 190 191
     */
    WDataSetFibers();

192
    /**
193
     * Get number of tracts in this data set.
194
     * \return number of fibers
195
     */
196
    size_t size() const;
197

198 199 200 201 202 203 204
    /**
     * Determines whether this dataset can be used as a texture.
     *
     * \return true if usable as texture.
     */
    virtual bool isTexture() const;

205 206 207 208 209
    /**
     * Gets the name of this prototype.
     *
     * \return the name.
     */
210
    virtual const std::string getName() const;
211 212 213 214 215 216

    /**
     * Gets the description for this prototype.
     *
     * \return the description
     */
217
    virtual const std::string getDescription() const;
218 219 220 221 222 223 224 225

    /**
     * Returns a prototype instantiated with the true type of the deriving class.
     *
     * \return the prototype.
     */
    static boost::shared_ptr< WPrototyped > getPrototype();

Mathias Goldau's avatar
Mathias Goldau committed
226
    /**
227 228
     * Getter for the lines' vertices
     * \return The vertices of the lines
Mathias Goldau's avatar
Mathias Goldau committed
229
     */
230
    VertexArray getVertices() const;
Mathias Goldau's avatar
Mathias Goldau committed
231 232

    /**
233 234
     * Return the indices that indicate at which vertex ID each line begins in the vertex array.
     * \return The start indices of the lines
Mathias Goldau's avatar
Mathias Goldau committed
235
     */
236
    IndexArray getLineStartIndexes() const;
Mathias Goldau's avatar
Mathias Goldau committed
237 238

    /**
239 240
     * Return the number of vertices for all lines.
     * \return The numbers of all lines' vertices
Mathias Goldau's avatar
Mathias Goldau committed
241
     */
242
    LengthArray getLineLengths() const;
Mathias Goldau's avatar
Mathias Goldau committed
243 244

    /**
245 246
     * Returns a reverse lookup table that allow do find out which vertex belongs to which line.
     * \return Lookup table from vertices to lines.
Mathias Goldau's avatar
Mathias Goldau committed
247
     */
248
    IndexArray getVerticesReverse() const;
Mathias Goldau's avatar
Mathias Goldau committed
249

schurade's avatar
schurade committed
250
    /**
251 252
     * Returns an array containing the tangents of the fibers at the vertices.
     * \return The tangents of the fibers.
schurade's avatar
schurade committed
253
     */
254
    TangentArray getTangents() const;
schurade's avatar
schurade committed
255 256

    /**
257 258
     * Reference to the vector storing the global colors.
     *
259
     * \return Pointer to the float array. This always is RGB.
schurade's avatar
schurade committed
260
     */
261
    ColorArray getGlobalColors() const;
schurade's avatar
schurade committed
262

263 264 265
    /**
     * Reference to the vector storing the local colors.
     *
266
     * \return Pointer to the float array. This always is RGB.
267
     */
268 269 270 271 272
    ColorArray getLocalColors() const;

    /**
     * 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
     * user to identify which color has which meaning. If the specified color array already exists, only an update is triggered and the name and
273
     * description is ignored. It detects the type of colors by its size.
274 275 276 277 278
     *
     * \param colors the color array. Needs to have exactly getVertices()->size() items.
     * \param name name of the color scheme. Should be a telling name.
     * \param description description. How calculated and so on.
     */
Mathias Goldau's avatar
[DOC]  
Mathias Goldau committed
279
    void addColorScheme( WDataSetFibers::ColorArray colors, std::string name, std::string description );
280 281 282

    /**
     * This method removes the specified color scheme from the list and triggers an update.
283
     *
284 285
     * \param colors the color array.
     */
Mathias Goldau's avatar
[DOC]  
Mathias Goldau committed
286
    void removeColorScheme( WDataSetFibers::ColorArray colors );
schurade's avatar
schurade committed
287

288 289 290 291 292 293 294 295
    /**
     * Replaces the specified old color scheme by the new color scheme. If the old color scheme did not exist, nothing happens.
     *
     * \param oldColors old colors to remove
     * \param newColors new colors to set
     */
    void replaceColorScheme( WDataSetFibers::ColorArray oldColors, WDataSetFibers::ColorArray newColors );

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    /**
     * Get the color scheme with the specified name. If it is not found, an exception gets thrown.
     *
     * \param name the name of the color scheme
     *
     * \return the color scheme
     * \throw WDHNoSuchDataSet if the name could not be found.
     */
    const boost::shared_ptr< ColorScheme > getColorScheme( std::string name ) const;

    /**
     * Get the color scheme with the specified ID. If the index is invalid, an exception gets thrown.
     *
     * \param idx the index
     *
     * \return the color scheme
     */
    const boost::shared_ptr< ColorScheme > getColorScheme( size_t idx ) const;

    /**
     * Convenience method returning the currently selected scheme. This is a comfortable alternative to using the color scheme selection
     * property.
     *
     * \return the current active color scheme
     */
    const boost::shared_ptr< ColorScheme > getColorScheme() const;

    /**
     * Returns the property controlling the color scheme selection.
     *
     * \return the property.
     */
    const WPropSelection getColorSchemeProperty() const;

Mathias Goldau's avatar
Mathias Goldau committed
330 331 332
    /**
     * returns the position in space for a vertex of a given fiber
     *
333 334 335 336
     * \param fiber Index of fiber
     * \param vertex Index of vertex in fiber.
     *
     * \return Position of the given vertex of the also given fiber
Mathias Goldau's avatar
Mathias Goldau committed
337
     */
338
    WPosition getPosition( size_t fiber, size_t vertex ) const;
Mathias Goldau's avatar
Mathias Goldau committed
339

schurade's avatar
schurade committed
340 341 342
    /**
     * calculates the tangent for a point on the fiber
     *
343 344 345 346
     * \param fiber Index of fiber
     * \param vertex Index of vertex in fiber
     *
     * \return Tangent of the given vertex of the also given fiber
schurade's avatar
schurade committed
347
     */
348
    WPosition getTangent( size_t fiber, size_t vertex ) const;
schurade's avatar
schurade committed
349

350
    /**
351
     * Get the bounding box.
352
     * \return The bounding box of all lines.
353
     */
354
    WBoundingBox getBoundingBox() const;
355

356
    /**
357
     * Constructs a WFiber out of the given tract number.
358
     *
359
     * \param numTract Number of the tract to generate a WFiber object for
360
     *
361
     * \return The WFiber object. Attention: copy by value!
362
     */
363
    WFiber operator[]( size_t numTract ) const;
364

365
protected:
366 367 368 369 370 371

    /**
     * The prototype as singleton.
     */
    static boost::shared_ptr< WPrototyped > m_prototype;

372
private:
373 374 375 376 377
    /**
     * This does the common initialisation of the constructors.
     */
    void init();

Mathias Goldau's avatar
Mathias Goldau committed
378
    /**
schurade's avatar
schurade committed
379
     * Point vector for all fibers
Mathias Goldau's avatar
Mathias Goldau committed
380
     */
381
    VertexArray m_vertices;
Mathias Goldau's avatar
Mathias Goldau committed
382

schurade's avatar
schurade committed
383 384 385
    /**
     * Point vector for tangents at each vertex, used for fake tubes
     */
386 387 388 389 390
    TangentArray m_tangents;

    // the following typedefs are for convenience.

    /**
391
     * An array of color arrays. The first two elements are: 0: global color, 1: local color
392
     */
393
    boost::shared_ptr< WItemSelection > m_colors;
394 395

    /**
396
     * Property keeping track of the active color in m_colors.
schurade's avatar
schurade committed
397
     */
398
    WPropSelection m_colorProp;
schurade's avatar
schurade committed
399

Mathias Goldau's avatar
Mathias Goldau committed
400
    /**
401 402 403
     * Line vector that contains the start index of its first point for each line.
     * \warning The index returned cannot be used in the vertices array until
     * the number of components for each point is multiplied.
Mathias Goldau's avatar
Mathias Goldau committed
404
     */
405
    IndexArray m_lineStartIndexes;
Mathias Goldau's avatar
Mathias Goldau committed
406 407 408 409

    /**
     * Line vector that contains the number of vertices for each line
     */
410
    LengthArray m_lineLengths;
Mathias Goldau's avatar
Mathias Goldau committed
411 412 413 414

    /**
     * Reverse lookup table for which point belongs to which fiber
     */
415
    IndexArray m_verticesReverse;
416

417 418 419 420
    /**
     * Axis aligned bounding box for all tract-vertices of this dataset.
     */
    WBoundingBox m_bb;
421 422 423
};

#endif  // WDATASETFIBERS_H