degate  0.1.2
RegularGrid.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 "RegularGrid.h"
00023 #include <math.h>
00024 
00025 using namespace degate;
00026 
00027 void RegularGrid::precalc_steps() {
00028         grid_offsets.clear();
00029         if(distance > 0) {
00030                 for(double i = min; i < max; i += distance) {
00031                         grid_offsets.push_back(lround(i));
00032                 }
00033                 grid_offsets.sort();
00034         }
00035 }
00036 
00037 int RegularGrid::snap_to_grid(int pos) const {
00038 
00039         if(pos <= min) return min;
00040         else if(pos >= max) return max;
00041     else {
00042                 if(distance == 0) return pos;
00043 
00044                 unsigned int grid_coord_x_lo = (unsigned int)((pos - min) / distance);
00045                 unsigned int grid_coord_x_hi = grid_coord_x_lo + 1;
00046 
00047                 if( (grid_coord_x_hi * distance + min - pos) < (pos - (grid_coord_x_lo * distance + min)))
00048                         return (int)(grid_coord_x_hi * distance + min);
00049                 else  return (int)(grid_coord_x_lo * distance + min);
00050     }
00051 
00052 }