We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › formula for 3d grid
Pages: 1 2 
formula for 3d grid (Read 2403 times)
formula for 3d grid
Jan 2nd, 2010, 9:13am
 
based on
Code:
void draw(){
 background(255);
 int x,y;
 for(int p = 0; p < g.length; p++){
   x = p % w;
   y = p / w;

   println("y "+y+"    x "+x);
   //println(g[p]);
   stroke(g[p]);

   point(10 + x* 10, 10+y*10);
 }
}
;

which is for a 2d grid, i tried to convert for a 3d grid.
Only they y has to start again once it reaches the bottom.
And the z is new...

Does someone know the magical formula for the 3d version?


Code:
int w = 3, h = 6, d = 9;
int Max = w*h*d;

for(int p = 0; p < Max; p++){
 int x = p % w;
 int y = p;//how
 int z = p;//how
 println("z : "+z+"   y : "+y+"   x : "+x);
}


Re: formula for 3d grid
Reply #1 - Jan 2nd, 2010, 9:57am
 
In 3D need a slightly more complicated approach - I tried this and it worked for me.
Smiley
Code:

 for(int p = 0; p < Max; p++){
   int z = p / (w * h);//how
   int y = (p - z * w * h) / h;//how
   int x = (p - z * w * h - y * h);
   println("z : "+z+"   y : "+y+"   x : "+x);
 }

Re: formula for 3d grid
Reply #2 - Jan 2nd, 2010, 11:35am
 
your on a good way but it's not correct.

int w = 3, h = 6, d = 9;

w is x
h is y
d is z

By you x and y are switched

Quote:
z : 8   y : 0   x : 0
z : 8   y : 0   x : 1
z : 8   y : 0   x : 2
z : 8   y : 0   x : 3
z : 8   y : 0   x : 4
z : 8   y : 0   x : 5
z : 8   y : 1   x : 0
z : 8   y : 1   x : 1
z : 8   y : 1   x : 2
z : 8   y : 1   x : 3
z : 8   y : 1   x : 4
z : 8   y : 1   x : 5
z : 8   y : 2   x : 0
z : 8   y : 2   x : 1
z : 8   y : 2   x : 2
z : 8   y : 2   x : 3
z : 8   y : 2   x : 4
z : 8   y : 2   x : 5


Also the x in prev version was correct.
so

Code:

int x = p % w;
int z = p / (w * h);//how


work, only y needs a working solution now.
Allready thx for the z!
Re: formula for 3d grid
Reply #3 - Jan 4th, 2010, 6:50am
 
Try this out, it appears to do what you want.
Smiley

Code:

int w = 3, h = 6, d = 9;

/* this bit made the mapping clearer
w is x 0-2
h is y 0-5
d is z 0-8
*/

void setup(){
int Max = w*h*d;
for(int p = 0; p < Max; p++){
int z = p / (w * h);
int y = (p - z * w * h)/w;
int x = (p - z * w * h)%w;
println("z : "+z+" y : "+y+" x : "+x);
}
}
Re: formula for 3d grid
Reply #4 - Jan 4th, 2010, 12:16pm
 
thank you so much Smiley
You will be part of history Wink

one question, why:
//int x = (p - z * w * h)%w;

instand of?
int x = p % w;
Re: formula for 3d grid
Reply #5 - Jan 5th, 2010, 6:53am
 
Code:
int x = (p - z * w * h)%w; 



w*d  is the number of elements per depth level so z*w*d are the number of elements used for filled depth levels so p-z*w*d is the element number within the depth level (z) of interest (now a 2D problem). So now modulo with the width to get the x position.

Smiley
Re: formula for 3d grid
Reply #6 - Jan 5th, 2010, 8:09am
 
haha i don't understand that at all.

So it's better to use this?

int x = (p - z * w * h)%w;
Re: formula for 3d grid
Reply #7 - Jan 5th, 2010, 9:46am
 
With the risk of asking a stupid question: why don't you just use a three dimensional array?
Re: formula for 3d grid
Reply #8 - Jan 5th, 2010, 10:07am
 
Quaestions aren't stupid.

I tried multiple things and i think this method will be the fastest to check if there's a point left/right/under/up/in front/behind of it (i hope). I can be wrong but i'm gonna try this way anyway. I have another sketch that doesn't use this method and there certain calculations get done according to 2 images to create a 3d shape out of those images. Atm it can take up to 5 min before frame 1 is drawn (after that it goes smooth).

Also it's nice to improve my skills (since i lack of that Smiley )
Re: formula for 3d grid
Reply #9 - Jan 5th, 2010, 10:11am
 
In Java a 2D array is an array of arrays, and a 3D array is an array of 2D arrays.

Using an element in a 3D array e.g. a[2][3][6] is slow because effectively we are doing 3 array retrievals and 3 arrays bounds checking. So using a 1D array and calculating the array index to simulate a 3D array is much quicker just 1 array retrieval and one arrays bounds checking.

This saving can be very significant if you have a large 3D array and you have to iterate over all the elements in the array.

Smiley
Re: formula for 3d grid
Reply #10 - Jan 5th, 2010, 11:05am
 
There's my answer

Smiley

Re: formula for 3d grid
Reply #11 - Jan 5th, 2010, 12:37pm
 
Quark wrote on Jan 5th, 2010, 10:11am:
So using a 1D array and calculating the array index to simulate a 3D array is much quicker just 1 array retrieval and one arrays bounds checking.


Except none of the methods above actually calculate array indices in a 1D array Tongue
Re: formula for 3d grid
Reply #12 - Jan 5th, 2010, 12:40pm
 
clankill3r wrote on Jan 5th, 2010, 10:07am:
Quaestions aren't stupid.

I tried multiple things and i think this method will be the fastest to check if there's a point left/right/under/up/in front/behind of it (i hope). I can be wrong but i'm gonna try this way anyway. I have another sketch that doesn't use this method and there certain calculations get done according to 2 images to create a 3d shape out of those images. Atm it can take up to 5 min before frame 1 is drawn (after that it goes smooth).


And if it is for checking left/right/...

Here is some code that clearly proves it isn't very efficient:

Code:

int w = 300, h = 600, d = 900;
void setup()
{  
 long startTime = millis();
 {  
   int max = w*h*d;
   for(int p = 0; p < max; p++)
   {
int z = p / (w * h);
int y = (p - z * w * h)/w;
int x = (p - z * w * h)%w;

check(x, y, z);
   }
 }
 println("First method: " + (millis()-startTime));
 
 startTime = millis();
 {  
   for(int x = 0; x < w; x++)
   {
for(int y = 0; y < h; y++)
{
 for(int z = 0; z < d; z++)
 {
   check(x, y, z);
 }
}
   }
 }
 println("Second method: " + (millis()-startTime));  
}

void check(int x, int y, int z)
{
 //Yes, ... this does not do anything
}


I made the w, h, d values bigger to make the time difference clearly visible

Result on my PC:
First method: 2563ms
Second method: 125ms


Re: formula for 3d grid
Reply #13 - Jan 5th, 2010, 2:50pm
 
Here is a simpler way of deriving the x, y & z coordinates:
x = p % w
y = (i / w) % h
z = i / (w * h)

As p increases, x changes the fastest, y second, and z slowest. This assumes that p will never have a value greater than w * h * d. If it may then you need to change z to:
z = (i / (w * h)) % d

This should make the pattern clearer.

HTH.
Re: formula for 3d grid
Reply #14 - Jan 6th, 2010, 2:24am
 
Nice one lollypop yes it does make the pattern clearer.

My statement when comparing 1D and 3D arrays was
Quote:
This saving can be very significant if you have a large 3D array and you have to iterate over all the elements in the array.

To make a valid test of this would require us to compare array access time.
The code below creates both 1D and 3D arrays each with 162 million elements, it first populates (writes) the arrays with numbers and then sees how long it takes to sum (read) the elements. The results on my PC for just 1 iteration of the array were


Populate arrays
Array 3D time = 3297
Array 1D time = 2844
Sum elements of the arrays
Array 3D time = 734     sum = -81007808
Array 1D time = 625     sum = -80992192


I first noticed this difference when I was working on a sketch to load and display 3D models stored in MD2 format. Avoiding multiple access to the same element in a 3D array greatly increased the frame rates achieved.
Smiley

Code:
int w = 300, h = 600, d = 900 , maxp;
int sum;
long time;

void setup(){
 maxp = w*h*d;
 byte a1d[] = new byte[maxp];
 byte a3d[][][] = new byte[w][h][d];
 
 byte c = 0;
 println("Populate arrays");
 time = millis();
 for(int x = 0; x < w; x++){
   for(int y = 0; y < h; y++){
     for(int z = 0; z < d; z++){
       a3d[x][y][z] = (byte)((c++ % 250)- 125);
     }
   }
 }
 time = millis()- time;
 println("Array 3D time = "+time);

 time = millis();
 for(int i = 0; i < maxp; i++){
   a1d[i] = (byte)((c++ % 250)- 125);
 }
 time = millis()- time;
 println("Array 1D time = "+time);

 println("Sum elements of the arrays");

 sum = 0;
 time = millis();
 for(int x = 0; x < w; x++){
   for(int y = 0; y < h; y++){
     for(int z = 0; z < d; z++){
       sum += a3d[x][y][z];
     }
   }
 }
 time = millis()- time;
 println("Array 3D time = "+time + "     sum = " + sum);

 sum = 0;
 time = millis();
 for(int i = 0; i < maxp; i++){
   sum += a1d[i];
 }
 time = millis()- time;
 println("Array 1D time = "+time + "     sum = " + sum);


}

Pages: 1 2