summaryrefslogtreecommitdiffstats
path: root/libdimension-python/Vector.c
blob: 3fdc90c01161063552e61979d622d53ad1fc042f (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
358
359
360
361
362
363
/*************************************************************************
 * Copyright (C) 2009-2011 Tavian Barnes <tavianator@tavianator.com>     *
 *                                                                       *
 * This file is part of The Dimension Python Module.                     *
 *                                                                       *
 * The Dimension Python Module is free software; you can redistribute it *
 * and/ or modify it under the terms of the GNU Lesser General Public    *
 * License as published by the Free Software Foundation; either version  *
 * 3 of the License, or (at your option) any later version.              *
 *                                                                       *
 * The Dimension Python Module 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 Lesser General Public License for more details.               *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

#include "Vector.h"

bool
dmnsn_py_Vector_args(dmnsn_vector *v, PyObject *args, PyObject *kwds)
{
  static char *kwlist[] = { "x", "y", "z", NULL };
  if (PyArg_ParseTupleAndKeywords(args, kwds, "ddd", kwlist,
                                  &v->x, &v->y, &v->z)) {
    return true;
  } else {
    if (kwds)
      return false;

    PyErr_Clear();

    dmnsn_py_Vector *vec;
    if (!PyArg_ParseTuple(args, "O!", &dmnsn_py_VectorType, &vec))
      return false;

    *v = vec->v;
    return true;
  }
}

static int
dmnsn_py_Vector_init(dmnsn_py_Vector *self, PyObject *args, PyObject *kwds)
{
  return dmnsn_py_Vector_args(&self->v, args, kwds) ? 0 : -1;
}

static PyObject *
dmnsn_py_Vector_repr(dmnsn_py_Vector *self)
{
  PyObject *x = PyFloat_FromDouble(self->v.x);
  PyObject *y = PyFloat_FromDouble(self->v.y);
  PyObject *z = PyFloat_FromDouble(self->v.z);

  if (!x || !y || !x) {
    Py_XDECREF(z);
    Py_XDECREF(y);
    Py_XDECREF(x);
    return NULL;
  }

  PyObject *repr = PyUnicode_FromFormat("dimension.Vector(%R, %R, %R)",
                                        x, y, z);
  Py_DECREF(z);
  Py_DECREF(y);
  Py_DECREF(x);
  return repr;
}

static PyObject *
dmnsn_py_Vector_str(dmnsn_py_Vector *self)
{
  PyObject *x = PyFloat_FromDouble(self->v.x);
  PyObject *y = PyFloat_FromDouble(self->v.y);
  PyObject *z = PyFloat_FromDouble(self->v.z);

  if (!x || !y || !x) {
    Py_XDECREF(z);
    Py_XDECREF(y);
    Py_XDECREF(x);
    return NULL;
  }

  PyObject *repr = PyUnicode_FromFormat("<%S, %S, %S>", x, y, z);
  Py_DECREF(z);
  Py_DECREF(y);
  Py_DECREF(x);
  return repr;
}

static PyObject *
dmnsn_py_Vector_richcompare(PyObject *lhs, PyObject *rhs, int op)
{
  if (!PyObject_TypeCheck(lhs, &dmnsn_py_VectorType)
      || !PyObject_TypeCheck(rhs, &dmnsn_py_VectorType))
  {
    PyErr_SetString(PyExc_TypeError,
                    "Vectors can only be compared with Vectors");
    return NULL;
  }

  dmnsn_py_Vector *vlhs = (dmnsn_py_Vector *)lhs;
  dmnsn_py_Vector *vrhs = (dmnsn_py_Vector *)rhs;

  bool equal =
    dmnsn_vector_norm(dmnsn_vector_sub(vlhs->v, vrhs->v)) < dmnsn_epsilon;

  PyObject *result;
  switch (op) {
  case Py_EQ:
    result = equal ? Py_True : Py_False;
    break;
  case Py_NE:
    result = !equal ? Py_True : Py_False;
    break;
  default:
    result = Py_NotImplemented;
    break;
  }

  Py_INCREF(result);
  return result;
}

static PyObject *
dmnsn_py_Vector_add(PyObject *lhs, PyObject *rhs)
{
  if (!PyObject_TypeCheck(lhs, &dmnsn_py_VectorType)
      || !PyObject_TypeCheck(rhs, &dmnsn_py_VectorType))
  {
    PyErr_SetString(PyExc_TypeError,
                    "Vectors can only be added to Vectors");
    return NULL;
  }

  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    dmnsn_py_Vector *vlhs = (dmnsn_py_Vector *)lhs;
    dmnsn_py_Vector *vrhs = (dmnsn_py_Vector *)rhs;
    ret->v = dmnsn_vector_add(vlhs->v, vrhs->v);
  }
  return (PyObject *)ret;
}

static PyObject *
dmnsn_py_Vector_sub(PyObject *lhs, PyObject *rhs)
{
  if (!PyObject_TypeCheck(lhs, &dmnsn_py_VectorType)
      || !PyObject_TypeCheck(rhs, &dmnsn_py_VectorType))
  {
    PyErr_SetString(PyExc_TypeError,
                    "Vectors can only be subtracted from Vectors");
    return NULL;
  }

  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    dmnsn_py_Vector *vlhs = (dmnsn_py_Vector *)lhs;
    dmnsn_py_Vector *vrhs = (dmnsn_py_Vector *)rhs;
    ret->v = dmnsn_vector_sub(vlhs->v, vrhs->v);
  }
  return (PyObject *)ret;
}

static PyObject *
dmnsn_py_Vector_mul(PyObject *lhs, PyObject *rhs)
{
  dmnsn_py_Vector *vec;
  double dbl;

  if (PyObject_TypeCheck(lhs, &dmnsn_py_VectorType)) {
    vec = (dmnsn_py_Vector *)lhs;
    dbl = PyFloat_AsDouble(rhs);
    if (PyErr_Occurred())
      return NULL;
  } else {
    vec = (dmnsn_py_Vector *)rhs;
    dbl = PyFloat_AsDouble(lhs);
    if (PyErr_Occurred())
      return NULL;
  }

  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    ret->v = dmnsn_vector_mul(dbl, vec->v);
  }
  return (PyObject *)ret;
}

static PyObject *
dmnsn_py_Vector_div(PyObject *lhs, PyObject *rhs)
{
  if (!PyObject_TypeCheck(lhs, &dmnsn_py_VectorType)) {
    PyErr_SetString(PyExc_TypeError,
                    "Vectors can only be divided by scalars");
    return NULL;
  }

  double drhs = PyFloat_AsDouble(rhs);
  if (PyErr_Occurred())
    return NULL;

  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    dmnsn_py_Vector *vlhs = (dmnsn_py_Vector *)lhs;
    ret->v = dmnsn_vector_div(vlhs->v, drhs);
  }
  return (PyObject *)ret;
}

static int
dmnsn_py_Vector_bool(PyObject *obj)
{
  dmnsn_py_Vector *vec = (dmnsn_py_Vector *)obj;
  return dmnsn_vector_norm(vec->v) >= dmnsn_epsilon;
}

static PyObject *
dmnsn_py_Vector_positive(PyObject *rhs)
{
  Py_INCREF(rhs);
  return rhs;
}

static PyObject *
dmnsn_py_Vector_negative(PyObject *rhs)
{
  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    dmnsn_py_Vector *vrhs = (dmnsn_py_Vector *)rhs;
    ret->v = dmnsn_vector_negate(vrhs->v);
  }
  return (PyObject *)ret;
}

PyObject *
dmnsn_py_Vector_cross(PyObject *self, PyObject *args)
{
  dmnsn_py_Vector *lhs, *rhs;
  if (!PyArg_ParseTuple(args, "O!O!",
                        &dmnsn_py_VectorType, &lhs,
                        &dmnsn_py_VectorType, &rhs))
    return NULL;

  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    ret->v = dmnsn_vector_cross(lhs->v, rhs->v);
  }
  return (PyObject *)ret;
}

PyObject *
dmnsn_py_Vector_dot(PyObject *self, PyObject *args)
{
  dmnsn_py_Vector *lhs, *rhs;
  if (!PyArg_ParseTuple(args, "O!O!",
                        &dmnsn_py_VectorType, &lhs,
                        &dmnsn_py_VectorType, &rhs))
    return NULL;

  return PyFloat_FromDouble(dmnsn_vector_dot(lhs->v, rhs->v));
}

PyObject *
dmnsn_py_Vector_proj(PyObject *self, PyObject *args)
{
  dmnsn_py_Vector *u, *d;
  if (!PyArg_ParseTuple(args, "O!O!",
                        &dmnsn_py_VectorType, &u,
                        &dmnsn_py_VectorType, &d))
    return NULL;

  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    ret->v = dmnsn_vector_proj(u->v, d->v);
  }
  return (PyObject *)ret;
}

static PyNumberMethods dmnsn_py_Vector_as_number = {
  .nb_add         = dmnsn_py_Vector_add,
  .nb_subtract    = dmnsn_py_Vector_sub,
  .nb_multiply    = dmnsn_py_Vector_mul,
  .nb_true_divide = dmnsn_py_Vector_div,
  .nb_bool        = dmnsn_py_Vector_bool,
  .nb_positive    = dmnsn_py_Vector_positive,
  .nb_negative    = dmnsn_py_Vector_negative,
};

static PyObject *
dmnsn_py_Vector_norm(dmnsn_py_Vector *self)
{
  return PyFloat_FromDouble(dmnsn_vector_norm(self->v));
}

static PyObject *
dmnsn_py_Vector_normalized(dmnsn_py_Vector *self)
{
  dmnsn_py_Vector *ret = PyObject_New(dmnsn_py_Vector, &dmnsn_py_VectorType);
  if (ret) {
    ret->v = dmnsn_vector_normalized(self->v);
  }
  return (PyObject *)ret;
}

static PyMethodDef dmnsn_py_Vector_methods[] = {
  { "norm", (PyCFunction)dmnsn_py_Vector_norm, METH_NOARGS,
    "Return the magnitude of the vector" },
  { "normalized", (PyCFunction)dmnsn_py_Vector_normalized, METH_NOARGS,
    "Return the magnitude of the vector" },
  { NULL }
};

static PyObject *
dmnsn_py_Vector_getx(dmnsn_py_Vector *self, void *closure)
{
  return PyFloat_FromDouble(self->v.x);
}

static PyObject *
dmnsn_py_Vector_gety(dmnsn_py_Vector *self, void *closure)
{
  return PyFloat_FromDouble(self->v.y);
}

static PyObject *
dmnsn_py_Vector_getz(dmnsn_py_Vector *self, void *closure)
{
  return PyFloat_FromDouble(self->v.z);
}

static PyGetSetDef dmnsn_py_Vector_getsetters[] = {
  { "x", (getter)dmnsn_py_Vector_getx, NULL, "x coordinate", NULL },
  { "y", (getter)dmnsn_py_Vector_gety, NULL, "y coordinate", NULL },
  { "z", (getter)dmnsn_py_Vector_getz, NULL, "z coordinate", NULL },
  { NULL }
};

PyTypeObject dmnsn_py_VectorType = {
  PyVarObject_HEAD_INIT(NULL, 0)
  .tp_name = "dimension.Vector",
  .tp_basicsize = sizeof(dmnsn_py_Vector),
  .tp_repr = (reprfunc)dmnsn_py_Vector_repr,
  .tp_str = (reprfunc)dmnsn_py_Vector_str,
  .tp_as_number = &dmnsn_py_Vector_as_number,
  .tp_flags = Py_TPFLAGS_DEFAULT,
  .tp_doc = "Dimension vector",
  .tp_richcompare = dmnsn_py_Vector_richcompare,
  .tp_methods = dmnsn_py_Vector_methods,
  .tp_getset = dmnsn_py_Vector_getsetters,
  .tp_init = (initproc)dmnsn_py_Vector_init,
};

bool
dmnsn_py_init_VectorType(void)
{
  dmnsn_py_VectorType.tp_new = PyType_GenericNew;
  Py_INCREF(&dmnsn_py_VectorType);
  return PyType_Ready(&dmnsn_py_VectorType) >= 0;
}