brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """Wrapper around libcdd, a polytope manipulation library.""" |
| 4 | |
| 5 | __author__ = 'Austin Schuh (austin.linux@gmail.com)' |
| 6 | |
| 7 | import ctypes |
Brian Silverman | dc3748d | 2014-03-30 12:41:52 -0700 | [diff] [blame] | 8 | import sys |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
| 10 | # Wrapper around PyFile_AsFile so that we can print out the error messages. |
| 11 | # Set the arg type and return types of the function call. |
| 12 | class FILE(ctypes.Structure): |
| 13 | pass |
| 14 | |
| 15 | ctypes.pythonapi.PyFile_AsFile.argtypes = [ctypes.py_object] |
| 16 | ctypes.pythonapi.PyFile_AsFile.restype = ctypes.POINTER(FILE) |
| 17 | |
| 18 | # Load and init libcdd. libcdd is a C library that implements algorithm to |
| 19 | # manipulate half space and vertex representations of polytopes. |
| 20 | # Unfortunately, the library was compiled with C++ even though it has a lot of C |
| 21 | # code in it, so all the symbol names are mangled. Ug. |
| 22 | libcdd = ctypes.cdll.LoadLibrary('libcdd.so') |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 23 | libcdd.dd_set_global_constants() |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 24 | |
| 25 | # The variable type mytype that libcdd defines (double[1]) |
| 26 | # See http://docs.python.org/2/library/ctypes.html#arrays for the documentation |
| 27 | # explaining why ctypes.c_double * 1 => double[1] |
| 28 | # libcdd defines mytype to various things so it can essentially template its |
| 29 | # functions. What a weird library. |
| 30 | mytype = ctypes.c_double * 1 |
| 31 | |
| 32 | |
| 33 | # Forward declaration for the polyhedra data structure. |
| 34 | class dd_polyhedradata(ctypes.Structure): |
| 35 | pass |
| 36 | |
| 37 | |
| 38 | # Definition of dd_matrixdata |
| 39 | class dd_matrixdata(ctypes.Structure): |
| 40 | _fields_ = [ |
| 41 | ("rowsize", ctypes.c_long), |
| 42 | ("linset", ctypes.POINTER(ctypes.c_ulong)), |
| 43 | ("colsize", ctypes.c_long), |
| 44 | ("representation", ctypes.c_int), |
| 45 | ("numbtype", ctypes.c_int), |
| 46 | ("matrix", ctypes.POINTER(ctypes.POINTER(mytype))), |
| 47 | ("objective", ctypes.c_int), |
| 48 | ("rowvec", ctypes.POINTER(mytype)), |
| 49 | ] |
| 50 | |
| 51 | # Define the input and output types for a bunch of libcdd functions. |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 52 | libcdd.dd_CreateMatrix.restype = ctypes.POINTER(dd_matrixdata) |
| 53 | libcdd.ddd_get_d.argtypes = [mytype] |
| 54 | libcdd.ddd_get_d.restype = ctypes.c_double |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 56 | libcdd.dd_CopyGenerators.argtypes = [ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 57 | ctypes.POINTER(dd_polyhedradata) |
| 58 | ] |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 59 | libcdd.dd_CopyGenerators.restype = ctypes.POINTER(dd_matrixdata) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 60 | |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 61 | libcdd.dd_DDMatrix2Poly.argtypes = [ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 62 | ctypes.POINTER(dd_matrixdata), |
| 63 | ctypes.POINTER(ctypes.c_int) |
| 64 | ] |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 65 | libcdd.dd_DDMatrix2Poly.restype = ( |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | ctypes.POINTER(dd_polyhedradata)) |
| 67 | |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 68 | libcdd.dd_FreeMatrix.argtypes = [ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 69 | ctypes.POINTER(dd_matrixdata) |
| 70 | ] |
| 71 | |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 72 | libcdd.dd_FreePolyhedra.argtypes = [ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | ctypes.POINTER(dd_polyhedradata) |
| 74 | ] |
| 75 | |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 76 | libcdd.ddd_set_d.argtypes = [ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 77 | mytype, |
| 78 | ctypes.c_double |
| 79 | ] |
| 80 | |
| 81 | |
| 82 | # Various enums. |
| 83 | DD_INEQUALITY = 1 |
| 84 | DD_REAL = 1 |
| 85 | DD_NO_ERRORS = 17 |
| 86 | |
| 87 | |
| 88 | def dd_CreateMatrix(rows, cols): |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 89 | return libcdd.dd_CreateMatrix(ctypes.c_long(rows), ctypes.c_long(cols)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def dd_set_d(mytype_address, double_value): |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 93 | libcdd.ddd_set_d(mytype_address, ctypes.c_double(double_value)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | def dd_CopyGenerators(polyhedraptr): |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 97 | return libcdd.dd_CopyGenerators(polyhedraptr) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def dd_get_d(mytype_address): |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 101 | return libcdd.ddd_get_d(mytype_address) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def dd_FreeMatrix(matrixptr): |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 105 | libcdd.dd_FreeMatrix(matrixptr) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def dd_FreePolyhedra(polyhedraptr): |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 109 | libcdd.dd_FreePolyhedra(polyhedraptr) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def dd_DDMatrix2Poly(matrixptr): |
| 113 | error = ctypes.c_int() |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 114 | polyhedraptr = libcdd.dd_DDMatrix2Poly(matrixptr, ctypes.byref(error)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 115 | |
| 116 | # Return None on error. |
| 117 | # The error values are enums, so they aren't exposed. |
Austin Schuh | f69ef92 | 2013-10-07 23:21:12 -0700 | [diff] [blame] | 118 | if error.value != DD_NO_ERRORS: |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 119 | # Dump out the errors to stderr |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 120 | libcdd.dd_WriteErrorMessages( |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 121 | ctypes.pythonapi.PyFile_AsFile(ctypes.py_object(sys.stdout)), |
| 122 | error) |
| 123 | dd_FreePolyhedra(polyhedraptr) |
| 124 | return None |
| 125 | return polyhedraptr |