author | Radek Brich <radek.brich@devl.cz> |
Mon, 17 Dec 2007 22:03:50 +0100 | |
branch | pyrit |
changeset 40 | 929aad02c5f2 |
parent 7 | bf17f9f84c91 |
child 44 | 3763b26244f0 |
permissions | -rw-r--r-- |
7
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
KdTree Algorithm |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
---------------- |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
def build_kdtree(root, shapes): |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
root = new KdNode |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
root.shapes = shapes |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
subdivide(root, bounding_box(shapes), 0) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
def subdivide(node, bbox, depth): |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
# choose split axis |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
axis = bbox.largest_extend() |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
# find split position |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
splitpos = find_split_position(axis) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
leftnode, rightnode = new KdNode[2] |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
for each shape: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
if (node->intersectleftnode()) leftnode->addprimitive( primitive ) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
if (node->intersectrightnode()) rightnode->addprimitive( primitive ) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
subdivide(leftnode, bbox.left_split(splitpos), depth+1) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
subdivide(rightnode, bbox.right_split(splitpos), depth+1) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
def find_split_position(axis): |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
mshapes = new ShapeListWithMarks(shapes) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
# sort new shape list according to left boundaries |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
mshapes.sort(axis) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
splitlist = new SplitList() |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
for each mshape from mshapes: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
splitlist.add_extremes(mshape, axis) |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
splitlist.sort() |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
for each split from splitlist: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
for each mshape from mshapes: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
if mshape.marked: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
continue |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
# if shape is on left side of split plane |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
if mshape.r_boundary <= split.pos: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
split.lnum++ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
mshape.mark() |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
# if shape is completely contained in split plane |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
if mshape.l_boundary == mshape.r_boundary == split.pos: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
if split.num_smaller_subcell == 0: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
split.num_bigger_subcell++ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
else: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
split.num_smaller_subcell++ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
# if shape is on right side of split plane |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
if mshape.l_boundary >= split.pos: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
split.r_num += mshapes.count - mshape.number |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
break |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
|
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
# if shape occupies both sides of split plane |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
if mshape.l_boundary < split.pos and mshape.r_boundary > split.pos: |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
split.l_num++ |
bf17f9f84c91
kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
split.r_num++ |