Bridges-C++ 3.5.0-dev2-1-ge3e57bf
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
9using namespace std;
10
11#include "DataStructure.h"
12#include "ElementVisualizer.h"
13#include "LinkVisualizer.h"
14#include <JSONutil.h>
15
16namespace 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
51 template <typename E> class Element {
52 //Used for access to generateJSON() and for links manipulation
53 template <typename K, typename E1, typename E2> friend class GraphAdjList;
54 template <typename K, typename E1, typename E2> friend class GraphAdjMatrix;
55 template <typename K> friend class Array;
56 template <typename K> friend class Array1D;
57 template <typename K> friend class Array2D;
58 template <typename K> friend class Array3D;
59
60 private:
61 bool debug() const {
62 return false;
63 }
68 static const unordered_map<const Shape, const string, hash<int >> & ShapeNames() {
69
70 static std::unordered_map<const Shape, const string, hash<int >> sn = {
71 {CIRCLE, "circle"},
72 {SQUARE, "square"},
73 {DIAMOND, "diamond"},
74 {CROSS, "cross"},
75 {TRIANGLE, "triangle"},
76 {WYE, "wye"},
77 {STAR, "star"}
78 };
79 return sn;
80 }
81
82 //this element's label
83 string label;
84 // appl. specific data stored with element
85 E value = E();
86 // this element's visualizer
87 ElementVisualizer* elvis;
88
89 protected:
90
91 // this element's collection of links
92 unordered_map<Element*, LinkVisualizer> links;
93
94 public:
102 explicit Element(const E& val = E(), const string& lab = string()) :
103 label(lab), value(val) {
104 elvis = new ElementVisualizer;
105 }
106
113 Element(const Element& e)
114 : label(e.label), value(e.value), elvis(new ElementVisualizer(*(e.elvis))), links(e.links) {
115 }
116
118 this->label = e.label;
119 this->value = e.value;
120 *(this->elvis) = *(e.elvis);
121 this->links = e.links;
122 return *this;
123 }
124
125 E& operator= (E const& e) {
126 this->value = e;
127 return this->value;
128 }
129
133 virtual ~Element() {
134 delete elvis; // removes the visualizer
135 }
136
142 return elvis;
143 }
150 return elvis;
151 }
163 if (links.find(const_cast<Element * >(el)) != links.end()) {
164 return &(links.at(const_cast<Element*>(el)));
165 }
166 if (debug())
167 cerr << "Element " << label << " not linked to Element "
168 << el->getLabel() << ", returning NULL" << endl;
169 return nullptr;
170 }
171
181 return const_cast<Element*> (this)->getLinkVisualizer(el);
182 }
183
188 string const & getLabel() const {
189 return label;
190 }
191
197 void setLabel(const string& lab) {
198 label = lab;
199 }
200
207 E const & getValue() const {
208 return value;
209 }
210
217 E & getValue() {
218 return value;
219 }
220
226 void setValue(const E& val) {
227 value = val;
228 }
229
230 protected:
235 virtual const string getElementRepresentation() const {
237 //write out ElementVisualizer properties
238
239 // first check if location is set and needs to be included
240 string loc_str = "";
241 if ( (elvis->getLocationX() != INFINITY) &&
242 (elvis->getLocationY() != INFINITY) ) {
243 loc_str = QUOTE + "location" + QUOTE + COLON +
244 OPEN_BOX +
245 JSONencode(elvis->getLocationX()) + COMMA +
246 JSONencode(elvis->getLocationY()) +
248 }
249 return OPEN_CURLY +
250 QUOTE + "color" + QUOTE + COLON + elvis->getColor().getCSSRepresentation()
251 + COMMA +
252 loc_str +
253 QUOTE + "shape" + QUOTE + COLON + QUOTE +
254 ShapeNames().at(elvis->getShape()) + QUOTE + COMMA +
255 QUOTE + "size" + QUOTE + COLON +
256 JSONencode(elvis->getSize()) + COMMA +
257 QUOTE + "name" + QUOTE + COLON + JSONencode( label) +
259 }
260 /*
261 virtual void getElementRepresentation(rapidjson::Document& d)
262 const {
263 using namespace rapidjson;
264 //write out ElementVisualizer properties
265
266 Value k, v;
267 d.SetObject();
268 Document::AllocatorType& allocator = d.GetAllocator();
269
270 Value el_obj(kObjectType);
271
272 // first check if location is set and needs to be included
273 if ( (elvis->getLocationX() != INFINITY) &&
274 (elvis->getLocationY() != INFINITY) ) {
275
276 Value loc_arr(kArrayType);
277 loc_arr.PushBack(v.SetDouble(elvis->getLocationX()),
278 allocator);
279 loc_arr.PushBack(v.SetDouble(elvis->getLocationY()),
280 allocator);
281 el_obj.AddMember("location", loc_arr, allocator);
282 }
283
284 // string col_rep = elvis->getColor().getCSSRepresentation();
285 // v.SetString(col_rep.c_str(), allocator);
286 Document d2;
287 elvis->getColor().getCSSRepresentation(d2);
288 el_obj.AddMember("color", d2["color"], allocator);
289 string s = ShapeNames().at(elvis->getShape());
290 v.SetString(s.c_str(), allocator);
291 el_obj.AddMember("shape", v, allocator);
292 el_obj.AddMember("size", v.SetDouble(elvis->getSize()), allocator);
293 v.SetString(label.c_str(), allocator);
294 el_obj.AddMember("name", v, allocator);
295
296 // put this into an element
297 d.AddMember ("element", el_obj, allocator);
298 }
299 */
310 static const string getLinkRepresentation(
311 const LinkVisualizer& lv,
312 const string& src, const string& dest) {
314
315 //write out LinkVisualizer properties
316 return OPEN_CURLY +
317 QUOTE + "color" + QUOTE + COLON + lv.getColor().getCSSRepresentation()
318 + COMMA +
319 (!lv.getLabel().empty() ?
320 (QUOTE + "label" + QUOTE + COLON +
321 JSONencode( lv.getLabel()) + COMMA) : "") +
322 QUOTE + "thickness" + QUOTE + COLON +
324 QUOTE + "source" + QUOTE + COLON + JSONencode(src) + COMMA +
325 QUOTE + "target" + QUOTE + COLON + JSONencode(dest) +
327 }
329 const LinkVisualizer& lv,
330 const string& src, const string& dest,
331 rapidjson::Document& d) {
332
333 using namespace rapidjson;
334 Document::AllocatorType& allocator = d.GetAllocator();
335 d.SetObject();
336 Value lv_obj, v, v2;
337 lv_obj.SetObject();
338
339 string col_str = lv.getColor().getCSSRepresentation();
340 v.SetString(col_str.c_str(), allocator);
341 lv_obj.AddMember("color", v, allocator);
342 if (!lv.getLabel().empty()) {
343 v.SetString(lv.getLabel().c_str(), allocator);
344 lv_obj.AddMember("label", v, allocator);
345 }
346 lv_obj.AddMember("thickness", v.SetDouble(lv.getThickness()), allocator);
347 v.SetString(src.c_str(), allocator);
348 lv_obj.AddMember("source", v, allocator);
349 v2.SetString(dest.c_str(), allocator);
350 lv_obj.AddMember("target", v2, allocator);
351 d.AddMember("link", lv_obj, allocator);
352 }
353 public:
359 void setSize(const double& sz) {
360 elvis->setSize(sz);
361 }
362
368 double getSize() const {
369 return elvis->getSize();
370 }
376 void setColor(const Color& col) {
377 elvis->setColor(col);
378 }
385 void setColor(const string col) {
386 elvis->setColor(col);
387 }
388
394 Color getColor() const {
395 return elvis->getColor();
396 }
397
403 void setOpacity(double opacity) {
404 elvis->setOpacity(opacity);
405 }
406
412 double getOpacity() {
413 return elvis->getOpacity();
414 }
422 void setShape(const Shape& shp) {
423 elvis->setShape(shp);
424 }
432 Shape getShape() const {
433 return elvis->getShape();
434 }
441 void setLocation(const double& locX, const double& locY) {
442 elvis->setLocation(locX, locY);
443 }
444
449 double getLocationX() const {
450 return elvis->getLocationX();
451 }
456 double getLocationY() const {
457 return elvis->getLocationY();
458 }
459 }; //end of Element class
460
461 }
462}//end of bridges namespace
463
464#endif
A BRIDGES 1D array type.
Definition: Array1D.h:53
A BRIDGES array type.
Definition: Array2D.h:42
A BRIDGES array type.
Definition: Array3D.h:42
The foundation of BRIDGES array types. It is not meant to be used directly by students.
Definition: Array.h:21
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:50
const string getCSSRepresentation() const
Definition: Color.h:403
This is the fundamental building block for all data structures in BRIDGES.
Definition: Element.h:51
static const string getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest)
Definition: Element.h:310
ElementVisualizer * getVisualizer()
Get the element visualizer object.
Definition: Element.h:141
void setColor(const Color &col)
Set the color of the Element.
Definition: Element.h:376
void setShape(const Shape &shp)
Set the shape of the element.
Definition: Element.h:422
Element(const E &val=E(), const string &lab=string())
Definition: Element.h:102
Element & operator=(const Element &e)
Definition: Element.h:117
void setValue(const E &val)
Sets generic object to "val".
Definition: Element.h:226
virtual const string getElementRepresentation() const
Gets the JSON string of the element representation.
Definition: Element.h:235
void setSize(const double &sz)
Sets size of the element.
Definition: Element.h:359
void setColor(const string col)
Set the color by name.
Definition: Element.h:385
void setLabel(const string &lab)
Sets label of this element.
Definition: Element.h:197
string const & getLabel() const
Gets the label of this element.
Definition: Element.h:188
LinkVisualizer * getLinkVisualizer(const Element *el) const
Returns the LinkVisualizer of element.
Definition: Element.h:180
E const & getValue() const
Gets the object held in the generic object E.
Definition: Element.h:207
double getLocationX() const
Gets the X coordinate of the location.
Definition: Element.h:449
double getLocationY() const
Gets the Y coordinate of the location.
Definition: Element.h:456
Element(const Element &e)
Definition: Element.h:113
E & getValue()
Gets the object held in the generic object E.
Definition: Element.h:217
double getSize() const
Get element size.
Definition: Element.h:368
LinkVisualizer * getLinkVisualizer(const Element *el)
Returns the LinkVisualizer of element.
Definition: Element.h:162
const ElementVisualizer * getVisualizer() const
Get the element visualizer object - constant version.
Definition: Element.h:149
static void getLinkRepresentation(const LinkVisualizer &lv, const string &src, const string &dest, rapidjson::Document &d)
Definition: Element.h:328
unordered_map< Element *, LinkVisualizer > links
Definition: Element.h:92
void setOpacity(double opacity)
Set opacity of element - use the 4th color component.
Definition: Element.h:403
Shape getShape() const
Returns the shape of the element.
Definition: Element.h:432
double getOpacity()
Definition: Element.h:412
virtual ~Element()
Definition: Element.h:133
Color getColor() const
Get the current color of the element.
Definition: Element.h:394
void setLocation(const double &locX, const double &locY)
Sets the location attributes of an element.
Definition: Element.h:441
This class maintains the visual properties of the a Bridges element.
Definition: ElementVisualizer.h:31
This class provides methods to represent adjacency list based graphs.
Definition: GraphAdjList.h:110
This class provides methods to represent adjacency matrix based graphs.
Definition: GraphAdjMatrix.h:37
std::string JSONencode(const T &d)
Definition: JSONutil.h:38
Shape
Definition: ElementVisualizer.h:10
@ DIAMOND
Definition: ElementVisualizer.h:10
@ CIRCLE
Definition: ElementVisualizer.h:10
@ SQUARE
Definition: ElementVisualizer.h:10
@ STAR
Definition: ElementVisualizer.h:10
@ WYE
Definition: ElementVisualizer.h:10
@ TRIANGLE
Definition: ElementVisualizer.h:10
@ CROSS
Definition: ElementVisualizer.h:10
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 COLON
Definition: DataStructure.h:52
const string OPEN_BOX
Definition: DataStructure.h:55
const string COMMA
Definition: DataStructure.h:51
const string OPEN_CURLY
Definition: DataStructure.h:53
const string CLOSE_BOX
Definition: DataStructure.h:56
const string CLOSE_CURLY
Definition: DataStructure.h:54
const string QUOTE
Definition: DataStructure.h:50