-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_load.py
70 lines (57 loc) · 1.87 KB
/
c_load.py
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
import ctypes as ct
from numpy.ctypeslib import ndpointer
import platform
import os
import re
uname = platform.uname()[0]
match uname:
case "Linux":
library = "./libzerdax.so"
case "Windows":
# library = r".\libzerdax.dll"
print("this program doesn't work on windows yet")
exit(1)
case _:
print(f"unsuported operating system: {uname}")
exit(1)
lib = ct.CDLL(library)
nthreads = os.cpu_count()
floaty = None
with open("c_filter.c", 'r') as file:
for line in file:
match = re.match(r"\s*#define\s+USE_DOUBLE\s+(\d+)\s*", line)
if match:
use_double_value = int(match.group(1))
if use_double_value == 0:
floaty = ct.c_float
else:
floaty = ct.c_double
break
if floaty is None:
print("Error finding #define USE_DOUBLE in c_filter.c")
exit(1)
def lfilter():
function = lib.filter
function.restype = None
function.argtypes = [ndpointer(floaty, flags="C_CONTIGUOUS"),
ndpointer(floaty, flags="C_CONTIGUOUS"),
ndpointer(floaty, flags="C_CONTIGUOUS"),
ct.c_size_t,
ct.c_int]
return function
def segments_distance():
function = lib.segments_distance
function.restype = ct.c_int32
function.argtypes = [ndpointer(ct.c_int32, flags="C_CONTIGUOUS"),
ndpointer(ct.c_int32, flags="C_CONTIGUOUS")]
return function
def lines_bundle():
function = lib.lines_bundle
function.restype = ct.c_int32
function.argtypes = [ndpointer(ct.c_int32, flags="C_CONTIGUOUS"),
ndpointer(ct.c_int32, flags="C_CONTIGUOUS"),
ct.c_int32, ct.c_int32]
return function
segments_distance = segments_distance()
lfilter = lfilter()
lines_bundle = lines_bundle()