简体   繁体   English

将Python嵌入到C ++中,从而调用两个函数并传递数组

[英]Embedding Python into C++ calling two functions and passing array around

I'm trying to embedding python functions into my c++ code for doing some symbolic and algebraic operations. 我正在尝试将python函数嵌入到我的c ++代码中,以执行一些符号和代数运算。

My idea: I want to create a matrix with symbolic variables depending on some input variables. 我的想法:我想根据某些输入变量创建一个包含符号变量的矩阵。 This matrix I want to use afterwards in another function. 此后,我想在另一个函数中使用此矩阵。 This function is generating some coordinates out of the symbolic matrix. 此函数从符号矩阵中生成一些坐标。 And i want then to pass this results to my c++ code back. 然后我想将此结果传递回我的C ++代码。

My problem: I have a problem to get this matrix out of python to my c++ code and back to the other function as input. 我的问题:我在将矩阵从python提取到我的c ++代码并返回到另一个函数作为输入时遇到了问题。 Because i want to pass the symbolic matrix via c++ to the other python function which should generate out of this matrix the results. 因为我想将符号矩阵通过c ++传递给应该从该矩阵生成结果的另一个python函数。 Just to try my python code and the integration into c++ i wrote the code below. 为了尝试我的python代码并将其集成到c ++中,我编写了以下代码。

Edit: To make my question more specific I wrote the whole work I want to do with this functions in Python. 编辑:为了使我的问题更具体,我在Python中编写了要使用此函数进行的全部工作。 To clarify I need one function which is generating the symbolic matrix and the other has to calculate the values. 为了明确起见,我需要一个函数来生成符号矩阵,而另一个函数必须计算值。 And for the calculation the values are coming from the inside of my C++ code. 为了进行计算,这些值来自我的C ++代码内部。

Here the example written in Python: 这里是用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()

There are several errors here: 这里有几个错误:

  1. The Python function generate_symbolic_transformation throws an exception, which makes resultObj be NULL . Python函数generate_symbolic_transformation引发异常,这使得resultObjNULL This propagates further and leads to the crash. 这会进一步传播并导致崩溃。

  2. Even if resultObj was not NULL , it would not have been returned properly to its caller, as the last two lines of CallPlugIn_generate_symbolic_transformation make sure that a value is only returned it it is NULL . 即使resultObj不为NULL ,也不会适当地将其返回给其调用方,因为CallPlugIn_generate_symbolic_transformation的最后两行确保仅返回一个值为NULL

But these are specific problems. 但是这些是特定的问题。 I would also make a few general suggestion that might help you catch problems early, saving time and effort: 我还会提出一些一般性建议,这些建议可以帮助您及早发现问题,从而节省时间和精力:

  1. Handle Python errors. 处理Python错误。 At the bare minimum, if a Python C/API function returns NULL , print the error. 至少,如果Python C / API函数返回NULL ,则输出错误。 For example: 例如:

if (!resultObj) { PyErr_Print(); }

This would have resulted in an error message similar to: 这将导致类似于以下内容的错误消息:

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 追溯(最近一次通话):文件“ /home/sterin/ClionProjects/numpy1/numpy_test.py”,第15行,在generate_symbolic_transformation T = T.subs([(thetax,0),(thetaz,0)])NameError :未定义全局名称“ thetax”

Helping you to catch the error much earlier. 帮助您更早地发现错误。

  1. Make sure no NULL values are provided as PyObject* arguments to Python C/API calls (unless specifically allowed). 确保没有提供NULL值作为Python C / API调用的PyObject*参数(除非特别允许)。 This would have caught the place where the NULL result have been used. 这将抓住使用NULL结果的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM