geometry.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @author Arnaud Ligot (CBlue SPRL) <arnaud@cblue.be>
  5. * @package chamilo.include.geometry
  6. */
  7. /**
  8. * Code
  9. */
  10. DEFINE('DEBUG', false);
  11. /**
  12. * poly_init - build the array which will store the image of the polygone
  13. * @param max[x] X resolution
  14. * @param max[y] Y resolution
  15. * @returns an array such as: for all i in [0..max[x][ : for all j in [0..max[y][ : array[i][j] = FALSE
  16. */
  17. function poly_init($max) {
  18. return array_fill(0, $max["x"]-1,
  19. array_fill(0, $max["y"]-1, FALSE));
  20. }
  21. /**
  22. * poly_compile - return an array which holds the image of the polygone
  23. * FALSE = blank pixel
  24. * TRUE = black pixel
  25. *
  26. * @param poly points from the polygone
  27. * example:
  28. * poly[0]['x'] = ...
  29. * poly[0]['y'] = ...
  30. * poly[1]['x'] = ...
  31. * poly[1]['y'] = ...
  32. * ...
  33. * poly[n]['x'] = <empty>
  34. * poly[n]['y'] = <empty>
  35. * poly[n+1]['x'] = <empty>
  36. * poly[n+1]['y'] = <empty>
  37. * ...
  38. * @param max see poly_init
  39. * @param boolean print or not a debug
  40. *
  41. * @returns an array such as: for all i in [0..max[x][ : for all j in [0..max[y][ : array[i][j] = in_poly(poly, i,j)
  42. * in_poly(poly,i,j) = true iff (i,j) is inside the polygon defined by poly
  43. */
  44. function poly_compile($poly, $max, $test = false) {
  45. $res = poly_init($max);
  46. // looking for EDGES
  47. // may be optimized by a dynamic choice
  48. // between Y and X based on max[y]<max[x]
  49. /*
  50. * bords cointains the edges of the polygone
  51. * it is an array of array,
  52. * there are an array for each collon of the image
  53. *
  54. * for all j in [O..max[y][ : for all i in bords[$j] :
  55. * (i,j) is a point inside an edge of the polygone
  56. */
  57. $bord_lenght = $max['x'];
  58. if ($max['y'] > $bord_lenght) {
  59. $bord_lenght = $max['y'];
  60. }
  61. //$bords = array_fill(0, $bord_lenght-1, array()); // building this array
  62. $bords = array_fill(0, $bord_lenght, array()); // building this array
  63. /* adding the first point of the polygone */
  64. if (is_array($bords[$poly[0]['y']])) //avoid warning
  65. array_push($bords[$poly[0]['y']], $poly[0]['x']);
  66. $i = 1; // we re-use $i and $old_pente bellow the loop
  67. $old_pente=0;
  68. for ( ; // for each points of the polygon but the first
  69. $i<sizeof($poly) && (!empty($poly[$i]['x']) && !empty($poly[$i]['y'])); $i++) {
  70. /* special cases */
  71. if ($poly[$i-1]['y'] == $poly[$i]['y']) {
  72. if ($poly[$i-1]['x'] == $poly[$i]['x'])
  73. continue; // twice the same point
  74. else { // infinite elevation of the edge
  75. if (is_array($bords[$poly[$i]['y']]))
  76. array_push($bords[$poly[$i]['y']],$poly[$i]['x']);
  77. $old_pente=0;
  78. continue;
  79. }
  80. }
  81. //echo 'point:'.$poly[$i]['y']; bug here
  82. // adding the point as a part of an edge
  83. if (is_array($bords[$poly[$i]['y']])) //avoid warning
  84. array_push($bords[$poly[$i]['y']], $poly[$i]['x']);
  85. if (DEBUG) echo '('.$poly[$i]['x'].';'.$poly[$i]['y'].') ';
  86. /* computing the elevation of the edge going */
  87. // from $poly[$i-1] to $poly[$i]
  88. $pente = ($poly[$i-1]['x']-$poly[$i]['x'])/
  89. ($poly[$i-1]['y']-$poly[$i]['y']);
  90. // if the sign of the elevation change from the one of the
  91. // previous edge, the point must be added a second time inside
  92. // $bords
  93. if ($i>1)
  94. if (($old_pente<0 && $pente>0)
  95. || ($old_pente>0 && $pente<0)) {
  96. if (is_array($bords[$poly[$i]['y']])) //avoid warning
  97. array_push($bords[$poly[$i]['y']],$poly[$i]['x']);
  98. if (DEBUG)
  99. echo '*('.$poly[$i]['x'].
  100. ';'.$poly[$i]['y'].') ';
  101. }
  102. /* detect the direction of the elevation in Y */
  103. $dy_inc = ($poly[$i]['y']-$poly[$i-1]['y']) > 0 ? 1 : -1;
  104. $x = $poly[$i-1]['x'];
  105. // if (DEBUG) echo "init: ".$poly[$i-1]['y']." dy_inc: ".$dy_inc.
  106. // " end: ".$poly[$i]['y']." pente:".$pente;
  107. /* computing points between $poly[$i-1]['y'] and $poly[$i-1]['y'] */
  108. // we iterate w/ $dy in ]$poly[$i-1]['y'],$poly[$i-1]['y'][
  109. // w/ $dy_inc as increment
  110. for ($dy = $poly[$i-1]['y']+$dy_inc;
  111. $dy != $poly[$i]['y'];
  112. $dy += $dy_inc) {
  113. $x += $pente*$dy_inc;
  114. array_push($bords[$dy], $x);
  115. // if (DEBUG) echo '/('.$x.';'.$dy.') ';
  116. }
  117. $old_pente = $pente;
  118. }
  119. // closing the polygone (the edge between $poly[$i-1] and $poly[0])
  120. if ($poly[$i-1]['y']!=$poly[0]['y']) {// droite--> rien à faire
  121. // elevation between $poly[0]['x'] and $poly[1]['x'])
  122. $rest = $poly[0]['y']-$poly[1]['y'];
  123. if ($rest!=0)
  124. $pente1 = ($poly[0]['x']-$poly[1]['x'])/($rest);
  125. else
  126. $pente1 = 0;
  127. // elevation between $poly[$i-1]['x'] and $poly[0]['x'])
  128. $pente = ($poly[$i-1]['x']-$poly[0]['x'])/
  129. ($poly[$i-1]['y']-$poly[0]['y']);
  130. // if (DEBUG) echo 'start('.$poly[$i-1]['x'].','.$poly[$i-1]['y'].
  131. // ')-end('.$poly[0]['x'].','.$poly[0]['y'].
  132. // ')-pente'.$pente;
  133. // doubling the first point if needed (see above)
  134. if (($pente1<0 && $pente>0) || ($pente1>0 && $pente<0)) {
  135. if (is_array($bords[$poly[$i]['y']]))
  136. array_push($bords[$poly[$i]['y']], round($poly[$i]['x']));
  137. //if (DEBUG) echo '('.$poly[$i-1]['x'].';'.$poly[$i-1]['y'].') ';
  138. }
  139. // doubling the last point if neededd
  140. if (($old_pente<0 && $pente>0) || ($old_pente>0 && $pente<0)) {
  141. if (is_array($bords[$poly[$i-1]['y']])) //avoid warning
  142. array_push($bords[$poly[$i-1]['y']], round($poly[$i-1]['x']));
  143. //if (DEBUG) echo '*('.$poly[$i-1]['x'].';'.$poly[$i-1]['y'].') ';
  144. }
  145. $dy_inc = ($poly[0]['y']-$poly[$i-1]['y']) > 0 ? 1 : -1;
  146. $x = $poly[$i-1]['x'];
  147. // if (DEBUG) echo "init: ".$poly[$i-1]['y']." dy_inc: ".$dy_inc.
  148. // " end: ".$poly[0]['y'];
  149. for ($dy = $poly[$i-1]['y']+$dy_inc;
  150. $dy != $poly[0]['y'];
  151. $dy += $dy_inc)
  152. {
  153. $x += $pente*$dy_inc;
  154. array_push($bords[$dy], round($x));
  155. // if (DEBUG) echo '/('.$x.';'.$dy.') ';
  156. }
  157. }
  158. /* filling the polygon */
  159. /* basic idea: we sort a column of edges.
  160. For each pair of point, we color the points in between */
  161. $n = count($bords);
  162. for ($i = 0; $i<$n; $i++) { // Y
  163. //error_log(__FILE__.' - Border Num '.$i,0);
  164. if (is_array($bords[$i])) {
  165. sort($bords[$i]);
  166. }
  167. for ($j = 0; $j<sizeof($bords[$i]);$j+=2) // bords
  168. for ($k = round($bords[$i][$j]); $k<=$bords[$i][$j+1];$k++) {
  169. $res[$k][$i] = true; //filling the array with trues
  170. if ($test == 1) {
  171. /*how to draw the polygon in a human way:
  172. In ubuntu : sudo apt-get install gnuplot
  173. Create an empty file with all points with the result of this echos (No commas, no point, no headers)
  174. In gnuplot:
  175. For 1 polygon: plot "/home/jmontoya/test"
  176. For 2 polygons: plot "/home/jmontoya/test", "/home/jmontoya/test2"
  177. A new window will appear with the plot
  178. */
  179. echo $k.' '.$i; echo '<br />';
  180. }
  181. }
  182. }
  183. return $res;
  184. }
  185. /**
  186. * poly_dump - dump an image on the screen
  187. *
  188. * @param array the polygone as output by poly_compile()
  189. * @param array see above (poly_init)
  190. * @param string Format ('raw' text or 'html')
  191. *
  192. * @return string html code of the representation of the polygone image
  193. */
  194. function poly_dump(&$poly, $max, $format='raw') {
  195. if ($format == 'html') {
  196. $s = "<div style='font-size: 8px; line-height:3px'><pre>\n";
  197. }
  198. for ($i=0; $i<$max['y']; $i++) {
  199. for($j=0; $j<$max['x']; $j++)
  200. if($poly[$j][$i] == TRUE)
  201. $s .= ($format=='html'?"<b>1</b>":'1');
  202. else
  203. $s .= "0";
  204. $s .= ($format=='html'?"<br />\n":"\n");
  205. }
  206. $s .= ($format=='html'?"</pre></div>\n":"\n");
  207. return $s;
  208. }
  209. /**
  210. * poly_result - compute statis for two polygones
  211. *
  212. * @param poly1 first polygone as returned by poly_compile
  213. * @param poly2 second ....
  214. * @param max resolution as specified for poly_init
  215. *
  216. * @returns (see below, UTSL)
  217. */
  218. function poly_result(&$poly1, &$poly2, $max) {
  219. $onlyIn1 = 0;
  220. $surfaceOf1 = 0;
  221. $surfaceOf2 = 0;
  222. for ($i=0; $i<$max['x']; $i++)
  223. for($j=0; $j<$max['y']; $j++) {
  224. if (isset($poly1[$i][$j]) && ($poly1[$i][$j] == TRUE)) {
  225. $surfaceOf1++;
  226. if (isset($poly2[$i][$j]) && ($poly2[$i][$j] == FALSE))
  227. $onlyIn1++;
  228. }
  229. if (isset($poly2[$i][$j]) && ($poly2[$i][$j] == TRUE))
  230. $surfaceOf2++;
  231. }
  232. return array (
  233. "s1" => $surfaceOf1,
  234. "s2" => $surfaceOf2,
  235. "both" => $surfaceOf1 - $onlyIn1,
  236. "s1Only" => $onlyIn1,
  237. "s2Only" => $surfaceOf2 - ($surfaceOf1 - $onlyIn1));
  238. }
  239. /**
  240. * poly_touch - compute statis for two polygones
  241. *
  242. * @param poly1 first polygone as returned by poly_compile
  243. * @param poly2 second ....
  244. * @param max resolution as specified for poly_init
  245. *
  246. * @returns (see below, UTSL)
  247. */
  248. function poly_touch(&$poly1, &$poly2, $max) {
  249. for ($i=0; $i<$max['x']; $i++) {
  250. for($j=0; $j<$max['y']; $j++) {
  251. if (isset($poly1[$i][$j]) && ($poly1[$i][$j] == true)
  252. && isset($poly2[$i][$j]) && ($poly2[$i][$j] == true)) {
  253. return true;
  254. }
  255. }
  256. }
  257. return FALSE;
  258. }
  259. /**
  260. * Convert a list of points in x1;y1|x2;y2|x3;y3 or x1;y1/x2;y2 format to
  261. * the format in which the functions in this library are expecting their data
  262. * @param string List of points in x1;y1|... format (or /)
  263. * @param string The points separator for the list (| or /)
  264. * @return array An array of points in the right format to use with the
  265. * local functions
  266. */
  267. function convert_coordinates($coords,$sep='|') {
  268. $points = array();
  269. $pairs = explode($sep,$coords);
  270. foreach ($pairs as $idx => $pcoord) {
  271. list($x,$y) = explode(';',$pcoord);
  272. $points[] = array('x'=>$x,'y'=>$y);
  273. }
  274. return $points;
  275. }
  276. /**
  277. * Returns the maximum coordinates in x,y (from 0,0) that the geometrical form
  278. * can reach
  279. * @param array Coordinates of one polygon
  280. * @return array ('x'=>val,'y'=>val)
  281. */
  282. function poly_get_max(&$coords1, &$coords2) {
  283. $mx = 0;
  284. $my = 0;
  285. foreach ($coords1 as $coord) {
  286. if ($coord['x'] > $mx) {
  287. $mx = $coord['x'];
  288. }
  289. if ($coord['y'] > $my) {
  290. $my = $coord['y'];
  291. }
  292. }
  293. foreach ($coords2 as $coord) {
  294. if ($coord['x'] > $mx) {
  295. $mx = $coord['x'];
  296. }
  297. if ($coord['y'] > $my) {
  298. $my = $coord['y'];
  299. }
  300. }
  301. return array('x'=>$mx,'y'=>$my);
  302. }