3d to 1d Array Conversion

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 7
Reputation: Joe689 is an unknown quantity at this point 
Solved Threads: 0
Joe689 Joe689 is offline Offline
Newbie Poster

3d to 1d Array Conversion

 
0
  #1
Feb 19th, 2007
for (i = 0; i < length; i++)
{
for (j = 0; j < width; j++)
{
for (k = 0; k < height; k++)
{
interpolated_1D[i* j *height + k] = interpolated_input[i][j][k] ;
}
}
}


Doesn't seem to work,,, What am I doing wrong... Everything is allocated fine. No seg faults.

thankls in advance
joe
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: 3d to 1d Array Conversion

 
0
  #2
Feb 19th, 2007
The problem lies in the formula you're using to calculate the 1 dimensional array. It should be:
  1. i*length*width + j*width + k
Since i is represents not only a row but also a column, you're going to have to multiply it by both to get a correct index value.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 329
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 37
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz

Re: 3d to 1d Array Conversion

 
0
  #3
Feb 19th, 2007
If you think about it, any multi-demensional array is in essence a 1D array..

If you have ever studied assembly language you would know this is true. My opinion would be to just keep your 3D array, and access everything through a 1D array subscript.

You could access every element allocated by this 3D array with just one subscript.. which in this case would be equal to i*j*k. Even though you may have originally allocated let's say, 5 elements for your first dimension, you could safely run out of bound of the first dimension because the memory allocated is contiguous. Example, if i=5, j=10, and k=20, you could safely traverse the 1D subscript up to 1000 elements.

You can safely overrun the first dimension of a multi-dimensional array to a value that is the product of all dimensions of that array in order to get 1D performance from the multi-dimensional array. This will save you code and overhead trying to unnecessarily copy a multi-dimensional array to a 1D array.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: Joe689 is an unknown quantity at this point 
Solved Threads: 0
Joe689 Joe689 is offline Offline
Newbie Poster

Re: 3d to 1d Array Conversion

 
0
  #4
Feb 19th, 2007
///////////////////////////"You can safely overrun the first dimension of a multi-dimensional array to a value that is the product of all dimensions of that array in order to get 1D performance from the multi-dimensional array. This will save you code and overhead trying to unnecessarily copy a multi-dimensional array to a 1D array."//////////////////


Could you explain more w/ code?


Thanks for the quick replies. I prefer the 3D array because I am doing a rotational matrix. However, VTK (a visualization library) requires a 1D array in order to make use of it.

Also, I am working with 2 full 3D 512*512*512 shorts, so memory is an issue, so I would like to avoid extra allocation espcially when both arrays are indentical. Is there a way to use pointers and not reallocate a new 1d array and just keep the allocated 3D array.

--In other words.. can I make a 1d array from my 3d without extra memory?

VTK needs a short *.... I have a short***
Last edited by Joe689; Feb 19th, 2007 at 7:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: 3d to 1d Array Conversion

 
0
  #5
Feb 19th, 2007
> --In other words.. can I make a 1d array from my 3d without extra memory?
Read my previous post. I *think* it works...

However, it's not very fast as explained by Clinton. Your choice: memory or speed. Generally in game programming it's memory since people buy lots of memory to run games anyway.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 329
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 37
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz

Re: 3d to 1d Array Conversion

 
0
  #6
Feb 19th, 2007
Here is a small example:

  1. //Declare a 3D array
  2. int myarray[5][10][20];
  3.  
  4. //Populate the array
  5. for(int i=0; i<5; i++)
  6. for( int j=0; j<10; j++)
  7. for(int k=0; k<20; k++)
  8.  
  9. myarray[i][j][k] = k;
  10.  
  11. //Display the array multi-dimensionally
  12. for(int i=0; i<5; i++)
  13. for( int j=0; j<10; j++)
  14. for(int k=0; k<20; k++)
  15.  
  16. cout << myarray[i][j][k] << endl;
  17.  
  18. //Display the array in one dimension
  19. for(int i=0; i<1000; i++)
  20.  
  21. cout << myarray[i] << endl;

Although it can be done this way, doesn't mean it should be done this way. Some would argue that this type of coding would be dancing barefoot on the broken glass of undefined behavior. However, my argument is, 'why try to make a 1D array out of a 3D array.. when a multi-dimensional array is already a 1D array...'
Last edited by Clinton Portis; Feb 19th, 2007 at 8:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: Joe689 is an unknown quantity at this point 
Solved Threads: 0
Joe689 Joe689 is offline Offline
Newbie Poster

Re: 3d to 1d Array Conversion

 
0
  #7
Feb 19th, 2007
That is pretty interesting, I never knew that. However I don't think that will help me. I have a *** short pointer.

I am trying to fulfill
SetImportVoidPointer (void *ptr)

I tried force-casting it for shits and giggles and got undesirable results...

Is there a way I can form a new *short pointer 1D array, with no memory allocation and just have it point to the 3D array
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: 3d to 1d Array Conversion

 
0
  #8
Feb 19th, 2007
I don't know if my computer is just being finnicky or what, but Clinton's code didn't work right for me. Here's some code and output (trivial changes to the code):
  1. int main()
  2. {
  3. //Declare a 3D array
  4. int myarray[5][10][20];
  5.  
  6. //Populate the array
  7. for(int i=0; i<5; i++)
  8. for( int j=0; j<10; j++)
  9. for(int k=0; k<20; k++)
  10.  
  11. myarray[i][j][k] = k;
  12.  
  13. //Display the array multi-dimensionally
  14. for(int i=0; i<5; i++)
  15. for( int j=0; j<10; j++)
  16. for(int k=0; k<20; k++)
  17.  
  18. cout << "multi: " << myarray[i][j][k] << endl;
  19.  
  20. //Display the array in one dimension
  21. for(int i=0; i<1000; i++)
  22. cout << "single: " << myarray[i] << endl;
  23. }
Output:
  1. multi: 0
  2. multi: 1
  3. multi: 2
  4. multi: 3
  5. multi: 4
  6. ...
  7. multi: 16
  8. multi: 17
  9. multi: 18
  10. multi: 19
  11. single: 0xbfbbf300
  12. single: 0xbfbbf620
  13. single: 0xbfbbf940
  14. ...
  15. single: 0xbfc81ea0
  16. single: 0xbfc821c0
  17. single: 0xbfc824e0
When I used this code, I got matching outputs for single and multi though:
  1. int main()
  2. {
  3. ... // same as before
  4.  
  5. //Display the array in one dimension
  6. for(int i=0; i<1000; i++)
  7. cout << "single: " << myarray[0][0][i] << endl;
  8.  
  9. }
it also worked with (**myarray)[i]

But like Clinton said, you don't want to mess with undefined behavior...
Last edited by Infarction; Feb 19th, 2007 at 10:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 329
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 37
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz

Re: 3d to 1d Array Conversion

 
0
  #9
Feb 20th, 2007
error on my part w/ overrunning the first dimension of the array instead of the last dimension.

The residual hex values are cause i think because of 'int' overflow.. here is a similar (tested) code:

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. //Declare a 3D array
  7. int myarray[2][4][6];
  8.  
  9. //Populate the array
  10. for(int i=0; i<2; i++)
  11. for( int j=0; j<4; j++)
  12. for(int k=0; k<6; k++)
  13.  
  14. myarray[i][j][k] = k;
  15.  
  16. //Display the array multi-dimensionally
  17. for(int i=0; i<2; i++)
  18. for( int j=0; j<4; j++)
  19. for(int k=0; k<6; k++)
  20.  
  21. cout << myarray[i][j][k] << endl;
  22.  
  23. cin.get();
  24.  
  25. //Traverse the entire array in just one subscript, similar to a 1D array
  26. for(int i=0; i<48; i++)
  27.  
  28. cout << myarray[0][0][i] << endl;
  29.  
  30. cin.get();
  31.  
  32. return 0;
  33. }
Last edited by Clinton Portis; Feb 20th, 2007 at 12:05 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 7
Reputation: Joe689 is an unknown quantity at this point 
Solved Threads: 0
Joe689 Joe689 is offline Offline
Newbie Poster

Re: 3d to 1d Array Conversion

 
0
  #10
Feb 20th, 2007
I know this is frowned upon but can I do:

short ***3d;
short *1d

3d allocation//////////////and fill./////

1d = (short*)3d;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC