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

Last change on this file since 3461 was 3272, checked in by cmv, 18 years ago

fix default argument given in method .cc SelectColorByName SelectColorByIndex cmv 20/06/2007

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