Region.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include <algorithm>
  2. #include "Region.h"
  3. #include <assert.h>
  4. using namespace NICE;
  5. using namespace std;
  6. void SegList::delseg(lsegp &lp)
  7. {
  8. lsegp hp = lp;
  9. lp = lp->next;
  10. delete hp;
  11. }
  12. void SegList::divseg(lsegp &lp,int x)
  13. {
  14. lp->next=new lseg(x+1,lp->x2,lp->next);
  15. lp->x2=x;
  16. }
  17. SegList & SegList::operator=(const SegList& src)
  18. {
  19. if (this==&src) return *this;
  20. list->del();
  21. delete list;
  22. list=NULL;
  23. lsegp sourcep=src.list;
  24. if (sourcep!=NULL)
  25. {
  26. list=new lseg(*sourcep);
  27. list->next=NULL;
  28. lsegp destp=list;
  29. sourcep=sourcep->next;
  30. while (sourcep!=NULL)
  31. {
  32. destp->next=new lseg(*sourcep);
  33. sourcep=sourcep->next;
  34. destp=destp->next;
  35. destp->next=NULL;
  36. }
  37. }
  38. area=src.area;
  39. return *this;
  40. }
  41. SegList::SegList(const SegList& r)
  42. {
  43. list=NULL;
  44. lsegp sourcep=r.list;
  45. if (sourcep!=NULL)
  46. {
  47. list=new lseg(*sourcep);
  48. list->next=NULL;
  49. lsegp destp=list;
  50. sourcep=sourcep->next;
  51. while (sourcep!=NULL)
  52. {
  53. destp->next=new lseg(*sourcep);
  54. sourcep=sourcep->next;
  55. destp=destp->next;
  56. destp->next=NULL;
  57. }
  58. }
  59. area=r.area;
  60. }
  61. int SegList::del(lsegp &lp,int x1,int x2)
  62. {
  63. int da=0;
  64. if (lp==NULL) return 0; // leere Liste
  65. int lx1=lp->x1;
  66. if (lx1>x2) return 0; // Bereich völlig vor erstem Segment
  67. int lx2=lp->x2;
  68. if (lx2<x1) return del(lp->next,x1,x2); // Bereich völlig nach erstem Segment
  69. if (lx1>=x1) // Bereich beginnt vor erstem segment
  70. {
  71. if (lx2>x2) // Segment von vorne Kürzen
  72. {
  73. lp->x1=x2+1;
  74. da=x2+1-lx1;
  75. area-=da;
  76. return da;
  77. }
  78. else
  79. {
  80. delseg(lp); // Segment liegt innerhalb des bereiches
  81. da=lx2-lx1+1;
  82. area-=da;
  83. return del(lp,x1,x2)+da;
  84. }
  85. }
  86. // Bereich beginnt im ersten segment -> Segment kürzen
  87. lp->x2=x1-1;
  88. if (lx2>x2) // ragt altes Segment über Bereich hinaus?
  89. {
  90. // zweites teilsegment einfügen
  91. lp->next=new lseg(x2+1,lx2,lp->next);
  92. da=x2-x1+1;
  93. area-=da;
  94. return da;
  95. }
  96. else
  97. {
  98. da=lx2-x1+1;
  99. area-=da;
  100. return del(lp->next,x1,x2)+da;
  101. }
  102. }
  103. int SegList::del(int x1,int x2)
  104. {
  105. assert(x1<=x2);
  106. return del(list,x1,x2);
  107. }
  108. int SegList::del(const SegList &s)
  109. {
  110. int da=0;
  111. if (s.isEmpty()) return 0;
  112. if (isEmpty()) return 0;
  113. lsegp lp=s.list;
  114. while (lp!=NULL)
  115. {
  116. da+=del(lp->x1,lp->x2);
  117. lp=lp->next;
  118. }
  119. return da;
  120. }
  121. int SegList::getMin() const
  122. {
  123. if ( list != NULL )
  124. return list->x1;
  125. else
  126. return -1;
  127. }
  128. int SegList::getMax() const
  129. {
  130. if ( list != NULL )
  131. {
  132. lsegp l = list;
  133. while (l->next != NULL)
  134. l = l->next;
  135. return l->x2;
  136. } else {
  137. return -1;
  138. }
  139. }
  140. int SegList::add(lsegp &lp,int x1,int x2)
  141. {
  142. int da;
  143. if (lp==NULL)
  144. {
  145. lp=new lseg(x1,x2);
  146. da=x2-x1+1;
  147. area+=da;
  148. return da;
  149. }
  150. int lx1=lp->x1;
  151. int lx2=lp->x2;
  152. if (lx2+1<x1)
  153. {
  154. return add(lp->next,x1,x2);
  155. }
  156. if (lp->connected(x1,x2))
  157. {
  158. if (lp->x1<x1) x1=lp->x1;
  159. if (lp->x2>x2) x2=lp->x2;
  160. delseg(lp);
  161. da=-lx2+lx1-1;
  162. area+=da;
  163. return add(lp,x1,x2)+da;
  164. }
  165. // Segment liegt vor der Liste
  166. lp=new lseg(x1,x2,lp);
  167. da= x2-x1+1;
  168. area+=da;
  169. return da;
  170. }
  171. int SegList::add(int x1,int x2)
  172. {
  173. assert(x1<=x2);
  174. return add(list,x1,x2);
  175. }
  176. int SegList::add(const SegList &s)
  177. {
  178. int da=0;
  179. if (s.isEmpty()) return 0;
  180. lsegp lp=s.list;
  181. while (lp!=NULL)
  182. {
  183. da+=add(lp->x1,lp->x2);
  184. lp=lp->next;
  185. }
  186. return da;
  187. }
  188. int SegList::andop(lsegp &p1,lsegp const &p2)
  189. {
  190. if (p2==NULL)
  191. {
  192. del(p1);
  193. area=0;
  194. return 1;
  195. }
  196. if (p1==NULL) return 1;
  197. if (p1->x2<p2->x1)
  198. {
  199. area-=p1->len();
  200. delseg(p1);
  201. return andop(p1,p2);
  202. }
  203. if (p2->x2<p1->x1) return andop(p1,p2->next);
  204. if (p1->x1<p2->x1)
  205. {
  206. area-=p2->x1-p1->x1;
  207. p1->x1=p2->x1;
  208. }
  209. if (p1->x2<=p2->x2) return andop(p1->next,p2);
  210. else
  211. {
  212. divseg(p1,p2->x2);
  213. return andop(p1->next,p2->next);
  214. }
  215. }
  216. int SegList::andop(const SegList &s)
  217. {
  218. if (area==0) return 1;
  219. if (s.isEmpty())
  220. {
  221. // ganze Liste löschen
  222. area=0;
  223. del(list);
  224. return 1;
  225. }
  226. andop(list,s.list);
  227. return 1;
  228. }
  229. bool SegList::inside(int x1) const
  230. {
  231. lsegp hp=list;
  232. while (hp!=NULL)
  233. {
  234. if (hp->inside(x1)) return true;
  235. hp=hp->next;
  236. }
  237. return false;
  238. }
  239. int Region::newy(int y)
  240. {
  241. if (sl.size()==0) {
  242. y0=y;
  243. }
  244. while (y>=y0+(int)sl.size())
  245. {
  246. sl.push_back(SegList());
  247. }
  248. while (y<y0)
  249. {
  250. sl.push_front(SegList());
  251. y0--;
  252. }
  253. return 1;
  254. }
  255. int Region::cuty()
  256. {
  257. if (sl.size()==0) return 1;
  258. while ((sl.size()>0)&&(sl[0].isEmpty()))
  259. {
  260. sl.pop_front();
  261. y0++;
  262. }
  263. while ((sl.size()>0)&&(sl[sl.size()-1].isEmpty()))
  264. {
  265. sl.pop_back();
  266. }
  267. return 1;
  268. }
  269. void Region::getRect ( int & xi, int & yi, int & xa, int & ya ) const
  270. {
  271. yi = y0;
  272. ya = y0 + sl.size() - 1;
  273. xa = -1;
  274. xi = 0;
  275. for ( deque<SegList>::const_iterator i = sl.begin();
  276. i != sl.end();
  277. i++ )
  278. {
  279. if ( (xi > i->getMin()) || (i == sl.begin()) )
  280. xi = i->getMin();
  281. if ( xa < i->getMax() )
  282. xa = i->getMax();
  283. }
  284. }
  285. int Region::getWidth () const
  286. {
  287. int max = 0;
  288. for ( deque<SegList>::const_iterator i = sl.begin();
  289. i != sl.end();
  290. i++ )
  291. {
  292. if ( max < i->getArea() ) max = i->getArea();
  293. }
  294. return max;
  295. }
  296. int Region::getHeight () const
  297. {
  298. return sl.size();
  299. }
  300. int Region::add(int x,int y)
  301. {
  302. int da;
  303. newy(y);
  304. da= sl[y-y0].add(x);
  305. area+=da;
  306. return da;
  307. }
  308. int Region::add(const Region &r)
  309. {
  310. int y;
  311. int da=0;
  312. for (y=0;y<(int)r.sl.size();y++)
  313. {
  314. newy(y+r.y0);
  315. da+=sl[y+r.y0-y0].add(r.sl[y]);
  316. }
  317. area+=da;
  318. return da;
  319. }
  320. int Region::add(int x1,int y1,int x2,int y2)
  321. {
  322. int y;
  323. int da=0;
  324. for (y=y1;y<=y2;y++)
  325. {
  326. newy(y);
  327. da+=sl[y-y0].add(x1,x2);
  328. }
  329. area+=da;
  330. return da;
  331. }
  332. int Region::del(int x,int y)
  333. {
  334. int da;
  335. int yh=y-y0;
  336. if (yh<0) return 0;
  337. if (yh>=(int)sl.size()) return 0;
  338. da= sl[y-y0].del(x);
  339. cuty();
  340. area-=da;
  341. return da;
  342. }
  343. int Region::del(const Region &r)
  344. {
  345. int y;
  346. int da=0;
  347. for (y=r.y0;y<r.y0+(int)r.sl.size();y++)
  348. {
  349. if (inside(y))
  350. da+=sl[y-y0].del(r.sl[y-r.y0]);
  351. }
  352. cuty();
  353. area-=da;
  354. return da;
  355. }
  356. int Region::del(int x1,int y1,int x2,int y2)
  357. {
  358. int y;
  359. int da=0;
  360. for (y=y1;y<=y2;y++)
  361. {
  362. int yh=y-y0;
  363. if ((yh>=0)&&(yh<(int)sl.size()))
  364. da+=sl[yh].del(x1,x2);
  365. }
  366. cuty();
  367. area-=da;
  368. return da;
  369. }
  370. int Region::andop(const Region &r)
  371. {
  372. int da=0;
  373. int y;
  374. for (y=0;y<(int)sl.size();y++)
  375. {
  376. if (r.inside(y+y0))
  377. {
  378. if (!sl[y].isEmpty())
  379. {
  380. da+=sl[y].andop(r.sl[y+y0-r.y0]);
  381. }
  382. }
  383. else sl[y].free();
  384. }
  385. area-=da;
  386. return da;
  387. }
  388. void Region::getCentroid ( double & x, double & y ) const
  389. {
  390. int anz = 0;
  391. x = 0.0;
  392. y = 0.0;
  393. for (int ly=0;ly<(int)sl.size();ly++)
  394. {
  395. SegList::lsegp fp=sl[ly].list;
  396. while (fp != NULL)
  397. {
  398. for (int i = fp->x1; i <= fp->x2; i++)
  399. {
  400. x += i;
  401. y += ly+y0;
  402. anz++;
  403. }
  404. fp = fp->next;
  405. }
  406. }
  407. x /= (double)anz;
  408. y /= (double)anz;
  409. }
  410. void Region::setIntersection ( const Region & x, const Region & y )
  411. {
  412. *this = x - (x - y);
  413. }
  414. bool Region::inside(int xp,int yp) const
  415. {
  416. int yh=yp-y0;
  417. if (yh<0) return false;
  418. if (yh>=(int)sl.size()) return false;
  419. return sl[yh].inside(xp);
  420. }
  421. bool Region::inside(int yp) const
  422. {
  423. int yh=yp-y0;
  424. if (yh<0) return false;
  425. if (yh>=(int)sl.size()) return false;
  426. return true;
  427. }