Code

fix by Preben S for LP bug 389780, flatness
[inkscape.git] / src / trace / potrace / auxiliary.h
1 /* Copyright (C) 2001-2007 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. */
5 /* This header file collects some general-purpose macros (and static
6    inline functions) that are used in various places. */
8 #ifndef AUXILIARY_H
9 #define AUXILIARY_H
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
15 /* ---------------------------------------------------------------------- */
16 /* point arithmetic */
18 #include "potracelib.h"
20 struct point_s {
21   long x;
22   long y;
23 };
24 typedef struct point_s point_t;
26 typedef potrace_dpoint_t dpoint_t;
28 /* convert point_t to dpoint_t */
29 static inline dpoint_t dpoint(point_t p) {
30   dpoint_t res;
31   res.x = p.x;
32   res.y = p.y;
33   return res;
34 }
36 /* range over the straight line segment [a,b] when lambda ranges over [0,1] */
37 static inline dpoint_t interval(double lambda, dpoint_t a, dpoint_t b) {
38   dpoint_t res;
40   res.x = a.x + lambda * (b.x - a.x);
41   res.y = a.y + lambda * (b.y - a.y);
42   return res;
43 }
45 /* ---------------------------------------------------------------------- */
46 /* some useful macros. Note: the "mod" macro works correctly for
47    negative a. Also note that the test for a>=n, while redundant,
48    speeds up the mod function by 70% in the average case (significant
49    since the program spends about 16% of its time here - or 40%
50    without the test). The "floordiv" macro returns the largest integer
51    <= a/n, and again this works correctly for negative a, as long as
52    a,n are integers and n>0. */
54 /* integer arithmetic */
56 static inline int mod(int a, int n) {
57   return a>=n ? a%n : a>=0 ? a : n-1-(-1-a)%n;
58 }
60 static inline int floordiv(int a, int n) {
61   return a>=0 ? a/n : -1-(-1-a)/n;
62 }
64 /* Note: the following work for integers and other numeric types. */
65 #undef sign
66 #undef abs
67 #undef min
68 #undef max
69 #undef sq
70 #undef cu
71 #define sign(x) ((x)>0 ? 1 : (x)<0 ? -1 : 0)
72 #define abs(a) ((a)>0 ? (a) : -(a))
73 #define min(a,b) ((a)<(b) ? (a) : (b))
74 #define max(a,b) ((a)>(b) ? (a) : (b))
75 #define sq(a) ((a)*(a))
76 #define cu(a) ((a)*(a)*(a))
78 #endif /* AUXILIARY_H */