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

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

gestion trace linges avec DrawPolygon cmv 02/12/03

File size: 11.9 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 "pintuple.h"
7
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
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.
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)
36// Constructeur. Si "ad == true", l'objet "nt" est détruit par
37// le destructeur de l'objet "PINTuple"
38// Note : "nt" doit être créé par new dans ce cas.
39//--
40
41/* --Methode-- */
42PINTuple::PINTuple(NTupleInterface* nt, bool ad)
43: PIDrawer()
44{
45 mNT = nt;
46 mAdDO = ad;
47 SetStats(true);
48 SetStatPosOffset();
49 ConnectPoints(false);
50 UseSizeScale(true, 5);
51 UseColorScale(true);
52 SelectXY(NULL, NULL);
53 SelectWt(NULL);
54 SelectErrBar();
55 SelectLabel(NULL);
56 SetName("NTupleDrw");
57}
58
59PINTuple::~PINTuple()
60{
61 if (mAdDO && mNT) delete mNT;
62}
63
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
81/* --Methode-- */
82void PINTuple::SelectXY(const char* px, const char* py)
83{
84string name;
85if (mNT == NULL) xK = yK = -1;
86if (px == NULL) xK = -1;
87else { name = px; xK = mNT->ColumnIndex(name); }
88if (py == NULL) yK = -1;
89else { name = py; yK = mNT->ColumnIndex(name); }
90}
91
92/* --Methode-- */
93void PINTuple::SelectWt(const char* pw)
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-- */
103void PINTuple::SelectLabel(const char* plabel)
104{
105if (plabel == NULL) lK = -1;
106else { string name = plabel; lK = mNT->ColumnIndex(name); }
107}
108
109/* --Methode-- */
110void PINTuple::SelectErrBar(const char* erbx, const char* erby)
111{
112string name;
113if (mNT == NULL) xebK = yebK = -1;
114if (erbx == NULL) xebK = -1;
115else { name = erbx; xebK = mNT->ColumnIndex(name); }
116if (erby == NULL) yebK = -1;
117else { name = erby; yebK = mNT->ColumnIndex(name); }
118}
119
120
121/* --Methode-- */
122void PINTuple::UpdateLimits()
123{
124 if (!mNT) return;
125 if (mNT->NbLines() <= 0) return;
126 if ( (xK < 0) || (yK < 0) ) return;
127
128 // Commencer par trouver nos limites
129 double xmin, xmax, ymin, ymax;
130 xmin = ymin = 9.e19;
131 xmax = ymax = -9.e19;
132 mNT->GetMinMax(xK, xmin, xmax);
133 mNT->GetMinMax(yK, ymin, ymax);
134 PIAxes::ReSizeMinMax(isLogScaleX(),xmin,xmax);
135 PIAxes::ReSizeMinMax(isLogScaleY(),ymin,ymax);
136 SetLimits(xmin,xmax,ymin,ymax);
137// SetAxesFlags(kBoxAxes | kExtTicks | kLabels); Ne pas faire - Reza 11/99
138}
139
140
141/* --Methode-- */
142#define NMXMULTP_LOCAL 30 // Pour multipoint sans new
143void PINTuple::Draw(PIGraphicUC* g, double xmin, double ymin, double xmax, double ymax)
144{
145double xp,yp,xer,yer,wp;
146double xl,yl;
147int nok,npolyg;
148
149if (!mNT) return;
150if (axesFlags != kAxesNone) DrawAxes(g);
151if ( (xK < 0) || (yK < 0) ) return;
152if (GetGraphicAtt().GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
153
154// Pour tracer des markers avec taille fonction de Wt (poids)
155double dw = (wMax-wMin)/nWbins;
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.;
161int nwc = 1;
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();
168 dwc = (wMax-wMin)/(double)nwc;
169}
170
171int msz,sz;
172
173PIMarker mmrk = GetGraphicAtt().GetMarker();
174PIMarker mrk;
175if (wK >= 0) mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_CircleMarker;
176else mrk = (mmrk != PI_NotDefMarker) ? mmrk : PI_DotMarker;
177msz = GetGraphicAtt().GetMarkerSz();
178if (msz < 1) msz = 1;
179g->SelMarker(msz, mrk);
180
181PIGrCoord uxmin, uxmax, uymin, uymax;
182g->GetGrSpace(uxmin, uxmax, uymin, uymax);
183double xmin2 = uxmin;
184double ymin2 = uymin;
185double xmax2 = uxmax;
186double ymax2 = uymax;
187
188nok = 0;
189xp = yp = xl = yl = 0;
190PIGrCoord xpolyg[NMXMULTP_LOCAL], ypolyg[NMXMULTP_LOCAL];
191npolyg = 0;
192for (long i=0; i<(long)mNT->NbLines(); i++) {
193 xl = xp; yl = yp;
194 xp = mNT->GetCell(i, xK);
195 yp = mNT->GetCell(i, yK);
196 if ( (xp < xmin2) || (xp > xmax2) || (yp < ymin2) || (yp > ymax2) ) continue;
197 nok++;
198 if ( (xp < xmin) || (xp > xmax) || (yp < ymin) || (yp > ymax) ) continue;
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
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
228 if ( xebK >= 0 ) {
229 xer = mNT->GetCell(i, xebK);
230 if(xer>0.) g->DrawLine(xp-xer, yp, xp+xer, yp);
231 }
232 if ( yebK >= 0 ) {
233 yer = mNT->GetCell(i, yebK);
234 if(yer>0.) g->DrawLine(xp, yp-yer, xp, yp+yer);
235 }
236
237 // Trace du marker
238 if ((wK >= 0)||(lK < 0)||(mmrk != PI_NotDefMarker)) g->DrawMarker(xp, yp);
239
240 // Trace eventuel du label
241 if (lK >= 0) g->DrawString(xp, yp, mNT->GetCelltoString(i, lK).c_str());
242
243}
244
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
249if (stats) { // Trace de stats
250 g->SelFontSz((YMax() - YMin())/30);
251 // La hauteur de la cellule
252 PIGrCoord a,d;
253 double cH = (double)g->GetFontHeight(a,d);
254 double cellHeight = 1.2 * cH;
255 // Les labels et leurs longueurs -> largeur de la cellule
256 char label[64];
257 sprintf(label, "N=%d (/%d)", nok, mNT->NbLines());
258 double cellWidth = 1.1 * (double)g->CalcStringWidth(label);
259 double xu, yu, cw;
260 double ofpx = spoX*(XMax()-XMin());
261 double ofpy = spoY*(YMax()-YMin());
262 // Les limites du cadre
263 xu = g->DeltaUCX(XMax(), - cellWidth);
264 yu = g->DeltaUCY(YMax(), - cellHeight);
265 double recw = XMax()-xu;
266 double rech = YMax()-yu;
267 xu += ofpx; yu += ofpy;
268 g->DrawBox(xu, yu, recw, rech);
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);
274 xu += ofpx; yu += ofpy;
275 g->DrawString(xu,yu,label);
276}
277
278if (cmap) delete cmap;
279return;
280}
281#undef NMXMULTP_LOCAL
282
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();
295for(long i=0; i<(long)mNT->NbLines(); i++) {
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}
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{
323 int optsz1 = opt.size();
324 if(optsz1<1) return(0);
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);
329 if(optsz1-ndec1<1) return(ndec1); // si tout a ete decode
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];
336 if(opts=="sta" || opts=="stat" || opts=="stats") SetStats(true);
337 else if( opts=="nsta" || opts=="nstat"
338 || opts=="nostat" || opts=="nostats") SetStats(false);
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 }
344 else if (opts == "connectpoints") ConnectPoints(true);
345 else if (opts == "noconnectpoints") ConnectPoints(false);
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 }
354
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}
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{
377info += " ---- PINTuple options help info : \n" ;
378info += " sta,stat,stats: activate statistic display\n";
379info += " nsta,nstat,nostat,nostats: deactivate statistic display\n";
380info += " statposoff=OffsetX,OffsetY : Position offset for Stats drawing \n";
381info += " as a fraction of total size \n";
382info += " connectpoints: The points are connected by a line \n";
383info += " noconnectpoints (this is the default) \n";
384info += " colorscale/nocolorscale (Use color scale for weight) \n";
385info += " sizescale/sizescale=nbins/nosizescale (Use marker size for weight) \n";
386info += " (and usual color/line/marker/... attribute decoding) \n";
387// On recupere ensuite la chaine info de la classe de base
388PIDrawer::GetOptionsHelpInfo(info);
389return;
390}
391
392
393
Note: See TracBrowser for help on using the repository browser.