-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyGeometryModule.m
More file actions
627 lines (523 loc) · 19.1 KB
/
Copy pathPyGeometryModule.m
File metadata and controls
627 lines (523 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//
// PyGeometryModule.m
// MapClueCD
//
// Created by Gaige B. Paulsen on 4/6/10.
// Copyright 2010 ClueTrust. All rights reserved.
//
#import "PyGeometryModule.h"
#import "MapGeometryExternalProtocol.h"
static NSMutableDictionary *libraryValues=NULL;
static PyObject *GeometryError=NULL;
static BOOL isLoaded=NO;
#define kPyGeometryModuleName "geometry"
#define kPyGeometryModuleDescription "Geometry Routines for Cartographica"
#pragma mark Python Declarations
PyObject *geometry_point_getAttr(PyObject *myself, PyObject *name);
int geometry_point_setAttr(PyObject *myself, PyObject *name, PyObject *newValue);
PyObject *geometry_rect_getAttr(PyObject *myself, PyObject *pyName);
int geometry_rect_setAttr(PyObject *myself, PyObject *name, PyObject *newValue);
PyObject *geometry_length( PyObject *self, PyObject *args);
PyObject *geometry_centroid( PyObject *self, PyObject *args);
PyObject *geometry_midpoint( PyObject *self, PyObject *args);
PyObject *geometry_area( PyObject *self, PyObject *args);
PyObject *geometry_bbox( PyObject *self, PyObject *args);
PyObject *geometry_pointcount( PyObject *self, PyObject *args);
PyObject *geometry_partcount( PyObject *self, PyObject *args);
static PyMethodDef geometryMethods[] = {
{ "length", geometry_length, METH_VARARGS, "returns the length of the geometry"},
{ "centroid", geometry_centroid, METH_VARARGS, "returns centroid as a point object"},
{ "area", geometry_area, METH_VARARGS, "returns area of the geometry"},
{ "midpoint", geometry_midpoint, METH_VARARGS, "returns midpoint as a point object"},
{ "bbox", geometry_bbox, METH_VARARGS, "returns bounding box as a rect object"},
{ "pointcount", geometry_pointcount, METH_VARARGS, "returns the point count as an integer"},
{ "partcount", geometry_partcount, METH_VARARGS, "returns the part count as an integer"},
{NULL, NULL, 0, NULL}
};
typedef struct {
PyObject_HEAD
double x,y,height,width;
} PyGeometryRectObject;
static PyTypeObject PyGeometryRectObjectType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "geometry.rect",
.tp_basicsize = sizeof(PyGeometryRectObject),
.tp_getattro = geometry_rect_getAttr,
.tp_setattro = geometry_rect_setAttr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "Geometric rectangle type.",
};
typedef struct {
PyObject_HEAD
double x,y,z,m;
NSUInteger identifier;
} PyGeometryPointObject;
static PyTypeObject PyGeometryPointObjectType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "geometry.point",
.tp_basicsize = sizeof(PyGeometryPointObject),
.tp_getattro = geometry_point_getAttr,
.tp_setattro = geometry_point_setAttr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = "Geometric point type.",
};
@implementation PyGeometryModule
+ (void)setLibraryValue:(id)value forKey:(NSString*)key
{
if (libraryValues)
[libraryValues setObject: value forKey: key];
else {
libraryValues = [NSMutableDictionary dictionaryWithObject: value forKey: key];
}
}
+ (void)clearLibraryValues
{
[libraryValues removeAllObjects];
}
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
kPyGeometryModuleName, /* m_name */
kPyGeometryModuleDescription, /* m_doc */
-1, /* m_size */
geometryMethods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
+ (PyObject*)loadModule
{
PyObject *module = PyModule_Create(&moduledef);
if (!module)
return NULL;
GeometryError = PyErr_NewException( "geometry.error", NULL, NULL);
Py_INCREF( GeometryError);
PyModule_AddObject( module, "error", GeometryError);
// now add our point type
PyGeometryPointObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyGeometryPointObjectType) < 0)
return NULL;
Py_INCREF(&PyGeometryPointObjectType);
PyModule_AddObject(module, "point", (PyObject *)&PyGeometryPointObjectType);
// now add our rect type
PyGeometryRectObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyGeometryRectObjectType) < 0)
return NULL;
Py_INCREF(&PyGeometryRectObjectType);
PyModule_AddObject(module, "rect", (PyObject *)&PyGeometryRectObjectType);
isLoaded = YES;
return module;
}
+ (void) unloadModule
{
isLoaded = NO;
if (GeometryError) {
Py_DECREF( GeometryError);
GeometryError = NULL;
}
// Py_DECREF(&PyGeometryPointObjectType); -- NOTE: doing this in unload causes one of the finalizes to fail
libraryValues = nil;
}
+ (char *)cModuleName
{
return kPyGeometryModuleName;
}
+ (char *)cDocString
{
return kPyGeometryModuleDescription;
}
+ (BOOL)interpretException:(PyObject*)exception object: (PyObject*)exceptionObject intoError:(NSError**)errorPtr
{
NSAssert( errorPtr, @"need an error pointer");
if (!errorPtr)
return NO; // since we can't give feedback, we didn't interpret it
if (!PyErr_GivenExceptionMatches( exception, GeometryError))
return NO;
NSInteger code = kPyGeometryError_noGeometry;
if (PyNumber_Check( exceptionObject))
code = PyLong_AsLong( exceptionObject);
NSMutableDictionary *info = [NSMutableDictionary dictionary];
switch (code) {
case kPyGeometryError_wrongKind:
[info setObject: NSLocalizedString( @"Wrong geometry for this function", @"Error")
forKey: NSLocalizedFailureReasonErrorKey];
[info setObject: NSLocalizedString( @"Wrong geometry kind", @"Error")
forKey: NSLocalizedDescriptionKey];
[info setObject: NSLocalizedString( @"This function doesn't work on this type of geometry", @"Error")
forKey: NSLocalizedRecoverySuggestionErrorKey];
break;
case kPyGeometryError_otherError:
[info setObject: NSLocalizedString( @"Invalid return from function", @"Error")
forKey: NSLocalizedFailureReasonErrorKey];
[info setObject: NSLocalizedString( @"Infinite point returned", @"Error")
forKey: NSLocalizedDescriptionKey];
[info setObject: NSLocalizedString( @"Something caused an error in the underlying function", @"Error")
forKey: NSLocalizedRecoverySuggestionErrorKey];
break;
case kPyGeometryError_noGeometry:
default:
[info setObject: NSLocalizedString( @"No geometry to work on", @"Error")
forKey: NSLocalizedFailureReasonErrorKey];
[info setObject: NSLocalizedString( @"No Geometry set in module", @"Error")
forKey: NSLocalizedDescriptionKey];
[info setObject: NSLocalizedString( @"Make sure this function is called with a valid geometry", @"Error")
forKey: NSLocalizedRecoverySuggestionErrorKey];
break;
}
*errorPtr = [NSError errorWithDomain: kPyGeometryModuleDomain code: code userInfo: info];
return YES;
}
@end
#pragma mark Point code
PyObject *geometry_point_getAttr(PyObject *myself, PyObject *pyName)
{
PyGeometryPointObject *point = (PyGeometryPointObject*)myself;
if (!myself) {
PyErr_SetString( PyExc_AttributeError, "no object");
return NULL;
}
// TODO check type
assert(Py_TYPE(point) == &PyGeometryPointObjectType);
if ((!pyName) || (!PyUnicode_Check( pyName))) {
PyErr_SetString( PyExc_AttributeError, "invalid type of name");
return NULL;
}
const char *name = PyUnicode_AsUTF8( pyName);
switch (name[0]) {
case 'x': return Py_BuildValue("f",point->x);//PyFloat_FromDouble( point->x);
case 'y': return PyFloat_FromDouble( point->y);
case 'z': return PyFloat_FromDouble( point->z);
case 'm': return PyFloat_FromDouble( point->m);
case 'i': return PyLong_FromLong( point->identifier);
}
return PyErr_Format( PyExc_AttributeError, "type object 'geometry.point' has no attribute '%s'", name);
}
int geometry_point_setAttr(PyObject *myself, PyObject *name, PyObject *newValue)
{
PyErr_SetString( PyExc_TypeError, "setting not yet allowed");
return -1;
}
#pragma mark Rect code
PyObject *geometry_rect_getAttr(PyObject *myself, PyObject *pyName)
{
PyGeometryRectObject *pRect = (PyGeometryRectObject*)myself;
if (!myself) {
PyErr_SetString( PyExc_AttributeError, "no object");
return NULL;
}
// TODO check type
assert(Py_TYPE(pRect) == &PyGeometryRectObjectType);
if ((!pyName) || (!PyUnicode_Check( pyName))) {
PyErr_SetString( PyExc_AttributeError, "invalid type of name");
return NULL;
}
const char *name = PyUnicode_AsUTF8(pyName);
switch (name[0]) {
case 'x': return Py_BuildValue("f",pRect->x);//PyFloat_FromDouble( point->x);
case 'y': return PyFloat_FromDouble( pRect->y);
case 'h': return PyFloat_FromDouble( pRect->height);
case 'w': return PyFloat_FromDouble( pRect->width);
// case 'o': return PyFloat_FromDouble( pRect->m); // origin as point
}
return PyErr_Format( PyExc_AttributeError, "type object 'geometry.point' has no attribute '%s'", name);
}
int geometry_rect_setAttr(PyObject *myself, PyObject *name, PyObject *newValue)
{
PyErr_SetString( PyExc_TypeError, "setting not yet allowed");
return -1;
}
#pragma mark Python Code
//kMapEntityDraw_Null = kShpFile_Shape_Null,
//kMapEntityDraw_Point= kShpFile_Shape_Point,
//kMapEntityDraw_Line = kShpFile_Shape_PolyLine,
//kMapEntityDraw_Poly = kShpFile_Shape_Polygon,
PyObject *geometry_length( PyObject *self, PyObject *args)
{
// ignore args, since we don't have any
int base=0;
if(!PyArg_ParseTuple(args, "|I:length", &base))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
// run the geometry function
__block NSNumber *resultDistance=nil;
NSCondition *condition = [[NSCondition alloc] init];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[condition lock];
[geometry distanceOfGeometry: geometryIndex asBase:base withReply:^(NSNumber *distance) {
[condition lock];
resultDistance = distance;
[condition signal];
[condition unlock];
}];
while(!resultDistance) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
// no value = wrong kind
if (!resultDistance) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_wrongKind));
return NULL;
}
return Py_BuildValue("f", resultDistance.doubleValue);
}
PyObject *geometry_area( PyObject *self, PyObject *args)
{
int base=0;
// ignore args, since we don't have any
if(!PyArg_ParseTuple(args, "|I:area", &base))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
// run the geometry function
__block NSNumber *resultArea=nil;
NSCondition *condition = [[NSCondition alloc] init];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[condition lock];
[geometry areaOfGeometry: geometryIndex asBase:base withReply:^(NSNumber *area) {
[condition lock];
resultArea = area;
[condition signal];
[condition unlock];
}];
while (!resultArea) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
// no value = wrong kind
if (!resultArea) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_wrongKind));
return NULL;
}
return Py_BuildValue("f", resultArea.doubleValue);
}
PyObject *geometry_centroid( PyObject *self, PyObject *args)
{
int base=0;
if(!PyArg_ParseTuple(args, "|I:centroid", &base))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
// run the geometry function
__block NSValue *resultPoint=nil;
NSCondition *condition = [[NSCondition alloc] init];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
__block BOOL done=NO;
[condition lock];
[geometry centroidOfGeometry: geometryIndex asBase:base withReply:^(NSValue *point) {
[condition lock];
resultPoint = point;
done=YES;
[condition signal];
[condition unlock];
}];
while (!done) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
// no value = wrong kind
if (!resultPoint) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_wrongKind));
return NULL;
}
NSPoint point = resultPoint.pointValue;
double z=0, m=0;
if (ISNULLPOINT(point.x,point.y)) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_otherError));
return NULL;
}
PyGeometryPointObject *pPoint = PyObject_New( PyGeometryPointObject, &PyGeometryPointObjectType);
if (!pPoint) {
return PyErr_NoMemory();
}
pPoint->x = point.x;
pPoint->y = point.y;
pPoint->z = z;
pPoint->m = m;
pPoint->identifier = 0;
return (PyObject*)pPoint;
}
PyObject *geometry_midpoint( PyObject *self, PyObject *args)
{
int base=0;
if(!PyArg_ParseTuple(args, "|I:midpoint", &base))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
// run the geometry function
__block NSValue *resultPoint=nil;
__block BOOL done=NO;
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
NSCondition *condition = [[NSCondition alloc] init];
[condition lock];
[geometry midpointOfGeometry: geometryIndex asBase:base withReply:^(NSValue *point) {
[condition lock];
resultPoint = point;
done = YES;
[condition signal];
[condition unlock];
}];
while (!done) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
// no value = wrong kind
if (!resultPoint) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_wrongKind));
return NULL;
}
NSPoint point = resultPoint.pointValue;
if (ISNULLPOINT(point.x,point.y)) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_otherError));
return NULL;
}
PyGeometryPointObject *pPoint = PyObject_New( PyGeometryPointObject, &PyGeometryPointObjectType);
if (!pPoint) {
return PyErr_NoMemory();
}
pPoint->x = point.x;
pPoint->y = point.y;
pPoint->z = 0;
pPoint->m = 0;
pPoint->identifier = 0;
return (PyObject*)pPoint;
}
PyObject *geometry_bbox( PyObject *self, PyObject *args)
{
int base=0;
if(!PyArg_ParseTuple(args, "|I:bbox", &base))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
// run the geometry function
__block NSValue *resultRect=nil;
__block BOOL done=NO;
NSCondition *condition = [[NSCondition alloc] init];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[condition lock];
[geometry boundingBoxOfGeometry: geometryIndex asBase:base withReply:^(NSValue *rect) {
[condition lock];
resultRect = rect;
done=YES;
[condition signal];
[condition unlock];
}];
while(!done) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
// no value = wrong kind
if (!resultRect) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_wrongKind));
return NULL;
}
PyGeometryRectObject *pRect = PyObject_New( PyGeometryRectObject, &PyGeometryRectObjectType);
if (!pRect) {
return PyErr_NoMemory();
}
NSRect boundingBox = resultRect.rectValue;
pRect->x = boundingBox.origin.x;
pRect->y = boundingBox.origin.y;
pRect->height = boundingBox.size.height;
pRect->width = boundingBox.size.width;
return (PyObject*)pRect;
}
PyObject *geometry_pointcount( PyObject *self, PyObject *args)
{
if(!PyArg_ParseTuple(args, ":pointcount"))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
__block NSNumber *resultCount;
__block BOOL done=NO;
NSCondition *condition = [[NSCondition alloc] init];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[condition lock];
[geometry pointCountOfGeometry: geometryIndex withReply:^(NSNumber *count) {
[condition lock];
resultCount = count;
done = YES;
[condition signal];
[condition unlock];
}];
while(!done) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
return Py_BuildValue("i", resultCount.intValue);
}
PyObject *geometry_partcount( PyObject *self, PyObject *args)
{
if(!PyArg_ParseTuple(args, ":partcount"))
return NULL;
id<MapGeometryExternalProtocol> geometry = [libraryValues objectForKey: @"geometry"];
if (!geometry) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
return NULL;
}
NSUInteger geometryIndex = [[libraryValues objectForKey: @"geometryIndex"] unsignedIntValue];
__block NSNumber *resultCount;
__block BOOL done = NO;
NSCondition *condition = [[NSCondition alloc] init];
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[condition lock];
[geometry partCountOfGeometry: geometryIndex withReply:^(NSNumber *count) {
[condition lock];
resultCount = count;
done=YES;
[condition signal];
[condition unlock];
}];
while(!done) {
if (![condition waitUntilDate: timeout]) {
PyErr_SetObject( GeometryError, Py_BuildValue( "i", kPyGeometryError_noGeometry));
break;
}
}
[condition unlock];
return Py_BuildValue("i", resultCount.intValue);
}