1
0

findopenrect.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include <precomp.h>
  2. #include "findopenrect.h"
  3. #include <bfc/pair.h>
  4. //CUT #define MAXTRIALS 10000 // unless time runs out
  5. #define MOUSEPENALTY 65536
  6. FindOpenRect::FindOpenRect()
  7. {
  8. timelimit = 100;
  9. iters = 0;
  10. xdiv = ydiv = 1;
  11. fn = compare_overlapArea;
  12. userdata = userdata2 = 0;
  13. bsfval = bsfdist = 0;
  14. }
  15. void FindOpenRect::setCompareRectsFn(compareRectsFn _fn)
  16. {
  17. fn = _fn;
  18. }
  19. RECT FindOpenRect::find(const RECT &vr, const PtrList<RECT> &list, const RECT &prev, unsigned long userdata, unsigned long userdata2)
  20. {
  21. beginFind(vr, list, prev, userdata, userdata2);
  22. return findMore();
  23. }
  24. void FindOpenRect::beginFind(const RECT &viewport, const PtrList<RECT> &_list,
  25. const RECT &_prev, unsigned long _userdata, unsigned long _userdata2)
  26. {
  27. vr = viewport;
  28. list.copyFrom(&_list);
  29. prev = _prev;
  30. userdata = _userdata;
  31. userdata2 = _userdata2;
  32. // reset best rect found so far
  33. //CUT bsf_nooverlap = prev;
  34. //CUT found_nooverlap=0;
  35. //CUT bsf_overlap = prev;
  36. //CUT bsfval_nooverlap = 1e100;
  37. //CUT bsfval_overlap = 1e100;
  38. bsfrect = prev;
  39. bsfval = 10e10;
  40. bsfdist = 10e10;
  41. //CUT POINT mousepos;
  42. //CUT Std::getMousePos(&mousepos);
  43. }
  44. RECT FindOpenRect::findMore()
  45. {
  46. int w = prev.right - prev.left, h = prev.bottom - prev.top;
  47. int vw = vr.right - vr.left;
  48. int vh = vr.bottom - vr.top;
  49. if (vw <= w || vh <= h)
  50. {
  51. DebugStringW(L"findopenrect: window too big for viewport");
  52. }
  53. double started = Wasabi::Std::getTimeStampMS();
  54. for (int c = 0; /*CUTc < MAXTRIALS*/; c++)
  55. {
  56. // too bad = crashy crash :P love, BU
  57. if (c != 0 && (vw <= w || vh <= h))
  58. break;
  59. // set the trial rect
  60. RECT tr;
  61. if (iters == 0)
  62. { // try prev
  63. tr = prev;
  64. }
  65. else
  66. {
  67. int x = Wasabi::Std::random32(vw - w) + vr.top;
  68. int y = Wasabi::Std::random32(vh - h) + vr.left;
  69. if (xdiv != 1) x -= x % xdiv;
  70. if (ydiv != 1) y -= y % ydiv;
  71. tr = Wasabi::Std::makeRect(x, y, x + w, y + h);
  72. }
  73. // add up the coverage of trial position
  74. trySingleRect(tr, TRUE);
  75. if (timelimit > 0 && c > 0 && (int)((Wasabi::Std::getTimeStampMS() - started)*1000.f) > timelimit)
  76. {
  77. // DebugString("FindOpenRect::find() timeout, %d iters", c);
  78. break;
  79. }
  80. }
  81. return getBestRectSoFar();
  82. }
  83. typedef Pair<RECT, double> RectV;
  84. namespace
  85. {
  86. class Sorter
  87. {
  88. public:
  89. static int compareItem(RectV *p1, RectV* p2)
  90. {
  91. return -CMP3(p1->b, p2->b);
  92. }
  93. };
  94. };
  95. double FindOpenRect::trySingleRect(const RECT &tr, int early_out)
  96. {
  97. #if 0
  98. PtrListQuickSorted<RectV, Sorter> candidates(list.getNumItems());
  99. foreach(list)
  100. double d = compare_overlapArea(tr, *list.getfor());
  101. candidates.addItem(new RectV(*list.getfor(), d));
  102. endfor
  103. candidates.sort();
  104. #else
  105. PtrList<RECT> candidates;
  106. candidates.copyFrom(&list);
  107. #endif
  108. double trarea = 0;
  109. foreach(candidates)
  110. #if 0
  111. RECT *wr = &candidates.getfor()->a;
  112. #else
  113. RECT *wr = candidates.getfor();
  114. #endif
  115. //CUT double bsf = MIN(found_nooverlap ? 0 : bsfval_nooverlap, bsfval_overlap);
  116. trarea += (*fn)(tr, *wr, userdata, userdata2, foreach_index, bsfval);
  117. // early quit if not breaking any records
  118. if (early_out && trarea >= bsfval) break;
  119. endfor
  120. #if 0
  121. candidates.deleteAll();
  122. #endif
  123. if (trarea < 0.005) trarea = 0;
  124. #if 0
  125. // found one that does not overlap!
  126. if (trarea == 0)
  127. {
  128. if (val < bsfval_nooverlap)
  129. { // if it's closer than prev closest, save it
  130. bsf_nooverlap = tr;
  131. bsfval_nooverlap = val;
  132. found_nooverlap = 1;
  133. }
  134. }
  135. else
  136. {
  137. if (trarea < bsfval_overlap)
  138. { // overlaps least stuff
  139. bsf_overlap = tr;
  140. bsfval_overlap = trarea;
  141. }
  142. }
  143. #endif
  144. if (trarea == 0)
  145. {
  146. // find the distance from the previous
  147. double val = SQRT(SQR(prev.left - tr.left) + SQR(prev.top - tr.top));
  148. if (bsfdist > val)
  149. {
  150. bsfrect = tr;
  151. bsfdist = val;
  152. }
  153. }
  154. else
  155. {
  156. if (trarea < bsfval)
  157. {
  158. bsfrect = tr;
  159. bsfval = trarea;
  160. }
  161. }
  162. iters++;
  163. return trarea;
  164. }
  165. double FindOpenRect::compare_overlapArea(const RECT &rect, const RECT &dest, unsigned long userdata, unsigned long userdata2, int index, double bsf)
  166. {
  167. RECT intersection;
  168. if (Wasabi::Std::rectIntersect(rect, dest, &intersection))
  169. {
  170. intersection.right -= intersection.left;
  171. intersection.bottom -= intersection.top;
  172. return intersection.right * intersection.bottom;
  173. }
  174. return 0;
  175. }
  176. void FindOpenRect::setTimeLimit(int ms)
  177. {
  178. timelimit = ms;
  179. }
  180. int FindOpenRect::getNumIters()
  181. {
  182. return iters;
  183. }
  184. void FindOpenRect::resetNumIters()
  185. {
  186. iters = 0;
  187. }
  188. double FindOpenRect::getBestValSoFar()
  189. {
  190. return (bsfval == 10e10) ? 0 : bsfval;
  191. }
  192. RECT FindOpenRect::getBestRectSoFar()
  193. {
  194. return bsfrect;
  195. }
  196. void FindOpenRect::setBestSoFar(double bsf, const RECT &bestrect)
  197. {
  198. if (bsf > 0 && bsfval > bsf)
  199. {
  200. bsfval = bsf;
  201. bsfrect = bestrect;
  202. bsfdist = SQRT(SQR(prev.left - bsfrect.left) + SQR(prev.top - bsfrect.top));
  203. //CUTDebugString("*******************8 better BSF ****************");
  204. }
  205. }
  206. RECT FindOpenRect::getOriginalRect()
  207. {
  208. return prev;
  209. }
  210. void FindOpenRect::setCoordDivisor(int _xdiv, int _ydiv)
  211. {
  212. xdiv = MAX(_xdiv, 1);
  213. ydiv = MAX(_ydiv, 1);
  214. }