Welcome to mirror list, hosted at ThFree Co, Russian Federation.

euler.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6b72460ccd431ccf4f9c9b457fce04a3f9128ca3 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * 
 * Contributor(s): Joseph Gilbert
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

#include "euler.h"

//doc strings
char Euler_Zero_doc[] = "() - set all values in the euler to 0";
char Euler_Unique_doc[] =
	"() - sets the euler rotation a unique shortest arc rotation - tests for gimbal lock";
char Euler_ToMatrix_doc[] =
	"() - returns a rotation matrix representing the euler rotation";
char Euler_ToQuat_doc[] =
	"() - returns a quaternion representing the euler rotation";

//methods table
struct PyMethodDef Euler_methods[] = {
	{"zero", ( PyCFunction ) Euler_Zero, METH_NOARGS,
	 Euler_Zero_doc},
	{"unique", ( PyCFunction ) Euler_Unique, METH_NOARGS,
	 Euler_Unique_doc},
	{"toMatrix", ( PyCFunction ) Euler_ToMatrix, METH_NOARGS,
	 Euler_ToMatrix_doc},
	{"toQuat", ( PyCFunction ) Euler_ToQuat, METH_NOARGS,
	 Euler_ToQuat_doc},
	{NULL, NULL, 0, NULL}
};

/*****************************/
//    Euler Python Object   
/*****************************/

//euler methods
PyObject *Euler_ToQuat( EulerObject * self )
{
	float *quat;
	int x;

	for( x = 0; x < 3; x++ ) {
		self->eul[x] *= ( float ) ( Py_PI / 180 );
	}
	quat = PyMem_Malloc( 4 * sizeof( float ) );
	EulToQuat( self->eul, quat );
	for( x = 0; x < 3; x++ ) {
		self->eul[x] *= ( float ) ( 180 / Py_PI );
	}
	return ( PyObject * ) newQuaternionObject( quat );
}

PyObject *Euler_ToMatrix( EulerObject * self )
{
	float *mat;
	int x;

	for( x = 0; x < 3; x++ ) {
		self->eul[x] *= ( float ) ( Py_PI / 180 );
	}
	mat = PyMem_Malloc( 3 * 3 * sizeof( float ) );
	EulToMat3( self->eul, ( float ( * )[3] ) mat );
	for( x = 0; x < 3; x++ ) {
		self->eul[x] *= ( float ) ( 180 / Py_PI );
	}
	return ( PyObject * ) newMatrixObject( mat, 3, 3 );
}

PyObject *Euler_Unique( EulerObject * self )
{
	float heading, pitch, bank;
	float pi2 = ( float ) Py_PI * 2.0f;
	float piO2 = ( float ) Py_PI / 2.0f;
	float Opi2 = 1.0f / pi2;

	//radians
	heading = self->eul[0] * ( float ) ( Py_PI / 180 );
	pitch = self->eul[1] * ( float ) ( Py_PI / 180 );
	bank = self->eul[2] * ( float ) ( Py_PI / 180 );

	//wrap heading in +180 / -180
	pitch += ( float ) Py_PI;
	pitch -= ( float ) floor( pitch * Opi2 ) * pi2;
	pitch -= ( float ) Py_PI;


	if( pitch < -piO2 ) {
		pitch = ( float ) -Py_PI - pitch;
		heading += ( float ) Py_PI;
		bank += ( float ) Py_PI;
	} else if( pitch > piO2 ) {
		pitch = ( float ) Py_PI - pitch;
		heading += ( float ) Py_PI;
		bank += ( float ) Py_PI;
	}
	//gimbal lock test
	if( fabs( pitch ) > piO2 - 1e-4 ) {
		heading += bank;
		bank = 0.0f;
	} else {
		bank += ( float ) Py_PI;
		bank -= ( float ) ( floor( bank * Opi2 ) ) * pi2;
		bank -= ( float ) Py_PI;
	}

	heading += ( float ) Py_PI;
	heading -= ( float ) ( floor( heading * Opi2 ) ) * pi2;
	heading -= ( float ) Py_PI;

	//back to degrees
	self->eul[0] = heading * ( float ) ( 180 / Py_PI );
	self->eul[1] = pitch * ( float ) ( 180 / Py_PI );
	self->eul[2] = bank * ( float ) ( 180 / Py_PI );

	return EXPP_incr_ret( Py_None );
}

PyObject *Euler_Zero( EulerObject * self )
{
	self->eul[0] = 0.0;
	self->eul[1] = 0.0;
	self->eul[2] = 0.0;

	return EXPP_incr_ret( Py_None );
}

static void Euler_dealloc( EulerObject * self )
{
	/* since we own this memory... */
	PyMem_Free( self->eul );

	PyObject_DEL( self );
}

static PyObject *Euler_getattr( EulerObject * self, char *name )
{
	if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) {
		return PyFloat_FromDouble( self->eul[name[0] - 'x'] );
	}
	return Py_FindMethod( Euler_methods, ( PyObject * ) self, name );
}

static int Euler_setattr( EulerObject * self, char *name, PyObject * e )
{
	float val;

	if( !PyArg_Parse( e, "f", &val ) )
		return EXPP_ReturnIntError( PyExc_TypeError,
					    "unable to parse float argument\n" );

	if( ELEM3( name[0], 'x', 'y', 'z' ) && name[1] == 0 ) {
		self->eul[name[0] - 'x'] = val;
		return 0;
	} else
		return -1;
}

/* Eulers Sequence methods */
static PyObject *Euler_item( EulerObject * self, int i )
{
	if( i < 0 || i >= 3 )
		return EXPP_ReturnPyObjError( PyExc_IndexError,
					      "array index out of range\n" );

	return Py_BuildValue( "f", self->eul[i] );
}

static PyObject *Euler_slice( EulerObject * self, int begin, int end )
{
	PyObject *list;
	int count;

	if( begin < 0 )
		begin = 0;
	if( end > 3 )
		end = 3;
	if( begin > end )
		begin = end;

	list = PyList_New( end - begin );

	for( count = begin; count < end; count++ ) {
		PyList_SetItem( list, count - begin,
				PyFloat_FromDouble( self->eul[count] ) );
	}
	return list;
}

static int Euler_ass_item( EulerObject * self, int i, PyObject * ob )
{
	if( i < 0 || i >= 3 )
		return EXPP_ReturnIntError( PyExc_IndexError,
					    "array assignment index out of range\n" );

	if( !PyNumber_Check( ob ) )
		return EXPP_ReturnIntError( PyExc_IndexError,
					    "Euler member must be a number\n" );

	if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) {
		return EXPP_ReturnIntError( PyExc_TypeError,
					    "int or float expected\n" );
	} else {
		self->eul[i] = ( float ) PyFloat_AsDouble( ob );
	}
	return 0;
}

static int Euler_ass_slice( EulerObject * self, int begin, int end,
			    PyObject * seq )
{
	int count, z;

	if( begin < 0 )
		begin = 0;
	if( end > 3 )
		end = 3;
	if( begin > end )
		begin = end;

	if( !PySequence_Check( seq ) )
		return EXPP_ReturnIntError( PyExc_TypeError,
					    "illegal argument type for built-in operation\n" );
	if( PySequence_Length( seq ) != ( end - begin ) )
		return EXPP_ReturnIntError( PyExc_TypeError,
					    "size mismatch in slice assignment\n" );

	z = 0;
	for( count = begin; count < end; count++ ) {
		PyObject *ob = PySequence_GetItem( seq, z );
		z++;

		if( !PyFloat_Check( ob ) && !PyInt_Check( ob ) ) {
			Py_DECREF( ob );
			return -1;
		} else {
			if( !PyArg_Parse( ob, "f", &self->eul[count] ) ) {
				Py_DECREF( ob );
				return -1;
			}
		}
	}
	return 0;
}

static PyObject *Euler_repr( EulerObject * self )
{
	int i, maxindex = 3 - 1;
	char ftoa[24];
	PyObject *str1, *str2;

	str1 = PyString_FromString( "[" );

	for( i = 0; i < maxindex; i++ ) {
		sprintf( ftoa, "%.4f, ", self->eul[i] );
		str2 = PyString_FromString( ftoa );
		if( !str1 || !str2 )
			goto error;
		PyString_ConcatAndDel( &str1, str2 );
	}

	sprintf( ftoa, "%.4f]\n", self->eul[maxindex] );
	str2 = PyString_FromString( ftoa );
	if( !str1 || !str2 )
		goto error;
	PyString_ConcatAndDel( &str1, str2 );

	if( str1 )
		return str1;

      error:
	Py_XDECREF( str1 );
	Py_XDECREF( str2 );
	return EXPP_ReturnPyObjError( PyExc_MemoryError,
				      "couldn't create PyString!\n" );
}

static PySequenceMethods Euler_SeqMethods = {
	( inquiry ) 0,		/* sq_length */
	( binaryfunc ) 0,	/* sq_concat */
	( intargfunc ) 0,	/* sq_repeat */
	( intargfunc ) Euler_item,	/* sq_item */
	( intintargfunc ) Euler_slice,	/* sq_slice */
	( intobjargproc ) Euler_ass_item,	/* sq_ass_item */
	( intintobjargproc ) Euler_ass_slice,	/* sq_ass_slice */
};

PyTypeObject euler_Type = {
	PyObject_HEAD_INIT( NULL ) 
	0,	/*ob_size */
	"euler",		/*tp_name */
	sizeof( EulerObject ),	/*tp_basicsize */
	0,			/*tp_itemsize */
	( destructor ) Euler_dealloc,	/*tp_dealloc */
	( printfunc ) 0,	/*tp_print */
	( getattrfunc ) Euler_getattr,	/*tp_getattr */
	( setattrfunc ) Euler_setattr,	/*tp_setattr */
	0,			/*tp_compare */
	( reprfunc ) Euler_repr,	/*tp_repr */
	0,			/*tp_as_number */
	&Euler_SeqMethods,	/*tp_as_sequence */
};

PyObject *newEulerObject( float *eul )
{
	EulerObject *self;
	int x;

	euler_Type.ob_type = &PyType_Type;

	self = PyObject_NEW( EulerObject, &euler_Type );

	/* 
	   we own the self->eul memory and will free it later.
	   if we received an input arg, copy to our internal array
	*/

	self->eul = PyMem_Malloc( 3 * sizeof( float ) );
	if( ! self->eul )
		return EXPP_ReturnPyObjError( PyExc_MemoryError,
					      "newEulerObject:PyMem_Malloc failed" );
	
	if( !eul ) {
		for( x = 0; x < 3; x++ ) {
			self->eul[x] = 0.0f;
		}
	} else{
		for( x = 0; x < 3; x++){
			self->eul[x] = eul[x];
		}
	}

	return ( PyObject * ) self;
}