source: Sophya/trunk/SophyaPI/PIext/pintuple.cc@ 2524

Last change on this file since 2524 was 2524, checked in by cmv, 22 years ago

OptionToString dans NTuple et NTuple3D cmv 23/03/04

File size: 13.4 KB
Line 
1// Peida Interactive - PI R. Ansari 97-99
2// Traceur (Drawer) pour NTupleInterface
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <iostream>
8#include <math.h>
9#include "pintuple.h"
10
11//++
12// Class PINTuple
13// Lib PIext
14// include pintuple.h
15//
16// Classe de traceur 2D (dans un plan) à partir des données
17// d'un objet implémentant l'interface *NTupleInterface*.
18// Les objets "PINTuple" peuvent tracer des signes (markers)
19// éventuellement avec des barres d'erreur et une étiquette
20// pour chaque point. Si un nom de colonne poids est spécifié,
21// la taille et/ou la couleur des signes (markers) sera fonction
22// de la valeur de poids.
23//--
24//++
25// Links Parents
26// PIDrawer
27//--
28//++
29// Links Voir aussi
30// NTupleInterface
31// PINTuple3D
32//--
33
34//++
35// Titre Constructeur
36//--
37//++
38// PINTuple(NTupleInterface* nt, bool ad)
39// Constructeur. Si "ad == true", l'objet "nt" est détruit par
40// le destructeur de l'objet "PINTuple"
41// Note : "nt" doit être créé par new dans ce cas.
42//--
43
44/* --Methode-- */
45PINTuple::PINTuple(NTupleInterface* nt, bool ad)
46: PIDrawer()
47{
48 mNT = nt;
49 mAdDO = ad;
50 SetStats(true);
51 SetStatPosOffset();
52 ConnectPoints(false);
53 UseSizeScale(true, 5);
54 UseColorScale(true);
55 SelectXY(NULL, NULL);
56 SelectWt(NULL);
57 SelectErrBar();
58 SelectLabel(NULL);
59 SetName("NTupleDrw");
60 NptDraw = 0;
61
62}
63
64PINTuple::~PINTuple()
65{
66 if (mAdDO && mNT) delete mNT;
67}
68
69//++
70// Titre Méthodes
71//--
72//++
73// void SelectXY(const char* px, const char* py)
74// Choix des noms de colonnes X,Y définissant les coordonnées des points.
75// Ces deux colonnes doivent être spécifiées pour obtenir un tracé.
76// void SelectErrBar(const char* erbx=NULL, const char* erby=NULL)
77// Choix des noms de colonnes pour le tracé des barres d'erreur.
78// void SelectWt(const char* pw=NULL, int nbins=10)
79// Choix du nom de colonne poids. Dans ce cas, la taille du signe
80// (marker) sera proportionnel à la valeur de cette colonne pour
81// chaque point.
82// void SelectLabel(const char* plabel=NULL)
83// Choix du nom de colonne correspondant à l'étiquette.
84//--
85
86/* --Methode-- */
87void PINTuple::SelectXY(const char* px, const char* py)
88{
89string name;
90if (mNT == NULL) xK = yK = -1;
91if (px == NULL) xK = -1;
92else { name = px; xK = mNT->ColumnIndex(name); }
93if (py == NULL) yK = -1;
94else { name = py; yK = mNT->ColumnIndex(name); }
95}
96
97/* --Methode-- */
98void PINTuple::SelectWt(const char* pw)
99{
100if (pw == NULL) wK = -1;
101else { string name = pw; wK = mNT->ColumnIndex(name); }
102
103if (wK >= 0) mNT->GetMinMax(wK, wMin, wMax);
104else { wMin = 0.; wMax = 1.; }
105}
106
107/* --Methode-- */
108void PINTuple::SelectLabel(const char* plabel)
109{
110if (plabel == NULL) lK = -1;
111else { string name = plabel; lK = mNT->ColumnIndex(name); }
112}
113
114/* --Methode-- */
115void PINTuple::SelectErrBar(const char* erbx, const char* erby)
116{
117string name;
118if (mNT == NULL) xebK = yebK = -1;
119if (erbx == NULL) xebK = -1;
120else { name = erbx; xebK = mNT->ColumnIndex(name); }
121if (erby == NULL) yebK = -1;
122else { name = erby; yebK = mNT->ColumnIndex(name); }
123}
124
125
126/* --Methode-- */
127void PINTuple::UpdateLimits()
128{
129 if (!mNT) return;
130 if (mNT->NbLines() <= 0) return;
131 if ( (xK < 0) || (yK < 0) ) return;
132
133 // Commencer par trouver nos limites
134 double xmin, xmax, ymin, ymax;
135 xmin = ymin = 9.e19;
136 xmax = ymax = -9.e19;
137 mNT->GetMinMax(xK, xmin, xmax);
138 mNT->GetMinMax(yK, ymin, ymax);
139 PIAxes::ReSizeMinMax(isLogScaleX(),xmin,xmax);
140 PIAxes::ReSizeMinMax(isLogScaleY(),ymin,ymax);
141 SetLimits(xmin,xmax,ymin,ymax);
142// SetAxesFlags(kBoxAxes | kExtTicks | kLabels); Ne pas faire - Reza 11/99
143}
144
145
146/* --Methode-- */
147#define NMXMULTP_LOCAL 30 // Pour multipoint sans new
148void PINTuple::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
149{
150double xp,yp,xer,yer,wp;
151double xl,yl;
152int nok,npolyg;
153
154if (!mNT) return;
155if (axesFlags != kAxesNone) DrawAxes(g);
156if ( (xK < 0) || (yK < 0) ) return;
157if (GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
158
159// Pour tracer des markers avec taille fonction de Wt (poids)
160double dw = (wMax-wMin)/nWbins;
161if (dw < 1.e-19) dw = 1.e-19;
162
163// Pour tracer des markers avec couleur en fonction de Wt (poids)
164PIColorMap * cmap = NULL;
165double dwc = 1.;
166int nwc = 1;
167bool revcmap;
168CMapId mcmapid = GetGraphicAtt().GetColMapId(revcmap);
169if( colorScale && (wK >= 0) && (mcmapid != CMAP_OTHER) ) {
170 cmap = new PIColorMap(mcmapid);
171 cmap->ReverseColorIndex(revcmap);
172 nwc = cmap->NCol();
173 dwc = (wMax-wMin)/(double)nwc;
174}
175
176int msz,sz;
177
178PIMarker mmrk = GetGraphicAtt().GetMarker();
179PIMarker mrk;
180if (wK >= 0) mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_CircleMarker;
181else mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_DotMarker;
182msz = GetGraphicAtt().GetMarkerSz();
183if (msz < 1) msz = 1;
184g->SelMarker(msz, mrk);
185
186PIGrCoord uxmin, uxmax, uymin, uymax;
187g->GetGrSpace(uxmin, uxmax, uymin, uymax);
188double xmin2 = uxmin;
189double ymin2 = uymin;
190double xmax2 = uxmax;
191double ymax2 = uymax;
192
193nok = 0;
194xp = yp = xl = yl = 0;
195PIGrCoord xpolyg[NMXMULTP_LOCAL], ypolyg[NMXMULTP_LOCAL];
196npolyg = 0;
197NptDraw = 0;
198for (long i=0; i<(long)mNT->NbLines(); i++) {
199 xl = xp; yl = yp;
200 xp = mNT->GetCell(i, xK);
201 yp = mNT->GetCell(i, yK);
202 if ( (xp < xmin2) || (xp > xmax2) || (yp < ymin2) || (yp > ymax2) ) continue;
203 nok++;
204 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
205
206 NptDraw++;
207// Taille - couleur de marker en fonction du poids
208 if (wK >= 0) wp = mNT->GetCell(i, wK);
209 if (mrkSzScale && (wK >= 0)) { // Changement de taille
210 sz = (int)((wp-wMin)/dw);
211 if (sz < 0) sz = 0;
212 if (sz > nWbins) sz = nWbins;
213 sz += msz;
214 if (sz < 2) g->SelMarker(sz, PI_DotMarker);
215 else g->SelMarker(sz, mrk);
216 }
217// Couleur du marker en fonction du poids
218 if( colorScale && (wK >= 0) && cmap ) {
219 int cid = (int)((wp-wMin)/dwc);
220 if (cid < 0) cid = 0;
221 if (cid >= nwc) cid = nwc-1;
222 g->SelForeground(*cmap, cid);
223 }
224
225 // Trace d'une ligne reliant les points
226 if( connectPts ) {
227 if(npolyg==0) {xpolyg[0]=xl; ypolyg[0]=yl; npolyg=1;}
228 if(npolyg<NMXMULTP_LOCAL)
229 {xpolyg[npolyg]=xp; ypolyg[npolyg]=yp; npolyg++;}
230 if(npolyg==NMXMULTP_LOCAL)
231 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
232 }
233
234 // Trace des erreurs selon X et Y
235 if ( xebK >= 0 ) {
236 xer = mNT->GetCell(i, xebK);
237 if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
238 }
239 if ( yebK >= 0 ) {
240 yer = mNT->GetCell(i, yebK);
241 if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
242 }
243
244 // Trace du marker
245 if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker)) g->DrawMarker(xp, yp);
246
247 // Trace eventuel du label
248 if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
249
250}
251
252// Fin du trace d'une ligne reliant les points si necessaire
253if( connectPts && npolyg>1 )
254 {g->DrawPolygon(xpolyg,ypolyg,npolyg,false); npolyg=0;}
255
256if (stats) { // Trace de stats
257 g->SelFontSz((YMax() - YMin())/30);
258 // La hauteur de la cellule
259 PIGrCoord a,d;
260 double cH = (double)g->GetFontHeight(a,d);
261 double cellHeight = 1.2 * cH;
262 // Les labels et leurs longueurs -> largeur de la cellule
263 char label[64];
264 sprintf(label, "N=%d (/%d)", nok, mNT->NbLines());
265 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
266 double xu, yu, cw;
267 double ofpx = spoX*(XMax()-XMin());
268 double ofpy = spoY*(YMax()-YMin());
269 // Les limites du cadre
270 xu = g->DeltaUCX(XMax(), - cellWidth);
271 yu = g->DeltaUCY(YMax(), - cellHeight);
272 double recw = XMax()-xu;
273 double rech = YMax()-yu;
274 xu += ofpx; yu += ofpy;
275 g->DrawBox(xu, yu, recw, rech);
276 // L'ecriture des labels (attention aux inversions possibles des axes!)
277 cw = (g->isAxeXDirRtoL()) ? -0.05*cellWidth : -0.95*cellWidth;
278 xu = g->DeltaUCX(XMax(),cw);
279 cw = (g->isAxeYDirUpDown()) ? -0.1*cH : -1.1*cH;
280 yu = g->DeltaUCY(YMax(),cw);
281 xu += ofpx; yu += ofpy;
282 g->DrawString(xu,yu,label);
283}
284
285if (cmap) delete cmap;
286return;
287}
288#undef NMXMULTP_LOCAL
289
290/* --Methode-- */
291void PINTuple::AppendTextInfo(string& info, double xmin, double ymin, double xmax, double ymax)
292{
293if (!mNT) return;
294if ( (xK < 0) || (yK < 0) ) return;
295
296int ncnt = 0;
297double xp,yp;
298char buff[128];
299sprintf(buff,"PINTuple: NLines= %d NCol= %d \n", mNT->NbLines(), mNT->NbColumns());
300info += buff;
301info += mNT->LineHeaderToString();
302for(long i=0; i<(long)mNT->NbLines(); i++) {
303 xp = mNT->GetCell(i, xK);
304 yp = mNT->GetCell(i, yK);
305 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
306 ncnt++;
307 if (ncnt > 101) continue;
308 info += mNT->LineToString(i);
309 }
310if (ncnt >= 101) info += " .... \n";
311sprintf(buff," %d points inside selected region \n", ncnt);
312info += buff;
313// printf("PINTuple::AppendTextInfo()-DBG %g %g %g %g - %d\n", xmin, ymin, xmax, ymax, ncnt);
314return;
315}
316
317/* La methode DecodeOptionString permet de decoder un ensemble d'options
318 et de parametre d'affichage specifie sous forme d'un vecteur de string.
319 Si rmdecopt == true, les options decodees sont supprimees du vecteur
320 de string fourni en entree - ce qui permet l'enchainement eventuel
321 de plusieurs decodages de string.
322 Les options peuvent etre sous forme de flag : "stat" "nostat"
323 ou plus complexes, par exemple "dynamic=-3,3"
324 Rc: La methode renvoie le nombre d'options decodees
325*/
326
327/* --Methode-- */
328int PINTuple::DecodeOptionString(vector<string> & opt, bool rmdecopt)
329{
330 int optsz1 = opt.size();
331 if(optsz1<1) return(0);
332 // On appelle d'abord le decodage de la classe PIDrawer de laquelle
333 // on herite. (Pas obligatoire) on decode donc ici les attributs de
334 // couleur, fontes ...
335 int ndec1 = PIDrawer::DecodeOptionString(opt, rmdecopt);
336 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
337
338 vector<string> udopt; // On gardera ici les options non decodees
339 unsigned int k = 0;
340 int ndec = opt.size();
341 for( k=0; k<opt.size(); k++ ) {
342 string opts = opt[k];
343 if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
344 else if( opts=="nsta" || opts=="nstat"
345 || opts=="nostat" || opts=="nostats") SetStats(false);
346 else if(opts.substr(0,11) == "statposoff=") {
347 float xo=0., yo=0.;
348 sscanf(opts.substr(11).c_str(),"%g,%g",&xo, &yo);
349 SetStatPosOffset(xo, yo);
350 }
351 else if (opts == "connectpoints") ConnectPoints(true);
352 else if (opts == "noconnectpoints") ConnectPoints(false);
353 else if (opts == "colorscale") UseColorScale(true);
354 else if (opts == "nocolorscale") UseColorScale(false);
355 else if (opts == "sizescale") UseSizeScale(true);
356 else if (opts == "nosizescale") UseSizeScale(false);
357 else if (opts.substr(0,10) == "sizescale=") {
358 int nbn = atoi(opts.substr(10).c_str());
359 UseSizeScale(true, nbn);
360 }
361
362 else {
363 // Si option non decode
364 ndec--;
365 // S'il faut supprimer les options decodees
366 if (rmdecopt) udopt.push_back(opts);
367 }
368 }
369 // S'il faut supprimer les options decodees, on remplace l'argument opt
370 // par le vecteur des options non decodees.
371 if (rmdecopt) opt = udopt;
372 return(ndec+ndec1);
373}
374
375int PINTuple::OptionToString(vector<string> & opt) const
376{
377 PIDrawer::OptionToString(opt);
378
379 char str[256];
380
381 if(stats) opt.push_back("stat"); else opt.push_back("nstat");
382
383 sprintf(str,"statposoff=%g,%g",spoX,spoY); opt.push_back(str);
384
385 if(connectPts) opt.push_back("connectpoints");
386 else opt.push_back("noconnectpoints");
387
388 if(colorScale) opt.push_back("colorscale");
389 else opt.push_back("nocolorscale");
390
391 if(mrkSzScale) {
392 if(nWbins>0) {sprintf(str,"sizescale=%d",nWbins); opt.push_back(str);}
393 else opt.push_back("sizescale");
394 } else opt.push_back("nosizescale");
395
396 return 1;
397}
398
399/* La methode GetOptionsHelpInfo(string& info) renvoie une chaine
400 avec la description des options comprises par ce drawer
401 Note: Il est preferable de ne pas initialiser la chaine
402 string info au depart, afin de permettre de mettre bout a
403 bout les aides de differents Drawer */
404
405/* --Methode-- */
406void PINTuple::GetOptionsHelpInfo(string& info)
407{
408info += " ---- PINTuple options help info : \n" ;
409info += " sta,stat,stats: activate statistic display\n";
410info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
411info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
412info += " as a fraction of total size \n";
413info += " connectpoints: The points are connected by a line \n";
414info += " noconnectpoints (this is the default) \n";
415info += " colorscale/nocolorscale (Use color scale for weight) \n";
416info += " sizescale/sizescale=nbins/nosizescale (Use marker size for weight) \n";
417info += " (and usual color/line/marker/... attribute decoding) \n";
418// On recupere ensuite la chaine info de la classe de base
419PIDrawer::GetOptionsHelpInfo(info);
420return;
421}
422
423
424/* --Methode-- */
425double PINTuple::GetDistanceToPoint(double x, double y)
426{
427 if(!mNT) return 1.e+9;
428 if( xK<0 || yK<0 ) return 1.e+9;
429
430 const int nessai = 100;
431 long inc = (NptDraw>nessai) ? (long)(NptDraw/nessai)+1 : 1;
432
433 double dist = -1.e+18;
434 long n = 0;
435 for(long i=0; i<(long)mNT->NbLines(); i++) {
436 double xp=mNT->GetCell(i,xK);
437 if(xp<XMin() || xp>XMax()) continue;
438 double yp=mNT->GetCell(i,yK);
439 if(yp<YMin() || yp>YMax()) continue;
440 if(n%inc==0) {
441 xp = (xp-x)/(XMax()-XMin())/0.5;
442 yp = (yp-y)/(YMax()-YMin())/0.5;
443 xp = xp*xp+yp*yp;
444 if(dist<0. || xp<dist) dist = xp;
445 }
446 n++;
447 }
448 dist=sqrt(fabs(dist));
449 //cout<<"PINTuple: xlim="<<XMin()<<","<<XMax()<<" ylim="<<YMin()<<","<<YMax()
450 // <<" NbLines="<<mNT->NbLines()<<" inc="<<inc<<endl;
451 //cout<<"....d="<<dist<<" x="<<x<<" y="<<y<<" NptDraw="<<NptDraw<<endl;
452
453 return dist;
454}
Note: See TracBrowser for help on using the repository browser.