~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/KX_PyMath.h

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2005-11-06 12:40:03 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051106124003-3pgs7tcg5rox96xg
Tags: 2.37a-1.1
* Non-maintainer upload.
* Split out parts of 01_SConstruct_debian.dpatch again: root_build_dir
  really needs to get adjusted before the clean target runs - closes: #333958,
  see #288882 for reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * $Id: KX_PyMath.h,v 1.6 2005/03/09 19:45:59 lukep Exp $
 
3
 *
 
4
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version. The Blender
 
10
 * Foundation also sells licenses for use in proprietary software under
 
11
 * the Blender License.  See http://www.blender.org/BL/ for information
 
12
 * about this.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software Foundation,
 
21
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
22
 *
 
23
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
24
 * All rights reserved.
 
25
 *
 
26
 * The Original Code is: all of this file.
 
27
 *
 
28
 * Contributor(s): none yet.
 
29
 *
 
30
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
31
 * Initialize Python thingies.
 
32
 */
 
33
 
 
34
#ifndef __KX_PYMATH_H__
 
35
#define __KX_PYMATH_H__
 
36
 
 
37
#include "MT_Point2.h"
 
38
#include "MT_Point3.h"
 
39
#include "MT_Vector2.h"
 
40
#include "MT_Vector3.h"
 
41
#include "MT_Vector4.h"
 
42
#include "MT_Matrix3x3.h"
 
43
#include "MT_Matrix4x4.h"
 
44
 
 
45
#include "KX_Python.h"
 
46
 
 
47
inline unsigned int Size(const MT_Matrix4x4&)          { return 4; }
 
48
inline unsigned int Size(const MT_Matrix3x3&)          { return 3; }
 
49
inline unsigned int Size(const MT_Tuple2&)                { return 2; }
 
50
inline unsigned int Size(const MT_Tuple3&)                { return 3; }
 
51
inline unsigned int Size(const MT_Tuple4&)                { return 4; }
 
52
 
 
53
/**
 
54
 *  Converts the given python matrix to an MT class.
 
55
 */
 
56
template<class T>
 
57
bool PyMatTo(PyObject* pymat, T& mat)
 
58
{
 
59
        bool noerror = true;
 
60
        mat.setIdentity();
 
61
        if (PySequence_Check(pymat))
 
62
        {
 
63
                unsigned int rows = PySequence_Size(pymat);
 
64
                if (rows != Size(mat))
 
65
                        return false;
 
66
                        
 
67
                for (unsigned int y = 0; noerror && y < Size(mat); y++)
 
68
                {
 
69
                        PyObject *pyrow = PySequence_GetItem(pymat, y); /* new ref */
 
70
                        if (!PyErr_Occurred() && PySequence_Check(pyrow))
 
71
                        {
 
72
                                unsigned int cols = PySequence_Size(pyrow);
 
73
                                if (cols != Size(mat))
 
74
                                        noerror = false;
 
75
                                else
 
76
                                {
 
77
                                        for( unsigned int x = 0; x < Size(mat); x++)
 
78
                                        {
 
79
                                                PyObject *item = PySequence_GetItem(pyrow, x); /* new ref */
 
80
                                                mat[y][x] = PyFloat_AsDouble(item);
 
81
                                                Py_DECREF(item);
 
82
                                        }
 
83
                                }
 
84
                        } else 
 
85
                                noerror = false;
 
86
                        Py_DECREF(pyrow);
 
87
                }
 
88
        } else 
 
89
                noerror = false;
 
90
         
 
91
        return noerror;
 
92
}
 
93
 
 
94
/**
 
95
 *  Converts a python list to a MT class.
 
96
 */
 
97
template<class T>
 
98
bool PyVecTo(PyObject* pyval, T& vec)
 
99
{
 
100
        if (PySequence_Check(pyval))
 
101
        {
 
102
                unsigned int numitems = PySequence_Size(pyval);
 
103
                if (numitems != Size(vec))
 
104
                        return false;
 
105
                        
 
106
                for (unsigned int x = 0; x < numitems; x++)
 
107
                {
 
108
                        PyObject *item = PySequence_GetItem(pyval, x); /* new ref */
 
109
                        vec[x] = PyFloat_AsDouble(item);
 
110
                        Py_DECREF(item);
 
111
                }
 
112
                
 
113
                return true;
 
114
        }
 
115
        
 
116
        return false;
 
117
}
 
118
 
 
119
/**
 
120
 *  Converts a python argument to an MT class.
 
121
 *  This paramater expects arguments as passed to a python method.
 
122
 */
 
123
template<class T>
 
124
bool PyVecArgTo(PyObject* args, T& vec)
 
125
{
 
126
        PyObject* pylist;
 
127
        if (PyArg_ParseTuple(args,"O",&pylist))
 
128
                return PyVecTo(pylist, vec);
 
129
                
 
130
        return false;
 
131
}
 
132
 
 
133
/**
 
134
 * Converts an MT_Matrix4x4 to a python object.
 
135
 */
 
136
PyObject* PyObjectFrom(const MT_Matrix4x4 &mat);
 
137
 
 
138
/**
 
139
 * Converts an MT_Matrix3x3 to a python object.
 
140
 */
 
141
PyObject* PyObjectFrom(const MT_Matrix3x3 &mat);
 
142
 
 
143
/**
 
144
 * Converts an MT_Tuple2 to a python object.
 
145
 */
 
146
PyObject* PyObjectFrom(const MT_Tuple2 &vec);
 
147
 
 
148
/**
 
149
 * Converts an MT_Tuple3 to a python object
 
150
 */
 
151
PyObject* PyObjectFrom(const MT_Tuple3 &vec);
 
152
 
 
153
/**
 
154
 * Converts an MT_Tuple4 to a python object.
 
155
 */
 
156
PyObject* PyObjectFrom(const MT_Tuple4 &pos);
 
157
 
 
158
/**
 
159
 * True if the given PyObject can be converted to an MT_Matrix
 
160
 * @param rank = 3 (for MT_Matrix3x3) or 4 (for MT_Matrix4x4)
 
161
 */
 
162
bool PyObject_IsMT_Matrix(PyObject *pymat, unsigned int rank);
 
163
 
 
164
#endif