Lines 54
##### Keywords
3d (3) templates (3) volume data (1)
##### Permissions
Owner: Stou S.
Viewable by Everyone
Editable by Stou S.
Solve a problem – Filter by language, license, keyword, owner, or search text to find code & info fast.

# A Linear/Bilinear/Trilinear Interpolation Class 0

 In Brief A Linear/Bilinear/Trilinear interpolation class circa January 2005. This is a template based version of some interpolation code I wrote for a course on volume data visualization.... more
 Language C++
# 's
` 1#if _MSC_VER > 1000 2#pragma once 3#endif 4     5#ifndef HPP_INTERPOL 6#define HPP_INTERPOL 7 8template<class T> class Interpolator 9{1011public:12    /*13        Linear interpolation14        target  - the target point, 0.0 - 1.015        v       - a pointer to an array of size 2 containg the two values16    17    */1819    inline static T Linear(float target, T *v)20    {21        return (T)(target*(v[1])+ (T(1.0f) - target)*(v[0]));22    }23    /* 24        BiLinear interpolation, linear interpolation in 2D25        target  - a 2D point (X,Y)26        v       - an array of size 4 containg values cockwise around the square starting from bottom left2728        cost: performs 3 linear interpolations29    */30    inline static T Bilinear(float *target, T *v)31    {32        T v_prime[2] = {33                        Linear(target[1], &(v[0])), 34                        Linear(target[1], &(v[2]))35                       }; 36        37        return Linear(target[0], v_prime);38        39    }4041    /* 42        TriLinear interpolation, linear interpolation in 2D43        target  - a 3D point (X,Y)44        v       - an array of size 8 containg the values of the 8 corners 45                    of a cube defined as two faces: 0-3 face one (front face) 46                                                    4-7 face two (back face)4748        cost: 7 linear interpolations49    */50    51    inline static T Trilinear(float *target, T *v)52    {53        T v_prime[2] = {54                         Bilinear(&(target[0]), &(v[0])),55                         Bilinear(&(target[1]), &(v[4]))    56                       };5758        return Linear(target[2], v_prime);59    }60};6162#endif // HPP_INTERPOL`

A Linear/Bilinear/Trilinear interpolation class circa January 2005. This is a template based version of some interpolation code I wrote for a course on volume data visualization.

Funny story... while templatizing it I found a major bug that had rendered Trilinear interpolations useless, and so now I realize why my Ray-Casted images looked so crappy. All fixed now.

The interpolation formula for two values, and is

Here's a simple driver/test code

# 's
` 1#include "interpolator.hpp" 2#include <iostream> 3 4using namespace std; 5 6int main() 7{ 8    // Test Linear interpolation 9    float fVarsLin[2] = {1.0, 2.0};10    int iVarsLin[2] = {100, 200};1112    float targetLin = 0.5;1314    cout<<"Linear (f): "<<Interpolator<float>::Linear(targetLin, fVarsLin)<<endl;15    cout<<"Linear (i): "<<Interpolator<int>::Linear(targetLin, iVarsLin)<<endl;1617    // Test Bilinear interpolation18    float fVarsBilin[4] = {1.0, 2.0, 3.0, 4.0};19    int iVarsBilin[4] = {100, 200, 300, 400};2021    float targetBilin[2] = {0.5, 1.0};2223    cout<<"Bilinear (f): "<<Interpolator<float>::Bilinear(targetBilin, fVarsBilin)<<endl;24    cout<<"Bilinear (i): "<<Interpolator<int>::Bilinear(targetBilin, iVarsBilin)<<endl;2526    // Test Trilinear interpolation27    float fVarsTrilin[8] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};28    int iVarsTrilin[8] = {100, 200, 300, 400, 500, 600, 700, 800};2930    float targetTrilin[3] = {0.5, 0.5, 0.5};3132    cout<<"Trilinear (f): "<<Interpolator<float>::Trilinear(targetTrilin, fVarsTrilin)<<endl;33    cout<<"Trilinear (i): "<<Interpolator<int>::Trilinear(targetTrilin, iVarsTrilin)<<endl;343536    return 0;37}`