Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
interpolation.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/interpolation/interpolation.hh
11 /// @brief Interpolation functions
12 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
13 
14 
15 #ifndef INCLUDED_numeric_interpolation_interpolation_hh
16 #define INCLUDED_numeric_interpolation_interpolation_hh
17 
18 
19 // Package headers
21 #include <numeric/NumericTraits.hh>
22 
23 // C++ headers
24 #include <utility/assert.hh>
25 #include <cmath>
26 
27 
28 namespace numeric {
29 namespace interpolation {
30 
31 
32 /// @brief Linearly interpolated value: f( x )
33 /// @note Extrapolates if x not in [ x1, x2 ]
34 template< typename X, typename F >
35 inline
36 F
38  X const & x,
39  X const & x1,
40  X const & x2,
41  F const & f1,
42  F const & f2
43 )
44 {
45  assert( x2 - x1 != X( 0 ) );
46  return f1 + ( ( x - x1 ) / ( x2 - x1 ) ) * ( f2 - f1 ); // f( x )
47 }
48 
49 
50 /// @brief Linearly interpolated value: f( x )
51 /// @note Extrapolates if a not in [ 0, 1 ]
52 template< typename X, typename F >
53 inline
54 F
56  X const & a, // Alpha fraction: ( x - x1 ) / ( x2 - x1 )
57  F const & f1,
58  F const & f2
59 )
60 {
61  return f1 + ( a * ( f2 - f1 ) ); // f( x )
62 }
63 
64 
65 /// @brief Linearly interpolated delta value: f( x ) - f1
66 /// @note Extrapolates if a not in [ 0, 1 ]
67 template< typename X, typename F >
68 inline
69 F
71  X const & a, // Alpha fraction: ( x - x1 ) / ( x2 - x1 )
72  F const & f1,
73  F const & f2
74 )
75 {
76  return a * ( f2 - f1 ); // f( x )
77 }
78 
79 
80 /// @brief Bilinearly interpolated value: f( x, y )
81 template< typename X, typename Y, typename F >
82 inline
83 F
85  X const & x,
86  X const & x1,
87  X const & x2,
88  Y const & y,
89  Y const & y1,
90  Y const & y2,
91  F const & f11, // f( x1, y1 )
92  F const & f12, // f( x1, y2 )
93  F const & f21, // f( x2, y1 )
94  F const & f22 // f( x2, y2 )
95 )
96 {
97  assert( x2 - x1 != X( 0 ) );
98  assert( y2 - y1 != Y( 0 ) );
99  X const ax( ( x - x2 ) / ( x2 - x1 ) ); // alpha_x fraction
100  Y const ay( ( y - y2 ) / ( y2 - y1 ) ); // alpha_y fraction
101  X const bx( X( 1.0 ) - ax ); // beta_x == 1 - alpha_x
102  Y const by( Y( 1.0 ) - ay ); // beta_y == 1 - alpha_y
103  return
104  ( bx * by * f11 ) +
105  ( bx * ay * f12 ) +
106  ( ax * by * f21 ) +
107  ( ax * ay * f22 );
108 }
109 
110 
111 /// @brief Bilinearly interpolated value
112 template< typename X, typename Y, typename F >
113 inline
114 F
116  X const & ax, // alpha_x fraction: ( x - x1 ) / ( x2 - x1 )
117  Y const & ay, // alpha_y fraction: ( y - y1 ) / ( y2 - y1 )
118  F const & f11, // f( x1, y1 )
119  F const & f12, // f( x1, y2 )
120  F const & f21, // f( x2, y1 )
121  F const & f22 // f( x2, y2 )
122 )
123 {
124  X const bx( X( 1.0 ) - ax ); // beta_x == 1 - alpha_x
125  Y const by( Y( 1.0 ) - ay ); // beta_y == 1 - alpha_y
126  return
127  ( bx * by * f11 ) +
128  ( bx * ay * f12 ) +
129  ( ax * by * f21 ) +
130  ( ax * ay * f22 );
131 }
132 
133 
134 /// @brief Bilinearly interpolated value
135 template< typename X, typename Y, typename F >
136 inline
137 F
139  X const & ax, // alpha_x fraction: ( x - x1 ) / ( x2 - x1 )
140  Y const & ay, // alpha_y fraction: ( y - y1 ) / ( y2 - y1 )
141  X const & bx, // beta_x == 1 - alpha_x
142  Y const & by, // beta_y == 1 - alpha_y
143  F const & f11, // f( x1, y1 )
144  F const & f12, // f( x1, y2 )
145  F const & f21, // f( x2, y1 )
146  F const & f22 // f( x2, y2 )
147 )
148 {
149  assert( eq_tol( bx, X( 1.0 ) - ax, NumericTraits< X >::tolerance() * 1000, NumericTraits< X >::tolerance() * 1000 ) );
150  assert( eq_tol( by, Y( 1.0 ) - ay, NumericTraits< Y >::tolerance() * 1000, NumericTraits< Y >::tolerance() * 1000 ) );
151  return
152  ( bx * by * f11 ) +
153  ( bx * ay * f12 ) +
154  ( ax * by * f21 ) +
155  ( ax * ay * f22 );
156 }
157 
158 
159 } // namespace interpolation
160 } // namespace numeric
161 
162 
163 #endif // INCLUDED_numeric_interpolation_interpolation_HH
Numeric type traits.
F interpolated_delta(X const &a, F const &f1, F const &f2)
Linearly interpolated delta value: f( x ) - f1.
std::string X(int const w)
Blank string.
Definition: format.cc:263
bool eq_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Equal within specified relative and absolute tolerances?
NumericTraits: Numeric type traits.
def x
F interpolated(X const &x, X const &x1, X const &x2, F const &f1, F const &f2)
Linearly interpolated value: f( x )
F bilinearly_interpolated(X const &x, X const &x1, X const &x2, Y const &y, Y const &y1, Y const &y2, F const &f11, F const &f12, F const &f21, F const &f22)
Bilinearly interpolated value: f( x, y )
std::string F(int const w, int const d, float const &t)
Fixed Point Format: float.
Definition: format.cc:387
Numeric functions.
def y