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