| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include <sstream> |
| 26 |
|
| 27 |
#include "GeometryPyCXX.h" |
| 28 |
#include "Vector3D.h" |
| 29 |
|
| 30 |
// generated out of Vector.pyi |
| 31 |
#include "VectorPy.h" |
| 32 |
#include "VectorPy.cpp" |
| 33 |
|
| 34 |
using namespace Base; |
| 35 |
|
| 36 |
// returns a string which represent the object e.g. when printed in python |
| 37 |
std::string VectorPy::representation() const |
| 38 |
{ |
| 39 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 40 |
Py::Float x(ptr->x); |
| 41 |
Py::Float y(ptr->y); |
| 42 |
Py::Float z(ptr->z); |
| 43 |
std::stringstream str; |
| 44 |
str << "Vector ("; |
| 45 |
str << static_cast<std::string>(x.repr()) << ", " << static_cast<std::string>(y.repr()) << ", " |
| 46 |
<< static_cast<std::string>(z.repr()); |
| 47 |
str << ")"; |
| 48 |
|
| 49 |
return str.str(); |
| 50 |
} |
| 51 |
|
| 52 |
PyObject* VectorPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/) |
| 53 |
{ |
| 54 |
// create a new instance of VectorPy and the Twin object |
| 55 |
return new VectorPy(new Vector3d); |
| 56 |
} |
| 57 |
|
| 58 |
// constructor method |
| 59 |
int VectorPy::PyInit(PyObject* args, PyObject* /*kwd*/) |
| 60 |
{ |
| 61 |
double x = 0.0; |
| 62 |
double y = 0.0; |
| 63 |
double z = 0.0; |
| 64 |
PyObject* object = nullptr; |
| 65 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 66 |
if (PyArg_ParseTuple(args, "|ddd", &x, &y, &z)) { |
| 67 |
ptr->Set(x, y, z); |
| 68 |
return 0; |
| 69 |
} |
| 70 |
PyErr_Clear(); // set by PyArg_ParseTuple() |
| 71 |
if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &object)) { |
| 72 |
*ptr = *(static_cast<VectorPy*>(object)->getVectorPtr()); |
| 73 |
return 0; |
| 74 |
} |
| 75 |
PyErr_Clear(); // set by PyArg_ParseTuple() |
| 76 |
if (PyArg_ParseTuple(args, "O", &object)) { |
| 77 |
try { |
| 78 |
*ptr = getVectorFromTuple<double>(object); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
catch (const Py::Exception&) { |
| 82 |
return -1; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected"); |
| 87 |
return -1; |
| 88 |
} |
| 89 |
|
| 90 |
PyObject* VectorPy::__reduce__(PyObject* args) const |
| 91 |
{ |
| 92 |
if (!PyArg_ParseTuple(args, "")) { |
| 93 |
return nullptr; |
| 94 |
} |
| 95 |
|
| 96 |
Py::Tuple tuple(2); |
| 97 |
|
| 98 |
Py::Object type(getTypeAsObject(&VectorPy::Type)); |
| 99 |
tuple.setItem(0, type); |
| 100 |
|
| 101 |
Vector3d v = this->value(); |
| 102 |
Py::Tuple xyz(3); |
| 103 |
xyz.setItem(0, Py::Float(v.x)); |
| 104 |
xyz.setItem(1, Py::Float(v.y)); |
| 105 |
xyz.setItem(2, Py::Float(v.z)); |
| 106 |
tuple.setItem(1, xyz); |
| 107 |
return Py::new_reference_to(tuple); |
| 108 |
} |
| 109 |
|
| 110 |
PyObject* VectorPy::number_add_handler(PyObject* self, PyObject* other) |
| 111 |
{ |
| 112 |
if (!PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 113 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 114 |
return nullptr; |
| 115 |
} |
| 116 |
if (!PyObject_TypeCheck(other, &(VectorPy::Type))) { |
| 117 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 118 |
return nullptr; |
| 119 |
} |
| 120 |
Vector3d a = static_cast<VectorPy*>(self)->value(); |
| 121 |
Vector3d b = static_cast<VectorPy*>(other)->value(); |
| 122 |
return new VectorPy(a + b); |
| 123 |
} |
| 124 |
|
| 125 |
PyObject* VectorPy::number_subtract_handler(PyObject* self, PyObject* other) |
| 126 |
{ |
| 127 |
if (!PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 128 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
if (!PyObject_TypeCheck(other, &(VectorPy::Type))) { |
| 132 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 133 |
return nullptr; |
| 134 |
} |
| 135 |
Vector3d a = static_cast<VectorPy*>(self)->value(); |
| 136 |
Vector3d b = static_cast<VectorPy*>(other)->value(); |
| 137 |
return new VectorPy(a - b); |
| 138 |
} |
| 139 |
|
| 140 |
PyObject* VectorPy::number_multiply_handler(PyObject* self, PyObject* other) |
| 141 |
{ |
| 142 |
if (PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 143 |
Vector3d a = static_cast<VectorPy*>(self)->value(); |
| 144 |
|
| 145 |
if (PyObject_TypeCheck(other, &(VectorPy::Type))) { |
| 146 |
Vector3d b = static_cast<VectorPy*>(other)->value(); |
| 147 |
Py::Float mult(a * b); |
| 148 |
return Py::new_reference_to(mult); |
| 149 |
} |
| 150 |
if (PyNumber_Check(other)) { |
| 151 |
double b = PyFloat_AsDouble(other); |
| 152 |
return new VectorPy(a * b); |
| 153 |
} |
| 154 |
PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); |
| 155 |
return nullptr; |
| 156 |
} |
| 157 |
if (PyObject_TypeCheck(other, &(VectorPy::Type))) { |
| 158 |
Vector3d a = static_cast<VectorPy*>(other)->value(); |
| 159 |
if (PyNumber_Check(self)) { |
| 160 |
double b = PyFloat_AsDouble(self); |
| 161 |
return new VectorPy(a * b); |
| 162 |
} |
| 163 |
PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); |
| 164 |
return nullptr; |
| 165 |
} |
| 166 |
PyErr_SetString(PyExc_TypeError, "First or second arg must be Vector"); |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
|
| 170 |
Py_ssize_t VectorPy::sequence_length(PyObject* /*unused*/) |
| 171 |
{ |
| 172 |
return 3; |
| 173 |
} |
| 174 |
|
| 175 |
PyObject* VectorPy::sequence_item(PyObject* self, Py_ssize_t index) |
| 176 |
{ |
| 177 |
if (!PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 178 |
PyErr_SetString(PyExc_TypeError, "first arg must be Vector"); |
| 179 |
return nullptr; |
| 180 |
} |
| 181 |
if ((index < 0) || (index > 2)) { |
| 182 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 183 |
return nullptr; |
| 184 |
} |
| 185 |
|
| 186 |
VectorPy* self_ = static_cast<VectorPy*>(self); |
| 187 |
if (self_->sequence.length() == 0) { |
| 188 |
self_->sequence = Py::List {3}; |
| 189 |
} |
| 190 |
|
| 191 |
unsigned short pos = index % 3; |
| 192 |
Vector3d vec = self_->value(); |
| 193 |
Py::Float item {vec[pos]}; |
| 194 |
self_->sequence.setItem(pos, item); |
| 195 |
|
| 196 |
return Py::new_reference_to(item); |
| 197 |
} |
| 198 |
|
| 199 |
int VectorPy::sequence_ass_item(PyObject* self, Py_ssize_t index, PyObject* value) |
| 200 |
{ |
| 201 |
if (!PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 202 |
PyErr_SetString(PyExc_TypeError, "first arg must be Vector"); |
| 203 |
return -1; |
| 204 |
} |
| 205 |
if (index < 0 || index > 2) { |
| 206 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
unsigned short pos = index % 3; |
| 211 |
|
| 212 |
if (PyNumber_Check(value)) { |
| 213 |
VectorPy::PointerType ptr = static_cast<VectorPy*>(self)->getVectorPtr(); |
| 214 |
(*ptr)[pos] = PyFloat_AsDouble(value); |
| 215 |
} |
| 216 |
else { |
| 217 |
PyErr_SetString(PyExc_ValueError, "value must be float"); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
// http://renesd.blogspot.de/2009/07/python3-c-api-simple-slicing-sqslice.html |
| 225 |
PyObject* VectorPy::mapping_subscript(PyObject* self, PyObject* item) |
| 226 |
{ |
| 227 |
if (PyIndex_Check(item)) { |
| 228 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 229 |
if (i == -1 && PyErr_Occurred()) { |
| 230 |
return nullptr; |
| 231 |
} |
| 232 |
if (i < 0) { |
| 233 |
i += sequence_length(self); |
| 234 |
} |
| 235 |
return sequence_item(self, i); |
| 236 |
} |
| 237 |
if (PySlice_Check(item)) { |
| 238 |
Py_ssize_t start = 0, stop = 0, step = 0, slicelength = 0, cur = 0, i = 0; |
| 239 |
PyObject* slice = item; |
| 240 |
|
| 241 |
if (PySlice_GetIndicesEx(slice, sequence_length(self), &start, &stop, &step, &slicelength) |
| 242 |
< 0) { |
| 243 |
return nullptr; |
| 244 |
} |
| 245 |
|
| 246 |
if (slicelength <= 0) { |
| 247 |
return PyTuple_New(0); |
| 248 |
} |
| 249 |
if (start == 0 && step == 1 && slicelength == sequence_length(self) |
| 250 |
&& PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 251 |
Vector3d v = static_cast<VectorPy*>(self)->value(); |
| 252 |
Py::Tuple xyz(3); |
| 253 |
xyz.setItem(0, Py::Float(v.x)); |
| 254 |
xyz.setItem(1, Py::Float(v.y)); |
| 255 |
xyz.setItem(2, Py::Float(v.z)); |
| 256 |
return Py::new_reference_to(xyz); |
| 257 |
} |
| 258 |
if (PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 259 |
Vector3d v = static_cast<VectorPy*>(self)->value(); |
| 260 |
Py::Tuple xyz(static_cast<size_t>(slicelength)); |
| 261 |
|
| 262 |
for (cur = start, i = 0; i < slicelength; cur += step, i++) { |
| 263 |
unsigned short pos = cur % 3; |
| 264 |
xyz.setItem(static_cast<Py::sequence_index_type>(i), Py::Float(v[pos])); |
| 265 |
} |
| 266 |
|
| 267 |
return Py::new_reference_to(xyz); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
PyErr_Format( |
| 272 |
PyExc_TypeError, |
| 273 |
"Vector indices must be integers or slices, not %.200s", |
| 274 |
Py_TYPE(item)->tp_name |
| 275 |
); |
| 276 |
return nullptr; |
| 277 |
} |
| 278 |
|
| 279 |
PyObject* VectorPy::add(PyObject* args) const |
| 280 |
{ |
| 281 |
PyObject* obj = nullptr; |
| 282 |
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) { |
| 283 |
return nullptr; |
| 284 |
} |
| 285 |
|
| 286 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 287 |
|
| 288 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 289 |
VectorPy::PointerType vect_ptr = vec->getVectorPtr(); |
| 290 |
|
| 291 |
Vector3d v = (*this_ptr) + (*vect_ptr); |
| 292 |
return new VectorPy(v); |
| 293 |
} |
| 294 |
|
| 295 |
PyObject* VectorPy::sub(PyObject* args) const |
| 296 |
{ |
| 297 |
PyObject* obj = nullptr; |
| 298 |
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) { |
| 299 |
return nullptr; |
| 300 |
} |
| 301 |
|
| 302 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 303 |
|
| 304 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 305 |
VectorPy::PointerType vect_ptr = vec->getVectorPtr(); |
| 306 |
|
| 307 |
Vector3d v = (*this_ptr) - (*vect_ptr); |
| 308 |
return new VectorPy(v); |
| 309 |
} |
| 310 |
|
| 311 |
PyObject* VectorPy::negative(PyObject* args) const |
| 312 |
{ |
| 313 |
if (!PyArg_ParseTuple(args, "")) { |
| 314 |
return nullptr; |
| 315 |
} |
| 316 |
|
| 317 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 318 |
Vector3d v = -(*this_ptr); |
| 319 |
return new VectorPy(v); |
| 320 |
} |
| 321 |
|
| 322 |
PyObject* VectorPy::richCompare(PyObject* v, PyObject* w, int op) |
| 323 |
{ |
| 324 |
if (PyObject_TypeCheck(v, &(VectorPy::Type)) && PyObject_TypeCheck(w, &(VectorPy::Type))) { |
| 325 |
Vector3d v1 = static_cast<VectorPy*>(v)->value(); |
| 326 |
Vector3d v2 = static_cast<VectorPy*>(w)->value(); |
| 327 |
|
| 328 |
PyObject* res = nullptr; |
| 329 |
if (op != Py_EQ && op != Py_NE) { |
| 330 |
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Vector"); |
| 331 |
return nullptr; |
| 332 |
} |
| 333 |
if (op == Py_EQ) { |
| 334 |
res = (v1 == v2) ? Py_True : Py_False; // NOLINT |
| 335 |
Py_INCREF(res); |
| 336 |
return res; |
| 337 |
} |
| 338 |
res = (v1 != v2) ? Py_True : Py_False; // NOLINT |
| 339 |
Py_INCREF(res); |
| 340 |
return res; |
| 341 |
} |
| 342 |
// This always returns False |
| 343 |
Py_INCREF(Py_NotImplemented); |
| 344 |
return Py_NotImplemented; |
| 345 |
} |
| 346 |
|
| 347 |
PyObject* VectorPy::isEqual(PyObject* args) const |
| 348 |
{ |
| 349 |
PyObject* obj = nullptr; |
| 350 |
double tolerance = 0; |
| 351 |
if (!PyArg_ParseTuple(args, "O!d", &(VectorPy::Type), &obj, &tolerance)) { |
| 352 |
return nullptr; |
| 353 |
} |
| 354 |
|
| 355 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 356 |
|
| 357 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 358 |
VectorPy::PointerType vect_ptr = vec->getVectorPtr(); |
| 359 |
|
| 360 |
Py::Boolean eq((*this_ptr).IsEqual(*vect_ptr, tolerance)); |
| 361 |
return Py::new_reference_to(eq); |
| 362 |
} |
| 363 |
|
| 364 |
PyObject* VectorPy::isParallel(PyObject* args) const |
| 365 |
{ |
| 366 |
PyObject* obj = nullptr; |
| 367 |
double tolerance = 0; |
| 368 |
if (!PyArg_ParseTuple(args, "O!d", &(VectorPy::Type), &obj, &tolerance)) { |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
|
| 372 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 373 |
|
| 374 |
VectorPy::PointerType v1_ptr = getVectorPtr(); |
| 375 |
VectorPy::PointerType v2_ptr = vec->getVectorPtr(); |
| 376 |
|
| 377 |
Py::Boolean parallel((*v1_ptr).IsParallel(*v2_ptr, tolerance)); |
| 378 |
return Py::new_reference_to(parallel); |
| 379 |
} |
| 380 |
|
| 381 |
PyObject* VectorPy::isNormal(PyObject* args) const |
| 382 |
{ |
| 383 |
PyObject* obj = nullptr; |
| 384 |
double tolerance = 0; |
| 385 |
if (!PyArg_ParseTuple(args, "O!d", &(VectorPy::Type), &obj, &tolerance)) { |
| 386 |
return nullptr; |
| 387 |
} |
| 388 |
|
| 389 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 390 |
|
| 391 |
VectorPy::PointerType v1_ptr = getVectorPtr(); |
| 392 |
VectorPy::PointerType v2_ptr = vec->getVectorPtr(); |
| 393 |
|
| 394 |
Py::Boolean normal((*v1_ptr).IsNormal(*v2_ptr, tolerance)); |
| 395 |
return Py::new_reference_to(normal); |
| 396 |
} |
| 397 |
|
| 398 |
PyObject* VectorPy::scale(PyObject* args) |
| 399 |
{ |
| 400 |
double factorX = 0.0; |
| 401 |
double factorY = 0.0; |
| 402 |
double factorZ = 0.0; |
| 403 |
if (!PyArg_ParseTuple(args, "ddd", &factorX, &factorY, &factorZ)) { |
| 404 |
return nullptr; |
| 405 |
} |
| 406 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 407 |
ptr->Scale(factorX, factorY, factorZ); |
| 408 |
|
| 409 |
return Py::new_reference_to(this); |
| 410 |
} |
| 411 |
|
| 412 |
PyObject* VectorPy::multiply(PyObject* args) |
| 413 |
{ |
| 414 |
double factor = 0.0; |
| 415 |
if (!PyArg_ParseTuple(args, "d", &factor)) { |
| 416 |
return nullptr; |
| 417 |
} |
| 418 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 419 |
ptr->Scale(factor, factor, factor); |
| 420 |
|
| 421 |
return Py::new_reference_to(this); |
| 422 |
} |
| 423 |
|
| 424 |
PyObject* VectorPy::dot(PyObject* args) const |
| 425 |
{ |
| 426 |
PyObject* obj = nullptr; |
| 427 |
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) { |
| 428 |
return nullptr; |
| 429 |
} |
| 430 |
|
| 431 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 432 |
|
| 433 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 434 |
VectorPy::PointerType vect_ptr = vec->getVectorPtr(); |
| 435 |
|
| 436 |
Py::Float mult((*this_ptr) * (*vect_ptr)); |
| 437 |
return Py::new_reference_to(mult); |
| 438 |
} |
| 439 |
|
| 440 |
PyObject* VectorPy::cross(PyObject* args) const |
| 441 |
{ |
| 442 |
PyObject* obj = nullptr; |
| 443 |
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) { |
| 444 |
return nullptr; |
| 445 |
} |
| 446 |
|
| 447 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 448 |
|
| 449 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 450 |
VectorPy::PointerType vect_ptr = vec->getVectorPtr(); |
| 451 |
|
| 452 |
Vector3d v = (*this_ptr) % (*vect_ptr); |
| 453 |
return new VectorPy(v); |
| 454 |
} |
| 455 |
|
| 456 |
PyObject* VectorPy::isOnLineSegment(PyObject* args) const |
| 457 |
{ |
| 458 |
PyObject* start = nullptr; |
| 459 |
PyObject* end = nullptr; |
| 460 |
if (!PyArg_ParseTuple(args, "OO", &start, &end)) { |
| 461 |
return nullptr; |
| 462 |
} |
| 463 |
if (!PyObject_TypeCheck(start, &(VectorPy::Type))) { |
| 464 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 465 |
return nullptr; |
| 466 |
} |
| 467 |
if (!PyObject_TypeCheck(end, &(VectorPy::Type))) { |
| 468 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 469 |
return nullptr; |
| 470 |
} |
| 471 |
|
| 472 |
VectorPy* start_vec = static_cast<VectorPy*>(start); |
| 473 |
VectorPy* end_vec = static_cast<VectorPy*>(end); |
| 474 |
|
| 475 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 476 |
VectorPy::PointerType start_ptr = start_vec->getVectorPtr(); |
| 477 |
VectorPy::PointerType end_ptr = end_vec->getVectorPtr(); |
| 478 |
|
| 479 |
Py::Boolean result = this_ptr->IsOnLineSegment(*start_ptr, *end_ptr); |
| 480 |
|
| 481 |
return Py::new_reference_to(result); |
| 482 |
} |
| 483 |
|
| 484 |
PyObject* VectorPy::getAngle(PyObject* args) const |
| 485 |
{ |
| 486 |
PyObject* obj = nullptr; |
| 487 |
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) { |
| 488 |
return nullptr; |
| 489 |
} |
| 490 |
|
| 491 |
VectorPy* vec = static_cast<VectorPy*>(obj); |
| 492 |
|
| 493 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 494 |
VectorPy::PointerType vect_ptr = vec->getVectorPtr(); |
| 495 |
|
| 496 |
Py::Float angle(this_ptr->GetAngle(*vect_ptr)); |
| 497 |
return Py::new_reference_to(angle); |
| 498 |
} |
| 499 |
|
| 500 |
PyObject* VectorPy::normalize(PyObject* args) |
| 501 |
{ |
| 502 |
if (!PyArg_ParseTuple(args, "")) { |
| 503 |
return nullptr; |
| 504 |
} |
| 505 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 506 |
if (ptr->Length() < Vector3d::epsilon()) { |
| 507 |
PyErr_SetString(PyExc_FC_GeneralError, "Cannot normalize null vector"); |
| 508 |
return nullptr; |
| 509 |
} |
| 510 |
|
| 511 |
ptr->Normalize(); |
| 512 |
|
| 513 |
return Py::new_reference_to(this); |
| 514 |
} |
| 515 |
|
| 516 |
PyObject* VectorPy::projectToLine(PyObject* args) |
| 517 |
{ |
| 518 |
PyObject* base = nullptr; |
| 519 |
PyObject* line = nullptr; |
| 520 |
if (!PyArg_ParseTuple(args, "OO", &base, &line)) { |
| 521 |
return nullptr; |
| 522 |
} |
| 523 |
if (!PyObject_TypeCheck(base, &(VectorPy::Type))) { |
| 524 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 525 |
return nullptr; |
| 526 |
} |
| 527 |
if (!PyObject_TypeCheck(line, &(VectorPy::Type))) { |
| 528 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 529 |
return nullptr; |
| 530 |
} |
| 531 |
|
| 532 |
VectorPy* base_vec = static_cast<VectorPy*>(base); |
| 533 |
VectorPy* line_vec = static_cast<VectorPy*>(line); |
| 534 |
|
| 535 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 536 |
VectorPy::PointerType base_ptr = base_vec->getVectorPtr(); |
| 537 |
VectorPy::PointerType line_ptr = line_vec->getVectorPtr(); |
| 538 |
|
| 539 |
this_ptr->ProjectToLine(*base_ptr, *line_ptr); |
| 540 |
|
| 541 |
return Py::new_reference_to(this); |
| 542 |
} |
| 543 |
|
| 544 |
PyObject* VectorPy::projectToPlane(PyObject* args) |
| 545 |
{ |
| 546 |
PyObject* base = nullptr; |
| 547 |
PyObject* line = nullptr; |
| 548 |
if (!PyArg_ParseTuple(args, "OO", &base, &line)) { |
| 549 |
return nullptr; |
| 550 |
} |
| 551 |
if (!PyObject_TypeCheck(base, &(VectorPy::Type))) { |
| 552 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 553 |
return nullptr; |
| 554 |
} |
| 555 |
if (!PyObject_TypeCheck(line, &(VectorPy::Type))) { |
| 556 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 557 |
return nullptr; |
| 558 |
} |
| 559 |
|
| 560 |
VectorPy* base_vec = static_cast<VectorPy*>(base); |
| 561 |
VectorPy* line_vec = static_cast<VectorPy*>(line); |
| 562 |
|
| 563 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 564 |
VectorPy::PointerType base_ptr = base_vec->getVectorPtr(); |
| 565 |
VectorPy::PointerType line_ptr = line_vec->getVectorPtr(); |
| 566 |
|
| 567 |
this_ptr->ProjectToPlane(*base_ptr, *line_ptr); |
| 568 |
|
| 569 |
return Py::new_reference_to(this); |
| 570 |
} |
| 571 |
|
| 572 |
PyObject* VectorPy::distanceToPoint(PyObject* args) const |
| 573 |
{ |
| 574 |
PyObject* pnt = nullptr; |
| 575 |
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &pnt)) { |
| 576 |
return nullptr; |
| 577 |
} |
| 578 |
|
| 579 |
VectorPy* base_vec = static_cast<VectorPy*>(pnt); |
| 580 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 581 |
VectorPy::PointerType base_ptr = base_vec->getVectorPtr(); |
| 582 |
|
| 583 |
Py::Float dist(Distance(*this_ptr, *base_ptr)); |
| 584 |
return Py::new_reference_to(dist); |
| 585 |
} |
| 586 |
|
| 587 |
PyObject* VectorPy::distanceToLine(PyObject* args) const |
| 588 |
{ |
| 589 |
PyObject* base = nullptr; |
| 590 |
PyObject* line = nullptr; |
| 591 |
if (!PyArg_ParseTuple(args, "OO", &base, &line)) { |
| 592 |
return nullptr; |
| 593 |
} |
| 594 |
if (!PyObject_TypeCheck(base, &(VectorPy::Type))) { |
| 595 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 596 |
return nullptr; |
| 597 |
} |
| 598 |
if (!PyObject_TypeCheck(line, &(VectorPy::Type))) { |
| 599 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 600 |
return nullptr; |
| 601 |
} |
| 602 |
|
| 603 |
VectorPy* base_vec = static_cast<VectorPy*>(base); |
| 604 |
VectorPy* line_vec = static_cast<VectorPy*>(line); |
| 605 |
|
| 606 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 607 |
VectorPy::PointerType base_ptr = base_vec->getVectorPtr(); |
| 608 |
VectorPy::PointerType line_ptr = line_vec->getVectorPtr(); |
| 609 |
|
| 610 |
Py::Float dist(this_ptr->DistanceToLine(*base_ptr, *line_ptr)); |
| 611 |
return Py::new_reference_to(dist); |
| 612 |
} |
| 613 |
|
| 614 |
PyObject* VectorPy::distanceToLineSegment(PyObject* args) const |
| 615 |
{ |
| 616 |
PyObject* base = nullptr; |
| 617 |
PyObject* line = nullptr; |
| 618 |
if (!PyArg_ParseTuple(args, "OO", &base, &line)) { |
| 619 |
return nullptr; |
| 620 |
} |
| 621 |
if (!PyObject_TypeCheck(base, &(VectorPy::Type))) { |
| 622 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 623 |
return nullptr; |
| 624 |
} |
| 625 |
if (!PyObject_TypeCheck(line, &(VectorPy::Type))) { |
| 626 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 627 |
return nullptr; |
| 628 |
} |
| 629 |
|
| 630 |
VectorPy* base_vec = static_cast<VectorPy*>(base); |
| 631 |
VectorPy* line_vec = static_cast<VectorPy*>(line); |
| 632 |
|
| 633 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 634 |
VectorPy::PointerType base_ptr = base_vec->getVectorPtr(); |
| 635 |
VectorPy::PointerType line_ptr = line_vec->getVectorPtr(); |
| 636 |
|
| 637 |
Vector3d v = this_ptr->DistanceToLineSegment(*base_ptr, *line_ptr); |
| 638 |
return new VectorPy(v); |
| 639 |
} |
| 640 |
|
| 641 |
PyObject* VectorPy::distanceToPlane(PyObject* args) const |
| 642 |
{ |
| 643 |
PyObject* base = nullptr; |
| 644 |
PyObject* line = nullptr; |
| 645 |
if (!PyArg_ParseTuple(args, "OO", &base, &line)) { |
| 646 |
return nullptr; |
| 647 |
} |
| 648 |
if (!PyObject_TypeCheck(base, &(VectorPy::Type))) { |
| 649 |
PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); |
| 650 |
return nullptr; |
| 651 |
} |
| 652 |
if (!PyObject_TypeCheck(line, &(VectorPy::Type))) { |
| 653 |
PyErr_SetString(PyExc_TypeError, "Second arg must be Vector"); |
| 654 |
return nullptr; |
| 655 |
} |
| 656 |
|
| 657 |
VectorPy* base_vec = static_cast<VectorPy*>(base); |
| 658 |
VectorPy* line_vec = static_cast<VectorPy*>(line); |
| 659 |
|
| 660 |
VectorPy::PointerType this_ptr = getVectorPtr(); |
| 661 |
VectorPy::PointerType base_ptr = base_vec->getVectorPtr(); |
| 662 |
VectorPy::PointerType line_ptr = line_vec->getVectorPtr(); |
| 663 |
|
| 664 |
Py::Float dist(this_ptr->DistanceToPlane(*base_ptr, *line_ptr)); |
| 665 |
return Py::new_reference_to(dist); |
| 666 |
} |
| 667 |
|
| 668 |
Py::Float VectorPy::getLength() const |
| 669 |
{ |
| 670 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 671 |
return Py::Float(ptr->Length()); |
| 672 |
} |
| 673 |
|
| 674 |
void VectorPy::setLength(Py::Float arg) |
| 675 |
{ |
| 676 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 677 |
double len = ptr->Length(); |
| 678 |
if (len < Vector3d::epsilon()) { |
| 679 |
throw Py::RuntimeError(std::string("Cannot set length of null vector")); |
| 680 |
} |
| 681 |
|
| 682 |
double val = static_cast<double>(arg) / len; |
| 683 |
ptr->x *= val; |
| 684 |
ptr->y *= val; |
| 685 |
ptr->z *= val; |
| 686 |
} |
| 687 |
|
| 688 |
Py::Float VectorPy::getx() const |
| 689 |
{ |
| 690 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 691 |
return Py::Float(ptr->x); |
| 692 |
} |
| 693 |
|
| 694 |
void VectorPy::setx(Py::Float arg) |
| 695 |
{ |
| 696 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 697 |
ptr->x = static_cast<double>(arg); |
| 698 |
} |
| 699 |
|
| 700 |
Py::Float VectorPy::gety() const |
| 701 |
{ |
| 702 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 703 |
return Py::Float(ptr->y); |
| 704 |
} |
| 705 |
|
| 706 |
void VectorPy::sety(Py::Float arg) |
| 707 |
{ |
| 708 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 709 |
ptr->y = static_cast<double>(arg); |
| 710 |
} |
| 711 |
|
| 712 |
Py::Float VectorPy::getz() const |
| 713 |
{ |
| 714 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 715 |
return Py::Float(ptr->z); |
| 716 |
} |
| 717 |
|
| 718 |
void VectorPy::setz(Py::Float arg) |
| 719 |
{ |
| 720 |
VectorPy::PointerType ptr = getVectorPtr(); |
| 721 |
ptr->z = static_cast<double>(arg); |
| 722 |
} |
| 723 |
|
| 724 |
PyObject* VectorPy::getCustomAttributes(const char* /*attr*/) const |
| 725 |
{ |
| 726 |
return nullptr; |
| 727 |
} |
| 728 |
|
| 729 |
int VectorPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) |
| 730 |
{ |
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
// TODO: for v0.18 |
| 735 |
// In generation script allow one to more precisely define which slots |
| 736 |
// of the number protocol should be supported instead of setting all. |
| 737 |
|
| 738 |
PyObject* VectorPy::number_divide_handler(PyObject* self, PyObject* other) |
| 739 |
{ |
| 740 |
if (PyObject_TypeCheck(self, &(VectorPy::Type)) && PyNumber_Check(other)) { |
| 741 |
// Vector passes PyNumber_Check because it sets nb_int and nb_float |
| 742 |
// slots of the PyNumberMethods structure. So, it must be explicitly |
| 743 |
// filered out here. |
| 744 |
if (PyObject_TypeCheck(other, &(VectorPy::Type))) { |
| 745 |
PyErr_Format( |
| 746 |
PyExc_TypeError, |
| 747 |
"unsupported operand type(s) for /: '%s' and '%s'", |
| 748 |
Py_TYPE(self)->tp_name, |
| 749 |
Py_TYPE(other)->tp_name |
| 750 |
); |
| 751 |
return nullptr; |
| 752 |
} |
| 753 |
|
| 754 |
Vector3d vec = static_cast<VectorPy*>(self)->value(); |
| 755 |
double div = PyFloat_AsDouble(other); |
| 756 |
if (div == 0.0) { |
| 757 |
PyErr_Format(PyExc_ZeroDivisionError, "'%s' division by zero", Py_TYPE(self)->tp_name); |
| 758 |
return nullptr; |
| 759 |
} |
| 760 |
|
| 761 |
vec /= div; |
| 762 |
return new VectorPy(vec); |
| 763 |
} |
| 764 |
|
| 765 |
PyErr_Format( |
| 766 |
PyExc_TypeError, |
| 767 |
"unsupported operand type(s) for /: '%s' and '%s'", |
| 768 |
Py_TYPE(self)->tp_name, |
| 769 |
Py_TYPE(other)->tp_name |
| 770 |
); |
| 771 |
return nullptr; |
| 772 |
} |
| 773 |
|
| 774 |
PyObject* VectorPy::number_remainder_handler(PyObject* self, PyObject* other) |
| 775 |
{ |
| 776 |
if (PyObject_TypeCheck(self, &(VectorPy::Type)) && PyObject_TypeCheck(other, &(VectorPy::Type))) { |
| 777 |
Vector3d a = static_cast<VectorPy*>(self)->value(); |
| 778 |
Vector3d b = static_cast<VectorPy*>(other)->value(); |
| 779 |
return new VectorPy(a % b); |
| 780 |
} |
| 781 |
|
| 782 |
PyErr_Format( |
| 783 |
PyExc_TypeError, |
| 784 |
"unsupported operand type(s) for %%: '%s' and '%s'", |
| 785 |
Py_TYPE(self)->tp_name, |
| 786 |
Py_TYPE(other)->tp_name |
| 787 |
); |
| 788 |
return nullptr; |
| 789 |
} |
| 790 |
|
| 791 |
PyObject* VectorPy::number_divmod_handler(PyObject* self, PyObject* other) |
| 792 |
{ |
| 793 |
PyErr_Format( |
| 794 |
PyExc_TypeError, |
| 795 |
"unsupported operand type(s) for divmod(): '%s' and '%s'", |
| 796 |
Py_TYPE(self)->tp_name, |
| 797 |
Py_TYPE(other)->tp_name |
| 798 |
); |
| 799 |
return nullptr; |
| 800 |
} |
| 801 |
|
| 802 |
PyObject* VectorPy::number_power_handler(PyObject* self, PyObject* other, PyObject* /*arg*/) |
| 803 |
{ |
| 804 |
PyErr_Format( |
| 805 |
PyExc_TypeError, |
| 806 |
"unsupported operand type(s) for ** or pow(): '%s' and '%s'", |
| 807 |
Py_TYPE(self)->tp_name, |
| 808 |
Py_TYPE(other)->tp_name |
| 809 |
); |
| 810 |
return nullptr; |
| 811 |
} |
| 812 |
|
| 813 |
PyObject* VectorPy::number_negative_handler(PyObject* self) |
| 814 |
{ |
| 815 |
if (PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 816 |
Vector3d vec = static_cast<VectorPy*>(self)->value(); |
| 817 |
return new VectorPy(-vec); |
| 818 |
} |
| 819 |
|
| 820 |
PyErr_Format(PyExc_TypeError, "bad operand type for unary -: '%s'", Py_TYPE(self)->tp_name); |
| 821 |
return nullptr; |
| 822 |
} |
| 823 |
|
| 824 |
PyObject* VectorPy::number_positive_handler(PyObject* self) |
| 825 |
{ |
| 826 |
if (PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 827 |
Vector3d vec = static_cast<VectorPy*>(self)->value(); |
| 828 |
return new VectorPy(vec); |
| 829 |
} |
| 830 |
|
| 831 |
PyErr_Format(PyExc_TypeError, "bad operand type for unary +: '%s'", Py_TYPE(self)->tp_name); |
| 832 |
return nullptr; |
| 833 |
} |
| 834 |
|
| 835 |
PyObject* VectorPy::number_absolute_handler(PyObject* self) |
| 836 |
{ |
| 837 |
if (PyObject_TypeCheck(self, &(VectorPy::Type))) { |
| 838 |
Vector3d vec = static_cast<VectorPy*>(self)->value(); |
| 839 |
vec.x = fabs(vec.x); |
| 840 |
vec.y = fabs(vec.y); |
| 841 |
vec.z = fabs(vec.z); |
| 842 |
return new VectorPy(vec); |
| 843 |
} |
| 844 |
|
| 845 |
PyErr_Format(PyExc_TypeError, "bad operand type for abs(): '%s'", Py_TYPE(self)->tp_name); |
| 846 |
return nullptr; |
| 847 |
} |
| 848 |
|
| 849 |
int VectorPy::number_nonzero_handler(PyObject* /*self*/) |
| 850 |
{ |
| 851 |
return 1; |
| 852 |
} |
| 853 |
|
| 854 |
PyObject* VectorPy::number_invert_handler(PyObject* self) |
| 855 |
{ |
| 856 |
PyErr_Format(PyExc_TypeError, "bad operand type for unary ~: '%s'", Py_TYPE(self)->tp_name); |
| 857 |
return nullptr; |
| 858 |
} |
| 859 |
|
| 860 |
PyObject* VectorPy::number_lshift_handler(PyObject* self, PyObject* other) |
| 861 |
{ |
| 862 |
PyErr_Format( |
| 863 |
PyExc_TypeError, |
| 864 |
"unsupported operand type(s) for <<: '%s' and '%s'", |
| 865 |
Py_TYPE(self)->tp_name, |
| 866 |
Py_TYPE(other)->tp_name |
| 867 |
); |
| 868 |
return nullptr; |
| 869 |
} |
| 870 |
|
| 871 |
PyObject* VectorPy::number_rshift_handler(PyObject* self, PyObject* other) |
| 872 |
{ |
| 873 |
PyErr_Format( |
| 874 |
PyExc_TypeError, |
| 875 |
"unsupported operand type(s) for >>: '%s' and '%s'", |
| 876 |
Py_TYPE(self)->tp_name, |
| 877 |
Py_TYPE(other)->tp_name |
| 878 |
); |
| 879 |
return nullptr; |
| 880 |
} |
| 881 |
|
| 882 |
PyObject* VectorPy::number_and_handler(PyObject* self, PyObject* other) |
| 883 |
{ |
| 884 |
PyErr_Format( |
| 885 |
PyExc_TypeError, |
| 886 |
"unsupported operand type(s) for &: '%s' and '%s'", |
| 887 |
Py_TYPE(self)->tp_name, |
| 888 |
Py_TYPE(other)->tp_name |
| 889 |
); |
| 890 |
return nullptr; |
| 891 |
} |
| 892 |
|
| 893 |
PyObject* VectorPy::number_xor_handler(PyObject* self, PyObject* other) |
| 894 |
{ |
| 895 |
PyErr_Format( |
| 896 |
PyExc_TypeError, |
| 897 |
"unsupported operand type(s) for ^: '%s' and '%s'", |
| 898 |
Py_TYPE(self)->tp_name, |
| 899 |
Py_TYPE(other)->tp_name |
| 900 |
); |
| 901 |
return nullptr; |
| 902 |
} |
| 903 |
|
| 904 |
PyObject* VectorPy::number_or_handler(PyObject* self, PyObject* other) |
| 905 |
{ |
| 906 |
PyErr_Format( |
| 907 |
PyExc_TypeError, |
| 908 |
"unsupported operand type(s) for |: '%s' and '%s'", |
| 909 |
Py_TYPE(self)->tp_name, |
| 910 |
Py_TYPE(other)->tp_name |
| 911 |
); |
| 912 |
return nullptr; |
| 913 |
} |
| 914 |
|
| 915 |
PyObject* VectorPy::number_int_handler(PyObject* self) |
| 916 |
{ |
| 917 |
PyErr_Format( |
| 918 |
PyExc_TypeError, |
| 919 |
"int() argument must be a string or a number, not '%s'", |
| 920 |
Py_TYPE(self)->tp_name |
| 921 |
); |
| 922 |
return nullptr; |
| 923 |
} |
| 924 |
|
| 925 |
PyObject* VectorPy::number_float_handler(PyObject* self) |
| 926 |
{ |
| 927 |
PyErr_Format( |
| 928 |
PyExc_TypeError, |
| 929 |
"float() argument must be a string or a number, not '%s'", |
| 930 |
Py_TYPE(self)->tp_name |
| 931 |
); |
| 932 |
return nullptr; |
| 933 |
} |