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