向接受固定大小数组的函数传递浮动指针

时间:2022-06-09 02:56:17

I have a number of functions that have the following form:

我有一些函数的形式如下:

typedef float arr3[3];
float newDistanceToLine(arr3 &p0, arr3 &p1, arr3 &p2);

and now find convenient to store lots of points into a long array:

现在可以方便地将很多点存储到一个长数组中:

int n_points = 14;
float *points;
points = new float[3*n_points];

Is there a way to pass pointers to different values of the array "points" to my functions accepting fixed size arrays? I know that the following fails, but, I would like to do something like:

是否有一种方法可以将指针传递给接受固定大小数组的数组“点”的不同值?我知道下面的方法失败了,但是,我想做如下的事情:

newDistanceToLine(&points[3], &points[6], &points[9]);

or get any help on how best to reuse my code.

或者得到任何关于如何最好地重用我的代码的帮助。

Thanks!

谢谢!

2 个解决方案

#1


3  

Change interface of your newDistanceToLine to use type that is based on pattern that can be called either array_View or span - read this discussion.

更改newDistanceToLine的接口以使用基于可以调用array_View或span的模式的类型——请阅读本文。

Something like this:

是这样的:

typedef float arr3[3];
class arr3_view
{
public:
    arr3_view(arr3& arr) : data(arr) {}
    arr3_view(float* data, std::size_t size) : data(data) 
    {
        if (size != 3) // or < 3 - I am not sure what is better for your case
          throw std::runtime_error("arr3 - wrong size of data: " + std::to_string(size));
    }

    float* begin() { return data; }
    float* end() { return data + 3; }
    float& operator [](std::size_t i) { return data[i]; }
    // and similar stuff as above for const versions

private:
    float* data;
};

float newDistanceToLine(arr3_view p0, arr3_view p1, arr3_view p2);

So - for you 9-elements arrays we will have such usage:

因此,对于你的9个元素数组,我们将有这样的用法:

newDistanceToLine(arr3_view(arr, 3), 
                  arr3_view(arr + 3, 3), 
                  arr3_view(arr + 6, 3));

#2


1  

Use data structure instead.

使用数据结构。

struct SPosition
{
SPosition( float x = 0, float y = 0, float z = 0)
    :X(x)
    ,Y(y)
    ,Z(z)
{

}
float X;
float Y;
float Z;
};

std::vector<SPosition> m_positions;

float newDistanceToLine( const SPosition& pt1, const SPosition& pt2, const SPosition& pt3 )
{
    // to do
    return 0.f;
};

#1


3  

Change interface of your newDistanceToLine to use type that is based on pattern that can be called either array_View or span - read this discussion.

更改newDistanceToLine的接口以使用基于可以调用array_View或span的模式的类型——请阅读本文。

Something like this:

是这样的:

typedef float arr3[3];
class arr3_view
{
public:
    arr3_view(arr3& arr) : data(arr) {}
    arr3_view(float* data, std::size_t size) : data(data) 
    {
        if (size != 3) // or < 3 - I am not sure what is better for your case
          throw std::runtime_error("arr3 - wrong size of data: " + std::to_string(size));
    }

    float* begin() { return data; }
    float* end() { return data + 3; }
    float& operator [](std::size_t i) { return data[i]; }
    // and similar stuff as above for const versions

private:
    float* data;
};

float newDistanceToLine(arr3_view p0, arr3_view p1, arr3_view p2);

So - for you 9-elements arrays we will have such usage:

因此,对于你的9个元素数组,我们将有这样的用法:

newDistanceToLine(arr3_view(arr, 3), 
                  arr3_view(arr + 3, 3), 
                  arr3_view(arr + 6, 3));

#2


1  

Use data structure instead.

使用数据结构。

struct SPosition
{
SPosition( float x = 0, float y = 0, float z = 0)
    :X(x)
    ,Y(y)
    ,Z(z)
{

}
float X;
float Y;
float Z;
};

std::vector<SPosition> m_positions;

float newDistanceToLine( const SPosition& pt1, const SPosition& pt2, const SPosition& pt3 )
{
    // to do
    return 0.f;
};