• Advertisement
Sign in to follow this  

data structure to represent a vector field

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What would be the best way to programatically represent a 2d/3d 'vector field' given a need to write code to calculate things like gradients convergence and vorticity. are the examples more or less equivalent or is one clearer/cleaner or to be prefered over another? Is there a better way ? //assuming 'matrix<T>' is a 2d indexable array of memory. const int nd = 2; ex1) matrix< float>[ nd] vf; ex2) matrix< float [nd]> vf; ex3) using field names struct V2 { int p0, p1 } ; matrix< V2> vf; ex4) struct V2 { matrix< float> p0, p1 } ; ex5) or as an additional dimension of multi-dim array ? ie boost multiarray vf[j][nd];

Share this post


Link to post
Share on other sites
Advertisement
[EDIT]made it more generic.

My suggestion:

template<class T, int D>
struct vector
{
T values[D];
T& operator[](int i){ assert(i<D); return values; }
};

template<class T, int D, int X, int Y=1, int Z=1>
struct vector_field
{
vector<T,D>& getAt( int x, int y=0, int z=0 )
{
assert( x<X && y<Y && z<Z );
return field[x + X*y + X*Y*z];
}
private:
vector<T,D> field[X*Y*Z];
};

void foo()
{
vector_field<float, 2, 512, 512> my2dVectorField512x512;
my2dVectorField512x512.getAt(42,24)[0] = 0;
}

The dimensions of the field and the vectors that it contains are independent. i.e. you could have a 2D field of 3D vectors, or a 3D field of 1D 'vectors'.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement