contrib/oxl/osl/osl_1d_half_kernel.txx
Go to the documentation of this file.
00001 // This is oxl/osl/osl_1d_half_kernel.txx
00002 #ifndef osl_1d_half_kernel_txx_
00003 #define osl_1d_half_kernel_txx_
00004 //:
00005 // \file
00006 // \author fsm
00007 
00008 #include "osl_1d_half_kernel.h"
00009 #include <vcl_iostream.h>
00010 #include <vcl_cassert.h>
00011 #include <vcl_cmath.h>
00012 
00013 //: helper routine used by create_gaussian
00014 //static/*FIXME*/
00015 float osl_compute_gauss_weight (float sigma, int mask_index)
00016 {
00017   float sum = 0;
00018 
00019   for (int repeat = 0; repeat < 6; repeat++)
00020   {
00021     double x = mask_index-0.5 + 0.2*repeat;
00022     sum += (float)vcl_exp( - (x*x) / (2 * sigma * sigma));
00023   }
00024 
00025   return sum / 6;
00026 }
00027 
00028 //: routine to generate a gaussian convolution mask.
00029 template <class T>
00030 void osl_create_gaussian (T gauss_sigma, osl_1d_half_kernel<T> *mask_ptr) {
00031   // stop adding entries to the Gauss mask when the size of an entry is less than this : */
00032   float const CN_GAUSS_CUTOFF_VALUE = 0.02f;
00033 
00034   unsigned mask_index = 0;
00035 
00036   double gauss_weight = osl_compute_gauss_weight((float)gauss_sigma, mask_index);
00037   while (gauss_weight > CN_GAUSS_CUTOFF_VALUE)
00038   {
00039     mask_ptr->array [mask_index] = gauss_weight;
00040     ++ mask_index;
00041 
00042     gauss_weight = osl_compute_gauss_weight((float)gauss_sigma, mask_index);
00043 
00044     if (mask_index == mask_ptr->capacity)
00045     {
00046       vcl_cerr << "mask size equal to capacity - must recompile with new mask size\n";
00047       assert(mask_index != mask_ptr->capacity);
00048     }
00049   }
00050 
00051   mask_ptr->count = mask_index;
00052 
00053   double total_mask_value = mask_ptr->array [0];
00054   for (mask_index = 1; mask_index < mask_ptr->count; mask_index++)
00055     total_mask_value += 2 * mask_ptr->array [mask_index];
00056 
00057   for (mask_index = 0; mask_index < mask_ptr->count; mask_index++)
00058   {
00059     mask_ptr->array [mask_index] /= total_mask_value;
00060 
00061     if (mask_ptr->array [mask_index] < CN_GAUSS_CUTOFF_VALUE)
00062       mask_ptr->count = mask_index;
00063   }
00064 }
00065 
00066 //----------------------------------------------------------------------------
00067 
00068 #define OSL_1D_HALF_KERNEL_INSTANTIATE(T) \
00069 template struct osl_1d_half_kernel<T >; \
00070 template void osl_create_gaussian(T , osl_1d_half_kernel<T > *)
00071 
00072 #endif // osl_1d_half_kernel_txx_