[英]Embedding Python into C++ calling two functions and passing array around
我正在嘗試將python函數嵌入到我的c ++代碼中,以執行一些符號和代數運算。
我的想法:我想根據某些輸入變量創建一個包含符號變量的矩陣。 此后,我想在另一個函數中使用此矩陣。 此函數從符號矩陣中生成一些坐標。 然后我想將此結果傳遞回我的C ++代碼。
我的問題:我在將矩陣從python提取到我的c ++代碼並返回到另一個函數作為輸入時遇到了問題。 因為我想將符號矩陣通過c ++傳遞給應該從該矩陣生成結果的另一個python函數。 為了嘗試我的python代碼並將其集成到c ++中,我編寫了以下代碼。
編輯:為了使我的問題更具體,我在Python中編寫了要使用此函數進行的全部工作。 為了明確起見,我需要一個函數來生成符號矩陣,而另一個函數必須計算值。 為了進行計算,這些值來自我的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()
這里有幾個錯誤:
Python函數generate_symbolic_transformation
引發異常,這使得resultObj
為NULL
。 這會進一步傳播並導致崩潰。
即使resultObj
不為NULL
,也不會適當地將其返回給其調用方,因為CallPlugIn_generate_symbolic_transformation
的最后兩行確保僅返回一個值為NULL
。
但是這些是特定的問題。 我還會提出一些一般性建議,這些建議可以幫助您及早發現問題,從而節省時間和精力:
NULL
,則輸出錯誤。 例如: if (!resultObj) { PyErr_Print(); }
這將導致類似於以下內容的錯誤消息:
追溯(最近一次通話):文件“ /home/sterin/ClionProjects/numpy1/numpy_test.py”,第15行,在generate_symbolic_transformation T = T.subs([(thetax,0),(thetaz,0)])NameError :未定義全局名稱“ thetax”
幫助您更早地發現錯誤。
NULL
值作為Python C / API調用的PyObject*
參數(除非特別允許)。 這將抓住使用NULL
結果的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.