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>
00013 #include <string>
00014 #include <sstream>
00015 #include <stdint.h>
00016 #include <iomanip>
00017
00018
00019
00020
00021 #include <QLabel>
00022 #include <QPainter>
00023 #include <QPixmap>
00024 #include <QFont>
00025
00026 namespace PSQt {
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 uint32_t fRGBA(const float R, const float G, const float B);
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 uint32_t HSV2RGBA(const float H, const float S, const float V);
00061
00062
00063
00064
00065
00066
00067 uint32_t*
00068 ColorTable(const unsigned& NColors=0, const float& H1=0, const float& H2=0);
00069
00070
00071
00072
00073
00074
00075
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
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
00139
00140 bool dir_exists(const std::string& dir);
00141
00142
00143
00144
00145
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
00241
00242
00243
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
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 if (ncolors) {
00270
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];
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;
00287 }
00288 }
00289
00290 return inda;
00291 }
00292
00293
00294
00295 }
00296
00297 #endif // QGUTILS_H