Bridges-C++  3.1.1
Bridges(C++API)
Symbol.h
Go to the documentation of this file.
1 
2 #include <string>
3 #include <vector>
4 #include <cmath>
5 
6 using namespace std;
7 
8 #include "DataStructure.h"
9 #include "Color.h"
10 
11 #ifndef SYMBOL_H
12 
13 #define SYMBOL_H
14 
15 
16 namespace bridges {
17  namespace datastructure {
18 
32  class Symbol {
33 
34  private:
35 
36  int identifier;
37  string name = string();
38 
39  string shape_type = "circle"; // rect, circle, polygon, label
40 
41  // specify default attributes
42  // defaults are not sent through JSON
43 
44  float default_location[2] = {0.0f, 0.0f};
45  Color default_fill_color{"blue"};
46  Color default_stroke_color{"white"};
47  int default_stroke_dash = 1;
48  float default_opacity = 1.0f;
49  float default_stroke_width = 1.0f;
50  string default_symbol = "circle";
51  int default_font_size = 12;
52 
53  // symbol attributes
54 
55  string label = string();
56  Color fillColor{"blue"},
57  strokeColor{"white"};
58  float opacity = 1.0f;
59  float strokeWidth = 1.0f;
60  int strokeDash = 1;
61  int fontSize = 12;
62  int textWidth = 100;
63  int textHeight = 50;
64  // symbol location
65  float location[2] = {0.0f, 0.0f};
66 
67  private:
68  int getNewIdentifier() {
69  static int ids = 0;
70  ids++;
71 
72  return ids - 1;
73  }
74 
75  protected:
81  void setShapeType (string s) {
82  shape_type = s;
83  }
89  string getShapeType() const {
90  return shape_type;
91  }
92 
93  public:
94 
95  Symbol() {
96  identifier = getNewIdentifier();
97  }
98 
102  virtual const string getSymbolRepresentation() const = 0;
103 
107  virtual vector<float> getDimensions() const = 0;
108 
112  Symbol(string symb) {
113  identifier = getNewIdentifier();
114  }
115 
123  return identifier;
124  }
125 
131  void setLabel(string lbl) {
132  label = lbl;
133  }
134 
140  string getLabel() const {
141  return label;
142  }
143 
144 
150  void setFillColor(Color c) {
151  fillColor = c;
152  }
158  void setFillColor(string c) {
159  fillColor = Color(c);
160  }
167  return fillColor;
168  }
169 
176  strokeColor = c;
177  }
178 
184  void setStrokeColor(string c) {
185  strokeColor = Color(c);
186  }
187 
194  return strokeColor;
195  }
196 
202  void setStrokeWidth(float strk_width) {
203  if (strokeWidth < 0.0f)
204  throw "Stroke width must be positive or null";
205  else
206  strokeWidth = strk_width;
207  }
208 
214  float getStrokeWidth() {
215  return strokeWidth;
216  }
217 
223  void setOpacity(float op) {
224  if (op < 0.0f || op > 1.0f)
225  throw "Opacity must be between 0 and 1";
226  else
227  opacity = op;
228  }
229 
235  float getOpacity() {
236  return opacity;
237  }
238 
244  void setStrokeDash(int dash) {
245  if (dash < 0 || dash > 10)
246  throw "Dash must be between 0 and 10 (inclusive)";
247  else
248  strokeDash = dash;
249  }
250 
257  return strokeDash;
258  }
259 
266  void setLocation(int x, int y) {
267  setLocation(float(x), float(y));
268  }
269 
276  void setCenter(float x, float y) {
277  setLocation(x, y);
278  }
279 
286  void setLocation(float x, float y) {
287  if ((x > -INFINITY && x < INFINITY) &&
288  (y > -INFINITY && y < INFINITY)) {
289  location[0] = x;
290  location[1] = y;
291  }
292  else
293  throw "Coordinates must be real numbers";
294  }
300  const float *getLocation() const {
301  return location;
302  }
303 
309  string getName() const {
310  return name;
311  }
312 
313  protected:
320  void translatePoint (float *pt, float tx, float ty) {
321  pt[0] += tx;
322  pt[1] += ty;
323  }
324 
331  void scalePoint (float *pt, float sx, float sy) {
332  pt[0] *= sx;
333  pt[1] *= sy;
334  }
335 
342  void rotatePoint (float *pt, float angle) {
343  // compute sin, cos
344  float angle_r = angle * M_PI / 180.;
345  float c = cos(angle_r);
346  float s = sin(angle_r);
347 
348  // rotate the point
349  float tmp[] = { pt[0]*c - pt[1]*s, tmp[1] = pt[0] * s + pt[1] * c};
350 
351  // assign to point
352  pt[0] = tmp[0];
353  pt[1] = tmp[1];
354  }
355 
362  const string getSymbolAttributeRepresentation() const {
363 
364  // first get all the non-geometric attributes
365 
366  string symbol_attr_json = OPEN_CURLY;
367 
368  if (fillColor.getRepresentation() !=
369  default_fill_color.getRepresentation()) {
370  symbol_attr_json += QUOTE + "fill" + QUOTE + COLON +
371  fillColor.getCSSRepresentation() + COMMA;
372  }
373 
374  if (opacity != default_opacity) {
375  symbol_attr_json += QUOTE + "opacity" + QUOTE + COLON +
376  to_string(opacity) + COMMA;
377  }
378 
379  if (strokeColor.getRepresentation() !=
380  default_stroke_color.getRepresentation()) {
381  symbol_attr_json += QUOTE + "stroke" + QUOTE + COLON +
382  strokeColor.getCSSRepresentation() + COMMA;
383  }
384 
385  if (strokeWidth != default_stroke_width) {
386  symbol_attr_json += QUOTE + "stroke-width" + QUOTE + COLON +
387  to_string(strokeWidth) + COMMA;
388  }
389 
390  if (strokeDash != default_stroke_dash) {
391  symbol_attr_json += QUOTE + "stroke-dasharray" + QUOTE + COLON +
392  to_string(strokeDash) + COMMA;
393  }
394 
395  if (location[0] != default_location[0] ||
396  location[1] != default_location[1]) {
397  symbol_attr_json += QUOTE + "location" + QUOTE + COLON +
398  OPEN_CURLY +
399  QUOTE + "x" + QUOTE + COLON +
400  to_string(location[0]) + COMMA +
401  QUOTE + "y" + QUOTE + COLON +
402  to_string(location[1]) +
403  CLOSE_CURLY + COMMA;
404  }
405 
406  return symbol_attr_json;
407  }
408  };
409  }
410 } // namespace bridges
411 
412 #endif
void setLabel(string lbl)
Definition: Symbol.h:131
void setStrokeColor(Color c)
Definition: Symbol.h:175
Color getStrokeColor()
Definition: Symbol.h:193
const string COLON
Definition: DataStructure.h:51
void scalePoint(float *pt, float sx, float sy)
Scale a 2D point.
Definition: Symbol.h:331
STL namespace.
const float * getLocation() const
Definition: Symbol.h:300
#define M_PI
Definition: OSMData.h:13
void setLocation(int x, int y)
Definition: Symbol.h:266
void setOpacity(float op)
Definition: Symbol.h:223
void rotatePoint(float *pt, float angle)
Rotate a 2D point (about Z)
Definition: Symbol.h:342
void setStrokeColor(string c)
Definition: Symbol.h:184
Symbol()
Definition: Symbol.h:95
const string CLOSE_CURLY
Definition: DataStructure.h:53
void setFillColor(string c)
Definition: Symbol.h:158
string getLabel() const
Definition: Symbol.h:140
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:32
void translatePoint(float *pt, float tx, float ty)
Translate a 2D point.
Definition: Symbol.h:320
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
void setLocation(float x, float y)
Definition: Symbol.h:286
const string OPEN_CURLY
Definition: DataStructure.h:52
const string getSymbolAttributeRepresentation() const
Definition: Symbol.h:362
void setCenter(float x, float y)
Definition: Symbol.h:276
int getStrokeDash()
Definition: Symbol.h:256
Symbol(string symb)
Definition: Symbol.h:112
string getName() const
Definition: Symbol.h:309
float getOpacity()
Definition: Symbol.h:235
void setShapeType(string s)
Set the shape type.
Definition: Symbol.h:81
void setFillColor(Color c)
Definition: Symbol.h:150
const string COMMA
Definition: DataStructure.h:50
float getStrokeWidth()
Definition: Symbol.h:214
Color getFillColor()
Definition: Symbol.h:166
const string QUOTE
Definition: DataStructure.h:49
int getIdentifier()
Definition: Symbol.h:122
string getShapeType() const
Definition: Symbol.h:89
void setStrokeDash(int dash)
Definition: Symbol.h:244
void setStrokeWidth(float strk_width)
Definition: Symbol.h:202