bool VoronoiDiagramGenerator::voronoi(int triangulate)
{
	struct Site *newsite, *bot, *top, *temp, *p;
	struct Site *v;
	struct Point newintstar;
	int pm;
	struct Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
	struct Edge *e;
	
	PQinitialize();
	bottomsite = nextone();
	out_site(bottomsite);
	bool retval = ELinitialize();

	if(!retval)
		return false;
	
	newsite = nextone();
	while(1)
	{

		if(!PQempty()) 
			newintstar = PQ_min();
		
		//if the lowest site has a smaller y value than the lowest vector intersection, process the site
		//otherwise process the vector intersection		

		if (newsite != (struct Site *)NULL 	&& (PQempty() || newsite -> coord.y < newintstar.y
			|| (newsite->coord.y == newintstar.y && newsite->coord.x < newintstar.x)))
		{/* new site is smallest - this is a site event*/
			out_site(newsite);						//output the site
			lbnd = ELleftbnd(&(newsite->coord));				//get the first HalfEdge to the LEFT of the new site
			rbnd = ELright(lbnd);						//get the first HalfEdge to the RIGHT of the new site
			bot = rightreg(lbnd);						//if this halfedge has no edge, , bot = bottom site (whatever that is)
			e = bisect(bot, newsite);					//create a new edge that bisects 
			bisector = HEcreate(e, le);					//create a new HalfEdge, setting its ELpm field to 0			
			ELinsert(lbnd, bisector);					//insert this new bisector edge between the left and right vectors in a linked list	

			if ((p = intersect(lbnd, bisector)) != (struct Site *) NULL) 	//if the new bisector intersects with the left edge, remove the left edge's vertex, and put in the new one
			{	
				PQdelete(lbnd);
				PQinsert(lbnd, p, dist(p,newsite));
			};
			lbnd = bisector;						
			bisector = HEcreate(e, re);					//create a new HalfEdge, setting its ELpm field to 1
			ELinsert(lbnd, bisector);					//insert the new HE to the right of the original bisector earlier in the IF stmt

			if ((p = intersect(bisector, rbnd)) != (struct Site *) NULL)	//if this new bisector intersects with the
			{	
				PQinsert(bisector, p, dist(p,newsite));			//push the HE into the ordered linked list of vertices
			};
			newsite = nextone();	
		}
		else if (!PQempty()) /* intersection is smallest - this is a vector event */			
		{	
			lbnd = PQextractmin();						//pop the HalfEdge with the lowest vector off the ordered list of vectors				
			llbnd = ELleft(lbnd);						//get the HalfEdge to the left of the above HE
			rbnd = ELright(lbnd);						//get the HalfEdge to the right of the above HE
			rrbnd = ELright(rbnd);						//get the HalfEdge to the right of the HE to the right of the lowest HE 
			bot = leftreg(lbnd);						//get the Site to the left of the left HE which it bisects
			top = rightreg(rbnd);						//get the Site to the right of the right HE which it bisects

			out_triple(bot, top, rightreg(lbnd));		//output the triple of sites, stating that a circle goes through them

			v = lbnd->vertex;						//get the vertex that caused this event
			makevertex(v);							//set the vertex number - couldn't do this earlier since we didn't know when it would be processed
			endpoint(lbnd->ELedge,lbnd->ELpm,v);	//set the endpoint of the left HalfEdge to be this vector
			endpoint(rbnd->ELedge,rbnd->ELpm,v);	//set the endpoint of the right HalfEdge to be this vector
			ELdelete(lbnd);							//mark the lowest HE for deletion - can't delete yet because there might be pointers to it in Hash Map	
			PQdelete(rbnd);							//remove all vertex events to do with the  right HE
			ELdelete(rbnd);							//mark the right HE for deletion - can't delete yet because there might be pointers to it in Hash Map	
			pm = le;								//set the pm variable to zero
			
			if (bot->coord.y > top->coord.y)		//if the site to the left of the event is higher than the Site
			{										//to the right of it, then swap them and set the 'pm' variable to 1
				temp = bot; 
				bot = top; 
				top = temp; 
				pm = re;
			}
			e = bisect(bot, top);					//create an Edge (or line) that is between the two Sites. This creates
													//the formula of the line, and assigns a line number to it
			bisector = HEcreate(e, pm);				//create a HE from the Edge 'e', and make it point to that edge with its ELedge field
			ELinsert(llbnd, bisector);				//insert the new bisector to the right of the left HE
			endpoint(e, re-pm, v);					//set one endpoint to the new edge to be the vector point 'v'.
													//If the site to the left of this bisector is higher than the right
													//Site, then this endpoint is put in position 0; otherwise in pos 1
			deref(v);								//delete the vector 'v'

			//if left HE and the new bisector don't intersect, then delete the left HE, and reinsert it 
			if((p = intersect(llbnd, bisector)) != (struct Site *) NULL)
			{	
				PQdelete(llbnd);
				PQinsert(llbnd, p, dist(p,bot));
			};

			//if right HE and the new bisector don't intersect, then reinsert it 
			if ((p = intersect(bisector, rrbnd)) != (struct Site *) NULL)
			{	
				PQinsert(bisector, p, dist(p,bot));
			};
		}
		else break;
	};

	


	for(lbnd=ELright(ELleftend); lbnd != ELrightend; lbnd=ELright(lbnd))
	{	
		e = lbnd -> ELedge;

		clip_line(e);
	};

	cleanup();

	return true;
	
}
示例#2
0
void
voronoi(Site *(*nextsite)(void))
{
    Site * newsite, * bot, * top, * temp, * p, * v ;
    Point newintstar ;
    int pm , c;
    Halfedge * lbnd, * rbnd, * llbnd, * rrbnd, * bisector ;
    Edge * e ;

    newintstar.x = newintstar.y = c = 0;
    
    PQinitialize() ;
    rubyvorState.bottomsite = (*nextsite)() ;
    out_site(rubyvorState.bottomsite) ;
    ELinitialize() ;
    newsite = (*nextsite)() ;
    
    while (1)
    {
        if(!PQempty())
            newintstar = PQ_min() ;
        
        if (newsite != (Site *)NULL && (PQempty()
            || newsite -> coord.y < newintstar.y
            || (newsite->coord.y == newintstar.y
            && newsite->coord.x < newintstar.x)))
        {
            /* new site is smallest */
            {
                out_site(newsite) ;
            }
            lbnd = ELleftbnd(&(newsite->coord)) ;
            rbnd = ELright(lbnd) ;
            bot = rightreg(lbnd) ;
            e = bisect(bot, newsite) ;
            bisector = HEcreate(e, le) ;
            ELinsert(lbnd, bisector) ;
            p = intersect(lbnd, bisector) ;
            if (p != (Site *)NULL)
            {
                PQdelete(lbnd) ;
                PQinsert(lbnd, p, dist(p,newsite)) ;
            }
            lbnd = bisector ;
            bisector = HEcreate(e, re) ;
            ELinsert(lbnd, bisector) ;
            p = intersect(bisector, rbnd) ;
            if (p != (Site *)NULL)
            {
                PQinsert(bisector, p, dist(p,newsite)) ;
            }
            newsite = (*nextsite)() ;
        }
        else if (!PQempty())   /* intersection is smallest */
        {
            lbnd = PQextractmin() ;
            llbnd = ELleft(lbnd) ;
            rbnd = ELright(lbnd) ;
            rrbnd = ELright(rbnd) ;
            bot = leftreg(lbnd) ;
            top = rightreg(rbnd) ;
            out_triple(bot, top, rightreg(lbnd)) ;
            v = lbnd->vertex ;
            makevertex(v) ;
            endpoint(lbnd->ELedge, lbnd->ELpm, v);
            endpoint(rbnd->ELedge, rbnd->ELpm, v) ;
            ELdelete(lbnd) ;
            PQdelete(rbnd) ;
            ELdelete(rbnd) ;
            pm = le ;
            if (bot->coord.y > top->coord.y)
            {
                temp = bot ;
                bot = top ;
                top = temp ;
                pm = re ;
            }
            e = bisect(bot, top) ;
            bisector = HEcreate(e, pm) ;
            ELinsert(llbnd, bisector) ;
            endpoint(e, re-pm, v) ;
            deref(v) ;
            p = intersect(llbnd, bisector) ;
            if (p  != (Site *) NULL)
            {
                PQdelete(llbnd) ;
                PQinsert(llbnd, p, dist(p,bot)) ;
            }
            p = intersect(bisector, rrbnd) ;
            if (p != (Site *) NULL)
            {
                PQinsert(bisector, p, dist(p,bot)) ;
            }
        }
        else
        {
            break ;
        }
    }
    
    for( lbnd = ELright(getELleftend()) ;
         lbnd != getELrightend() ;
         lbnd = ELright(lbnd))
    {
        e = lbnd->ELedge ;
        out_ep(e) ;
    }
}
示例#3
0
文件: voronoi.c 项目: ellert/graphviz
void voronoi(int triangulate, Site * (*nextsite) (void))
{
    Site *newsite, *bot, *top, *temp, *p;
    Site *v;
    Point newintstar = {0};
    char pm;
    Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
    Edge *e;

    edgeinit();
    siteinit();
    PQinitialize();
    bottomsite = (*nextsite) ();
#ifdef STANDALONE
    out_site(bottomsite);
#endif
    ELinitialize();

    newsite = (*nextsite) ();
    while (1) {
	if (!PQempty())
	    newintstar = PQ_min();

	if (newsite != (struct Site *) NULL && (PQempty()
						|| newsite->coord.y <
						newintstar.y
						|| (newsite->coord.y ==
						    newintstar.y
						    && newsite->coord.x <
						    newintstar.x))) {
	    /* new site is smallest */
#ifdef STANDALONE
	    out_site(newsite);
#endif
	    lbnd = ELleftbnd(&(newsite->coord));
	    rbnd = ELright(lbnd);
	    bot = rightreg(lbnd);
	    e = gvbisect(bot, newsite);
	    bisector = HEcreate(e, le);
	    ELinsert(lbnd, bisector);
	    if ((p = hintersect(lbnd, bisector)) != (struct Site *) NULL) {
		PQdelete(lbnd);
		PQinsert(lbnd, p, dist(p, newsite));
	    }
	    lbnd = bisector;
	    bisector = HEcreate(e, re);
	    ELinsert(lbnd, bisector);
	    if ((p = hintersect(bisector, rbnd)) != (struct Site *) NULL)
		PQinsert(bisector, p, dist(p, newsite));
	    newsite = (*nextsite) ();
	} else if (!PQempty()) {
	    /* intersection is smallest */
	    lbnd = PQextractmin();
	    llbnd = ELleft(lbnd);
	    rbnd = ELright(lbnd);
	    rrbnd = ELright(rbnd);
	    bot = leftreg(lbnd);
	    top = rightreg(rbnd);
#ifdef STANDALONE
	    out_triple(bot, top, rightreg(lbnd));
#endif
	    v = lbnd->vertex;
	    makevertex(v);
	    endpoint(lbnd->ELedge, lbnd->ELpm, v);
	    endpoint(rbnd->ELedge, rbnd->ELpm, v);
	    ELdelete(lbnd);
	    PQdelete(rbnd);
	    ELdelete(rbnd);
	    pm = le;
	    if (bot->coord.y > top->coord.y) {
		temp = bot;
		bot = top;
		top = temp;
		pm = re;
	    }
	    e = gvbisect(bot, top);
	    bisector = HEcreate(e, pm);
	    ELinsert(llbnd, bisector);
	    endpoint(e, re - pm, v);
	    deref(v);
	    if ((p = hintersect(llbnd, bisector)) != (struct Site *) NULL) {
		PQdelete(llbnd);
		PQinsert(llbnd, p, dist(p, bot));
	    }
	    if ((p = hintersect(bisector, rrbnd)) != (struct Site *) NULL) {
		PQinsert(bisector, p, dist(p, bot));
	    }
	} else
	    break;
    }

    for (lbnd = ELright(ELleftend); lbnd != ELrightend;
	 lbnd = ELright(lbnd)) {
	e = lbnd->ELedge;
	clip_line(e);
#ifdef STANDALONE
	out_ep(e);
#endif
    }
}
示例#4
0
int voronoi(struct Site *(*nextsite) (void))
{
    struct Site *newsite, *bot, *top, *temp, *p;
    struct Site *v;
    struct Point newintstar;
    int pm;
    struct Halfedge *lbnd, *rbnd, *llbnd, *rrbnd, *bisector;
    struct Edge *e;
    int counter = 0;

    PQinitialize();
    bottomsite = (*nextsite) ();
    ELinitialize();

    newsite = (*nextsite) ();
    while (1) {
        if (!PQempty())
            newintstar = PQ_min();

        if (newsite != (struct Site *)NULL &&
                (PQempty() || newsite->coord.y < newintstar.y ||
                 (newsite->coord.y == newintstar.y &&
                  newsite->coord.x < newintstar.x))) {	/* new site is smallest */

            G_percent(counter++, nsites, 2);

            lbnd = ELleftbnd(&(newsite->coord));
            rbnd = ELright(lbnd);
            bot = rightreg(lbnd);
            e = bisect(bot, newsite);
            bisector = HEcreate(e, le);
            ELinsert(lbnd, bisector);
            if ((p = intersect(lbnd, bisector)) != (struct Site *)NULL) {
                PQdelete(lbnd);
                PQinsert(lbnd, p, dist(p, newsite));
            }
            lbnd = bisector;
            bisector = HEcreate(e, re);
            ELinsert(lbnd, bisector);
            if ((p = intersect(bisector, rbnd)) != (struct Site *)NULL) {
                PQinsert(bisector, p, dist(p, newsite));
            }
            /* get next site, but ensure that it doesn't have the same
               coordinates as the previous. If so, step over to the following
               site. Andrea Aime 4/7/2001 */
            do
                temp = (*nextsite) ();
            while (temp != (struct Site *)NULL &&
                    temp->coord.x == newsite->coord.x &&
                    temp->coord.y == newsite->coord.y);
            newsite = temp;
        }
        else if (!PQempty()) {
            /* intersection is smallest */
            lbnd = PQextractmin();
            llbnd = ELleft(lbnd);
            rbnd = ELright(lbnd);
            rrbnd = ELright(rbnd);
            bot = leftreg(lbnd);
            top = rightreg(rbnd);

            v = lbnd->vertex;
            makevertex(v);
            endpoint(lbnd->ELedge, lbnd->ELpm, v);
            endpoint(rbnd->ELedge, rbnd->ELpm, v);
            ELdelete(lbnd);
            PQdelete(rbnd);
            ELdelete(rbnd);
            pm = le;
            if (bot->coord.y > top->coord.y) {
                temp = bot;
                bot = top;
                top = temp;
                pm = re;
            }
            e = bisect(bot, top);
            bisector = HEcreate(e, pm);
            ELinsert(llbnd, bisector);
            endpoint(e, re - pm, v);
            deref(v);
            if ((p = intersect(llbnd, bisector)) != (struct Site *)NULL) {
                PQdelete(llbnd);
                PQinsert(llbnd, p, dist(p, bot));
            }
            if ((p = intersect(bisector, rrbnd)) != (struct Site *)NULL) {
                PQinsert(bisector, p, dist(p, bot));
            }
        }
        else
            break;
    }

    G_percent(1, 1, 1);

    return 0;
}