bribera-rubyvor 0.0.3 → 0.0.4
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 +9 -0
- data/Manifest.txt +7 -0
- data/Rakefile +1 -1
- data/ext/extconf.rb +2 -2
- data/ext/output.c +2 -0
- data/ext/rb_cComputation.c +281 -0
- data/ext/rb_cPriorityQueue.c +106 -0
- data/ext/ruby_vor.c +41 -0
- data/ext/ruby_vor.h +23 -0
- data/ext/vdefs.h +2 -1
- data/ext/voronoi.c +2 -11
- data/lib/ruby_vor/computation.rb +126 -15
- data/lib/ruby_vor/point.rb +4 -0
- data/lib/ruby_vor/priority_queue.rb +60 -0
- data/lib/ruby_vor/version.rb +1 -1
- data/lib/ruby_vor.rb +3 -2
- data/rubyvor.gemspec +8 -6
- data/test/test_computation.rb +175 -0
- data/test/test_priority_queue.rb +56 -0
- data/test/test_voronoi_interface.rb +6 -3
- metadata +11 -2
- data/ext/voronoi_interface.c +0 -217
data/ext/voronoi_interface.c
DELETED
@@ -1,217 +0,0 @@
|
|
1
|
-
#include <ruby.h>
|
2
|
-
#include <vdefs.h>
|
3
|
-
#include <stdio.h>
|
4
|
-
#include <stdlib.h>
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
VoronoiState rubyvorState;
|
10
|
-
|
11
|
-
static VALUE rb_mRubyVor;
|
12
|
-
static VALUE rb_mVDDT;
|
13
|
-
static VALUE rb_cComputation;
|
14
|
-
static int repeat, rit;
|
15
|
-
|
16
|
-
// Static method definitions
|
17
|
-
static VALUE from_points(VALUE, VALUE);
|
18
|
-
|
19
|
-
static Site * readone(void), * nextone(void);
|
20
|
-
static int scomp(const void *, const void *);
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
void
|
25
|
-
Init_voronoi_interface(void)
|
26
|
-
{
|
27
|
-
// Set up our Modules and Class.
|
28
|
-
rb_mRubyVor = rb_define_module("RubyVor");
|
29
|
-
rb_mVDDT = rb_define_module_under(rb_mRubyVor, "VDDT");
|
30
|
-
rb_cComputation = rb_define_class_under(rb_mVDDT, "Computation", rb_cObject);
|
31
|
-
|
32
|
-
// Add methods.
|
33
|
-
rb_define_singleton_method(rb_cComputation, "from_points", from_points, 1);
|
34
|
-
}
|
35
|
-
|
36
|
-
|
37
|
-
//
|
38
|
-
// Class methods
|
39
|
-
//
|
40
|
-
static VALUE
|
41
|
-
from_points(VALUE self, VALUE pointsArray)
|
42
|
-
{
|
43
|
-
//VALUE returnValue;
|
44
|
-
VALUE * inPtr, newComp;
|
45
|
-
ID x, y;
|
46
|
-
|
47
|
-
long i, inSize;
|
48
|
-
|
49
|
-
// TODO: remove
|
50
|
-
repeat = 1;
|
51
|
-
|
52
|
-
for (rit = 0; rit < repeat; rit++) {
|
53
|
-
|
54
|
-
// Require T_ARRAY
|
55
|
-
//Check_Type(pointsArray, T_ARRAY);
|
56
|
-
|
57
|
-
// Intern our point access methods
|
58
|
-
x = rb_intern("x");
|
59
|
-
y = rb_intern("y");
|
60
|
-
|
61
|
-
// Load up point count & points pointer.
|
62
|
-
inSize = RARRAY(pointsArray)->len;
|
63
|
-
inPtr = RARRAY(pointsArray)->ptr;
|
64
|
-
|
65
|
-
// Require nonzero size and x & y methods on each array object
|
66
|
-
if (inSize < 1)
|
67
|
-
rb_raise(rb_eRuntimeError, "points array have a nonzero length");
|
68
|
-
for (i = 0; i < inSize; i++) {
|
69
|
-
if(!rb_respond_to(inPtr[i], x) || !rb_respond_to(inPtr[i], y))
|
70
|
-
rb_raise(rb_eRuntimeError, "members of points array must respond to 'x' and 'y'");
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
// Initialize rubyvorState
|
75
|
-
initialize_state(/* debug? */ 0);
|
76
|
-
debug_memory();
|
77
|
-
|
78
|
-
// Create our return object.
|
79
|
-
newComp = rb_funcall(self, rb_intern("new"), 1, pointsArray);
|
80
|
-
// Store it in rubyvorState so we can populate its values.
|
81
|
-
rubyvorState.comp = (void *) &newComp;
|
82
|
-
//
|
83
|
-
// Read in the sites, sort, and compute xmin, xmax, ymin, ymax
|
84
|
-
//
|
85
|
-
// TODO: refactor this block into a separate method for clarity?
|
86
|
-
//
|
87
|
-
{
|
88
|
-
// Allocate memory for 4000 sites...
|
89
|
-
rubyvorState.sites = (Site *) myalloc(4000 * sizeof(Site));
|
90
|
-
|
91
|
-
|
92
|
-
// Iterate over the arrays, doubling the incoming values.
|
93
|
-
for (i=0; i<inSize; i++)
|
94
|
-
{
|
95
|
-
rubyvorState.sites[rubyvorState.nsites].coord.x = NUM2DBL(rb_funcall(inPtr[i], x, 0));
|
96
|
-
rubyvorState.sites[rubyvorState.nsites].coord.y = NUM2DBL(rb_funcall(inPtr[i], y, 0));
|
97
|
-
|
98
|
-
//
|
99
|
-
rubyvorState.sites[rubyvorState.nsites].sitenbr = rubyvorState.nsites;
|
100
|
-
rubyvorState.sites[rubyvorState.nsites++].refcnt = 0;
|
101
|
-
|
102
|
-
// Allocate for 4000 more if we just hit a multiple of 4000...
|
103
|
-
if (rubyvorState.nsites % 4000 == 0)
|
104
|
-
{
|
105
|
-
rubyvorState.sites = (Site *)myrealloc(rubyvorState.sites,(rubyvorState.nsites+4000)*sizeof(Site),rubyvorState.nsites*sizeof(Site));
|
106
|
-
}
|
107
|
-
}
|
108
|
-
|
109
|
-
// Sort the Sites
|
110
|
-
qsort((void *)rubyvorState.sites, rubyvorState.nsites, sizeof(Site), scomp) ;
|
111
|
-
|
112
|
-
// Pull the minimum values.
|
113
|
-
rubyvorState.xmin = rubyvorState.sites[0].coord.x;
|
114
|
-
rubyvorState.xmax = rubyvorState.sites[0].coord.x;
|
115
|
-
for (i=1; i < rubyvorState.nsites; ++i)
|
116
|
-
{
|
117
|
-
if (rubyvorState.sites[i].coord.x < rubyvorState.xmin)
|
118
|
-
{
|
119
|
-
rubyvorState.xmin = rubyvorState.sites[i].coord.x;
|
120
|
-
}
|
121
|
-
if (rubyvorState.sites[i].coord.x > rubyvorState.xmax)
|
122
|
-
{
|
123
|
-
rubyvorState.xmax = rubyvorState.sites[i].coord.x;
|
124
|
-
}
|
125
|
-
}
|
126
|
-
rubyvorState.ymin = rubyvorState.sites[0].coord.y;
|
127
|
-
rubyvorState.ymax = rubyvorState.sites[rubyvorState.nsites-1].coord.y;
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
|
132
|
-
// Perform the computation
|
133
|
-
voronoi(nextone);
|
134
|
-
debug_memory();
|
135
|
-
|
136
|
-
// Get rid of our comp reference
|
137
|
-
rubyvorState.comp = (void *)NULL;
|
138
|
-
|
139
|
-
// Free our allocated objects
|
140
|
-
free_all();
|
141
|
-
debug_memory();
|
142
|
-
|
143
|
-
if (rubyvorState.debug)
|
144
|
-
fprintf(stderr,"FINISHED ITERATION %i\n", rit + 1);
|
145
|
-
|
146
|
-
|
147
|
-
} // end repeat...
|
148
|
-
|
149
|
-
return newComp;
|
150
|
-
}
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
//
|
156
|
-
// Static C methods
|
157
|
-
//
|
158
|
-
|
159
|
-
/*** sort sites on y, then x, coord ***/
|
160
|
-
static int
|
161
|
-
scomp(const void * vs1, const void * vs2)
|
162
|
-
{
|
163
|
-
Point * s1 = (Point *)vs1 ;
|
164
|
-
Point * s2 = (Point *)vs2 ;
|
165
|
-
|
166
|
-
if (s1->y < s2->y)
|
167
|
-
{
|
168
|
-
return (-1) ;
|
169
|
-
}
|
170
|
-
if (s1->y > s2->y)
|
171
|
-
{
|
172
|
-
return (1) ;
|
173
|
-
}
|
174
|
-
if (s1->x < s2->x)
|
175
|
-
{
|
176
|
-
return (-1) ;
|
177
|
-
}
|
178
|
-
if (s1->x > s2->x)
|
179
|
-
{
|
180
|
-
return (1) ;
|
181
|
-
}
|
182
|
-
return (0) ;
|
183
|
-
}
|
184
|
-
|
185
|
-
/*** return a single in-storage site ***/
|
186
|
-
static Site *
|
187
|
-
nextone(void)
|
188
|
-
{
|
189
|
-
Site * s ;
|
190
|
-
|
191
|
-
if (rubyvorState.siteidx < rubyvorState.nsites)
|
192
|
-
{
|
193
|
-
s = &rubyvorState.sites[rubyvorState.siteidx++];
|
194
|
-
return (s) ;
|
195
|
-
}
|
196
|
-
else
|
197
|
-
{
|
198
|
-
return ((Site *)NULL) ;
|
199
|
-
}
|
200
|
-
}
|
201
|
-
|
202
|
-
|
203
|
-
/*** read one site ***/
|
204
|
-
static Site *
|
205
|
-
readone(void)
|
206
|
-
{
|
207
|
-
Site * s ;
|
208
|
-
|
209
|
-
s = (Site *)getfree(&(rubyvorState.sfl)) ;
|
210
|
-
s->refcnt = 0 ;
|
211
|
-
s->sitenbr = rubyvorState.siteidx++ ;
|
212
|
-
if (scanf("%f %f", &(s->coord.x), &(s->coord.y)) == EOF)
|
213
|
-
{
|
214
|
-
return ((Site *)NULL ) ;
|
215
|
-
}
|
216
|
-
return (s) ;
|
217
|
-
}
|