Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d38673c

Browse files
committed
Simplify and remove redundant code.
svn path=/trunk/matplotlib/; revision=6176
1 parent 2e71c34 commit d38673c

1 file changed

Lines changed: 92 additions & 72 deletions

File tree

src/agg_py_path_iterator.h

Lines changed: 92 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ class SimplifyPath
145145
m_haveMin(false), m_lastMax(false), m_maxX(0.0), m_maxY(0.0),
146146
m_minX(0.0), m_minY(0.0), m_lastWrittenX(0.0), m_lastWrittenY(0.0),
147147
m_done(false)
148-
#if DEBUG_SIMPLIFY
149-
, m_pushed(0), m_skipped(0)
150-
#endif
148+
#if DEBUG_SIMPLIFY
149+
, m_pushed(0), m_skipped(0)
150+
#endif
151151
{
152152
// empty
153153
}
154154

155-
#if DEBUG_SIMPLIFY
156-
~SimplifyPath()
157-
{
158-
if (m_simplify)
159-
printf("%d %d\n", m_pushed, m_skipped);
160-
}
161-
#endif
155+
#if DEBUG_SIMPLIFY
156+
~SimplifyPath()
157+
{
158+
if (m_simplify)
159+
printf("%d %d\n", m_pushed, m_skipped);
160+
}
161+
#endif
162162

163163
void rewind(unsigned path_id)
164164
{
@@ -198,30 +198,18 @@ class SimplifyPath
198198
// will be popped from the queue in subsequent calls. The following
199199
// block will empty the queue before proceeding to the main loop below.
200200
// -- Michael Droettboom
201-
if (m_queue_read < m_queue_write)
202-
{
203-
const item& front = m_queue[m_queue_read++];
204-
unsigned cmd = front.cmd;
205-
*x = front.x;
206-
*y = front.y;
207-
#if DEBUG_SIMPLIFY
208-
printf((cmd == agg::path_cmd_move_to) ? "|" : "-");
209-
printf(" 1 %f %f\n", *x, *y);
210-
#endif
201+
if (flush_queue(&cmd, x, y)) {
211202
return cmd;
212203
}
213204

214-
m_queue_read = 0;
215-
m_queue_write = 0;
216-
217205
// If the queue is now empty, and the path was fully consumed
218206
// in the last call to the main loop, return agg::path_cmd_stop to
219207
// signal that there are no more points to emit.
220208
if (m_done)
221209
{
222-
#if DEBUG_SIMPLIFY
223-
printf(".\n");
224-
#endif
210+
#if DEBUG_SIMPLIFY
211+
printf(".\n");
212+
#endif
225213
return agg::path_cmd_stop;
226214
}
227215

@@ -242,16 +230,16 @@ class SimplifyPath
242230
//if we are starting a new path segment, move to the first point
243231
// + init
244232

245-
#if DEBUG_SIMPLIFY
246-
printf("x, y, code: %f, %f, %d\n", *x, *y, cmd);
247-
#endif
233+
#if DEBUG_SIMPLIFY
234+
printf("x, y, code: %f, %f, %d\n", *x, *y, cmd);
235+
#endif
248236
if (m_moveto || cmd == agg::path_cmd_move_to)
249237
{
250238
// m_moveto check is not generally needed because
251239
// m_source generates an initial moveto; but it
252240
// is retained for safety in case circumstances
253241
// arise where this is not true.
254-
if (m_origdNorm2 && !m_after_moveto)
242+
if (m_origdNorm2 != 0.0 && !m_after_moveto)
255243
{
256244
// m_origdNorm2 is nonzero only if we have a vector;
257245
// the m_after_moveto check ensures we push this
@@ -267,7 +255,7 @@ class SimplifyPath
267255
// line segment, hence a break in the line, just
268256
// like clipping, so we treat it the same way.
269257
m_clipped = true;
270-
if (m_queue_read < m_queue_write)
258+
if (queue_nonempty())
271259
{
272260
// If we did a push, empty the queue now.
273261
break;
@@ -279,9 +267,9 @@ class SimplifyPath
279267
// Don't render line segments less than one pixel long
280268
if (fabs(*x - m_lastx) < 1.0 && fabs(*y - m_lasty) < 1.0)
281269
{
282-
#if DEBUG_SIMPLIFY
283-
m_skipped++;
284-
#endif
270+
#if DEBUG_SIMPLIFY
271+
m_skipped++;
272+
#endif
285273
continue;
286274
}
287275

@@ -296,9 +284,9 @@ class SimplifyPath
296284
m_lastx = *x;
297285
m_lasty = *y;
298286
m_clipped = true;
299-
#if DEBUG_SIMPLIFY
300-
m_skipped++;
301-
#endif
287+
#if DEBUG_SIMPLIFY
288+
m_skipped++;
289+
#endif
302290
continue;
303291
}
304292

@@ -307,11 +295,11 @@ class SimplifyPath
307295
// this orig vector is the reference vector we will build
308296
// up the line to
309297

310-
if (m_origdNorm2 == 0)
298+
if (m_origdNorm2 == 0.0)
311299
{
312300
if (m_clipped)
313301
{
314-
m_queue[m_queue_write++].set(agg::path_cmd_move_to, m_lastx, m_lasty);
302+
queue_push(agg::path_cmd_move_to, m_lastx, m_lasty);
315303
m_clipped = false;
316304
}
317305

@@ -329,9 +317,9 @@ class SimplifyPath
329317
m_lasty = m_maxY = *y;
330318
m_lastWrittenX = m_minX = m_lastx;
331319
m_lastWrittenY = m_minY = m_lasty;
332-
#if DEBUG_SIMPLIFY
333-
m_skipped++;
334-
#endif
320+
#if DEBUG_SIMPLIFY
321+
m_skipped++;
322+
#endif
335323
continue;
336324
}
337325

@@ -395,9 +383,9 @@ class SimplifyPath
395383

396384
m_lastx = *x;
397385
m_lasty = *y;
398-
#if DEBUG_SIMPLIFY
399-
m_skipped++;
400-
#endif
386+
#if DEBUG_SIMPLIFY
387+
m_skipped++;
388+
#endif
401389
continue;
402390
}
403391
//if we get here, then this vector was not similar enough to the
@@ -422,33 +410,23 @@ class SimplifyPath
422410
{
423411
if (m_haveMin)
424412
{
425-
m_queue[m_queue_write++].set(agg::path_cmd_line_to, m_minX, m_minY);
413+
queue_push(agg::path_cmd_line_to, m_minX, m_minY);
426414
}
427-
m_queue[m_queue_write++].set(agg::path_cmd_line_to, m_maxX, m_maxY);
415+
queue_push(agg::path_cmd_line_to, m_maxX, m_maxY);
428416
}
429417
m_done = true;
430418
}
431419

432420
// Return the first item in the queue, if any, otherwise
433421
// indicate that we're done.
434-
if (m_queue_read < m_queue_write)
435-
{
436-
const item& front = m_queue[m_queue_read++];
437-
unsigned cmd = front.cmd;
438-
*x = front.x;
439-
*y = front.y;
440-
#if DEBUG_SIMPLIFY
441-
printf((cmd == agg::path_cmd_move_to) ? "|" : "-");
442-
printf(" 3 %f %f\n", *x, *y);
443-
444-
#endif
422+
if (flush_queue(&cmd, x, y)) {
445423
return cmd;
446424
}
447425
else
448426
{
449-
#if DEBUG_SIMPLIFY
450-
printf(".\n");
451-
#endif
427+
#if DEBUG_SIMPLIFY
428+
printf(".\n");
429+
#endif
452430
return agg::path_cmd_stop;
453431
}
454432
}
@@ -459,6 +437,7 @@ class SimplifyPath
459437
bool m_simplify;
460438
double m_width, m_height;
461439

440+
static const int m_queue_size = 6;
462441
struct item
463442
{
464443
item() {}
@@ -474,7 +453,7 @@ class SimplifyPath
474453
};
475454
int m_queue_read;
476455
int m_queue_write;
477-
item m_queue[6];
456+
item m_queue[m_queue_size];
478457

479458
bool m_moveto;
480459
bool m_after_moveto;
@@ -497,23 +476,65 @@ class SimplifyPath
497476
double m_lastWrittenY;
498477
bool m_done;
499478

500-
#if DEBUG_SIMPLIFY
501-
unsigned m_pushed;
502-
unsigned m_skipped;
503-
#endif
479+
#if DEBUG_SIMPLIFY
480+
unsigned m_pushed;
481+
unsigned m_skipped;
482+
#endif
483+
484+
inline void queue_push(const unsigned cmd, const double& x, const double& y)
485+
{
486+
#if DEBUG_SIMPLIFY
487+
if (m_queue_write >= m_queue_size)
488+
throw "Simplification queue overflow";
489+
#endif
490+
m_queue[m_queue_write++].set(cmd, x, y);
491+
}
504492

505-
void _push(double* x, double* y)
493+
inline bool queue_nonempty()
494+
{
495+
return m_queue_read < m_queue_write;
496+
}
497+
498+
inline bool flush_queue(unsigned *cmd, double *x, double *y)
499+
{
500+
if (!queue_nonempty())
501+
{
502+
#if DEBUG_SIMPLIFY
503+
if (m_queue_read >= m_queue_size)
504+
throw "Simplification queue overflow";
505+
#endif
506+
507+
const item& front = m_queue[m_queue_read++];
508+
*cmd = front.cmd;
509+
*x = front.x;
510+
*y = front.y;
511+
512+
#if DEBUG_SIMPLIFY
513+
printf((cmd == agg::path_cmd_move_to) ? "|" : "-");
514+
printf(" 1 %f %f\n", *x, *y);
515+
#endif
516+
517+
return true;
518+
}
519+
520+
m_queue_read = 0;
521+
m_queue_write = 0;
522+
523+
return false;
524+
}
525+
526+
inline void _push(double* x, double* y)
506527
{
507528
if (m_haveMin)
508529
{
509-
m_queue[m_queue_write++].set(agg::path_cmd_line_to, m_minX, m_minY);
530+
queue_push(agg::path_cmd_line_to, m_minX, m_minY);
510531
}
511-
m_queue[m_queue_write++].set(agg::path_cmd_line_to, m_maxX, m_maxY);
532+
queue_push(agg::path_cmd_line_to, m_maxX, m_maxY);
512533

513534
//if we clipped some segments between this line and the next line
514535
//we are starting, we also need to move to the last point.
515536
if (m_clipped) {
516-
m_queue[m_queue_write++].set(agg::path_cmd_move_to, m_lastx, m_lasty);
537+
queue_push(agg::path_cmd_move_to, m_lastx, m_lasty);
517538
}
518539
else if (!m_lastMax)
519540
{
@@ -523,7 +544,7 @@ class SimplifyPath
523544
//the line just drawn.
524545

525546
//Would be move_to if not for the artifacts
526-
m_queue[m_queue_write++].set(agg::path_cmd_line_to, m_lastx, m_lasty);
547+
queue_push(agg::path_cmd_line_to, m_lastx, m_lasty);
527548
}
528549

529550
//now reset all the variables to get ready for the next line
@@ -544,7 +565,6 @@ class SimplifyPath
544565
#if DEBUG_SIMPLIFY
545566
m_pushed += m_queue_write - m_queue_read;
546567
#endif
547-
548568
}
549569

550570
};

0 commit comments

Comments
 (0)