2015-12-03 15:46:17 +00:00
|
|
|
/* Copyright (C) 2001-2015 Peter Selinger.
|
2010-06-10 18:43:12 +00:00
|
|
|
* This file is part of Potrace. It is free software and it is covered
|
|
|
|
* by the GNU General Public License. See the file COPYING for details. */
|
|
|
|
|
|
|
|
/* operations on potrace_progress_t objects, which are defined in
|
|
|
|
* potracelib.h. Note: the code attempts to minimize runtime overhead
|
|
|
|
* when no progress monitoring was requested. It also tries to
|
|
|
|
* minimize excessive progress calculations beneath the "epsilon"
|
|
|
|
* threshold. */
|
|
|
|
|
|
|
|
#ifndef PROGRESS_H
|
|
|
|
#define PROGRESS_H
|
|
|
|
|
|
|
|
/* structure to hold progress bar callback data */
|
|
|
|
struct progress_s
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
void (* callback)( double progress, void* privdata ); /* callback fn */
|
|
|
|
void* data; /* callback function's private data */
|
2010-06-10 18:43:12 +00:00
|
|
|
double min, max; /* desired range of progress, e.g. 0.0 to 1.0 */
|
|
|
|
double epsilon; /* granularity: can skip smaller increments */
|
|
|
|
double b; /* upper limit of subrange in superrange units */
|
|
|
|
double d_prev; /* previous value of d */
|
|
|
|
};
|
|
|
|
typedef struct progress_s progress_t;
|
|
|
|
|
|
|
|
/* notify given progress object of current progress. Note that d is
|
|
|
|
* given in the 0.0-1.0 range, which will be scaled and translated to
|
|
|
|
* the progress object's range. */
|
|
|
|
static inline void progress_update( double d, progress_t* prog )
|
|
|
|
{
|
|
|
|
double d_scaled;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
if( prog != NULL && prog->callback != NULL )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
d_scaled = prog->min * (1 - d) + prog->max * d;
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
if( d == 1.0 || d_scaled >= prog->d_prev + prog->epsilon )
|
|
|
|
{
|
|
|
|
prog->callback( prog->min * (1 - d) + prog->max * d, prog->data );
|
|
|
|
prog->d_prev = d_scaled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* start a subrange of the given progress object. The range is
|
|
|
|
* narrowed to [a..b], relative to 0.0-1.0 coordinates. If new range
|
|
|
|
* is below granularity threshold, disable further subdivisions. */
|
2015-12-03 15:46:17 +00:00
|
|
|
static inline void progress_subrange_start( double a,
|
|
|
|
double b,
|
|
|
|
const progress_t* prog,
|
|
|
|
progress_t* sub )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
double min, max;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
if( prog == NULL || prog->callback == NULL )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
sub->callback = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
min = prog->min * (1 - a) + prog->max * a;
|
|
|
|
max = prog->min * (1 - b) + prog->max * b;
|
|
|
|
|
|
|
|
if( max - min < prog->epsilon )
|
|
|
|
{
|
|
|
|
sub->callback = NULL; /* no further progress info in subrange */
|
|
|
|
sub->b = b;
|
|
|
|
return;
|
|
|
|
}
|
2015-12-03 15:46:17 +00:00
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
sub->callback = prog->callback;
|
2015-12-03 15:46:17 +00:00
|
|
|
sub->data = prog->data;
|
|
|
|
sub->epsilon = prog->epsilon;
|
2010-06-10 18:43:12 +00:00
|
|
|
sub->min = min;
|
|
|
|
sub->max = max;
|
|
|
|
sub->d_prev = prog->d_prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void progress_subrange_end( progress_t* prog, progress_t* sub )
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
if( prog != NULL && prog->callback != NULL )
|
2010-06-10 18:43:12 +00:00
|
|
|
{
|
|
|
|
if( sub->callback == NULL )
|
|
|
|
{
|
|
|
|
progress_update( sub->b, prog );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prog->d_prev = sub->d_prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* PROGRESS_H */
|