| 1 | #include "ruby.h"
|
|---|
| 2 | #include "Shape.h"
|
|---|
| 3 |
|
|---|
| 4 | static VALUE rb_cShape = 0;
|
|---|
| 5 | static VALUE rb_cPolygon = 0;
|
|---|
| 6 | static VALUE rb_cPolyline = 0;
|
|---|
| 7 | static VALUE rb_cPoint = 0;
|
|---|
| 8 | static VALUE rb_cBoundingBox = 0;
|
|---|
| 9 |
|
|---|
| 10 | VALUE getRubyESRIShapePolygonClass()
|
|---|
| 11 | {
|
|---|
| 12 | return rb_cPolygon;
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | VALUE getRubyESRIShapePolylineClass()
|
|---|
| 16 | {
|
|---|
| 17 | return rb_cPolyline;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | VALUE getRubyESRIShapePointClass()
|
|---|
| 21 | {
|
|---|
| 22 | return rb_cPoint;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | VALUE getRubyESRIShapeBoundingBoxClass()
|
|---|
| 26 | {
|
|---|
| 27 | return rb_cBoundingBox;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | #ifndef RUBYESRISHAPE_TEST
|
|---|
| 31 |
|
|---|
| 32 | static VALUE rb_shape_allocate(VALUE klass);
|
|---|
| 33 | static void rb_shape_free(void *ptr);
|
|---|
| 34 | static void rb_shape_mark(void *ptr);
|
|---|
| 35 |
|
|---|
| 36 | static VALUE rb_polygon_allocate(VALUE klass);
|
|---|
| 37 | static void rb_polygon_free(void *ptr);
|
|---|
| 38 | static void rb_polygon_mark(void *ptr);
|
|---|
| 39 |
|
|---|
| 40 | static VALUE rb_polyline_allocate(VALUE klass);
|
|---|
| 41 | static void rb_polyline_free(void *ptr);
|
|---|
| 42 |
|
|---|
| 43 | static void rb_point_free(void *ptr);
|
|---|
| 44 |
|
|---|
| 45 | // Shape
|
|---|
| 46 |
|
|---|
| 47 | static VALUE rb_shape_allocate(VALUE klass)
|
|---|
| 48 | {
|
|---|
| 49 | RShapeData* indata = ALLOC(RShapeData);
|
|---|
| 50 | indata->pThis = c_Shape_new();
|
|---|
| 51 |
|
|---|
| 52 | return Data_Wrap_Struct(klass, rb_shape_mark, rb_shape_free, indata);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | static void rb_shape_free(void *ptr)
|
|---|
| 56 | {
|
|---|
| 57 | if (ptr)
|
|---|
| 58 | {
|
|---|
| 59 | if (((RShapeData*)ptr)->pThis)
|
|---|
| 60 | c_Shape_delete(((RShapeData*)ptr)->pThis);
|
|---|
| 61 |
|
|---|
| 62 | free(ptr);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | static void rb_shape_mark(void *ptr)
|
|---|
| 67 | {
|
|---|
| 68 | if (ptr)
|
|---|
| 69 | {
|
|---|
| 70 | if (((RShapeData*)ptr)->pThis)
|
|---|
| 71 | c_Shape_markRubyGC(((RShapeData*)ptr)->pThis);
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | static VALUE rb_shape_initialize(VALUE self, VALUE shape_name)
|
|---|
| 76 | {
|
|---|
| 77 | RShapeData* indata;
|
|---|
| 78 | es_error_t rv;
|
|---|
| 79 | Data_Get_Struct(self, RShapeData, indata);
|
|---|
| 80 |
|
|---|
| 81 | rv = c_Shape_readFromFile(indata->pThis, STR2CSTR(shape_name));
|
|---|
| 82 | if (rv == ESHP_CANNOTOPEN)
|
|---|
| 83 | rb_raise(rb_eIOError, "cannot open the shape file.");
|
|---|
| 84 | else if (rv == ESHP_UNMATCHED_SHAPETYPE)
|
|---|
| 85 | rb_raise(rb_eIOError, "shape types unmatched between files.");
|
|---|
| 86 |
|
|---|
| 87 | /* ESHP_NOINDEXFILE doesn't raise any errors */
|
|---|
| 88 |
|
|---|
| 89 | return Qnil;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | static VALUE rb_shape_aref(VALUE self, VALUE index)
|
|---|
| 93 | {
|
|---|
| 94 | RShapeData* indata;
|
|---|
| 95 | Data_Get_Struct(self, RShapeData, indata);
|
|---|
| 96 |
|
|---|
| 97 | return c_Shape_getRecordAt(indata->pThis, NUM2LONG(index));
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | static VALUE rb_shape_length(VALUE self)
|
|---|
| 101 | {
|
|---|
| 102 | RShapeData* indata;
|
|---|
| 103 | Data_Get_Struct(self, RShapeData, indata);
|
|---|
| 104 |
|
|---|
| 105 | return INT2NUM( c_Shape_getLength(indata->pThis) );
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | static VALUE rb_shape_each(VALUE self)
|
|---|
| 109 | {
|
|---|
| 110 | RShapeData* indata;
|
|---|
| 111 | int len, i;
|
|---|
| 112 | Data_Get_Struct(self, RShapeData, indata);
|
|---|
| 113 |
|
|---|
| 114 | len = c_Shape_getLength(indata->pThis);
|
|---|
| 115 | for (i = 0;i < len;i++)
|
|---|
| 116 | {
|
|---|
| 117 | volatile VALUE arr = rb_ary_new2(2);
|
|---|
| 118 | rb_ary_push(arr, INT2NUM(i+1) );
|
|---|
| 119 | rb_ary_push(arr, c_Shape_getRecordAt(indata->pThis, i+1) );
|
|---|
| 120 |
|
|---|
| 121 | rb_yield(arr);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | return self;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static VALUE rb_shape_type(VALUE self)
|
|---|
| 128 | {
|
|---|
| 129 | RShapeData* indata;
|
|---|
| 130 | Data_Get_Struct(self, RShapeData, indata);
|
|---|
| 131 |
|
|---|
| 132 | return INT2NUM( c_Shape_getType(indata->pThis) );
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // Polygon
|
|---|
| 136 |
|
|---|
| 137 | static VALUE rb_polygon_allocate(VALUE klass)
|
|---|
| 138 | {
|
|---|
| 139 | RPolygonData* indata = ALLOC(RPolygonData);
|
|---|
| 140 | indata->pThis = NULL;
|
|---|
| 141 |
|
|---|
| 142 | return Data_Wrap_Struct(klass, rb_polygon_mark, rb_polygon_free, indata);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | static void rb_polygon_free(void *ptr)
|
|---|
| 146 | {
|
|---|
| 147 | if (ptr)
|
|---|
| 148 | {
|
|---|
| 149 | if (((RPolygonData*)ptr)->pThis)
|
|---|
| 150 | c_Polygon_delete(((RPolygonData*)ptr)->pThis);
|
|---|
| 151 | free(ptr);
|
|---|
| 152 |
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | static void rb_polygon_mark(void *ptr)
|
|---|
| 157 | {
|
|---|
| 158 | if (ptr)
|
|---|
| 159 | {
|
|---|
| 160 | if (((RPolygonData*)ptr)->pThis)
|
|---|
| 161 | c_Polygon_markRubyGC(((RPolygonData*)ptr)->pThis);
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | static VALUE rb_polygon_aref(VALUE self, VALUE index)
|
|---|
| 166 | {
|
|---|
| 167 | RPolygonData* indata;
|
|---|
| 168 | Data_Get_Struct(self, RPolygonData, indata);
|
|---|
| 169 |
|
|---|
| 170 | return c_Polygon_getPartAt(indata->pThis, NUM2LONG(index));
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | static VALUE rb_polygon_length(VALUE self)
|
|---|
| 174 | {
|
|---|
| 175 | RPolygonData* indata;
|
|---|
| 176 | Data_Get_Struct(self, RPolygonData, indata);
|
|---|
| 177 |
|
|---|
| 178 | return INT2NUM( c_Polygon_getPartsLength(indata->pThis) );
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | static VALUE rb_polygon_each(VALUE self)
|
|---|
| 182 | {
|
|---|
| 183 | RPolygonData* indata;
|
|---|
| 184 | int len, i;
|
|---|
| 185 | Data_Get_Struct(self, RPolygonData, indata);
|
|---|
| 186 |
|
|---|
| 187 | len = c_Polygon_getPartsLength(indata->pThis);
|
|---|
| 188 | for (i = 0;i < len;i++)
|
|---|
| 189 | {
|
|---|
| 190 | VALUE arr = rb_ary_new2(2);
|
|---|
| 191 | rb_ary_push(arr, INT2NUM(i) );
|
|---|
| 192 | rb_ary_push(arr, c_Polygon_getPartAt(indata->pThis, i) );
|
|---|
| 193 |
|
|---|
| 194 | rb_yield(arr);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | return self;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | static VALUE rb_polygon_bounding_box(VALUE self)
|
|---|
| 201 | {
|
|---|
| 202 | RPolygonData* indata;
|
|---|
| 203 | Data_Get_Struct(self, RPolygonData, indata);
|
|---|
| 204 |
|
|---|
| 205 | return c_Polygon_getBoundingBox(indata->pThis);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | // Polyline
|
|---|
| 209 |
|
|---|
| 210 | static VALUE rb_polyline_allocate(VALUE klass)
|
|---|
| 211 | {
|
|---|
| 212 | RPolylineData* indata = ALLOC(RPolylineData);
|
|---|
| 213 | indata->pThis = NULL;
|
|---|
| 214 |
|
|---|
| 215 | return Data_Wrap_Struct(klass, 0, rb_polyline_free, indata);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | static void rb_polyline_free(void *ptr)
|
|---|
| 219 | {
|
|---|
| 220 | if (ptr)
|
|---|
| 221 | {
|
|---|
| 222 | if (((RPolylineData*)ptr)->pThis)
|
|---|
| 223 | c_Polyline_delete(((RPolylineData*)ptr)->pThis);
|
|---|
| 224 | free(ptr);
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | static VALUE rb_polyline_aref(VALUE self, VALUE index)
|
|---|
| 229 | {
|
|---|
| 230 | RPolylineData* indata;
|
|---|
| 231 | Data_Get_Struct(self, RPolylineData, indata);
|
|---|
| 232 |
|
|---|
| 233 | return c_Polyline_getPartAt(indata->pThis, NUM2LONG(index));
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | static VALUE rb_polyline_length(VALUE self)
|
|---|
| 237 | {
|
|---|
| 238 | RPolylineData* indata;
|
|---|
| 239 | Data_Get_Struct(self, RPolylineData, indata);
|
|---|
| 240 |
|
|---|
| 241 | return INT2NUM( c_Polyline_getPartsLength(indata->pThis) );
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | static VALUE rb_polyline_each(VALUE self)
|
|---|
| 245 | {
|
|---|
| 246 | RPolylineData* indata;
|
|---|
| 247 | int len, i;
|
|---|
| 248 | Data_Get_Struct(self, RPolylineData, indata);
|
|---|
| 249 |
|
|---|
| 250 | len = c_Polyline_getPartsLength(indata->pThis);
|
|---|
| 251 | for (i = 0;i < len;i++)
|
|---|
| 252 | {
|
|---|
| 253 | VALUE arr = rb_ary_new2(2);
|
|---|
| 254 | rb_ary_push(arr, INT2NUM(i) );
|
|---|
| 255 | rb_ary_push(arr, c_Polyline_getPartAt(indata->pThis, i) );
|
|---|
| 256 |
|
|---|
| 257 | rb_yield(arr);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | return self;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | // Point
|
|---|
| 264 |
|
|---|
| 265 | static VALUE rb_point_allocate(VALUE klass)
|
|---|
| 266 | {
|
|---|
| 267 | RPointData* indata = ALLOC(RPointData);
|
|---|
| 268 | indata->pThis = NULL;
|
|---|
| 269 |
|
|---|
| 270 | return Data_Wrap_Struct(klass, 0, rb_point_free, indata);
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | static void rb_point_free(void *ptr)
|
|---|
| 274 | {
|
|---|
| 275 | if (ptr)
|
|---|
| 276 | {
|
|---|
| 277 | if (((RPointData*)ptr)->pThis)
|
|---|
| 278 | c_Point_delete(((RPointData*)ptr)->pThis);
|
|---|
| 279 | free(ptr);
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | static VALUE rb_point_x(VALUE self)
|
|---|
| 284 | {
|
|---|
| 285 | RPointData* indata;
|
|---|
| 286 | Data_Get_Struct(self, RPointData, indata);
|
|---|
| 287 |
|
|---|
| 288 | return rb_float_new( c_Point_getX(indata->pThis) );
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | static VALUE rb_point_y(VALUE self)
|
|---|
| 292 | {
|
|---|
| 293 | RPointData* indata;
|
|---|
| 294 | Data_Get_Struct(self, RPointData, indata);
|
|---|
| 295 |
|
|---|
| 296 | return rb_float_new( c_Point_getY(indata->pThis) );
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | static VALUE rb_point_z(VALUE self)
|
|---|
| 300 | {
|
|---|
| 301 | RPointData* indata;
|
|---|
| 302 | Data_Get_Struct(self, RPointData, indata);
|
|---|
| 303 |
|
|---|
| 304 | return rb_float_new( c_Point_getZ(indata->pThis) );
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | static VALUE rb_point_m(VALUE self)
|
|---|
| 308 | {
|
|---|
| 309 | RPointData* indata;
|
|---|
| 310 | Data_Get_Struct(self, RPointData, indata);
|
|---|
| 311 |
|
|---|
| 312 | return rb_float_new( c_Point_getM(indata->pThis) );
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | // BoundingBox
|
|---|
| 316 | static VALUE rb_bbox_allocate(VALUE klass)
|
|---|
| 317 | {
|
|---|
| 318 | RubyBoundingBox* indata = ALLOC(RubyBoundingBox);
|
|---|
| 319 | return Data_Wrap_Struct(klass, 0, 0, indata);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | static VALUE rb_bbox_x_min(VALUE self)
|
|---|
| 323 | {
|
|---|
| 324 | RubyBoundingBox* indata;
|
|---|
| 325 | Data_Get_Struct(self, RubyBoundingBox, indata);
|
|---|
| 326 | return rb_float_new( indata->xMin );
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | static VALUE rb_bbox_x_max(VALUE self)
|
|---|
| 330 | {
|
|---|
| 331 | RubyBoundingBox* indata;
|
|---|
| 332 | Data_Get_Struct(self, RubyBoundingBox, indata);
|
|---|
| 333 | return rb_float_new( indata->xMax );
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | static VALUE rb_bbox_y_min(VALUE self)
|
|---|
| 337 | {
|
|---|
| 338 | RubyBoundingBox* indata;
|
|---|
| 339 | Data_Get_Struct(self, RubyBoundingBox, indata);
|
|---|
| 340 | return rb_float_new( indata->yMin );
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | static VALUE rb_bbox_y_max(VALUE self)
|
|---|
| 344 | {
|
|---|
| 345 | RubyBoundingBox* indata;
|
|---|
| 346 | Data_Get_Struct(self, RubyBoundingBox, indata);
|
|---|
| 347 | return rb_float_new( indata->yMax );
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | static VALUE rb_bbox_z_min(VALUE self)
|
|---|
| 351 | {
|
|---|
| 352 | RubyBoundingBox* indata;
|
|---|
| 353 | Data_Get_Struct(self, RubyBoundingBox, indata);
|
|---|
| 354 | return rb_float_new( indata->zMin );
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | static VALUE rb_bbox_z_max(VALUE self)
|
|---|
| 358 | {
|
|---|
| 359 | RubyBoundingBox* indata;
|
|---|
| 360 | Data_Get_Struct(self, RubyBoundingBox, indata);
|
|---|
| 361 | return rb_float_new( indata->zMax );
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | void Init_RubyESRIShape()
|
|---|
| 365 | {
|
|---|
| 366 | VALUE rb_mRubyESRIShape;
|
|---|
| 367 |
|
|---|
| 368 | // module
|
|---|
| 369 | rb_mRubyESRIShape = rb_define_module("RubyESRIShape");
|
|---|
| 370 |
|
|---|
| 371 | // class
|
|---|
| 372 | rb_cShape = rb_define_class_under (rb_mRubyESRIShape, "Shape", rb_cObject);
|
|---|
| 373 | rb_define_alloc_func(rb_cShape, rb_shape_allocate);
|
|---|
| 374 |
|
|---|
| 375 | rb_define_private_method(rb_cShape, "initialize", rb_shape_initialize, 1);
|
|---|
| 376 | rb_define_method(rb_cShape, "[]", rb_shape_aref , 1);
|
|---|
| 377 | rb_define_method(rb_cShape, "length", rb_shape_length, 0);
|
|---|
| 378 | rb_define_method(rb_cShape, "each", rb_shape_each , 0);
|
|---|
| 379 | rb_define_method(rb_cShape, "type", rb_shape_type , 0);
|
|---|
| 380 |
|
|---|
| 381 | // class
|
|---|
| 382 | rb_cPolygon = rb_define_class_under (rb_mRubyESRIShape, "Polygon", rb_cObject);
|
|---|
| 383 | rb_define_alloc_func(rb_cPolygon, rb_polygon_allocate);
|
|---|
| 384 | rb_define_method(rb_cPolygon, "[]" , rb_polygon_aref , 1);
|
|---|
| 385 | rb_define_method(rb_cPolygon, "length", rb_polygon_length, 0);
|
|---|
| 386 | rb_define_method(rb_cPolygon, "each" , rb_polygon_each , 0);
|
|---|
| 387 | rb_define_method(rb_cPolygon, "bounding_box", rb_polygon_bounding_box, 0);
|
|---|
| 388 |
|
|---|
| 389 | // class
|
|---|
| 390 | rb_cPolyline = rb_define_class_under (rb_mRubyESRIShape, "Polyline", rb_cObject);
|
|---|
| 391 | rb_define_alloc_func(rb_cPolyline, rb_polyline_allocate);
|
|---|
| 392 | rb_define_method(rb_cPolyline, "length", rb_polyline_length, 0);
|
|---|
| 393 | rb_define_method(rb_cPolyline, "[]" , rb_polyline_aref , 1);
|
|---|
| 394 | rb_define_method(rb_cPolyline, "each" , rb_polyline_each , 0);
|
|---|
| 395 |
|
|---|
| 396 | // class
|
|---|
| 397 | rb_cPoint = rb_define_class_under (rb_mRubyESRIShape, "Point", rb_cObject);
|
|---|
| 398 | rb_define_alloc_func(rb_cPoint, rb_point_allocate);
|
|---|
| 399 | rb_define_method(rb_cPoint, "x", rb_point_x, 0);
|
|---|
| 400 | rb_define_method(rb_cPoint, "y", rb_point_y, 0);
|
|---|
| 401 | rb_define_method(rb_cPoint, "z", rb_point_z, 0);
|
|---|
| 402 | rb_define_method(rb_cPoint, "m", rb_point_m, 0);
|
|---|
| 403 |
|
|---|
| 404 | // class
|
|---|
| 405 | rb_cBoundingBox = rb_define_class_under (rb_mRubyESRIShape, "BoundingBox", rb_cObject);
|
|---|
| 406 | rb_define_alloc_func(rb_cBoundingBox, rb_bbox_allocate);
|
|---|
| 407 |
|
|---|
| 408 | rb_define_method(rb_cBoundingBox, "x_min", rb_bbox_x_min, 0);
|
|---|
| 409 | rb_define_method(rb_cBoundingBox, "x_max", rb_bbox_x_max, 0);
|
|---|
| 410 | rb_define_method(rb_cBoundingBox, "y_min", rb_bbox_y_min, 0);
|
|---|
| 411 | rb_define_method(rb_cBoundingBox, "y_max", rb_bbox_y_max, 0);
|
|---|
| 412 | rb_define_method(rb_cBoundingBox, "z_min", rb_bbox_z_min, 0);
|
|---|
| 413 | rb_define_method(rb_cBoundingBox, "z_max", rb_bbox_z_max, 0);
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | #else
|
|---|
| 417 |
|
|---|
| 418 | void Init_RubyESRIShape()
|
|---|
| 419 | {
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | #endif |
|---|