3d Grid

I have to do a generic class Grid3D that implements a 3-dimensional structure of cells. Each cell can be accessed by giving the coordinates of the plane (z), row (y) and column (x).
I'll be able to request and write the value of a cell to
position (z, y, x) through operator (). Ex: G (3,1,6) = G (3,2,2).

I think that i have to do a multi-dimensional array (array of array of array) but i don't know how to do it. I can't use vector, is a specific request.

Help me please :)

int 3dArray[xSize][ySize][zSize];

There's a 3d array of ints, You retreive an element like this

int val = 3dArray[xPos][yPos][zPos];

and set the data at an element like this

3dArray[xPos][yPos][zPos] = val;

Last edited on
Thanks :) Tomorrow I'll see that tutorial and i will try some code. I will ask you again if i have a problem
I would suggest making a wrapper class, as is mentioned in the article hamsterman gave. Store it as a 1d array using the equation: index=z*width*height+y*width+x
Ok I have a problem, I have my 3D array
int 3dArray[xSize][ySize][zSize];
how can i refer to it with a pointer?

I know that for 1D array i can do like this
1
2
3
4
int array[3]={-1,-2,-3};
int *p;
p=array;
p++;  // array[1] 


I know that for 2D array i can do like this
1
2
3
4
5
int array[2][3]={{1,2,3},{4,5,6}};
int (*p)[3]; // Pointer to an array of 3 elements (int)
p=array; 
p[0][1]=0;
p++; // it moves to the second rows of matrix 


and now, how can i do for my 3D array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
template<typename T>
struct array3d{
	protected:
		T* data;
		unsigned w,h,l;
	public:
		array3d(){
			data=NULL;
			len=0;}
		array3d(const array3d<T>& other){
			unsigned x,size=other.w*other.h*other.l;
			data=new T[size];
			w=other.w;
			h=other.h;
			l=other.l;
			for(x=0;x<size;++x){
				data[x]=other.data[x];}}
		array3d(unsigned width,unsigned height,unsigned length){
			w=width;
			h=height;
			l=length;
			data=new T[w*l*h];}
		~array3d(){
			delete[] data;}
		inline T& operator()(unsigned x,unsigned y,unsigned z){
			return data[z*width*height+y*width+x];}
		inline const T& operator()(unsigned x,unsigned y,unsigned z) const{
			return data[z*width*height+y*width+x];}
		inline unsigned size() const{
			return w*h*l;}};

Using the code above, you can have a 3d array without any of that confusing notation. It's also easy to extend to have more functionality.
Last edited on
Oh great Thank you.

I try
Ok, I do it.
I have implemented the code you posted above and i've implemented some other methods.

Now I have to implement a generic function evaluate that recive as parameter a grid3d A and a property P, and make as result a grid3d B of boolean where B(x,y,z)=true if P(A(x,y,z)) is vefify (is true). for example:
- I have a grid3d of int and i want to verify which cell have a number in a range ( or if the number is int)
- I have a grid3d of couple name-surname, and i want to verify witch cell contain a specific surname (or witch cell contain a surname that start with a specific letter)

I suppose that i should use functor for do this. But i don't know how.
Or maybe there is an another method to do this.

Help me please :)
I try to do this:

in Grid3D.h
1
2
3
4
5
6
7
8
9
10
11
12
template <typename T, typename F> 
 void evaluate(const Grid3D<T> &a,const F &pred){
             int sz_x = a.sz_x;
             int sz_y = a.sz_y;
             int sz_z = a.sz_z;
                                          
             Grid3D<bool> b(sz_x,sz_y,sz_z,false);  //costruttore secondario
             for(typename Grid3D<T>::size_type i = 0; i < a.size(); ++i){
                if(pred(a[i]))
                   b[i] = true;
             }
    }

as a global function

and then in the main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct predicate{ // say if a number is in a range min-max
       int min; 
       int max;    
       
	   predicate() : min(0), max(5) {}
       
	   bool operator()(int v) const {
		  return ((v > min) & (v < max));
       }
	};

     predicate pred;
    
    Grid3D<int> a(2,1,1); 
     
    evaluate(a,pred);


but i have an error

In function `int main(int, char**)':
no matching function for call to `evaluate(Grid3D<int>&, main(int, char**)::predicate&)'
why with function like this
1
2
3
bool greater_than(const int &a) {
        return a>0;
    } 

that is a global function
evaluate(grid2,greater_than) // grid2 is a grid3d
it runs

Instead

with the struct above and
evaluate(a,pred);

it doesn't run
Topic archived. No new replies allowed.