PSQt/src/GeoImage.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------
00002 // File and Version Information:
00003 //   $Id: GeoImage.cpp 12033 2016-06-06 23:06:35Z dubrovin@SLAC.STANFORD.EDU $
00004 //
00005 // Author: Mikhail S. Dubrovin
00006 //---------------------------------------------------------------------
00007 
00008 //--------------------------
00009 #include "PSQt/GeoImage.h"
00010 #include "PSQt/Logger.h"
00011 #include "PSQt/QGUtils.h"
00012 
00013 #include <iostream>    // for std::cout
00014 #include <fstream>     // for std::ifstream(fname)
00015 #include <cstdlib>     // for rand()
00016 #include <cstring> // for memcpy
00017 
00018 //#include <math.h>  // atan2
00019 //using namespace std; // for cout without std::
00020 
00021 namespace PSQt {
00022 
00023 //--------------------------
00024 
00025   GeoImage::GeoImage(PSCalib::GeometryAccess* geometry, const std::string& fname_img, const int& xcent, const int& ycent)
00026   : QObject(NULL)
00027   , m_fname_geo(std::string())
00028   , m_fname_img(fname_img)
00029   , m_xcent(xcent)
00030   , m_ycent(ycent)
00031   , m_geometry(geometry)
00032   , m_ndaio(0)
00033 {
00034   this -> setGeometryPars();
00035   this -> loadImageArrayFromFile(m_fname_img);
00036   this -> connectTestSignalsSlots();
00037 }
00038 
00039 //--------------------------
00040 
00041 GeoImage::GeoImage(const std::string& fname_geo, const std::string& fname_img, const int& xcent, const int& ycent) 
00042   : QObject(NULL)
00043   , m_fname_geo(fname_geo)
00044   , m_fname_img(fname_img)
00045   , m_xcent(xcent)
00046   , m_ycent(ycent)
00047   , m_geometry(0)
00048   , m_ndaio(0)
00049 {
00050   this -> checkFileNames();
00051   this -> loadGeometryFromFile(m_fname_geo);
00052   this -> setGeometryPars();
00053   this -> loadImageArrayFromFile(m_fname_img);
00054   this -> connectTestSignalsSlots();
00055 }
00056 
00057 //--------------------------
00058 void
00059 GeoImage::connectTestSignalsSlots()
00060 {
00061   connect(this, SIGNAL(imageIsUpdated(ndarray<GeoImage::raw_image_t,2>&)), 
00062           this, SLOT(testSignalImageIsUpdated(ndarray<GeoImage::raw_image_t,2>&)));
00063 }
00064 
00065 //--------------------------
00066 void
00067 GeoImage::checkFileNames()
00068 {
00069   const std::string base_dir = "/reg/g/psdm/detector/alignment/cspad/calib-cxi-ds1-2014-05-15/";
00070   const std::string fname_geo = base_dir + "calib/CsPad::CalibV1/CxiDs1.0:Cspad.0/geometry/2-end.data"; 
00071   const std::string fname_nda = base_dir + "cspad-arr-cxid2714-r0023-lysozyme-rings.txt"; 
00072 
00073   if (m_fname_geo.empty()) m_fname_geo = fname_geo;
00074   if (m_fname_img.empty()) m_fname_img = fname_nda;
00075 
00076   MsgInLog(_name_(), INFO, "Use fname_geo: " + m_fname_geo); 
00077   MsgInLog(_name_(), INFO, "Use fname_img: " + m_fname_img); 
00078 }
00079 
00080 //--------------------------
00081 void
00082 GeoImage::loadGeometryFromFile(const std::string& fname_geo)
00083 {
00084   m_fname_geo = fname_geo;
00085 
00086   if (m_geometry) delete m_geometry;
00087   m_geometry = new PSCalib::GeometryAccess(m_fname_geo);
00088   MsgInLog(_name_(), INFO, "Geometry is loaded from file" + m_fname_geo); 
00089 }
00090 
00091 //--------------------------
00092 void
00093 GeoImage::setGeometryPars()
00094 {
00095   m_pix_scale_size = m_geometry->get_pixel_scale_size();
00096   m_size = m_geometry->get_top_geo()->get_size_geo_array();
00097 
00098   stringstream ss; 
00099   ss << "Set geometry parameters: pix_scale_size[um] = " << m_pix_scale_size
00100      << "  size = " << m_size;
00101   MsgInLog(_name_(), DEBUG, ss.str()); 
00102 }
00103 
00104 //--------------------------
00105 void
00106 GeoImage::loadImageArrayFromFile(const std::string& fname)
00107 {
00108   m_fname_img = fname;
00109 
00110   if (m_ndaio) delete m_ndaio;
00111   unsigned shape[] = {m_size};  
00112   m_ndaio = new GeoImage::NDAIO(m_fname_img, shape, 1, 0);
00113   //m_ndaio->print_ndarray();
00114   m_anda = m_ndaio->get_ndarray(); // or get_ndarray(fname);  
00115   //std::cout << m_anda << '\n';
00116   stringstream ss; 
00117   ss << "Image array of size " << m_anda.size() << " is loaded from file" << m_fname_img;
00118   MsgInLog(_name_(), INFO,  ss.str()); 
00119 }
00120 
00121 //--------------------------
00122 //--------------------------
00123 //--------- SLOTS ----------
00124 //--------------------------
00125 //--------------------------
00126 void 
00127 GeoImage::onGeometryIsLoaded(PSCalib::GeometryAccess* geometry)
00128 {
00129   MsgInLog(_name_(), INFO, "onGeometryIsLoaded() - begin update"); 
00130 
00131   if (m_geometry) delete m_geometry;
00132   m_geometry = geometry;
00133 
00134 
00135   this -> setGeometryPars();
00136 
00137   this -> updateImage();
00138 }
00139 
00140 //--------------------------
00141 void 
00142 GeoImage::onGeoIsChanged(shpGO& geo)
00143 {
00144   //std::cout << "testSignalGeoIsChanged():\n";
00145   //geo->print_geo();
00146   //m_geo->print_geo();
00147   MsgInLog(_name_(), DEBUG, "onGeoIsChanged(): " + geo->str_data()); // string_geo()); 
00148 
00149   //m_geometry->print_list_of_geos();
00150 
00151   this -> updateImage();
00152 }
00153 
00154 //--------------------------
00155 void 
00156 GeoImage::onImageFileNameIsChanged(const std::string& fname)
00157 {
00158   MsgInLog(_name_(), INFO, "onImageFileNameIsChanged(): fname = " + fname); 
00159 
00160   this -> loadImageArrayFromFile(fname);
00161   updateImage();
00162 }
00163 
00164 //--------------------------
00165 void 
00166 GeoImage::updateImage()
00167 {
00168   MsgInLog(_name_(), DEBUG, "updateImage(): re-generate raw image ndarray and emit signal: imageIsUpdated(nda)"); 
00169 
00170   //const ndarray<GeoImage::image_t,2> nda = getRandomImage();
00171   //const ndarray<GeoImage::image_t, 2> nda = getNormalizedImage();
00172   //emit normImageIsUpdated(nda);
00173 
00174   //ndarray<const GeoImage::raw_image_t, 2> nda = getImage();
00175   ndarray<GeoImage::raw_image_t, 2>& nda = getImage();
00176   emit imageIsUpdated(nda);
00177 }
00178 
00179 //--------------------------
00180 void 
00181 GeoImage::setFirstImage()
00182 {
00183   this -> updateImage();
00184 }
00185 
00186 //--------------------------
00187 
00188 //const ndarray<const GeoImage::raw_image_t,2>
00189 ndarray<GeoImage::raw_image_t,2> &
00190 GeoImage::getImage()
00191 {
00192   const unsigned* iX;
00193   const unsigned* iY;
00194   unsigned        isize;
00195 
00196   m_geometry->get_top_geo()->evaluate_pixel_coords(true,true);
00197   double pix_scale_size =m_geometry->get_pixel_scale_size();
00198   //const int xy0_off_pix[2] = {2000,2000};
00199   const int xy0_off_pix[2] = {m_xcent,m_ycent};
00200   //m_geometry->get_pixel_coord_indexes(iX, iY, isize);
00201   //m_geometry->get_pixel_coord_indexes(iX, iY, isize, ioname, ioindex, pix_scale_size_um, xy0_off_pix);
00202   //m_geometry->get_pixel_coord_indexes(iX, iY, isize, std::string(), 0, pix_scale_size, xy0_off_pix);
00203   const double zplane=0;
00204   m_geometry->get_pixel_xy_inds_at_z(iX, iY, isize, zplane, std::string(), 0, pix_scale_size, xy0_off_pix);
00205 
00206   //return PSCalib::GeometryAccess::img_from_pixel_arrays(iX, iY, &m_anda[0], isize);
00207   return m_geometry->ref_img_from_pixel_arrays(iX, iY, &m_anda[0], isize);
00208 }
00209 
00210 //--------------------------
00211 //--------------------------
00212 //------ For tests ---------
00213 //--------------------------
00214 //--------------------------
00215 void 
00216 GeoImage::testSignalImageIsUpdated(ndarray<GeoImage::raw_image_t,2>& nda)
00217 {  
00218   stringstream ss; ss << "testSignalImageIsUpdated(), size = " << nda.size();
00219   MsgInLog(_name_(), DEBUG, ss.str()); 
00220 }
00221 
00222 //--------------------------
00223 
00224 ndarray<GeoImage::image_t,2>
00225 GeoImage::getNormalizedImage()
00226 {
00227   typedef PSCalib::GeometryAccess::image_t raw_image_t; // double
00228   typedef GeoImage::image_t image_t;                   // uint32_t
00229 
00230   static unsigned counter = 0;
00231   stringstream ss; ss << "getNormalizedImage() " << ++counter;
00232   MsgInLog(_name_(), INFO, ss.str());
00233 
00234   const ndarray<const raw_image_t, 2> dnda = this->getImage();
00235 
00236   return getUint32NormalizedImage<const raw_image_t>(dnda); // from QGUtils
00237 }
00238 
00239 //--------------------------
00240 
00241 ndarray<GeoImage::image_t,2>
00242 GeoImage::getRandomImage()
00243 {
00244   const unsigned rows = 1750;
00245   const unsigned cols = 1750;
00246   float hue1 = rand()%360; // 0;
00247   float hue2 = hue1 + 360;
00248   stringstream ss; ss << "Color bar image for hue angle range " << hue1 << " : " << hue2;
00249   MsgInLog(_name_(), INFO, ss.str());
00250   return getColorBarImage(rows, cols, hue1, hue2); // from QGUtils
00251 }
00252 
00253 //--------------------------
00254 //--------------------------
00255 //--------------------------
00256 //--------------------------
00257 
00258 } // namespace PSQt
00259 
00260 //--------------------------
00261 
00262 

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7