Add const variable types for the SetArray methods in ON_SimpleArray

template <class T>
void ON_SimpleArray<T>::SetArray(T* p)
{
  if ( m_a && m_a != p )
    onfree(m_a);
  m_a = p;
}

The SetArray method for ON_SimpleArray is defined above.
In general c++ practice, p should not be modified.

It is fine though to be like this, but it will be very helpful to add an overload type of

template <class T>
void ON_SimpleArray<T>::SetArray(const T* p)

I’m working with other cpp libs, such as Eigen, whose methods often return a const type pointer.
The practice to convert it to a const is a bit tedious and actually should not be needed.

Actually, since you cannot convert a const pointer to a non-const, but just copy it, it doubles the computation time by not providing such an override method.

This is something I cannot do on the user side, and need you developers to do the modification.

Hi @xliotx,

You might consider using ON_SimpleArray<T>::Append.

There is always const_cast too.

ON_SimpleArray<ON_3dPoint> list;
list.SetArray(const_cast<ON_3dPoint*>(const_points), point_count, point_count);
list.KeepArray();

– Dale

1 Like

It can be pretty dangerous to do the const cast technique. Can you provide some more details on exactly what it is you are trying to do?

I agree. I used Append() in the end and didn’t use a const_cast, and that’s exactly why I think there should be an overload function for that purpose.

To respond to your questions, I’m developing a GH plugin for mesh processing using many methods developed and existing in the computer graphics community (cpp mainly).

For this purpose, all computation is done on the cpp side, using libraries like eigen. and then transfer data back to the Rhino side. I guess that’s why I’m posting pretty frequently in the past a few days…

For the question specifically in this post, I’d like to convert a eigen::VectorXd class, which provide a method vec.array() as a const T* so that I can initialize a ON_SimpleArray<T> with it. However, there is no overload function for the openNURBS class, and I need to manually convert it to a middle form or assign the data one by one using the Append method.

Hope it is clear this time.

Absolutely use the Append functions for this. SetArray is a rarely used function for only special cases.

Hi, me again.

What is the correct way of appending an ON_3dPointArray to a ON_SimpleArray<ON_3dPointArray>* isoP?

If I do:

    ON_3dPointArray tmpL(0);
    isoP->Append(tmpL.Count(), tmpL);

I will get the error:

I got this question because I have the pointer of

            var isoPcpp = new Rhino.Runtime.InteropWrappers.SimpleArrayArrayPoint3d();

from C# side, and want to pass data back from the cpp side.

However, after some search, it seems it is not supposed to use ON_SimpleArray<ON_3dPointArray> but rather ON_ClassArray<ON_3dPointArray>? Then how should I create the pointer on the C# side?

OK, I see the issue…

Rhino.Runtime.InteropWrappers.SimpleArrayArrayPoint3d();

is a container of pointers… which means it is ON_SimpleArray<ON_3dPointArray*>* instead of ON_SimpleArray<ON_3dPointArray>*

@stevebaer OK. The a new question then emerges:

if I create severalON_3dPointArray on the CPP side, and append it to my ON_SimpleArray<ON_3dPointArray*>* where the pointer is created on the C# side, then series of ON_3dPointArray will be unavailable after the cpp function ends its lifecycle, and I won’t get anything from the C# side.

So the question is re-framed to:

What is the CORRECT way of using Rhino.Runtime.InteropWrappers.SimpleArrayArrayPoint3d() between C# and cpp ?

You need to append instances created on the heap with the new keyword instead of on the stack.
ON_3dPointArray* a = new ON_3dPointArray():

The .NET class manages the lifetime of this newed up instance and will delete when either it is being garbage collected or you call Dispose()