Advertisement
Guest User

quadtree.h

a guest
Oct 20th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. /**
  2.  * @file quadtree.h
  3.  * quadtree class definition.
  4.  * @date Spring 2008
  5.  * @date Modified Summer 2014
  6.  */
  7.  
  8. #ifndef QUADTREE_H_
  9. #define QUADTREE_H_
  10.  
  11. #include <iostream>
  12. #include <cstdint>
  13. #include "epng.h"
  14.  
  15. namespace cs225
  16. {
  17.  
  18. /**
  19.  * A tree structure that is used to compress epng::png images.
  20.  */
  21. class quadtree
  22. {
  23.   public:
  24.      //The constructors and destrcutor
  25.      quadtree();
  26.      quadtree(const epng::png& source, uint64_t resolution);
  27.      quadtree(const quadtree& other);
  28.      quadtree(quadtree&& other);
  29.      ~quadtree() = default;
  30.  
  31.      /**
  32.       * Assignment operator
  33.       * @param The element to be assigned into this quadtree
  34.       * @return A reference to the current quadtree
  35.       */
  36.      quadtree& operator=(quadtree other);
  37.  
  38.      /**
  39.       * Access operator
  40.       * @param x-coordinate, y-coordinate of pixel to access
  41.       * @return Reference to the pixel at the coordinates
  42.       */
  43.      const epng::rgba_pixel& operator()(uint64_t x, uint64_t y) const;
  44.  
  45.      /**
  46.       * Replaces current object with d by d block from the source image
  47.       * @param The source image, the size of the block
  48.       * @return Void
  49.       */
  50.      void build_tree(const epng::png& source, uint64_t resolution);
  51.  
  52.      /**
  53.       * Returns underlying png object represented by the quadtree
  54.       * @param None
  55.       * @return A png object
  56.       */
  57.      epng::png decompress() const;
  58.  
  59.      /**
  60.       * Rotates the image clockwise by 90 degrees
  61.       * @param None
  62.       * @return Void
  63.       */
  64.      void rotate_clockwise();
  65.  
  66.      /**
  67.       * Compresses the image the quadtree represents
  68.       * @param The integer tolerance between two nodes
  69.       * @return Void
  70.       */
  71.      void prune(uint32_t tolerance);//TODO
  72.  
  73.      /**
  74.       * Gets the number of leaves in the tree if it were pruned
  75.       * @param The integer tolerance between two nodes
  76.       * @return Number of leaves
  77.       */
  78.      uint64_t pruned_size(uint32_t tolerance) const;//TODO
  79.  
  80.      /**
  81.       * Calculates and returns the minimum tolerance to gaurantee that no more than num_leaves remain upon pruning
  82.       * @param The number of leaves to remain
  83.       * @return Minimum tolerance to give the number of leaves specified
  84.       */
  85.      uint32_t ideal_prune(uint64_t num_leaves) const;//TODO
  86.  
  87.   private:
  88.     /**
  89.      * A simple class representing a single node of a quadtree.
  90.      */
  91.     class node
  92.     {
  93.       public:
  94.         //Constructors for the node class
  95.         node();
  96.         node(const node& other);
  97.  
  98.         /**
  99.         * helper function for the copy constructor
  100.         * @param A node class
  101.         * @return A reference to an epng::rgba_pixel
  102.         */
  103.         const epng::rgba_pixel& copy_nodes(const node& other);
  104.  
  105.         /**
  106.          * Helper function to clear an existing quadtree
  107.          * @param Reference to the node class
  108.          * @return Void
  109.          */
  110.         //void clear(node& subroot);
  111.  
  112.         std::unique_ptr<node> northwest;
  113.         std::unique_ptr<node> northeast;
  114.         std::unique_ptr<node> southwest;
  115.         std::unique_ptr<node> southeast;
  116.  
  117.         //The pixel stored as this node's "data"
  118.         epng::rgba_pixel element;
  119.     };
  120.  
  121.     //The root and # of nodes in the tree
  122.     std::unique_ptr<node> root_;
  123.     uint64_t size_;
  124.  
  125.     /**
  126.      * Helper function to clear an existing quadtree
  127.      * @param Reference to the node class
  128.      * @return Void
  129.      */
  130.     void clear(node& subroot);
  131.  
  132.      /**
  133.       * Helper function to swap the contents of two quadtrees
  134.       * @param The quadtree to swap with
  135.       * @return Void
  136.       */
  137.      void swap(quadtree& other);
  138.  
  139.      /**
  140.       * Helper function to access a pixel from the quadtree
  141.       * @param A subroot of the tree, a pixel, x-coordinate of pixel, y-coordinate of pixel, x-bound of block, y-bound of block
  142.       * @return Reference to the pixel at the coordinates
  143.       */
  144.      const epng::rgba_pixel& find_pixel(node& subroot, epng::rgba_pixel& temp, const uint64_t x, const uint64_t y,
  145.                                         uint64_t x_bound, uint64_t y_bound) const;
  146.  
  147.      /**
  148.       * Helper function to traverse tree and return the pixel at the leaves
  149.       * @param A subroot of the tree, the png image to make, x-coordinate of pixel, y-coordinate of pixel
  150.       * @return Reference to the pixel at the coordinates
  151.       */
  152.      const epng::png& get_pixels(const node& subroot, epng::png& out_png, uint64_t x, uint64_t y, uint64_t level) const;
  153.  
  154.      /**
  155.       * Helper function to build the quad tree utilizing a breadth-first like build algorithm
  156.       * @param The source image, the size of the block
  157.       * @return Void
  158.       */
  159.      void build_tree(const epng::png source, const uint64_t scope, node& subroot, uint64_t level, const uint64_t height,
  160.                      uint64_t x, uint64_t y, uint64_t iter_x, uint64_t iter_y, int& base);
  161.  
  162.      /**
  163.       * Rotates the image by 90 clockwise
  164.       * @param A reference to a node
  165.       * @return Void
  166.       */
  167.      void rotate_clockwise(node& subroot);
  168.  
  169. /**** Do not remove this line or copy its contents here! ****/
  170. #include "quadtree_given.h"
  171. };
  172. }
  173. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement