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

Last change on this file since 2793 was 2793, checked in by ansari, 20 years ago

Petites corrections liees au trace d'axes - Reza 2 Juin 2005

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