4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
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.
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
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
/*****************************************
20
* Copyright (C) 2003 Bertrand 'blam' LAMY
21
*****************************************/
31
static PyObject* PyP3World_GetBox (P3_world* w) {
34
int edited = P3_FALSE;
35
GLfloat box[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
36
P3_object_get_box ((P3_any_object*) w, NULL, box, &edited);
38
p = (P3_point*) PyP3Point_Type.tp_alloc (&PyP3Point_Type, 0);
39
p->parent = (P3_coordsys*) w;
40
Py_INCREF ((PyObject*) w);
41
memcpy (p->coord, box, 3 * sizeof (GLfloat));
42
PyTuple_SET_ITEM (o, 0, (PyObject*) p);
43
p = (P3_point*) PyP3Point_Type.tp_alloc (&PyP3Point_Type, 0);
44
p->parent = (P3_coordsys*) w;
45
Py_INCREF ((PyObject*) w);
46
memcpy (p->coord, box + 3, 3 * sizeof (GLfloat));
47
PyTuple_SET_ITEM (o, 1, (PyObject*) p);
51
static PyObject* PyP3World_GetDimensions (P3_world* w) {
53
int edited = P3_FALSE;
54
GLfloat box[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
55
P3_object_get_box ((P3_any_object*) w, NULL, box, &edited);
56
tuple = PyTuple_New (3);
57
PyTuple_SET_ITEM (tuple, 0, PyFloat_FromDouble ((double) box[3] - box[0]));
58
PyTuple_SET_ITEM (tuple, 1, PyFloat_FromDouble ((double) box[4] - box[1]));
59
PyTuple_SET_ITEM (tuple, 2, PyFloat_FromDouble ((double) box[5] - box[2]));
63
static PyObject* PyP3World_SetDimensions (P3_world* w, PyObject* args) {
65
dim[0] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 0));
66
dim[1] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1));
67
dim[2] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 2));
68
P3_world_set_dimensions (w, dim);
73
static PyObject* PyP3World_GetRaypickContext (P3_world* w, PyObject* args) {
74
PyP3RaypickContext* rc;
80
/* args are: (point, radius, [raypick_context]) */
81
if (PySequence_Size (args) == 3) {
82
rc = (PyP3RaypickContext*) PySequence_Fast_GET_ITEM (args, 2);
84
rc = (PyP3RaypickContext*) PyP3RaypickContext_Type.tp_alloc (&PyP3RaypickContext_Type, 0);
85
rc->chunk = P3_chunk_new ();
87
PyP3_GetPosition (PySequence_Fast_GET_ITEM (args, 0), &coord, &csys);
88
memcpy (sphere, coord, 3 * sizeof (GLfloat));
90
o = PySequence_Fast_GET_ITEM (args, 0);
91
p = PyObject_GetAttrString (o, "x");
92
sphere[0] = (GLfloat) PyFloat_AS_DOUBLE (p);
94
p = PyObject_GetAttrString (o, "y");
95
sphere[1] = (GLfloat) PyFloat_AS_DOUBLE (p);
97
p = PyObject_GetAttrString (o, "z");
98
sphere[2] = (GLfloat) PyFloat_AS_DOUBLE (p);
100
p = PyObject_GetAttrString (o, "parent");
101
if (p != Py_None) P3_point_by_matrix (sphere, P3_coordsys_get_root_matrix ((P3_coordsys*) p));
104
sphere[3] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1));
105
if (csys != NULL) P3_point_by_matrix (sphere, P3_coordsys_get_root_matrix (csys));
106
P3_get_raypick_context ((P3_any_object*) w, NULL, sphere, sphere, rc->chunk);
107
return (PyObject*) rc;
110
static PyObject* PyP3World_BuildMesh (P3_world* w, PyObject* args) {
111
P3_xmesh_from_world ((P3_xmesh*) PySequence_Fast_GET_ITEM (args, 0), w,
112
P3_to_radians ((GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1))),
113
(int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)));
119
static PyObject* PyP3World_BuildMorph (P3_world* w) {
121
data = (P3_morph_data*) PyObject_CallMethod (P3Module, "new_morph", NULL, NULL);
122
if (PyErr_Occurred() != NULL) { PyErr_Print(); }
123
return (PyObject*) P3_morph_data_from_world (data, w);
127
static int PyP3World_Init (P3_world* a, PyObject* args, PyObject* kwds) {
132
static void PyP3World_Dealloc (P3_world* a) {
133
PyObject_GC_UnTrack ((PyObject*) a);
134
Py_XDECREF (a->shape);
135
Py_XDECREF (a->parent);
136
Py_XDECREF (a->children);
137
Py_XDECREF (a->atmosphere);
138
a->ob_type->tp_free ((PyObject*) a);
141
static int PyP3World_Traverse (P3_world* a, visitproc visit, void* arg) {
143
if (a->shape != NULL) {
144
err = visit ((PyObject*) a->shape, arg);
145
if (err) { return err; }
147
if (a->parent != NULL) {
148
err = visit ((PyObject*) a->parent, arg);
149
if (err) { return err; }
151
if (a->children != NULL) {
152
err = visit ((PyObject*) a->children, arg);
153
if (err) { return err; }
155
if (a->atmosphere != NULL) {
156
err = visit ((PyObject*) a->atmosphere, arg);
157
if (err) { return err; }
162
static int PyP3World_Clear (P3_world* a) {
163
Py_XDECREF (a->shape);
165
Py_XDECREF (a->parent);
167
Py_XDECREF (a->children);
169
Py_XDECREF (a->atmosphere);
170
a->atmosphere = NULL;
174
static PyObject* PyP3World_GetState (P3_world* a) {
175
P3_chunk* chunk = P3_chunk_new ();
177
P3_world_get_data (a, chunk);
178
tuple = PyTuple_New (5);
179
PyTuple_SET_ITEM (tuple, 0, PyString_FromStringAndSize ((char*) chunk->content, chunk->nb));
180
// HACK because previous object saved their parent
182
PyTuple_SET_ITEM (tuple, 1, Py_None);
183
if (a->shape == NULL) {
185
PyTuple_SET_ITEM (tuple, 2, Py_None);
187
Py_INCREF ((PyObject*) a->shape);
188
PyTuple_SET_ITEM (tuple, 2, (PyObject*) a->shape);
190
if (a->children == NULL) {
192
PyTuple_SET_ITEM (tuple, 3, Py_None);
194
Py_INCREF ((PyObject*) a->children);
195
PyTuple_SET_ITEM (tuple, 3, (PyObject*) a->children);
197
if (a->atmosphere == NULL) {
199
PyTuple_SET_ITEM (tuple, 4, Py_None);
201
Py_INCREF ((PyObject*) a->atmosphere);
202
PyTuple_SET_ITEM (tuple, 4, (PyObject*) a->atmosphere);
204
P3_chunk_dealloc (chunk);
208
static PyObject* PyP3World_SetState (P3_world* a, PyObject* args) {
209
P3_chunk* chunk = P3_chunk_new ();
212
o = PySequence_Fast_GET_ITEM (args, 0);
213
chunk->content = PyString_AS_STRING (o);
214
P3_world_set_data (a, chunk);
215
a->shape = (P3_any_object*) PySequence_Fast_GET_ITEM (args, 2);
216
if ((PyObject*) a->shape == Py_None) {
219
Py_INCREF ((PyObject*) a->shape);
221
a->children = (P3_children) PySequence_Fast_GET_ITEM (args, 3);
222
if ((PyObject*) a->children == Py_None) {
225
Py_INCREF ((PyObject*) a->children);
227
a->atmosphere = (P3_atmosphere*) PySequence_Fast_GET_ITEM (args, 4);
228
if ((PyObject*) a->atmosphere == Py_None) {
229
a->atmosphere = NULL;
231
Py_INCREF ((PyObject*) a->atmosphere);
233
for (i = 0; i < P3_children_size (a->children); i++) {
234
o = (PyObject*) P3_children_get (a->children, i);
235
PyObject_SetAttrString (o, "_parent", (PyObject*) a);
242
static PyMethodDef PyP3World_Methods[] = {
244
// PYP3_COORDSYS_FUNCS,
246
{ "_getstate", (PyCFunction) PyP3World_GetState, METH_NOARGS },
247
{ "_setstate", (PyCFunction) PyP3World_SetState, METH_O },
248
{ "set_dimensions", (PyCFunction) PyP3World_SetDimensions, METH_VARARGS },
249
{ "get_dimensions", (PyCFunction) PyP3World_GetDimensions, METH_NOARGS },
250
{ "get_box", (PyCFunction) PyP3World_GetBox, METH_NOARGS },
251
{ "_to_shape", (PyCFunction) PyP3World_BuildMesh, METH_VARARGS },
252
// { "_to_morph", (PyCFunction) PyP3World_BuildMorph, METH_NOARGS },
253
{ "RaypickContext", (PyCFunction) PyP3World_GetRaypickContext, METH_VARARGS },
254
{ NULL, NULL } /* sentinel */
261
PY_GET_SET_ON_OBJECT (World, P3_world*, Atmosphere, atmosphere, P3_atmosphere*)
262
PY_GET_SET_ON_OBJECT (World, P3_world*, Children, children, PyObject*)
264
static PyGetSetDef PyP3World_GetSets[] = {
265
// PYP3_CHILD_GETSETS,
266
// PYP3_SOLID_GETSETS,
267
// PYP3_COORDSYS_GETSETS,
268
// PYP3_VISIBLE_GETSETS,
270
{ "children", (getter) PyP3World_GetChildren, (setter) PyP3World_SetChildren, NULL },
271
{ "atmosphere", (getter) PyP3World_GetAtmosphere, (setter) PyP3World_SetAtmosphere, NULL },
273
// { "shape", (getter) PyP3Volume_GetShape, (setter) PyP3Volume_SetShape, NULL },
282
PyTypeObject PyP3World_Type = {
283
PyObject_HEAD_INIT(NULL)
288
(destructor) PyP3World_Dealloc,/* tp_dealloc */
295
0,/* tp_as_sequence */
296
0,/* tp_as_mapping */
300
PYP3_GENERIC_GETATTR,/* tp_getattro */
303
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,/* tp_flags */
305
(traverseproc) PyP3World_Traverse,/* tp_traverse */
306
(inquiry) PyP3World_Clear,/* tp_clear */
307
0,/* tp_richcompare */
308
0,/* tp_weaklistoffset */
311
(PyMethodDef*) &PyP3World_Methods,/* tp_methods */
313
(PyGetSetDef*) &PyP3World_GetSets,/* tp_getset */
314
&PyP3Volume_Type,/* tp_base */
318
0,/* tp_dictoffset */
319
(initproc) PyP3World_Init,/* tp_init */
320
PYP3_GENERIC_ALLOC,/* tp_alloc */
321
(newfunc) PyP3Object_New,/* tp_new */
322
PYP3_GENERIC_GC_FREE,/* tp_free */