Carve tool?

Blitz3D Forums/Blitz3D Programming/Carve tool?

Picklesworth(Posted 2004) [#1]
How do you make a carve tool that works on any shape mesh?


Caff(Posted 2004) [#2]
You need to use CSG subtraction. This is not an easy subject to tackle, but I think the official name would be a Boolean Subtraction.

Have a look at this code archive piece, it might be useful (I don't know I just quickly searched it out)

http://www.blitzbasic.com/codearcs/codearcs.php?code=552

[edit] The reason I say it's not easy to tackle is because it invariably screws up anything-less-than-simple geometry unless your a genius coder [/edit]


Picklesworth(Posted 2004) [#3]
I see what you mean. And I'm not a genius by a long shot :D

I think I'll start a little open source community project off my idea in a while to see if somebody else can do the hard stuff :D


podperson(Posted 2004) [#4]
Caff is correct, but I can outline the basic principles.

1) To boolean two solids together you first need to look for any intersections of their constituent polygons. In other words, for every polygon in A and every polygon in B, do they intersect? If so, cut each of them in two accordingly.

Clarification: for a given polygon in A you'll need to find all intersecting polygons in B and then cut the polygon from A apart. (And vice versa.) A given polygon in B won't generally cut a polygon in A in two on its own ... this is where things get hairy.

Note that this is a MAJOR operation for large polygon counts, and that the math here is a little hairy. (If A has a polygons and B has b polygons you'll need to perform a x b intersection operations.) Also note that it's considerably simpler if you're only dealing with triangles, so it's best to tesselate your solids before you start.

2) Now, do we throw away a given polygon or not?

If we're UNIONing, then we need to toss out polygons in the interior of the final object (i.e. any polygons from A contained in B and any polygons in B contained in A).

If we're SUBTRACTing B from A we need to toss out any polygons originally from A that are in B, and any polygons in B that aren't in A, and then flip any polygons in B that are in A.

If we're INTERSECTING A and B we need to toss out any polygons in A that weren't in B, and any in B that weren't in A.

Finally: to determine if a given polygon is INSIDE a shape you make a point in the middle of it (by averaging its vertices) and fire a linepick out in some direction and count the number of times you hit polygons in the object you want to know if you're inside. If it's an ODD number of times, you're inside it. If it's an EVEN number of times (ZERO is even) you're outside it.

Clarification: I don't know off the top of my head whether linepick (in polygon mode) will hit backfaces. If not, you'd need make a flipped copy of your objects and test against both.

And that's it. Easy huh?


Picklesworth(Posted 2004) [#5]
AAAAAAAAAAAAAAAAH!!


podperson(Posted 2004) [#6]
I hope that's a good AAAAAAAH :)


Picklesworth(Posted 2004) [#7]
Yes it sure is! I'm never looking at a carve tool, or a knife again!
For that matter, not even the word Boolean operation.

Thanks anyway if I ever overcome that fear though. That little guide could prove useful :D