Helper function to get a "valid" bounding box */
| 71 | |
| 72 | /* Helper function to get a "valid" bounding box */ |
| 73 | void GetValidBoundingBox(const BoundingBox& boxIn, BoundingBox& boxOut, |
| 74 | const BoundingBox& boxLimits) { |
| 75 | boxOut.SetX(std::max(boxIn.GetX(), boxLimits.GetX())); |
| 76 | boxOut.SetY(std::max(boxIn.GetY(), boxLimits.GetY())); |
| 77 | |
| 78 | /* If we have changed x and/or y, we compensate by reducing the height and/or width */ |
| 79 | int boxOutWidth = static_cast<int>(boxIn.GetWidth()) - |
| 80 | std::max(0, (boxOut.GetX() - boxIn.GetX())); |
| 81 | int boxOutHeight = static_cast<int>(boxIn.GetHeight()) - |
| 82 | std::max(0, (boxOut.GetY() - boxIn.GetY())); |
| 83 | |
| 84 | /* This suggests that there was no overlap on x or/and y axis */ |
| 85 | if (boxOutHeight <= 0 || boxOutWidth <= 0) |
| 86 | { |
| 87 | boxOut = BoundingBox{0, 0, 0, 0}; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | const int limitBoxRightX = boxLimits.GetX() + static_cast<int>(boxLimits.GetWidth()); |
| 92 | const int limitBoxRightY = boxLimits.GetY() + static_cast<int>(boxLimits.GetHeight()); |
| 93 | const int boxRightX = boxOut.GetX() + boxOutWidth; |
| 94 | const int boxRightY = boxOut.GetY() + boxOutHeight; |
| 95 | |
| 96 | if (boxRightX > limitBoxRightX) |
| 97 | { |
| 98 | boxOutWidth -= (boxRightX - limitBoxRightX); |
| 99 | } |
| 100 | |
| 101 | if (boxRightY > limitBoxRightY) |
| 102 | { |
| 103 | boxOutHeight -= (boxRightY - limitBoxRightY); |
| 104 | } |
| 105 | |
| 106 | /* This suggests value has rolled over because of very high numbers, not handled for now */ |
| 107 | if (boxOutHeight <= 0 || boxOutWidth <= 0) |
| 108 | { |
| 109 | boxOut = BoundingBox{0, 0, 0, 0}; |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | boxOut.SetHeight(static_cast<unsigned int>(boxOutHeight)); |
| 114 | boxOut.SetWidth(static_cast<unsigned int>(boxOutWidth)); |
| 115 | } |
| 116 | }// namespace od |