libAssistants.h

Go to the documentation of this file.
00001 #ifndef __LIBASSISTANTS_H__
00002 #define __LIBASSISTANTS_H__
00003 
00004 #include "SourceRegistry.h"
00005 #include "SourceCombinePointwiseOperator.h"
00006 #include "SourceMaskCombinePointwiseOperator.h"
00007 
00008 // TODO: find proper homes for these things
00009 
00010 
00011 //============================================================================
00012 // Generic Interpolate assistant
00013 //============================================================================
00014 // uses MAKE_ASSISTANT() created assistant functions
00015 template<class SOURCE>
00016 SourceAccessorOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE), Real, SOURCE>*
00017 Interpolate(InterpolationType it, SOURCE* psrc)
00018 {
00019   switch (it)
00020     {
00021     case Nearest:
00022       return NearestNeighbor(psrc);
00023     case Linear:
00024       return LinearInterpolation(psrc);
00025     case Cubic:
00026       return CubicInterpolation(psrc);
00027     default:
00028       {
00029       ERROR("unknown interpolation type %d", it);
00030       }
00031     }
00032 }
00033 
00034 //============================================================================
00035 // Rasterize assistant
00036 //============================================================================
00037 template<class SOURCE>
00038 inline ImageOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE)>*
00039 Rasterize(SOURCE* psrcIn)
00040 {
00041   typedef ImageOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE)> SRC_OUT;
00042 
00043   ClaimPointer(psrcIn);
00044   SRC_OUT *psrcOut = new SRC_OUT();
00045   AllocatedCopy(*psrcOut, psrcIn);
00046 
00047   ReleasePointer(psrcIn);
00048   HandoffPointer(psrcOut);
00049   return psrcOut;
00050 }
00051 
00052 //============================================================================
00053 // Rasterize assistant
00054 //============================================================================
00055 template<class SOURCE_OUT, class SOURCE_IN>
00056 inline SOURCE_OUT*
00057 RasterizeInto(SOURCE_OUT *psrcOut, SOURCE_IN* psrcIn)
00058 {
00059   ClaimPointer(psrcIn);
00060   ClaimPointer(psrcOut);
00061   AllocatedCopy(*psrcOut, psrcIn);
00062   ReleasePointer(psrcIn);
00063   HandoffPointer(psrcOut);
00064   return psrcOut;
00065 }
00066 
00067 //============================================================================
00068 // GaussianBlurAlong assistant 
00069 //    provides Gaussian Blur Along a single dimension
00070 //============================================================================
00071 MAKE_ASSISTANT(
00072     GaussianBlurAlong,
00073     SourceAccessorConvolutionOf,
00074     (
00075           {
00076           PointOf<TypeOfDimensionality(SOURCE), Real> ptSigma(1.);
00077           ptSigma.Dim(int(dt))=rSigma;
00078           PointOf<TypeOfDimensionality(SOURCE), int> ptSupport(1);
00079           ptSupport.Dim(int(dt))=cSupport;
00080 
00081           psrcOut->SetFilter(GaussianCurve(ptSigma, ptSupport));
00082           }),
00083     const DimensionType& dt, const Real& rSigma, const int& cSupport,
00084 );
00085 
00086 //============================================================================
00087 // DownsampleAlong assistant 
00088 //    provides Downsampling with 1-2-1 filtering along a single dimension
00089 //============================================================================
00090 template<class SOURCE>
00091 inline SourceTransformScaleOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE),
00092     TypeOfPrecision(SOURCE), SourceAccessorConvolutionOf<TypeOfData(SOURCE),
00093         TypeOfDimensionality(SOURCE), TypeOfPrecision(SOURCE), SOURCE> > *
00094 DownsampleAlong(const DimensionType& dt, SOURCE* psrcIn)
00095 {
00096   typedef PointOf<TypeOfDimensionality(SOURCE), int> POINT;
00097   POINT ptSupport(1);
00098   ptSupport.Dim(int(dt)) = 3;
00099   ImageOf<Real, TypeOfDimensionality(SOURCE)>* piFilter = new ImageOf<Real,
00100       TypeOfDimensionality(SOURCE)> ;
00101   piFilter->Allocate(ptSupport);
00102 
00103   // NOTE: is there a cleaner way to do this?
00104   Real vr[3] =
00105   { .25, .5, .25 };
00106   int n = 0;
00107   forpoint(POINT,pt,0,ptSupport)
00108     {
00109     SetPoint(*piFilter,pt,vr[n]);
00110     n++;
00111     }
00112 
00113   PointOf<TypeOfDimensionality(SOURCE), Real> ptScale(1.);
00114   ptScale.Dim(int(dt)) = .5;
00115 
00116   typedef SourceAccessorConvolutionOf<TypeOfData(SOURCE),
00117       TypeOfDimensionality(SOURCE), TypeOfPrecision(SOURCE), SOURCE> SRC_OUT;
00118 
00119   SRC_OUT *psrcOut = new SRC_OUT;
00120   psrcOut->SetSource(psrcIn);
00121   psrcOut->SetFilter(HandoffPointer(piFilter));
00122   return ScaleBy(ptScale, HandoffPointer(psrcOut));
00123 }
00124 
00125 //============================================================================
00126 //============================================================================
00127 template<class DATA, class PRECISION>
00128 ImageOf<DATA, 2>* RasterizeDownsample(SourceOf<DATA, 2, PRECISION>* psrcIn)
00129 {
00130   return Rasterize(DownsampleAlong(DimensionY, Rasterize(DownsampleAlong(
00131       DimensionX, psrcIn))));
00132 }
00133 
00134 //============================================================================
00135 //============================================================================
00136 template<class SOURCE>
00137 ImageOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE)>* RasterizeDownsample(SOURCE* psrcIn)
00138 {
00139   return Rasterize(DownsampleAlong(DimensionZ, Rasterize(DownsampleAlong(
00140       DimensionY, Rasterize(DownsampleAlong(DimensionX, psrcIn))))));
00141 }
00142 
00143 //============================================================================
00144 //============================================================================
00145 template<class SOURCE>
00146 ImageOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE)>* RasterizeDownsample(int cTimes, SOURCE* psrcIn)
00147 {
00148   if (cTimes == 0)
00149     {
00150     return Rasterize(psrcIn);
00151     }
00152 
00153   ClaimPointer(psrcIn);
00154   ImageOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE)>* piOut =
00155       RasterizeDownsample(psrcIn);
00156   ReleasePointer(psrcIn);
00157   while (cTimes > 1)
00158     {
00159     ImageOf<TypeOfData(SOURCE), TypeOfDimensionality(SOURCE)>* piT =
00160         RasterizeDownsample(piOut);
00161     piOut = piT;
00162     cTimes--;
00163     }
00164   return piOut;
00165 }
00166 
00167 #endif

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1