|
1 // bbox_intersect.ysl2 |
|
2 // |
|
3 // bounding boxes intersection tests |
|
4 |
|
5 // Rates 1D intersection of 2 segments A and B |
|
6 // described respectively with a0,a1 and b0,b1 |
|
7 def "func:intersect_1d" { |
|
8 // it is assumed that a1 > a0 and b1 > b0 |
|
9 param "a0"; |
|
10 param "a1"; |
|
11 param "b0"; |
|
12 param "b1"; |
|
13 |
|
14 const "d0", "$a0 >= $b0"; |
|
15 const "d1", "$a1 >= $b1"; |
|
16 choose { |
|
17 when "not($d0) and $d1" |
|
18 // b contained in a |
|
19 // a0<b0 b1<a1 |
|
20 // a +--------+ |
|
21 // b +--+ |
|
22 result "3"; |
|
23 when "$d0 and not($d1)" |
|
24 // a contained in b |
|
25 // b0<a0 a1<b1 |
|
26 // a +--+ |
|
27 // b +--------+ |
|
28 result "2"; |
|
29 when "$d0 and $d1 and $a0 < $b1" |
|
30 // a and b are overlapped |
|
31 // b0<a0<b1<a1 |
|
32 // a +-----+ |
|
33 // b +-----+ |
|
34 result "1"; |
|
35 when "not($d0) and not($d1) and $b0 < $a1" |
|
36 // a and b are overlapped |
|
37 // a0<b0<a1<b1 |
|
38 // a +-----+ |
|
39 // b +-----+ |
|
40 result "1"; |
|
41 // since orientation doesn't matter, |
|
42 // rated same as previous symetrical overlapping |
|
43 otherwise |
|
44 result "0"; /* no intersection*/ |
|
45 } |
|
46 } |
|
47 |
|
48 |
|
49 // Rates intersection A and B areas described with x,y,w and h |
|
50 // attributes passed as $a and $b parameters. |
|
51 // |
|
52 // returns : |
|
53 // 0 - no intersection |
|
54 // .-----. |
|
55 // .-----. | b| |
|
56 // | | | | |
|
57 // | | '-----' |
|
58 // |a | |
|
59 // '-----' |
|
60 // |
|
61 // 1 - overlapping |
|
62 // .-----. |
|
63 // .---|--. b| |
|
64 // | | | | |
|
65 // | '-----' |
|
66 // |a | |
|
67 // '------' |
|
68 // |
|
69 // 2 - overlapping |
|
70 // .-----. |
|
71 // | a | |
|
72 // .---|-----|---. |
|
73 // | '-----' | |
|
74 // | b | |
|
75 // '-------------' |
|
76 // |
|
77 // 3 - overlapping |
|
78 // .-----. |
|
79 // | b | |
|
80 // .---|-----|---. |
|
81 // | '-----' | |
|
82 // | a | |
|
83 // '-------------' |
|
84 // |
|
85 // 4 - a contained in b |
|
86 // .-------------. |
|
87 // | .-----. | |
|
88 // | | a | | |
|
89 // |b '-----' | |
|
90 // '-------------' |
|
91 // |
|
92 // 6 - overlapping |
|
93 // .----. |
|
94 // | b| |
|
95 // .---|----|---. |
|
96 // |a | | | |
|
97 // '---|----|---' |
|
98 // '----' |
|
99 // |
|
100 // 9 - b contained in a |
|
101 // .-------------. |
|
102 // | .-----. | |
|
103 // | | b | | |
|
104 // |a '-----' | |
|
105 // '-------------' |
|
106 // |
|
107 def "func:intersect" { |
|
108 param "a"; |
|
109 param "b"; |
|
110 |
|
111 const "x_intersect", "func:intersect_1d($a/@x, $a/@x+$a/@w, $b/@x, $b/@x+$b/@w)"; |
|
112 |
|
113 choose{ |
|
114 when "$x_intersect != 0"{ |
|
115 const "y_intersect", "func:intersect_1d($a/@y, $a/@y+$a/@h, $b/@y, $b/@y+$b/@h)"; |
|
116 result "$x_intersect * $y_intersect"; |
|
117 } |
|
118 otherwise result "0"; |
|
119 } |
|
120 } |