A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
norm_linear.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_COLOR_NORM_LINEAR_H_
2 #define GENESIS_UTILS_TOOLS_COLOR_NORM_LINEAR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
39 
40 #include <cassert>
41 #include <cmath>
42 #include <limits>
43 #include <map>
44 #include <stdexcept>
45 #include <string>
46 #include <vector>
47 
48 namespace genesis {
49 namespace utils {
50 
51 // =================================================================================================
52 // Color Normalization
53 // =================================================================================================
54 
60  : public ColorNormalization
61 {
62 public:
63 
64  // -------------------------------------------------------------------------
65  // Constructors and Rule of Five
66  // -------------------------------------------------------------------------
67 
71  ColorNormalizationLinear() = default;
72 
76  ColorNormalizationLinear( double min, double max )
77  : min_value_( min )
78  , max_value_( max )
79  {
81  }
82 
86  ColorNormalizationLinear( std::vector<double> const& values )
87  {
88  autoscale( values.begin(), values.end() );
89  }
90 
94  template <class ForwardIterator>
95  ColorNormalizationLinear( ForwardIterator first, ForwardIterator last )
96  {
97  autoscale( first, last );
98  }
99 
100  virtual ~ColorNormalizationLinear() = default;
101 
104 
107 
108  // -------------------------------------------------------------------------
109  // Accessors
110  // -------------------------------------------------------------------------
111 
115  double min_value() const
116  {
117  return min_value_;
118  }
119 
123  double max_value() const
124  {
125  return max_value_;
126  }
127 
128  // -------------------------------------------------------------------------
129  // Modificators
130  // -------------------------------------------------------------------------
131 
135  ColorNormalizationLinear& scale( double min, double max )
136  {
137  min_value_ = min;
138  max_value_ = max;
139  update_hook_( min, max );
140  return *this;
141  }
142 
146  ColorNormalizationLinear& autoscale( std::vector<double> const& values )
147  {
148  return autoscale( values.begin(), values.end() );
149  }
150 
154  ColorNormalizationLinear& autoscale_min( std::vector<double> const& values )
155  {
156  return autoscale_min( values.begin(), values.end() );
157  }
158 
162  ColorNormalizationLinear& autoscale_max( std::vector<double> const& values )
163  {
164  return autoscale_max( values.begin(), values.end() );
165  }
166 
178  template <class ForwardIterator>
179  ColorNormalizationLinear& autoscale( ForwardIterator first, ForwardIterator last )
180  {
181  // New values, so that we first do not override the current ones.
182  auto min = std::numeric_limits<double>::max();
183  auto max = std::numeric_limits<double>::lowest();
184 
185  size_t cnt = 0;
186 
187  while( first != last ) {
188  if( ! std::isfinite( *first ) || *first == mask_value() ) {
189  ++first;
190  continue;
191  }
192  if( *first < min ) {
193  min = *first;
194  }
195  if( *first > max ) {
196  max = *first;
197  }
198 
199  ++cnt;
200  ++first;
201  }
202 
203  // Only update if we found values.
204  if( cnt == 0 ) {
205  return *this;
206  }
207 
208  // Set the values simply to what we found.
209  min_value_ = min;
210  max_value_ = max;
211  update_hook_( min, max );
212 
213  return *this;
214  }
215 
219  template <class ForwardIterator>
220  ColorNormalizationLinear& autoscale_min( ForwardIterator first, ForwardIterator last )
221  {
222  auto const max = max_value_;
223  autoscale( first, last );
224  max_value_ = max;
225  update_hook_( min_value_, max_value_ );
226  return *this;
227  }
228 
232  template <class ForwardIterator>
233  ColorNormalizationLinear& autoscale_max( ForwardIterator first, ForwardIterator last )
234  {
235  auto const min = min_value_;
236  autoscale( first, last );
237  min_value_ = min;
238  update_hook_( min_value_, max_value_ );
239  return *this;
240  }
241 
246  {
247  min_value_ = value;
248  update_hook_( min_value_, max_value_ );
249  return *this;
250  }
251 
256  {
257  max_value_ = value;
258  update_hook_( min_value_, max_value_ );
259  return *this;
260  }
261 
262  // -------------------------------------------------------------------------
263  // (Pure) Virtual Functions
264  // -------------------------------------------------------------------------
265 
269  virtual bool is_valid_() const override
270  {
271  return min_value_ < max_value_;
272  }
273 
274 protected:
275 
279  virtual void is_valid_or_throw_() const
280  {
281  if( min_value_ >= max_value_ ) {
282  throw std::runtime_error( "Invalid Color Normalization with min >= max." );
283  }
284  }
285 
292  virtual double normalize_( double value ) const override
293  {
294  // Make sure that the norm is set up correctly.
296 
297  // Extreme cases.
298  if( value < min_value_ ) {
299  return -1.0;
300  }
301  if( value > max_value_ ) {
302  return 2.0;
303  }
304  assert( min_value_ <= value && value <= max_value_ );
305 
306  // Bring value into the range [ 0.0, 1.0 ].
307  auto const pos = ( value - min_value_ ) / ( max_value_ - min_value_ );
308  return pos;
309  }
310 
315  virtual void update_hook_( double min, double max )
316  {
317  // Nothing to do for this class.
318  (void) min;
319  (void) max;
320  }
321 
322  // -------------------------------------------------------------------------
323  // Data Members
324  // -------------------------------------------------------------------------
325 
326 private:
327 
328  double min_value_ = 0.0;
329  double max_value_ = 1.0;
330 
331 };
332 
333 } // namespace utils
334 } // namespace genesis
335 
336 #endif // include guard
virtual double normalize_(double value) const override
Normalization function.
ColorNormalizationLinear & max_value(double value)
ColorNormalizationLinear & min_value(double value)
Default Color normalization, using a sequential linear scaling in the range [ min, max ].
Definition: norm_linear.hpp:59
ColorNormalizationLinear & scale(double min, double max)
double max_value() const
Minimum value, that is, where to end the color scale.
ColorNormalizationLinear & autoscale(std::vector< double > const &values)
virtual void is_valid_or_throw_() const
Throw if the ranges are incorrect.
ColorNormalizationLinear(double min, double max)
Constructor that sets min() and max() to the provided values.
Definition: norm_linear.hpp:76
Header of Color class.
virtual bool is_valid_() const override
Return whether the ranges are correct.
ColorNormalizationLinear & autoscale(ForwardIterator first, ForwardIterator last)
Set the min and max of the Palette so that they reflect the min and max valid values that are found i...
ColorNormalizationLinear & autoscale_min(std::vector< double > const &values)
ColorNormalizationLinear(ForwardIterator first, ForwardIterator last)
Constructor that sets min() and max() to the min and max of the provided range.
Definition: norm_linear.hpp:95
ColorNormalizationLinear & autoscale_min(ForwardIterator first, ForwardIterator last)
Same as autoscale(), but only updates the min_value().
Provides some commonly used string utility functions.
virtual void update_hook_(double min, double max)
Called whenever the min and max are set automatically. Gives derived classes a chance to update their...
Base class for color normalization.
double min_value() const
Minimum value, that is, where to begin the color scale.
ColorNormalizationLinear(std::vector< double > const &values)
Constructor that sets min() and max() to the min and max of the provided values.
Definition: norm_linear.hpp:86
ColorNormalizationLinear & operator=(ColorNormalizationLinear const &)=default
ColorNormalizationLinear()=default
Constructor that sets min == 0.0 and max == 1.0.
double mask_value() const
Mask value that identifies invalid values.
ColorNormalizationLinear & autoscale_max(ForwardIterator first, ForwardIterator last)
Same as autoscale(), but only updates the max_value().
ColorNormalizationLinear & autoscale_max(std::vector< double > const &values)