00001 #ifndef GRADIENT_H_
00002 #define GRADIENT_H_
00003
00004 #include <string>
00005 #include "Vector.h"
00006 #include "Data.h"
00007
00013 class Gradient {
00014 public:
00015 Gradient(Data* input, Vector<> boundaryValues = Vector<>(0,0,0));
00016 ~Gradient();
00017
00018
00019 unsigned short xdim() const { return mX; }
00020 unsigned short ydim() const { return mY; }
00021 unsigned short zdim() const { return mZ; }
00022
00023 Vector<> getData(unsigned int aX, unsigned int aY, unsigned int aZ) const{
00024 assert(aX < mX);
00025 assert(aY < mY);
00026 assert(aZ < mZ);
00027
00028 const unsigned int Ystride = mX;
00029 const unsigned int Zstride = Ystride * mY;
00030
00031 return mData[aZ * Zstride + aY * Ystride + aX];
00032 }
00033
00034 Vector<> getData_RangeCheck(int aX, int aY, int aZ){
00035
00036
00037 if ((aX<0) || (aX>=mX)) return Vector<>(0,0,0);
00038 if ((aY<0) || (aY>=mY)) return Vector<>(0,0,0);
00039 if ((aZ<0) || (aZ>=mZ)) return Vector<>(0,0,0);
00040
00041
00042 const unsigned int Ystride = (unsigned int) mX;
00043 const unsigned int Zstride = (unsigned int) Ystride * mY;
00044
00045 return mData[aZ * Zstride + aY * Ystride + aX];
00046 }
00047
00048 Vector<> getDataTrilinearInterpolated(float aX, float aY, float aZ){
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 int lowerX = (int) floor(aX);
00066 int lowerY = (int) floor(aY);
00067 int lowerZ = (int) floor(aZ);
00068 int upperX = lowerX+1;
00069 int upperY = lowerY+1;
00070 int upperZ = lowerZ+1;
00071
00072
00073 Vector<> A = getData_RangeCheck(lowerX, upperY, lowerZ);
00074 Vector<> B = getData_RangeCheck(upperX, upperY, lowerZ);
00075 Vector<> C = getData_RangeCheck(upperX, lowerY, lowerZ);
00076 Vector<> D = getData_RangeCheck(lowerX, lowerY, lowerZ);
00077
00078 Vector<> E = getData_RangeCheck(lowerX, upperY, upperZ);
00079 Vector<> F = getData_RangeCheck(upperX, upperY, upperZ);
00080 Vector<> G = getData_RangeCheck(upperX, lowerY, upperZ);
00081 Vector<> H = getData_RangeCheck(lowerX, lowerY, upperZ);
00082
00083 float floatX = aX - lowerX;
00084 float floatY = aY - lowerY;
00085 float floatZ = aZ - lowerZ;
00086
00087
00088
00089
00090
00091
00092
00093 Vector<> AE = E * floatZ + A * (1-floatZ);
00094 Vector<> BF = F * floatZ + B * (1-floatZ);
00095 Vector<> CG = G * floatZ + C * (1-floatZ);
00096
00097 Vector<> DH = H * floatZ + D * (1-floatZ);
00098
00099 Vector<> AEDH = AE * floatY + DH * (1-floatY);
00100 Vector<> BFCG = BF * floatY + CG * (1-floatY);
00101
00102 return (BFCG * floatX + AEDH * (1-floatX)).normalized();
00103 }
00104
00105
00106
00107
00108 private:
00109 Vector<>* mData;
00110
00111 Vector<> mBoundaryValues;
00112
00113 unsigned short mX, mY, mZ;
00114
00115 };
00116
00117 #endif