OpenWalnut  1.3.1
WDataSetFibers.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WDATASETFIBERS_H
26 #define WDATASETFIBERS_H
27 
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #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 
40 
41 // forward declarations
42 class WFiber;
43 
44 /**
45  * Represents a simple set of WFibers.
46  */
47 class WDataSetFibers : public WDataSet // NOLINT
48 {
49 public:
50  // some type alias for the used arrays.
51  /**
52  * Pointer to dataset.
53  */
54  typedef boost::shared_ptr< WDataSetFibers > SPtr;
55 
56  /**
57  * Pointer to const dataset.
58  */
59  typedef boost::shared_ptr< const WDataSetFibers > ConstSPtr;
60 
61  /**
62  * List of vertex coordinates in term of components of vertices.
63  */
64  typedef boost::shared_ptr< std::vector< float > > VertexArray;
65 
66  /**
67  * Index list indexing fibers in VertexArray in terms of vertex numbers.
68  */
69  typedef boost::shared_ptr< std::vector< size_t > > IndexArray;
70 
71  /**
72  * Lengths of fibers in terms of verties.
73  */
74  typedef boost::shared_ptr< std::vector< size_t > > LengthArray;
75 
76  /**
77  * Tangents at each vertex in VertexArray.
78  */
79  typedef boost::shared_ptr< std::vector< float > > TangentArray;
80 
81  /**
82  * Colors for each vertex in VertexArray.
83  */
84  typedef boost::shared_ptr< std::vector< float > > ColorArray;
85 
86  /**
87  * Item used in the selection below also containing color info.
88  */
90  {
91  friend class WDataSetFibers;
92  public:
93  /**
94  * different kinds of color arrays can be used in this class. This enum defines their possible types.
95  */
96  typedef enum
97  {
98  GRAY = 1, //!< gray value per vertex
99  RGB = 3, //!< rgb per vertex
100  RGBA = 4 //!< rgba per vertex
101  }
102  ColorMode;
103 
104  /**
105  * Constructor. Creates new item.
106  *
107  * \param name name, name of item.
108  * \param description description of item. Can be empty.
109  * \param icon icon, can be NULL
110  * \param color the color array of this item.
111  * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
112  */
113  ColorScheme( std::string name, std::string description, const char** icon, ColorArray color, ColorMode mode = RGB ):
114  WItemSelectionItem( name, description, icon ),
115  m_color( color ),
116  m_mode( mode )
117  {
118  };
119 
120  /**
121  * Get the color.
122  *
123  * \return the color array.
124  */
126  {
127  return m_color;
128  };
129 
130  /**
131  * Returns the mode of the color scheme.
132  *
133  * \return the mode.
134  */
136  {
137  return m_mode;
138  };
139 
140  protected:
141  /**
142  * Sets the color array for this item.
143  *
144  * \param color the color to set.
145  * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
146  */
147  void setColor( ColorArray color, ColorMode mode = RGB )
148  {
149  m_color = color;
150  m_mode = mode;
151  };
152 
153  private:
154  /**
155  * The color array associated with the item.
156  */
158 
159  /**
160  * Coloring mode.
161  */
163  };
164 
165  /**
166  * Constructs a new set of fibers.
167  *
168  * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
169  * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
170  * \param lineLengths how many vertices belong to a fiber
171  * \param verticesReverse stores for each vertex the index of the corresponding fiber
172  * \param boundingBox The bounding box of the fibers (first minimum, second maximum).
173  */
174  WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
175  boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
176  boost::shared_ptr< std::vector< size_t > > lineLengths,
177  boost::shared_ptr< std::vector< size_t > > verticesReverse,
178  WBoundingBox boundingBox );
179 
180  /**
181  * Constructs a new set of fibers. This constructor determines the bounding box by using the coordinates of the vertices.
182  *
183  * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
184  * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
185  * \param lineLengths how many vertices belong to a fiber
186  * \param verticesReverse stores for each vertex the index of the corresponding fiber
187  */
188  WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
189  boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
190  boost::shared_ptr< std::vector< size_t > > lineLengths,
191  boost::shared_ptr< std::vector< size_t > > verticesReverse );
192 
193  /**
194  * Constructs a new set of tracts. The constructed instance is not usable but needed for prototype mechanism.
195  */
196  WDataSetFibers();
197 
198  /**
199  * Get number of tracts in this data set.
200  * \return number of fibers
201  */
202  size_t size() const;
203 
204  /**
205  * Determines whether this dataset can be used as a texture.
206  *
207  * \return true if usable as texture.
208  */
209  virtual bool isTexture() const;
210 
211  /**
212  * Gets the name of this prototype.
213  *
214  * \return the name.
215  */
216  virtual const std::string getName() const;
217 
218  /**
219  * Gets the description for this prototype.
220  *
221  * \return the description
222  */
223  virtual const std::string getDescription() const;
224 
225  /**
226  * Returns a prototype instantiated with the true type of the deriving class.
227  *
228  * \return the prototype.
229  */
230  static boost::shared_ptr< WPrototyped > getPrototype();
231 
232  /**
233  * Getter for the lines' vertices
234  * \return The vertices of the lines
235  */
236  VertexArray getVertices() const;
237 
238  /**
239  * Return the indices that indicate at which vertex ID each line begins in the vertex array.
240  * \return The start indices of the lines
241  */
243 
244  /**
245  * Return the number of vertices for all lines.
246  * \return The numbers of all lines' vertices
247  */
248  LengthArray getLineLengths() const;
249 
250  /**
251  * Returns a reverse lookup table that allow do find out which vertex belongs to which line.
252  * \return Lookup table from vertices to lines.
253  */
255 
256  /**
257  * Returns an array containing the tangents of the fibers at the vertices.
258  * \return The tangents of the fibers.
259  */
260  TangentArray getTangents() const;
261 
262  /**
263  * Reference to the vector storing the global colors.
264  *
265  * \return Pointer to the float array. This always is RGB.
266  */
267  ColorArray getGlobalColors() const;
268 
269  /**
270  * Reference to the vector storing the local colors.
271  *
272  * \return Pointer to the float array. This always is RGB.
273  */
274  ColorArray getLocalColors() const;
275 
276  /**
277  * 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
278  * user to identify which color has which meaning. If the specified color array already exists, only an update is triggered and the name and
279  * description is ignored. It detects the type of colors by its size.
280  *
281  * \param colors the color array. Needs to have exactly getVertices()->size() items.
282  * \param name name of the color scheme. Should be a telling name.
283  * \param description description. How calculated and so on.
284  */
285  void addColorScheme( WDataSetFibers::ColorArray colors, std::string name, std::string description );
286 
287  /**
288  * This method removes the specified color scheme from the list and triggers an update.
289  *
290  * \param colors the color array.
291  */
293 
294  /**
295  * Replaces the specified old color scheme by the new color scheme. If the old color scheme did not exist, nothing happens.
296  *
297  * \param oldColors old colors to remove
298  * \param newColors new colors to set
299  */
301 
302  /**
303  * Get the color scheme with the specified name. If it is not found, an exception gets thrown.
304  *
305  * \param name the name of the color scheme
306  *
307  * \return the color scheme
308  * \throw WDHNoSuchDataSet if the name could not be found.
309  */
310  const boost::shared_ptr< ColorScheme > getColorScheme( std::string name ) const;
311 
312  /**
313  * Get the color scheme with the specified ID. If the index is invalid, an exception gets thrown.
314  *
315  * \param idx the index
316  *
317  * \return the color scheme
318  */
319  const boost::shared_ptr< ColorScheme > getColorScheme( size_t idx ) const;
320 
321  /**
322  * Convenience method returning the currently selected scheme. This is a comfortable alternative to using the color scheme selection
323  * property.
324  *
325  * \return the current active color scheme
326  */
327  const boost::shared_ptr< ColorScheme > getColorScheme() const;
328 
329  /**
330  * Returns the property controlling the color scheme selection.
331  *
332  * \return the property.
333  */
334  const WPropSelection getColorSchemeProperty() const;
335 
336  /**
337  * returns the position in space for a vertex of a given fiber
338  *
339  * \param fiber Index of fiber
340  * \param vertex Index of vertex in fiber.
341  *
342  * \return Position of the given vertex of the also given fiber
343  */
344  WPosition getPosition( size_t fiber, size_t vertex ) const;
345 
346  /**
347  * calculates the tangent for a point on the fiber
348  *
349  * \param fiber Index of fiber
350  * \param vertex Index of vertex in fiber
351  *
352  * \return Tangent of the given vertex of the also given fiber
353  */
354  WPosition getTangent( size_t fiber, size_t vertex ) const;
355 
356  /**
357  * Get the bounding box.
358  * \return The bounding box of all lines.
359  */
361 
362  /**
363  * Constructs a WFiber out of the given tract number.
364  *
365  * \param numTract Number of the tract to generate a WFiber object for
366  *
367  * \return The WFiber object. Attention: copy by value!
368  */
369  WFiber operator[]( size_t numTract ) const;
370 
371 protected:
372  /**
373  * The prototype as singleton.
374  */
375  static boost::shared_ptr< WPrototyped > m_prototype;
376 
377 private:
378  /**
379  * This does the common initialisation of the constructors.
380  */
381  void init();
382 
383  /**
384  * Point vector for all fibers
385  */
387 
388  /**
389  * Point vector for tangents at each vertex, used for fake tubes
390  */
392 
393  // the following typedefs are for convenience.
394 
395  /**
396  * An array of color arrays. The first two elements are: 0: global color, 1: local color
397  */
398  boost::shared_ptr< WItemSelection > m_colors;
399 
400  /**
401  * Property keeping track of the active color in m_colors.
402  */
403  WPropSelection m_colorProp;
404 
405  /**
406  * Line vector that contains the start index of its first point for each line.
407  * \warning The index returned cannot be used in the vertices array until
408  * the number of components for each point is multiplied.
409  */
411 
412  /**
413  * Line vector that contains the number of vertices for each line
414  */
416 
417  /**
418  * Reverse lookup table for which point belongs to which fiber
419  */
421 
422  /**
423  * Axis aligned bounding box for all tract-vertices of this dataset.
424  */
426 };
427 
428 #endif // WDATASETFIBERS_H