Rectangle.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Image/Rectangle.H"
00039
00040 #include "Util/Assert.H"
00041 #include "Util/StringConversions.H"
00042 #include "Util/log.H"
00043
00044 #include <sstream>
00045
00046
00047
00048
00049
00050
00051 std::string convertToString(const Rectangle& val)
00052 {
00053 if (val.isValid())
00054 {
00055 std::stringstream s;
00056 s<<val.left()<<','<<val.top()<<','<<val.rightO()<<','<<val.bottomO();
00057 return s.str();
00058 }
00059
00060 return "-1,-1,-1,-1";
00061 }
00062
00063
00064 void convertFromString(const std::string& str, Rectangle& val)
00065 {
00066 std::stringstream s; int t = -1, b = -1, l = -1, r = -1;
00067 char c; s<<str; s>>l>>c>>t>>c>>r>>c>>b;
00068 if (s.fail())
00069 conversion_error::raise<Rectangle>(str);
00070
00071 val = Rectangle::tlbrO(t, l, b, r);
00072 }
00073
00074
00075 Rectangle constrainRect(const Rectangle& in,
00076 const Rectangle& bounds,
00077 int minw, int maxw,
00078 int minh, int maxh)
00079 {
00080 ASSERT(minw >= 0);
00081 ASSERT(maxw >= minw);
00082 ASSERT(minh >= 0);
00083 ASSERT(maxh >= minh);
00084
00085 Rectangle result = in.getOverlap(bounds);
00086
00087 ASSERT(bounds.contains(result));
00088
00089 minw = std::min(minw, bounds.width());
00090 maxw = std::min(maxw, bounds.width());
00091
00092 minh = std::min(minh, bounds.height());
00093 maxh = std::min(maxh, bounds.height());
00094
00095
00096
00097
00098
00099 if (result.width() < minw)
00100 {
00101 const int diff = minw - result.width();
00102 result = Rectangle::tlbrO(result.top(),
00103 result.left() - diff / 2,
00104 result.bottomO(),
00105 result.rightO() + diff - diff / 2);
00106 ASSERT(result.width() == minw);
00107 }
00108
00109 else if (result.width() > maxw)
00110 {
00111 const int diff = result.width() - maxw;
00112 result = Rectangle::tlbrO(result.top(),
00113 result.left() + diff / 2,
00114 result.bottomO(),
00115 result.rightO() - diff + diff / 2);
00116 ASSERT(result.width() == maxw);
00117 }
00118
00119
00120
00121
00122
00123
00124 if (result.left() < 0)
00125 {
00126 result += Point2D<int>(-result.left(), 0);
00127 ASSERT(result.left() == 0);
00128 }
00129 else if (result.rightO() > bounds.width())
00130 {
00131 result -= Point2D<int>(result.rightO() - bounds.width(), 0);
00132 ASSERT(result.rightO() == bounds.width());
00133 }
00134 ASSERT(bounds.contains(result));
00135
00136
00137
00138
00139
00140 if (result.height() < minh)
00141 {
00142 const int diff = minh - result.height();
00143 result = Rectangle::tlbrO(result.top() - diff / 2,
00144 result.left(),
00145 result.bottomO() + diff - diff / 2,
00146 result.rightO());
00147 ASSERT(result.height() == minh);
00148 }
00149 else if (result.height() > maxh)
00150 {
00151 const int diff = result.height() - maxh;
00152 result = Rectangle::tlbrO(result.top() + diff / 2,
00153 result.left(),
00154 result.bottomO() - diff + diff / 2,
00155 result.rightO());
00156 ASSERT(result.height() == maxh);
00157 }
00158
00159
00160
00161
00162
00163
00164 if (result.top() < 0)
00165 {
00166 result += Point2D<int>(0, -result.top());
00167 ASSERT(result.top() == 0);
00168 }
00169 else if (result.bottomO() > bounds.height())
00170 {
00171 result -= Point2D<int>(0, result.bottomO() - bounds.height());
00172 ASSERT(result.bottomO() == bounds.height());
00173 }
00174 ASSERT(bounds.contains(result));
00175
00176
00177
00178
00179
00180 ASSERT(result.width() >= minw);
00181 ASSERT(result.width() <= maxw);
00182 ASSERT(result.height() >= minh);
00183 ASSERT(result.height() <= maxh);
00184 ASSERT(bounds.contains(result));
00185
00186 return result;
00187 }
00188
00189
00190
00191
00192
00193