Boost logo

Ublas :

Subject: [ublas] fixed size vector in boost::numeric::ublas?
From: Markus Grabner (grabner_at_[hidden])
Date: 2010-07-22 18:16:48

        Hi all!

    In boost::numeric::ublas, there exist different vector variants
(unbounded and bounded). All of them can be resized dynamically (up to a
maximum size defined at compile time in the case of the bounded vector).
Therefore the vector class (or the underlying storage array) have a size
member which indicates the current size of the vector.

However, it would be extremely useful to have a vector of fixed size, i.e.,
one which can't be resized dynamically, but has its size statically defined
at compile time. Such a vector does not require a size member and only
consumes N*sizeof(T) bytes of storage, where N is the (static) vector size
and T the value type. Many libraries dealing with geometric data (OpenGL and
OpenInventor being prominent examples) store their data in compact arrays in
their native data types, e.g., the values (x0,y0,z0,x1,y1,z1,x2,y2,z2)
stored in consecutive memory locations define three points in 3D space. By
means of a fixed size vector, the entire boost::numeric::ublas functionality
could be applied to such arrays by a simple reinterpret_cast on each
element. This is not possible with the bounded or unbounded vectors since
the vector must be constructed before with its size member properly
initialized, and the actual vector data must therefore be copied explicitly
before use.

I tried to create a fixed_array class to implement this proposal (see
attachment), it can be used, e.g., to create the equivalent of the
OpenInventor "SbVec3f" data type as follows:

typedef boost::numeric::ublas::vector<float,fixed_array<float,3> > vec3d_t;

It is probably not the cleanest solution, and I skipped the serialization
stuff altogether, but it demonstrates an approach how this can be done.

Is such a feature already available in ublas and did I just miss it? If not,
what do you think about adding it to improve boost's interoperability with
other libraries?

        Thanks & kind regards,

Markus Grabner
Institute for Computer Graphics and Vision
Graz University of Technology, Inffeldgasse 16a/II, 8010 Graz, Austria