Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Spline2d.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2015.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Christian Ehrlich $
32 // $Authors: Christian Ehrlich $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_MATH_MISC_SPLINE2D_H
36 #define OPENMS_MATH_MISC_SPLINE2D_H
37 
38 
39 #include <Eigen/Core>
40 #include <unsupported/Eigen/Splines>
41 
42 #include <vector>
43 #include <cassert>
44 #include <map>
45 
46 
47 namespace OpenMS {
51  template<typename ValType = double>
52  class Spline2d
53  {
54  private:
55  typedef Eigen::Matrix< ValType, Eigen::Dynamic, Eigen::Dynamic > MatrixT;
56  typedef Eigen::Matrix< ValType, Eigen::Dynamic, 1 > VectorT;
57 
58  public:
59  typedef ValType value_type;
60 
66  Spline2d(unsigned degree, const std::vector<ValType>& x,
67  const std::vector<ValType>& y)
68  {
69  assert(!x.empty());
70  assert(x.size() == y.size());
71  size_t num_data_points = x.size();
72  MatrixT raw_values (2, num_data_points);
73  for (unsigned i=0; i<num_data_points; ++i)
74  {
75  raw_values(0, i) = x.at(i);
76  raw_values(1, i) = y.at(i);
77  }
78  initialize(degree, raw_values);
79  }
80 
82  Spline2d(unsigned degree, const std::map<ValType, ValType>& m)
83  {
84  size_t num_data_points = m.size();
85  MatrixT raw_values (2, num_data_points);
86  typename std::map<ValType, ValType>::const_iterator map_it;
87  unsigned colIdx = 0;
88  for (map_it = m.begin(); map_it != m.end(); ++map_it, ++colIdx)
89  {
90  raw_values(0, colIdx) = map_it->first;
91  raw_values(1, colIdx) = map_it->second;
92  }
93  initialize(degree, raw_values);
94  }
95 
97  Spline2d(unsigned degree, const MatrixT& raw_values)
98  {
99  initialize(degree, raw_values);
100  }
101 
103  ValType eval(ValType x) const
104  {
105  return spline_( getNormIndex(x) )(1);
106  }
108  ValType derivatives(ValType x, unsigned order) const
109  {
110  return spline_.derivatives( getNormIndex(x), order )(3);
111  }
112 
113  private:
114 
115  Eigen::Spline<ValType,2> spline_;
116  ValType minXCoeff_;
117  ValType maxXCoeff_;
118 
120  ValType
121  getNormIndex(ValType x) const
122  {
123  return (x - minXCoeff_) / (maxXCoeff_ - minXCoeff_);
124  }
125 
126  void
127  initialize(unsigned degree, const MatrixT& raw_values)
128  {
129  minXCoeff_ = raw_values.row(0).minCoeff();
130  maxXCoeff_ = raw_values.row(0).maxCoeff();
131 
132  // setup spline
133  size_t num_data_points = raw_values.row(0).size();
134  assert(num_data_points > degree);
135  VectorT uvalues (num_data_points);
136  for (Eigen::DenseIndex j=0; j<(Eigen::DenseIndex) num_data_points; ++j)
137  {
138  uvalues(j) = getNormIndex(raw_values(0, j));
139  }
140  spline_ = Eigen::SplineFitting< Eigen::Spline<ValType,2> >::Interpolate(raw_values, degree, uvalues.transpose());
141  }
142  };
143 }//namespace
144 
145 #endif /* OPENMS_MATH_MISC_SPLINE2D_H */
Eigen::Matrix< ValType, Eigen::Dynamic, 1 > VectorT
Definition: Spline2d.h:56
ValType maxXCoeff_
Definition: Spline2d.h:117
Wrapper for Spline interpolation.
Definition: Spline2d.h:52
ValType value_type
Definition: Spline2d.h:59
Eigen::Matrix< ValType, Eigen::Dynamic, Eigen::Dynamic > MatrixT
Definition: Spline2d.h:55
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
ValType derivatives(ValType x, unsigned order) const
Definition: Spline2d.h:108
ValType eval(ValType x) const
Definition: Spline2d.h:103
Eigen::Spline< ValType, 2 > spline_
Definition: Spline2d.h:115
void initialize(unsigned degree, const MatrixT &raw_values)
Definition: Spline2d.h:127
Spline2d(unsigned degree, const std::map< ValType, ValType > &m)
Definition: Spline2d.h:82
Spline2d(unsigned degree, const MatrixT &raw_values)
Definition: Spline2d.h:97
ValType minXCoeff_
Definition: Spline2d.h:116
Spline2d(unsigned degree, const std::vector< ValType > &x, const std::vector< ValType > &y)
Definition: Spline2d.h:66
ValType getNormIndex(ValType x) const
Definition: Spline2d.h:121

OpenMS / TOPP release 2.0.0 Documentation generated on Thu Aug 20 2015 01:44:29 using doxygen 1.8.9.1