SpatialMetrics.C
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 #ifndef NEURO_SPATIALMETRICS_C_DEFINED
00039 #define NEURO_SPATIALMETRICS_C_DEFINED
00040 
00041 #include "Neuro/SpatialMetrics.H"
00042 
00043 #include "Component/GlobalOpts.H"
00044 #include "Component/ModelOptionDef.H"
00045 #include "Component/OptionManager.H"
00046 #include "Image/Point2D.H"
00047 #include "Media/MediaOpts.H"
00048 #include "Neuro/NeuroOpts.H"
00049 #include "Util/TextLog.H"
00050 #include "Util/log.H"
00051 #include "Util/sformat.H"
00052 
00053 #include <algorithm>
00054 
00055 
00056 SpatialMetrics::SpatialMetrics(OptionManager& mgr,
00057                                const std::string& descrName,
00058                                const std::string& tagName)
00059   :
00060   ModelComponent(mgr, descrName, tagName),
00061   itsLogFile(&OPT_TextLogFile, this),
00062   itsInputFrameDims(&OPT_InputFrameDims, this),
00063   itsFoveaRadius(&OPT_FoveaRadius, this),
00064   itsFOAradius(&OPT_FOAradius, this),
00065   itsPPD(&OPT_PixelsPerDegree, this)
00066 {}
00067 
00068 
00069 SpatialMetrics::~SpatialMetrics()
00070 {}
00071 
00072 
00073 int SpatialMetrics::getFoveaRadius() const
00074 { return itsFoveaRadius.getVal(); }
00075 
00076 
00077 int SpatialMetrics::getFOAradius() const
00078 { return itsFOAradius.getVal(); }
00079 
00080 
00081 double SpatialMetrics::getPPD() const
00082 { return itsPPD.getVal().ppd(); }
00083 
00084 
00085 double SpatialMetrics::getPPDX() const
00086 { return itsPPD.getVal().ppdx(); }
00087 
00088 
00089 double SpatialMetrics::getPPDY() const
00090 { return itsPPD.getVal().ppdy(); }
00091 
00092 
00093 void SpatialMetrics::paramChanged(ModelParamBase* param,
00094                                   const bool valueChanged,
00095                                   ParamClient::ChangeStatus* status)
00096 {
00097   ModelComponent::paramChanged(param, valueChanged, status);
00098 
00099   OptionManager& mgr = this->getManager();
00100 
00101   if (param == &itsInputFrameDims && itsInputFrameDims.getVal().isNonEmpty())
00102     {
00103       const Dims indims = itsInputFrameDims.getVal();
00104 
00105       {
00106         
00107         int foaRadius = 0;
00108         convertFromString(mgr.getOptionValString(&OPT_FOAradius),
00109                           foaRadius);
00110         if (foaRadius == 0) {
00111           foaRadius = std::min(indims.w(), indims.h()) / 12;
00112           mgr.setOptionValString(&OPT_FOAradius,
00113                                  convertToString(foaRadius));
00114           LINFO("Using FOA radius = %d pixels", foaRadius);
00115           textLog(itsLogFile.getVal(),
00116                   "FOAradius", sformat("%d", foaRadius));
00117         }
00118       }
00119 
00120       {
00121         
00122         int foveaRadius = 0;
00123         convertFromString(mgr.getOptionValString(&OPT_FoveaRadius),
00124                           foveaRadius);
00125         if (foveaRadius == 0) {
00126           foveaRadius = std::min(indims.w(), indims.h()) / 12;
00127           mgr.setOptionValString(&OPT_FoveaRadius,
00128                                  convertToString(foveaRadius));
00129           LINFO("Using fovea radius = %d pixels", foveaRadius);
00130           textLog(itsLogFile.getVal(),
00131                   "FoveaRadius", sformat("%d", foveaRadius));
00132         }
00133       }
00134 
00135     }
00136 }
00137 
00138 
00139 void SpatialMetrics::setFoveaRadius(int val)
00140 {
00141   getManager().setOptionValString(&OPT_FoveaRadius, convertToString(val));
00142 }
00143 
00144 
00145 void SpatialMetrics::setFOAradius(int val)
00146 {
00147   getManager().setOptionValString(&OPT_FOAradius, convertToString(val));
00148 }
00149 
00150 
00151 void SpatialMetrics::pix2deg(const Point2D<int>& pixloc,
00152                              double& xdeg, double& ydeg) const
00153 {
00154   const double width = double(itsInputFrameDims.getVal().w());
00155   const double height = double(itsInputFrameDims.getVal().h());
00156   const double ppdx = itsPPD.getVal().ppdx();
00157   const double ppdy = itsPPD.getVal().ppdy();
00158 
00159   xdeg = atan((2.0 * pixloc.i / width - 1.0) *
00160               tan(width / ppdx * M_PI / 360.0)) * 180.0 / M_PI;
00161   ydeg = atan((1.0 - 2.0 * pixloc.j / height) *
00162               tan(height / ppdy * M_PI / 360.0)) * 180.0 / M_PI;
00163 }
00164 
00165 
00166 void SpatialMetrics::deg2pix(const double xdeg, const double ydeg,
00167                              Point2D<int>& pixloc) const
00168 {
00169   const double width = double(itsInputFrameDims.getVal().w());
00170   const double height = double(itsInputFrameDims.getVal().h());
00171   const double ppdx = itsPPD.getVal().ppdx();
00172   const double ppdy = itsPPD.getVal().ppdy();
00173 
00174   pixloc.i = int(0.5 * width * (1.0 + tan(xdeg * M_PI / 180.0) /
00175                                 tan(width / ppdx * M_PI / 360.0)));
00176   pixloc.j = int(0.5 * height * (1.0 - tan(ydeg * M_PI / 180.0) /
00177                                  tan(height / ppdy * M_PI / 360.0)));
00178 }
00179 
00180 
00181 Dims SpatialMetrics::getInputFrameDims() const
00182 { return itsInputFrameDims.getVal(); }
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 #endif // NEURO_SPATIALMETRICS_C_DEFINED