source: Sophya/trunk/SophyaPI/PI/piaxes.cc@ 2164

Last change on this file since 2164 was 2164, checked in by ansari, 23 years ago
  • Creation d'une classe gestionnaire de trace d'elements a etre utilise

ds la classe PIElDrawer (Traceur d'elements, d'axes 2D et titres, etc ...)

  • Ajout d'une nouvelle classe PIElDrawer3D , remplissant les memes

fonctions pour les objets 3D

  • Ajout de trace de label d'axes ds PIAxes
  • Ajout methode Set3DBox ds PIDrawer3D - Preparation en vue du

transfert du trace de boite/axes 3D a l'objet PIElDrawer3D

Reza 7/8/2002

File size: 24.4 KB
Line 
1// Module PI : Peida Interactive PIAxes
2// Methodes de trace des axes
3// R. Ansari - 2002
4// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
5
6#include "machdefs.h"
7#include <stdio.h>
8#include <iostream.h>
9#include <strings.h>
10#include <math.h>
11#include "piaxes.h"
12
13//++
14// Class PIAxes
15// Lib PI
16// include piaxes.h
17//
18// Classe gestionnaire de tracé d'axes. A utiliser dans
19// un objet PIDrawer.
20//--
21//++
22// Links Voir
23// PIDrawer
24//--
25
26//++
27// Titre Constructeurs et méthodes
28//--
29
30
31inline void dble_SWAP(double& a,double& b) {double tmp=a; a=b; b=tmp;}
32
33//++
34// PIAxes()
35// Constructeur.
36//--
37
38/* --Methode-- */
39PIAxes::PIAxes()
40{
41 setupDone = false;
42}
43
44/* --Methode-- */
45PIAxes::~PIAxes()
46{
47}
48
49//++
50// void DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt, unsigned int flags, bool afsz)
51// Trace les axes en utilisant les limites de l'objet PIGraphicUC.
52// "flags" spécifie les attributs d'axes. Constantes prédéfinies:
53//| kStdAxes : Axes passant par le centre
54//| kBoxAxes : Axes entourant le tracé
55//| kIntTicks, kExtTicks
56//| kMajTicks, kMinTicks
57//| kAxesDflt = kStdAxes | kTicks | kLabels
58//| kGridOn, kAxesNone
59// Si "afsz == true", la taille de fonte est choisie automatiquement.
60//
61// void DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt, unsigned int flags, bool afsz, \
62// double xmin, double xmax, double ymin, double ymax)
63// Tracé d'axes avec spécification des limites d'axes.
64//
65// void DrawXCaption(PIGraphicUC* g, string const& xLabel, PIGraphicAtt const& att)
66// Tracé du label d'axe X.
67// void DrawYCaption(PIGraphicUC* g, string const& YLabel, PIGraphicAtt const& att)
68// Tracé du label d'axe Y.
69//--
70
71/* --Methode-- */
72void PIAxes::DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt,
73 unsigned int flags, bool afsz)
74{
75 PIGrCoord xmin, xmax, ymin, ymax;
76 g->GetGrSpace(xmin, xmax, ymin, ymax);
77 DrawXYAxes(g, gratt, flags, afsz, (double)xmin, (double)xmax,
78 (double)ymin, (double)ymax);
79}
80
81/* --Methode-- */
82void PIAxes::DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt,
83 unsigned int flags, bool afsz,
84 double xmin, double xmax, double ymin, double ymax)
85{
86 Setup(g, xmin, xmax, ymin, ymax);
87 // g->NoClip();
88
89 if (gratt.GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
90 else g->SelLine(gratt.GetLineAtt());
91 if (gratt.GetColor() == PI_NotDefColor) g->SelForeground(PI_Black);
92 else g->SelForeground(gratt.GetColor());
93
94 if(afsz) {
95 double fsz = xMajTickLen*3.5;
96 g->SelFontSz(fsz);
97 }
98
99 if (flags & kStdAxes) {
100
101 // Les axes
102
103 g->DrawLine(xMin, (yMin+yMax)/2., xMax, (yMin+yMax)/2.);
104 g->DrawLine((xMin+xMax)/2., yMin, (xMin+xMax)/2., yMax);
105
106 // La grille en pointilles
107
108 if (flags & kGridOn) DrawGrid(g);
109
110 // Les ticks majeurs
111
112 if (flags & kMajTicks) {
113 DrawHTicks(g, (yMin+yMax)/2., xMajTickLen, xMajTickLen, xMajTicks);
114 DrawVTicks(g, (xMin+xMax)/2., yMajTickLen, yMajTickLen, yMajTicks);
115 }
116
117 // Les ticks mineurs
118
119 if (flags & kMinTicks) {
120 DrawHTicks(g, (yMin+yMax)/2., xMinTickLen, xMinTickLen, xMinTicks);
121 DrawVTicks(g, (xMin+xMax)/2., yMinTickLen, yMinTickLen, yMinTicks);
122 }
123
124 // Les labels
125
126 if (flags & kLabels) {
127 if (!aYdir)
128 DrawHLabels(g, (yMin+yMax)/2.-xMajTickLen*2, xMajTicks, PI_VerticalTop);
129 else
130 DrawHLabels(g, (yMin+yMax)/2.+xMajTickLen*2, xMajTicks, PI_VerticalTop);
131 if (!aXdir)
132 DrawVLabels(g, (xMin+xMax)/2.-yMajTickLen*2, yMajTicks, PI_HorizontalRight);
133 else
134 DrawVLabels(g, (xMin+xMax)/2.+yMajTickLen*2, yMajTicks, PI_HorizontalRight);
135 }
136
137 }
138
139 if (flags & kBoxAxes) {
140
141 // La boite
142
143 g->DrawLine(xMin, yMin, xMax, yMin);
144 g->DrawLine(xMax, yMin, xMax, yMax);
145 g->DrawLine(xMax, yMax, xMin, yMax);
146 g->DrawLine(xMin, yMax, xMin, yMin);
147
148 // Longueur des ticks
149
150 double extXMajTickLen = flags&kExtTicks ? xMajTickLen : 0;
151 double intXMajTickLen = flags&kIntTicks ? xMajTickLen : 0;
152 double extXMinTickLen = flags&kExtTicks ? xMinTickLen : 0;
153 double intXMinTickLen = flags&kIntTicks ? xMinTickLen : 0;
154 double extYMajTickLen = flags&kExtTicks ? yMajTickLen : 0;
155 double intYMajTickLen = flags&kIntTicks ? yMajTickLen : 0;
156 double extYMinTickLen = flags&kExtTicks ? yMinTickLen : 0;
157 double intYMinTickLen = flags&kIntTicks ? yMinTickLen : 0;
158
159 // La grille en pointilles
160
161 if (flags & kGridOn) DrawGrid(g);
162
163 // Les ticks majeurs
164
165 if (flags & kMajTicks) {
166 DrawHTicks(g, yMin, intXMajTickLen, extXMajTickLen, xMajTicks);
167 DrawHTicks(g, yMax, extXMajTickLen, intXMajTickLen, xMajTicks);
168 DrawVTicks(g, xMin, extYMajTickLen, intYMajTickLen, yMajTicks);
169 DrawVTicks(g, xMax, intYMajTickLen, extYMajTickLen, yMajTicks);
170 }
171
172 // Les ticks mineurs
173
174 if (flags & kMinTicks) {
175 DrawHTicks(g, yMin, intXMinTickLen, extXMinTickLen, xMinTicks);
176 DrawHTicks(g, yMax, extXMinTickLen, intXMinTickLen, xMinTicks);
177 DrawVTicks(g, xMin, extYMinTickLen, intYMinTickLen, yMinTicks);
178 DrawVTicks(g, xMax, intYMinTickLen, extYMinTickLen, yMinTicks);
179 }
180
181
182 // Les labels
183
184 if (flags & kLabels) {
185 if (!aYdir) {
186 DrawHLabels(g, g->DeltaUCY(yMin, -xMajTickLen*2), xMajTicks, PI_VerticalTop);
187 }
188 else {
189 DrawHLabels(g, g->DeltaUCY(yMax, xMajTickLen*2), xMajTicks, PI_VerticalTop);
190 }
191 if (!aXdir) {
192 DrawVLabels(g, g->DeltaUCX(xMin, -yMajTickLen*2), yMajTicks, PI_HorizontalRight);
193 }
194 else {
195 DrawVLabels(g, g->DeltaUCX(xMax, yMajTickLen*2), yMajTicks, PI_HorizontalRight);
196 }
197 }
198 }
199 g->Clip();
200
201}
202
203/* --Methode-- */
204void PIAxes::DrawXCaption(PIGraphicUC* g, PIGraphicAtt& att, unsigned int flags, string const& xLabel)
205{
206 if (xLabel.length() < 1) return;
207 if (!setupDone) {
208 PIGrCoord xmin, xmax, ymin, ymax;
209 g->GetGrSpace(xmin, xmax, ymin, ymax);
210 Setup(g, xmin, xmax, ymin, ymax);
211 }
212 if ( (att.GetFontName() != PI_DefaultFont) ||
213 (att.GetFontAtt() != PI_NotDefFontAtt) )
214 g->SelFont(att.GetFont());
215 PIColors fcol = att.GetFgColor();
216 if (fcol != PI_NotDefColor) g->SelForeground(fcol);
217
218 double fy, fh;
219 fh = 0.090*(yMax-yMin);
220 if (flags&kBoxAxes) {
221 if (g->isAxeYDirUpDown()) fy = yMax;
222 else fy = yMin;
223 }
224
225 else fy = 0.5*(yMin+yMax);
226 if (g->isAxeYDirUpDown()) fy += fh;
227 else fy -= fh;
228 g->DrawString(xMax-(xMax-xMin)*0.30, fy, xLabel.c_str(),
229 PI_HorizontalCenter | PI_VerticalCenter);
230}
231
232/* --Methode-- */
233void PIAxes::DrawYCaption(PIGraphicUC* g, PIGraphicAtt& att, unsigned int flags, string const& yLabel)
234{
235 if (yLabel.length() < 1) return;
236 if (!setupDone) {
237 PIGrCoord xmin, xmax, ymin, ymax;
238 g->GetGrSpace(xmin, xmax, ymin, ymax);
239 Setup(g, xmin, xmax, ymin, ymax);
240 }
241 if ( (att.GetFontName() != PI_DefaultFont) ||
242 (att.GetFontAtt() != PI_NotDefFontAtt) )
243 g->SelFont(att.GetFont());
244 PIColors fcol = att.GetFgColor();
245 if (fcol != PI_NotDefColor) g->SelForeground(fcol);
246
247 double fx, fh;
248 fh = 0.125*(xMax-xMin);
249 if (yLabel.length() > 0) {
250 if (flags&kBoxAxes) {
251 if (g->isAxeXDirRtoL()) fx = xMax;
252 else fx = xMin;
253 }
254 else fx = 0.5*(xMin+xMax);
255 if (g->isAxeXDirRtoL()) fx += fh;
256 else fx -= fh;
257 unsigned long txtflg = PI_HorizontalCenter | PI_VerticalCenter | PI_TextDirectionVerticalUp;
258 if (g->isAxeYDirUpDown())
259 txtflg = PI_HorizontalCenter | PI_VerticalCenter | PI_TextDirectionVerticalDown;
260 g->DrawString(fx, yMax-(yMax-yMin)*0.30, yLabel.c_str(), txtflg);
261 }
262}
263
264/* --Methode-- */
265void PIAxes::Setup(PIGraphicUC* g, double xmin, double xmax,
266 double ymin, double ymax)
267{
268
269 xMin = xmin; xMax = xmax;
270 yMin = ymin; yMax = ymax;
271
272 aXdir = g->isAxeXDirRtoL();
273 aYdir = g->isAxeYDirUpDown();
274 aXlog = g->isLogScaleX();
275 aYlog = g->isLogScaleY();
276
277 int ntick_x = (aXlog) ? 10 : 10;
278 if(aXlog) BestTicksLog(xMin,xMax,ntick_x,xMajTicks,xMinTicks);
279 else BestTicks(xMin,xMax,ntick_x,xMajTicks,xMinTicks);
280
281 int ntick_y = (aYlog) ? 12 : 12;
282 if(aYlog) BestTicksLog(yMin,yMax,ntick_y,yMajTicks,yMinTicks);
283 else BestTicks(yMin,yMax,ntick_y,yMajTicks,yMinTicks);
284
285 yMajTickLen = (xMax-xMin)/100;
286 yMinTickLen = (xMax-xMin)/250;
287 xMajTickLen = (yMax-yMin)/100;
288 xMinTickLen = (yMax-yMin)/250;
289
290 setupDone = true;
291}
292
293
294/* --Methode-- */
295void PIAxes::DrawHTicks(PIGraphicUC* g, double y, double tickUp,
296 double tickDown, vector<double>& xticks)
297{
298 if(xticks.size()==0) return;
299 for(unsigned int i=0;i<xticks.size();i++) {
300 if(xticks[i]<xMin) continue;
301 if(xticks[i]>xMax) break;
302 g->DrawLine(xticks[i],g->DeltaUCY(y,-tickDown),xticks[i],g->DeltaUCY(y,tickUp));
303 }
304}
305
306/* --Methode-- */
307void PIAxes::DrawVTicks(PIGraphicUC* g, double x, double tickLeft,
308 double tickRight, vector<double>& yticks)
309{
310 if(yticks.size()==0) return;
311 for(unsigned int i=0;i<yticks.size();i++) {
312 if(yticks[i]<yMin) continue;
313 if(yticks[i]>yMax) break;
314 g->DrawLine(g->DeltaUCX(x,-tickLeft),yticks[i],g->DeltaUCX(x,tickRight),yticks[i]);
315 }
316}
317
318/* --Methode-- */
319void PIAxes::DrawHLabels(PIGraphicUC* g, double y, vector<double>& xticks, unsigned long just)
320{
321 if(xticks.size()==0) return;
322
323 // Choix du bon format pour les labels des axes
324 string format; double xstep;
325 int npuiss = Le_Bon_Format(xticks,format,xstep);
326 double fac=(npuiss!=0)? fac=pow(10.,(double)npuiss): 1.;
327
328 char label[64];
329 double dum,xpixdeb,xpixfin,largpix;
330 g->UC2GrC(xMin-2.*(xMax-xMin),y,xpixfin,dum);
331 for(unsigned int i=0;i<xticks.size();i++) {
332 if(xticks[i]<xMin) continue;
333 if(xticks[i]>xMax) break;
334 //Attention erreur d'arrondi x->0 (on code 5.1698e-26 au lieu de 0)
335 double xx = (fabs(xticks[i]/xstep)<1.e-5) ? 0.: xticks[i];
336 sprintf(label,format.c_str(),xx/fac); Arrange_Label(label);
337 double largeur = g->CalcStringWidth(label);
338 g->DUC2GrC(largeur,0.,largpix,dum);
339 g->UC2GrC(xticks[i],y,xpixdeb,dum); xpixdeb -= largpix/2.;
340 //cout<<"xticks="<<xticks[i]<<" largpix="<<largpix
341 // <<" xpixdeb="<<xpixdeb<<" xpixfin="<<xpixfin<<endl;
342 if((aXdir && xpixdeb<xpixfin) || (!aXdir && xpixdeb>xpixfin)) {
343 g->DrawString(xticks[i],y,label,PI_HorizontalCenter|just);
344 xpixfin = xpixdeb + 1.1*largpix;
345 }
346 }
347
348 if(npuiss!=0) {
349 PIGrCoord asc,desc;
350 double h = g->GetFontHeight(asc,desc);
351 if((aYdir && (just&PI_VerticalBottom)) || (!aYdir && (just&PI_VerticalTop))) h=-h;
352 double xm = (aXdir)? xMin: xMax;
353 double ym = g->DeltaUCY(y,1.5*h);
354 sprintf(label,"%d",npuiss);
355 g->DrawCompString(xm,ym,"x 10",label,NULL,PI_HorizontalCenter|just);
356 }
357
358}
359
360/* --Methode-- */
361void PIAxes::DrawVLabels(PIGraphicUC* g, double x, vector<double>& yticks, unsigned long just)
362{
363 if(yticks.size()==0) return;
364
365 // Choix du bon format pour les labels des axes;
366 string format; double ystep;
367 int npuiss = Le_Bon_Format(yticks,format,ystep);
368 double fac=(npuiss!=0)? fac=pow(10.,(double)npuiss): 1.;
369
370 char label[64];
371 PIGrCoord asc,desc;
372 double dum,ypixdeb,ypixfin,hautpix,hauteur=g->GetFontHeight(asc,desc);
373 g->DUC2GrC(0.,hauteur,dum,hautpix);
374 g->UC2GrC(x,yMin-2.*(yMax-yMin),dum,ypixfin);
375 for(unsigned int i=0;i<yticks.size();i++) {
376 if(yticks[i]<yMin) continue;
377 if(yticks[i]>yMax) break;
378 double yy = (fabs(yticks[i]/ystep)<1.e-5) ? 0.: yticks[i];
379 sprintf(label,format.c_str(),yy/fac); Arrange_Label(label);
380 g->UC2GrC(x,yticks[i],dum,ypixdeb); ypixdeb -= hautpix/2.;
381 // -- Attention: ypix=0 est en haut de l'ecran
382 // (ypix croissants vers le bas de l'ecran)
383 // donc bien que yMin<yMax on a yMinPix>yMaxPix
384 //cout<<"yticks="<<yticks[i]<<" hautpix="<<hautpix
385 // <<" ypixdeb="<<ypixdeb<<" ypixfin="<<ypixfin<<endl;
386 if((aYdir && ypixdeb>ypixfin) || (!aYdir && ypixdeb<ypixfin)) {
387 g->DrawString(x,yticks[i],label,PI_VerticalCenter|just);
388 ypixfin = ypixdeb + 1.1*hautpix;
389 }
390 }
391
392 if(npuiss!=0) {
393 if(aYdir) hauteur = -hauteur;
394 double ym = (aYdir)? yMin: yMax; ym = g->DeltaUCY(ym,hauteur);
395 sprintf(label,"%d",npuiss);
396 g->DrawCompString(x,ym,"x 10",label,NULL,PI_VerticalBottom|just);
397 }
398
399}
400
401/* --Methode-- */
402void PIAxes::DrawGrid(PIGraphicUC* g)
403{
404 PILineAtt savlineatt = g->GetLineAtt();
405 g->SelLine(PI_ThinDottedLine);
406
407 if(xMajTicks.size()>0)
408 for(unsigned int i=0;i<xMajTicks.size();i++) {
409 if(xMajTicks[i]>xMax) break;
410 g->DrawLine(xMajTicks[i], yMin, xMajTicks[i], yMax);
411 }
412
413 if(yMajTicks.size()>0)
414 for(unsigned int i=0;i<yMajTicks.size();i++) {
415 if(yMajTicks[i]>yMax) break;
416 g->DrawLine(xMin, yMajTicks[i], xMax, yMajTicks[i]);
417 }
418
419 g->SelLine(savlineatt);
420}
421
422////////////////////////////////////////////////////////////////////////
423//////////////////// METHODES STATIQUES ////////////////////////////////
424////////////////////////////////////////////////////////////////////////
425/* --Methode-Static-- */
426void PIAxes::ReSizeMinMax(bool axelog,double& vmin,double& vmax,double garde)
427// Calcul du min et du max du display a partir des valeurs min et max a plotter
428{
429 if(garde<0. || garde>=1.) garde = 0.025;
430 // cout<<"ReSizeMinMax[log="<<axelog<<",garde="<<garde<<"] vmin="<<vmin<<" vmax="<<vmax<<endl;
431 // Cas d'une echelle lineaire
432 if(!axelog || vmax<=0.) {
433 double dv = garde*(vmax-vmin);
434 vmin -= dv;
435 vmax += dv;
436 }
437
438 // Cas d'une echelle log avec un range raisonnable
439 else if(vmin>0.) {
440 double dv = pow(vmax/vmin,garde);
441 vmin /= dv;
442 vmax *= dv;
443 }
444
445 // Cas d'une echelle log avec un range de-raisonnable
446 else if(vmin<=0.) {
447 if(vmin<0.) vmin += garde*vmin;
448 if(vmax==1.) vmax = 1.+garde;
449 if(vmax>1.) vmax = pow(vmax,1.+garde);
450 else vmax = pow(vmax,1.-garde);
451 }
452
453 // cout<<" vmin="<<vmin<<" vmax="<<vmax<<endl;
454}
455
456/* --Methode-Static-- */
457void PIAxes::BestTicks(double xmin,double xmax,int nticks
458 ,vector<double>& majticks,vector<double>& minticks)
459// *** Calcul de l'intervalle entre les ticks et de la valeur du premier tick
460// pour un axe lineaire
461{
462 if(nticks<=0) nticks = 1;
463
464 double d=xmax-xmin; if(d<1.e-100) d=1.e-100;
465 double ld = log10(d);
466 double fld = floor( ((ld<0.)? -ld: ld) );
467 double del,del0;
468 fld = (ld>=0.)? fld-2.: -(fld+2.);
469 del0 = del = pow(10.,fld);
470 // *** Intervalle entre les ticks
471 // xmin xmax d ld fld -->fld del0
472 // 1 1500 1499 3.17 3 1 10^1
473 // 1 9500 9499 3.98 3 1 10^1
474 // 1 1.005 0.005 -2.3 3 -5 10^-5
475 // 1 1.995 0.995 -0.0022 1 -3 10^-3
476 // - Et recherche de la valeur del={del0,2*del0,...,20*del0,...}
477 // telle que "nticks*del" soit le plus petit nombre ">=xmax-xmin"
478 double fac[9] = {2.,5.,10.,20.,50.,100.,200.,500.,1000.};
479 for(int k=0;k<9;k++) {
480 //cout<<"BestTicks: "<<k<<" del="<<del<<" d/del="<<d/del<<"<"<<nticks<<endl;
481 if(d/del < (double)nticks) break;
482 del=fac[k]*del0;
483 }
484 double steptick=del;
485 //*** Valeur du premier tick
486 majticks.resize(0);
487 double xfirsttick = floor(fabs(xmin)/steptick)*steptick;
488 if(xmin<0.) xfirsttick *= -1.;
489 if(xfirsttick<xmin) xfirsttick += steptick;
490 while(xfirsttick<=xmax+steptick/10.)
491 {majticks.push_back(xfirsttick); xfirsttick+= steptick;}
492 //*** Gestion des ticks mineurs
493 minticks.resize(0);
494 if(majticks.size()>1) {
495 double steptickmin = steptick/5.;
496 double xfirsttickmin = majticks[0];
497 while(xfirsttickmin<=xmax+steptickmin/10.)
498 {minticks.push_back(xfirsttickmin); xfirsttickmin+= steptickmin;}
499 }
500}
501/* --Methode-Static-- */
502void PIAxes::BestTicksLog(double xmin,double xmax,int nticks
503 ,vector<double>& majticks,vector<double>& minticks)
504// *** Calcul des ticks pour un axe logarithmique
505{
506 if(nticks<=0) nticks = 1;
507 //cout<<"BestTicksLog: xmin="<<xmin<<" xmax="<<xmax<<" nticks="<<nticks<<endl;
508
509 // Si xmax<=0, on garde BestTicks
510 if(xmax<=0. ) {
511 //cout<<"Choix de BestTicks car xmax="<<xmax<<endl;
512 BestTicks(xmin,xmax,nticks,majticks,minticks);
513 return;
514 }
515
516 int dmin, dmax=int(floor(log10(xmax)));
517 if(xmin>0.) {
518 // Dynamique trop faible, on garde BestTicks
519 if(xmax/xmin<5.) {
520 //cout<<"Choix de BestTicks car xmax/xmin="<<xmax/xmin<<" <5"<<endl;
521 BestTicks(xmin,xmax,nticks,majticks,minticks);
522 return;
523 }
524 dmin=int(floor(log10(xmin)));
525 } else {
526 if(dmax>3) dmin = dmax/3;
527 else dmin = dmax-1;
528 }
529 if(dmax==dmin) dmax++; else if(dmax<dmin) dmax=dmin+1;
530 int inc = (dmax-dmin+1)/nticks; if(inc<1) inc=1;
531 //cout<<" dmin="<<dmin<<" dmax="<<dmax<<" inc="<<inc<<endl;
532
533 majticks.resize(0);
534 {for(int i=dmin;i<=dmax;i+=inc) {
535 double x = pow(10.,(double)i);
536 if(x<xmin || x>xmax) continue;
537 majticks.push_back(x);
538 }}
539 //cout<<"majticks.size()="<<majticks.size()<<endl;
540
541 // Pas de puissance de 10 dans l'intervalle on garde BestTicks
542 if(majticks.size()==0) {
543 BestTicks(xmin,xmax,nticks,majticks,minticks);
544 return;
545 }
546
547 // Pas suffisamment de ticks majeurs
548 if((int)majticks.size()<=nticks/2) {
549 int nins = nticks/(majticks.size()+1);
550 if(nins<=0) nins=1;
551 //cout<<"nins="<<nins<<endl;
552 // Sequence judicieuse pour remplir les ticks manquants
553 // nins = 1 on insere 3
554 // 2 2 5
555 // 3 2 4 6
556 // 4 1.5 2 4 6
557 // >=5 on reste au cas precedent
558 double seqmaj[4][4] = {{3.,0,0,0},{2.,5.,0,0},{2.,4.,6.,0},{1.5,2.,4.,6.}};
559 if(nins>4) nins=4;
560 vector<double> tmp;
561 {for(unsigned int i=0;i<=majticks.size();i++) {
562 double xt;
563 if(i<majticks.size()) xt = majticks[i]/10.;
564 else xt = majticks[i-1];
565 for(int n=0;n<nins;n++) {
566 double xins = seqmaj[nins-1][n]*xt;
567 if(xins<xmin || xins>xmax) continue;
568 tmp.push_back(xins);
569 }
570 if(i<majticks.size()) tmp.push_back(majticks[i]);
571 }}
572 majticks = tmp;
573 }
574 //cout<<"...majticks.size()="<<majticks.size()<<endl;
575
576 // Les ticks mineurs
577 minticks.resize(0);
578 {for(unsigned int i=0;i<majticks.size()-1;i++) {
579 double dx = (majticks[i+1]-majticks[i])/10.;
580 minticks.push_back(majticks[i]);
581 for(int j=2;j<=8;j+=2) {
582 double x = majticks[i] + j*dx;
583 if(x<xmin || x>xmax) continue;
584 minticks.push_back(x);
585 }
586 }}
587 minticks.push_back(majticks[majticks.size()-1]);
588 //cout<<"...minticks.size()="<<minticks.size()<<endl;
589
590 // Si on a xmin<=0., on insere zero dans les ticks majeurs
591 if(xmin<=0.) {
592 vector<double> tmp = majticks;
593 majticks.resize(0); majticks.push_back(0.);
594 for(unsigned int i=0;i<tmp.size();i++) majticks.push_back(tmp[i]);
595 //cout<<"...xmin="<<xmin<<"<=0. add majticks[0]="<<majticks[0]<<endl;
596 }
597
598}
599
600/* --Methode-Static-- */
601int PIAxes::BonFormatAxes(double xmin,double xmax,double xstep
602 ,string& format,int typf,int add_digit)
603// *** Calcul format optimal pour ecrire les labels numeriques des axes:
604// ---- Input
605// . xmin,xmax : limites du plot sur l'axe considere.
606// . xstep : distance entre les ticks.
607// . add_digit : nombre de digits a ajouter au nombre de digits minimum.
608// . typf : type de format en sortie
609// 0 : format optimum %-nn.mme ou %-nn.mmf selon valeurs
610// 1 : format %-nn.mme
611// 2 : format %-nn.mmf pour imprimer x/10^npuiss
612// tel que x/10^npuiss soit entre [0,10]
613// 3 : format %-nn.mmf
614// ---- Output
615// . format : le format d'impression
616// ---- Return:
617// Si typ=0 ou 1 ou 3
618// "ndig" : nombre de digits necessaires pour distinguer
619// les valeurs xmin+k*dx (<=xmax)
620// Si typ=2
621// "npuiss" : tel que x/10^npuiss soit entre 0 et 10
622// Dans ce cas le format est celui qui imprime x/10^npuiss
623{
624 format = "%-5g"; // format par default
625 if(xmin>=xmax) {if(typf==2) return 0; else return -1;}
626
627 if(xstep<=0. || xstep>xmax-xmin) xstep = xmax-xmin;
628
629 double axmin=fabs(xmin), axmax=fabs(xmax);
630 if(axmin>axmax) dble_SWAP(axmin,axmax);
631
632 double l10amax = log10(axmax), l10xstep = log10(xstep);
633 int il10amax = int(floor(l10amax));
634
635 // choix du type de format
636 char ftype = 'e';
637 int npuiss = 0;
638 if(typf==2) {
639 npuiss = il10amax-1; // nombre entre [0,10]
640 //npuiss = il10amax; // nombre entre [0,1]
641 if(npuiss<-300 || npuiss>300) {
642 ftype='e'; npuiss=0;
643 } else {
644 // On recalcule les valeurs de decision pour axmax/10^npuiss, xstep/10^npuiss
645 l10amax -= (double)npuiss; l10xstep -= (double)npuiss;
646 il10amax = int(floor(l10amax));
647 ftype = 'f';
648 }
649 } else if(typf==1) {
650 ftype='e';
651 } else if(typf==3) {
652 ftype='f';
653 } else {
654 ftype='e';
655 // On evite d'ecrire +a.bbbe+ccc -> format %f
656 // Ex: 1.2345e+2 -> 123.45 / -1.2345e+2 -> -123.45
657 // 1.2345e-1 -> 0.12345 / -1.2345e-1 -> -0.12345
658 if((axmin>=1e-4 || axmin==0.) && axmax<1e4) ftype='f';
659 }
660
661 //printf("BonFormatAxes[npuiss=%d]: xmin=%-21.14e xmax=%-21.14e\n",npuiss,xmin,xmax);
662 //printf(" xstep=%-21.14e log10(xstep/10^%d)=%g\n",xstep,npuiss,l10xstep);
663 //printf(" axmax=%-21.14e log10(axmax/10^%d)=%g diff=%g\n"
664 // ,axmax,npuiss,l10amax,l10amax-l10xstep);
665
666 // Nombre de digits necessaires pour ecrire axmax et xstep
667 int ndig = il10amax - int(floor(l10xstep));
668 if(ndig<0) ndig *= -1; ndig += 1;
669 //printf("ndig=%d",ndig);
670
671 // Add more digits (or suppress digits)
672 ndig += add_digit; if(ndig<0) ndig=0;
673 //printf(" + %d ==> ndig=%d\n",add_digit,ndig);
674
675 // Calcul du bon format
676 char str[16];
677 if(ftype=='f') { // Calcul du format %-nn.mmf
678 int mm=-1, nn;
679 if(il10amax<0) { // +0.12345 +0.0012345 +0.0012345 ...
680 mm = ndig - il10amax - 1; nn = mm+3;
681 } else { // +1.2345 +12.345 +123.45 ...
682 mm = ndig - il10amax - 1; nn = ndig+2;
683 }
684 //printf("format %%f : mm=%d nn=%d\n",mm,nn);
685 if(mm<0.) mm=0; if(nn<mm+3) nn=mm+3;
686 sprintf(str,"%%-%d.%df",nn,mm);
687 } else if(ftype=='e') { // Calcul du format %-nn.mme
688 // +d.<--ddd-->e+123
689 // 1 2 34567 ==> nn=ndig+7 mm=ndig-1
690 sprintf(str,"%%-%d.%de",ndig+7,ndig-1);
691 }
692
693 format = str;
694 //printf("format=[%s]\n",format.c_str());
695
696 if(typf==2) return npuiss;
697 return ndig;
698}
699
700/* --Methode-- */
701int PIAxes::Le_Bon_Format(vector<double>& xticks,string& format,double& xstep)
702// Methode static de decision du bon format
703// Decide quel format est le mieux adapte pour ecrire les labels des axes
704// Decide si une puissance de 10 doit etre deportee en bout d'axe
705// - Input:
706// xticks : vecteur des ticks a ecrire (calcule par BestTicks)
707// - Output:
708// format : format a utiliser
709// xstep : step entre 2 graduations
710// - Return:
711// npuiss : si format choisit avec ecriture
712// avec label des puissances de 10 deporte
713// 0 : sinon
714{
715 format="%g"; xstep=1.;
716 if(xticks.size()<=1) return 0;
717
718 // On decide du format
719 xstep=xticks[1]-xticks[0];
720 int npuiss = BonFormatAxes(xticks[0],xticks[xticks.size()-1],xstep,format,2,1);
721 if(npuiss>=-2 && npuiss<=2) {
722 npuiss = 0;
723 BonFormatAxes(xticks[0],xticks[xticks.size()-1],xstep,format,3,1);
724 }
725
726 return npuiss;
727}
728
729void PIAxes::Arrange_Label(char *label)
730// --- Mise en forme optimale du label numerique
731// Enleve les blancs, les zeros, le point et les e00
732// inutiles a la fin d'un label
733{
734 size_t lenl=strlen(label);
735 if(lenl==0) return;
736
737 // --- On enleve les blancs et plus au debut du label
738 if(label[0]==' ' || label[0]=='+') {
739 char *str=new char[lenl+2];
740 strcpy(str,label);
741 unsigned i=0;
742 for(i=0;i<lenl;i++) if(str[i]!=' ' && str[i]!='+') break;
743 strcpy(label,&str[i]);
744 delete [] str;
745 lenl=strlen(label);
746 if(lenl==0) return;
747 }
748
749 // --- On enleve les blancs a la fin du label
750 if(label[lenl-1]==' ') {
751 for(int i=lenl-1;i>=0;i--) {
752 if(label[i]!=' ') break;
753 label[i]='\0';
754 }
755 lenl=strlen(label);
756 }
757
758 // --- On enleve les e... E... non-significatifs
759 // ex: a.be-zzz a.be+zzz a.bezzz avec zzz=0,00,000,...
760 // Attention on n'enleve pas si: a.be+10
761 // Attention on ne fait rien si: e+10
762 char* e=index(label,'e');
763 if(e==NULL) e=index(label,'E');
764 if(e) {
765 for(int i=lenl-1;i>=0;i--) {
766 if(isdigit(label[i]) && label[i]!='0') break;
767 if(label[i]=='e' || label[i]=='E')
768 {label[i]='\0'; lenl=strlen(label); break;}
769 }
770 }
771
772 // --- On enleve les zeros non-significatifs a la fin du label
773 // On enleve des zeros ou le point si: ab. ab.czzz avec zzz=0,00,000,...
774 // Attention on n'enleve pas de zeros si: abzzz
775 // Attention a ne pas enlever des zeros si on a ab.ccce+a0
776 // Attention on traite 0eaaa -> 0
777 if(index(label,'.')==NULL) return; // Recherche d'un point
778 string stre;
779 if(e) {if(e==label) return; stre=e; *e='\0'; lenl=strlen(label);}
780 {for(int i=lenl-1;i>=0;i--) {
781 if(label[i]=='0') label[i]='\0';
782 else if(label[i]=='.') {
783 if(i>0) label[i]='\0';
784 else {
785 // Attention: ".e+10" -> "1e+10" MAIS "." -> "0"
786 if(e) strcpy(label,"1"); else strcpy(label,"0");
787 }
788 break;
789 }
790 else break; // Ni un point ni un zero
791 }}
792 if(e) {
793 if(strlen(label)==1) if(label[0]=='0') return;
794 strcat(label,stre.c_str());
795 }
796
797}
Note: See TracBrowser for help on using the repository browser.