svghmi/geometry.ysl2
branchsvghmi
changeset 2873 022db76c3bff
parent 2872 83adf8859c55
child 2875 6a12e1084deb
equal deleted inserted replaced
2872:83adf8859c55 2873:022db76c3bff
       
     1 // geometry.ysl2
       
     2 //
       
     3 // Geometry (bounding box intersection) definitions
       
     4 
       
     5 // This retrieves geometry obtained through "inkscape -S"
       
     6 // already parsed by python and presented as a list of
       
     7 // <bbox x="0" y="0" w="42" h="42">
       
     8 const "geometry", "ns:GetSVGGeometry()";
       
     9 
       
    10 // Debug data
       
    11 function "debug_geometry" {
       
    12     | GEOMETRY : ID, x, y, w, h
       
    13     foreach "$geometry[@Id = $hmi_elements/@id]"
       
    14         |  «@Id» «@x» «@y» «@w» «@h»
       
    15 }
       
    16 !debug_output_calls.append("debug_geometry")
       
    17 
       
    18 // Rates 1D intersection of 2 segments A and B
       
    19 // described respectively with a0,a1 and b0,b1
       
    20 def "func:intersect_1d" {
       
    21     // it is assumed that a1 > a0 and b1 > b0
       
    22     param "a0";
       
    23     param "a1";
       
    24     param "b0";
       
    25     param "b1";
       
    26 
       
    27     const "d0", "$a0 >= $b0";
       
    28     const "d1", "$a1 >= $b1";
       
    29     choose {
       
    30         when "not($d0) and $d1"
       
    31             // b contained in a
       
    32             //   a0<b0 b1<a1
       
    33             // a +--------+
       
    34             // b    +--+
       
    35             result "3";
       
    36         when "$d0 and not($d1)"
       
    37             // a contained in b
       
    38             //   b0<a0 a1<b1
       
    39             // a    +--+
       
    40             // b +--------+
       
    41             result "2";
       
    42         when "$d0 and $d1 and $a0 < $b1"
       
    43             // a and b are overlapped 
       
    44             //   b0<a0<b1<a1
       
    45             // a    +-----+
       
    46             // b +-----+
       
    47             result "1";
       
    48         when "not($d0) and not($d1) and $b0 < $a1"
       
    49             // a and b are overlapped
       
    50             //   a0<b0<a1<b1
       
    51             // a +-----+
       
    52             // b    +-----+
       
    53             result "1";
       
    54             // since orientation doesn't matter,
       
    55             // rated same as previous symetrical overlapping
       
    56         otherwise
       
    57             result "0"; /* no intersection*/
       
    58     }
       
    59 }
       
    60 
       
    61 
       
    62 // Rates intersection A and B areas described with x,y,w and h 
       
    63 // attributes passed as $a and $b parameters.
       
    64 // 
       
    65 // returns :
       
    66 // 0 - no intersection
       
    67 //            .-----.
       
    68 //    .-----. |    b|
       
    69 //    |     | |     |
       
    70 //    |     | '-----'
       
    71 //    |a    |
       
    72 //    '-----'
       
    73 //
       
    74 // 1 - overlapping
       
    75 //        .-----.
       
    76 //    .---|--. b|
       
    77 //    |   |  |  |
       
    78 //    |   '-----'
       
    79 //    |a     |
       
    80 //    '------'
       
    81 //
       
    82 // 2 - overlapping
       
    83 //        .-----.
       
    84 //        |  a  |
       
    85 //    .---|-----|---.
       
    86 //    |   '-----'   |
       
    87 //    | b           |
       
    88 //    '-------------'
       
    89 //
       
    90 // 3 - overlapping
       
    91 //        .-----.
       
    92 //        |  b  |
       
    93 //    .---|-----|---.
       
    94 //    |   '-----'   |
       
    95 //    | a           |
       
    96 //    '-------------'
       
    97 //
       
    98 // 4 - a contained in b
       
    99 //    .-------------.
       
   100 //    |   .-----.   |
       
   101 //    |   |  a  |   |
       
   102 //    |b  '-----'   |
       
   103 //    '-------------'
       
   104 //
       
   105 // 6 - overlapping
       
   106 //        .----.
       
   107 //        |   b|
       
   108 //    .---|----|---.
       
   109 //    |a  |    |   |
       
   110 //    '---|----|---'
       
   111 //        '----'
       
   112 //
       
   113 // 9 - b contained in a
       
   114 //    .-------------.
       
   115 //    |   .-----.   |
       
   116 //    |   |  b  |   |
       
   117 //    |a  '-----'   |
       
   118 //    '-------------'
       
   119 //
       
   120 def "func:intersect" {
       
   121     param "a";
       
   122     param "b";
       
   123 
       
   124     const "x_intersect", "func:intersect_1d($a/@x, $a/@x+$a/@w, $b/@x, $b/@x+$b/@w)";
       
   125 
       
   126     choose{
       
   127         when "$x_intersect != 0"{
       
   128             const "y_intersect", "func:intersect_1d($a/@y, $a/@y+$a/@h, $b/@y, $b/@y+$b/@h)";
       
   129             result "$x_intersect * $y_intersect";
       
   130         }
       
   131         otherwise result "0";
       
   132     }
       
   133 }
       
   134 
       
   135 // return overlapping geometry for a given element
       
   136 // all intersercting element are returned
       
   137 // except groups, that must be contained to be counted in
       
   138 def "func:overlapping_geometry" {
       
   139     param "elt";
       
   140     const "groups", "/svg:svg | //svg:g";
       
   141     const "g", "$geometry[@Id = $elt/@id]"; 
       
   142     const "candidates", "$geometry[@Id != $elt/@id]";
       
   143     result """$candidates[(@Id = $groups/@id and (func:intersect($g, .) = 9)) or 
       
   144                           (not(@Id = $groups/@id) and (func:intersect($g, .) > 0 ))]""";
       
   145 }