gpt4 book ai didi

c++ - 将 Python 嵌入到 C++ 中调用两个函数并传递数组

转载 作者:行者123 更新时间:2023-11-30 05:40:26 24 4
gpt4 key购买 nike

我正在尝试将 python 函数嵌入到我的 C++ 代码中以执行一些符号和代数运算。

我的想法:我想根据一些输入变量创建一个带有符号变量的矩阵。这个矩阵我想在另一个函数中使用。此函数从符号矩阵中生成一些坐标。然后我想将此结果传递回我的 C++ 代码。

我的问题:我无法将这个矩阵从 python 中提取到我的 C++ 代码中,然后返回到另一个函数作为输入。因为我想通过 C++ 将符号矩阵传递给另一个应该从该矩阵中生成结果的 python 函数。只是为了尝试我的 python 代码和与 c++ 的集成,我编写了以下代码。

编辑:为了让我的问题更具体,我用 Python 编写了我想用这个函数做的全部工作。为了澄清,我需要一个生成符号矩阵的函数,另一个函数必须计算值。对于计算,这些值来 self 的 C++ 代码内部。

这里是用 Python 编写的示例:

from sympy import *
import numpy as np


def main():
start_pos = [1,2,3]
end_pos = [4,5,6]

generic_function = generate_symbolic_transformation(start_pos,end_pos)
calculate_symbolic_transformation(generic_function,90,0.5);

# Calculation of the symbolic transformation depending on the input
def calculate_symbolic_transformation(generic_function,thetay_value,k_value):
thetay = symbols('thetay')
k = symbols('k')
transf_matrix = MatrixSymbol('tm',4,4)
transf_matrix = Matrix(transf_matrix)
transf_matrix = sympify(generic_function)
transf_matrix = transf_matrix.subs([(thetay,thetay_value),(k,k_value)])
print transf_matrix
return 1

# Generation of the symbolic transformation depending on the input
def generate_symbolic_transformation(Start_pos_coords,End_pos_coords):

# Symbolic startposition
Start_pos = MatrixSymbol('S',3,1)
Start_pos = Matrix(Start_pos)
Start_pos[0] = Start_pos_coords[0]
Start_pos[1] = Start_pos_coords[1]
Start_pos[2] = Start_pos_coords[2]

print Start_pos

# Symbolic endposition
End_pos = MatrixSymbol('E',3,1)
End_pos = Matrix(End_pos)
End_pos[0] = End_pos_coords[0]
End_pos[1] = End_pos_coords[1]
End_pos[2] = End_pos_coords[2]

print End_pos

# Symbolic rotation matric
R = MatrixSymbol('R',3,3)

# Symbolic transformation matric
T = MatrixSymbol('T',4,4)

# Necessary symbolic variabls
k = symbols('k')
thetax = symbols('thetax')
thetay = symbols('thetay')
thetaz = symbols('thetaz')

# For rotation of EulerAngles RzRyRx:
Rx = MatrixSymbol('Rx',3,3)
Ry = MatrixSymbol('Ry',3,3)
Rz = MatrixSymbol('Rz',3,3)

# Filling Rx rotation matric
# | 1 0 0 |
# | 0 -cos(thetax) sin(thetax) |
# | 0 sin(thetax) cos(thetax) |

Rx = Matrix(Rx)
Rx[0,0] = 1
Rx[0,1] = 0
Rx[0,2] = 0
Rx[1,0] = 0
Rx[1,1] = cos(thetax)
Rx[1,2] = -sin(thetax)
Rx[2,0] = 0
Rx[2,1] = sin(thetax)
Rx[2,2] = cos(thetax)

# Filling Ry rotation matric
# | cos(thetay) 0 sin(thetay) |
# | 0 1 0 |
# | -sin(thetay) 0 cos(thetay) |

Ry = Matrix(Ry)
Ry[0,0] = cos(thetay)
Ry[0,1] = 0
Ry[0,2] = sin(thetay)
Ry[1,0] = 0
Ry[1,1] = 1
Ry[1,2] = 0
Ry[2,0] = -sin(thetay)
Ry[2,1] = 0
Ry[2,2] = cos(thetay)

# Filling Rz rotation matric
# | cos(thetaz) -sin(thetaz) 0 |
# | sin(thetaz) cos(thetaz) 0 |
# | 0 0 1 |

Rz = Matrix(Rz)
Rz[0,0] = cos(thetaz)
Rz[0,1] = -sin(thetaz)
Rz[0,2] = 0
Rz[1,0] = sin(thetaz)
Rz[1,1] = cos(thetaz)
Rz[1,2] = 0
Rz[2,0] = 0
Rz[2,1] = 0
Rz[2,2] = 1

# Generating the rotation matric
R = Rz*Ry*Rx

# Generating the linear translation
# Symbolic 3D line function
Translation = MatrixSymbol('Tl',3,1)
Translation = Start_pos + k * (End_pos-Start_pos)

# Integrate it into the transformation matric
# | R T |
# | 000 1 |
T = Matrix(T)

i=0
for r in range(4):
for c in range(4):
if (c < 3 and r < 3):
T[r,c] = R[r,c]
elif (c == 3 and r < 3):
T[r,c] = Translation[i]
++i
elif (c < 3 and r == 3):
T[r,c] = 0
else:
T[r,c] = 1

## Save the created matrics with symbolic variables into global object
T = T.subs([(thetax,0),(thetaz,0)])
return T

if __name__ == "__main__":
main()

最佳答案

这里有几个错误:

  1. Python 函数 generate_symbolic_transformation抛出异常,这使得 resultObjNULL .这会进一步传播并导致崩溃。

  2. 即使resultObj不是NULL , 它不会被正确地返回给它的调用者,因为 CallPlugIn_generate_symbolic_transformation 的最后两行确保只返回一个值,它是 NULL .

但这些都是具体问题。我还会提出一些一般性建议,可能会帮助您及早发现问题,从而节省时间和精力:

  1. 处理 Python 错误。至少,如果 Python C/API 函数返回 NULL , 打印错误。例如:


if (!resultObj)
{
PyErr_Print();
}

这会导致类似于以下的错误消息:

Traceback (most recent call last): File "/home/sterin/ClionProjects/numpy1/numpy_test.py", line 15, in generate_symbolic_transformation T = T.subs([(thetax,0),(thetaz,0)]) NameError: global name 'thetax' is not defined

帮助您更早地发现错误。

  1. 确保没有 NULL值提供为 PyObject* Python C/API 调用的参数(除非特别允许)。这会捕获 NULL 的地方结果已被使用。

关于c++ - 将 Python 嵌入到 C++ 中调用两个函数并传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com