Rectangular border box

Source: Internet
Author: User

Address: http://www.cppblog.com/lovedday/archive/2008/02/23/43122.html

Rectangular border box

Another common geometric element used to define an object is a rectangular boundary frame, which can be aligned with the axis or in any direction. The axis alignment rectangular boundary box has a limit that its edges must be perpendicular to the coordinate axis. The abbreviation AABB is often used to represent axially aligned bounding box (axis-aligned rectangular boundary box), and OBB is used to represent oriented bounding box (Direction rectangular boundary box ). The axis alignment rectangular boundary box is not only easy to create, but also easy to use.

A 3D AABB is a simple, and each side is parallel to a coordinate plane. The rectangular boundary frame is not necessarily a cube. Its length, width, and height can be different from each other. In Figure 12.10, some simple 3D objects and their AABB are drawn.

 

AABB expression

First, we will introduce some important properties of AABB and the methods used to reference these values. Points in AABB meet the following equations:

Xmin ≤ x ≤ xmax

Ymin ≤ y ≤ Ymax

Zmin ≤ z ≤ zmax

Two important points are:

PMin = [xmin ymin zmin]

PMax = [xmax Ymax zmax]

Central PointCIs:

C= (PMin +PMax)/2

"Dimension vector"SYesfromPMin pointsPThe max vector contains the length, width, and height of the rectangle boundary:

S=PMax-PMin

You can also find the "radius vector" of the rectangular boundary box"RIt is directed from the centerPMax vector:

R=PMax-C=S/2

Clearly define an AABB onlyPMin,PMax,C,S,RTwo of the five vectors (SAndRExcept for pairing, any two of them can be paired ). In some cases, some matching forms are more useful than others. We recommend that you usePMin andPMax indicates a boundary box, because in actual application, they are used more frequentlyC,S,R. Of coursePMin andPIt is easy for Max to calculate any of the other three.

In our c ++ code, use the following class to represent AABB, which is a thumbnail code list.

# Ifndef aabb3_h
# Define aabb3_h

# Include "vector3.h"

Class cmatrix4x3;

//---------------------------------------------------------------------------
// Implement a 3D axially aligned bounding box
//---------------------------------------------------------------------------
Class caabb3
{
Public:
Cvector3 min, Max;

Public:
// Query for Dimentions
Cvector3 size () const {return max-min ;}
Float x_size () {return Max. X-min. X ;}
Float y_size () {return Max. Y-min. Y ;}
Float z_size () {return Max. Z-min. Z ;}
Cvector3 Center () const {return (min + max) * 0.5f ;}

// Fetch one of the eight Corner Points
Cvector3 corner (int I) const;

// "Empty" the box, by setting the values to really large/small numbers.
Void empty ();

// Add a point to the box
Void add (const cvector3 & P );

// Add an AABB to the box
Void add (const caabb3 & Box );

// Return true if the box is empty
Bool is_empty () const;

// Return true if the box contains a point
Bool contains (const cvector3 & P) const;

// Transform the box and compute the new AABB
Void set_to_transformed_box (const caabb3 & box, const cmatrix4x3 & M );

// Return the clostet point on this box to another point
Cvector3 clostet_point_to (const cvector3 & P) const;
};

# Endif

 

Computing AABB

It is very easy to calculate the AABB of a vertex set. First, set the minimum and maximum values to "Positive and negative infinity" or any number that is larger or smaller than the actual number used. Next, traverse all vertices and expand the border box until it contains all vertices.

We introduced two helper functions in the caabb class. The first function is responsible for "Clearing" AABB:

//---------------------------------------------------------------------------
// "Empty" the box, by setting the values to really large/small numbers.
//---------------------------------------------------------------------------
Void caabb3: Empty ()
{
Const float big_number = 1e37f;

Min. x = min. Y = min. z = big_number;
Max. x = max. Y = max. z =-big_number;
}

The second function adds a single vertex to AABB and, if necessary, extends AABB to include each vertex:

//---------------------------------------------------------------------------
// Add a point to the box
//---------------------------------------------------------------------------
Void caabb3: add (const cvector3 & P)
{
// Expand the box as necessary to contain the point
If (p. x <min. X) min. x = p. x;
If (p. x> MAX. X) max. x = p. x;
If (P. Y <min. Y) min. Y = P. Y;
If (P. Y> MAX. Y) max. Y = P. Y;
If (P. z <min. Z) min. z = P. Z;
If (P. z> MAX. Z) max. z = P. Z;
}

Now, you can use the following code to create a rectangular border box from a point set:

Listing 12.1: computing the AABB for a set of points

// Our list of points
Const int N;
Vector3 list [N];

// First, empty the box
Aabb3 box;
Box. Empty ();

// Add each point into the box
For (INT I = 0; I <n; ++ I)
Box. Add (list [I]);

Obtain the vertex of AABB:

// Configure //--------------------------------------------------------------------------------------
// Return one of the 8 corner points. The points are numbered as follows:
//
// 6 7
//------------------------------
/// |/|
/// |/|
/// |/|
/// |/|
/// |/|
/// |/|
/// |/|
/// |/|
/// |/|
// 2/| 3/|
// ------------------------------/|
// |
// | + Y
// | 4 |
// | ----------------- | ---------- |
// |/5 |
// |/| + Z
// |/|
// |/
// |/
// |/
// |/
// |/
// |/
// |/--------------- + X
//------------------------------
// 0 1
//
// Bit 0 selects min. x vs. Max. x
// Bit 1 selects min. y vs. Max. Y
// Bit 2 selects min. Z vs. Max. Z
// Configure //--------------------------------------------------------------------------------------
Cvector3 caabb3: corner (int I) const
{
Assert (I> = 0 & I <= 7); // make sure index is in range

Return cvector3 (I & 1 )? Max. X: min. X,
(I & 2 )? Max. Y: min. y,
(I & 4 )? Max. Z: min. z );
}

For more information about other functions, see note:

//---------------------------------------------------------------------------
// Add an AABB to the box
//---------------------------------------------------------------------------
Void caabb3: add (const caabb3 & box)
{
// Expand the box as necessary
If (box. Min. x <min. X) min. x = Box. Min. X;
If (box. Min. x> MAX. X) max. x = Box. Min. X;
If (box. Min. Y <min. Y) min. Y = Box. Min. Y;
If (box. Min. Y> MAX. Y) max. Y = Box. Min. Y;
If (box. Min. z <min. Z) min. z = Box. Min. Z;
If (box. Min. z> MAX. Z) max. z = Box. Min. Z;
}

//---------------------------------------------------------------------------
// Return true if the box is empty
//---------------------------------------------------------------------------
Bool caabb3: is_empty () const
{
// Check if we're inverted on any axis
Return (min. x> MAX. x) | (min. Y> MAX. Y) | (min. z> MAX. z );
}

//---------------------------------------------------------------------------
// Return true if the box contains a point
//---------------------------------------------------------------------------
Bool caabb3: Contains (const cvector3 & P) const
{
// Check for overlap on each axis
Return (p. x> = min. X) & (p. x <= max. X )&&
(P. Y> = min. Y) & (P. Y <= max. Y )&&
(P. z> = min. Z) & (P. z <= max. z );
}

//---------------------------------------------------------------------------
// Return the closest point on this box to another point
//---------------------------------------------------------------------------
Cvector3 caabb3: clostet_point_to (const cvector3 & P) const
{
// "Push" P into the box, on each dimension.

Cvector3 R;

If (p. x <min. X)
R. x = min. X;
Else if (p. x> MAX. X)
R. x = max. X;
Else
R. x = p. x;

If (P. Y <min. Y)
R. Y = min. Y;
Else if (P. Y> MAX. Y)
R. Y = max. Y;
Else
R. Y = P. Y;

If (P. z <min. Z)
R. z = min. Z;
Else if (P. z> MAX. Z)
R. z = max. Z;
Else
R. z = P. Z;

Return R;
}

 

AABB and boundary ball

In many cases, AABB is more suitable for defining a ball than a boundary ball:

(1) The AABB for computing a point set is easier to implement in programming and can be completed in a short period of time. It is much more difficult to calculate the boundary ball.

(2) AABB provides a "more compact" boundary for many objects in the real world. Of course, for some objects, the boundary ball is better (imagine an object itself is a sphere ). In extreme cases, the size of AABB may be only 1/2 of the size of the boundary ball. In most cases, the size of the boundary ball is much larger than that of the rectangle frame, compare the boundary ball and AABB of the pole. Figure 12.11 shows the comparison between the AABB and the boundary ball of different objects.

The fundamental problem with the boundary ball is that its shape has only one degree of freedom-radius, while AABB has three degrees of freedom-length, width, and height. Therefore, it can adjust these degrees of freedom to adapt to different objects. For most objects in Figure 12.11, except for the star in the upper-right corner, AABB is smaller than the boundary ball. For this star, the boundary ball is only a little smaller than that of AABB. As shown in figure 12.11, we can see that AABB is very sensitive to the direction of the object. Compare the AABB of the following two guns. In the figure, the gun sizes are the same but in different directions. Note that the boundary ball sizes are the same in this case, because the boundary ball is not sensitive to the object direction.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.