00001
00002
00003
00004
00006 #ifndef __BASEOP_RESCALE_HPP
00007 #define __BASEOP_RESCALE_HPP
00008
00009 #include "gridopDllApi.h"
00010 #include <grid/Representation.hpp>
00011
00012 #include <vector/LinearIpol.hpp>
00013 #include <vector/Interpolate.hpp>
00014
00015 namespace Fiber
00016 {
00017
00018 template <int Dims, typename Type>
00019 struct ResampleArray
00020 {
00021
00022 static RefPtr<MemBase> compute(const MultiIndex<Dims>&DestDims, const RefPtr<MemBase>&DataSrc)
00023 {
00024 RefPtr<ReferencingMemArray<Dims, Type> > Src = DataSrc;
00025 if (!Src)
00026 return NullPtr();
00027
00028 MultiArray<Dims,Type> & SourceData = *Src;
00029 MultiIndex<Dims> DimsSrc = SourceData.Size();
00030
00031 RefPtr<MemArray<Dims, Type> > Dst = new MemArray<Dims, Type>( DestDims );
00032 MultiArray<Dims,Type> & DestData = *Dst;
00033
00034
00035 FixedArray<double, Dims> IndexScale;
00036 for(int i=0; i<Dims; i++)
00037 {
00038 IndexScale[i] = double(DimsSrc[i])/DestDims[i];
00039 }
00040
00041 MultiIndex<Dims> MI;
00042 FixedArray<double,Dims> EvalPoint;
00043 Interpolate<Dims, Type, LinearIpol<Type> > Ipol(SourceData, EvalPoint);
00044 do
00045 {
00046 for(int i=0; i<Dims; i++)
00047 EvalPoint[i] = MI[i]*IndexScale[i];
00048
00049 DestData[ MI ] = Ipol.eval();
00050 }
00051 while( MI.inc(DestDims) );
00052
00053 return Dst;
00054 }
00055
00056 static RefPtr<MemBase> rescale(double scale, const RefPtr<MemBase>&DataSrc, const MemBase::Creator_t&Crec)
00057 {
00058 RefPtr<ReferencingMemArray<Dims, Type> > Src = DataSrc;
00059 if (!Src)
00060 return NullPtr();
00061
00062 MultiArray<Dims,Type> & SourceData = *Src;
00063 MultiIndex<Dims> DimsSrc = SourceData.Size();
00064
00065 MultiIndex<Dims> DestDims;
00066 for(int i=0; i<Dims; i++)
00067 {
00068 DestDims[i] = index_t( scale*DimsSrc[i] );
00069 }
00070
00071 RefPtr<MemArray<Dims, Type> > Dst = new MemArray<Dims, Type>( DestDims, Crec );
00072 MultiArray<Dims,Type> & DestData = *Dst;
00073
00074 MultiIndex<Dims> MI;
00075 FixedArray<double,Dims> EvalPoint;
00076 Interpolate<Dims, Type, LinearIpol<Type> > Ipol(SourceData, EvalPoint);
00077 do
00078 {
00079 for(int i=0; i<Dims; i++)
00080 EvalPoint[i] = MI[i]/scale;
00081
00082 DestData[ MI ] = Ipol.eval();
00083 }
00084 while( MI.inc(DestDims) );
00085
00086 return Dst;
00087 }
00088 };
00089
00090
00095 extern gridop_API RefPtr<MemBase> rescale(double scale, const RefPtr<MemBase>&Data, const MemBase::Creator_t&Crec = NullPtr() );
00096
00097 }
00098
00099 #endif