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

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

gestion trace linges avec DrawPolygon cmv 02/12/03

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