1 | // Python - C extension for finite_volumes util module. |
---|
2 | // |
---|
3 | // To compile (Python2.3): |
---|
4 | // gcc -c util_ext.c -I/usr/include/python2.3 -o util_ext.o -Wall -O |
---|
5 | // gcc -shared util_ext.o -o util_ext.so |
---|
6 | // |
---|
7 | // See the module util.py |
---|
8 | // |
---|
9 | // |
---|
10 | // Ole Nielsen, GA 2004 |
---|
11 | |
---|
12 | #include "Python.h" |
---|
13 | #include "Numeric/arrayobject.h" |
---|
14 | #include "math.h" |
---|
15 | |
---|
16 | //Shared snippets |
---|
17 | #include "util_ext.h" |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | int _point_on_line(double x, double y, |
---|
22 | double x0, double y0, |
---|
23 | double x1, double y1) { |
---|
24 | /*Determine whether a point is on a line segment |
---|
25 | |
---|
26 | Input: x, y, x0, x0, x1, y1: where |
---|
27 | point is given by x, y |
---|
28 | line is given by (x0, y0) and (x1, y1) |
---|
29 | |
---|
30 | */ |
---|
31 | |
---|
32 | double a0, a1, a_normal0, a_normal1, b0, b1, len_a, len_b; |
---|
33 | |
---|
34 | a0 = x - x0; |
---|
35 | a1 = y - y0; |
---|
36 | |
---|
37 | a_normal0 = a1; |
---|
38 | a_normal1 = -a0; |
---|
39 | |
---|
40 | b0 = x1 - x0; |
---|
41 | b1 = y1 - y0; |
---|
42 | |
---|
43 | if ( a_normal0*b0 + a_normal1*b1 == 0 ) { |
---|
44 | //Point is somewhere on the infinite extension of the line |
---|
45 | |
---|
46 | len_a = sqrt(a0*a0 + a1*a1); |
---|
47 | len_b = sqrt(b0*b0 + b1*b1); |
---|
48 | |
---|
49 | if (a0*b0 + a1*b1 >= 0 && len_a <= len_b) { |
---|
50 | return 1; |
---|
51 | } else { |
---|
52 | return 0; |
---|
53 | } |
---|
54 | } else { |
---|
55 | return 0; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | // Gateways to Python |
---|
62 | PyObject *point_on_line(PyObject *self, PyObject *args) { |
---|
63 | // |
---|
64 | // point_on_line(x, y, x0, y0, x1, y1) |
---|
65 | // |
---|
66 | |
---|
67 | double x, y, x0, y0, x1, y1; |
---|
68 | int res; |
---|
69 | PyObject *result; |
---|
70 | |
---|
71 | // Convert Python arguments to C |
---|
72 | if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1)) |
---|
73 | return NULL; |
---|
74 | |
---|
75 | |
---|
76 | // Call underlying routine |
---|
77 | res = _point_on_line(x, y, x0, y0, x1, y1); |
---|
78 | |
---|
79 | // Return values a and b |
---|
80 | result = Py_BuildValue("i", res); |
---|
81 | return result; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | PyObject *gradient(PyObject *self, PyObject *args) { |
---|
86 | // |
---|
87 | // a,b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
88 | // |
---|
89 | |
---|
90 | double x0, y0, x1, y1, x2, y2, q0, q1, q2, a, b; |
---|
91 | PyObject *result; |
---|
92 | |
---|
93 | // Convert Python arguments to C |
---|
94 | if (!PyArg_ParseTuple(args, "ddddddddd", &x0, &y0, &x1, &y1, &x2, &y2, |
---|
95 | &q0, &q1, &q2)) |
---|
96 | return NULL; |
---|
97 | |
---|
98 | |
---|
99 | // Call underlying routine |
---|
100 | _gradient(x0, y0, x1, y1, x2, y2, |
---|
101 | q0, q1, q2, &a, &b); |
---|
102 | |
---|
103 | // Return values a and b |
---|
104 | result = Py_BuildValue("dd", a, b); |
---|
105 | return result; |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | // Method table for python module |
---|
110 | static struct PyMethodDef MethodTable[] = { |
---|
111 | /* The cast of the function is necessary since PyCFunction values |
---|
112 | * only take two PyObject* parameters, and rotate() takes |
---|
113 | * three. |
---|
114 | */ |
---|
115 | |
---|
116 | //{"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"}, |
---|
117 | {"gradient", gradient, METH_VARARGS, "Print out"}, |
---|
118 | {"point_on_line", point_on_line, METH_VARARGS, "Print out"}, |
---|
119 | {NULL, NULL, 0, NULL} /* sentinel */ |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | // Module initialisation |
---|
125 | void initutil_ext(void){ |
---|
126 | Py_InitModule("util_ext", MethodTable); |
---|
127 | |
---|
128 | import_array(); //Necessary for handling of NumPY structures |
---|
129 | } |
---|
130 | |
---|