auxiliary.h 2.1 KB

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