Bridges-C++  3.2.0
Bridges(C++API)
Element.h
Go to the documentation of this file.
1 #ifndef ELEMENT_H
2 
3 #define ELEMENT_H
4 
5 #include <unordered_set>
6 #include <unordered_map>
7 #include <cmath>
8 
9 using namespace std;
10 
11 #include "DataStructure.h"
12 #include "ElementVisualizer.h"
13 #include "LinkVisualizer.h"
14 #include <JSONutil.h>
15 
16 namespace bridges {
17  namespace datastructure {
18  // forward Declarations
19  template <typename K, typename E1, typename E2> class GraphAdjList;
20  template <typename K, typename E1, typename E2> class GraphAdjMatrix;
21 
52  template <typename E> class Element {
53  //Used for access to generateJSON() and for links manipulation
54  template <typename K, typename E1, typename E2> friend class GraphAdjList;
55  template <typename K, typename E1, typename E2> friend class GraphAdjMatrix;
56  template <typename K> friend class Array;
57  template <typename K> friend class Array1D;
58  template <typename K> friend class Array2D;
59  template <typename K> friend class Array3D;
60 
61  private:
62  bool debug() const {
63  return false;
64  }
69  static const unordered_map<const Shape, const string, hash<int>>& ShapeNames() {
70 
71  static std::unordered_map<const Shape, const string, hash<int>> sn = {
72  {CIRCLE, "circle"},
73  {SQUARE, "square"},
74  {DIAMOND, "diamond"},
75  {CROSS, "cross"},
76  {TRIANGLE, "triangle"},
77  {WYE, "wye"},
78  {STAR, "star"}
79  };
80  return sn;
81  }
82 
83 
84 
85  //this element's label
86  string label;
87  // appl. specific data stored with element
88  E value = E();
89  // this element's visualizer
90  ElementVisualizer* elvis;
91 
92  protected:
93 
94  // this element's collection of links
95  unordered_map<Element*, LinkVisualizer> links;
96 
97  public:
105  explicit Element(const E& val = E(), const string& lab = string()) :
106  label(lab), value(val) {
107  elvis = new ElementVisualizer;
108  }
109 
116  Element(const Element& e)
117  : label(e.label), value(e.value), elvis(new ElementVisualizer(*(e.elvis))), links(e.links) {
118  }
119 
120  Element& operator= (const Element& e) {
121  this->label = e.label;
122  this->value = e.value;
123  *(this->elvis) = *(e.elvis);
124  this->links = e.links;
125  return *this;
126  }
127 
128  E& operator= (E const& e) {
129  this->value = e;
130  return this->value;
131  }
132 
136  virtual ~Element() {
137  delete elvis; // removes the visualizer
138  }
139 
145  return elvis;
146  }
153  return elvis;
154  }
166  if (links.find(const_cast<Element*>(el)) != links.end()) {
167  return &(links.at(const_cast<Element*>(el)));
168  }
169  if (debug())
170  cerr << "Element " << label << " not linked to Element "
171  << el->getLabel() << ", returning NULL" << endl;
172  return nullptr;
173  }
174 
184  return const_cast<Element*> (this)->getLinkVisualizer(el);
185  }
186 
191  string const & getLabel() const {
192  return label;
193  }
194 
200  void setLabel(const string& lab) {
201  label = lab;
202  }
203 
210  E const & getValue() const {
211  return value;
212  }
213 
220  E & getValue() {
221  return value;
222  }
223 
229  void setValue(const E& val) {
230  value = val;
231  }
232 
233  protected:
238  virtual const string getElementRepresentation() const {
240  //write out ElementVisualizer properties
241 
242  // first check if location is set and needs to be included
243  string loc_str = "";
244  if ( (elvis->getLocationX() != INFINITY) &&
245  (elvis->getLocationY() != INFINITY) ) {
246  loc_str = QUOTE + "location" + QUOTE + COLON +
247  OPEN_BOX +
248  JSONencode(elvis->getLocationX()) + COMMA +
249  JSONencode(elvis->getLocationY()) +
250  CLOSE_BOX + COMMA;
251  }
252  return OPEN_CURLY +
253  QUOTE + "color" + QUOTE + COLON + elvis->getColor().getCSSRepresentation()
254  + COMMA +
255  loc_str +
256  QUOTE + "shape" + QUOTE + COLON + QUOTE +
257  ShapeNames().at(elvis->getShape()) + QUOTE + COMMA +
258  QUOTE + "size" + QUOTE + COLON +
259  JSONencode(elvis->getSize()) + COMMA +
260  QUOTE + "name" + QUOTE + COLON + JSONencode( label) +
261  CLOSE_CURLY;
262  }
273  static const string getLinkRepresentation(const LinkVisualizer& lv,
274  const string& src, const string& dest) {
276 
277 
278  //write out LinkVisualizer properties
279  return OPEN_CURLY +
280  QUOTE + "color" + QUOTE + COLON + lv.getColor().getCSSRepresentation()
281  + COMMA +
282  (!lv.getLabel().empty() ?
283  (QUOTE + "label" + QUOTE + COLON +
284  JSONencode( lv.getLabel()) + COMMA) : "") +
285  QUOTE + "thickness" + QUOTE + COLON +
286  JSONencode(lv.getThickness()) + COMMA +
287  QUOTE + "source" + QUOTE + COLON + JSONencode(src) + COMMA +
288  QUOTE + "target" + QUOTE + COLON + JSONencode(dest) +
289  CLOSE_CURLY;
290  }
291  public:
297  void setSize(const double& sz) {
298  elvis->setSize(sz);
299  }
300 
306  double getSize() const {
307  return elvis->getSize();
308  }
314  void setColor(const Color& col) {
315  elvis->setColor(col);
316  }
323  void setColor(const string col) {
324  elvis->setColor(col);
325  }
326 
332  Color getColor() const {
333  return elvis->getColor();
334  }
335 
341  void setOpacity(double opacity) {
342  elvis->setOpacity(opacity);
343  }
344 
350  double getOpacity() {
351  return elvis->getOpacity();
352  }
360  void setShape(const Shape& shp) {
361  elvis->setShape(shp);
362  }
370  Shape getShape() const {
371  return elvis->getShape();
372  }
379  void setLocation(const double& locX, const double& locY) {
380  elvis->setLocation(locX, locY);
381  }
382 
387  double getLocationX() const {
388  return elvis->getLocationX();
389  }
394  double getLocationY() const {
395  return elvis->getLocationY();
396  }
397  }; //end of Element class
398 
399  }
400 }//end of bridges namespace
401 
402 #endif
double getLocationY() const
get Y coordinate of element location
Definition: ElementVisualizer.h:173
double getLocationX() const
get X coordinate of element location
Definition: ElementVisualizer.h:166
void setSize(const double &sz)
Sets size of the element.
Definition: Element.h:297
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:229
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:52
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:273
Element(const E &val=E(), const string &lab=string())
Definition: Element.h:105
Definition: ElementVisualizer.h:10
Definition: ElementVisualizer.h:10
void setOpacity(double opacity)
Set opacity of element.
Definition: ElementVisualizer.h:121
E & getValue()
Gets the object held in the generic object E.
Definition: Element.h:220
const string COLON
Definition: DataStructure.h:51
STL namespace.
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:210
This class provides methods to represent adjacency list based graphs.
Definition: Element.h:19
Shape getShape() const
Returns the shape of the element.
Definition: Element.h:370
string const & getLabel() const
Gets the label of this element.
Definition: Element.h:191
Definition: ElementVisualizer.h:10
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
const string OPEN_BOX
Definition: DataStructure.h:54
void setOpacity(double opacity)
Set opacity of element - use the 4th color component.
Definition: Element.h:341
void setColor(const Color &col)
Set the color to "col".
Definition: ElementVisualizer.h:94
const string CLOSE_CURLY
Definition: DataStructure.h:53
double getOpacity()
Get opacity of element.
Definition: ElementVisualizer.h:131
virtual const string getElementRepresentation() const
Gets the JSON string of the element representation.
Definition: Element.h:238
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
void setShape(const Shape &shp)
Set the shape of the element.
Definition: Element.h:360
LinkVisualizer * getLinkVisualizer(const Element *el) const
Returns the LinkVisualizer of element.
Definition: Element.h:183
void setLocation(const double &locX, const double &locY)
Definition: ElementVisualizer.h:157
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string OPEN_CURLY
Definition: DataStructure.h:52
void setColor(const Color &col)
Set the color of the Element.
Definition: Element.h:314
const string CLOSE_BOX
Definition: DataStructure.h:55
void setLocation(const double &locX, const double &locY)
Sets the location attributes of an element.
Definition: Element.h:379
double getSize() const
Get element size.
Definition: Element.h:306
Color getColor() const
Get the current color of the element.
Definition: Element.h:332
void setSize(const double &sz)
Definition: ElementVisualizer.h:79
const string getCSSRepresentation() const
Definition: Color.h:404
A BRIDGES array type.
Definition: Array2D.h:43
const ElementVisualizer * getVisualizer() const
Get the element visualizer object - constant version.
Definition: Element.h:152
A BRIDGES 1D array type.
Definition: Array1D.h:53
A BRIDGES array type.
Definition: Array3D.h:43
double getSize() const
Definition: ElementVisualizer.h:86
void setLabel(const string &lab)
Sets label of this element.
Definition: Element.h:200
This class provides methods to represent adjacency matrix based graphs.
Definition: Element.h:20
Color getColor() const
Return the element color.
Definition: ElementVisualizer.h:110
Definition: ElementVisualizer.h:10
Shape
Definition: ElementVisualizer.h:10
double getOpacity()
Definition: Element.h:350
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:144
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:95
Definition: ElementVisualizer.h:10
double getLocationY() const
Gets the Y coordinate of the location.
Definition: Element.h:394
void setShape(const Shape &shp)
Set the shape of the element.
Definition: ElementVisualizer.h:140
const string COMMA
Definition: DataStructure.h:50
Shape getShape() const
Return the shape of the element.
Definition: ElementVisualizer.h:147
double getLocationX() const
Gets the X coordinate of the location.
Definition: Element.h:387
const string QUOTE
Definition: DataStructure.h:49
Element(const Element &e)
Definition: Element.h:116
void setColor(const string col)
Set the color by name.
Definition: Element.h:323
virtual ~Element()
Definition: Element.h:136
Definition: ElementVisualizer.h:10
Definition: ElementVisualizer.h:10
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:165