This repository was archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathshadowmap.c
More file actions
106 lines (87 loc) · 2.87 KB
/
shadowmap.c
File metadata and controls
106 lines (87 loc) · 2.87 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <numpy/arrayobject.h>
/* Helper functions adapted from
http://wiki.scipy.org/Cookbook/C_Extensions/NumPy_arrays */
static int is_floatmatrix(PyArrayObject *mat) {
if (PyArray_TYPE(mat) != NPY_DOUBLE || PyArray_NDIM(mat) != 2) {
printf("%d\n", PyArray_TYPE(mat));
PyErr_SetString(PyExc_ValueError,
"Array must be of type Float and 2 dimensional.");
return 0;
}
return 1;
}
static double **ptrvector(long n) {
double **v;
v = (double **)malloc((size_t) (n * sizeof(double)));
if (!v) {
printf("Allocation of memory for double array failed.");
exit(0);
}
return v;
}
static void free_Carrayptrs(double **v) {
free((char*) v);
}
static double **pymatrix_to_Carrayptrs(PyArrayObject *arrayin) {
double **c, *a;
int i,n,m;
npy_intp *dims = PyArray_DIMS(arrayin);
n = dims[0];
m = dims[1];
c = ptrvector(n);
a = (double *) PyArray_DATA(arrayin); /* pointer to arrayin data as double */
for (i = 0; i < n; i++) {
c[i] = a + i * m;
}
return c;
}
static PyObject *shadowmap_calculate(PyObject *self, PyObject *args) {
PyArrayObject *heightmap_arr, *shadowmap_arr;
double sun_x, sun_y, sun_z, view_alt, z_max, x, y, z;
double **shadowmap, **heightmap;
npy_intp *dims;
int i, j, lit;
if (!PyArg_ParseTuple(args, "O!ddddd", &PyArray_Type, &heightmap_arr,
&sun_x, &sun_y, &sun_z, &view_alt, &z_max)) {
return NULL;
}
if (heightmap_arr == NULL || !is_floatmatrix(heightmap_arr)) {
return NULL;
}
dims = PyArray_DIMS(heightmap_arr);
shadowmap_arr = (PyArrayObject*) PyArray_ZEROS(2, dims, NPY_DOUBLE, 0);
heightmap = pymatrix_to_Carrayptrs(heightmap_arr);
shadowmap = pymatrix_to_Carrayptrs(shadowmap_arr);
for (i = 0; i < dims[0]; i++) {
for (j = 0; j < dims[1]; j++) {
x = (double)j;
y = (double)i;
z = heightmap[i][j] + view_alt;
lit = 1;
while (x >= 0 && x < dims[0] && y >= 0 && y < dims[1] && z <= z_max) {
if (z < heightmap[(int)y][(int)x]) {
lit = 0;
break;
}
x += sun_x;
y += sun_y;
z += sun_z;
}
shadowmap[i][j] = lit;
}
}
free_Carrayptrs(heightmap);
free_Carrayptrs(shadowmap);
return PyArray_Return(shadowmap_arr);
}
static PyMethodDef ShadowMapMethods[] = {
{"calculate", shadowmap_calculate, METH_VARARGS,
"Calculate a shadowmap."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC initc_shadowmap(void) {
(void) Py_InitModule("c_shadowmap", ShadowMapMethods);
import_array(); // Must be present for NumPy. Called first after above line.
}