13.07.2015 Views

Improvements on the kd-tree

Improvements on the kd-tree

Improvements on the kd-tree

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

László Szécsi and Balázs Benedek / <str<strong>on</strong>g>Improvements</str<strong>on</strong>g> <strong>on</strong> <strong>the</strong> <strong>kd</strong>-<strong>tree</strong>hits for every primitive is unacceptably slow, in c<strong>on</strong>trastto <strong>the</strong> results achieved by space subdivisi<strong>on</strong>. In <strong>the</strong> lattercase, we <strong>on</strong>ly need to traverse cells al<strong>on</strong>g <strong>the</strong> ray and <strong>on</strong>lycompute intersecti<strong>on</strong>s for promising candidates. Best resultsam<strong>on</strong>g <strong>the</strong> spatial subdivisi<strong>on</strong> schemes are delivered by <strong>the</strong>BSP and <strong>kd</strong>-<strong>tree</strong>s. The <strong>kd</strong>-<strong>tree</strong> we use in this article is a binary,n<strong>on</strong>-balanced, spatial subdivisi<strong>on</strong> data structure, withaxis-aligned cutting planes associated to its n<strong>on</strong>-leaf nodes,and subsets of scene objects stored in <strong>the</strong> leaf nodes.The power of <strong>the</strong> structure lies in its flexibility. Cuttingplanes can be positi<strong>on</strong>ed depending <strong>on</strong> <strong>the</strong> locati<strong>on</strong> of <strong>the</strong>scene objects, so at <strong>the</strong> cost of some calculati<strong>on</strong> <strong>the</strong> soluti<strong>on</strong>resulting in an optimal traversal time can be chosen. Thecutting planes being axis aligned is a minor limitati<strong>on</strong>, asarbitrarily positi<strong>on</strong>ed planes may produce a better <strong>tree</strong>, butfinding <strong>the</strong> optimum would be less effective. Fur<strong>the</strong>rmore,storing <strong>the</strong> data describing <strong>the</strong> cutting planes requires lessmemory space, and it is far easier to compute <strong>the</strong> ray-planeintersecti<strong>on</strong>.2.2. Traversal al<strong>on</strong>g a rayDuring <strong>the</strong> image syn<strong>the</strong>sis a large number of ray-scene intersecti<strong>on</strong>shave to be computed. Compared to <strong>the</strong> <strong>on</strong>e-timec<strong>on</strong>structi<strong>on</strong> of <strong>the</strong> <strong>tree</strong> this means such a difference of scale,that it is worth taking every cost just to speed up traversal inmost of <strong>the</strong> cases.The sequential ray traversal algorithm is based <strong>on</strong> <strong>the</strong> spatialproximity search using <strong>the</strong> <strong>kd</strong>-<strong>tree</strong>. First we take <strong>the</strong> originof <strong>the</strong> ray, and locate <strong>the</strong> cell c<strong>on</strong>taining it by walkingdown <strong>the</strong> <strong>tree</strong> from its root. Within <strong>the</strong> cell found, we carryout all intersecti<strong>on</strong> tests with <strong>the</strong> objects bel<strong>on</strong>ging to <strong>the</strong>cell. If no intersecti<strong>on</strong> within <strong>the</strong> cell was found, we proceedto <strong>the</strong> next cell. In order to find it, we use <strong>the</strong> same methodas before. We calculate <strong>the</strong> point where <strong>the</strong> ray leaves <strong>the</strong>cell, which is exactly where it enters <strong>the</strong> next. We translateit a tiny bit fur<strong>the</strong>r al<strong>on</strong>g <strong>the</strong> ray to resolve ambiguity, andrepeat <strong>the</strong> whole process using <strong>the</strong> spatial proximity searchwith this next point. We have to remark that <strong>the</strong> algorithmmay skip cells of extremely little or zero width. Although<strong>the</strong>se may seem useless at <strong>the</strong> first sight, <strong>the</strong>y can actuallyrightfully appear in <strong>kd</strong>-<strong>tree</strong>s for scenes where <strong>the</strong>re are numerousaxis-aligned polyg<strong>on</strong>s. This may be <strong>the</strong> case with geometricalscenes, typically boxes and rooms. Ano<strong>the</strong>r drawbackof this algorithm is that it starts from <strong>the</strong> root of <strong>the</strong><strong>tree</strong> for every new cell though it is very probable that twocells following each o<strong>the</strong>r are near each o<strong>the</strong>r in <strong>the</strong> structure.Therefore <strong>on</strong>e node could be visited many times.The recursive ray traversal algorithm eliminates <strong>the</strong> maindrawbacks of <strong>the</strong> sequential ray traversal algorithm and visitsevery node and leaf just <strong>on</strong>ly <strong>on</strong>ce 2 . We check if <strong>the</strong> rayintersects <strong>the</strong> volumes corresp<strong>on</strong>ding to <strong>the</strong> left and rightsub-<strong>tree</strong>s. The sub-<strong>tree</strong>s are traversed in <strong>the</strong> very same way,if necessary, starting with <strong>the</strong> <strong>on</strong>e nearer to <strong>the</strong> origin. Toterminate <strong>the</strong> recursi<strong>on</strong> <strong>the</strong> leaves of <strong>the</strong> <strong>tree</strong> are handled in<strong>the</strong> same manner as above. The implementati<strong>on</strong> of <strong>the</strong> algorithmneeds a traversal stack to store data about <strong>the</strong> sub-<strong>tree</strong>sneeded to be processed later.Whichever algorithm we use, we will walk through <strong>the</strong>leaf cells al<strong>on</strong>g <strong>the</strong> ray, and test possible intersecti<strong>on</strong>s for<strong>the</strong> segment inside <strong>the</strong> cell. If intersecti<strong>on</strong>s were found, <strong>the</strong>closest is taken, else <strong>the</strong> ray has to be followed <strong>on</strong>. C<strong>on</strong>sequently,<strong>the</strong> objective is to have minimal number of objectsin a cell, and if a ray intersects a cell, it should, with highprobability, also intersect an object within. This, pushed toits extremes, it accomplished when all objects are delimitedby six fitting cutting planes. However, if <strong>the</strong> bounding boxesof <strong>the</strong> objects overlap, like in most scenes, <strong>the</strong>n such cutsmay intersects several objects, adding <strong>the</strong>m to both child volumes,resulting in superfluously large list in <strong>the</strong> leaves, andworse-than-optimal traversal time.2.3. C<strong>on</strong>structing a <strong>kd</strong>-<strong>tree</strong> and possibledecisi<strong>on</strong>-making heuristicsThe <strong>tree</strong> can be built in a recursive way. Processing a volumeinvolves <strong>the</strong> choice and storage of <strong>the</strong> cutting plane, and<strong>the</strong> processing of <strong>the</strong> two new sub-volumes. The decisi<strong>on</strong> tomake is where to place <strong>the</strong> cutting plane, and if it is worthsubdividing <strong>the</strong> volume at all. This may be based <strong>on</strong> someheuristic scheme, or an estimati<strong>on</strong> of <strong>the</strong> resulting traversalcost.The first, most obvious method is to cut <strong>the</strong> volume intotwo equal halves, using <strong>the</strong> spatial median, similarly to <strong>the</strong>oc<strong>tree</strong> approach where we care little about <strong>the</strong> positi<strong>on</strong> of<strong>the</strong> objects when subdividing a volume. The resulting <strong>tree</strong>will of course not be balanced, and it is easy to c<strong>on</strong>struct ascene where this method comes near to useless. Similarly to<strong>the</strong> oc<strong>tree</strong>, spatial median subdivisi<strong>on</strong> performs well in caseof evenly distributed objects.Ano<strong>the</strong>r simple and more promising approach is to makeboth sub-volumes c<strong>on</strong>tain <strong>the</strong> same number of objects. Thepositi<strong>on</strong> with this property is called <strong>the</strong> object median. Tofind it, we have to do a ’select and partiti<strong>on</strong>’ median search.This can be c<strong>on</strong>sidered a modified versi<strong>on</strong> of <strong>the</strong> ’quick sort’algorithm that <strong>on</strong>ly sorts <strong>the</strong> partiti<strong>on</strong> c<strong>on</strong>taining <strong>the</strong> halvingelement of <strong>the</strong> array. This simpler procedure will also separate<strong>the</strong> array into elements smaller and greater than <strong>the</strong>median, and outperforms ’quick sort’. As <strong>the</strong> resulting <strong>tree</strong>would be balanced, its representati<strong>on</strong> could be simple andcompact. Fur<strong>the</strong>rmore, a balanced <strong>kd</strong>-<strong>tree</strong> can be c<strong>on</strong>sideredto be optimal for several tasks, such as proximity search.However, in ray casting, we do not <strong>on</strong>ly need to find an object,but to follow a ray through several cells intersected.Therefore, <strong>the</strong> probability of a sub-volume being hit by aray plays an important role in <strong>the</strong> expected time cost of <strong>the</strong>rendering algorithm. The object median method disregardsthat aspect. The unfortunate c<strong>on</strong>sequence for <strong>the</strong> optimal <strong>tree</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!