Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SketchDataRoot.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 
00003 #ifndef INCLUDED_SketchDataRoot_h
00004 #define INCLUDED_SketchDataRoot_h
00005 
00006 #include <string>
00007 
00008 #include "Shared/ProjectInterface.h"
00009 
00010 #include "SketchTypes.h"
00011 #include "Point.h"
00012 
00013 namespace DualCoding {
00014 
00015 class BaseData;
00016 class ShapeRoot;
00017 class ShapeSpace;
00018 class SketchSpace;
00019 
00020 //! Parent class for SketchData<T>
00021 class SketchDataRoot {
00022 private:
00023   //! The SketchSpace that owns the pool containing this SketchData object.
00024   SketchSpace *space;
00025   
00026   //! Name of this sketch.
00027   std::string name;
00028 
00029   //! Sketch-specific integer ID, for unique identification
00030   int id;
00031 
00032   //! Integer ID of the "parent" Sketch, 0 if no parent; used in GUI
00033   int parentId;
00034 
00035   //! Reference count for the sketch. When SketchPool detects this as 0, it may reuse it.
00036   int refcount;
00037 
00038   //! If true, don't garbage collect this sketch when refcount is zero, so it can still be retrieved by GET_SKETCH
00039   bool retained;
00040 
00041   //! True if the Sketch is currently viewable via the SketchGUI.
00042   bool viewable;
00043 
00044   //! Last time this sketch was included in a sketch list sent to the SketchGUI
00045   int refreshTag;
00046 
00047   //! True if we've tried to clear this sketch but the SketchGUI was looking at it
00048   bool clearPending;
00049 
00050   //! Color to use for displaying this sketch.  
00051   //! Only meaningful for Sketch<bool> but info is preserved for inheritance by children
00052   //! or in case we coerce bool to int or float and then back to bool.
00053   rgb color;
00054 
00055   //! Which color map to use; default is to use the robot's own color table.
00056   //! Other tables are used for displaying grayscale images, or scaled quantities like
00057   //! distance or area using a continuous color scale from red to blue.
00058   ColorMapType_t colormap;
00059 
00060   template<typename T> friend class SketchPool;
00061   template<typename T> friend class Sketch;
00062   template<typename T> friend class SketchData;
00063   friend class SketchRoot;
00064 
00065 public:
00066   SketchDataRoot(SketchSpace *s) : 
00067     space(s), name(), id(0), parentId(0), 
00068     refcount(0), retained(false), viewable(false), refreshTag(0), clearPending(false),
00069     color((ProjectInterface::getNumColors() != -1U) ? ProjectInterface::getColorRGB(1) : rgb(0,0,255)), // color 0 is invalid, so use color 1 as default, or blue if colors aren't loaded yet
00070     colormap(segMap) {}
00071 
00072   virtual ~SketchDataRoot() {};
00073 
00074   //! Returns the SketchSpace that owns the pool containing this SketchData object.
00075   SketchSpace& getSpace() const { return *space; }
00076 
00077   //! Returns the ShapeSpace associated with the SketchSpace for this Sketch
00078   ShapeSpace& getDualSpace() const;
00079 
00080   //! Return the id of this sketch
00081   int getId() const { return id; }
00082 
00083   //! Return the id of this sketch's parent; value is zero for parentless sketches
00084   int getParentId() const { return parentId; }
00085 
00086   //! Set the parent id of a sketch; normally used only inside visops functions
00087   void setParentId(int const _id) { parentId = _id; }
00088 
00089   //! Return the id of the closest viewable ancestor; will be the sketch's own id if it's viewable
00090   int getViewableId() const { return (isViewable() ? getId() : getParentId()); }
00091 
00092   //! Returns true if the sketch is currently viewable by the SketchGUI
00093   bool isViewable() const { return viewable; }
00094 
00095   //! Sets whether the sketch is viewabile by the SketchGUI
00096   void setViewable(bool const v=true) { viewable=v; }
00097 
00098   //! Sets or clears the retain bit, which prevents a sketch from being reclaimed even if its reference count goes to zero; such a sketch may still be retrieved via GET_SKETCH
00099   void retain(bool const r=true) { retained=r; }
00100 
00101   //! Returns true if the sketch is currently retained
00102   bool isRetained() const { return retained; }
00103 
00104   //! Returns the reference count of the sketch; should always be positive in user code
00105   int getRefcount() const { return refcount; }
00106 
00107   //! Returns the current color assigned to the sketch; only used for rendering Sketch<bool>
00108   rgb getColor() const { return color; }
00109 
00110   //@{
00111   //! Set the color of the sketch
00112   void setColor(const rgb &_color) { color = _color; }
00113   void setColor(const std::string &colorname);
00114   void setColor(const color_index cindex);
00115   //@}
00116 
00117   //! Return the sketch's current colormap setting
00118   ColorMapType_t getColorMap() const { return colormap; }
00119 
00120   //! Change the colormap of a sketch
00121   void setColorMap(const ColorMapType_t _map) { colormap = _map; }
00122 
00123   //! Return the name of a sketch as a string
00124   const std::string& getName() const { return name; }
00125 
00126   //! Change the name of a sketch.  Note that since the name is actually stored in the SketchData object, all Sketch objects pointing to this sketch will be affected.
00127   void setName(const std::string &_name) { name = _name; }
00128 
00129   //! Return the type of data stored in this sketch.  Virtual function overridden by each SketchData<T> class.
00130   virtual SketchType_t getType() const=0;
00131 
00132   //! Make a sketch viewable and change its name; used by NEW_SKETCH macro
00133   void V(std::string const &_name="");
00134 
00135   //! Make a sketch non-viewable and change its name; used by NEW_SKETCH_N macro
00136   void N(std::string const &_name="");
00137 
00138   //@{
00139   //! Set parentId and inherit color and colormap properties from a parent sketch
00140   void inheritFrom(const SketchDataRoot &parent);
00141   void inheritFrom(const ShapeRoot &parent);
00142   void inheritFrom(const BaseData &parent);
00143   //@}
00144 
00145   //@{
00146   //! Width and height of sketches in this space.
00147   const unsigned int getWidth() const;
00148   const unsigned int getHeight() const;
00149   const unsigned int getNumPixels() const;
00150   //@}
00151 
00152   //! X coordinate encoded by sketch index
00153   int indexX(int index) { return index % getWidth(); }
00154 
00155   //! Y coordinate encoded by sketch index
00156   int indexY(int index) { return index / getWidth(); }
00157 
00158   //! Convert an index to a Point, using TmatInv
00159   Point indexPoint(const int index);
00160 
00161   //! Converts (x,y) into a sketch index
00162   int indexOf(int x, int y) { return y*getWidth() + x; }
00163 
00164   //! Save a sketch to a buffer; used in the SketchGUI interface
00165   virtual unsigned int saveBuffer(char buf[], unsigned int avail) const;
00166 
00167   virtual size_t savePixels(char buf[], size_t avail) const =0; //!< handle copying pixels to buffer
00168   
00169 private:
00170   SketchDataRoot(const SketchDataRoot&); //!< never call this
00171   SketchDataRoot& operator=(const SketchDataRoot&); //!< never call this
00172 };
00173 
00174 } // namespace
00175 
00176 #endif

DualCoding 5.1CVS
Generated Mon May 9 04:56:27 2016 by Doxygen 1.6.3