geometrybinary-treebspspace-partitioningbsp-tree

Geometry of binary space partitioning tree


I am implementing a BSP tree for 2D space. There was a problem with determining the position of the line relative to the splitting vector. (Front/Back). In my case, a coordinate plane with positive values ​​is used, why the scalar product is unlikely to help. Could you help with solving the problem?

There was an idea to solve this using the equation of a straight line, but the method turned out to be too voluminous and hardly the most effective. Many sites mention just the scalar product, but, unfortunately, there is no explanation of front-back anywhere. As for the data structure itself, there is an understanding.


Solution

  • With a 2D BSP, the objects you organize will usually be something like line segments or polygons. Each of these objects will be represented by a collection of points. To check whether an object is on the front or back side (or both) with respect to a dividing line (or plane/hyperplane in higher dimensions) you iterate over the points and classify them with respect to the line. If all the points are all on the front side of the line, the object is in front of the line, and likewise for the back side. If some of the points are in front and others in back, the object straddles the line.

    If you express your dividing lines using the standard Ax + By + C = 0, then you simply take each point p = (x, y) from your object, compute Ax + By + C, and if that's positive the point is in "front" of the line, and if it's negative the point is in "back". If it's zero, the point is on the line, which for the sake of the BSP you can just treat as being in front.

    The scalar product / dot product can help here if you want to store A and B in a vector, which then becomes the normal vector N of the line. You can then compute dot(N, p) and compare it to -C to classify each point.

    For the simpler case where you are using axis-aligned lines to build your BSP tree, your dividing lines will also be simpler -- x or y will just be a constant. That said, I'd recommend you still use the dot product, allowing either A or B to be zero as appropriate. Your code will be a lot simpler, as you won't have to have different cases for lines parallel to different axes.