Bridges-C++  3.2.0
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 
33  class Symbol {
34 
35  private:
36 
37  int identifier;
38  string name = string();
39 
40  string shape_type = "circle"; // rect, circle, polygon, label
41 
42  // specify default attributes
43  // defaults are not sent through JSON
44 
45  float default_location[2] = {0.0f, 0.0f};
46  Color default_fill_color{"blue"};
47  Color default_stroke_color{"white"};
48  int default_stroke_dash = 1;
49  float default_opacity = 1.0f;
50  float default_stroke_width = 1.0f;
51  string default_symbol = "circle";
52  int default_font_size = 12;
53 
54  // symbol attributes
55 
56  string label = string();
57  Color fillColor{"blue"},
58  strokeColor{"white"};
59  float opacity = 1.0f;
60  float strokeWidth = 1.0f;
61  int strokeDash = 1;
62  int fontSize = 12;
63  int textWidth = 100;
64  int textHeight = 50;
65  // symbol location
66  float location[2] = {0.0f, 0.0f};
67 
68  private:
69  int getNewIdentifier() {
70  static int ids = 0;
71  ids++;
72 
73  return ids - 1;
74  }
75 
76  protected:
82  void setShapeType (string s) {
83  shape_type = s;
84  }
90  string getShapeType() const {
91  return shape_type;
92  }
93 
94  public:
95 
99  Symbol() {
100  identifier = getNewIdentifier();
101  }
102 
106  virtual const string getSymbolRepresentation() const = 0;
107 
112  virtual vector<float> getDimensions() const = 0;
113 
119  Symbol(string symb) {
120  identifier = getNewIdentifier();
121  }
122 
132  return identifier;
133  }
134 
140  void setLabel(string lbl) {
141  label = lbl;
142  }
143 
149  string getLabel() const {
150  return label;
151  }
152 
153 
159  void setFillColor(Color c) {
160  fillColor = c;
161  }
167  void setFillColor(string c) {
168  fillColor = Color(c);
169  }
176  return fillColor;
177  }
178 
185  strokeColor = c;
186  }
187 
193  void setStrokeColor(string c) {
194  strokeColor = Color(c);
195  }
196 
203  return strokeColor;
204  }
205 
211  void setStrokeWidth(float strk_width) {
212  if (strokeWidth < 0.0f)
213  throw "Stroke width must be positive or null";
214  else
215  strokeWidth = strk_width;
216  }
217 
223  float getStrokeWidth() {
224  return strokeWidth;
225  }
226 
232  void setOpacity(float op) {
233  if (op < 0.0f || op > 1.0f)
234  throw "Opacity must be between 0 and 1";
235  else
236  opacity = op;
237  }
238 
244  float getOpacity() {
245  return opacity;
246  }
247 
253  void setStrokeDash(int dash) {
254  if (dash < 0 || dash > 10)
255  throw "Dash must be between 0 and 10 (inclusive)";
256  else
257  strokeDash = dash;
258  }
259 
266  return strokeDash;
267  }
268 
275  void setLocation(int x, int y) {
276  setLocation(float(x), float(y));
277  }
278 
285  void setLocation(double x, double y) {
286  setLocation(float(x), float(y));
287  }
288 
289 
296  void setCenter(float x, float y) {
297  setLocation(x, y);
298  }
299 
306  void setLocation(float x, float y) {
307  if ((x > -INFINITY && x < INFINITY) &&
308  (y > -INFINITY && y < INFINITY)) {
309  location[0] = x;
310  location[1] = y;
311  }
312  else
313  throw "Coordinates must be real numbers";
314  }
320  const float *getLocation() const {
321  return location;
322  }
323 
329  string getName() const {
330  return name;
331  }
332 
333  protected:
340  void translatePoint (float *pt, float tx, float ty) {
341  pt[0] += tx;
342  pt[1] += ty;
343  }
344 
351  void scalePoint (float *pt, float sx, float sy) {
352  pt[0] *= sx;
353  pt[1] *= sy;
354  }
355 
362  void rotatePoint (float *pt, float angle) {
363  // compute sin, cos
364  float angle_r = angle * M_PI / 180.;
365  float c = cos(angle_r);
366  float s = sin(angle_r);
367 
368  // rotate the point
369  float tmp[] = { pt[0]*c - pt[1]*s, tmp[1] = pt[0] * s + pt[1] * c};
370 
371  // assign to point
372  pt[0] = tmp[0];
373  pt[1] = tmp[1];
374  }
375 
384  const string getSymbolAttributeRepresentation() const {
385 
386  // first get all the non-geometric attributes
387 
388  string symbol_attr_json = OPEN_CURLY;
389 
390  if (fillColor.getRepresentation() !=
391  default_fill_color.getRepresentation()) {
392  symbol_attr_json += QUOTE + "fill" + QUOTE + COLON +
393  fillColor.getCSSRepresentation() + COMMA;
394  }
395 
396  if (opacity != default_opacity) {
397  symbol_attr_json += QUOTE + "opacity" + QUOTE + COLON +
398  to_string(opacity) + COMMA;
399  }
400 
401  if (strokeColor.getRepresentation() !=
402  default_stroke_color.getRepresentation()) {
403  symbol_attr_json += QUOTE + "stroke" + QUOTE + COLON +
404  strokeColor.getCSSRepresentation() + COMMA;
405  }
406 
407  if (strokeWidth != default_stroke_width) {
408  symbol_attr_json += QUOTE + "stroke-width" + QUOTE + COLON +
409  to_string(strokeWidth) + COMMA;
410  }
411 
412  if (strokeDash != default_stroke_dash) {
413  symbol_attr_json += QUOTE + "stroke-dasharray" + QUOTE + COLON +
414  to_string(strokeDash) + COMMA;
415  }
416 
417  if (location[0] != default_location[0] ||
418  location[1] != default_location[1]) {
419  symbol_attr_json += QUOTE + "location" + QUOTE + COLON +
420  OPEN_CURLY +
421  QUOTE + "x" + QUOTE + COLON +
422  to_string(location[0]) + COMMA +
423  QUOTE + "y" + QUOTE + COLON +
424  to_string(location[1]) +
425  CLOSE_CURLY + COMMA;
426  }
427 
428  return symbol_attr_json;
429  }
430  };
431  }
432 } // namespace bridges
433 
434 #endif
void setLocation(double x, double y)
This method sets the symbol location.
Definition: Symbol.h:285
void setLabel(string lbl)
Set the symbol label.
Definition: Symbol.h:140
void setStrokeColor(Color c)
Definition: Symbol.h:184
Color getStrokeColor()
This method gets stroke color.
Definition: Symbol.h:202
const string COLON
Definition: DataStructure.h:51
void scalePoint(float *pt, float sx, float sy)
Scale a 2D point.
Definition: Symbol.h:351
STL namespace.
const float * getLocation() const
This method gets the symbol location.
Definition: Symbol.h:320
#define M_PI
Definition: OSMData.h:13
void setLocation(int x, int y)
This method sets the symbol location.
Definition: Symbol.h:275
void setOpacity(float op)
This method sets the symbol opacity.
Definition: Symbol.h:232
void rotatePoint(float *pt, float angle)
Rotate a 2D point (about Z)
Definition: Symbol.h:362
void setStrokeColor(string c)
This method sets the symbol stroke color.
Definition: Symbol.h:193
Symbol()
default constructor
Definition: Symbol.h:99
const string CLOSE_CURLY
Definition: DataStructure.h:53
void setFillColor(string c)
This method sets the symbol fill color.
Definition: Symbol.h:167
string getLabel() const
Get the symbol label.
Definition: Symbol.h:149
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:33
void translatePoint(float *pt, float tx, float ty)
Translate a 2D point.
Definition: Symbol.h:340
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)
This method sets the ssymbol location.
Definition: Symbol.h:306
const string OPEN_CURLY
Definition: DataStructure.h:52
const string getSymbolAttributeRepresentation() const
Get the JSON of the symbol representation.
Definition: Symbol.h:384
void setCenter(float x, float y)
This method sets the symbol location.
Definition: Symbol.h:296
int getStrokeDash()
This method gets stroke dash level.
Definition: Symbol.h:265
Symbol(string symb)
Create a symbol of type "symb".
Definition: Symbol.h:119
string getName() const
This method gets the name of the symbol.
Definition: Symbol.h:329
float getOpacity()
This method gets symbol opacity.
Definition: Symbol.h:244
void setShapeType(string s)
Set the shape type.
Definition: Symbol.h:82
void setFillColor(Color c)
Set the symbol fill color.
Definition: Symbol.h:159
const string COMMA
Definition: DataStructure.h:50
float getStrokeWidth()
This method gets stroke width.
Definition: Symbol.h:223
Color getFillColor()
This method gets fill color.
Definition: Symbol.h:175
const string QUOTE
Definition: DataStructure.h:49
int getIdentifier()
return the symbol identifier.
Definition: Symbol.h:131
string getShapeType() const
Definition: Symbol.h:90
void setStrokeDash(int dash)
This method sets the stroke dash level.
Definition: Symbol.h:253
void setStrokeWidth(float strk_width)
This method sets the symbol stroke width.
Definition: Symbol.h:211