Blender V5.3
mathutils_Vector.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include <Python.h>
12
13#include "mathutils.hh"
14
15#include "BLI_math_base_safe.hh"
16#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector_c.hh"
19#include "BLI_utildefines.hh"
20
22
23#ifndef MATH_STANDALONE
24# include "BLI_dynstr.hh"
25#endif
26
27namespace blender {
28
34#define MAX_DIMENSIONS 4
35
41#define SWIZZLE_BITS_PER_AXIS 3
42#define SWIZZLE_VALID_AXIS 0x4
43#define SWIZZLE_AXIS 0x3
44
45static PyObject *Vector_copy(VectorObject *self);
46static PyObject *Vector_deepcopy(VectorObject *self, PyObject *args);
47
48/* -------------------------------------------------------------------- */
51
62 VectorObject *vec,
63 MatrixObject *mat)
64{
65 float vec_cpy[MAX_DIMENSIONS];
66 int row, col, z = 0, vec_num = vec->vec_num;
67
68 if (mat->row_num != vec_num) {
69 if (mat->row_num == 4 && vec_num == 3) {
70 vec_cpy[3] = 1.0f;
71 }
72 else {
73 PyErr_SetString(PyExc_ValueError,
74 "vector * matrix: matrix column size "
75 "and the vector size must be the same");
76 return -1;
77 }
78 }
79
80 if (BaseMath_ReadCallback(vec) == -1 || BaseMath_ReadCallback(mat) == -1) {
81 return -1;
82 }
83
84 memcpy(vec_cpy, vec->vec, vec_num * sizeof(float));
85
86 r_vec[3] = 1.0f;
87 /* Multiplication. */
88 for (col = 0; col < mat->col_num; col++) {
89 double dot = 0.0;
90 for (row = 0; row < mat->row_num; row++) {
91 dot += double(MATRIX_ITEM(mat, row, col) * vec_cpy[row]);
92 }
93 r_vec[z++] = float(dot);
94 }
95 return 0;
96}
97
98static PyObject *vec__apply_to_copy(PyObject *(*vec_func)(VectorObject *), VectorObject *self)
99{
100 PyObject *ret = Vector_copy(self);
101 PyObject *ret_dummy = vec_func(reinterpret_cast<VectorObject *>(ret));
102 if (ret_dummy) {
103 Py_DECREF(ret_dummy);
104 return ret;
105 }
106 /* error */
107 Py_DECREF(ret);
108 return nullptr;
109}
110
112static PyObject *Vector_to_tuple_ex(VectorObject *self, int ndigits)
113{
114 PyObject *ret;
115 int i;
116
117 ret = PyTuple_New(self->vec_num);
118
119 if (ndigits >= 0) {
120 for (i = 0; i < self->vec_num; i++) {
121 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(double_round(double(self->vec[i]), ndigits)));
122 }
123 }
124 else {
125 for (i = 0; i < self->vec_num; i++) {
126 PyTuple_SET_ITEM(ret, i, PyFloat_FromDouble(self->vec[i]));
127 }
128 }
129
130 return ret;
131}
132
134
135/* -------------------------------------------------------------------- */
138
143static PyObject *Vector_vectorcall(PyObject *type,
144 PyObject *const *args,
145 const size_t nargsf,
146 PyObject *kwnames)
147{
148 if (kwnames && PyTuple_GET_SIZE(kwnames)) [[unlikely]] {
149 PyErr_SetString(PyExc_TypeError,
150 "Vector(): "
151 "takes no keyword args");
152 return nullptr;
153 }
154
155 float *vec = nullptr;
156 int vec_num = 3; /* Default to a 3D vector. */
157
158 const size_t nargs = PyVectorcall_NARGS(nargsf);
159 switch (nargs) {
160 case 0: {
161 vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
162
163 if (vec == nullptr) {
164 PyErr_SetString(PyExc_MemoryError,
165 "Vector(): "
166 "problem allocating pointer space");
167 return nullptr;
168 }
169
170 std::fill_n(vec, vec_num, 0.0f);
171 break;
172 }
173 case 1: {
174 if ((vec_num = mathutils_array_parse_alloc(&vec, 2, args[0], "mathutils.Vector()")) == -1) {
175 return nullptr;
176 }
177 break;
178 }
179 default: {
180 PyErr_Format(PyExc_TypeError,
181 "mathutils.Vector(): "
182 "takes at most 1 argument (%zd given)",
183 nargs);
184 return nullptr;
185 }
186 }
187 return Vector_CreatePyObject_alloc(vec, vec_num, reinterpret_cast<PyTypeObject *>(type));
188}
189
190static PyObject *Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
191{
192 if (kwds && PyDict_GET_SIZE(kwds)) [[unlikely]] {
193 PyErr_SetString(PyExc_TypeError,
194 "Vector(): "
195 "takes no keyword args");
196 return nullptr;
197 }
198 PyObject *const *args_array = &PyTuple_GET_ITEM(args, 0);
199 const size_t args_array_num = PyTuple_GET_SIZE(args);
200 return Vector_vectorcall(
201 reinterpret_cast<PyObject *>(type), args_array, args_array_num, nullptr);
202}
203
205
206/* -------------------------------------------------------------------- */
209
211 /* Wrap. */
212 C_Vector_Fill_doc,
213 ".. classmethod:: Fill(size, fill=0.0, /)\n"
214 "\n"
215 " Create a vector of length size with all values set to fill.\n"
216 "\n"
217 " :param size: The length of the vector to be created.\n"
218 " :type size: int\n"
219 " :param fill: The value used to fill the vector.\n"
220 " :type fill: float\n"
221 " :return: A new vector.\n"
222 " :rtype: :class:`Vector`\n");
223static PyObject *C_Vector_Fill(PyObject *cls, PyObject *args)
224{
225 float *vec;
226 int vec_num;
227 float fill = 0.0f;
228
229 if (!PyArg_ParseTuple(args,
230 "i" /* `size` */
231 "|" /* Optional arguments. */
232 "f" /* `fill` */
233 ":Vector.Fill",
234 &vec_num,
235 &fill))
236 {
237 return nullptr;
238 }
239
240 if (vec_num < 2) {
241 PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
242 return nullptr;
243 }
244
245 vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
246
247 if (vec == nullptr) {
248 PyErr_SetString(PyExc_MemoryError,
249 "Vector.Fill(): "
250 "problem allocating pointer space");
251 return nullptr;
252 }
253
254 std::fill_n(vec, vec_num, fill);
255
256 return Vector_CreatePyObject_alloc(vec, vec_num, reinterpret_cast<PyTypeObject *>(cls));
257}
258
260 /* Wrap. */
261 C_Vector_Range_doc,
262 ".. classmethod:: Range(start, stop, step=1, /)\n"
263 "\n"
264 " Create a vector filled with a range of values.\n"
265 "\n"
266 " This method can also be called with a single argument, "
267 "in which case the argument is interpreted as ``stop`` and ``start`` defaults to 0.\n"
268 "\n"
269 " :param start: The start of the range used to fill the vector.\n"
270 " :type start: int\n"
271 " :param stop: The end of the range used to fill the vector.\n"
272 " :type stop: int\n"
273 " :param step: The step between successive values in the vector.\n"
274 " :type step: int\n"
275 " :return: A new vector.\n"
276 " :rtype: :class:`Vector`\n");
277static PyObject *C_Vector_Range(PyObject *cls, PyObject *args)
278{
279 float *vec;
280 int stop, vec_num;
281 int start = 0;
282 int step = 1;
283
284 if (!PyArg_ParseTuple(args,
285 "i" /* `start` */
286 "|" /* Optional arguments. */
287 "i" /* `stop` */
288 "i" /* `step` */
289 ":Vector.Range",
290 &start,
291 &stop,
292 &step))
293 {
294 return nullptr;
295 }
296
297 switch (PyTuple_GET_SIZE(args)) {
298 case 1: {
299 vec_num = start;
300 start = 0;
301 break;
302 }
303 case 2: {
304 if (start >= stop) {
305 PyErr_SetString(PyExc_RuntimeError,
306 "Start value is larger "
307 "than the stop value");
308 return nullptr;
309 }
310
311 vec_num = stop - start;
312 break;
313 }
314 default: {
315 if (start >= stop) {
316 PyErr_SetString(PyExc_RuntimeError,
317 "Start value is larger "
318 "than the stop value");
319 return nullptr;
320 }
321
322 vec_num = (stop - start);
323
324 if ((vec_num % step) != 0) {
325 vec_num += step;
326 }
327
328 vec_num /= step;
329
330 break;
331 }
332 }
333
334 if (vec_num < 2) {
335 PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
336 return nullptr;
337 }
338
339 vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
340
341 if (vec == nullptr) {
342 PyErr_SetString(PyExc_MemoryError,
343 "Vector.Range(): "
344 "problem allocating pointer space");
345 return nullptr;
346 }
347
348 range_vn_fl(vec, vec_num, float(start), float(step));
349
350 return Vector_CreatePyObject_alloc(vec, vec_num, reinterpret_cast<PyTypeObject *>(cls));
351}
352
354 /* Wrap. */
355 C_Vector_Linspace_doc,
356 ".. classmethod:: Linspace(start, stop, size, /)\n"
357 "\n"
358 " Create a vector of the specified size which is filled with linearly spaced "
359 "values between start and stop values.\n"
360 "\n"
361 " :param start: The start of the range used to fill the vector.\n"
362 " :type start: float\n"
363 " :param stop: The end of the range used to fill the vector.\n"
364 " :type stop: float\n"
365 " :param size: The size of the vector to be created.\n"
366 " :type size: int\n"
367 " :return: A new vector.\n"
368 " :rtype: :class:`Vector`\n");
369static PyObject *C_Vector_Linspace(PyObject *cls, PyObject *args)
370{
371 float *vec;
372 int vec_num;
373 float start, end, step;
374
375 if (!PyArg_ParseTuple(args,
376 "f" /* `start` */
377 "f" /* `stop` */
378 "i" /* `size` */
379 ":Vector.Linspace",
380 &start,
381 &end,
382 &vec_num))
383 {
384 return nullptr;
385 }
386
387 if (vec_num < 2) {
388 PyErr_SetString(PyExc_RuntimeError, "Vector.Linspace(): invalid size");
389 return nullptr;
390 }
391
392 step = (end - start) / float(vec_num - 1);
393
394 vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
395
396 if (vec == nullptr) {
397 PyErr_SetString(PyExc_MemoryError,
398 "Vector.Linspace(): "
399 "problem allocating pointer space");
400 return nullptr;
401 }
402
403 range_vn_fl(vec, vec_num, start, step);
404
405 return Vector_CreatePyObject_alloc(vec, vec_num, reinterpret_cast<PyTypeObject *>(cls));
406}
407
409 /* Wrap. */
410 C_Vector_Repeat_doc,
411 ".. classmethod:: Repeat(vector, size, /)\n"
412 "\n"
413 " Create a vector by repeating the values in vector until the required size is reached.\n"
414 "\n"
415 " :param vector: The vector to draw values from.\n"
416 " :type vector: :class:`Vector`\n"
417 " :param size: The size of the vector to be created.\n"
418 " :type size: int\n"
419 " :return: A new vector.\n"
420 " :rtype: :class:`Vector`\n");
421static PyObject *C_Vector_Repeat(PyObject *cls, PyObject *args)
422{
423 float *vec;
424 float *iter_vec = nullptr;
425 int i, vec_num, value_num;
426 PyObject *value;
427
428 if (!PyArg_ParseTuple(args,
429 "O" /* `vector` */
430 "i" /* `size` */
431 ":Vector.Repeat",
432 &value,
433 &vec_num))
434 {
435 return nullptr;
436 }
437
438 if (vec_num < 2) {
439 PyErr_SetString(PyExc_RuntimeError, "Vector.Repeat(): invalid vec_num");
440 return nullptr;
441 }
442
443 if ((value_num = mathutils_array_parse_alloc(
444 &iter_vec, 2, value, "Vector.Repeat(vector, vec_num), invalid 'vector' arg")) == -1)
445 {
446 return nullptr;
447 }
448
449 if (iter_vec == nullptr) {
450 PyErr_SetString(PyExc_MemoryError,
451 "Vector.Repeat(): "
452 "problem allocating pointer space");
453 return nullptr;
454 }
455
456 vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
457
458 if (vec == nullptr) {
459 PyMem_Free(iter_vec);
460 PyErr_SetString(PyExc_MemoryError,
461 "Vector.Repeat(): "
462 "problem allocating pointer space");
463 return nullptr;
464 }
465
466 i = 0;
467 while (i < vec_num) {
468 vec[i] = iter_vec[i % value_num];
469 i++;
470 }
471
472 PyMem_Free(iter_vec);
473
474 return Vector_CreatePyObject_alloc(vec, vec_num, reinterpret_cast<PyTypeObject *>(cls));
475}
476
478
479/* -------------------------------------------------------------------- */
482
484 /* Wrap. */
485 Vector_zero_doc,
486 ".. method:: zero()\n"
487 "\n"
488 " Set all values to zero.\n");
489static PyObject *Vector_zero(VectorObject *self)
490{
491 if (BaseMath_Prepare_ForWrite(self) == -1) {
492 return nullptr;
493 }
494
495 std::fill_n(self->vec, self->vec_num, 0.0f);
496
497 if (BaseMath_WriteCallback(self) == -1) {
498 return nullptr;
499 }
500
501 Py_RETURN_NONE;
502}
503
505
506/* -------------------------------------------------------------------- */
509
511 /* Wrap. */
512 Vector_normalize_doc,
513 ".. method:: normalize()\n"
514 "\n"
515 " Normalize the vector, making the length of the vector always 1.0.\n"
516 "\n"
517 " .. warning:: Normalizing a vector where all values are zero has no effect.\n"
518 "\n"
519 " .. note:: For 4D vectors, only the x, y, z components are normalized;\n"
520 " the w component is left untouched.\n"
521 " The resulting 4D vector may not have unit length.\n");
522static PyObject *Vector_normalize(VectorObject *self)
523{
524 const int vec_num = (self->vec_num == 4 ? 3 : self->vec_num);
525 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
526 return nullptr;
527 }
528
529 normalize_vn(self->vec, vec_num);
530
531 (void)BaseMath_WriteCallback(self);
532 Py_RETURN_NONE;
533}
535 /* Wrap. */
536 Vector_normalized_doc,
537 ".. method:: normalized()\n"
538 "\n"
539 " Return a new, normalized vector.\n"
540 "\n"
541 " .. note:: For 4D vectors, only the x, y, z components are normalized;\n"
542 " the w component is left untouched.\n"
543 " The resulting 4D vector may not have unit length.\n"
544 "\n"
545 " :return: a normalized copy of the vector\n"
546 " :rtype: :class:`Vector`\n");
547static PyObject *Vector_normalized(VectorObject *self)
548{
550}
551
553
554/* -------------------------------------------------------------------- */
557
559 /* Wrap. */
560 Vector_resize_doc,
561 ".. method:: resize(size, /)\n"
562 "\n"
563 " Resize the vector to have size number of elements.\n"
564 "\n"
565 " :param size: The new size of the vector.\n"
566 " :type size: int\n");
567static PyObject *Vector_resize(VectorObject *self, PyObject *value)
568{
569 int vec_num;
570
571 if (BaseMathObject_Prepare_ForResize(self, "Vector.resize()") == -1) [[unlikely]] {
572 /* An exception has been raised. */
573
574 return nullptr;
575 }
576
577 if (((vec_num = PyC_Long_AsI32(value)) == -1) && PyErr_Occurred()) {
578 PyErr_SetString(PyExc_TypeError,
579 "Vector.resize(size): "
580 "expected size argument to be an integer");
581 return nullptr;
582 }
583
584 if (vec_num < 2) {
585 PyErr_SetString(PyExc_RuntimeError, "Vector.resize(): invalid size");
586 return nullptr;
587 }
588
589 self->vec = static_cast<float *>(PyMem_Realloc(self->vec, (vec_num * sizeof(float))));
590 if (self->vec == nullptr) {
591 PyErr_SetString(PyExc_MemoryError,
592 "Vector.resize(): "
593 "problem allocating pointer space");
594 return nullptr;
595 }
596
597 /* If the vector has increased in length, set all new elements to 0.0f */
598 if (vec_num > self->vec_num) {
599 std::fill_n(self->vec + self->vec_num, vec_num - self->vec_num, 0.0f);
600 }
601
602 self->vec_num = vec_num;
603 Py_RETURN_NONE;
604}
605
607 /* Wrap. */
608 Vector_resized_doc,
609 ".. method:: resized(size, /)\n"
610 "\n"
611 " Return a resized copy of the vector with size number of elements.\n"
612 "\n"
613 " :param size: The new size of the vector.\n"
614 " :type size: int\n"
615 " :return: A new vector.\n"
616 " :rtype: :class:`Vector`\n");
617static PyObject *Vector_resized(VectorObject *self, PyObject *value)
618{
619 int vec_num;
620 float *vec;
621
622 if (((vec_num = PyC_Long_AsI32(value)) == -1) && PyErr_Occurred()) {
623 return nullptr;
624 }
625
626 if (vec_num < 2) {
627 PyErr_SetString(PyExc_RuntimeError, "Vector.resized(): invalid size");
628 return nullptr;
629 }
630
631 if (BaseMath_ReadCallback(self) == -1) {
632 return nullptr;
633 }
634
635 vec = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
636
637 if (vec == nullptr) {
638 PyErr_SetString(PyExc_MemoryError,
639 "Vector.resized(): "
640 "problem allocating pointer space");
641 return nullptr;
642 }
643
644 const int vec_num_copy = std::min(self->vec_num, vec_num);
645 memcpy(vec, self->vec, vec_num_copy * sizeof(float));
646 if (vec_num_copy != vec_num) {
647 std::fill_n(vec + vec_num_copy, vec_num - vec_num_copy, 0.0f);
648 }
649
650 return Vector_CreatePyObject_alloc(vec, vec_num, nullptr);
651}
652
654 /* Wrap. */
655 Vector_resize_2d_doc,
656 ".. method:: resize_2d()\n"
657 "\n"
658 " Resize the vector to 2D (x, y).\n");
659static PyObject *Vector_resize_2d(VectorObject *self)
660{
661 if (BaseMathObject_Prepare_ForResize(self, "Vector.resize_2d()") == -1) [[unlikely]] {
662 /* An exception has been raised. */
663 return nullptr;
664 }
665
666 self->vec = static_cast<float *>(PyMem_Realloc(self->vec, sizeof(float[2])));
667 if (self->vec == nullptr) {
668 PyErr_SetString(PyExc_MemoryError,
669 "Vector.resize_2d(): "
670 "problem allocating pointer space");
671 return nullptr;
672 }
673
674 self->vec_num = 2;
675 Py_RETURN_NONE;
676}
677
679 /* Wrap. */
680 Vector_resize_3d_doc,
681 ".. method:: resize_3d()\n"
682 "\n"
683 " Resize the vector to 3D (x, y, z).\n");
684static PyObject *Vector_resize_3d(VectorObject *self)
685{
686 if (BaseMathObject_Prepare_ForResize(self, "Vector.resize_3d()") == -1) [[unlikely]] {
687 /* An exception has been raised. */
688 return nullptr;
689 }
690
691 self->vec = static_cast<float *>(PyMem_Realloc(self->vec, sizeof(float[3])));
692 if (self->vec == nullptr) {
693 PyErr_SetString(PyExc_MemoryError,
694 "Vector.resize_3d(): "
695 "problem allocating pointer space");
696 return nullptr;
697 }
698
699 if (self->vec_num == 2) {
700 self->vec[2] = 0.0f;
701 }
702
703 self->vec_num = 3;
704 Py_RETURN_NONE;
705}
706
708 /* Wrap. */
709 Vector_resize_4d_doc,
710 ".. method:: resize_4d()\n"
711 "\n"
712 " Resize the vector to 4D (x, y, z, w).\n");
713static PyObject *Vector_resize_4d(VectorObject *self)
714{
715 if (BaseMathObject_Prepare_ForResize(self, "Vector.resize_4d()") == -1) [[unlikely]] {
716 /* An exception has been raised. */
717 return nullptr;
718 }
719
720 self->vec = static_cast<float *>(PyMem_Realloc(self->vec, sizeof(float[4])));
721 if (self->vec == nullptr) {
722 PyErr_SetString(PyExc_MemoryError,
723 "Vector.resize_4d(): "
724 "problem allocating pointer space");
725 return nullptr;
726 }
727
728 if (self->vec_num == 2) {
729 self->vec[2] = 0.0f;
730 self->vec[3] = 1.0f;
731 }
732 else if (self->vec_num == 3) {
733 self->vec[3] = 1.0f;
734 }
735 self->vec_num = 4;
736 Py_RETURN_NONE;
737}
738
740
741/* -------------------------------------------------------------------- */
744
746 /* Wrap. */
747 Vector_to_2d_doc,
748 ".. method:: to_2d()\n"
749 "\n"
750 " Return a 2d copy of the vector.\n"
751 "\n"
752 " :return: a new vector\n"
753 " :rtype: :class:`Vector`\n");
754static PyObject *Vector_to_2d(VectorObject *self)
755{
756 if (BaseMath_ReadCallback(self) == -1) {
757 return nullptr;
758 }
759
760 return Vector_CreatePyObject(self->vec, 2, Py_TYPE(self));
761}
763 /* Wrap. */
764 Vector_to_3d_doc,
765 ".. method:: to_3d()\n"
766 "\n"
767 " Return a 3d copy of the vector.\n"
768 "\n"
769 " :return: a new vector\n"
770 " :rtype: :class:`Vector`\n");
771static PyObject *Vector_to_3d(VectorObject *self)
772{
773 float tvec[3] = {0.0f};
774
775 if (BaseMath_ReadCallback(self) == -1) {
776 return nullptr;
777 }
778
779 memcpy(tvec, self->vec, sizeof(float) * std::min(self->vec_num, 3));
780 return Vector_CreatePyObject(tvec, 3, Py_TYPE(self));
781}
783 /* Wrap. */
784 Vector_to_4d_doc,
785 ".. method:: to_4d()\n"
786 "\n"
787 " Return a 4d copy of the vector.\n"
788 "\n"
789 " :return: a new vector\n"
790 " :rtype: :class:`Vector`\n");
791static PyObject *Vector_to_4d(VectorObject *self)
792{
793 float tvec[4] = {0.0f, 0.0f, 0.0f, 1.0f};
794
795 if (BaseMath_ReadCallback(self) == -1) {
796 return nullptr;
797 }
798
799 memcpy(tvec, self->vec, sizeof(float) * std::min(self->vec_num, 4));
800 return Vector_CreatePyObject(tvec, 4, Py_TYPE(self));
801}
802
804
805/* -------------------------------------------------------------------- */
808
810 /* Wrap. */
811 Vector_to_tuple_doc,
812 ".. method:: to_tuple(precision=-1, /)\n"
813 "\n"
814 " Return this vector as a tuple with a given precision.\n"
815 "\n"
816 " :param precision: The number to round the value to in [-1, 21].\n"
817 " :type precision: int\n"
818 " :return: the values of the vector rounded by *precision*\n"
819 " :rtype: tuple[float, ...]\n");
820static PyObject *Vector_to_tuple(VectorObject *self, PyObject *args)
821{
822 int ndigits = -1;
823
824 if (!PyArg_ParseTuple(args,
825 "|" /* Optional arguments. */
826 "i" /* `precision` */
827 ":to_tuple",
828 &ndigits))
829 {
830 return nullptr;
831 }
832
833 if (ndigits > 22 || ndigits < -1) {
834 PyErr_SetString(PyExc_ValueError,
835 "Vector.to_tuple(precision): "
836 "precision must be between -1 and 21");
837 return nullptr;
838 }
839
840 if (BaseMath_ReadCallback(self) == -1) {
841 return nullptr;
842 }
843
844 return Vector_to_tuple_ex(self, ndigits);
845}
846
848
849/* -------------------------------------------------------------------- */
852
854 /* Wrap. */
855 Vector_to_track_quat_doc,
856 ".. method:: to_track_quat(track='Z', up='Y', /)\n"
857 "\n"
858 " Return a quaternion rotation from the vector and the track and up axis.\n"
859 "\n"
860 " :param track: Track axis string.\n"
861 " :type track: Literal['X', 'Y', 'Z', '-X', '-Y', '-Z']\n"
862 " :param up: Up axis string.\n"
863 " :type up: Literal['X', 'Y', 'Z']\n"
864 " :return: rotation from the vector and the track and up axis.\n"
865 " :rtype: :class:`Quaternion`\n");
866static PyObject *Vector_to_track_quat(VectorObject *self, PyObject *args)
867{
868 float vec[3], quat[4];
869 const char *strack = nullptr;
870 const char *sup = nullptr;
871 short track = 2, up = 1;
872
873 if (!PyArg_ParseTuple(args,
874 "|" /* Optional arguments. */
875 "s" /* `track` */
876 "s" /* `up` */
877 ":to_track_quat",
878 &strack,
879 &sup))
880 {
881 return nullptr;
882 }
883
884 if (self->vec_num != 3) {
885 PyErr_SetString(PyExc_TypeError,
886 "Vector.to_track_quat(): "
887 "only for 3D vectors");
888 return nullptr;
889 }
890
891 if (BaseMath_ReadCallback(self) == -1) {
892 return nullptr;
893 }
894
895 if (strack) {
896 const char *axis_err_msg = "only X, -X, Y, -Y, Z or -Z for track axis";
897
898 if (strlen(strack) == 2) {
899 if (strack[0] == '-') {
900 switch (strack[1]) {
901 case 'X': {
902 track = 3;
903 break;
904 }
905 case 'Y': {
906 track = 4;
907 break;
908 }
909 case 'Z': {
910 track = 5;
911 break;
912 }
913 default: {
914 PyErr_SetString(PyExc_ValueError, axis_err_msg);
915 return nullptr;
916 }
917 }
918 }
919 else {
920 PyErr_SetString(PyExc_ValueError, axis_err_msg);
921 return nullptr;
922 }
923 }
924 else if (strlen(strack) == 1) {
925 switch (strack[0]) {
926 case '-':
927 case 'X': {
928 track = 0;
929 break;
930 }
931 case 'Y': {
932 track = 1;
933 break;
934 }
935 case 'Z': {
936 track = 2;
937 break;
938 }
939 default: {
940 PyErr_SetString(PyExc_ValueError, axis_err_msg);
941 return nullptr;
942 }
943 }
944 }
945 else {
946 PyErr_SetString(PyExc_ValueError, axis_err_msg);
947 return nullptr;
948 }
949 }
950
951 if (sup) {
952 const char *axis_err_msg = "only X, Y or Z for up axis";
953 if (strlen(sup) == 1) {
954 switch (*sup) {
955 case 'X': {
956 up = 0;
957 break;
958 }
959 case 'Y': {
960 up = 1;
961 break;
962 }
963 case 'Z': {
964 up = 2;
965 break;
966 }
967 default: {
968 PyErr_SetString(PyExc_ValueError, axis_err_msg);
969 return nullptr;
970 }
971 }
972 }
973 else {
974 PyErr_SetString(PyExc_ValueError, axis_err_msg);
975 return nullptr;
976 }
977 }
978
979 if (track == up) {
980 PyErr_SetString(PyExc_ValueError, "Can't have the same axis for track and up");
981 return nullptr;
982 }
983
984 /* Flip vector around, since #vec_to_quat expect a vector from target to tracking object
985 * and the python function expects the inverse (a vector to the target). */
986 negate_v3_v3(vec, self->vec);
987
988 vec_to_quat(quat, vec, track, up);
989
990 return Quaternion_CreatePyObject(quat, nullptr);
991}
992
994
995/* -------------------------------------------------------------------- */
998
1000 /* Wrap. */
1001 Vector_orthogonal_doc,
1002 ".. method:: orthogonal()\n"
1003 "\n"
1004 " Return a perpendicular vector.\n"
1005 "\n"
1006 " :return: a new vector 90 degrees from this vector.\n"
1007 " :rtype: :class:`Vector`\n"
1008 "\n"
1009 " .. note:: the axis is undefined, only use when any orthogonal vector is acceptable.\n");
1010static PyObject *Vector_orthogonal(VectorObject *self)
1011{
1012 float vec[3];
1013
1014 if (self->vec_num > 3) {
1015 PyErr_SetString(PyExc_TypeError,
1016 "Vector.orthogonal(): "
1017 "Vector must be 3D or 2D");
1018 return nullptr;
1019 }
1020
1021 if (BaseMath_ReadCallback(self) == -1) {
1022 return nullptr;
1023 }
1024
1025 if (self->vec_num == 3) {
1026 ortho_v3_v3(vec, self->vec);
1027 }
1028 else {
1029 ortho_v2_v2(vec, self->vec);
1030 }
1031
1032 return Vector_CreatePyObject(vec, self->vec_num, Py_TYPE(self));
1033}
1034
1036
1037/* -------------------------------------------------------------------- */
1043
1045 /* Wrap. */
1046 Vector_reflect_doc,
1047 ".. method:: reflect(mirror, /)\n"
1048 "\n"
1049 " Return the reflection vector from the *mirror* argument.\n"
1050 "\n"
1051 " :param mirror: This vector could be a normal from the reflecting surface.\n"
1052 " :type mirror: :class:`Vector`\n"
1053 " :return: The reflected vector matching the size of this vector.\n"
1054 " :rtype: :class:`Vector`\n");
1055static PyObject *Vector_reflect(VectorObject *self, PyObject *value)
1056{
1057 int value_num;
1058 float mirror[3], vec[3];
1059 float reflect[3] = {0.0f};
1060 float tvec[MAX_DIMENSIONS];
1061
1062 if (BaseMath_ReadCallback(self) == -1) {
1063 return nullptr;
1064 }
1065
1066 if ((value_num = mathutils_array_parse(
1067 tvec, 2, 4, value, "Vector.reflect(other), invalid 'other' arg")) == -1)
1068 {
1069 return nullptr;
1070 }
1071
1072 if (self->vec_num < 2 || self->vec_num > 4) {
1073 PyErr_SetString(PyExc_ValueError, "Vector must be 2D, 3D or 4D");
1074 return nullptr;
1075 }
1076
1077 mirror[0] = tvec[0];
1078 mirror[1] = tvec[1];
1079 mirror[2] = (value_num > 2) ? tvec[2] : 0.0f;
1080
1081 vec[0] = self->vec[0];
1082 vec[1] = self->vec[1];
1083 vec[2] = (value_num > 2) ? self->vec[2] : 0.0f;
1084
1085 normalize_v3(mirror);
1086 reflect_v3_v3v3(reflect, vec, mirror);
1087
1088 return Vector_CreatePyObject(reflect, self->vec_num, Py_TYPE(self));
1089}
1090
1092
1093/* -------------------------------------------------------------------- */
1096
1098 /* Wrap. */
1099 Vector_cross_doc,
1100 ".. method:: cross(other, /)\n"
1101 "\n"
1102 " Return the cross product of this vector and another.\n"
1103 "\n"
1104 " :param other: The other vector to perform the cross product with.\n"
1105 " :type other: :class:`Vector`\n"
1106 " :return: The cross product as a vector or a float when 2D vectors are used.\n"
1107 " :rtype: :class:`Vector` | float\n"
1108 "\n"
1109 " .. note:: both vectors must be 2D or 3D\n");
1110static PyObject *Vector_cross(VectorObject *self, PyObject *value)
1111{
1112 PyObject *ret;
1113 float tvec[3];
1114
1115 if (BaseMath_ReadCallback(self) == -1) {
1116 return nullptr;
1117 }
1118
1119 if (self->vec_num > 3) {
1120 PyErr_SetString(PyExc_ValueError, "Vector must be 2D or 3D");
1121 return nullptr;
1122 }
1123
1125 tvec, self->vec_num, self->vec_num, value, "Vector.cross(other), invalid 'other' arg") ==
1126 -1)
1127 {
1128 return nullptr;
1129 }
1130
1131 if (self->vec_num == 3) {
1132 ret = Vector_CreatePyObject(nullptr, 3, Py_TYPE(self));
1133 cross_v3_v3v3((reinterpret_cast<VectorObject *>(ret))->vec, self->vec, tvec);
1134 }
1135 else {
1136 /* size == 2 */
1137 ret = PyFloat_FromDouble(cross_v2v2(self->vec, tvec));
1138 }
1139 return ret;
1140}
1141
1143
1144/* -------------------------------------------------------------------- */
1147
1149 /* Wrap. */
1150 Vector_dot_doc,
1151 ".. method:: dot(other, /)\n"
1152 "\n"
1153 " Return the dot product of this vector and another.\n"
1154 "\n"
1155 " :param other: The other vector to perform the dot product with.\n"
1156 " :type other: :class:`Vector`\n"
1157 " :return: The dot product.\n"
1158 " :rtype: float\n");
1159static PyObject *Vector_dot(VectorObject *self, PyObject *value)
1160{
1161 float *tvec;
1162 PyObject *ret;
1163
1164 if (BaseMath_ReadCallback(self) == -1) {
1165 return nullptr;
1166 }
1167
1169 &tvec, self->vec_num, value, "Vector.dot(other), invalid 'other' arg") == -1)
1170 {
1171 return nullptr;
1172 }
1173
1174 ret = PyFloat_FromDouble(dot_vn_vn(self->vec, tvec, self->vec_num));
1175 PyMem_Free(tvec);
1176 return ret;
1177}
1178
1180
1181/* -------------------------------------------------------------------- */
1184
1186 /* Wrap. */
1187 Vector_angle_doc,
1188 ".. method:: angle(other, fallback=None, /)\n"
1189 "\n"
1190 " Return the angle between two vectors.\n"
1191 "\n"
1192 " .. note:: For 4D vectors, only the x, y, z components are used.\n"
1193 "\n"
1194 " :param other: another vector to compare the angle with\n"
1195 " :type other: :class:`Vector`\n"
1196 " :param fallback: return this when the angle can't be calculated (zero length vector),\n"
1197 " (instead of raising a :exc:`ValueError`).\n"
1198 " :type fallback: Any\n"
1199 " :return: angle in radians or fallback when given\n"
1200 " :rtype: float | Any\n");
1201static PyObject *Vector_angle(VectorObject *self, PyObject *args)
1202{
1203 const int vec_num = std::min(self->vec_num, 3); /* 4D angle makes no sense */
1204 float tvec[MAX_DIMENSIONS];
1205 PyObject *value;
1206 double dot = 0.0f, dot_self = 0.0f, dot_other = 0.0f;
1207 int x;
1208 PyObject *fallback = nullptr;
1209
1210 if (!PyArg_ParseTuple(args,
1211 "O" /* `other` */
1212 "|" /* Optional arguments. */
1213 "O" /* `fallback` */
1214 ":angle",
1215 &value,
1216 &fallback))
1217 {
1218 return nullptr;
1219 }
1220
1221 if (BaseMath_ReadCallback(self) == -1) {
1222 return nullptr;
1223 }
1224
1225 if (self->vec_num > 4) {
1226 PyErr_SetString(PyExc_ValueError, "Vector must be 2D, 3D or 4D");
1227 return nullptr;
1228 }
1229
1230 /* don't use clamped size, rule of thumb is vector sizes must match,
1231 * even though n this case 'w' is ignored */
1233 tvec, self->vec_num, self->vec_num, value, "Vector.angle(other), invalid 'other' arg") ==
1234 -1)
1235 {
1236 return nullptr;
1237 }
1238
1239 for (x = 0; x < vec_num; x++) {
1240 dot_self += double(self->vec[x]) * double(self->vec[x]);
1241 dot_other += double(tvec[x]) * double(tvec[x]);
1242 dot += double(self->vec[x]) * double(tvec[x]);
1243 }
1244
1245 if (!dot_self || !dot_other) {
1246 /* avoid exception */
1247 if (fallback) {
1248 Py_INCREF(fallback);
1249 return fallback;
1250 }
1251
1252 PyErr_SetString(PyExc_ValueError,
1253 "Vector.angle(other): "
1254 "zero length vectors have no valid angle");
1255 return nullptr;
1256 }
1257
1258 return PyFloat_FromDouble(safe_acosf(dot / (sqrt(dot_self) * sqrt(dot_other))));
1259}
1260
1262
1263/* -------------------------------------------------------------------- */
1266
1268 /* Wrap. */
1269 Vector_angle_signed_doc,
1270 ".. method:: angle_signed(other, fallback=None, /)\n"
1271 "\n"
1272 " Return the signed angle between two 2D vectors (clockwise is positive).\n"
1273 "\n"
1274 " :param other: another vector to compare the angle with\n"
1275 " :type other: :class:`Vector`\n"
1276 " :param fallback: return this when the angle can't be calculated (zero length vector),\n"
1277 " (instead of raising a :exc:`ValueError`).\n"
1278 " :type fallback: Any\n"
1279 " :return: angle in radians or fallback when given\n"
1280 " :rtype: float | Any\n");
1281static PyObject *Vector_angle_signed(VectorObject *self, PyObject *args)
1282{
1283 float tvec[2];
1284
1285 PyObject *value;
1286 PyObject *fallback = nullptr;
1287
1288 if (!PyArg_ParseTuple(args,
1289 "O" /* `other` */
1290 "|" /* Optional arguments. */
1291 "O" /* `fallback` */
1292 ":angle_signed",
1293 &value,
1294 &fallback))
1295 {
1296 return nullptr;
1297 }
1298
1299 if (BaseMath_ReadCallback(self) == -1) {
1300 return nullptr;
1301 }
1302
1304 tvec, 2, 2, value, "Vector.angle_signed(other), invalid 'other' arg") == -1)
1305 {
1306 return nullptr;
1307 }
1308
1309 if (self->vec_num != 2) {
1310 PyErr_SetString(PyExc_ValueError, "Vector must be 2D");
1311 return nullptr;
1312 }
1313
1314 if (is_zero_v2(self->vec) || is_zero_v2(tvec)) {
1315 /* avoid exception */
1316 if (fallback) {
1317 Py_INCREF(fallback);
1318 return fallback;
1319 }
1320
1321 PyErr_SetString(PyExc_ValueError,
1322 "Vector.angle_signed(other): "
1323 "zero length vectors have no valid angle");
1324 return nullptr;
1325 }
1326
1327 return PyFloat_FromDouble(angle_signed_v2v2(self->vec, tvec));
1328}
1329
1331
1332/* -------------------------------------------------------------------- */
1335
1337 /* Wrap. */
1338 Vector_rotation_difference_doc,
1339 ".. method:: rotation_difference(other, /)\n"
1340 "\n"
1341 " Returns a quaternion representing the rotational difference between this\n"
1342 " vector and another.\n"
1343 "\n"
1344 " :param other: second vector.\n"
1345 " :type other: :class:`Vector`\n"
1346 " :return: the rotational difference between the two vectors.\n"
1347 " :rtype: :class:`Quaternion`\n"
1348 "\n"
1349 " .. note:: 2D vectors raise an :exc:`AttributeError`.\n");
1350static PyObject *Vector_rotation_difference(VectorObject *self, PyObject *value)
1351{
1352 float quat[4], vec_a[3], vec_b[MAX_DIMENSIONS];
1353
1354 if (self->vec_num < 3 || self->vec_num > 4) {
1355 PyErr_SetString(PyExc_ValueError,
1356 "vec.difference(value): "
1357 "expects both vectors to be size 3 or 4");
1358 return nullptr;
1359 }
1360
1361 if (BaseMath_ReadCallback(self) == -1) {
1362 return nullptr;
1363 }
1364
1366 vec_b, 3, MAX_DIMENSIONS, value, "Vector.difference(other), invalid 'other' arg") == -1)
1367 {
1368 return nullptr;
1369 }
1370
1371 normalize_v3_v3(vec_a, self->vec);
1372 normalize_v3(vec_b);
1373
1374 rotation_between_vecs_to_quat(quat, vec_a, vec_b);
1375
1376 return Quaternion_CreatePyObject(quat, nullptr);
1377}
1378
1380
1381/* -------------------------------------------------------------------- */
1384
1386 /* Wrap. */
1387 Vector_project_doc,
1388 ".. method:: project(other, /)\n"
1389 "\n"
1390 " Return the projection of this vector onto the *other*.\n"
1391 "\n"
1392 " :param other: second vector.\n"
1393 " :type other: :class:`Vector`\n"
1394 " :return: the parallel projection vector\n"
1395 " :rtype: :class:`Vector`\n");
1396static PyObject *Vector_project(VectorObject *self, PyObject *value)
1397{
1398 const int vec_num = self->vec_num;
1399 float *tvec;
1400 double dot = 0.0f, dot2 = 0.0f;
1401 int x;
1402
1403 if (BaseMath_ReadCallback(self) == -1) {
1404 return nullptr;
1405 }
1406
1408 &tvec, vec_num, value, "Vector.project(other), invalid 'other' arg") == -1)
1409 {
1410 return nullptr;
1411 }
1412
1413 /* get dot products */
1414 for (x = 0; x < vec_num; x++) {
1415 dot += double(self->vec[x] * tvec[x]);
1416 dot2 += double(tvec[x] * tvec[x]);
1417 }
1418 /* projection */
1419 dot /= dot2;
1420 for (x = 0; x < vec_num; x++) {
1421 tvec[x] *= float(dot);
1422 }
1423 return Vector_CreatePyObject_alloc(tvec, vec_num, Py_TYPE(self));
1424}
1425
1427
1428/* -------------------------------------------------------------------- */
1431
1433 /* Wrap. */
1434 Vector_lerp_doc,
1435 ".. method:: lerp(other, factor, /)\n"
1436 "\n"
1437 " Returns the interpolation of two vectors.\n"
1438 "\n"
1439 " :param other: value to interpolate with.\n"
1440 " :type other: :class:`Vector`\n"
1441 " :param factor: The interpolation value in [0.0, 1.0].\n"
1442 " :type factor: float\n"
1443 " :return: The interpolated vector.\n"
1444 " :rtype: :class:`Vector`\n");
1445static PyObject *Vector_lerp(VectorObject *self, PyObject *args)
1446{
1447 const int vec_num = self->vec_num;
1448 PyObject *value = nullptr;
1449 float fac;
1450 float *tvec;
1451
1452 if (!PyArg_ParseTuple(args,
1453 "O" /* `other` */
1454 "f" /* `factor` */
1455 ":lerp",
1456 &value,
1457 &fac))
1458 {
1459 return nullptr;
1460 }
1461
1462 if (BaseMath_ReadCallback(self) == -1) {
1463 return nullptr;
1464 }
1465
1467 &tvec, vec_num, value, "Vector.lerp(other), invalid 'other' arg") == -1)
1468 {
1469 return nullptr;
1470 }
1471
1472 interp_vn_vn(tvec, self->vec, 1.0f - fac, vec_num);
1473
1474 return Vector_CreatePyObject_alloc(tvec, vec_num, Py_TYPE(self));
1475}
1476
1478
1479/* -------------------------------------------------------------------- */
1482
1484 /* Wrap. */
1485 Vector_slerp_doc,
1486 ".. method:: slerp(other, factor, fallback=None, /)\n"
1487 "\n"
1488 " Returns the interpolation of two non-zero vectors (spherical coordinates).\n"
1489 "\n"
1490 " :param other: value to interpolate with.\n"
1491 " :type other: :class:`Vector`\n"
1492 " :param factor: The interpolation value typically in [0.0, 1.0].\n"
1493 " :type factor: float\n"
1494 " :param fallback: return this when the vector can't be calculated (zero length "
1495 "vector or direct opposites),\n"
1496 " (instead of raising a :exc:`ValueError`).\n"
1497 " :type fallback: Any\n"
1498 " :return: The interpolated vector.\n"
1499 " :rtype: :class:`Vector`\n");
1500static PyObject *Vector_slerp(VectorObject *self, PyObject *args)
1501{
1502 const int vec_num = self->vec_num;
1503 PyObject *value = nullptr;
1504 float fac, cosom, w[2];
1505 float self_vec[3], other_vec[3], ret_vec[3];
1506 float self_len_sq, other_len_sq;
1507 int x;
1508 PyObject *fallback = nullptr;
1509
1510 if (!PyArg_ParseTuple(args,
1511 "O" /* `other` */
1512 "f" /* `factor` */
1513 "|" /* Optional arguments. */
1514 "O" /* `fallback` */
1515 ":slerp",
1516 &value,
1517 &fac,
1518 &fallback))
1519 {
1520 return nullptr;
1521 }
1522
1523 if (BaseMath_ReadCallback(self) == -1) {
1524 return nullptr;
1525 }
1526
1527 if (self->vec_num > 3) {
1528 PyErr_SetString(PyExc_ValueError, "Vector must be 2D or 3D");
1529 return nullptr;
1530 }
1531
1533 other_vec, vec_num, vec_num, value, "Vector.slerp(other), invalid 'other' arg") == -1)
1534 {
1535 return nullptr;
1536 }
1537
1538 self_len_sq = normalize_vn_vn(self_vec, self->vec, vec_num);
1539 other_len_sq = normalize_vn(other_vec, vec_num);
1540
1541 /* use fallbacks for zero length vectors */
1542 if ((self_len_sq < FLT_EPSILON) || (other_len_sq < FLT_EPSILON)) [[unlikely]] {
1543 /* avoid exception */
1544 if (fallback) {
1545 Py_INCREF(fallback);
1546 return fallback;
1547 }
1548
1549 PyErr_SetString(PyExc_ValueError,
1550 "Vector.slerp(): "
1551 "zero length vectors unsupported");
1552 return nullptr;
1553 }
1554
1555 /* We have sane state, execute slerp */
1556 cosom = float(dot_vn_vn(self_vec, other_vec, vec_num));
1557
1558 /* direct opposite, can't slerp */
1559 if (cosom < (-1.0f + FLT_EPSILON)) [[unlikely]] {
1560 /* avoid exception */
1561 if (fallback) {
1562 Py_INCREF(fallback);
1563 return fallback;
1564 }
1565
1566 PyErr_SetString(PyExc_ValueError,
1567 "Vector.slerp(): "
1568 "opposite vectors unsupported");
1569 return nullptr;
1570 }
1571
1572 interp_dot_slerp(fac, cosom, w);
1573
1574 for (x = 0; x < vec_num; x++) {
1575 ret_vec[x] = (w[0] * self_vec[x]) + (w[1] * other_vec[x]);
1576 }
1577
1578 return Vector_CreatePyObject(ret_vec, vec_num, Py_TYPE(self));
1579}
1580
1582
1583/* -------------------------------------------------------------------- */
1586
1588 /* Wrap. */
1589 Vector_rotate_doc,
1590 ".. method:: rotate(other, /)\n"
1591 "\n"
1592 " Rotate the vector by a rotation value.\n"
1593 "\n"
1594 " .. note:: 2D vectors are a special case that can only be rotated by a 2x2 matrix.\n"
1595 "\n"
1596 " :param other: rotation component of mathutils value\n"
1597 " :type other: :class:`Euler` | :class:`Quaternion` | :class:`Matrix`\n");
1598static PyObject *Vector_rotate(VectorObject *self, PyObject *value)
1599{
1600 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
1601 return nullptr;
1602 }
1603
1604 if (self->vec_num == 2) {
1605 /* Special case for 2D Vector with 2x2 matrix, so we avoid resizing it to a 3x3. */
1606 float other_rmat[2][2];
1607 MatrixObject *pymat;
1608 if (!Matrix_Parse2x2(value, &pymat)) {
1609 return nullptr;
1610 }
1611 normalize_m2_m2(other_rmat, reinterpret_cast<const float (*)[2]>(pymat->matrix));
1612 /* Equivalent to a rotation along the Z axis. */
1613 mul_m2_v2(other_rmat, self->vec);
1614 }
1615 else {
1616 float other_rmat[3][3];
1617
1618 if (mathutils_any_to_rotmat(other_rmat, value, "Vector.rotate(value)") == -1) {
1619 return nullptr;
1620 }
1621
1622 mul_m3_v3(other_rmat, self->vec);
1623 }
1624
1625 (void)BaseMath_WriteCallback(self);
1626 Py_RETURN_NONE;
1627}
1628
1630
1631/* -------------------------------------------------------------------- */
1634
1636 /* Wrap. */
1637 Vector_negate_doc,
1638 ".. method:: negate()\n"
1639 "\n"
1640 " Set all values to their negative.\n");
1641static PyObject *Vector_negate(VectorObject *self)
1642{
1643 if (BaseMath_ReadCallback(self) == -1) {
1644 return nullptr;
1645 }
1646
1647 negate_vn(self->vec, self->vec_num);
1648
1649 (void)BaseMath_WriteCallback(self); /* already checked for error */
1650 Py_RETURN_NONE;
1651}
1652
1654
1655/* -------------------------------------------------------------------- */
1658
1660 /* Wrap. */
1661 Vector_copy_doc,
1662 ".. method:: copy()\n"
1663 "\n"
1664 " Returns a copy of this vector.\n"
1665 "\n"
1666 " :return: A copy of the vector.\n"
1667 " :rtype: :class:`Vector`\n"
1668 "\n"
1669 " .. note:: use this to get a copy of a wrapped vector with\n"
1670 " no reference to the original data.\n");
1671static PyObject *Vector_copy(VectorObject *self)
1672{
1673 if (BaseMath_ReadCallback(self) == -1) {
1674 return nullptr;
1675 }
1676
1677 return Vector_CreatePyObject(self->vec, self->vec_num, Py_TYPE(self));
1678}
1679static PyObject *Vector_deepcopy(VectorObject *self, PyObject *args)
1680{
1681 if (!PyC_CheckArgs_DeepCopy(args)) {
1682 return nullptr;
1683 }
1684 return Vector_copy(self);
1685}
1686
1688
1689/* -------------------------------------------------------------------- */
1692
1693static PyObject *Vector_repr(VectorObject *self)
1694{
1695 PyObject *ret, *tuple;
1696
1697 if (BaseMath_ReadCallback(self) == -1) {
1698 return nullptr;
1699 }
1700
1701 tuple = Vector_to_tuple_ex(self, -1);
1702 ret = PyUnicode_FromFormat("Vector(%R)", tuple);
1703 Py_DECREF(tuple);
1704 return ret;
1705}
1706
1707#ifndef MATH_STANDALONE
1708static PyObject *Vector_str(VectorObject *self)
1709{
1710 int i;
1711
1712 DynStr *ds;
1713
1714 if (BaseMath_ReadCallback(self) == -1) {
1715 return nullptr;
1716 }
1717
1718 ds = BLI_dynstr_new();
1719
1720 BLI_dynstr_append(ds, "<Vector (");
1721
1722 for (i = 0; i < self->vec_num; i++) {
1723 BLI_dynstr_appendf(ds, i ? ", %.4f" : "%.4f", self->vec[i]);
1724 }
1725
1726 BLI_dynstr_append(ds, ")>");
1727
1728 return mathutils_dynstr_to_py(ds); /* frees ds */
1729}
1730#endif
1731
1733
1734/* -------------------------------------------------------------------- */
1737
1738static int Vector_getbuffer(PyObject *obj, Py_buffer *view, int flags)
1739{
1740 VectorObject *self = reinterpret_cast<VectorObject *>(obj);
1741 if (BaseMath_Prepare_ForBufferAccess(self, view, flags) == -1) [[unlikely]] {
1742 return -1;
1743 }
1744 if (BaseMath_ReadCallback(self) == -1) [[unlikely]] {
1745 return -1;
1746 }
1747
1748 memset(view, 0, sizeof(*view));
1749
1750 view->obj = reinterpret_cast<PyObject *>(self);
1751 view->buf = static_cast<void *>(self->vec);
1752 view->len = Py_ssize_t(self->vec_num * sizeof(float));
1753 view->itemsize = sizeof(float);
1754 view->ndim = 1;
1755 if ((flags & PyBUF_WRITABLE) == 0) {
1756 view->readonly = 1;
1757 }
1758 if (flags & PyBUF_FORMAT) {
1759 view->format = const_cast<char *>("f");
1760 }
1761
1762 self->flag |= BASE_MATH_FLAG_HAS_BUFFER_VIEW;
1763
1764 Py_INCREF(self);
1765 return 0;
1766}
1767
1768static void Vector_releasebuffer(PyObject * /*exporter*/, Py_buffer *view)
1769{
1770 VectorObject *self = reinterpret_cast<VectorObject *>(view->obj);
1771 self->flag &= ~BASE_MATH_FLAG_HAS_BUFFER_VIEW;
1772
1773 if (view->readonly == 0) {
1774 if (BaseMath_WriteCallback(self) == -1) [[unlikely]] {
1775 PyErr_Print();
1776 }
1777 }
1778}
1779
1780static PyBufferProcs Vector_as_buffer = {
1781 static_cast<getbufferproc>(Vector_getbuffer),
1782 static_cast<releasebufferproc>(Vector_releasebuffer),
1783};
1784
1786
1787/* -------------------------------------------------------------------- */
1790
1791static PyObject *Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
1792{
1793 VectorObject *vecA = nullptr, *vecB = nullptr;
1794 int result = 0;
1795 const double epsilon = 0.000001f;
1796 double lenA, lenB;
1797
1798 if (!VectorObject_Check(objectA) || !VectorObject_Check(objectB)) {
1799 if (comparison_type == Py_NE) {
1800 Py_RETURN_TRUE;
1801 }
1802
1803 Py_RETURN_FALSE;
1804 }
1805 vecA = reinterpret_cast<VectorObject *>(objectA);
1806 vecB = reinterpret_cast<VectorObject *>(objectB);
1807
1808 if (BaseMath_ReadCallback(vecA) == -1 || BaseMath_ReadCallback(vecB) == -1) {
1809 return nullptr;
1810 }
1811
1812 if (vecA->vec_num != vecB->vec_num) {
1813 if (comparison_type == Py_NE) {
1814 Py_RETURN_TRUE;
1815 }
1816
1817 Py_RETURN_FALSE;
1818 }
1819
1820 switch (comparison_type) {
1821 case Py_LT: {
1822 lenA = len_squared_vn(vecA->vec, vecA->vec_num);
1823 lenB = len_squared_vn(vecB->vec, vecB->vec_num);
1824 if (lenA < lenB) {
1825 result = 1;
1826 }
1827 break;
1828 }
1829 case Py_LE: {
1830 lenA = len_squared_vn(vecA->vec, vecA->vec_num);
1831 lenB = len_squared_vn(vecB->vec, vecB->vec_num);
1832 if (lenA < lenB) {
1833 result = 1;
1834 }
1835 else {
1836 result = (((lenA + epsilon) > lenB) && ((lenA - epsilon) < lenB));
1837 }
1838 break;
1839 }
1840 case Py_EQ: {
1841 result = EXPP_VectorsAreEqual(vecA->vec, vecB->vec, vecA->vec_num, 1);
1842 break;
1843 }
1844 case Py_NE: {
1845 result = !EXPP_VectorsAreEqual(vecA->vec, vecB->vec, vecA->vec_num, 1);
1846 break;
1847 }
1848 case Py_GT: {
1849 lenA = len_squared_vn(vecA->vec, vecA->vec_num);
1850 lenB = len_squared_vn(vecB->vec, vecB->vec_num);
1851 if (lenA > lenB) {
1852 result = 1;
1853 }
1854 break;
1855 }
1856 case Py_GE: {
1857 lenA = len_squared_vn(vecA->vec, vecA->vec_num);
1858 lenB = len_squared_vn(vecB->vec, vecB->vec_num);
1859 if (lenA > lenB) {
1860 result = 1;
1861 }
1862 else {
1863 result = (((lenA + epsilon) > lenB) && ((lenA - epsilon) < lenB));
1864 }
1865 break;
1866 }
1867 default: {
1868 printf("The result of the comparison could not be evaluated");
1869 break;
1870 }
1871 }
1872 if (result == 1) {
1873 Py_RETURN_TRUE;
1874 }
1875
1876 Py_RETURN_FALSE;
1877}
1878
1880
1881/* -------------------------------------------------------------------- */
1884
1885static Py_hash_t Vector_hash(VectorObject *self)
1886{
1887 if (BaseMath_ReadCallback(self) == -1) {
1888 return -1;
1889 }
1890
1891 if (BaseMathObject_Prepare_ForHash(self) == -1) {
1892 return -1;
1893 }
1894
1895 return mathutils_array_hash(self->vec, self->vec_num);
1896}
1897
1899
1900/* -------------------------------------------------------------------- */
1903
1905static Py_ssize_t Vector_len(VectorObject *self)
1906{
1907 return self->vec_num;
1908}
1909
1910static PyObject *vector_item_internal(VectorObject *self, int i, const bool is_attr)
1911{
1912 if (i < 0 || i >= self->vec_num) {
1913 if (is_attr) {
1914 PyErr_Format(PyExc_AttributeError,
1915 "Vector.%c: unavailable on %dd vector",
1916 *((static_cast<const char *>("xyzw")) + i),
1917 self->vec_num);
1918 }
1919 else {
1920 PyErr_SetString(PyExc_IndexError, "vector[index]: out of range");
1921 }
1922 return nullptr;
1923 }
1924
1925 if (BaseMath_ReadIndexCallback(self, i) == -1) {
1926 return nullptr;
1927 }
1928
1929 return PyFloat_FromDouble(self->vec[i]);
1930}
1931
1933static PyObject *Vector_item(VectorObject *self, Py_ssize_t i)
1934{
1935 return vector_item_internal(self, i, false);
1936}
1937
1938static int vector_ass_item_internal(VectorObject *self, int i, PyObject *value, const bool is_attr)
1939{
1940 float scalar;
1941
1942 if (BaseMath_Prepare_ForWrite(self) == -1) {
1943 return -1;
1944 }
1945
1946 if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
1947 /* parsed item not a number */
1948 PyErr_SetString(PyExc_TypeError,
1949 "vector[index] = x: "
1950 "assigned value not a number");
1951 return -1;
1952 }
1953
1954 if (i < 0 || i >= self->vec_num) {
1955 if (is_attr) {
1956 PyErr_Format(PyExc_AttributeError,
1957 "Vector.%c = x: unavailable on %dd vector",
1958 *((static_cast<const char *>("xyzw")) + i),
1959 self->vec_num);
1960 }
1961 else {
1962 PyErr_SetString(PyExc_IndexError,
1963 "vector[index] = x: "
1964 "assignment index out of range");
1965 }
1966 return -1;
1967 }
1968 self->vec[i] = scalar;
1969
1970 if (BaseMath_WriteIndexCallback(self, i) == -1) {
1971 return -1;
1972 }
1973 return 0;
1974}
1975
1977static int Vector_ass_item(VectorObject *self, Py_ssize_t i, PyObject *value)
1978{
1979 return vector_ass_item_internal(self, i, value, false);
1980}
1981
1983static PyObject *Vector_slice(VectorObject *self,
1984 Py_ssize_t start,
1985 Py_ssize_t step,
1986 Py_ssize_t slice_length)
1987{
1988 if (BaseMath_ReadCallback(self) == -1) {
1989 return nullptr;
1990 }
1991
1992 PyObject *tuple = PyTuple_New(slice_length);
1993 Py_ssize_t index = start;
1994 for (Py_ssize_t i = 0; i < slice_length; i++, index += step) {
1995 BLI_assert(index >= 0 && index < self->vec_num);
1996 PyTuple_SET_ITEM(tuple, i, PyFloat_FromDouble(self->vec[index]));
1997 }
1998 return tuple;
1999}
2000
2007 VectorObject *self, Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, PyObject *seq)
2008{
2009 float *vec = nullptr;
2010
2011 /* Subset writes merge into existing values, so sync the source first. */
2012 if (mathutils_slice_is_subset(start, step, slice_length, self->vec_num)) {
2013 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
2014 return -1;
2015 }
2016 }
2017 else {
2018 if (BaseMath_Prepare_ForWrite(self) == -1) {
2019 return -1;
2020 }
2021 }
2022
2023 const int parsed_size = mathutils_array_parse_alloc(
2024 &vec, int(slice_length), seq, "vector[slice] = seq");
2025 if (parsed_size == -1) {
2026 return -1;
2027 }
2028
2029 /* The vector could be resized in this case.
2030 * Rely on explicit use of the `.resize()` method because (unlike lists),
2031 * these are more typically fixed size collections.
2032 * Resizing is more likely to be a mistake as it isn't a common operation. */
2033 if (parsed_size != slice_length) {
2034 PyMem_Free(vec);
2035 PyErr_Format(PyExc_ValueError,
2036 "vector[slice] = seq: sequence size is %d, expected %d",
2037 parsed_size,
2038 int(slice_length));
2039 return -1;
2040 }
2041
2042 Py_ssize_t index = start;
2043 for (Py_ssize_t i = 0; i < slice_length; i++, index += step) {
2044 BLI_assert(index >= 0 && index < self->vec_num);
2045 self->vec[index] = vec[i];
2046 }
2047
2048 PyMem_Free(vec);
2049
2050 if (BaseMath_WriteCallback(self) == -1) {
2051 return -1;
2052 }
2053
2054 return 0;
2055}
2056
2058static PyObject *Vector_subscript(VectorObject *self, PyObject *item)
2059{
2060 if (PyIndex_Check(item)) {
2061 Py_ssize_t i;
2062 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2063 if (i == -1 && PyErr_Occurred()) {
2064 return nullptr;
2065 }
2066 if (i < 0) {
2067 i += self->vec_num;
2068 }
2069 return Vector_item(self, i);
2070 }
2071 if (PySlice_Check(item)) {
2072 Py_ssize_t start, stop, step, slice_length;
2073
2074 if (PySlice_GetIndicesEx(item, self->vec_num, &start, &stop, &step, &slice_length) < 0) {
2075 return nullptr;
2076 }
2077
2078 return Vector_slice(self, start, step, slice_length);
2079 }
2080
2081 PyErr_Format(
2082 PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
2083 return nullptr;
2084}
2085
2087static int Vector_ass_subscript(VectorObject *self, PyObject *item, PyObject *value)
2088{
2089 if (PyIndex_Check(item)) {
2090 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
2091 if (i == -1 && PyErr_Occurred()) {
2092 return -1;
2093 }
2094 if (i < 0) {
2095 i += self->vec_num;
2096 }
2097 return Vector_ass_item(self, i, value);
2098 }
2099 if (PySlice_Check(item)) {
2100 Py_ssize_t start, stop, step, slice_length;
2101
2102 if (PySlice_GetIndicesEx(item, self->vec_num, &start, &stop, &step, &slice_length) < 0) {
2103 return -1;
2104 }
2105
2106 return Vector_ass_slice(self, start, step, slice_length, value);
2107 }
2108
2109 PyErr_Format(
2110 PyExc_TypeError, "vector indices must be integers, not %.200s", Py_TYPE(item)->tp_name);
2111 return -1;
2112}
2113
2115
2116/* -------------------------------------------------------------------- */
2119
2121static PyObject *Vector_add(PyObject *v1, PyObject *v2)
2122{
2123 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2124 float *vec = nullptr;
2125
2127 PyErr_Format(PyExc_AttributeError,
2128 "Vector addition: (%s + %s) "
2129 "invalid type for this operation",
2130 Py_TYPE(v1)->tp_name,
2131 Py_TYPE(v2)->tp_name);
2132 return nullptr;
2133 }
2134 vec1 = reinterpret_cast<VectorObject *>(v1);
2135 vec2 = reinterpret_cast<VectorObject *>(v2);
2136
2137 if (BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
2138 return nullptr;
2139 }
2140
2141 /* VECTOR + VECTOR. */
2142 if (vec1->vec_num != vec2->vec_num) {
2143 PyErr_SetString(PyExc_AttributeError,
2144 "Vector addition: "
2145 "vectors must have the same dimensions for this operation");
2146 return nullptr;
2147 }
2148
2149 vec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
2150 if (vec == nullptr) {
2151 PyErr_SetString(PyExc_MemoryError,
2152 "Vector(): "
2153 "problem allocating pointer space");
2154 return nullptr;
2155 }
2156
2157 add_vn_vnvn(vec, vec1->vec, vec2->vec, vec1->vec_num);
2158
2159 return Vector_CreatePyObject_alloc(vec, vec1->vec_num, Py_TYPE(v1));
2160}
2161
2163static PyObject *Vector_iadd(PyObject *v1, PyObject *v2)
2164{
2165 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2166
2168 PyErr_Format(PyExc_AttributeError,
2169 "Vector addition: (%s += %s) "
2170 "invalid type for this operation",
2171 Py_TYPE(v1)->tp_name,
2172 Py_TYPE(v2)->tp_name);
2173 return nullptr;
2174 }
2175 vec1 = reinterpret_cast<VectorObject *>(v1);
2176 vec2 = reinterpret_cast<VectorObject *>(v2);
2177
2178 if (vec1->vec_num != vec2->vec_num) {
2179 PyErr_SetString(PyExc_AttributeError,
2180 "Vector addition: "
2181 "vectors must have the same dimensions for this operation");
2182 return nullptr;
2183 }
2184
2185 if (BaseMath_ReadCallback_ForWrite(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
2186 return nullptr;
2187 }
2188
2189 add_vn_vn(vec1->vec, vec2->vec, vec1->vec_num);
2190
2191 (void)BaseMath_WriteCallback(vec1);
2192 Py_INCREF(v1);
2193 return v1;
2194}
2195
2197static PyObject *Vector_sub(PyObject *v1, PyObject *v2)
2198{
2199 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2200 float *vec;
2201
2203 PyErr_Format(PyExc_AttributeError,
2204 "Vector subtraction: (%s - %s) "
2205 "invalid type for this operation",
2206 Py_TYPE(v1)->tp_name,
2207 Py_TYPE(v2)->tp_name);
2208 return nullptr;
2209 }
2210 vec1 = reinterpret_cast<VectorObject *>(v1);
2211 vec2 = reinterpret_cast<VectorObject *>(v2);
2212
2213 if (BaseMath_ReadCallback(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
2214 return nullptr;
2215 }
2216
2217 if (vec1->vec_num != vec2->vec_num) {
2218 PyErr_SetString(PyExc_AttributeError,
2219 "Vector subtraction: "
2220 "vectors must have the same dimensions for this operation");
2221 return nullptr;
2222 }
2223
2224 vec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
2225 if (vec == nullptr) {
2226 PyErr_SetString(PyExc_MemoryError,
2227 "Vector(): "
2228 "problem allocating pointer space");
2229 return nullptr;
2230 }
2231
2232 sub_vn_vnvn(vec, vec1->vec, vec2->vec, vec1->vec_num);
2233
2234 return Vector_CreatePyObject_alloc(vec, vec1->vec_num, Py_TYPE(v1));
2235}
2236
2238static PyObject *Vector_isub(PyObject *v1, PyObject *v2)
2239{
2240 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2241
2243 PyErr_Format(PyExc_AttributeError,
2244 "Vector subtraction: (%s -= %s) "
2245 "invalid type for this operation",
2246 Py_TYPE(v1)->tp_name,
2247 Py_TYPE(v2)->tp_name);
2248 return nullptr;
2249 }
2250 vec1 = reinterpret_cast<VectorObject *>(v1);
2251 vec2 = reinterpret_cast<VectorObject *>(v2);
2252
2253 if (vec1->vec_num != vec2->vec_num) {
2254 PyErr_SetString(PyExc_AttributeError,
2255 "Vector subtraction: "
2256 "vectors must have the same dimensions for this operation");
2257 return nullptr;
2258 }
2259
2260 if (BaseMath_ReadCallback_ForWrite(vec1) == -1 || BaseMath_ReadCallback(vec2) == -1) {
2261 return nullptr;
2262 }
2263
2264 sub_vn_vn(vec1->vec, vec2->vec, vec1->vec_num);
2265
2266 (void)BaseMath_WriteCallback(vec1);
2267 Py_INCREF(v1);
2268 return v1;
2269}
2270
2271/* Multiply internal implementation `object * object`, `object *= object`. */
2272
2274{
2275 float vec_cpy[MAX_DIMENSIONS];
2276 int row, col, z = 0;
2277
2278 if (mat->col_num != vec->vec_num) {
2279 if (mat->col_num == 4 && vec->vec_num == 3) {
2280 vec_cpy[3] = 1.0f;
2281 }
2282 else {
2283 PyErr_SetString(PyExc_ValueError,
2284 "matrix * vector: "
2285 "len(matrix.col) and len(vector) must be the same, "
2286 "except for 4x4 matrix * 3D vector.");
2287 return -1;
2288 }
2289 }
2290
2291 memcpy(vec_cpy, vec->vec, vec->vec_num * sizeof(float));
2292
2293 r_vec[3] = 1.0f;
2294
2295 for (row = 0; row < mat->row_num; row++) {
2296 double dot = 0.0f;
2297 for (col = 0; col < mat->col_num; col++) {
2298 dot += double(MATRIX_ITEM(mat, row, col) * vec_cpy[col]);
2299 }
2300 r_vec[z++] = float(dot);
2301 }
2302
2303 return 0;
2304}
2305
2306static PyObject *vector_mul_float(VectorObject *vec, const float scalar)
2307{
2308 float *tvec = static_cast<float *>(PyMem_Malloc(vec->vec_num * sizeof(float)));
2309 if (tvec == nullptr) {
2310 PyErr_SetString(PyExc_MemoryError,
2311 "vec * float: "
2312 "problem allocating pointer space");
2313 return nullptr;
2314 }
2315
2316 mul_vn_vn_fl(tvec, vec->vec, vec->vec_num, scalar);
2317 return Vector_CreatePyObject_alloc(tvec, vec->vec_num, Py_TYPE(vec));
2318}
2319
2320static PyObject *vector_mul_vec(VectorObject *vec1, VectorObject *vec2)
2321{
2322 float *tvec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
2323 if (tvec == nullptr) {
2324 PyErr_SetString(PyExc_MemoryError,
2325 "vec * vec: "
2326 "problem allocating pointer space");
2327 return nullptr;
2328 }
2329
2330 mul_vn_vnvn(tvec, vec1->vec, vec2->vec, vec1->vec_num);
2331 return Vector_CreatePyObject_alloc(tvec, vec1->vec_num, Py_TYPE(vec1));
2332}
2333
2335static PyObject *Vector_mul(PyObject *v1, PyObject *v2)
2336{
2337 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2338 float scalar;
2339
2340 if (VectorObject_Check(v1)) {
2341 vec1 = reinterpret_cast<VectorObject *>(v1);
2342 if (BaseMath_ReadCallback(vec1) == -1) {
2343 return nullptr;
2344 }
2345 }
2346 if (VectorObject_Check(v2)) {
2347 vec2 = reinterpret_cast<VectorObject *>(v2);
2348 if (BaseMath_ReadCallback(vec2) == -1) {
2349 return nullptr;
2350 }
2351 }
2352
2353 /* Intentionally don't support (Quaternion) here, uses reverse order instead. */
2354
2355 /* make sure v1 is always the vector */
2356 if (vec1 && vec2) {
2357 if (vec1->vec_num != vec2->vec_num) {
2358 PyErr_SetString(PyExc_ValueError,
2359 "Vector multiplication: "
2360 "vectors must have the same dimensions for this operation");
2361 return nullptr;
2362 }
2363
2364 /* element-wise product */
2365 return vector_mul_vec(vec1, vec2);
2366 }
2367 if (vec1) {
2368 if (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0) { /* VEC * FLOAT */
2369 return vector_mul_float(vec1, scalar);
2370 }
2371 }
2372 else if (vec2) {
2373 if (((scalar = PyFloat_AsDouble(v1)) == -1.0f && PyErr_Occurred()) == 0) { /* FLOAT * VEC */
2374 return vector_mul_float(vec2, scalar);
2375 }
2376 }
2377
2378 PyErr_Format(PyExc_TypeError,
2379 "Element-wise multiplication: "
2380 "not supported between '%.200s' and '%.200s' types",
2381 Py_TYPE(v1)->tp_name,
2382 Py_TYPE(v2)->tp_name);
2383 return nullptr;
2384}
2385
2387static PyObject *Vector_imul(PyObject *v1, PyObject *v2)
2388{
2389 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2390 float scalar;
2391
2392 if (VectorObject_Check(v1)) {
2393 vec1 = reinterpret_cast<VectorObject *>(v1);
2394 if (BaseMath_ReadCallback(vec1) == -1) {
2395 return nullptr;
2396 }
2397 }
2398 if (VectorObject_Check(v2)) {
2399 vec2 = reinterpret_cast<VectorObject *>(v2);
2400 if (BaseMath_ReadCallback(vec2) == -1) {
2401 return nullptr;
2402 }
2403 }
2404
2405 if (BaseMath_ReadCallback_ForWrite(vec1) == -1) {
2406 return nullptr;
2407 }
2408
2409 /* Intentionally don't support (Quaternion, Matrix) here, uses reverse order instead. */
2410
2411 if (vec1 && vec2) {
2412 if (vec1->vec_num != vec2->vec_num) {
2413 PyErr_SetString(PyExc_ValueError,
2414 "Vector multiplication: "
2415 "vectors must have the same dimensions for this operation");
2416 return nullptr;
2417 }
2418
2419 /* Element-wise product in-place. */
2420 mul_vn_vn(vec1->vec, vec2->vec, vec1->vec_num);
2421 }
2422 else if (vec1 && (((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) == 0)) {
2423 /* VEC *= FLOAT */
2424 mul_vn_fl(vec1->vec, vec1->vec_num, scalar);
2425 }
2426 else {
2427 PyErr_Format(PyExc_TypeError,
2428 "In place element-wise multiplication: "
2429 "not supported between '%.200s' and '%.200s' types",
2430 Py_TYPE(v1)->tp_name,
2431 Py_TYPE(v2)->tp_name);
2432 return nullptr;
2433 }
2434
2435 (void)BaseMath_WriteCallback(vec1);
2436 Py_INCREF(v1);
2437 return v1;
2438}
2439
2441static PyObject *Vector_matmul(PyObject *v1, PyObject *v2)
2442{
2443 VectorObject *vec1 = nullptr, *vec2 = nullptr;
2444 int vec_num;
2445
2446 if (VectorObject_Check(v1)) {
2447 vec1 = reinterpret_cast<VectorObject *>(v1);
2448 if (BaseMath_ReadCallback(vec1) == -1) {
2449 return nullptr;
2450 }
2451 }
2452 if (VectorObject_Check(v2)) {
2453 vec2 = reinterpret_cast<VectorObject *>(v2);
2454 if (BaseMath_ReadCallback(vec2) == -1) {
2455 return nullptr;
2456 }
2457 }
2458
2459 /* Intentionally don't support (Quaternion) here, uses reverse order instead. */
2460
2461 /* make sure v1 is always the vector */
2462 if (vec1 && vec2) {
2463 if (vec1->vec_num != vec2->vec_num) {
2464 PyErr_SetString(PyExc_ValueError,
2465 "Vector multiplication: "
2466 "vectors must have the same dimensions for this operation");
2467 return nullptr;
2468 }
2469
2470 /* Dot product. */
2471 return PyFloat_FromDouble(dot_vn_vn(vec1->vec, vec2->vec, vec1->vec_num));
2472 }
2473 if (vec1) {
2474 if (MatrixObject_Check(v2)) {
2475 /* VEC @ MATRIX */
2476 float tvec[MAX_DIMENSIONS];
2477
2478 if (BaseMath_ReadCallback((MatrixObject *)v2) == -1) {
2479 return nullptr;
2480 }
2481 if (row_vector_multiplication(tvec, vec1, reinterpret_cast<MatrixObject *>(v2)) == -1) {
2482 return nullptr;
2483 }
2484
2485 if ((reinterpret_cast<MatrixObject *>(v2))->row_num == 4 && vec1->vec_num == 3) {
2486 vec_num = 3;
2487 }
2488 else {
2489 vec_num = (reinterpret_cast<MatrixObject *>(v2))->col_num;
2490 }
2491
2492 return Vector_CreatePyObject(tvec, vec_num, Py_TYPE(vec1));
2493 }
2494 }
2495
2496 PyErr_Format(PyExc_TypeError,
2497 "Vector multiplication: "
2498 "not supported between '%.200s' and '%.200s' types",
2499 Py_TYPE(v1)->tp_name,
2500 Py_TYPE(v2)->tp_name);
2501 return nullptr;
2502}
2503
2505static PyObject *Vector_imatmul(PyObject *v1, PyObject *v2)
2506{
2507 PyErr_Format(PyExc_TypeError,
2508 "In place vector multiplication: "
2509 "not supported between '%.200s' and '%.200s' types",
2510 Py_TYPE(v1)->tp_name,
2511 Py_TYPE(v2)->tp_name);
2512 return nullptr;
2513}
2514
2516static PyObject *Vector_div(PyObject *v1, PyObject *v2)
2517{
2518 float *vec = nullptr, scalar;
2519 VectorObject *vec1 = nullptr;
2520
2521 if (!VectorObject_Check(v1)) { /* not a vector */
2522 PyErr_SetString(PyExc_TypeError,
2523 "Vector division: "
2524 "Vector must be divided by a float");
2525 return nullptr;
2526 }
2527 vec1 = reinterpret_cast<VectorObject *>(v1); /* vector */
2528
2529 if (BaseMath_ReadCallback(vec1) == -1) {
2530 return nullptr;
2531 }
2532
2533 if ((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) {
2534 /* parsed item not a number */
2535 PyErr_SetString(PyExc_TypeError,
2536 "Vector division: "
2537 "Vector must be divided by a float");
2538 return nullptr;
2539 }
2540
2541 if (scalar == 0.0f) {
2542 PyErr_SetString(PyExc_ZeroDivisionError,
2543 "Vector division: "
2544 "divide by zero error");
2545 return nullptr;
2546 }
2547
2548 vec = static_cast<float *>(PyMem_Malloc(vec1->vec_num * sizeof(float)));
2549
2550 if (vec == nullptr) {
2551 PyErr_SetString(PyExc_MemoryError,
2552 "vec / value: "
2553 "problem allocating pointer space");
2554 return nullptr;
2555 }
2556
2557 mul_vn_vn_fl(vec, vec1->vec, vec1->vec_num, 1.0f / scalar);
2558
2559 return Vector_CreatePyObject_alloc(vec, vec1->vec_num, Py_TYPE(v1));
2560}
2561
2563static PyObject *Vector_idiv(PyObject *v1, PyObject *v2)
2564{
2565 float scalar;
2566 VectorObject *vec1 = reinterpret_cast<VectorObject *>(v1);
2567
2568 if (BaseMath_ReadCallback_ForWrite(vec1) == -1) {
2569 return nullptr;
2570 }
2571
2572 if ((scalar = PyFloat_AsDouble(v2)) == -1.0f && PyErr_Occurred()) {
2573 /* parsed item not a number */
2574 PyErr_SetString(PyExc_TypeError,
2575 "Vector division: "
2576 "Vector must be divided by a float");
2577 return nullptr;
2578 }
2579
2580 if (scalar == 0.0f) {
2581 PyErr_SetString(PyExc_ZeroDivisionError,
2582 "Vector division: "
2583 "divide by zero error");
2584 return nullptr;
2585 }
2586
2587 mul_vn_fl(vec1->vec, vec1->vec_num, 1.0f / scalar);
2588
2589 (void)BaseMath_WriteCallback(vec1);
2590
2591 Py_INCREF(v1);
2592 return v1;
2593}
2594
2596static PyObject *Vector_neg(VectorObject *self)
2597{
2598 float *tvec;
2599
2600 if (BaseMath_ReadCallback(self) == -1) {
2601 return nullptr;
2602 }
2603
2604 tvec = static_cast<float *>(PyMem_Malloc(self->vec_num * sizeof(float)));
2605 negate_vn_vn(tvec, self->vec, self->vec_num);
2606 return Vector_CreatePyObject_alloc(tvec, self->vec_num, Py_TYPE(self));
2607}
2608
2610
2611/* -------------------------------------------------------------------- */
2614
2615static PySequenceMethods Vector_SeqMethods = {
2616 /*sq_length*/ reinterpret_cast<lenfunc>(Vector_len),
2617 /*sq_concat*/ nullptr,
2618 /*sq_repeat*/ nullptr,
2619 /*sq_item*/ reinterpret_cast<ssizeargfunc>(Vector_item),
2620 /*was_sq_slice*/ nullptr, /* DEPRECATED. */
2621 /*sq_ass_item*/ reinterpret_cast<ssizeobjargproc>(Vector_ass_item),
2622 /*was_sq_ass_slice*/ nullptr, /* DEPRECATED. */
2623 /*sq_contains*/ nullptr,
2624 /*sq_inplace_concat*/ nullptr,
2625 /*sq_inplace_repeat*/ nullptr,
2626};
2627
2628static PyMappingMethods Vector_AsMapping = {
2629 /*mp_length*/ reinterpret_cast<lenfunc>(Vector_len),
2630 /*mp_subscript*/ reinterpret_cast<binaryfunc>(Vector_subscript),
2631 /*mp_ass_subscript*/ reinterpret_cast<objobjargproc>(Vector_ass_subscript),
2632};
2633
2634static PyNumberMethods Vector_NumMethods = {
2635 /*nb_add*/ static_cast<binaryfunc>(Vector_add),
2636 /*nb_subtract*/ static_cast<binaryfunc>(Vector_sub),
2637 /*nb_multiply*/ static_cast<binaryfunc>(Vector_mul),
2638 /*nb_remainder*/ nullptr,
2639 /*nb_divmod*/ nullptr,
2640 /*nb_power*/ nullptr,
2641 /*nb_negative*/ reinterpret_cast<unaryfunc>(Vector_neg),
2642 /*nb_positive*/ reinterpret_cast<unaryfunc>(Vector_copy),
2643 /*nb_absolute*/ nullptr,
2644 /*nb_bool*/ nullptr,
2645 /*nb_invert*/ nullptr,
2646 /*nb_lshift*/ nullptr,
2647 /*nb_rshift*/ nullptr,
2648 /*nb_and*/ nullptr,
2649 /*nb_xor*/ nullptr,
2650 /*nb_or*/ nullptr,
2651 /*nb_int*/ nullptr,
2652 /*nb_reserved*/ nullptr,
2653 /*nb_float*/ nullptr,
2654 /*nb_inplace_add*/ Vector_iadd,
2655 /*nb_inplace_subtract*/ Vector_isub,
2656 /*nb_inplace_multiply*/ Vector_imul,
2657 /*nb_inplace_remainder*/ nullptr,
2658 /*nb_inplace_power*/ nullptr,
2659 /*nb_inplace_lshift*/ nullptr,
2660 /*nb_inplace_rshift*/ nullptr,
2661 /*nb_inplace_and*/ nullptr,
2662 /*nb_inplace_xor*/ nullptr,
2663 /*nb_inplace_or*/ nullptr,
2664 /*nb_floor_divide*/ nullptr,
2665 /*nb_true_divide*/ Vector_div,
2666 /*nb_inplace_floor_divide*/ nullptr,
2667 /*nb_inplace_true_divide*/ Vector_idiv,
2668 /*nb_index*/ nullptr,
2669 /*nb_matrix_multiply*/ static_cast<binaryfunc>(Vector_matmul),
2670 /*nb_inplace_matrix_multiply*/ static_cast<binaryfunc>(Vector_imatmul),
2671};
2672
2674
2675/* -------------------------------------------------------------------- */
2678
2679/* Vector axis: `vector.x/y/z/w`. */
2680
2682 /* Wrap. */
2683 Vector_axis_x_doc,
2684 "Vector X axis.\n"
2685 "\n"
2686 ":type: float\n");
2688 /* Wrap. */
2689 Vector_axis_y_doc,
2690 "Vector Y axis.\n"
2691 "\n"
2692 ":type: float\n");
2694 /* Wrap. */
2695 Vector_axis_z_doc,
2696 "Vector Z axis (3D Vectors only).\n"
2697 "\n"
2698 ":type: float\n");
2700 /* Wrap. */
2701 Vector_axis_w_doc,
2702 "Vector W axis (4D Vectors only).\n"
2703 "\n"
2704 ":type: float\n");
2705
2706static PyObject *Vector_axis_get(VectorObject *self, void *type)
2707{
2708 return vector_item_internal(self, POINTER_AS_INT(type), true);
2709}
2710
2711static int Vector_axis_set(VectorObject *self, PyObject *value, void *type)
2712{
2713 return vector_ass_item_internal(self, POINTER_AS_INT(type), value, true);
2714}
2715
2716/* `Vector.length`. */
2717
2719 /* Wrap. */
2720 Vector_length_doc,
2721 "Vector Length.\n"
2722 "\n"
2723 ":type: float\n");
2724static PyObject *Vector_length_get(VectorObject *self, void * /*closure*/)
2725{
2726 if (BaseMath_ReadCallback(self) == -1) {
2727 return nullptr;
2728 }
2729
2730 return PyFloat_FromDouble(sqrt(dot_vn_vn(self->vec, self->vec, self->vec_num)));
2731}
2732
2733static int Vector_length_set(VectorObject *self, PyObject *value)
2734{
2735 double dot = 0.0f, param;
2736
2737 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
2738 return -1;
2739 }
2740
2741 if ((param = PyFloat_AsDouble(value)) == -1.0 && PyErr_Occurred()) {
2742 PyErr_SetString(PyExc_TypeError, "length must be set to a number");
2743 return -1;
2744 }
2745
2746 if (param < 0.0) {
2747 PyErr_SetString(PyExc_ValueError, "cannot set a vectors length to a negative value");
2748 return -1;
2749 }
2750 if (param == 0.0) {
2751 std::fill_n(self->vec, self->vec_num, 0.0f);
2752 return 0;
2753 }
2754
2755 dot = dot_vn_vn(self->vec, self->vec, self->vec_num);
2756
2757 if (!dot) {
2758 /* can't sqrt zero */
2759 return 0;
2760 }
2761
2762 dot = sqrt(dot);
2763
2764 if (dot == param) {
2765 return 0;
2766 }
2767
2768 dot = dot / param;
2769
2770 mul_vn_fl(self->vec, self->vec_num, 1.0 / dot);
2771
2772 (void)BaseMath_WriteCallback(self); /* checked already */
2773
2774 return 0;
2775}
2776
2777/* `Vector.length_squared`. */
2779 /* Wrap. */
2780 Vector_length_squared_doc,
2781 "Vector length squared (v.dot(v)).\n"
2782 "\n"
2783 ":type: float\n");
2784static PyObject *Vector_length_squared_get(VectorObject *self, void * /*closure*/)
2785{
2786 if (BaseMath_ReadCallback(self) == -1) {
2787 return nullptr;
2788 }
2789
2790 return PyFloat_FromDouble(dot_vn_vn(self->vec, self->vec, self->vec_num));
2791}
2792
2793/* `Vector.xyzw`, etc.. */
2795 /* Wrap. */
2796 Vector_swizzle_doc,
2797 ":type: :class:`Vector`");
2880
2884static PyObject *Vector_swizzle_get(VectorObject *self, void *closure)
2885{
2886 size_t axis_to;
2887 size_t axis_from;
2888 float vec[MAX_DIMENSIONS];
2889 uint swizzleClosure;
2890
2891 if (BaseMath_ReadCallback(self) == -1) {
2892 return nullptr;
2893 }
2894
2895 /* Unpack the axes from the closure into an array. */
2896 axis_to = 0;
2897 swizzleClosure = POINTER_AS_INT(closure);
2898 while (swizzleClosure & SWIZZLE_VALID_AXIS) {
2899 axis_from = swizzleClosure & SWIZZLE_AXIS;
2900 if (axis_from >= self->vec_num) {
2901 PyErr_SetString(PyExc_AttributeError,
2902 "Vector swizzle: "
2903 "specified axis not present");
2904 return nullptr;
2905 }
2906
2907 vec[axis_to] = self->vec[axis_from];
2908 swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
2909 axis_to++;
2910 }
2911
2912 return Vector_CreatePyObject(vec, axis_to, Py_TYPE(self));
2913}
2914
2926static int Vector_swizzle_set(VectorObject *self, PyObject *value, void *closure)
2927{
2928 size_t size_from;
2929 float scalarVal;
2930
2931 size_t axis_from;
2932 size_t axis_to;
2933
2934 uint swizzleClosure;
2935
2936 float tvec[MAX_DIMENSIONS];
2937 float vec_assign[MAX_DIMENSIONS];
2938
2939 if (BaseMath_ReadCallback_ForWrite(self) == -1) {
2940 return -1;
2941 }
2942
2943 /* Check that the closure can be used with this vector: even 2D vectors have
2944 * swizzles defined for axes z and w, but they would be invalid. */
2945 swizzleClosure = POINTER_AS_INT(closure);
2946 axis_from = 0;
2947
2948 while (swizzleClosure & SWIZZLE_VALID_AXIS) {
2949 axis_to = swizzleClosure & SWIZZLE_AXIS;
2950 if (axis_to >= self->vec_num) {
2951 PyErr_SetString(PyExc_AttributeError,
2952 "Vector swizzle: "
2953 "specified axis not present");
2954 return -1;
2955 }
2956 swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
2957 axis_from++;
2958 }
2959
2960 if (((scalarVal = PyFloat_AsDouble(value)) == -1 && PyErr_Occurred()) == 0) {
2961 int i;
2962
2963 for (i = 0; i < MAX_DIMENSIONS; i++) {
2964 vec_assign[i] = scalarVal;
2965 }
2966
2967 size_from = axis_from;
2968 }
2969 else if (PyErr_Clear(), /* run but ignore the result */
2970 (size_from = size_t(mathutils_array_parse(
2971 vec_assign, 2, 4, value, "Vector.**** = swizzle assignment"))) == size_t(-1))
2972 {
2973 return -1;
2974 }
2975
2976 if (axis_from != size_from) {
2977 PyErr_SetString(PyExc_AttributeError, "Vector swizzle: size does not match swizzle");
2978 return -1;
2979 }
2980
2981 /* Copy vector contents onto swizzled axes. */
2982 axis_from = 0;
2983 swizzleClosure = POINTER_AS_INT(closure);
2984
2985 /* We must first copy current vec into tvec, else some org values may be lost.
2986 * See #31760.
2987 * Swizzle axes are limited to #MAX_DIMENSIONS, values after them are left as-is. */
2988 const int tvec_num = std::min(self->vec_num, MAX_DIMENSIONS);
2989 memcpy(tvec, self->vec, tvec_num * sizeof(float));
2990
2991 while (swizzleClosure & SWIZZLE_VALID_AXIS) {
2992 axis_to = swizzleClosure & SWIZZLE_AXIS;
2993 tvec[axis_to] = vec_assign[axis_from];
2994 swizzleClosure = swizzleClosure >> SWIZZLE_BITS_PER_AXIS;
2995 axis_from++;
2996 }
2997
2998 /* We must copy back the whole tvec into vec, else some changes may be lost (e.g. xz...).
2999 * See #31760. */
3000 memcpy(self->vec, tvec, tvec_num * sizeof(float));
3001 /* continue with BaseMathObject_WriteCallback at the end */
3002
3003 if (BaseMath_WriteCallback(self) == -1) {
3004 return -1;
3005 }
3006
3007 return 0;
3008}
3009
3010#define _SWIZZLE1(a) ((a) | SWIZZLE_VALID_AXIS)
3011#define _SWIZZLE2(a, b) (_SWIZZLE1(a) | (((b) | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS)))
3012#define _SWIZZLE3(a, b, c) \
3013 (_SWIZZLE2(a, b) | (((c) | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 2)))
3014#define _SWIZZLE4(a, b, c, d) \
3015 (_SWIZZLE3(a, b, c) | (((d) | SWIZZLE_VALID_AXIS) << (SWIZZLE_BITS_PER_AXIS * 3)))
3016
3017#define SWIZZLE2(a, b) POINTER_FROM_INT(_SWIZZLE2(a, b))
3018#define SWIZZLE3(a, b, c) POINTER_FROM_INT(_SWIZZLE3(a, b, c))
3019#define SWIZZLE4(a, b, c, d) POINTER_FROM_INT(_SWIZZLE4(a, b, c, d))
3020
3021#define VECTOR_SWIZZLE2_RW_DEF(attr, a, b) \
3022 { \
3023 attr, \
3024 (getter)Vector_swizzle_get, \
3025 (setter)Vector_swizzle_set, \
3026 Vector_swizzle_doc, \
3027 SWIZZLE2(a, b), \
3028 }
3029#define VECTOR_SWIZZLE2_RO_DEF(attr, a, b) \
3030 { \
3031 attr, \
3032 (getter)Vector_swizzle_get, \
3033 (setter) nullptr, \
3034 Vector_swizzle_doc, \
3035 SWIZZLE2(a, b), \
3036 }
3037#define VECTOR_SWIZZLE3_RW_DEF(attr, a, b, c) \
3038 { \
3039 attr, \
3040 (getter)Vector_swizzle_get, \
3041 (setter)Vector_swizzle_set, \
3042 Vector_swizzle_doc, \
3043 SWIZZLE3(a, b, c), \
3044 }
3045#define VECTOR_SWIZZLE3_RO_DEF(attr, a, b, c) \
3046 { \
3047 attr, \
3048 (getter)Vector_swizzle_get, \
3049 (setter) nullptr, \
3050 Vector_swizzle_doc, \
3051 SWIZZLE3(a, b, c), \
3052 }
3053#define VECTOR_SWIZZLE4_RW_DEF(attr, a, b, c, d) \
3054 { \
3055 attr, \
3056 (getter)Vector_swizzle_get, \
3057 (setter)Vector_swizzle_set, \
3058 Vector_swizzle_doc, \
3059 SWIZZLE4(a, b, c, d), \
3060 }
3061#define VECTOR_SWIZZLE4_RO_DEF(attr, a, b, c, d) \
3062 {attr, (getter)Vector_swizzle_get, (setter) nullptr, Vector_swizzle_doc, SWIZZLE4(a, b, c, d)}
3063
3065
3066/* -------------------------------------------------------------------- */
3069
3070#ifdef __GNUC__
3071# ifdef __clang__
3072# pragma clang diagnostic push
3073# pragma clang diagnostic ignored "-Wcast-function-type"
3074# else
3075# pragma GCC diagnostic push
3076# pragma GCC diagnostic ignored "-Wcast-function-type"
3077# endif
3078#endif
3079
3080static PyGetSetDef Vector_getseters[] = {
3081 {"x",
3082 reinterpret_cast<getter>(Vector_axis_get),
3083 reinterpret_cast<setter>(Vector_axis_set),
3084 Vector_axis_x_doc,
3085 POINTER_FROM_INT(0)},
3086 {"y",
3087 reinterpret_cast<getter>(Vector_axis_get),
3088 reinterpret_cast<setter>(Vector_axis_set),
3089 Vector_axis_y_doc,
3090 POINTER_FROM_INT(1)},
3091 {"z",
3092 reinterpret_cast<getter>(Vector_axis_get),
3093 reinterpret_cast<setter>(Vector_axis_set),
3094 Vector_axis_z_doc,
3095 POINTER_FROM_INT(2)},
3096 {"w",
3097 reinterpret_cast<getter>(Vector_axis_get),
3098 reinterpret_cast<setter>(Vector_axis_set),
3099 Vector_axis_w_doc,
3100 POINTER_FROM_INT(3)},
3101 {"length",
3102 reinterpret_cast<getter>(Vector_length_get),
3103 reinterpret_cast<setter>(Vector_length_set),
3104 Vector_length_doc,
3105 nullptr},
3106 {"length_squared",
3107 reinterpret_cast<getter>(Vector_length_squared_get),
3108 static_cast<setter>(nullptr),
3109 Vector_length_squared_doc,
3110 nullptr},
3111 {"magnitude",
3112 reinterpret_cast<getter>(Vector_length_get),
3113 reinterpret_cast<setter>(Vector_length_set),
3114 Vector_length_doc,
3115 nullptr},
3116 {"is_wrapped",
3117 reinterpret_cast<getter>(BaseMathObject_is_wrapped_get),
3118 static_cast<setter>(nullptr),
3120 nullptr},
3121 {"is_frozen",
3122 reinterpret_cast<getter>(BaseMathObject_is_frozen_get),
3123 static_cast<setter>(nullptr),
3125 nullptr},
3126 {"is_valid",
3127 reinterpret_cast<getter>(BaseMathObject_is_valid_get),
3128 static_cast<setter>(nullptr),
3130 nullptr},
3131 {"owner",
3132 reinterpret_cast<getter>(BaseMathObject_owner_get),
3133 static_cast<setter>(nullptr),
3135 nullptr},
3136
3137 /* Auto-generated swizzle attributes, see Python script above. */
3138 VECTOR_SWIZZLE2_RO_DEF("xx", 0, 0),
3139 VECTOR_SWIZZLE3_RO_DEF("xxx", 0, 0, 0),
3140 VECTOR_SWIZZLE4_RO_DEF("xxxx", 0, 0, 0, 0),
3141 VECTOR_SWIZZLE4_RO_DEF("xxxy", 0, 0, 0, 1),
3142 VECTOR_SWIZZLE4_RO_DEF("xxxz", 0, 0, 0, 2),
3143 VECTOR_SWIZZLE4_RO_DEF("xxxw", 0, 0, 0, 3),
3144 VECTOR_SWIZZLE3_RO_DEF("xxy", 0, 0, 1),
3145 VECTOR_SWIZZLE4_RO_DEF("xxyx", 0, 0, 1, 0),
3146 VECTOR_SWIZZLE4_RO_DEF("xxyy", 0, 0, 1, 1),
3147 VECTOR_SWIZZLE4_RO_DEF("xxyz", 0, 0, 1, 2),
3148 VECTOR_SWIZZLE4_RO_DEF("xxyw", 0, 0, 1, 3),
3149 VECTOR_SWIZZLE3_RO_DEF("xxz", 0, 0, 2),
3150 VECTOR_SWIZZLE4_RO_DEF("xxzx", 0, 0, 2, 0),
3151 VECTOR_SWIZZLE4_RO_DEF("xxzy", 0, 0, 2, 1),
3152 VECTOR_SWIZZLE4_RO_DEF("xxzz", 0, 0, 2, 2),
3153 VECTOR_SWIZZLE4_RO_DEF("xxzw", 0, 0, 2, 3),
3154 VECTOR_SWIZZLE3_RO_DEF("xxw", 0, 0, 3),
3155 VECTOR_SWIZZLE4_RO_DEF("xxwx", 0, 0, 3, 0),
3156 VECTOR_SWIZZLE4_RO_DEF("xxwy", 0, 0, 3, 1),
3157 VECTOR_SWIZZLE4_RO_DEF("xxwz", 0, 0, 3, 2),
3158 VECTOR_SWIZZLE4_RO_DEF("xxww", 0, 0, 3, 3),
3159 VECTOR_SWIZZLE2_RW_DEF("xy", 0, 1),
3160 VECTOR_SWIZZLE3_RO_DEF("xyx", 0, 1, 0),
3161 VECTOR_SWIZZLE4_RO_DEF("xyxx", 0, 1, 0, 0),
3162 VECTOR_SWIZZLE4_RO_DEF("xyxy", 0, 1, 0, 1),
3163 VECTOR_SWIZZLE4_RO_DEF("xyxz", 0, 1, 0, 2),
3164 VECTOR_SWIZZLE4_RO_DEF("xyxw", 0, 1, 0, 3),
3165 VECTOR_SWIZZLE3_RO_DEF("xyy", 0, 1, 1),
3166 VECTOR_SWIZZLE4_RO_DEF("xyyx", 0, 1, 1, 0),
3167 VECTOR_SWIZZLE4_RO_DEF("xyyy", 0, 1, 1, 1),
3168 VECTOR_SWIZZLE4_RO_DEF("xyyz", 0, 1, 1, 2),
3169 VECTOR_SWIZZLE4_RO_DEF("xyyw", 0, 1, 1, 3),
3170 VECTOR_SWIZZLE3_RW_DEF("xyz", 0, 1, 2),
3171 VECTOR_SWIZZLE4_RO_DEF("xyzx", 0, 1, 2, 0),
3172 VECTOR_SWIZZLE4_RO_DEF("xyzy", 0, 1, 2, 1),
3173 VECTOR_SWIZZLE4_RO_DEF("xyzz", 0, 1, 2, 2),
3174 VECTOR_SWIZZLE4_RW_DEF("xyzw", 0, 1, 2, 3),
3175 VECTOR_SWIZZLE3_RW_DEF("xyw", 0, 1, 3),
3176 VECTOR_SWIZZLE4_RO_DEF("xywx", 0, 1, 3, 0),
3177 VECTOR_SWIZZLE4_RO_DEF("xywy", 0, 1, 3, 1),
3178 VECTOR_SWIZZLE4_RW_DEF("xywz", 0, 1, 3, 2),
3179 VECTOR_SWIZZLE4_RO_DEF("xyww", 0, 1, 3, 3),
3180 VECTOR_SWIZZLE2_RW_DEF("xz", 0, 2),
3181 VECTOR_SWIZZLE3_RO_DEF("xzx", 0, 2, 0),
3182 VECTOR_SWIZZLE4_RO_DEF("xzxx", 0, 2, 0, 0),
3183 VECTOR_SWIZZLE4_RO_DEF("xzxy", 0, 2, 0, 1),
3184 VECTOR_SWIZZLE4_RO_DEF("xzxz", 0, 2, 0, 2),
3185 VECTOR_SWIZZLE4_RO_DEF("xzxw", 0, 2, 0, 3),
3186 VECTOR_SWIZZLE3_RW_DEF("xzy", 0, 2, 1),
3187 VECTOR_SWIZZLE4_RO_DEF("xzyx", 0, 2, 1, 0),
3188 VECTOR_SWIZZLE4_RO_DEF("xzyy", 0, 2, 1, 1),
3189 VECTOR_SWIZZLE4_RO_DEF("xzyz", 0, 2, 1, 2),
3190 VECTOR_SWIZZLE4_RW_DEF("xzyw", 0, 2, 1, 3),
3191 VECTOR_SWIZZLE3_RO_DEF("xzz", 0, 2, 2),
3192 VECTOR_SWIZZLE4_RO_DEF("xzzx", 0, 2, 2, 0),
3193 VECTOR_SWIZZLE4_RO_DEF("xzzy", 0, 2, 2, 1),
3194 VECTOR_SWIZZLE4_RO_DEF("xzzz", 0, 2, 2, 2),
3195 VECTOR_SWIZZLE4_RO_DEF("xzzw", 0, 2, 2, 3),
3196 VECTOR_SWIZZLE3_RW_DEF("xzw", 0, 2, 3),
3197 VECTOR_SWIZZLE4_RO_DEF("xzwx", 0, 2, 3, 0),
3198 VECTOR_SWIZZLE4_RW_DEF("xzwy", 0, 2, 3, 1),
3199 VECTOR_SWIZZLE4_RO_DEF("xzwz", 0, 2, 3, 2),
3200 VECTOR_SWIZZLE4_RO_DEF("xzww", 0, 2, 3, 3),
3201 VECTOR_SWIZZLE2_RW_DEF("xw", 0, 3),
3202 VECTOR_SWIZZLE3_RO_DEF("xwx", 0, 3, 0),
3203 VECTOR_SWIZZLE4_RO_DEF("xwxx", 0, 3, 0, 0),
3204 VECTOR_SWIZZLE4_RO_DEF("xwxy", 0, 3, 0, 1),
3205 VECTOR_SWIZZLE4_RO_DEF("xwxz", 0, 3, 0, 2),
3206 VECTOR_SWIZZLE4_RO_DEF("xwxw", 0, 3, 0, 3),
3207 VECTOR_SWIZZLE3_RW_DEF("xwy", 0, 3, 1),
3208 VECTOR_SWIZZLE4_RO_DEF("xwyx", 0, 3, 1, 0),
3209 VECTOR_SWIZZLE4_RO_DEF("xwyy", 0, 3, 1, 1),
3210 VECTOR_SWIZZLE4_RW_DEF("xwyz", 0, 3, 1, 2),
3211 VECTOR_SWIZZLE4_RO_DEF("xwyw", 0, 3, 1, 3),
3212 VECTOR_SWIZZLE3_RW_DEF("xwz", 0, 3, 2),
3213 VECTOR_SWIZZLE4_RO_DEF("xwzx", 0, 3, 2, 0),
3214 VECTOR_SWIZZLE4_RW_DEF("xwzy", 0, 3, 2, 1),
3215 VECTOR_SWIZZLE4_RO_DEF("xwzz", 0, 3, 2, 2),
3216 VECTOR_SWIZZLE4_RO_DEF("xwzw", 0, 3, 2, 3),
3217 VECTOR_SWIZZLE3_RO_DEF("xww", 0, 3, 3),
3218 VECTOR_SWIZZLE4_RO_DEF("xwwx", 0, 3, 3, 0),
3219 VECTOR_SWIZZLE4_RO_DEF("xwwy", 0, 3, 3, 1),
3220 VECTOR_SWIZZLE4_RO_DEF("xwwz", 0, 3, 3, 2),
3221 VECTOR_SWIZZLE4_RO_DEF("xwww", 0, 3, 3, 3),
3222 VECTOR_SWIZZLE2_RW_DEF("yx", 1, 0),
3223 VECTOR_SWIZZLE3_RO_DEF("yxx", 1, 0, 0),
3224 VECTOR_SWIZZLE4_RO_DEF("yxxx", 1, 0, 0, 0),
3225 VECTOR_SWIZZLE4_RO_DEF("yxxy", 1, 0, 0, 1),
3226 VECTOR_SWIZZLE4_RO_DEF("yxxz", 1, 0, 0, 2),
3227 VECTOR_SWIZZLE4_RO_DEF("yxxw", 1, 0, 0, 3),
3228 VECTOR_SWIZZLE3_RO_DEF("yxy", 1, 0, 1),
3229 VECTOR_SWIZZLE4_RO_DEF("yxyx", 1, 0, 1, 0),
3230 VECTOR_SWIZZLE4_RO_DEF("yxyy", 1, 0, 1, 1),
3231 VECTOR_SWIZZLE4_RO_DEF("yxyz", 1, 0, 1, 2),
3232 VECTOR_SWIZZLE4_RO_DEF("yxyw", 1, 0, 1, 3),
3233 VECTOR_SWIZZLE3_RW_DEF("yxz", 1, 0, 2),
3234 VECTOR_SWIZZLE4_RO_DEF("yxzx", 1, 0, 2, 0),
3235 VECTOR_SWIZZLE4_RO_DEF("yxzy", 1, 0, 2, 1),
3236 VECTOR_SWIZZLE4_RO_DEF("yxzz", 1, 0, 2, 2),
3237 VECTOR_SWIZZLE4_RW_DEF("yxzw", 1, 0, 2, 3),
3238 VECTOR_SWIZZLE3_RW_DEF("yxw", 1, 0, 3),
3239 VECTOR_SWIZZLE4_RO_DEF("yxwx", 1, 0, 3, 0),
3240 VECTOR_SWIZZLE4_RO_DEF("yxwy", 1, 0, 3, 1),
3241 VECTOR_SWIZZLE4_RW_DEF("yxwz", 1, 0, 3, 2),
3242 VECTOR_SWIZZLE4_RO_DEF("yxww", 1, 0, 3, 3),
3243 VECTOR_SWIZZLE2_RO_DEF("yy", 1, 1),
3244 VECTOR_SWIZZLE3_RO_DEF("yyx", 1, 1, 0),
3245 VECTOR_SWIZZLE4_RO_DEF("yyxx", 1, 1, 0, 0),
3246 VECTOR_SWIZZLE4_RO_DEF("yyxy", 1, 1, 0, 1),
3247 VECTOR_SWIZZLE4_RO_DEF("yyxz", 1, 1, 0, 2),
3248 VECTOR_SWIZZLE4_RO_DEF("yyxw", 1, 1, 0, 3),
3249 VECTOR_SWIZZLE3_RO_DEF("yyy", 1, 1, 1),
3250 VECTOR_SWIZZLE4_RO_DEF("yyyx", 1, 1, 1, 0),
3251 VECTOR_SWIZZLE4_RO_DEF("yyyy", 1, 1, 1, 1),
3252 VECTOR_SWIZZLE4_RO_DEF("yyyz", 1, 1, 1, 2),
3253 VECTOR_SWIZZLE4_RO_DEF("yyyw", 1, 1, 1, 3),
3254 VECTOR_SWIZZLE3_RO_DEF("yyz", 1, 1, 2),
3255 VECTOR_SWIZZLE4_RO_DEF("yyzx", 1, 1, 2, 0),
3256 VECTOR_SWIZZLE4_RO_DEF("yyzy", 1, 1, 2, 1),
3257 VECTOR_SWIZZLE4_RO_DEF("yyzz", 1, 1, 2, 2),
3258 VECTOR_SWIZZLE4_RO_DEF("yyzw", 1, 1, 2, 3),
3259 VECTOR_SWIZZLE3_RO_DEF("yyw", 1, 1, 3),
3260 VECTOR_SWIZZLE4_RO_DEF("yywx", 1, 1, 3, 0),
3261 VECTOR_SWIZZLE4_RO_DEF("yywy", 1, 1, 3, 1),
3262 VECTOR_SWIZZLE4_RO_DEF("yywz", 1, 1, 3, 2),
3263 VECTOR_SWIZZLE4_RO_DEF("yyww", 1, 1, 3, 3),
3264 VECTOR_SWIZZLE2_RW_DEF("yz", 1, 2),
3265 VECTOR_SWIZZLE3_RW_DEF("yzx", 1, 2, 0),
3266 VECTOR_SWIZZLE4_RO_DEF("yzxx", 1, 2, 0, 0),
3267 VECTOR_SWIZZLE4_RO_DEF("yzxy", 1, 2, 0, 1),
3268 VECTOR_SWIZZLE4_RO_DEF("yzxz", 1, 2, 0, 2),
3269 VECTOR_SWIZZLE4_RW_DEF("yzxw", 1, 2, 0, 3),
3270 VECTOR_SWIZZLE3_RO_DEF("yzy", 1, 2, 1),
3271 VECTOR_SWIZZLE4_RO_DEF("yzyx", 1, 2, 1, 0),
3272 VECTOR_SWIZZLE4_RO_DEF("yzyy", 1, 2, 1, 1),
3273 VECTOR_SWIZZLE4_RO_DEF("yzyz", 1, 2, 1, 2),
3274 VECTOR_SWIZZLE4_RO_DEF("yzyw", 1, 2, 1, 3),
3275 VECTOR_SWIZZLE3_RO_DEF("yzz", 1, 2, 2),
3276 VECTOR_SWIZZLE4_RO_DEF("yzzx", 1, 2, 2, 0),
3277 VECTOR_SWIZZLE4_RO_DEF("yzzy", 1, 2, 2, 1),
3278 VECTOR_SWIZZLE4_RO_DEF("yzzz", 1, 2, 2, 2),
3279 VECTOR_SWIZZLE4_RO_DEF("yzzw", 1, 2, 2, 3),
3280 VECTOR_SWIZZLE3_RW_DEF("yzw", 1, 2, 3),
3281 VECTOR_SWIZZLE4_RW_DEF("yzwx", 1, 2, 3, 0),
3282 VECTOR_SWIZZLE4_RO_DEF("yzwy", 1, 2, 3, 1),
3283 VECTOR_SWIZZLE4_RO_DEF("yzwz", 1, 2, 3, 2),
3284 VECTOR_SWIZZLE4_RO_DEF("yzww", 1, 2, 3, 3),
3285 VECTOR_SWIZZLE2_RW_DEF("yw", 1, 3),
3286 VECTOR_SWIZZLE3_RW_DEF("ywx", 1, 3, 0),
3287 VECTOR_SWIZZLE4_RO_DEF("ywxx", 1, 3, 0, 0),
3288 VECTOR_SWIZZLE4_RO_DEF("ywxy", 1, 3, 0, 1),
3289 VECTOR_SWIZZLE4_RW_DEF("ywxz", 1, 3, 0, 2),
3290 VECTOR_SWIZZLE4_RO_DEF("ywxw", 1, 3, 0, 3),
3291 VECTOR_SWIZZLE3_RO_DEF("ywy", 1, 3, 1),
3292 VECTOR_SWIZZLE4_RO_DEF("ywyx", 1, 3, 1, 0),
3293 VECTOR_SWIZZLE4_RO_DEF("ywyy", 1, 3, 1, 1),
3294 VECTOR_SWIZZLE4_RO_DEF("ywyz", 1, 3, 1, 2),
3295 VECTOR_SWIZZLE4_RO_DEF("ywyw", 1, 3, 1, 3),
3296 VECTOR_SWIZZLE3_RW_DEF("ywz", 1, 3, 2),
3297 VECTOR_SWIZZLE4_RW_DEF("ywzx", 1, 3, 2, 0),
3298 VECTOR_SWIZZLE4_RO_DEF("ywzy", 1, 3, 2, 1),
3299 VECTOR_SWIZZLE4_RO_DEF("ywzz", 1, 3, 2, 2),
3300 VECTOR_SWIZZLE4_RO_DEF("ywzw", 1, 3, 2, 3),
3301 VECTOR_SWIZZLE3_RO_DEF("yww", 1, 3, 3),
3302 VECTOR_SWIZZLE4_RO_DEF("ywwx", 1, 3, 3, 0),
3303 VECTOR_SWIZZLE4_RO_DEF("ywwy", 1, 3, 3, 1),
3304 VECTOR_SWIZZLE4_RO_DEF("ywwz", 1, 3, 3, 2),
3305 VECTOR_SWIZZLE4_RO_DEF("ywww", 1, 3, 3, 3),
3306 VECTOR_SWIZZLE2_RW_DEF("zx", 2, 0),
3307 VECTOR_SWIZZLE3_RO_DEF("zxx", 2, 0, 0),
3308 VECTOR_SWIZZLE4_RO_DEF("zxxx", 2, 0, 0, 0),
3309 VECTOR_SWIZZLE4_RO_DEF("zxxy", 2, 0, 0, 1),
3310 VECTOR_SWIZZLE4_RO_DEF("zxxz", 2, 0, 0, 2),
3311 VECTOR_SWIZZLE4_RO_DEF("zxxw", 2, 0, 0, 3),
3312 VECTOR_SWIZZLE3_RW_DEF("zxy", 2, 0, 1),
3313 VECTOR_SWIZZLE4_RO_DEF("zxyx", 2, 0, 1, 0),
3314 VECTOR_SWIZZLE4_RO_DEF("zxyy", 2, 0, 1, 1),
3315 VECTOR_SWIZZLE4_RO_DEF("zxyz", 2, 0, 1, 2),
3316 VECTOR_SWIZZLE4_RW_DEF("zxyw", 2, 0, 1, 3),
3317 VECTOR_SWIZZLE3_RO_DEF("zxz", 2, 0, 2),
3318 VECTOR_SWIZZLE4_RO_DEF("zxzx", 2, 0, 2, 0),
3319 VECTOR_SWIZZLE4_RO_DEF("zxzy", 2, 0, 2, 1),
3320 VECTOR_SWIZZLE4_RO_DEF("zxzz", 2, 0, 2, 2),
3321 VECTOR_SWIZZLE4_RO_DEF("zxzw", 2, 0, 2, 3),
3322 VECTOR_SWIZZLE3_RW_DEF("zxw", 2, 0, 3),
3323 VECTOR_SWIZZLE4_RO_DEF("zxwx", 2, 0, 3, 0),
3324 VECTOR_SWIZZLE4_RW_DEF("zxwy", 2, 0, 3, 1),
3325 VECTOR_SWIZZLE4_RO_DEF("zxwz", 2, 0, 3, 2),
3326 VECTOR_SWIZZLE4_RO_DEF("zxww", 2, 0, 3, 3),
3327 VECTOR_SWIZZLE2_RW_DEF("zy", 2, 1),
3328 VECTOR_SWIZZLE3_RW_DEF("zyx", 2, 1, 0),
3329 VECTOR_SWIZZLE4_RO_DEF("zyxx", 2, 1, 0, 0),
3330 VECTOR_SWIZZLE4_RO_DEF("zyxy", 2, 1, 0, 1),
3331 VECTOR_SWIZZLE4_RO_DEF("zyxz", 2, 1, 0, 2),
3332 VECTOR_SWIZZLE4_RW_DEF("zyxw", 2, 1, 0, 3),
3333 VECTOR_SWIZZLE3_RO_DEF("zyy", 2, 1, 1),
3334 VECTOR_SWIZZLE4_RO_DEF("zyyx", 2, 1, 1, 0),
3335 VECTOR_SWIZZLE4_RO_DEF("zyyy", 2, 1, 1, 1),
3336 VECTOR_SWIZZLE4_RO_DEF("zyyz", 2, 1, 1, 2),
3337 VECTOR_SWIZZLE4_RO_DEF("zyyw", 2, 1, 1, 3),
3338 VECTOR_SWIZZLE3_RO_DEF("zyz", 2, 1, 2),
3339 VECTOR_SWIZZLE4_RO_DEF("zyzx", 2, 1, 2, 0),
3340 VECTOR_SWIZZLE4_RO_DEF("zyzy", 2, 1, 2, 1),
3341 VECTOR_SWIZZLE4_RO_DEF("zyzz", 2, 1, 2, 2),
3342 VECTOR_SWIZZLE4_RO_DEF("zyzw", 2, 1, 2, 3),
3343 VECTOR_SWIZZLE3_RW_DEF("zyw", 2, 1, 3),
3344 VECTOR_SWIZZLE4_RW_DEF("zywx", 2, 1, 3, 0),
3345 VECTOR_SWIZZLE4_RO_DEF("zywy", 2, 1, 3, 1),
3346 VECTOR_SWIZZLE4_RO_DEF("zywz", 2, 1, 3, 2),
3347 VECTOR_SWIZZLE4_RO_DEF("zyww", 2, 1, 3, 3),
3348 VECTOR_SWIZZLE2_RO_DEF("zz", 2, 2),
3349 VECTOR_SWIZZLE3_RO_DEF("zzx", 2, 2, 0),
3350 VECTOR_SWIZZLE4_RO_DEF("zzxx", 2, 2, 0, 0),
3351 VECTOR_SWIZZLE4_RO_DEF("zzxy", 2, 2, 0, 1),
3352 VECTOR_SWIZZLE4_RO_DEF("zzxz", 2, 2, 0, 2),
3353 VECTOR_SWIZZLE4_RO_DEF("zzxw", 2, 2, 0, 3),
3354 VECTOR_SWIZZLE3_RO_DEF("zzy", 2, 2, 1),
3355 VECTOR_SWIZZLE4_RO_DEF("zzyx", 2, 2, 1, 0),
3356 VECTOR_SWIZZLE4_RO_DEF("zzyy", 2, 2, 1, 1),
3357 VECTOR_SWIZZLE4_RO_DEF("zzyz", 2, 2, 1, 2),
3358 VECTOR_SWIZZLE4_RO_DEF("zzyw", 2, 2, 1, 3),
3359 VECTOR_SWIZZLE3_RO_DEF("zzz", 2, 2, 2),
3360 VECTOR_SWIZZLE4_RO_DEF("zzzx", 2, 2, 2, 0),
3361 VECTOR_SWIZZLE4_RO_DEF("zzzy", 2, 2, 2, 1),
3362 VECTOR_SWIZZLE4_RO_DEF("zzzz", 2, 2, 2, 2),
3363 VECTOR_SWIZZLE4_RO_DEF("zzzw", 2, 2, 2, 3),
3364 VECTOR_SWIZZLE3_RO_DEF("zzw", 2, 2, 3),
3365 VECTOR_SWIZZLE4_RO_DEF("zzwx", 2, 2, 3, 0),
3366 VECTOR_SWIZZLE4_RO_DEF("zzwy", 2, 2, 3, 1),
3367 VECTOR_SWIZZLE4_RO_DEF("zzwz", 2, 2, 3, 2),
3368 VECTOR_SWIZZLE4_RO_DEF("zzww", 2, 2, 3, 3),
3369 VECTOR_SWIZZLE2_RW_DEF("zw", 2, 3),
3370 VECTOR_SWIZZLE3_RW_DEF("zwx", 2, 3, 0),
3371 VECTOR_SWIZZLE4_RO_DEF("zwxx", 2, 3, 0, 0),
3372 VECTOR_SWIZZLE4_RW_DEF("zwxy", 2, 3, 0, 1),
3373 VECTOR_SWIZZLE4_RO_DEF("zwxz", 2, 3, 0, 2),
3374 VECTOR_SWIZZLE4_RO_DEF("zwxw", 2, 3, 0, 3),
3375 VECTOR_SWIZZLE3_RW_DEF("zwy", 2, 3, 1),
3376 VECTOR_SWIZZLE4_RW_DEF("zwyx", 2, 3, 1, 0),
3377 VECTOR_SWIZZLE4_RO_DEF("zwyy", 2, 3, 1, 1),
3378 VECTOR_SWIZZLE4_RO_DEF("zwyz", 2, 3, 1, 2),
3379 VECTOR_SWIZZLE4_RO_DEF("zwyw", 2, 3, 1, 3),
3380 VECTOR_SWIZZLE3_RO_DEF("zwz", 2, 3, 2),
3381 VECTOR_SWIZZLE4_RO_DEF("zwzx", 2, 3, 2, 0),
3382 VECTOR_SWIZZLE4_RO_DEF("zwzy", 2, 3, 2, 1),
3383 VECTOR_SWIZZLE4_RO_DEF("zwzz", 2, 3, 2, 2),
3384 VECTOR_SWIZZLE4_RO_DEF("zwzw", 2, 3, 2, 3),
3385 VECTOR_SWIZZLE3_RO_DEF("zww", 2, 3, 3),
3386 VECTOR_SWIZZLE4_RO_DEF("zwwx", 2, 3, 3, 0),
3387 VECTOR_SWIZZLE4_RO_DEF("zwwy", 2, 3, 3, 1),
3388 VECTOR_SWIZZLE4_RO_DEF("zwwz", 2, 3, 3, 2),
3389 VECTOR_SWIZZLE4_RO_DEF("zwww", 2, 3, 3, 3),
3390 VECTOR_SWIZZLE2_RW_DEF("wx", 3, 0),
3391 VECTOR_SWIZZLE3_RO_DEF("wxx", 3, 0, 0),
3392 VECTOR_SWIZZLE4_RO_DEF("wxxx", 3, 0, 0, 0),
3393 VECTOR_SWIZZLE4_RO_DEF("wxxy", 3, 0, 0, 1),
3394 VECTOR_SWIZZLE4_RO_DEF("wxxz", 3, 0, 0, 2),
3395 VECTOR_SWIZZLE4_RO_DEF("wxxw", 3, 0, 0, 3),
3396 VECTOR_SWIZZLE3_RW_DEF("wxy", 3, 0, 1),
3397 VECTOR_SWIZZLE4_RO_DEF("wxyx", 3, 0, 1, 0),
3398 VECTOR_SWIZZLE4_RO_DEF("wxyy", 3, 0, 1, 1),
3399 VECTOR_SWIZZLE4_RW_DEF("wxyz", 3, 0, 1, 2),
3400 VECTOR_SWIZZLE4_RO_DEF("wxyw", 3, 0, 1, 3),
3401 VECTOR_SWIZZLE3_RW_DEF("wxz", 3, 0, 2),
3402 VECTOR_SWIZZLE4_RO_DEF("wxzx", 3, 0, 2, 0),
3403 VECTOR_SWIZZLE4_RW_DEF("wxzy", 3, 0, 2, 1),
3404 VECTOR_SWIZZLE4_RO_DEF("wxzz", 3, 0, 2, 2),
3405 VECTOR_SWIZZLE4_RO_DEF("wxzw", 3, 0, 2, 3),
3406 VECTOR_SWIZZLE3_RO_DEF("wxw", 3, 0, 3),
3407 VECTOR_SWIZZLE4_RO_DEF("wxwx", 3, 0, 3, 0),
3408 VECTOR_SWIZZLE4_RO_DEF("wxwy", 3, 0, 3, 1),
3409 VECTOR_SWIZZLE4_RO_DEF("wxwz", 3, 0, 3, 2),
3410 VECTOR_SWIZZLE4_RO_DEF("wxww", 3, 0, 3, 3),
3411 VECTOR_SWIZZLE2_RW_DEF("wy", 3, 1),
3412 VECTOR_SWIZZLE3_RW_DEF("wyx", 3, 1, 0),
3413 VECTOR_SWIZZLE4_RO_DEF("wyxx", 3, 1, 0, 0),
3414 VECTOR_SWIZZLE4_RO_DEF("wyxy", 3, 1, 0, 1),
3415 VECTOR_SWIZZLE4_RW_DEF("wyxz", 3, 1, 0, 2),
3416 VECTOR_SWIZZLE4_RO_DEF("wyxw", 3, 1, 0, 3),
3417 VECTOR_SWIZZLE3_RO_DEF("wyy", 3, 1, 1),
3418 VECTOR_SWIZZLE4_RO_DEF("wyyx", 3, 1, 1, 0),
3419 VECTOR_SWIZZLE4_RO_DEF("wyyy", 3, 1, 1, 1),
3420 VECTOR_SWIZZLE4_RO_DEF("wyyz", 3, 1, 1, 2),
3421 VECTOR_SWIZZLE4_RO_DEF("wyyw", 3, 1, 1, 3),
3422 VECTOR_SWIZZLE3_RW_DEF("wyz", 3, 1, 2),
3423 VECTOR_SWIZZLE4_RW_DEF("wyzx", 3, 1, 2, 0),
3424 VECTOR_SWIZZLE4_RO_DEF("wyzy", 3, 1, 2, 1),
3425 VECTOR_SWIZZLE4_RO_DEF("wyzz", 3, 1, 2, 2),
3426 VECTOR_SWIZZLE4_RO_DEF("wyzw", 3, 1, 2, 3),
3427 VECTOR_SWIZZLE3_RO_DEF("wyw", 3, 1, 3),
3428 VECTOR_SWIZZLE4_RO_DEF("wywx", 3, 1, 3, 0),
3429 VECTOR_SWIZZLE4_RO_DEF("wywy", 3, 1, 3, 1),
3430 VECTOR_SWIZZLE4_RO_DEF("wywz", 3, 1, 3, 2),
3431 VECTOR_SWIZZLE4_RO_DEF("wyww", 3, 1, 3, 3),
3432 VECTOR_SWIZZLE2_RW_DEF("wz", 3, 2),
3433 VECTOR_SWIZZLE3_RW_DEF("wzx", 3, 2, 0),
3434 VECTOR_SWIZZLE4_RO_DEF("wzxx", 3, 2, 0, 0),
3435 VECTOR_SWIZZLE4_RW_DEF("wzxy", 3, 2, 0, 1),
3436 VECTOR_SWIZZLE4_RO_DEF("wzxz", 3, 2, 0, 2),
3437 VECTOR_SWIZZLE4_RO_DEF("wzxw", 3, 2, 0, 3),
3438 VECTOR_SWIZZLE3_RW_DEF("wzy", 3, 2, 1),
3439 VECTOR_SWIZZLE4_RW_DEF("wzyx", 3, 2, 1, 0),
3440 VECTOR_SWIZZLE4_RO_DEF("wzyy", 3, 2, 1, 1),
3441 VECTOR_SWIZZLE4_RO_DEF("wzyz", 3, 2, 1, 2),
3442 VECTOR_SWIZZLE4_RO_DEF("wzyw", 3, 2, 1, 3),
3443 VECTOR_SWIZZLE3_RO_DEF("wzz", 3, 2, 2),
3444 VECTOR_SWIZZLE4_RO_DEF("wzzx", 3, 2, 2, 0),
3445 VECTOR_SWIZZLE4_RO_DEF("wzzy", 3, 2, 2, 1),
3446 VECTOR_SWIZZLE4_RO_DEF("wzzz", 3, 2, 2, 2),
3447 VECTOR_SWIZZLE4_RO_DEF("wzzw", 3, 2, 2, 3),
3448 VECTOR_SWIZZLE3_RO_DEF("wzw", 3, 2, 3),
3449 VECTOR_SWIZZLE4_RO_DEF("wzwx", 3, 2, 3, 0),
3450 VECTOR_SWIZZLE4_RO_DEF("wzwy", 3, 2, 3, 1),
3451 VECTOR_SWIZZLE4_RO_DEF("wzwz", 3, 2, 3, 2),
3452 VECTOR_SWIZZLE4_RO_DEF("wzww", 3, 2, 3, 3),
3453 VECTOR_SWIZZLE2_RO_DEF("ww", 3, 3),
3454 VECTOR_SWIZZLE3_RO_DEF("wwx", 3, 3, 0),
3455 VECTOR_SWIZZLE4_RO_DEF("wwxx", 3, 3, 0, 0),
3456 VECTOR_SWIZZLE4_RO_DEF("wwxy", 3, 3, 0, 1),
3457 VECTOR_SWIZZLE4_RO_DEF("wwxz", 3, 3, 0, 2),
3458 VECTOR_SWIZZLE4_RO_DEF("wwxw", 3, 3, 0, 3),
3459 VECTOR_SWIZZLE3_RO_DEF("wwy", 3, 3, 1),
3460 VECTOR_SWIZZLE4_RO_DEF("wwyx", 3, 3, 1, 0),
3461 VECTOR_SWIZZLE4_RO_DEF("wwyy", 3, 3, 1, 1),
3462 VECTOR_SWIZZLE4_RO_DEF("wwyz", 3, 3, 1, 2),
3463 VECTOR_SWIZZLE4_RO_DEF("wwyw", 3, 3, 1, 3),
3464 VECTOR_SWIZZLE3_RO_DEF("wwz", 3, 3, 2),
3465 VECTOR_SWIZZLE4_RO_DEF("wwzx", 3, 3, 2, 0),
3466 VECTOR_SWIZZLE4_RO_DEF("wwzy", 3, 3, 2, 1),
3467 VECTOR_SWIZZLE4_RO_DEF("wwzz", 3, 3, 2, 2),
3468 VECTOR_SWIZZLE4_RO_DEF("wwzw", 3, 3, 2, 3),
3469 VECTOR_SWIZZLE3_RO_DEF("www", 3, 3, 3),
3470 VECTOR_SWIZZLE4_RO_DEF("wwwx", 3, 3, 3, 0),
3471 VECTOR_SWIZZLE4_RO_DEF("wwwy", 3, 3, 3, 1),
3472 VECTOR_SWIZZLE4_RO_DEF("wwwz", 3, 3, 3, 2),
3473 VECTOR_SWIZZLE4_RO_DEF("wwww", 3, 3, 3, 3),
3474
3475#undef AXIS_FROM_CHAR
3476#undef SWIZZLE1
3477#undef SWIZZLE2
3478#undef SWIZZLE3
3479#undef SWIZZLE4
3480#undef _SWIZZLE1
3481#undef _SWIZZLE2
3482#undef _SWIZZLE3
3483#undef _SWIZZLE4
3484
3485#undef VECTOR_SWIZZLE2_RW_DEF
3486#undef VECTOR_SWIZZLE2_RO_DEF
3487#undef VECTOR_SWIZZLE3_RW_DEF
3488#undef VECTOR_SWIZZLE3_RO_DEF
3489#undef VECTOR_SWIZZLE4_RW_DEF
3490#undef VECTOR_SWIZZLE4_RO_DEF
3491
3492 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
3493};
3494
3495#ifdef __GNUC__
3496# ifdef __clang__
3497# pragma clang diagnostic pop
3498# else
3499# pragma GCC diagnostic pop
3500# endif
3501#endif
3502
3504
3505/* -------------------------------------------------------------------- */
3508
3509#ifdef __GNUC__
3510# ifdef __clang__
3511# pragma clang diagnostic push
3512# pragma clang diagnostic ignored "-Wcast-function-type"
3513# else
3514# pragma GCC diagnostic push
3515# pragma GCC diagnostic ignored "-Wcast-function-type"
3516# endif
3517#endif
3518
3519static PyMethodDef Vector_methods[] = {
3520 /* Class Methods */
3521 {"Fill",
3522 static_cast<PyCFunction>(C_Vector_Fill),
3523 METH_VARARGS | METH_CLASS,
3524 C_Vector_Fill_doc},
3525 {"Range",
3526 static_cast<PyCFunction>(C_Vector_Range),
3527 METH_VARARGS | METH_CLASS,
3528 C_Vector_Range_doc},
3529 {"Linspace",
3530 static_cast<PyCFunction>(C_Vector_Linspace),
3531 METH_VARARGS | METH_CLASS,
3532 C_Vector_Linspace_doc},
3533 {"Repeat",
3534 static_cast<PyCFunction>(C_Vector_Repeat),
3535 METH_VARARGS | METH_CLASS,
3536 C_Vector_Repeat_doc},
3537
3538 /* In place only. */
3539 {"zero", reinterpret_cast<PyCFunction>(Vector_zero), METH_NOARGS, Vector_zero_doc},
3540 {"negate", reinterpret_cast<PyCFunction>(Vector_negate), METH_NOARGS, Vector_negate_doc},
3541
3542 /* Operate on original or copy. */
3543 {"normalize",
3544 reinterpret_cast<PyCFunction>(Vector_normalize),
3545 METH_NOARGS,
3546 Vector_normalize_doc},
3547 {"normalized",
3548 reinterpret_cast<PyCFunction>(Vector_normalized),
3549 METH_NOARGS,
3550 Vector_normalized_doc},
3551
3552 {"resize", reinterpret_cast<PyCFunction>(Vector_resize), METH_O, Vector_resize_doc},
3553 {"resized", reinterpret_cast<PyCFunction>(Vector_resized), METH_O, Vector_resized_doc},
3554 {"to_2d", reinterpret_cast<PyCFunction>(Vector_to_2d), METH_NOARGS, Vector_to_2d_doc},
3555 {"resize_2d",
3556 reinterpret_cast<PyCFunction>(Vector_resize_2d),
3557 METH_NOARGS,
3558 Vector_resize_2d_doc},
3559 {"to_3d", reinterpret_cast<PyCFunction>(Vector_to_3d), METH_NOARGS, Vector_to_3d_doc},
3560 {"resize_3d",
3561 reinterpret_cast<PyCFunction>(Vector_resize_3d),
3562 METH_NOARGS,
3563 Vector_resize_3d_doc},
3564 {"to_4d", reinterpret_cast<PyCFunction>(Vector_to_4d), METH_NOARGS, Vector_to_4d_doc},
3565 {"resize_4d",
3566 reinterpret_cast<PyCFunction>(Vector_resize_4d),
3567 METH_NOARGS,
3568 Vector_resize_4d_doc},
3569 {"to_tuple",
3570 reinterpret_cast<PyCFunction>(Vector_to_tuple),
3571 METH_VARARGS,
3572 Vector_to_tuple_doc},
3573 {"to_track_quat",
3574 reinterpret_cast<PyCFunction>(Vector_to_track_quat),
3575 METH_VARARGS,
3576 Vector_to_track_quat_doc},
3577 {"orthogonal",
3578 reinterpret_cast<PyCFunction>(Vector_orthogonal),
3579 METH_NOARGS,
3580 Vector_orthogonal_doc},
3581
3582 /* Operation between 2 or more types. */
3583 {"reflect", reinterpret_cast<PyCFunction>(Vector_reflect), METH_O, Vector_reflect_doc},
3584 {"cross", reinterpret_cast<PyCFunction>(Vector_cross), METH_O, Vector_cross_doc},
3585 {"dot", reinterpret_cast<PyCFunction>(Vector_dot), METH_O, Vector_dot_doc},
3586 {"angle", reinterpret_cast<PyCFunction>(Vector_angle), METH_VARARGS, Vector_angle_doc},
3587 {"angle_signed",
3588 reinterpret_cast<PyCFunction>(Vector_angle_signed),
3589 METH_VARARGS,
3590 Vector_angle_signed_doc},
3591 {"rotation_difference",
3592 reinterpret_cast<PyCFunction>(Vector_rotation_difference),
3593 METH_O,
3594 Vector_rotation_difference_doc},
3595 {"project", reinterpret_cast<PyCFunction>(Vector_project), METH_O, Vector_project_doc},
3596 {"lerp", reinterpret_cast<PyCFunction>(Vector_lerp), METH_VARARGS, Vector_lerp_doc},
3597 {"slerp", reinterpret_cast<PyCFunction>(Vector_slerp), METH_VARARGS, Vector_slerp_doc},
3598 {"rotate", reinterpret_cast<PyCFunction>(Vector_rotate), METH_O, Vector_rotate_doc},
3599
3600 /* Base-math methods. */
3601 {"freeze",
3602 reinterpret_cast<PyCFunction>(BaseMathObject_freeze),
3603 METH_NOARGS,
3605
3606 {"copy", reinterpret_cast<PyCFunction>(Vector_copy), METH_NOARGS, Vector_copy_doc},
3607 {"__copy__", reinterpret_cast<PyCFunction>(Vector_copy), METH_NOARGS, nullptr},
3608 {"__deepcopy__", reinterpret_cast<PyCFunction>(Vector_deepcopy), METH_VARARGS, nullptr},
3609 {nullptr, nullptr, 0, nullptr},
3610};
3611
3612#ifdef __GNUC__
3613# ifdef __clang__
3614# pragma clang diagnostic pop
3615# else
3616# pragma GCC diagnostic pop
3617# endif
3618#endif
3619
3621
3622/* -------------------------------------------------------------------- */
3629
3630#ifdef MATH_STANDALONE
3631# define Vector_str nullptr
3632#endif
3633
3635 /* Wrap. */
3636 vector_doc,
3637 ".. class:: Vector(seq=(0.0, 0.0, 0.0), /)\n"
3638 "\n"
3639 " This object gives access to Vectors in Blender.\n"
3640 "\n"
3641 " :param seq: Components of the vector, must be a sequence of at least two.\n"
3642 " :type seq: Sequence[float]\n");
3643PyTypeObject vector_Type = {
3644 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
3645 /*tp_name*/ "Vector",
3646 /*tp_basicsize*/ sizeof(VectorObject),
3647 /*tp_itemsize*/ 0,
3648 /*tp_dealloc*/ reinterpret_cast<destructor>(BaseMathObject_dealloc),
3649 /*tp_vectorcall_offset*/ 0,
3650 /*tp_getattr*/ nullptr,
3651 /*tp_setattr*/ nullptr,
3652 /*tp_as_async*/ nullptr,
3653 /*tp_repr*/ reinterpret_cast<reprfunc>(Vector_repr),
3654 /*tp_as_number*/ &Vector_NumMethods,
3655 /*tp_as_sequence*/ &Vector_SeqMethods,
3656 /*tp_as_mapping*/ &Vector_AsMapping,
3657 /*tp_hash*/ reinterpret_cast<hashfunc>(Vector_hash),
3658 /*tp_call*/ nullptr,
3659 /*tp_str*/ reinterpret_cast<reprfunc>(Vector_str),
3660 /*tp_getattro*/ nullptr,
3661 /*tp_setattro*/ nullptr,
3662 /*tp_as_buffer*/ &Vector_as_buffer,
3663 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
3664 /*tp_doc*/ vector_doc,
3665 /*tp_traverse*/ reinterpret_cast<traverseproc>(BaseMathObject_traverse),
3666 /*tp_clear*/ reinterpret_cast<inquiry>(BaseMathObject_clear),
3667 /*tp_richcompare*/ static_cast<richcmpfunc>(Vector_richcmpr),
3668 /*tp_weaklistoffset*/ 0,
3669 /*tp_iter*/ nullptr,
3670 /*tp_iternext*/ nullptr,
3671 /*tp_methods*/ Vector_methods,
3672 /*tp_members*/ nullptr,
3673 /*tp_getset*/ Vector_getseters,
3674 /*tp_base*/ nullptr,
3675 /*tp_dict*/ nullptr,
3676 /*tp_descr_get*/ nullptr,
3677 /*tp_descr_set*/ nullptr,
3678 /*tp_dictoffset*/ 0,
3679 /*tp_init*/ nullptr,
3680 /*tp_alloc*/ nullptr,
3681 /*tp_new*/ Vector_new,
3682 /*tp_free*/ nullptr,
3683 /*tp_is_gc*/ reinterpret_cast<inquiry>(BaseMathObject_is_gc),
3684 /*tp_bases*/ nullptr,
3685 /*tp_mro*/ nullptr,
3686 /*tp_cache*/ nullptr,
3687 /*tp_subclasses*/ nullptr,
3688 /*tp_weaklist*/ nullptr,
3689 /*tp_del*/ nullptr,
3690 /*tp_version_tag*/ 0,
3691 /*tp_finalize*/ nullptr,
3692 /*tp_vectorcall*/ Vector_vectorcall,
3693};
3694
3695#ifdef MATH_STANDALONE
3696# undef Vector_str nullptr
3697#endif
3698
3700
3701/* -------------------------------------------------------------------- */
3704
3705PyObject *Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
3706{
3707 VectorObject *self;
3708 float *vec_alloc;
3709
3710 if (vec_num < 2) {
3711 PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
3712 return nullptr;
3713 }
3714
3715 vec_alloc = static_cast<float *>(PyMem_Malloc(vec_num * sizeof(float)));
3716 if (vec_alloc == nullptr) [[unlikely]] {
3717 PyErr_SetString(PyExc_MemoryError,
3718 "Vector(): "
3719 "problem allocating data");
3720 return nullptr;
3721 }
3722
3723 self = BASE_MATH_NEW(VectorObject, vector_Type, base_type);
3724 if (self) {
3725 self->vec = vec_alloc;
3726 self->vec_num = vec_num;
3727
3728 /* Initialize callbacks as nullptr. */
3729 self->cb_user = nullptr;
3730 self->cb_type = self->cb_subtype = 0;
3731
3732 if (vec) {
3733 memcpy(self->vec, vec, vec_num * sizeof(float));
3734 }
3735 else { /* new empty */
3736 std::fill_n(self->vec, vec_num, 0.0f);
3737 if (vec_num == 4) { /* do the homogeneous thing */
3738 self->vec[3] = 1.0f;
3739 }
3740 }
3741 self->flag = BASE_MATH_FLAG_DEFAULT;
3742 }
3743 else {
3744 PyMem_Free(vec_alloc);
3745 }
3746
3747 return reinterpret_cast<PyObject *>(self);
3748}
3749
3750PyObject *Vector_CreatePyObject_wrap(float *vec, const int vec_num, PyTypeObject *base_type)
3751{
3752 VectorObject *self;
3753
3754 if (vec_num < 2) {
3755 PyErr_SetString(PyExc_RuntimeError, "Vector(): invalid size");
3756 return nullptr;
3757 }
3758
3759 self = BASE_MATH_NEW(VectorObject, vector_Type, base_type);
3760 if (self) {
3761 self->vec_num = vec_num;
3762
3763 /* Initialize callbacks as nullptr. */
3764 self->cb_user = nullptr;
3765 self->cb_type = self->cb_subtype = 0;
3766
3767 self->vec = vec;
3769 }
3770 return reinterpret_cast<PyObject *>(self);
3771}
3772
3773PyObject *Vector_CreatePyObject_cb(PyObject *cb_user, int vec_num, uchar cb_type, uchar cb_subtype)
3774{
3775 VectorObject *self = reinterpret_cast<VectorObject *>(
3776 Vector_CreatePyObject(nullptr, vec_num, nullptr));
3777 if (self) {
3778 self->cb_user = Py_NewRef(cb_user);
3779 self->cb_type = cb_type;
3780 self->cb_subtype = cb_subtype;
3781 BLI_assert(!PyObject_GC_IsTracked((PyObject *)self));
3782 PyObject_GC_Track(self);
3783 }
3784
3785 return reinterpret_cast<PyObject *>(self);
3786}
3787
3788PyObject *Vector_CreatePyObject_alloc(float *vec, const int vec_num, PyTypeObject *base_type)
3789{
3790 VectorObject *self;
3791 self = reinterpret_cast<VectorObject *>(Vector_CreatePyObject_wrap(vec, vec_num, base_type));
3792 if (self) {
3793 self->flag &= ~BASE_MATH_FLAG_IS_WRAP;
3794 }
3795
3796 return reinterpret_cast<PyObject *>(self);
3797}
3798
3800
3801} // namespace blender
A dynamically sized string ADT.
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define MAX_DIMENSIONS
#define FLT_EPSILON
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define BaseMathObject_Prepare_ForResize(_self, error_prefix)
Definition mathutils.hh:180
#define BaseMath_ReadCallback_ForWrite(_self)
Definition mathutils.hh:157
#define BaseMath_Prepare_ForBufferAccess(_self, _view, _flags)
Definition mathutils.hh:189
#define BaseMath_ReadIndexCallback(_self, _index)
Definition mathutils.hh:151
#define BaseMath_WriteCallback(_self)
Definition mathutils.hh:149
#define BASE_MATH_NEW(struct_name, root_type, base_type)
Definition mathutils.hh:29
#define BaseMathObject_Prepare_ForHash(_self)
Definition mathutils.hh:172
#define BASE_MATH_FLAG_DEFAULT
Definition mathutils.hh:54
#define BaseMath_Prepare_ForWrite(_self)
Definition mathutils.hh:167
#define BaseMath_ReadCallback(_self)
Definition mathutils.hh:147
#define BaseMath_WriteIndexCallback(_self, _index)
Definition mathutils.hh:153
#define MatrixObject_Check(v)
#define MATRIX_ITEM(_mat, _row, _col)
#define VECTOR_SWIZZLE2_RO_DEF(attr, a, b)
#define VECTOR_SWIZZLE3_RW_DEF(attr, a, b, c)
#define SWIZZLE_VALID_AXIS
#define SWIZZLE_AXIS
#define VECTOR_SWIZZLE2_RW_DEF(attr, a, b)
#define SWIZZLE_BITS_PER_AXIS
#define VECTOR_SWIZZLE4_RO_DEF(attr, a, b, c, d)
#define VECTOR_SWIZZLE3_RO_DEF(attr, a, b, c)
#define VECTOR_SWIZZLE4_RW_DEF(attr, a, b, c, d)
#define VectorObject_Check(v)
BMesh Iterators.
char BaseMathObject_owner_doc[]
Definition mathutils.cc:686
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_vn_fl(float *array_tar, int size, float f)
static PyObject * Vector_length_get(VectorObject *self, void *)
char BaseMathObject_freeze_doc[]
Definition mathutils.cc:716
MINLINE void negate_v3_v3(float r[3], const float a[3])
void mul_m3_v3(const float M[3][3], float r[3])
int EXPP_VectorsAreEqual(const float *vecA, const float *vecB, int size, int floatSteps)
Definition mathutils.cc:511
float normalize_vn_vn(float *array_tar, const float *array_src, int size)
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
MINLINE bool is_zero_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
bool mathutils_slice_is_subset(Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, Py_ssize_t array_num)
Definition mathutils.hh:241
static PyObject * Vector_subscript(VectorObject *self, PyObject *item)
static PyObject * Vector_neg(VectorObject *self)
void BaseMathObject_dealloc(BaseMathObject *self)
Definition mathutils.cc:766
static PyObject * C_Vector_Linspace(PyObject *cls, PyObject *args)
unsigned int uint
Definition BKE_text.h:55
int PyC_CheckArgs_DeepCopy(PyObject *args)
void sub_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, int size)
static PyObject * Vector_lerp(VectorObject *self, PyObject *args)
static PyObject * Vector_axis_get(VectorObject *self, void *type)
static PyObject * Vector_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject * Vector_to_3d(VectorObject *self)
Py_hash_t mathutils_array_hash(const float *array, size_t array_len)
Definition mathutils.cc:69
void add_vn_vn(float *array_tar, const float *array_src, int size)
static PyObject * Vector_cross(VectorObject *self, PyObject *value)
void vec_to_quat(float q[4], const float vec[3], short axis, short upflag)
PyObject * mathutils_dynstr_to_py(DynStr *ds)
Definition mathutils.cc:523
float normalize_vn(float *array_tar, int size)
void mul_vn_vn_fl(float *array_tar, const float *array_src, int size, float f)
MINLINE float cross_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
static PyObject * Vector_idiv(PyObject *v1, PyObject *v2)
static PyObject * vector_item_internal(VectorObject *self, int i, const bool is_attr)
static PyObject * C_Vector_Range(PyObject *cls, PyObject *args)
static int Vector_axis_set(VectorObject *self, PyObject *value, void *type)
static PyObject * Vector_slice(VectorObject *self, Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length)
static PyObject * C_Vector_Fill(PyObject *cls, PyObject *args)
static PyObject * Vector_to_track_quat(VectorObject *self, PyObject *args)
PyObject * Vector_CreatePyObject_wrap(float *vec, const int vec_num, PyTypeObject *base_type)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
PyObject * BaseMathObject_owner_get(BaseMathObject *self, void *)
Definition mathutils.cc:690
static PyObject * Vector_negate(VectorObject *self)
MINLINE float safe_acosf(float a)
static PySequenceMethods Vector_SeqMethods
int Matrix_Parse2x2(PyObject *o, void *p)
void ortho_v2_v2(float out[2], const float v[2])
static PyObject * Vector_matmul(PyObject *v1, PyObject *v2)
static PyObject * vec__apply_to_copy(PyObject *(*vec_func)(VectorObject *), VectorObject *self)
float angle_signed_v2v2(const float v1[2], const float v2[2]) ATTR_WARN_UNUSED_RESULT
double double_round(double x, int ndigits)
Definition math_base.cc:30
void interp_vn_vn(float *array_tar, const float *array_src, float t, int size)
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
static PyObject * Vector_resize_4d(VectorObject *self)
int BaseMathObject_clear(BaseMathObject *self)
Definition mathutils.cc:748
void interp_dot_slerp(float t, float cosom, float r_w[2])
PyObject * BaseMathObject_freeze(BaseMathObject *self)
Definition mathutils.cc:725
static PyObject * Vector_add(PyObject *v1, PyObject *v2)
static Py_hash_t Vector_hash(VectorObject *self)
static PyObject * Vector_imatmul(PyObject *v1, PyObject *v2)
static PyObject * Vector_length_squared_get(VectorObject *self, void *)
void range_vn_fl(float *array_tar, int size, float start, float step)
static PyGetSetDef Vector_getseters[]
static PyObject * Vector_repr(VectorObject *self)
static int Vector_getbuffer(PyObject *obj, Py_buffer *view, int flags)
void normalize_m2_m2(float R[2][2], const float M[2][2]) ATTR_NONNULL()
PyObject * Vector_CreatePyObject_alloc(float *vec, const int vec_num, PyTypeObject *base_type)
int BaseMathObject_is_gc(BaseMathObject *self)
Definition mathutils.cc:788
int mathutils_array_parse_alloc(float **array, int array_num_min, PyObject *value, const char *error_prefix)
Definition mathutils.cc:195
static PyObject * Vector_to_tuple(VectorObject *self, PyObject *args)
static PyObject * Vector_str(VectorObject *self)
PyObject * BaseMathObject_is_frozen_get(BaseMathObject *self, void *)
Definition mathutils.cc:705
static PyObject * Vector_resize_3d(VectorObject *self)
static PyObject * Vector_iadd(PyObject *v1, PyObject *v2)
static PyObject * Vector_zero(VectorObject *self)
void BLI_dynstr_append(DynStr *__restrict ds, const char *cstr) ATTR_NONNULL()
Definition BLI_dynstr.cc:58
PyObject * Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
PyObject * BaseMathObject_is_valid_get(BaseMathObject *self, void *)
Definition mathutils.cc:711
static PyObject * Vector_copy(VectorObject *self)
unsigned char uchar
Definition BKE_text.h:58
double dot_vn_vn(const float *array_src_a, const float *array_src_b, int size) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static Py_ssize_t Vector_len(VectorObject *self)
PyObject * Quaternion_CreatePyObject(const float quat[4], PyTypeObject *base_type)
static PyObject * Vector_angle(VectorObject *self, PyObject *args)
static int vector_ass_item_internal(VectorObject *self, int i, PyObject *value, const bool is_attr)
static PyObject * Vector_item(VectorObject *self, Py_ssize_t i)
static PyObject * Vector_vectorcall(PyObject *type, PyObject *const *args, const size_t nargsf, PyObject *kwnames)
static PyObject * Vector_angle_signed(VectorObject *self, PyObject *args)
PyObject * Vector_CreatePyObject_cb(PyObject *cb_user, int vec_num, uchar cb_type, uchar cb_subtype)
@ BASE_MATH_FLAG_IS_WRAP
Definition mathutils.hh:39
@ BASE_MATH_FLAG_HAS_BUFFER_VIEW
Definition mathutils.hh:52
static PyObject * Vector_resize_2d(VectorObject *self)
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
double len_squared_vn(const float *array, int size) ATTR_WARN_UNUSED_RESULT
int column_vector_multiplication(float r_vec[4], VectorObject *vec, MatrixObject *mat)
sqrt(x)+1/max(0
static PyObject * Vector_reflect(VectorObject *self, PyObject *value)
static int Vector_ass_subscript(VectorObject *self, PyObject *item, PyObject *value)
void mul_vn_vn(float *array_tar, const float *array_src, int size)
static int row_vector_multiplication(float r_vec[MAX_DIMENSIONS], VectorObject *vec, MatrixObject *mat)
void mul_m2_v2(const float mat[2][2], float vec[2])
static PyObject * Vector_sub(PyObject *v1, PyObject *v2)
static PyObject * Vector_isub(PyObject *v1, PyObject *v2)
void reflect_v3_v3v3(float out[3], const float v[3], const float normal[3])
void add_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, int size)
static int Vector_swizzle_set(VectorObject *self, PyObject *value, void *closure)
static PyObject * Vector_imul(PyObject *v1, PyObject *v2)
static PyObject * Vector_normalize(VectorObject *self)
void ortho_v3_v3(float out[3], const float v[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
static PyObject * Vector_dot(VectorObject *self, PyObject *value)
static PyObject * Vector_mul(PyObject *v1, PyObject *v2)
static PyObject * Vector_richcmpr(PyObject *objectA, PyObject *objectB, int comparison_type)
MINLINE float normalize_v3(float n[3])
static PyNumberMethods Vector_NumMethods
static void Vector_releasebuffer(PyObject *, Py_buffer *view)
static PyObject * Vector_deepcopy(VectorObject *self, PyObject *args)
static PyObject * Vector_rotation_difference(VectorObject *self, PyObject *value)
PyObject * BaseMathObject_is_wrapped_get(BaseMathObject *self, void *)
Definition mathutils.cc:698
char BaseMathObject_is_valid_doc[]
Definition mathutils.cc:710
void sub_vn_vn(float *array_tar, const float *array_src, int size)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg)
Definition mathutils.cc:742
static PyObject * Vector_to_4d(VectorObject *self)
static int Vector_ass_slice(VectorObject *self, Py_ssize_t start, Py_ssize_t step, Py_ssize_t slice_length, PyObject *seq)
static PyObject * Vector_slerp(VectorObject *self, PyObject *args)
static PyObject * Vector_swizzle_get(VectorObject *self, void *closure)
static PyObject * Vector_rotate(VectorObject *self, PyObject *value)
static PyMappingMethods Vector_AsMapping
static PyObject * Vector_normalized(VectorObject *self)
void negate_vn(float *array_tar, int size)
int mathutils_any_to_rotmat(float rmat[3][3], PyObject *value, const char *error_prefix)
Definition mathutils.cc:426
void negate_vn_vn(float *array_tar, const float *array_src, int size)
char BaseMathObject_is_frozen_doc[]
Definition mathutils.cc:703
static PyObject * Vector_resized(VectorObject *self, PyObject *value)
static int Vector_ass_item(VectorObject *self, Py_ssize_t i, PyObject *value)
static PyMethodDef Vector_methods[]
static PyObject * Vector_div(PyObject *v1, PyObject *v2)
void mul_vn_vnvn(float *array_tar, const float *array_src_a, const float *array_src_b, int size)
static PyObject * C_Vector_Repeat(PyObject *cls, PyObject *args)
static int Vector_length_set(VectorObject *self, PyObject *value)
static PyObject * Vector_resize(VectorObject *self, PyObject *value)
static PyObject * Vector_project(VectorObject *self, PyObject *value)
static PyObject * Vector_to_2d(VectorObject *self)
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
static PyBufferProcs Vector_as_buffer
static PyObject * Vector_to_tuple_ex(VectorObject *self, int ndigits)
PyTypeObject vector_Type
static PyObject * vector_mul_float(VectorObject *vec, const float scalar)
static PyObject * Vector_orthogonal(VectorObject *self)
static PyObject * vector_mul_vec(VectorObject *vec1, VectorObject *vec2)
char BaseMathObject_is_wrapped_doc[]
Definition mathutils.cc:696
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float