计算变换球体的 AABB
我有一个在对象空间中由中心点和半径表示的球体。使用可能包括缩放、旋转和平移的变换矩阵将球体变换为世界空间。我需要为世界空间中的球体构建一个轴对齐的边界框,但我不知道该怎么做。
这是我目前的方法,适用于某些情况:
public void computeBoundingBox() {
// center is the middle of the sphere
// averagePosition is the middle of the AABB
// getObjToWorldTransform() is a matrix from obj to world space
getObjToWorldTransform().rightMultiply(center, averagePosition);
Point3 onSphere = new Point3(center);
onSphere.scaleAdd(radius, new Vector3(1, 1, 1));
getObjToWorldTransform().rightMultiply(onSphere);
// but how do you know that the transformed radius is uniform?
double transformedRadius = onSphere.distance(averagePosition);
// maxBound is the upper limit of the AABB
maxBound.set(averagePosition);
maxBound.scaleAdd(transformedRadius, new Vector3(1, 1, 1));
// minBound is the lower limit of the AABB
minBound.set(averagePosition);
minBound.scaleAdd(transformedRadius, new Vector3(-1,-1,-1));
}
但是,我怀疑这是否总是有效。不均匀缩放不应该失败吗?
I have a sphere represented in object space by a center point and a radius. The sphere is transformed into world space with a transformation matrix that may include scales, rotations, and translations. I need to build a axis aligned bounding box for the sphere in world space, but I'm not sure how to do it.
Here is my current approach, that works for some cases:
public void computeBoundingBox() {
// center is the middle of the sphere
// averagePosition is the middle of the AABB
// getObjToWorldTransform() is a matrix from obj to world space
getObjToWorldTransform().rightMultiply(center, averagePosition);
Point3 onSphere = new Point3(center);
onSphere.scaleAdd(radius, new Vector3(1, 1, 1));
getObjToWorldTransform().rightMultiply(onSphere);
// but how do you know that the transformed radius is uniform?
double transformedRadius = onSphere.distance(averagePosition);
// maxBound is the upper limit of the AABB
maxBound.set(averagePosition);
maxBound.scaleAdd(transformedRadius, new Vector3(1, 1, 1));
// minBound is the lower limit of the AABB
minBound.set(averagePosition);
minBound.scaleAdd(transformedRadius, new Vector3(-1,-1,-1));
}
However, I am skeptical that this would always work. Shouldn't it fail for non-uniform scaling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,变换后的球体将是某种椭球体。为其获得精确的边界框并不难;如果您不想完成所有数学运算:
M
是您的变换矩阵(缩放、旋转、平移等),S
的定义R
如最后所述,R< 计算
x
、y
和z
边界/code> 如前所述。一般圆锥曲线(包括球体及其变换)可以表示为对称 4x4 矩阵:当
p^t 时,齐次点
p
位于圆锥曲线S
内部S p < 0 。通过矩阵 M 变换空间会按如下方式变换 S 矩阵(下面的约定是点是列向量):圆锥曲线的对偶适用于平面而不是点,由 S 的倒数表示;对于平面 q(表示为行向量):
因此,您正在寻找与变换二次曲线相切的轴对齐平面:
要查找与 R 相切的 xy 对齐平面:
同样,对于 xz 对齐平面:
和 yz -对齐平面:
这为您提供了变换后的球体的精确边界框。
In general, a transformed sphere will be an ellipsoid of some sort. It's not too hard to get an exact bounding box for it; if you don't want go through all the math:
M
is your transformation matrix (scales, rotations, translations, etc.)S
belowR
as described towards the endx
,y
, andz
bounds based onR
as described last.A general conic (which includes spheres and their transforms) can be represented as a symmetric 4x4 matrix: a homogeneous point
p
is inside the conicS
whenp^t S p < 0
. Transforming your space by the matrix M transforms the S matrix as follows (the convention below is that points are column vectors):The dual of the conic, which applies to planes instead of points, is represented by the inverse of S; for plane q (represented as a row vector):
So, you're looking for axis-aligned planes that are tangent to the transformed conic:
To find xy-aligned planes tangent to R:
Similarly, for xz-aligned planes:
and yz-aligned planes:
This gives you an exact bounding box for the transformed sphere.
@comingstorm 的答案很好,但可以简化很多。如果
M
是球体的变换矩阵,从 1 开始索引,那么(假设球体在变换之前半径为 1,其中心位于原点。)
我写了一篇博客文章,其中证明 此处,对于合理的堆栈溢出来说太长了回答。
@comingstorm's answer is great but can be simplified a lot. If
M
is the sphere's transformation matrix, indexed from 1, then(This assumes the sphere had radius 1 and its center at the origin before it was transformed.)
I wrote a blog post with the proof here, which is much too long for a reasonable Stack Overflow answer.
@comingstorm 的答案很优雅,因为它使用了齐次坐标和圆锥曲线的对偶性。
该问题也可以视为约束最大化问题,可以通过拉格朗日乘子法求解。以 y 轴的 AABB 为例。优化目标为
,约束是椭球方程
拉格朗日为
其中 lambda 是乘数。极值只是以下方程的解
给出
@comingstorm's answer is elegant in the way that it uses the homogeneous coordinates and the duality of the conic.
The problem can also be viewed as a constrained maximization problem that can be solved by the Lagrange multiplier method. Use the AABB at y axis as an example. The optimization target is
and the constraint is the ellipsoid equation
and the Lagrange is
where lambda is the multiplier. The extrema are simply the solutions of the following equations
which gives
这不适用于非均匀缩放。可以使用拉格朗日乘子(KKT 定理)计算任意可逆仿射变换,我相信它会变得很难看。
但是 - 您确定需要精确的 AABB 吗?您可以通过变换球体的原始 AABB 并得到其 AABB 来近似它。它比精确的 AABB 更大,因此可能适合您的应用。
为此,我们需要三个伪函数:
GetAABB(sphere)
将获取球体的 AABB。GetAABB(points-list)
将获取给定点集的 AABB(只是所有点的最小/最大坐标)。GetAABBCorners(p, q)
将获取 AABB 的所有 8 个角点(其中 p 和 q)。This will not work for non-uniform scaling. It is possible to calculate for arbitrary invertible affine transform with Lagrange multipliers (KKT theorem) and I believe it will get ugly.
However - are you sure you need an exact AABB? You can approximate it by transforming the original AABB of the sphere and getting its AABB. It is larger than the exact AABB so it might fit your application.
For this we need to have three pseudo-functions:
GetAABB(sphere)
will get the AABB of a sphere.GetAABB(points-list)
will get the AABB of the given set of points (just the min/max coordinates over all points).GetAABBCorners(p, q)
will get all the 8 corner points of an AABB (p and q are among them).