2
* $Id: point.c 14444 2008-04-16 22:40:48Z hos $
4
* ***** BEGIN GPL LICENSE BLOCK *****
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.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software Foundation,
18
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21
* All rights reserved.
23
* This is a new part of Blender.
25
* Contributor(s): Joseph Gilbert
27
* ***** END GPL LICENSE BLOCK *****
29
#include "Mathutils.h"
31
#include "BLI_blenlib.h"
32
#include "BKE_utildefines.h"
33
#include "gen_utils.h"
35
//-------------------------DOC STRINGS ---------------------------
36
char Point_Zero_doc[] = "() - set all values in the point to 0";
37
char Point_toVector_doc[] = "() - create a vector representation of this point";
38
//-----------------------METHOD DEFINITIONS ----------------------
39
struct PyMethodDef Point_methods[] = {
40
{"zero", (PyCFunction) Point_Zero, METH_NOARGS, Point_Zero_doc},
41
{"toVector", (PyCFunction) Point_toVector, METH_NOARGS, Point_toVector_doc},
44
//-----------------------------METHODS----------------------------
45
//--------------------------Vector.toPoint()----------------------
46
//create a new point object to represent this vector
47
PyObject *Point_toVector(PointObject * self)
52
for(x = 0; x < self->size; x++){
53
vec[x] = self->coord[x];
56
return newVectorObject(vec, self->size, Py_NEW);
58
//----------------------------Point.zero() ----------------------
59
//set the point data to 0,0,0
60
PyObject *Point_Zero(PointObject * self)
63
for(x = 0; x < self->size; x++) {
64
self->coord[x] = 0.0f;
66
return EXPP_incr_ret((PyObject*)self);
68
//----------------------------dealloc()(internal) ----------------
70
static void Point_dealloc(PointObject * self)
72
Py_XDECREF(self->coerced_object);
74
if(self->data.py_data){
75
PyMem_Free(self->data.py_data);
79
//----------------------------getattr()(internal) ----------------
80
//object.attribute access (get)
81
static PyObject *Point_getattr(PointObject * self, char *name)
84
return PyFloat_FromDouble(self->coord[0]);
85
}else if(STREQ(name, "y")){
86
return PyFloat_FromDouble(self->coord[1]);
87
}else if(STREQ(name, "z")){
89
return PyFloat_FromDouble(self->coord[2]);
91
return EXPP_ReturnPyObjError(PyExc_AttributeError,
92
"point.z: illegal attribute access\n");
95
if(STREQ(name, "wrapped")){
96
if(self->wrapped == Py_WRAP)
97
return EXPP_incr_ret((PyObject *)Py_True);
99
return EXPP_incr_ret((PyObject *)Py_False);
101
return Py_FindMethod(Point_methods, (PyObject *) self, name);
103
//----------------------------setattr()(internal) ----------------
104
//object.attribute access (set)
105
static int Point_setattr(PointObject * self, char *name, PyObject * v)
109
f = PyNumber_Float(v);
110
if(f == NULL) { // parsed item not a number
111
return EXPP_ReturnIntError(PyExc_TypeError,
112
"point.attribute = x: argument not a number\n");
116
self->coord[0] = (float)PyFloat_AS_DOUBLE(f);
117
}else if(STREQ(name, "y")){
118
self->coord[1] = (float)PyFloat_AS_DOUBLE(f);
119
}else if(STREQ(name, "z")){
121
self->coord[2] = (float)PyFloat_AS_DOUBLE(f);
124
return EXPP_ReturnIntError(PyExc_AttributeError,
125
"point.z = x: illegal attribute access\n");
129
return EXPP_ReturnIntError(PyExc_AttributeError,
130
"point.attribute = x: unknown attribute\n");
136
//----------------------------print object (internal)-------------
137
//print the object to screen
138
static PyObject *Point_repr(PointObject * self)
141
char buffer[48], str[1024];
143
BLI_strncpy(str,"[",1024);
144
for(i = 0; i < self->size; i++){
145
if(i < (self->size - 1)){
146
sprintf(buffer, "%.6f, ", self->coord[i]);
149
sprintf(buffer, "%.6f", self->coord[i]);
153
strcat(str, "](point)");
155
return PyString_FromString(str);
157
//---------------------SEQUENCE PROTOCOLS------------------------
158
//----------------------------len(object)------------------------
160
static int Point_len(PointObject * self)
164
//----------------------------object[]---------------------------
165
//sequence accessor (get)
166
static PyObject *Point_item(PointObject * self, int i)
168
if(i < 0 || i >= self->size)
169
return EXPP_ReturnPyObjError(PyExc_IndexError,
170
"point[attribute]: array index out of range\n");
172
return PyFloat_FromDouble( (double)self->coord[i] );
175
//----------------------------object[]-------------------------
176
//sequence accessor (set)
177
static int Point_ass_item(PointObject * self, int i, PyObject * ob)
181
f = PyNumber_Float(ob);
182
if(f == NULL) { // parsed item not a number
183
return EXPP_ReturnIntError(PyExc_TypeError,
184
"point[attribute] = x: argument not a number\n");
187
if(i < 0 || i >= self->size){
189
return EXPP_ReturnIntError(PyExc_IndexError,
190
"point[attribute] = x: array assignment index out of range\n");
192
self->coord[i] = (float)PyFloat_AS_DOUBLE(f);
196
//----------------------------object[z:y]------------------------
197
//sequence slice (get)
198
static PyObject *Point_slice(PointObject * self, int begin, int end)
200
PyObject *list = NULL;
203
CLAMP(begin, 0, self->size);
204
CLAMP(end, 0, self->size);
205
begin = MIN2(begin,end);
207
list = PyList_New(end - begin);
208
for(count = begin; count < end; count++) {
209
PyList_SetItem(list, count - begin,
210
PyFloat_FromDouble(self->coord[count]));
215
//----------------------------object[z:y]------------------------
216
//sequence slice (set)
217
static int Point_ass_slice(PointObject * self, int begin, int end,
224
CLAMP(begin, 0, self->size);
225
CLAMP(end, 0, self->size);
226
begin = MIN2(begin,end);
228
size = PySequence_Length(seq);
229
if(size != (end - begin)){
230
return EXPP_ReturnIntError(PyExc_TypeError,
231
"point[begin:end] = []: size mismatch in slice assignment\n");
234
for (i = 0; i < size; i++) {
235
v = PySequence_GetItem(seq, i);
236
if (v == NULL) { // Failed to read sequence
237
return EXPP_ReturnIntError(PyExc_RuntimeError,
238
"point[begin:end] = []: unable to read sequence\n");
240
f = PyNumber_Float(v);
241
if(f == NULL) { // parsed item not a number
243
return EXPP_ReturnIntError(PyExc_TypeError,
244
"point[begin:end] = []: sequence argument not a number\n");
247
coord[i] = (float)PyFloat_AS_DOUBLE(f);
250
//parsed well - now set in point
251
for(y = 0; y < size; y++){
252
self->coord[begin + y] = coord[y];
256
//------------------------NUMERIC PROTOCOLS----------------------
257
//------------------------obj + obj------------------------------
259
static PyObject *Point_add(PyObject * v1, PyObject * v2)
263
PointObject *coord1 = NULL, *coord2 = NULL;
264
VectorObject *vec = NULL;
266
coord1 = (PointObject*)v1;
267
coord2 = (PointObject*)v2;
269
if(!coord1->coerced_object){
270
if(coord2->coerced_object){
271
if(VectorObject_Check(coord2->coerced_object)){ //POINT + VECTOR
273
vec = (VectorObject*)coord2->coerced_object;
275
if(vec->size == size){
276
for(x = 0; x < size; x++){
277
coord[x] = coord1->coord[x] + vec->vec[x];
280
return EXPP_ReturnPyObjError(PyExc_AttributeError,
281
"Point addition: arguments are the wrong size....\n");
283
return newPointObject(coord, size, Py_NEW);
285
}else{ //POINT + POINT
287
if(coord2->size == size){
288
for(x = 0; x < size; x++) {
289
coord[x] = coord1->coord[x] + coord2->coord[x];
292
return EXPP_ReturnPyObjError(PyExc_AttributeError,
293
"Point addition: arguments are the wrong size....\n");
295
return newPointObject(coord, size, Py_NEW);
299
return EXPP_ReturnPyObjError(PyExc_AttributeError,
300
"Point addition: arguments not valid for this operation....\n");
302
//------------------------obj - obj------------------------------
304
static PyObject *Point_sub(PyObject * v1, PyObject * v2)
308
PointObject *coord1 = NULL, *coord2 = NULL;
310
coord1 = (PointObject*)v1;
311
coord2 = (PointObject*)v2;
313
if(coord1->coerced_object || coord2->coerced_object){
314
return EXPP_ReturnPyObjError(PyExc_AttributeError,
315
"Point subtraction: arguments not valid for this operation....\n");
317
if(coord1->size != coord2->size){
318
return EXPP_ReturnPyObjError(PyExc_AttributeError,
319
"Point subtraction: points must have the same dimensions for this operation\n");
323
for(x = 0; x < size; x++) {
324
coord[x] = coord1->coord[x] - coord2->coord[x];
327
//Point - Point = Vector
328
return newVectorObject(coord, size, Py_NEW);
330
//------------------------obj * obj------------------------------
332
static PyObject *Point_mul(PyObject * p1, PyObject * p2)
335
float coord[3], scalar;
336
PointObject *coord1 = NULL, *coord2 = NULL;
338
MatrixObject *mat = NULL;
339
QuaternionObject *quat = NULL;
341
coord1 = (PointObject*)p1;
342
coord2 = (PointObject*)p2;
344
if(coord1->coerced_object){
345
if (PyFloat_Check(coord1->coerced_object) ||
346
PyInt_Check(coord1->coerced_object)){ // FLOAT/INT * POINT
347
f = PyNumber_Float(coord1->coerced_object);
348
if(f == NULL) { // parsed item not a number
349
return EXPP_ReturnPyObjError(PyExc_TypeError,
350
"Point multiplication: arguments not acceptable for this operation\n");
353
scalar = (float)PyFloat_AS_DOUBLE(f);
355
for(x = 0; x < size; x++) {
356
coord[x] = coord2->coord[x] * scalar;
359
return newPointObject(coord, size, Py_NEW);
362
if(coord2->coerced_object){
363
if (PyFloat_Check(coord2->coerced_object) ||
364
PyInt_Check(coord2->coerced_object)){ // POINT * FLOAT/INT
365
f = PyNumber_Float(coord2->coerced_object);
366
if(f == NULL) { // parsed item not a number
367
return EXPP_ReturnPyObjError(PyExc_TypeError,
368
"Point multiplication: arguments not acceptable for this operation\n");
371
scalar = (float)PyFloat_AS_DOUBLE(f);
373
for(x = 0; x < size; x++) {
374
coord[x] = coord1->coord[x] * scalar;
377
return newPointObject(coord, size, Py_NEW);
378
}else if(MatrixObject_Check(coord2->coerced_object)){ //POINT * MATRIX
379
mat = (MatrixObject*)coord2->coerced_object;
380
return row_point_multiplication(coord1, mat);
381
}else if(QuaternionObject_Check(coord2->coerced_object)){ //POINT * QUATERNION
382
quat = (QuaternionObject*)coord2->coerced_object;
383
if(coord1->size != 3){
384
return EXPP_ReturnPyObjError(PyExc_TypeError,
385
"Point multiplication: only 3D point rotations (with quats) currently supported\n");
387
return quat_rotation((PyObject*)coord1, (PyObject*)quat);
392
return EXPP_ReturnPyObjError(PyExc_TypeError,
393
"Point multiplication: arguments not acceptable for this operation\n");
395
//-------------------------- -obj -------------------------------
396
//returns the negative of this object
397
static PyObject *Point_neg(PointObject *self)
402
for(x = 0; x < self->size; x++)
403
coord[x] = -self->coord[x];
405
return newPointObject(coord, self->size, Py_NEW);
408
//------------------------coerce(obj, obj)-----------------------
409
//coercion of unknown types to type PointObject for numeric protocols
410
/*Coercion() is called whenever a math operation has 2 operands that
411
it doesn't understand how to evaluate. 2+Matrix for example. We want to
412
evaluate some of these operations like: (vector * 2), however, for math
413
to proceed, the unknown operand must be cast to a type that python math will
414
understand. (e.g. in the case above case, 2 must be cast to a vector and
415
then call vector.multiply(vector, scalar_cast_as_vector)*/
416
static int Point_coerce(PyObject ** p1, PyObject ** p2)
418
if(VectorObject_Check(*p2) || PyFloat_Check(*p2) || PyInt_Check(*p2) ||
419
MatrixObject_Check(*p2) || QuaternionObject_Check(*p2)) {
420
PyObject *coerced = EXPP_incr_ret(*p2);
421
*p2 = newPointObject(NULL,3,Py_NEW);
422
((PointObject*)*p2)->coerced_object = coerced;
427
return EXPP_ReturnIntError(PyExc_TypeError,
428
"point.coerce(): unknown operand - can't coerce for numeric protocols");
430
//-----------------PROTOCOL DECLARATIONS--------------------------
431
static PySequenceMethods Point_SeqMethods = {
432
(inquiry) Point_len, /* sq_length */
433
(binaryfunc) 0, /* sq_concat */
434
(intargfunc) 0, /* sq_repeat */
435
(intargfunc) Point_item, /* sq_item */
436
(intintargfunc) Point_slice, /* sq_slice */
437
(intobjargproc) Point_ass_item, /* sq_ass_item */
438
(intintobjargproc) Point_ass_slice, /* sq_ass_slice */
440
static PyNumberMethods Point_NumMethods = {
441
(binaryfunc) Point_add, /* __add__ */
442
(binaryfunc) Point_sub, /* __sub__ */
443
(binaryfunc) Point_mul, /* __mul__ */
444
(binaryfunc) 0, /* __div__ */
445
(binaryfunc) 0, /* __mod__ */
446
(binaryfunc) 0, /* __divmod__ */
447
(ternaryfunc) 0, /* __pow__ */
448
(unaryfunc) Point_neg, /* __neg__ */
449
(unaryfunc) 0, /* __pos__ */
450
(unaryfunc) 0, /* __abs__ */
451
(inquiry) 0, /* __nonzero__ */
452
(unaryfunc) 0, /* __invert__ */
453
(binaryfunc) 0, /* __lshift__ */
454
(binaryfunc) 0, /* __rshift__ */
455
(binaryfunc) 0, /* __and__ */
456
(binaryfunc) 0, /* __xor__ */
457
(binaryfunc) 0, /* __or__ */
458
(coercion) Point_coerce, /* __coerce__ */
459
(unaryfunc) 0, /* __int__ */
460
(unaryfunc) 0, /* __long__ */
461
(unaryfunc) 0, /* __float__ */
462
(unaryfunc) 0, /* __oct__ */
463
(unaryfunc) 0, /* __hex__ */
466
//------------------PY_OBECT DEFINITION--------------------------
467
PyTypeObject point_Type = {
468
PyObject_HEAD_INIT(NULL)
470
"point", /*tp_name */
471
sizeof(PointObject), /*tp_basicsize */
473
(destructor) Point_dealloc, /*tp_dealloc */
474
(printfunc) 0, /*tp_print */
475
(getattrfunc) Point_getattr, /*tp_getattr */
476
(setattrfunc) Point_setattr, /*tp_setattr */
478
(reprfunc) Point_repr, /*tp_repr */
479
&Point_NumMethods, /*tp_as_number */
480
&Point_SeqMethods, /*tp_as_sequence */
482
//------------------------newPointObject (internal)-------------
483
//creates a new point object
484
/*pass Py_WRAP - if point is a WRAPPER for data allocated by BLENDER
485
(i.e. it was allocated elsewhere by MEM_mallocN())
486
pass Py_NEW - if point is not a WRAPPER and managed by PYTHON
487
(i.e. it must be created here with PyMEM_malloc())*/
488
PyObject *newPointObject(float *coord, int size, int type)
493
point_Type.ob_type = &PyType_Type;
494
self = PyObject_NEW(PointObject, &point_Type);
495
self->data.blend_data = NULL;
496
self->data.py_data = NULL;
497
if(size > 3 || size < 2)
500
self->coerced_object = NULL;
503
self->data.blend_data = coord;
504
self->coord = self->data.blend_data;
505
self->wrapped = Py_WRAP;
506
}else if (type == Py_NEW){
507
self->data.py_data = PyMem_Malloc(size * sizeof(float));
508
self->coord = self->data.py_data;
509
if(!coord) { //new empty
510
for(x = 0; x < size; x++){
511
self->coord[x] = 0.0f;
514
for(x = 0; x < size; x++){
515
self->coord[x] = coord[x];
518
self->wrapped = Py_NEW;
522
return (PyObject *) self;