trap.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @file
  3. * @brief trapezoid elements and utilities for partition.c
  4. *
  5. * See [Fast polygon triangulation based on Seidel's algorithm](http://gamma.cs.unc.edu/SEIDEL/)
  6. *
  7. */
  8. /*************************************************************************
  9. * Copyright (c) 2011 AT&T Intellectual Property
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v1.0
  12. * which accompanies this distribution, and is available at
  13. * https://www.eclipse.org/legal/epl-v10.html
  14. *
  15. * Contributors: Details at https://graphviz.org
  16. *************************************************************************/
  17. #pragma once
  18. #include <stdbool.h>
  19. #include <stddef.h>
  20. /* Segment attributes */
  21. typedef struct {
  22. pointf v0, v1; /* two endpoints */
  23. bool is_inserted; /* inserted in trapezoidation yet ? */
  24. int root0, root1; /* root nodes in Q */
  25. int next; /* Next logical segment */
  26. int prev; /* Previous segment */
  27. } segment_t;
  28. /* Trapezoid attributes */
  29. typedef struct {
  30. int lseg, rseg; /* two adjoining segments */
  31. pointf hi, lo; /* max/min y-values */
  32. int u0, u1;
  33. int d0, d1;
  34. int sink; /* pointer to corresponding in Q */
  35. int usave, uside; /* I forgot what this means */
  36. int state;
  37. } trap_t;
  38. /// an array of trapezoids
  39. typedef struct {
  40. size_t length;
  41. trap_t *data;
  42. } traps_t;
  43. #define ST_VALID 1 /* for trapezium state */
  44. #define ST_INVALID 2
  45. #define C_EPS 1.0e-7 /* tolerance value: Used for making */
  46. /* all decisions about collinearity or */
  47. /* left/right of segment. Decrease */
  48. /* this value if the input points are */
  49. /* spaced very close together */
  50. #define FP_EQUAL(s, t) (fabs(s - t) <= C_EPS)
  51. /**
  52. * @brief double floating point three-way comparison
  53. *
  54. * Returns -1, 0, or 1 if f1 respectively, less than,
  55. * almost equal with tolerance C_EPS, or greater than f2.
  56. * The purpose of the function is workaround precision issues.
  57. */
  58. static inline int dfp_cmp(double f1, double f2) {
  59. double d = f1 - f2;
  60. if (d < -C_EPS)
  61. return -1;
  62. if (d > C_EPS)
  63. return 1;
  64. return 0;
  65. }
  66. #define _equal_to(v0,v1) \
  67. (FP_EQUAL((v0)->y, (v1)->y) && FP_EQUAL((v0)->x, (v1)->x))
  68. #define _greater_than(v0, v1) \
  69. (((v0)->y > (v1)->y + C_EPS) ? true : (((v0)->y < (v1)->y - C_EPS) ? false : ((v0)->x > (v1)->x)))
  70. extern traps_t construct_trapezoids(int, segment_t*, int*);