RectT.tcc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "core/image/RectT.h"
  2. namespace NICE {
  3. template <class T>
  4. bool RectT<T>::intersect(const RectT<T>& rect, RectT<T>& intersectRect) {
  5. Ipp32s xdata[4] = {left, right(), rect.left, rect.right()};
  6. Ipp32s ydata[4] = {top, bottom(), rect.top, rect.bottom()};
  7. VectorT<Ipp32s> xCoords(xdata, 4); xCoords.sortAscend();
  8. VectorT<Ipp32s> yCoords(ydata, 4); yCoords.sortAscend();
  9. // check if there is no intersection
  10. if( (xCoords[1]==right() && xCoords[2]==rect.left) ||
  11. (xCoords[1]==rect.right() && xCoords[2]==left ) ||
  12. (yCoords[1]==bottom() && yCoords[2]==rect.top ) ||
  13. (yCoords[1]==rect.bottom() && yCoords[2]==top ) ) {
  14. intersectRect = RectT<T>();
  15. return false;
  16. }
  17. // rectangles are equal
  18. else if( xCoords[0]==xCoords[1] && xCoords[2]==xCoords[3] &&
  19. yCoords[0]==yCoords[1] && yCoords[2]==yCoords[3] ) {
  20. intersectRect = RectT<T>(xCoords[1], yCoords[1], xCoords[2]-xCoords[1], yCoords[2]-yCoords[1]);
  21. return true;
  22. }
  23. // vertical adjacent rectangles
  24. else if( xCoords[1]==xCoords[2] ) {
  25. intersectRect = RectT<T>(xCoords[1], yCoords[1], 1, yCoords[2]-yCoords[1]);
  26. return true;
  27. }
  28. // horizontal adjacend rectangles
  29. else if( yCoords[1]==yCoords[2] ) {
  30. intersectRect = RectT<T>(xCoords[1], yCoords[1], xCoords[2]-xCoords[1], 1);
  31. return true;
  32. }
  33. // there is an intersection, so we calc the intersected rectangle
  34. else {
  35. intersectRect = RectT<T>(xCoords[1], yCoords[1], xCoords[2]-xCoords[1], yCoords[2]-yCoords[1]);
  36. return true;
  37. }
  38. intersectRect = RectT<T>();
  39. return false;
  40. }
  41. } // namespace NICE