ad54a81f156f246cffbad27802462bf7c0807fa9
[blender.git] / source / blender / freestyle / intern / python / BPy_ContextFunctions.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file source/blender/freestyle/intern/python/BPy_ContextFunctions.cpp
22  *  \ingroup freestyle
23  */
24
25 #include "BPy_ContextFunctions.h"
26 #include "BPy_Convert.h"
27
28 #include "../stroke/ContextFunctions.h"
29
30 using namespace Freestyle;
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 ///////////////////////////////////////////////////////////////////////////////////////////
37
38 //------------------------ MODULE FUNCTIONS ----------------------------------
39
40 static char ContextFunctions_get_time_stamp___doc__[] =
41 ".. function:: get_time_stamp()\n"
42 "\n"
43 "   Returns the system time stamp.\n"
44 "\n"
45 "   :return: The system time stamp.\n"
46 "   :rtype: int\n";
47
48 static PyObject *
49 ContextFunctions_get_time_stamp(PyObject *self)
50 {
51         return PyLong_FromLong(ContextFunctions::GetTimeStampCF());
52 }
53
54 static char ContextFunctions_get_canvas_width___doc__[] =
55 ".. method:: get_canvas_width()\n"
56 "\n"
57 "   Returns the canvas width.\n"
58 "\n"
59 "   :return: The canvas width.\n"
60 "   :rtype: int\n";
61
62 static PyObject *
63 ContextFunctions_get_canvas_width(PyObject *self)
64 {
65         return PyLong_FromLong(ContextFunctions::GetCanvasWidthCF());
66 }
67
68 static char ContextFunctions_get_canvas_height___doc__[] =
69 ".. method:: get_canvas_height()\n"
70 "\n"
71 "   Returns the canvas height.\n"
72 "\n"
73 "   :return: The canvas height.\n"
74 "   :rtype: int\n";
75
76 static PyObject *
77 ContextFunctions_get_canvas_height(PyObject *self)
78 {
79         return PyLong_FromLong(ContextFunctions::GetCanvasHeightCF());
80 }
81
82 static char ContextFunctions_get_border___doc__[] =
83 ".. method:: get_border()\n"
84 "\n"
85 "   Returns the border.\n"
86 "\n"
87 "   :return: A tuple of 4 numbers (xmin, ymin, xmax, ymax).\n"
88 "   :rtype: tuple\n";
89
90 static PyObject *
91 ContextFunctions_get_border(PyObject *self)
92 {
93         BBox<Vec2i> border(ContextFunctions::GetBorderCF());
94         PyObject *v = PyTuple_New(4);
95         PyTuple_SET_ITEMS(v,
96                 PyLong_FromLong(border.getMin().x()),
97                 PyLong_FromLong(border.getMin().y()),
98                 PyLong_FromLong(border.getMax().x()),
99                 PyLong_FromLong(border.getMax().y()));
100         return v;
101 }
102
103 static char ContextFunctions_load_map___doc__[] =
104 ".. function:: load_map(file_name, map_name, num_levels=4, sigma=1.0)\n"
105 "\n"
106 "   Loads an image map for further reading.\n"
107 "\n"
108 "   :arg file_name: The name of the image file.\n"
109 "   :type file_name: str\n"
110 "   :arg map_name: The name that will be used to access this image.\n"
111 "   :type map_name: str\n"
112 "   :arg num_levels: The number of levels in the map pyramid\n"
113 "      (default = 4).  If num_levels == 0, the complete pyramid is\n"
114 "      built.\n"
115 "   :type num_levels: int\n"
116 "   :arg sigma: The sigma value of the gaussian function.\n"
117 "   :type sigma: float\n";
118
119 static PyObject *
120 ContextFunctions_load_map(PyObject *self, PyObject *args, PyObject *kwds)
121 {
122         static const char *kwlist[] = {"file_name", "map_name", "num_levels", "sigma", NULL};
123         char *fileName, *mapName;
124         unsigned nbLevels = 4;
125         float sigma = 1.0;
126
127         if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|If", (char **)kwlist, &fileName, &mapName, &nbLevels, &sigma))
128                 return NULL;
129         ContextFunctions::LoadMapCF(fileName, mapName, nbLevels, sigma);
130         Py_RETURN_NONE;
131 }
132
133 static char ContextFunctions_read_map_pixel___doc__[] =
134 ".. function:: read_map_pixel(map_name, level, x, y)\n"
135 "\n"
136 "   Reads a pixel in a user-defined map.\n"
137 "\n"
138 "   :arg map_name: The name of the map.\n"
139 "   :type map_name: str\n"
140 "   :arg level: The level of the pyramid in which we wish to read the\n"
141 "      pixel.\n"
142 "   :type level: int\n"
143 "   :arg x: The x coordinate of the pixel we wish to read.  The origin\n"
144 "      is in the lower-left corner.\n"
145 "   :type x: int\n"
146 "   :arg y: The y coordinate of the pixel we wish to read.  The origin\n"
147 "      is in the lower-left corner.\n"
148 "   :type y: int\n"
149 "   :return: The floating-point value stored for that pixel.\n"
150 "   :rtype: float\n";
151
152 static PyObject *
153 ContextFunctions_read_map_pixel(PyObject *self, PyObject *args, PyObject *kwds)
154 {
155         static const char *kwlist[] = {"map_name", "level", "x", "y", NULL};
156         char *mapName;
157         int level;
158         unsigned x, y;
159
160         if (!PyArg_ParseTupleAndKeywords(args, kwds, "siII", (char **)kwlist, &mapName, &level, &x, &y))
161                 return NULL;
162         return PyFloat_FromDouble(ContextFunctions::ReadMapPixelCF(mapName, level, x, y));
163 }
164
165 static char ContextFunctions_read_complete_view_map_pixel___doc__[] =
166 ".. function:: read_complete_view_map_pixel(level, x, y)\n"
167 "\n"
168 "   Reads a pixel in the complete view map.\n"
169 "\n"
170 "   :arg level: The level of the pyramid in which we wish to read the\n"
171 "      pixel.\n"
172 "   :type level: int\n"
173 "   :arg x: The x coordinate of the pixel we wish to read.  The origin\n"
174 "      is in the lower-left corner.\n"
175 "   :type x: int\n"
176 "   :arg y: The y coordinate of the pixel we wish to read.  The origin\n"
177 "      is in the lower-left corner.\n"
178 "   :type y: int\n"
179 "   :return: The floating-point value stored for that pixel.\n"
180 "   :rtype: float\n";
181
182 static PyObject *
183 ContextFunctions_read_complete_view_map_pixel(PyObject *self, PyObject *args, PyObject *kwds)
184 {
185         static const char *kwlist[] = {"level", "x", "y", NULL};
186         int level;
187         unsigned x, y;
188
189         if (!PyArg_ParseTupleAndKeywords(args, kwds, "iII", (char **)kwlist, &level, &x, &y))
190                 return NULL;
191         return PyFloat_FromDouble(ContextFunctions::ReadCompleteViewMapPixelCF(level, x, y));
192 }
193
194 static char ContextFunctions_read_directional_view_map_pixel___doc__[] =
195 ".. function:: read_directional_view_map_pixel(orientation, level, x, y)\n"
196 "\n"
197 "   Reads a pixel in one of the oriented view map images.\n"
198 "\n"
199 "   :arg orientation: The number telling which orientation we want to\n"
200 "      check.\n"
201 "   :type orientation: int\n"
202 "   :arg level: The level of the pyramid in which we wish to read the\n"
203 "      pixel.\n"
204 "   :type level: int\n"
205 "   :arg x: The x coordinate of the pixel we wish to read.  The origin\n"
206 "      is in the lower-left corner.\n"
207 "   :type x: int\n"
208 "   :arg y: The y coordinate of the pixel we wish to read.  The origin\n"
209 "      is in the lower-left corner.\n"
210 "   :type y: int\n"
211 "   :return: The floating-point value stored for that pixel.\n"
212 "   :rtype: float\n";
213
214 static PyObject *
215 ContextFunctions_read_directional_view_map_pixel(PyObject *self, PyObject *args, PyObject *kwds)
216 {
217         static const char *kwlist[] = {"orientation", "level", "x", "y", NULL};
218         int orientation, level;
219         unsigned x, y;
220
221         if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiII", (char **)kwlist, &orientation, &level, &x, &y))
222                 return NULL;
223         return PyFloat_FromDouble(ContextFunctions::ReadDirectionalViewMapPixelCF(orientation, level, x, y));
224 }
225
226 static char ContextFunctions_get_selected_fedge___doc__[] =
227 ".. function:: get_selected_fedge()\n"
228 "\n"
229 "   Returns the selected FEdge.\n"
230 "\n"
231 "   :return: The selected FEdge.\n"
232 "   :rtype: :class:`FEdge`\n";
233
234 static PyObject *
235 ContextFunctions_get_selected_fedge(PyObject *self)
236 {
237         FEdge *fe = ContextFunctions::GetSelectedFEdgeCF();
238         if (fe)
239                 return Any_BPy_FEdge_from_FEdge(*fe);
240         Py_RETURN_NONE;
241 }
242
243 /*-----------------------ContextFunctions module docstring-------------------------------*/
244
245 static char module_docstring[] = "The Blender Freestyle.ContextFunctions submodule\n\n";
246
247 /*-----------------------ContextFunctions module functions definitions-------------------*/
248
249 static PyMethodDef module_functions[] = {
250         {"get_time_stamp", (PyCFunction)ContextFunctions_get_time_stamp, METH_NOARGS,
251                            ContextFunctions_get_time_stamp___doc__},
252         {"get_canvas_width", (PyCFunction)ContextFunctions_get_canvas_width, METH_NOARGS,
253                              ContextFunctions_get_canvas_width___doc__},
254         {"get_canvas_height", (PyCFunction)ContextFunctions_get_canvas_height, METH_NOARGS,
255                               ContextFunctions_get_canvas_height___doc__},
256         {"get_border", (PyCFunction)ContextFunctions_get_border, METH_NOARGS,
257                        ContextFunctions_get_border___doc__},
258         {"load_map", (PyCFunction)ContextFunctions_load_map, METH_VARARGS | METH_KEYWORDS,
259                      ContextFunctions_load_map___doc__},
260         {"read_map_pixel", (PyCFunction)ContextFunctions_read_map_pixel, METH_VARARGS | METH_KEYWORDS,
261                            ContextFunctions_read_map_pixel___doc__},
262         {"read_complete_view_map_pixel", (PyCFunction)ContextFunctions_read_complete_view_map_pixel,
263                                          METH_VARARGS | METH_KEYWORDS,
264                                          ContextFunctions_read_complete_view_map_pixel___doc__},
265         {"read_directional_view_map_pixel", (PyCFunction)ContextFunctions_read_directional_view_map_pixel,
266                                             METH_VARARGS | METH_KEYWORDS,
267                                             ContextFunctions_read_directional_view_map_pixel___doc__},
268         {"get_selected_fedge", (PyCFunction)ContextFunctions_get_selected_fedge, METH_NOARGS,
269                                ContextFunctions_get_selected_fedge___doc__},
270         {NULL, NULL, 0, NULL}
271 };
272
273 /*-----------------------ContextFunctions module definition--------------------------------*/
274
275 static PyModuleDef module_definition = {
276     PyModuleDef_HEAD_INIT,
277     "Freestyle.ContextFunctions",
278     module_docstring,
279     -1,
280     module_functions
281 };
282
283 //------------------- MODULE INITIALIZATION --------------------------------
284
285 int ContextFunctions_Init(PyObject *module)
286 {
287         PyObject *m;
288
289         if (module == NULL)
290                 return -1;
291
292         m = PyModule_Create(&module_definition);
293         if (m == NULL)
294                 return -1;
295         Py_INCREF(m);
296         PyModule_AddObject(module, "ContextFunctions", m);
297
298         return 0;
299 }
300
301 ///////////////////////////////////////////////////////////////////////////////////////////
302
303 #ifdef __cplusplus
304 }
305 #endif