KiCad PCB EDA Suite
progress.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2015 Peter Selinger.
2  * This file is part of Potrace. It is free software and it is covered
3  * by the GNU General Public License. See the file COPYING for details. */
4 
5 /* operations on potrace_progress_t objects, which are defined in
6  * potracelib.h. Note: the code attempts to minimize runtime overhead
7  * when no progress monitoring was requested. It also tries to
8  * minimize excessive progress calculations beneath the "epsilon"
9  * threshold. */
10 
11 #ifndef PROGRESS_H
12 #define PROGRESS_H
13 
14 /* structure to hold progress bar callback data */
15 struct progress_s
16 {
17  void (* callback)( double progress, void* privdata ); /* callback fn */
18  void* data; /* callback function's private data */
19  double min, max; /* desired range of progress, e.g. 0.0 to 1.0 */
20  double epsilon; /* granularity: can skip smaller increments */
21  double b; /* upper limit of subrange in superrange units */
22  double d_prev; /* previous value of d */
23 };
24 typedef struct progress_s progress_t;
25 
26 /* notify given progress object of current progress. Note that d is
27  * given in the 0.0-1.0 range, which will be scaled and translated to
28  * the progress object's range. */
29 static inline void progress_update( double d, progress_t* prog )
30 {
31  double d_scaled;
32 
33  if( prog != NULL && prog->callback != NULL )
34  {
35  d_scaled = prog->min * (1 - d) + prog->max * d;
36 
37  if( d == 1.0 || d_scaled >= prog->d_prev + prog->epsilon )
38  {
39  prog->callback( prog->min * (1 - d) + prog->max * d, prog->data );
40  prog->d_prev = d_scaled;
41  }
42  }
43 }
44 
45 
46 /* start a subrange of the given progress object. The range is
47  * narrowed to [a..b], relative to 0.0-1.0 coordinates. If new range
48  * is below granularity threshold, disable further subdivisions. */
49 static inline void progress_subrange_start( double a,
50  double b,
51  const progress_t* prog,
52  progress_t* sub )
53 {
54  double min, max;
55 
56  if( prog == NULL || prog->callback == NULL )
57  {
58  sub->callback = NULL;
59  return;
60  }
61 
62  min = prog->min * (1 - a) + prog->max * a;
63  max = prog->min * (1 - b) + prog->max * b;
64 
65  if( max - min < prog->epsilon )
66  {
67  sub->callback = NULL; /* no further progress info in subrange */
68  sub->b = b;
69  return;
70  }
71 
72  sub->callback = prog->callback;
73  sub->data = prog->data;
74  sub->epsilon = prog->epsilon;
75  sub->min = min;
76  sub->max = max;
77  sub->d_prev = prog->d_prev;
78 }
79 
80 
81 static inline void progress_subrange_end( progress_t* prog, progress_t* sub )
82 {
83  if( prog != NULL && prog->callback != NULL )
84  {
85  if( sub->callback == NULL )
86  {
87  progress_update( sub->b, prog );
88  }
89  else
90  {
91  prog->d_prev = sub->d_prev;
92  }
93  }
94 }
95 
96 
97 #endif /* PROGRESS_H */
static void progress_subrange_start(double a, double b, const progress_t *prog, progress_t *sub)
Definition: progress.h:49
static void progress_subrange_end(progress_t *prog, progress_t *sub)
Definition: progress.h:81
double b
Definition: progress.h:21
double max
Definition: progress.h:19
double d_prev
Definition: progress.h:22
double min
Definition: progress.h:19
void * data
Definition: progress.h:18
double epsilon
Definition: progress.h:20
static void progress_update(double d, progress_t *prog)
Definition: progress.h:29
#define max(a, b)
Definition: auxiliary.h:86
void(* callback)(double progress, void *privdata)
Definition: progress.h:17
#define min(a, b)
Definition: auxiliary.h:85