bribera-rubyvor 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +7 -0
- data/Manifest.txt +20 -0
- data/README.txt +45 -0
- data/Rakefile +36 -0
- data/ext/Doc +30 -0
- data/ext/edgelist.c +206 -0
- data/ext/extconf.rb +3 -0
- data/ext/geometry.c +221 -0
- data/ext/heap.c +120 -0
- data/ext/memory.c +120 -0
- data/ext/output.c +262 -0
- data/ext/vdefs.h +149 -0
- data/ext/voronoi.c +277 -0
- data/ext/voronoi_interface.c +213 -0
- data/lib/ruby_vor/decomposition.rb +22 -0
- data/lib/ruby_vor/point.rb +19 -0
- data/lib/ruby_vor/version.rb +3 -0
- data/lib/ruby_vor.rb +13 -0
- data/rubyvor.gemspec +34 -0
- data/test/test_voronoi_interface.rb +158 -0
- metadata +85 -0
data/ext/heap.c
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
|
2
|
+
/*** HEAP.C ***/
|
3
|
+
|
4
|
+
|
5
|
+
#include "vdefs.h"
|
6
|
+
|
7
|
+
VoronoiState rubyvorState;
|
8
|
+
|
9
|
+
static int PQmin, PQcount, PQhashsize ;
|
10
|
+
static Halfedge * PQhash ;
|
11
|
+
|
12
|
+
void
|
13
|
+
PQinsert(Halfedge * he, Site * v, float offset)
|
14
|
+
{
|
15
|
+
Halfedge * last, * next ;
|
16
|
+
|
17
|
+
he->vertex = v ;
|
18
|
+
ref(v) ;
|
19
|
+
he->ystar = v->coord.y + offset ;
|
20
|
+
last = &PQhash[ PQbucket(he)] ;
|
21
|
+
while ((next = last->PQnext) != (Halfedge *)NULL &&
|
22
|
+
(he->ystar > next->ystar ||
|
23
|
+
(he->ystar == next->ystar &&
|
24
|
+
v->coord.x > next->vertex->coord.x)))
|
25
|
+
{
|
26
|
+
last = next ;
|
27
|
+
}
|
28
|
+
he->PQnext = last->PQnext ;
|
29
|
+
last->PQnext = he ;
|
30
|
+
PQcount++ ;
|
31
|
+
}
|
32
|
+
|
33
|
+
void
|
34
|
+
PQdelete(Halfedge * he)
|
35
|
+
{
|
36
|
+
Halfedge * last;
|
37
|
+
|
38
|
+
if(he -> vertex != (Site *) NULL)
|
39
|
+
{
|
40
|
+
last = &PQhash[PQbucket(he)] ;
|
41
|
+
while (last -> PQnext != he)
|
42
|
+
{
|
43
|
+
last = last->PQnext ;
|
44
|
+
}
|
45
|
+
last->PQnext = he->PQnext;
|
46
|
+
PQcount-- ;
|
47
|
+
deref(he->vertex) ;
|
48
|
+
he->vertex = (Site *)NULL ;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
int
|
53
|
+
PQbucket(Halfedge * he)
|
54
|
+
{
|
55
|
+
int bucket ;
|
56
|
+
|
57
|
+
|
58
|
+
if (he->ystar < rubyvorState.ymin) bucket = 0;
|
59
|
+
else if (he->ystar >= rubyvorState.ymax) bucket = PQhashsize-1;
|
60
|
+
else bucket = (he->ystar - rubyvorState.ymin)/rubyvorState.deltay * PQhashsize;
|
61
|
+
if (bucket < 0)
|
62
|
+
{
|
63
|
+
bucket = 0 ;
|
64
|
+
}
|
65
|
+
if (bucket >= PQhashsize)
|
66
|
+
{
|
67
|
+
bucket = PQhashsize-1 ;
|
68
|
+
}
|
69
|
+
if (bucket < PQmin)
|
70
|
+
{
|
71
|
+
PQmin = bucket ;
|
72
|
+
}
|
73
|
+
return (bucket);
|
74
|
+
}
|
75
|
+
|
76
|
+
int
|
77
|
+
PQempty(void)
|
78
|
+
{
|
79
|
+
return (PQcount == 0) ;
|
80
|
+
}
|
81
|
+
|
82
|
+
|
83
|
+
Point
|
84
|
+
PQ_min(void)
|
85
|
+
{
|
86
|
+
Point answer ;
|
87
|
+
|
88
|
+
while (PQhash[PQmin].PQnext == (Halfedge *)NULL)
|
89
|
+
{
|
90
|
+
++PQmin ;
|
91
|
+
}
|
92
|
+
answer.x = PQhash[PQmin].PQnext->vertex->coord.x ;
|
93
|
+
answer.y = PQhash[PQmin].PQnext->ystar ;
|
94
|
+
return (answer) ;
|
95
|
+
}
|
96
|
+
|
97
|
+
Halfedge *
|
98
|
+
PQextractmin(void)
|
99
|
+
{
|
100
|
+
Halfedge * curr ;
|
101
|
+
|
102
|
+
curr = PQhash[PQmin].PQnext ;
|
103
|
+
PQhash[PQmin].PQnext = curr->PQnext ;
|
104
|
+
PQcount-- ;
|
105
|
+
return (curr) ;
|
106
|
+
}
|
107
|
+
|
108
|
+
void
|
109
|
+
PQinitialize(void)
|
110
|
+
{
|
111
|
+
int i ;
|
112
|
+
|
113
|
+
PQcount = PQmin = 0 ;
|
114
|
+
PQhashsize = 4 * rubyvorState.sqrt_nsites ;
|
115
|
+
PQhash = (Halfedge *)myalloc(PQhashsize * sizeof *PQhash) ;
|
116
|
+
for (i = 0 ; i < PQhashsize; i++)
|
117
|
+
{
|
118
|
+
PQhash[i].PQnext = (Halfedge *)NULL ;
|
119
|
+
}
|
120
|
+
}
|
data/ext/memory.c
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
|
2
|
+
/*** MEMORY.C ***/
|
3
|
+
|
4
|
+
#include <stdio.h>
|
5
|
+
#include <stdlib.h> /* malloc(), exit() */
|
6
|
+
|
7
|
+
#include "vdefs.h"
|
8
|
+
|
9
|
+
VoronoiState rubyvorState;
|
10
|
+
|
11
|
+
static char** memory_map;
|
12
|
+
static int nallocs = 0;
|
13
|
+
|
14
|
+
void
|
15
|
+
freeinit(Freelist * fl, int size)
|
16
|
+
{
|
17
|
+
fl->head = (Freenode *)NULL ;
|
18
|
+
fl->nodesize = size ;
|
19
|
+
}
|
20
|
+
|
21
|
+
char *
|
22
|
+
getfree(Freelist * fl)
|
23
|
+
{
|
24
|
+
int i ;
|
25
|
+
Freenode * t ;
|
26
|
+
if (fl->head == (Freenode *)NULL)
|
27
|
+
{
|
28
|
+
t = (Freenode *) myalloc(rubyvorState.sqrt_nsites * fl->nodesize) ;
|
29
|
+
for(i = 0 ; i < rubyvorState.sqrt_nsites ; i++)
|
30
|
+
{
|
31
|
+
makefree((Freenode *)((char *)t+i*fl->nodesize), fl) ;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
t = fl->head ;
|
35
|
+
fl->head = (fl->head)->nextfree ;
|
36
|
+
return ((char *)t) ;
|
37
|
+
}
|
38
|
+
|
39
|
+
void
|
40
|
+
makefree(Freenode * curr, Freelist * fl)
|
41
|
+
{
|
42
|
+
curr->nextfree = fl->head ;
|
43
|
+
fl->head = curr ;
|
44
|
+
}
|
45
|
+
|
46
|
+
int total_alloc;
|
47
|
+
|
48
|
+
void
|
49
|
+
update_memory_map(char * newp)
|
50
|
+
{
|
51
|
+
if (nallocs % 1000 == 0)
|
52
|
+
{
|
53
|
+
if (nallocs == 0)
|
54
|
+
memory_map = (char **)malloc((nallocs+1000)*sizeof(char*));
|
55
|
+
else
|
56
|
+
memory_map = (char **)realloc(memory_map,(nallocs+1000)*sizeof(char*));
|
57
|
+
}
|
58
|
+
memory_map[nallocs++] = newp;
|
59
|
+
}
|
60
|
+
|
61
|
+
char *
|
62
|
+
myalloc(unsigned n)
|
63
|
+
{
|
64
|
+
char * t ;
|
65
|
+
if ((t=(char*)malloc(n)) == (char *) 0)
|
66
|
+
{
|
67
|
+
fprintf(stderr,"Insufficient memory processing site %d (%d bytes in use)\n",
|
68
|
+
rubyvorState.siteidx, total_alloc) ;
|
69
|
+
exit(0) ;
|
70
|
+
}
|
71
|
+
total_alloc += n ;
|
72
|
+
|
73
|
+
update_memory_map(t);
|
74
|
+
return (t) ;
|
75
|
+
}
|
76
|
+
|
77
|
+
char *
|
78
|
+
myrealloc(void * oldp, unsigned n, unsigned oldn)
|
79
|
+
{
|
80
|
+
char * newp;
|
81
|
+
int i;
|
82
|
+
|
83
|
+
if ((newp=(char*)realloc(oldp, n)) == (char *) 0)
|
84
|
+
{
|
85
|
+
fprintf(stderr,"Insufficient memory processing site %d (%d bytes in use)\n",
|
86
|
+
rubyvorState.siteidx, total_alloc) ;
|
87
|
+
exit(0) ;
|
88
|
+
}
|
89
|
+
|
90
|
+
total_alloc += (n - oldn) ;
|
91
|
+
|
92
|
+
update_memory_map(newp);
|
93
|
+
|
94
|
+
// Mark oldp as freed, since free() was called by realloc.
|
95
|
+
for (i=0; i<nallocs; i++) {
|
96
|
+
if (memory_map[i] != (char*)0 && memory_map[i] == oldp) {
|
97
|
+
memory_map[i] = (char*)0;
|
98
|
+
break;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
return (newp) ;
|
102
|
+
}
|
103
|
+
|
104
|
+
|
105
|
+
void free_all(void)
|
106
|
+
{
|
107
|
+
int i;
|
108
|
+
for (i=0; i<nallocs; i++)
|
109
|
+
{
|
110
|
+
if (memory_map[i] != (char*)0)
|
111
|
+
{
|
112
|
+
free(memory_map[i]);
|
113
|
+
memory_map[i] = (char*)0;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
free(memory_map);
|
117
|
+
nallocs = 0;
|
118
|
+
|
119
|
+
debug_memory();
|
120
|
+
}
|
data/ext/output.c
ADDED
@@ -0,0 +1,262 @@
|
|
1
|
+
|
2
|
+
/*** OUTPUT.C ***/
|
3
|
+
|
4
|
+
|
5
|
+
#include <stdio.h>
|
6
|
+
|
7
|
+
#include "vdefs.h"
|
8
|
+
|
9
|
+
VoronoiState rubyvorState;
|
10
|
+
|
11
|
+
static float pxmin, pxmax, pymin, pymax, cradius;
|
12
|
+
|
13
|
+
void openpl(void) {}
|
14
|
+
|
15
|
+
#pragma argsused
|
16
|
+
void line(float ax, float ay, float bx, float by) {}
|
17
|
+
|
18
|
+
#pragma argsused
|
19
|
+
void circle(float ax, float ay, float radius) {}
|
20
|
+
|
21
|
+
#pragma argsused
|
22
|
+
void range(float pxmin, float pxmax, float pymin, float pymax) {}
|
23
|
+
|
24
|
+
void
|
25
|
+
out_bisector(Edge * e)
|
26
|
+
{
|
27
|
+
// Save line to our ruby object
|
28
|
+
(*rubyvorState.storeL)(e->a, e->b, e->c);
|
29
|
+
|
30
|
+
|
31
|
+
// printf("l %f %f %f\n", e->a, e->b, e->c);
|
32
|
+
|
33
|
+
if (rubyvorState.plot)
|
34
|
+
line(e->reg[0]->coord.x, e->reg[0]->coord.y, e->reg[1]->coord.x, e->reg[1]->coord.y);
|
35
|
+
|
36
|
+
if (rubyvorState.debug)
|
37
|
+
printf("line(%d) %gx+%gy=%g, bisecting %d %d\n", e->edgenbr, e->a, e->b, e->c, e->reg[le]->sitenbr, e->reg[re]->sitenbr);
|
38
|
+
}
|
39
|
+
|
40
|
+
void
|
41
|
+
out_ep(Edge * e)
|
42
|
+
{
|
43
|
+
// Save endpoint to our ruby object
|
44
|
+
(*rubyvorState.storeE)(e->edgenbr,
|
45
|
+
e->ep[le] != (Site *)NULL ? e->ep[le]->sitenbr : -1,
|
46
|
+
e->ep[re] != (Site *)NULL ? e->ep[re]->sitenbr : -1);
|
47
|
+
|
48
|
+
/*
|
49
|
+
printf("e %d", e->edgenbr);
|
50
|
+
printf(" %d ", e->ep[le] != (Site *)NULL ? e->ep[le]->sitenbr : -1) ;
|
51
|
+
printf("%d\n", e->ep[re] != (Site *)NULL ? e->ep[re]->sitenbr : -1) ;
|
52
|
+
*/
|
53
|
+
|
54
|
+
if (rubyvorState.plot)
|
55
|
+
clip_line(e) ;
|
56
|
+
}
|
57
|
+
|
58
|
+
void
|
59
|
+
out_vertex(Site * v)
|
60
|
+
{
|
61
|
+
// Save vertex to our ruby object
|
62
|
+
(*rubyvorState.storeV)(v->coord.x, v->coord.y);
|
63
|
+
|
64
|
+
// printf ("v %f %f\n", v->coord.x, v->coord.y) ;
|
65
|
+
|
66
|
+
if (rubyvorState.debug)
|
67
|
+
printf("vertex(%d) at %f %f\n", v->sitenbr, v->coord.x, v->coord.y);
|
68
|
+
}
|
69
|
+
|
70
|
+
void
|
71
|
+
out_site(Site * s)
|
72
|
+
{
|
73
|
+
// Save site to our ruby object
|
74
|
+
(*rubyvorState.storeS)(s->coord.x, s->coord.y);
|
75
|
+
|
76
|
+
//printf("s %f %f\n", s->coord.x, s->coord.y) ;
|
77
|
+
|
78
|
+
if (rubyvorState.plot)
|
79
|
+
circle (s->coord.x, s->coord.y, cradius);
|
80
|
+
|
81
|
+
if (rubyvorState.debug)
|
82
|
+
printf("site (%d) at %f %f\n", s->sitenbr, s->coord.x, s->coord.y);
|
83
|
+
}
|
84
|
+
|
85
|
+
void
|
86
|
+
out_triple(Site * s1, Site * s2, Site * s3)
|
87
|
+
{
|
88
|
+
// Save triplet to our ruby object
|
89
|
+
(*rubyvorState.storeT)(s1->sitenbr, s2->sitenbr, s3->sitenbr);
|
90
|
+
|
91
|
+
if (rubyvorState.debug)
|
92
|
+
{
|
93
|
+
printf("%d %d %d\n", s1->sitenbr, s2->sitenbr, s3->sitenbr);
|
94
|
+
printf("circle through left=%d right=%d bottom=%d\n",
|
95
|
+
s1->sitenbr, s2->sitenbr, s3->sitenbr) ;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
void
|
100
|
+
plotinit(void)
|
101
|
+
{
|
102
|
+
float dx, dy, d ;
|
103
|
+
|
104
|
+
dy = rubyvorState.ymax - rubyvorState.ymin ;
|
105
|
+
dx = rubyvorState.xmax - rubyvorState.xmin ;
|
106
|
+
d = ( dx > dy ? dx : dy) * 1.1 ;
|
107
|
+
pxmin = rubyvorState.xmin - (d-dx) / 2.0 ;
|
108
|
+
pxmax = rubyvorState.xmax + (d-dx) / 2.0 ;
|
109
|
+
pymin = rubyvorState.ymin - (d-dy) / 2.0 ;
|
110
|
+
pymax = rubyvorState.ymax + (d-dy) / 2.0 ;
|
111
|
+
cradius = (pxmax - pxmin) / 350.0 ;
|
112
|
+
openpl() ;
|
113
|
+
range(pxmin, pymin, pxmax, pymax) ;
|
114
|
+
}
|
115
|
+
|
116
|
+
void
|
117
|
+
clip_line(Edge * e)
|
118
|
+
{
|
119
|
+
Site * s1, * s2 ;
|
120
|
+
float x1, x2, y1, y2 ;
|
121
|
+
|
122
|
+
if (e->a == 1.0 && e->b >= 0.0)
|
123
|
+
{
|
124
|
+
s1 = e->ep[1] ;
|
125
|
+
s2 = e->ep[0] ;
|
126
|
+
}
|
127
|
+
else
|
128
|
+
{
|
129
|
+
s1 = e->ep[0] ;
|
130
|
+
s2 = e->ep[1] ;
|
131
|
+
}
|
132
|
+
if (e->a == 1.0)
|
133
|
+
{
|
134
|
+
y1 = pymin ;
|
135
|
+
if (s1 != (Site *)NULL && s1->coord.y > pymin)
|
136
|
+
{
|
137
|
+
y1 = s1->coord.y ;
|
138
|
+
}
|
139
|
+
if (y1 > pymax)
|
140
|
+
{
|
141
|
+
return ;
|
142
|
+
}
|
143
|
+
x1 = e->c - e->b * y1 ;
|
144
|
+
y2 = pymax ;
|
145
|
+
if (s2 != (Site *)NULL && s2->coord.y < pymax)
|
146
|
+
{
|
147
|
+
y2 = s2->coord.y ;
|
148
|
+
}
|
149
|
+
if (y2 < pymin)
|
150
|
+
{
|
151
|
+
return ;
|
152
|
+
}
|
153
|
+
x2 = e->c - e->b * y2 ;
|
154
|
+
if (((x1 > pxmax) && (x2 > pxmax)) || ((x1 < pxmin) && (x2 < pxmin)))
|
155
|
+
{
|
156
|
+
return ;
|
157
|
+
}
|
158
|
+
if (x1 > pxmax)
|
159
|
+
{
|
160
|
+
x1 = pxmax ;
|
161
|
+
y1 = (e->c - x1) / e->b ;
|
162
|
+
}
|
163
|
+
if (x1 < pxmin)
|
164
|
+
{
|
165
|
+
x1 = pxmin ;
|
166
|
+
y1 = (e->c - x1) / e->b ;
|
167
|
+
}
|
168
|
+
if (x2 > pxmax)
|
169
|
+
{
|
170
|
+
x2 = pxmax ;
|
171
|
+
y2 = (e->c - x2) / e->b ;
|
172
|
+
}
|
173
|
+
if (x2 < pxmin)
|
174
|
+
{
|
175
|
+
x2 = pxmin ;
|
176
|
+
y2 = (e->c - x2) / e->b ;
|
177
|
+
}
|
178
|
+
}
|
179
|
+
else
|
180
|
+
{
|
181
|
+
x1 = pxmin ;
|
182
|
+
if (s1 != (Site *)NULL && s1->coord.x > pxmin)
|
183
|
+
{
|
184
|
+
x1 = s1->coord.x ;
|
185
|
+
}
|
186
|
+
if (x1 > pxmax)
|
187
|
+
{
|
188
|
+
return ;
|
189
|
+
}
|
190
|
+
y1 = e->c - e->a * x1 ;
|
191
|
+
x2 = pxmax ;
|
192
|
+
if (s2 != (Site *)NULL && s2->coord.x < pxmax)
|
193
|
+
{
|
194
|
+
x2 = s2->coord.x ;
|
195
|
+
}
|
196
|
+
if (x2 < pxmin)
|
197
|
+
{
|
198
|
+
return ;
|
199
|
+
}
|
200
|
+
y2 = e->c - e->a * x2 ;
|
201
|
+
if (((y1 > pymax) && (y2 > pymax)) || ((y1 < pymin) && (y2 <pymin)))
|
202
|
+
{
|
203
|
+
return ;
|
204
|
+
}
|
205
|
+
if (y1> pymax)
|
206
|
+
{
|
207
|
+
y1 = pymax ;
|
208
|
+
x1 = (e->c - y1) / e->a ;
|
209
|
+
}
|
210
|
+
if (y1 < pymin)
|
211
|
+
{
|
212
|
+
y1 = pymin ;
|
213
|
+
x1 = (e->c - y1) / e->a ;
|
214
|
+
}
|
215
|
+
if (y2 > pymax)
|
216
|
+
{
|
217
|
+
y2 = pymax ;
|
218
|
+
x2 = (e->c - y2) / e->a ;
|
219
|
+
}
|
220
|
+
if (y2 < pymin)
|
221
|
+
{
|
222
|
+
y2 = pymin ;
|
223
|
+
x2 = (e->c - y2) / e->a ;
|
224
|
+
}
|
225
|
+
}
|
226
|
+
line(x1,y1,x2,y2);
|
227
|
+
}
|
228
|
+
|
229
|
+
/* Linux-specific. */
|
230
|
+
void
|
231
|
+
debug_memory(void)
|
232
|
+
{
|
233
|
+
if (!rubyvorState.debug)
|
234
|
+
return;
|
235
|
+
|
236
|
+
char buf[30];
|
237
|
+
FILE* pf;
|
238
|
+
|
239
|
+
unsigned size;// total program size
|
240
|
+
unsigned resident;// resident set size
|
241
|
+
unsigned share;// shared pages
|
242
|
+
unsigned text;// text (code)
|
243
|
+
unsigned lib;// library
|
244
|
+
unsigned data;// data/stack
|
245
|
+
unsigned dt;// dirty pages (unused in Linux 2.6)
|
246
|
+
|
247
|
+
int retVal;
|
248
|
+
|
249
|
+
float totalSize;
|
250
|
+
|
251
|
+
snprintf(buf, 30, "/proc/%u/statm", (unsigned)getpid());
|
252
|
+
pf = fopen(buf, "r");
|
253
|
+
if (NULL != pf)
|
254
|
+
{
|
255
|
+
retVal = fscanf(pf, "%u", &size);
|
256
|
+
//, %u, %u ... etc &resident, &share, &text, &lib, &data);
|
257
|
+
|
258
|
+
totalSize = (float)size / 1024.0;
|
259
|
+
fprintf(stderr, "%f ", totalSize);
|
260
|
+
}
|
261
|
+
fclose(pf);
|
262
|
+
}
|