IMP  2.0.0
The Integrative Modeling Platform
KernelParameters.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em/KernelParameters.h
3  * \brief Calculates and stores gaussian kernel parameters.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEM_KERNEL_PARAMETERS_H
10 #define IMPEM_KERNEL_PARAMETERS_H
11 #include "def.h"
12 #include <IMP/base_types.h>
13 #include <boost/scoped_array.hpp>
14 #include <IMP/exception.h>
15 #include <IMP/log.h>
16 #include <map>
17 #include <cmath>
18 #include <algorithm>
19 #include <iostream>
20 #include <iomanip>
21 #include <vector>
22 #include <limits>
23 
24 IMPEM_BEGIN_NAMESPACE
25 
26 //! Calculates kernel parameters as a function of a specific radius.
28  public:
30  float radii,float rsigsq,float timessig,
31  float sq2pi3,float inv_rsigsq, float rnormfac, float rkdist);
32  //! Show
33  void show(std::ostream& s=std::cout) const;
34  //! Gets the value of the volume sigma
35  inline float get_vsig() const { return vsig_;}
36  //! Gets the value of volume sigma squared
37  inline float get_vsigsq() const { return vsigsq_;}
38  //! Gets the inverse sigma squared
39  inline float get_inv_sigsq() const { return inv_sigsq_;}
40  //! Gets the sigma value
41  inline float get_sig() const { return sig_;}
42  //! Gets the value of kdist parameter
43  inline float get_kdist() const { return kdist_;}
44  //! Gets the value of normfac parameter
45  inline float get_normfac() const { return normfac_;}
47  vsig_= std::numeric_limits<float>::quiet_NaN();
48  vsigsq_= std::numeric_limits<float>::quiet_NaN();
49  inv_sigsq_= std::numeric_limits<float>::quiet_NaN();
50  sig_= std::numeric_limits<float>::quiet_NaN();
51  kdist_= std::numeric_limits<float>::quiet_NaN();
52  normfac_= std::numeric_limits<float>::quiet_NaN();
53  }
54  protected:
55  //! vsig
56  float vsig_;
57  //! square of vsig
58  float vsigsq_;
59  //! the inverse of sigma square
60  float inv_sigsq_;
61  //! the sigma
62  float sig_;
63  //! the kernel distance (= elements for summation)
64  float kdist_;
65  //! normalization factor
66  float normfac_;
67 };
68 
71 
72 //! Calculates and stores Gaussian kernel parameters as a function
73 //! of a specific radius.
74 class IMPEMEXPORT KernelParameters
75 {
76 public:
78  initialized_ = false;
79  }
80 
81  KernelParameters(float resolution) {
82  init(resolution);
83  initialized_ = true;
84  }
85 
87 
88  //! Sets the parameters that depend on the radius of a given particle.
89  /** The other variables of the parameters
90  (rsigsq,timessig,sq2pi3,inv_rsigsq,rnormfac,rkdist) must have been set.
91  \param[in] radius the radius
92  \return the radius based parameters
93  */
94  const RadiusDependentKernelParameters& set_params(float radius);
95 
96  //! Finds the precomputed parameters given a particle radius.
97  /**
98  \param[in] radius searching for parameters of this radius
99  \param[in] eps used for numerical stability
100  \note The parameters are indexes by the radius. To maintain
101  numerical stability, look for a radius within +-eps from the
102  queried radius.
103  \note if parameters for this radius were not found, a warning is printed
104  and the parameters are calculated using set_params().
105  */
106  const RadiusDependentKernelParameters& get_params(
107  float radius,float eps=0.001);
108 
109  //! Get sigma as a function of the resolution according to the
110  //! full width at half maximum criterion
111  inline float get_rsig() const {return rsig_;}
112  //! Get squared sigma as a function of the resolution according to the
113  //! full width at half maximum criterion
114  inline float get_rsigsq() const {return rsigsq_;}
115  //! Get the inverse of sigma sqaured
116  inline float get_inv_rsigsq() const {return inv_rsigsq_;}
117  //! Gets the number of sigma used.
118  /**
119  \note We use 3, which means that 99% of the density is considered
120  */
121  inline float get_timessig() const {return timessig_;}
122  //! Get the non-sigma portion of the Gaussian normalization factor
123  inline float get_sq2pi3() const {return sq2pi3_;}
124  //! Get the Gaussian normalization factor
125  inline float get_rnormfac() const {return rnormfac_;}
126  //! Get the length of the Gaussian (sigma*number_of_sigmas_used)
127  inline float get_rkdist() const {return rkdist_;}
128  //! Gets the value of lim parameter
129  inline float get_lim() const {return lim_;}
130 
131  IMP_SHOWABLE_INLINE(KernelParameters, out << "rsig: " << rsig_ << std::endl;);
132 protected:
133  float rsig_,rsigsq_,timessig_,sq2pi3_,inv_rsigsq_,rnormfac_,rkdist_,lim_;
134  bool initialized_;
135  std::map <float,const RadiusDependentKernelParameters* > radii2params_;
136  void init(float resolution);
137 };
138 
140 
141 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
142 class IMPEMEXPORT Kernel3D {
143 public:
144  Kernel3D(){}
145  Kernel3D(const Kernel3D &other) {
146  size_=other.size_;
147  dim_ext_=other.dim_ext_;
148  data_.reset(new double[size_]);
149  std::copy(other.data_.get(),other.data_.get()+size_,data_.get());
150  }
151  Kernel3D(int size,int ext) {
152  size_=size;dim_ext_=ext;
153  data_.reset(new double[size_]);
154  IMP_INTERNAL_CHECK(data_,"Can not allocate vector\n");
155  for(int i=0;i<size_;i++) data_[i]=0.;
156  }
157  double *get_data() const {return data_.get();}
158  int get_size() const {return size_;}
159  int get_extent() const {return dim_ext_;}
160 protected:
161  boost::scoped_array<double> data_;
162  int size_;
163  int dim_ext_;
164 };
165 IMPEMEXPORT
166 Kernel3D create_3d_gaussian (double sigma,double sigma_factor);
167 IMPEMEXPORT
168 Kernel3D create_3d_laplacian();
169 //! Truncate a kernel according to an input sigma
170 IMPEMEXPORT
171 Kernel3D get_truncated(double *data,int extent,double sigmap,double sigma_fac);
172 #endif
173 IMPEM_END_NAMESPACE
174 
175 #endif /* IMPEM_KERNEL_PARAMETERS_H */