File indexing completed on 2024-12-22 04:04:07

0001 /* Copyright (C) 2001-2019 Peter Selinger.
0002    This file is part of Potrace. It is free software and it is covered
0003    by the GNU General Public License. See the file COPYING for details. */
0004 
0005 /* This header file collects some general-purpose macros (and static
0006    inline functions) that are used in various places. */
0007 
0008 #ifndef AUXILIARY_H
0009 #define AUXILIARY_H
0010 
0011 #ifdef HAVE_CONFIG_H
0012 #include <config.h>
0013 #endif
0014 
0015 #include <stdlib.h>
0016 
0017 /* ---------------------------------------------------------------------- */
0018 /* point arithmetic */
0019 
0020 #include "potracelib.h"
0021 
0022 struct point_s {
0023   long x;
0024   long y;
0025 };
0026 typedef struct point_s point_t;
0027 
0028 typedef potrace_dpoint_t dpoint_t;
0029 
0030 /* convert point_t to dpoint_t */
0031 static inline dpoint_t dpoint(point_t p) {
0032   dpoint_t res;
0033   res.x = p.x;
0034   res.y = p.y;
0035   return res;
0036 }
0037 
0038 /* range over the straight line segment [a,b] when lambda ranges over [0,1] */
0039 static inline dpoint_t interval(double lambda, dpoint_t a, dpoint_t b) {
0040   dpoint_t res;
0041 
0042   res.x = a.x + lambda * (b.x - a.x);
0043   res.y = a.y + lambda * (b.y - a.y);
0044   return res;
0045 }
0046 
0047 /* ---------------------------------------------------------------------- */
0048 /* some useful macros. Note: the "mod" macro works correctly for
0049    negative a. Also note that the test for a>=n, while redundant,
0050    speeds up the mod function by 70% in the average case (significant
0051    since the program spends about 16% of its time here - or 40%
0052    without the test). The "floordiv" macro returns the largest integer
0053    <= a/n, and again this works correctly for negative a, as long as
0054    a,n are integers and n>0. */
0055 
0056 /* integer arithmetic */
0057 
0058 static inline int mod(int a, int n) {
0059   return a>=n ? a%n : a>=0 ? a : n-1-(-1-a)%n;
0060 }
0061 
0062 static inline int floordiv(int a, int n) {
0063   return a>=0 ? a/n : -1-(-1-a)/n;
0064 }
0065 
0066 /* Note: the following work for integers and other numeric types. */
0067 #undef sign
0068 #undef abs
0069 #undef min
0070 #undef max
0071 #undef sq
0072 #undef cu
0073 #define sign(x) ((x)>0 ? 1 : (x)<0 ? -1 : 0)
0074 #define abs(a) ((a)>0 ? (a) : -(a))
0075 #define min(a,b) ((a)<(b) ? (a) : (b))
0076 #define max(a,b) ((a)>(b) ? (a) : (b))
0077 #define sq(a) ((a)*(a))
0078 #define cu(a) ((a)*(a)*(a))
0079 
0080 #endif /* AUXILIARY_H */