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
|
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2004-2006 Alistair Riddoch
//
// 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.
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// $Id: Py_Point3D.cpp,v 1.13 2007-07-30 18:12:51 alriddoch Exp $
#include "Py_Point3D.h"
#include "Py_Vector3D.h"
#include "Py_Object.h"
static PyObject * Point3D_mag(PyPoint3D * self)
{
return PyFloat_FromDouble(sqrt(sqrMag(self->coords)));
}
static PyObject *Point3D_unit_vector_to(PyPoint3D * self, PyPoint3D * other)
{
if (!PyPoint3D_Check(other)) {
PyErr_SetString(PyExc_TypeError, "Can get unit vector to Point3D");
return NULL;
}
PyVector3D * ret = newPyVector3D();
if (ret == NULL) {
return NULL;
}
ret->coords = (other->coords - self->coords);
ret->coords.normalize();
return (PyObject *)ret;
}
static PyObject * Point3D_distance(PyPoint3D * self, PyPoint3D * other)
{
if (!PyPoint3D_Check(other)) {
PyErr_SetString(PyExc_TypeError, "Can get distance to other Point3D");
return NULL;
}
return PyFloat_FromDouble(distance(self->coords, other->coords));
}
static PyObject * Point3D_is_valid(PyPoint3D * self)
{
PyObject * ret = self->coords.isValid() ? Py_True : Py_False;
Py_INCREF(ret);
return ret;
}
static PyMethodDef Point3D_methods[] = {
{"mag", (PyCFunction)Point3D_mag, METH_NOARGS},
{"unit_vector_to", (PyCFunction)Point3D_unit_vector_to, METH_O},
{"distance", (PyCFunction)Point3D_distance, METH_O},
{"is_valid", (PyCFunction)Point3D_is_valid, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
static void Point3D_dealloc(PyPoint3D *self)
{
self->coords.~Point3D();
self->ob_type->tp_free(self);
}
static int Point3D_print(PyPoint3D * self, FILE * fp, int)
{
// if (flags & Py_PRINT_RAW) {
// }
fprintf(fp, "(%lf %lf %lf", self->coords.x(), self->coords.y(), self->coords.z());
return 0;
}
static PyObject* Point3D_repr(PyPoint3D * self)
{
char buf[64];
::snprintf(buf, 64, "(%f, %f, %f)", self->coords.x(), self->coords.y(), self->coords.z());
return PyString_FromString(buf);
}
static PyObject * Point3D_getattr(PyPoint3D *self, char *name)
{
//if (!self->coords) {
//PyErr_SetString(PyExc_TypeError, "unset Point");
//return NULL;
//}
if (strcmp(name, "x") == 0) { return PyFloat_FromDouble(self->coords.x()); }
if (strcmp(name, "y") == 0) { return PyFloat_FromDouble(self->coords.y()); }
if (strcmp(name, "z") == 0) { return PyFloat_FromDouble(self->coords.z()); }
return Py_FindMethod(Point3D_methods, (PyObject *)self, name);
}
static int Point3D_compare(PyPoint3D * self, PyPoint3D * other)
{
if (!PyPoint3D_Check(other)) {
return -1;
}
if (self->coords == other->coords) {
return 0;
}
return 1;
}
static PyPoint3D * Point3D_num_add(PyPoint3D * self, PyVector3D*other)
{
if (!PyVector3D_Check(other)) {
PyErr_SetString(PyExc_TypeError, "Can only add Vector3D to Point3D");
return NULL;
}
PyPoint3D * ret = newPyPoint3D();
if (ret == NULL) {
return NULL;
}
ret->coords = (self->coords + other->coords);
return ret;
}
static PyObject * Point3D_num_sub(PyPoint3D * self, PyObject * other)
{
if (PyVector3D_Check(other)) {
PyVector3D * ovec = (PyVector3D *)other;
PyPoint3D * ret = newPyPoint3D();
if (ret == NULL) {
return NULL;
}
ret->coords = (self->coords - ovec->coords);
return (PyObject *)ret;
} else if (PyPoint3D_Check(other)) {
PyPoint3D * opoint = (PyPoint3D *)other;
PyVector3D * ret = newPyVector3D();
if (ret == NULL) {
return NULL;
}
ret->coords = (self->coords - opoint->coords);
return (PyObject *)ret;
} else {
PyErr_SetString(PyExc_TypeError, "Can only subtract Vector3D or Point3D from Point3D");
return NULL;
}
}
static int Point3D_num_coerce(PyObject ** self, PyObject ** other)
{
Py_INCREF(*self);
Py_INCREF(*other);
return 0;
}
static int Point3D_init(PyPoint3D * self, PyObject * args, PyObject * kwds)
{
PyObject * clist;
new (&(self->coords)) Point3D();
switch (PyTuple_Size(args)) {
case 0:
break;
case 1:
clist = PyTuple_GetItem(args, 0);
if (!PyList_Check(clist) || PyList_Size(clist) != 3) {
PyErr_SetString(PyExc_TypeError, "Point3D() from single value must a list 3 long");
return -1;
}
for(int i = 0; i < 3; i++) {
PyObject * item = PyList_GetItem(clist, i);
if (PyInt_Check(item)) {
self->coords[i] = (float)PyInt_AsLong(item);
} else if (PyFloat_Check(item)) {
self->coords[i] = PyFloat_AsDouble(item);
} else if (PyMessageElement_Check(item)) {
PyMessageElement * mitem = (PyMessageElement*)item;
if (!mitem->m_obj->isNum()) {
PyErr_SetString(PyExc_TypeError, "Point3D() must take list of floats, or ints");
return -1;
}
self->coords[i] = mitem->m_obj->asNum();
} else {
PyErr_SetString(PyExc_TypeError, "Point3D() must take list of floats, or ints");
return -1;
}
}
self->coords.setValid();
break;
case 3:
for(int i = 0; i < 3; i++) {
PyObject * item = PyTuple_GetItem(args, i);
if (PyInt_Check(item)) {
self->coords[i] = (float)PyInt_AsLong(item);
} else if (PyFloat_Check(item)) {
self->coords[i] = PyFloat_AsDouble(item);
} else {
PyErr_SetString(PyExc_TypeError, "Point3D() must take list of floats, or ints");
return -1;
}
}
self->coords.setValid();
break;
default:
PyErr_SetString(PyExc_TypeError, "Point3D must take list of floats, or ints, 3 ints or 3 floats");
return -1;
break;
}
return 0;
}
PyObject * Point3D_new(PyTypeObject * type, PyObject *, PyObject *)
{
// This looks allot like the default implementation, except we call the
// in-place constructor.
PyPoint3D * self = (PyPoint3D *)type->tp_alloc(type, 0);
if (self != NULL) {
new (&(self->coords)) Point3D();
}
return (PyObject *)self;
}
static PyNumberMethods Point3D_num = {
(binaryfunc)Point3D_num_add, /* nb_add */
(binaryfunc)Point3D_num_sub, /* nb_subtract */
0, /* nb_multiply */
0, /* nb_divide */
0, /* nb_remainder */
0, /* nb_divmod */
0, /* nb_power */
0, /* nb_negative */
0, /* nb_positive */
0, /* nb_absolute */
0, /* nb_nonzero */
0, /* nb_invert */
0, /* nb_lshift */
0, /* nb_rshift */
0, /* nb_and */
0, /* nb_xor */
0, /* nb_or */
Point3D_num_coerce, /* nb_coerce */
0, /* nb_int */
0, /* nb_long */
0, /* nb_float */
0, /* nb_oct */
0 /* nb_hex */
};
PyTypeObject PyPoint3D_Type = {
PyObject_HEAD_INIT(0)
0, // ob_size
"Point3D.Point3D", // tp_name
sizeof(PyPoint3D), // tp_basicsize
0, // tp_itemsize
// methods
(destructor)Point3D_dealloc, // tp_dealloc
(printfunc)Point3D_print, // tp_print
(getattrfunc)Point3D_getattr, // tp_getattr
0, // tp_setattr
(cmpfunc)Point3D_compare, // tp_compare
(reprfunc)Point3D_repr, // tp_repr
&Point3D_num, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
"Point3D objects", // tp_doc
0, // tp_travers
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
0, // tp_methods
0, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
(initproc)Point3D_init, // tp_init
0, // tp_alloc
Point3D_new, // tp_new
};
PyPoint3D * newPyPoint3D()
{
#if 0
PyPoint3D * self;
self = PyObject_NEW(PyPoint3D, &PyPoint3D_Type);
if (self == NULL) {
return NULL;
}
new (&(self->coords)) Point3D();
return self;
#else
return (PyPoint3D *)PyPoint3D_Type.tp_new(&PyPoint3D_Type, 0, 0);
#endif
}
|