# data structure to represent a vector field

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

## 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 on other sites

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'.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011266
• ### Who's Online (See full list)

There are no registered users currently online

×