00001
00007 #include <string>
00008 #include <iostream>
00009 #include <fstream>
00010 #include <cmath>
00011 #include "boundingbox.h"
00012
00013 BoundingBox::BoundingBox()
00014 {
00015 m_x1 = 0;
00016 m_y1 = 0;
00017 m_x2 = 0;
00018 m_y2 = 0;
00019 }
00020
00021 BoundingBox::BoundingBox(int x1, int y1, int w, int h)
00022 {
00023 SetBoundingBox(x1, y1, w, h);
00024 }
00025
00026 BoundingBox::~BoundingBox()
00027 {
00028 }
00029
00030 void BoundingBox::Display() const
00031 {
00032 std::cout << "Corner 1 : " << m_x1 << " " << m_y1 << "\t";
00033 std::cout << "Corner 2 : " << m_x2 << " " << m_y2 << std::endl;
00034 }
00035
00036 bool BoundingBox::IsEmpty() const
00037 {
00038 return ((m_x2 == m_x1) ||
00039 (m_y2 == m_y1));
00040 }
00041
00042 void BoundingBox::SetBoundingBox(int x1, int y1, int w, int h)
00043 {
00044 m_x1 = x1;
00045 m_y1 = y1;
00046 m_x2 = x1 + w - 1;
00047 m_y2 = y1 + h - 1;
00048 }
00049
00050 int BoundingBox::GetWidth() const
00051 {
00052 if ((m_x2 == 0) && (m_x1 == 0)) {
00053 return 0;
00054 } else {
00055 return m_x2 - m_x1 + 1;
00056 }
00057 }
00058
00059 int BoundingBox::GetHeight() const
00060 {
00061 if ((m_y2 == 0) && (m_y1 == 0)) {
00062 return 0;
00063 } else {
00064 return m_y2 - m_y1 + 1;
00065 }
00066 }
00067
00068 void BoundingBox::Validate()
00069 {
00070
00071 if (m_x1 > m_x2)
00072 {
00073 int x = m_x1;
00074 m_x1 = m_x2;
00075 m_x2 = x;
00076 }
00077
00078 if (m_y1 > m_y2)
00079 {
00080 int y = m_y1;
00081 m_y1 = m_y2;
00082 m_y2 = y;
00083 }
00084 }
00085
00086 void BoundingBox::SetCoord(int x1, int y1, int x2, int y2)
00087 {
00088 m_x1 = x1;
00089 m_y1 = y1;
00090 m_x2 = x2;
00091 m_y2 = y2;
00092 }
00093
00094 void BoundingBox::MakeSquare()
00095 {
00096
00097 if (GetWidth() < GetHeight()) {
00098 if (m_y1 < m_y2) {
00099 m_y2 = m_y1 + std::abs(m_x2 - m_x1);
00100 } else {
00101 m_y2 = m_y1 - std::abs(m_x2 - m_x1);
00102 }
00103 } else {
00104 if (m_x1 < m_x2) {
00105 m_x2 = m_x1 + std::abs(m_y2 - m_y1);
00106 } else {
00107 m_x2 = m_x1 - std::abs(m_y2 - m_y1);
00108 }
00109 }
00110 }
00111
00112 void BoundingBox::Move(int dx, int dy)
00113 {
00114 m_x1 += dx;
00115 m_y1 += dy;
00116 m_x2 += dx;
00117 m_y2 += dy;
00118 }
00119
00120 bool BoundingBox::IsInside(int x, int y, bool border ) const
00121 {
00122 if (border
00123 && (x >= m_x1)
00124 && (x <= m_x2)
00125 && (y >= m_y1)
00126 && (y <= m_y2)) {
00127 return true;
00128 } else if (!border
00129 && (x > m_x1)
00130 && (x < m_x2)
00131 && (y > m_y1)
00132 && (y < m_y2)) {
00133 return true;
00134 }
00135 return false;
00136 }
00137
00138 bool BoundingBox::IsInside(const BoundingBox& bb) const
00139 {
00140 return ((bb.m_x1 > m_x1)
00141 && (bb.m_x2 < m_x2)
00142 && (bb.m_y1 > m_y1)
00143 && (bb.m_y2 < m_y2));
00144 }
00145
00146 BoundingBox BoundingBox::operator|(const BoundingBox &bb) const
00147 {
00148 BoundingBox tmp;
00149 if (m_x1 < bb.m_x1) {
00150 tmp.m_x1 = m_x1;
00151 } else {
00152 tmp.m_x1 = bb.m_x1;
00153 }
00154
00155 if (m_x2 > bb.m_x2) {
00156 tmp.m_x2 = m_x2;
00157 } else {
00158 tmp.m_x2 = bb.m_x2;
00159 }
00160
00161 if (m_y1 < bb.m_y1) {
00162 tmp.m_y1 = m_y1;
00163 } else {
00164 tmp.m_y1 = bb.m_y1;
00165 }
00166
00167 if (m_y2 > bb.m_y2) {
00168 tmp.m_y2 = m_y2;
00169 } else {
00170 tmp.m_y2 = bb.m_y2;
00171 }
00172 return tmp;
00173 }
00174
00175 BoundingBox& BoundingBox::operator|=(const BoundingBox &bb)
00176 {
00177 *this = *this | bb;
00178 return *this;
00179 }
00180
00181 bool BoundingBox::operator==(const BoundingBox& bb)
00182 {
00183 return ((m_x1 == bb.m_x1)
00184 && (m_y1 == bb.m_y1)
00185 && (m_y1 == bb.m_y1)
00186 && (m_y2 == bb.m_y2));
00187 }
00188
00189 BoundingBox& BoundingBox::operator=(const BoundingBox& bb)
00190 {
00191 m_x1 = bb.m_x1;
00192 m_x2 = bb.m_x2;
00193 m_y1 = bb.m_y1;
00194 m_y2 = bb.m_y2;
00195 return *this;
00196 }
00197
00198 int BoundingBox::GetOriginX() const {
00199 return m_x1;
00200 }
00201
00202 int BoundingBox::GetOriginY() const {
00203 return m_y1;
00204 }
00205
00206 int BoundingBox::GetEndX() const {
00207 return m_x2;
00208 }
00209
00210 int BoundingBox::GetEndY() const {
00211 return m_y2;
00212 }
00213
00214 void BoundingBox::SetOriginX(int pos) {
00215 m_x1 = pos;
00216 }
00217
00218 void BoundingBox::SetOriginY(int pos) {
00219 m_y1 = pos;
00220 }
00221
00222 void BoundingBox::SetEndX(int pos) {
00223 m_x2 = pos;
00224 }
00225
00226 void BoundingBox::SetEndY(int pos) {
00227 m_y2 = pos;
00228 }