vtkAxisExtended.cxx 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCellLocator.h

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkAxisExtended.h"
17 18

#include "vtkMath.h" // for VTK_DBL_EPSILON
19 20 21
#include "vtkStdString.h"
#include "vtkObjectFactory.h"

22
#include <sstream>
23

24 25
#include <cmath>
#include <algorithm>
26 27 28


vtkStandardNewMacro(vtkAxisExtended);
29 30 31

vtkAxisExtended::vtkAxisExtended()
{
32 33 34 35 36 37 38
  this->FontSize = 0;
  this->DesiredFontSize = 10;
  this->Precision = 3;
  this->LabelFormat = 0;
  this->Orientation = 0;
  this->LabelLegibilityChanged = true;
  this->IsAxisVertical = false;
39 40 41 42 43 44
}

vtkAxisExtended::~vtkAxisExtended()
{
}

45
// This method return a value to make step sizes corresponding to low q and j values more preferable
46 47
double vtkAxisExtended::Simplicity(int qIndex, int qLength, int j, double lmin,
                                   double lmax, double lstep)
48 49 50
{
  double eps = VTK_DBL_EPSILON * 100;
  int v = 1 ;
51
  ++qIndex;
52 53

  double rem = fmod(lmin,lstep);
54
  if((rem < eps || (lstep - rem ) < eps ) &&  lmin <= 0 && lmax >= 0)
55
    {
56
    v = 0;
57 58 59
    }
  else
    {
60
    v = 1;  // v is 1 is lebelling includes zero
61 62
    }

63
  return 1.0 - (qIndex - 1.0) / (qLength - 1.0) - j + v;
64 65
}

66 67
// This method returns the maximum possible value of simplicity value given q
// and j
68 69
double vtkAxisExtended::SimplicityMax(int qIndex, int qLength, int j)
{
70
  int v = 1;
71
  ++qIndex;
72
  return 1.0 - (qIndex - 1.0) / (qLength - 1.0) - j + v;
73 74
}

75 76 77 78
// This method makes the data range approximately same as the labeling range
// more preferable
double vtkAxisExtended::Coverage(double dmin, double dmax, double lmin,
                                 double lmax)
79
{
80 81
  double coverage = 1.0 - 0.5 * (pow(dmax - lmax, 2) + pow(dmin - lmin, 2)
                                 / pow(0.1 * (dmax - dmin), 2));
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  return coverage;
}


//This gives the maximum possible value of coverage given the step size
double vtkAxisExtended::CoverageMax(double dmin, double dmax, double span)
{
  double range = dmax - dmin;
  if (span > range)
    {
    double half = (span - range)/2;
    return 1- 0.5 * (pow(half, 2) + pow(half, 2) / pow(0.1*(range),2));
    }
  else
    {
    return 1.0;
    }
}

101 102 103 104
// This method return a value to make the density of the labels close to the
// user given value
double vtkAxisExtended::Density(int k, double m, double dmin, double dmax,
                                double lmin, double lmax)
105 106 107 108
{
  double r = (k-1)/(lmax-lmin);
  double rt = (m-1) / (std::max(lmax,dmax) - std::min(dmin,lmin));

109
  return 2 - std::max(r/rt, rt/r);
110 111
}

112 113
// Derives the maximum values for density given k (number of ticks) and m
// (user given)
114 115 116 117
double vtkAxisExtended::DensityMax(int k, double m)
{
  if(k >= m)
    {
118
    return 2 - (k-1) / (m-1);
119 120 121 122 123 124 125 126
    }
  else
    {
    return 1;
    }
}

// This methods gives a weighing factor for each label depending on the range
127
// The coding for the different formats
128 129 130 131 132 133
//   1 - Scientific 5 * 10^6
//   2 - Decimal e.g. 5000
//   3 - K e.g. 5K
//   4 - Factored K e.g. 5(K)
//   5 - M e.g. 5M
//   6 - Factored M e.g. 5(M)
134
//   7 - Factored Decimals e.g. 5 (thousands)
135 136 137 138
//   8 - Factored Scientific 5 (10^6)
double vtkAxisExtended::FormatLegibilityScore(double n, int format)
{
  switch(format)
139
    {
140 141 142
    case 1:
      return 0.25;
    case 2:
143
      if(std::abs(n) > 0.0001 && std::abs(n) < 1000000)
144 145 146 147 148 149 150 151
        {
        return 1.0;
        }
      else
        {
        return 0.0;
        }
    case 3:
152
      if(std::abs(n) > 1000 &&  std::abs(n) < 1000000)
153 154 155 156 157 158 159 160
        {
        return 0.75;
        }
      else
        {
        return 0.0;
        }
    case 4:
161
      if(std::abs(n) > 1000 && std::abs(n) < 1000000)
162 163 164 165 166 167 168 169
        {
        return 0.4;
        }
      else
        {
        return 0.0;
        }
    case 5:
170
      if(std::abs(n) > 1000000 && std::abs(n) < 1000000000)
171 172 173 174 175 176 177 178
        {
        return 0.75;
        }
      else
        {
        return 0.0;
        }
    case 6:
179
      if(std::abs(n) > 1000000 && std::abs(n) < 1000000000)
180 181 182 183 184 185 186 187 188 189 190 191 192
        {
        return 0.4;
        }
      else
        {
        return 0.0;
        }
    case 7:
      return 0.5;
    case 8:
      return 0.3;
    default:
      return 0.0;
193
    }
194 195 196 197 198 199
}


// This method returns the length of the label given the format
int vtkAxisExtended::FormatStringLength(int format, double n, int precision)
{
200
  std::ostringstream ostr;
201
  ostr.imbue(std::locale::classic());
202
  int numSize(0);
203 204

  switch(format)
205
    {
206 207
    case 1:
      ostr.precision(precision);
208
      ostr.setf(std::ios::scientific, std::ios::floatfield);
209 210 211 212 213 214 215 216 217
      ostr<<n;
      numSize = (int) ostr.str().length();
      return numSize;
    case 2:
      ostr << n;
      if((std::ceil(n)-std::floor(n)) != 0.0 )
        {
        ostr.precision(precision);
        }
218 219 220
      // Gets the length of the string with the current format without the end
      // character
      numSize = (int) ostr.str().length()-1;
221 222 223 224 225 226 227 228 229 230 231 232 233
      return numSize;
    case 3:
      ostr.setf(ios::fixed, ios::floatfield);
      ostr << n/1000;
      if((std::ceil(n/1000.0)-std::floor(n/1000.0)) != 0.0 )
        {
        ostr.precision(precision);
        }
      numSize = (int) ostr.str().length()-1;
      return numSize+1; // minus three zeros + K
    case 4:
      ostr.setf(ios::fixed, ios::floatfield);
      ostr << n/1000;
234
      if((std::ceil(n/1000.0)-std::floor(n/1000.0)) != 0.0)
235 236 237
        {
        ostr.precision(precision);
        }
238
      numSize = static_cast<int>(ostr.str().length() - 1);
239 240 241 242
      return numSize; // minus three zeros
    case 5:
      ostr.setf(ios::fixed, ios::floatfield);
      ostr << n/1000000;
243
      if((std::ceil(n/1000000.0) - std::floor(n/1000000.0)) != 0.0)
244 245 246 247
        {
        ostr.precision(precision);
        }
      numSize = (int) ostr.str().length()-1;
248
      return numSize; // minus six zeros
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    case 6:
      ostr.setf(ios::fixed, ios::floatfield);
      ostr << n/1000000;
      if((std::ceil(n/1000000.0)-std::floor(n/1000000.0)) != 0.0 )
        {
        ostr.precision(precision);
        }
      numSize = (int) ostr.str().length()-1;
      return numSize+1; // minus six zeros + M
    case 7:
      ostr.setf(ios::fixed, ios::floatfield);
      ostr << n/1000;
      if((std::ceil(n/1000.0)-std::floor(n/1000.0)) != 0.0 )
        {
        ostr.precision(precision);
        }
      numSize = (int) ostr.str().length()-1;
      return numSize;  // Three 0's get reduced
    case 8:
      ostr.precision(precision);
269
      ostr.setf(std::ios::scientific, std::ios::floatfield);
270 271 272 273 274
      ostr<<n/1000;
      numSize = (int) ostr.str().length();
      return numSize;
    default:
      return 0;
275
    }
276 277
}

278 279
// This methods determines the optimum notation, font size and orientation of
// labels from an exhaustive search
280 281 282
double vtkAxisExtended::Legibility(double lmin, double lmax, double lstep,
                                   double scaling,
                                   vtkVector<int, 3>& parameters)
283
{
284
  int numTicks = static_cast<int>((lmax - lmin) / lstep);
285
  double* tickPositions = new double[numTicks];
286 287
  int fontSizes[8] = { 8, 9, 10, 12, 14, 18, 20, 24 };
  for(int i = 0; i< numTicks; ++i)
288 289 290 291
    {
    tickPositions[i] = lmax + i*lstep;
    }

292
 // this->LabelLegibilityChanged = true;
293 294 295 296 297 298
  int bestFormat = 1;
  int bestOrientation = 0;
  int bestFontSize = this->DesiredFontSize;

  double bestLegScore = 0.0;

299 300
  for(int iFormat = 1; iFormat < 9; ++iFormat)
    {
301
    double formatLegSum = 0.0;
302
    for(int i = 0; i<numTicks; ++i)
303 304 305 306
      {
      formatLegSum += FormatLegibilityScore(tickPositions[i], iFormat);
      }

307 308
    // Average of label legibility scores
    formatLegSum = formatLegSum / numTicks;
309 310 311 312

    double eps = VTK_DBL_EPSILON * 100;
    int v = 1 ;
    double rem = fmod(lmin,lstep);
313
    if((rem < eps || (lstep - rem ) < eps ) &&  lmin <=0 && lmax >=0)
314 315 316 317 318 319 320 321
      {
      v = 0;
      }
    else
      {
      v = 1;  // v is 1 is lebelling includes zero
      }

322 323
    formatLegSum = 0.9 * formatLegSum + 0.1 * v;

324
    double fontLegSum = 0.0;
325 326 327

    // 8 font sizes are checked
    for (int fontIndex = 0; fontIndex < 8 ; ++fontIndex)
328 329 330 331 332 333
      {
      int iFont = fontSizes[fontIndex];
      if(iFont == this->DesiredFontSize)
        {
        fontLegSum = 1.0;
        }
334 335
      // fontSizes[0] is the minimum font size
      else if ( iFont<this->DesiredFontSize && iFont >= fontSizes[0])
336
        {
337 338
        fontLegSum = 0.2 * (iFont - fontSizes[0] + 1)
            / (this->DesiredFontSize - fontSizes[0]);
339 340 341
        }
      else
        {
342
        fontLegSum = -100.0;
343 344
        }

345 346 347
      for(int iOrientation = 0 ; iOrientation <2 ; ++iOrientation)
        {
        double orientLegSum = (iOrientation == 0) ? 1 : -0.5;
348
        // Here the gap between two consecutive labels is calculated as:
349 350
        // 2*Actual distance (in pixels) among two ticks - string lengths of
        // the two largest labels
351 352
        double overlapLegSum = 1.0;

353
        double legScore = (formatLegSum + fontLegSum + orientLegSum
354
                           + overlapLegSum) / 4;
355 356 357 358 359
        if(legScore > bestLegScore )
          {
          if(numTicks>1)
            {
            double fontExtent;
360 361
            if((this->IsAxisVertical && iOrientation) ||
               (!this->IsAxisVertical && !iOrientation) )
362
              {
363 364 365 366 367
              fontExtent =
                  (FormatStringLength(iFormat,tickPositions[numTicks-1],
                                      this->Precision) +
                   FormatStringLength(iFormat,tickPositions[numTicks-2],
                                      this->Precision))*iFont;
368 369 370
              }
            else
              {
371
              fontExtent = iFont * 2;
372 373 374
              }
            double tickDistance = lstep * scaling;
            double labelingGap= 2*(tickDistance) - fontExtent;
375 376
            // 1.1 for line spacing
            overlapLegSum = std::min(1.0,2 - 3* iFont *1.1 / labelingGap );
377 378 379 380 381 382 383 384
            /*if(labelingGap > 3*iFont)
              {
              overlapLegSum = 1.0;
              }
            else if(labelingGap < 3*iFont && labelingGap > 0)
              {
              overlapLegSum = 2 - 3* iFont / labelingGap ;
              }
385
            else
386 387 388 389 390
              {
              overlapLegSum = -100;
              }*/
            }

391 392
          legScore = (formatLegSum + fontLegSum + orientLegSum +
                      overlapLegSum)/4;
393 394 395 396 397 398 399 400 401 402 403

          if ( legScore > bestLegScore)
            {
            bestFormat = iFormat;
            bestOrientation = iOrientation;
            bestFontSize = iFont;
            bestLegScore = legScore;
            }
          }
        }
      }
404
    }
405

406 407 408
  parameters[0] = bestFormat;
  parameters[1] = bestFontSize;
  parameters[2] = bestOrientation;
409
  delete [] tickPositions;
410
  return bestLegScore;
411 412 413
}

// This method implements the algorithm given in the paper
414 415 416
vtkVector3d vtkAxisExtended::GenerateExtendedTickLabels(double dmin,
                                                        double dmax, double m,
                                                        double scaling)
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
{
  double Q[] = {1, 5, 2, 2.5, 4, 3};
  double w[] = {0.25, 0.2, 0.5, 0.05};
  double eps = VTK_DBL_EPSILON * 100;
  vtkVector3d ans;

  this->LabelLegibilityChanged = false;
  if(dmin > dmax)
    {
    double temp = dmin;
    dmin = dmax;
    dmax = temp;
    }

  if( dmax - dmin < eps)
    {
    ans[0] = dmin; ans[1]= dmax; ans[2]= m;
    //return Sequence(dmin,dmax, m);
    return ans;
    }

  int qLength = 6;//Q.Length(); // Hard Coded

440
  //list<double> best;
441
  double bestScore = -2;
442
  double bestLmin(0), bestLmax(0), bestLstep(0);
443 444 445

  const int INF = 100; //INT_MAX;  Again 100 is hard coded

446
  int j = 1;
447 448
  while(j < INF)
    {
449
    for(int qIndex = 0; qIndex < qLength; ++qIndex)
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
      {
      double sm = SimplicityMax(qIndex, qLength, j);
      if((w[0]*sm + w[1] + w[2] + w[3]) < bestScore)
        {
        j = INF;
        break;
        }

      int k = 2;
      while(k < INF)
        {
        double dm = DensityMax(k,m);
        if((w[0]*sm + w[1] + w[2]*dm + w[3]) < bestScore)
          {
          break;
          }
        double delta = (dmax- dmin)/((k+1)*j*Q[qIndex]) ;
        double z = ceil(log10(delta));
468
        while(z < INF)
469
          {
David Partyka's avatar
David Partyka committed
470
          double step = j*Q[qIndex]*pow(10.0,z);
471
          //double cm = CoverageMax(dmin, dmax, step*(k-1));
472 473 474 475 476
          if((w[0]*sm + w[1] + w[2]*dm + w[3]) < bestScore)
            {
            break;
            }

477 478
          int minStart = static_cast<int>(std::floor(dmax / step) * j - (k-1) * j);
          int maxStart = static_cast<int>(std::ceil(dmin/step) * j);
479 480 481

          if(minStart > maxStart)
            {
482
            ++z;
483 484 485
            continue;
            }

486
          for(int start = minStart; start <= maxStart; ++start)
487 488 489 490 491 492 493 494 495 496 497 498 499 500
            {
            double lmin = start * (step/j);
            double lmax = lmin + step*(k-1);
            double lstep = step;

            double s = Simplicity(qIndex, qLength, j, lmin, lmax, lstep);
            double c = Coverage(dmin, dmax, lmin, lmax);
            double g = Density(k,m,dmin, dmax, lmin, lmax);

            double score = w[0]*s + w[1]*c + w[2]*g + w[3];

            if(score < bestScore)
               continue;

501
            //vtkVector<double,4> l = this->Legibility(lmin, lmax, lstep, scaling);
502

503 504 505 506 507
            vtkVector<int, 3> legibilityIndex;
            double newScore = this->Legibility(lmin, lmax, lstep, scaling,
                                               legibilityIndex);

            score = w[0] * s + w[1] * c + w[2] * g + w[3] * newScore;
508 509 510 511 512 513 514

            if(score > bestScore)
              {
              bestScore = score;
              bestLmin = lmin;
              bestLmax = lmax;
              bestLstep = lstep;
515 516 517
              this->LabelFormat = legibilityIndex[0]; // label format
              this->FontSize = legibilityIndex[1]; // label font size
              this->Orientation = legibilityIndex[2]; // label orientation
518 519
              }
            }
520
          ++z;
521
          }
522
        ++k;
523 524
        }
      }
525
    ++j;
526 527 528 529 530 531 532
    }
  ans[0] = bestLmin;
  ans[1] = bestLmax;
  ans[2] = bestLstep;
  //vtkVector3d answers(bestLmin, bestLmax, bestLstep);
  // return Sequence(bestLmin, bestLmax, bestLstep);
  return ans;
533
}
534 535 536 537 538 539 540 541 542 543

void vtkAxisExtended::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Orientation: " << this->Orientation << endl;
  os << indent << "FontSize: " << this->FontSize << endl;
  os << indent << "DesiredFontSize: " << this->DesiredFontSize << endl;
  os << indent << "Precision: " << this->Precision << endl;
  os << indent << "LabelFormat: " << this->LabelFormat << endl;
}