00001
00002
00003 #ifndef __SOURCECOMBINELAYOUT_H__
00004 #define __SOURCECOMBINELAYOUT_H__
00005
00006 #include "SourceGenerics.h"
00007 #include "SourceCombine.h"
00008 #include "Promotion.h"
00009 #include "libUtility.h"
00010 #include <math.h>
00011
00012
00013
00014
00015 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00016 class SourceCombineLayoutBaseOf: public SourceCombineOf<DATA, DIMENSIONALITY,
00017 PRECISION, SOURCE>
00018 {
00019
00020 protected:
00021 typedef PointOf<DIMENSIONALITY, PRECISION> type_point;
00022 VectorOf<type_point> m_vecpt;
00023
00024 public:
00025 void AddSourcePosition(type_point& pt)
00026 {
00027 m_vecpt.Append(pt);
00028 }
00029
00030 virtual void SetSourcePosition(int n, type_point& pt)
00031 {
00032 m_vecpt[n] = pt;
00033 }
00034
00035
00036 virtual void AutoArrange()
00037 {
00038 int c = this->m_vecpsrc.C();
00039
00040 type_point ptMax(0.);
00041
00042 for (int n = 0; n < c; n++)
00043 {
00044 this->m_vecpsrc[n]->PrepareForAccess();
00045 ptMax = max(ptMax, this->m_vecpsrc[n]->Size());
00046 }
00047
00048 typedef PointOf<DIMENSIONALITY, int> POINT;
00049
00050 POINT ptEnd;
00051 if (ptMax.Dim(0) > ptMax.Dim(1) * 1.2)
00052 {
00053 ptEnd = POINT(floor(pow(c, 1. / DIMENSIONALITY)));
00054
00055 for (int n = DIMENSIONALITY - 1 && c > ptEnd.CVolume(); n >= 0; n--)
00056 {
00057 ptEnd.Dim(n)++;
00058 }
00059 }
00060 else
00061 {
00062 ptEnd = POINT(ceil(pow(c, 1. / DIMENSIONALITY)));
00063 }
00064
00065 m_vecpt.SetSize(c);
00066
00067 int n = 0;
00068 forpoint (POINT,npt,0,ptEnd)
00069 {
00070 m_vecpt[n]=ptMax*npt;
00071
00072 n++;
00073 if (n>=c)
00074 {
00075 break;
00076 }
00077 }
00078 }
00079
00080 virtual String Describe() const
00081 {
00082 String s;
00083 for (int n=0; n<m_vecpt.C(); n++)
00084 {
00085 s += _LINE + "Offset[" + n + "]" + m_vecpt[n].Describe() + LINE_;
00086 }
00087
00088 return (
00089 _LINE + "SourceCombineOf(" + DIMENSIONALITY + "D)" + LINE_ +
00090 + (_INDENT + s) +
00091 this->DescribeCommon()
00092 );
00093 }
00094
00095 virtual void PrepareForAccessAction() const
00096 {
00097 this->m_ptSize=m_vecpt[0]+this->m_vecpsrc[0]->Size();
00098
00099 for (int n=1; n<m_vecpt.C(); n++)
00100 {
00101 this->m_ptSize=max(this->m_ptSize, m_vecpt[n]+this->m_vecpsrc[n]->Size());
00102 }
00103 }
00104
00105 };
00106
00107
00108
00109 template<class DATA, int DIMENSIONALITY, class PRECISION, class SOURCE>
00110 class SourceCombineLayoutOf: public SourceCombineOf<DATA, DIMENSIONALITY,
00111 PRECISION, SOURCE>
00112 {
00113 };
00114
00115
00116
00117
00118 template<class DATA, class PRECISION, class SOURCE>
00119 class SourceCombineLayoutOf<DATA, 2, PRECISION, SOURCE> : public SourceCombineLayoutBaseOf<
00120 DATA, 2, PRECISION, SOURCE>
00121 {
00122 public:
00123 SOURCE_ACTUALS_2D ;
00124
00125 void Get(DATA& dataOut, const PRECISION &rX, const PRECISION &rY) const
00126 {
00127 const int& c=this->m_vecpt.C();
00128 type_point pt=type_point(rX,rY);
00129 for (int n=c-1; n>=0; n--)
00130 {
00131 type_point dpt=pt-this->m_vecpt[n];
00132
00133 if ( (!(dpt < 0)) && (!(dpt > this->PSource(n)->Size())) )
00134 {
00135 if ( (dpt < 1) || (dpt > (this->PSource(n)->Size()-1)) )
00136 {
00137 dataOut=MaxValue(DATA);
00138 }
00139 else
00140 {
00141 ::GetPoint(*this->PSource(n), dataOut, dpt);
00142 }
00143 return;
00144 }
00145 }
00146 dataOut=MidValue(DATA);
00147 }
00148
00149 void Set(const PRECISION &rX, const PRECISION &rY, const DATA& data)
00150 {
00151 const int& c=this->m_vecpt.C();
00152 type_point pt=type_point(rX,rY);
00153 for (int n=c-1; n>=0; n--)
00154 {
00155 type_point dpt=pt-this->m_vecpt[n];
00156
00157 if ( (!(dpt < 0)) && (!(dpt > this->PSource(n)->Size())) )
00158 {
00159 ::SetPoint(*this->PSource(n), dpt, data);
00160 return;
00161 }
00162 }
00163 }
00164 };
00165
00166
00167
00168
00169 template<class DATA, class PRECISION, class SOURCE>
00170 class SourceCombineLayoutOf<DATA, 3, PRECISION, SOURCE> : public SourceCombineLayoutBaseOf<
00171 DATA, 3, PRECISION, SOURCE>
00172 {
00173 public:
00174 SOURCE_ACTUALS_3D ;
00175
00176 void Get(DATA& dataOut, const PRECISION &rX, const PRECISION &rY, const PRECISION &rZ) const
00177 {
00178 const int& c=this->m_vecpt.C();
00179 type_point pt=type_point(rX,rY,rZ);
00180 for (int n=c-1; n>=0; n--)
00181 {
00182 type_point dpt=pt-this->m_vecpt[n];
00183
00184 if ( (!(dpt < 0)) && (!(dpt > this->PSource(n)->Size())) )
00185 {
00186 ::GetPoint(*this->PSource(n), dataOut, dpt);
00187 return;
00188 }
00189 }
00190 dataOut=DATA(0);
00191 }
00192
00193 void Set(const PRECISION &rX, const PRECISION &rY, const PRECISION &rZ, const DATA& data)
00194 {
00195 const int& c=this->m_vecpt.C();
00196 type_point pt=type_point(rX,rY,rZ);
00197 for (int n=c-1; n>=0; n--)
00198 {
00199 type_point dpt=pt-this->m_vecpt[n];
00200
00201 if ( (!(dpt < 0)) && (!(dpt > this->PSource(n)->Size())) )
00202 {
00203 ::SetPoint(*this->PSource(n), dpt, data);
00204 return;
00205 }
00206 }
00207 }
00208 };
00209
00210 template<class DATA, int DIMENSIONALITY, class PRECISION>
00211 class TypeSourceCombineLayoutGenericOf
00212 {
00213 public:
00214 typedef SourceCombineLayoutOf<DATA, DIMENSIONALITY, PRECISION, SourceOf<DATA,
00215 DIMENSIONALITY, PRECISION> > type;
00216 };
00217
00218 #define SourceCombineLayoutGeneric(_data,_dims,_precision) \
00219 TypeSourceCombineLayoutGenericOf<_data,_dims,_precision>::type
00220
00221 typedef SourceCombineLayoutGeneric(Pixel8BitGrey,3,Real) GenericLayout_G3R;
00222 typedef SourceCombineLayoutGeneric(Pixel8BitGrey,3,int) GenericLayout_G3I;
00223 typedef SourceCombineLayoutGeneric(Pixel8BitGrey,2,Real) GenericLayout_G2R;
00224 typedef SourceCombineLayoutGeneric(Pixel8BitGrey,2,int) GenericLayout_G2I;
00225
00226 #endif
00227