Changeset 936 for trunk


Ignore:
Timestamp:
Mar 11, 2008, 10:40:37 AM (16 years ago)
Author:
gah
Message:

Axis.cpp cleanup

Location:
trunk/vizservers/nanovis
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/vizservers/nanovis/Axis.cpp

    r933 r936  
    8080}
    8181
    82 Ticks::Ticks(TickSweep &sweep)
     82void
     83Ticks::SetTicks(void)
    8384{
    8485    _nTicks = 0;
    85     fprintf(stderr, "Ticks nsteps=%d, step=%g, initial=%g\n",
    86             sweep.NumSteps(), sweep.Step(), sweep.Initial());
    87     _ticks = new float[sweep.NumSteps()];
    88     if (sweep.Step() == 0.0) {
     86    _ticks = new float[_nSteps];
     87    if (_step == 0.0) {
    8988        /* Hack: A zero step indicates to use log values. */
    9089        unsigned int i;
     
    102101            1.0
    103102        };
    104         for (i = 0; i < sweep.NumSteps(); i++) {
     103        for (i = 0; i < _nSteps; i++) {
    105104            _ticks[i] = logTable[i];
    106105        }
     
    109108        unsigned int i;
    110109   
    111         value = sweep.Initial(); /* Start from smallest axis tick */
    112         for (i = 0; i < sweep.NumSteps(); i++) {
    113             value = sweep.Initial() + (sweep.Step() * i);
    114             _ticks[i] = UROUND(value, sweep.Step());
    115             fprintf(stderr, "ticks[%d] = %g (value=%g)\n",
    116                     i, _ticks[i], value);
    117         }
    118     }
    119     _nTicks = sweep.NumSteps();
    120 }
    121 
    122 Axis::Axis(const char *name)
     110        value = _initial;       /* Start from smallest axis tick */
     111        for (i = 0; i < _nSteps; i++) {
     112            value = _initial + (_step * i);
     113            _ticks[i] = UROUND(value, _step);
     114        }
     115    }
     116    _nTicks = _nSteps;
     117}
     118
     119Axis::Axis(const char *name) :
     120    _major(5), _minor(2)
    123121{
    124122    _name = NULL;
    125123    SetName(name);
    126     _flags = AUTOSCALE_MAJOR | AUTOSCALE_MINOR;
    127124    _title = NULL;
    128125    _valueMin = DBL_MAX, _valueMax = -DBL_MAX;
     
    131128    _range = 0.0, _scale = 0.0;
    132129    _reqStep = 0.0;
    133     _reqNumMinorTicks = 2;
    134     _reqNumMajorTicks = 5;
    135     _reqStep = 0.0;
    136     _t1Ptr = _t2Ptr = NULL;
    137 }
    138 
    139 Axis::~Axis(void)
    140 {
    141     if (_t1Ptr != NULL) {
    142         delete _t1Ptr;
    143     }
    144     if (_t2Ptr != NULL) {
    145         delete _t2Ptr;
    146     }
    147     _minorTicks.Reset();
    148     _majorTicks.Reset();
    149130}
    150131
     
    331312             * decade.  Instead, treat the axis as a linear scale.  */
    332313            range = NiceNum(range, 0);
    333             majorStep = NiceNum(range / _reqNumMajorTicks, 1);
     314            majorStep = NiceNum(range / _major.reqNumTicks, 1);
    334315            tickMin = UFLOOR(tickMin, majorStep);
    335316            tickMax = UCEIL(tickMax, majorStep);
     
    364345        }
    365346    }
    366     _majorSweep.SetValues(majorStep, nMajor, floor(tickMin));
    367     _minorSweep.SetValues(minorStep, nMinor, minorStep);
     347    _major.SetValues(majorStep, nMajor, floor(tickMin));
     348    _minor.SetValues(minorStep, nMinor, minorStep);
    368349    _min = tickMin;
    369350    _max = tickMax;
     
    460441        } else {
    461442            range = NiceNum(range, 0);
    462             step = NiceNum(range / _reqNumMajorTicks, 1);
     443            step = NiceNum(range / _major.reqNumTicks, 1);
    463444        }
    464445       
     
    471452    fprintf(stderr, "LinearScale %s (nTicks=%d, step=%g)\n", _name,
    472453            nTicks, step);
    473     _majorSweep.SetValues(tickMin, step, nTicks);
     454    _major.SetValues(tickMin, step, nTicks);
    474455
    475456    /*
     
    488469    /* Now calculate the minor tick step and number. */
    489470
    490     if ((_reqNumMinorTicks > 0) && (_flags & AUTOSCALE_MAJOR)) {
    491         nTicks = _reqNumMinorTicks - 1;
     471    if ((_minor.reqNumTicks > 0) && (_minor.IsAutoScale())) {
     472        nTicks = _minor.reqNumTicks - 1;
    492473        step = 1.0 / (nTicks + 1);
    493474    } else {
     
    497478                                 * create minor log-scale tick marks.  */
    498479    }
    499     _minorSweep.SetValues(step, step, nTicks);
     480    _minor.SetValues(step, step, nTicks);
    500481    fprintf(stderr, "leaving LinearScale %s (min=%g, max=%g)\n",
    501482            _name, _min, _max);
    502483}
    503484
    504 
    505 void
    506 Axis::SweepTicks()
    507 {
    508     if (_flags & AUTOSCALE_MAJOR) {
    509         if (_t1Ptr != NULL) {
    510             delete _t1Ptr;
    511         }
    512         _t1Ptr = new Ticks(_majorSweep);
    513     }
    514     if (_flags & AUTOSCALE_MINOR) {
    515         if (_t2Ptr != NULL) {
    516             delete _t2Ptr;
    517         }
    518         _t2Ptr = new Ticks(_minorSweep);
    519     }
    520 }
    521485
    522486void
     
    529493        LinearScale();
    530494    }
    531     SweepTicks();
     495    _major.SweepTicks();
     496    _minor.SweepTicks();
    532497    MakeTicks();
    533498}
     
    537502Axis::MakeTicks(void)
    538503{
     504    _major.Reset();
     505    _minor.Reset();
    539506    int i;
    540 
    541     _majorTicks.Reset();
    542     _minorTicks.Reset();
    543     for (i = 0; i < _t1Ptr->NumTicks(); i++) {
    544         union {
    545             float x;
    546             void *clientData;
    547         } value;
     507    for (i = 0; i < _major.NumTicks(); i++) {
    548508        double t1, t2;
    549509        int j;
    550510       
    551         t1 = _t1Ptr->GetTick(i);
     511        t1 = _major.GetTick(i);
    552512        /* Minor ticks */
    553         for (j = 0; j < _t2Ptr->NumTicks(); j++) {
    554             t2 = t1 + (_majorSweep.Step() * _t2Ptr->GetTick(j));
     513        for (j = 0; j < _minor.NumTicks(); j++) {
     514            t2 = t1 + (_major.Step() * _minor.GetTick(j));
    555515            if (!InRange(t2)) {
    556516                continue;
    557517            }
    558518            if (t1 == t2) {
    559                 continue;               // Don't add duplicate minor ticks.
     519                continue;       // Don't add duplicate minor ticks.
    560520            }
    561             value.x = t2;
    562             _minorTicks.Append(value.clientData);
     521            _minor.Append(t2);
    563522        }
    564523        if (!InRange(t1)) {
    565524            continue;
    566525        }
    567         value.x = t1;
    568         _majorTicks.Append(value.clientData);
    569     }
    570 }
    571 
    572 bool
    573 Axis::FirstMajor(TickIter &iter)
    574 {
    575     ChainLink *linkPtr;
    576 
    577     linkPtr = _majorTicks.FirstLink();
    578     iter.SetStartingLink(linkPtr);
    579     return (linkPtr != NULL);
    580 }
    581 
    582 bool
    583 Axis::FirstMinor(TickIter &iter)
    584 {
    585     ChainLink *linkPtr;
    586 
    587     linkPtr = _minorTicks.FirstLink();
    588     iter.SetStartingLink(linkPtr);
    589     return (linkPtr != NULL);
     526        _major.Append(t1);
     527    }
    590528}
    591529
  • trunk/vizservers/nanovis/Axis.h

    r933 r936  
    33
    44#include "Chain.h"
     5
     6class Axis;
    57
    68class NaN {
     
    5658 * ----------------------------------------------------------------------
    5759 *
    58  * TickSweep --
    59  *
    60  *      Structure containing information where step intervalthe ticks (major or
     60 * Ticks --
     61 *
     62 *      Structure containing information where the ticks (major or
    6163 *      minor) will be displayed on the graph.
    6264 *
    6365 * ----------------------------------------------------------------------
    6466 */
    65 class TickSweep {
    66 private:
     67class Ticks {
     68    bool _autoscale;            /* Indicates if the ticks are autoscaled. */
     69    /*
     70     * Tick locations may be generated in two fashions
     71     */
     72    /*   1. an array of values provided by the user. */
     73    int _nTicks;                /* # of ticks on axis */
     74    float *_ticks;              /* Array of tick values (alloc-ed).  Right now
     75                                 * it's a float so we don't have to allocate
     76                                 * store for the list of ticks generated.  In
     77                                 * the future when we store both the tick
     78                                 * label and the value in the list, we may
     79                                 * extend this to a double.
     80                                 */
     81    /*
     82     *   2. a defined sweep of initial, step, and nsteps.  This in turn
     83     *      will generate the an array of values.
     84     */
    6785    double _initial;            /* Initial value */
    6886    double _step;               /* Size of interval */
    6987    unsigned int _nSteps;       /* Number of intervals. */
     88
     89    /*
     90     *This finally generates a list of ticks values that exclude duplicate
     91     * minor ticks (that are always represented by major ticks).  In the
     92     * future, the string representing the tick label will be stored in
     93     * the chain.
     94     */
     95    Chain _chain;               
     96
     97    void SetTicks(void);        /* Routine used internally to create the array
     98                                 * of ticks as defined by a given sweep. */
     99    void *GetClientData(float x) {
     100        union {
     101            float x;
     102            void *clientData;
     103        } value;
     104        value.x = x;
     105        return value.clientData;
     106    }
     107
    70108public:
    71     void SetValues(double initial, double step, unsigned int nSteps) {
    72         _initial = initial, _step = step, _nSteps = nSteps;
    73     }
    74     double Initial(void) {
    75         return _initial;
    76     }
    77     double Step(void) {
    78         return _step;
    79     }
    80     unsigned int NumSteps(void) {
    81         return _nSteps;
    82     }
    83 };
    84 
    85 /*
    86  * ----------------------------------------------------------------------
    87  *
    88  * Ticks --
    89  *
    90  *      Structure containing information where the ticks (major or
    91  *      minor) will be displayed on the graph.
    92  *
    93  * ----------------------------------------------------------------------
    94  */
    95 class Ticks {
    96 private:
    97     int _nTicks;                /* # of ticks on axis */
    98     float *_ticks;              /* Array of tick values (malloc-ed). */
    99 public:
    100     Ticks(TickSweep &sweep);
    101     Ticks(float *ticks, int nTicks) {
    102         _ticks = ticks, _nTicks = nTicks;
     109    int reqNumTicks;            /* Default number of ticks to be displayed. */
     110
     111    Ticks(int numTicks) {
     112        reqNumTicks = numTicks;
     113        _ticks = NULL;
     114        _nTicks = 0;
     115        _autoscale = true;
    103116    }
    104117    ~Ticks(void) {
     
    106119            delete [] _ticks;
    107120        }
     121        _chain.Reset();
     122       
     123    }
     124    void SetTicks(float *ticks, int nTicks) {
     125        _ticks = ticks, _nTicks = nTicks;
    108126    }
    109127    int NumTicks(void) {
     
    116134        return _ticks[i];
    117135    }
     136    void Reset(void) {
     137        _chain.Reset();
     138    }
     139    float Step() {
     140        return _step;
     141    }
     142    void Append (float x) {
     143        _chain.Append(GetClientData(x));
     144    }   
     145    void SetValues(double initial, double step, unsigned int nSteps) {
     146        _initial = initial, _step = step, _nSteps = nSteps;
     147    }
     148    bool IsAutoScale(void) {
     149        return _autoscale;
     150    }
     151    void SweepTicks(void) {
     152        if (_flags & AUTOSCALE) {
     153            if (_ticks != NULL) {
     154                delete [] _ticks;
     155            }
     156            SetTicks();
     157        }
     158    }
     159    bool FirstTick(TickIter &iter) {
     160        ChainLink *linkPtr;
     161
     162        linkPtr = _chain.FirstLink();
     163        iter.SetStartingLink(linkPtr);
     164        return (linkPtr != NULL);
     165    }
    118166};
     167
    119168
    120169/*
     
    131180    /* Flags associated with the axis. */
    132181    enum AxisFlags {
    133         AUTOSCALE_MAJOR=(1<<0),
    134         AUTOSCALE_MINOR=(1<<1),
    135         DESCENDING=(1<<2),
    136         LOGSCALE=(1<<3),
    137         TIGHT_MIN=(1<<4),
    138         TIGHT_MAX=(1<<5)
     182        AUTOSCALE=(1<<0),
     183        DESCENDING=(1<<1),
     184        LOGSCALE=(1<<2),
     185        TIGHT_MIN=(1<<3),
     186        TIGHT_MAX=(1<<4)
    139187    };
    140188
     
    164212    double _scale;              /* Scale factor for axis (1.0/_range) */
    165213
    166     double _reqStep;            /* If > 0.0, overrides the computed major
    167                                  * tick interval.  Otherwise a stepsize
    168                                  * is automatically calculated, based
    169                                  * upon the range of elements mapped to the
    170                                  * axis. The default value is 0.0. */
    171 
    172     Ticks *_t1Ptr;              /* Array of major tick positions. May be
    173                                  * set by the user or generated from the
    174                                  * major sweep below. */
    175 
    176     Ticks *_t2Ptr;              /* Array of minor tick positions. May be
    177                                  * set by the user or generated from the
    178                                  * minor sweep below. */
    179 
    180     TickSweep _minorSweep, _majorSweep;
    181    
    182     Chain _minorTicks, _majorTicks;
    183 
    184     int _reqNumMajorTicks;      /* Default number of ticks to be displayed. */
    185     int _reqNumMinorTicks;      /* If non-zero, represents the
    186                                  * requested the number of minor ticks
    187                                  * to be uniformally displayed along
    188                                  * each major tick. */
     214    double _reqStep;            /* If > 0.0, overrides the computed major tick
     215                                 * interval.  Otherwise a stepsize is
     216                                 * automatically calculated, based upon the
     217                                 * range of elements mapped to the axis. The
     218                                 * default value is 0.0. */
     219
     220    Ticks _major, _minor;
    189221
    190222    void LogScale(void);
     
    194226
    195227public:
     228    Axis(const char *name);
     229
    196230    void ResetRange(void);
    197231    void FixRange(double min, double max);
    198     Axis(const char *name);
    199     ~Axis(void);
    200     void SweepTicks(void);
    201232    void SetScale(double min, double max);
    202233    double Scale(void) {
     
    206237        return _range;
    207238    }
    208     bool FirstMinor(TickIter &iter);
    209     bool FirstMajor(TickIter &iter);
     239    bool FirstMajor(TickIter &iter) {
     240        return _major.FirstTick(iter);
     241    }
     242    bool FirstMinor(TickIter &iter) {
     243        return _minor.FirstTick(iter);
     244    }
    210245    void GetDataLimits(double min, double max);
    211246    double Map(double x);
     
    262297    }
    263298    void SetNumMinorTicksOption(int n) {
    264         _reqNumMinorTicks = n;
     299        _minor.reqNumTicks = n;
    265300    }
    266301    void SetNumMajorTicksOption(int n) {
    267         _reqNumMajorTicks = n;
     302        _major.reqNumTicks = n;
    268303    }
    269304};
    270305
    271 
    272 
    273306#endif /*_AXIS_H*/
  • trunk/vizservers/nanovis/Chain.h

    r934 r936  
    3030 * A ChainLink is the container structure for the Chain.
    3131 */
    32 
    33 
    3432class ChainLink {
    3533    friend class Chain;
Note: See TracChangeset for help on using the changeset viewer.