all groups > dotnet performance > march 2004 >
You're in the

dotnet performance

group:

Creating BSP tree


Creating BSP tree Morten Nielsen
3/23/2004 6:34:20 PM
dotnet performance:
I'm trying to generate a Binary Space Partion tree. All the code-examples
that I have been looking at, all use C pointer, malloc() etc, which is
pretty hard to convert to C#.
I have created my own Create_BSP function, but its slooooow and very memory
consuming, when the tree-depth gets above around 20.

Does anyone have any C# optimized tree-creating code (preferable for a BSP
tree) that I could have a look at?

Btw. here's the node-class used in my BSP tree. Maybe someone can see if
this is all wrong.

public class BSP_Node
{
public Point3 min; //struct with three floats
public Point3 max; //struct with three floats
public BSP_Node[] child; //child nodes
public Face[] obj; //struct with three Point3
public BSP_Node()
{
min = new Point3();
max = new Point3();
child = new BSP_Node[2];
}
}

Re: Creating BSP tree Justin Rogers
3/23/2004 7:35:15 PM
Morten, I'm not quite done with the optimized BSP tree that I'm working on,
but you can use array indices instead of pointers. Sorry I can't be more
forthcoming with information on how to make this modification and have it
work appropriately. Hopefully I'll have the code done shortly and can simply
reference it.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

[quoted text, click to view]

Re: Creating BSP tree Simon Sheffield
3/23/2004 8:05:33 PM
You could try changing the BSP_Node to a struct instead of a class.
As long as you use arrays (as you are) rather than generic containers (such
as ArrayList) you won't incur the overhead of boxing, adn because you are
using less reference types there will be less garbage collection to do.

Simon.

[quoted text, click to view]

Re: Creating BSP tree Morten Nielsen
3/23/2004 8:37:48 PM
[quoted text, click to view]

You can't make a self-referencing struct. The original C-kode examples all
uses struct, with pointers to childs of the same type. This is not possible
with C#.

/Morten

Re: Creating BSP tree Simon Sheffield
3/23/2004 9:09:27 PM
good point.

[quoted text, click to view]

Re: Creating BSP tree Morten Nielsen
3/24/2004 1:30:35 PM
[quoted text, click to view]

I think I know what you mean, but I'm not quite sure on what that would
help, or else I didn't understand you.

Instead of "public Face[] obj;" in the class, you would want a "public int
index;" ?
....where 'index' refers to the index in a Face[] list ?

/Morten

Re: Creating BSP tree Justin Rogers
3/24/2004 4:40:08 PM
That is pretty much the idea, yes. In some cases, you can even use the array
as the partioning tree, but those are pretty special cases. The only problem
with
using an offset to an index is the growth factor that you hit if you overrun
your
allocated array. I recommend allocating a large enough array to suit your needs
so this doesn't happen. While you become slightly memory dependent, your speed
increases quite a bit and the locality of your nodes is maintained for purposes
of GC.

There are loads of optimizations. If you think you'd like to go the path of
nit-picking
let me know (contact through my blog), and we can do something over email with
our probably different implementations.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

[quoted text, click to view]

AddThis Social Bookmark Button