PSFEstimationwithCPSO
psf.cpp
00001 /*
00002  * PSF.cpp
00003  *
00004  *  Created on: 27/05/2012
00005  *  Author: Peter Frank Perroni (pfperroni@inf.ufpr.br)
00006  */
00007 
00008 #include "psf.hpp"
00009 
00010 using namespace std;
00011 
00021 PSF::PSF(cl_context context, cl_command_queue _command_queue, int img_size, int phase_size, int _n_zernikes){
00022         command_queue = _command_queue;
00023         img_area = img_size * img_size;
00024         size_fft = phase_size * phase_size;
00025         n_zernikes = _n_zernikes;
00026 
00027         CREATE_BUFFER(context, CL_MEM_READ_WRITE, size_fft * SIZEOF_WORD, NULL, cl_psf);
00028         CREATE_BUFFER(context, CL_MEM_READ_WRITE, size_fft * SIZEOF_FFTTYPE, NULL, cl_pupil);
00029         CREATE_BUFFER(context, CL_MEM_READ_WRITE, size_fft * SIZEOF_FFTTYPE, NULL, cl_focus);
00030         CREATE_BUFFER(context, CL_MEM_READ_WRITE, img_area * SIZEOF_WORD, NULL, cl_psfe);
00031         CREATE_BUFFER(context, CL_MEM_READ_WRITE, REDUCTION_NBLOCKS * SIZEOF_WORD, NULL, cl_sum);
00032         CREATE_BUFFER(context, CL_MEM_READ_WRITE, img_area * SIZEOF_FFTTYPE, NULL, cl_fft_psfe);
00033 
00034         vn_pupil = new viennacl::vector<WORD>(cl_pupil, size_fft*2);
00035         vn_focus = new viennacl::vector<WORD>(cl_focus, size_fft*2);
00036         vn_fft_psfe = new viennacl::vector<WORD>(cl_fft_psfe, img_area*2);
00037 
00038         coefs = new WORD[n_zernikes];
00039         has_coefs = false;
00040         has_pupil = false;
00041 }
00042 
00046 PSF::~PSF() {
00047         delete vn_pupil;
00048         delete vn_focus;
00049         delete vn_fft_psfe;
00050 
00051         clReleaseMemObject(cl_pupil);
00052         clReleaseMemObject(cl_focus);
00053         clReleaseMemObject(cl_psf);
00054         clReleaseMemObject(cl_sum);
00055         clReleaseMemObject(cl_psfe);
00056         clReleaseMemObject(cl_fft_psfe);
00057         if(has_coefs) clReleaseMemObject(cl_coefs);
00058         if(has_pupil) clReleaseMemObject(cl_external_pupil);
00059 
00060         delete coefs;
00061 }
00062 
00072 void PSF::setCoefsPosition(cl_mem _cl_coefs, int offset){
00073         if(has_coefs) clReleaseMemObject(cl_coefs);
00074         clRetainMemObject(_cl_coefs);
00075         cl_coefs = _cl_coefs;
00076         coefs_offset = offset;
00077         has_coefs = true;
00078 }
00079 
00090 void PSF::setPupilPosition(cl_mem _cl_pupil, int offset){
00091         if(has_pupil) clReleaseMemObject(cl_external_pupil);
00092         clRetainMemObject(_cl_pupil);
00093         cl_external_pupil = _cl_pupil;
00094         pupil_offset = offset;
00095         has_pupil = true;
00096 }
00097 
00103 void PSF::refreshPupil(bool block){
00104         clMemcpyDeviceToDeviceOffset(command_queue, cl_pupil, 0, cl_external_pupil, pupil_offset * SIZEOF_FFTTYPE, size_fft * SIZEOF_FFTTYPE, block);
00105 }
00106 
00110 void PSF::pupilToFocusFft(){
00111         viennacl::fft(*vn_pupil, *vn_focus);
00112 }
00113 
00117 void PSF::psfeFft(){
00118         viennacl::inplace_fft(*vn_fft_psfe);
00119 }
00120 
00126 WORD* PSF::getCoefs(){
00127         clMemcpyDeviceToHostOffset(command_queue, coefs, cl_coefs, coefs_offset * SIZEOF_WORD, n_zernikes * SIZEOF_WORD);
00128         return coefs;
00129 }
 All Classes Functions