
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	
	defines circular apertures for use in image definitions

	circle number [cc=] [cr=] [rad=]  [int] [del]
	
	if no args specified makes a list of circles 

	9/22/92 - call to exit(0) -rrt
			- removed screen puts
	9/28/94 - modify error messages (put name of program)
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pcvista.h"
#include "pcvimage.h"


#ifdef PROTO
int main(int, char **);
static void circlelist(int);
static int make_newcircle(void);
#else
static void circlelist();
static int make_newcircle();
#endif

int show_flag, delete_flag, int_flag;

char *progname = "circle";

#define USAGE "usage: circle [cr=] [cc=] [rad=] [int] [show] [del] [help]"

int 
main(argc, argv)
int argc;
char *argv[];
{
	int i, cr=-1, cc=-1, rad=-1, circleno = -1;
	char *gotit, buf[BIG_BUF];

	if (argc == 1) {
		circlelist(circleno);
		exit(0);
	}

	for(i = 1; i < argc; i++) {		/* extract new values from command line */
		if ((gotit = find("cc", argv[i])) != NULL) {
			cc = (int)(evaluate(gotit) + .5);
			continue;
		}
		if ((gotit = find("cr", argv[i])) != NULL) {
			cr = (int)(evaluate(gotit) + .5);
			continue;
		}
		if ((gotit = find("rad", argv[i])) != NULL) {
			rad = (int)(evaluate(gotit) + .5);
			continue;
		}
		if (keyword("del", argv[i])) {
			delete_flag = 1;
			continue;
		}
		if (keyword("help", argv[i])) {
			error(1,USAGE);
			continue;
		}
		if (keyword("show", argv[i])) {
			show_flag = 1;
			continue;
		}
		if (keyword("int", argv[i])) {
			if (circleno == -1)
				circleno = make_newcircle();
			int_flag = 1;
			continue;
		}
		if (i == 1) {
			circleno = (int)(evaluate(argv[1]) + 0.5);		
			if (circleno >= NBOX)
				error(-1, "circle: maximum number of circles exceeded");
			getcircle(circleno,&cr,&cc,&rad);
			continue;
		}		
		fprintf(stderr,"circle: invalid option '%s'\n",argv[i]);
		exit(1);
	}
	if (delete_flag) {
		if (circleno != -1) {
			sprintf(buf, "circle%d=", circleno);
			putsym(buf);
			exit(0);
		}
		else
			error(-1, "circle: no circle number specified");
	}

	if (show_flag || int_flag) { 
		image_init(TV_GREYSCALE);
	}
		
	if (int_flag)
		int_circle(&cr, &cc, &rad);
	if (circleno != -1) {
		sprintf(buf,"circle%d=%d %d %d", circleno, cr, cc, rad);
		putsym(buf);
	}
	circlelist(circleno);
	exit(0);
}

static void 
circlelist(circleno)
int circleno;
{
	int circle, cr, cc, rad, circleflag;
	char buf[NBUF];

	if (circleno == -1) {
		for (circle = 0, circleflag = 0; circle < NBOX; circle++) { 
			if (getcircle(circle, &cr, &cc, &rad) == 0) {
				sprintf(buf,"circle #%d cr=%d cc=%d rad=%d",
					circle, cr, cc, rad);
				if (show_flag) {
					puts(buf);
					show_circle(cr, cc, rad);
				}
				else
					puts(buf);
				circleflag = 1;
			}
		}
		if (circleflag == 0)
			puts("no circles found");
	}
	else {
		if (getcircle(circleno, &cr, &cc, &rad))
			error(-1, "circle: program error");
		printf("circle #%d cr=%d cc=%d rad=%d\n",
			circleno, cr, cc, rad);
		if ((show_flag) || (int_flag)) {
			show_circle(cr, cc, rad);
		}
	}
}


static int 
make_newcircle()			/* finds first unused circle number */
{
	int i, cc, cr, rad;

	for (i = 0; i < NBOX; i++)
		if (getcircle(i, &cr, &cc, &rad))
			return(i);
	return(-1);
}

