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

vector.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e893a33dcdcccfbe4948e73cdc80c7b1206d4b3 (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
/*
 *
 * ***** 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): Willian P. Germano
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

/* This file is the old bpython opy_vector.c with minor modifications */

#include "vector.h"

/*****************************/
/*    Vector Python Object   */
/*****************************/
#define VectorObject_Check(v)	((v)->ob_type == &Vector_Type)

static void Vector_dealloc(VectorObject *self)
{
	PyMem_DEL(self);
}

static PyObject *Vector_getattr(VectorObject *self, char *name)
{
	if (self->size==3 && ELEM3(name[0], 'x', 'y', 'z') && name[1]==0)
		return PyFloat_FromDouble(self->vec[ name[0]-'x' ]);

	return EXPP_ReturnPyObjError(PyExc_AttributeError, "attribute not found");
}

static int Vector_setattr(VectorObject *self, char *name, PyObject *v)
{
	float val;
	
	if (!PyArg_Parse(v, "f", &val))
			return EXPP_ReturnIntError(PyExc_TypeError,
								"expected float argument");

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

/* Vectors Sequence methods */

static int Vector_len(VectorObject *self) 
{
	return self->size;
}

static PyObject *Vector_item(VectorObject *self, int i)
{
	if (i < 0 || i >= self->size)
			return EXPP_ReturnPyObjError (PyExc_IndexError,
											"array index out of range");

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

static PyObject *Vector_slice(VectorObject *self, int begin, int end)
{
	PyObject *list;
	int count;
	
	if (begin < 0) begin= 0;
	if (end > self->size) end= self->size;
	if (begin > end) begin= end;

	list= PyList_New(end-begin);

	for (count = begin; count < end; count++)
		PyList_SetItem(list, count-begin, PyFloat_FromDouble(self->vec[count]));

	return list;
}

static int Vector_ass_item(VectorObject *self, int i, PyObject *ob)
{
	if (i < 0 || i >= self->size)
		return EXPP_ReturnIntError(PyExc_IndexError,
										"array assignment index out of range");

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

	self->vec[i]= PyFloat_AsDouble(ob);

	return 0;
}

static int Vector_ass_slice(VectorObject *self, int begin, int end, PyObject *seq)
{
	int count;
	
	if (begin < 0) begin= 0;
	if (end > self->size) end= self->size;
	if (begin > end) begin= end;

	if (!PySequence_Check(seq))
		return EXPP_ReturnIntError(PyExc_TypeError,
										"illegal argument type for built-in operation");

	if (PySequence_Length(seq) != (end - begin))
		return EXPP_ReturnIntError(PyExc_TypeError,
										"size mismatch in slice assignment");

	for (count = begin; count < end; count++) {
		PyObject *ob = PySequence_GetItem(seq, count);
		
		if (!PyArg_Parse(ob, "f", &self->vec[count])) {
			Py_DECREF(ob);
			return -1;
		}
		
		Py_DECREF(ob);
	}

	return 0;
}

PyObject *EXPP_tuple_repr(PyObject *self, int size)
{
	PyObject *repr, *comma, *item;
	int i;

/*@	note: a value must be built because the list is decrefed!
 * otherwise we have nirvana pointers inside python.. */

	repr = PyString_FromString("(");
	if (!repr) return 0;

	item = PySequence_GetItem(self, 0); 
	PyString_ConcatAndDel(&repr, PyObject_Repr(item));
	Py_DECREF(item);

	comma = PyString_FromString(", ");

	for (i = 1; i < size; i++) {
		PyString_Concat(&repr, comma);
		item = PySequence_GetItem(self, i);
		PyString_ConcatAndDel(&repr, PyObject_Repr(item));
		Py_DECREF(item);
	}

	PyString_ConcatAndDel(&repr, PyString_FromString(")"));
	Py_DECREF(comma);

	return repr;

}

static PyObject *Vector_repr (VectorObject *self)
{
	return EXPP_tuple_repr((PyObject *) self, self->size);
}

static PySequenceMethods Vector_SeqMethods =
{
	(inquiry)			Vector_len,			          /* sq_length */
	(binaryfunc)		0,				            	/* sq_concat */
	(intargfunc)		0,					            /* sq_repeat */
	(intargfunc)		Vector_item,		        /* sq_item */
	(intintargfunc)		Vector_slice,	      	/* sq_slice */
	(intobjargproc)		Vector_ass_item,	    /* sq_ass_item */
	(intintobjargproc)	Vector_ass_slice,	  /* sq_ass_slice	*/
};

PyTypeObject Vector_Type =
{
	PyObject_HEAD_INIT(&PyType_Type)
	0,								           /*ob_size*/
	"Vector",						         /*tp_name*/
	sizeof(VectorObject),			   /*tp_basicsize*/
	0,								           /*tp_itemsize*/
	/* methods */
	(destructor)	Vector_dealloc,	/*tp_dealloc*/
	(printfunc)		0,				      /*tp_print*/
	(getattrfunc)	Vector_getattr,	/*tp_getattr*/
	(setattrfunc)	Vector_setattr,	/*tp_setattr*/
	0,								            /*tp_compare*/
	(reprfunc)		Vector_repr,	  /*tp_repr*/
	0,								            /*tp_as_number*/
	&Vector_SeqMethods,			    	/*tp_as_sequence*/
};

PyObject *newVectorObject(float *vec, int size)
{
	VectorObject *self;
	
	self= PyObject_NEW(VectorObject, &Vector_Type);
	
	self->vec= vec;
	self->size= size;
	
	return (PyObject*) self;
}