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