2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pymathutils
|
2011-02-27 20:10:08 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-01-22 15:58:18 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2011-02-14 04:15:25 +00:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2024-09-24 12:56:16 +02:00
|
|
|
#include "mathutils.hh"
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2023-11-07 19:49:01 +01:00
|
|
|
#include "BLI_math_base_safe.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_matrix.h"
|
|
|
|
|
#include "BLI_math_rotation.h"
|
|
|
|
|
#include "BLI_math_vector.h"
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2012-12-08 01:16:59 +00:00
|
|
|
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "../generic/py_capi_utils.hh"
|
2017-08-20 15:44:54 +10:00
|
|
|
|
2012-12-08 01:16:59 +00:00
|
|
|
#ifndef MATH_STANDALONE
|
|
|
|
|
# include "BLI_dynstr.h"
|
|
|
|
|
#endif
|
2011-01-07 18:36:47 +00:00
|
|
|
|
2017-09-09 10:47:57 +10:00
|
|
|
/**
|
|
|
|
|
* Higher dimensions are supported, for many common operations
|
|
|
|
|
* (dealing with vector/matrix multiply or handling as 3D locations)
|
|
|
|
|
* stack memory is used with a fixed size - defined here.
|
|
|
|
|
*/
|
2011-02-05 06:14:50 +00:00
|
|
|
#define MAX_DIMENSIONS 4
|
2011-01-07 19:18:31 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/**
|
|
|
|
|
* Swizzle axes get packed into a single value that is used as a closure. Each
|
2012-03-03 20:36:09 +00:00
|
|
|
* axis uses SWIZZLE_BITS_PER_AXIS bits. The first bit (SWIZZLE_VALID_AXIS) is
|
2022-05-25 12:33:15 +10:00
|
|
|
* used as a sentinel: if it is unset, the axis is not valid.
|
|
|
|
|
*/
|
2009-06-17 20:33:34 +00:00
|
|
|
#define SWIZZLE_BITS_PER_AXIS 3
|
|
|
|
|
#define SWIZZLE_VALID_AXIS 0x4
|
|
|
|
|
#define SWIZZLE_AXIS 0x3
|
|
|
|
|
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_copy(VectorObject *self);
|
2012-03-30 11:35:58 +00:00
|
|
|
static PyObject *Vector_deepcopy(VectorObject *self, PyObject *args);
|
2022-05-25 12:33:15 +10:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Utilities
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Row vector multiplication - (Vector * Matrix)
|
|
|
|
|
* <pre>
|
|
|
|
|
* [x][y][z] * [1][4][7]
|
|
|
|
|
* [2][5][8]
|
|
|
|
|
* [3][6][9]
|
|
|
|
|
* </pre>
|
|
|
|
|
* \note vector/matrix multiplication is not commutative.
|
|
|
|
|
*/
|
2020-09-04 20:59:13 +02:00
|
|
|
static int row_vector_multiplication(float r_vec[MAX_DIMENSIONS],
|
2011-09-19 14:29:21 +00:00
|
|
|
VectorObject *vec,
|
2022-05-25 12:33:15 +10:00
|
|
|
MatrixObject *mat)
|
|
|
|
|
{
|
|
|
|
|
float vec_cpy[MAX_DIMENSIONS];
|
|
|
|
|
int row, col, z = 0, vec_num = vec->vec_num;
|
|
|
|
|
|
|
|
|
|
if (mat->row_num != vec_num) {
|
|
|
|
|
if (mat->row_num == 4 && vec_num == 3) {
|
|
|
|
|
vec_cpy[3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
|
"vector * matrix: matrix column size "
|
|
|
|
|
"and the vector size must be the same");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BaseMath_ReadCallback(vec) == -1 || BaseMath_ReadCallback(mat) == -1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(vec_cpy, vec->vec, vec_num * sizeof(float));
|
|
|
|
|
|
|
|
|
|
r_vec[3] = 1.0f;
|
|
|
|
|
/* Multiplication. */
|
|
|
|
|
for (col = 0; col < mat->col_num; col++) {
|
|
|
|
|
double dot = 0.0;
|
|
|
|
|
for (row = 0; row < mat->row_num; row++) {
|
2023-07-21 10:59:54 +10:00
|
|
|
dot += double(MATRIX_ITEM(mat, row, col) * vec_cpy[row]);
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
2023-07-21 10:59:54 +10:00
|
|
|
r_vec[z++] = float(dot);
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject *vec__apply_to_copy(PyObject *(*vec_func)(VectorObject *), VectorObject *self)
|
|
|
|
|
{
|
|
|
|
|
PyObject *ret = Vector_copy(self);
|
|
|
|
|
PyObject *ret_dummy = vec_func((VectorObject *)ret);
|
|
|
|
|
if (ret_dummy) {
|
|
|
|
|
Py_DECREF(ret_dummy);
|
2025-01-26 20:08:04 +01:00
|
|
|
return ret;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
/* error */
|
|
|
|
|
Py_DECREF(ret);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \note #BaseMath_ReadCallback must be called beforehand. */
|
|
|
|
|
static PyObject *Vector_to_tuple_ex(VectorObject *self, int ndigits)
|
|
|
|
|
{
|
|
|
|
|
PyObject *ret;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
ret = PyTuple_New(self->vec_num);
|
|
|
|
|
|
|
|
|
|
if (ndigits >= 0) {
|
|
|
|
|
for (i = 0; i < self->vec_num; i++) {
|
2023-07-21 10:59:54 +10:00
|
|
|
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round(double(self->vec[i]), ndigits)));
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (i = 0; i < self->vec_num; i++) {
|
|
|
|
|
PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->vec[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: `__new__` / `mathutils.Vector()`
|
|
|
|
|
* \{ */
|
2009-06-22 04:26:48 +00:00
|
|
|
|
2017-09-09 10:47:57 +10:00
|
|
|
/**
|
|
|
|
|
* Supports 2D, 3D, and 4D vector objects both int and float values
|
2011-02-05 06:14:50 +00:00
|
|
|
* accepted. Mixed float and int values accepted. Ints are parsed to float
|
|
|
|
|
*/
|
2012-06-26 14:49:49 +00:00
|
|
|
static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
2009-06-20 02:44:57 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
float *vec = nullptr;
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num = 3; /* default to a 3D vector */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-26 14:49:49 +00:00
|
|
|
if (kwds && PyDict_Size(kwds)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"Vector(): "
|
|
|
|
|
"takes no keyword args");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-06-26 14:49:49 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-18 08:50:06 +00:00
|
|
|
switch (PyTuple_GET_SIZE(args)) {
|
2012-03-26 06:55:09 +00:00
|
|
|
case 0:
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2012-03-26 06:55:09 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
|
"Vector(): "
|
|
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-03-26 06:55:09 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
copy_vn_fl(vec, vec_num, 0.0f);
|
2012-03-26 06:55:09 +00:00
|
|
|
break;
|
|
|
|
|
case 1:
|
2022-03-28 11:06:01 +11:00
|
|
|
if ((vec_num = mathutils_array_parse_alloc(
|
2012-03-26 06:55:09 +00:00
|
|
|
&vec, 2, PyTuple_GET_ITEM(args, 0), "mathutils.Vector()")) == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2012-03-26 06:55:09 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"mathutils.Vector(): "
|
2013-04-07 15:09:06 +00:00
|
|
|
"more than a single arg given");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-20 02:44:57 +00:00
|
|
|
}
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec_num, type);
|
2009-06-20 02:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Class Methods
|
|
|
|
|
* \{ */
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
C_Vector_Fill_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. classmethod:: Fill(size, fill=0.0, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Create a vector of length size with all values set to fill.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg size: The length of the vector to be created.\n"
|
|
|
|
|
" :type size: int\n"
|
|
|
|
|
" :arg fill: The value used to fill the vector.\n"
|
|
|
|
|
" :type fill: float\n");
|
2011-12-18 07:27:11 +00:00
|
|
|
static PyObject *C_Vector_Fill(PyObject *cls, PyObject *args)
|
|
|
|
|
{
|
|
|
|
|
float *vec;
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num;
|
2011-12-24 13:26:30 +00:00
|
|
|
float fill = 0.0f;
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "i|f:Vector.Fill", &vec_num, &fill)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2012-03-26 06:55:09 +00:00
|
|
|
"Vector.Fill(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
copy_vn_fl(vec, vec_num, fill);
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec_num, (PyTypeObject *)cls);
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
C_Vector_Range_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. classmethod:: Range(start, stop, step=1, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Create a filled with a range of values.\n"
|
|
|
|
|
"\n"
|
2025-07-20 17:18:26 +05:00
|
|
|
" This method can also be called with a single argument, "
|
|
|
|
|
"in which case the argument is interpreted as ``stop`` and ``start`` defaults to 0.\n"
|
|
|
|
|
"\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
" :arg start: The start of the range used to fill the vector.\n"
|
|
|
|
|
" :type start: int\n"
|
|
|
|
|
" :arg stop: The end of the range used to fill the vector.\n"
|
|
|
|
|
" :type stop: int\n"
|
|
|
|
|
" :arg step: The step between successive values in the vector.\n"
|
|
|
|
|
" :type step: int\n");
|
2011-12-18 07:27:11 +00:00
|
|
|
static PyObject *C_Vector_Range(PyObject *cls, PyObject *args)
|
|
|
|
|
{
|
|
|
|
|
float *vec;
|
2022-03-28 11:06:01 +11:00
|
|
|
int stop, vec_num;
|
2011-12-24 13:26:30 +00:00
|
|
|
int start = 0;
|
|
|
|
|
int step = 1;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-18 07:27:11 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "i|ii:Vector.Range", &start, &stop, &step)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-18 08:50:06 +00:00
|
|
|
switch (PyTuple_GET_SIZE(args)) {
|
2012-03-26 06:55:09 +00:00
|
|
|
case 1:
|
2022-03-28 11:06:01 +11:00
|
|
|
vec_num = start;
|
2012-03-26 06:55:09 +00:00
|
|
|
start = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
if (start >= stop) {
|
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
|
"Start value is larger "
|
|
|
|
|
"than the stop value");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-03-26 06:55:09 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
vec_num = stop - start;
|
2012-03-26 06:55:09 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (start >= stop) {
|
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
|
"Start value is larger "
|
|
|
|
|
"than the stop value");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-03-26 06:55:09 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
vec_num = (stop - start);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if ((vec_num % step) != 0) {
|
|
|
|
|
vec_num += step;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
vec_num /= step;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-26 06:55:09 +00:00
|
|
|
break;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2012-02-02 01:07:04 +00:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-02-02 01:07:04 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-12-25 11:36:26 +00:00
|
|
|
"Vector.Range(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
range_vn_fl(vec, vec_num, float(start), float(step));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec_num, (PyTypeObject *)cls);
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
C_Vector_Linspace_doc,
|
2025-08-12 22:41:59 +00:00
|
|
|
".. classmethod:: Linspace(start, stop, size, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Create a vector of the specified size which is filled with linearly spaced "
|
|
|
|
|
"values between start and stop values.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg start: The start of the range used to fill the vector.\n"
|
|
|
|
|
" :type start: int\n"
|
|
|
|
|
" :arg stop: The end of the range used to fill the vector.\n"
|
|
|
|
|
" :type stop: int\n"
|
|
|
|
|
" :arg size: The size of the vector to be created.\n"
|
|
|
|
|
" :type size: int\n");
|
2011-12-18 07:27:11 +00:00
|
|
|
static PyObject *C_Vector_Linspace(PyObject *cls, PyObject *args)
|
|
|
|
|
{
|
|
|
|
|
float *vec;
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num;
|
2011-12-18 07:27:11 +00:00
|
|
|
float start, end, step;
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "ffi:Vector.Linspace", &start, &end, &vec_num)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector.Linspace(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 10:59:54 +10:00
|
|
|
step = (end - start) / float(vec_num - 1);
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-12-25 11:36:26 +00:00
|
|
|
"Vector.Linspace(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
range_vn_fl(vec, vec_num, start, step);
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec_num, (PyTypeObject *)cls);
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2011-12-18 07:27:11 +00:00
|
|
|
C_Vector_Repeat_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. classmethod:: Repeat(vector, size, /)\n"
|
2011-12-18 07:27:11 +00:00
|
|
|
"\n"
|
|
|
|
|
" Create a vector by repeating the values in vector until the required size is reached.\n"
|
|
|
|
|
"\n"
|
2024-06-04 15:40:57 +00:00
|
|
|
" :arg vector: The vector to draw values from.\n"
|
|
|
|
|
" :type vector: :class:`mathutils.Vector`\n"
|
2011-12-18 07:27:11 +00:00
|
|
|
" :arg size: The size of the vector to be created.\n"
|
|
|
|
|
" :type size: int\n");
|
|
|
|
|
static PyObject *C_Vector_Repeat(PyObject *cls, PyObject *args)
|
|
|
|
|
{
|
|
|
|
|
float *vec;
|
2023-07-21 02:18:59 +02:00
|
|
|
float *iter_vec = nullptr;
|
2022-03-28 11:06:01 +11:00
|
|
|
int i, vec_num, value_num;
|
2011-12-18 07:27:11 +00:00
|
|
|
PyObject *value;
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "Oi:Vector.Repeat", &value, &vec_num)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector.Repeat(): invalid vec_num");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if ((value_num = mathutils_array_parse_alloc(
|
|
|
|
|
&iter_vec, 2, value, "Vector.Repeat(vector, vec_num), invalid 'vector' arg")) == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (iter_vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-12-25 11:36:26 +00:00
|
|
|
"Vector.Repeat(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2012-06-26 16:58:58 +00:00
|
|
|
PyMem_Free(iter_vec);
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-12-25 11:36:26 +00:00
|
|
|
"Vector.Repeat(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
i = 0;
|
2022-03-28 11:06:01 +11:00
|
|
|
while (i < vec_num) {
|
|
|
|
|
vec[i] = iter_vec[i % value_num];
|
2011-12-18 07:27:11 +00:00
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyMem_Free(iter_vec);
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec_num, (PyTypeObject *)cls);
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Zero
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_zero_doc,
|
|
|
|
|
".. method:: zero()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Set all values to zero.\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_zero(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_Prepare_ForWrite(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2015-02-15 11:26:31 +11:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
copy_vn_fl(self->vec, self->vec_num, 0.0f);
|
2011-01-23 08:37:34 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_WriteCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-28 18:42:41 +00:00
|
|
|
|
2011-02-05 06:14:50 +00:00
|
|
|
Py_RETURN_NONE;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Normalize
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_normalize_doc,
|
|
|
|
|
".. method:: normalize()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Normalize the vector, making the length of the vector always 1.0.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" .. warning:: Normalizing a vector where all values are zero has no effect.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" .. note:: Normalize works for vectors of all sizes,\n"
|
|
|
|
|
" however 4D Vectors w axis is left untouched.\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_normalize(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
const int vec_num = (self->vec_num == 4 ? 3 : self->vec_num);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
normalize_vn(self->vec, vec_num);
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2010-10-20 12:11:09 +00:00
|
|
|
(void)BaseMath_WriteCallback(self);
|
2015-01-30 02:12:02 +11:00
|
|
|
Py_RETURN_NONE;
|
2011-02-05 06:14:50 +00:00
|
|
|
}
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_normalized_doc,
|
|
|
|
|
".. method:: normalized()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Return a new, normalized vector.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: a normalized copy of the vector\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_normalized(VectorObject *self)
|
|
|
|
|
{
|
2020-06-22 14:51:20 +10:00
|
|
|
return vec__apply_to_copy(Vector_normalize, self);
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Resize
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_resize_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. method:: resize(size, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Resize the vector to have size number of elements.\n");
|
2011-12-18 07:27:11 +00:00
|
|
|
static PyObject *Vector_resize(VectorObject *self, PyObject *value)
|
|
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-08-14 12:26:05 +10:00
|
|
|
if (UNLIKELY(BaseMathObject_Prepare_ForResize(self, "Vector.resize()") == -1)) {
|
|
|
|
|
/* An exception has been raised. */
|
2025-08-16 06:14:19 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if ((vec_num = PyC_Long_AsI32(value)) == -1) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"Vector.resize(size): "
|
|
|
|
|
"expected size argument to be an integer");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector.resize(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
self->vec = static_cast<float *>(PyMem_Realloc(self->vec, (vec_num * sizeof(float))));
|
|
|
|
|
if (self->vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2012-03-26 06:55:09 +00:00
|
|
|
"Vector.resize(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-18 07:27:11 +00:00
|
|
|
/* If the vector has increased in length, set all new elements to 0.0f */
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num > self->vec_num) {
|
|
|
|
|
copy_vn_fl(self->vec + self->vec_num, vec_num - self->vec_num, 0.0f);
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num = vec_num;
|
2011-12-18 07:27:11 +00:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_resized_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. method:: resized(size, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Return a resized copy of the vector with size number of elements.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: a new vector\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-12-18 07:27:11 +00:00
|
|
|
static PyObject *Vector_resized(VectorObject *self, PyObject *value)
|
|
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num;
|
2011-12-18 07:27:11 +00:00
|
|
|
float *vec;
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if ((vec_num = PyLong_AsLong(value)) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector.resized(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-12-25 11:36:26 +00:00
|
|
|
"Vector.resized(): "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
copy_vn_fl(vec, vec_num, 0.0f);
|
|
|
|
|
memcpy(vec, self->vec, self->vec_num * sizeof(float));
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec_num, nullptr);
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_resize_2d_doc,
|
|
|
|
|
".. method:: resize_2d()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Resize the vector to 2D (x, y).\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_resize_2d(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2025-08-14 12:26:05 +10:00
|
|
|
if (UNLIKELY(BaseMathObject_Prepare_ForResize(self, "Vector.resize_2d()") == -1)) {
|
|
|
|
|
/* An exception has been raised. */
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
self->vec = static_cast<float *>(PyMem_Realloc(self->vec, sizeof(float[2])));
|
|
|
|
|
if (self->vec == nullptr) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-09-19 15:13:16 +00:00
|
|
|
"Vector.resize_2d(): "
|
2011-07-14 01:25:05 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num = 2;
|
2011-02-05 06:14:50 +00:00
|
|
|
Py_RETURN_NONE;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_resize_3d_doc,
|
|
|
|
|
".. method:: resize_3d()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Resize the vector to 3D (x, y, z).\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_resize_3d(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2025-08-14 12:26:05 +10:00
|
|
|
if (UNLIKELY(BaseMathObject_Prepare_ForResize(self, "Vector.resize_3d()") == -1)) {
|
|
|
|
|
/* An exception has been raised. */
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
self->vec = static_cast<float *>(PyMem_Realloc(self->vec, sizeof(float[3])));
|
|
|
|
|
if (self->vec == nullptr) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-09-19 15:13:16 +00:00
|
|
|
"Vector.resize_3d(): "
|
2011-07-14 01:25:05 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num == 2) {
|
2009-06-17 20:33:34 +00:00
|
|
|
self->vec[2] = 0.0f;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num = 3;
|
2011-02-05 06:14:50 +00:00
|
|
|
Py_RETURN_NONE;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_resize_4d_doc,
|
|
|
|
|
".. method:: resize_4d()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Resize the vector to 4D (x, y, z, w).\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_resize_4d(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2025-08-14 12:26:05 +10:00
|
|
|
if (UNLIKELY(BaseMathObject_Prepare_ForResize(self, "Vector.resize_4d()") == -1)) {
|
|
|
|
|
/* An exception has been raised. */
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
self->vec = static_cast<float *>(PyMem_Realloc(self->vec, sizeof(float[4])));
|
|
|
|
|
if (self->vec == nullptr) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2011-09-19 15:13:16 +00:00
|
|
|
"Vector.resize_4d(): "
|
2011-07-14 01:25:05 +00:00
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num == 2) {
|
2009-06-17 20:33:34 +00:00
|
|
|
self->vec[2] = 0.0f;
|
|
|
|
|
self->vec[3] = 1.0f;
|
2011-03-19 11:12:48 +00:00
|
|
|
}
|
2022-03-28 11:06:01 +11:00
|
|
|
else if (self->vec_num == 3) {
|
2009-06-17 20:33:34 +00:00
|
|
|
self->vec[3] = 1.0f;
|
|
|
|
|
}
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num = 4;
|
2011-02-05 06:14:50 +00:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
2022-05-25 12:33:15 +10:00
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: To N-dimensions
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_to_2d_doc,
|
|
|
|
|
".. method:: to_2d()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Return a 2d copy of the vector.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: a new vector\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_to_2d(VectorObject *self)
|
|
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2015-01-04 17:03:54 +11:00
|
|
|
return Vector_CreatePyObject(self->vec, 2, Py_TYPE(self));
|
2011-02-05 06:14:50 +00:00
|
|
|
}
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_to_3d_doc,
|
|
|
|
|
".. method:: to_3d()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Return a 3d copy of the vector.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: a new vector\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_to_3d(VectorObject *self)
|
|
|
|
|
{
|
2011-12-24 13:26:30 +00:00
|
|
|
float tvec[3] = {0.0f};
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2023-11-07 16:33:19 +11:00
|
|
|
memcpy(tvec, self->vec, sizeof(float) * std::min(self->vec_num, 3));
|
2015-01-04 17:03:54 +11:00
|
|
|
return Vector_CreatePyObject(tvec, 3, Py_TYPE(self));
|
2011-02-05 06:14:50 +00:00
|
|
|
}
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_to_4d_doc,
|
|
|
|
|
".. method:: to_4d()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Return a 4d copy of the vector.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: a new vector\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-05 06:14:50 +00:00
|
|
|
static PyObject *Vector_to_4d(VectorObject *self)
|
|
|
|
|
{
|
2011-12-24 13:26:30 +00:00
|
|
|
float tvec[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2023-11-07 16:33:19 +11:00
|
|
|
memcpy(tvec, self->vec, sizeof(float) * std::min(self->vec_num, 4));
|
2015-01-04 17:03:54 +11:00
|
|
|
return Vector_CreatePyObject(tvec, 4, Py_TYPE(self));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2009-11-29 22:42:33 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: To Tuple
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_to_tuple_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. method:: to_tuple(precision=-1, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
2025-07-20 17:18:26 +05:00
|
|
|
" Return this vector as a tuple with a given precision.\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" :arg precision: The number to round the value to in [-1, 21].\n"
|
|
|
|
|
" :type precision: int\n"
|
|
|
|
|
" :return: the values of the vector rounded by *precision*\n"
|
2024-12-14 15:36:02 +11:00
|
|
|
" :rtype: tuple[float, ...]\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_to_tuple(VectorObject *self, PyObject *args)
|
2010-04-25 03:34:16 +00:00
|
|
|
{
|
2025-07-27 13:43:59 +10:00
|
|
|
int ndigits = -1;
|
2010-04-25 03:34:16 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "|i:to_tuple", &ndigits)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-05-30 01:42:04 +00:00
|
|
|
|
2025-07-27 13:43:59 +10:00
|
|
|
if (ndigits > 22 || ndigits < -1) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2025-07-27 13:43:59 +10:00
|
|
|
"Vector.to_tuple(precision): "
|
|
|
|
|
"precision must be between -1 and 21");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-11-29 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-11-30 22:32:04 +00:00
|
|
|
|
2019-08-01 18:34:52 +10:00
|
|
|
return Vector_to_tuple_ex(self, ndigits);
|
2009-11-29 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: To Track Quaternion
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_to_track_quat_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. method:: to_track_quat(track='Z', up='Y', /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Return a quaternion rotation from the vector and the track and up axis.\n"
|
|
|
|
|
"\n"
|
2025-07-20 17:18:26 +05:00
|
|
|
" :arg track: Track axis string.\n"
|
|
|
|
|
" :type track: Literal['-', 'X', 'Y', 'Z', '-X', '-Y', '-Z']\n"
|
|
|
|
|
" :arg up: Up axis string.\n"
|
|
|
|
|
" :type up: Literal['X', 'Y', 'Z']\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
" :return: rotation from the vector and the track and up axis.\n"
|
|
|
|
|
" :rtype: :class:`Quaternion`\n");
|
2011-03-19 11:12:48 +00:00
|
|
|
static PyObject *Vector_to_track_quat(VectorObject *self, PyObject *args)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
|
|
|
|
float vec[3], quat[4];
|
2023-07-21 02:18:59 +02:00
|
|
|
const char *strack = nullptr;
|
|
|
|
|
const char *sup = nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
short track = 2, up = 1;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "|ss:to_track_quat", &strack, &sup)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num != 3) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2011-09-19 15:13:16 +00:00
|
|
|
"Vector.to_track_quat(): "
|
2011-07-14 01:25:05 +00:00
|
|
|
"only for 3D vectors");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
if (strack) {
|
2011-12-24 13:26:30 +00:00
|
|
|
const char *axis_err_msg = "only X, -X, Y, -Y, Z or -Z for track axis";
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
if (strlen(strack) == 2) {
|
|
|
|
|
if (strack[0] == '-') {
|
2011-12-18 08:50:06 +00:00
|
|
|
switch (strack[1]) {
|
2009-06-17 20:33:34 +00:00
|
|
|
case 'X':
|
|
|
|
|
track = 3;
|
|
|
|
|
break;
|
|
|
|
|
case 'Y':
|
|
|
|
|
track = 4;
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
track = 5;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, axis_err_msg);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, axis_err_msg);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (strlen(strack) == 1) {
|
2011-12-18 08:50:06 +00:00
|
|
|
switch (strack[0]) {
|
2012-03-16 21:39:56 +00:00
|
|
|
case '-':
|
|
|
|
|
case 'X':
|
|
|
|
|
track = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 'Y':
|
|
|
|
|
track = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
track = 2;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, axis_err_msg);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
else {
|
2011-07-14 09:54:03 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, axis_err_msg);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
if (sup) {
|
2011-12-24 13:26:30 +00:00
|
|
|
const char *axis_err_msg = "only X, Y or Z for up axis";
|
2009-06-17 20:33:34 +00:00
|
|
|
if (strlen(sup) == 1) {
|
2011-12-18 08:50:06 +00:00
|
|
|
switch (*sup) {
|
2012-03-16 21:39:56 +00:00
|
|
|
case 'X':
|
|
|
|
|
up = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 'Y':
|
|
|
|
|
up = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
up = 2;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, axis_err_msg);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
else {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, axis_err_msg);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
if (track == up) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Can't have the same axis for track and up");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-17 00:54:22 +10:00
|
|
|
/* Flip vector around, since #vec_to_quat expect a vector from target to tracking object
|
|
|
|
|
* and the python function expects the inverse (a vector to the target). */
|
2010-11-28 06:03:30 +00:00
|
|
|
negate_v3_v3(vec, self->vec);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
vec_to_quat(quat, vec, track, up);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
return Quaternion_CreatePyObject(quat, nullptr);
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Orthogonal
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2014-03-18 00:37:43 +11:00
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2014-03-18 00:37:43 +11:00
|
|
|
Vector_orthogonal_doc,
|
|
|
|
|
".. method:: orthogonal()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Return a perpendicular vector.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: a new vector 90 degrees from this vector.\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" .. note:: the axis is undefined, only use when any orthogonal vector is acceptable.\n");
|
|
|
|
|
static PyObject *Vector_orthogonal(VectorObject *self)
|
|
|
|
|
{
|
|
|
|
|
float vec[3];
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num > 3) {
|
2014-03-18 00:37:43 +11:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"Vector.orthogonal(): "
|
2014-07-23 23:08:55 +09:00
|
|
|
"Vector must be 3D or 2D");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-03-18 00:37:43 +11:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2014-03-18 00:37:43 +11:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num == 3) {
|
2014-07-23 23:08:55 +09:00
|
|
|
ortho_v3_v3(vec, self->vec);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
|
|
|
|
else {
|
2014-07-23 23:08:55 +09:00
|
|
|
ortho_v2_v2(vec, self->vec);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2014-03-18 00:37:43 +11:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject(vec, self->vec_num, Py_TYPE(self));
|
2014-03-18 00:37:43 +11:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Reflect
|
|
|
|
|
*
|
|
|
|
|
* `Vector.reflect(mirror)`: return a reflected vector on the mirror normal:
|
|
|
|
|
* `vec - ((2 * dot(vec, mirror)) * mirror)`.
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_reflect_doc,
|
2025-08-19 02:36:42 +00:00
|
|
|
".. method:: reflect(mirror, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Return the reflection vector from the *mirror* argument.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg mirror: This vector could be a normal from the reflecting surface.\n"
|
|
|
|
|
" :type mirror: :class:`Vector`\n"
|
|
|
|
|
" :return: The reflected vector matching the size of this vector.\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_reflect(VectorObject *self, PyObject *value)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
int value_num;
|
2009-09-08 03:25:00 +00:00
|
|
|
float mirror[3], vec[3];
|
2011-02-04 03:06:23 +00:00
|
|
|
float reflect[3] = {0.0f};
|
|
|
|
|
float tvec[MAX_DIMENSIONS];
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if ((value_num = mathutils_array_parse(
|
2019-03-30 06:12:48 +11:00
|
|
|
tvec, 2, 4, value, "Vector.reflect(other), invalid 'other' arg")) == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-04 03:06:23 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num < 2 || self->vec_num > 4) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Vector must be 2D, 3D or 4D");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-04 03:06:23 +00:00
|
|
|
mirror[0] = tvec[0];
|
|
|
|
|
mirror[1] = tvec[1];
|
2022-03-28 11:06:01 +11:00
|
|
|
mirror[2] = (value_num > 2) ? tvec[2] : 0.0f;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
vec[0] = self->vec[0];
|
|
|
|
|
vec[1] = self->vec[1];
|
2022-03-28 11:06:01 +11:00
|
|
|
vec[2] = (value_num > 2) ? self->vec[2] : 0.0f;
|
2011-02-04 03:06:23 +00:00
|
|
|
|
2010-01-22 11:10:24 +00:00
|
|
|
normalize_v3(mirror);
|
2009-11-10 20:43:45 +00:00
|
|
|
reflect_v3_v3v3(reflect, vec, mirror);
|
2011-02-04 03:06:23 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject(reflect, self->vec_num, Py_TYPE(self));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Cross Product
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_cross_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. method:: cross(other, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Return the cross product of this vector and another.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: The other vector to perform the cross product with.\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :return: The cross product as a vector or a float when 2D vectors are used.\n"
|
|
|
|
|
" :rtype: :class:`Vector` | float\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" .. note:: both vectors must be 2D or 3D\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_cross(VectorObject *self, PyObject *value)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2014-02-05 23:32:51 +11:00
|
|
|
PyObject *ret;
|
|
|
|
|
float tvec[3];
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-04 03:06:23 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num > 3) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Vector must be 2D or 3D");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (mathutils_array_parse(
|
2022-03-28 11:06:01 +11:00
|
|
|
tvec, self->vec_num, self->vec_num, value, "Vector.cross(other), invalid 'other' arg") ==
|
|
|
|
|
-1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2014-02-05 23:32:51 +11:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num == 3) {
|
2023-07-21 02:18:59 +02:00
|
|
|
ret = Vector_CreatePyObject(nullptr, 3, Py_TYPE(self));
|
2014-02-05 23:32:51 +11:00
|
|
|
cross_v3_v3v3(((VectorObject *)ret)->vec, self->vec, tvec);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* size == 2 */
|
|
|
|
|
ret = PyFloat_FromDouble(cross_v2v2(self->vec, tvec));
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Dot Product
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_dot_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. method:: dot(other, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Return the dot product of this vector and another.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: The other vector to perform the dot product with.\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
|
|
|
|
" :return: The dot product.\n"
|
|
|
|
|
" :rtype: float\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_dot(VectorObject *self, PyObject *value)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2011-12-18 07:27:11 +00:00
|
|
|
float *tvec;
|
2012-06-26 16:58:58 +00:00
|
|
|
PyObject *ret;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-18 07:27:11 +00:00
|
|
|
if (mathutils_array_parse_alloc(
|
2022-03-28 11:06:01 +11:00
|
|
|
&tvec, self->vec_num, value, "Vector.dot(other), invalid 'other' arg") == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
ret = PyFloat_FromDouble(dot_vn_vn(self->vec, tvec, self->vec_num));
|
2011-12-18 07:27:11 +00:00
|
|
|
PyMem_Free(tvec);
|
2012-06-26 16:58:58 +00:00
|
|
|
return ret;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Angle
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2011-05-24 16:05:51 +00:00
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2011-05-24 16:05:51 +00:00
|
|
|
Vector_angle_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: angle(other, fallback=None, /)\n"
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
"\n"
|
|
|
|
|
" Return the angle between two vectors.\n"
|
|
|
|
|
"\n"
|
2010-05-30 01:42:04 +00:00
|
|
|
" :arg other: another vector to compare the angle with\n"
|
2010-01-27 21:33:39 +00:00
|
|
|
" :type other: :class:`Vector`\n"
|
2015-03-06 18:50:58 +11:00
|
|
|
" :arg fallback: return this when the angle can't be calculated (zero length vector),\n"
|
|
|
|
|
" (instead of raising a :exc:`ValueError`).\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :type fallback: Any\n"
|
2010-08-16 12:14:09 +00:00
|
|
|
" :return: angle in radians or fallback when given\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :rtype: float | Any\n");
|
2010-05-30 01:42:04 +00:00
|
|
|
static PyObject *Vector_angle(VectorObject *self, PyObject *args)
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
{
|
2023-11-07 16:33:19 +11:00
|
|
|
const int vec_num = std::min(self->vec_num, 3); /* 4D angle makes no sense */
|
2011-02-04 03:06:23 +00:00
|
|
|
float tvec[MAX_DIMENSIONS];
|
|
|
|
|
PyObject *value;
|
2011-12-24 13:26:30 +00:00
|
|
|
double dot = 0.0f, dot_self = 0.0f, dot_other = 0.0f;
|
2011-02-04 03:06:23 +00:00
|
|
|
int x;
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *fallback = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "O|O:angle", &value, &fallback)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-29 19:40:27 +00:00
|
|
|
/* don't use clamped size, rule of thumb is vector sizes must match,
|
|
|
|
|
* even though n this case 'w' is ignored */
|
2019-03-30 06:12:48 +11:00
|
|
|
if (mathutils_array_parse(
|
2022-03-28 11:06:01 +11:00
|
|
|
tvec, self->vec_num, self->vec_num, value, "Vector.angle(other), invalid 'other' arg") ==
|
|
|
|
|
-1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num > 4) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Vector must be 2D, 3D or 4D");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
for (x = 0; x < vec_num; x++) {
|
2023-07-21 10:59:54 +10:00
|
|
|
dot_self += double(self->vec[x]) * double(self->vec[x]);
|
|
|
|
|
dot_other += double(tvec[x]) * double(tvec[x]);
|
|
|
|
|
dot += double(self->vec[x]) * double(tvec[x]);
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-29 19:40:27 +00:00
|
|
|
if (!dot_self || !dot_other) {
|
2010-05-30 01:42:04 +00:00
|
|
|
/* avoid exception */
|
2011-11-16 03:56:34 +00:00
|
|
|
if (fallback) {
|
2010-05-30 01:42:04 +00:00
|
|
|
Py_INCREF(fallback);
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
|
"Vector.angle(other): "
|
|
|
|
|
"zero length vectors have no valid angle");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-07 19:49:01 +01:00
|
|
|
return PyFloat_FromDouble(safe_acosf(dot / (sqrt(dot_self) * sqrt(dot_other))));
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Angle Signed
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2012-03-14 06:14:15 +00:00
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2012-03-14 06:14:15 +00:00
|
|
|
Vector_angle_signed_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: angle_signed(other, fallback=None, /)\n"
|
2012-03-14 06:14:15 +00:00
|
|
|
"\n"
|
|
|
|
|
" Return the signed angle between two 2D vectors (clockwise is positive).\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: another vector to compare the angle with\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
2015-03-06 18:50:58 +11:00
|
|
|
" :arg fallback: return this when the angle can't be calculated (zero length vector),\n"
|
|
|
|
|
" (instead of raising a :exc:`ValueError`).\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :type fallback: Any\n"
|
2012-03-14 06:14:15 +00:00
|
|
|
" :return: angle in radians or fallback when given\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :rtype: float | Any\n");
|
2012-03-14 06:14:15 +00:00
|
|
|
static PyObject *Vector_angle_signed(VectorObject *self, PyObject *args)
|
|
|
|
|
{
|
|
|
|
|
float tvec[2];
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
PyObject *value;
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *fallback = nullptr;
|
2012-03-14 06:14:15 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "O|O:angle_signed", &value, &fallback)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2012-03-14 06:14:15 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2012-03-14 06:14:15 +00:00
|
|
|
|
|
|
|
|
if (mathutils_array_parse(
|
|
|
|
|
tvec, 2, 2, value, "Vector.angle_signed(other), invalid 'other' arg") == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-03-14 06:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num != 2) {
|
2012-03-14 06:14:15 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Vector must be 2D");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2012-03-14 06:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_zero_v2(self->vec) || is_zero_v2(tvec)) {
|
|
|
|
|
/* avoid exception */
|
|
|
|
|
if (fallback) {
|
|
|
|
|
Py_INCREF(fallback);
|
|
|
|
|
return fallback;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
|
"Vector.angle_signed(other): "
|
|
|
|
|
"zero length vectors have no valid angle");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-03-14 06:14:15 +00:00
|
|
|
|
|
|
|
|
return PyFloat_FromDouble(angle_signed_v2v2(self->vec, tvec));
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Rotation Difference
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_rotation_difference_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: rotation_difference(other, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Returns a quaternion representing the rotational difference between this\n"
|
|
|
|
|
" vector and another.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: second vector.\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
|
|
|
|
" :return: the rotational difference between the two vectors.\n"
|
|
|
|
|
" :rtype: :class:`Quaternion`\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" .. note:: 2D vectors raise an :exc:`AttributeError`.\n");
|
2011-04-04 05:17:23 +00:00
|
|
|
static PyObject *Vector_rotation_difference(VectorObject *self, PyObject *value)
|
2010-01-27 15:29:21 +00:00
|
|
|
{
|
2010-01-27 21:33:39 +00:00
|
|
|
float quat[4], vec_a[3], vec_b[3];
|
2010-01-27 15:29:21 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num < 3 || self->vec_num > 4) {
|
2011-07-14 09:54:03 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2011-07-14 01:25:05 +00:00
|
|
|
"vec.difference(value): "
|
|
|
|
|
"expects both vectors to be size 3 or 4");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2010-01-27 15:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-01-27 15:29:21 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (mathutils_array_parse(
|
|
|
|
|
vec_b, 3, MAX_DIMENSIONS, value, "Vector.difference(other), invalid 'other' arg") == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-01-27 15:29:21 +00:00
|
|
|
|
2010-01-27 21:33:39 +00:00
|
|
|
normalize_v3_v3(vec_a, self->vec);
|
2011-02-04 03:06:23 +00:00
|
|
|
normalize_v3(vec_b);
|
2010-01-27 21:33:39 +00:00
|
|
|
|
|
|
|
|
rotation_between_vecs_to_quat(quat, vec_a, vec_b);
|
2010-01-27 15:29:21 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
return Quaternion_CreatePyObject(quat, nullptr);
|
2010-01-27 15:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Project
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_project_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: project(other, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Return the projection of this vector onto the *other*.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: second vector.\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
|
|
|
|
" :return: the parallel projection vector\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_project(VectorObject *self, PyObject *value)
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
const int vec_num = self->vec_num;
|
2018-07-27 17:40:01 +10:00
|
|
|
float *tvec;
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
double dot = 0.0f, dot2 = 0.0f;
|
2011-02-04 03:06:23 +00:00
|
|
|
int x;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-07-27 17:40:01 +10:00
|
|
|
if (mathutils_array_parse_alloc(
|
2022-03-28 11:06:01 +11:00
|
|
|
&tvec, vec_num, value, "Vector.project(other), invalid 'other' arg") == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-10-20 20:20:02 +00:00
|
|
|
/* get dot products */
|
2022-03-28 11:06:01 +11:00
|
|
|
for (x = 0; x < vec_num; x++) {
|
2023-07-21 10:59:54 +10:00
|
|
|
dot += double(self->vec[x] * tvec[x]);
|
|
|
|
|
dot2 += double(tvec[x] * tvec[x]);
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
2012-10-20 20:20:02 +00:00
|
|
|
/* projection */
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
dot /= dot2;
|
2022-03-28 11:06:01 +11:00
|
|
|
for (x = 0; x < vec_num; x++) {
|
2023-07-21 10:59:54 +10:00
|
|
|
tvec[x] *= float(dot);
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(tvec, vec_num, Py_TYPE(self));
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Linear Interpolation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_lerp_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: lerp(other, factor, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Returns the interpolation of two vectors.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: value to interpolate with.\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
|
|
|
|
" :arg factor: The interpolation value in [0.0, 1.0].\n"
|
|
|
|
|
" :type factor: float\n"
|
|
|
|
|
" :return: The interpolated vector.\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2011-02-04 03:06:23 +00:00
|
|
|
static PyObject *Vector_lerp(VectorObject *self, PyObject *args)
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
const int vec_num = self->vec_num;
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *value = nullptr;
|
2015-02-15 11:31:39 +11:00
|
|
|
float fac;
|
|
|
|
|
float *tvec;
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "Of:lerp", &value, &fac)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-05-30 01:42:04 +00:00
|
|
|
|
2012-06-26 16:58:58 +00:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (mathutils_array_parse_alloc(
|
|
|
|
|
&tvec, vec_num, value, "Vector.lerp(other), invalid 'other' arg") == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
interp_vn_vn(tvec, self->vec, 1.0f - fac, vec_num);
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(tvec, vec_num, Py_TYPE(self));
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Spherical Interpolation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_slerp_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: slerp(other, factor, fallback=None, /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" Returns the interpolation of two non-zero vectors (spherical coordinates).\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: value to interpolate with.\n"
|
|
|
|
|
" :type other: :class:`Vector`\n"
|
|
|
|
|
" :arg factor: The interpolation value typically in [0.0, 1.0].\n"
|
|
|
|
|
" :type factor: float\n"
|
|
|
|
|
" :arg fallback: return this when the vector can't be calculated (zero length "
|
|
|
|
|
"vector or direct opposites),\n"
|
|
|
|
|
" (instead of raising a :exc:`ValueError`).\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :type fallback: Any\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
" :return: The interpolated vector.\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n");
|
2014-03-31 13:18:23 +11:00
|
|
|
static PyObject *Vector_slerp(VectorObject *self, PyObject *args)
|
|
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
const int vec_num = self->vec_num;
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *value = nullptr;
|
2014-03-31 13:18:23 +11:00
|
|
|
float fac, cosom, w[2];
|
2014-04-18 20:44:50 +10:00
|
|
|
float self_vec[3], other_vec[3], ret_vec[3];
|
|
|
|
|
float self_len_sq, other_len_sq;
|
2014-03-31 13:18:23 +11:00
|
|
|
int x;
|
2023-07-21 02:18:59 +02:00
|
|
|
PyObject *fallback = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!PyArg_ParseTuple(args, "Of|O:slerp", &value, &fac, &fallback)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-31 13:18:23 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num > 3) {
|
2014-03-31 13:18:23 +11:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Vector must be 2D or 3D");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-04-18 20:44:50 +10:00
|
|
|
if (mathutils_array_parse(
|
2022-03-28 11:06:01 +11:00
|
|
|
other_vec, vec_num, vec_num, value, "Vector.slerp(other), invalid 'other' arg") == -1)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
self_len_sq = normalize_vn_vn(self_vec, self->vec, vec_num);
|
|
|
|
|
other_len_sq = normalize_vn(other_vec, vec_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-31 13:18:23 +11:00
|
|
|
/* use fallbacks for zero length vectors */
|
2014-04-18 20:44:50 +10:00
|
|
|
if (UNLIKELY((self_len_sq < FLT_EPSILON) || (other_len_sq < FLT_EPSILON))) {
|
2014-03-31 13:18:23 +11:00
|
|
|
/* avoid exception */
|
|
|
|
|
if (fallback) {
|
|
|
|
|
Py_INCREF(fallback);
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
|
"Vector.slerp(): "
|
|
|
|
|
"zero length vectors unsupported");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-31 13:18:23 +11:00
|
|
|
/* We have sane state, execute slerp */
|
2023-07-21 10:59:54 +10:00
|
|
|
cosom = float(dot_vn_vn(self_vec, other_vec, vec_num));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-31 13:18:23 +11:00
|
|
|
/* direct opposite, can't slerp */
|
|
|
|
|
if (UNLIKELY(cosom < (-1.0f + FLT_EPSILON))) {
|
|
|
|
|
/* avoid exception */
|
|
|
|
|
if (fallback) {
|
|
|
|
|
Py_INCREF(fallback);
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
|
"Vector.slerp(): "
|
|
|
|
|
"opposite vectors unsupported");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-03-31 13:18:23 +11:00
|
|
|
interp_dot_slerp(fac, cosom, w);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
for (x = 0; x < vec_num; x++) {
|
2014-04-18 20:44:50 +10:00
|
|
|
ret_vec[x] = (w[0] * self_vec[x]) + (w[1] * other_vec[x]);
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject(ret_vec, vec_num, Py_TYPE(self));
|
2014-03-31 13:18:23 +11:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Rotate
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2020-02-20 13:57:32 +11:00
|
|
|
PyDoc_STRVAR(
|
2024-01-25 10:22:16 +11:00
|
|
|
/* Wrap. */
|
2020-02-20 13:57:32 +11:00
|
|
|
Vector_rotate_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. function:: rotate(other, /)\n"
|
2020-02-20 13:57:32 +11:00
|
|
|
"\n"
|
|
|
|
|
" Rotate the vector by a rotation value.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" .. note:: 2D vectors are a special case that can only be rotated by a 2x2 matrix.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg other: rotation component of mathutils value\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :type other: :class:`Euler` | :class:`Quaternion` | :class:`Matrix`\n");
|
2011-02-05 09:57:02 +00:00
|
|
|
static PyObject *Vector_rotate(VectorObject *self, PyObject *value)
|
2010-09-29 05:15:55 +00:00
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-09-29 05:15:55 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (self->vec_num == 2) {
|
2020-02-20 13:57:32 +11:00
|
|
|
/* Special case for 2D Vector with 2x2 matrix, so we avoid resizing it to a 3x3. */
|
|
|
|
|
float other_rmat[2][2];
|
|
|
|
|
MatrixObject *pymat;
|
|
|
|
|
if (!Matrix_Parse2x2(value, &pymat)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2020-02-20 13:57:32 +11:00
|
|
|
}
|
|
|
|
|
normalize_m2_m2(other_rmat, (const float(*)[2])pymat->matrix);
|
|
|
|
|
/* Equivalent to a rotation along the Z axis. */
|
|
|
|
|
mul_m2_v2(other_rmat, self->vec);
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2020-02-20 13:57:32 +11:00
|
|
|
else {
|
|
|
|
|
float other_rmat[3][3];
|
2010-09-29 05:15:55 +00:00
|
|
|
|
2020-02-20 13:57:32 +11:00
|
|
|
if (mathutils_any_to_rotmat(other_rmat, value, "Vector.rotate(value)") == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2020-02-20 13:57:32 +11:00
|
|
|
}
|
2010-09-29 05:15:55 +00:00
|
|
|
|
2020-02-20 13:57:32 +11:00
|
|
|
mul_m3_v3(other_rmat, self->vec);
|
|
|
|
|
}
|
2010-09-29 05:15:55 +00:00
|
|
|
|
2011-02-05 09:57:02 +00:00
|
|
|
(void)BaseMath_WriteCallback(self);
|
|
|
|
|
Py_RETURN_NONE;
|
2010-09-29 05:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Negate
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_negate_doc,
|
|
|
|
|
".. method:: negate()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Set all values to their negative.\n");
|
2022-05-25 12:33:15 +10:00
|
|
|
static PyObject *Vector_negate(VectorObject *self)
|
|
|
|
|
{
|
|
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
negate_vn(self->vec, self->vec_num);
|
|
|
|
|
|
|
|
|
|
(void)BaseMath_WriteCallback(self); /* already checked for error */
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Methods: Copy/Deep-Copy
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_copy_doc,
|
|
|
|
|
".. function:: copy()\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Returns a copy of this vector.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :return: A copy of the vector.\n"
|
|
|
|
|
" :rtype: :class:`Vector`\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" .. note:: use this to get a copy of a wrapped vector with\n"
|
|
|
|
|
" no reference to the original data.\n");
|
2010-04-25 03:34:16 +00:00
|
|
|
static PyObject *Vector_copy(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject(self->vec, self->vec_num, Py_TYPE(self));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2012-03-30 11:35:58 +00:00
|
|
|
static PyObject *Vector_deepcopy(VectorObject *self, PyObject *args)
|
|
|
|
|
{
|
2018-06-26 09:26:52 +02:00
|
|
|
if (!PyC_CheckArgs_DeepCopy(args)) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-06-26 09:26:52 +02:00
|
|
|
}
|
2012-03-30 11:35:58 +00:00
|
|
|
return Vector_copy(self);
|
|
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: `__repr__` & `__str__`
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2010-04-25 03:34:16 +00:00
|
|
|
static PyObject *Vector_repr(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2010-04-25 03:34:16 +00:00
|
|
|
PyObject *ret, *tuple;
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-04-19 22:02:53 +00:00
|
|
|
|
2019-08-01 18:34:52 +10:00
|
|
|
tuple = Vector_to_tuple_ex(self, -1);
|
2011-12-24 13:26:30 +00:00
|
|
|
ret = PyUnicode_FromFormat("Vector(%R)", tuple);
|
2010-04-25 03:34:16 +00:00
|
|
|
Py_DECREF(tuple);
|
2010-04-19 22:02:53 +00:00
|
|
|
return ret;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2010-04-19 22:02:53 +00:00
|
|
|
|
2012-12-08 01:16:59 +00:00
|
|
|
#ifndef MATH_STANDALONE
|
2011-12-20 03:37:55 +00:00
|
|
|
static PyObject *Vector_str(VectorObject *self)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
DynStr *ds;
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-12-20 03:37:55 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
ds = BLI_dynstr_new();
|
2011-12-20 03:37:55 +00:00
|
|
|
|
|
|
|
|
BLI_dynstr_append(ds, "<Vector (");
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
for (i = 0; i < self->vec_num; i++) {
|
2011-12-20 03:37:55 +00:00
|
|
|
BLI_dynstr_appendf(ds, i ? ", %.4f" : "%.4f", self->vec[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-20 04:11:23 +00:00
|
|
|
BLI_dynstr_append(ds, ")>");
|
2011-12-20 03:37:55 +00:00
|
|
|
|
|
|
|
|
return mathutils_dynstr_to_py(ds); /* frees ds */
|
|
|
|
|
}
|
2012-12-08 01:16:59 +00:00
|
|
|
#endif
|
2011-12-20 03:37:55 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
2025-08-16 06:14:19 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Buffer Protocol
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static int Vector_getbuffer(PyObject *obj, Py_buffer *view, int flags)
|
|
|
|
|
{
|
|
|
|
|
VectorObject *self = (VectorObject *)obj;
|
|
|
|
|
if (UNLIKELY(BaseMath_Prepare_ForBufferAccess(self, view, flags) == -1)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (UNLIKELY(BaseMath_ReadCallback(self) == -1)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(view, 0, sizeof(*view));
|
|
|
|
|
|
|
|
|
|
view->obj = (PyObject *)self;
|
|
|
|
|
view->buf = (void *)self->vec;
|
|
|
|
|
view->len = Py_ssize_t(self->vec_num * sizeof(float));
|
|
|
|
|
view->itemsize = sizeof(float);
|
|
|
|
|
view->ndim = 1;
|
|
|
|
|
if ((flags & PyBUF_WRITABLE) == 0) {
|
|
|
|
|
view->readonly = 1;
|
|
|
|
|
}
|
|
|
|
|
if (flags & PyBUF_FORMAT) {
|
|
|
|
|
view->format = (char *)"f";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->flag |= BASE_MATH_FLAG_HAS_BUFFER_VIEW;
|
|
|
|
|
|
|
|
|
|
Py_INCREF(self);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void Vector_releasebuffer(PyObject * /*exporter*/, Py_buffer *view)
|
|
|
|
|
{
|
|
|
|
|
VectorObject *self = (VectorObject *)view->obj;
|
|
|
|
|
self->flag &= ~BASE_MATH_FLAG_HAS_BUFFER_VIEW;
|
|
|
|
|
|
|
|
|
|
if (view->readonly == 0) {
|
|
|
|
|
if (UNLIKELY(BaseMath_WriteCallback(self) == -1)) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyBufferProcs Vector_as_buffer = {
|
|
|
|
|
(getbufferproc)Vector_getbuffer,
|
|
|
|
|
(releasebufferproc)Vector_releasebuffer,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Rich Compare
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static PyObject *Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vecA = nullptr, *vecB = nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
int result = 0;
|
|
|
|
|
const double epsilon = 0.000001f;
|
|
|
|
|
double lenA, lenB;
|
|
|
|
|
|
|
|
|
|
if (!VectorObject_Check(objectA) || !VectorObject_Check(objectB)) {
|
|
|
|
|
if (comparison_type == Py_NE) {
|
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
vecA = (VectorObject *)objectA;
|
|
|
|
|
vecB = (VectorObject *)objectB;
|
|
|
|
|
|
|
|
|
|
if (BaseMath_ReadCallback(vecA) == -1 || BaseMath_ReadCallback(vecB) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vecA->vec_num != vecB->vec_num) {
|
|
|
|
|
if (comparison_type == Py_NE) {
|
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (comparison_type) {
|
|
|
|
|
case Py_LT:
|
|
|
|
|
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
|
|
|
|
|
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
|
|
|
|
|
if (lenA < lenB) {
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Py_LE:
|
|
|
|
|
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
|
|
|
|
|
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
|
|
|
|
|
if (lenA < lenB) {
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
result = (((lenA + epsilon) > lenB) && ((lenA - epsilon) < lenB));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Py_EQ:
|
|
|
|
|
result = EXPP_VectorsAreEqual(vecA->vec, vecB->vec, vecA->vec_num, 1);
|
|
|
|
|
break;
|
|
|
|
|
case Py_NE:
|
|
|
|
|
result = !EXPP_VectorsAreEqual(vecA->vec, vecB->vec, vecA->vec_num, 1);
|
|
|
|
|
break;
|
|
|
|
|
case Py_GT:
|
|
|
|
|
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
|
|
|
|
|
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
|
|
|
|
|
if (lenA > lenB) {
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Py_GE:
|
|
|
|
|
lenA = len_squared_vn(vecA->vec, vecA->vec_num);
|
|
|
|
|
lenB = len_squared_vn(vecB->vec, vecB->vec_num);
|
|
|
|
|
if (lenA > lenB) {
|
|
|
|
|
result = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
result = (((lenA + epsilon) > lenB) && ((lenA - epsilon) < lenB));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("The result of the comparison could not be evaluated");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (result == 1) {
|
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Hash (`__hash__`)
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static Py_hash_t Vector_hash(VectorObject *self)
|
|
|
|
|
{
|
|
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (BaseMathObject_Prepare_ForHash(self) == -1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mathutils_array_hash(self->vec, self->vec_num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Sequence & Mapping Protocols Implementation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
/** Sequence length: `len(object)`. */
|
2022-11-08 12:03:38 +11:00
|
|
|
static Py_ssize_t Vector_len(VectorObject *self)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
return self->vec_num;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2022-05-25 12:33:15 +10:00
|
|
|
|
2014-01-28 03:52:21 +11:00
|
|
|
static PyObject *vector_item_internal(VectorObject *self, int i, const bool is_attr)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (i < 0) {
|
2022-03-28 11:06:01 +11:00
|
|
|
i = self->vec_num - i;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-06-25 10:11:37 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (i < 0 || i >= self->vec_num) {
|
2012-03-16 21:39:56 +00:00
|
|
|
if (is_attr) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
2011-09-19 15:13:16 +00:00
|
|
|
"Vector.%c: unavailable on %dd vector",
|
2019-07-04 21:10:06 +10:00
|
|
|
*(((const char *)"xyzw") + i),
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num);
|
2011-07-14 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyErr_SetString(PyExc_IndexError, "vector[index]: out of range");
|
|
|
|
|
}
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadIndexCallback(self, i) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
return PyFloat_FromDouble(self->vec[i]);
|
|
|
|
|
}
|
2011-03-17 04:43:58 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Sequence accessor (get): `x = object[i]`. */
|
2022-11-08 12:03:38 +11:00
|
|
|
static PyObject *Vector_item(VectorObject *self, Py_ssize_t i)
|
2011-03-17 04:43:58 +00:00
|
|
|
{
|
2013-09-14 00:30:56 +00:00
|
|
|
return vector_item_internal(self, i, false);
|
2011-03-17 04:43:58 +00:00
|
|
|
}
|
2022-05-25 12:33:15 +10:00
|
|
|
|
2014-01-28 03:52:21 +11:00
|
|
|
static int vector_ass_item_internal(VectorObject *self, int i, PyObject *value, const bool is_attr)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2010-04-25 03:34:16 +00:00
|
|
|
float scalar;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_Prepare_ForWrite(self) == -1) {
|
2015-02-15 11:26:31 +11:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-07-10 14:41:19 +10:00
|
|
|
if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
|
|
|
|
|
/* parsed item not a number */
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"vector[index] = x: "
|
2014-01-30 16:45:20 +11:00
|
|
|
"assigned value not a number");
|
2009-06-17 20:33:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (i < 0) {
|
2022-03-28 11:06:01 +11:00
|
|
|
i = self->vec_num - i;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (i < 0 || i >= self->vec_num) {
|
2011-11-16 03:56:34 +00:00
|
|
|
if (is_attr) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
2011-09-19 15:13:16 +00:00
|
|
|
"Vector.%c = x: unavailable on %dd vector",
|
2019-07-04 21:10:06 +10:00
|
|
|
*(((const char *)"xyzw") + i),
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num);
|
2011-07-14 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyErr_SetString(PyExc_IndexError,
|
|
|
|
|
"vector[index] = x: "
|
|
|
|
|
"assignment index out of range");
|
|
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2009-06-18 23:12:29 +00:00
|
|
|
self->vec[i] = scalar;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_WriteIndexCallback(self, i) == -1) {
|
2009-06-22 04:26:48 +00:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Sequence accessor (set): `object[i] = x`. */
|
2022-11-08 12:03:38 +11:00
|
|
|
static int Vector_ass_item(VectorObject *self, Py_ssize_t i, PyObject *value)
|
2011-03-17 04:43:58 +00:00
|
|
|
{
|
2013-09-14 00:30:56 +00:00
|
|
|
return vector_ass_item_internal(self, i, value, false);
|
2011-03-17 04:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Sequence slice accessor (get): `x = object[i:j]`. */
|
2010-04-25 03:34:16 +00:00
|
|
|
static PyObject *Vector_slice(VectorObject *self, int begin, int end)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2010-12-24 03:51:34 +00:00
|
|
|
PyObject *tuple;
|
2009-06-17 20:33:34 +00:00
|
|
|
int count;
|
|
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
CLAMP(begin, 0, self->vec_num);
|
2019-03-30 06:12:48 +11:00
|
|
|
if (end < 0) {
|
2022-03-28 11:06:01 +11:00
|
|
|
end = self->vec_num + end + 1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2022-03-28 11:06:01 +11:00
|
|
|
CLAMP(end, 0, self->vec_num);
|
2024-01-22 15:58:18 +01:00
|
|
|
begin = std::min(begin, end);
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
tuple = PyTuple_New(end - begin);
|
2011-10-13 01:29:08 +00:00
|
|
|
for (count = begin; count < end; count++) {
|
2010-12-24 03:51:34 +00:00
|
|
|
PyTuple_SET_ITEM(tuple, count - begin, PyFloat_FromDouble(self->vec[count]));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-24 03:51:34 +00:00
|
|
|
return tuple;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2022-05-25 12:33:15 +10:00
|
|
|
|
|
|
|
|
/** Sequence slice accessor (set): `object[i:j] = x`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static int Vector_ass_slice(VectorObject *self, int begin, int end, PyObject *seq)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num = 0;
|
2023-07-21 02:18:59 +02:00
|
|
|
float *vec = nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(self) == -1) {
|
2009-06-22 04:26:48 +00:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-01-13 21:44:18 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
CLAMP(begin, 0, self->vec_num);
|
|
|
|
|
CLAMP(end, 0, self->vec_num);
|
2024-01-22 15:58:18 +01:00
|
|
|
begin = std::min(begin, end);
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
vec_num = (end - begin);
|
|
|
|
|
if (mathutils_array_parse_alloc(&vec, vec_num, seq, "vector[begin:end] = [...]") == -1) {
|
2012-06-26 16:58:58 +00:00
|
|
|
return -1;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2012-03-26 06:55:09 +00:00
|
|
|
"vec[:] = seq: "
|
2011-12-18 07:27:11 +00:00
|
|
|
"problem allocating pointer space");
|
2009-06-17 20:33:34 +00:00
|
|
|
return -1;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* Parsed well - now set in vector. */
|
|
|
|
|
memcpy(self->vec + begin, vec, vec_num * sizeof(float));
|
|
|
|
|
|
|
|
|
|
PyMem_Free(vec);
|
|
|
|
|
|
|
|
|
|
if (BaseMath_WriteCallback(self) == -1) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sequence generic subscript (get): `x = object[...]`. */
|
|
|
|
|
static PyObject *Vector_subscript(VectorObject *self, PyObject *item)
|
|
|
|
|
{
|
|
|
|
|
if (PyIndex_Check(item)) {
|
|
|
|
|
Py_ssize_t i;
|
|
|
|
|
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
|
|
|
|
if (i == -1 && PyErr_Occurred()) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
i += self->vec_num;
|
|
|
|
|
}
|
|
|
|
|
return Vector_item(self, i);
|
|
|
|
|
}
|
|
|
|
|
if (PySlice_Check(item)) {
|
|
|
|
|
Py_ssize_t start, stop, step, slicelength;
|
|
|
|
|
|
|
|
|
|
if (PySlice_GetIndicesEx(item, self->vec_num, &start, &stop, &step, &slicelength) < 0) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (slicelength <= 0) {
|
|
|
|
|
return PyTuple_New(0);
|
|
|
|
|
}
|
|
|
|
|
if (step == 1) {
|
|
|
|
|
return Vector_slice(self, start, stop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_IndexError, "slice steps not supported with vectors");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyErr_Format(
|
|
|
|
|
PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2022-05-25 12:33:15 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Sequence generic subscript (set): `object[...] = x`. */
|
|
|
|
|
static int Vector_ass_subscript(VectorObject *self, PyObject *item, PyObject *value)
|
|
|
|
|
{
|
|
|
|
|
if (PyIndex_Check(item)) {
|
|
|
|
|
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
|
|
|
|
|
if (i == -1 && PyErr_Occurred()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (i < 0) {
|
|
|
|
|
i += self->vec_num;
|
|
|
|
|
}
|
|
|
|
|
return Vector_ass_item(self, i, value);
|
|
|
|
|
}
|
|
|
|
|
if (PySlice_Check(item)) {
|
|
|
|
|
Py_ssize_t start, stop, step, slicelength;
|
|
|
|
|
|
|
|
|
|
if (PySlice_GetIndicesEx(item, self->vec_num, &start, &stop, &step, &slicelength) < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-01-13 21:44:18 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
if (step == 1) {
|
|
|
|
|
return Vector_ass_slice(self, start, stop, value);
|
|
|
|
|
}
|
2012-06-26 16:58:58 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
PyErr_SetString(PyExc_IndexError, "slice steps not supported with vectors");
|
2009-06-22 04:26:48 +00:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-01-13 21:44:18 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
PyErr_Format(
|
|
|
|
|
PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
|
|
|
|
|
return -1;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Numeric Protocol Implementation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
/** Addition: `object + object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_add(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
|
|
|
|
float *vec = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-01-23 08:37:34 +00:00
|
|
|
if (!VectorObject_Check(v1) || !VectorObject_Check(v2)) {
|
2011-11-13 09:20:04 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
|
"Vector addition: (%s + %s) "
|
|
|
|
|
"invalid type for this operation",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2012-03-26 06:55:09 +00:00
|
|
|
vec1 = (VectorObject *)v1;
|
|
|
|
|
vec2 = (VectorObject *)v2;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-26 21:35:18 +10:00
|
|
|
/* VECTOR + VECTOR. */
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
|
"Vector addition: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-01-23 08:37:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
|
|
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
|
"Vector(): "
|
|
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
add_vn_vnvn(vec, vec1->vec, vec2->vec, vec1->vec_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec1->vec_num, Py_TYPE(v1));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Addition in-place: `object += object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_iadd(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
2009-11-26 16:19:20 +00:00
|
|
|
|
|
|
|
|
if (!VectorObject_Check(v1) || !VectorObject_Check(v2)) {
|
2011-11-13 09:20:04 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
|
"Vector addition: (%s += %s) "
|
|
|
|
|
"invalid type for this operation",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-11-26 16:19:20 +00:00
|
|
|
}
|
2012-03-26 06:55:09 +00:00
|
|
|
vec1 = (VectorObject *)v1;
|
|
|
|
|
vec2 = (VectorObject *)v2;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
|
"Vector addition: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-11-26 16:19:20 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
add_vn_vn(vec1->vec, vec2->vec, vec1->vec_num);
|
2009-11-26 16:19:20 +00:00
|
|
|
|
2010-10-20 12:11:09 +00:00
|
|
|
(void)BaseMath_WriteCallback(vec1);
|
2011-01-23 08:37:34 +00:00
|
|
|
Py_INCREF(v1);
|
2009-11-26 16:19:20 +00:00
|
|
|
return v1;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Subtraction: `object - object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_sub(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
float *vec;
|
2009-06-17 20:33:34 +00:00
|
|
|
|
|
|
|
|
if (!VectorObject_Check(v1) || !VectorObject_Check(v2)) {
|
2011-11-13 09:20:04 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
|
"Vector subtraction: (%s - %s) "
|
|
|
|
|
"invalid type for this operation",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2012-03-26 06:55:09 +00:00
|
|
|
vec1 = (VectorObject *)v1;
|
|
|
|
|
vec2 = (VectorObject *)v2;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
|
"Vector subtraction: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-01-23 08:37:34 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
|
|
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
|
"Vector(): "
|
|
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
sub_vn_vnvn(vec, vec1->vec, vec2->vec, vec1->vec_num);
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec1->vec_num, Py_TYPE(v1));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Subtraction in-place: `object -= object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_isub(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
|
|
|
|
|
if (!VectorObject_Check(v1) || !VectorObject_Check(v2)) {
|
2011-11-13 09:20:04 +00:00
|
|
|
PyErr_Format(PyExc_AttributeError,
|
|
|
|
|
"Vector subtraction: (%s -= %s) "
|
|
|
|
|
"invalid type for this operation",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2012-03-26 06:55:09 +00:00
|
|
|
vec1 = (VectorObject *)v1;
|
|
|
|
|
vec2 = (VectorObject *)v2;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
|
"Vector subtraction: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
sub_vn_vn(vec1->vec, vec2->vec, vec1->vec_num);
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2010-10-20 12:11:09 +00:00
|
|
|
(void)BaseMath_WriteCallback(vec1);
|
2011-01-23 08:37:34 +00:00
|
|
|
Py_INCREF(v1);
|
2009-06-17 20:33:34 +00:00
|
|
|
return v1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* Multiply internal implementation `object * object`, `object *= object`. */
|
2010-11-12 01:38:18 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
int column_vector_multiplication(float r_vec[MAX_DIMENSIONS], VectorObject *vec, MatrixObject *mat)
|
2010-11-12 01:38:18 +00:00
|
|
|
{
|
2011-01-23 08:37:34 +00:00
|
|
|
float vec_cpy[MAX_DIMENSIONS];
|
2011-12-20 11:37:55 +00:00
|
|
|
int row, col, z = 0;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (mat->col_num != vec->vec_num) {
|
|
|
|
|
if (mat->col_num == 4 && vec->vec_num == 3) {
|
2011-01-23 08:37:34 +00:00
|
|
|
vec_cpy[3] = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-02-11 14:27:36 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2011-07-14 01:25:05 +00:00
|
|
|
"matrix * vector: "
|
2012-03-26 06:55:09 +00:00
|
|
|
"len(matrix.col) and len(vector) must be the same, "
|
2012-01-03 14:34:41 +00:00
|
|
|
"except for 4x4 matrix * 3D vector.");
|
2010-11-12 01:38:18 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
memcpy(vec_cpy, vec->vec, vec->vec_num * sizeof(float));
|
2011-01-23 08:37:34 +00:00
|
|
|
|
2011-12-20 11:37:55 +00:00
|
|
|
r_vec[3] = 1.0f;
|
2010-11-12 01:38:18 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
for (row = 0; row < mat->row_num; row++) {
|
2011-12-26 00:42:35 +00:00
|
|
|
double dot = 0.0f;
|
2022-03-28 11:06:01 +11:00
|
|
|
for (col = 0; col < mat->col_num; col++) {
|
2023-07-21 10:59:54 +10:00
|
|
|
dot += double(MATRIX_ITEM(mat, row, col) * vec_cpy[col]);
|
2010-11-12 01:38:18 +00:00
|
|
|
}
|
2023-07-21 10:59:54 +10:00
|
|
|
r_vec[z++] = float(dot);
|
2010-11-12 01:38:18 +00:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2010-11-12 01:38:18 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-09 09:16:04 +00:00
|
|
|
static PyObject *vector_mul_float(VectorObject *vec, const float scalar)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
float *tvec = static_cast<float *>(PyMem_Malloc(vec->vec_num * sizeof(float)));
|
|
|
|
|
if (tvec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
|
"vec * float: "
|
|
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_vn_fl(tvec, vec->vec, vec->vec_num, scalar);
|
|
|
|
|
return Vector_CreatePyObject_alloc(tvec, vec->vec_num, Py_TYPE(vec));
|
2011-01-09 09:16:04 +00:00
|
|
|
}
|
2020-07-14 22:45:33 +10:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
static PyObject *vector_mul_vec(VectorObject *vec1, VectorObject *vec2)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
float *tvec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
|
|
|
|
|
if (tvec == nullptr) {
|
2018-08-10 14:53:38 +02:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
2018-08-22 10:10:12 +10:00
|
|
|
"vec * vec: "
|
|
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2011-01-09 09:16:04 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_vnvn(tvec, vec1->vec, vec2->vec, vec1->vec_num);
|
|
|
|
|
return Vector_CreatePyObject_alloc(tvec, vec1->vec_num, Py_TYPE(vec1));
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2020-07-14 22:45:33 +10:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Multiplication (element-wise or scalar): `object * object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_mul(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
2009-06-18 23:12:29 +00:00
|
|
|
float scalar;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-26 06:55:09 +00:00
|
|
|
if (VectorObject_Check(v1)) {
|
2011-12-24 13:26:30 +00:00
|
|
|
vec1 = (VectorObject *)v1;
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec1) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2012-03-26 06:55:09 +00:00
|
|
|
if (VectorObject_Check(v2)) {
|
2011-12-24 13:26:30 +00:00
|
|
|
vec2 = (VectorObject *)v2;
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-21 11:03:37 +11:00
|
|
|
/* Intentionally don't support (Quaternion) here, uses reverse order instead. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
/* make sure v1 is always the vector */
|
2011-03-19 11:12:48 +00:00
|
|
|
if (vec1 && vec2) {
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2011-07-14 09:54:03 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2011-07-14 01:25:05 +00:00
|
|
|
"Vector multiplication: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
/* element-wise product */
|
|
|
|
|
return vector_mul_vec(vec1, vec2);
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
if (vec1) {
|
2018-08-10 14:53:38 +02:00
|
|
|
if (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0) { /* VEC * FLOAT */
|
2011-01-09 09:16:04 +00:00
|
|
|
return vector_mul_float(vec1, scalar);
|
2010-08-02 00:08:01 +00:00
|
|
|
}
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-01-09 09:16:04 +00:00
|
|
|
else if (vec2) {
|
2011-12-24 13:26:30 +00:00
|
|
|
if (((scalar = PyFloat_AsDouble(v1)) == -1.0f && PyErr_Occurred()) == 0) { /* FLOAT * VEC */
|
2011-01-09 09:16:04 +00:00
|
|
|
return vector_mul_float(vec2, scalar);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2009-06-18 23:12:29 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2018-08-22 10:10:12 +10:00
|
|
|
"Element-wise multiplication: "
|
2011-07-14 01:25:05 +00:00
|
|
|
"not supported between '%.200s' and '%.200s' types",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Multiplication in-place (element-wise or scalar): `object *= object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_imul(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
2009-06-18 23:12:29 +00:00
|
|
|
float scalar;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
if (VectorObject_Check(v1)) {
|
|
|
|
|
vec1 = (VectorObject *)v1;
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec1) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
if (VectorObject_Check(v2)) {
|
|
|
|
|
vec2 = (VectorObject *)v2;
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(vec1) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-01-21 11:03:37 +11:00
|
|
|
/* Intentionally don't support (Quaternion, Matrix) here, uses reverse order instead. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
if (vec1 && vec2) {
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2018-08-10 14:53:38 +02:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2018-08-22 10:10:12 +10:00
|
|
|
"Vector multiplication: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-07-14 22:45:33 +10:00
|
|
|
/* Element-wise product in-place. */
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_vn(vec1->vec, vec2->vec, vec1->vec_num);
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2023-05-02 09:32:44 +10:00
|
|
|
else if (vec1 && (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0)) {
|
|
|
|
|
/* VEC *= FLOAT */
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_fl(vec1->vec, vec1->vec_num, scalar);
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2011-11-13 09:20:04 +00:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2019-08-04 12:51:44 +10:00
|
|
|
"In place element-wise multiplication: "
|
2018-08-22 10:10:12 +10:00
|
|
|
"not supported between '%.200s' and '%.200s' types",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-18 23:12:29 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
(void)BaseMath_WriteCallback(vec1);
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_INCREF(v1);
|
2009-06-22 04:26:48 +00:00
|
|
|
return v1;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Multiplication (matrix multiply): `object @ object`. */
|
2018-08-10 14:53:38 +02:00
|
|
|
static PyObject *Vector_matmul(PyObject *v1, PyObject *v2)
|
|
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *vec1 = nullptr, *vec2 = nullptr;
|
2022-03-28 11:06:01 +11:00
|
|
|
int vec_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
if (VectorObject_Check(v1)) {
|
|
|
|
|
vec1 = (VectorObject *)v1;
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec1) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
if (VectorObject_Check(v2)) {
|
|
|
|
|
vec2 = (VectorObject *)v2;
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
/* Intentionally don't support (Quaternion) here, uses reverse order instead. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
/* make sure v1 is always the vector */
|
|
|
|
|
if (vec1 && vec2) {
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec1->vec_num != vec2->vec_num) {
|
2018-08-10 14:53:38 +02:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2018-08-22 10:10:12 +10:00
|
|
|
"Vector multiplication: "
|
|
|
|
|
"vectors must have the same dimensions for this operation");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-26 21:35:18 +10:00
|
|
|
/* Dot product. */
|
2022-03-28 11:06:01 +11:00
|
|
|
return PyFloat_FromDouble(dot_vn_vn(vec1->vec, vec2->vec, vec1->vec_num));
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
if (vec1) {
|
2018-08-10 14:53:38 +02:00
|
|
|
if (MatrixObject_Check(v2)) {
|
|
|
|
|
/* VEC @ MATRIX */
|
|
|
|
|
float tvec[MAX_DIMENSIONS];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback((MatrixObject *)v2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2018-08-10 14:53:38 +02:00
|
|
|
if (row_vector_multiplication(tvec, vec1, (MatrixObject *)v2) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (((MatrixObject *)v2)->row_num == 4 && vec1->vec_num == 3) {
|
|
|
|
|
vec_num = 3;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2022-03-28 11:06:01 +11:00
|
|
|
vec_num = ((MatrixObject *)v2)->col_num;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject(tvec, vec_num, Py_TYPE(vec1));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-10 14:53:38 +02:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2018-08-22 10:10:12 +10:00
|
|
|
"Vector multiplication: "
|
|
|
|
|
"not supported between '%.200s' and '%.200s' types",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Multiplication in-place (matrix multiply): `object @= object`. */
|
2018-08-10 14:53:38 +02:00
|
|
|
static PyObject *Vector_imatmul(PyObject *v1, PyObject *v2)
|
|
|
|
|
{
|
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
2019-08-04 12:51:44 +10:00
|
|
|
"In place vector multiplication: "
|
2018-08-22 10:10:12 +10:00
|
|
|
"not supported between '%.200s' and '%.200s' types",
|
|
|
|
|
Py_TYPE(v1)->tp_name,
|
|
|
|
|
Py_TYPE(v2)->tp_name);
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2018-08-10 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Division: `object / object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_div(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
float *vec = nullptr, scalar;
|
|
|
|
|
VectorObject *vec1 = nullptr;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2011-11-16 03:56:34 +00:00
|
|
|
if (!VectorObject_Check(v1)) { /* not a vector */
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"Vector division: "
|
|
|
|
|
"Vector must be divided by a float");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-12-18 07:27:11 +00:00
|
|
|
vec1 = (VectorObject *)v1; /* vector */
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(vec1) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-04-25 03:34:16 +00:00
|
|
|
|
2019-07-10 14:41:19 +10:00
|
|
|
if ((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) {
|
|
|
|
|
/* parsed item not a number */
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"Vector division: "
|
|
|
|
|
"Vector must be divided by a float");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
if (scalar == 0.0f) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ZeroDivisionError,
|
|
|
|
|
"Vector division: "
|
|
|
|
|
"divide by zero error");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
if (vec == nullptr) {
|
2011-12-18 07:27:11 +00:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
|
"vec / value: "
|
|
|
|
|
"problem allocating pointer space");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_vn_fl(vec, vec1->vec, vec1->vec_num, 1.0f / scalar);
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return Vector_CreatePyObject_alloc(vec, vec1->vec_num, Py_TYPE(v1));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Division in-place: `object /= object`. */
|
2011-06-02 08:29:16 +00:00
|
|
|
static PyObject *Vector_idiv(PyObject *v1, PyObject *v2)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
|
|
|
|
float scalar;
|
2012-03-26 06:55:09 +00:00
|
|
|
VectorObject *vec1 = (VectorObject *)v1;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(vec1) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2009-06-18 23:12:29 +00:00
|
|
|
|
2019-07-10 14:41:19 +10:00
|
|
|
if ((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) {
|
|
|
|
|
/* parsed item not a number */
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
|
"Vector division: "
|
|
|
|
|
"Vector must be divided by a float");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2010-04-25 03:34:16 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
if (scalar == 0.0f) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ZeroDivisionError,
|
|
|
|
|
"Vector division: "
|
|
|
|
|
"divide by zero error");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_fl(vec1->vec, vec1->vec_num, 1.0f / scalar);
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2010-10-20 12:11:09 +00:00
|
|
|
(void)BaseMath_WriteCallback(vec1);
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2011-03-19 11:12:48 +00:00
|
|
|
Py_INCREF(v1);
|
2009-06-17 20:33:34 +00:00
|
|
|
return v1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** Negative (returns the negative of this object): `-object`. */
|
2009-06-17 20:33:34 +00:00
|
|
|
static PyObject *Vector_neg(VectorObject *self)
|
|
|
|
|
{
|
2011-12-18 07:27:11 +00:00
|
|
|
float *tvec;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
tvec = static_cast<float *>(PyMem_Malloc(self->vec_num * sizeof(float)));
|
2022-03-28 11:06:01 +11:00
|
|
|
negate_vn_vn(tvec, self->vec, self->vec_num);
|
|
|
|
|
return Vector_CreatePyObject_alloc(tvec, self->vec_num, Py_TYPE(self));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
2015-02-15 10:46:14 +11:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Protocol Declarations
|
|
|
|
|
* \{ */
|
2015-02-15 10:46:14 +11:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
static PySequenceMethods Vector_SeqMethods = {
|
2022-11-07 22:34:35 +11:00
|
|
|
/*sq_length*/ (lenfunc)Vector_len,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*sq_concat*/ nullptr,
|
|
|
|
|
/*sq_repeat*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*sq_item*/ (ssizeargfunc)Vector_item,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*was_sq_slice*/ nullptr, /* DEPRECATED. */
|
2022-11-07 22:34:35 +11:00
|
|
|
/*sq_ass_item*/ (ssizeobjargproc)Vector_ass_item,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*was_sq_ass_slice*/ nullptr, /* DEPRECATED. */
|
|
|
|
|
/*sq_contains*/ nullptr,
|
2023-07-22 12:14:47 +10:00
|
|
|
/*sq_inplace_concat*/ nullptr,
|
|
|
|
|
/*sq_inplace_repeat*/ nullptr,
|
2009-07-01 13:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static PyMappingMethods Vector_AsMapping = {
|
2023-04-14 14:26:42 +10:00
|
|
|
/*mp_length*/ (lenfunc)Vector_len,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*mp_subscript*/ (binaryfunc)Vector_subscript,
|
|
|
|
|
/*mp_ass_subscript*/ (objobjargproc)Vector_ass_subscript,
|
2009-06-17 20:33:34 +00:00
|
|
|
};
|
2009-08-10 00:07:34 +00:00
|
|
|
|
2009-06-28 13:27:06 +00:00
|
|
|
static PyNumberMethods Vector_NumMethods = {
|
2022-11-07 22:34:35 +11:00
|
|
|
/*nb_add*/ (binaryfunc)Vector_add,
|
|
|
|
|
/*nb_subtract*/ (binaryfunc)Vector_sub,
|
|
|
|
|
/*nb_multiply*/ (binaryfunc)Vector_mul,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*nb_remainder*/ nullptr,
|
|
|
|
|
/*nb_divmod*/ nullptr,
|
|
|
|
|
/*nb_power*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*nb_negative*/ (unaryfunc)Vector_neg,
|
2023-07-22 12:14:47 +10:00
|
|
|
/*nb_positive*/ (unaryfunc)Vector_copy,
|
|
|
|
|
/*nb_absolute*/ nullptr,
|
|
|
|
|
/*nb_bool*/ nullptr,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*nb_invert*/ nullptr,
|
|
|
|
|
/*nb_lshift*/ nullptr,
|
|
|
|
|
/*nb_rshift*/ nullptr,
|
|
|
|
|
/*nb_and*/ nullptr,
|
|
|
|
|
/*nb_xor*/ nullptr,
|
|
|
|
|
/*nb_or*/ nullptr,
|
|
|
|
|
/*nb_int*/ nullptr,
|
|
|
|
|
/*nb_reserved*/ nullptr,
|
|
|
|
|
/*nb_float*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*nb_inplace_add*/ Vector_iadd,
|
|
|
|
|
/*nb_inplace_subtract*/ Vector_isub,
|
|
|
|
|
/*nb_inplace_multiply*/ Vector_imul,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*nb_inplace_remainder*/ nullptr,
|
|
|
|
|
/*nb_inplace_power*/ nullptr,
|
|
|
|
|
/*nb_inplace_lshift*/ nullptr,
|
|
|
|
|
/*nb_inplace_rshift*/ nullptr,
|
|
|
|
|
/*nb_inplace_and*/ nullptr,
|
|
|
|
|
/*nb_inplace_xor*/ nullptr,
|
|
|
|
|
/*nb_inplace_or*/ nullptr,
|
|
|
|
|
/*nb_floor_divide*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*nb_true_divide*/ Vector_div,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*nb_inplace_floor_divide*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*nb_inplace_true_divide*/ Vector_idiv,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*nb_index*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*nb_matrix_multiply*/ (binaryfunc)Vector_matmul,
|
|
|
|
|
/*nb_inplace_matrix_multiply*/ (binaryfunc)Vector_imatmul,
|
2009-06-28 13:27:06 +00:00
|
|
|
};
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Get/Set Item Implementation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
/* Vector axis: `vector.x/y/z/w`. */
|
2011-12-25 11:36:26 +00:00
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_axis_x_doc,
|
|
|
|
|
"Vector X axis.\n"
|
|
|
|
|
"\n"
|
2025-08-22 14:05:28 +10:00
|
|
|
":type: float\n");
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_axis_y_doc,
|
|
|
|
|
"Vector Y axis.\n"
|
|
|
|
|
"\n"
|
2025-08-22 14:05:28 +10:00
|
|
|
":type: float\n");
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_axis_z_doc,
|
|
|
|
|
"Vector Z axis (3D Vectors only).\n"
|
|
|
|
|
"\n"
|
2025-08-22 14:05:28 +10:00
|
|
|
":type: float\n");
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_axis_w_doc,
|
|
|
|
|
"Vector W axis (4D Vectors only).\n"
|
|
|
|
|
"\n"
|
2025-08-22 14:05:28 +10:00
|
|
|
":type: float\n");
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2011-12-24 04:58:01 +00:00
|
|
|
static PyObject *Vector_axis_get(VectorObject *self, void *type)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2018-09-19 12:05:58 +10:00
|
|
|
return vector_item_internal(self, POINTER_AS_INT(type), true);
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-24 04:58:01 +00:00
|
|
|
static int Vector_axis_set(VectorObject *self, PyObject *value, void *type)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2018-09-19 12:05:58 +10:00
|
|
|
return vector_ass_item_internal(self, POINTER_AS_INT(type), value, true);
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* `Vector.length`. */
|
2011-12-25 11:36:26 +00:00
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_length_doc,
|
|
|
|
|
"Vector Length.\n"
|
|
|
|
|
"\n"
|
2025-08-22 14:05:28 +10:00
|
|
|
":type: float\n");
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *Vector_length_get(VectorObject *self, void * /*closure*/)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return PyFloat_FromDouble(sqrt(dot_vn_vn(self->vec, self->vec, self->vec_num)));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-24 04:58:01 +00:00
|
|
|
static int Vector_length_set(VectorObject *self, PyObject *value)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
|
|
|
|
double dot = 0.0f, param;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(self) == -1) {
|
2009-06-22 04:26:48 +00:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2010-04-25 03:34:16 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
if ((param = PyFloat_AsDouble(value)) == -1.0 && PyErr_Occurred()) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_TypeError, "length must be set to a number");
|
2009-06-17 20:33:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2011-03-28 17:08:33 +00:00
|
|
|
if (param < 0.0) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_ValueError, "cannot set a vectors length to a negative value");
|
2009-06-17 20:33:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2011-03-28 17:08:33 +00:00
|
|
|
if (param == 0.0) {
|
2022-03-28 11:06:01 +11:00
|
|
|
copy_vn_fl(self->vec, self->vec_num, 0.0f);
|
2009-06-17 20:33:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
dot = dot_vn_vn(self->vec, self->vec, self->vec_num);
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (!dot) {
|
2021-06-22 10:42:32 -07:00
|
|
|
/* can't sqrt zero */
|
2009-06-17 20:33:34 +00:00
|
|
|
return 0;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
dot = sqrt(dot);
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (dot == param) {
|
2009-06-17 20:33:34 +00:00
|
|
|
return 0;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
dot = dot / param;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
mul_vn_fl(self->vec, self->vec_num, 1.0 / dot);
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2010-10-20 12:11:09 +00:00
|
|
|
(void)BaseMath_WriteCallback(self); /* checked already */
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* `Vector.length_squared`. */
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_length_squared_doc,
|
|
|
|
|
"Vector length squared (v.dot(v)).\n"
|
|
|
|
|
"\n"
|
2025-08-22 14:05:28 +10:00
|
|
|
":type: float\n");
|
2023-07-21 02:18:59 +02:00
|
|
|
static PyObject *Vector_length_squared_get(VectorObject *self, void * /*closure*/)
|
2011-08-11 09:40:14 +00:00
|
|
|
{
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-08-11 09:40:14 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
return PyFloat_FromDouble(dot_vn_vn(self->vec, self->vec, self->vec_num));
|
2011-08-11 09:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-09 13:45:07 +10:00
|
|
|
/* `Vector.xyzw`, etc.. */
|
|
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
Vector_swizzle_doc,
|
|
|
|
|
":type: :class:`Vector`");
|
2017-09-09 10:47:57 +10:00
|
|
|
/**
|
|
|
|
|
* Python script used to make swizzle array:
|
|
|
|
|
*
|
|
|
|
|
* \code{.py}
|
|
|
|
|
* SWIZZLE_BITS_PER_AXIS = 3
|
|
|
|
|
* SWIZZLE_VALID_AXIS = 0x4
|
|
|
|
|
*
|
|
|
|
|
* axis_dict = {}
|
2024-07-09 13:45:07 +10:00
|
|
|
* axis_pos = {"x": 0, "y": 1, "z": 2, "w": 3}
|
|
|
|
|
* axis_chars = "xyzw"
|
2019-09-30 17:06:28 +10:00
|
|
|
* while len(axis_chars) >= 2:
|
|
|
|
|
* for axis_0 in axis_chars:
|
2017-09-09 10:47:57 +10:00
|
|
|
* axis_0_pos = axis_pos[axis_0]
|
2019-09-30 17:06:28 +10:00
|
|
|
* for axis_1 in axis_chars:
|
2017-09-09 10:47:57 +10:00
|
|
|
* axis_1_pos = axis_pos[axis_1]
|
|
|
|
|
* axis_dict[axis_0 + axis_1] = (
|
2024-07-09 13:45:07 +10:00
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) | "
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) << SWIZZLE_BITS_PER_AXIS))"
|
|
|
|
|
* ).format(axis_0_pos, axis_1_pos)
|
|
|
|
|
* if len(axis_chars) <= 2:
|
|
|
|
|
* continue
|
|
|
|
|
* for axis_2 in axis_chars:
|
|
|
|
|
* axis_2_pos = axis_pos[axis_2]
|
|
|
|
|
* axis_dict[axis_0 + axis_1 + axis_2] = (
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) | "
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) << SWIZZLE_BITS_PER_AXIS) | "
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 2)))"
|
|
|
|
|
* ).format(axis_0_pos, axis_1_pos, axis_2_pos)
|
|
|
|
|
* if len(axis_chars) <= 3:
|
|
|
|
|
* continue
|
|
|
|
|
* for axis_3 in axis_chars:
|
|
|
|
|
* axis_3_pos = axis_pos[axis_3]
|
|
|
|
|
* axis_dict[axis_0 + axis_1 + axis_2 + axis_3] = (
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) | "
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) << SWIZZLE_BITS_PER_AXIS) | "
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 2)) | "
|
|
|
|
|
* "(({:d} | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 3)))"
|
|
|
|
|
* ).format(axis_0_pos, axis_1_pos, axis_2_pos, axis_3_pos)
|
2017-09-09 10:47:57 +10:00
|
|
|
*
|
2019-09-30 17:06:28 +10:00
|
|
|
* axis_chars = axis_chars[:-1]
|
2017-09-09 10:47:57 +10:00
|
|
|
* items = list(axis_dict.items())
|
2019-04-29 19:59:13 +10:00
|
|
|
* items.sort(
|
2024-07-09 13:45:07 +10:00
|
|
|
* key=lambda a: a[0].replace("x", "0").replace("y", "1").replace("z", "2").replace("w", "3")
|
2019-04-29 19:59:13 +10:00
|
|
|
* )
|
2017-09-09 10:47:57 +10:00
|
|
|
*
|
2024-07-09 13:45:07 +10:00
|
|
|
* for size in range(2, 5):
|
|
|
|
|
* for rw_pass in (True, False):
|
|
|
|
|
* key_args = ", ".join(list("abcd"[:size]))
|
|
|
|
|
*
|
|
|
|
|
* print("#define VECTOR_SWIZZLE{:d}_{:s}_DEF(attr, {:s}) \\".format(
|
|
|
|
|
* size,
|
|
|
|
|
* "RW" if rw_pass else "RO",
|
|
|
|
|
* key_args,
|
|
|
|
|
* ))
|
|
|
|
|
* print(" {{attr, (getter){:s}, (setter){:s}, {:s}, SWIZZLE{:d}({:s}), }}".format(
|
|
|
|
|
* "Vector_swizzle_get",
|
|
|
|
|
* "Vector_swizzle_set" if rw_pass else "nullptr",
|
|
|
|
|
* "Vector_swizzle_doc",
|
|
|
|
|
* size,
|
|
|
|
|
* key_args,
|
|
|
|
|
* ))
|
|
|
|
|
* print()
|
|
|
|
|
*
|
2017-09-09 10:47:57 +10:00
|
|
|
* unique = set()
|
|
|
|
|
* for key, val in items:
|
|
|
|
|
* num = eval(val)
|
2024-07-09 13:45:07 +10:00
|
|
|
* key_args = ", ".join(["{:d}".format(axis_pos[c]) for c in key.lower()])
|
|
|
|
|
* macro = "VECTOR_SWIZZLE{:d}_{:s}_DEF".format(
|
|
|
|
|
* len(key),
|
|
|
|
|
* "RW" if len(set(key)) == len(key) else "RO",
|
|
|
|
|
* )
|
|
|
|
|
* print(" {:s}(\"{:s}\", {:s}),".format(
|
|
|
|
|
* macro,
|
|
|
|
|
* key,
|
|
|
|
|
* key_args,
|
|
|
|
|
* ))
|
2017-09-09 10:47:57 +10:00
|
|
|
* unique.add(num)
|
|
|
|
|
*
|
|
|
|
|
* if len(unique) != len(items):
|
|
|
|
|
* print("ERROR, duplicate values found")
|
|
|
|
|
* \endcode
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a new Vector according to the provided swizzle bits.
|
|
|
|
|
*/
|
2011-12-24 04:58:01 +00:00
|
|
|
static PyObject *Vector_swizzle_get(VectorObject *self, void *closure)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2010-04-25 19:27:59 +00:00
|
|
|
size_t axis_to;
|
|
|
|
|
size_t axis_from;
|
2009-06-17 20:33:34 +00:00
|
|
|
float vec[MAX_DIMENSIONS];
|
2020-02-20 15:38:58 +11:00
|
|
|
uint swizzleClosure;
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback(self) == -1) {
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
/* Unpack the axes from the closure into an array. */
|
2010-04-25 19:27:59 +00:00
|
|
|
axis_to = 0;
|
2018-09-19 12:05:58 +10:00
|
|
|
swizzleClosure = POINTER_AS_INT(closure);
|
2011-12-25 11:36:26 +00:00
|
|
|
while (swizzleClosure & SWIZZLE_VALID_AXIS) {
|
2010-04-25 19:27:59 +00:00
|
|
|
axis_from = swizzleClosure & SWIZZLE_AXIS;
|
2022-03-28 11:06:01 +11:00
|
|
|
if (axis_from >= self->vec_num) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
|
"Vector swizzle: "
|
|
|
|
|
"specified axis not present");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2010-01-31 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
2010-04-25 19:27:59 +00:00
|
|
|
vec[axis_to] = self->vec[axis_from];
|
2009-06-17 20:33:34 +00:00
|
|
|
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
|
2010-04-25 19:27:59 +00:00
|
|
|
axis_to++;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2011-02-09 09:20:17 +00:00
|
|
|
|
2015-01-04 17:03:54 +11:00
|
|
|
return Vector_CreatePyObject(vec, axis_to, Py_TYPE(self));
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-09 10:47:57 +10:00
|
|
|
/**
|
2018-09-02 18:28:27 +10:00
|
|
|
* Set the items of this vector using a swizzle.
|
2011-12-25 11:36:26 +00:00
|
|
|
* - If value is a vector or list this operates like an array copy, except that
|
|
|
|
|
* the destination is effectively re-ordered as defined by the swizzle. At
|
2023-05-20 21:17:09 +10:00
|
|
|
* most `min(len(source), len(destination))` values will be copied.
|
2011-12-25 11:36:26 +00:00
|
|
|
* - If the value is scalar, it is copied to all axes listed in the swizzle.
|
|
|
|
|
* - If an axis appears more than once in the swizzle, the final occurrence is
|
|
|
|
|
* the one that determines its value.
|
2012-03-03 20:36:09 +00:00
|
|
|
*
|
2017-09-09 10:47:57 +10:00
|
|
|
* \return 0 on success and -1 on failure. On failure, the vector will be unchanged.
|
|
|
|
|
*/
|
2011-12-24 04:58:01 +00:00
|
|
|
static int Vector_swizzle_set(VectorObject *self, PyObject *value, void *closure)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2010-04-25 19:27:59 +00:00
|
|
|
size_t size_from;
|
2009-06-17 20:33:34 +00:00
|
|
|
float scalarVal;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-25 19:27:59 +00:00
|
|
|
size_t axis_from;
|
|
|
|
|
size_t axis_to;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-02-20 15:38:58 +11:00
|
|
|
uint swizzleClosure;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-25 19:27:59 +00:00
|
|
|
float tvec[MAX_DIMENSIONS];
|
|
|
|
|
float vec_assign[MAX_DIMENSIONS];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_ReadCallback_ForWrite(self) == -1) {
|
2009-06-22 04:26:48 +00:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
/* Check that the closure can be used with this vector: even 2D vectors have
|
2011-12-25 11:36:26 +00:00
|
|
|
* swizzles defined for axes z and w, but they would be invalid. */
|
2018-09-19 12:05:58 +10:00
|
|
|
swizzleClosure = POINTER_AS_INT(closure);
|
2011-12-24 13:26:30 +00:00
|
|
|
axis_from = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-07-14 01:25:05 +00:00
|
|
|
while (swizzleClosure & SWIZZLE_VALID_AXIS) {
|
2010-04-25 19:27:59 +00:00
|
|
|
axis_to = swizzleClosure & SWIZZLE_AXIS;
|
2022-03-28 11:06:01 +11:00
|
|
|
if (axis_to >= self->vec_num) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
|
|
|
|
"Vector swizzle: "
|
|
|
|
|
"specified axis not present");
|
2009-06-17 20:33:34 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
|
2010-04-25 19:27:59 +00:00
|
|
|
axis_from++;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
if (((scalarVal = PyFloat_AsDouble(value)) == -1 && PyErr_Occurred()) == 0) {
|
2010-04-25 19:27:59 +00:00
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-25 11:36:26 +00:00
|
|
|
for (i = 0; i < MAX_DIMENSIONS; i++) {
|
2011-12-24 13:26:30 +00:00
|
|
|
vec_assign[i] = scalarVal;
|
2011-12-25 11:36:26 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-24 13:26:30 +00:00
|
|
|
size_from = axis_from;
|
2010-04-25 19:27:59 +00:00
|
|
|
}
|
2025-01-26 20:08:04 +01:00
|
|
|
else if (PyErr_Clear(), /* run but ignore the result */
|
2023-07-31 19:57:28 +10:00
|
|
|
(size_from = size_t(mathutils_array_parse(
|
|
|
|
|
vec_assign, 2, 4, value, "Vector.**** = swizzle assignment"))) == size_t(-1))
|
2023-01-25 11:03:17 +11:00
|
|
|
{
|
2010-04-25 19:27:59 +00:00
|
|
|
return -1;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-16 03:56:34 +00:00
|
|
|
if (axis_from != size_from) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_AttributeError, "Vector swizzle: size does not match swizzle");
|
2010-04-25 19:27:59 +00:00
|
|
|
return -1;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-04-25 19:27:59 +00:00
|
|
|
/* Copy vector contents onto swizzled axes. */
|
|
|
|
|
axis_from = 0;
|
2018-09-19 12:05:58 +10:00
|
|
|
swizzleClosure = POINTER_AS_INT(closure);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-11 18:58:34 +00:00
|
|
|
/* We must first copy current vec into tvec, else some org values may be lost.
|
2023-02-12 14:37:16 +11:00
|
|
|
* See #31760.
|
2022-03-28 11:06:01 +11:00
|
|
|
* Assuming self->vec_num can't be higher than MAX_DIMENSIONS! */
|
|
|
|
|
memcpy(tvec, self->vec, self->vec_num * sizeof(float));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-26 06:55:09 +00:00
|
|
|
while (swizzleClosure & SWIZZLE_VALID_AXIS) {
|
2010-04-25 19:27:59 +00:00
|
|
|
axis_to = swizzleClosure & SWIZZLE_AXIS;
|
|
|
|
|
tvec[axis_to] = vec_assign[axis_from];
|
|
|
|
|
swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
|
|
|
|
|
axis_from++;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-11 18:58:34 +00:00
|
|
|
/* We must copy back the whole tvec into vec, else some changes may be lost (e.g. xz...).
|
2023-02-12 14:37:16 +11:00
|
|
|
* See #31760. */
|
2022-03-28 11:06:01 +11:00
|
|
|
memcpy(self->vec, tvec, self->vec_num * sizeof(float));
|
2010-04-25 19:27:59 +00:00
|
|
|
/* continue with BaseMathObject_WriteCallback at the end */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-30 06:12:48 +11:00
|
|
|
if (BaseMath_WriteCallback(self) == -1) {
|
2009-06-22 04:26:48 +00:00
|
|
|
return -1;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2020-08-07 12:41:06 +02:00
|
|
|
|
|
|
|
|
return 0;
|
2019-03-30 06:12:48 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-09-09 09:58:19 +10:00
|
|
|
#define _SWIZZLE1(a) ((a) | SWIZZLE_VALID_AXIS)
|
|
|
|
|
#define _SWIZZLE2(a, b) (_SWIZZLE1(a) | (((b) | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS)))
|
|
|
|
|
#define _SWIZZLE3(a, b, c) \
|
|
|
|
|
(_SWIZZLE2(a, b) | (((c) | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 2)))
|
|
|
|
|
#define _SWIZZLE4(a, b, c, d) \
|
|
|
|
|
(_SWIZZLE3(a, b, c) | (((d) | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 3)))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-19 12:05:58 +10:00
|
|
|
#define SWIZZLE2(a, b) POINTER_FROM_INT(_SWIZZLE2(a, b))
|
|
|
|
|
#define SWIZZLE3(a, b, c) POINTER_FROM_INT(_SWIZZLE3(a, b, c))
|
|
|
|
|
#define SWIZZLE4(a, b, c, d) POINTER_FROM_INT(_SWIZZLE4(a, b, c, d))
|
2016-07-31 20:41:24 +10:00
|
|
|
|
2024-07-09 13:45:07 +10:00
|
|
|
#define VECTOR_SWIZZLE2_RW_DEF(attr, a, b) \
|
|
|
|
|
{ \
|
|
|
|
|
attr, (getter)Vector_swizzle_get, (setter)Vector_swizzle_set, Vector_swizzle_doc, \
|
|
|
|
|
SWIZZLE2(a, b), \
|
|
|
|
|
}
|
|
|
|
|
#define VECTOR_SWIZZLE2_RO_DEF(attr, a, b) \
|
|
|
|
|
{ \
|
|
|
|
|
attr, (getter)Vector_swizzle_get, (setter) nullptr, Vector_swizzle_doc, SWIZZLE2(a, b), \
|
|
|
|
|
}
|
|
|
|
|
#define VECTOR_SWIZZLE3_RW_DEF(attr, a, b, c) \
|
|
|
|
|
{ \
|
|
|
|
|
attr, (getter)Vector_swizzle_get, (setter)Vector_swizzle_set, Vector_swizzle_doc, \
|
|
|
|
|
SWIZZLE3(a, b, c), \
|
|
|
|
|
}
|
|
|
|
|
#define VECTOR_SWIZZLE3_RO_DEF(attr, a, b, c) \
|
|
|
|
|
{ \
|
|
|
|
|
attr, (getter)Vector_swizzle_get, (setter) nullptr, Vector_swizzle_doc, SWIZZLE3(a, b, c), \
|
|
|
|
|
}
|
|
|
|
|
#define VECTOR_SWIZZLE4_RW_DEF(attr, a, b, c, d) \
|
|
|
|
|
{ \
|
|
|
|
|
attr, (getter)Vector_swizzle_get, (setter)Vector_swizzle_set, Vector_swizzle_doc, \
|
|
|
|
|
SWIZZLE4(a, b, c, d), \
|
|
|
|
|
}
|
|
|
|
|
#define VECTOR_SWIZZLE4_RO_DEF(attr, a, b, c, d) \
|
|
|
|
|
{ \
|
|
|
|
|
attr, (getter)Vector_swizzle_get, (setter) nullptr, Vector_swizzle_doc, SWIZZLE4(a, b, c, d) \
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Get/Set Item Definitions
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2025-04-01 12:06:03 +11:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# ifdef __clang__
|
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
|
# pragma clang diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
# else
|
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
# endif
|
2023-07-21 13:42:35 +10:00
|
|
|
#endif
|
|
|
|
|
|
2009-06-17 20:33:34 +00:00
|
|
|
static PyGetSetDef Vector_getseters[] = {
|
2023-08-03 19:58:30 +10:00
|
|
|
{"x",
|
|
|
|
|
(getter)Vector_axis_get,
|
|
|
|
|
(setter)Vector_axis_set,
|
|
|
|
|
Vector_axis_x_doc,
|
|
|
|
|
POINTER_FROM_INT(0)},
|
|
|
|
|
{"y",
|
|
|
|
|
(getter)Vector_axis_get,
|
|
|
|
|
(setter)Vector_axis_set,
|
|
|
|
|
Vector_axis_y_doc,
|
|
|
|
|
POINTER_FROM_INT(1)},
|
|
|
|
|
{"z",
|
|
|
|
|
(getter)Vector_axis_get,
|
|
|
|
|
(setter)Vector_axis_set,
|
|
|
|
|
Vector_axis_z_doc,
|
|
|
|
|
POINTER_FROM_INT(2)},
|
|
|
|
|
{"w",
|
|
|
|
|
(getter)Vector_axis_get,
|
|
|
|
|
(setter)Vector_axis_set,
|
|
|
|
|
Vector_axis_w_doc,
|
|
|
|
|
POINTER_FROM_INT(3)},
|
2023-07-21 02:18:59 +02:00
|
|
|
{"length", (getter)Vector_length_get, (setter)Vector_length_set, Vector_length_doc, nullptr},
|
2019-07-04 21:10:06 +10:00
|
|
|
{"length_squared",
|
2011-12-25 11:36:26 +00:00
|
|
|
(getter)Vector_length_squared_get,
|
2023-07-21 02:18:59 +02:00
|
|
|
(setter) nullptr,
|
2011-12-25 11:36:26 +00:00
|
|
|
Vector_length_squared_doc,
|
2023-07-21 02:18:59 +02:00
|
|
|
nullptr},
|
|
|
|
|
{"magnitude",
|
|
|
|
|
(getter)Vector_length_get,
|
|
|
|
|
(setter)Vector_length_set,
|
|
|
|
|
Vector_length_doc,
|
|
|
|
|
nullptr},
|
2019-07-04 21:10:06 +10:00
|
|
|
{"is_wrapped",
|
2011-12-25 11:36:26 +00:00
|
|
|
(getter)BaseMathObject_is_wrapped_get,
|
2023-07-21 02:18:59 +02:00
|
|
|
(setter) nullptr,
|
2011-12-25 11:36:26 +00:00
|
|
|
BaseMathObject_is_wrapped_doc,
|
2023-07-21 02:18:59 +02:00
|
|
|
nullptr},
|
2019-07-04 21:10:06 +10:00
|
|
|
{"is_frozen",
|
2015-02-15 14:10:46 +11:00
|
|
|
(getter)BaseMathObject_is_frozen_get,
|
2023-07-21 02:18:59 +02:00
|
|
|
(setter) nullptr,
|
2015-02-15 14:10:46 +11:00
|
|
|
BaseMathObject_is_frozen_doc,
|
2023-07-21 02:18:59 +02:00
|
|
|
nullptr},
|
2021-09-03 21:18:03 +10:00
|
|
|
{"is_valid",
|
|
|
|
|
(getter)BaseMathObject_is_valid_get,
|
2023-07-21 02:18:59 +02:00
|
|
|
(setter) nullptr,
|
2021-09-03 21:18:03 +10:00
|
|
|
BaseMathObject_is_valid_doc,
|
2023-07-21 02:18:59 +02:00
|
|
|
nullptr},
|
|
|
|
|
{"owner",
|
|
|
|
|
(getter)BaseMathObject_owner_get,
|
|
|
|
|
(setter) nullptr,
|
|
|
|
|
BaseMathObject_owner_doc,
|
|
|
|
|
nullptr},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-05 12:47:46 +10:00
|
|
|
/* Auto-generated swizzle attributes, see Python script above. */
|
2024-07-09 13:45:07 +10:00
|
|
|
VECTOR_SWIZZLE2_RO_DEF("xx", 0, 0),
|
|
|
|
|
VECTOR_SWIZZLE3_RO_DEF("xxx", 0, 0, 0),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxxx", 0, 0, 0, 0),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxxy", 0, 0, 0, 1),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxxz", 0, 0, 0, 2),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxxw", 0, 0, 0, 3),
|
|
|
|
|
VECTOR_SWIZZLE3_RO_DEF("xxy", 0, 0, 1),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxyx", 0, 0, 1, 0),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxyy", 0, 0, 1, 1),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxyz", 0, 0, 1, 2),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxyw", 0, 0, 1, 3),
|
|
|
|
|
VECTOR_SWIZZLE3_RO_DEF("xxz", 0, 0, 2),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxzx", 0, 0, 2, 0),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxzy", 0, 0, 2, 1),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("xxzz", 0, 0, 2, 2),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
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("wwzw", 3, 3, 2, 3),
|
|
|
|
|
VECTOR_SWIZZLE3_RO_DEF("www", 3, 3, 3),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("wwwx", 3, 3, 3, 0),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("wwwy", 3, 3, 3, 1),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("wwwz", 3, 3, 3, 2),
|
|
|
|
|
VECTOR_SWIZZLE4_RO_DEF("wwww", 3, 3, 3, 3),
|
2016-07-31 20:41:24 +10:00
|
|
|
|
|
|
|
|
#undef AXIS_FROM_CHAR
|
2017-09-09 09:58:19 +10:00
|
|
|
#undef SWIZZLE1
|
|
|
|
|
#undef SWIZZLE2
|
|
|
|
|
#undef SWIZZLE3
|
|
|
|
|
#undef SWIZZLE4
|
|
|
|
|
#undef _SWIZZLE1
|
|
|
|
|
#undef _SWIZZLE2
|
|
|
|
|
#undef _SWIZZLE3
|
|
|
|
|
#undef _SWIZZLE4
|
2016-07-31 20:41:24 +10:00
|
|
|
|
2024-07-09 13:45:07 +10:00
|
|
|
#undef VECTOR_SWIZZLE2_RW_DEF
|
|
|
|
|
#undef VECTOR_SWIZZLE2_RO_DEF
|
|
|
|
|
#undef VECTOR_SWIZZLE3_RW_DEF
|
|
|
|
|
#undef VECTOR_SWIZZLE3_RO_DEF
|
|
|
|
|
#undef VECTOR_SWIZZLE4_RW_DEF
|
|
|
|
|
#undef VECTOR_SWIZZLE4_RO_DEF
|
|
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
{nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
|
2009-06-17 20:33:34 +00:00
|
|
|
};
|
|
|
|
|
|
2025-04-01 12:06:03 +11:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# ifdef __clang__
|
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
|
# else
|
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
|
# endif
|
2023-07-21 13:42:35 +10:00
|
|
|
#endif
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
2011-02-05 06:14:50 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Method Definitions
|
|
|
|
|
* \{ */
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
|
2025-04-01 12:06:03 +11:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# ifdef __clang__
|
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
|
# pragma clang diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
# else
|
|
|
|
|
# pragma GCC diagnostic push
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
|
|
|
# endif
|
2023-07-21 13:42:35 +10:00
|
|
|
#endif
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
static PyMethodDef Vector_methods[] = {
|
2011-12-18 07:27:11 +00:00
|
|
|
/* Class Methods */
|
|
|
|
|
{"Fill", (PyCFunction)C_Vector_Fill, METH_VARARGS | METH_CLASS, C_Vector_Fill_doc},
|
|
|
|
|
{"Range", (PyCFunction)C_Vector_Range, METH_VARARGS | METH_CLASS, C_Vector_Range_doc},
|
|
|
|
|
{"Linspace", (PyCFunction)C_Vector_Linspace, METH_VARARGS | METH_CLASS, C_Vector_Linspace_doc},
|
|
|
|
|
{"Repeat", (PyCFunction)C_Vector_Repeat, METH_VARARGS | METH_CLASS, C_Vector_Repeat_doc},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:56:58 +10:00
|
|
|
/* In place only. */
|
2011-02-04 03:06:23 +00:00
|
|
|
{"zero", (PyCFunction)Vector_zero, METH_NOARGS, Vector_zero_doc},
|
|
|
|
|
{"negate", (PyCFunction)Vector_negate, METH_NOARGS, Vector_negate_doc},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:56:58 +10:00
|
|
|
/* Operate on original or copy. */
|
2011-02-05 06:14:50 +00:00
|
|
|
{"normalize", (PyCFunction)Vector_normalize, METH_NOARGS, Vector_normalize_doc},
|
|
|
|
|
{"normalized", (PyCFunction)Vector_normalized, METH_NOARGS, Vector_normalized_doc},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-12-18 07:27:11 +00:00
|
|
|
{"resize", (PyCFunction)Vector_resize, METH_O, Vector_resize_doc},
|
|
|
|
|
{"resized", (PyCFunction)Vector_resized, METH_O, Vector_resized_doc},
|
2011-02-05 06:14:50 +00:00
|
|
|
{"to_2d", (PyCFunction)Vector_to_2d, METH_NOARGS, Vector_to_2d_doc},
|
|
|
|
|
{"resize_2d", (PyCFunction)Vector_resize_2d, METH_NOARGS, Vector_resize_2d_doc},
|
|
|
|
|
{"to_3d", (PyCFunction)Vector_to_3d, METH_NOARGS, Vector_to_3d_doc},
|
|
|
|
|
{"resize_3d", (PyCFunction)Vector_resize_3d, METH_NOARGS, Vector_resize_3d_doc},
|
|
|
|
|
{"to_4d", (PyCFunction)Vector_to_4d, METH_NOARGS, Vector_to_4d_doc},
|
|
|
|
|
{"resize_4d", (PyCFunction)Vector_resize_4d, METH_NOARGS, Vector_resize_4d_doc},
|
2011-02-04 03:06:23 +00:00
|
|
|
{"to_tuple", (PyCFunction)Vector_to_tuple, METH_VARARGS, Vector_to_tuple_doc},
|
|
|
|
|
{"to_track_quat", (PyCFunction)Vector_to_track_quat, METH_VARARGS, Vector_to_track_quat_doc},
|
2014-03-18 00:37:43 +11:00
|
|
|
{"orthogonal", (PyCFunction)Vector_orthogonal, METH_NOARGS, Vector_orthogonal_doc},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:56:58 +10:00
|
|
|
/* Operation between 2 or more types. */
|
2011-02-04 03:06:23 +00:00
|
|
|
{"reflect", (PyCFunction)Vector_reflect, METH_O, Vector_reflect_doc},
|
|
|
|
|
{"cross", (PyCFunction)Vector_cross, METH_O, Vector_cross_doc},
|
|
|
|
|
{"dot", (PyCFunction)Vector_dot, METH_O, Vector_dot_doc},
|
|
|
|
|
{"angle", (PyCFunction)Vector_angle, METH_VARARGS, Vector_angle_doc},
|
2012-03-14 06:14:15 +00:00
|
|
|
{"angle_signed", (PyCFunction)Vector_angle_signed, METH_VARARGS, Vector_angle_signed_doc},
|
2011-04-04 05:17:23 +00:00
|
|
|
{"rotation_difference",
|
|
|
|
|
(PyCFunction)Vector_rotation_difference,
|
|
|
|
|
METH_O,
|
|
|
|
|
Vector_rotation_difference_doc},
|
2011-02-04 03:06:23 +00:00
|
|
|
{"project", (PyCFunction)Vector_project, METH_O, Vector_project_doc},
|
|
|
|
|
{"lerp", (PyCFunction)Vector_lerp, METH_VARARGS, Vector_lerp_doc},
|
2014-03-31 13:18:23 +11:00
|
|
|
{"slerp", (PyCFunction)Vector_slerp, METH_VARARGS, Vector_slerp_doc},
|
2011-02-05 09:57:02 +00:00
|
|
|
{"rotate", (PyCFunction)Vector_rotate, METH_O, Vector_rotate_doc},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:56:58 +10:00
|
|
|
/* Base-math methods. */
|
2015-02-15 11:26:31 +11:00
|
|
|
{"freeze", (PyCFunction)BaseMathObject_freeze, METH_NOARGS, BaseMathObject_freeze_doc},
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
{"copy", (PyCFunction)Vector_copy, METH_NOARGS, Vector_copy_doc},
|
2023-07-21 02:18:59 +02:00
|
|
|
{"__copy__", (PyCFunction)Vector_copy, METH_NOARGS, nullptr},
|
|
|
|
|
{"__deepcopy__", (PyCFunction)Vector_deepcopy, METH_VARARGS, nullptr},
|
|
|
|
|
{nullptr, nullptr, 0, nullptr},
|
Mathutils refactor & include in sphinx generated docs, (TODO, include getset'ers in docs)
- Mathutils.MidpointVecs --> vector.lerp(other, fac)
- Mathutils.AngleBetweenVecs --> vector.angle(other)
- Mathutils.ProjectVecs --> vector.project(other)
- Mathutils.DifferenceQuats --> quat.difference(other)
- Mathutils.Slerp --> quat.slerp(other, fac)
- Mathutils.Rand: removed, use pythons random module
- Mathutils.RotationMatrix(angle, size, axis_flag, axis) --> Mathutils.RotationMatrix(angle, size, axis); merge axis & axis_flag args
- Matrix.scalePart --> Matrix.scale_part
- Matrix.translationPart --> Matrix.translation_part
- Matrix.rotationPart --> Matrix.rotation_part
- toMatrix --> to_matrix
- toEuler --> to_euler
- toQuat --> to_quat
- Vector.toTrackQuat --> Vector.to_track_quat
2010-01-25 09:44:04 +00:00
|
|
|
};
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2025-04-01 12:06:03 +11:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# ifdef __clang__
|
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
|
# else
|
|
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
|
# endif
|
2023-07-21 13:42:35 +10:00
|
|
|
#endif
|
|
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: Python Object Definition
|
|
|
|
|
*
|
|
|
|
|
* \note #Py_TPFLAGS_CHECKTYPES allows us to avoid casting all types to Vector when coercing
|
2017-09-09 10:47:57 +10:00
|
|
|
* but this means for eg that (vec * mat) and (mat * vec)
|
|
|
|
|
* both get sent to Vector_mul and it needs to sort out the order
|
2022-05-25 12:33:15 +10:00
|
|
|
* \{ */
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-11-07 22:34:35 +11:00
|
|
|
#ifdef MATH_STANDALONE
|
2023-07-21 02:18:59 +02:00
|
|
|
# define Vector_str nullptr
|
2022-11-07 22:34:35 +11:00
|
|
|
#endif
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
vector_doc,
|
2025-07-20 17:18:26 +05:00
|
|
|
".. class:: Vector(seq=(0.0, 0.0, 0.0), /)\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
"\n"
|
|
|
|
|
" This object gives access to Vectors in Blender.\n"
|
|
|
|
|
"\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :arg seq: Components of the vector, must be a sequence of at least two.\n"
|
|
|
|
|
" :type seq: Sequence[float]\n");
|
2009-06-17 20:33:34 +00:00
|
|
|
PyTypeObject vector_Type = {
|
2023-07-21 02:18:59 +02:00
|
|
|
/*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_name*/ "Vector",
|
|
|
|
|
/*tp_basicsize*/ sizeof(VectorObject),
|
|
|
|
|
/*tp_itemsize*/ 0,
|
|
|
|
|
/*tp_dealloc*/ (destructor)BaseMathObject_dealloc,
|
|
|
|
|
/*tp_vectorcall_offset*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_getattr*/ nullptr,
|
|
|
|
|
/*tp_setattr*/ nullptr,
|
|
|
|
|
/*tp_as_async*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_repr*/ (reprfunc)Vector_repr,
|
|
|
|
|
/*tp_as_number*/ &Vector_NumMethods,
|
|
|
|
|
/*tp_as_sequence*/ &Vector_SeqMethods,
|
|
|
|
|
/*tp_as_mapping*/ &Vector_AsMapping,
|
|
|
|
|
/*tp_hash*/ (hashfunc)Vector_hash,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_call*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_str*/ (reprfunc)Vector_str,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_getattro*/ nullptr,
|
|
|
|
|
/*tp_setattro*/ nullptr,
|
2025-08-16 06:14:19 +00:00
|
|
|
/*tp_as_buffer*/ &Vector_as_buffer,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
|
|
|
|
|
/*tp_doc*/ vector_doc,
|
|
|
|
|
/*tp_traverse*/ (traverseproc)BaseMathObject_traverse,
|
|
|
|
|
/*tp_clear*/ (inquiry)BaseMathObject_clear,
|
|
|
|
|
/*tp_richcompare*/ (richcmpfunc)Vector_richcmpr,
|
|
|
|
|
/*tp_weaklistoffset*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_iter*/ nullptr,
|
|
|
|
|
/*tp_iternext*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_methods*/ Vector_methods,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_members*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_getset*/ Vector_getseters,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_base*/ nullptr,
|
|
|
|
|
/*tp_dict*/ nullptr,
|
|
|
|
|
/*tp_descr_get*/ nullptr,
|
|
|
|
|
/*tp_descr_set*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_dictoffset*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_init*/ nullptr,
|
|
|
|
|
/*tp_alloc*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_new*/ Vector_new,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_free*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_is_gc*/ (inquiry)BaseMathObject_is_gc,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_bases*/ nullptr,
|
|
|
|
|
/*tp_mro*/ nullptr,
|
|
|
|
|
/*tp_cache*/ nullptr,
|
|
|
|
|
/*tp_subclasses*/ nullptr,
|
|
|
|
|
/*tp_weaklist*/ nullptr,
|
|
|
|
|
/*tp_del*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
/*tp_version_tag*/ 0,
|
2023-07-21 02:18:59 +02:00
|
|
|
/*tp_finalize*/ nullptr,
|
|
|
|
|
/*tp_vectorcall*/ nullptr,
|
2022-11-07 22:34:35 +11:00
|
|
|
};
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-11-07 22:34:35 +11:00
|
|
|
#ifdef MATH_STANDALONE
|
2023-07-21 02:18:59 +02:00
|
|
|
# undef Vector_str nullptr
|
2012-12-08 01:16:59 +00:00
|
|
|
#endif
|
2009-06-17 20:33:34 +00:00
|
|
|
|
2022-05-25 12:33:15 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vector Type: C/API Constructors
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
PyObject *Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
|
2009-06-17 20:33:34 +00:00
|
|
|
{
|
2011-02-24 05:46:57 +00:00
|
|
|
VectorObject *self;
|
2015-01-04 17:03:54 +11:00
|
|
|
float *vec_alloc;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2011-07-14 01:25:05 +00:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2011-02-24 04:58:51 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
vec_alloc = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
|
|
|
|
|
if (UNLIKELY(vec_alloc == nullptr)) {
|
2015-01-04 17:03:54 +11:00
|
|
|
PyErr_SetString(PyExc_MemoryError,
|
|
|
|
|
"Vector(): "
|
|
|
|
|
"problem allocating data");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2015-01-04 17:03:54 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-04 17:03:54 +11:00
|
|
|
self = BASE_MATH_NEW(VectorObject, vector_Type, base_type);
|
2011-11-16 03:56:34 +00:00
|
|
|
if (self) {
|
2015-01-04 17:03:54 +11:00
|
|
|
self->vec = vec_alloc;
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num = vec_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
/* Initialize callbacks as nullptr. */
|
|
|
|
|
self->cb_user = nullptr;
|
2011-12-24 13:26:30 +00:00
|
|
|
self->cb_type = self->cb_subtype = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-04 17:03:54 +11:00
|
|
|
if (vec) {
|
2022-03-28 11:06:01 +11:00
|
|
|
memcpy(self->vec, vec, vec_num * sizeof(float));
|
2011-02-24 05:46:57 +00:00
|
|
|
}
|
2015-01-04 17:03:54 +11:00
|
|
|
else { /* new empty */
|
2022-03-28 11:06:01 +11:00
|
|
|
copy_vn_fl(self->vec, vec_num, 0.0f);
|
|
|
|
|
if (vec_num == 4) { /* do the homogeneous thing */
|
2015-01-04 17:03:54 +11:00
|
|
|
self->vec[3] = 1.0f;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2011-02-24 05:46:57 +00:00
|
|
|
}
|
2015-01-04 17:03:54 +11:00
|
|
|
self->flag = BASE_MATH_FLAG_DEFAULT;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
PyMem_Free(vec_alloc);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-04 17:03:54 +11:00
|
|
|
return (PyObject *)self;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
PyObject *Vector_CreatePyObject_wrap(float *vec, const int vec_num, PyTypeObject *base_type)
|
2015-01-04 17:03:54 +11:00
|
|
|
{
|
|
|
|
|
VectorObject *self;
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
if (vec_num < 2) {
|
2015-01-04 17:03:54 +11:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
|
2023-07-21 02:18:59 +02:00
|
|
|
return nullptr;
|
2015-01-04 17:03:54 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self = BASE_MATH_NEW(VectorObject, vector_Type, base_type);
|
|
|
|
|
if (self) {
|
2022-03-28 11:06:01 +11:00
|
|
|
self->vec_num = vec_num;
|
2015-01-04 17:03:54 +11:00
|
|
|
|
2023-07-21 02:18:59 +02:00
|
|
|
/* Initialize callbacks as nullptr. */
|
|
|
|
|
self->cb_user = nullptr;
|
2015-01-04 17:03:54 +11:00
|
|
|
self->cb_type = self->cb_subtype = 0;
|
|
|
|
|
|
|
|
|
|
self->vec = vec;
|
|
|
|
|
self->flag = BASE_MATH_FLAG_DEFAULT | BASE_MATH_FLAG_IS_WRAP;
|
2011-02-24 05:46:57 +00:00
|
|
|
}
|
|
|
|
|
return (PyObject *)self;
|
2009-06-17 20:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
PyObject *Vector_CreatePyObject_cb(PyObject *cb_user, int vec_num, uchar cb_type, uchar cb_subtype)
|
2009-06-22 04:26:48 +00:00
|
|
|
{
|
2023-07-21 02:18:59 +02:00
|
|
|
VectorObject *self = (VectorObject *)Vector_CreatePyObject(nullptr, vec_num, nullptr);
|
2011-11-16 03:56:34 +00:00
|
|
|
if (self) {
|
2011-02-24 05:46:57 +00:00
|
|
|
Py_INCREF(cb_user);
|
2012-03-17 10:46:02 +00:00
|
|
|
self->cb_user = cb_user;
|
|
|
|
|
self->cb_type = cb_type;
|
|
|
|
|
self->cb_subtype = cb_subtype;
|
2022-09-28 16:09:12 +10:00
|
|
|
BLI_assert(!PyObject_GC_IsTracked((PyObject *)self));
|
2011-03-03 06:01:31 +00:00
|
|
|
PyObject_GC_Track(self);
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2010-11-28 06:03:30 +00:00
|
|
|
|
2009-06-25 10:11:37 +00:00
|
|
|
return (PyObject *)self;
|
2009-06-22 04:26:48 +00:00
|
|
|
}
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2022-03-28 11:06:01 +11:00
|
|
|
PyObject *Vector_CreatePyObject_alloc(float *vec, const int vec_num, PyTypeObject *base_type)
|
2011-12-18 07:27:11 +00:00
|
|
|
{
|
2015-01-04 17:03:54 +11:00
|
|
|
VectorObject *self;
|
2022-03-28 11:06:01 +11:00
|
|
|
self = (VectorObject *)Vector_CreatePyObject_wrap(vec, vec_num, base_type);
|
2015-01-04 17:03:54 +11:00
|
|
|
if (self) {
|
2020-02-20 15:09:44 +11:00
|
|
|
self->flag &= ~BASE_MATH_FLAG_IS_WRAP;
|
2015-01-04 17:03:54 +11:00
|
|
|
}
|
2011-12-18 07:27:11 +00:00
|
|
|
|
2015-01-04 17:03:54 +11:00
|
|
|
return (PyObject *)self;
|
2011-12-18 07:27:11 +00:00
|
|
|
}
|
2022-05-25 12:33:15 +10:00
|
|
|
|
|
|
|
|
/** \} */
|