source: trunk/src2/voronoi/main.c @ 1016

Last change on this file since 1016 was 666, checked in by mmc, 17 years ago

Added voronoi code, which is currently used by the nanovis server
to generate triangular meshes for point clouds.

File size: 3.0 KB
Line 
1/*** MAIN.C ***/
2
3#include <stdio.h>
4#include <stdlib.h>  /* realloc(), qsort() */
5
6#include "vdefs.h"
7
8Site * readone(void), * nextone(void) ;
9void readsites(void) ;
10
11int sorted, triangulate, plot, debug, nsites, siteidx ;
12float xmin, xmax, ymin, ymax ;
13Site * sites ;
14Freelist sfl ;
15
16int
17main(int argc, char *argv[])
18    {
19    int c ;
20    Site *(*next)() ;
21
22    sorted = triangulate = plot = debug = 0 ;
23    while ((c = getopt(argc, argv, "dpst")) != EOF)
24        {
25        switch(c)
26            {
27            case 'd':
28                debug = 1 ;
29                break ;
30            case 's':
31                sorted = 1 ;
32                break ;
33            case 't':
34                triangulate = 1 ;
35                break ;
36            case 'p':
37                plot = 1 ;
38                break ;
39            }
40        }
41
42    freeinit(&sfl, sizeof(Site)) ;
43    if (sorted)
44        {
45        scanf("%d %f %f %f %f", &nsites, &xmin, &xmax, &ymin, &ymax) ;
46        next = readone ;
47        }
48    else
49        {
50        readsites() ;
51        next = nextone ;
52        }
53    siteidx = 0 ;
54    geominit() ;
55    if (plot)
56        {
57        plotinit() ;
58        }
59    voronoi(next) ;
60    return (0) ;
61    }
62
63/*** sort sites on y, then x, coord ***/
64
65int
66scomp(const void * vs1, const void * vs2)
67    {
68    Point * s1 = (Point *)vs1 ;
69    Point * s2 = (Point *)vs2 ;
70
71    if (s1->y < s2->y)
72        {
73        return (-1) ;
74        }
75    if (s1->y > s2->y)
76        {
77        return (1) ;
78        }
79    if (s1->x < s2->x)
80        {
81        return (-1) ;
82        }
83    if (s1->x > s2->x)
84        {
85        return (1) ;
86        }
87    return (0) ;
88    }
89
90/*** return a single in-storage site ***/
91
92Site *
93nextone(void)
94    {
95    Site * s ;
96
97    if (siteidx < nsites)
98        {
99        s = &sites[siteidx++];
100        return (s) ;
101        }
102    else
103        {
104        return ((Site *)NULL) ;
105        }
106    }
107
108/*** read all sites, sort, and compute xmin, xmax, ymin, ymax ***/
109
110void
111readsites(void)
112    {
113    int i ;
114
115    nsites = 0 ;
116    sites = (Site *) myalloc(4000 * sizeof(Site));
117    while (scanf("%f %f", &sites[nsites].coord.x,
118&sites[nsites].coord.y) !=EOF)
119        {
120        sites[nsites].sitenbr = nsites ;
121        sites[nsites++].refcnt = 0 ;
122        if (nsites % 4000 == 0)
123            sites = (Site *)
124realloc(sites,(nsites+4000)*sizeof(Site));
125        }
126
127    qsort((void *)sites, nsites, sizeof(Site), scomp) ;
128    xmin = sites[0].coord.x ;
129    xmax = sites[0].coord.x ;
130    for (i = 1 ; i < nsites ; ++i)
131        {
132        if(sites[i].coord.x < xmin)
133            {
134            xmin = sites[i].coord.x ;
135            }
136        if (sites[i].coord.x > xmax)
137            {
138            xmax = sites[i].coord.x ;
139            }
140        }
141    ymin = sites[0].coord.y ;
142    ymax = sites[nsites-1].coord.y ;
143    }
144
145/*** read one site ***/
146
147Site *
148readone(void)
149    {
150    Site * s ;
151
152    s = (Site *)getfree(&sfl) ;
153    s->refcnt = 0 ;
154    s->sitenbr = siteidx++ ;
155    if (scanf("%f %f", &(s->coord.x), &(s->coord.y)) == EOF)
156        {
157        return ((Site *)NULL ) ;
158        }
159    return (s) ;
160    }
161
Note: See TracBrowser for help on using the repository browser.