patterncppMinor
Linear Interpolation C++
Viewed 0 times
linearinterpolationstackoverflow
Problem
I have to write a collection of methods for performing linear, bilinear and trilinear interpolation. I have also to write some tests to show that interpolation is exact for polynomials (which should be the case using these interpolation methods).
I ended up writing the following classes as core for my interpolation
Here one-second compile/run github repo (gtest required).
```
#ifndef LINEARINTERPOLATE_H
#define LINEARINTERPOLATE_H
#include
namespace FL{
typedef unsigned int uint;
template
struct point{
T coords [DIM] ;
T val;
inline const T coord(const int c) const{
assert(c >= 0 && c
class LinearInterpolator{
public:
/*
y
^ p1
| /
| /
| /
| p
| /
| p0
|
|
o-------------------------> x
*/
/* P: point the lie between a and b
* a,b boundary of the 1D cuboid, a& Linear ( point& p , const point&a, const point&b, int c=0 ){
T x_d = (p.coord(c)-a.coord(c)) * (1/(b.coord(c) - a.coord(c)));
p.val = Linear(a.val,b.val,x_d);
return p;
}
/* P: point the lie inside the cuboid defined by the first two values of v
v[0] & Linear ( point& p , const point v , int c=0 ){
T x_d = (p.coord(c)-v[0].coord(c)) * (1/(v[1].coord(c) - v[0].coord(c)));
p.val = Linear(v[0].val,v[1].val,x_d);
return p;
}
/*----------------BILINEAR------------------------
y
^
| p2.......p3
| . .
| . .
| . .
| . .
| p0.......p1
|
|
o-------------------------> x
p point that lie in the cuboid defined by the 4 values array v
-------------------------------------------------*/
point& Bilinear ( point& p , const point*v ){
T x_d = (p.coord(0)-v[0].coord(0)) * (1/(v[1].coord(0) - v[0].coord(0)));
T y_d
I ended up writing the following classes as core for my interpolation
Here one-second compile/run github repo (gtest required).
```
#ifndef LINEARINTERPOLATE_H
#define LINEARINTERPOLATE_H
#include
namespace FL{
typedef unsigned int uint;
template
struct point{
T coords [DIM] ;
T val;
inline const T coord(const int c) const{
assert(c >= 0 && c
class LinearInterpolator{
public:
/*
y
^ p1
| /
| /
| /
| p
| /
| p0
|
|
o-------------------------> x
*/
/* P: point the lie between a and b
* a,b boundary of the 1D cuboid, a& Linear ( point& p , const point&a, const point&b, int c=0 ){
T x_d = (p.coord(c)-a.coord(c)) * (1/(b.coord(c) - a.coord(c)));
p.val = Linear(a.val,b.val,x_d);
return p;
}
/* P: point the lie inside the cuboid defined by the first two values of v
v[0] & Linear ( point& p , const point v , int c=0 ){
T x_d = (p.coord(c)-v[0].coord(c)) * (1/(v[1].coord(c) - v[0].coord(c)));
p.val = Linear(v[0].val,v[1].val,x_d);
return p;
}
/*----------------BILINEAR------------------------
y
^
| p2.......p3
| . .
| . .
| . .
| . .
| p0.......p1
|
|
o-------------------------> x
p point that lie in the cuboid defined by the 4 values array v
-------------------------------------------------*/
point& Bilinear ( point& p , const point*v ){
T x_d = (p.coord(0)-v[0].coord(0)) * (1/(v[1].coord(0) - v[0].coord(0)));
T y_d
Solution
I'd prefer to include the C++ header `
I don't see a good reason for creating a reciprocal and multiplying by that, instead of simply dividing. It appears to me to be unnecessary duplication:
becomes:
We might want to make a helper function for these very similar "portion" computations.
We can make
Instead of the
BTW, a "1D cuboid" is normally called a line, and a "2D cuboid" a rectangle.
, to be consistent. Use the C compatibility headers only in code that must compile with a C compiler.
I don't see the need for the final argument c to Linear(), given that it's passed to point::coords(), for which the only valid value is 0. Also there's an inaccurate comment there: a,b boundary of the 1D cuboid, a<=b - but we need a to avoid division by zero.I don't see a good reason for creating a reciprocal and multiplying by that, instead of simply dividing. It appears to me to be unnecessary duplication:
T x_d = (p.coord(c)-a.coord(c)) * (1/(b.coord(c) - a.coord(c)));becomes:
T x_d = (p.coord(c)-a.coord(c)) / (b.coord(c) - a.coord(c));We might want to make a helper function for these very similar "portion" computations.
We can make
point::coord() constexpr, and all the members of LinearInterpolator can be made both constexpr and static.Instead of the
static_cast of 1.0 to T, we might make a simple constant:private:
static constexpr T unity = 1.0;BTW, a "1D cuboid" is normally called a line, and a "2D cuboid" a rectangle.
Code Snippets
T x_d = (p.coord(c)-a.coord(c)) * (1/(b.coord(c) - a.coord(c)));T x_d = (p.coord(c)-a.coord(c)) / (b.coord(c) - a.coord(c));private:
static constexpr T unity = 1.0;Context
StackExchange Code Review Q#120795, answer score: 3
Revisions (0)
No revisions yet.