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 | """Wrapper around libcdd, a polytope manipulation library.""" |
| 3 | |
| 4 | __author__ = 'Austin Schuh (austin.linux@gmail.com)' |
| 5 | |
| 6 | import ctypes |
Campbell Crowley | 15e4d7e | 2015-11-21 18:12:48 -0800 | [diff] [blame] | 7 | import os |
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 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | # Load and init libcdd. libcdd is a C library that implements algorithm to |
| 11 | # manipulate half space and vertex representations of polytopes. |
| 12 | # Unfortunately, the library was compiled with C++ even though it has a lot of C |
| 13 | # code in it, so all the symbol names are mangled. Ug. |
Campbell Crowley | 15e4d7e | 2015-11-21 18:12:48 -0800 | [diff] [blame] | 14 | libcdd = None |
| 15 | for path in os.environ.get('PYTHONPATH').split(':'): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 16 | try: |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 17 | libcdd = ctypes.cdll.LoadLibrary( |
| 18 | os.path.join(path, 'third_party/cddlib/_cddlib.so')) |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 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 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 52 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 53 | # Define the input and output types for a bunch of libcdd functions. |
Austin Schuh | bf9d650 | 2015-09-11 23:59:15 -0700 | [diff] [blame] | 54 | libcdd.dd_CreateMatrix.restype = ctypes.POINTER(dd_matrixdata) |
| 55 | libcdd.ddd_get_d.argtypes = [mytype] |
| 56 | libcdd.ddd_get_d.restype = ctypes.c_double |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 57 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 58 | libcdd.dd_CopyGenerators.argtypes = [ctypes.POINTER(dd_polyhedradata)] |
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 | ] |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 65 | libcdd.dd_DDMatrix2Poly.restype = (ctypes.POINTER(dd_polyhedradata)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 66 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 67 | libcdd.dd_FreeMatrix.argtypes = [ctypes.POINTER(dd_matrixdata)] |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 68 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 69 | libcdd.dd_FreePolyhedra.argtypes = [ctypes.POINTER(dd_polyhedradata)] |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 70 | |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 71 | libcdd.ddd_set_d.argtypes = [mytype, ctypes.c_double] |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 72 | |
| 73 | # Various enums. |
| 74 | DD_INEQUALITY = 1 |
| 75 | DD_REAL = 1 |
| 76 | DD_NO_ERRORS = 17 |
| 77 | |
| 78 | |
| 79 | def dd_CreateMatrix(rows, cols): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 80 | return libcdd.dd_CreateMatrix(ctypes.c_long(rows), ctypes.c_long(cols)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def dd_set_d(mytype_address, double_value): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 84 | libcdd.ddd_set_d(mytype_address, ctypes.c_double(double_value)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def dd_CopyGenerators(polyhedraptr): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 88 | return libcdd.dd_CopyGenerators(polyhedraptr) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | def dd_get_d(mytype_address): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 92 | return libcdd.ddd_get_d(mytype_address) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def dd_FreeMatrix(matrixptr): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 96 | libcdd.dd_FreeMatrix(matrixptr) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | def dd_FreePolyhedra(polyhedraptr): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 100 | libcdd.dd_FreePolyhedra(polyhedraptr) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def dd_DDMatrix2Poly(matrixptr): |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 104 | error = ctypes.c_int() |
| 105 | polyhedraptr = libcdd.dd_DDMatrix2Poly(matrixptr, ctypes.byref(error)) |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 106 | |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 107 | # Return None on error. |
| 108 | # The error values are enums, so they aren't exposed. |
| 109 | if error.value != DD_NO_ERRORS: |
| 110 | # TODO(austin): Dump out the errors to stderr |
| 111 | #libcdd.dd_WriteErrorMessages( |
| 112 | # ctypes.pythonapi.PyFile_AsFile(ctypes.py_object(sys.stdout)), |
| 113 | # error) |
| 114 | dd_FreePolyhedra(polyhedraptr) |
| 115 | return None |
| 116 | return polyhedraptr |