-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathplplotc.i
More file actions
1703 lines (1584 loc) · 53.7 KB
/
Copy pathplplotc.i
File metadata and controls
1703 lines (1584 loc) · 53.7 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%module plplotc
//
// Copyright (C) 2002 Gary Bishop
// Copyright (C) 2002-2026 Alan W. Irwin
// Copyright (C) 2004-2005 Rafael Laboissiere
// Copyright (C) 2004-2013 Andrew Ross
// Copyright (C) 2009 Werner Smekal
// Copyright (C) 2013 Arjen Markus
// Copyright (C) 2017 Hazen Babcock
//This file is part of PLplot.
//
//PLplot is free software; you can redistribute it and/or modify
//it under the terms of the GNU Library General Public License as published by
//the Free Software Foundation; version 2 of the License.
//
//PLplot is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU Library General Public License for more details.
//
//You should have received a copy of the GNU Library General Public License
//along with the file PLplot; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//
//A SWIG interface to PLplot for Python. This wrapper is different from
//the one supplied in plmodule.c (now stored in the "old" subdirectory of
//the top-level build tree for reference purposes) in that:
//
// 1) it strictly provides the C-API with the usual change of not
// requiring lengths for arrays,
//
// 2) it attempts to provide the entire API including callbacks for
// plcont and plshade.
//
// 3) it works both with the single and double-precision versions of the
// PLplot library.
//
//This is known to work with swig-1.3.17 (versions prior to 1.3.14 *known* not
//to work, 1.3.14-1.3.16 might work but you are on your own). The resulting
//interface works for both python-1.5.2 (RedHat 7.3) and python-2.1.3 (Debian
//woody).
//
//
%include <typemaps.i>
%{
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <arrayobject.h>
#include "plplot.h"
#include "plplotP.h"
#define NPY_PLINT NPY_INT32
#ifdef PL_DOUBLE
#define NPY_PLFLT NPY_FLOAT64
#else
#define NPY_PLFLT NPY_FLOAT32
#endif
// python-1.5 compatibility mode?
#if !defined ( PySequence_Fast_GET_ITEM )
#define PySequence_Fast_GET_ITEM PySequence_GetItem
#endif
#define PySequence_Size PySequence_Length
%}
#ifdef PL_DOUBLE_INTERFACE
typedef double PLFLT;
#else
typedef float PLFLT;
#endif
// This assumes that C int is 32-bit - swig doesn't know about int32_t
// Ideally we should have a typemap for it
typedef int PLINT;
typedef unsigned int PLUNICODE;
typedef PLINT PLBOOL;
// We have to get import_array called in our extension before we can use Numeric
%init
%{
import_array();
%}
// I hate global variables but this is the best way I can think of to manage consistency
// checking among function arguments.
%{
static PLINT Alen = 0;
static PLINT Xlen = 0, Ylen = 0;
%}
// The following typemaps take care of marshaling values into and out of PLplot functions. The
//Array rules are trickly because of the need for length checking. These rules manage
//some global variables (above) to handle consistency checking amoung parameters.
//
//Naming rules:
// Array (sets Alen to dim[0])
// ArrayCk (tests that dim[0] == Alen)
// ArrayX (sets Xlen to dim[0]
// ArrayCkX (tests dim[0] == Xlen)
// ArrayY (sets Ylen to dim[1])
// ArrayCkY (tests dim[1] == Ylen)
// Matrix (sets Xlen to dim[0], Ylen to dim[1])
// MatrixCk (test Xlen == dim[0] && Ylen == dim[1])
//
//--------------------------------------------------------------------------
// PLINT arrays
//--------------------------------------------------------------------------
// If Python integers are not 32-bit then we need to do some casting
#if SIZEOF_LONG != 4
%wrapper
%{
PyArrayObject* myIntArray_ContiguousFromObject( PyObject* in, int type, int mindims, int maxdims );
// some really twisted stuff to allow calling a single precision library from python
PyArrayObject* myIntArray_ContiguousFromObject( PyObject* in, int PL_UNUSED( type ), int mindims, int maxdims )
{
PyArrayObject* tmp = (PyArrayObject *) PyArray_ContiguousFromObject( in, NPY_PLINT,
mindims, maxdims );
if ( !tmp )
{
// could be an incoming long array which can't be "safely" converted, do it anyway
if ( PyArray_Check( in ) )
{
PyErr_Clear();
tmp = (PyArrayObject *) PyArray_Cast( (PyArrayObject *) in, NPY_PLINT );
}
}
return tmp;
}
%}
#else
%wrapper
%{
#define myIntArray_ContiguousFromObject PyArray_ContiguousFromObject
%}
#endif
// With preceding count
%typemap( in ) ( PLINT n, const PLINT * Array ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
$1 = Alen = PyArray_DIMS( tmp )[0];
$2 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) ( PLINT n, const PLINT * Array )
{
Py_CLEAR( tmp$argnum );
}
// Trailing count and check consistency with previous
%typemap( in ) ( const PLINT * ArrayCk, PLINT n ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$2 = PyArray_DIMS( tmp )[0];
$1 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) ( const PLINT * ArrayCk, PLINT n )
{
Py_CLEAR( tmp$argnum );
}
// No count but check consistency with previous
%typemap( in ) const PLINT * ArrayCk( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLINT * ArrayCk { Py_CLEAR( tmp$argnum ); }
// No count but check consistency with previous or NULL
%typemap( in ) const PLINT * ArrayCkNull( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLINT * ArrayCkNull { Py_CLEAR( tmp$argnum ); }
// Weird case to allow argument to be one shorter than others
%typemap( in ) const PLINT * ArrayCkMinus1( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] < Alen - 1 )
{
PyErr_SetString( PyExc_ValueError, "Vector must be at least length of others minus 1." );
return NULL;
}
$1 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLINT * ArrayCkMinus1 { Py_CLEAR( tmp$argnum ); }
%typemap( in ) const PLINT * ArrayCkMinus1Null( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] < Alen - 1 )
{
PyErr_SetString( PyExc_ValueError, "Vector must be at least length of others minus 1." );
return NULL;
}
$1 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLINT * ArrayCkMinus1Null { Py_CLEAR( tmp$argnum ); }
// No length but remember size to check others
%typemap( in ) const PLINT * Array( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
Alen = PyArray_DIMS( tmp )[0];
$1 = (PLINT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLINT * Array { Py_CLEAR( tmp$argnum ); }
// set X and Y length for later consistency checking
%typemap( in ) const PLINT * ArrayN( PyArrayObject * tmp = NULL )
{
int i;
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
Xlen = PyArray_DIMS( tmp )[0];
$1 = (PLINT *) PyArray_DATA( tmp );
Ylen = -1;
for ( i = 0; i < Xlen; i++ )
if ( $1[i] > Ylen )
Ylen = $1[i];
}
%typemap( freearg ) const PLINT * ArrayN { Py_CLEAR( tmp$argnum ); }
// With trailing count and NULL array option.
%typemap( in ) ( const PLINT * ArrayNull, PLINT n ) ( PyArrayObject * tmp = NULL )
{
if ( $input != Py_None )
{
tmp = (PyArrayObject *) myIntArray_ContiguousFromObject( $input, NPY_PLINT, 1, 1 );
if ( tmp == NULL )
return NULL;
$1 = (PLINT *) PyArray_DATA( tmp );
$2 = PyArray_DIMS( tmp )[0];
}
else
{
$1 = NULL;
$2 = 0;
}
}
%typemap( freearg ) ( const PLINT * ArrayNull, PLINT n )
{
Py_CLEAR( tmp$argnum );
}
//--------------------------------------------------------------------------
// PLFLT Arrays
//--------------------------------------------------------------------------
#ifndef PL_DOUBLE_INTERFACE
%wrapper
%{
// some really twisted stuff to allow calling a single precision library from python
PyArrayObject* myArray_ContiguousFromObject( PyObject* in, int type, int mindims, int maxdims )
{
PyArrayObject* tmp = (PyArrayObject *) PyArray_ContiguousFromObject( in, NPY_FLOAT32,
mindims, maxdims );
if ( !tmp )
{
// could be an incoming double array which can't be "safely" converted, do it anyway
if ( PyArray_Check( in ) )
{
PyErr_Clear();
tmp = (PyArrayObject *) PyArray_Cast( (PyArrayObject *) in, NPY_FLOAT32 );
}
}
return tmp;
}
%}
#else
%wrapper
%{
#define myArray_ContiguousFromObject PyArray_ContiguousFromObject
%}
#endif
// with preceding count
%typemap( in ) ( PLINT n, const PLFLT * Array ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
$1 = Alen = PyArray_DIMS( tmp )[0];
$2 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) ( PLINT n, const PLFLT * Array )
{
Py_CLEAR( tmp$argnum );
}
// trailing count and check consistency with previous
%typemap( in ) ( const PLFLT * ArrayCk, PLINT n ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLFLT *) PyArray_DATA( tmp );
$2 = PyArray_DIMS( tmp )[0];
}
%typemap( freearg ) ( const PLFLT * ArrayCk, PLINT n )
{
Py_CLEAR( tmp$argnum );
}
// trailing count and check consistency with previous
%typemap( in ) ( const PLFLT * ArrayCkNull, PLINT n ) ( PyArrayObject * tmp = NULL )
{
if ( $input != Py_None )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLFLT *) PyArray_DATA( tmp );
$2 = PyArray_DIMS( tmp )[0];
}
else
{
$1 = NULL;
$2 = 0;
}
}
%typemap( freearg ) ( const PLFLT * ArrayCkNull, PLINT n )
{
Py_CLEAR( tmp$argnum );
}
// no count, but check consistency with previous
%typemap( in ) const PLFLT * ArrayCk( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLFLT * ArrayCk { Py_CLEAR( tmp$argnum ); }
// no count, but check consistency with previous, or NULL
%typemap( in ) const PLFLT * ArrayCkNull( PyArrayObject * tmp = NULL )
{
if ( $input != Py_None )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLFLT *) PyArray_DATA( tmp );
}
else
{
$1 = NULL;
}
}
%typemap( freearg ) const PLFLT * ArrayCkNull { Py_CLEAR( tmp$argnum ); }
// check consistency with X dimension of previous
%typemap( in ) const PLFLT * ArrayCkX( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Xlen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLFLT * ArrayCkX { Py_CLEAR( tmp$argnum ); }
// check consistency with Y dimension of previous
%typemap( in ) const PLFLT * ArrayCkY( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Ylen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLFLT * ArrayCkY { Py_CLEAR( tmp$argnum ); }
// set X length for later consistency checking, with trailing count
%typemap( in ) ( const PLFLT * ArrayX, PLINT nx ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Xlen = PyArray_DIMS( tmp )[0];
$2 = Xlen;
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) ( const PLFLT * ArrayX, PLINT nx )
{
Py_CLEAR( tmp$argnum );
}
// set X length for later consistency checking
%typemap( in ) const PLFLT * ArrayX( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Xlen = PyArray_DIMS( tmp )[0];
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLFLT * ArrayX { Py_CLEAR( tmp$argnum ); }
// Set Y length for later consistency checking, with trailing count
%typemap( in ) ( const PLFLT * ArrayY, PLINT ny ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Ylen = PyArray_DIMS( tmp )[0];
$2 = Ylen;
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) ( const PLFLT * ArrayY, PLINT ny )
{
Py_CLEAR( tmp$argnum );
}
// set Y length for later consistency checking
%typemap( in ) const PLFLT * ArrayY( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Ylen = PyArray_DIMS( tmp )[0];
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLFLT * ArrayY { Py_CLEAR( tmp$argnum ); }
// with trailing count
%typemap( in ) ( const PLFLT * Array, PLINT n ) ( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
$2 = PyArray_DIMS( tmp )[0];
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) ( const PLFLT * Array, PLINT n )
{
Py_CLEAR( tmp$argnum );
}
// with no count
%typemap( in ) const PLFLT * Array( PyArrayObject * tmp = NULL )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Alen = PyArray_DIMS( tmp )[0];
$1 = (PLFLT *) PyArray_DATA( tmp );
}
%typemap( freearg ) const PLFLT * Array { Py_CLEAR( tmp$argnum ); }
// with no count
%typemap( in ) const PLFLT * ArrayNull( PyArrayObject * tmp = NULL )
{
if ( $input != Py_None )
{
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Alen = PyArray_DIMS( tmp )[0];
$1 = (PLFLT *) PyArray_DATA( tmp );
}
else
{
$1 = NULL;
Alen = 0;
}
}
%typemap( freearg ) const PLFLT * ArrayNull { Py_CLEAR( tmp$argnum ); }
// 2D array with trailing dimensions, check consistency with previous
%typemap( in ) ( const PLFLT * *MatrixCk, PLINT nx, PLINT ny ) ( PyArrayObject * tmp = NULL )
{
int i, size;
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 2, 2 );
if ( tmp == NULL )
return NULL;
if ( Xlen != PyArray_DIMS( tmp )[0] || Ylen != PyArray_DIMS( tmp )[1] )
{
PyErr_SetString( PyExc_ValueError, "Vectors must match matrix." );
return NULL;
}
$2 = PyArray_DIMS( tmp )[0];
$3 = PyArray_DIMS( tmp )[1];
size = $3;
$1 = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) $2 );
for ( i = 0; i < $2; i++ )
$1[i] = ( (PLFLT *) PyArray_DATA( tmp ) + i * size );
}
%typemap( freearg ) ( const PLFLT * *MatrixCk, PLINT nx, PLINT ny )
{
Py_CLEAR( tmp$argnum );
free( $1 );
}
// 2D array with trailing dimensions, set the X, Y size for later checking
%typemap( in ) ( const PLFLT * *Matrix, PLINT nx, PLINT ny ) ( PyArrayObject * tmp = NULL )
{
int i, size;
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 2, 2 );
if ( tmp == NULL )
return NULL;
Xlen = $2 = PyArray_DIMS( tmp )[0];
Ylen = $3 = PyArray_DIMS( tmp )[1];
size = $3;
$1 = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) $2 );
for ( i = 0; i < $2; i++ )
$1[i] = ( (PLFLT *) PyArray_DATA( tmp ) + i * size );
}
%typemap( freearg ) ( const PLFLT * *Matrix, PLINT nx, PLINT ny )
{
Py_CLEAR( tmp$argnum );
free( $1 );
}
// 2D array with no dimensions, set the X, Y size for later checking
%typemap( in ) const PLFLT **Matrix( PyArrayObject * tmp = NULL )
{
int i, size;
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 2, 2 );
if ( tmp == NULL )
return NULL;
Xlen = PyArray_DIMS( tmp )[0];
Ylen = PyArray_DIMS( tmp )[1];
size = Ylen;
$1 = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) Xlen );
for ( i = 0; i < Xlen; i++ )
$1[i] = ( (PLFLT *) PyArray_DATA( tmp ) + i * size );
}
%typemap( freearg ) const PLFLT **Matrix {
Py_CLEAR( tmp$argnum );
free( $1 );
}
// 2D array, check for consistency
%typemap( in ) const PLFLT **MatrixCk( PyArrayObject * tmp = NULL )
{
int i, size;
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 2, 2 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Xlen || PyArray_DIMS( tmp )[1] != Ylen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must match matrix." );
return NULL;
}
size = Ylen;
$1 = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) Xlen );
for ( i = 0; i < Xlen; i++ )
$1[i] = ( (PLFLT *) PyArray_DATA( tmp ) + i * size );
}
%typemap( freearg ) const PLFLT **MatrixCk {
Py_CLEAR( tmp$argnum );
free( $1 );
}
// 2D array, check for consistency and modify in place version (no longer used
// in favor of correct output version in the combined typemap below).
%typemap( in ) PLFLT **OutMatrixCk( PyArrayObject * tmp = NULL )
{
int i, size;
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 2, 2 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Xlen || PyArray_DIMS( tmp )[1] != Ylen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must match matrix." );
return NULL;
}
size = Ylen;
$1 = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) Xlen );
for ( i = 0; i < Xlen; i++ )
$1[i] = ( (PLFLT *) PyArray_DATA( tmp ) + i * size );
}
%typemap( freearg ) PLFLT **OutMatrixCk {
Py_CLEAR( tmp$argnum );
free( $1 );
}
// Combined typemap useful for specialized case of plgriddata.
// Set Y length for consistency checking, with trailing count
// combined with 2D output array, check for consistency
%typemap( in ) ( const PLFLT * ArrayY, PLINT ny, PLFLT **OutMatrixCk ) ( PyArrayObject * tmp = NULL, PyObject * array = NULL )
{
int i, size;
npy_intp dims[2];
tmp = (PyArrayObject *) myArray_ContiguousFromObject( $input, NPY_PLFLT, 1, 1 );
if ( tmp == NULL )
return NULL;
Ylen = PyArray_DIMS( tmp )[0];
$2 = Ylen;
$1 = (PLFLT *) PyArray_DATA( tmp );
// Make created 2D array have dimensions from prior ArrayX in the argument
// list and this ArrayY.
dims[0] = Xlen;
dims[1] = Ylen;
array = PyArray_SimpleNew( 2, dims, NPY_PLFLT );
if ( !array )
return NULL;
size = Ylen;
$3 = (PLFLT **) malloc( sizeof ( double * ) * (size_t) Xlen );
for ( i = 0; i < Xlen; i++ )
$3[i] = ( (PLFLT *) PyArray_DATA( (PyArrayObject *) array ) + i * size );
}
%typemap( freearg ) ( const PLFLT * ArrayY, PLINT ny, PLFLT **OutMatrixCk )
{
Py_CLEAR( tmp$argnum );
free( $3 );
}
%typemap( argout ) ( const PLFLT * ArrayY, PLINT ny, PLFLT **OutMatrixCk )
{
$result = SWIG_Python_AppendOutput( $result, array$argnum, $isvoid);
}
//**************************
// special for pllegend / plcolorbar, char ** ArrayCk
//***************************
// no count, but check consistency with previous. Always allow NULL strings.
%typemap( in ) const char **ArrayCk( PyArrayObject * tmp = NULL )
{
int i;
tmp = (PyArrayObject *) PyArray_ContiguousFromObject( $input, NPY_STRING, 1, 1 );
if ( tmp == NULL )
return NULL;
if ( PyArray_DIMS( tmp )[0] != Alen )
{
PyErr_SetString( PyExc_ValueError, "Vectors must be same length." );
return NULL;
}
$1 = (char **) malloc( sizeof ( char* ) * (size_t) Alen );
for ( i = 0; i < Alen; i++ )
{
$1[i] = (char *) PyArray_DATA( tmp ) + i * PyArray_STRIDES( tmp )[0];
if ( $1[i] == NULL )
{
free( $1 );
return NULL;
}
}
}
%typemap( freearg ) const char **ArrayCk { Py_CLEAR( tmp$argnum ); free( $1 ); }
// With count. Always allow NULL strings.
%typemap( in ) ( PLINT n, const char **Array ) ( PyArrayObject * tmp = NULL )
{
int i;
tmp = (PyArrayObject *) PyArray_ContiguousFromObject( $input, NPY_STRING, 1, 1 );
if ( tmp == NULL )
return NULL;
Alen = PyArray_DIMS( tmp )[0];
$1 = Alen;
$2 = (char **) malloc( sizeof ( char* ) * (size_t) Alen );
for ( i = 0; i < Alen; i++ )
{
$2[i] = (char *) PyArray_DATA( tmp ) + i * PyArray_STRIDES( tmp )[0];
if ( $2[i] == NULL )
{
free( $2 );
return NULL;
}
}
}
%typemap( freearg ) ( PLINT n, const char **Array )
{
Py_CLEAR( tmp$argnum ); free( $2 );
}
//**************************
// String returning functions
//***************************
// This currently just used for plgdev, plgfnam, and plgver which apparently
// have a limit of 80 bytes. But to (hopefully) be safe for any future use
// have a 1000 byte limit here.
%typemap( in, numinputs = 0 ) char *OUTPUT( char buff[1000] )
{
$1 = buff;
}
%typemap( argout ) char *OUTPUT {
PyObject *o = PyString_FromString( $1 );
$result = SWIG_AppendOutput( $result, o );
}
//**************************
// A trick for docstrings
//***************************
%define DOC( func, string )
%wrapper
%{
# define _doc_ ## func string
%}
%enddef
// All code associated with special call-back functions.
// Identity transformation.
%typemap( in, numinputs = 0 ) PLPointer IGNORE {
$1 = NULL;
}
void
pltr0( PLFLT x, PLFLT y, PLFLT *OUTPUT, PLFLT *OUTPUT, PLPointer IGNORE );
// This typemap takes care of converting a Python 2-tuple of 1D Arrays to the
// PLcGrid structure that pltr1 expects
%wrapper
%{
PyArrayObject *pltr_xg, *pltr_yg;
static PLcGrid tmpGrid1;
static PLcGrid2 tmpGrid2;
PLcGrid* marshal_PLcGrid1( PyObject* input, int isimg );
void cleanup_PLcGrid1( void );
PLcGrid2* marshal_PLcGrid2( PyObject* input, int isimg );
void cleanup_PLcGrid2( void );
PLcGrid* marshal_PLcGrid1( PyObject* input, int isimg )
{
// fprintf(stderr, "marshal PLcGrid1\n");
if ( !PySequence_Check( input ) || PySequence_Size( input ) != 2 )
{
PyErr_SetString( PyExc_ValueError, "Expected a sequence of two arrays." );
return NULL;
}
pltr_xg = (PyArrayObject *) myArray_ContiguousFromObject( PySequence_Fast_GET_ITEM( input, 0 ),
NPY_PLFLT, 1, 1 );
pltr_yg = (PyArrayObject *) myArray_ContiguousFromObject( PySequence_Fast_GET_ITEM( input, 1 ),
NPY_PLFLT, 1, 1 );
if ( pltr_xg == 0 || pltr_yg == 0 )
{
PyErr_SetString( PyExc_ValueError, "Expected a sequence to two 1D arrays." );
return NULL;
}
tmpGrid1.nx = (PLINT) PyArray_DIMS( pltr_xg )[0];
tmpGrid1.ny = (PLINT) PyArray_DIMS( pltr_yg )[0];
if ( isimg == 0 )
{
if ( Xlen != tmpGrid1.nx || Ylen != tmpGrid1.ny )
{
PyErr_SetString( PyExc_ValueError, "pltr arguments must have X and Y dimensions of first arg." );
return NULL;
}
}
else
{
if ( Xlen != tmpGrid1.nx - 1 || Ylen != tmpGrid1.ny - 1 )
{
PyErr_SetString( PyExc_ValueError, "pltr arguments must have X and Y dimensions of first arg + 1." );
return NULL;
}
}
tmpGrid1.xg = (PLFLT *) PyArray_DATA( pltr_xg );
tmpGrid1.yg = (PLFLT *) PyArray_DATA( pltr_yg );
return &tmpGrid1;
}
void cleanup_PLcGrid1( void )
{
// fprintf(stderr, "cleanup PLcGrid1\n");
Py_CLEAR( pltr_xg );
Py_CLEAR( pltr_yg );
}
PLcGrid2* marshal_PLcGrid2( PyObject* input, int isimg )
{
int i, size;
// fprintf(stderr, "marshal PLcGrid2\n");
if ( !PySequence_Check( input ) || PySequence_Size( input ) != 2 )
{
PyErr_SetString( PyExc_ValueError, "Expected a sequence of two arrays." );
return NULL;
}
pltr_xg = (PyArrayObject *) myArray_ContiguousFromObject( PySequence_Fast_GET_ITEM( input, 0 ),
NPY_PLFLT, 2, 2 );
pltr_yg = (PyArrayObject *) myArray_ContiguousFromObject( PySequence_Fast_GET_ITEM( input, 1 ),
NPY_PLFLT, 2, 2 );
if ( pltr_xg == 0 || pltr_yg == 0 )
{
PyErr_SetString( PyExc_ValueError, "Expected a sequence of two 2D arrays." );
return NULL;
}
if ( PyArray_DIMS( pltr_xg )[0] != PyArray_DIMS( pltr_yg )[0] ||
PyArray_DIMS( pltr_xg )[1] != PyArray_DIMS( pltr_yg )[1] )
{
PyErr_SetString( PyExc_ValueError, "Arrays must be same size." );
return NULL;
}
tmpGrid2.nx = (PLINT) PyArray_DIMS( pltr_xg )[0];
tmpGrid2.ny = (PLINT) PyArray_DIMS( pltr_xg )[1];
if ( isimg == 0 )
{
if ( Xlen != tmpGrid2.nx || Ylen != tmpGrid2.ny )
{
PyErr_SetString( PyExc_ValueError, "pltr arguments must have X and Y dimensions of first arg." );
return NULL;
}
}
else
{
if ( Xlen != tmpGrid2.nx - 1 || Ylen != tmpGrid2.ny - 1 )
{
PyErr_SetString( PyExc_ValueError, "pltr arguments must have X and Y dimensions of first arg + 1." );
return NULL;
}
}
size = tmpGrid2.ny;
tmpGrid2.xg = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) tmpGrid2.nx );
for ( i = 0; i < tmpGrid2.nx; i++ )
tmpGrid2.xg[i] = ( (PLFLT *) PyArray_DATA( pltr_xg ) + i * size );
tmpGrid2.yg = (PLFLT **) malloc( sizeof ( PLFLT* ) * (size_t) tmpGrid2.nx );
for ( i = 0; i < tmpGrid2.nx; i++ )
tmpGrid2.yg[i] = ( (PLFLT *) PyArray_DATA( pltr_yg ) + i * size );
return &tmpGrid2;
}
void cleanup_PLcGrid2( void )
{
// fprintf(stderr, "cleanup PLcGrid2\n");
free( tmpGrid2.xg );
free( tmpGrid2.yg );
Py_CLEAR( pltr_xg );
Py_CLEAR( pltr_yg );
}
%}
%typemap( in ) PLcGrid * cgrid {
$1 = marshal_PLcGrid1( $input, 0 );
if ( !$1 )
return NULL;
}
%typemap( freearg ) PLcGrid * cgrid {
cleanup_PLcGrid1();
}
// Does linear interpolation from singly dimensioned coord arrays.
void
pltr1( PLFLT x, PLFLT y, PLFLT *OUTPUT, PLFLT *OUTPUT, PLcGrid* cgrid );
// This typemap marshals a Python 2-tuple of 2D arrays into the PLcGrid2
// structure that pltr2 expects
%typemap( in ) PLcGrid2 * cgrid {
$1 = marshal_PLcGrid2( $input, 0 );
if ( !$1 )
return NULL;
}
%typemap( freearg ) PLcGrid2 * cgrid {
cleanup_PLcGrid2();
}
// Does linear interpolation from doubly dimensioned coord arrays
// (column dominant, as per normal C 2d arrays).
void
pltr2( PLFLT x, PLFLT y, PLFLT *OUTPUT, PLFLT *OUTPUT, PLcGrid2* cgrid );
typedef PLINT ( *defined_func )( PLFLT, PLFLT );
typedef void ( *fill_func )( PLINT, const PLFLT*, const PLFLT* );
typedef void ( *pltr_func )( PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer );
typedef void ( *ct_func )( PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer );
typedef void ( *mapform_func )( PLINT, PLFLT *, PLFLT* );
typedef PLFLT ( *f2eval_func )( PLINT, PLINT, PLPointer );
typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
%{
typedef PLINT ( *defined_func )( PLFLT, PLFLT );
typedef void ( *fill_func )( PLINT, const PLFLT*, const PLFLT* );
typedef void ( *pltr_func )( PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer );
typedef void ( *ct_func )( PLFLT, PLFLT, PLFLT *, PLFLT*, PLPointer );
typedef void ( *mapform_func )( PLINT, PLFLT *, PLFLT* );
typedef PLFLT ( *f2eval_func )( PLINT, PLINT, PLPointer );
typedef void ( *label_func )( PLINT, PLFLT, char *, PLINT, PLPointer );
%}
#if 0
// We need to get this PyThreadState structure initialized to use it for controlling
// threads on the callback. Probably not *really* necessary since I'm not allowing
// threads through the call
%init
%{
save_interp = PyThreadState_Get()->interp;
%}
#endif
%wrapper
%{
// helper code for handling the callback
#if 0
static PyInterpreterState *save_interp = NULL;
#endif
enum callback_type { CB_0, CB_1, CB_2, CB_Python } pltr_type;
PyObject* python_pltr = NULL;
PyObject* python_f2eval = NULL;
PyObject* python_ct = NULL;
PyObject* python_mapform = NULL;
PyObject* python_label = NULL;
#if 0
#define MY_BLOCK_THREADS { \
PyThreadState *prev_state, *new_state; \
/* need to have started a thread at some stage */ \
/* for the following to work */ \
PyEval_AcquireLock(); \
new_state = PyThreadState_New( save_interp ); \
prev_state = PyThreadState_Swap( new_state );
#define MY_UNBLOCK_THREADS \
new_state = PyThreadState_Swap( prev_state ); \
PyThreadState_Clear( new_state ); \
PyEval_ReleaseLock(); \
PyThreadState_Delete( new_state ); \
}
#else
#define MY_BLOCK_THREADS
#define MY_UNBLOCK_THREADS
#endif
// Function prototypes
void do_pltr_callback( PLFLT x, PLFLT y, PLFLT *tx, PLFLT *ty, PLPointer data );
PLFLT do_f2eval_callback( PLINT x, PLINT y, PLPointer data );
void do_label_callback( PLINT axis, PLFLT value, char *string, PLINT len, PLPointer data );
void do_ct_callback( PLFLT x, PLFLT y, PLFLT *xt, PLFLT *yt, PLPointer data );
void do_mapform_callback( PLINT n, PLFLT *x, PLFLT *y );