Bridges-C++  3.1.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  protected:
26  // points of the polyline
27  vector<float> points;
28 
29  public:
30  // constructors
31  Polyline () {
32  points.clear();
33  setShapeType("polyline");
34  }
35 
40  Polyline (vector<float> pts) {
41  setPolyline(pts);
42  }
43 
48  string getDataStructType() {
49  return "polyline";
50  }
51 
57  string getName() const {
58  return "polyline";
59  }
60 
66  void addPoint(float x, float y) {
67  if ((x > -INFINITY) && (x < INFINITY) &&
68  (y > -INFINITY) && (y < INFINITY)) {
69  points.push_back(x);
70  points.push_back(y);
71  }
72  else {
73  throw "Invalid points for polyline!";
74  }
75  }
81  vector<float> getPoints() {
82  return points;
83  }
84 
85  void setPolyline (vector<float> pts) {
86  points = pts;
87  setShapeType("polyline");
88  }
89 
95  void translate(float tx, float ty) {
96  // translate the points
97  for (int k = 0; k < points.size(); k += 2) {
98  points[k] += tx;
99  points[k + 1] += ty;
100  }
101  }
102 
108  void rotate(float angle) {
109  // get center of polyline
110  float center[2];
111  getCenter(center);
112  // translate the center to the origin
113  float transl[] = {-center[0], -center[1]};
114  translate (transl[0], transl[1]);
115  // rotate the points
116  for (int k = 0; k < points.size(); k += 2) {
117  float tmp[] = { points[k], points[k + 1] };
118  rotatePoint (tmp, angle);
119  points[k] = tmp[0];
120  points[k + 1] = tmp[1];
121  }
122  // translate back
123  transl[0] = center[0];
124  transl[1] = center[1];
125  translate (transl[0], transl[1]);
126  }
127 
133  void scale(float sx, float sy) {
134  // get center of polyline
135  float center[2];
136  getCenter(center);
137  // translate the center to the origin
138  float transl[] = {-center[0], -center[1]};
139  translate (transl[0], transl[1]);
140  // scale the points
141  for (int k = 0; k < points.size(); k += 2) {
142  points[k] *= sx;
143  points[k + 1] *= sy;
144  }
145  // translate back
146  transl[0] = center[0];
147  transl[1] = center[1];
148  translate(transl[0], transl[1]);
149  }
150 
154  void getCenter(float *center) {
155  float bbox[4];
156  bbox[0] = bbox[1] = INFINITY;
157  bbox[2] = bbox[3] = -INFINITY;
158  for (int k = 0; k < points.size(); k += 2) {
159  if (points[k] < bbox[0])
160  bbox[0] = points[k];
161  if (points[k] > bbox[2])
162  bbox[2] = points[k];
163  if (points[k + 1] < bbox[1])
164  bbox[1] = points[k + 1];
165  if (points[k + 1] > bbox[3])
166  bbox[3] = points[k + 1];
167  }
168  center[0] = bbox[0] + (bbox[2] - bbox[0]) / 2.;
169  center[1] = bbox[1] + (bbox[3] - bbox[1]) / 2.;
170  }
171 
178  vector<float> getDimensions() const {
179 
180  vector<float> dims(4);
181  dims[0] = dims[2] = INFINITY;
182  dims[1] = dims[3] = -INFINITY;
183  float x, y;
184  for (std::size_t i = 0, size = points.size();
185  i < size; i += 2) {
186  x = points.at(i);
187  y = points.at(i + 1);
188  if (x < dims[0])
189  dims[0] = x;
190  if (x > dims[1])
191  dims[1] = x;
192  if (y < dims[2])
193  dims[2] = y;
194  if (y > dims[3])
195  dims[3] = y;
196  }
197  return dims;
198  }
199 
205  const string getSymbolRepresentation() const {
206 
207  string shape_json = getSymbolAttributeRepresentation();
208  string shape = getShapeType();
209 
210  shape_json +=
211  QUOTE + "name" + QUOTE + COLON + QUOTE + getLabel() + QUOTE + COMMA +
212  QUOTE + "shape" + QUOTE + COLON + QUOTE + shape + QUOTE + COMMA;
213 
214  // add point list to polygons
215  shape_json += QUOTE + "points" + QUOTE + COLON + OPEN_BOX;
216  vector<float>::iterator it;
217  for (int k = 0; k < points.size(); k++) {
218  shape_json += to_string(points[k]) + COMMA;
219  }
220 
221  // remove last comma
222  if (points.size())
223  shape_json.erase(shape_json.size() - 1);
224  shape_json += CLOSE_BOX + CLOSE_CURLY;
225 
226  return shape_json;
227  }
228  };
229  }
230 } // namespace bridges
231 
232 #endif
void scale(float sx, float sy)
Scale the polyline about its center.
Definition: Polyline.h:133
const string getSymbolRepresentation() const
Definition: Polyline.h:205
Polyline(vector< float > pts)
Definition: Polyline.h:40
vector< float > getDimensions() const
Definition: Polyline.h:178
string getDataStructType()
Definition: Polyline.h:48
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 l *
Definition: Polyline.h:108
This is an abstract class for deriving a number of Symbol shape objects, for use in a SymbolCollectio...
Definition: Symbol.h:32
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)
Definition: Polyline.h:154
const string CLOSE_BOX
Definition: DataStructure.h:55
void setPolyline(vector< float > pts)
Definition: Polyline.h:85
void addPoint(float x, float y)
Definition: Polyline.h:66
vector< float > getPoints()
Definition: Polyline.h:81
Polyline()
Definition: Polyline.h:31
string getName() const
Definition: Polyline.h:57
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:95