Skip to content

Commit 89fd7e2

Browse files
committedJan 16, 2025·
vec_mat_interop in separate file, continued
1 parent 643f68c commit 89fd7e2

File tree

3 files changed

+119
-100
lines changed

3 files changed

+119
-100
lines changed
 

‎plugins/OGF/gompy/interpreter/python_interpreter.cpp

+1-100
Original file line numberDiff line numberDiff line change
@@ -1142,75 +1142,6 @@ namespace {
11421142
return result;
11431143
}
11441144

1145-
template<class T> inline PyObject* graphiteveccomp_to_python(
1146-
T value
1147-
) {
1148-
geo_argused(value);
1149-
geo_assert_not_reached;
1150-
}
1151-
1152-
template<> inline PyObject* graphiteveccomp_to_python<double>(
1153-
double value
1154-
) {
1155-
return PyFloat_FromDouble(value);
1156-
}
1157-
1158-
template<> inline PyObject*
1159-
graphiteveccomp_to_python<Numeric::int32>(
1160-
Numeric::int32 value
1161-
) {
1162-
return PyLong_FromLong(long(value));
1163-
}
1164-
1165-
1166-
template <unsigned int N, class T> inline PyObject* graphitevec_to_python(
1167-
const ::GEO::vecng<N,T>& V
1168-
) {
1169-
PyObject* result = PyList_New(N);
1170-
for(index_t i=0; i<N; ++i) {
1171-
PyList_SetItem(result, i, graphiteveccomp_to_python(V[i]));
1172-
}
1173-
return result;
1174-
}
1175-
1176-
template <unsigned int N, class T> inline PyObject* graphitevec_to_python(
1177-
const Any& val
1178-
) {
1179-
if(val.meta_type() != ogf_meta<::GEO::vecng<N,T> >::type()) {
1180-
return nullptr;
1181-
}
1182-
::GEO::vecng<N,T> V;
1183-
val.get_value(V);
1184-
return graphitevec_to_python(V);
1185-
}
1186-
1187-
1188-
template <unsigned int N, class T> inline PyObject* graphitemat_to_python(
1189-
const ::GEO::Matrix<N,T>& M
1190-
) {
1191-
PyObject* result = PyList_New(N);
1192-
for(index_t i=0; i<index_t(N); ++i) {
1193-
PyObject* row = PyList_New(N);
1194-
for(index_t j=0; j<index_t(N); ++j) {
1195-
PyList_SetItem(row, j, graphiteveccomp_to_python(M(i,j)));
1196-
}
1197-
PyList_SetItem(result, i, row);
1198-
}
1199-
return result;
1200-
}
1201-
1202-
template <unsigned int N, class T> inline PyObject* graphitemat_to_python(
1203-
const Any& val
1204-
) {
1205-
if(val.meta_type() != ogf_meta<::GEO::Matrix<N,T> >::type()) {
1206-
return nullptr;
1207-
}
1208-
::GEO::Matrix<N,T> M;
1209-
val.get_value(M);
1210-
return graphitemat_to_python(M);
1211-
}
1212-
1213-
12141145
/*************************************************************************/
12151146

12161147
PyObject* graphite_to_python(const Any& arg, MetaType* mtype) {
@@ -1315,41 +1246,11 @@ namespace {
13151246
return PyFloat_FromDouble(value);
13161247
}
13171248

1318-
PyObject* result = nullptr;
1319-
if(result == nullptr) {
1320-
result = graphitevec_to_python<2,double>(arg);
1321-
}
1322-
if(result == nullptr) {
1323-
result = graphitevec_to_python<3,double>(arg);
1324-
}
1325-
if(result == nullptr) {
1326-
result = graphitevec_to_python<4,double>(arg);
1327-
}
1328-
if(result == nullptr) {
1329-
result = graphitevec_to_python<2,Numeric::int32>(arg);
1330-
}
1331-
if(result == nullptr) {
1332-
result = graphitevec_to_python<3,Numeric::int32>(arg);
1333-
}
1334-
if(result == nullptr) {
1335-
result = graphitevec_to_python<4,Numeric::int32>(arg);
1336-
}
1337-
if(result == nullptr) {
1338-
result = graphitemat_to_python<2,double>(arg);
1339-
}
1340-
if(result == nullptr) {
1341-
result = graphitemat_to_python<3,double>(arg);
1342-
}
1343-
if(result == nullptr) {
1344-
result = graphitemat_to_python<4,double>(arg);
1345-
}
1346-
1347-
1249+
PyObject* result = graphite_mat_vec_to_python(arg);
13481250
if(result != nullptr) {
13491251
return result;
13501252
}
13511253

1352-
13531254
std::string value = arg.as_string();
13541255
return string_to_python(value);
13551256
}

‎plugins/OGF/gompy/interpreter/vec_mat_interop.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,81 @@ namespace {
193193
result.set_value(M);
194194
return true;
195195
}
196+
197+
/*************************************************************************/
198+
199+
template<class T> inline PyObject* graphiteveccomp_to_python(
200+
T value
201+
) {
202+
geo_argused(value);
203+
geo_assert_not_reached;
204+
}
205+
206+
template<> inline PyObject* graphiteveccomp_to_python<double>(
207+
double value
208+
) {
209+
return PyFloat_FromDouble(value);
210+
}
211+
212+
template<> inline PyObject*
213+
graphiteveccomp_to_python<Numeric::int32>(
214+
Numeric::int32 value
215+
) {
216+
return PyLong_FromLong(long(value));
217+
}
218+
219+
220+
template <unsigned int N, class T> inline PyObject* graphitevec_to_python(
221+
const ::GEO::vecng<N,T>& V
222+
) {
223+
PyObject* result = PyList_New(N);
224+
for(index_t i=0; i<N; ++i) {
225+
PyList_SetItem(result, i, graphiteveccomp_to_python(V[i]));
226+
}
227+
return result;
228+
}
229+
230+
template <unsigned int N, class T> inline PyObject* graphitevec_to_python(
231+
const Any& val
232+
) {
233+
if(val.meta_type() != ogf_meta<::GEO::vecng<N,T> >::type()) {
234+
return nullptr;
235+
}
236+
::GEO::vecng<N,T> V;
237+
val.get_value(V);
238+
return graphitevec_to_python(V);
239+
}
240+
241+
242+
template <unsigned int N, class T> inline PyObject* graphitemat_to_python(
243+
const ::GEO::Matrix<N,T>& M
244+
) {
245+
PyObject* result = PyList_New(N);
246+
for(index_t i=0; i<index_t(N); ++i) {
247+
PyObject* row = PyList_New(N);
248+
for(index_t j=0; j<index_t(N); ++j) {
249+
PyList_SetItem(row, j, graphiteveccomp_to_python(M(i,j)));
250+
}
251+
PyList_SetItem(result, i, row);
252+
}
253+
return result;
254+
}
255+
256+
template <unsigned int N, class T> inline PyObject* graphitemat_to_python(
257+
const Any& val
258+
) {
259+
if(val.meta_type() != ogf_meta<::GEO::Matrix<N,T> >::type()) {
260+
return nullptr;
261+
}
262+
::GEO::Matrix<N,T> M;
263+
val.get_value(M);
264+
return graphitemat_to_python(M);
265+
}
266+
196267
}
197268

269+
/*****************************************************************************/
270+
198271
namespace OGF {
199272

200273
bool python_to_graphite_mat_vec(PyObject* obj, Any& result, MetaType* mtype){
@@ -241,4 +314,40 @@ namespace OGF {
241314
return false;
242315
}
243316

317+
/***********************************************************************/
318+
319+
PyObject* graphite_mat_vec_to_python(const Any& arg) {
320+
PyObject* result = nullptr;
321+
if(result == nullptr) {
322+
result = graphitevec_to_python<2,double>(arg);
323+
}
324+
if(result == nullptr) {
325+
result = graphitevec_to_python<3,double>(arg);
326+
}
327+
if(result == nullptr) {
328+
result = graphitevec_to_python<4,double>(arg);
329+
}
330+
if(result == nullptr) {
331+
result = graphitevec_to_python<2,Numeric::int32>(arg);
332+
}
333+
if(result == nullptr) {
334+
result = graphitevec_to_python<3,Numeric::int32>(arg);
335+
}
336+
if(result == nullptr) {
337+
result = graphitevec_to_python<4,Numeric::int32>(arg);
338+
}
339+
if(result == nullptr) {
340+
result = graphitemat_to_python<2,double>(arg);
341+
}
342+
if(result == nullptr) {
343+
result = graphitemat_to_python<3,double>(arg);
344+
}
345+
if(result == nullptr) {
346+
result = graphitemat_to_python<4,double>(arg);
347+
}
348+
return result;
349+
}
350+
351+
/***********************************************************************/
352+
244353
}

‎plugins/OGF/gompy/interpreter/vec_mat_interop.h

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ namespace OGF {
5757
*/
5858
bool python_to_graphite_mat_vec(PyObject* obj, Any& result, MetaType* mtype);
5959

60+
61+
/**
62+
* \brief Converts a Graphite object into a Python object
63+
* \details Works with vec2, vec3, vec4 of doubles and integers, and
64+
* with mat4 of doubles.
65+
* \param[in] matvec the input vec or mat stored in an Any
66+
* \return the Python object, or nullptr if conversion was not possible
67+
*/
68+
PyObject* graphite_mat_vec_to_python(const Any& matvec);
6069
}
6170

6271
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.