2015-12-03 15:46:17 +00:00
|
|
|
/* Copyright (C) 2001-2015 Peter Selinger.
|
2010-06-10 18:43:12 +00:00
|
|
|
* This file is part of Potrace. It is free software and it is covered
|
|
|
|
* by the GNU General Public License. See the file COPYING for details. */
|
|
|
|
|
|
|
|
/* This header file collects some general-purpose macros (and static
|
|
|
|
* inline functions) that are used in various places. */
|
|
|
|
|
|
|
|
#ifndef AUXILIARY_H
|
|
|
|
#define AUXILIARY_H
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-06-10 18:43:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "config.h"
|
2010-06-10 18:43:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* point arithmetic */
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
#include "potracelib.h"
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
struct point_s
|
|
|
|
{
|
2015-12-03 15:46:17 +00:00
|
|
|
long x;
|
|
|
|
long y;
|
2010-06-10 18:43:12 +00:00
|
|
|
};
|
2015-12-03 15:46:17 +00:00
|
|
|
typedef struct point_s point_t;
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
typedef potrace_dpoint_t dpoint_t;
|
|
|
|
|
|
|
|
/* convert point_t to dpoint_t */
|
|
|
|
static inline dpoint_t dpoint( point_t p )
|
|
|
|
{
|
|
|
|
dpoint_t res;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
res.x = p.x;
|
|
|
|
res.y = p.y;
|
2010-06-10 18:43:12 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* range over the straight line segment [a,b] when lambda ranges over [0,1] */
|
|
|
|
static inline dpoint_t interval( double lambda, dpoint_t a, dpoint_t b )
|
|
|
|
{
|
|
|
|
dpoint_t res;
|
|
|
|
|
2015-12-03 15:46:17 +00:00
|
|
|
res.x = a.x + lambda * (b.x - a.x);
|
|
|
|
res.y = a.y + lambda * (b.y - a.y);
|
2010-06-10 18:43:12 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
/* some useful macros. Note: the "mod" macro works correctly for
|
|
|
|
* negative a. Also note that the test for a>=n, while redundant,
|
|
|
|
* speeds up the mod function by 70% in the average case (significant
|
|
|
|
* since the program spends about 16% of its time here - or 40%
|
|
|
|
* without the test). The "floordiv" macro returns the largest integer
|
|
|
|
* <= a/n, and again this works correctly for negative a, as long as
|
|
|
|
* a,n are integers and n>0. */
|
|
|
|
|
|
|
|
/* integer arithmetic */
|
|
|
|
|
|
|
|
static inline int mod( int a, int n )
|
|
|
|
{
|
|
|
|
return a>=n ? a % n : a>=0 ? a : n - 1 - (-1 - a) % n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int floordiv( int a, int n )
|
|
|
|
{
|
|
|
|
return a>=0 ? a / n : -1 - (-1 - a) / n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Note: the following work for integers and other numeric types. */
|
|
|
|
#undef sign
|
|
|
|
#undef abs
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
#undef sq
|
|
|
|
#undef cu
|
|
|
|
#define sign( x ) ( (x)>0 ? 1 : (x)<0 ? -1 : 0 )
|
|
|
|
#define abs( a ) ( (a)>0 ? (a) : -(a) )
|
|
|
|
#define min( a, b ) ( (a)<(b) ? (a) : (b) )
|
|
|
|
#define max( a, b ) ( (a)>(b) ? (a) : (b) )
|
|
|
|
#define sq( a ) ( (a) * (a) )
|
2011-11-24 17:32:51 +00:00
|
|
|
#define cu( a ) ( (a) * (a) * (a) )
|
2010-06-10 18:43:12 +00:00
|
|
|
|
|
|
|
#endif /* AUXILIARY_H */
|