Bridges-C++  3.2.0
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  protected:
26  // points of the polyline
27  vector<float> points;
28 
29  public:
33  Polyline () {
34  points.clear();
35  setShapeType("polyline");
36  }
37 
42  Polyline (vector<float> pts) {
43  setPolyline(pts);
44  }
45 
50  string getDataStructType() {
51  return "polyline";
52  }
53 
59  string getName() const {
60  return "polyline";
61  }
62 
68  void addPoint(float x, float y) {
69  if ((x > -INFINITY) && (x < INFINITY) &&
70  (y > -INFINITY) && (y < INFINITY)) {
71  points.push_back(x);
72  points.push_back(y);
73  }
74  else {
75  throw "Invalid points for polyline!";
76  }
77  }
83  vector<float> getPoints() {
84  return points;
85  }
86 
91  void setPolyline (vector<float> pts) {
92  points = pts;
93  setShapeType("polyline");
94  }
95 
101  void translate(float tx, float ty) {
102  // translate the points
103  for (int k = 0; k < points.size(); k += 2) {
104  points[k] += tx;
105  points[k + 1] += ty;
106  }
107  }
108 
114  void rotate(float angle) {
115  // get center of polyline
116  float center[2];
117  getCenter(center);
118  // translate the center to the origin
119  float transl[] = {-center[0], -center[1]};
120  translate (transl[0], transl[1]);
121  // rotate the points
122  for (int k = 0; k < points.size(); k += 2) {
123  float tmp[] = { points[k], points[k + 1] };
124  rotatePoint (tmp, angle);
125  points[k] = tmp[0];
126  points[k + 1] = tmp[1];
127  }
128  // translate back
129  transl[0] = center[0];
130  transl[1] = center[1];
131  translate (transl[0], transl[1]);
132  }
133 
139  void scale(float sx, float sy) {
140  // get center of polyline
141  float center[2];
142  getCenter(center);
143  // translate the center to the origin
144  float transl[] = {-center[0], -center[1]};
145  translate (transl[0], transl[1]);
146  // scale the points
147  for (int k = 0; k < points.size(); k += 2) {
148  points[k] *= sx;
149  points[k + 1] *= sy;
150  }
151  // translate back
152  transl[0] = center[0];
153  transl[1] = center[1];
154  translate(transl[0], transl[1]);
155  }
156 
161  void getCenter(float *center) {
162  float bbox[4];
163  bbox[0] = bbox[1] = INFINITY;
164  bbox[2] = bbox[3] = -INFINITY;
165  for (int k = 0; k < points.size(); k += 2) {
166  if (points[k] < bbox[0])
167  bbox[0] = points[k];
168  if (points[k] > bbox[2])
169  bbox[2] = points[k];
170  if (points[k + 1] < bbox[1])
171  bbox[1] = points[k + 1];
172  if (points[k + 1] > bbox[3])
173  bbox[3] = points[k + 1];
174  }
175  center[0] = bbox[0] + (bbox[2] - bbox[0]) / 2.;
176  center[1] = bbox[1] + (bbox[3] - bbox[1]) / 2.;
177  }
178 
185  vector<float> getDimensions() const {
186 
187  vector<float> dims(4);
188  dims[0] = dims[2] = INFINITY;
189  dims[1] = dims[3] = -INFINITY;
190  float x, y;
191  for (std::size_t i = 0, size = points.size();
192  i < size; i += 2) {
193  x = points.at(i);
194  y = points.at(i + 1);
195  if (x < dims[0])
196  dims[0] = x;
197  if (x > dims[1])
198  dims[1] = x;
199  if (y < dims[2])
200  dims[2] = y;
201  if (y > dims[3])
202  dims[3] = y;
203  }
204  return dims;
205  }
206 
212  const string getSymbolRepresentation() const {
213 
214  string shape_json = getSymbolAttributeRepresentation();
215  string shape = getShapeType();
216 
217  shape_json +=
218  QUOTE + "name" + QUOTE + COLON + QUOTE + getLabel() + QUOTE + COMMA +
219  QUOTE + "shape" + QUOTE + COLON + QUOTE + shape + QUOTE + COMMA;
220 
221  // add point list to polygons
222  shape_json += QUOTE + "points" + QUOTE + COLON + OPEN_BOX;
223  vector<float>::iterator it;
224  for (int k = 0; k < points.size(); k++) {
225  shape_json += to_string(points[k]) + COMMA;
226  }
227 
228  // remove last comma
229  if (points.size())
230  shape_json.erase(shape_json.size() - 1);
231  shape_json += CLOSE_BOX + CLOSE_CURLY;
232 
233  return shape_json;
234  }
235  };
236  }
237 } // namespace bridges
238 
239 #endif
void scale(float sx, float sy)
Scale the polyline about its center.
Definition: Polyline.h:139
const string getSymbolRepresentation() const
This method returns the JSON representation of the shape.
Definition: Polyline.h:212
Polyline(vector< float > pts)
Construct a polyline from sequence of points.
Definition: Polyline.h:42
vector< float > getDimensions() const
This method returns the dimensions of the shape: min and max values in X and Y.
Definition: Polyline.h:185
string getDataStructType()
Get the name of the data type.
Definition: Polyline.h:50
const string COLON
Definition: DataStructure.h:51
STL namespace.
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
void rotate(float angle)
rotate the polyline about its center
Definition: Polyline.h:114
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:33
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:161
const string CLOSE_BOX
Definition: DataStructure.h:55
void setPolyline(vector< float > pts)
Construct a polyline from sequence of points.
Definition: Polyline.h:91
void addPoint(float x, float y)
This method adds a point to the polyline.
Definition: Polyline.h:68
vector< float > getPoints()
This method returns the point list of the polyline.
Definition: Polyline.h:83
Polyline()
default constructor
Definition: Polyline.h:33
string getName() const
This method gets the name of the shape.
Definition: Polyline.h:59
vector< float > points
Definition: Polyline.h:27
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49
void translate(float tx, float ty)
Translate the polyline.
Definition: Polyline.h:101