/************************   caddy.h    ***********************/

#include <stdio.h>
#include <math.h>
#include <stdtypes.h> 


/******** CONSTANTS ************/

#define INFINITY 99999999999.0    /* a really big number */
#define EPSILON .01           /* a small number to make math stuff work */

#define LESS 0                /* halfplane defined by line is below it */ 
#define GREATER 1             /* halfplane defined by line is above it */ 

#define SMALL_RADIUS 20.0
#define LARGE_RADIUS 200.0

#define DIST_WEIGHT 4.0      /* the penalty for taking a path which goes through
				fat polygons */

/*********** MACROS **************/

/* min, nax, abs, sqr */
#define my_min(x,y) ((x<y)?x:y)
#define my_max(x,y) ((x>y)?x:y)
#define abs(x) (((x)<0.0)?(-(x)):(x))
#define sqr(x) ((x)*(x))

/* mod for negatives integers as well */
#define modulo(a,b) ((a>=0)?(a%b):(b-((-a)%b)))

/* checks if z is between x and y regardless of the order of x and y */
#define between(x,y,z) (((x)<(y))?(((z)>(x)) && ((z)<(y))):(((x)>(y))?(((z)>(y)) && ((z)<(x))):(SAME((z),(x)))))

/* floor of x/2 */
#define floor_div(x) ((((x)%2)==0)?((x)/2):(((x)-1)/2))

/* x modulo pi */
#define modpi(x) (((x)>=(2.0*PI))?((x)-(2.0*PI)):(x))


#define ROUTEMAP_MY_BETWEEN(x,y,z)\
  ((x<(y)) ? (((z)>=(x)) && ((z)<=(y))) : \
   (((x)>(y)) ? (((z)<=(x)) && ((z)>=(y))) : (abs((z)-(x))<EPSILON)))

#define ROUTEMAP_MY_TOTAL_BETWEEN(x,y,z)\
  (((x)<(y)) ? (((z)>(x)) && ((z)<(y))) : \
   (((x)>(y)) ? (((z)<(x)) && ((z)>(y))) : (abs((z)-(x))<EPSILON)))

#define ROUTEMAP_WITHIN_SEGMENT(a,b,c)\
  (ROUTEMAP_MY_BETWEEN((a)->x,(b)->x,(c)->x) && \
   ROUTEMAP_MY_BETWEEN((a)->y,(b)->y,(c)->y))

#define ROUTEMAP_TOTAL_WITHIN_SEGMENT(a,b,c)\
  (ROUTEMAP_MY_TOTAL_BETWEEN((a)->x,(b)->x,(c)->x) && \
   ROUTEMAP_MY_TOTAL_BETWEEN((a)->y,(b)->y,(c)->y))


#define ROUTEMAP_POINT_IN_BOUNDING_BOX(box,p)\
(((p)->x >= (box)->bottom.x) && ((p)->x <= (box)->top.x)\
 && ((p)->y >= (box)->bottom.y) && ((p)->y <= (box)->top.y))

#define ROUTEMAP_RECTANGLE_OVERLAP(top1,bot1,top2,bot2)\
(!(((bot1)->x > (top2)->x) || ((top1)->x < (bot2)->x)\
 || ((bot1)->y > (top2)->y) || ((top1)->y < (bot2)->y)))

/*********** TYPES *****************/

typedef struct point_type
{
  float32 x,y;
  int32 radius_num;  /* which radius level the point belongs to */
  int32 which_poly;  /* -1 if it's a glue point */
  int32 vert_num;
  int32 orig_vert;   /* 1 if it is one of the original verts of the polygon.
			0 if it is caused by an intersection with the grid */
}   point;

typedef struct heap_element_type
{
  float32 key;       /* heap is keyed by euclidean distance */
  int32 ver_num;     /* the actual elements are the vertex numbers */
} heap_element;

typedef struct list_type
{
  int32 element;           /* linked list of integers */
  struct list_type* next;
} list;

typedef struct graph_type
{
  int32 num;          /* number of vertices */
  point* v;         /* array of vertex locations */
  list **adj;       /* adjacency list representation of edges */
} graph;

typedef struct bound_box_type
{
  point top, bottom;
} bound_box;

typedef struct polygonal_type
{
  int32 num_vertices;    
  point* vertex;
  bound_box b;
} polygonal;

typedef struct map_type
{
  int32 num_no_gos;      /* number of obstacles */
  polygonal **no_gos;    /* no_gos[i][j] is the j_th obstacle expanded by width[i] */
} map;

/* geometry.c */
extern int32 point_in_polygon();
extern int32 poly_intersection();
extern int32 routemap_line_hits_local_poly();

/* initialize.c */
extern void init_graph();
extern void read_visib_graph_from_file();
extern void dump_visib_graph_to_file();







