aprox. volume of box to box intersection

Started by
4 comments, last by quasar3d 12 years, 5 months ago
my math skills are a bit on the low side, so here goes:
is there a sensible (read fast-ish) way to calculate approximate volume of intersection between two oriented boxes (A, mtxA, B, mtxB) ? the result should be in relation to box A, ie:
  • A is not intersecting B, res = 0.0
  • A is partially intersecting B, res = 0.5
  • A is inside B, res = 1.0


I kinda need to calculate influence of objects B on object A. I could replace boxes with spheres to possibly ease the calculations. Again, I just need an approximation, not the precise values.
Advertisement
How approximately do you need it?

A really crude way would be to just generate a bunch of random points inside box A, then compute the fraction of those points that also lie inside B.

How approximately do you need it?

A really crude way would be to just generate a bunch of random points inside box A, then compute the fraction of those points that also lie inside B.


that's really crude :)



I think I going to simplify it with spheres, perhaps something like:
f = ( 1 - d / (rA + rB) ) * (rB / rA)

where:
rA, rB is the radius
d is distance between two sphere centres

I would just need a better spherical factor
If you're going to use spheres then you can compute the intersection volume exactly without an algorithm.


real sphere2sphere(sphere c1, sphere c2) {
vec delta = c2.pos - c1.pos;
real cr = c1.rad + c2.rad;
real ds = dot(delta,delta);

if(ds > cr*cr) return 0; //not intersecting
elif(ds < eps) return min(volume(c1.rad),volume(c2.rad)); //special case equal position (stability)
else {
real d = sqrt(ds);
real x1 = 0.5*(d - (c2.rad*c2.rad - c1.rad*c1.rad)/d);
real x2 = d - x1;

if (x1<=-c1.rad) return volume(c1.rad); //total containment
elif(x2<=-c2.rad) return volume(c2.rad); //total containment
else return cap_volume(x1,c1.rad) + cap_volume(x2,c2.rad);
}
}

real cap_volume(real x, real r) {
real h = r-x;
return pi*h*h*(3*r-h)/3;
}
real volume(real r) return cap_volume(-r,r);
I have a suggestion, but it depends on the assumption that by "oriented boxes" you mean something like this(but in 3D). If that is the case, then you can get the volume of intersection by subtracting the distance between the far sides of both boxes from the combined width of both boxes (for each of the 3 axes), changing any negative results to zero, then multiplying all three results to get a volume.

[quote name='quasar3d' timestamp='1321892893' post='4886249']
How approximately do you need it?

A really crude way would be to just generate a bunch of random points inside box A, then compute the fraction of those points that also lie inside B.


that's really crude :)



I think I going to simplify it with spheres, perhaps something like:
f = ( 1 - d / (rA + rB) ) * (rB / rA)

where:
rA, rB is the radius
d is distance between two sphere centres

I would just need a better spherical factor
[/quote]

Using random points isn't such an unusual way. It even has a name: http://en.wikipedia.org/wiki/Monte_Carlo_integration

Also, I think that if you use a sphere based method, the result you get might in general be pretty far off, so I think it can be worth it to calculate the chance that a certain number of random points will be off by a greater amount. I wouldn't be surprised if relatively few points already give pretty good results.

This topic is closed to new replies.

Advertisement