OpenWalnut  1.5.0dev
WTransferFunction.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 WTRANSFERFUNCTION_H
26 #define WTRANSFERFUNCTION_H
27 
28 #include <iosfwd>
29 #include <vector>
30 
31 #include "WColor.h"
32 
33 /**
34  * A class that stores a 1D transfer function which consists
35  * of a linear interpolation of alpha and color values.
36  */
37 class WTransferFunction // NOLINT problem with macro
38 {
39 public:
40  /**
41  * Default constructor of a meaningless transfer function
42  */
44  {
45  }
46 
47  /**
48  * Deep copy constructor.
49  *
50  * \param rhs the value to histogram
51  */
53  : m_colors( rhs.m_colors ), m_alphas( rhs.m_alphas ), m_isomin( rhs.m_isomin ), m_isomax( rhs.m_isomax )
54  , m_opacityScale( 1.0 ) , m_histogram( rhs.m_histogram )
55  {
56  }
57 
58  /**
59  * Deep copy operator
60  *
61  * \param rhs the value to copy
62  * \returns reference to current object
63  * \returns reference to current object
64  */
66  {
67  this->m_colors = rhs.m_colors;
68  this->m_alphas = rhs.m_alphas;
69  this->m_isomin = rhs.m_isomin;
70  this->m_isomax = rhs.m_isomax;
71  this->m_histogram = rhs.m_histogram;
72  return ( *this );
73  }
74 
75  /**
76  * Check equivalence of two transfer functions.
77  *
78  * \returns true if this object contains exactly the same
79  * data as rhs
80  * \param rhs object to compare with
81  */
82  bool operator==( const WTransferFunction &rhs ) const;
83 
84  /**
85  * Check equivalence of two transfer functions.
86  *
87  * \returns negated result of operator==
88  * \param rhs the value to compare with
89  */
90  bool operator!=( const WTransferFunction &rhs ) const;
91 
92  /**
93  * Default destuctor.
94  */
96  {
97  }
98 
99  /**
100  * Get the number of alphas
101  *
102  * \returns the number of alpha points
103  */
104  size_t numAlphas() const
105  {
106  return m_alphas.size();
107  }
108 
109  /**
110  * Get the number of colors.
111  *
112  * \returns the number of color points
113  */
114  size_t numColors() const
115  {
116  return m_colors.size();
117  }
118 
119  /**
120  * Get the scaling factor for the opacity.
121  *
122  * \returns scaling factor
123  */
124  double getOpacityScale() const
125  {
126  return m_opacityScale;
127  }
128 
129  /**
130  * Set the scaling factor for the opacity.
131  * \param factor scaling factor of opacity values
132  */
133  void setOpacityScale( double factor )
134  {
135  m_opacityScale = factor;
136  }
137 
138  /**
139  * Get the isovalue at a given index in the alpha values.
140  *
141  * \param i the index of the point to query
142  * \returns the alpha values position/isovalue at index i
143  */
144  double getAlphaIsovalue( size_t i ) const
145  {
146  return m_alphas.at( i ).iso;
147  }
148 
149  /**
150  * The isovalue of the color at a given index.
151  *
152  * \param i the index of the point to query.
153  * \returns the color values position/isovalue at index i
154  */
155  double getColorIsovalue( size_t i ) const
156  {
157  return m_colors.at( i ).iso;
158  }
159 
160  /**
161  * Get alpha at given index.
162  *
163  * \param i the index to query
164  * \returns the alpha value at index i
165  */
166  double getAlpha( size_t i ) const
167  {
168  return m_alphas.at( i ).alpha*m_opacityScale;
169  }
170 
171  /**
172  * Get color at given index.
173  *
174  * \param i the index to query
175  * \returns the color at index i
176  */
177  const WColor& getColor( size_t i ) const
178  {
179  return m_colors.at( i ).color;
180  }
181 
182  /**
183  * Insert a new color point.
184  *
185  * \param iso the new iso value
186  * \param color the new color at the given iso value
187  */
188  void addColor( double iso, const WColor& color );
189 
190  /**
191  * Insert a new alpha point.
192  *
193  * \param iso the new iso value
194  * \param alpha the new alpha value at the given iso value
195  */
196  void addAlpha( double iso, double alpha );
197 
198  /**
199  * Set the histogram going along with the transfer function
200  *
201  * This should be changed in the future to be handled in a different
202  * way. A good option would be to introduce an object encapsulating
203  * a transfer function and histogram information.
204  *
205  * \param data the histogram data between m_isomin and m_isomax
206  */
207  void setHistogram( std::vector< double > & data )
208  {
209  m_histogram.swap( data );
210  }
211 
212  /**
213  * Clears the histogram data so the gui won't show any
214  */
216  {
217  m_histogram.clear();
218  }
219 
220  /**
221  * Returns the histogram.
222  *
223  * \returns a reference to the internal representation of the histogram
224  */
225  const std::vector< double >& getHistogram() const
226  {
227  return m_histogram;
228  }
229 
230  /**
231  * sample/render the transfer function linearly between min and max in an RGBA texture.
232  * \param array pointer to an allocated data structure
233  * \param width is the number of RGBA samples.
234  * \param min the lowest value to be sampled
235  * \param max the hightes value to be sampled
236  * \post array contains the sampled data
237  * \pre array is allocated and has space for width elements
238  */
239  void sample1DTransferFunction( unsigned char*array, int width, double min, double max ) const;
240 
241  /**
242  * Try to estimate a transfer function from an RGBA image.
243  *
244  * \param rgba: values between 0 and 255 representing the red, green, and blue channel
245  * \param size: number of color entries in rgba
246  * \returns approximated transfer function
247  */
248  static WTransferFunction createFromRGBA( unsigned char const * const rgba, size_t size );
249 private:
250  /**
251  * Prototype for a storage element
252  */
253  struct Entry
254  {
255  /**
256  * Default constructor
257  * \param iso the iso value
258  */
259  explicit Entry( double iso ) : iso( iso )
260  {
261  }
262 
263  /**
264  * comparison by isovalue
265  * \param rhs entry to compare t
266  * \returns true if this->iso <= rhs.iso
267  */
268  bool operator <= ( const Entry &rhs ) const
269  {
270  return iso <= rhs.iso;
271  }
272 
273  /** the isovalue */
274  double iso;
275  };
276 
277  /**
278  * Color entries are linearly interpolated colors along isovalues
279  */
280  struct ColorEntry : public Entry
281  {
282  /** default constructor
283  * \param iso the isovalue
284  * \param color the color at the isovalue
285  */
286  ColorEntry( double iso, WColor color ) : Entry( iso ), color( color )
287  {
288  }
289 
290  /**
291  * comparison operator to check for changes
292  * \param rhs ColorEntry to compare to
293  * \returns true if rhs equals this
294  */
295  bool operator==( const ColorEntry& rhs ) const
296  {
297  return iso == rhs.iso && color == rhs.color;
298  }
299 
300  /** holds the current color at isovalue Entry::iso */
301  WColor color;
302  };
303 
304  /**
305  * Alpha entries represent linearly interpolated transparency values
306  * along the isovalue scale. Alpha is in the range [ 0...1 ] where
307  * 1 signifies entirely opaque ( which makes it more an opacity value instead
308  * of alpha, but I use the commonly used language here )
309  */
310  struct AlphaEntry : public Entry
311  {
312  /** default constructor
313  * \param iso the isovalue
314  * \param alpha the alpha at the isovalue in the range from 0 = transparent to 1 = opaque
315  */
316  AlphaEntry( double iso, double alpha ) : Entry( iso ), alpha( alpha )
317  {
318  }
319 
320  /**
321  * comparison operator to check for changes
322  * \param rhs AlphaEntry to compare to
323  * \returns true if rhs is equal to this
324  */
325  bool operator==( const AlphaEntry& rhs ) const
326  {
327  return iso == rhs.iso && alpha == rhs.alpha;
328  }
329 
330  /** holds the current alpha at isovalue Entry::iso */
331  double alpha;
332  };
333 
334  /**
335  * Templatized comparison predicate for internal searching
336  */
337  template<typename T>
338  struct LessPred
339  {
340  /** constructs a predicate that compares for less than iso
341  * \param iso: used iso value
342  */
343  explicit LessPred( double iso ) : iso( iso )
344  {
345  }
346 
347  /** isovalue-based comparison
348  * \param t the object to compare to
349  * \returns true if iso is less than t.iso
350  */
351  bool operator()( const T& t )
352  {
353  return iso < t.iso;
354  }
355 
356  /** the isovalue to compare to */
357  double iso;
358  };
359 
360  /**
361  * Sorted list of colors
362  */
363  std::vector<ColorEntry> m_colors;
364 
365  /**
366  * Sorted list of alpha values.
367  */
368  std::vector<AlphaEntry> m_alphas;
369 
370  /**
371  * The smallest used iso value.
372  */
373  double m_isomin;
374 
375  /**
376  * The largest used iso value.
377  */
378  double m_isomax;
379 
380  /**
381  * Factor by which the output opacity is scaled
382  * to allow for easier specification of very small
383  * opacity values.
384  */
386 
387  /**
388  * Sores a histogram. This is used for property-handling only
389  * and does not change the transfer function at all.
390  */
391  std::vector< double > m_histogram;
392 
393  friend std::ostream& operator << ( std::ostream& out, const WTransferFunction& tf );
394 };
395 
396 /**
397  * Default output operator. Currently stores values the same way as it is done in the properties.
398  * This code should only be used for debugging and you should not rely on the interface.
399  *
400  * \param tf The transfer function to output
401  * \param out The stream to which we write
402  *
403  * \returns reference to out
404  */
405 std::ostream& operator << ( std::ostream& out, const WTransferFunction& tf );
406 
407 #endif // WTRANSFERFUNCTION_H
A class that stores a 1D transfer function which consists of a linear interpolation of alpha and colo...
bool operator==(const WTransferFunction &rhs) const
Check equivalence of two transfer functions.
double m_isomax
The largest used iso value.
double getColorIsovalue(size_t i) const
The isovalue of the color at a given index.
WTransferFunction(const WTransferFunction &rhs)
Deep copy constructor.
WTransferFunction()
Default constructor of a meaningless transfer function.
WTransferFunction & operator=(const WTransferFunction &rhs)
Deep copy operator.
const std::vector< double > & getHistogram() const
Returns the histogram.
bool operator!=(const WTransferFunction &rhs) const
Check equivalence of two transfer functions.
void setHistogram(std::vector< double > &data)
Set the histogram going along with the transfer function.
std::vector< ColorEntry > m_colors
Sorted list of colors.
double getOpacityScale() const
Get the scaling factor for the opacity.
void setOpacityScale(double factor)
Set the scaling factor for the opacity.
friend std::ostream & operator<<(std::ostream &out, const WTransferFunction &tf)
Default output operator.
std::vector< AlphaEntry > m_alphas
Sorted list of alpha values.
std::vector< double > m_histogram
Sores a histogram.
void removeHistogram()
Clears the histogram data so the gui won't show any.
double m_isomin
The smallest used iso value.
size_t numAlphas() const
Get the number of alphas.
double getAlphaIsovalue(size_t i) const
Get the isovalue at a given index in the alpha values.
size_t numColors() const
Get the number of colors.
void sample1DTransferFunction(unsigned char *array, int width, double min, double max) const
sample/render the transfer function linearly between min and max in an RGBA texture.
~WTransferFunction()
Default destuctor.
double getAlpha(size_t i) const
Get alpha at given index.
void addColor(double iso, const WColor &color)
Insert a new color point.
const WColor & getColor(size_t i) const
Get color at given index.
void addAlpha(double iso, double alpha)
Insert a new alpha point.
static WTransferFunction createFromRGBA(unsigned char const *const rgba, size_t size)
Try to estimate a transfer function from an RGBA image.
double m_opacityScale
Factor by which the output opacity is scaled to allow for easier specification of very small opacity ...
Alpha entries represent linearly interpolated transparency values along the isovalue scale.
AlphaEntry(double iso, double alpha)
default constructor
bool operator==(const AlphaEntry &rhs) const
comparison operator to check for changes
double alpha
holds the current alpha at isovalue Entry::iso
Color entries are linearly interpolated colors along isovalues.
ColorEntry(double iso, WColor color)
default constructor
bool operator==(const ColorEntry &rhs) const
comparison operator to check for changes
WColor color
holds the current color at isovalue Entry::iso
Prototype for a storage element.
bool operator<=(const Entry &rhs) const
comparison by isovalue
Entry(double iso)
Default constructor.
Templatized comparison predicate for internal searching.
double iso
the isovalue to compare to
bool operator()(const T &t)
isovalue-based comparison
LessPred(double iso)
constructs a predicate that compares for less than iso