21 #ifndef _MIRTKBSPLINEBIASFIELD_H 23 #define _MIRTKBSPLINEBIASFIELD_H 26 #include "mirtk/BiasField.h" 28 #define BIASLOOKUPTABLESIZE 1000 32 class BSplineBiasField :
public BiasField
34 mirtkObjectMacro(BSplineBiasField);
39 static double B0(
double);
42 static double B1(
double);
45 static double B2(
double);
48 static double B3(
double);
51 static double B0_I(
double);
54 static double B1_I(
double);
57 static double B2_I(
double);
60 static double B3_I(
double);
63 static double B0_II(
double);
66 static double B1_II(
double);
69 static double B2_II(
double);
72 static double B3_II(
double);
75 virtual void Subdivide2D();
78 virtual void Subdivide3D();
81 static double InitialAntiCausalCoefficient(
double *,
int,
double z);
84 static double InitialCausalCoefficient(
double *,
int,
double,
double);
87 static void ConvertToInterpolationCoefficients(
double* c,
int DataLength,
double* z,
int NbPoles,
double Tolerance);
91 void ComputeCoefficients(
double* dbias, RealImage& coeffs);
94 static double LookupTable[BIASLOOKUPTABLESIZE][4];
98 static double LookupTable_I[BIASLOOKUPTABLESIZE][4];
102 static double LookupTable_II[BIASLOOKUPTABLESIZE][4];
107 static double B (
int,
double);
110 static double N(
int i,
double u,
int L);
113 static double B_I(
int,
double);
116 static double B_II(
int,
double);
122 BSplineBiasField(
const GreyImage &image,
double dx,
double dy,
double dz);
125 BSplineBiasField(
const GreyImage &image,
int x,
int y,
int z,
bool bounding_box =
false,
int padding = -1);
128 BSplineBiasField(
const class BSplineBiasField &);
131 virtual ~BSplineBiasField();
134 virtual int LUTSize()
const;
140 virtual double Approximate(
double *,
double *,
double *,
double *,
int);
143 virtual void WeightedLeastSquares(
double *x1,
double *y1,
double *z1,
double *bias,
double *weights,
int no);
149 virtual void Interpolate(
double* dbias);
152 virtual void Subdivide();
155 virtual double FFD1(
double,
double,
double)
const;
158 virtual double FFD2(
double,
double,
double)
const;
161 virtual double Bias(
double,
double,
double);
164 virtual double Bias2(
double,
double,
double);
167 virtual void Read (
char *);
170 virtual void Write(
char *);
173 virtual void Print();
176 virtual int Ind(
int,
int,
int);
179 inline int BSplineBiasField::Ind(
int a,
int b,
int c)
184 if(a>_x-1)
return -1;
185 if(b>_y-1)
return -1;
186 if(c>_z-1)
return -1;
188 return a+b*_x+c*_y*_x;
191 inline double BSplineBiasField::B(
int i,
double t)
195 return (1-t)*(1-t)*(1-t)/6.0;
197 return (3*t*t*t - 6*t*t + 4)/6.0;
199 return (-3*t*t*t + 3*t*t + 3*t + 1)/6.0;
352 inline double BSplineBiasField::B_I(
int i,
double t)
356 return -(1-t)*(1-t)/2.0;
358 return (9*t*t - 12*t)/6.0;
360 return (-9*t*t + 6*t + 3)/6.0;
367 inline double BSplineBiasField::B_II(
int i,
double t)
382 inline double BSplineBiasField::B0(
double t)
384 return (1-t)*(1-t)*(1-t)/6.0;
387 inline double BSplineBiasField::B1(
double t)
389 return (3*t*t*t - 6*t*t + 4)/6.0;
392 inline double BSplineBiasField::B2(
double t)
394 return (-3*t*t*t + 3*t*t + 3*t + 1)/6.0;
397 inline double BSplineBiasField::B3(
double t)
402 inline double BSplineBiasField::B0_I(
double t)
404 return -(1-t)*(1-t)/2.0;
407 inline double BSplineBiasField::B1_I(
double t)
409 return (9*t*t - 12*t)/6.0;
412 inline double BSplineBiasField::B2_I(
double t)
414 return (-9*t*t + 6*t + 3)/6.0;
417 inline double BSplineBiasField::B3_I(
double t)
422 inline double BSplineBiasField::B0_II(
double t)
427 inline double BSplineBiasField::B1_II(
double t)
432 inline double BSplineBiasField::B2_II(
double t)
437 inline double BSplineBiasField::B3_II(
double t)
442 inline int BSplineBiasField::LUTSize()
const 444 return BIASLOOKUPTABLESIZE;
447 inline double BSplineBiasField::Bias(
double x,
double y,
double z)
456 this->WorldToLattice(u, v, w);
466 for (k = 0; k < 4; k++){
467 for (j = 0; j < 4; j++){
468 for (i = 0; i < 4; i++){
469 if(((i+l-1)>=0)&&((i+l-1)<_x)&&((j+m-1)>=0)&&((j+m-1)<_y)&&((k+n-1)>=0)&&((k+n-1)<_z))
470 value += N(i+l-1,u,_x) * N(j+m-1,v,_y) * N(k+n-1,w,_z)*_data[k+n-1][j+m-1][i+l-1];
478 inline double BSplineBiasField::Bias2(
double x,
double y,
double z)
487 this->WorldToLattice(u, v, w);
490 return this->FFD2(u, v, w);
int Read(const char *name, UniquePtr< double[]> &data, int *dtype=nullptr, ImageAttributes *attr=nullptr, void *=nullptr, const char *scalars_name=nullptr, bool cell_data=false)
Read data sequence from any supported input file type.
std::ostream & Print(std::ostream &os, T value)
Print single argument to output stream.