degate  0.1.2
BoundingBox.cc
Go to the documentation of this file.
00001 /*
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #include "globals.h"
00023 #include "BoundingBox.h"
00024 #include <algorithm>
00025 #include <boost/format.hpp>
00026 
00027 using namespace degate;
00028 
00029 BoundingBox::BoundingBox() {
00030   max_y = min_y = max_x = min_x = 0;
00031 }
00032 
00033 BoundingBox::BoundingBox(int min_x, int max_x, int min_y, int max_y) {
00034   this->min_x = std::min(min_x, max_x);
00035   this->max_x = std::max(min_x, max_x);
00036   this->min_y = std::min(min_y, max_y);
00037   this->max_y = std::max(min_y, max_y);
00038 }
00039 
00040 BoundingBox::BoundingBox(int width, int height) {
00041   this->min_x = 0;
00042   this->max_x = width;
00043   this->min_y = 0;
00044   this->max_y = height;
00045 }
00046 
00047 BoundingBox::BoundingBox(const BoundingBox& o) {
00048   this->min_x = o.min_x;
00049   this->max_x = o.max_x;
00050   this->min_y = o.min_y;
00051   this->max_y = o.max_y;
00052 }
00053 
00054 BoundingBox::~BoundingBox() {
00055 }
00056 
00057 BoundingBox const& BoundingBox::get_bounding_box() const {
00058   return *this;
00059 }
00060 
00061 bool BoundingBox::in_shape(int x, int y, int max_distance) const {
00062   return (x >= min_x - max_distance && x <= max_x + max_distance &&
00063           y >= min_y - max_distance && y <= max_y + max_distance) ? true : false;
00064 }
00065 
00066 bool BoundingBox::operator==(const BoundingBox& other) const {
00067     return (min_x == other.min_x &&
00068             max_x == other.max_x &&
00069             min_y == other.min_y &&
00070             max_y == other.max_y);
00071 }
00072 
00073 bool BoundingBox::operator!=(const BoundingBox& other) const {
00074     return !(*this == other);
00075 }
00076 
00077 
00078 bool BoundingBox::in_bounding_box(BoundingBox const& bbox) const {
00079   return (min_x >= bbox.min_x &&
00080           max_x <= bbox.max_x &&
00081           min_y >= bbox.min_y &&
00082           max_y <= bbox.max_y);
00083 }
00084 
00085 bool BoundingBox::intersects(BoundingBox const & rect) const {
00086 
00087   return !( rect.min_x > max_x ||
00088             rect.max_x < min_x ||
00089             rect.min_y > max_y ||
00090             rect.max_y < min_y);
00091 }
00092 
00093 
00094 /**
00095  * Check, if rectangle rect is complete within rectangle represented by this.
00096  */
00097 
00098 bool BoundingBox::complete_within(BoundingBox const & rect) const {
00099 
00100   return (min_x <= rect.min_x &&
00101           max_x >= rect.max_x &&
00102           min_y <= rect.min_y &&
00103           max_y >= rect.max_y);
00104 }
00105 
00106 unsigned int BoundingBox::get_width() const {
00107   return max_x - min_x;
00108 }
00109 
00110 unsigned int BoundingBox::get_height() const {
00111   return max_y - min_y;
00112 }
00113 
00114 int BoundingBox::get_min_x() const {
00115   return min_x;
00116 }
00117 
00118 int BoundingBox::get_max_x() const {
00119   return max_x;
00120 }
00121 
00122 int BoundingBox::get_min_y() const {
00123   return min_y;
00124 }
00125 
00126 int BoundingBox::get_max_y() const {
00127   return max_y;
00128 }
00129 
00130 void BoundingBox::set_min_x(int min_x) {
00131   this->min_x = std::min(min_x, max_x);
00132   this->max_x = std::max(min_x, max_x);
00133 }
00134 
00135 void BoundingBox::set_min_y(int min_y) {
00136   this->min_y = std::min(min_y, max_y);
00137   this->max_y = std::max(min_y, max_y);
00138 }
00139 
00140 void BoundingBox::set_max_x(int max_x) {
00141   this->min_x = std::min(min_x, max_x);
00142   this->max_x = std::max(min_x, max_x);
00143 }
00144 
00145 void BoundingBox::set_max_y(int max_y) {
00146   this->min_y = std::min(min_y, max_y);
00147   this->max_y = std::max(min_y, max_y);
00148 }
00149 
00150 void BoundingBox::shift_y(int delta_y) {
00151   min_y += delta_y;
00152   max_y += delta_y;
00153 }
00154 
00155 void BoundingBox::shift_x(int delta_x) {
00156   min_x += delta_x;
00157   max_x += delta_x;
00158 }
00159 
00160 void BoundingBox::shift(int delta_x, int delta_y) {
00161   shift_x(delta_x);
00162   shift_y(delta_y);
00163 }
00164 
00165 int BoundingBox::get_center_x() const {
00166   return min_x + get_width() / 2;
00167 }
00168 
00169 int BoundingBox::get_center_y() const {
00170   return min_y + get_height() / 2;
00171 }
00172 
00173 void BoundingBox::set(int min_x, int max_x, int min_y, int max_y) {
00174   this->min_x = std::min(min_x, max_x);
00175   this->max_x = std::max(min_x, max_x);
00176   this->min_y = std::min(min_y, max_y);
00177   this->max_y = std::max(min_y, max_y);
00178 }
00179 
00180 void BoundingBox::print(std::ostream & os, int n_tabs) const {
00181   os << gen_tabs(n_tabs) << to_string();
00182 }
00183 
00184 std::string BoundingBox::to_string() const {
00185   boost::format f("x = %1% .. %2% / y = %3% ... %4%");
00186   f % min_x % max_x % min_y % max_y;
00187   return f.str();
00188 }