You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.3 KiB

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