Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Rectangular Convex Decomposition of Arbitrary 3D Grid of Points

Author
Message
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 26th Jan 2014 08:01
I have a 3D grid of binary data values (either the point is solid or it's not). I need to generate a physics mesh from that grid, but it needs to be able to move, so I can't use triangle mesh, I must use a compound shape made of boxes. I need to find the largest boxes (or rather the least number of boxes) that the grid can be split into. Here's a 2D representation of what I want to do in 3D:



The first image shows each point as its own box - terribly inefficient (22 boxes). The second image shows what I would like the grid to become (4 boxes).

I realise there are convex decomposition libraries out there, but I need this to be exact, not approximate, and I thought there might be some easier method when the data is guaranteed to be in a grid. Also, I need boxes, not just convex shapes.

Any tips, pointers or help would be much appreciated

Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 26th Jan 2014 11:04
If you need this as a real mesh to feed to some physics plugin, can't you just create a vertex array and add into it to create each box, then scan through the array for identical vertices and meld those together? Of course you'd still need to triangulate it so it would probably be a bit tricky, but that should likely give the best performance once you have the mesh generated.


"Why do programmers get Halloween and Christmas mixed up?"
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 26th Jan 2014 11:24 Edited at: 26th Jan 2014 11:37
@Rudolpho:

Unfortunately it's not that simple. Plus, that's not actually the issue. The problem is not how to create a physics mesh from the grid, but how to split the grid up into convex boxes so it can be represented in the physics system with a compound shape made of boxes using the least number of boxes possible. Thanks anyway

Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 26th Jan 2014 12:02
One way would be to check to see what the largest box a point would fit into by checking the points next to it step by step until you find the largest possible box. The 2D method would be to check rows vs columns from that point until it doesn't form a box, then to check columns vs rows to see if a larger box is possible. Then you'd need to check each dot in the box to see if any point is part of a larger box and if so, exclude it and other if need be and the result would be the box... I think.

One way to find out I guess is to just program it. Back soon...

"Get in the Van!" - Van B
Le Verdier
13
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 26th Jan 2014 17:45
The closest stuff which comes to mind is Bounding Volume Hierarchy

http://en.m.wikipedia.org/wiki/Bounding_volume_hierarchy

Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 1st Feb 2014 05:27
Quote: "One way would be to check to see what the largest box a point would fit into by checking the points next to it step by step until you find the largest possible box. The 2D method would be to check rows vs columns from that point until it doesn't form a box, then to check columns vs rows to see if a larger box is possible. Then you'd need to check each dot in the box to see if any point is part of a larger box and if so, exclude it and other if need be and the result would be the box... I think."


Ok, so this is basically the idea I had when I finally just decided to go for it and program it in any way I could think of, except more complete.

Quote: "The closest stuff which comes to mind is Bounding Volume Hierarchy"


I'm not sure you understand what I mean, but at any rate, I found my own solution.

Here's before and after photos:





The algorithm currently takes about 55ms on my PC with a grid that size (64x64), but this is without any optimisations whatsoever. It's pretty close to optimal (I'd say 85-95%), certainly good enough for my liking.

Here's the current source code for anyone to do with as they please



Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 1st Feb 2014 14:49
Nice problem and solution.

I suspect it can be optimised but whether it's worth the effort is another matter. I guess your solution is a "greedy" one and such methods often work well in optimisation problems. I've copied your code in case I have any bright ideas (unlikely I suspect ).



Powered by Free Banners
Clonkex
Forum Vice President
14
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 2nd Feb 2014 07:39
Thanks

Quote: "I guess your solution is a "greedy" one"


Not entirely sure what would be described as "greedy". It's pretty brute force.

I was very surprised when I discovered that using array insert at top was quite a bit faster than array insert at bottom. I would have assumed DBPro's array's internally used a vector or similar and adding on the end would be faster than shuffling down and adding at the start. I've been using array insert at bottom for years

I was doing some tests earlier, and it turns out that adding the elements to the array is by far the slowest part, so I'll see if I can come up with a more elegant solution over just adding one when necessary.

Weirdly, I just tried it again and it was completing in just 16ms!! I must have had more background stuff running last time than I thought!

Lastly, I think there's still some bugs in it. I studied the resulting combinings of a few random generations and I'm pretty sure it's ignoring my wishes occasionally and favouring Y-combining over X-combining... bit strange.

Latest code with a couple of fixes:



Login to post a reply

Server time is: 2025-05-16 06:29:19
Your offset time is: 2025-05-16 06:29:19