计算变换球体的AABB

Nic*_*ner 11 graphics 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));
}
Run Code Online (Sandbox Code Playgroud)

但是,我怀疑这总是有效的.它不应该因非均匀缩放而失败吗?

com*_*orm 10

通常,变换的球体将是某种椭圆体.得到一个精确的边界框并不难; 如果你不想通过所有数学:

  • 请注意,这M是您的转换矩阵(比例,旋转,翻译等)
  • 阅读S下面的定义
  • R按照最后描述的方式进行计算
  • 计算x,yz基于边界R的最后描述.

一般圆锥(包括球体及其变换)可以表示为对称的4x4矩阵:均匀点p在圆锥内部Sp^t S p < 0.通过矩阵M转换空间M转换S矩阵如下(下面的约定是点是列向量):

A unit-radius sphere about the origin is represented by:
S = [ 1  0  0  0 ]
    [ 0  1  0  0 ]
    [ 0  0  1  0 ]
    [ 0  0  0 -1 ]

point p is on the conic surface when:
0 = p^t S p
  = p^t M^t M^t^-1 S M^-1 M p
  = (M p)^t (M^-1^t S M^-1) (M p)

transformed point (M p) should preserve incidence
-> conic S transformed by matrix M is:  (M^-1^t S M^-1)
Run Code Online (Sandbox Code Playgroud)

圆锥的对偶,适用于平面而不是点,由S的倒数表示; 对于平面q(表示为行向量):

plane q is tangent to the conic when:
0 = q S^-1 q^t
  = q M^-1 M S^-1 M^t M^t^-1 q^t
  = (q M^-1) (M S^-1 M^t) (q M^-1)^t

transformed plane (q M^-1) should preserve incidence
-> dual conic transformed by matrix M is:  (M S^-1 M^t)
Run Code Online (Sandbox Code Playgroud)

因此,您正在寻找与变换后的圆锥相切的轴对齐平面:

let (M S^-1 M^t) = R = [ r11 r12 r13 r14 ]  (note that R is symmetric: R=R^t)
                       [ r12 r22 r23 r24 ]
                       [ r13 r23 r33 r34 ]
                       [ r14 r24 r34 r44 ]

axis-aligned planes are:
  xy planes:  [ 0 0 1 -z ]
  xz planes:  [ 0 1 0 -y ]
  yz planes:  [ 1 0 0 -x ]
Run Code Online (Sandbox Code Playgroud)

要找到与R相切的xy对齐平面:

  [0 0 1 -z] R [ 0 ] = r33 - 2 r34 z + r44 z^2 = 0
               [ 0 ]
               [ 1 ]
               [-z ]

  so, z = ( 2 r34 +/- sqrt(4 r34^2 - 4 r44 r33) ) / ( 2 r44 )
        = (r34 +/- sqrt(r34^2 - r44 r33) ) / r44
Run Code Online (Sandbox Code Playgroud)

同样,对于xz对齐的平面:

      y = (r24 +/- sqrt(r24^2 - r44 r22) ) / r44
Run Code Online (Sandbox Code Playgroud)

和yz对齐的平面:

      x = (r14 +/- sqrt(r14^2 - r44 r11) ) / r44
Run Code Online (Sandbox Code Playgroud)

这为您提供了转换球体的精确边界框.


Tav*_*nes 5

@comingstorm 的答案很好,但可以简化很多。如果M是球体的变换矩阵,索引从 1 开始,则

x = M[1,4] +/- sqrt(M[1,1]^2 + M[1,2]^2 + M[1,3]^2)
y = M[2,4] +/- sqrt(M[2,1]^2 + M[2,2]^2 + M[2,3]^2)
z = M[3,4] +/- sqrt(M[3,1]^2 + M[3,2]^2 + M[3,3]^2)
Run Code Online (Sandbox Code Playgroud)

(这假设球体在变换之前半径为 1,其中心位于原点。)

我在这里写了一篇带有证明的博客文章,对于合理的 Stack Overflow 答案来说太长了。