Skip to content

Commit a91baad

Browse files
committed
proof-of-concept, very messy, only works for arrays of floats
1 parent 36b615a commit a91baad

File tree

3 files changed

+129
-69
lines changed

3 files changed

+129
-69
lines changed

PyGLM/functions/detail/func_common.h

Lines changed: 101 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <stdio.h>
4+
35
#include "../../compiler_setup.h"
46

57
#include "../../types/all.h"
@@ -262,88 +264,118 @@ case get_format_specifier<bool>(): \
262264
return apply_min_from_PyObject_vector_vector<L, bool>(items); \
263265
}
264266

265-
// #define GLM_ARRAY_MINMAX_IF_IS_VEC(F, T) switch (arr1->shape[0]) {\
266-
// case 1:\
267-
// return F<glm::vec<1, T>>(arr1->data);
268-
// case 2:\
269-
// return F<glm::vec<2, T>>(arr1->data);
270-
// case 3:\
271-
// return F<glm::vec<3, T>>(arr1->data);
272-
// case 4:\
273-
// return F<glm::vec<4, T>>(arr1->data);
274-
// default:\
275-
// PyGLM_ASSERT(0, "Invalid shape occured. This should not have happened.");\
276-
// }
267+
#define GLM_ARRAY_RETURN_CALL_IF_IS_VEC(A, F, T) switch (A->shape[0]) {\
268+
case 1:\
269+
return pack<1, T>(F<glm::vec<1, T>>(reinterpret_cast<glm::vec<1, T>*>(A->data), A->itemCount));\
270+
case 2:\
271+
// printf("min for vec2\n");\
272+
return pack<2, T>(F<glm::vec<2, T>>(reinterpret_cast<glm::vec<2, T>*>(A->data), A->itemCount));\
273+
case 3:\
274+
return pack<3, T>(F<glm::vec<3, T>>(reinterpret_cast<glm::vec<3, T>*>(A->data), A->itemCount));\
275+
case 4:\
276+
return pack<4, T>(F<glm::vec<4, T>>(reinterpret_cast<glm::vec<4, T>*>(A->data), A->itemCount));\
277+
default:\
278+
PyGLM_ASSERT(0, "Invalid shape occured. This should not have happened.");\
279+
}
277280

278-
// template<typename T>
279-
// static T
280-
// min_internal(T* items) {
281-
// T minimum = items[0];
282-
// for (T item : items) {
283-
// if (item == minimum) {
284-
// continue;
285-
// }
286-
// minimum = glm::min(item, minimum);
287-
// }
288-
// return minimum;
289-
// }
281+
template<typename T>
282+
static T
283+
min_internal(T* items, Py_ssize_t count) {
284+
T minimum = items[0];
285+
for (Py_ssize_t i = 0; i < count; i++) {
286+
T item = items[i];
287+
// if (item == minimum) {
288+
// continue;
289+
// }
290+
minimum = glm::min(item, minimum);
291+
}
292+
return minimum;
293+
}
290294

291295
static PyObject*
292296
min_(PyObject*, PyObject* args) {
293297
PyObject *arg1, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL;
294298
if (!PyArg_UnpackTuple(args, "min", 1, 4, &arg1, &arg2, &arg3, &arg4)) return NULL;
299+
// printf("min_\n");
295300
if (arg2 == NULL) {
301+
// printf("min_ A\n");
296302
if (PyObject_TypeCheck(arg1, &glmArrayType)) {
303+
// printf("min_ B\n");
297304
// arg1 is a pyglm array
298305
glmArray* arr1 = (glmArray*)arg1;
306+
if (arr1->itemCount > 1) {
299307

300-
// Below this line is wrong. Gotta generate a switch-case to call
301-
// the correct template to apply_min over the contents of a glmArray
302-
PyGLMTypeObject* pti = (PyGLMTypeObject*)type;
308+
// // Below this line is wrong. Gotta generate a switch-case to call
309+
// // the correct template to apply_min over the contents of a glmArray
310+
// PyGLMTypeObject* pti = (PyGLMTypeObject*)type;
303311

304-
// switch (pti->C) {
305-
// case 1:
306-
// min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(1);
307-
// break;
308-
// case 2:
309-
// min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(2);
310-
// break;
311-
// case 3:
312-
// min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(3);
313-
// break;
314-
// case 4:
315-
// min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(4);
316-
// break;
317-
// }
312+
// // switch (pti->C) {
313+
// // case 1:
314+
// // min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(1);
315+
// // break;
316+
// // case 2:
317+
// // min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(2);
318+
// // break;
319+
// // case 3:
320+
// // min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(3);
321+
// // break;
322+
// // case 4:
323+
// // min_GEN_TYPE_SWITCH_STATEMENT_FOR_VECTOR(4);
324+
// // break;
325+
// // }
318326

319-
// if (arr1->glmType == PyGLM_TYPE_VEC) {
320-
// switch (arr1->format) {
321-
// case 'f':
322-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, float);
323-
// case 'd':
324-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, double);
325-
// case 'i':
326-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, int32);
327-
// case 'I':
328-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, uint32);
329-
// case 'b':
330-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, int8);
331-
// case 'B':
332-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, uint8);
333-
// case 'h':
334-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, int16);
335-
// case 'H':
336-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, uint16);
337-
// case 'q':
338-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, int64);
339-
// case 'Q':
340-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, uint64);
341-
// case '?':
342-
// GLM_ARRAY_MIN_IF_IS_VEC(min_internal, bool);
343-
// default:
344-
// PyGLM_ASSERT(0, "Invalid format specifier. This should not have happened.");
345-
// }
346-
// }
327+
// printf("min_ %d %d\n", arr1->glmType, PyGLM_TYPE_VEC);
328+
if (arr1->glmType == PyGLM_TYPE_VEC) {
329+
switch (arr1->format) {
330+
case 'f':
331+
switch (arr1->shape[0]) {
332+
case 1:
333+
return pack<1, float>(min_internal<glm::vec<1, float>>(reinterpret_cast<glm::vec<1, float>*>(arr1->data), arr1->itemCount));
334+
case 2:
335+
// printf("min for vec2\n");
336+
return pack<2, float>(min_internal<glm::vec<2, float>>(reinterpret_cast<glm::vec<2, float>*>(arr1->data), arr1->itemCount));
337+
case 3:
338+
return pack<3, float>(min_internal<glm::vec<3, float>>(reinterpret_cast<glm::vec<3, float>*>(arr1->data), arr1->itemCount));
339+
case 4:
340+
return pack<4, float>(min_internal<glm::vec<4, float>>(reinterpret_cast<glm::vec<4, float>*>(arr1->data), arr1->itemCount));
341+
default:
342+
PyGLM_ASSERT(0, "Invalid shape occured. This should not have happened.");
343+
}
344+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, float);
345+
// case 'd':
346+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, double);
347+
// case 'i':
348+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, int32);
349+
// case 'I':
350+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, uint32);
351+
// case 'b':
352+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, int8);
353+
// case 'B':
354+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, uint8);
355+
// case 'h':
356+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, int16);
357+
// case 'H':
358+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, uint16);
359+
// case 'q':
360+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, int64);
361+
// case 'Q':
362+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, uint64);
363+
// case '?':
364+
// GLM_ARRAY_RETURN_CALL_IF_IS_VEC(arr1, min_internal, bool);
365+
default:
366+
PyGLM_ASSERT(0, "Invalid format specifier. This should not have happened.");
367+
}
368+
}
369+
if (arr1->glmType == PyGLM_TYPE_CTYPES) {
370+
switch (arr1->format) {
371+
case 'f':
372+
// printf("min for floats\n");
373+
return pack(min_internal<float>(reinterpret_cast<float*>(arr1->data), arr1->itemCount));
374+
default:
375+
PyGLM_ASSERT(0, "Invalid format specifier. This should not have happened.");
376+
}
377+
}
378+
}
347379
}
348380
if (PyObject_IterCheck(arg1)) {
349381
PyObject* iterator = PyObject_GetIter(arg1);

test_min_glm.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import glm
2+
from glm import min as glm_min
3+
4+
a = glm.array([glm.vec2(x, x + 1) for x in range(0, 10000, 2)])
5+
a_x, a_y = a.split_components()
6+
7+
for i in range(99999):
8+
# PyGLM
9+
glm_min(a_x)
10+
glm_min(a_y)
11+
12+
# Python
13+
# min(a_x)
14+
# min(a_y)

test_min_py.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import glm
2+
from glm import min as glm_min
3+
4+
a = glm.array([glm.vec2(x, x + 1) for x in range(0, 10000, 2)])
5+
a_x, a_y = a.split_components()
6+
7+
for i in range(99999):
8+
# PyGLM
9+
# glm_min(a_x)
10+
# glm_min(a_y)
11+
12+
# Python
13+
min(a_x)
14+
min(a_y)

0 commit comments

Comments
 (0)