Bridges-C++  3.1.1
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 
44  template <typename E> class Element {
45  //Used for access to generateJSON() and for links manipulation
46  template <typename K, typename E1, typename E2> friend class GraphAdjList;
47  template <typename K, typename E1, typename E2> friend class GraphAdjMatrix;
48  template <typename K> friend class Array;
49  template <typename K> friend class Array1D;
50  template <typename K> friend class Array2D;
51  template <typename K> friend class Array3D;
52 
53  private:
54  bool debug() const {
55  return false;
56  }
61  static const unordered_map<const Shape, const string, hash<int>>& ShapeNames() {
62 
63  static std::unordered_map<const Shape, const string, hash<int>> sn = {
64  {CIRCLE, "circle"},
65  {SQUARE, "square"},
66  {DIAMOND, "diamond"},
67  {CROSS, "cross"},
68  {TRIANGLE, "triangle"},
69  {WYE, "wye"},
70  {STAR, "star"}
71  };
72  return sn;
73  }
74 
75 
76 
77  //this element's label
78  string label;
79  // appl. specific data stored with element
80  E value = E();
81  // this element's visualizer
82  ElementVisualizer* elvis;
83 
84  protected:
85 
86  // this element's collection of links
87  unordered_map<Element*, LinkVisualizer> links;
88 
89  public:
97  explicit Element(const E& val = E(), const string& lab = string()) :
98  label(lab), value(val) {
99  elvis = new ElementVisualizer;
100  }
101 
108  Element(const Element& e)
109  : label(e.label), value(e.value), elvis(new ElementVisualizer(*(e.elvis))), links(e.links) {
110  }
111 
112  Element& operator= (const Element& e) {
113  this->label = e.label;
114  this->value = e.value;
115  *(this->elvis) = *(e.elvis);
116  this->links = e.links;
117  return *this;
118  }
119 
120  E& operator= (E const& e) {
121  this->value = e;
122  return this->value;
123  }
124 
128  virtual ~Element() {
129  delete elvis; // removes the visualizer
130  }
131 
137  return elvis;
138  }
145  return elvis;
146  }
155  if (links.find(const_cast<Element*>(el)) != links.end()) {
156  return &(links.at(const_cast<Element*>(el)));
157  }
158  if (debug())
159  cerr << "Element " << label << " not linked to Element "
160  << el->getLabel() << ", returning NULL" << endl;
161  return nullptr;
162  }
163 
171  return const_cast<Element*> (this)->getLinkVisualizer(el);
172  }
173 
178  string const & getLabel() const {
179  return label;
180  }
181 
187  void setLabel(const string& lab) {
188  label = lab;
189  }
190 
195  E const & getValue() const {
196  return value;
197  }
198 
203  E & getValue() {
204  return value;
205  }
206 
212  void setValue(const E& val) {
213  value = val;
214  }
215 
216  protected:
221  virtual const string getElementRepresentation() const {
223  //write out ElementVisualizer properties
224 
225  // first check if location is set and needs to be included
226  string loc_str = "";
227  if ( (elvis->getLocationX() != INFINITY) &&
228  (elvis->getLocationY() != INFINITY) ) {
229  loc_str = QUOTE + "location" + QUOTE + COLON +
230  OPEN_BOX +
231  JSONencode(elvis->getLocationX()) + COMMA +
232  JSONencode(elvis->getLocationY()) +
233  CLOSE_BOX + COMMA;
234  }
235  return OPEN_CURLY +
236  QUOTE + "color" + QUOTE + COLON + elvis->getColor().getCSSRepresentation()
237  + COMMA +
238  loc_str +
239  QUOTE + "shape" + QUOTE + COLON + QUOTE +
240  ShapeNames().at(elvis->getShape()) + QUOTE + COMMA +
241  QUOTE + "size" + QUOTE + COLON +
242  JSONencode(elvis->getSize()) + COMMA +
243  QUOTE + "name" + QUOTE + COLON + JSONencode( label) +
244  CLOSE_CURLY;
245  }
256  static const string getLinkRepresentation(const LinkVisualizer& lv,
257  const string& src, const string& dest) {
259 
260 
261  //write out LinkVisualizer properties
262  return OPEN_CURLY +
263  QUOTE + "color" + QUOTE + COLON + lv.getColor().getCSSRepresentation()
264  + COMMA +
265  (!lv.getLabel().empty() ?
266  (QUOTE + "label" + QUOTE + COLON +
267  JSONencode( lv.getLabel()) + COMMA) : "") +
268  QUOTE + "thickness" + QUOTE + COLON +
269  JSONencode(lv.getThickness()) + COMMA +
270  QUOTE + "source" + QUOTE + COLON + JSONencode(src) + COMMA +
271  QUOTE + "target" + QUOTE + COLON + JSONencode(dest) +
272  CLOSE_CURLY;
273  }
274  public:
280  void setSize(const double& sz) {
281  elvis->setSize(sz);
282  }
283 
289  double getSize() const {
290  return elvis->getSize();
291  }
297  void setColor(const Color& col) {
298  elvis->setColor(col);
299  }
305  void setColor(const string col) {
306  elvis->setColor(col);
307  }
308 
314  Color getColor() const {
315  return elvis->getColor();
316  }
317 
323  void setOpacity(double opacity) {
324  elvis->setOpacity(opacity);
325  }
326 
332  double getOpacity() {
333  return elvis->getOpacity();
334  }
342  void setShape(const Shape& shp) {
343  elvis->setShape(shp);
344  }
351  Shape getShape() const {
352  return elvis->getShape();
353  }
360  void setLocation(const double& locX, const double& locY) {
361  elvis->setLocation(locX, locY);
362  }
363 
368  double getLocationX() const {
369  return elvis->getLocationX();
370  }
375  double getLocationY() const {
376  return elvis->getLocationY();
377  }
378  }; //end of Element class
379 
380  }
381 }//end of bridges namespace
382 
383 #endif
double getLocationY() const
Definition: ElementVisualizer.h:153
double getLocationX() const
Definition: ElementVisualizer.h:147
void setSize(const double &sz)
Sets size of the element.
Definition: Element.h:280
void setValue(const E &val)
Definition: Element.h:212
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:44
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:256
Element(const E &val=E(), const string &lab=string())
Definition: Element.h:97
Definition: ElementVisualizer.h:10
Definition: ElementVisualizer.h:10
void setOpacity(double opacity)
Definition: ElementVisualizer.h:104
E & getValue()
Definition: Element.h:203
const string COLON
Definition: DataStructure.h:51
STL namespace.
E const & getValue() const
Definition: Element.h:195
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:351
string const & getLabel() const
Definition: Element.h:178
Definition: ElementVisualizer.h:10
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:28
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)
Definition: Element.h:323
void setColor(const Color &col)
Set the color to "col".
Definition: ElementVisualizer.h:79
const string CLOSE_CURLY
Definition: DataStructure.h:53
double getOpacity()
Get opacity of element.
Definition: ElementVisualizer.h:114
virtual const string getElementRepresentation() const
Definition: Element.h:221
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:342
LinkVisualizer * getLinkVisualizer(const Element *el) const
Definition: Element.h:170
void setLocation(const double &locX, const double &locY)
Definition: ElementVisualizer.h:139
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:297
const string CLOSE_BOX
Definition: DataStructure.h:55
void setLocation(const double &locX, const double &locY)
Definition: Element.h:360
double getSize() const
Definition: Element.h:289
Color getColor() const
Get the current color of the element.
Definition: Element.h:314
void setSize(const double &sz)
Definition: ElementVisualizer.h:64
const string getCSSRepresentation() const
Definition: Color.h:404
A BRIDGES array type.
Definition: Array2D.h:43
const ElementVisualizer * getVisualizer() const
Definition: Element.h:144
A BRIDGES 1D array type.
Definition: Array1D.h:53
A BRIDGES array type.
Definition: Array3D.h:43
double getSize() const
Definition: ElementVisualizer.h:71
void setLabel(const string &lab)
Definition: Element.h:187
This class provides methods to represent adjacency matrix based graphs.
Definition: Element.h:20
Color getColor() const
Definition: ElementVisualizer.h:95
Definition: ElementVisualizer.h:10
Shape
Definition: ElementVisualizer.h:10
double getOpacity()
Definition: Element.h:332
ElementVisualizer * getVisualizer()
Definition: Element.h:136
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:87
Definition: ElementVisualizer.h:10
double getLocationY() const
Definition: Element.h:375
void setShape(const Shape &shp)
Set the shape of the element.
Definition: ElementVisualizer.h:122
const string COMMA
Definition: DataStructure.h:50
Shape getShape() const
Return the shape of the element.
Definition: ElementVisualizer.h:129
double getLocationX() const
Definition: Element.h:368
const string QUOTE
Definition: DataStructure.h:49
Element(const Element &e)
Definition: Element.h:108
void setColor(const string col)
Set the color by name.
Definition: Element.h:305
virtual ~Element()
Definition: Element.h:128
Definition: ElementVisualizer.h:10
Definition: ElementVisualizer.h:10
std::string JSONencode(const T &d)
Definition: JSONutil.h:37
LinkVisualizer * getLinkVisualizer(const Element *el)
Definition: Element.h:154