added quat class

upped version to v0.2

--HG--
branch : py3dutil
This commit is contained in:
cecilkorik 2008-10-26 06:29:03 +00:00
parent 1b8f548735
commit 40b2fd87f3
5 changed files with 93 additions and 16 deletions

View file

@ -1,6 +1,7 @@
#include "obarr.h" #include "obarr.h"
/*#include "cgrid.h"*/ /*#include "cgrid.h"*/
#include "vect.h" #include "vect.h"
#include "quat.h"
static PyMethodDef ModMethods[] = { static PyMethodDef ModMethods[] = {
@ -13,14 +14,17 @@ initpy3dutil(void)
PyObject* m; PyObject* m;
ObarrObjectType.tp_new = PyType_GenericNew; ObarrObjectType.tp_new = PyType_GenericNew;
/*CgridObjectType.tp_new = PyType_GenericNew;*/
VectObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&ObarrObjectType) < 0) if (PyType_Ready(&ObarrObjectType) < 0)
return; return;
/*if (PyType_Ready(&CgridObjectType) < 0) /*CgridObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&CgridObjectType) < 0)
return;*/ return;*/
VectObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&VectObjectType) < 0) if (PyType_Ready(&VectObjectType) < 0)
return; return;
QuatObjectType.tp_new = PyType_GenericNew;
if (PyType_Ready(&QuatObjectType) < 0)
return;
(void) Py_InitModule("py3dutil", ModMethods); (void) Py_InitModule("py3dutil", ModMethods);
m = Py_InitModule3("py3dutil", NULL, m = Py_InitModule3("py3dutil", NULL,
@ -29,9 +33,11 @@ initpy3dutil(void)
return; return;
Py_INCREF(&ObarrObjectType); Py_INCREF(&ObarrObjectType);
/*Py_INCREF(&CgridObjectType);*/
Py_INCREF(&VectObjectType);
PyModule_AddObject(m, "obarr", (PyObject *)&ObarrObjectType); PyModule_AddObject(m, "obarr", (PyObject *)&ObarrObjectType);
/*PyModule_AddObject(m, "cgrid", (PyObject *)&CgridObjectType);*/ /*Py_INCREF(&CgridObjectType);
PyModule_AddObject(m, "cgrid", (PyObject *)&CgridObjectType);*/
Py_INCREF(&VectObjectType);
PyModule_AddObject(m, "vect", (PyObject *)&VectObjectType); PyModule_AddObject(m, "vect", (PyObject *)&VectObjectType);
Py_INCREF(&QuatObjectType);
PyModule_AddObject(m, "quat", (PyObject *)&QuatObjectType);
} }

59
quat.h Normal file
View file

@ -0,0 +1,59 @@
#include <Python.h>
#include <structmember.h>
#include "vect.h"
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
typedef struct QuatObject {
PyObject_HEAD
double elements[4];
} QuatObject;
#define Quat_Check(op) PyObject_TypeCheck(op, &QuatObjectType)
// internal functions
PyObject* quat_get_element(PyObject* self_in, long index);
void quat_multiply_internal(QuatObject* q1, QuatObject* q2, QuatObject* qr);
void quat_multiply_vect_internal(QuatObject* self, VectObject* v, VectObject* rv);
double quat_mag_internal(QuatObject* self);
double quat_mag2_internal(QuatObject* self);
void quat_normalize_internal(QuatObject* self);
void quat_get_conjugate_internal(QuatObject* self, QuatObject* rv);
// Python API functions
int Quat_init(QuatObject *self, PyObject *args, PyObject *kwds);
PyObject* Quat_getx(PyObject* self_in, void* closure);
PyObject* Quat_gety(PyObject* self_in, void* closure);
PyObject* Quat_getz(PyObject* self_in, void* closure);
PyObject* Quat_getw(PyObject* self_in, void* closure);
int Quat_set_notallowed(PyObject* self_in, PyObject* value, void* closure);
PyObject* Quat_repr(PyObject *self_in);
int Quat_true(PyObject *self_in);
PyObject* Quat_mul(PyObject *self_in, PyObject *other_in);
PyObject* Quat_ip_normalize(PyObject *self_in, PyObject *unused);
PyObject* Quat_mag(PyObject *self_in, PyObject *unused);
PyObject* Quat_mag2(PyObject *self_in, PyObject *unused);
PyObject* Quat_copy(PyObject *self_in, PyObject *unused);
PyObject* Quat_get_angle(PyObject *self_in, PyObject *unused);
PyObject* Quat_get_conjugate(PyObject *self_in, PyObject *unused);
PyObject* Quat_get_matrix(PyObject *self_in, PyObject *unused);
PyObject* Quat_slerp(PyObject *self_in, PyObject *args);
PyObject* Quat_slerp_turn(PyObject *self_in, PyObject *args);
Py_ssize_t Quat_len(PyObject *self_in);
PyObject* Quat_item(PyObject *self_in, Py_ssize_t index);
extern PyNumberMethods Quat_as_number[];
extern PySequenceMethods Quat_as_seq[];
extern PyGetSetDef Quat_getset[];
extern PyMethodDef Quat_methods[];
extern struct PyMemberDef Quat_members[];
extern PyTypeObject QuatObjectType;

View file

@ -2,7 +2,7 @@ from distutils.core import setup, Extension
from cPickle import load, dump from cPickle import load, dump
import os import os
module1 = Extension('py3dutil', sources = ['py3dutil.c', 'obarr.c', 'red_black_tree.c', 'misc.c', 'vect.c']) module1 = Extension('py3dutil', sources = ['py3dutil.c', 'obarr.c', 'red_black_tree.c', 'misc.c', 'vect.c', 'quat.c'])
buildno = 0 buildno = 0
if os.path.exists('buildno'): if os.path.exists('buildno'):
@ -11,7 +11,7 @@ if os.path.exists('buildno'):
dump(buildno, open('buildno', 'wb')) dump(buildno, open('buildno', 'wb'))
setup (name = 'py3dutil', setup (name = 'py3dutil',
version = '0.1.%.4d' % (buildno,), version = '0.2.%.4d' % (buildno,),
description = 'Accelerator library for 3d games', description = 'Accelerator library for 3d games',
author = 'Bradley Lawrence', author = 'Bradley Lawrence',
author_email = 'py3dutil@iambitter.org', author_email = 'py3dutil@iambitter.org',

22
vect.c
View file

@ -375,11 +375,22 @@ PyObject* Vect_mag2(PyObject *self_in, PyObject *unused)
return PyFloat_FromDouble(d); return PyFloat_FromDouble(d);
} }
double vect_dotprod_internal(VectObject *self, VectObject *other)
{
int i;
double d = 0.0;
for (i = 0; i < VECLEN; i++)
d += self->elements[i] * other->elements[i];
if (d >= 1.0)
return 0.0;
return acos(d);
}
PyObject* Vect_dotprod(PyObject *self_in, PyObject *args) PyObject* Vect_dotprod(PyObject *self_in, PyObject *args)
{ {
VectObject *self, *other; VectObject *self, *other;
double d; double d;
long i;
if (!Vect_Check(self_in)) if (!Vect_Check(self_in))
{ {
PyErr_SetString(PyExc_TypeError, "not a vector"); PyErr_SetString(PyExc_TypeError, "not a vector");
@ -397,14 +408,9 @@ PyObject* Vect_dotprod(PyObject *self_in, PyObject *args)
return 0.0 return 0.0
return math.acos(value) * 180.0 / math.pi return math.acos(value) * 180.0 / math.pi
*/ */
d = 0.0;
for (i = 0; i < VECLEN; i++)
d += self->elements[i] * other->elements[i];
if (d >= 1.0)
return PyFloat_FromDouble(0.0);
return PyFloat_FromDouble(acos(d) * RAD2DEG); d = vect_dotprod_internal(self, other) * RAD2DEG;
return PyFloat_FromDouble(d);
} }
#define A1 self->elements[0] #define A1 self->elements[0]

6
vect.h
View file

@ -1,3 +1,6 @@
#ifndef VECT_H_INCLUDED
#define VECT_H_INCLUDED
#include <Python.h> #include <Python.h>
#include <structmember.h> #include <structmember.h>
@ -29,6 +32,7 @@ typedef struct VectObject {
// internal functions // internal functions
PyObject* vect_get_element(PyObject* self_in, long index); PyObject* vect_get_element(PyObject* self_in, long index);
double vect_dotprod_internal(VectObject *self, VectObject *other);
// Python API functions // Python API functions
int Vect_init(VectObject *self, PyObject *args, PyObject *kwds); int Vect_init(VectObject *self, PyObject *args, PyObject *kwds);
@ -75,3 +79,5 @@ extern PyGetSetDef Vect_getset[];
extern PyMethodDef Vect_methods[]; extern PyMethodDef Vect_methods[];
extern struct PyMemberDef Vect_members[]; extern struct PyMemberDef Vect_members[];
extern PyTypeObject VectObjectType; extern PyTypeObject VectObjectType;
#endif