1 // interp.h 2 // 3 // Copyright (C) 2003, 2004 Jason Bevins 4 // 5 // This library is free software; you can redistribute it and/or modify it 6 // under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation; either version 2.1 of the License, or (at 8 // your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, but WITHOUT 11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 // License (COPYING.txt) for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with this library; if not, write to the Free Software Foundation, 17 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 // 19 // The developer's email is jlbezigvins@gmzigail.com (for great email, take 20 // off every 'zig'.) 21 // 22 module noise.interp; 23 24 /// @addtogroup libnoise 25 /// @{ 26 27 /// Performs cubic interpolation between two values bound between two other 28 /// values. 29 /// 30 /// @param n0 The value before the first value. 31 /// @param n1 The first value. 32 /// @param n2 The second value. 33 /// @param n3 The value after the second value. 34 /// @param a The alpha value. 35 /// 36 /// @returns The interpolated value. 37 /// 38 /// The alpha value should range from 0.0 to 1.0. If the alpha value is 39 /// 0.0, this function returns @a n1. If the alpha value is 1.0, this 40 /// function returns @a n2. 41 double CubicInterp (double n0, double n1, double n2, double n3, 42 double a) 43 { 44 double p = (n3 - n2) - (n0 - n1); 45 double q = (n0 - n1) - p; 46 double r = n2 - n0; 47 double s = n1; 48 return p * a * a * a + q * a * a + r * a + s; 49 } 50 51 /// Performs linear interpolation between two values. 52 /// 53 /// @param n0 The first value. 54 /// @param n1 The second value. 55 /// @param a The alpha value. 56 /// 57 /// @returns The interpolated value. 58 /// 59 /// The alpha value should range from 0.0 to 1.0. If the alpha value is 60 /// 0.0, this function returns @a n0. If the alpha value is 1.0, this 61 /// function returns @a n1. 62 double LinearInterp (double n0, double n1, double a) 63 { 64 return ((1.0 - a) * n0) + (a * n1); 65 } 66 67 /// Maps a value onto a cubic S-curve. 68 /// 69 /// @param a The value to map onto a cubic S-curve. 70 /// 71 /// @returns The mapped value. 72 /// 73 /// @a a should range from 0.0 to 1.0. 74 /// 75 /// The derivitive of a cubic S-curve is zero at @a a = 0.0 and @a a = 76 /// 1.0 77 double SCurve3 (double a) 78 { 79 return (a * a * (3.0 - 2.0 * a)); 80 } 81 82 /// Maps a value onto a quintic S-curve. 83 /// 84 /// @param a The value to map onto a quintic S-curve. 85 /// 86 /// @returns The mapped value. 87 /// 88 /// @a a should range from 0.0 to 1.0. 89 /// 90 /// The first derivitive of a quintic S-curve is zero at @a a = 0.0 and 91 /// @a a = 1.0 92 /// 93 /// The second derivitive of a quintic S-curve is zero at @a a = 0.0 and 94 /// @a a = 1.0 95 double SCurve5 (double a) 96 { 97 double a3 = a * a * a; 98 double a4 = a3 * a; 99 double a5 = a4 * a; 100 return (6.0 * a5) - (15.0 * a4) + (10.0 * a3); 101 } 102 103 // @}