Last modified 27 June 2003 by jdavis@math.wisc.edu

SMKInterpolator

Inherits from NSObject
Conforms to NSCoding, NSCopying

SMKInterpolator provides a simple mechanism to help you control how much real time a program event requires. To use the interpolator, you set its duration and then start it. Subsequently you can query the interpolator to determine how much of its duration has passed. As an extra feature, you can set a tolerance to make the interpolator complete slightly early, which can be useful.

For example, say you're displaying a 3D scene. You want the camera to pan from one direction to another, and you want it to take half a second to do so. Set the interpolator's duration to 500,000 microseconds and start it. Then repeatedly draw the scene and query the interpolator. Use the result of the queries to determine how far through its panning the camera should be on each redrawing. Stop when a query returns 1.

Note that SMKInterpolator does not provide real-time facilities; there is no guarantee of precision for its timing results.

Before using SMKInterpolator, you must set its duration using the following methods.

- (id)setDuration:(unsigned int)duration;

Sets the interpolator's duration in microseconds. Note that the limit on the duration is about 4 billion microseconds, or about an hour.

- (unsigned int)duration;

Returns the duration, in microseconds.

To start (or restart) the interpolator's timer, use this method.

- (BOOL)start;

Starts the interpolator, returning YES if and only if no error occurred in creating a time stamp.

Subsequently, you can query the interpolator to see how much of its duration has passed using progress (which you can view as either a noun or a verb).

- (float)progress;

Returns the (approximate) fraction of the duration that has passed since the interpolator was started. This value is always between 0 and 1.

That's all you need to know to use SMKInterpolator. The class's only remaining feature is tolerance. Sometimes it is desirable to have the timer complete slightly early rather than slightly late. Returning to our example above, say we want to pan the camera for 0.5 seconds, and after 0.48 seconds of panning, we've redrawn the screen five times. That is, each redrawing is taking about 0.1 seconds, and we only have enough time left for one fifth of a redrawing. If this is too little time to be useful, we want progress to just report that the duration is over (a value of 1, rather than the true fraction, 0.96). If the tolerance is set above one fifth, then it will do just that.

In other words, let averageInterval be the average interval between successive queries to progress. (SMKInterpolator does indeed track this.) If the timer is within tolerance * averageInterval of the end of the duration, then progress will return 1.0.

- (id)setTolerance:(float)tolerance;

Sets the tolerance of the interpolator to the indicated value, which must be nonnegative. By default it is 0, so that progress returns true fractions of the duration.

- (float)tolerance;

Returns the tolerance.