OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #include "WExportDataHandler.h"
40 
41 // forward declarations
42 class WFiber;
43 
44 /**
45  * Represents a simple set of WFibers.
46  */
48 {
49 public:
50 
51  // some type alias for the used arrays.
52 
53  /**
54  * List of vertex coordinates in term of components of vertices.
55  */
56  typedef boost::shared_ptr< std::vector< float > > VertexArray;
57 
58  /**
59  * Index list indexing fibers in VertexArray in terms of vertex numbers.
60  */
61  typedef boost::shared_ptr< std::vector< size_t > > IndexArray;
62 
63  /**
64  * Lengths of fibers in terms of verties.
65  */
66  typedef boost::shared_ptr< std::vector< size_t > > LengthArray;
67 
68  /**
69  * Tangents at each vertex in VertexArray.
70  */
71  typedef boost::shared_ptr< std::vector< float > > TangentArray;
72 
73  /**
74  * Colors for each vertex in VertexArray.
75  */
76  typedef boost::shared_ptr< std::vector< float > > ColorArray;
77 
78  /**
79  * Item used in the selection below also containing color info.
80  */
82  {
83  friend class WDataSetFibers;
84  public:
85 
86  /**
87  * different kinds of color arrays can be used in this class. This enum defines their possible types.
88  */
89  typedef enum
90  {
91  GRAY = 1, //!< gray value per vertex
92  RGB = 3, //!< rgb per vertex
93  RGBA = 4 //!< rgba per vertex
94  }
95  ColorMode;
96 
97  /**
98  * Constructor. Creates new item.
99  *
100  * \param name name, name of item.
101  * \param description description of item. Can be empty.
102  * \param icon icon, can be NULL
103  * \param color the color array of this item.
104  * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
105  */
106  ColorScheme( std::string name, std::string description, const char** icon, ColorArray color, ColorMode mode = RGB ):
107  WItemSelectionItem( name, description, icon ),
108  m_color( color ),
109  m_mode( mode )
110  {
111  };
112 
113  /**
114  * Get the color.
115  *
116  * \return the color array.
117  */
118  ColorArray getColor() const
119  {
120  return m_color;
121  };
122 
123  /**
124  * Returns the mode of the color scheme.
125  *
126  * \return the mode.
127  */
128  ColorMode getMode() const
129  {
130  return m_mode;
131  };
132 
133  protected:
134 
135  /**
136  * Sets the color array for this item.
137  *
138  * \param color the color to set.
139  * \param mode the mode of the color array. This defines whether the colors are luminance, RGB or RGBA
140  */
141  void setColor( ColorArray color, ColorMode mode = RGB )
142  {
143  m_color = color;
144  m_mode = mode;
145  };
146 
147  private:
148  /**
149  * The color array associated with the item.
150  */
151  ColorArray m_color;
152 
153  /**
154  * Coloring mode.
155  */
157  };
158 
159  /**
160  * Constructs a new set of fibers.
161  *
162  * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
163  * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
164  * \param lineLengths how many vertices belong to a fiber
165  * \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  */
168  WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
169  boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
170  boost::shared_ptr< std::vector< size_t > > lineLengths,
171  boost::shared_ptr< std::vector< size_t > > verticesReverse,
172  WBoundingBox boundingBox );
173 
174  /**
175  * Constructs a new set of fibers. This constructor determines the bounding box by using the coordinates of the vertices.
176  *
177  * \param vertices the vertices of the fibers, stored in x1,y1,z1,x2,y2,z2, ..., xn,yn,zn scheme
178  * \param lineStartIndexes the index in which the fiber start (index of the 3D-vertex, not the index of the float in the vertices vector)
179  * \param lineLengths how many vertices belong to a fiber
180  * \param verticesReverse stores for each vertex the index of the corresponding fiber
181  */
182  WDataSetFibers( boost::shared_ptr< std::vector< float > >vertices,
183  boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
184  boost::shared_ptr< std::vector< size_t > > lineLengths,
185  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  WDataSetFibers();
191 
192  /**
193  * Get number of tracts in this data set.
194  * \return number of fibers
195  */
196  size_t size() const;
197 
198  /**
199  * Determines whether this dataset can be used as a texture.
200  *
201  * \return true if usable as texture.
202  */
203  virtual bool isTexture() const;
204 
205  /**
206  * Gets the name of this prototype.
207  *
208  * \return the name.
209  */
210  virtual const std::string getName() const;
211 
212  /**
213  * Gets the description for this prototype.
214  *
215  * \return the description
216  */
217  virtual const std::string getDescription() const;
218 
219  /**
220  * Returns a prototype instantiated with the true type of the deriving class.
221  *
222  * \return the prototype.
223  */
224  static boost::shared_ptr< WPrototyped > getPrototype();
225 
226  /**
227  * Getter for the lines' vertices
228  * \return The vertices of the lines
229  */
230  VertexArray getVertices() const;
231 
232  /**
233  * Return the indices that indicate at which vertex ID each line begins in the vertex array.
234  * \return The start indices of the lines
235  */
237 
238  /**
239  * Return the number of vertices for all lines.
240  * \return The numbers of all lines' vertices
241  */
242  LengthArray getLineLengths() const;
243 
244  /**
245  * Returns a reverse lookup table that allow do find out which vertex belongs to which line.
246  * \return Lookup table from vertices to lines.
247  */
249 
250  /**
251  * Returns an array containing the tangents of the fibers at the vertices.
252  * \return The tangents of the fibers.
253  */
254  TangentArray getTangents() const;
255 
256  /**
257  * Reference to the vector storing the global colors.
258  *
259  * \return Pointer to the float array. This always is RGB.
260  */
261  ColorArray getGlobalColors() const;
262 
263  /**
264  * Reference to the vector storing the local colors.
265  *
266  * \return Pointer to the float array. This always is RGB.
267  */
268  ColorArray getLocalColors() const;
269 
270  /**
271  * 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
272  * 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  * \param colors the color array. Needs to have exactly getVertices()->size() items.
276  * \param name name of the color scheme. Should be a telling name.
277  * \param description description. How calculated and so on.
278  */
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  * \param colors the color array.
285  */
287 
288  /**
289  * Replaces the specified old color scheme by the new color scheme. If the old color scheme did not exist, nothing happens.
290  *
291  * \param oldColors old colors to remove
292  * \param newColors new colors to set
293  */
295 
296  /**
297  * Get the color scheme with the specified name. If it is not found, an exception gets thrown.
298  *
299  * \param name the name of the color scheme
300  *
301  * \return the color scheme
302  * \throw WDHNoSuchDataSet if the name could not be found.
303  */
304  const boost::shared_ptr< ColorScheme > getColorScheme( std::string name ) const;
305 
306  /**
307  * Get the color scheme with the specified ID. If the index is invalid, an exception gets thrown.
308  *
309  * \param idx the index
310  *
311  * \return the color scheme
312  */
313  const boost::shared_ptr< ColorScheme > getColorScheme( size_t idx ) const;
314 
315  /**
316  * Convenience method returning the currently selected scheme. This is a comfortable alternative to using the color scheme selection
317  * property.
318  *
319  * \return the current active color scheme
320  */
321  const boost::shared_ptr< ColorScheme > getColorScheme() const;
322 
323  /**
324  * Returns the property controlling the color scheme selection.
325  *
326  * \return the property.
327  */
328  const WPropSelection getColorSchemeProperty() const;
329 
330  /**
331  * returns the position in space for a vertex of a given fiber
332  *
333  * \param fiber Index of fiber
334  * \param vertex Index of vertex in fiber.
335  *
336  * \return Position of the given vertex of the also given fiber
337  */
338  WPosition getPosition( size_t fiber, size_t vertex ) const;
339 
340  /**
341  * calculates the tangent for a point on the fiber
342  *
343  * \param fiber Index of fiber
344  * \param vertex Index of vertex in fiber
345  *
346  * \return Tangent of the given vertex of the also given fiber
347  */
348  WPosition getTangent( size_t fiber, size_t vertex ) const;
349 
350  /**
351  * Get the bounding box.
352  * \return The bounding box of all lines.
353  */
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  * The prototype as singleton.
369  */
370  static boost::shared_ptr< WPrototyped > m_prototype;
371 
372 private:
373  /**
374  * This does the common initialisation of the constructors.
375  */
376  void init();
377 
378  /**
379  * Point vector for all fibers
380  */
382 
383  /**
384  * Point vector for tangents at each vertex, used for fake tubes
385  */
387 
388  // the following typedefs are for convenience.
389 
390  /**
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.
397  */
398  WPropSelection m_colorProp;
399 
400  /**
401  * Line vector that contains the start index of its first point for each line.
402  * \warning The index returned cannot be used in the vertices array until
403  * the number of components for each point is multiplied.
404  */
406 
407  /**
408  * Line vector that contains the number of vertices for each line
409  */
411 
412  /**
413  * Reverse lookup table for which point belongs to which fiber
414  */
416 
417  /**
418  * Axis aligned bounding box for all tract-vertices of this dataset.
419  */
421 };
422 
423 #endif // WDATASETFIBERS_H