License Public Domain
Lines 54
Keywords
3d (3) interpolation (2) templates (3) volume data (1) volume rendering (2)
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by Stou S.
Hide
Need a quick chart or graph for your blog? Try our reStructured Text renderer. Join Siafoo Now or Learn More

A Linear/Bilinear/Trilinear Interpolation Class Atom Feed 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
# '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{
10
11public:
12 /*
13 Linear interpolation
14 target - the target point, 0.0 - 1.0
15 v - a pointer to an array of size 2 containg the two values
16
17 */
18
19 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 2D
25 target - a 2D point (X,Y)
26 v - an array of size 4 containg values cockwise around the square starting from bottom left
27
28 cost: performs 3 linear interpolations
29 */
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 }
40
41 /*
42 TriLinear interpolation, linear interpolation in 2D
43 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)
47
48 cost: 7 linear interpolations
49 */
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 };
57
58 return Linear(target[2], v_prime);
59 }
60};
61
62#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};
11
12 float targetLin = 0.5;
13
14 cout<<"Linear (f): "<<Interpolator<float>::Linear(targetLin, fVarsLin)<<endl;
15 cout<<"Linear (i): "<<Interpolator<int>::Linear(targetLin, iVarsLin)<<endl;
16
17 // Test Bilinear interpolation
18 float fVarsBilin[4] = {1.0, 2.0, 3.0, 4.0};
19 int iVarsBilin[4] = {100, 200, 300, 400};
20
21 float targetBilin[2] = {0.5, 1.0};
22
23 cout<<"Bilinear (f): "<<Interpolator<float>::Bilinear(targetBilin, fVarsBilin)<<endl;
24 cout<<"Bilinear (i): "<<Interpolator<int>::Bilinear(targetBilin, iVarsBilin)<<endl;
25
26 // Test Trilinear interpolation
27 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};
29
30 float targetTrilin[3] = {0.5, 0.5, 0.5};
31
32 cout<<"Trilinear (f): "<<Interpolator<float>::Trilinear(targetTrilin, fVarsTrilin)<<endl;
33 cout<<"Trilinear (i): "<<Interpolator<int>::Trilinear(targetTrilin, iVarsTrilin)<<endl;
34
35
36 return 0;
37}

Comments

updated over 7 years ago (09 Sep 2009 at 01:37 PM) by kumar
Hi

Modifying the function Linear makes this run for user defined vectors and vectors from external libs like blitz/tvmet.

inline static T Linear(float target, T *v){
return (T) ( target * (v[1]) + ( T(1.0f) - target ) * (v[0]) );
}

Just include the T(1.0f) instead of the 1.0f. Thanks for the code !!! You rock.