PSQt/include/QGUtils.h

Go to the documentation of this file.
00001 #ifndef QGUTILS_H
00002 #define QGUTILS_H
00003 
00004 #include "ndarray/ndarray.h"
00005 #include "PSQt/Logger.h"
00006 
00007 #include "PSQt/GUAxes.h"
00008 #include <QGraphicsScene>
00009 #include <QGraphicsView>
00010 #include <QGraphicsPathItem>
00011 
00012 #include <math.h>   // atan2, abs, fmod
00013 #include <string> 
00014 #include <sstream>  // for stringstream
00015 #include <stdint.h> // uint8_t, uint32_t, etc.
00016 #include <iomanip>  // for setw
00017 //#include "PSQt/GeoImage.h"
00018 //#include <QPainter>
00019 //#include <QPen>
00020 
00021 #include <QLabel>
00022 #include <QPainter>
00023 #include <QPixmap>
00024 #include <QFont>
00025 
00026 namespace PSQt {
00027 
00028 //--------------------------
00029 
00030 /**
00031  *  This software was developed for the LCLS project.  If you use all or 
00032  *  part of it, please give an appropriate acknowledgment.
00033  *
00034  *  @see 
00035  *
00036  *  @version $Id: QGUtils.h 9841 2015-03-26 18:35:44Z dubrovin@SLAC.STANFORD.EDU $
00037  *
00038  *  @author Mikhail Dubrovin
00039  */
00040 
00041 //--------------------------
00042 
00043 /**
00044  * @brief Returns uint32_t color code for R, G, and B parameters.
00045  * R - red   [0,1] 
00046  * G - green [0,1] 
00047  * B - blue  [0,1] 
00048  */ 
00049 uint32_t fRGBA(const float R, const float G, const float B);
00050 
00051 /**
00052  * @brief Returns uint32_t color code for H, S, and V parameters.
00053  * H - hue [0,360) 
00054  * S - black/white satturation [0,1] 
00055  * V - value of color [0,1] 
00056  * ??? http://www.rapidtables.com/convert/color/hsv-to-rgb.htm
00057  * http://en.wikipedia.org/wiki/HSL_and_HSV
00058  * http://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both
00059  */ 
00060 uint32_t HSV2RGBA(const float H, const float S, const float V);
00061 
00062 /**
00063  * @brief Returns uint32_t* color table for requested parameters.
00064  * NColors - number of colors
00065  * H1, H2=[-360,360] - extended to negative hue range
00066  */ 
00067 uint32_t*
00068   ColorTable(const unsigned& NColors=0, const float& H1=0, const float& H2=0);
00069 
00070 /**
00071  * @brief Returns 2-d ndarray with colorbar image
00072  * @param[in] rows - number of rows in the color bar image
00073  * @param[in] cols - number of columns and colors in the color bar image *
00074  * @param[in] hue1 - first limit of the hue angle [0,360]
00075  * @param[in] hue2 - second limit of the hue angle [0,360]
00076  */ 
00077 ndarray<uint32_t, 2> 
00078 getColorBarImage(const unsigned& rows =   20, 
00079                  const unsigned& cols = 1024,
00080                  const float&    hue1 = -120,
00081                  const float&    hue2 =   60) ;
00082 
00083 /**
00084  * @brief Sets image as a pixmap for label
00085  */ 
00086 void 
00087   setPixmapForLabel(const QImage& image, QPixmap*& pixmap, QLabel*& label);
00088 
00089 //--------------------------
00090 
00091  int string_to_int(const std::string& str);
00092 
00093 //--------------------------
00094 
00095  void print_font_status(const QFont& font);
00096 
00097 //--------------------------
00098 //--------------------------
00099 
00100 template<typename T>
00101   std::string val_to_string(const T& v, const unsigned& prec=0)
00102   {
00103     std::stringstream ss; ss << std::fixed << std::setprecision(prec) << v;
00104     return ss.str();
00105   }
00106 
00107 //--------------------------
00108 
00109  void set_pen(QPen & pen, const std::string & opt);
00110  void set_brush(QBrush & brush, const std::string & opt);
00111  void set_pen_brush(QPen & pen, QBrush & brush, const std::string & opt);
00112 
00113 //--------------------------
00114 
00115  QGraphicsPathItem* 
00116    graph(QGraphicsScene* scene, const float* x, const float* y, const int n=1, const std::string& opt=std::string("-bT"));
00117 
00118  QGraphicsPathItem* 
00119    graph(QGraphicsView * view,  const float* x, const float* y, const int n=1, const std::string& opt=std::string("-bT"));
00120 
00121 //--------------------------
00122 
00123  QGraphicsPathItem* 
00124    hist(QGraphicsScene* scene, const float* x, const float* y, const int n=1, const std::string& opt=std::string("-bT"));
00125 
00126  QGraphicsPathItem* 
00127    hist(QGraphicsView * view,  const float* x, const float* y, const int n=1, const std::string& opt=std::string("-bT"));
00128 
00129 //--------------------------
00130  
00131  bool is_file(const std::string& path);
00132  bool is_directory(const std::string& path);
00133  bool is_link(const std::string& path);
00134 
00135 //--------------------------
00136  
00137 /**
00138  * @brief check if directory exists
00139  */ 
00140  bool dir_exists(const std::string& dir);
00141 
00142 //--------------------------
00143  
00144 /**
00145  * @brief check if file exists
00146  */ 
00147  bool file_exists (const std::string& fname);
00148 
00149 //--------------------------
00150  
00151  bool path_exists (const std::string& path);
00152 
00153 //--------------------------
00154  
00155  std::string basename(const std::string& path);
00156  std::string dirname(const std::string& path);
00157 
00158 //--------------------------
00159 
00160  std::string split_string_left(const std::string& s, size_t& pos, const char& sep);
00161 
00162 //--------------------------
00163  void splitext(const std::string& path, std::string& root, std::string& ext);
00164 
00165 //--------------------------
00166  std::string stringTimeStamp(const std::string& format=std::string("%Y-%m-%d-%H:%M:%S"));
00167 
00168 //--------------------------
00169  std::string getFileNameWithTStamp(const std::string& fname);
00170 
00171 //--------------------------
00172  std::string getGeometryFileName(const std::string& fname="geometry.txt", const bool& add_tstamp=true);
00173 
00174 //--------------------------
00175 
00176 template <typename T>
00177 void
00178   getMinMax(const ndarray<T,2>& nda, float& vmin, float& vmax)
00179   {
00180     float v = 0;
00181     vmin = nda[0][0];
00182     vmax = nda[0][0];
00183     typename ndarray<T, 2>::iterator itd = nda.begin();
00184     for(; itd!=nda.end(); ++itd) { 
00185       v = float(*itd);
00186       if( v < vmin ) vmin = v;
00187       if( v > vmax ) vmax = v;
00188     }
00189   }
00190 
00191 //--------------------------
00192 
00193 template <typename T>
00194 void
00195   getAveRms(const ndarray<T,2>& nda, double& ave, double& rms)
00196   {
00197     double val = 0;
00198     double s0 = 0;
00199     double s1 = 0;
00200     double s2 = 0;
00201 
00202     typename ndarray<T, 2>::iterator itd = nda.begin();
00203     for(; itd!=nda.end(); ++itd) { 
00204       val = *itd;
00205       s0 += 1;
00206       s1 += val;
00207       s2 += val*val;
00208     }
00209     
00210     if(s0) {
00211       s1 /= s0;
00212       s2 /= s0;
00213     }
00214 
00215     ave = s1;
00216     rms = sqrt(s2-s1*s1);
00217   }
00218 
00219 //--------------------------
00220 
00221 template <typename T>
00222 ndarray<uint32_t,2> 
00223   getUint32NormalizedImage (const ndarray<T,2>& dnda, const float& amin=0, const float& amax=0, const unsigned& ncolors=0, const float& hue1=-120, const float& hue2=-360)
00224 {
00225   typedef uint32_t image_t;
00226 
00227   MsgInLog("QGUtils", DEBUG, "getUint32NormalizedImage(): convert raw ndarray to colored uint32_t");
00228 
00229   ndarray<image_t, 2> inda(dnda.shape());
00230 
00231   float dmin, dmax;
00232 
00233   if (amin || amax) {
00234     dmin = amin;
00235     dmax = amax;
00236   }
00237   else {
00238     getMinMax(dnda, dmin, dmax);
00239 
00240     //double dave, drms;
00241     //getAveRms(dnda, dave, drms);
00242     //dmin = dave - 1*drms;
00243     //dmax = dave + 10*drms;
00244   }
00245 
00246   std::stringstream ssd; ssd << "getUint32NormalizedImage():" 
00247                              << " dmin:"  << dmin
00248                              << " dmax:"  << dmax
00249                              << " h1:"    << hue1
00250                              << " h2:"    << hue2
00251                              << " ncols:" << ncolors;
00252 
00253   MsgInLog("QGUtils", DEBUG, ssd.str());
00254 
00255   typename ndarray<T, 2>::iterator itd = dnda.begin();
00256 
00257   //double dmin = dnda[0][0];
00258   //double dmax = dnda[0][0];
00259 
00260   //for(; itd!=dnda.end(); ++itd) { 
00261   //  if( *itd < dmin ) dmin = *itd;
00262   //  if( *itd > dmax ) dmax = *itd;
00263   //}
00264 
00265   //std::stringstream ssd; ssd << "getUint32NormalizedImage(): dmin: " << dmin << " dmax: " << dmax;
00266   //MsgInLog("QGUtils", DEBUG, ssd.str());
00267 
00268   // Convert image of type T to uint32_t Format_ARGB32
00269   if (ncolors) {
00270     //const unsigned ncolors = 1024;
00271     float k = (dmax-dmin) ? ncolors/(dmax-dmin) : 1; 
00272     image_t* ctable = ColorTable(ncolors, hue1, hue2);
00273     ndarray<image_t, 2>::iterator iti;
00274     for(itd=dnda.begin(), iti=inda.begin(); itd!=dnda.end(); ++itd, ++iti) { 
00275       float da = *itd-dmin;
00276       unsigned cind = (da>0) ? unsigned(da*k) : 0;
00277       cind = (cind<ncolors) ? cind : ncolors-1;
00278       *iti = ctable[cind]; // converts to 24bits adds alpha layer
00279     }
00280     delete [] ctable;
00281   }
00282   else {
00283     T k = (dmax-dmin) ? 0xFFFFFF/(dmax-dmin) : 1; 
00284     ndarray<image_t, 2>::iterator iti;
00285     for(itd=dnda.begin(), iti=inda.begin(); itd!=dnda.end(); ++itd, ++iti) { 
00286       *iti = image_t( (*itd-dmin)*k ) + 0xFF000000; // converts to 24bits adds alpha layer
00287     }
00288   }    
00289 
00290   return inda;
00291 }
00292 
00293 //--------------------------
00294 
00295 } // namespace PSQt
00296 
00297 #endif // QGUTILS_H

Generated on 19 Dec 2016 for PSANAmodules by  doxygen 1.4.7