I am constructing a collection of polylines like code below.
Is it possible to use ON_SimpleArray instead of std::vector?
Or ON_SimpleArray can only store simple data like double, int and pointers to objects like ON_SimpleArray<ON_Polyline*> but not ON_SimpleArray<ON_Polyline>?

I tried to use ON_SimpleArray<ON_Polyline> and Append polylines but the app crashes for attempting to read or write protected memory.

    std::vector<ON_Polyline> polylinePairs(VertexSumsCount);
    ON_Polyline polyline;

    int counter = 0;
    int lastCount = VertexSums[counter];
    for (int i = 0; i < PositionsXYZ_N ; i+=3) {

        ON_3dPoint p(PositionsXYZ[i], PositionsXYZ[i + 1], PositionsXYZ[i + 2]);

        if (polyline.Count() == lastCount) {
            polylinePairs[counter] = polyline;
            polyline.Empty();//Clear points from the polyline
            lastCount = VertexSums[++counter]; //Take next polyline Count

Hi Petras,

Is there a reason why you don’t want to use std::vector?

If I recall correctly, ON_SimpleArray can only be initialized as default types (e.g. bool, char, short, int, float, etc.) as well as as pointers to these types, thus the prefix “simple”.
Since, opennurbs is open-source, you could simply add ON_Polyline support to ON_SimpleArray yourself. Keep in mind that ON_Polyline is already a custom array of sorts. It inherits from ON_3dPointArray!

1 Like


It was just a curiosity.

But your comments confirms what I thought, that ON_SimplyArray is only for simple types.

ON_ClassArray is what you want to use for data that requires destructors

Thanks, does it mean that every time I construct ON_Polyline, I need to call destructor or the data is deleted after the call of the function? I am talking here not about arrays but individual data structures such as ON_Polyline, ON_Brep, ON_Mesh an so on.

You don’t need to call the destructor. ON_ClassArray<ON_Polyline> will destroy the internal ON_Polyline data for you.


Sorry I have too many questions, but I am new to OpenNurbs.

When I create point array with opennurbs and I know the size of array, how do I add the point to it?

ON_3dPointArray pointlist(10);
pointlist[0] = ON_Point3d(0,0,0);

or I need to append points:

I am really curious why it is not better to use std:vector<ON_Point3d> instead

Passing a number to the constructor sets the array’s capacity, but not the count. The capacity is the size of the internally managed array of data. The count is the number of elements that the ON_…Array thinks it has in it (which is always less than of equal to capacity).

For the first technique, you should call SetCount(10) first and then you can use the indexing operator to set individual elements.

The second technique of calling Append will just work and is what I typically do.

You can use std::vector<ON_3dPoint> as well. It is very similar to our ON_SimpleArray class. I wouldn’t say one is any better than the other; they have both been around for many many years and are very similar. I personally use the ON_…Array templates, but if you are more comfortable with std::vector then use that.

Thank you very much again.

If I write this the array gets filled with number of points:

 ON_3dPointArray p(10);

        for (int j = 0; j < p.Count(); j++)
          p[j] = ON_3dPoint(0, 1, 2);

but I write this nothing gets added :

 ON_3dPointArray p();

        for (int j = 0; j < p.Count(); j++)
          p[j] = ON_3dPoint(0, 1, 2);

Since constructor input is capacity and setcount is number of elements.
How should I know what is the size of capacity?
Is it correct that adding element by index I must specify both capacity and count?

There are Reserve and SetCapacity functions that you can look at to see what they do. I typically use the simpler Append function to get around all of these issues.

1 Like