Binary split space (BSP)

Source: Internet
Author: User

Binary split space (BSP)
1. What is a BSP tree?
The BSP Tree is a binary tree used to sort and search for elements in an n-dimensional space. The BSP tree represents the whole space, and any point in the BSP tree represents a convex sub-space. Each contact contains a "hyperplane", which splits the space represented by this contact into two subspaces. In addition to the reference of two child contacts, each contact can also save one or more elements.

 

For n-dimensional spaces, the hyperplane is an object of the N-1 dimension. Generally, the BSP tree is used to represent two-dimensional or three-dimensional space. At this time, elements in the space refer to the line segment and the multi-edge type respectively.

Thanks to its powerful sorting and classification functions, BSP trees are widely used. The BSP tree can be seen from hidden area removal, light tracing, solid modeling, and robot action planning.

2. Example
To understand the BSP tree, you can start with a simple example of two-dimensional space. To simplify the problem, we assume that the line segments in the space are all parallel to the X or Y axis, and each time we divide the space into two equal parts. For example, if a square in the XY plane is split for the first time, it is divided into two equal parts in the X direction. The subsequent split is based on X-> Y-> X... . Unless human intervention is performed, this process is performed recursively. Describes the next process and the corresponding BSP tree.

How to Create a BSP Tree
Introduction
Given a group of polygon in a 3D space, the BSP tree we want to create should contain each of them. The algorithm for creating a BSP tree is very simple:
1. Select the split plane
How to Create a BSP Tree
Introduction
Given a group of polygon in a 3D space, the BSP tree we want to create should contain each of them. The algorithm for creating a BSP tree is very simple:
1. Select the split plane
The selection of split planes depends on how the BSP tree will be used and the ordering conditions of polygon in the space. In some cases, use the plane of a polygon in the space to split (called automatic split ). In other cases, the plane perpendicular to the coordinate axis is often used as a split plane.
In any case, it is very important to evaluate the strategy for selecting the split plane. A common idea is to make the structure of the BSP tree as close as possible to a balanced binary tree, but this is costly. To implement a fully balanced BSP tree, a polygon must be divided into two parts when it is at the intersection of the split plane. A bad split plane selection policy produces many such splits.
2. Split a polygon set
The polygon set needs to be divided into two subsets according to the split plane selected in step 1. If a polygon is completely on one side of the split plane, you only need to add it to the corresponding subset. If it is at the intersection of the split plane, it is first divided into two parts and then added to the two subsets respectively.
3. When to stop splitting
It is also related to the specific application to determine the conditions under which the recursive process of creating a BSP tree is stopped. It can be stopped when the number of polygon contained in the contact point is smaller than a certain boundary, or it can be stopped when the depth of the BSP Tree exceeds a certain boundary.

 

Pseudocode
Struct bsp_tree
{
Plane partition;
List polygons;
Bsp_tree * front, * back;
};

This struct definition will be used all the time in the following discussions. It indicates a contact of the BSP Tree, including a split plane, a list of polygon in the split plane, and a pointer to the child contact.

Void buld_bsp_tree (bsp_tree * tree, list polygons)
{
Polygon * root = polygons. get_from_list ();
Tree-> partition = root-> get_plane ();
Tree-> polygons. add_to_list (Root );
List front_list, back_list;
Polygon * poly;

While (poly = polygons. get_from_list ())! = 0)
{
Int result = tree-> partition. classify_polygon (poly );

Switch (result)
{
Case coincident: // total surface
Tree-> polygons. add_to_list (poly );
Break;
Case in_back_of:
Back_list.add_to_list (poly );
Break;
Case in_front_of
Front_list.add_to_list (poly );
Break;
Case spanning:
Polygon * front_piece, * back_piece;
Split_polygon (poly, tree-> partition, front_piece, back_piece );
Back_list.add_to_list (back_piece );
Front_list.add_to_list (front_piece );
Break;
}
}

If (! Front_list.is-> empty_list ())
{
Tree-> front = new bsp_tree;
Build_bsp_tree (tree-> front, front_list );
}
If (! Back_list-> is_empty_list ())
{
Tree-> back = new bsp_tree;
Build_bsp_tree (tree-> back, back_list );
}
}

The buld_bsp_tree function recursively creates a BSP tree based on the preceding steps. It uses the plane of the first polygon in the input polygon list as the split plane. It is assumed that every polygon in this list is a convex polygon.
Obviously, this function can be improved through a more reasonable choice Strategy for split planes, which will be described in detail later.
How to use a plane to classify a polygon

 

Use a plane to classify a polygon, that is, to determine which side of the polygon is located on the plane. This is usually called a front/back test. It is done by testing the relationship between each vertex of a polygon and the position of the plane. The basic algorithm is to retrieve each edge of a polygon and find the two vertices that are located on both sides of the polygon. The intersection points are the new vertices of the two polygon separated.

Implementation:
To classify vertices in a plane, you only need to place the XYZ coordinate value of the vertex into the plane equation. Ax + by + cz + D = 0, and the absolute value of the result is the distance from the vertex to the plane, the result symbol is related to the direction of the Plane Normal. If the vertex is located in the half plane indicated by the plane normal, the result is positive. Otherwise, the result is negative.

The following is the c ++ pseudocode for dividing a convex polygon.
# Define Epsilon numeric_limits <float >:: Epsilon ()

Void splitpolygon (polygon * poly, plane * part, polygon ** front, polygon ** back)
{
Size_t vernum = poly-> numvertices ();

Point PTA, PTB;
Float dista, distb;
STD: vector <point> verfront, verback;

PTA = poly-> vertex [verNum-1];
Dista = poly-> classifypoint (PTA); // calculates the signed distance.

For (size_t I = Epsilon; ++ I <vernum ;)
{
PTB = poly-> vertex [I];
Distb = poly-> classifypoint (PTB );

If (distb> Epsilon & dista <-Epsilon)
{
Vector v = PTA-PTB;
Normalize (v );
Float sect =-part-> classifypoint (PTA)/DOT (poly-> normal (), V );
Verfront. push_back (PTA + (V * sect ));
Verback. push_back (PTA + (V * sect ));
Verfront. push_back (PTB );
}
Else if (distb <-Epsilon & dista> epsilon)
{
Vector v = PTB-PTA;
Normalize (v );
Float sect =-part-> classifypoint (PTB)/DOT (poly-> normal (), V );
Verfront. push_back (PTB + (V * sect ));
Verback. push_back (PTB + (V * sect ));
Verback. push_back (PTB );
}
Else if (distb> Epsilon & dista> epsilon)
{
Verfront. push_back (PTB );
}
Else if (distb <-Epsilon & dista <-Epsilon)
{
Verback. push_back (PTB );
}
Else if (distb>-Epsilon & distb <epsilon)
{
Verfront. push_back (PTB );
Verback. push_back (PTB );
}

PTA = PTB;
Dista = distb;
}

If (! Verfront. Empty ())
* Front = new polygon (verfront );
If (! Verback. Empty ())
* Back = new polygon (verback );
}

 

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.