Saucy Dumpling
Saucy Dumpling

Reputation: 148

Cython cannot convert python object to const float *

I am wrapping a plotting library written in C++, and am currently working on the final plot function. I am using imgui-plot, so credit goes to soulthreads for writing that. That being said, I have modified the code a bit by moving all the structs in the header file to parent level and then assigning them to the plotconfig struct.

Here are snippets of relevant code edited for readability:

imgui-plot.h

struct Values{
    const float xs* = nullptr
    const float ys* = nullptr
};

struct PlotConfig{
    Values values
};

cimgui.pxd

cdef extern from 'imgui-plot.h' namespace 'ImGui':
    cdef struct Values:
        const float xs*
        const float ys*

    cdef struct PlotConfig:
        Values values

core.pyx

import cimgui

def plot(x_data, y_data):
    cdef cimgui.PlotConfig conf
    conf.values.xs = x_data
    conf.values.ys = y_data

    cimgui.Plot(conf)

Error I am getting

> conf.values.xs = x_data

Cannot convert python object to 'const float *'

I tried putting the x_data in to a vector and making it non constant. I am realizing now that the error likely has something to do with the variable being a constant pointer or reference to memory location, and python's object abstraction of arrays.

Anyway, I would really appreciate some tips on this.

Upvotes: 1

Views: 665

Answers (1)

Saucy Dumpling
Saucy Dumpling

Reputation: 148

For those wondering, these seems to have worked for me. I created a vector, pushed the values on to it, and assigned the pointer to the first value.

core.pyx

from libcpp.vector cimport vector
import cimgui

def plot(x_data, y_data):
    cdef cimgui.PlotConfig conf
    cdef vector[float] x_s
    cdef vector[float] y_s

    for x in x_data:
      x_s.push_back(x)
    for y in y_data:
      y_s.push_back(y)

    conf.values.xs = &x_s[0]
    conf.values.ys = &y_s[0]

    cimgui.Plot(conf)

Upvotes: 1

Related Questions