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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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