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