Bridges-C++  3.4.1
Bridges(C++API)
Polyline.h
Go to the documentation of this file.
1 #include "DataStructure.h"
2 #include "Symbol.h"
3 #include <vector>
4 
5 using namespace std;
6 
7 
8 #ifndef POLYLINE_H
9 
10 #define POLYLINE_H
11 
12 namespace bridges {
13  namespace datastructure {
24  class Polyline : public Symbol {
25 
26  protected:
27  // points of the polyline
28  vector<float> points;
29 
30  public:
34  Polyline () {
35  points.clear();
36  }
37 
42  Polyline (vector<float> pts) {
43  setPolyline(pts);
44  }
45 
51  virtual string getShapeType() const override {
52  return "polyline";
53  }
54 
60  void addPoint(float x, float y) {
61  if ((x > -INFINITY) && (x < INFINITY) &&
62  (y > -INFINITY) && (y < INFINITY)) {
63  points.push_back(x);
64  points.push_back(y);
65  }
66  else {
67  throw "Invalid points for polyline!";
68  }
69  }
75  vector<float> getPoints() {
76  return points;
77  }
78 
83  void setPolyline (vector<float> pts) {
84  points = pts;
85  }
86 
91  void getCenter(float *center) {
92  float bbox[4];
93  bbox[0] = bbox[1] = INFINITY;
94  bbox[2] = bbox[3] = -INFINITY;
95  for (int k = 0; k < points.size(); k += 2) {
96  if (points[k] < bbox[0])
97  bbox[0] = points[k];
98  if (points[k] > bbox[2])
99  bbox[2] = points[k];
100  if (points[k + 1] < bbox[1])
101  bbox[1] = points[k + 1];
102  if (points[k + 1] > bbox[3])
103  bbox[3] = points[k + 1];
104  }
105  center[0] = bbox[0] + (bbox[2] - bbox[0]) / 2.;
106  center[1] = bbox[1] + (bbox[3] - bbox[1]) / 2.;
107  }
108 
109 
115  const string getSymbolRepresentation() const override {
116 
117  string shape_json = getSymbolAttributeRepresentation();
118  string shape = getShapeType();
119 
120  // add point list to polygons
121  shape_json += QUOTE + "points" + QUOTE + COLON + OPEN_BOX;
122  vector<float>::iterator it;
123  for (int k = 0; k < points.size(); k++) {
124  shape_json += to_string(points[k]) + COMMA;
125  }
126 
127  // remove last comma
128  if (points.size())
129  shape_json.erase(shape_json.size() - 1);
130  shape_json += CLOSE_BOX + CLOSE_CURLY;
131 
132  return shape_json;
133  }
134  };
135  }
136 } // namespace bridges
137 
138 #endif
Polyline(vector< float > pts)
Construct a polyline from sequence of points.
Definition: Polyline.h:42
const string COLON
Definition: DataStructure.h:51
const string OPEN_BOX
Definition: DataStructure.h:54
const string CLOSE_CURLY
Definition: DataStructure.h:53
This class defines a polyline and is part of the symbol collection. A polyline has a set of vertices ...
Definition: Polyline.h:24
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:32
virtual string getShapeType() const override
This method gets the type of the shape.
Definition: Polyline.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 getCenter(float *center)
Get center of polyline - use its bounding box.
Definition: Polyline.h:91
const string CLOSE_BOX
Definition: DataStructure.h:55
void setPolyline(vector< float > pts)
Construct a polyline from sequence of points.
Definition: Polyline.h:83
const string getSymbolRepresentation() const override
This method returns the JSON representation of the shape.
Definition: Polyline.h:115
void addPoint(float x, float y)
This method adds a point to the polyline.
Definition: Polyline.h:60
vector< float > getPoints()
This method returns the point list of the polyline.
Definition: Polyline.h:75
Polyline()
default constructor
Definition: Polyline.h:34
vector< float > points
Definition: Polyline.h:28
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49