A library for working with phylogenetic and population genetic data.
v0.32.0
norm_linear.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_COLOR_NORM_LINEAR_H_
2 #define GENESIS_UTILS_COLOR_NORM_LINEAR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2023 Lucas Czech
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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
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  explicit 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() override = 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  autoscale_( first, last, true, true );
182  return *this;
183  }
184 
188  template <class ForwardIterator>
189  ColorNormalizationLinear& autoscale_min( ForwardIterator first, ForwardIterator last )
190  {
191  autoscale_( first, last, true, false );
192  return *this;
193  }
194 
198  template <class ForwardIterator>
199  ColorNormalizationLinear& autoscale_max( ForwardIterator first, ForwardIterator last )
200  {
201  autoscale_( first, last, false, true );
202  return *this;
203  }
204 
209  {
210  min_value_ = value;
211  ColorNormalizationLinear::update_hook_( min_value_, max_value_ );
212  return *this;
213  }
214 
219  {
220  max_value_ = value;
221  ColorNormalizationLinear::update_hook_( min_value_, max_value_ );
222  return *this;
223  }
224 
225  // -------------------------------------------------------------------------
226  // Internal Helper Functions
227  // -------------------------------------------------------------------------
228 
229 protected:
230 
231  template <class ForwardIterator>
233  ForwardIterator first, ForwardIterator last,
234  bool set_min, bool set_max
235  ) {
236  // New values, so that we first do not override the current ones.
237  auto min = std::numeric_limits<double>::max();
238  auto max = std::numeric_limits<double>::lowest();
239 
240  size_t cnt = 0;
241  while( first != last ) {
242  if( ! std::isfinite( *first ) || *first == mask_value() ) {
243  ++first;
244  continue;
245  }
246  if( *first < min ) {
247  min = *first;
248  }
249  if( *first > max ) {
250  max = *first;
251  }
252 
253  ++cnt;
254  ++first;
255  }
256 
257  // Only update if we found values.
258  if( cnt == 0 ) {
259  return *this;
260  }
261 
262  // Set the values simply to what we found.
263  if( set_min ) {
264  min_value_ = min;
265  }
266  if( set_max ) {
267  max_value_ = max;
268  }
270 
271  return *this;
272  }
273 
274  // -------------------------------------------------------------------------
275  // (Pure) Virtual Functions
276  // -------------------------------------------------------------------------
277 
278 protected:
279 
283  virtual bool is_valid_() const override
284  {
285  return min_value_ < max_value_;
286  }
287 
291  virtual void is_valid_or_throw_() const
292  {
293  if( min_value_ >= max_value_ ) {
294  throw std::runtime_error( "Invalid Color Normalization with min >= max." );
295  }
296  }
297 
304  virtual double normalize_( double value ) const override
305  {
306  // Make sure that the norm is set up correctly.
308 
309  // Extreme cases.
310  if( value < min_value_ ) {
311  return -1.0;
312  }
313  if( value > max_value_ ) {
314  return 2.0;
315  }
316  assert( min_value_ <= value && value <= max_value_ );
317 
318  // Bring value into the range [ 0.0, 1.0 ].
319  auto const pos = ( value - min_value_ ) / ( max_value_ - min_value_ );
320  return pos;
321  }
322 
327  virtual void update_hook_( double min, double max )
328  {
329  // Nothing to do for this class.
330  (void) min;
331  (void) max;
332  }
333 
334  // -------------------------------------------------------------------------
335  // Data Members
336  // -------------------------------------------------------------------------
337 
338 private:
339 
340  double min_value_ = 0.0;
341  double max_value_ = 1.0;
342 
343 };
344 
345 } // namespace utils
346 } // namespace genesis
347 
348 #endif // include guard
genesis::utils::ColorNormalizationLinear::autoscale_
ColorNormalizationLinear & autoscale_(ForwardIterator first, ForwardIterator last, bool set_min, bool set_max)
Definition: norm_linear.hpp:232
genesis::utils::ColorNormalizationLinear::normalize_
virtual double normalize_(double value) const override
Normalization function.
Definition: norm_linear.hpp:304
genesis::utils::ColorNormalizationLinear::autoscale
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...
Definition: norm_linear.hpp:179
genesis::utils::ColorNormalization
Base class for color normalization.
Definition: normalization.hpp:52
genesis::utils::ColorNormalizationLinear::max_value
ColorNormalizationLinear & max_value(double value)
Minimum value, that is, where to end the color scale.
Definition: norm_linear.hpp:218
map.hpp
genesis::utils::ColorNormalizationLinear::ColorNormalizationLinear
ColorNormalizationLinear()=default
Constructor that sets min == 0.0 and max == 1.0.
genesis::utils::ColorNormalization::mask_value
double mask_value() const
Mask value that identifies invalid values.
Definition: normalization.hpp:83
genesis::utils::ColorNormalizationLinear::~ColorNormalizationLinear
virtual ~ColorNormalizationLinear() override=default
genesis::utils::ColorNormalizationLinear::ColorNormalizationLinear
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
genesis::utils::ColorNormalizationLinear::autoscale_min
ColorNormalizationLinear & autoscale_min(std::vector< double > const &values)
Definition: norm_linear.hpp:154
genesis::utils::ColorNormalizationLinear::is_valid_
virtual bool is_valid_() const override
Return whether the ranges are correct.
Definition: norm_linear.hpp:283
string.hpp
Provides some commonly used string utility functions.
genesis::utils::ColorNormalizationLinear::ColorNormalizationLinear
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
genesis::utils::ColorNormalizationLinear::min_value
double min_value() const
Minimum value, that is, where to begin the color scale.
Definition: norm_linear.hpp:115
genesis::utils::ColorNormalizationLinear
Default Color normalization, using a sequential linear scaling in the range [ min,...
Definition: norm_linear.hpp:59
genesis::utils::ColorNormalizationLinear::is_valid_or_throw_
virtual void is_valid_or_throw_() const
Throw if the ranges are incorrect.
Definition: norm_linear.hpp:291
genesis::utils::ColorNormalizationLinear::ColorNormalizationLinear
ColorNormalizationLinear(double min, double max)
Constructor that sets min() and max() to the provided values.
Definition: norm_linear.hpp:76
genesis::utils::ColorNormalizationLinear::min_value
ColorNormalizationLinear & min_value(double value)
Minimum value, that is, where to begin the color scale.
Definition: norm_linear.hpp:208
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::ColorNormalizationLinear::autoscale_min
ColorNormalizationLinear & autoscale_min(ForwardIterator first, ForwardIterator last)
Same as autoscale(), but only updates the min_value().
Definition: norm_linear.hpp:189
color.hpp
Header of Color class.
genesis::utils::ColorNormalizationLinear::autoscale_max
ColorNormalizationLinear & autoscale_max(std::vector< double > const &values)
Definition: norm_linear.hpp:162
tickmarks.hpp
genesis::utils::ColorNormalizationLinear::update_hook_
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...
Definition: norm_linear.hpp:327
normalization.hpp
genesis::utils::ColorNormalizationLinear::max_value
double max_value() const
Minimum value, that is, where to end the color scale.
Definition: norm_linear.hpp:123
genesis::utils::ColorNormalizationLinear::autoscale
ColorNormalizationLinear & autoscale(std::vector< double > const &values)
Definition: norm_linear.hpp:146
genesis::utils::ColorNormalizationLinear::scale
ColorNormalizationLinear & scale(double min, double max)
Definition: norm_linear.hpp:135
genesis::utils::ColorNormalizationLinear::autoscale_max
ColorNormalizationLinear & autoscale_max(ForwardIterator first, ForwardIterator last)
Same as autoscale(), but only updates the max_value().
Definition: norm_linear.hpp:199
genesis::utils::ColorNormalizationLinear::operator=
ColorNormalizationLinear & operator=(ColorNormalizationLinear const &)=default