degate 0.1.1
Rectangle.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 "Rectangle.h"
00023 #include "globals.h"
00024 #include <iostream>
00025 #include <algorithm>
00026 
00027 using namespace degate;
00028 
00029 Rectangle::Rectangle() :
00030   min_x(0),
00031   max_x(0),
00032   min_y(0),
00033   max_y(0) {
00034   calculate_bounding_box();
00035 }
00036 
00037 Rectangle::Rectangle(int min_x, int max_x, int min_y, int max_y) {
00038 
00039   this->min_x = std::min(min_x, max_x);
00040   this->max_x = std::max(min_x, max_x);
00041   this->min_y = std::min(min_y, max_y);
00042   this->max_y = std::max(min_y, max_y);
00043   calculate_bounding_box();
00044 }
00045 
00046 Rectangle::Rectangle(const Rectangle& o) {
00047   this->min_x = o.min_x;
00048   this->max_x = o.max_x;
00049   this->min_y = o.min_y;
00050   this->max_y = o.max_y;
00051   calculate_bounding_box();
00052 }
00053 
00054 Rectangle::~Rectangle() {
00055 }
00056 
00057 bool Rectangle::in_shape(int x, int y, int max_distance) const {
00058   return (x >= min_x - max_distance && x <= max_x + max_distance &&
00059           y >= min_y - max_distance && y <= max_y + max_distance) ? true : false;
00060 }
00061 
00062 BoundingBox const& Rectangle::get_bounding_box() const {
00063   return bounding_box;
00064 }
00065 
00066 
00067 bool Rectangle::operator==(const Rectangle& other) const {
00068   return (min_x == other.min_x &&
00069           max_x == other.max_x &&
00070           min_y == other.min_y &&
00071           max_y == other.max_y);
00072 }
00073 
00074 bool Rectangle::operator!=(const Rectangle& other) const {
00075   return !(*this == other);
00076 }
00077 
00078 /**
00079  * Check, if this rectangle is in the bounding box.
00080  */
00081 
00082 bool Rectangle::in_bounding_box(BoundingBox const& bbox) const {
00083 
00084   return ( bbox.get_min_x() <= min_x ||
00085            bbox.get_max_x() >= max_x ||
00086            bbox.get_min_y() <= min_y ||
00087            bbox.get_max_y() >= max_y);
00088 }
00089 
00090 bool Rectangle::intersects(Rectangle const & rect) const {
00091 
00092   return !( rect.min_x > max_x ||
00093             rect.max_x < min_x ||
00094             rect.min_y > max_y ||
00095             rect.max_y < min_y);
00096 }
00097 
00098 
00099 /**
00100  * Check, if rectangle rect is complete within rectangle represented by this.
00101  */
00102 
00103 bool Rectangle::complete_within(Rectangle const & rect) const {
00104 
00105   return (min_x <= rect.min_x &&
00106           max_x >= rect.max_x &&
00107           min_y <= rect.min_y &&
00108           max_y >= rect.max_y);
00109 }
00110 
00111 unsigned int Rectangle::get_width() const {
00112   return max_x - min_x;
00113 }
00114 
00115 unsigned int Rectangle::get_height() const {
00116   return max_y - min_y;
00117 }
00118 
00119 int Rectangle::get_min_x() const {
00120   return min_x;
00121 }
00122 
00123 int Rectangle::get_max_x() const {
00124   return max_x;
00125 }
00126 
00127 int Rectangle::get_min_y() const {
00128   return min_y;
00129 }
00130 
00131 int Rectangle::get_max_y() const {
00132   return max_y;
00133 }
00134 
00135 void Rectangle::set_min_x(int min_x) {
00136   this->min_x = std::min(min_x, max_x);
00137   this->max_x = std::max(min_x, max_x);
00138   calculate_bounding_box();
00139 }
00140 
00141 void Rectangle::set_min_y(int min_y) {
00142   this->min_y = std::min(min_y, max_y);
00143   this->max_y = std::max(min_y, max_y);
00144   calculate_bounding_box();
00145 }
00146 
00147 void Rectangle::set_max_x(int max_x) {
00148   this->min_x = std::min(min_x, max_x);
00149   this->max_x = std::max(min_x, max_x);
00150   calculate_bounding_box();
00151 }
00152 
00153 void Rectangle::set_max_y(int max_y) {
00154   this->min_y = std::min(min_y, max_y);
00155   this->max_y = std::max(min_y, max_y);
00156   calculate_bounding_box();
00157 }
00158 
00159 void Rectangle::shift_y(int delta_y) {
00160   min_y += delta_y;
00161   max_y += delta_y;
00162   calculate_bounding_box();
00163 }
00164 
00165 void Rectangle::shift_x(int delta_x) {
00166   min_x += delta_x;
00167   max_x += delta_x;
00168   calculate_bounding_box();
00169 }
00170 
00171 
00172 int Rectangle::get_center_x() const {
00173   return min_x + get_width() / 2;
00174 }
00175 
00176 int Rectangle::get_center_y() const {
00177   return min_y + get_height() / 2;
00178 }
00179 
00180 void Rectangle::set_position(int min_x, int max_x, int min_y, int max_y) {
00181   this->min_x = std::min(min_x, max_x);
00182   this->max_x = std::max(min_x, max_x);
00183   this->min_y = std::min(min_y, max_y);
00184   this->max_y = std::max(min_y, max_y);
00185   calculate_bounding_box();
00186 }
00187 
00188 void Rectangle::calculate_bounding_box() {
00189   bounding_box = BoundingBox(min_x, max_x, min_y, max_y);
00190 }