|
-
Mar 5th, 2005, 04:32 PM
#1
Thread Starter
Not NoteMe
An ND array in a 1D array
I want to represent a binary tree, quad tree, oct tree etc. as one generic tree structure that can be used for any.
I've got a 1D array, that holds the children of a tree data structure.
The tree has the number of dimensions associated with it.
The number of children it has is:2^Dimensions since i'm divining things in two, for each dimension.
What i'm doing with the tree is dividing up an ND space into equil parts. i.e. for 2D space i'd need 4 chidren etc.
The trouble i'm having is when i come to work out the upper and lower bounds of each child (given the upper and lower bounds of the parent).
I need a way of knowing, for a given dimension (e.g. X,Y,Z...) in a given child (indexed in trhe 1D array), what the upper and lower bounds would be for this child
Thanks for any help, i'm really stuck with this and i need a solution pretty soon.
Sorry for the dodgy explanation, here's what i did with 3 dimensions, which may or may not help:
Code:
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
//Setup boundaries
m_pChildren[i][j][k]->m_pLowBounds[0] = (byte)(m_pLowBounds[0] + i * ((m_pHighBounds[0] - m_pLowBounds[0])/2.0f));
m_pChildren[i][j][k]->m_pHighBounds[0] = (byte)(m_pLowBounds[0] + (1+i) * ((m_pHighBounds[0] - m_pLowBounds[0])/2.0f));
m_pChildren[i][j][k]->m_pLowBounds[1] = (byte)(m_pLowBounds[1] + (j * ((m_pHighBounds[1] - m_pLowBounds[1])/2.0f)));
m_pChildren[i][j][k]->m_pHighBounds[1] = (byte)(m_pLowBounds[1] + (1+j) * ((m_pHighBounds[1] - m_pLowBounds[1])/2.0f));
m_pChildren[i][j][k]->m_pLowBounds[2] = (byte)(m_pLowBounds[2] + k * ((m_pHighBounds[2] - m_pLowBounds[2])/2.0f));
m_pChildren[i][j][k]->m_pHighBounds[2] = (byte)(m_pLowBounds[2] + (1+k) * ((m_pHighBounds[2] - m_pLowBounds[2])/2.0f));
}
}
}
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Mar 5th, 2005, 05:36 PM
#2
Thread Starter
Not NoteMe
Re: An ND array in a 1D array
Cracked it!
Basically i recursivly split the 1D array, remember whether i chose the left or the right each time, that dictates which branch i'm going down in terms of upper and lower bounds.
Code:
for(UINT i=0;i < (UINT)(1 << m_iDimensions);i++)
{
m_pChildren[i] = new CColourSpaceTree(m_iDepth+1,m_iDimensions);
//Recursivly divide the 1D array, remembering which side we chose each time
UINT Low = 0;
UINT High = 1<<m_iDimensions;
UINT Mid;
UINT LowValue;
for(UINT j=0;j<m_iDimensions;j++)
{
Mid = ((High-Low)>>1)+Low;
if(i <= Mid)
{
LowValue = 0;
High = Mid;
}
else
{
LowValue = 1;
Low = Mid;
}
m_pChildren[i]->m_pLowBounds[j] = m_pLowBounds[j] + LowValue * ((m_pHighBounds[j] - m_pLowBounds[j])/2.0f);
m_pChildren[i]->m_pHighBounds[j] = m_pLowBounds[j] + (LowValue + 1) * ((m_pHighBounds[j] - m_pLowBounds[j])/2.0f);
m_pChildren[i]->m_pParent = this;
}
}
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Mar 5th, 2005, 06:23 PM
#3
Thread Starter
Not NoteMe
Re: An ND array in a 1D array
Hmm, doesn't quite work, if anyone can see what i'm doing wrong, or can provide an answer i'd greatly appreciate it.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Mar 6th, 2005, 07:05 AM
#4
transcendental analytic
Re: An ND array in a 1D array
One thing you could do is to use a trick with templates, in recursion
template <int DIM,int D>
where DIM is the dimension count and D is the current dimension. In the function you call itself with the template parameters <DIM, D-1>. Make sure the function inlines, and Low, High and Mid will be calculated at compiletime. Then have partial template specialization (this requires a compiler that can do this, if you have msvc6 then theres a tecnique to this anyway which I have posted in the c++ forum, just search for partial template specialisation) with parameters <DIM, 0> to terminate the recursion.
Btw,
Mid = ((High-Low)>>1)+Low;
is the same as
Mid = ((High+Low)>>1);
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 6th, 2005, 07:31 AM
#5
Thread Starter
Not NoteMe
Re: An ND array in a 1D array
That's a very neat idea, thanks.
I managed to solve the problem anyway though. I don't think i'll use that template method this time since i want to be able to vary high and low on the top level tree, plus i don't have long until this is due in.
Definatly something to think about for another time though. 
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|