// Module PI : Peida Interactive PIAxes // Methodes de trace des axes // R. Ansari - 2002 // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA #include "machdefs.h" #include "piaxes.h" #include #include inline void dble_SWAP(double a, double b) {double tmp=a; a=b; b=tmp;} /* --Methode-- */ PIAxes::PIAxes() { } /* --Methode-- */ PIAxes::~PIAxes() { } /* --Methode-- */ void PIAxes::DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt, unsigned int flags, bool afsz) { PIGrCoord xmin, xmax, ymin, ymax; g->GetGrSpace(xmin, xmax, ymin, ymax); DrawXYAxes(g, gratt, flags, afsz, (double)xmin, (double)xmax, (double)ymin, (double)ymax); } /* --Methode-- */ void PIAxes::DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt, unsigned int flags, bool afsz, double xmin, double xmax, double ymin, double ymax) { Setup(g, xmin, xmax, ymin, ymax); // g->NoClip(); if (gratt.GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine); else g->SelLine(gratt.GetLineAtt()); if (gratt.GetColor() == PI_NotDefColor) g->SelForeground(PI_Black); else g->SelForeground(gratt.GetColor()); if(afsz) { double fsz = xMajTickLen*3.5; g->SelFontSz(fsz); } if (flags & kStdAxes) { // Les axes g->DrawLine(xMin, (yMin+yMax)/2., xMax, (yMin+yMax)/2.); g->DrawLine((xMin+xMax)/2., yMin, (xMin+xMax)/2., yMax); // La grille en pointilles if (flags & kGridOn) DrawGrid(g); // Les ticks majeurs if (flags & kMajTicks) { DrawHTicks(g, (yMin+yMax)/2., xMajTickLen, xMajTickLen, xFirstMajTick, xMajTickStep); DrawVTicks(g, (xMin+xMax)/2., yMajTickLen, yMajTickLen, yFirstMajTick, yMajTickStep); } // Les ticks mineurs if (flags & kMinTicks) { DrawHTicks(g, (yMin+yMax)/2., xMinTickLen, xMinTickLen, xFirstMinTick, xMinTickStep); DrawVTicks(g, (xMin+xMax)/2., yMinTickLen, yMinTickLen, yFirstMinTick, yMinTickStep); } // Les labels if (flags & kLabels) { if (!aYdir) DrawHLabels(g, (yMin+yMax)/2.-xMajTickLen*2, xFirstMajTick, xMajTickStep,PI_VerticalTop); else DrawHLabels(g, (yMin+yMax)/2.+xMajTickLen*2, xFirstMajTick, xMajTickStep,PI_VerticalTop); if (!aXdir) DrawVLabels(g, (xMin+xMax)/2.-yMajTickLen*2, yFirstMajTick, yMajTickStep,PI_HorizontalRight); else DrawVLabels(g, (xMin+xMax)/2.+yMajTickLen*2, yFirstMajTick, yMajTickStep,PI_HorizontalRight); } } if (flags & kBoxAxes) { // La boite g->DrawLine(xMin, yMin, xMax, yMin); g->DrawLine(xMax, yMin, xMax, yMax); g->DrawLine(xMax, yMax, xMin, yMax); g->DrawLine(xMin, yMax, xMin, yMin); // Longueur des ticks double extXMajTickLen = flags&kExtTicks ? xMajTickLen : 0; double intXMajTickLen = flags&kIntTicks ? xMajTickLen : 0; double extXMinTickLen = flags&kExtTicks ? xMinTickLen : 0; double intXMinTickLen = flags&kIntTicks ? xMinTickLen : 0; double extYMajTickLen = flags&kExtTicks ? yMajTickLen : 0; double intYMajTickLen = flags&kIntTicks ? yMajTickLen : 0; double extYMinTickLen = flags&kExtTicks ? yMinTickLen : 0; double intYMinTickLen = flags&kIntTicks ? yMinTickLen : 0; // La grille en pointilles if (flags & kGridOn) DrawGrid(g); // Les ticks majeurs if (flags & kMajTicks) { DrawHTicks(g, yMin, intXMajTickLen, extXMajTickLen, xFirstMajTick, xMajTickStep); DrawHTicks(g, yMax, extXMajTickLen, intXMajTickLen, xFirstMajTick, xMajTickStep); DrawVTicks(g, xMin, extYMajTickLen, intYMajTickLen, yFirstMajTick, yMajTickStep); DrawVTicks(g, xMax, intYMajTickLen, extYMajTickLen, yFirstMajTick, yMajTickStep); } // Les ticks mineurs if (flags & kMinTicks) { DrawHTicks(g, yMin, intXMinTickLen, extXMinTickLen, xFirstMinTick, xMinTickStep); DrawHTicks(g, yMax, extXMinTickLen, intXMinTickLen, xFirstMinTick, xMinTickStep); DrawVTicks(g, xMin, extYMinTickLen, intYMinTickLen, yFirstMinTick, yMinTickStep); DrawVTicks(g, xMax, intYMinTickLen, extYMinTickLen, yFirstMinTick, yMinTickStep); } // Les labels if (flags & kLabels) { if (!aYdir) { DrawHLabels(g, g->DeltaUCY(yMin, -xMajTickLen*2), xFirstMajTick, xMajTickStep,PI_VerticalTop); //DrawHLabels(g, g->DeltaUCY(yMax, xMajTickLen*2), xFirstMajTick, xMajTickStep,PI_VerticalBottom); } else { //DrawHLabels(g, g->DeltaUCY(yMin, -xMajTickLen*2), xFirstMajTick, xMajTickStep,PI_VerticalBottom); DrawHLabels(g, g->DeltaUCY(yMax, xMajTickLen*2), xFirstMajTick, xMajTickStep,PI_VerticalTop); } if (!aXdir) { DrawVLabels(g, g->DeltaUCX(xMin, -yMajTickLen*2), yFirstMajTick, yMajTickStep,PI_HorizontalRight); //DrawVLabels(g, g->DeltaUCX(xMax, yMajTickLen*2), yFirstMajTick, yMajTickStep,PI_HorizontalLeft); } else { //DrawVLabels(g, g->DeltaUCX(xMin, -yMajTickLen*2), yFirstMajTick, yMajTickStep,PI_HorizontalLeft); DrawVLabels(g, g->DeltaUCX(xMax, yMajTickLen*2), yFirstMajTick, yMajTickStep,PI_HorizontalRight); } } } g->Clip(); } /* --Methode-- */ void PIAxes::Setup(PIGraphicUC* g, double xmin, double xmax, double ymin, double ymax) { xMin = xmin; xMax = xmax; yMin = ymin; yMax = ymax; aXdir = g->isAxeXDirRtoL(); aYdir = g->isAxeYDirUpDown(); aXlog = g->isLogScaleX(); aYlog = g->isLogScaleY(); int ntick_x = (aXlog) ? 6 : 10; BestTicks(xMin,xMax,ntick_x,xMajTickStep,xFirstMajTick); xMinTickStep = xMajTickStep/5; xFirstMinTick = floor(xMin / xMinTickStep) * xMinTickStep; if(xFirstMinTick < xMin) xFirstMinTick += xMinTickStep; int ntick_y = (aYlog) ? 6 : 12; BestTicks(yMin,yMax,ntick_y,yMajTickStep,yFirstMajTick); yMinTickStep = yMajTickStep/5; yFirstMinTick = floor(yMin / yMinTickStep) * yMinTickStep; if(yFirstMinTick < yMin) yFirstMinTick += yMinTickStep; yMajTickLen = (xMax-xMin)/100; yMinTickLen = (xMax-xMin)/250; xMajTickLen = (yMax-yMin)/100; xMinTickLen = (yMax-yMin)/250; } /* --Methode-- */ void PIAxes::DrawHTicks(PIGraphicUC* g, double y, double tickUp, double tickDown, double xBeg, double xStep) { for (double x=xBeg; x<=xMax+xStep/10.; x += xStep) g->DrawLine(x, g->DeltaUCY(y, -tickDown), x, g->DeltaUCY(y, tickUp)); } /* --Methode-- */ void PIAxes::DrawVTicks(PIGraphicUC* g, double x, double tickLeft, double tickRight, double yBeg, double yStep) { for (double y=yBeg; y<=yMax; y += yStep) g->DrawLine(g->DeltaUCX(x, -tickLeft), y, g->DeltaUCX(x,tickRight), y); } /* --Methode-- */ void PIAxes::DrawHLabels(PIGraphicUC* g, double y, double xBeg, double xStep, unsigned long just) { // Choix du bon format pour les labels des axes; char label[64]; string format; double fac=1.; bool p10=false; int npuiss = BonFormatAxes(xBeg,xMax,xStep,format,2,1); if(npuiss<-2 || npuiss>3) {p10 = true; fac=pow(10.,(double)npuiss);} else BonFormatAxes(xBeg,xMax,xStep,format,0,1); double xOffset = 0; double xlastlabelfin = xBeg - 2.*(xMax-xBeg); for(double x=xBeg; x<=xMax+xStep/10.; x+=xStep) { //Attention erreur d'arrondi x->0 (on code 5.1698e-26 au lieu de 0) double xx = (fabs(x/xStep)<1.e-5) ? 0.: x; sprintf(label,format.c_str(),xx/fac); for(int kk=0;kk<64;kk++) if(label[kk]==' ') {label[kk]='\0'; break;} double largeur = g->CalcStringWidth(label); if(aXdir) xOffset = largeur/2; else xOffset=-largeur/2; if(x+xOffset > xlastlabelfin) { g->DrawString(x,y,label,PI_HorizontalCenter|just); xlastlabelfin = x + xOffset + 1.1*largeur; } } if(p10) { PIGrCoord asc,desc; double h = g->GetFontHeight(asc,desc); if((aYdir && (just&PI_VerticalBottom)) || (!aYdir && (just&PI_VerticalTop)) ) h = -h; double xm = (aXdir)? xMin: xMax; double ym = g->DeltaUCY(y,1.5*h); sprintf(label,"%d",npuiss); g->DrawCompString(xm,ym,"x 10",label,NULL,PI_HorizontalCenter|just); } } /* --Methode-- */ void PIAxes::DrawVLabels(PIGraphicUC* g, double x, double yBeg, double yStep, unsigned long just) { // Choix du bon format pour les labels des axes; char label[64]; string format; double fac=1.; bool p10=false; int npuiss = BonFormatAxes(yBeg,yMax,yStep,format,2,1); if(npuiss<-2 || npuiss>3) {p10 = true; fac=pow(10.,(double)npuiss);} else BonFormatAxes(yBeg,yMax,yStep,format,0,1); for(double y=yBeg; y<=yMax; y += yStep) { double yy = (fabs(y/yStep)<1.e-5) ? 0.: y; sprintf(label,format.c_str(),yy/fac); for(int kk=0;kk<64;kk++) if(label[kk]==' ') {label[kk]='\0'; break;} g->DrawString(x,y,label,PI_VerticalCenter|just); } if(p10) { PIGrCoord asc,desc; double h = g->GetFontHeight(asc,desc); if(aYdir) h = -h; double ym = (aYdir)? yMin: yMax; ym = g->DeltaUCY(ym,h); sprintf(label,"%d",npuiss); g->DrawCompString(x,ym,"x 10",label,NULL,PI_VerticalBottom|just); } } /* --Methode-- */ void PIAxes::DrawGrid(PIGraphicUC* g) { PILineAtt savlineatt = g->GetLineAtt(); // g->SelLine(PI_ThinDashedLine); g->SelLine(PI_ThinDottedLine); for (double x=xFirstMajTick; x<=xMax; x += xMajTickStep) g->DrawLine(x, yMin, x, yMax); for (double y=yFirstMajTick; y<=yMax; y += yMajTickStep) g->DrawLine(xMin, y, xMax, y); g->SelLine(savlineatt); } /////////////////////////////////////////////////////////////////////////// /* --Methode-Static-- */ void PIAxes::BestTicks(double xmin,double xmax,int nticks ,double& steptick,double& xfirsttick) // *** Calcul de l'intervalle entre les ticks et de la valeur du premier tick { double d=xmax-xmin; if(d<1.e-100) d=1.e-100; //if (d<1.e-39) d=1.e-39; double ld = log10(d); double fld = floor( ((ld<0.)? -ld: ld) ); double del,del0; if(ld>=0.) {fld-=1.; del0=del=pow(10.,fld);} else {fld+=2.; del0=del=pow(10.,-fld);} // *** Intervalle entre les ticks // xmin xmax d ld fld -->fld del0 // 1 1500 1499 3.17 3 2 10^2 // 1 9500 9499 3.98 3 2 10^2 // 1 1.005 0.005 -2.3 2 4 10^-4 // 1 1.995 0.995 -0.0022 0 2 10^-2 // - Et recherche de la valeur del={del0,2*del0,...,20*del0} // telle que "nticks*del" soit le plus petit nombre ">=d" // Par exemple, si nticks=10: // 1-/ pour le 2sd cas ou d=9499 et del0=100 : // del = 100 200 500 1000 2000 // nticks*del = 1000 2000 5000 10000 20000 // d/del = 94.9 47.4 18.9 | 9.499 4.749 ==> majt = 1000 // 2-/ pour le 3ieme cas ou d=5e-3 et del0=1e-4 : // del = 1e-4 2e-4 5e-4 1e-3 2e-3 // nticks*del = 1e-3 2e-3 5e-3 1e-2 2e-2 // d/del = 50 25 | 10 5 2.5 ==> majt = 5e-4 int k=0; double fac[4] = {2.,5.,10.,20.}; while(d/del>(double)nticks && k<4 ) {del=fac[k]*del0; k++;} steptick=del; //*** Valeur du premier tick xfirsttick = floor(xmin/steptick); if(xmin<0.) xfirsttick+=1.; xfirsttick *= steptick; if(xfirsttick=xmax) {if(typf==2) return 0; else return -1;} if(xstep<=0. || xstep>xmax-xmin) xstep = xmax-xmin; double axmin=fabs(xmin), axmax=fabs(xmax); if(axmin>axmax) dble_SWAP(axmin,axmax); double l10amax = log10(axmax), l10xstep = log10(xstep); int il10amax = int(floor(l10amax)); // choix du type de format char ftype = 'e'; int npuiss = 0; if(typf==2) { npuiss = il10amax; if(npuiss<-300 || npuiss>300) { ftype='e'; npuiss=0; } else { // On recalcule les valeurs de decision pour axmax/10^npuiss, xstep/10^npuiss l10amax -= (double)npuiss; l10xstep -= (double)npuiss; il10amax = int(floor(l10amax)); ftype = 'f'; } } else if(typf==1) { ftype='e'; } else { ftype='e'; // On evite d'ecrire +a.bbbe+ccc -> format %f // Ex: 1.2345e+2 -> 123.45 / -1.2345e+2 -> -123.45 // 1.2345e-1 -> 0.12345 / -1.2345e-1 -> -0.12345 if((axmin>=1e-4 || axmin==0.) && axmax<1e4) ftype='f'; } //printf("BonFormatAxes[npuiss=%d]: xmin=%-21.14e xmax=%-21.14e\n",npuiss,xmin,xmax); //printf(" xstep=%-21.14e log10(xstep/10^%d)=%g\n",xstep,npuiss,l10xstep); //printf(" axmax=%-21.14e log10(axmax/10^%d)=%g diff=%g\n" // ,axmax,npuiss,l10amax,l10amax-l10xstep); // Nombre de digits necessaires pour ecrire axmax et xstep int ndig = il10amax - int(floor(l10xstep)); if(ndig<0) ndig *= -1; ndig += 1; //printf("ndig=%d",ndig); // Add more digits (or suppress digits) ndig += add_digit; if(ndig<0) ndig=0; //printf(" + %d ==> ndig=%d\n",add_digit,ndig); // Calcul du bon format char str[16]; if(ftype=='f') { // Calcul du format %-nn.mmf int mm=-1, nn; if(il10amax<0) { // +0.12345 +0.0012345 +0.0012345 ... mm = ndig - il10amax - 1; nn = mm+3; } else { // +1.2345 +12.345 +123.45 ... mm = ndig - il10amax - 1; nn = ndig+2; } //printf("format %%f : mm=%d nn=%d\n",mm,nn); if(mm<0.) mm=0; if(nne+123 // 1 2 34567 ==> nn=ndig+7 mm=ndig-1 sprintf(str,"%%-%d.%de",ndig+7,ndig-1); } format = str; //printf("format=[%s]\n",format.c_str()); if(typf==2) return npuiss; return ndig; }