source: Sophya/trunk/SophyaPI/PIext/pihisto2d.cc@ 3520

Last change on this file since 3520 was 3520, checked in by ansari, 17 years ago

Ajout/codage methode P2DArrayAdapter::MeanVal() pour les classes adapter heritant de P2DArrayAdapter, Reza 11/09/2008

File size: 33.8 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <pisysdep.h>
4#include PIAPP_H
5
6#include <iostream>
7#include <math.h>
8
9#include "sopnamsp.h"
10#include "pihisto2d.h"
11#include "srandgen.h"
12
13static int dbg = 0;
14
15
16//------ Implementation classe P2DHistoWrapper
17P2DHistoWrapper::P2DHistoWrapper(int_4 asx, int_4 asy)
18 : P2DArrayAdapter(asx, asy) ,
19 mScale(1.) , mOff(0.) , mRetFg(0)
20{
21}
22
23P2DHistoWrapper::~P2DHistoWrapper()
24{
25}
26
27
28double P2DHistoWrapper::MeanVal(int ix1, int ix2, int jy1, int jy2)
29{
30 int ec;
31 if (ix1>ix2) { ec=ix1; ix1=ix2; ix2=ec; }
32 if (jy1>jy2) { ec=jy1; jy1=jy2; jy2=ec; }
33 double ss = 0.;
34 for(int j=jy1; j<=jy2; j++)
35 for(int i=ix1; i<=ix2; i++) ss += (double)((*this)(i, j));
36 ss /= (double)((jy2-jy1+1)*(ix2-ix1+1));
37 return ss;
38}
39
40int P2DHistoWrapper::GetStatInfoAsText(vector<string> & /* text */ )
41{
42 return 0;
43}
44
45int P2DHistoWrapper::DecodeOptionString(vector<string> & opt, bool rmdecopt)
46{
47 if(opt.size() < 1) return(0);
48
49 vector<string> udopt; // On gardera ici les options non decodees
50 unsigned int k = 0;
51 int ndec = opt.size();
52 for( k=0; k<opt.size(); k++ ) {
53 string & opts = opt[k];
54 if(opts.substr(0,7) == "hs1") {
55 mScale = 1.; mOff = 0.;
56 }
57 else if(opts.substr(0,7) == "hscale=") {
58 mScale = atof(opts.substr(7).c_str());
59 }
60 else if(opts.substr(0,8) == "hoffset=") {
61 mOff = atof(opts.substr(8).c_str());
62 }
63 else if(opts.substr(0,8) == "hbincont") {
64 mRetFg = 0;
65 }
66 else if(opts.substr(0,7) == "hbinerr") {
67 mRetFg = 1;
68 }
69 else if(opts.substr(0,7) == "hbinent") {
70 mRetFg = 2;
71 }
72 else {
73 ndec--;
74 // S'il faut supprimer les options decodees
75 if (rmdecopt) udopt.push_back(opts);
76 }
77 }
78 // S'il faut supprimer les options decodees, on remplace l'argument opt
79 // par le vecteur des options non decodees.
80 if (rmdecopt) opt = udopt;
81 return(ndec);
82}
83
84int
85P2DHistoWrapper::OptionToString(vector<string> & opt) const
86{
87 char buff[64];
88 sprintf(buff, "hscale=%g", mScale); opt.push_back(buff);
89 sprintf(buff, "hoffset=%g", mOff); opt.push_back(buff);
90 if (mRetFg == 2) opt.push_back("hbinent");
91 else if (mRetFg == 1) opt.push_back("hbinerr");
92 else opt.push_back("hbincont");
93
94 return 1;
95}
96
97//++
98// Class PIHisto2D
99// Lib PIext
100// include pihisto2d.h
101//
102// Classes de dessin des histogrammes a 2 dimensions pour
103// objets *Histo2D*
104//--
105
106//++
107// Links Parents
108// PIDrawer
109//--
110
111//++
112// Titre Constructeur, méthodes
113//--
114
115//++
116PIHisto2D::PIHisto2D(P2DHistoWrapper* histowp, bool ad)
117//
118// Createur d'une classe de dessin pour l'histogramme 2D histo.
119//--
120: PIDrawer(), mHistoWp(histowp), mAdDO(ad), mLogScale(10.), mFPoints(0.5)
121{
122// mAdDO : Flag pour suppression automatique de mHistoWp
123// Attention: mFPoints n'est initialise que si on display par nuages de points
124// mLogScale n'est initialise que si on utilise une echelle log
125UseScale();
126UseColors();
127UseDisplay();
128UseDyn();
129UseFrac();
130SetStats();
131SetStatPosOffset();
132SetName("Histo2DDrw");
133// PIHisto2D has specific control tools
134mFgSpecContWind = true;
135}
136
137//++
138PIHisto2D::~PIHisto2D()
139//
140// Destructeur.
141//--
142{
143// La fonction virtuelle DeactivateControlWindow() doit etre appele
144// a ce niveau - En effet au niveau du destructeur de base, il
145// semble pointer sur la fonction de la classe de base
146// Reza - Octobre 2002
147// Desactivation totale de la fenetre de controle specialise
148// ---> parametre d'appel PIBaseWdgGen* wdg=NULL
149DeactivateControlWindow(NULL);
150if(mAdDO && mHistoWp!=NULL) delete mHistoWp;
151}
152
153//++
154void PIHisto2D::UseColors(bool fg,CMapId cmap,bool revcmap)
155//
156// Choix de la couleur si fg=true avec la color map cmap.
157// (pour la couleur cmap cf picmap.h).
158// Independamment du choix du display, la dynamique est
159// codee sur la color map donnant ainsi une double
160// information. Par exemple, carres de tailles variables
161// en couleur. Cette option est incontournable dans le cas
162// d'un display par des carres de taille fixe.
163// revcmap doit etre mis a "true" si on veut avoir une color map
164// inversee.
165//| -**- gestion dans H2WinArg par menu deroulant Black&White etc...
166//--
167{
168mFgCol = fg; mCmap = cmap; mRevCmap = revcmap;
169}
170
171//++
172void PIHisto2D::UseScale(unsigned short type,double logscale)
173//
174// Pour changer les echelles (lineaire ou logarithmique)
175//| Type = 0 : echelle lineaire
176//| = 1 : echelle log10
177//| -**- Explication du codage en type=0 (lineaire) :
178//| 1. [hmin,hmax] -> [0,1]
179//| h -> f = (h-hmin)/(hmax-hmin)
180//| 2. codage de f=[0,1] sur la dynamique du display choisi
181//| -**- Explication du codage en type=1 (logarithmique base 10) :
182//| 1. map lineaire entre 0 et 1:
183//| [hmin,hmax] -> [0,1]
184//| h -> f = (h-hmin)/(hmax-hmin)
185//| 2. transformation logarithmique de base 10 :
186//| [0,1] -> [0,1]
187//| f -> lf = log10(1.+f*(logscale-1))/log10(logscale)
188//| 3. codage de lf=[0,1] sur la dynamique du display choisi
189//| -**- gestion dans H2WinArg par menu deroulant Lineaire/Log10
190//| et "logscale" par saisie de valeur dans champ LogScal
191//--
192{
193if(type==0) mTypScal=0;
194else if(type==1) {mTypScal=1; if(logscale>1.) mLogScale=logscale;}
195else mTypScal=0;
196}
197
198//++
199void PIHisto2D::UseDisplay(unsigned short type,double fnpt)
200//
201// Type de Display
202//| Type = 0 : carres de tailles variables
203//| Type = 1 : nuages de points
204//| Le nombre de points a utiliser est fnpt*N
205//| ou N est le nombre de pixels ecran contenu
206//| dans un bin de l'histogramme.
207//| Type = 2 : code a la "hbook2" " .+123...9AB...YZ*"
208//| (cf detail PIHisto2D::HPrint2)
209//| Type = 3 : carres de taille fixe (couleur).
210//| -**- gestion dans H2WinArg par menu deroulant Carres_Var etc...
211//| et "fnpt" par saisie de valeur dans champ PerPt
212//--
213{
214if(type==0) mTypDisp = 0;
215else if(type==1) {
216 mTypDisp = 1;
217 if(fnpt<0.) mFPoints = 0.;
218 else if(fnpt>1.) mFPoints = 1.;
219 else mFPoints = fnpt;
220}
221else if(type==2) mTypDisp = 2;
222else if(type==3) mTypDisp = 3;
223else mTypDisp = 1;
224}
225
226//++
227void PIHisto2D::UseDyn(double fmin,double fmax)
228//
229// Gestion de la dynamique a representer:
230//| La dynamique va etre transformee de [hmin,hmax] vers [0,1] selon
231//| [hmin,hmax] -> [0,1]
232//| h -> f = (h-hmin)/(hmax-hmin)
233//| Par la suite, selon ce qui est demande, f va coder le display
234//| ou etre transforme en une autre echelle [0,1] (ex: echelle log10).
235//| Si hmax<=hmin, ils sont forces a la dynamique totale de l'histo2D.
236//| -**- gestion dans H2WinArg par saisie de valeurs dans champ Dyn
237//--
238{
239if( (mHistoWp) && (mHistoWp->NBinX()>0) && (mHistoWp->NBinY()>0) && (fmin>=fmax) ) {
240 double v,hmin,hmax;
241 hmin = hmax = (*mHistoWp)(0,0);
242 for (int_4 i=0; i<mHistoWp->NBinX(); i++)
243 for (int_4 j=0; j<mHistoWp->NBinY(); j++) {
244 v = (*mHistoWp)(i, j);
245 if(v<hmin) hmin = v;
246 if(v>hmax) hmax = v;
247 }
248 fmin = hmin; fmax = hmax;
249}
250
251if(fmin>=fmax) fmax = fmin+1.;
252mHMin = fmin; mHMax = fmax;
253}
254
255//++
256void PIHisto2D::UseFrac(double frmin,double frmax)
257//
258// Pour definir la fraction de la dynamique a dessiner:
259//| Selon le type de display (f=[0,1] cf PIHisto2D::UseDyn),
260//| - on ne dessine rien si f <= frmin dans les cas de display avec
261//| des nuages de points ou des carres de tailles variables.
262//| Pour un display "a la hbook2" on force frmin = 0.
263//| - frmax n'est utilise que pour la representation avec
264//| des carres de tailles variables: c'est la taille
265//| maximum que peut avoir le carre exprimee en unite
266//| de la taille du bin (ex: si frmax=0.8 le carre
267//| le + grand qui pourra etre dessine dans un bin
268//| aura une taille egale a 0.8*(taille du bin)).
269//| -**- gestion dans H2WinArg par saisie de valeurs dans champ Frac
270//--
271{
272if(frmin<0. || frmin>=1.) frmin = 0.;
273if(frmax<=0. || frmax>1. ) frmax = 1.;
274if(frmin>=frmax) {frmin=0.1; frmax=0.9;}
275mFracMin = frmin; mFracMax = frmax;
276}
277
278//++
279void PIHisto2D::Print(int lp)
280//
281// Print de l'etat des options du display.
282//--
283{
284printf("PIHisto2D::Print FgCol=%d Cmap=%d (Rev=%d) TypScal=%d TypDisp=%d (FPoints=%g)\n"
285 ,(int)mFgCol,(int)mCmap,(int)mRevCmap,mTypScal,mTypDisp,mFPoints);
286printf(" Dyn=%g,%g Frac=%g,%g LogSc=%g H=%p\n"
287 ,mHMin,mHMax,mFracMin,mFracMax,mLogScale,mHistoWp);
288fflush(stdout);
289}
290
291//++
292void PIHisto2D::UpdateLimits()
293//
294// Definition des tailles graphiques en fonction
295// des caracteristiques de l'histogramme a dessiner.
296//--
297{
298 if(!mHistoWp) return;
299 SetLimits(mHistoWp->XMin(), mHistoWp->XMax(), mHistoWp->YMin() , mHistoWp->YMax());
300}
301
302//++
303void PIHisto2D::ShowControlWindow(PIBaseWdgGen* wdg)
304//
305// Affichage de la fenetre de controle H2WinArg
306//--
307{
308 H2WinArg::SetCurrentPIHisto2D(this);
309 H2WinArg::ShowPIHisto2DTools(wdg);
310}
311
312//++
313void PIHisto2D::DeactivateControlWindow(PIBaseWdgGen* wdg)
314//
315// Desactivation de la fenetre de controle specialisee
316//--
317{
318 if (H2WinArg::GetCurrentPIHisto2D() == this) {
319 // si wdg != NULL, c'est un Detach (Drawer detache du PIBaseWdg
320 // si wdg == NULL, c'est un delete du PIHisto2D (du PIDrawer)
321 if ((wdg == NULL) || (H2WinArg::GetCurrentBaseWdg() == wdg)) {
322 H2WinArg::SetCurrentBaseWdg(NULL);
323 H2WinArg::SetCurrentPIHisto2D(NULL);
324 H2WinArg::HidePIHisto2DTools();
325 }
326 }
327 PIDrawer::DeactivateControlWindow(wdg);
328 return;
329}
330
331//++
332void PIHisto2D::Draw(PIGraphicUC* g,double xmin,double ymin,double xmax,double ymax)
333//
334// Dessin de l'histogramme.
335//| -**- Code de dessin selon choix des options:
336//| (detail voir UseColors UseScale UseDisplay UseDyn UseFrac)
337//| - [hmin,hmax] -> f=[0,1]
338//| (Choix hmin,hmax champ Dyn de H2WinArg)
339//| - Eventuellement ech Log -> re-codage log10 entre f=[0,1]
340//| (Choix menu deroulant et champ LogScal de H2WinArg)
341//| - Restriction de f=[0,1] -> f=[Frac(min),Frac(max)]
342//| (Choix champ Frac de H2WinArg)
343//| -**- Puis selon display:
344//| 0 carres variables, menu "Carres Var." de H2WinArg:
345//| if(f>Frac(min)) taille carre = f * Frac(max) * taille_du_bin
346//| 1 nuage de points, menu "....." et champ PerPt de H2WinArg:
347//| if(f>Frac(min)) npoints = f * PerPt * npoints_ecran_dans_bin
348//| 2 code hbook2, menu ".12..Z*" de H2WinArg:
349//| if(f>0) map de f=]0,1] dans ".+...Z*"
350//| 3 carres pleins, menu "Carres Pleins" et couleurs de H2WinArg):
351//| couleur = lut[ f * nombre_d_entree_dans_la_lut ]
352//--
353{
354if (axesFlags != kAxesNone) DrawAxes(g);
355
356if(!mHistoWp) return;
357if(mHistoWp->NBinX()<=0 || mHistoWp->NBinY()<=0) return;
358
359// Caracteristiques histogramme
360double dx = mHistoWp->WBinX(),dy = mHistoWp->WBinY();
361double p1dx,p1dy;
362g->DGrC2UC(1.,1.,p1dx,p1dy);
363
364// Gamme a representer entre [0,1] mais >=fracmin et scale fracmax
365double fracmin=FMin(), fracmax=FMax();
366double llscale = log10(LogScale());
367
368// gestion Couleurs.
369PIColors fgcoul = GetGraphicAtt().GetFgColor();
370PIColors bgcoul = GetGraphicAtt().GetBgColor();
371PIColorMap* cmap=NULL;
372int ncol = 0;
373if (mFgCol) {
374 cmap = new PIColorMap(mCmap);
375 cmap->ReverseColorIndex(mRevCmap);
376 ncol = cmap->NCol();
377 if(mTypDisp==3) fracmin=-1.;
378}
379
380// gestion epaisseur de ligne
381PILineAtt LineAtt = GetGraphicAtt().GetLineAtt();
382if(LineAtt == PI_NotDefLineAtt) GetGraphicAtt().SetLineAtt(PI_ThinLine);
383
384// gestion Markers ou plot avec des points.
385PIMarker Mk = GetGraphicAtt().GetMarker();
386int MkSz = GetGraphicAtt().GetMarkerSize();
387int npt = 1;
388if(mTypDisp==1) {
389 g->SelMarker(1,PI_DotMarker);
390 npt = (int)(NPixBin(g)*FPoints()); if(npt<=0) npt = 2;
391}
392
393// gestion Font.
394PIFontAtt FontAtt = GetGraphicAtt().GetFontAtt();
395int FontSize = GetGraphicAtt().GetFontSzPt();
396if(mTypDisp==2) {
397 double dxg,dyg,dg;
398 g->DUC2GrC(dx,dy,dxg,dyg);
399 dg =(dxg<dyg) ? dxg : dyg;
400 int npix = (int) (dg*0.9); if(npix<8) npix = 8;
401 g->SelFontSzPt(npix,FontAtt);
402 fracmin = 0;
403}
404
405// Plot de l'histogramme
406for(int i=0; i<mHistoWp->NBinX(); i++)
407 for(int j=0; j<mHistoWp->NBinY(); j++) {
408
409 r_8 left0,bottom0;
410 mHistoWp->BinLowEdge(i,j,left0,bottom0);
411
412 // Gestion de la dynamique a dessiner
413 double frac = ((*mHistoWp)(i,j)-HMin())/(HMax()-HMin());
414 if(frac<0.) continue;
415 if(mTypScal==1) { // echelle log10
416 frac = log10(1.+frac*(LogScale()-1.))/llscale;
417 if(frac<0.) continue;
418 }
419 if(frac<=fracmin) continue;
420 if(frac>1.) frac = 1.;
421 double fracred = frac * fracmax;
422
423 // Gestion de la couleur
424 int icol = 0;
425 if (cmap) {
426 icol = int(ncol*frac);
427 if(icol>=ncol) icol = ncol-1; else if(icol<0) icol=0;
428 g->SelForeground(*cmap,icol);
429 }
430
431// Pour ne pas dessiner en dehors des axes
432 if ( (left0+dx/2. < xmin) || (left0+dx/2. > xmax) ||
433 (bottom0+dy/2. < ymin) || (bottom0+dy/2. > ymax) ) continue;
434
435 // Dessin proprement dit selon le choix graphique.
436 if(mTypDisp==0) {
437 //..... carres de tailles variables
438 double left = left0 + 0.5*(1.-fracred)*dx, width = fracred*dx;
439 double bottom = bottom0 + 0.5*(1.-fracred)*dy, height = fracred*dy;
440 if (cmap) g->DrawFBox(left,bottom,width,height);
441 else g->DrawBox(left,bottom,width,height);
442 } else if(mTypDisp==1) {
443 //..... nuage de points .....
444 int ipt = int(npt *frac);
445 for(int k=0;k<ipt;k++) {
446 double x = left0 + frand01()*dx;
447 double y = bottom0 + frand01()*dy;
448 g->DrawMarker(x,y);
449 }
450 } else if(mTypDisp==2) {
451 //..... type hbook2/hprint .+23-Z*
452 char c[2];
453 c[0] = HPrint2(frac); c[1]='\0';
454 double x = left0 + dx/2.;
455 double y = bottom0 + dy/2.;
456 g->DrawString(x,y,c,PI_HorizontalCenter|PI_VerticalCenter);
457 } else if(mTypDisp==3) {
458 //..... carres de tailles fixes (avec gestion de continuite)
459 if (cmap) g->DrawFBox(left0,bottom0,dx+p1dx,dy+p1dy);
460 else g->DrawBox(left0,bottom0,dx+p1dx,dy+p1dy);
461 }
462
463}
464
465// Remise dans les conditions ulterieures pour la suite du graphique.
466GetGraphicAtt().SetMarkerAtt(MkSz,Mk);
467GetGraphicAtt().SetColAtt(fgcoul,bgcoul);
468g->SelFontSzPt(FontSize,FontAtt);
469GetGraphicAtt().SetLineAtt(LineAtt);
470if (cmap) delete cmap;
471
472// Fin du dessin, ecriture de la statistique.
473if(stats) DrawStats(g);
474}
475
476//++
477void PIHisto2D::GetClickInfo(string& info,double x,double y,double x0,double y0,bool fgdiff)
478//
479// Info specifique du drawer pour la position x,y
480//--
481{
482P2DHistoWrapper* h = HistoWrapper();
483if(h == NULL) return;
484
485int_4 i,j;
486i = floor((x-h->XMin())/h->WBinX());
487j = floor((y-h->YMin())/h->WBinY());
488
489if(i>=0 && i<h->NBinX() && j>=0 && j<h->NBinY()) {
490 char str[64];
491 if(fgdiff) {
492 int i0,j0;
493 i0 = floor((x0-h->XMin())/h->WBinX());
494 j0 = floor((y0-h->YMin())/h->WBinY());
495 if(i0>=0 && i0<h->NBinX() && j0>=0 && j0<h->NBinY()) {
496 sprintf(str," DV=%g",(*h)(i,j)-(*h)(i0,j0));
497 info += str;
498 } else {
499 info += " DV=?";
500 }
501 }
502 sprintf(str," V=%g",(*h)(i,j));
503 info += str;
504} else {
505 info += " V=?";
506}
507
508return;
509}
510
511//++
512void PIHisto2D::DrawStats(PIGraphicUC* g)
513//
514// Dessin des informations statistiques de l'histogramme.
515//--
516{
517 if (!mHistoWp) return;
518 if (GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
519 g->SelFontSz((YMax() - YMin())/30);
520
521 // La hauteur de la cellule
522 PIGrCoord a, d;
523 double cH = (double)g->GetFontHeight(a,d);
524 vector<string> lines;
525 int nlig = mHistoWp->GetStatInfoAsText(lines);
526 if (nlig < 1) return;
527
528 double cellHeight = nlig*1.2 * cH;
529
530 int idxll = 0;
531
532 int kl;
533 // on recherche la ligne la plus longue
534 for(kl=1; kl<nlig; kl++)
535 if ( lines[kl].length() > lines[idxll].length() ) idxll = kl;
536
537 double cellWidth = 1.1 * (double)g->CalcStringWidth(lines[idxll].c_str());
538
539
540 double ofpx = spoX*(XMax()-XMin());
541 double ofpy = spoY*(YMax()-YMin());
542
543 double xu, yu, cw;
544 // Les limites du cadre
545 xu = g->DeltaUCX(XMax(), -cellWidth);
546 yu = g->DeltaUCY(YMax(), -cellHeight);
547 double recw = XMax()-xu;
548 double rech = YMax()-yu;
549 xu += ofpx; yu += ofpy;
550 g->DrawBox(xu, yu, recw, rech);
551
552 // L'ecriture des labels
553 cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
554 xu = g->DeltaUCX(XMax(),cw);
555
556 cw = (g->isAxeYDirUpDown()) ? -0.15*cH : -1.15*cH;
557 yu = g->DeltaUCY(YMax(),cw);
558 xu += ofpx; yu += ofpy;
559
560 for(kl=0; kl<nlig; kl++) {
561 g->DrawString(xu, yu, lines[kl].c_str() );
562 cw += -1.15*cH;
563 yu = g->DeltaUCY(YMax(),cw); yu += ofpy;
564 }
565
566
567 // printf("H[%d,%d] Dynamique: [%g,%g] Frac [%g,%g]\n"
568 // ,mHistoWp->NBinX(),mHistoWp->NBinY(),HMin(),HMax(),FMin(),FMax());
569}
570
571//++
572char PIHisto2D::HPrint2(double f)
573//
574// Codage des valeurs en caracteres (fct privee).
575//| f entre [0,1] mappee entre valeur=[0,37]
576//| si <0 alors =0, si >1 alors 1
577//| Display 4 ==> 4<=valeur<5
578//| C ==> 12<=valeur<13
579//| ==> valeur<=0
580//| * ==> valeur>=1
581//| . ==> 0<valeur<1
582//|------------------------------------------
583//| 1 2 3
584//| 01234567890123456789012345678901234567
585//| .+23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*
586//|------------------------------------------
587//--
588{
589char str[39] = " .+23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*";
590int i;
591if(f<=0.) i = 0;
592else if(f>=1.) i = 37;
593else { i = (int) (f*36.); i++;}
594if(i<0) i=0; else if (i>=38) i = 37;
595return str[i];
596}
597
598//++
599int PIHisto2D::NPixBin(PIGraphicUC* g)
600//
601// Nombre de pixels ecran dans un bin d'histogramme
602// (fct privee).
603//--
604{
605double dx = mHistoWp->WBinX(),dy = mHistoWp->WBinY();
606double dxg,dyg;
607g->DUC2GrC(dx,dy,dxg,dyg);
608int np = (int) dxg * (int) dyg;
609//printf("PIHisto2D::NPixBin H dx=%g dy=%g, G dx=%g dy=%g, np = %d\n"
610// ,dx,dy,dxg,dyg,np);
611return np;
612}
613
614//++
615int PIHisto2D::DecodeOptionString(vector<string> & opt, bool rmdecopt)
616//
617// Decodage des options
618//--
619{
620 // Decodage des options generales pidrawer
621 int optsz1 = opt.size();
622 if(optsz1<1) return(0);
623 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
624 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
625
626 // Options generales pidrawer interessant le display Histo2D
627 bool rev;
628 if(GetGraphicAtt().GetColMapId(rev) != CMAP_OTHER) {
629 UseColors(true,GetGraphicAtt().GetColMapId(),rev);
630 } else UseColors(false);
631
632 // Decodage des options propres au display Histo2D
633 vector<string> udopt; // On gardera ici les options non decodees
634 unsigned int k = 0;
635 int ndec = opt.size();
636 bool listopt=false;
637 for( k=0; k<opt.size(); k++ ) {
638 string opts = opt[k];
639 if(opts=="h2help") {
640 string info; GetOptionsHelpInfo(info);
641 size_t q = info.find("PIHisto2D");
642 if(q<info.length()-1) cout<<info.substr(q)<<endl;
643 } else if(opts=="h2list") {
644 listopt=true;
645 } else if(opts=="sta" || opts=="stat" || opts=="stats") {
646 SetStats(true);
647 } else if( opts=="nsta" || opts=="nstat"
648 || opts=="nostat" || opts=="nostats") {
649 SetStats(false);
650 } else if(opts.substr(0,11) == "statposoff=") {
651 double xo=0., yo=0.;
652 sscanf(opts.substr(11).c_str(),"%lf,%lf",&xo, &yo);
653 SetStatPosOffset(xo, yo);
654 } else if(opts.substr(0,8)=="h2scale=") {
655 unsigned short t=TypScale(); double ls=LogScale();
656 if(opts.substr(8,3)=="lin") t=0;
657 else if(opts.substr(8,3)=="log")
658 {t=1; sscanf(opts.c_str(),"h2scale=log,%lf",&ls);}
659 UseScale(t,ls);
660 } else if(opts.substr(0,7)=="h2disp=") {
661 unsigned short t=TypDisplay(); double fpts=FPoints();
662 if(opts.substr(7,3)=="var") t=0;
663 else if(opts.substr(7,3)=="hbk") t=2;
664 else if(opts.substr(7,3)=="img") t=3;
665 else if(opts.substr(7,3)=="pts")
666 {t=1; sscanf(opts.c_str(),"h2disp=pts,%lf",&fpts);}
667 UseDisplay(t,fpts);
668 } else if(opts.substr(0,6)=="h2dyn=") {
669 double hmin=HMin(),hmax=HMax(); size_t q = opts.find(',');
670 sscanf(opts.c_str(),"h2dyn=%lf",&hmin);
671 if(q<opts.length()-1) sscanf(opts.substr(q+1).c_str(),"%lf",&hmax);
672 UseDyn(hmin,hmax);
673 } else if(opts.substr(0,7)=="h2frac=") {
674 double fmin=FMin(),fmax=FMax(); size_t q = opts.find(',');
675 sscanf(opts.c_str(),"h2frac=%lf",&fmin);
676 if(q<opts.length()-1) sscanf(opts.substr(q+1).c_str(),"%lf",&fmax);
677 UseFrac(fmin,fmax);
678 } else {
679 ndec--;
680 // S'il faut supprimer les options decodees
681 if (rmdecopt) udopt.push_back(opts);
682 }
683 }
684
685 // S'il faut supprimer les options decodees, on remplace l'argument opt
686 // par le vecteur des options non decodees.
687 if (rmdecopt) opt = udopt;
688
689 // Decodage des options par le histo-wrapper
690 int ndec2 = 0;
691 if ( ( optsz1-ndec1-ndec > 0) && (mHistoWp) ) {
692 ndec2 = mHistoWp->DecodeOptionString(opt, rmdecopt);
693 }
694
695 // Liste des options si demande
696 if(listopt) Print();
697
698 return(ndec+ndec1+ndec2);
699}
700
701int PIHisto2D::OptionToString(vector<string> & opt) const
702{
703 PIDrawer::OptionToString(opt);
704 char str[256];
705
706 if(stats) opt.push_back("stat"); else opt.push_back("nstat");
707
708 sprintf(str,"statposoff=%f,%f",spoX,spoY);
709 opt.push_back(str);
710
711 if(mTypDisp==0) sprintf(str,"h2disp=var");
712 else if(mTypDisp==1) sprintf(str,"h2disp=pts,%g",mFPoints);
713 else if(mTypDisp==2) sprintf(str,"h2disp=hbk");
714 else if(mTypDisp==3) sprintf(str,"h2disp=img");
715 opt.push_back(str);
716
717 if(mTypScal==0) sprintf(str,"h2scale=lin");
718 else if(mTypScal==1) sprintf(str,"h2disp=log,%g",mLogScale);
719 opt.push_back(str);
720
721 sprintf(str,"h2dyn=%g,%g",mHMin,mHMax);
722 opt.push_back(str);
723
724 sprintf(str,"h2frac=%g,%g",mFracMin,mFracMax);
725 opt.push_back(str);
726
727 if(mRevCmap) opt.push_back("revcmap");
728
729 // Les options du Histo2DWrapper :
730 if(mHistoWp) mHistoWp->OptionToString(opt);
731 return 1;
732}
733
734
735//++
736void PIHisto2D::GetOptionsHelpInfo(string& info)
737//
738// Help relatif au options
739//--
740{
741info += " ---- PIHisto2D options help info (see also ALT-O): \n" ;
742info += "- h2help: get this help text\n";
743info += "- h2list: list choosen options\n";
744info += "- sta,stat,stats: activate statistic display\n";
745info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
746info += "- h2disp=typ[,fracpts]: choose display type\n";
747info += " typ=var: variable size boxes\n";
748info += " typ=hbk: \"a la hbook2\"\n";
749info += " typ=img: image like (use \"h2col\" for color map)\n";
750info += " typ=pts: point clouds (fracpts=max possible fraction\n";
751info += " of used pixels per bin [0,1])\n";
752info += "- h2scale=lin/log[,logscale]: choose linear or logarithmic scale\n";
753info += "- h2dyn=[hmin][,hmax]: choose histogramme range for display\n";
754info += "- use graphic att. to define color table (ex: grey32,midas_heat,...)\n";
755info += " (see general graphicatt description)\n";
756info += "- use \"revcmap\" to reverse color table\n";
757info += "- h2frac=[fmin][,fmax]: choose sub-range display [0,1]\n";
758info += " ---- HistoWrapper options : \n" ;
759info += " hbincont: select bin content as Y value for display (default) \n";
760info += " hbinerr: select bin error as Y value for display \n";
761info += " hbinent: select bin entries as Y value for display \n";
762info += " hscale=value : multiplicative factor for Y value \n" ;
763info += " hoffset=value : additive coefficient for Y value \n" ;
764info += " hs1: set hscale=1 hoffset=0 (default) \n" ;
765// On recupere ensuite la chaine info de la classe de base
766PIDrawer::GetOptionsHelpInfo(info);
767return;
768}
769
770/////////////////////////////////////////////////////////////////
771// Classe H2WinArg
772/////////////////////////////////////////////////////////////////
773//++
774// Class H2WinArg
775// Lib PIext
776// include pihisto2d.h
777//
778// Fenêtre de dialogue pour le choix des options de tracé pour "PIHisto2D"
779// Classe de fenêtre de dialogue permettant de modifier interactivement
780// Les différents attributs de visualisation pour les *PIImage* .
781//--
782//++
783// Links Parents
784// PIWindow
785//--
786//++
787// Links Voir aussi
788// PIHisto2D
789// PIH2DWdg
790//--
791
792//++
793// Titre Constructeur, méthodes
794//--
795
796PIBaseWdgGen* H2WinArg::mBWdg = NULL;
797PIHisto2D* H2WinArg::mH2DDrw = NULL;
798static H2WinArg* cur_h2winarg = NULL;
799
800void H2WinArg::ShowPIHisto2DTools()
801{
802 if (cur_h2winarg == NULL) cur_h2winarg = new H2WinArg(PIApplicationGetApp());
803 cur_h2winarg->Show();
804}
805
806void H2WinArg::ShowPIHisto2DTools(PIBaseWdgGen* cbw)
807{
808 if (cur_h2winarg == NULL) cur_h2winarg = new H2WinArg(PIApplicationGetApp());
809 mBWdg = cbw;
810 cur_h2winarg->Show();
811}
812
813void H2WinArg::HidePIHisto2DTools()
814{
815 if (cur_h2winarg != NULL) cur_h2winarg->Hide();
816}
817
818void H2WinArg::SetCurrentBaseWdg(PIBaseWdgGen* cbw)
819{
820 mBWdg = cbw;
821}
822
823void H2WinArg::SetCurrentPIHisto2D(PIHisto2D* h2ddrw)
824{
825 mH2DDrw = h2ddrw;
826}
827
828PIBaseWdgGen* H2WinArg::GetCurrentBaseWdg()
829{
830 return(mBWdg);
831}
832
833PIHisto2D* H2WinArg::GetCurrentPIHisto2D()
834{
835 return(mH2DDrw);
836}
837
838//++
839H2WinArg::H2WinArg(PIMsgHandler* par)
840//
841// Creation de la fenetre de gestion des parametres
842// des dessins des histogrammes 2D. Cette fenetre de
843// dialogue est partagee par tous les widget de dessin
844// des histogrammes 2D. Pour faire apparaitre la fenetre
845// tapez ALT-O.
846//--
847//++
848//| - Menu 1: Choix du type de display
849//| Carres variables, nuages de points, caracteres a la hbook2
850//| et carres de tailles fixe (couleur ou niveauz de gris).
851//| - Menu 2: Choix du type d'echelle
852//| Lineaire ou logarithmique
853//| - Menu 3: Choix de la couleur
854//| noir et blanc, niveau de gris et couleurs diverses.
855//| - Champ texte Dyn: Pour donner la dynamique, si min>=max
856//| alors prend le min et le max de l'histogramme
857//| (cf PIHisto2D::UseDyn)
858//| - Champ texte Frac: fraction mini et maxi
859//| (cf PIHisto2D::UseFrac)
860//| - Champ texte LogScal: niveau de scaling pour le choix d'une
861//| echelle logarithmique (cf PIHisto2D::UseScale)
862//--
863//++
864//| - Curseur interactif PerPt: pourcentage de points a dessiner
865//| dans chaque bin (cf PIHisto2D::UseDisplay)
866//| - Bouton Apply: dessiner avec les options affichees
867//| - Bouton Dismiss: fermeture de la fenetre de dialogue.
868//| - Bouton Get: re-prendre les valeurs de display stoquees
869//| pour un histogramme donne.
870//| - Bouton Print: Imprimer les caracteristiques du display
871//| et de l'histogramme.
872//--
873: PIWindow((PIMsgHandler *)par,"H2D-Options",PIWK_dialog,250,260,150,150)
874, mFgCol(false), mCmap(CMAP_GREYINV32), mRevCmap(false)
875, mTypScal(0) , mLogScale(10.)
876, mTypDisp(0) , mFPoints(0.5)
877, mHMin(1.) , mHMax(-1.)
878, mFracMin(0.1), mFracMax(0.9)
879{
880if(dbg) printf("H2WinArg::H2WinArg %p par=%p\n",this,par);
881
882// Taille automatique
883int bsx, bsy;
884PIApplicationPrefCompSize(bsx, bsy); // environ 6 lettres
885int spx = (bsx>=10) ? bsx/10 : 1; // intervalle entre lettres X
886int spy = (bsy>=5) ? bsy/5 : 1; // intervalle entre lettres Y
887int wszx = 5*spx+bsx+int(2.5*bsx); // Taille fenetre en X
888int wszy = 11*spy+int(8.5*bsy); // Taille fenetre en Y
889SetSize(wszx, wszy);
890
891// menu du style de display des bins
892 int cpx = 2*spx, cpy = 2*spy;
893mOPop[0] = new PIOptMenu(this,"optmen-h2d-1",2*bsx,bsy,cpx,cpy);
894mOPop[0]->AppendItem("Carres Var." ,6101);
895mOPop[0]->AppendItem("....." ,6102);
896mOPop[0]->AppendItem(".+12..Z*" ,6103);
897mOPop[0]->AppendItem("Carres Pleins",6104);
898mOPop[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
899
900// Menu du choix de la dynamique
901 cpy += bsy+spy;
902mOPop[1] = new PIOptMenu(this,"optmen-h2d-2",2*bsx,bsy,cpx,cpy);
903mOPop[1]->AppendItem("Lineaire",6201);
904mOPop[1]->AppendItem("Log10" ,6202);
905mOPop[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
906
907// Menu du choix de la table des couleurs
908 cpy += bsy+spy;
909mOPop[2] = new PIOptMenu(this,"optmen-h2d-3",2*bsx,bsy,cpx,cpy);
910mOPop[2]->AppendItem("Black&White",7000);
911mCasc[0] = new PIMenu(mOPop[2]->Menu(),"PIStd-128Col");
912mCasc[1] = new PIMenu(mOPop[2]->Menu(), "PIUniCol32");
913mCasc[2] = new PIMenu(mOPop[2]->Menu(),"MIDAS-CMap");
914int kcc,nsct1=5,nsct2=9,nsct3=17,nsct4=PIColorMap::NumberStandardColorMaps()-2;
915for(kcc=0; kcc<nsct1; kcc++)
916 mOPop[2]->AppendItem(PIColorMap::GetStandardColorMapName(kcc).c_str(),7001+kcc);
917for(kcc=nsct1; kcc<nsct2; kcc++)
918 mCasc[0]->AppendItem(PIColorMap::GetStandardColorMapName(kcc).c_str(),7001+kcc);
919mOPop[2]->AppendPDMenu(mCasc[0]);
920for(kcc=nsct2; kcc<nsct3; kcc++)
921 mCasc[1]->AppendItem(PIColorMap::GetStandardColorMapName(kcc).c_str(),7001+kcc);
922mOPop[2]->AppendPDMenu(mCasc[1]);
923for(kcc=nsct3; kcc<nsct4; kcc++)
924 mCasc[2]->AppendItem(PIColorMap::GetStandardColorMapName(kcc).c_str(),7001+kcc);
925mOPop[2]->AppendPDMenu(mCasc[2]);
926for(kcc=nsct4; kcc<PIColorMap::NumberStandardColorMaps(); kcc++)
927 mOPop[2]->AppendItem(PIColorMap::GetStandardColorMapName(kcc).c_str(),7001+kcc);
928//mOPop[2]->SetValue(7000);
929mOPop[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
930
931// Reverse color map
932 cpy += bsy+spy;
933mCkb = new PICheckBox(this,"Reverse CMap",8001,2*bsx,bsy,cpx,cpy);
934mCkb->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
935
936// Labels et zones de saisie texte
937 cpy += bsy+spy;
938mLab[0] = new PILabel(this," Dyn: ",bsx,bsy,cpx,cpy);
939mLab[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
940mText[0] = new PIText(this,"Dynamique",int(2.5*bsx),bsy,cpx+bsx+spx,cpy);
941mText[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
942 cpy += bsy+spy;
943mLab[1] = new PILabel(this," Frac: ",bsx,bsy,cpx,cpy);
944mLab[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
945mText[1] = new PIText(this,"Fraction",int(2.5*bsx),bsy,cpx+bsx+spx,cpy);
946mText[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
947 cpy += bsy+spy;
948mLab[2] = new PILabel(this," LogScal: ",bsx,bsy,cpx,cpy);
949mLab[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
950mText[2] = new PIText(this,"LogScale",int(2.5*bsx),bsy,cpx+bsx+spx,cpy);
951mText[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
952
953// Labels et curseur mobile
954cpy += bsy+spy;
955mLab[3] = new PILabel(this," PerPt: ",bsx,bsy,cpx,cpy+int(0.25*bsy));
956mLab[3]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
957mPScal = new PIScale(this,"FracPoints",6401,kSDirLtoR
958 ,int(2.5*bsx),int(1.25*bsy),cpx+bsx+spx,cpy);
959mPScal->SetMinMax(0,100);
960mPScal->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
961
962//
963SetText();
964//
965
966// Boutons
967 cpx = 2*bsx+5*spx, cpy = 2*spy;
968mBut[0] = new PIButton(this,"Apply",6001,bsx,bsy,cpx,cpy);
969mBut[0]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
970 cpy += bsy+spy;
971mBut[1] = new PIButton(this,"Dismiss",6002,bsx,bsy,cpx,cpy);
972mBut[1]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
973 cpy += bsy+spy;
974mBut[2] = new PIButton(this,"Get",6003,bsx,bsy,cpx,cpy);
975mBut[2]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
976 cpy += bsy+spy;
977mBut[3] = new PIButton(this,"Print",6004,bsx,bsy,cpx,cpy);
978mBut[3]->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
979// FinishCreate();
980}
981
982//++
983H2WinArg::~H2WinArg()
984//
985// Destructeur.
986//--
987{
988int i;
989if(dbg) printf("H2WinArg::~H2WinArg %p\n",this);
990for(i=0;i<3;i++) delete mCasc[i];
991for(i=0;i<3;i++) delete mOPop[i];
992for(i=0;i<4;i++) delete mBut[i];
993for(i=0;i<4;i++) delete mLab[i];
994for(i=0;i<3;i++) delete mText[i];
995delete mCkb;
996delete mPScal;
997}
998
999//++
1000void H2WinArg::Show()
1001//
1002// Initialisation sur ouverture ALT-O
1003//--
1004{
1005 if(dbg) printf("H2WinArg::Show() mH2DDrw=%p\n",mH2DDrw);
1006 // Pour recuperer les valeurs du Drawer sur lequel on fait ALT-O
1007 PIWindow::Show();
1008 return;
1009}
1010
1011//++
1012void H2WinArg::SetText()
1013//
1014// Gestion des fenetres de saisie de texte et des pop-menus.
1015//--
1016{
1017if(dbg) printf("H2WinArg::SetText()\n");
1018string sdum;
1019char str[256];
1020
1021sprintf(str,"%g %g",mHMin,mHMax);
1022mText[0]->SetText(str);
1023
1024sprintf(str,"%g %g",mFracMin,mFracMax);
1025mText[1]->SetText(str);
1026
1027sprintf(str,"%g",mLogScale);
1028mText[2]->SetText(str);
1029if(mTypScal==1) mText[2]->SetSensitive();
1030 else mText[2]->SetUnSensitive();
1031
1032if(mTypDisp==0) {sdum="Carres Var."; mOPop[0]->SetValueStr(sdum);}
1033else if(mTypDisp==1) {sdum="....."; mOPop[0]->SetValueStr(sdum);}
1034else if(mTypDisp==2) {sdum=".+12..Z*"; mOPop[0]->SetValueStr(sdum);}
1035else if(mTypDisp==3) {sdum="Carres Pleins"; mOPop[0]->SetValueStr(sdum);}
1036
1037if(mTypScal==0) {sdum="Lineaire"; mOPop[1]->SetValueStr(sdum);}
1038else if(mTypScal==1) {sdum="Log10"; mOPop[1]->SetValueStr(sdum);}
1039
1040if(!mFgCol) {mOPop[2]->SetValue(7000);}
1041else {
1042 for(int kk=0;kk<PIColorMap::NumberStandardColorMaps();kk++)
1043 if(mCmap == PIColorMap::GetStandardColorMapId(kk))
1044 {mOPop[2]->SetValue(7001+kk); break;}
1045}
1046mCkb->SetState(mRevCmap);
1047
1048mPScal->SetValue(int(mFPoints*100.));
1049if(mTypDisp==1) mPScal->SetSensitive();
1050 else mPScal->SetUnSensitive();
1051}
1052
1053//++
1054void H2WinArg::Process(PIMessage msg, PIMsgHandler* sender, void*)
1055//
1056// Gestions des messages.
1057//--
1058{
1059if(dbg) printf("H2WinArg::Process(%d-%d , %p ...) \n"
1060 ,(int)UserMsg(msg),(int)ModMsg(msg),sender);
1061
1062// if(!mH2Wdg) return;
1063if(!mBWdg) return;
1064// PIHisto2D* mpih = mH2Wdg->GetPIHisto();
1065PIHisto2D* mpih = mH2DDrw;
1066if(!mpih) return;
1067
1068int opt = UserMsg(msg);
1069
1070 if(opt == 6101) {mTypDisp = 0;}
1071else if(opt == 6102) {mTypDisp = 1;}
1072else if(opt == 6103) {mTypDisp = 2;}
1073else if(opt == 6104) {mTypDisp = 3;}
1074
1075else if(opt == 6201) {mTypScal = 0;}
1076else if(opt == 6202) {mTypScal = 1;}
1077
1078else if(opt == 7000) {mFgCol = false;}
1079else if(opt >= 7001 && opt <8000) {
1080 int k = opt-7001;
1081 mFgCol = true;
1082 mCmap = PIColorMap::GetStandardColorMapId(k);
1083}
1084
1085else if(opt == 8001) mRevCmap = mCkb->GetState();
1086
1087else if(opt == 6401) mFPoints = mPScal->GetValue()/100.;
1088
1089else if(opt==6001) {
1090 sscanf(mText[0]->GetText().c_str(),"%lf %lf",&mHMin,&mHMax);
1091 sscanf(mText[1]->GetText().c_str(),"%lf %lf",&mFracMin,&mFracMax);
1092 sscanf(mText[2]->GetText().c_str(),"%lf",&mLogScale);
1093 mpih->UseColors(mFgCol,mCmap,mRevCmap);
1094 mpih->UseScale(mTypScal,mLogScale);
1095 mpih->UseDisplay(mTypDisp,mFPoints);
1096 mpih->UseDyn(mHMin,mHMax);
1097 mpih->UseFrac(mFracMin,mFracMax);
1098 mBWdg->Refresh(); // On rafraichit le dessin (tout le PIScDrawWdg)
1099}
1100else if(opt==6002) this->Hide();
1101else if (opt==6003) {
1102 mFgCol = mpih->Color(); mCmap = mpih->ColMap();
1103 mRevCmap = mpih->IsColMapRev();
1104 mTypScal = mpih->TypScale(); mLogScale = mpih->LogScale();
1105 mTypDisp = mpih->TypDisplay(); mFPoints = mpih->FPoints();
1106 mHMin = mpih->HMin(); mHMax = mpih->HMax();
1107 mFracMin = mpih->FMin(); mFracMax = mpih->FMax();
1108}
1109else if(opt==6004) mpih->Print(2);
1110
1111SetText();
1112
1113if(dbg) {
1114 printf("H2WinArg::Process opt=%d col=%d,%d,%d scal=%d disp=%d npt=%g\n"
1115 ,opt,(int)mFgCol,(int)mCmap,(int)mRevCmap,mTypScal,mTypDisp,mFPoints);
1116 printf(" min,max= %g,%g frac= %g,%g logsc= %g\n"
1117 ,mHMin,mHMax,mFracMin,mFracMax,mLogScale);
1118}
1119
1120}
Note: See TracBrowser for help on using the repository browser.