Skip to content

Commit

Permalink
Added vec4 <-> quat conversion methods (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuzu-Typ committed Nov 9, 2022
1 parent 253fe1b commit a21b576
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions PyGLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ static PyMethodDef glmmethods[] = {
TERNARY_METHODS,

// PyGLM functions
CUSTOM_METHODS,

{ "silence", (PyCFunction)silence, METH_O, silence_docstr },
//{ "_get_type_info", (PyCFunction)_get_type_info, METH_VARARGS, "_get_type_info(accepted_types, object) -> None\nAn internal testing funtion to check wether or not the type checking works correctly." },
#ifdef HAS_TEST
Expand Down
2 changes: 2 additions & 0 deletions PyGLM/functions/other/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#include "binary.h"

#include "ternary.h"

#include "custom.h"
48 changes: 48 additions & 0 deletions PyGLM/functions/other/custom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "../../compiler_setup.h"
#include "../../internal_functions/all.h"

PyDoc_STRVAR(quat_to_vec4_docstr,
"quat_to_vec4(quat) -> vec4\n"
" Component wise conversion of quat to vec4."
);
static PyObject*
quat_to_vec4_(PyObject*, PyObject* arg) {
if (Py_TYPE(arg) == PyGLM_QUA_TYPE<float>()) {
glm::qua<float> q = unpack_qua<float>(arg);
return pack(glm::vec<4, float>(q.x, q.y, q.z, q.w));
}

if (Py_TYPE(arg) == PyGLM_QUA_TYPE<double>()) {
glm::qua<double> q = unpack_qua<double>(arg);
return pack(glm::vec<4, double>(q.x, q.y, q.z, q.w));
}

PyGLM_TYPEERROR_O("Invalid argument type for 'quat_to_vec4'. Expected 'quat', got ", arg);
return NULL;
}

PyDoc_STRVAR(vec4_to_quat_docstr,
"vec4_to_quat(vec4) -> quat\n"
" Component wise conversion of vec4 to quat."
);
static PyObject*
vec4_to_quat_(PyObject*, PyObject* arg) {
if (Py_TYPE(arg) == PyGLM_VEC_TYPE<4, float>()) {
glm::vec<4, float> v = unpack_vec<4, float>(arg);
return pack(glm::qua<float>(v.w, v.x, v.y, v.z));
}

if (Py_TYPE(arg) == PyGLM_VEC_TYPE<4, double>()) {
glm::vec<4, double> v = unpack_vec<4, double>(arg);
return pack(glm::qua<double>(v.w, v.x, v.y, v.z));
}

PyGLM_TYPEERROR_O("Invalid argument type for 'vec4_to_quat'. Expected 'vec4', got ", arg);
return NULL;
}

#define CUSTOM_METHODS \
{ "quat_to_vec4", (PyCFunction)quat_to_vec4_, METH_O, quat_to_vec4_docstr }, \
{ "vec4_to_quat", (PyCFunction)vec4_to_quat_, METH_O, vec4_to_quat_docstr }
21 changes: 21 additions & 0 deletions test/PyGLM_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,27 @@ def test_specific():
assert isinstance(glm.license, str)
#/Specific #

# Custom #
def test_custom():
for vec_type in (glm.vec4, glm.dvec4):
v = vec_type(1, 2, 3, 4)

q = glm.vec4_to_quat(v)

assert v.x == q.x
assert v.y == q.y
assert v.z == q.z
assert v.w == q.w

v = glm.quat_to_vec4(q)

assert v.x == q.x
assert v.y == q.y
assert v.z == q.z
assert v.w == q.w

#/Custom #

# Initialization #
## vec1
def test_vec1_types():
Expand Down

0 comments on commit a21b576

Please sign in to comment.