degate  0.1.2
Layer.h
Go to the documentation of this file.
00001 /* -*-c++-*-
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 #ifndef __LAYER_H__
00024 #define __LAYER_H__
00025 
00026 #include "globals.h"
00027 
00028 #include "Rectangle.h"
00029 #include "QuadTree.h"
00030 #include "PlacedLogicModelObject.h"
00031 
00032 #include "Image.h"
00033 #include "ScalingManager.h"
00034 
00035 #include <set>
00036 #include <stdexcept>
00037 
00038 namespace degate {
00039 
00040   /**
00041    * Representation of a chip layer.
00042    */
00043   class Layer : public DeepCopyable {
00044 
00045     friend class LogicModel;
00046 
00047   public:
00048 
00049     /**
00050      * Enums to declare the type of a layer.
00051      */
00052 
00053     enum LAYER_TYPE {
00054       UNDEFINED = 0,
00055       METAL = 1,
00056       LOGIC = 2,
00057       TRANSISTOR = 3
00058     };
00059 
00060     typedef std::shared_ptr<PlacedLogicModelObject> quadtree_element_type;
00061 
00062     typedef region_iterator<quadtree_element_type> qt_region_iterator;
00063     typedef qt_region_iterator object_iterator;
00064 
00065   private:
00066 
00067     QuadTree<quadtree_element_type> quadtree;
00068 
00069     LAYER_TYPE layer_type;
00070 
00071     layer_position_t layer_pos;
00072 
00073     std::shared_ptr<ScalingManager<BackgroundImage> > scaling_manager;
00074 
00075     // store shared pointers to objects, that belong to the layer
00076     typedef std::map<object_id_t, PlacedLogicModelObject_shptr> object_collection;
00077     object_collection objects;
00078 
00079     bool enabled;
00080     std::string description;
00081 
00082     layer_id_t layer_id;
00083 
00084   protected:
00085 
00086     /**
00087      * Add an logic model object into this layer.
00088      * @throw DegateRuntimeException Is thrown if the object
00089      *   cannot be inserted into the quadtree.
00090      * @throw DegateLogicException
00091      */
00092 
00093     void add_object(std::shared_ptr<PlacedLogicModelObject> o);
00094 
00095 
00096     /**
00097      * Remove object from layer.
00098      * @throw DegateRuntimeException Is thrown if the object
00099      *   cannot be removed from the quadtree.
00100      */
00101 
00102     void remove_object(std::shared_ptr<PlacedLogicModelObject> o);
00103 
00104   public:
00105 
00106 
00107     /**
00108      * Create a new logic model layer.
00109      */
00110 
00111     Layer(BoundingBox const & bbox, LAYER_TYPE _layer_type = Layer::UNDEFINED);
00112 
00113     /**
00114      * Create a new logic model layer.
00115      */
00116 
00117     Layer(BoundingBox const & bbox, LAYER_TYPE _layer_type,
00118           BackgroundImage_shptr img);
00119 
00120     /**
00121      * Destruct a layer.
00122      */
00123 
00124     virtual ~Layer();
00125     
00126     //@{
00127     DeepCopyable_shptr cloneShallow() const;
00128     void cloneDeepInto(DeepCopyable_shptr destination, oldnew_t *oldnew) const;
00129     //@}
00130 
00131     /**
00132      * Get the width of a layer.
00133      */
00134 
00135     unsigned int get_width() const;
00136 
00137     /**
00138      * Get the height of a layer.
00139      */
00140 
00141     unsigned int get_height() const;
00142 
00143     /**
00144      * Get the bounding box for a layer.
00145      */
00146     BoundingBox const& get_bounding_box() const;
00147 
00148     /**
00149      * Get layer type of this layer as human readable string, e.g. the string
00150      * "metal" for a layer of type Layer::METAL .
00151      */
00152 
00153     const std::string get_layer_type_as_string() const;
00154 
00155     /**
00156      * Get a layer type type as human readable string.
00157      */
00158     static const std::string get_layer_type_as_string(LAYER_TYPE _layer_type);
00159 
00160     /**
00161      * Parse a layer type indicating string.
00162      * @exception DegateRuntimeException This exception is thrown if the string
00163      *   cannot be parsed.
00164      */
00165     static LAYER_TYPE get_layer_type_from_string(std::string const& layer_type_str);
00166 
00167 
00168     /**
00169      * Get layer type.
00170      */
00171 
00172     LAYER_TYPE get_layer_type() const;
00173 
00174     /**
00175      * Set layer type.
00176      */
00177 
00178     void set_layer_type(LAYER_TYPE _layer_type);
00179 
00180 
00181     /**
00182      * Check if a layer has logic model objects or not.
00183      */
00184 
00185     bool is_empty() const;
00186 
00187 
00188     /**
00189      * Get the position of the layer within the layer stack.
00190      */
00191 
00192     layer_position_t get_layer_pos() const;
00193 
00194 
00195     /**
00196      * Get an iterator to iterate over all placed objects.
00197      */
00198 
00199     object_iterator objects_begin();
00200 
00201     /**
00202      * Get an end iterator.
00203      */
00204 
00205     object_iterator objects_end();
00206 
00207     /**
00208      * Get an iterator to iterate over a region.
00209      */
00210 
00211     qt_region_iterator region_begin(int min_x, int max_x, int min_y, int max_y);
00212 
00213     /**
00214      * Get an iterator to iterate over a region.
00215      */
00216 
00217     qt_region_iterator region_begin(BoundingBox const & bbox);
00218 
00219     /**
00220      * Get an end marker for region iteration.
00221      */
00222 
00223     qt_region_iterator region_end();
00224 
00225 
00226     /**
00227      * Set the background image for a layer.
00228      * Calling this method will also initialize the ScalingManager, that
00229      * handles multiple prescaled versions of the background image.
00230      * The ScalingManager will write the prescaled images into subdirectories
00231      * of the the background image.
00232      */
00233 
00234     void set_image(BackgroundImage_shptr img);
00235 
00236 
00237     /**
00238      * Get the background image.
00239      * @return Returns a shared pointer to the background image.
00240      * @exception DegateLogicException If you did not set the background image, then this
00241      *   exception is thrown.
00242      * @see set_image()
00243      */
00244 
00245     BackgroundImage_shptr get_image();
00246 
00247     /**
00248      * Get the directory name for the image, that represents the
00249      * background image of the layer.
00250      * @exception DegateLogicException If you did not set the background image, then this
00251      *   exception is thrown.
00252      */
00253 
00254     std::string get_image_filename() const;
00255 
00256     /**
00257      * Check if the layer has a background image.
00258      */
00259 
00260     bool has_background_image() const;
00261 
00262 
00263     /**
00264      * Unset the background image.
00265      * This will destroy the image and it's scaling manager object and it will remove
00266      * the data from the project dir.
00267      * @exception DegateLogicException This excpetion is thrown if there is no background image.
00268      */
00269 
00270     void unset_image();
00271 
00272     /**
00273      * Get the scaling manager.
00274      * If you want to access the background image of a layer, that is the
00275      * chip surface depicting image, you have to ask for the ScalingMananger.
00276      * From the scaling mananger you will get the image.
00277      * @return Returns a shared pointer to the  scaling manager object.
00278      *   The pointer can be a NULL pointer. This is the case if you did not
00279      *   initialized it via set_image()
00280      * @see set_image()
00281      */
00282 
00283     ScalingManager_shptr get_scaling_manager();
00284 
00285     /**
00286      * Print the layer.
00287      */
00288     void print(std::ostream & os = std::cout);
00289 
00290     /**
00291      * Notify the layer that a shape of a logic model object changed.
00292      * This will adjust the quadtree.
00293      * @exception CollectionLookupException This exception is thrown if
00294      *    thetre is no object in the layer, that has this object ID.
00295      * @exception InvalidObjectIDException Is raised, if \p object_id
00296      *    has an invalid ID.
00297      */
00298 
00299     void notify_shape_change(object_id_t object_id);
00300 
00301     /**
00302      * Get an object at a specific position.
00303      * If multiple objects are placed at coordinate \p x, \p y, then the first
00304      * one is returned. But if there is a gate port, the port is returned.
00305      * @param x The x-position.
00306      * @param y The y-position.
00307      * @param max_distance It is possible to check for objects, which are
00308      *   not directly placed in a way, that \p x and \p y are within the shape
00309      *   of an object. Therefore the parameter \p max_distance specifies an
00310      *   allowed distance to the object.
00311      * @return If there is an object at the position, a shared pointer to
00312      *   it is returned. If there is no object, then a NULL pointer representation
00313      *   is returned.
00314      */
00315 
00316     PlacedLogicModelObject_shptr get_object_at_position(int x, int y, int max_distance = 0);
00317 
00318     /**
00319      * Check for placed objects in a region of type given by template param.
00320      * @return Returns true, if there is a an object of the specified type in the region.
00321      *   Else it returns false.
00322      */
00323 
00324     template<typename LogicModelObjectType>
00325     bool exists_type_in_region(unsigned int min_x, unsigned int max_x,
00326                                unsigned int min_y, unsigned int max_y) {
00327       for(Layer::qt_region_iterator iter = quadtree.region_iter_begin(min_x, max_x, min_y, max_y);
00328           iter != quadtree.region_iter_end(); ++iter) {
00329         
00330         if(std::dynamic_pointer_cast<LogicModelObjectType>(*iter) != NULL) {
00331           return true;
00332         }
00333       }
00334       return false;      
00335     }
00336 
00337 
00338     /**
00339      * Check for placed gates in a region and return the distance to
00340      * the boundary.
00341      * @return Returns the distance from \p x to the right boundary or
00342      *   from \p y to the bottom boundary depending on \p query_horizontal_distance.
00343      *   If there is no gate, this method returns 0.
00344      */
00345 
00346     unsigned int get_distance_to_gate_boundary(unsigned int x, unsigned int y,
00347                                                bool query_horizontal_distance = true,
00348                                                unsigned int width = 0,
00349                                                unsigned int height = 0);
00350 
00351 
00352     /**
00353      * Enable a layer.
00354      */
00355 
00356     void set_enabled(bool state = true);
00357 
00358     /**
00359      * Check if a layer is enabled.
00360      * Enabled means, that the layer is visible to the user.
00361      */
00362 
00363     bool is_enabled() const;
00364 
00365 
00366     /**
00367      * Get layer description.
00368      */
00369 
00370     std::string get_description() const;
00371 
00372 
00373     /**
00374      * Set layer description.
00375      */
00376 
00377     void set_description(std::string const& description);
00378 
00379 
00380     /**
00381      * Set layer position.
00382      */
00383     void set_layer_pos(layer_position_t pos) { layer_pos = pos; }
00384 
00385     /**
00386      * Set the layer ID. The layer ID is not an object ID. The only requirement is
00387      * that each layer has a unique and position-independend ID.
00388      */
00389 
00390     virtual void set_layer_id(layer_id_t lid) { layer_id = lid; }
00391 
00392     /**
00393      * Get the layer ID.
00394      */
00395 
00396     virtual layer_id_t get_layer_id() const { return layer_id; }
00397 
00398     /**
00399      * Check if the layer has a valid layer ID.
00400      */
00401 
00402     virtual bool has_valid_layer_id() const { return layer_id != 0; }
00403 
00404   };
00405 
00406 }
00407 
00408 #endif