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

Last change on this file since 2672 was 2642, checked in by ansari, 21 years ago

Correction petit bug avec connectpoints ds PINTuple - Reza 14/12/2004

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